5959#define PHP_SOURCE_MAGIC_TYPE "application/x-httpd-php-source"
6060#define PHP_SCRIPT "php7-script"
6161
62+ /* if apache's version is newer than 2.2.31 or 2.4.16 */
63+ #if MODULE_MAGIC_COOKIE == 0x41503232UL && AP_MODULE_MAGIC_AT_LEAST (20051115 ,40 ) || \
64+ MODULE_MAGIC_COOKIE == 0x41503234UL && AP_MODULE_MAGIC_AT_LEAST (20120211 ,47 )
65+ #define php_ap_map_http_request_error ap_map_http_request_error
66+ #else
67+ static int php_ap_map_http_request_error (apr_status_t rv , int status )
68+ {
69+ switch (rv ) {
70+ case AP_FILTER_ERROR : {
71+ return AP_FILTER_ERROR ;
72+ }
73+ case APR_ENOSPC : {
74+ return HTTP_REQUEST_ENTITY_TOO_LARGE ;
75+ }
76+ case APR_ENOTIMPL : {
77+ return HTTP_NOT_IMPLEMENTED ;
78+ }
79+ case APR_ETIMEDOUT : {
80+ return HTTP_REQUEST_TIME_OUT ;
81+ }
82+ default : {
83+ return status ;
84+ }
85+ }
86+ }
87+ #endif
88+
6289/* A way to specify the location of the php.ini dir in an apache directive */
6390char * apache2_php_ini_path_override = NULL ;
6491#if defined(PHP_WIN32 ) && defined(ZTS )
@@ -180,6 +207,7 @@ php_apache_sapi_read_post(char *buf, size_t count_bytes)
180207 php_struct * ctx = SG (server_context );
181208 request_rec * r ;
182209 apr_bucket_brigade * brigade ;
210+ apr_status_t ret ;
183211
184212 r = ctx -> r ;
185213 brigade = ctx -> brigade ;
@@ -191,7 +219,7 @@ php_apache_sapi_read_post(char *buf, size_t count_bytes)
191219 * need to make sure that if data is available we fill the buffer completely.
192220 */
193221
194- while (ap_get_brigade (r -> input_filters , brigade , AP_MODE_READBYTES , APR_BLOCK_READ , len ) == APR_SUCCESS ) {
222+ while (( ret = ap_get_brigade (r -> input_filters , brigade , AP_MODE_READBYTES , APR_BLOCK_READ , len ) ) == APR_SUCCESS ) {
195223 apr_brigade_flatten (brigade , buf , & len );
196224 apr_brigade_cleanup (brigade );
197225 tlen += len ;
@@ -202,6 +230,15 @@ php_apache_sapi_read_post(char *buf, size_t count_bytes)
202230 len = count_bytes - tlen ;
203231 }
204232
233+ if (ret != APR_SUCCESS ) {
234+ AP2 (post_read_error ) = 1 ;
235+ if (APR_STATUS_IS_TIMEUP (ret )) {
236+ SG (sapi_headers ).http_response_code = php_ap_map_http_request_error (ret , HTTP_REQUEST_TIME_OUT );
237+ } else {
238+ SG (sapi_headers ).http_response_code = php_ap_map_http_request_error (ret , HTTP_BAD_REQUEST );
239+ }
240+ }
241+
205242 return tlen ;
206243}
207244
@@ -510,6 +547,7 @@ static int php_apache_request_ctor(request_rec *r, php_struct *ctx)
510547 SG (request_info ).proto_num = r -> proto_num ;
511548 SG (request_info ).request_uri = apr_pstrdup (r -> pool , r -> uri );
512549 SG (request_info ).path_translated = apr_pstrdup (r -> pool , r -> filename );
550+ AP2 (post_read_error ) = 0 ;
513551 r -> no_local_copy = 1 ;
514552
515553 content_length = (char * ) apr_table_get (r -> headers_in , "Content-Length" );
@@ -679,6 +717,13 @@ zend_first_try {
679717 brigade = ctx -> brigade ;
680718 }
681719
720+ if (AP2 (post_read_error )) {
721+ ap_log_rerror (APLOG_MARK , APLOG_ERR , 0 , r , "Error while attempting to read POST data: %d" , SG (sapi_headers ).http_response_code );
722+ apr_brigade_cleanup (brigade );
723+ PHPAP_INI_OFF ;
724+ return SG (sapi_headers ).http_response_code ;
725+ }
726+
682727 if (AP2 (last_modified )) {
683728 ap_update_mtime (r , r -> finfo .mtime );
684729 ap_set_last_modified (r );
0 commit comments