Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Original design: Shane Caraveo <shane@caraveo.com> | |
12 | | | Authors: Andi Gutmans <andi@php.net> | |
13 | | | Zeev Suraski <zeev@php.net> | |
14 | | +----------------------------------------------------------------------+ |
15 | | */ |
16 | | |
17 | | #include <ctype.h> |
18 | | #include <sys/stat.h> |
19 | | #include <locale.h> |
20 | | |
21 | | #include "php.h" |
22 | | #include "SAPI.h" |
23 | | #include "php_variables.h" |
24 | | #include "php_ini.h" |
25 | | #ifdef ZTS |
26 | | #include "TSRM.h" |
27 | | #endif |
28 | | #ifdef HAVE_SYS_TIME_H |
29 | | #include <sys/time.h> |
30 | | #elif defined(PHP_WIN32) |
31 | | #include "win32/time.h" |
32 | | #endif |
33 | | |
34 | | #include "rfc1867.h" |
35 | | |
36 | | #include "php_content_types.h" |
37 | | |
38 | | #ifdef ZTS |
39 | | SAPI_API int sapi_globals_id; |
40 | | SAPI_API size_t sapi_globals_offset; |
41 | | #else |
42 | | sapi_globals_struct sapi_globals; |
43 | | #endif |
44 | | |
45 | | static void _type_dtor(zval *zv) |
46 | 0 | { |
47 | 0 | free(Z_PTR_P(zv)); |
48 | 0 | } |
49 | | |
50 | | static void sapi_globals_ctor(sapi_globals_struct *sapi_globals) |
51 | 2 | { |
52 | 2 | memset(sapi_globals, 0, sizeof(*sapi_globals)); |
53 | 2 | zend_hash_init(&sapi_globals->known_post_content_types, 8, NULL, _type_dtor, 1); |
54 | 2 | php_setup_sapi_content_types(); |
55 | 2 | } |
56 | | |
57 | | static void sapi_globals_dtor(sapi_globals_struct *sapi_globals) |
58 | 0 | { |
59 | 0 | zend_hash_destroy(&sapi_globals->known_post_content_types); |
60 | 0 | } |
61 | | |
62 | | /* True globals (no need for thread safety) */ |
63 | | SAPI_API sapi_module_struct sapi_module; |
64 | | |
65 | | |
66 | | SAPI_API void sapi_startup(sapi_module_struct *sf) |
67 | 2 | { |
68 | 2 | sf->ini_entries = NULL; |
69 | 2 | sapi_module = *sf; |
70 | | |
71 | | #ifdef ZTS |
72 | | ts_allocate_fast_id(&sapi_globals_id, &sapi_globals_offset, sizeof(sapi_globals_struct), (ts_allocate_ctor) sapi_globals_ctor, (ts_allocate_dtor) sapi_globals_dtor); |
73 | | # ifdef PHP_WIN32 |
74 | | _configthreadlocale(_ENABLE_PER_THREAD_LOCALE); |
75 | | # endif |
76 | | #else |
77 | 2 | sapi_globals_ctor(&sapi_globals); |
78 | 2 | #endif |
79 | | |
80 | | #ifdef PHP_WIN32 |
81 | | tsrm_win32_startup(); |
82 | | #endif |
83 | | |
84 | 2 | reentrancy_startup(); |
85 | 2 | } |
86 | | |
87 | | SAPI_API void sapi_shutdown(void) |
88 | 0 | { |
89 | | #ifdef ZTS |
90 | | ts_free_id(sapi_globals_id); |
91 | | #else |
92 | 0 | sapi_globals_dtor(&sapi_globals); |
93 | 0 | #endif |
94 | |
|
95 | 0 | reentrancy_shutdown(); |
96 | |
|
97 | | #ifdef PHP_WIN32 |
98 | | tsrm_win32_shutdown(); |
99 | | #endif |
100 | 0 | } |
101 | | |
102 | | |
103 | | SAPI_API void sapi_free_header(sapi_header_struct *sapi_header) |
104 | 77.7k | { |
105 | 77.7k | efree(sapi_header->header); |
106 | 77.7k | } |
107 | | |
108 | | /* {{{ call a header function */ |
109 | | PHP_FUNCTION(header_register_callback) |
110 | 0 | { |
111 | 0 | zend_fcall_info fci; |
112 | 0 | zend_fcall_info_cache fcc; |
113 | |
|
114 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "f", &fci, &fcc) == FAILURE) { |
115 | 0 | RETURN_THROWS(); |
116 | 0 | } |
117 | | |
118 | 0 | if (Z_TYPE(SG(callback_func)) != IS_UNDEF) { |
119 | 0 | zval_ptr_dtor(&SG(callback_func)); |
120 | 0 | SG(fci_cache) = empty_fcall_info_cache; |
121 | 0 | } |
122 | | |
123 | | /* Don't store callback if headers have already been sent: |
124 | | * It won't get used and we won't have a chance to release it. */ |
125 | 0 | if (!SG(headers_sent)) { |
126 | 0 | ZVAL_COPY(&SG(callback_func), &fci.function_name); |
127 | 0 | } |
128 | |
|
129 | 0 | RETURN_TRUE; |
130 | 0 | } |
131 | | /* }}} */ |
132 | | |
133 | | static void sapi_run_header_callback(zval *callback) |
134 | 0 | { |
135 | 0 | int error; |
136 | 0 | zend_fcall_info fci; |
137 | 0 | char *callback_error = NULL; |
138 | 0 | zval retval; |
139 | |
|
140 | 0 | if (zend_fcall_info_init(callback, 0, &fci, &SG(fci_cache), NULL, &callback_error) == SUCCESS) { |
141 | 0 | fci.retval = &retval; |
142 | |
|
143 | 0 | error = zend_call_function(&fci, &SG(fci_cache)); |
144 | 0 | if (error == FAILURE) { |
145 | 0 | goto callback_failed; |
146 | 0 | } else { |
147 | 0 | zval_ptr_dtor(&retval); |
148 | 0 | } |
149 | 0 | } else { |
150 | 0 | callback_failed: |
151 | 0 | php_error_docref(NULL, E_WARNING, "Could not call the sapi_header_callback"); |
152 | 0 | } |
153 | | |
154 | 0 | if (callback_error) { |
155 | 0 | efree(callback_error); |
156 | 0 | } |
157 | 0 | } |
158 | | |
159 | | SAPI_API void sapi_handle_post(void *arg) |
160 | 0 | { |
161 | 0 | if (SG(request_info).post_entry && SG(request_info).content_type_dup) { |
162 | 0 | SG(request_info).post_entry->post_handler(SG(request_info).content_type_dup, arg); |
163 | 0 | efree(SG(request_info).content_type_dup); |
164 | 0 | SG(request_info).content_type_dup = NULL; |
165 | 0 | } |
166 | 0 | } |
167 | | |
168 | | SAPI_API void sapi_read_post_data(void) |
169 | 0 | { |
170 | 0 | sapi_post_entry *post_entry; |
171 | 0 | uint32_t content_type_length = (uint32_t)strlen(SG(request_info).content_type); |
172 | 0 | char *content_type = estrndup(SG(request_info).content_type, content_type_length); |
173 | 0 | char *p; |
174 | 0 | char oldchar=0; |
175 | 0 | void (*post_reader_func)(void) = NULL; |
176 | | |
177 | | |
178 | | /* dedicated implementation for increased performance: |
179 | | * - Make the content type lowercase |
180 | | * - Trim descriptive data, stay with the content-type only |
181 | | */ |
182 | 0 | for (p = content_type; p < content_type + content_type_length; p++) { |
183 | 0 | switch (*p) { |
184 | 0 | case ';': |
185 | 0 | case ',': |
186 | 0 | case ' ': |
187 | 0 | content_type_length = p-content_type; |
188 | 0 | oldchar = *p; |
189 | 0 | *p = 0; |
190 | 0 | break; |
191 | 0 | default: |
192 | 0 | *p = tolower((unsigned char)*p); |
193 | 0 | break; |
194 | 0 | } |
195 | 0 | } |
196 | | |
197 | | /* now try to find an appropriate POST content handler */ |
198 | 0 | if ((post_entry = zend_hash_str_find_ptr(&SG(known_post_content_types), content_type, |
199 | 0 | content_type_length)) != NULL) { |
200 | | /* found one, register it for use */ |
201 | 0 | SG(request_info).post_entry = post_entry; |
202 | 0 | post_reader_func = post_entry->post_reader; |
203 | 0 | } else { |
204 | | /* fallback */ |
205 | 0 | SG(request_info).post_entry = NULL; |
206 | 0 | if (UNEXPECTED(!sapi_module.default_post_reader)) { |
207 | | /* this should not happen as there should always be a default_post_reader */ |
208 | 0 | SG(request_info).content_type_dup = NULL; |
209 | 0 | sapi_module.sapi_error(E_WARNING, "Unsupported content type: '%s'", content_type); |
210 | 0 | efree(content_type); |
211 | 0 | return; |
212 | 0 | } |
213 | 0 | } |
214 | 0 | if (oldchar) { |
215 | 0 | *(p-1) = oldchar; |
216 | 0 | } |
217 | | |
218 | | /* the content_type_dup is not set at this stage so no need to try to free it first */ |
219 | 0 | SG(request_info).content_type_dup = content_type; |
220 | |
|
221 | 0 | if(post_reader_func) { |
222 | 0 | post_reader_func(); |
223 | 0 | } |
224 | |
|
225 | 0 | if(sapi_module.default_post_reader) { |
226 | 0 | sapi_module.default_post_reader(); |
227 | 0 | } |
228 | 0 | } |
229 | | |
230 | | SAPI_API size_t sapi_read_post_block(char *buffer, size_t buflen) |
231 | 0 | { |
232 | 0 | size_t read_bytes; |
233 | |
|
234 | 0 | if (!sapi_module.read_post) { |
235 | 0 | return 0; |
236 | 0 | } |
237 | | |
238 | 0 | read_bytes = sapi_module.read_post(buffer, buflen); |
239 | |
|
240 | 0 | if (read_bytes > 0) { |
241 | | /* gogo */ |
242 | 0 | SG(read_post_bytes) += read_bytes; |
243 | 0 | } |
244 | 0 | if (read_bytes < buflen) { |
245 | | /* done */ |
246 | 0 | SG(post_read) = 1; |
247 | 0 | } |
248 | |
|
249 | 0 | return read_bytes; |
250 | 0 | } |
251 | | |
252 | | SAPI_API SAPI_POST_READER_FUNC(sapi_read_standard_form_data) |
253 | 0 | { |
254 | 0 | zend_long post_max_size = REQUEST_PARSE_BODY_OPTION_GET(post_max_size, SG(post_max_size)); |
255 | |
|
256 | 0 | if (post_max_size > 0 && SG(request_info).content_length > post_max_size) { |
257 | 0 | php_error_docref(NULL, E_WARNING, "POST Content-Length of " ZEND_LONG_FMT " bytes exceeds the limit of " ZEND_LONG_FMT " bytes", |
258 | 0 | SG(request_info).content_length, post_max_size); |
259 | 0 | return; |
260 | 0 | } |
261 | | |
262 | | |
263 | 0 | SG(request_info).request_body = php_stream_temp_create_ex(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE, PG(upload_tmp_dir)); |
264 | |
|
265 | 0 | if (sapi_module.read_post) { |
266 | 0 | size_t read_bytes; |
267 | |
|
268 | 0 | for (;;) { |
269 | 0 | char buffer[SAPI_POST_BLOCK_SIZE]; |
270 | |
|
271 | 0 | read_bytes = sapi_read_post_block(buffer, SAPI_POST_BLOCK_SIZE); |
272 | |
|
273 | 0 | if (read_bytes > 0) { |
274 | 0 | if (php_stream_write(SG(request_info).request_body, buffer, read_bytes) != read_bytes) { |
275 | | /* if parts of the stream can't be written, purge it completely */ |
276 | 0 | php_stream_truncate_set_size(SG(request_info).request_body, 0); |
277 | 0 | php_error_docref(NULL, E_WARNING, "POST data can't be buffered; all data discarded"); |
278 | 0 | break; |
279 | 0 | } |
280 | 0 | } |
281 | | |
282 | 0 | if (post_max_size > 0 && SG(read_post_bytes) > post_max_size) { |
283 | 0 | php_error_docref(NULL, E_WARNING, "Actual POST length does not match Content-Length, and exceeds " ZEND_LONG_FMT " bytes", post_max_size); |
284 | 0 | break; |
285 | 0 | } |
286 | | |
287 | 0 | if (read_bytes < SAPI_POST_BLOCK_SIZE) { |
288 | | /* done */ |
289 | 0 | break; |
290 | 0 | } |
291 | 0 | } |
292 | 0 | php_stream_rewind(SG(request_info).request_body); |
293 | 0 | } |
294 | 0 | } |
295 | | |
296 | | |
297 | | static inline char *get_default_content_type(uint32_t prefix_len, uint32_t *len) |
298 | 38.8k | { |
299 | 38.8k | char *mimetype, *charset, *content_type; |
300 | 38.8k | uint32_t mimetype_len, charset_len; |
301 | | |
302 | 38.8k | if (SG(default_mimetype)) { |
303 | 38.8k | mimetype = SG(default_mimetype); |
304 | 38.8k | mimetype_len = (uint32_t)strlen(SG(default_mimetype)); |
305 | 38.8k | } else { |
306 | 0 | mimetype = SAPI_DEFAULT_MIMETYPE; |
307 | 0 | mimetype_len = sizeof(SAPI_DEFAULT_MIMETYPE) - 1; |
308 | 0 | } |
309 | 38.8k | if (SG(default_charset)) { |
310 | 38.8k | charset = SG(default_charset); |
311 | 38.8k | charset_len = (uint32_t)strlen(SG(default_charset)); |
312 | 38.8k | } else { |
313 | 0 | charset = SAPI_DEFAULT_CHARSET; |
314 | 0 | charset_len = sizeof(SAPI_DEFAULT_CHARSET) - 1; |
315 | 0 | } |
316 | | |
317 | 38.8k | if (*charset && strncasecmp(mimetype, "text/", 5) == 0) { |
318 | 38.8k | char *p; |
319 | | |
320 | 38.8k | *len = prefix_len + mimetype_len + sizeof("; charset=") - 1 + charset_len; |
321 | 38.8k | content_type = (char*)emalloc(*len + 1); |
322 | 38.8k | p = content_type + prefix_len; |
323 | 38.8k | p = zend_mempcpy(p, mimetype, mimetype_len); |
324 | 38.8k | p = zend_mempcpy(p, "; charset=", sizeof("; charset=") - 1); |
325 | 38.8k | memcpy(p, charset, charset_len + 1); |
326 | 38.8k | } else { |
327 | 0 | *len = prefix_len + mimetype_len; |
328 | 0 | content_type = (char*)emalloc(*len + 1); |
329 | 0 | memcpy(content_type + prefix_len, mimetype, mimetype_len + 1); |
330 | 0 | } |
331 | 38.8k | return content_type; |
332 | 38.8k | } |
333 | | |
334 | | |
335 | | SAPI_API char *sapi_get_default_content_type(void) |
336 | 0 | { |
337 | 0 | uint32_t len; |
338 | |
|
339 | 0 | return get_default_content_type(0, &len); |
340 | 0 | } |
341 | | |
342 | | |
343 | | SAPI_API void sapi_get_default_content_type_header(sapi_header_struct *default_header) |
344 | 38.8k | { |
345 | 38.8k | uint32_t len; |
346 | | |
347 | 38.8k | default_header->header = get_default_content_type(sizeof("Content-type: ")-1, &len); |
348 | 38.8k | default_header->header_len = len; |
349 | 38.8k | memcpy(default_header->header, "Content-type: ", sizeof("Content-type: ") - 1); |
350 | 38.8k | } |
351 | | |
352 | | /* |
353 | | * Add charset on content-type header if the MIME type starts with |
354 | | * "text/", the default_charset directive is not empty and |
355 | | * there is not already a charset option in there. |
356 | | * |
357 | | * If "mimetype" is non-NULL, it should point to a pointer allocated |
358 | | * with emalloc(). If a charset is added, the string will be |
359 | | * re-allocated and the new length is returned. If mimetype is |
360 | | * unchanged, 0 is returned. |
361 | | * |
362 | | */ |
363 | | SAPI_API size_t sapi_apply_default_charset(char **mimetype, size_t len) |
364 | 0 | { |
365 | 0 | char *charset, *newtype; |
366 | 0 | size_t newlen; |
367 | 0 | charset = SG(default_charset) ? SG(default_charset) : SAPI_DEFAULT_CHARSET; |
368 | |
|
369 | 0 | if (*mimetype != NULL) { |
370 | 0 | if (*charset && strncmp(*mimetype, "text/", 5) == 0 && strstr(*mimetype, "charset=") == NULL) { |
371 | 0 | newlen = len + (sizeof(";charset=")-1) + strlen(charset); |
372 | 0 | newtype = emalloc(newlen + 1); |
373 | 0 | PHP_STRLCPY(newtype, *mimetype, newlen + 1, len); |
374 | 0 | strlcat(newtype, ";charset=", newlen + 1); |
375 | 0 | strlcat(newtype, charset, newlen + 1); |
376 | 0 | efree(*mimetype); |
377 | 0 | *mimetype = newtype; |
378 | 0 | return newlen; |
379 | 0 | } |
380 | 0 | } |
381 | 0 | return 0; |
382 | 0 | } |
383 | | |
384 | | SAPI_API void sapi_activate_headers_only(void) |
385 | 0 | { |
386 | 0 | if (SG(request_info).headers_read == 1) |
387 | 0 | return; |
388 | 0 | SG(request_info).headers_read = 1; |
389 | 0 | zend_llist_init(&SG(sapi_headers).headers, sizeof(sapi_header_struct), |
390 | 0 | (void (*)(void *)) sapi_free_header, 0); |
391 | 0 | SG(sapi_headers).send_default_content_type = 1; |
392 | | |
393 | | /* SG(sapi_headers).http_response_code = 200; */ |
394 | 0 | SG(sapi_headers).http_status_line = NULL; |
395 | 0 | SG(sapi_headers).mimetype = NULL; |
396 | 0 | SG(read_post_bytes) = 0; |
397 | 0 | SG(request_info).request_body = NULL; |
398 | 0 | SG(request_info).current_user = NULL; |
399 | 0 | SG(request_info).current_user_length = 0; |
400 | 0 | SG(request_info).no_headers = 0; |
401 | 0 | SG(request_info).post_entry = NULL; |
402 | 0 | SG(global_request_time) = 0; |
403 | | |
404 | | /* |
405 | | * It's possible to override this general case in the activate() callback, |
406 | | * if necessary. |
407 | | */ |
408 | 0 | if (SG(request_info).request_method && !strcmp(SG(request_info).request_method, "HEAD")) { |
409 | 0 | SG(request_info).headers_only = 1; |
410 | 0 | } else { |
411 | 0 | SG(request_info).headers_only = 0; |
412 | 0 | } |
413 | 0 | if (SG(server_context)) { |
414 | 0 | SG(request_info).cookie_data = sapi_module.read_cookies(); |
415 | 0 | if (sapi_module.activate) { |
416 | 0 | sapi_module.activate(); |
417 | 0 | } |
418 | 0 | } |
419 | 0 | if (sapi_module.input_filter_init ) { |
420 | 0 | sapi_module.input_filter_init(); |
421 | 0 | } |
422 | 0 | } |
423 | | |
424 | | /* |
425 | | * Called from php_request_startup() for every request. |
426 | | */ |
427 | | |
428 | | SAPI_API void sapi_activate(void) |
429 | 38.8k | { |
430 | 38.8k | zend_llist_init(&SG(sapi_headers).headers, sizeof(sapi_header_struct), (void (*)(void *)) sapi_free_header, 0); |
431 | 38.8k | SG(sapi_headers).send_default_content_type = 1; |
432 | | |
433 | | /* |
434 | | SG(sapi_headers).http_response_code = 200; |
435 | | */ |
436 | 38.8k | SG(sapi_headers).http_status_line = NULL; |
437 | 38.8k | SG(sapi_headers).mimetype = NULL; |
438 | 38.8k | SG(headers_sent) = 0; |
439 | 38.8k | ZVAL_UNDEF(&SG(callback_func)); |
440 | 38.8k | SG(read_post_bytes) = 0; |
441 | 38.8k | SG(request_info).request_body = NULL; |
442 | 38.8k | SG(request_info).current_user = NULL; |
443 | 38.8k | SG(request_info).current_user_length = 0; |
444 | 38.8k | SG(request_info).no_headers = 0; |
445 | 38.8k | SG(request_info).post_entry = NULL; |
446 | 38.8k | SG(request_info).proto_num = 1000; /* Default to HTTP 1.0 */ |
447 | 38.8k | SG(global_request_time) = 0; |
448 | 38.8k | SG(post_read) = 0; |
449 | | /* It's possible to override this general case in the activate() callback, if necessary. */ |
450 | 38.8k | if (SG(request_info).request_method && !strcmp(SG(request_info).request_method, "HEAD")) { |
451 | 0 | SG(request_info).headers_only = 1; |
452 | 38.8k | } else { |
453 | 38.8k | SG(request_info).headers_only = 0; |
454 | 38.8k | } |
455 | 38.8k | SG(rfc1867_uploaded_files) = NULL; |
456 | 38.8k | SG(request_parse_body_context).throw_exceptions = false; |
457 | 38.8k | memset(&SG(request_parse_body_context).options_cache, 0, sizeof(SG(request_parse_body_context).options_cache)); |
458 | | |
459 | 38.8k | if (sapi_module.pre_request_init) { |
460 | 0 | sapi_module.pre_request_init(); |
461 | 0 | } |
462 | | |
463 | | /* Handle request method */ |
464 | 38.8k | if (SG(server_context)) { |
465 | 0 | if (PG(enable_post_data_reading) |
466 | 0 | && SG(request_info).content_type |
467 | 0 | && SG(request_info).request_method |
468 | 0 | && !strcmp(SG(request_info).request_method, "POST")) { |
469 | | /* HTTP POST may contain form data to be processed into variables |
470 | | * depending on given content type */ |
471 | 0 | sapi_read_post_data(); |
472 | 0 | } else { |
473 | 0 | SG(request_info).content_type_dup = NULL; |
474 | 0 | } |
475 | | |
476 | | /* Cookies */ |
477 | 0 | SG(request_info).cookie_data = sapi_module.read_cookies(); |
478 | 0 | } |
479 | 38.8k | if (sapi_module.activate) { |
480 | 0 | sapi_module.activate(); |
481 | 0 | } |
482 | 38.8k | if (sapi_module.input_filter_init) { |
483 | 0 | sapi_module.input_filter_init(); |
484 | 0 | } |
485 | 38.8k | } |
486 | | |
487 | | |
488 | | static void sapi_send_headers_free(void) |
489 | 77.7k | { |
490 | 77.7k | if (SG(sapi_headers).http_status_line) { |
491 | 0 | efree(SG(sapi_headers).http_status_line); |
492 | 0 | SG(sapi_headers).http_status_line = NULL; |
493 | 0 | } |
494 | 77.7k | } |
495 | | |
496 | | SAPI_API void sapi_deactivate_module(void) |
497 | 38.8k | { |
498 | 38.8k | zend_llist_destroy(&SG(sapi_headers).headers); |
499 | 38.8k | if (SG(request_info).request_body) { |
500 | 0 | SG(request_info).request_body = NULL; |
501 | 38.8k | } else if (SG(server_context)) { |
502 | 0 | if (!SG(post_read)) { |
503 | | /* make sure we've consumed all request input data */ |
504 | 0 | char dummy[SAPI_POST_BLOCK_SIZE]; |
505 | 0 | size_t read_bytes; |
506 | |
|
507 | 0 | do { |
508 | 0 | read_bytes = sapi_read_post_block(dummy, SAPI_POST_BLOCK_SIZE); |
509 | 0 | } while (SAPI_POST_BLOCK_SIZE == read_bytes); |
510 | 0 | } |
511 | 0 | } |
512 | 38.8k | if (SG(request_info).auth_user) { |
513 | 0 | efree(SG(request_info).auth_user); |
514 | 0 | SG(request_info).auth_user = NULL; |
515 | 0 | } |
516 | 38.8k | if (SG(request_info).auth_password) { |
517 | 0 | efree(SG(request_info).auth_password); |
518 | 0 | SG(request_info).auth_password = NULL; |
519 | 0 | } |
520 | 38.8k | if (SG(request_info).auth_digest) { |
521 | 0 | efree(SG(request_info).auth_digest); |
522 | 0 | SG(request_info).auth_digest = NULL; |
523 | 0 | } |
524 | 38.8k | if (SG(request_info).content_type_dup) { |
525 | 0 | efree(SG(request_info).content_type_dup); |
526 | 0 | } |
527 | 38.8k | if (SG(request_info).current_user) { |
528 | 0 | efree(SG(request_info).current_user); |
529 | 0 | } |
530 | 38.8k | if (sapi_module.deactivate) { |
531 | 0 | sapi_module.deactivate(); |
532 | 0 | } |
533 | 38.8k | } |
534 | | |
535 | | SAPI_API void sapi_deactivate_destroy(void) |
536 | 38.8k | { |
537 | 38.8k | if (SG(rfc1867_uploaded_files)) { |
538 | 0 | destroy_uploaded_files_hash(); |
539 | 0 | } |
540 | 38.8k | if (SG(sapi_headers).mimetype) { |
541 | 0 | efree(SG(sapi_headers).mimetype); |
542 | 0 | SG(sapi_headers).mimetype = NULL; |
543 | 0 | } |
544 | 38.8k | sapi_send_headers_free(); |
545 | 38.8k | SG(sapi_started) = 0; |
546 | 38.8k | SG(headers_sent) = 0; |
547 | 38.8k | SG(request_info).headers_read = 0; |
548 | 38.8k | SG(global_request_time) = 0; |
549 | 38.8k | } |
550 | | |
551 | | SAPI_API void sapi_deactivate(void) |
552 | 2 | { |
553 | 2 | sapi_deactivate_module(); |
554 | 2 | sapi_deactivate_destroy(); |
555 | 2 | } |
556 | | |
557 | | |
558 | | SAPI_API void sapi_initialize_empty_request(void) |
559 | 2 | { |
560 | 2 | SG(server_context) = NULL; |
561 | 2 | SG(request_info).request_method = NULL; |
562 | 2 | SG(request_info).auth_digest = SG(request_info).auth_user = SG(request_info).auth_password = NULL; |
563 | 2 | SG(request_info).content_type_dup = NULL; |
564 | 2 | } |
565 | | |
566 | | |
567 | | static int sapi_extract_response_code(const char *header_line) |
568 | 0 | { |
569 | 0 | int code = 200; |
570 | 0 | const char *ptr; |
571 | |
|
572 | 0 | for (ptr = header_line; *ptr; ptr++) { |
573 | 0 | if (*ptr == ' ' && *(ptr + 1) != ' ') { |
574 | 0 | code = atoi(ptr + 1); |
575 | 0 | break; |
576 | 0 | } |
577 | 0 | } |
578 | |
|
579 | 0 | return code; |
580 | 0 | } |
581 | | |
582 | | |
583 | | static void sapi_update_response_code(int ncode) |
584 | 0 | { |
585 | | /* if the status code did not change, we do not want |
586 | | to change the status line, and no need to change the code */ |
587 | 0 | if (SG(sapi_headers).http_response_code == ncode) { |
588 | 0 | return; |
589 | 0 | } |
590 | | |
591 | 0 | if (SG(sapi_headers).http_status_line) { |
592 | 0 | efree(SG(sapi_headers).http_status_line); |
593 | 0 | SG(sapi_headers).http_status_line = NULL; |
594 | 0 | } |
595 | 0 | SG(sapi_headers).http_response_code = ncode; |
596 | 0 | } |
597 | | |
598 | | /* |
599 | | * since zend_llist_del_element only removes one matched item once, |
600 | | * we should remove them manually |
601 | | */ |
602 | | static void sapi_remove_header(zend_llist *l, char *name, size_t len, size_t prefix_len) |
603 | 38.8k | { |
604 | 38.8k | sapi_header_struct *header; |
605 | 38.8k | zend_llist_element *next; |
606 | 38.8k | zend_llist_element *current=l->head; |
607 | | |
608 | 38.8k | while (current) { |
609 | 0 | header = (sapi_header_struct *)(current->data); |
610 | 0 | next = current->next; |
611 | | /* |
612 | | * prefix_len is set for DELETE_PREFIX (used for deleting i.e. |
613 | | * "Set-Cookie: PHPSESSID=", where we need more than just key) |
614 | | * look for the : otherwise |
615 | | */ |
616 | 0 | if (header->header_len > len |
617 | 0 | && (header->header[len] == ':' || (prefix_len && len > prefix_len)) |
618 | 0 | && !strncasecmp(header->header, name, len)) { |
619 | 0 | if (current->prev) { |
620 | 0 | current->prev->next = next; |
621 | 0 | } else { |
622 | 0 | l->head = next; |
623 | 0 | } |
624 | 0 | if (next) { |
625 | 0 | next->prev = current->prev; |
626 | 0 | } else { |
627 | 0 | l->tail = current->prev; |
628 | 0 | } |
629 | 0 | sapi_free_header(header); |
630 | 0 | efree(current); |
631 | 0 | --l->count; |
632 | 0 | } |
633 | 0 | current = next; |
634 | 0 | } |
635 | 38.8k | } |
636 | | |
637 | | SAPI_API int sapi_add_header_ex(const char *header_line, size_t header_line_len, bool duplicate, bool replace) |
638 | 38.8k | { |
639 | 38.8k | sapi_header_line ctr = {0}; |
640 | 38.8k | int r; |
641 | | |
642 | 38.8k | ctr.line = header_line; |
643 | 38.8k | ctr.line_len = header_line_len; |
644 | | |
645 | 38.8k | r = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, |
646 | 38.8k | &ctr); |
647 | | |
648 | 38.8k | if (!duplicate) |
649 | 0 | efree((void *) header_line); |
650 | | |
651 | 38.8k | return r; |
652 | 38.8k | } |
653 | | |
654 | | static void sapi_header_add_op(sapi_header_op_enum op, sapi_header_struct *sapi_header) |
655 | 38.8k | { |
656 | 38.8k | if (!sapi_module.header_handler || |
657 | 38.8k | (SAPI_HEADER_ADD & sapi_module.header_handler(sapi_header, op, &SG(sapi_headers)))) { |
658 | 38.8k | if (op == SAPI_HEADER_REPLACE) { |
659 | 38.8k | char *colon_offset = strchr(sapi_header->header, ':'); |
660 | | |
661 | 38.8k | if (colon_offset) { |
662 | 38.8k | char sav = *colon_offset; |
663 | | |
664 | 38.8k | *colon_offset = 0; |
665 | 38.8k | sapi_remove_header(&SG(sapi_headers).headers, sapi_header->header, strlen(sapi_header->header), 0); |
666 | 38.8k | *colon_offset = sav; |
667 | 38.8k | } |
668 | 38.8k | } |
669 | 38.8k | zend_llist_add_element(&SG(sapi_headers).headers, (void *) sapi_header); |
670 | 38.8k | } else { |
671 | 0 | sapi_free_header(sapi_header); |
672 | 0 | } |
673 | 38.8k | } |
674 | | |
675 | | SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg) |
676 | 38.8k | { |
677 | 38.8k | sapi_header_struct sapi_header; |
678 | 38.8k | char *colon_offset; |
679 | 38.8k | char *header_line; |
680 | 38.8k | size_t header_line_len, header_len; |
681 | 38.8k | int http_response_code; |
682 | | |
683 | 38.8k | if (SG(headers_sent) && !SG(request_info).no_headers) { |
684 | 0 | const char *output_start_filename = php_output_get_start_filename(); |
685 | 0 | int output_start_lineno = php_output_get_start_lineno(); |
686 | |
|
687 | 0 | if (output_start_filename) { |
688 | 0 | sapi_module.sapi_error(E_WARNING, "Cannot modify header information - headers already sent by (output started at %s:%d)", |
689 | 0 | output_start_filename, output_start_lineno); |
690 | 0 | } else { |
691 | 0 | sapi_module.sapi_error(E_WARNING, "Cannot modify header information - headers already sent"); |
692 | 0 | } |
693 | 0 | return FAILURE; |
694 | 0 | } |
695 | | |
696 | 38.8k | switch (op) { |
697 | 0 | case SAPI_HEADER_SET_STATUS: |
698 | 0 | sapi_update_response_code((int)(intptr_t) arg); |
699 | 0 | return SUCCESS; |
700 | | |
701 | 0 | case SAPI_HEADER_ADD: |
702 | 38.8k | case SAPI_HEADER_REPLACE: |
703 | 38.8k | case SAPI_HEADER_DELETE_PREFIX: |
704 | 38.8k | case SAPI_HEADER_DELETE: { |
705 | 38.8k | sapi_header_line *p = arg; |
706 | | |
707 | 38.8k | if (!p->line || !p->line_len) { |
708 | 0 | return FAILURE; |
709 | 0 | } |
710 | 38.8k | header_line = estrndup(p->line, p->line_len); |
711 | 38.8k | header_line_len = p->line_len; |
712 | 38.8k | if (op == SAPI_HEADER_DELETE_PREFIX) { |
713 | 0 | header_len = p->header_len; |
714 | 0 | http_response_code = 0; |
715 | 38.8k | } else { |
716 | 38.8k | header_len = 0; |
717 | 38.8k | http_response_code = p->response_code; |
718 | 38.8k | } |
719 | 38.8k | break; |
720 | 38.8k | } |
721 | | |
722 | 0 | case SAPI_HEADER_DELETE_ALL: |
723 | 0 | if (sapi_module.header_handler) { |
724 | 0 | sapi_module.header_handler(&sapi_header, op, &SG(sapi_headers)); |
725 | 0 | } |
726 | 0 | zend_llist_clean(&SG(sapi_headers).headers); |
727 | 0 | return SUCCESS; |
728 | | |
729 | 0 | default: |
730 | 0 | return FAILURE; |
731 | 38.8k | } |
732 | | |
733 | | /* cut off trailing spaces, linefeeds and carriage-returns */ |
734 | 38.8k | if (header_line_len && isspace((unsigned char)header_line[header_line_len - 1])) { |
735 | 0 | do { |
736 | 0 | header_line_len--; |
737 | 0 | } while(header_line_len && isspace((unsigned char)header_line[header_line_len - 1])); |
738 | 0 | header_line[header_line_len]='\0'; |
739 | 0 | } |
740 | | |
741 | 38.8k | if (op == SAPI_HEADER_DELETE || op == SAPI_HEADER_DELETE_PREFIX) { |
742 | 0 | if (op == SAPI_HEADER_DELETE && strchr(header_line, ':')) { |
743 | 0 | efree(header_line); |
744 | 0 | sapi_module.sapi_error(E_WARNING, "Header to delete may not contain colon."); |
745 | 0 | return FAILURE; |
746 | 0 | } |
747 | 0 | if (sapi_module.header_handler) { |
748 | 0 | sapi_header.header = header_line; |
749 | 0 | sapi_header.header_len = header_line_len; |
750 | 0 | sapi_module.header_handler(&sapi_header, op, &SG(sapi_headers)); |
751 | 0 | } |
752 | 0 | sapi_remove_header(&SG(sapi_headers).headers, header_line, header_line_len, header_len); |
753 | 0 | efree(header_line); |
754 | 0 | return SUCCESS; |
755 | 38.8k | } else { |
756 | | /* new line/NUL character safety check */ |
757 | 38.8k | uint32_t i; |
758 | 1.08M | for (i = 0; i < header_line_len; i++) { |
759 | | /* RFC 7230 ch. 3.2.4 deprecates folding support */ |
760 | 1.05M | if (header_line[i] == '\n' || header_line[i] == '\r') { |
761 | 0 | efree(header_line); |
762 | 0 | sapi_module.sapi_error(E_WARNING, "Header may not contain " |
763 | 0 | "more than a single header, new line detected"); |
764 | 0 | return FAILURE; |
765 | 0 | } |
766 | 1.05M | if (header_line[i] == '\0') { |
767 | 0 | efree(header_line); |
768 | 0 | sapi_module.sapi_error(E_WARNING, "Header may not contain NUL bytes"); |
769 | 0 | return FAILURE; |
770 | 0 | } |
771 | 1.05M | } |
772 | 38.8k | } |
773 | | |
774 | 38.8k | sapi_header.header = header_line; |
775 | 38.8k | sapi_header.header_len = header_line_len; |
776 | | |
777 | | /* Check the header for a few cases that we have special support for in SAPI */ |
778 | 38.8k | if (header_line_len>=5 |
779 | 38.8k | && !strncasecmp(header_line, "HTTP/", 5)) { |
780 | | /* filter out the response code */ |
781 | 0 | sapi_update_response_code(sapi_extract_response_code(header_line)); |
782 | | /* sapi_update_response_code doesn't free the status line if the code didn't change */ |
783 | 0 | if (SG(sapi_headers).http_status_line) { |
784 | 0 | efree(SG(sapi_headers).http_status_line); |
785 | 0 | } |
786 | 0 | SG(sapi_headers).http_status_line = header_line; |
787 | 0 | return SUCCESS; |
788 | 38.8k | } else { |
789 | 38.8k | colon_offset = strchr(header_line, ':'); |
790 | 38.8k | if (colon_offset) { |
791 | 38.8k | *colon_offset = 0; |
792 | 38.8k | if (!strcasecmp(header_line, "Content-Type")) { |
793 | 0 | char *ptr = colon_offset+1, *mimetype = NULL, *newheader; |
794 | 0 | size_t len = header_line_len - (ptr - header_line), newlen; |
795 | 0 | while (*ptr == ' ') { |
796 | 0 | ptr++; |
797 | 0 | len--; |
798 | 0 | } |
799 | |
|
800 | 0 | mimetype = estrdup(ptr); |
801 | 0 | newlen = sapi_apply_default_charset(&mimetype, len); |
802 | 0 | if (!SG(sapi_headers).mimetype){ |
803 | 0 | SG(sapi_headers).mimetype = estrdup(mimetype); |
804 | 0 | } |
805 | |
|
806 | 0 | if (newlen != 0) { |
807 | 0 | newlen += sizeof("Content-type: "); |
808 | 0 | newheader = emalloc(newlen); |
809 | 0 | PHP_STRLCPY(newheader, "Content-type: ", newlen, sizeof("Content-type: ")-1); |
810 | 0 | strlcat(newheader, mimetype, newlen); |
811 | 0 | sapi_header.header = newheader; |
812 | 0 | sapi_header.header_len = (uint32_t)(newlen - 1); |
813 | 0 | efree(header_line); |
814 | 0 | } |
815 | 0 | efree(mimetype); |
816 | 0 | SG(sapi_headers).send_default_content_type = 0; |
817 | 38.8k | } else if (!strcasecmp(header_line, "Content-Length")) { |
818 | | /* Script is setting Content-length. The script cannot reasonably |
819 | | * know the size of the message body after compression, so it's best |
820 | | * to disable compression altogether. This contributes to making scripts |
821 | | * portable between setups that have and don't have zlib compression |
822 | | * enabled globally. See req #44164 */ |
823 | 0 | zend_string *key = ZSTR_INIT_LITERAL("zlib.output_compression", 0); |
824 | 0 | zend_alter_ini_entry_chars(key, |
825 | 0 | "0", sizeof("0") - 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME); |
826 | 0 | zend_string_release_ex(key, 0); |
827 | 38.8k | } else if (!strcasecmp(header_line, "Location")) { |
828 | 0 | if ((SG(sapi_headers).http_response_code < 300 || |
829 | 0 | SG(sapi_headers).http_response_code > 399) && |
830 | 0 | SG(sapi_headers).http_response_code != 201) { |
831 | | /* Return a Found Redirect if one is not already specified */ |
832 | 0 | if (http_response_code) { /* user specified redirect code */ |
833 | 0 | sapi_update_response_code(http_response_code); |
834 | 0 | } else if (SG(request_info).proto_num > 1000 && |
835 | 0 | SG(request_info).request_method && |
836 | 0 | strcmp(SG(request_info).request_method, "HEAD") && |
837 | 0 | strcmp(SG(request_info).request_method, "GET")) { |
838 | 0 | sapi_update_response_code(303); |
839 | 0 | } else { |
840 | 0 | sapi_update_response_code(302); |
841 | 0 | } |
842 | 0 | } |
843 | 38.8k | } else if (!strcasecmp(header_line, "WWW-Authenticate")) { /* HTTP Authentication */ |
844 | 0 | sapi_update_response_code(401); /* authentication-required */ |
845 | 0 | } |
846 | 38.8k | if (sapi_header.header==header_line) { |
847 | 38.8k | *colon_offset = ':'; |
848 | 38.8k | } |
849 | 38.8k | } |
850 | 38.8k | } |
851 | 38.8k | if (http_response_code) { |
852 | 0 | sapi_update_response_code(http_response_code); |
853 | 0 | } |
854 | 38.8k | sapi_header_add_op(op, &sapi_header); |
855 | 38.8k | return SUCCESS; |
856 | 38.8k | } |
857 | | |
858 | | |
859 | | SAPI_API int sapi_send_headers(void) |
860 | 38.8k | { |
861 | 38.8k | int retval; |
862 | 38.8k | int ret = FAILURE; |
863 | | |
864 | 38.8k | if (SG(headers_sent) || SG(request_info).no_headers) { |
865 | 0 | return SUCCESS; |
866 | 0 | } |
867 | | |
868 | | /* Success-oriented. We set headers_sent to 1 here to avoid an infinite loop |
869 | | * in case of an error situation. |
870 | | */ |
871 | 38.8k | if (SG(sapi_headers).send_default_content_type && sapi_module.send_headers) { |
872 | 0 | uint32_t len = 0; |
873 | 0 | char *default_mimetype = get_default_content_type(0, &len); |
874 | |
|
875 | 0 | if (default_mimetype && len) { |
876 | 0 | sapi_header_struct default_header; |
877 | |
|
878 | 0 | SG(sapi_headers).mimetype = default_mimetype; |
879 | |
|
880 | 0 | default_header.header_len = sizeof("Content-type: ") - 1 + len; |
881 | 0 | default_header.header = emalloc(default_header.header_len + 1); |
882 | |
|
883 | 0 | memcpy(default_header.header, "Content-type: ", sizeof("Content-type: ") - 1); |
884 | 0 | memcpy(default_header.header + sizeof("Content-type: ") - 1, SG(sapi_headers).mimetype, len + 1); |
885 | |
|
886 | 0 | sapi_header_add_op(SAPI_HEADER_ADD, &default_header); |
887 | 0 | } else { |
888 | 0 | efree(default_mimetype); |
889 | 0 | } |
890 | 0 | SG(sapi_headers).send_default_content_type = 0; |
891 | 0 | } |
892 | | |
893 | 38.8k | if (Z_TYPE(SG(callback_func)) != IS_UNDEF) { |
894 | 0 | zval cb; |
895 | 0 | ZVAL_COPY_VALUE(&cb, &SG(callback_func)); |
896 | 0 | ZVAL_UNDEF(&SG(callback_func)); |
897 | 0 | sapi_run_header_callback(&cb); |
898 | 0 | zval_ptr_dtor(&cb); |
899 | 0 | } |
900 | | |
901 | 38.8k | SG(headers_sent) = 1; |
902 | | |
903 | 38.8k | if (sapi_module.send_headers) { |
904 | 0 | retval = sapi_module.send_headers(&SG(sapi_headers)); |
905 | 38.8k | } else { |
906 | 38.8k | retval = SAPI_HEADER_DO_SEND; |
907 | 38.8k | } |
908 | | |
909 | 38.8k | switch (retval) { |
910 | 0 | case SAPI_HEADER_SENT_SUCCESSFULLY: |
911 | 0 | ret = SUCCESS; |
912 | 0 | break; |
913 | 38.8k | case SAPI_HEADER_DO_SEND: { |
914 | 38.8k | sapi_header_struct http_status_line; |
915 | 38.8k | char buf[255]; |
916 | | |
917 | 38.8k | if (SG(sapi_headers).http_status_line) { |
918 | 0 | http_status_line.header = SG(sapi_headers).http_status_line; |
919 | 0 | http_status_line.header_len = (uint32_t)strlen(SG(sapi_headers).http_status_line); |
920 | 38.8k | } else { |
921 | 38.8k | http_status_line.header = buf; |
922 | 38.8k | http_status_line.header_len = slprintf(buf, sizeof(buf), "HTTP/1.0 %d X", SG(sapi_headers).http_response_code); |
923 | 38.8k | } |
924 | 38.8k | sapi_module.send_header(&http_status_line, SG(server_context)); |
925 | 38.8k | } |
926 | 38.8k | zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t) sapi_module.send_header, SG(server_context)); |
927 | 38.8k | if(SG(sapi_headers).send_default_content_type) { |
928 | 38.8k | sapi_header_struct default_header; |
929 | | |
930 | 38.8k | sapi_get_default_content_type_header(&default_header); |
931 | 38.8k | sapi_module.send_header(&default_header, SG(server_context)); |
932 | 38.8k | sapi_free_header(&default_header); |
933 | 38.8k | } |
934 | 38.8k | sapi_module.send_header(NULL, SG(server_context)); |
935 | 38.8k | ret = SUCCESS; |
936 | 38.8k | break; |
937 | 0 | case SAPI_HEADER_SEND_FAILED: |
938 | 0 | SG(headers_sent) = 0; |
939 | 0 | ret = FAILURE; |
940 | 0 | break; |
941 | 38.8k | } |
942 | | |
943 | 38.8k | sapi_send_headers_free(); |
944 | | |
945 | 38.8k | return ret; |
946 | 38.8k | } |
947 | | |
948 | | |
949 | | SAPI_API int sapi_register_post_entries(const sapi_post_entry *post_entries) |
950 | 2 | { |
951 | 2 | const sapi_post_entry *p=post_entries; |
952 | | |
953 | 6 | while (p->content_type) { |
954 | 4 | if (sapi_register_post_entry(p) == FAILURE) { |
955 | 0 | return FAILURE; |
956 | 0 | } |
957 | 4 | p++; |
958 | 4 | } |
959 | 2 | return SUCCESS; |
960 | 2 | } |
961 | | |
962 | | |
963 | | SAPI_API int sapi_register_post_entry(const sapi_post_entry *post_entry) |
964 | 4 | { |
965 | 4 | int ret; |
966 | 4 | zend_string *key; |
967 | 4 | if (SG(sapi_started) && EG(current_execute_data)) { |
968 | 0 | return FAILURE; |
969 | 0 | } |
970 | 4 | key = zend_string_init(post_entry->content_type, post_entry->content_type_len, 1); |
971 | 4 | GC_MAKE_PERSISTENT_LOCAL(key); |
972 | 4 | ret = zend_hash_add_mem(&SG(known_post_content_types), key, |
973 | 4 | (void *) post_entry, sizeof(sapi_post_entry)) ? SUCCESS : FAILURE; |
974 | 4 | zend_string_release_ex(key, 1); |
975 | 4 | return ret; |
976 | 4 | } |
977 | | |
978 | | SAPI_API void sapi_unregister_post_entry(const sapi_post_entry *post_entry) |
979 | 0 | { |
980 | 0 | if (SG(sapi_started) && EG(current_execute_data)) { |
981 | 0 | return; |
982 | 0 | } |
983 | 0 | zend_hash_str_del(&SG(known_post_content_types), post_entry->content_type, |
984 | 0 | post_entry->content_type_len); |
985 | 0 | } |
986 | | |
987 | | |
988 | | SAPI_API int sapi_register_default_post_reader(void (*default_post_reader)(void)) |
989 | 2 | { |
990 | 2 | if (SG(sapi_started) && EG(current_execute_data)) { |
991 | 0 | return FAILURE; |
992 | 0 | } |
993 | 2 | sapi_module.default_post_reader = default_post_reader; |
994 | 2 | return SUCCESS; |
995 | 2 | } |
996 | | |
997 | | |
998 | | SAPI_API int sapi_register_treat_data(void (*treat_data)(int arg, char *str, zval *destArray)) |
999 | 2 | { |
1000 | 2 | if (SG(sapi_started) && EG(current_execute_data)) { |
1001 | 0 | return FAILURE; |
1002 | 0 | } |
1003 | 2 | sapi_module.treat_data = treat_data; |
1004 | 2 | return SUCCESS; |
1005 | 2 | } |
1006 | | |
1007 | | 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)) |
1008 | 2 | { |
1009 | 2 | if (SG(sapi_started) && EG(current_execute_data)) { |
1010 | 0 | return FAILURE; |
1011 | 0 | } |
1012 | 2 | sapi_module.input_filter = input_filter; |
1013 | 2 | sapi_module.input_filter_init = input_filter_init; |
1014 | 2 | return SUCCESS; |
1015 | 2 | } |
1016 | | |
1017 | | SAPI_API int sapi_flush(void) |
1018 | 7.56k | { |
1019 | 7.56k | if (sapi_module.flush) { |
1020 | 7.56k | sapi_module.flush(SG(server_context)); |
1021 | 7.56k | return SUCCESS; |
1022 | 7.56k | } else { |
1023 | 0 | return FAILURE; |
1024 | 0 | } |
1025 | 7.56k | } |
1026 | | |
1027 | | SAPI_API zend_stat_t *sapi_get_stat(void) |
1028 | 0 | { |
1029 | 0 | if (sapi_module.get_stat) { |
1030 | 0 | return sapi_module.get_stat(); |
1031 | 0 | } else { |
1032 | 0 | if (!SG(request_info).path_translated || (VCWD_STAT(SG(request_info).path_translated, &SG(global_stat)) == -1)) { |
1033 | 0 | return NULL; |
1034 | 0 | } |
1035 | 0 | return &SG(global_stat); |
1036 | 0 | } |
1037 | 0 | } |
1038 | | |
1039 | | SAPI_API char *sapi_getenv(const char *name, size_t name_len) |
1040 | 0 | { |
1041 | 0 | char *value, *tmp; |
1042 | |
|
1043 | 0 | if (!sapi_module.getenv) { |
1044 | 0 | return NULL; |
1045 | 0 | } |
1046 | 0 | if (!strncasecmp(name, "HTTP_PROXY", name_len)) { |
1047 | | /* Ugly fix for HTTP_PROXY issue, see bug #72573 */ |
1048 | 0 | return NULL; |
1049 | 0 | } |
1050 | 0 | tmp = sapi_module.getenv(name, name_len); |
1051 | 0 | if (!tmp) { |
1052 | 0 | return NULL; |
1053 | 0 | } |
1054 | 0 | value = estrdup(tmp); |
1055 | | #ifdef PHP_WIN32 |
1056 | | if (strlen(sapi_module.name) == sizeof("cgi-fcgi") - 1 && !strcmp(sapi_module.name, "cgi-fcgi")) { |
1057 | | /* XXX more modules to go, if needed. */ |
1058 | | free(tmp); |
1059 | | } |
1060 | | #endif |
1061 | 0 | if (sapi_module.input_filter) { |
1062 | 0 | sapi_module.input_filter(PARSE_STRING, name, &value, strlen(value), NULL); |
1063 | 0 | } |
1064 | 0 | return value; |
1065 | 0 | } |
1066 | | |
1067 | | SAPI_API int sapi_get_fd(int *fd) |
1068 | 0 | { |
1069 | 0 | if (sapi_module.get_fd) { |
1070 | 0 | return sapi_module.get_fd(fd); |
1071 | 0 | } else { |
1072 | 0 | return FAILURE; |
1073 | 0 | } |
1074 | 0 | } |
1075 | | |
1076 | | SAPI_API int sapi_force_http_10(void) |
1077 | 0 | { |
1078 | 0 | if (sapi_module.force_http_10) { |
1079 | 0 | return sapi_module.force_http_10(); |
1080 | 0 | } else { |
1081 | 0 | return FAILURE; |
1082 | 0 | } |
1083 | 0 | } |
1084 | | |
1085 | | |
1086 | | SAPI_API int sapi_get_target_uid(uid_t *obj) |
1087 | 0 | { |
1088 | 0 | if (sapi_module.get_target_uid) { |
1089 | 0 | return sapi_module.get_target_uid(obj); |
1090 | 0 | } else { |
1091 | 0 | return FAILURE; |
1092 | 0 | } |
1093 | 0 | } |
1094 | | |
1095 | | SAPI_API int sapi_get_target_gid(gid_t *obj) |
1096 | 0 | { |
1097 | 0 | if (sapi_module.get_target_gid) { |
1098 | 0 | return sapi_module.get_target_gid(obj); |
1099 | 0 | } else { |
1100 | 0 | return FAILURE; |
1101 | 0 | } |
1102 | 0 | } |
1103 | | |
1104 | | SAPI_API double sapi_get_request_time(void) |
1105 | 38.8k | { |
1106 | 38.8k | if(SG(global_request_time)) return SG(global_request_time); |
1107 | | |
1108 | 38.8k | if (!sapi_module.get_request_time |
1109 | 38.8k | || sapi_module.get_request_time(&SG(global_request_time)) == FAILURE) { |
1110 | 38.8k | struct timeval tp = {0}; |
1111 | 38.8k | if (!gettimeofday(&tp, NULL)) { |
1112 | 38.8k | SG(global_request_time) = (double)(tp.tv_sec + tp.tv_usec / 1000000.00); |
1113 | 38.8k | } else { |
1114 | 0 | SG(global_request_time) = (double)time(0); |
1115 | 0 | } |
1116 | 38.8k | } |
1117 | 38.8k | return SG(global_request_time); |
1118 | 38.8k | } |
1119 | | |
1120 | 0 | SAPI_API void sapi_terminate_process(void) { |
1121 | 0 | if (sapi_module.terminate_process) { |
1122 | 0 | sapi_module.terminate_process(); |
1123 | 0 | } |
1124 | 0 | } |
1125 | | |
1126 | | SAPI_API void sapi_add_request_header(const char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg) /* {{{ */ |
1127 | 0 | { |
1128 | 0 | zval *return_value = (zval*)arg; |
1129 | 0 | char *buf = NULL; |
1130 | |
|
1131 | 0 | ALLOCA_FLAG(use_heap) |
1132 | |
|
1133 | 0 | if (var_len > 5 && |
1134 | 0 | var[0] == 'H' && |
1135 | 0 | var[1] == 'T' && |
1136 | 0 | var[2] == 'T' && |
1137 | 0 | var[3] == 'P' && |
1138 | 0 | var[4] == '_') { |
1139 | |
|
1140 | 0 | const char *p; |
1141 | 0 | char *str; |
1142 | |
|
1143 | 0 | var_len -= 5; |
1144 | 0 | p = var + 5; |
1145 | 0 | var = str = buf = do_alloca(var_len + 1, use_heap); |
1146 | 0 | *str++ = *p++; |
1147 | 0 | while (*p) { |
1148 | 0 | if (*p == '_') { |
1149 | 0 | *str++ = '-'; |
1150 | 0 | p++; |
1151 | 0 | if (*p) { |
1152 | 0 | *str++ = *p++; |
1153 | 0 | } |
1154 | 0 | } else if (*p >= 'A' && *p <= 'Z') { |
1155 | 0 | *str++ = (*p++ - 'A' + 'a'); |
1156 | 0 | } else { |
1157 | 0 | *str++ = *p++; |
1158 | 0 | } |
1159 | 0 | } |
1160 | 0 | *str = 0; |
1161 | 0 | } else if (var_len == sizeof("CONTENT_TYPE")-1 && |
1162 | 0 | memcmp(var, "CONTENT_TYPE", sizeof("CONTENT_TYPE")-1) == 0) { |
1163 | 0 | var = "Content-Type"; |
1164 | 0 | } else if (var_len == sizeof("CONTENT_LENGTH")-1 && |
1165 | 0 | memcmp(var, "CONTENT_LENGTH", sizeof("CONTENT_LENGTH")-1) == 0) { |
1166 | 0 | var = "Content-Length"; |
1167 | 0 | } else { |
1168 | 0 | return; |
1169 | 0 | } |
1170 | 0 | add_assoc_stringl_ex(return_value, var, var_len, val, val_len); |
1171 | 0 | if (buf) { |
1172 | | free_alloca(buf, use_heap); |
1173 | 0 | } |
1174 | 0 | } |
1175 | | /* }}} */ |