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