Skip to content

Commit 80c8d84

Browse files
Zheng SHAOnikic
Zheng SHAO
authored andcommitted
Fixed bug #61471
1 parent 7a0adb4 commit 80c8d84

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ PHP NEWS
1010
. Fixed bug #73954 (NAN check fails on Alpine Linux with musl). (Andrea)
1111
. Fixed bug #74039 (is_infinite(-INF) returns false). (Christian Schmidt)
1212

13+
- Apache:
14+
. Fixed bug #61471 (Incomplete POST does not timeout but is passed to PHP).
15+
(Zheng Shao)
16+
1317
- GD:
1418
. Fixed bug #74031 (ReflectionFunction for imagepng is missing last two
1519
parameters). (finwe)

sapi/apache2handler/php_apache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ typedef struct {
7373
zend_bool engine;
7474
zend_bool xbithack;
7575
zend_bool last_modified;
76+
zend_bool post_read_error;
7677
} php_apache2_info_struct;
7778

7879
extern zend_module_entry apache2_module_entry;

sapi/apache2handler/sapi_apache2.c

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,33 @@
6363
#define PHP_SOURCE_MAGIC_TYPE "application/x-httpd-php-source"
6464
#define PHP_SCRIPT "php7-script"
6565

66+
/* if apache's version is newer than 2.2.31 or 2.4.16 */
67+
#if MODULE_MAGIC_COOKIE == 0x41503232UL && AP_MODULE_MAGIC_AT_LEAST(20051115,40) || \
68+
MODULE_MAGIC_COOKIE == 0x41503234UL && AP_MODULE_MAGIC_AT_LEAST(20120211,47)
69+
#define php_ap_map_http_request_error ap_map_http_request_error
70+
#else
71+
static int php_ap_map_http_request_error(apr_status_t rv, int status)
72+
{
73+
switch (rv) {
74+
case AP_FILTER_ERROR: {
75+
return AP_FILTER_ERROR;
76+
}
77+
case APR_ENOSPC: {
78+
return HTTP_REQUEST_ENTITY_TOO_LARGE;
79+
}
80+
case APR_ENOTIMPL: {
81+
return HTTP_NOT_IMPLEMENTED;
82+
}
83+
case APR_ETIMEDOUT: {
84+
return HTTP_REQUEST_TIME_OUT;
85+
}
86+
default: {
87+
return status;
88+
}
89+
}
90+
}
91+
#endif
92+
6693
/* A way to specify the location of the php.ini dir in an apache directive */
6794
char *apache2_php_ini_path_override = NULL;
6895
#if defined(PHP_WIN32) && defined(ZTS)
@@ -184,6 +211,7 @@ php_apache_sapi_read_post(char *buf, size_t count_bytes)
184211
php_struct *ctx = SG(server_context);
185212
request_rec *r;
186213
apr_bucket_brigade *brigade;
214+
apr_status_t ret;
187215

188216
r = ctx->r;
189217
brigade = ctx->brigade;
@@ -195,7 +223,7 @@ php_apache_sapi_read_post(char *buf, size_t count_bytes)
195223
* need to make sure that if data is available we fill the buffer completely.
196224
*/
197225

198-
while (ap_get_brigade(r->input_filters, brigade, AP_MODE_READBYTES, APR_BLOCK_READ, len) == APR_SUCCESS) {
226+
while ((ret=ap_get_brigade(r->input_filters, brigade, AP_MODE_READBYTES, APR_BLOCK_READ, len)) == APR_SUCCESS) {
199227
apr_brigade_flatten(brigade, buf, &len);
200228
apr_brigade_cleanup(brigade);
201229
tlen += len;
@@ -206,6 +234,15 @@ php_apache_sapi_read_post(char *buf, size_t count_bytes)
206234
len = count_bytes - tlen;
207235
}
208236

237+
if (ret != APR_SUCCESS) {
238+
AP2(post_read_error) = 1;
239+
if (APR_STATUS_IS_TIMEUP(ret)) {
240+
SG(sapi_headers).http_response_code = php_ap_map_http_request_error(ret, HTTP_REQUEST_TIME_OUT);
241+
} else {
242+
SG(sapi_headers).http_response_code = php_ap_map_http_request_error(ret, HTTP_BAD_REQUEST);
243+
}
244+
}
245+
209246
return tlen;
210247
}
211248

@@ -487,6 +524,7 @@ static int php_apache_request_ctor(request_rec *r, php_struct *ctx)
487524
SG(request_info).proto_num = r->proto_num;
488525
SG(request_info).request_uri = apr_pstrdup(r->pool, r->uri);
489526
SG(request_info).path_translated = apr_pstrdup(r->pool, r->filename);
527+
AP2(post_read_error) = 0;
490528
r->no_local_copy = 1;
491529

492530
content_length = (char *) apr_table_get(r->headers_in, "Content-Length");
@@ -656,6 +694,13 @@ zend_first_try {
656694
brigade = ctx->brigade;
657695
}
658696

697+
if (AP2(post_read_error)) {
698+
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Error while attempting to read POST data: %d", SG(sapi_headers).http_response_code);
699+
apr_brigade_cleanup(brigade);
700+
PHPAP_INI_OFF;
701+
return SG(sapi_headers).http_response_code;
702+
}
703+
659704
if (AP2(last_modified)) {
660705
ap_update_mtime(r, r->finfo.mtime);
661706
ap_set_last_modified(r);

0 commit comments

Comments
 (0)