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