xref: /PHP-5.3/main/SAPI.h (revision a2045ff3)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2013 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 "zend.h"
25 #include "zend_API.h"
26 #include "zend_llist.h"
27 #include "zend_operators.h"
28 #ifdef PHP_WIN32
29 #include "win95nt.h"
30 #endif
31 #include <sys/stat.h>
32 
33 #define SAPI_OPTION_NO_CHDIR 1
34 
35 #define SAPI_POST_BLOCK_SIZE 4000
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 	uint 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 *post_data, *raw_post_data;
83 	char *cookie_data;
84 	long content_length;
85 	uint post_data_length, raw_post_data_length;
86 
87 	char *path_translated;
88 	char *request_uri;
89 
90 	const char *content_type;
91 
92 	zend_bool headers_only;
93 	zend_bool no_headers;
94 	zend_bool headers_read;
95 
96 	sapi_post_entry *post_entry;
97 
98 	char *content_type_dup;
99 
100 	/* for HTTP authentication */
101 	char *auth_user;
102 	char *auth_password;
103 	char *auth_digest;
104 
105 	/* this is necessary for the CGI SAPI module */
106 	char *argv0;
107 
108 	char *current_user;
109 	int current_user_length;
110 
111 	/* this is necessary for CLI module */
112 	int argc;
113 	char **argv;
114 	int proto_num;
115 } sapi_request_info;
116 
117 
118 typedef struct _sapi_globals_struct {
119 	void *server_context;
120 	sapi_request_info request_info;
121 	sapi_headers_struct sapi_headers;
122 	int read_post_bytes;
123 	unsigned char headers_sent;
124 	struct stat global_stat;
125 	char *default_mimetype;
126 	char *default_charset;
127 	HashTable *rfc1867_uploaded_files;
128 	long post_max_size;
129 	int options;
130 	zend_bool sapi_started;
131 	time_t global_request_time;
132 	HashTable known_post_content_types;
133 } sapi_globals_struct;
134 
135 
136 BEGIN_EXTERN_C()
137 #ifdef ZTS
138 # define SG(v) TSRMG(sapi_globals_id, sapi_globals_struct *, v)
139 SAPI_API extern int sapi_globals_id;
140 #else
141 # define SG(v) (sapi_globals.v)
142 extern SAPI_API sapi_globals_struct sapi_globals;
143 #endif
144 
145 SAPI_API void sapi_startup(sapi_module_struct *sf);
146 SAPI_API void sapi_shutdown(void);
147 SAPI_API void sapi_activate(TSRMLS_D);
148 SAPI_API void sapi_deactivate(TSRMLS_D);
149 SAPI_API void sapi_initialize_empty_request(TSRMLS_D);
150 END_EXTERN_C()
151 
152 /*
153  * This is the preferred and maintained API for
154  * operating on HTTP headers.
155  */
156 
157 /*
158  * Always specify a sapi_header_line this way:
159  *
160  *     sapi_header_line ctr = {0};
161  */
162 
163 typedef struct {
164 	char *line; /* If you allocated this, you need to free it yourself */
165 	uint line_len;
166 	long response_code; /* long due to zend_parse_parameters compatibility */
167 } sapi_header_line;
168 
169 typedef enum {					/* Parameter: 			*/
170 	SAPI_HEADER_REPLACE,		/* sapi_header_line* 	*/
171 	SAPI_HEADER_ADD,			/* sapi_header_line* 	*/
172 	SAPI_HEADER_DELETE,			/* sapi_header_line* 	*/
173 	SAPI_HEADER_DELETE_ALL,		/* void					*/
174 	SAPI_HEADER_SET_STATUS		/* int 					*/
175 } sapi_header_op_enum;
176 
177 BEGIN_EXTERN_C()
178 SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg TSRMLS_DC);
179 
180 /* Deprecated functions. Use sapi_header_op instead. */
181 SAPI_API int sapi_add_header_ex(char *header_line, uint header_line_len, zend_bool duplicate, zend_bool replace TSRMLS_DC);
182 #define sapi_add_header(a, b, c) sapi_add_header_ex((a),(b),(c),1 TSRMLS_CC)
183 
184 
185 SAPI_API int sapi_send_headers(TSRMLS_D);
186 SAPI_API void sapi_free_header(sapi_header_struct *sapi_header);
187 SAPI_API void sapi_handle_post(void *arg TSRMLS_DC);
188 
189 SAPI_API int sapi_register_post_entries(sapi_post_entry *post_entry TSRMLS_DC);
190 SAPI_API int sapi_register_post_entry(sapi_post_entry *post_entry TSRMLS_DC);
191 SAPI_API void sapi_unregister_post_entry(sapi_post_entry *post_entry TSRMLS_DC);
192 SAPI_API int sapi_register_default_post_reader(void (*default_post_reader)(TSRMLS_D));
193 SAPI_API int sapi_register_treat_data(void (*treat_data)(int arg, char *str, zval *destArray TSRMLS_DC));
194 SAPI_API int sapi_register_input_filter(unsigned int (*input_filter)(int arg, char *var, char **val, unsigned int val_len, unsigned int *new_val_len TSRMLS_DC), unsigned int (*input_filter_init)(TSRMLS_D));
195 
196 SAPI_API int sapi_flush(TSRMLS_D);
197 SAPI_API struct stat *sapi_get_stat(TSRMLS_D);
198 SAPI_API char *sapi_getenv(char *name, size_t name_len TSRMLS_DC);
199 
200 SAPI_API char *sapi_get_default_content_type(TSRMLS_D);
201 SAPI_API void sapi_get_default_content_type_header(sapi_header_struct *default_header TSRMLS_DC);
202 SAPI_API size_t sapi_apply_default_charset(char **mimetype, size_t len TSRMLS_DC);
203 SAPI_API void sapi_activate_headers_only(TSRMLS_D);
204 
205 SAPI_API int sapi_get_fd(int *fd TSRMLS_DC);
206 SAPI_API int sapi_force_http_10(TSRMLS_D);
207 
208 SAPI_API int sapi_get_target_uid(uid_t * TSRMLS_DC);
209 SAPI_API int sapi_get_target_gid(gid_t * TSRMLS_DC);
210 SAPI_API time_t sapi_get_request_time(TSRMLS_D);
211 SAPI_API void sapi_terminate_process(TSRMLS_D);
212 END_EXTERN_C()
213 
214 struct _sapi_module_struct {
215 	char *name;
216 	char *pretty_name;
217 
218 	int (*startup)(struct _sapi_module_struct *sapi_module);
219 	int (*shutdown)(struct _sapi_module_struct *sapi_module);
220 
221 	int (*activate)(TSRMLS_D);
222 	int (*deactivate)(TSRMLS_D);
223 
224 	int (*ub_write)(const char *str, unsigned int str_length TSRMLS_DC);
225 	void (*flush)(void *server_context);
226 	struct stat *(*get_stat)(TSRMLS_D);
227 	char *(*getenv)(char *name, size_t name_len TSRMLS_DC);
228 
229 	void (*sapi_error)(int type, const char *error_msg, ...);
230 
231 	int (*header_handler)(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers TSRMLS_DC);
232 	int (*send_headers)(sapi_headers_struct *sapi_headers TSRMLS_DC);
233 	void (*send_header)(sapi_header_struct *sapi_header, void *server_context TSRMLS_DC);
234 
235 	int (*read_post)(char *buffer, uint count_bytes TSRMLS_DC);
236 	char *(*read_cookies)(TSRMLS_D);
237 
238 	void (*register_server_variables)(zval *track_vars_array TSRMLS_DC);
239 	void (*log_message)(char *message);
240 	time_t (*get_request_time)(TSRMLS_D);
241 	void (*terminate_process)(TSRMLS_D);
242 
243 	char *php_ini_path_override;
244 
245 	void (*block_interruptions)(void);
246 	void (*unblock_interruptions)(void);
247 
248 	void (*default_post_reader)(TSRMLS_D);
249 	void (*treat_data)(int arg, char *str, zval *destArray TSRMLS_DC);
250 	char *executable_location;
251 
252 	int php_ini_ignore;
253 
254 	int (*get_fd)(int *fd TSRMLS_DC);
255 
256 	int (*force_http_10)(TSRMLS_D);
257 
258 	int (*get_target_uid)(uid_t * TSRMLS_DC);
259 	int (*get_target_gid)(gid_t * TSRMLS_DC);
260 
261 	unsigned int (*input_filter)(int arg, char *var, char **val, unsigned int val_len, unsigned int *new_val_len TSRMLS_DC);
262 
263 	void (*ini_defaults)(HashTable *configuration_hash);
264 	int phpinfo_as_text;
265 
266 	char *ini_entries;
267 	const zend_function_entry *additional_functions;
268 	unsigned int (*input_filter_init)(TSRMLS_D);
269 };
270 
271 
272 struct _sapi_post_entry {
273 	char *content_type;
274 	uint content_type_len;
275 	void (*post_reader)(TSRMLS_D);
276 	void (*post_handler)(char *content_type_dup, void *arg TSRMLS_DC);
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		""
289 #define SAPI_PHP_VERSION_HEADER		"X-Powered-By: PHP/" PHP_VERSION
290 
291 #define SAPI_POST_READER_FUNC(post_reader) void post_reader(TSRMLS_D)
292 #define SAPI_POST_HANDLER_FUNC(post_handler) void post_handler(char *content_type_dup, void *arg TSRMLS_DC)
293 
294 #define SAPI_TREAT_DATA_FUNC(treat_data) void treat_data(int arg, char *str, zval* destArray TSRMLS_DC)
295 #define SAPI_INPUT_FILTER_FUNC(input_filter) unsigned int input_filter(int arg, char *var, char **val, unsigned int val_len, unsigned int *new_val_len TSRMLS_DC)
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 
306 #endif /* SAPI_H */
307 
308 /*
309  * Local variables:
310  * tab-width: 4
311  * c-basic-offset: 4
312  * End:
313  */
314