From 972631be7105a3120a405dc0d25c2dbb7179cd13 Mon Sep 17 00:00:00 2001 From: Zeev Suraski Date: Thu, 13 Jan 2000 17:37:25 +0000 Subject: [PATCH] - Added flush() support to SAPI - Got rid of the old flush() implemenetation in favour of the new one - Added implicit_flush() support to the output buffering layer. @- Added implicit_flush() to control whether flush() should be called @ implicitly after any output (Zeev) --- ext/session/php_session.h | 2 +- ext/standard/basic_functions.c | 22 +---- ext/standard/head.c | 159 --------------------------------- ext/standard/output.c | 48 +++++++--- ext/standard/php_output.h | 4 + main/SAPI.c | 12 +++ main/SAPI.h | 4 + main/output.c | 48 +++++++--- main/php_output.h | 4 + sapi/aolserver/aolserver.c | 1 + sapi/apache/mod_php4.c | 17 +++- sapi/cgi/cgi_main.c | 11 ++- sapi/isapi/php4isapi.c | 5 +- sapi/phttpd/phttpd.c | 1 + sapi/roxen/roxen.c | 7 +- sapi/servlet/servlet.c | 5 +- sapi/thttpd/thttpd.c | 2 + 17 files changed, 139 insertions(+), 213 deletions(-) diff --git a/ext/session/php_session.h b/ext/session/php_session.h index 005b451295..5fa3bdd6a6 100644 --- a/ext/session/php_session.h +++ b/ext/session/php_session.h @@ -152,7 +152,7 @@ typedef struct ps_serializer_struct { #ifdef TRANS_SID void session_adapt_uris(const char *, uint, char **, uint *); #else -#define session_adapt_uris(a,b,c,d) do { } while(0) +#define session_adapt_uris(a,b,c,d) #endif void _php_session_auto_start(void *data); diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index eee7221c71..a8b1d4dc42 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -595,28 +595,10 @@ PHP_FUNCTION(strval) convert_to_string(return_value); } + PHP_FUNCTION(flush) { -#if APACHE - SLS_FETCH(); -#endif - -#if APACHE -# if MODULE_MAGIC_NUMBER > 19970110 - rflush(((request_rec *) SG(server_context))); -# else - bflush(((request_rec *) SG(server_context))->connection->client); -# endif -#endif -#if FHTTPD - /*FIXME -- what does it flush really? the whole response?*/ -#endif -#if CGI_BINARY - fflush(stdout); -#endif -#if USE_SAPI - sapi_rqst->flush(sapi_rqst->scid); -#endif + sapi_flush(); } diff --git a/ext/standard/head.c b/ext/standard/head.c index 2fa708eda5..a6b973cec6 100644 --- a/ext/standard/head.c +++ b/ext/standard/head.c @@ -203,163 +203,6 @@ PHP_FUNCTION(Header) } - - - -#if 0 -/* - * php_header() flushes the header info built up using calls to - * the Header() function. If type is 1, a redirect to str is done. - * Otherwise type should be 0 and str NULL. - * - * The function returns non-zero if output is allowed after the - * call, and zero otherwise. Any call to php_header() must check - * the return status and if false, no output must be sent. This - * is in order to correctly handle HEAD requests. - */ -PHPAPI int php_header(void) -{ -#if APACHE - CookieList *cookie; - int len = 0; - time_t t; - char *dt, *cookievalue = NULL; -#endif -#if APACHE || defined(USE_SAPI) || FHTTPD - char *tempstr; -#endif - PLS_FETCH(); - SLS_FETCH(); - - if (PG(header_is_being_sent)) { - return 0; - } else { - PG(header_is_being_sent) = 1; - } - -#if APACHE - if (!((request_rec *) SG(server_context))) { /* we're not in a request, allow output */ - PG(header_is_being_sent) = 0; - return 1; - } - if ((php_print_header && !php_header_printed) || (php_print_header && php_header_printed == 2)) { - cookie = php_pop_cookie_list(); - while (cookie) { - if (cookie->name) - len += strlen(cookie->name); - if (cookie->value) { - cookievalue = php_url_encode(cookie->value, strlen (cookie->value)); - len += strlen(cookievalue); - } - if (cookie->path) - len += strlen(cookie->path); - if (cookie->domain) - len += strlen(cookie->domain); - tempstr = emalloc(len + 100); - if (!cookie->value || (cookie->value && !*cookie->value)) { - /* - * MSIE doesn't delete a cookie when you set it to a null value - * so in order to force cookies to be deleted, even on MSIE, we - * pick an expiry date 1 year and 1 second in the past - */ - sprintf(tempstr, "%s=deleted", cookie->name); - t = time(NULL) - 31536001; - strcat(tempstr, "; expires="); - dt = php_std_date(t); - strcat(tempstr, dt); - efree(dt); - } else { - /* FIXME: XXX: this is not binary data safe */ - sprintf(tempstr, "%s=%s", cookie->name, cookie->value ? cookievalue : ""); - if (cookie->name) efree(cookie->name); - if (cookie->value) efree(cookie->value); - if (cookievalue) efree(cookievalue); - cookie->name=NULL; - cookie->value=NULL; - cookievalue=NULL; - if (cookie->expires > 0) { - strcat(tempstr, "; expires="); - dt = php_std_date(cookie->expires); - strcat(tempstr, dt); - efree(dt); - } - } - if (cookie->path && strlen(cookie->path)) { - strcat(tempstr, "; path="); - strcat(tempstr, cookie->path); - efree(cookie->path); - cookie->path=NULL; - } - if (cookie->domain && strlen(cookie->domain)) { - strcat(tempstr, "; domain="); - strcat(tempstr, cookie->domain); - efree(cookie->domain); - cookie->domain=NULL; - } - if (cookie->secure) { - strcat(tempstr, "; secure"); - } - table_add(((request_rec *) SG(server_context))->headers_out, "Set-Cookie", tempstr); - if (cookie->domain) efree(cookie->domain); - if (cookie->path) efree(cookie->path); - if (cookie->name) efree(cookie->name); - if (cookie->value) efree(cookie->value); - if (cookievalue) efree(cookievalue); - efree(cookie); - cookie = php_pop_cookie_list(); - efree(tempstr); - } - php_header_printed = 1; - header_called = 1; - send_http_header(((request_rec *) SG(server_context))); - if (((request_rec *) SG(server_context))->header_only) { - set_header_request(1); - PG(header_is_being_sent) = 0; - return(0); - } - } -#else - if (php_print_header && !php_header_printed) { - if (!cont_type) { -#if USE_SAPI - sapi_rqst->header(sapi_rqst->scid,"Content-type: text/html\015\012\015\012"); -#else - PUTS_H("Content-type: text/html\015\012\015\012"); -#endif - } else { -#if 0 /*WIN32|WINNT / *M$ does us again*/ - if (!strcmp(cont_type,"text/html")){ -#endif -#if USE_SAPI - tempstr=emalloc(strlen(cont_type)+18); - sprintf(tempstr,"Content-type: %s\015\012\015\012",cont_type); - sapi_rqst->header(sapi_rqst->scid,tempstr); - efree(tempstr); -#else - PUTS_H("Content-type:"); - PUTS_H(cont_type); - PUTS_H("\015\012\015\012"); -#endif - efree(cont_type); -#if 0 /*WIN32|WINNT / *M$ does us again*/ - } else { - PUTS_H("\015\012"); - }/*end excluding output of text/html*/ -#endif - } -#if USE_SAPI - sapi_rqst->flush(sapi_rqst->scid); -#else - fflush(stdout); -#endif - php_header_printed = 1; - header_called = 1; - } -#endif - PG(header_is_being_sent) = 0; - return(1); -} -#else PHPAPI int php_header() { SLS_FETCH(); @@ -370,8 +213,6 @@ PHPAPI int php_header() return 1; /* allow output */ } } -#endif - void php_push_cookie_list(char *name, char *value, time_t expires, char *path, char *domain, int secure) diff --git a/ext/standard/output.c b/ext/standard/output.c index ab5ee333e7..5ba4c92831 100644 --- a/ext/standard/output.c +++ b/ext/standard/output.c @@ -37,12 +37,6 @@ static void php_ob_prepend(const char *text, uint text_length); #endif static inline void php_ob_send(void); -void php_start_ob_buffering(void); -void php_end_ob_buffering(int send_buffer); -int php_ob_get_buffer(pval *p); - -/* HEAD support */ -void set_header_request(int value); typedef struct { int (*php_body_write)(const char *str, uint str_length); /* string output */ @@ -51,6 +45,7 @@ typedef struct { uint ob_size; uint ob_block_size; uint ob_text_length; + unsigned char implicit_flush; } php_output_globals; #ifdef ZTS @@ -82,13 +77,14 @@ static void php_output_init_globals(OLS_D) OG(ob_size) = 0; OG(ob_block_size) = 0; OG(ob_text_length) = 0; + OG(implicit_flush) = 0; } PHP_GINIT_FUNCTION(output) { #ifdef ZTS - output_globals_id = ts_allocate_id(sizeof(php_output_globals), NULL, NULL); + output_globals_id = ts_allocate_id(sizeof(php_output_globals), (ts_allocate_ctor) php_output_init_globals, NULL); #else php_output_init_globals(OLS_C); #endif @@ -186,6 +182,18 @@ PHPAPI void php_end_ob_buffering(int send_buffer) } +PHPAPI void php_start_implicit_flush() +{ + php_end_ob_buffering(1); /* Switch out of output buffering if we're in it */ + +} + + +PHPAPI void php_end_implicit_flush() +{ +} + + /* * Output buffering - implementation */ @@ -321,6 +329,7 @@ static int php_ub_body_write_no_header(const char *str, uint str_length) free(newstr); } + return result; } @@ -347,10 +356,6 @@ static int php_ub_body_write(const char *str, uint str_length) * HEAD support */ -void set_header_request(int value) -{ - /* deprecated */ -} PHP_FUNCTION(ob_start) { @@ -378,6 +383,27 @@ PHP_FUNCTION(ob_get_contents) } +PHP_FUNCTION(ob_implicit_flush) +{ + zval **zv_flag; + int flag; + + switch(ZEND_NUM_ARGS()) { + case 0: + flag = 1; + break; + case 1: + if (zend_get_parameters_ex(1, &zv_flag)==FAILURE) { + RETURN_FALSE; + } + convert_to_long_ex(zv_flag); + flag = (*zv_flag)->value.lval; + break; + } +} + + + /* * Local variables: * tab-width: 4 diff --git a/ext/standard/php_output.h b/ext/standard/php_output.h index 63e692e2e2..17c8424429 100644 --- a/ext/standard/php_output.h +++ b/ext/standard/php_output.h @@ -27,6 +27,10 @@ PHPAPI int php_body_write(const char *str, uint str_length); PHPAPI int php_header_write(const char *str, uint str_length); PHPAPI void php_start_ob_buffering(void); PHPAPI void php_end_ob_buffering(int send_buffer); +PHPAPI int php_ob_get_buffer(pval *p); +PHPAPI void php_start_implicit_flush(); +PHPAPI void php_end_implicit_flush(); + extern zend_module_entry output_module_entry; #define phpext_output_ptr &output_module_entry diff --git a/main/SAPI.c b/main/SAPI.c index f5782321e2..30d0db76b1 100644 --- a/main/SAPI.c +++ b/main/SAPI.c @@ -357,3 +357,15 @@ SAPI_API int sapi_register_default_post_reader(void (*default_post_reader)(char return SUCCESS; } + +SAPI_API int sapi_flush() +{ + if (sapi_module.flush) { + SLS_FETCH(); + + sapi_module.flush(SG(server_context)); + return SUCCESS; + } else { + return FAILURE; + } +} diff --git a/main/SAPI.h b/main/SAPI.h index 67df3af30e..d5ebcdc62f 100644 --- a/main/SAPI.h +++ b/main/SAPI.h @@ -126,6 +126,9 @@ SAPI_API int sapi_register_post_reader(sapi_post_content_type_reader *post_conte SAPI_API void sapi_unregister_post_reader(sapi_post_content_type_reader *post_content_type_reader); SAPI_API int sapi_register_default_post_reader(void (*default_post_reader)(char *content_type_dup SLS_DC)); +SAPI_API int sapi_flush(); + + struct _sapi_module_struct { char *name; @@ -133,6 +136,7 @@ struct _sapi_module_struct { int (*shutdown)(struct _sapi_module_struct *sapi_module); int (*ub_write)(const char *str, unsigned int str_length); + void (*flush)(void *server_context); void (*sapi_error)(int type, const char *error_msg, ...); diff --git a/main/output.c b/main/output.c index ab5ee333e7..5ba4c92831 100644 --- a/main/output.c +++ b/main/output.c @@ -37,12 +37,6 @@ static void php_ob_prepend(const char *text, uint text_length); #endif static inline void php_ob_send(void); -void php_start_ob_buffering(void); -void php_end_ob_buffering(int send_buffer); -int php_ob_get_buffer(pval *p); - -/* HEAD support */ -void set_header_request(int value); typedef struct { int (*php_body_write)(const char *str, uint str_length); /* string output */ @@ -51,6 +45,7 @@ typedef struct { uint ob_size; uint ob_block_size; uint ob_text_length; + unsigned char implicit_flush; } php_output_globals; #ifdef ZTS @@ -82,13 +77,14 @@ static void php_output_init_globals(OLS_D) OG(ob_size) = 0; OG(ob_block_size) = 0; OG(ob_text_length) = 0; + OG(implicit_flush) = 0; } PHP_GINIT_FUNCTION(output) { #ifdef ZTS - output_globals_id = ts_allocate_id(sizeof(php_output_globals), NULL, NULL); + output_globals_id = ts_allocate_id(sizeof(php_output_globals), (ts_allocate_ctor) php_output_init_globals, NULL); #else php_output_init_globals(OLS_C); #endif @@ -186,6 +182,18 @@ PHPAPI void php_end_ob_buffering(int send_buffer) } +PHPAPI void php_start_implicit_flush() +{ + php_end_ob_buffering(1); /* Switch out of output buffering if we're in it */ + +} + + +PHPAPI void php_end_implicit_flush() +{ +} + + /* * Output buffering - implementation */ @@ -321,6 +329,7 @@ static int php_ub_body_write_no_header(const char *str, uint str_length) free(newstr); } + return result; } @@ -347,10 +356,6 @@ static int php_ub_body_write(const char *str, uint str_length) * HEAD support */ -void set_header_request(int value) -{ - /* deprecated */ -} PHP_FUNCTION(ob_start) { @@ -378,6 +383,27 @@ PHP_FUNCTION(ob_get_contents) } +PHP_FUNCTION(ob_implicit_flush) +{ + zval **zv_flag; + int flag; + + switch(ZEND_NUM_ARGS()) { + case 0: + flag = 1; + break; + case 1: + if (zend_get_parameters_ex(1, &zv_flag)==FAILURE) { + RETURN_FALSE; + } + convert_to_long_ex(zv_flag); + flag = (*zv_flag)->value.lval; + break; + } +} + + + /* * Local variables: * tab-width: 4 diff --git a/main/php_output.h b/main/php_output.h index 63e692e2e2..17c8424429 100644 --- a/main/php_output.h +++ b/main/php_output.h @@ -27,6 +27,10 @@ PHPAPI int php_body_write(const char *str, uint str_length); PHPAPI int php_header_write(const char *str, uint str_length); PHPAPI void php_start_ob_buffering(void); PHPAPI void php_end_ob_buffering(int send_buffer); +PHPAPI int php_ob_get_buffer(pval *p); +PHPAPI void php_start_implicit_flush(); +PHPAPI void php_end_implicit_flush(); + extern zend_module_entry output_module_entry; #define phpext_output_ptr &output_module_entry diff --git a/sapi/aolserver/aolserver.c b/sapi/aolserver/aolserver.c index a6e2fe96b5..2491c31f2d 100644 --- a/sapi/aolserver/aolserver.c +++ b/sapi/aolserver/aolserver.c @@ -297,6 +297,7 @@ static sapi_module_struct sapi_module = { php_module_shutdown_wrapper, /* shutdown */ php_ns_sapi_ub_write, /* unbuffered write */ + NULL, /* flush */ php_error, /* error handler */ diff --git a/sapi/apache/mod_php4.c b/sapi/apache/mod_php4.c index 9b7f2a2f57..246312be38 100644 --- a/sapi/apache/mod_php4.c +++ b/sapi/apache/mod_php4.c @@ -124,7 +124,7 @@ void php_save_umask(void) } -static int zend_apache_ub_write(const char *str, uint str_length) +static int sapi_apache_ub_write(const char *str, uint str_length) { int ret; SLS_FETCH(); @@ -143,6 +143,17 @@ static int zend_apache_ub_write(const char *str, uint str_length) } + +static void sapi_apache_flush(void *server_context) +{ +#if MODULE_MAGIC_NUMBER > 19970110 + rflush((request_rec *) server_context); +#else + bflush((request_rec *) server_context->connection->client); +#endif +} + + int sapi_apache_read_post(char *buffer, uint count_bytes SLS_DC) { uint total_read_bytes=0, read_bytes; @@ -217,7 +228,9 @@ static sapi_module_struct sapi_module = { php_module_startup, /* startup */ php_module_shutdown_wrapper, /* shutdown */ - zend_apache_ub_write, /* unbuffered write */ + sapi_apache_ub_write, /* unbuffered write */ + sapi_apache_flush, /* flush */ + php_error, /* error handler */ diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c index 7e41932c08..538861faab 100644 --- a/sapi/cgi/cgi_main.c +++ b/sapi/cgi/cgi_main.c @@ -83,12 +83,18 @@ PHPAPI extern char *optarg; PHPAPI extern int optind; -static int zend_cgibin_ub_write(const char *str, uint str_length) +static int sapi_cgibin_ub_write(const char *str, uint str_length) { return fwrite(str, 1, str_length, stdout); } +static void sapi_cgibin_flush(void *server_context) +{ + fflush(stdout); +} + + static void sapi_cgi_send_header(sapi_header_struct *sapi_header, void *server_context) { if (sapi_header) { @@ -126,7 +132,8 @@ static sapi_module_struct sapi_module = { php_module_startup, /* startup */ php_module_shutdown_wrapper, /* shutdown */ - zend_cgibin_ub_write, /* unbuffered write */ + sapi_cgibin_ub_write, /* unbuffered write */ + sapi_cgibin_flush, /* flush */ php_error, /* error handler */ diff --git a/sapi/isapi/php4isapi.c b/sapi/isapi/php4isapi.c index 3bc02b912b..f85b181d48 100644 --- a/sapi/isapi/php4isapi.c +++ b/sapi/isapi/php4isapi.c @@ -130,7 +130,7 @@ static zend_module_entry php_isapi_module = { }; -static int zend_isapi_ub_write(const char *str, uint str_length) +static int sapi_isapi_ub_write(const char *str, uint str_length) { DWORD num_bytes = str_length; LPEXTENSION_CONTROL_BLOCK ecb; @@ -296,7 +296,8 @@ static sapi_module_struct sapi_module = { php_isapi_startup, /* startup */ php_module_shutdown_wrapper, /* shutdown */ - zend_isapi_ub_write, /* unbuffered write */ + sapi_isapi_ub_write, /* unbuffered write */ + NULL, /* flush */ php_error, /* error handler */ diff --git a/sapi/phttpd/phttpd.c b/sapi/phttpd/phttpd.c index a6697952cd..5906e16faf 100644 --- a/sapi/phttpd/phttpd.c +++ b/sapi/phttpd/phttpd.c @@ -168,6 +168,7 @@ static sapi_module_struct sapi_module = { php_module_shutdown_wrapper, /* shutdown */ php_phttpd_sapi_ub_write, /* unbuffered write */ + NULL, /* flush */ php_error, /* error handler */ diff --git a/sapi/roxen/roxen.c b/sapi/roxen/roxen.c index 56d358a2d2..d9c588fa96 100644 --- a/sapi/roxen/roxen.c +++ b/sapi/roxen/roxen.c @@ -519,15 +519,16 @@ static sapi_module_struct sapi_module = { "Roxen", php_module_startup, /* startup */ - pike_module_exit, /* shutdown */ + pike_module_exit, /* shutdown */ php_roxen_sapi_ub_write, /* unbuffered write */ + NULL, /* flush */ php_error, /* error handler */ - php_roxen_sapi_header_handler, /* header handler */ + php_roxen_sapi_header_handler, /* header handler */ php_roxen_sapi_send_headers, /* send headers handler */ - NULL, /* send header handler */ + NULL, /* send header handler */ php_roxen_sapi_read_post, /* read POST data */ php_roxen_sapi_read_cookies, /* read Cookies */ diff --git a/sapi/servlet/servlet.c b/sapi/servlet/servlet.c index 7fcce6fb3b..0682dc0578 100644 --- a/sapi/servlet/servlet.c +++ b/sapi/servlet/servlet.c @@ -122,7 +122,7 @@ void ThrowServletException (JNIEnv *jenv, char *msg) { * sapi callbacks */ -static int zend_servlet_ub_write(const char *str, uint str_length) +static int sapi_servlet_ub_write(const char *str, uint str_length) { SLS_FETCH(); if (!SG(server_context)) { @@ -215,7 +215,8 @@ static sapi_module_struct sapi_module = { php_module_startup, /* startup */ php_module_shutdown_wrapper, /* shutdown */ - zend_servlet_ub_write, /* unbuffered write */ + sapi_servlet_ub_write, /* unbuffered write */ + NULL, /* flush */ php_error, /* error handler */ diff --git a/sapi/thttpd/thttpd.c b/sapi/thttpd/thttpd.c index 81877c305d..fed6c578bd 100644 --- a/sapi/thttpd/thttpd.c +++ b/sapi/thttpd/thttpd.c @@ -108,6 +108,8 @@ static sapi_module_struct sapi_module = { php_module_shutdown_wrapper, sapi_thttpd_ub_write, + NULL, + php_error, NULL, -- 2.40.0