You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1094 lines
31 KiB

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