From 2438490addfbfba51e12246a74588b2382caa08a Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Wed, 14 Aug 2013 14:42:36 +0200 Subject: slim post data --- ext/standard/php_fopen_wrapper.c | 62 ++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 34 deletions(-) (limited to 'ext/standard/php_fopen_wrapper.c') diff --git a/ext/standard/php_fopen_wrapper.c b/ext/standard/php_fopen_wrapper.c index 0fb27baacd..ca0b92ebde 100644 --- a/ext/standard/php_fopen_wrapper.c +++ b/ext/standard/php_fopen_wrapper.c @@ -71,43 +71,20 @@ static size_t php_stream_input_write(php_stream *stream, const char *buf, size_t static size_t php_stream_input_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) /* {{{ */ { - off_t *position = (off_t*)stream->abstract; - size_t read_bytes = 0; - - if (!stream->eof) { - if (SG(request_info).raw_post_data) { /* data has already been read by a post handler */ - read_bytes = SG(request_info).raw_post_data_length - *position; - if (read_bytes <= count) { - stream->eof = 1; - } else { - read_bytes = count; - } - if (read_bytes) { - memcpy(buf, SG(request_info).raw_post_data + *position, read_bytes); - } - } else if (sapi_module.read_post) { - read_bytes = sapi_module.read_post(buf, count TSRMLS_CC); - if (read_bytes <= 0) { - stream->eof = 1; - read_bytes = 0; - } - /* Increment SG(read_post_bytes) only when something was actually read. */ - SG(read_post_bytes) += read_bytes; - } else { - stream->eof = 1; - } - } + php_stream *inner = stream->abstract; - *position += read_bytes; + if (inner && inner->ops->read) { + size_t read = inner->ops->read(inner, buf, count TSRMLS_CC); + stream->eof = inner->eof; + return read; + } - return read_bytes; + return -1; } /* }}} */ static int php_stream_input_close(php_stream *stream, int close_handle TSRMLS_DC) /* {{{ */ { - efree(stream->abstract); - return 0; } /* }}} */ @@ -118,13 +95,25 @@ static int php_stream_input_flush(php_stream *stream TSRMLS_DC) /* {{{ */ } /* }}} */ +static int php_stream_input_seek(php_stream *stream, off_t offset, int whence, off_t *newoffset TSRMLS_DC) /* {{{ */ +{ + php_stream *inner = stream->abstract; + + if (inner && inner->ops->seek) { + return inner->ops->seek(inner, offset, whence, newoffset TSRMLS_CC); + } + + return -1; +} +/* }}} */ + php_stream_ops php_stream_input_ops = { php_stream_input_write, php_stream_input_read, php_stream_input_close, php_stream_input_flush, "Input", - NULL, /* seek */ + php_stream_input_seek, NULL, /* cast */ NULL, /* stat */ NULL /* set_option */ @@ -210,7 +199,12 @@ php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, const char *pa } return NULL; } - return php_stream_alloc(&php_stream_input_ops, ecalloc(1, sizeof(off_t)), 0, "rb"); + if (SG(request_info).request_body) { + php_stream_rewind(SG(request_info).request_body); + } else { + sapi_read_standard_form_data(TSRMLS_C); + } + return php_stream_alloc(&php_stream_input_ops, SG(request_info).request_body, 0, "rb"); } if (!strcasecmp(path, "stdin")) { @@ -259,8 +253,8 @@ php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, const char *pa fd = dup(STDERR_FILENO); } } else if (!strncasecmp(path, "fd/", 3)) { - char *start, - *end; + const char *start; + char *end; long fildes_ori; int dtablesize; -- cgit v1.2.1 From bb1f9d3826c968ce0c099598ded6f646b3da6429 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Wed, 14 Aug 2013 14:42:36 +0200 Subject: slim post data --- ext/standard/php_fopen_wrapper.c | 62 ++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 34 deletions(-) (limited to 'ext/standard/php_fopen_wrapper.c') diff --git a/ext/standard/php_fopen_wrapper.c b/ext/standard/php_fopen_wrapper.c index 0fb27baacd..ca0b92ebde 100644 --- a/ext/standard/php_fopen_wrapper.c +++ b/ext/standard/php_fopen_wrapper.c @@ -71,43 +71,20 @@ static size_t php_stream_input_write(php_stream *stream, const char *buf, size_t static size_t php_stream_input_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) /* {{{ */ { - off_t *position = (off_t*)stream->abstract; - size_t read_bytes = 0; - - if (!stream->eof) { - if (SG(request_info).raw_post_data) { /* data has already been read by a post handler */ - read_bytes = SG(request_info).raw_post_data_length - *position; - if (read_bytes <= count) { - stream->eof = 1; - } else { - read_bytes = count; - } - if (read_bytes) { - memcpy(buf, SG(request_info).raw_post_data + *position, read_bytes); - } - } else if (sapi_module.read_post) { - read_bytes = sapi_module.read_post(buf, count TSRMLS_CC); - if (read_bytes <= 0) { - stream->eof = 1; - read_bytes = 0; - } - /* Increment SG(read_post_bytes) only when something was actually read. */ - SG(read_post_bytes) += read_bytes; - } else { - stream->eof = 1; - } - } + php_stream *inner = stream->abstract; - *position += read_bytes; + if (inner && inner->ops->read) { + size_t read = inner->ops->read(inner, buf, count TSRMLS_CC); + stream->eof = inner->eof; + return read; + } - return read_bytes; + return -1; } /* }}} */ static int php_stream_input_close(php_stream *stream, int close_handle TSRMLS_DC) /* {{{ */ { - efree(stream->abstract); - return 0; } /* }}} */ @@ -118,13 +95,25 @@ static int php_stream_input_flush(php_stream *stream TSRMLS_DC) /* {{{ */ } /* }}} */ +static int php_stream_input_seek(php_stream *stream, off_t offset, int whence, off_t *newoffset TSRMLS_DC) /* {{{ */ +{ + php_stream *inner = stream->abstract; + + if (inner && inner->ops->seek) { + return inner->ops->seek(inner, offset, whence, newoffset TSRMLS_CC); + } + + return -1; +} +/* }}} */ + php_stream_ops php_stream_input_ops = { php_stream_input_write, php_stream_input_read, php_stream_input_close, php_stream_input_flush, "Input", - NULL, /* seek */ + php_stream_input_seek, NULL, /* cast */ NULL, /* stat */ NULL /* set_option */ @@ -210,7 +199,12 @@ php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, const char *pa } return NULL; } - return php_stream_alloc(&php_stream_input_ops, ecalloc(1, sizeof(off_t)), 0, "rb"); + if (SG(request_info).request_body) { + php_stream_rewind(SG(request_info).request_body); + } else { + sapi_read_standard_form_data(TSRMLS_C); + } + return php_stream_alloc(&php_stream_input_ops, SG(request_info).request_body, 0, "rb"); } if (!strcasecmp(path, "stdin")) { @@ -259,8 +253,8 @@ php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, const char *pa fd = dup(STDERR_FILENO); } } else if (!strncasecmp(path, "fd/", 3)) { - char *start, - *end; + const char *start; + char *end; long fildes_ori; int dtablesize; -- cgit v1.2.1 From 449d4c0b1c6ea0f5dfe7b56c99d9fc4f2033d27c Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 10 Sep 2013 13:13:33 +0200 Subject: make reading php://input JIT if enable_post_data_reading=0 --- ext/standard/php_fopen_wrapper.c | 55 +++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 6 deletions(-) (limited to 'ext/standard/php_fopen_wrapper.c') diff --git a/ext/standard/php_fopen_wrapper.c b/ext/standard/php_fopen_wrapper.c index ca0b92ebde..aa7924d211 100644 --- a/ext/standard/php_fopen_wrapper.c +++ b/ext/standard/php_fopen_wrapper.c @@ -63,6 +63,12 @@ php_stream_ops php_stream_output_ops = { NULL /* set_option */ }; +typedef struct php_stream_input { /* {{{ */ + php_stream **body_ptr; + off_t position; +} php_stream_input_t; +/* }}} */ + static size_t php_stream_input_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC) /* {{{ */ { return -1; @@ -71,20 +77,52 @@ static size_t php_stream_input_write(php_stream *stream, const char *buf, size_t static size_t php_stream_input_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) /* {{{ */ { - php_stream *inner = stream->abstract; + php_stream_input_t *input = stream->abstract; + size_t read; + //fprintf(stderr, "Attempt to read %lu bytes (%lu)\n", count, SG(read_post_bytes)); + + if (!SG(post_read) && SG(read_post_bytes) < input->position + count) { + /* read requested data from SAPI */ + int read_bytes = sapi_read_post_block(buf, count TSRMLS_CC); + + //fprintf(stderr, "Did read %d bytes\n", read_bytes); + if (read_bytes > 0) { + php_stream_seek(*input->body_ptr, 0, SEEK_END); + php_stream_write(*input->body_ptr, buf, read_bytes); + } + } + + php_stream_seek(*input->body_ptr, input->position, SEEK_SET); + read = (*input->body_ptr)->ops->read(*input->body_ptr, buf, count TSRMLS_CC); + + if (!read || read == (size_t) -1) { + stream->eof = 1; + } else { + input->position += read; + } + + return read; +} +/* }}} */ + +static size_t php_stream_input_read_x(php_stream *stream, char *buf, size_t count TSRMLS_DC) /* {{{ */ +{ + php_stream_input_t *input = stream->abstract; + php_stream *inner = *input->body_ptr; if (inner && inner->ops->read) { size_t read = inner->ops->read(inner, buf, count TSRMLS_CC); stream->eof = inner->eof; return read; } - return -1; } /* }}} */ static int php_stream_input_close(php_stream *stream, int close_handle TSRMLS_DC) /* {{{ */ { + efree(stream->abstract); + return 0; } /* }}} */ @@ -193,18 +231,23 @@ php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, const char *pa } if (!strcasecmp(path, "input")) { + php_stream_input_t *input; + if ((options & STREAM_OPEN_FOR_INCLUDE) && !PG(allow_url_include) ) { if (options & REPORT_ERRORS) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "URL file-access is disabled in the server configuration"); } return NULL; } - if (SG(request_info).request_body) { - php_stream_rewind(SG(request_info).request_body); + + input = ecalloc(1, sizeof(*input)); + if (*(input->body_ptr = &SG(request_info).request_body)) { + php_stream_rewind(*input->body_ptr); } else { - sapi_read_standard_form_data(TSRMLS_C); + *input->body_ptr = php_stream_temp_create(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE); } - return php_stream_alloc(&php_stream_input_ops, SG(request_info).request_body, 0, "rb"); + + return php_stream_alloc(&php_stream_input_ops, input, 0, "rb"); } if (!strcasecmp(path, "stdin")) { -- cgit v1.2.1 From 71bee63fad5418642c87c588cc9e22ca44186ce6 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 10 Sep 2013 13:16:24 +0200 Subject: remove unused code --- ext/standard/php_fopen_wrapper.c | 16 ---------------- 1 file changed, 16 deletions(-) (limited to 'ext/standard/php_fopen_wrapper.c') diff --git a/ext/standard/php_fopen_wrapper.c b/ext/standard/php_fopen_wrapper.c index aa7924d211..27acb28f52 100644 --- a/ext/standard/php_fopen_wrapper.c +++ b/ext/standard/php_fopen_wrapper.c @@ -79,13 +79,11 @@ static size_t php_stream_input_read(php_stream *stream, char *buf, size_t count { php_stream_input_t *input = stream->abstract; size_t read; - //fprintf(stderr, "Attempt to read %lu bytes (%lu)\n", count, SG(read_post_bytes)); if (!SG(post_read) && SG(read_post_bytes) < input->position + count) { /* read requested data from SAPI */ int read_bytes = sapi_read_post_block(buf, count TSRMLS_CC); - //fprintf(stderr, "Did read %d bytes\n", read_bytes); if (read_bytes > 0) { php_stream_seek(*input->body_ptr, 0, SEEK_END); php_stream_write(*input->body_ptr, buf, read_bytes); @@ -105,20 +103,6 @@ static size_t php_stream_input_read(php_stream *stream, char *buf, size_t count } /* }}} */ -static size_t php_stream_input_read_x(php_stream *stream, char *buf, size_t count TSRMLS_DC) /* {{{ */ -{ - php_stream_input_t *input = stream->abstract; - php_stream *inner = *input->body_ptr; - - if (inner && inner->ops->read) { - size_t read = inner->ops->read(inner, buf, count TSRMLS_CC); - stream->eof = inner->eof; - return read; - } - return -1; -} -/* }}} */ - static int php_stream_input_close(php_stream *stream, int close_handle TSRMLS_DC) /* {{{ */ { efree(stream->abstract); -- cgit v1.2.1 From 0f78d8612a7b16f1bbe3fb80a99896d7163c0aa7 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 17 Sep 2013 13:44:02 +0200 Subject: we need to use the full stream wrapper for filters --- ext/standard/php_fopen_wrapper.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'ext/standard/php_fopen_wrapper.c') diff --git a/ext/standard/php_fopen_wrapper.c b/ext/standard/php_fopen_wrapper.c index ca0b92ebde..f624cf4958 100644 --- a/ext/standard/php_fopen_wrapper.c +++ b/ext/standard/php_fopen_wrapper.c @@ -73,8 +73,8 @@ static size_t php_stream_input_read(php_stream *stream, char *buf, size_t count { php_stream *inner = stream->abstract; - if (inner && inner->ops->read) { - size_t read = inner->ops->read(inner, buf, count TSRMLS_CC); + if (inner) { + size_t read = php_stream_read(inner, buf, count); stream->eof = inner->eof; return read; } @@ -99,8 +99,10 @@ static int php_stream_input_seek(php_stream *stream, off_t offset, int whence, o { php_stream *inner = stream->abstract; - if (inner && inner->ops->seek) { - return inner->ops->seek(inner, offset, whence, newoffset TSRMLS_CC); + if (inner) { + int sought = php_stream_seek(inner, offset, whence); + *newoffset = inner->position; + return sought; } return -1; -- cgit v1.2.1 From e6084da4735c945cb071c4d9259ea0d702eb77c6 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 17 Sep 2013 13:59:49 +0200 Subject: final bits --- ext/standard/php_fopen_wrapper.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'ext/standard/php_fopen_wrapper.c') diff --git a/ext/standard/php_fopen_wrapper.c b/ext/standard/php_fopen_wrapper.c index 064eee21eb..76f77ebf7b 100644 --- a/ext/standard/php_fopen_wrapper.c +++ b/ext/standard/php_fopen_wrapper.c @@ -91,7 +91,7 @@ static size_t php_stream_input_read(php_stream *stream, char *buf, size_t count } php_stream_seek(*input->body_ptr, input->position, SEEK_SET); - read = (*input->body_ptr)->ops->read(*input->body_ptr, buf, count TSRMLS_CC); + read = php_stream_read(*input->body_ptr, buf, count); if (!read || read == (size_t) -1) { stream->eof = 1; @@ -105,6 +105,9 @@ static size_t php_stream_input_read(php_stream *stream, char *buf, size_t count static int php_stream_input_close(php_stream *stream, int close_handle TSRMLS_DC) /* {{{ */ { + efree(stream->abstract); + stream->abstract = NULL; + return 0; } /* }}} */ -- cgit v1.2.1