@@ -180,10 +180,10 @@ SAPI_API void sapi_handle_post(void *arg TSRMLS_DC)
180180{
181181 if (SG (request_info ).post_entry && SG (request_info ).content_type_dup ) {
182182 SG (request_info ).post_entry -> post_handler (SG (request_info ).content_type_dup , arg TSRMLS_CC );
183- if (SG (request_info ).post_data ) {
184- efree (SG (request_info ).post_data );
185- SG (request_info ).post_data = NULL ;
186- }
183+ /* if (SG(request_info).request_body ) {
184+ php_stream_close (SG(request_info).request_body );
185+ SG(request_info).request_body = NULL;
186+ }*/
187187 efree (SG (request_info ).content_type_dup );
188188 SG (request_info ).content_type_dup = NULL ;
189189 }
@@ -253,35 +253,40 @@ static void sapi_read_post_data(TSRMLS_D)
253253SAPI_API SAPI_POST_READER_FUNC (sapi_read_standard_form_data )
254254{
255255 int read_bytes ;
256- int allocated_bytes = SAPI_POST_BLOCK_SIZE + 1 ;
257256
258257 if ((SG (post_max_size ) > 0 ) && (SG (request_info ).content_length > SG (post_max_size ))) {
259258 php_error_docref (NULL TSRMLS_CC , E_WARNING , "POST Content-Length of %ld bytes exceeds the limit of %ld bytes" ,
260259 SG (request_info ).content_length , SG (post_max_size ));
261260 return ;
262261 }
263- SG (request_info ).post_data = emalloc ( allocated_bytes );
262+ SG (request_info ).request_body = php_stream_temp_create ( TEMP_STREAM_DEFAULT , SAPI_POST_BLOCK_SIZE );
264263
265- for (;;) {
266- read_bytes = sapi_module .read_post (SG (request_info ).post_data + SG (read_post_bytes ), SAPI_POST_BLOCK_SIZE TSRMLS_CC );
267- if (read_bytes <=0 ) {
268- break ;
269- }
270- SG (read_post_bytes ) += read_bytes ;
271- if ((SG (post_max_size ) > 0 ) && (SG (read_post_bytes ) > SG (post_max_size ))) {
272- php_error_docref (NULL TSRMLS_CC , E_WARNING , "Actual POST length does not match Content-Length, and exceeds %ld bytes" , SG (post_max_size ));
273- break ;
274- }
275- if (read_bytes < SAPI_POST_BLOCK_SIZE ) {
276- break ;
277- }
278- if (SG (read_post_bytes )+ SAPI_POST_BLOCK_SIZE >= allocated_bytes ) {
279- allocated_bytes = SG (read_post_bytes )+ SAPI_POST_BLOCK_SIZE + 1 ;
280- SG (request_info ).post_data = erealloc (SG (request_info ).post_data , allocated_bytes );
264+ if (sapi_module .read_post ) {
265+ for (;;) {
266+ char buffer [SAPI_POST_BLOCK_SIZE ];
267+
268+ read_bytes = sapi_module .read_post (buffer , SAPI_POST_BLOCK_SIZE TSRMLS_CC );
269+ if (read_bytes <=0 ) {
270+ /* failure */
271+ break ;
272+ }
273+ SG (read_post_bytes ) += read_bytes ;
274+
275+ if ((SG (post_max_size ) > 0 ) && (SG (read_post_bytes ) > SG (post_max_size ))) {
276+ php_error_docref (NULL TSRMLS_CC , E_WARNING , "Actual POST length does not match Content-Length, and exceeds %ld bytes" , SG (post_max_size ));
277+ break ;
278+ }
279+
280+ php_stream_write (SG (request_info ).request_body , buffer , read_bytes );
281+
282+ if (read_bytes < SAPI_POST_BLOCK_SIZE ) {
283+ /* done */
284+ break ;
285+ }
281286 }
287+
288+ php_stream_rewind (SG (request_info ).request_body );
282289 }
283- SG (request_info ).post_data [SG (read_post_bytes )] = 0 ; /* terminating NULL */
284- SG (request_info ).post_data_length = SG (read_post_bytes );
285290}
286291
287292
@@ -387,8 +392,7 @@ SAPI_API void sapi_activate_headers_only(TSRMLS_D)
387392 SG (sapi_headers ).http_status_line = NULL ;
388393 SG (sapi_headers ).mimetype = NULL ;
389394 SG (read_post_bytes ) = 0 ;
390- SG (request_info ).post_data = NULL ;
391- SG (request_info ).raw_post_data = NULL ;
395+ SG (request_info ).request_body = NULL ;
392396 SG (request_info ).current_user = NULL ;
393397 SG (request_info ).current_user_length = 0 ;
394398 SG (request_info ).no_headers = 0 ;
@@ -433,8 +437,7 @@ SAPI_API void sapi_activate(TSRMLS_D)
433437 SG (callback_run ) = 0 ;
434438 SG (callback_func ) = NULL ;
435439 SG (read_post_bytes ) = 0 ;
436- SG (request_info ).post_data = NULL ;
437- SG (request_info ).raw_post_data = NULL ;
440+ SG (request_info ).request_body = NULL ;
438441 SG (request_info ).current_user = NULL ;
439442 SG (request_info ).current_user_length = 0 ;
440443 SG (request_info ).no_headers = 0 ;
@@ -452,14 +455,15 @@ SAPI_API void sapi_activate(TSRMLS_D)
452455
453456 /* Handle request method */
454457 if (SG (server_context )) {
455- if (PG (enable_post_data_reading ) && SG (request_info ).request_method ) {
456- if (SG (request_info ).content_type && !strcmp (SG (request_info ).request_method , "POST" )) {
458+ if (SG (request_info ).request_method ) {
459+ if (PG (enable_post_data_reading )
460+ && SG (request_info ).content_type
461+ && !strcmp (SG (request_info ).request_method , "POST" )) {
457462 /* HTTP POST may contain form data to be processed into variables
458463 * depending on given content type */
459464 sapi_read_post_data (TSRMLS_C );
460465 } else {
461- /* Any other method with content payload will fill $HTTP_RAW_POST_DATA
462- * if it is enabled by always_populate_raw_post_data.
466+ /* Any other method with content payload will fill php://input stream.
463467 * It's up to the webserver to decide whether to allow a method or not. */
464468 SG (request_info ).content_type_dup = NULL ;
465469 if (sapi_module .default_post_reader ) {
@@ -494,9 +498,9 @@ static void sapi_send_headers_free(TSRMLS_D)
494498SAPI_API void sapi_deactivate (TSRMLS_D )
495499{
496500 zend_llist_destroy (& SG (sapi_headers ).headers );
497- if (SG (request_info ).post_data ) {
498- efree ( SG (request_info ).post_data ) ;
499- } else if (SG (server_context )) {
501+ if (SG (request_info ).request_body ) {
502+ SG (request_info ).request_body = NULL ;
503+ } else if (SG (server_context )) {
500504 if (sapi_module .read_post ) {
501505 /* make sure we've consumed all request input data */
502506 char dummy [SAPI_POST_BLOCK_SIZE ];
@@ -507,9 +511,6 @@ SAPI_API void sapi_deactivate(TSRMLS_D)
507511 }
508512 }
509513 }
510- if (SG (request_info ).raw_post_data ) {
511- efree (SG (request_info ).raw_post_data );
512- }
513514 if (SG (request_info ).auth_user ) {
514515 efree (SG (request_info ).auth_user );
515516 }
0 commit comments