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