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