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