From 4079f914bdad143193e05b2c93ffbbd9e75002ae Mon Sep 17 00:00:00 2001 From: Andi Gutmans Date: Sun, 9 May 1999 18:40:59 +0000 Subject: [PATCH] * Make read_post() read input by chunks instead of returning a single string. This will allow us to efficiently support file upload through SAPI in the future. * Fixes --- cgi_main.c | 11 +++++------ ext/standard/basic_functions.c | 10 +++++----- ext/standard/post.c | 17 ++++++++++++++--- main/SAPI.c | 31 ++++++++++++++++++++++++++++++- main/SAPI.h | 4 +++- 5 files changed, 57 insertions(+), 16 deletions(-) diff --git a/cgi_main.c b/cgi_main.c index e34d0e7e37..6760dbfba9 100644 --- a/cgi_main.c +++ b/cgi_main.c @@ -85,20 +85,19 @@ static void sapi_cgi_send_header(sapi_header_struct *sapi_header, void *server_c } -static char *sapi_cgi_read_post(SLS_D) +static int sapi_cgi_read_post(char *buffer, uint count_bytes SLS_DC) { uint read_bytes=0, tmp_read_bytes; - char *result = (char *) emalloc(SG(request_info).content_length+1); - while (read_bytes < SG(request_info).content_length) { - tmp_read_bytes = read(0, result+read_bytes, SG(request_info).content_length-read_bytes); + count_bytes = MIN(count_bytes, SG(request_info).content_length-SG(read_post_bytes)); + while (read_bytes < count_bytes) { + tmp_read_bytes = read(0, buffer+read_bytes, count_bytes-read_bytes); if (tmp_read_bytes<=0) { break; } read_bytes += tmp_read_bytes; } - result[read_bytes]=0; - return result; + return read_bytes; } diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 471bb121fd..0e0c2cb80e 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -1809,11 +1809,11 @@ PHP_FUNCTION(register_shutdown_function) ZEND_API void php_get_highlight_struct(zend_syntax_highlighter_ini *syntax_highlighter_ini) { - syntax_highlighter_ini->highlight_comment = INI_STR("highlight_comment"); - syntax_highlighter_ini->highlight_default = INI_STR("highlight_default"); - syntax_highlighter_ini->highlight_html = INI_STR("highlight_html"); - syntax_highlighter_ini->highlight_keyword = INI_STR("highlight_keyword"); - syntax_highlighter_ini->highlight_string = INI_STR("highlight_string"); + syntax_highlighter_ini->highlight_comment = INI_STR("highlight.comment"); + syntax_highlighter_ini->highlight_default = INI_STR("highlight.default"); + syntax_highlighter_ini->highlight_html = INI_STR("highlight.html"); + syntax_highlighter_ini->highlight_keyword = INI_STR("highlight.keyword"); + syntax_highlighter_ini->highlight_string = INI_STR("highlight.string"); } diff --git a/ext/standard/post.c b/ext/standard/post.c index 7edcc70788..52260585d9 100644 --- a/ext/standard/post.c +++ b/ext/standard/post.c @@ -320,6 +320,7 @@ void php3_treat_data(int arg, char *str) { char *res = NULL, *var, *val; pval *array_ptr; + int free_buffer; ELS_FETCH(); PLS_FETCH(); SLS_FETCH(); @@ -353,20 +354,28 @@ void php3_treat_data(int arg, char *str) break; } - if (arg == PARSE_POST) { + if (arg == PARSE_POST) { /* POST data */ res = php3_getpost(array_ptr PLS_CC); - } else if (arg == PARSE_GET) { /* Get data */ + free_buffer = 0; + } else if (arg == PARSE_GET) { /* GET data */ var = SG(request_info).query_string; if (var && *var) { res = (char *) estrdup(var); + free_buffer = 1; + } else { + free_buffer = 0; } } else if (arg == PARSE_COOKIE) { /* Cookie data */ var = SG(request_info).cookie_data; if (var && *var) { res = (char *) estrdup(var); + free_buffer = 1; + } else { + free_buffer = 0; } } else if (arg == PARSE_STRING) { /* String data */ res = str; + free_buffer = 1; } if (!res) { return; @@ -397,7 +406,9 @@ void php3_treat_data(int arg, char *str) var = strtok(NULL, PG(arg_separator)); } } - efree(res); + if (free_buffer) { + efree(res); + } } diff --git a/main/SAPI.c b/main/SAPI.c index a5b9b21b9d..1649458b97 100644 --- a/main/SAPI.c +++ b/main/SAPI.c @@ -58,14 +58,43 @@ static void sapi_free_header(sapi_header_struct *sapi_header) } +#undef SAPI_POST_BLOCK_SIZE +#define SAPI_POST_BLOCK_SIZE 2 + +static void sapi_read_post_data(SLS_D) +{ + int read_bytes, total_read_bytes=0; + int allocated_bytes=SAPI_POST_BLOCK_SIZE+1; + + SG(request_info).post_data = emalloc(allocated_bytes); + + for (;;) { + read_bytes = sapi_module.read_post(SG(request_info).post_data+total_read_bytes, SAPI_POST_BLOCK_SIZE SLS_CC); + if (read_bytes<=0) { + break; + } + total_read_bytes += read_bytes; + if (read_bytes < SAPI_POST_BLOCK_SIZE) { + break; + } + if (total_read_bytes+SAPI_POST_BLOCK_SIZE >= allocated_bytes) { + allocated_bytes = total_read_bytes+SAPI_POST_BLOCK_SIZE+1; + SG(request_info).post_data = erealloc(SG(request_info).post_data, allocated_bytes); + } + } + SG(request_info).post_data[total_read_bytes] = 0; /* terminating NULL */ +} + + SAPI_API void sapi_activate(SLS_D) { zend_llist_init(&SG(sapi_headers).headers, sizeof(sapi_header_struct), (void (*)(void *)) sapi_free_header, 0); SG(sapi_headers).send_default_content_type = 1; SG(sapi_headers).http_response_code = 200; SG(headers_sent) = 0; + SG(read_post_bytes) = 0; if (SG(server_context)) { - SG(request_info).post_data = sapi_module.read_post(SLS_C); + sapi_read_post_data(SLS_C); SG(request_info).cookie_data = sapi_module.read_cookies(SLS_C); } } diff --git a/main/SAPI.h b/main/SAPI.h index 03bc4f7868..0ea9b8d8c7 100644 --- a/main/SAPI.h +++ b/main/SAPI.h @@ -4,6 +4,7 @@ #include "zend.h" #include "zend_llist.h" +#define SAPI_POST_BLOCK_SIZE 4000 #if WIN32||WINNT # ifdef SAPI_EXPORTS @@ -55,6 +56,7 @@ typedef struct { void *server_context; sapi_request_info request_info; sapi_headers_struct sapi_headers; + uint read_post_bytes; unsigned char headers_sent; } sapi_globals_struct; @@ -99,7 +101,7 @@ struct _sapi_module_struct { int (*send_headers)(sapi_headers_struct *sapi_headers SLS_DC); void (*send_header)(sapi_header_struct *sapi_header, void *server_context); - char *(*read_post)(SLS_D); + int (*read_post)(char *buffer, uint count_bytes SLS_DC); char *(*read_cookies)(SLS_D); }; -- 2.40.0