Skip to content

Commit 7385027

Browse files
committed
Avoid SAPI api changes
1 parent f10e45f commit 7385027

9 files changed

+104
-79
lines changed

ext/standard/html.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,7 +1584,8 @@ PHP_FUNCTION(request_parse_body)
15841584
if (input_stream_zv) {
15851585
php_stream_from_zval(input_stream, input_stream_zv);
15861586
}
1587-
SG(rfc1867_input_stream) = input_stream;
1587+
SG(request_parse_body_context).input_stream = input_stream;
1588+
SG(request_parse_body_context).throw_exceptions = true;
15881589

15891590
const char *content_type_c;
15901591
if (content_type) {
@@ -1597,15 +1598,21 @@ PHP_FUNCTION(request_parse_body)
15971598
}
15981599
}
15991600

1600-
sapi_read_post_data_ex(content_type_c);
1601+
sapi_read_post_data(content_type_c);
16011602
if (!SG(request_info).post_entry) {
16021603
zend_throw_exception_ex(NULL, 0, "Content-Type \"%s\" is not supported", content_type_c);
16031604
RETURN_THROWS();
16041605
}
16051606

1606-
zval post, files;
1607-
array_init(&post);
1608-
array_init(&files);
1609-
sapi_handle_post_ex(&post, &files);
1607+
zval post, files, old_post, old_files;
1608+
ZVAL_COPY_VALUE(&old_post, &PG(http_globals)[TRACK_VARS_POST]);
1609+
ZVAL_COPY_VALUE(&old_files, &PG(http_globals)[TRACK_VARS_FILES]);
1610+
array_init(&PG(http_globals)[TRACK_VARS_POST]);
1611+
array_init(&PG(http_globals)[TRACK_VARS_FILES]);
1612+
sapi_handle_post(&PG(http_globals)[TRACK_VARS_POST]);
1613+
ZVAL_COPY_VALUE(&post, &PG(http_globals)[TRACK_VARS_POST]);
1614+
ZVAL_COPY_VALUE(&files, &PG(http_globals)[TRACK_VARS_FILES]);
1615+
ZVAL_COPY_VALUE(&PG(http_globals)[TRACK_VARS_POST], &old_post);
1616+
ZVAL_COPY_VALUE(&PG(http_globals)[TRACK_VARS_FILES], &old_files);
16101617
RETURN_ARR(zend_new_pair(&post, &files));
16111618
}

main/SAPI.c

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,16 @@ static void sapi_run_header_callback(zval *callback)
160160
}
161161
}
162162

163-
SAPI_API void sapi_handle_post_ex(zval *arg, zval *files)
163+
SAPI_API void sapi_handle_post(void *arg)
164164
{
165165
if (SG(request_info).post_entry && SG(request_info).content_type_dup) {
166-
SG(request_info).post_entry->post_handler(SG(request_info).content_type_dup, arg, files);
166+
SG(request_info).post_entry->post_handler(SG(request_info).content_type_dup, arg);
167167
efree(SG(request_info).content_type_dup);
168168
SG(request_info).content_type_dup = NULL;
169169
}
170170
}
171171

172-
SAPI_API void sapi_handle_post(zval *arg)
173-
{
174-
sapi_handle_post_ex(arg, NULL);
175-
}
176-
177-
SAPI_API void sapi_read_post_data_ex(const char *content_type_)
172+
SAPI_API void sapi_read_post_data(const char *content_type_)
178173
{
179174
sapi_post_entry *post_entry;
180175
uint32_t content_type_length = (uint32_t)strlen(content_type_);
@@ -236,11 +231,6 @@ SAPI_API void sapi_read_post_data_ex(const char *content_type_)
236231
}
237232
}
238233

239-
SAPI_API void sapi_read_post_data(void)
240-
{
241-
sapi_read_post_data_ex(SG(request_info).content_type);
242-
}
243-
244234
SAPI_API size_t sapi_read_post_block(char *buffer, size_t buflen)
245235
{
246236
size_t read_bytes;
@@ -265,10 +255,6 @@ SAPI_API size_t sapi_read_post_block(char *buffer, size_t buflen)
265255

266256
SAPI_API SAPI_POST_READER_FUNC(sapi_read_standard_form_data)
267257
{
268-
if (SG(rfc1867_input_stream)) {
269-
return;
270-
}
271-
272258
if ((SG(post_max_size) > 0) && (SG(request_info).content_length > SG(post_max_size))) {
273259
php_error_docref(NULL, E_WARNING, "POST Content-Length of " ZEND_LONG_FMT " bytes exceeds the limit of " ZEND_LONG_FMT " bytes",
274260
SG(request_info).content_length, SG(post_max_size));
@@ -471,7 +457,8 @@ SAPI_API void sapi_activate(void)
471457
SG(request_info).headers_only = 0;
472458
}
473459
SG(rfc1867_uploaded_files) = NULL;
474-
SG(rfc1867_input_stream) = NULL;
460+
SG(request_parse_body_context).input_stream = NULL;
461+
SG(request_parse_body_context).throw_exceptions = false;
475462

476463
/* Handle request method */
477464
if (SG(server_context)) {
@@ -481,7 +468,7 @@ SAPI_API void sapi_activate(void)
481468
&& !strcmp(SG(request_info).request_method, "POST")) {
482469
/* HTTP POST may contain form data to be processed into variables
483470
* depending on given content type */
484-
sapi_read_post_data();
471+
sapi_read_post_data(SG(request_info).content_type);
485472
} else {
486473
SG(request_info).content_type_dup = NULL;
487474
}
@@ -1035,7 +1022,7 @@ SAPI_API zend_stat_t *sapi_get_stat(void)
10351022
SAPI_API char *sapi_getenv(const char *name, size_t name_len)
10361023
{
10371024
char *value, *tmp;
1038-
1025+
10391026
if (!sapi_module.getenv) {
10401027
return NULL;
10411028
}

main/SAPI.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ typedef struct {
108108
int proto_num;
109109
} sapi_request_info;
110110

111+
typedef struct {
112+
php_stream *input_stream;
113+
bool throw_exceptions;
114+
} sapi_request_parse_body_context;
111115

112116
typedef struct _sapi_globals_struct {
113117
void *server_context;
@@ -120,14 +124,14 @@ typedef struct _sapi_globals_struct {
120124
char *default_mimetype;
121125
char *default_charset;
122126
HashTable *rfc1867_uploaded_files;
123-
php_stream *rfc1867_input_stream;
124127
zend_long post_max_size;
125128
int options;
126129
bool sapi_started;
127130
double global_request_time;
128131
HashTable known_post_content_types;
129132
zval callback_func;
130133
zend_fcall_info_cache fci_cache;
134+
sapi_request_parse_body_context request_parse_body_context;
131135
} sapi_globals_struct;
132136

133137

@@ -186,10 +190,8 @@ SAPI_API int sapi_add_header_ex(const char *header_line, size_t header_line_len,
186190

187191
SAPI_API int sapi_send_headers(void);
188192
SAPI_API void sapi_free_header(sapi_header_struct *sapi_header);
189-
SAPI_API void sapi_handle_post(zval *arg);
190-
SAPI_API void sapi_handle_post_ex(zval *arg, zval *files);
191-
SAPI_API void sapi_read_post_data(void);
192-
SAPI_API void sapi_read_post_data_ex(const char *content_type);
193+
SAPI_API void sapi_handle_post(void *arg);
194+
SAPI_API void sapi_read_post_data(const char *content_type);
193195
SAPI_API size_t sapi_read_post_block(char *buffer, size_t buflen);
194196
SAPI_API int sapi_register_post_entries(const sapi_post_entry *post_entry);
195197
SAPI_API int sapi_register_post_entry(const sapi_post_entry *post_entry);
@@ -275,7 +277,7 @@ struct _sapi_post_entry {
275277
char *content_type;
276278
uint32_t content_type_len;
277279
void (*post_reader)(void);
278-
void (*post_handler)(char *content_type_dup, zval *arg, zval *files);
280+
void (*post_handler)(char *content_type_dup, void *arg);
279281
};
280282

281283
/* header_handler() constants */
@@ -291,7 +293,7 @@ struct _sapi_post_entry {
291293
#define SAPI_PHP_VERSION_HEADER "X-Powered-By: PHP/" PHP_VERSION
292294

293295
#define SAPI_POST_READER_FUNC(post_reader) void post_reader(void)
294-
#define SAPI_POST_HANDLER_FUNC(post_handler) void post_handler(char *content_type_dup, zval *arg, zval *files)
296+
#define SAPI_POST_HANDLER_FUNC(post_handler) void post_handler(char *content_type_dup, void *arg)
295297

296298
#define SAPI_TREAT_DATA_FUNC(treat_data) void treat_data(int arg, char *str, zval* destArray)
297299
#define SAPI_INPUT_FILTER_FUNC(input_filter) unsigned int input_filter(int arg, const char *var, char **val, size_t val_len, size_t *new_val_len)

main/php_content_types.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static const sapi_post_entry php_post_entries[] = {
3131
/* {{{ SAPI_POST_READER_FUNC */
3232
SAPI_API SAPI_POST_READER_FUNC(php_default_post_reader)
3333
{
34-
if (SG(request_info).request_method && !strcmp(SG(request_info).request_method, "POST")) {
34+
if (!SG(request_parse_body_context).input_stream && !strcmp(SG(request_info).request_method, "POST")) {
3535
if (NULL == SG(request_info).post_entry) {
3636
/* no post handler registered, so we just swallow the data */
3737
sapi_read_standard_form_data();

main/php_variables.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "php_content_types.h"
2626
#include "SAPI.h"
2727
#include "zend_globals.h"
28+
#include "zend_exceptions.h"
2829

2930
/* for systems that need to override reading of environment variables */
3031
void _php_import_environment_variables(zval *array_ptr);
@@ -323,7 +324,7 @@ typedef struct post_var_data {
323324
size_t already_scanned;
324325
} post_var_data_t;
325326

326-
static bool add_post_var(zval *arr, post_var_data_t *var, bool eof, bool store_in_global)
327+
static bool add_post_var(zval *arr, post_var_data_t *var, bool eof)
327328
{
328329
char *start, *ksep, *vsep, *val;
329330
size_t klen, vlen;
@@ -364,8 +365,7 @@ static bool add_post_var(zval *arr, post_var_data_t *var, bool eof, bool store_i
364365
vlen = php_url_decode(val, vlen);
365366
}
366367

367-
int filter_type = store_in_global ? PARSE_POST : PARSE_STRING;
368-
if (sapi_module.input_filter(filter_type, var->ptr, &val, vlen, &new_vlen)) {
368+
if (sapi_module.input_filter(PARSE_POST, var->ptr, &val, vlen, &new_vlen)) {
369369
php_register_variable_safe(var->ptr, val, new_vlen, arr);
370370
}
371371
efree(val);
@@ -375,13 +375,13 @@ static bool add_post_var(zval *arr, post_var_data_t *var, bool eof, bool store_i
375375
return 1;
376376
}
377377

378-
static inline int add_post_vars(zval *arr, post_var_data_t *vars, bool eof, bool store_in_global)
378+
static inline int add_post_vars(zval *arr, post_var_data_t *vars, bool eof)
379379
{
380380
uint64_t max_vars = PG(max_input_vars);
381381

382382
vars->ptr = ZSTR_VAL(vars->str.s);
383383
vars->end = ZSTR_VAL(vars->str.s) + ZSTR_LEN(vars->str.s);
384-
while (add_post_var(arr, vars, eof, store_in_global)) {
384+
while (add_post_var(arr, vars, eof)) {
385385
if (++vars->cnt > max_vars) {
386386
php_error_docref(NULL, E_WARNING,
387387
"Input variables exceeded %" PRIu64 ". "
@@ -405,33 +405,40 @@ static inline int add_post_vars(zval *arr, post_var_data_t *vars, bool eof, bool
405405
SAPI_API SAPI_POST_HANDLER_FUNC(php_std_post_handler)
406406
{
407407
zval *arr = (zval *) arg;
408-
php_stream *s = SG(rfc1867_input_stream) ? SG(rfc1867_input_stream) : SG(request_info).request_body;
408+
bool custom_input_stream = SG(request_parse_body_context).input_stream;
409+
php_stream *s = custom_input_stream
410+
? SG(request_parse_body_context).input_stream
411+
: SG(request_info).request_body;
409412
post_var_data_t post_data;
410-
bool store_in_global = files == NULL;
411413

412414
if (s && SUCCESS == php_stream_rewind(s)) {
413415
ssize_t total_len = 0;
414416
zend_long post_max_size = SG(post_max_size);
415-
bool check_post_max_size = s == SG(rfc1867_input_stream);
417+
bool throw_exceptions = SG(request_parse_body_context).throw_exceptions;
418+
419+
#define EMIT_WARNING_OR_ERROR(...) \
420+
(throw_exceptions \
421+
? zend_throw_exception_ex(NULL, 0, __VA_ARGS__) \
422+
: sapi_module.sapi_error(E_WARNING, __VA_ARGS__))
416423

417424
memset(&post_data, 0, sizeof(post_data));
418425

419426
while (!php_stream_eof(s)) {
420427
char buf[SAPI_POST_HANDLER_BUFSIZ] = {0};
421428
ssize_t len = php_stream_read(s, buf, SAPI_POST_HANDLER_BUFSIZ);
422429

423-
if (UNEXPECTED(check_post_max_size)) {
430+
if (UNEXPECTED(custom_input_stream)) {
424431
total_len += len;
425432
if ((post_max_size > 0) && (total_len > SG(post_max_size))) {
426-
php_error_docref(NULL, E_WARNING, "POST length exceeds post_max_size (" ZEND_LONG_FMT " bytes)", SG(post_max_size));
433+
EMIT_WARNING_OR_ERROR("POST length exceeds post_max_size (" ZEND_LONG_FMT " bytes)", SG(post_max_size));
427434
break;
428435
}
429436
}
430437

431438
if (len > 0) {
432439
smart_str_appendl(&post_data.str, buf, len);
433440

434-
if (SUCCESS != add_post_vars(arr, &post_data, 0, store_in_global)) {
441+
if (SUCCESS != add_post_vars(arr, &post_data, 0)) {
435442
smart_str_free(&post_data.str);
436443
return;
437444
}
@@ -443,12 +450,13 @@ SAPI_API SAPI_POST_HANDLER_FUNC(php_std_post_handler)
443450
}
444451

445452
if (post_data.str.s) {
446-
add_post_vars(arr, &post_data, 1, store_in_global);
453+
add_post_vars(arr, &post_data, 1);
447454
smart_str_free(&post_data.str);
448455
}
449456
}
450457
}
451458
#undef SAPI_POST_HANDLER_BUFSIZ
459+
#undef EMIT_WARNING_OR_ERROR
452460

453461
SAPI_API SAPI_INPUT_FILTER_FUNC(php_default_input_filter)
454462
{

0 commit comments

Comments
 (0)