xref: /PHP-7.3/main/SAPI.h (revision 9afce019)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author:  Zeev Suraski <zeev@php.net>                                 |
16    +----------------------------------------------------------------------+
17 */
18 
19 #ifndef SAPI_H
20 #define SAPI_H
21 
22 #include "php.h"
23 #include "zend.h"
24 #include "zend_API.h"
25 #include "zend_llist.h"
26 #include "zend_operators.h"
27 #ifdef PHP_WIN32
28 #include "win32/php_stdint.h"
29 #endif
30 #include <sys/stat.h>
31 
32 #define SAPI_OPTION_NO_CHDIR 1
33 #define SAPI_POST_BLOCK_SIZE 0x4000
34 
35 #ifdef PHP_WIN32
36 #	ifdef SAPI_EXPORTS
37 #		define SAPI_API __declspec(dllexport)
38 #	else
39 #		define SAPI_API __declspec(dllimport)
40 #	endif
41 #elif defined(__GNUC__) && __GNUC__ >= 4
42 #	define SAPI_API __attribute__ ((visibility("default")))
43 #else
44 #	define SAPI_API
45 #endif
46 
47 #undef shutdown
48 
49 typedef struct {
50 	char *header;
51 	size_t header_len;
52 } sapi_header_struct;
53 
54 
55 typedef struct {
56 	zend_llist headers;
57 	int http_response_code;
58 	unsigned char send_default_content_type;
59 	char *mimetype;
60 	char *http_status_line;
61 } sapi_headers_struct;
62 
63 
64 typedef struct _sapi_post_entry sapi_post_entry;
65 typedef struct _sapi_module_struct sapi_module_struct;
66 
67 BEGIN_EXTERN_C()
68 extern SAPI_API sapi_module_struct sapi_module;  /* true global */
69 END_EXTERN_C()
70 
71 /* Some values in this structure needs to be filled in before
72  * calling sapi_activate(). We WILL change the `char *' entries,
73  * so make sure that you allocate a separate buffer for them
74  * and that you free them after sapi_deactivate().
75  */
76 
77 typedef struct {
78 	const char *request_method;
79 	char *query_string;
80 	char *cookie_data;
81 	zend_long content_length;
82 
83 	char *path_translated;
84 	char *request_uri;
85 
86 	/* Do not use request_body directly, but the php://input stream wrapper instead */
87 	struct _php_stream *request_body;
88 
89 	const char *content_type;
90 
91 	zend_bool headers_only;
92 	zend_bool no_headers;
93 	zend_bool headers_read;
94 
95 	sapi_post_entry *post_entry;
96 
97 	char *content_type_dup;
98 
99 	/* for HTTP authentication */
100 	char *auth_user;
101 	char *auth_password;
102 	char *auth_digest;
103 
104 	/* this is necessary for the CGI SAPI module */
105 	char *argv0;
106 
107 	char *current_user;
108 	int current_user_length;
109 
110 	/* this is necessary for CLI module */
111 	int argc;
112 	char **argv;
113 	int proto_num;
114 } sapi_request_info;
115 
116 
117 typedef struct _sapi_globals_struct {
118 	void *server_context;
119 	sapi_request_info request_info;
120 	sapi_headers_struct sapi_headers;
121 	int64_t read_post_bytes;
122 	unsigned char post_read;
123 	unsigned char headers_sent;
124 	zend_stat_t global_stat;
125 	char *default_mimetype;
126 	char *default_charset;
127 	HashTable *rfc1867_uploaded_files;
128 	zend_long post_max_size;
129 	int options;
130 	zend_bool sapi_started;
131 	double global_request_time;
132 	HashTable known_post_content_types;
133 	zval callback_func;
134 	zend_fcall_info_cache fci_cache;
135 } sapi_globals_struct;
136 
137 
138 BEGIN_EXTERN_C()
139 #ifdef ZTS
140 # define SG(v) ZEND_TSRMG(sapi_globals_id, sapi_globals_struct *, v)
141 SAPI_API extern int sapi_globals_id;
142 #else
143 # define SG(v) (sapi_globals.v)
144 extern SAPI_API sapi_globals_struct sapi_globals;
145 #endif
146 
147 SAPI_API void sapi_startup(sapi_module_struct *sf);
148 SAPI_API void sapi_shutdown(void);
149 SAPI_API void sapi_activate(void);
150 SAPI_API void sapi_deactivate(void);
151 SAPI_API void sapi_initialize_empty_request(void);
152 SAPI_API void sapi_add_request_header(char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg);
153 END_EXTERN_C()
154 
155 /*
156  * This is the preferred and maintained API for
157  * operating on HTTP headers.
158  */
159 
160 /*
161  * Always specify a sapi_header_line this way:
162  *
163  *     sapi_header_line ctr = {0};
164  */
165 
166 typedef struct {
167 	char *line; /* If you allocated this, you need to free it yourself */
168 	size_t line_len;
169 	zend_long response_code; /* long due to zend_parse_parameters compatibility */
170 } sapi_header_line;
171 
172 typedef enum {					/* Parameter: 			*/
173 	SAPI_HEADER_REPLACE,		/* sapi_header_line* 	*/
174 	SAPI_HEADER_ADD,			/* sapi_header_line* 	*/
175 	SAPI_HEADER_DELETE,			/* sapi_header_line* 	*/
176 	SAPI_HEADER_DELETE_ALL,		/* void					*/
177 	SAPI_HEADER_SET_STATUS		/* int 					*/
178 } sapi_header_op_enum;
179 
180 BEGIN_EXTERN_C()
181 SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg);
182 
183 /* Deprecated functions. Use sapi_header_op instead. */
184 SAPI_API int sapi_add_header_ex(char *header_line, size_t header_line_len, zend_bool duplicate, zend_bool replace);
185 #define sapi_add_header(a, b, c) sapi_add_header_ex((a),(b),(c),1)
186 
187 
188 SAPI_API int sapi_send_headers(void);
189 SAPI_API void sapi_free_header(sapi_header_struct *sapi_header);
190 SAPI_API void sapi_handle_post(void *arg);
191 SAPI_API size_t sapi_read_post_block(char *buffer, size_t buflen);
192 SAPI_API int sapi_register_post_entries(const sapi_post_entry *post_entry);
193 SAPI_API int sapi_register_post_entry(const sapi_post_entry *post_entry);
194 SAPI_API void sapi_unregister_post_entry(const sapi_post_entry *post_entry);
195 SAPI_API int sapi_register_default_post_reader(void (*default_post_reader)(void));
196 SAPI_API int sapi_register_treat_data(void (*treat_data)(int arg, char *str, zval *destArray));
197 SAPI_API int sapi_register_input_filter(unsigned int (*input_filter)(int arg, char *var, char **val, size_t val_len, size_t *new_val_len), unsigned int (*input_filter_init)(void));
198 
199 SAPI_API int sapi_flush(void);
200 SAPI_API zend_stat_t *sapi_get_stat(void);
201 SAPI_API char *sapi_getenv(char *name, size_t name_len);
202 
203 SAPI_API char *sapi_get_default_content_type(void);
204 SAPI_API void sapi_get_default_content_type_header(sapi_header_struct *default_header);
205 SAPI_API size_t sapi_apply_default_charset(char **mimetype, size_t len);
206 SAPI_API void sapi_activate_headers_only(void);
207 
208 SAPI_API int sapi_get_fd(int *fd);
209 SAPI_API int sapi_force_http_10(void);
210 
211 SAPI_API int sapi_get_target_uid(uid_t *);
212 SAPI_API int sapi_get_target_gid(gid_t *);
213 SAPI_API double sapi_get_request_time(void);
214 SAPI_API void sapi_terminate_process(void);
215 END_EXTERN_C()
216 
217 struct _sapi_module_struct {
218 	char *name;
219 	char *pretty_name;
220 
221 	int (*startup)(struct _sapi_module_struct *sapi_module);
222 	int (*shutdown)(struct _sapi_module_struct *sapi_module);
223 
224 	int (*activate)(void);
225 	int (*deactivate)(void);
226 
227 	size_t (*ub_write)(const char *str, size_t str_length);
228 	void (*flush)(void *server_context);
229 	zend_stat_t *(*get_stat)(void);
230 	char *(*getenv)(char *name, size_t name_len);
231 
232 	void (*sapi_error)(int type, const char *error_msg, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
233 
234 	int (*header_handler)(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers);
235 	int (*send_headers)(sapi_headers_struct *sapi_headers);
236 	void (*send_header)(sapi_header_struct *sapi_header, void *server_context);
237 
238 	size_t (*read_post)(char *buffer, size_t count_bytes);
239 	char *(*read_cookies)(void);
240 
241 	void (*register_server_variables)(zval *track_vars_array);
242 	void (*log_message)(char *message, int syslog_type_int);
243 	double (*get_request_time)(void);
244 	void (*terminate_process)(void);
245 
246 	char *php_ini_path_override;
247 
248 	void (*default_post_reader)(void);
249 	void (*treat_data)(int arg, char *str, zval *destArray);
250 	char *executable_location;
251 
252 	int php_ini_ignore;
253 	int php_ini_ignore_cwd; /* don't look for php.ini in the current directory */
254 
255 	int (*get_fd)(int *fd);
256 
257 	int (*force_http_10)(void);
258 
259 	int (*get_target_uid)(uid_t *);
260 	int (*get_target_gid)(gid_t *);
261 
262 	unsigned int (*input_filter)(int arg, char *var, char **val, size_t val_len, size_t *new_val_len);
263 
264 	void (*ini_defaults)(HashTable *configuration_hash);
265 	int phpinfo_as_text;
266 
267 	char *ini_entries;
268 	const zend_function_entry *additional_functions;
269 	unsigned int (*input_filter_init)(void);
270 };
271 
272 struct _sapi_post_entry {
273 	char *content_type;
274 	uint32_t content_type_len;
275 	void (*post_reader)(void);
276 	void (*post_handler)(char *content_type_dup, void *arg);
277 };
278 
279 /* header_handler() constants */
280 #define SAPI_HEADER_ADD			(1<<0)
281 
282 
283 #define SAPI_HEADER_SENT_SUCCESSFULLY	1
284 #define SAPI_HEADER_DO_SEND				2
285 #define SAPI_HEADER_SEND_FAILED			3
286 
287 #define SAPI_DEFAULT_MIMETYPE		"text/html"
288 #define SAPI_DEFAULT_CHARSET		PHP_DEFAULT_CHARSET
289 #define SAPI_PHP_VERSION_HEADER		"X-Powered-By: PHP/" PHP_VERSION
290 
291 #define SAPI_POST_READER_FUNC(post_reader) void post_reader(void)
292 #define SAPI_POST_HANDLER_FUNC(post_handler) void post_handler(char *content_type_dup, void *arg)
293 
294 #define SAPI_TREAT_DATA_FUNC(treat_data) void treat_data(int arg, char *str, zval* destArray)
295 #define SAPI_INPUT_FILTER_FUNC(input_filter) unsigned int input_filter(int arg, char *var, char **val, size_t val_len, size_t *new_val_len)
296 
297 BEGIN_EXTERN_C()
298 SAPI_API SAPI_POST_READER_FUNC(sapi_read_standard_form_data);
299 SAPI_API SAPI_POST_READER_FUNC(php_default_post_reader);
300 SAPI_API SAPI_TREAT_DATA_FUNC(php_default_treat_data);
301 SAPI_API SAPI_INPUT_FILTER_FUNC(php_default_input_filter);
302 END_EXTERN_C()
303 
304 #define STANDARD_SAPI_MODULE_PROPERTIES \
305 	NULL, /* php_ini_path_override   */ \
306 	NULL, /* default_post_reader     */ \
307 	NULL, /* treat_data              */ \
308 	NULL, /* executable_location     */ \
309 	0,    /* php_ini_ignore          */ \
310 	0,    /* php_ini_ignore_cwd      */ \
311 	NULL, /* get_fd                  */ \
312 	NULL, /* force_http_10           */ \
313 	NULL, /* get_target_uid          */ \
314 	NULL, /* get_target_gid          */ \
315 	NULL, /* input_filter            */ \
316 	NULL, /* ini_defaults            */ \
317 	0,    /* phpinfo_as_text;        */ \
318 	NULL, /* ini_entries;            */ \
319 	NULL, /* additional_functions    */ \
320 	NULL  /* input_filter_init       */
321 
322 #endif /* SAPI_H */
323 
324 /*
325  * Local variables:
326  * tab-width: 4
327  * c-basic-offset: 4
328  * End:
329  * vim600: sw=4 ts=4 fdm=marker
330  * vim<600: sw=4 ts=4
331  */
332