Skip to content

Commit e20af30

Browse files
authored
Clean up SAPI POST readers (#24057)
* sapi: remove default post reader There is only one of these; there's no point in the indirection here as it is never overrode, and set very early in main.c. Move the default post handling into the SAPI handling, and remove the pointless check if a default POST reader exists. This stuff was originally introduced in 98d95dd. * sapi: move default php_post_entries into SAPI.c mbstring had a duplicate of this; remove some indirection and provide this as a symbol so it can be switched back to w/o duplication. * sapi: Move DEFAULT_POST_CONTENT_TYPE into SAPI.h * sapi: Remove indirection for treat data/input filter init * sapi: Eliminate php_content_types All its contents have been folded elsewhere. XXX: Perhaps php_std_post_handler belongs in php_variables.h? * sapi: move php_std_post_handler per Gina * sapi: POST handler/reader entries in NEWS
1 parent 423fd33 commit e20af30

11 files changed

Lines changed: 34 additions & 119 deletions

File tree

‎UPGRADING.INTERNALS‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ PHP 8.7 INTERNALS UPGRADE NOTES
6666
. The sapi_get_target_uid() function was removed.
6767
. The sapi_get_target_gid() function was removed.
6868
. The sapi_activate_headers_only() function was removed.
69+
. The sapi_register_default_post_reader() function was removed. The default
70+
POST reader indirection was vestigial. Register POST entries instead.
71+
. The php_startup_sapi_content_types() function was removed.
72+
. The php_setup_sapi_content_types() function was removed.
6973
. Removed zend_hrtime_posix_clock_id. zend_hrtime() always uses
7074
CLOCK_MONOTONIC on POSIX platforms.
7175
. The INIT_CLASS_ENTRY_INIT_METHODS() macro, which was previously used as
@@ -100,6 +104,9 @@ PHP 8.7 INTERNALS UPGRADE NOTES
100104
. ZEND_IN_RANGE_OF(x, T) was added to check whether the value of an integer
101105
x is representable in the integer type T.
102106
. ZEND_TYPE_MIN(T) and ZEND_TYPE_MAX(T) were added.
107+
. php_post_entries is now exposed as a symbol. Anything replacing POST
108+
handlers should use this when switching back to PHP's defaults, rather
109+
than having a copy of it.
103110

104111
- Deprecated:
105112
. ZEND_LONG_INT_OVFL(), ZEND_LONG_INT_UDFL(), ZEND_LONG_EXCEEDS_INT(),
@@ -166,6 +173,7 @@ PHP 8.7 INTERNALS UPGRADE NOTES
166173
. The force_http_10 method has been removed.
167174
. The get_fd method has been removed.
168175
. The get_target_uid and get_target_gid methods have been removed.
176+
. The default_post_reader method has been removed.
169177

170178
- Per-thread TSRM resources are now released best-effort at normal thread exit,
171179
because native TLS resource destructors can only run on their owning thread and

‎configure.ac‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,6 @@ PHP_ADD_SOURCES([main], m4_normalize([
16601660
main.c
16611661
network.c
16621662
output.c
1663-
php_content_types.c
16641663
php_ini_builder.c
16651664
php_ini.c
16661665
php_glob.c

‎ext/mbstring/mbstring.c‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646

4747
#include "php_globals.h"
4848
#include "rfc1867.h"
49-
#include "php_content_types.h"
5049
#include "SAPI.h"
5150
#include "php_unicode.h"
5251
#include "TSRM.h"
@@ -208,14 +207,6 @@ zend_module_entry mbstring_module_entry = {
208207
};
209208
/* }}} */
210209

211-
/* {{{ static sapi_post_entry php_post_entries[] */
212-
static const sapi_post_entry php_post_entries[] = {
213-
{ DEFAULT_POST_CONTENT_TYPE, sizeof(DEFAULT_POST_CONTENT_TYPE)-1, sapi_read_standard_form_data, php_std_post_handler },
214-
{ MULTIPART_CONTENT_TYPE, sizeof(MULTIPART_CONTENT_TYPE)-1, NULL, rfc1867_post_handler },
215-
{ NULL, 0, NULL, NULL }
216-
};
217-
/* }}} */
218-
219210
#ifdef COMPILE_DL_MBSTRING
220211
#ifdef ZTS
221212
ZEND_TSRMLS_CACHE_DEFINE()

‎main/SAPI.c‎

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333

3434
#include "rfc1867.h"
3535

36-
#include "php_content_types.h"
37-
3836
#ifdef ZTS
3937
SAPI_API int sapi_globals_id;
4038
ZEND_TLS_API TSRM_TLS TSRM_TLS_MODEL_ATTR sapi_globals_struct sapi_globals;
@@ -52,7 +50,7 @@ static void sapi_globals_ctor(sapi_globals_struct *sapi_globals)
5250
{
5351
memset(sapi_globals, 0, sizeof(*sapi_globals));
5452
zend_hash_init(&sapi_globals->known_post_content_types, 8, NULL, _type_dtor, 1);
55-
php_setup_sapi_content_types();
53+
sapi_register_post_entries(php_post_entries);
5654
}
5755

5856
static void sapi_globals_dtor(sapi_globals_struct *sapi_globals)
@@ -182,13 +180,6 @@ SAPI_API void sapi_read_post_data(void)
182180
} else {
183181
/* fallback */
184182
SG(request_info).post_entry = NULL;
185-
if (UNEXPECTED(!sapi_module.default_post_reader)) {
186-
/* this should not happen as there should always be a default_post_reader */
187-
SG(request_info).content_type_dup = NULL;
188-
sapi_module.sapi_error(E_WARNING, "Unsupported content type: '%s'", content_type);
189-
efree(content_type);
190-
return;
191-
}
192183
}
193184
if (oldchar) {
194185
*(p-1) = oldchar;
@@ -201,8 +192,11 @@ SAPI_API void sapi_read_post_data(void)
201192
post_reader_func();
202193
}
203194

204-
if(sapi_module.default_post_reader) {
205-
sapi_module.default_post_reader();
195+
if (!strcmp(SG(request_info).request_method, "POST")) {
196+
if (NULL == SG(request_info).post_entry) {
197+
/* no post handler registered, so we just swallow the data */
198+
sapi_read_standard_form_data();
199+
}
206200
}
207201
}
208202

@@ -885,6 +879,13 @@ SAPI_API zend_result sapi_send_headers(void)
885879
return ret;
886880
}
887881

882+
/* {{{ php_post_entries[] */
883+
const sapi_post_entry php_post_entries[] = {
884+
{ DEFAULT_POST_CONTENT_TYPE, sizeof(DEFAULT_POST_CONTENT_TYPE)-1, sapi_read_standard_form_data, php_std_post_handler },
885+
{ MULTIPART_CONTENT_TYPE, sizeof(MULTIPART_CONTENT_TYPE)-1, NULL, rfc1867_post_handler },
886+
{ NULL, 0, NULL, NULL }
887+
};
888+
/* }}} */
888889

889890
SAPI_API zend_result sapi_register_post_entries(const sapi_post_entry *post_entries)
890891
{
@@ -923,17 +924,6 @@ SAPI_API void sapi_unregister_post_entry(const sapi_post_entry *post_entry)
923924
post_entry->content_type_len);
924925
}
925926

926-
927-
SAPI_API zend_result sapi_register_default_post_reader(void (*default_post_reader)(void))
928-
{
929-
if (SG(sapi_started) && EG(current_execute_data)) {
930-
return FAILURE;
931-
}
932-
sapi_module.default_post_reader = default_post_reader;
933-
return SUCCESS;
934-
}
935-
936-
937927
SAPI_API zend_result sapi_register_treat_data(void (*treat_data)(int arg, char *str, zval *destArray))
938928
{
939929
if (SG(sapi_started) && EG(current_execute_data)) {

‎main/SAPI.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ SAPI_API size_t sapi_read_post_block(char *buffer, size_t buflen);
215215
SAPI_API zend_result sapi_register_post_entries(const sapi_post_entry *post_entry);
216216
SAPI_API zend_result sapi_register_post_entry(const sapi_post_entry *post_entry);
217217
SAPI_API void sapi_unregister_post_entry(const sapi_post_entry *post_entry);
218-
SAPI_API zend_result sapi_register_default_post_reader(void (*default_post_reader)(void));
219218
SAPI_API zend_result sapi_register_treat_data(void (*treat_data)(int arg, char *str, zval *destArray));
220219
SAPI_API zend_result sapi_register_input_filter(unsigned int (*input_filter)(int arg, const char *var, char **val, size_t val_len, size_t *new_val_len), unsigned int (*input_filter_init)(void));
221220

@@ -260,7 +259,6 @@ struct _sapi_module_struct {
260259

261260
char *php_ini_path_override;
262261

263-
void (*default_post_reader)(void);
264262
void (*treat_data)(int arg, char *str, zval *destArray);
265263
char *executable_location;
266264

@@ -286,6 +284,11 @@ struct _sapi_post_entry {
286284
void (*post_handler)(char *content_type_dup, void *arg);
287285
};
288286

287+
BEGIN_EXTERN_C()
288+
/* Default POST entries to switch back to when overrode */
289+
extern SAPI_API const sapi_post_entry php_post_entries[];
290+
END_EXTERN_C()
291+
289292
/* header_handler() constants */
290293
#define SAPI_HEADER_ADD (1<<0)
291294

@@ -304,16 +307,16 @@ struct _sapi_post_entry {
304307
#define SAPI_TREAT_DATA_FUNC(treat_data) void treat_data(int arg, char *str, zval* destArray)
305308
#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)
306309

310+
#define DEFAULT_POST_CONTENT_TYPE "application/x-www-form-urlencoded"
311+
307312
BEGIN_EXTERN_C()
308313
SAPI_API SAPI_POST_READER_FUNC(sapi_read_standard_form_data);
309-
SAPI_API SAPI_POST_READER_FUNC(php_default_post_reader);
310314
SAPI_API SAPI_TREAT_DATA_FUNC(php_default_treat_data);
311315
SAPI_API SAPI_INPUT_FILTER_FUNC(php_default_input_filter);
312316
END_EXTERN_C()
313317

314318
#define STANDARD_SAPI_MODULE_PROPERTIES \
315319
NULL, /* php_ini_path_override */ \
316-
NULL, /* default_post_reader */ \
317320
NULL, /* treat_data */ \
318321
NULL, /* executable_location */ \
319322
0, /* php_ini_ignore */ \

‎main/main.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
#include "zend_smart_string.h"
8181
#include "zend_smart_str.h"
8282

83-
#include "php_content_types.h"
8483
#include "php_ticks.h"
8584
#include "php_streams.h"
8685
#include "php_open_temporary_file.h"
@@ -2358,7 +2357,9 @@ zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additi
23582357
zuv.html_errors = 1;
23592358
php_startup_auto_globals();
23602359
zend_set_utility_values(&zuv);
2361-
php_startup_sapi_content_types();
2360+
2361+
sapi_register_treat_data(php_default_treat_data);
2362+
sapi_register_input_filter(php_default_input_filter, NULL);
23622363

23632364
/* Begin to fingerprint the process state */
23642365
zend_startup_system_id();

‎main/php_content_types.c‎

Lines changed: 0 additions & 55 deletions
This file was deleted.

‎main/php_content_types.h‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎main/php_variables.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "zend_smart_str.h"
2121
#include "php_variables.h"
2222
#include "php_globals.h"
23-
#include "php_content_types.h"
2423
#include "SAPI.h"
2524
#include "zend_globals.h"
2625
#include "zend_exceptions.h"

‎main/php_variables.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ PHPAPI void php_register_variable_safe(const char *var, const char *val, size_t
3636
PHPAPI void php_register_variable_ex(const char *var, zval *val, zval *track_vars_array);
3737
PHPAPI void php_register_known_variable(const char *var, size_t var_len, zval *value, zval *track_vars_array);
3838

39+
SAPI_API SAPI_POST_HANDLER_FUNC(php_std_post_handler);
40+
3941
PHPAPI void php_build_argv(const char *s, zval *track_vars_array);
4042
PHPAPI zend_result php_hash_environment(void);
4143
END_EXTERN_C()

0 commit comments

Comments
 (0)