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.

990 lines
28 KiB

27 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
27 years ago
26 years ago
21 years ago
23 years ago
23 years ago
22 years ago
21 years ago
21 years ago
21 years ago
21 years ago
23 years ago
23 years ago
23 years ago
27 years ago
26 years ago
27 years ago
26 years ago
26 years ago
23 years ago
24 years ago
19 years ago
20 years ago
23 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
26 years ago
26 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 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
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 6 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2009 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_ZLIB
  30. #include "ext/zlib/php_zlib.h"
  31. #endif
  32. #ifdef ZTS
  33. #include "TSRM.h"
  34. #endif
  35. #ifdef HAVE_SYS_TIME_H
  36. #include <sys/time.h>
  37. #endif
  38. #include "rfc1867.h"
  39. #ifdef PHP_WIN32
  40. #define STRCASECMP stricmp
  41. #else
  42. #define STRCASECMP strcasecmp
  43. #endif
  44. #include "php_content_types.h"
  45. #ifdef ZTS
  46. SAPI_API int sapi_globals_id;
  47. #else
  48. sapi_globals_struct sapi_globals;
  49. #endif
  50. static void sapi_globals_ctor(sapi_globals_struct *sapi_globals TSRMLS_DC)
  51. {
  52. memset(sapi_globals, 0, sizeof(*sapi_globals));
  53. zend_hash_init_ex(&sapi_globals->known_post_content_types, 5, NULL, NULL, 1, 0);
  54. php_setup_sapi_content_types(TSRMLS_C);
  55. }
  56. static void sapi_globals_dtor(sapi_globals_struct *sapi_globals TSRMLS_DC)
  57. {
  58. zend_hash_destroy(&sapi_globals->known_post_content_types);
  59. }
  60. /* True globals (no need for thread safety) */
  61. SAPI_API sapi_module_struct sapi_module;
  62. SAPI_API void sapi_startup(sapi_module_struct *sf)
  63. {
  64. sf->ini_entries = NULL;
  65. sapi_module = *sf;
  66. #ifdef ZTS
  67. ts_allocate_id(&sapi_globals_id, sizeof(sapi_globals_struct), (ts_allocate_ctor) sapi_globals_ctor, (ts_allocate_dtor) sapi_globals_dtor);
  68. #else
  69. sapi_globals_ctor(&sapi_globals);
  70. #endif
  71. virtual_cwd_startup(); /* Could use shutdown to free the main cwd but it would just slow it down for CGI */
  72. #ifdef PHP_WIN32
  73. tsrm_win32_startup();
  74. #endif
  75. reentrancy_startup();
  76. }
  77. SAPI_API void sapi_shutdown(void)
  78. {
  79. #ifdef ZTS
  80. ts_free_id(sapi_globals_id);
  81. #else
  82. sapi_globals_dtor(&sapi_globals);
  83. #endif
  84. reentrancy_shutdown();
  85. virtual_cwd_shutdown();
  86. #ifdef PHP_WIN32
  87. tsrm_win32_shutdown();
  88. #endif
  89. }
  90. SAPI_API void sapi_free_header(sapi_header_struct *sapi_header)
  91. {
  92. efree(sapi_header->header);
  93. }
  94. SAPI_API void sapi_handle_post(void *arg TSRMLS_DC)
  95. {
  96. if (SG(request_info).post_entry && SG(request_info).content_type_dup) {
  97. /*
  98. * We need to re-populate post_data from the stored raw POST data in case we had
  99. * any before, so that the handler can get to it.
  100. */
  101. if (SG(request_info).post_data == NULL && SG(request_info).raw_post_data != NULL) {
  102. SG(request_info).post_data = estrndup(SG(request_info).raw_post_data, SG(request_info).raw_post_data_length);
  103. }
  104. SG(request_info).post_entry->post_handler(SG(request_info).content_type_dup, arg TSRMLS_CC);
  105. if (SG(request_info).post_data) {
  106. efree(SG(request_info).post_data);
  107. SG(request_info).post_data = NULL;
  108. }
  109. #if 0 /* UTODO see if this works */
  110. efree(SG(request_info).content_type_dup);
  111. SG(request_info).content_type_dup = NULL;
  112. #endif
  113. }
  114. }
  115. static void sapi_read_post_data(TSRMLS_D)
  116. {
  117. sapi_post_entry *post_entry;
  118. uint content_type_length = strlen(SG(request_info).content_type);
  119. char *content_type = estrndup(SG(request_info).content_type, content_type_length);
  120. char *p;
  121. char oldchar=0;
  122. void (*post_reader_func)(TSRMLS_D) = NULL;
  123. /* dedicated implementation for increased performance:
  124. * - Make the content type lowercase
  125. * - Trim descriptive data, stay with the content-type only
  126. */
  127. for (p=content_type; p<content_type+content_type_length; p++) {
  128. switch (*p) {
  129. case ';':
  130. case ',':
  131. case ' ':
  132. content_type_length = p-content_type;
  133. oldchar = *p;
  134. *p = 0;
  135. break;
  136. default:
  137. *p = tolower(*p);
  138. break;
  139. }
  140. }
  141. /* now try to find an appropriate POST content handler */
  142. if (zend_hash_find(&SG(known_post_content_types), content_type,
  143. content_type_length+1, (void **) &post_entry) == SUCCESS) {
  144. /* found one, register it for use */
  145. SG(request_info).post_entry = post_entry;
  146. post_reader_func = post_entry->post_reader;
  147. } else {
  148. /* fallback */
  149. SG(request_info).post_entry = NULL;
  150. if (!sapi_module.default_post_reader) {
  151. /* no default reader ? */
  152. SG(request_info).content_type_dup = NULL;
  153. sapi_module.sapi_error(E_WARNING, "Unsupported content type: '%s'", content_type);
  154. return;
  155. }
  156. }
  157. if (oldchar) {
  158. *(p-1) = oldchar;
  159. }
  160. SG(request_info).content_type_dup = content_type;
  161. if(post_reader_func) {
  162. post_reader_func(TSRMLS_C);
  163. }
  164. if(sapi_module.default_post_reader) {
  165. sapi_module.default_post_reader(TSRMLS_C);
  166. }
  167. }
  168. SAPI_API SAPI_POST_READER_FUNC(sapi_read_standard_form_data)
  169. {
  170. int read_bytes;
  171. int allocated_bytes=SAPI_POST_BLOCK_SIZE+1;
  172. if (SG(request_info).content_length > SG(post_max_size)) {
  173. php_error_docref(NULL TSRMLS_CC, E_WARNING, "POST Content-Length of %ld bytes exceeds the limit of %ld bytes",
  174. SG(request_info).content_length, SG(post_max_size));
  175. return;
  176. }
  177. SG(request_info).post_data = emalloc(allocated_bytes);
  178. for (;;) {
  179. read_bytes = sapi_module.read_post(SG(request_info).post_data+SG(read_post_bytes), SAPI_POST_BLOCK_SIZE TSRMLS_CC);
  180. if (read_bytes<=0) {
  181. break;
  182. }
  183. SG(read_post_bytes) += read_bytes;
  184. if (SG(read_post_bytes) > SG(post_max_size)) {
  185. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Actual POST length does not match Content-Length, and exceeds %ld bytes", SG(post_max_size));
  186. break;
  187. }
  188. if (read_bytes < SAPI_POST_BLOCK_SIZE) {
  189. break;
  190. }
  191. if (SG(read_post_bytes)+SAPI_POST_BLOCK_SIZE >= allocated_bytes) {
  192. allocated_bytes = SG(read_post_bytes)+SAPI_POST_BLOCK_SIZE+1;
  193. SG(request_info).post_data = erealloc(SG(request_info).post_data, allocated_bytes);
  194. }
  195. }
  196. SG(request_info).post_data[SG(read_post_bytes)] = 0; /* terminating NULL */
  197. SG(request_info).post_data_length = SG(read_post_bytes);
  198. }
  199. /* {{{ sapi_update_default_charset */
  200. SAPI_API void sapi_update_default_charset(TSRMLS_D)
  201. {
  202. const char *canonical_name = NULL;
  203. UErrorCode status = U_ZERO_ERROR;
  204. canonical_name = ucnv_getName(ZEND_U_CONVERTER(UG(output_encoding_conv)), &status);
  205. /*
  206. * UTODO handle NULL return which signifies that MIME standard has no
  207. * name for this converter
  208. */
  209. SG(default_charset) = (char *)ucnv_getStandardName(canonical_name, "MIME", &status);
  210. }
  211. /* }}} */
  212. SAPI_API char *sapi_get_default_content_type(TSRMLS_D)
  213. {
  214. char *mimetype, *charset, *content_type;
  215. mimetype = SG(default_mimetype) ? SG(default_mimetype) : SAPI_DEFAULT_MIMETYPE;
  216. /*
  217. * Apache SAPI may invoke this function directly, before php_request_startup() is
  218. * called, so we need to update the default charset explicitly.
  219. */
  220. sapi_update_default_charset(TSRMLS_C);
  221. charset = SG(default_charset) ? SG(default_charset) : SAPI_DEFAULT_CHARSET;
  222. if (strncasecmp(mimetype, "text/", 5) == 0 && *charset) {
  223. int len = strlen(mimetype) + sizeof("; charset=") + strlen(charset); /* sizeof() includes \0 */
  224. content_type = emalloc(len);
  225. snprintf(content_type, len, "%s; charset=%s", mimetype, charset);
  226. } else {
  227. content_type = estrdup(mimetype);
  228. }
  229. return content_type;
  230. }
  231. SAPI_API void sapi_get_default_content_type_header(sapi_header_struct *default_header TSRMLS_DC)
  232. {
  233. char *default_content_type = sapi_get_default_content_type(TSRMLS_C);
  234. int default_content_type_len = strlen(default_content_type);
  235. default_header->header_len = (sizeof("Content-type: ")-1) + default_content_type_len;
  236. default_header->header = emalloc(default_header->header_len+1);
  237. memcpy(default_header->header, "Content-type: ", sizeof("Content-type: "));
  238. memcpy(default_header->header+sizeof("Content-type: ")-1, default_content_type, default_content_type_len);
  239. default_header->header[default_header->header_len] = 0;
  240. efree(default_content_type);
  241. }
  242. /*
  243. * Add charset on content-type header if the MIME type starts with
  244. * "text/", the default_charset directive is not empty and
  245. * there is not already a charset option in there.
  246. *
  247. * If "mimetype" is non-NULL, it should point to a pointer allocated
  248. * with emalloc(). If a charset is added, the string will be
  249. * re-allocated and the new length is returned. If mimetype is
  250. * unchanged, 0 is returned.
  251. *
  252. */
  253. SAPI_API size_t sapi_apply_default_charset(char **mimetype, size_t len TSRMLS_DC)
  254. {
  255. char *charset, *newtype;
  256. size_t newlen;
  257. charset = SG(default_charset) ? SG(default_charset) : SAPI_DEFAULT_CHARSET;
  258. if (*mimetype != NULL) {
  259. if (*charset && strncmp(*mimetype, "text/", 5) == 0 && strstr(*mimetype, "charset=") == NULL) {
  260. newlen = len + (sizeof(";charset=")-1) + strlen(charset);
  261. newtype = emalloc(newlen + 1);
  262. PHP_STRLCPY(newtype, *mimetype, newlen + 1, len);
  263. strlcat(newtype, ";charset=", newlen + 1);
  264. strlcat(newtype, charset, newlen + 1);
  265. efree(*mimetype);
  266. *mimetype = newtype;
  267. return newlen;
  268. }
  269. }
  270. return 0;
  271. }
  272. SAPI_API void sapi_activate_headers_only(TSRMLS_D)
  273. {
  274. if (SG(request_info).headers_read == 1)
  275. return;
  276. SG(request_info).headers_read = 1;
  277. zend_llist_init(&SG(sapi_headers).headers, sizeof(sapi_header_struct),
  278. (void (*)(void *)) sapi_free_header, 0);
  279. SG(sapi_headers).send_default_content_type = 1;
  280. /* SG(sapi_headers).http_response_code = 200; */
  281. SG(sapi_headers).http_status_line = NULL;
  282. SG(sapi_headers).mimetype = NULL;
  283. SG(read_post_bytes) = 0;
  284. SG(request_info).post_data = NULL;
  285. SG(request_info).raw_post_data = NULL;
  286. SG(request_info).current_user = NULL;
  287. SG(request_info).current_user_length = 0;
  288. SG(request_info).no_headers = 0;
  289. SG(request_info).post_entry = NULL;
  290. SG(global_request_time) = 0;
  291. /*
  292. * It's possible to override this general case in the activate() callback,
  293. * if necessary.
  294. */
  295. if (SG(request_info).request_method && !strcmp(SG(request_info).request_method, "HEAD")) {
  296. SG(request_info).headers_only = 1;
  297. } else {
  298. SG(request_info).headers_only = 0;
  299. }
  300. if (SG(server_context)) {
  301. SG(request_info).cookie_data = sapi_module.read_cookies(TSRMLS_C);
  302. if (sapi_module.activate) {
  303. sapi_module.activate(TSRMLS_C);
  304. }
  305. }
  306. if (sapi_module.input_filter_init ) {
  307. sapi_module.input_filter_init(TSRMLS_C);
  308. }
  309. }
  310. /*
  311. * Called from php_request_startup() for every request.
  312. */
  313. SAPI_API void sapi_activate(TSRMLS_D)
  314. {
  315. zend_llist_init(&SG(sapi_headers).headers, sizeof(sapi_header_struct), (void (*)(void *)) sapi_free_header, 0);
  316. SG(sapi_headers).send_default_content_type = 1;
  317. /*
  318. SG(sapi_headers).http_response_code = 200;
  319. */
  320. SG(sapi_headers).http_status_line = NULL;
  321. SG(sapi_headers).mimetype = NULL;
  322. SG(headers_sent) = 0;
  323. SG(read_post_bytes) = 0;
  324. SG(request_info).post_data = NULL;
  325. SG(request_info).raw_post_data = NULL;
  326. SG(request_info).current_user = NULL;
  327. SG(request_info).current_user_length = 0;
  328. SG(request_info).no_headers = 0;
  329. SG(request_info).post_entry = NULL;
  330. SG(request_info).proto_num = 1000; /* Default to HTTP 1.0 */
  331. SG(global_request_time) = 0;
  332. /* It's possible to override this general case in the activate() callback, if
  333. * necessary.
  334. */
  335. if (SG(request_info).request_method && !strcmp(SG(request_info).request_method, "HEAD")) {
  336. SG(request_info).headers_only = 1;
  337. } else {
  338. SG(request_info).headers_only = 0;
  339. }
  340. SG(rfc1867_uploaded_files) = NULL;
  341. SG(rfc1867_vars) = NULL;
  342. SG(rfc1867_vars) = NULL;
  343. /* handle request mehtod */
  344. if (SG(server_context)) {
  345. if ( SG(request_info).request_method) {
  346. if(!strcmp(SG(request_info).request_method, "POST")
  347. && (SG(request_info).content_type)) {
  348. /* HTTP POST -> may contain form data to be read into variables
  349. depending on content type given
  350. */
  351. sapi_read_post_data(TSRMLS_C);
  352. } else {
  353. /* any other method with content payload will fill
  354. $HTTP_RAW_POST_DATA if enabled by always_populate_raw_post_data
  355. it is up to the webserver to decide whether to allow a method or not
  356. */
  357. SG(request_info).content_type_dup = NULL;
  358. if(sapi_module.default_post_reader) {
  359. sapi_module.default_post_reader(TSRMLS_C);
  360. }
  361. }
  362. } else {
  363. SG(request_info).content_type_dup = NULL;
  364. }
  365. /* Cookies */
  366. SG(request_info).cookie_data = sapi_module.read_cookies(TSRMLS_C);
  367. if (sapi_module.activate) {
  368. sapi_module.activate(TSRMLS_C);
  369. }
  370. }
  371. if (sapi_module.input_filter_init ) {
  372. sapi_module.input_filter_init(TSRMLS_C);
  373. }
  374. }
  375. static void sapi_send_headers_free(TSRMLS_D)
  376. {
  377. if (SG(sapi_headers).http_status_line) {
  378. efree(SG(sapi_headers).http_status_line);
  379. SG(sapi_headers).http_status_line = NULL;
  380. }
  381. }
  382. SAPI_API void sapi_deactivate(TSRMLS_D)
  383. {
  384. zend_llist_destroy(&SG(sapi_headers).headers);
  385. if (SG(request_info).post_data) {
  386. efree(SG(request_info).post_data);
  387. } else if (SG(server_context)) {
  388. if(sapi_module.read_post) {
  389. /* make sure we've consumed all request input data */
  390. char dummy[SAPI_POST_BLOCK_SIZE];
  391. int read_bytes;
  392. while((read_bytes = sapi_module.read_post(dummy, sizeof(dummy)-1 TSRMLS_CC)) > 0) {
  393. SG(read_post_bytes) += read_bytes;
  394. }
  395. }
  396. }
  397. if (SG(request_info).raw_post_data) {
  398. efree(SG(request_info).raw_post_data);
  399. }
  400. if (SG(request_info).auth_user) {
  401. efree(SG(request_info).auth_user);
  402. }
  403. if (SG(request_info).auth_password) {
  404. efree(SG(request_info).auth_password);
  405. }
  406. if (SG(request_info).auth_digest) {
  407. efree(SG(request_info).auth_digest);
  408. }
  409. if (SG(request_info).content_type_dup) {
  410. efree(SG(request_info).content_type_dup);
  411. }
  412. if (SG(request_info).current_user) {
  413. efree(SG(request_info).current_user);
  414. }
  415. if (sapi_module.deactivate) {
  416. sapi_module.deactivate(TSRMLS_C);
  417. }
  418. if (SG(rfc1867_uploaded_files)) {
  419. destroy_uploaded_files_hash(TSRMLS_C);
  420. }
  421. if (SG(rfc1867_vars)) {
  422. zend_hash_destroy(SG(rfc1867_vars));
  423. FREE_HASHTABLE(SG(rfc1867_vars));
  424. }
  425. if (SG(rfc1867_files_vars)) {
  426. zend_hash_destroy(SG(rfc1867_files_vars));
  427. FREE_HASHTABLE(SG(rfc1867_files_vars));
  428. }
  429. if (SG(sapi_headers).mimetype) {
  430. efree(SG(sapi_headers).mimetype);
  431. SG(sapi_headers).mimetype = NULL;
  432. }
  433. sapi_send_headers_free(TSRMLS_C);
  434. SG(sapi_started) = 0;
  435. SG(headers_sent) = 0;
  436. SG(request_info).headers_read = 0;
  437. SG(global_request_time) = 0;
  438. }
  439. SAPI_API void sapi_initialize_empty_request(TSRMLS_D)
  440. {
  441. SG(server_context) = NULL;
  442. SG(request_info).request_method = NULL;
  443. SG(request_info).auth_digest = SG(request_info).auth_user = SG(request_info).auth_password = NULL;
  444. SG(request_info).content_type_dup = NULL;
  445. }
  446. static int sapi_extract_response_code(const char *header_line)
  447. {
  448. int code = 200;
  449. const char *ptr;
  450. for (ptr = header_line; *ptr; ptr++) {
  451. if (*ptr == ' ' && *(ptr + 1) != ' ') {
  452. code = atoi(ptr + 1);
  453. break;
  454. }
  455. }
  456. return code;
  457. }
  458. static void sapi_update_response_code(int ncode TSRMLS_DC)
  459. {
  460. /* if the status code did not change, we do not want
  461. to change the status line, and no need to change the code */
  462. if (SG(sapi_headers).http_response_code == ncode) {
  463. return;
  464. }
  465. if (SG(sapi_headers).http_status_line) {
  466. efree(SG(sapi_headers).http_status_line);
  467. SG(sapi_headers).http_status_line = NULL;
  468. }
  469. SG(sapi_headers).http_response_code = ncode;
  470. }
  471. static int sapi_find_matching_header(void *element1, void *element2)
  472. {
  473. int len = strlen((char*)element2);
  474. return strncasecmp(((sapi_header_struct*)element1)->header, (char*)element2, len) == 0 && ((sapi_header_struct*)element1)->header[len] == ':';
  475. }
  476. SAPI_API int sapi_add_header_ex(char *header_line, uint header_line_len, zend_bool duplicate, zend_bool replace TSRMLS_DC)
  477. {
  478. sapi_header_line ctr = {0};
  479. int r;
  480. ctr.line = header_line;
  481. ctr.line_len = header_line_len;
  482. r = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD,
  483. &ctr TSRMLS_CC);
  484. if (!duplicate)
  485. efree(header_line);
  486. return r;
  487. }
  488. SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg TSRMLS_DC)
  489. {
  490. int retval;
  491. sapi_header_struct sapi_header;
  492. char *colon_offset;
  493. long myuid = 0L;
  494. char *header_line;
  495. uint header_line_len;
  496. int http_response_code;
  497. if (SG(headers_sent) && !SG(request_info).no_headers) {
  498. char *output_start_filename = php_output_get_start_filename(TSRMLS_C);
  499. int output_start_lineno = php_output_get_start_lineno(TSRMLS_C);
  500. if (output_start_filename) {
  501. sapi_module.sapi_error(E_WARNING, "Cannot modify header information - headers already sent by (output started at %s:%d)",
  502. output_start_filename, output_start_lineno);
  503. } else {
  504. sapi_module.sapi_error(E_WARNING, "Cannot modify header information - headers already sent");
  505. }
  506. return FAILURE;
  507. }
  508. switch (op) {
  509. case SAPI_HEADER_SET_STATUS:
  510. sapi_update_response_code((int)(zend_intptr_t) arg TSRMLS_CC);
  511. return SUCCESS;
  512. case SAPI_HEADER_ADD:
  513. case SAPI_HEADER_REPLACE:
  514. case SAPI_HEADER_DELETE: {
  515. sapi_header_line *p = arg;
  516. if (!p->line || !p->line_len) {
  517. return FAILURE;
  518. }
  519. header_line = p->line;
  520. header_line_len = p->line_len;
  521. http_response_code = p->response_code;
  522. break;
  523. }
  524. case SAPI_HEADER_DELETE_ALL:
  525. if (sapi_module.header_handler) {
  526. sapi_module.header_handler(&sapi_header, op, &SG(sapi_headers) TSRMLS_CC);
  527. }
  528. zend_llist_clean(&SG(sapi_headers).headers);
  529. return SUCCESS;
  530. default:
  531. return FAILURE;
  532. }
  533. header_line = estrndup(header_line, header_line_len);
  534. /* cut of trailing spaces, linefeeds and carriage-returns */
  535. while(header_line_len && isspace(header_line[header_line_len-1]))
  536. header_line[--header_line_len]='\0';
  537. if (op == SAPI_HEADER_DELETE) {
  538. if (strchr(header_line, ':')) {
  539. efree(header_line);
  540. sapi_module.sapi_error(E_WARNING, "Header to delete may not contain colon.");
  541. return FAILURE;
  542. }
  543. } else {
  544. /* new line safety check */
  545. char *s = header_line, *e = header_line + header_line_len, *p;
  546. while (s < e && (p = memchr(s, '\n', (e - s)))) {
  547. if (*(p + 1) == ' ' || *(p + 1) == '\t') {
  548. s = p + 1;
  549. continue;
  550. }
  551. efree(header_line);
  552. sapi_module.sapi_error(E_WARNING, "Header may not contain more than a single header, new line detected.");
  553. return FAILURE;
  554. }
  555. }
  556. sapi_header.header = header_line;
  557. sapi_header.header_len = header_line_len;
  558. if (op == SAPI_HEADER_DELETE) {
  559. if (sapi_module.header_handler) {
  560. sapi_module.header_handler(&sapi_header, op, &SG(sapi_headers) TSRMLS_CC);
  561. }
  562. zend_llist_del_element(&SG(sapi_headers).headers, sapi_header.header, (int(*)(void*, void*))sapi_find_matching_header);
  563. sapi_free_header(&sapi_header);
  564. return SUCCESS;
  565. }
  566. /* Check the header for a few cases that we have special support for in SAPI */
  567. if (header_line_len>=5
  568. && !strncasecmp(header_line, "HTTP/", 5)) {
  569. /* filter out the response code */
  570. sapi_update_response_code(sapi_extract_response_code(header_line) TSRMLS_CC);
  571. /* sapi_update_response_code doesn't free the status line if the code didn't change */
  572. if (SG(sapi_headers).http_status_line) {
  573. efree(SG(sapi_headers).http_status_line);
  574. }
  575. SG(sapi_headers).http_status_line = header_line;
  576. return SUCCESS;
  577. } else {
  578. colon_offset = strchr(header_line, ':');
  579. if (colon_offset) {
  580. *colon_offset = 0;
  581. if (!STRCASECMP(header_line, "Content-Type")) {
  582. char *ptr = colon_offset+1, *mimetype = NULL, *newheader;
  583. size_t len = header_line_len - (ptr - header_line), newlen;
  584. while (*ptr == ' ') {
  585. ptr++;
  586. len--;
  587. }
  588. #if HAVE_ZLIB
  589. if(!strncmp(ptr, "image/", sizeof("image/")-1)) {
  590. zend_alter_ini_entry("zlib.output_compression", sizeof("zlib.output_compression"), "0", sizeof("0") - 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  591. }
  592. #endif
  593. mimetype = estrdup(ptr);
  594. newlen = sapi_apply_default_charset(&mimetype, len TSRMLS_CC);
  595. if (!SG(sapi_headers).mimetype){
  596. SG(sapi_headers).mimetype = estrdup(mimetype);
  597. }
  598. if (newlen != 0) {
  599. newlen += sizeof("Content-type: ");
  600. newheader = emalloc(newlen);
  601. PHP_STRLCPY(newheader, "Content-type: ", newlen, sizeof("Content-type: ")-1);
  602. strlcat(newheader, mimetype, newlen);
  603. sapi_header.header = newheader;
  604. sapi_header.header_len = newlen - 1;
  605. efree(header_line);
  606. }
  607. efree(mimetype);
  608. SG(sapi_headers).send_default_content_type = 0;
  609. } else if (!STRCASECMP(header_line, "Location")) {
  610. if ((SG(sapi_headers).http_response_code < 300 ||
  611. SG(sapi_headers).http_response_code > 307) &&
  612. SG(sapi_headers).http_response_code != 201) {
  613. /* Return a Found Redirect if one is not already specified */
  614. if (http_response_code) { /* user specified redirect code */
  615. sapi_update_response_code(http_response_code TSRMLS_CC);
  616. } else if (SG(request_info).proto_num > 1000 &&
  617. SG(request_info).request_method &&
  618. strcmp(SG(request_info).request_method, "HEAD") &&
  619. strcmp(SG(request_info).request_method, "GET")) {
  620. sapi_update_response_code(303 TSRMLS_CC);
  621. } else {
  622. sapi_update_response_code(302 TSRMLS_CC);
  623. }
  624. }
  625. } else if (!STRCASECMP(header_line, "WWW-Authenticate")) { /* HTTP Authentication */
  626. sapi_update_response_code(401 TSRMLS_CC); /* authentication-required */
  627. myuid = php_getuid();
  628. efree(header_line);
  629. sapi_header.header_len = spprintf(&sapi_header.header, 0, "WWW-Authenticate: Basic realm=\"%ld\"", myuid);
  630. }
  631. if (sapi_header.header==header_line) {
  632. *colon_offset = ':';
  633. }
  634. }
  635. }
  636. if (http_response_code) {
  637. sapi_update_response_code(http_response_code TSRMLS_CC);
  638. }
  639. if (sapi_module.header_handler) {
  640. retval = sapi_module.header_handler(&sapi_header, op, &SG(sapi_headers) TSRMLS_CC);
  641. } else {
  642. retval = SAPI_HEADER_ADD;
  643. }
  644. if (retval & SAPI_HEADER_ADD) {
  645. /* in replace mode first remove the header if it already exists in the headers llist */
  646. if (op == SAPI_HEADER_REPLACE) {
  647. colon_offset = strchr(sapi_header.header, ':');
  648. if (colon_offset) {
  649. char sav;
  650. sav = *colon_offset;
  651. *colon_offset = 0;
  652. zend_llist_del_element(&SG(sapi_headers).headers, sapi_header.header, (int(*)(void*, void*))sapi_find_matching_header);
  653. *colon_offset = sav;
  654. }
  655. }
  656. zend_llist_add_element(&SG(sapi_headers).headers, (void *) &sapi_header);
  657. } else {
  658. sapi_free_header(&sapi_header);
  659. }
  660. return SUCCESS;
  661. }
  662. SAPI_API int sapi_send_headers(TSRMLS_D)
  663. {
  664. int retval;
  665. int ret = FAILURE;
  666. if (SG(headers_sent) || SG(request_info).no_headers) {
  667. return SUCCESS;
  668. }
  669. /* Success-oriented. We set headers_sent to 1 here to avoid an infinite loop
  670. * in case of an error situation.
  671. */
  672. if (SG(sapi_headers).send_default_content_type && sapi_module.send_headers) {
  673. sapi_header_struct default_header;
  674. sapi_get_default_content_type_header(&default_header TSRMLS_CC);
  675. sapi_add_header_ex(default_header.header, default_header.header_len, 0, 0 TSRMLS_CC);
  676. }
  677. SG(headers_sent) = 1;
  678. if (sapi_module.send_headers) {
  679. retval = sapi_module.send_headers(&SG(sapi_headers) TSRMLS_CC);
  680. } else {
  681. retval = SAPI_HEADER_DO_SEND;
  682. }
  683. switch (retval) {
  684. case SAPI_HEADER_SENT_SUCCESSFULLY:
  685. ret = SUCCESS;
  686. break;
  687. case SAPI_HEADER_DO_SEND: {
  688. sapi_header_struct http_status_line;
  689. char buf[255];
  690. if (SG(sapi_headers).http_status_line) {
  691. http_status_line.header = SG(sapi_headers).http_status_line;
  692. http_status_line.header_len = strlen(SG(sapi_headers).http_status_line);
  693. } else {
  694. http_status_line.header = buf;
  695. http_status_line.header_len = sprintf(buf, "HTTP/1.0 %d X", SG(sapi_headers).http_response_code);
  696. }
  697. sapi_module.send_header(&http_status_line, SG(server_context) TSRMLS_CC);
  698. }
  699. zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t) sapi_module.send_header, SG(server_context) TSRMLS_CC);
  700. if(SG(sapi_headers).send_default_content_type) {
  701. sapi_header_struct default_header;
  702. sapi_get_default_content_type_header(&default_header TSRMLS_CC);
  703. sapi_module.send_header(&default_header, SG(server_context) TSRMLS_CC);
  704. sapi_free_header(&default_header);
  705. }
  706. sapi_module.send_header(NULL, SG(server_context) TSRMLS_CC);
  707. ret = SUCCESS;
  708. break;
  709. case SAPI_HEADER_SEND_FAILED:
  710. SG(headers_sent) = 0;
  711. ret = FAILURE;
  712. break;
  713. }
  714. sapi_send_headers_free(TSRMLS_C);
  715. return ret;
  716. }
  717. SAPI_API int sapi_register_post_entries(sapi_post_entry *post_entries TSRMLS_DC)
  718. {
  719. sapi_post_entry *p=post_entries;
  720. while (p->content_type) {
  721. if (sapi_register_post_entry(p TSRMLS_CC) == FAILURE) {
  722. return FAILURE;
  723. }
  724. p++;
  725. }
  726. return SUCCESS;
  727. }
  728. SAPI_API int sapi_register_post_entry(sapi_post_entry *post_entry TSRMLS_DC)
  729. {
  730. if (SG(sapi_started) && EG(in_execution)) {
  731. return FAILURE;
  732. }
  733. return zend_hash_add(&SG(known_post_content_types),
  734. post_entry->content_type, post_entry->content_type_len+1,
  735. (void *) post_entry, sizeof(sapi_post_entry), NULL);
  736. }
  737. SAPI_API void sapi_unregister_post_entry(sapi_post_entry *post_entry TSRMLS_DC)
  738. {
  739. if (SG(sapi_started) && EG(in_execution)) {
  740. return;
  741. }
  742. zend_hash_del(&SG(known_post_content_types), post_entry->content_type,
  743. post_entry->content_type_len+1);
  744. }
  745. SAPI_API int sapi_register_default_post_reader(void (*default_post_reader)(TSRMLS_D))
  746. {
  747. TSRMLS_FETCH();
  748. if (SG(sapi_started) && EG(in_execution)) {
  749. return FAILURE;
  750. }
  751. sapi_module.default_post_reader = default_post_reader;
  752. return SUCCESS;
  753. }
  754. SAPI_API int sapi_register_treat_data(void (*treat_data)(int arg, char *str, zval *destArray TSRMLS_DC))
  755. {
  756. TSRMLS_FETCH();
  757. if (SG(sapi_started) && EG(in_execution)) {
  758. return FAILURE;
  759. }
  760. sapi_module.treat_data = treat_data;
  761. return SUCCESS;
  762. }
  763. 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))
  764. {
  765. TSRMLS_FETCH();
  766. if (SG(sapi_started) && EG(in_execution)) {
  767. return FAILURE;
  768. }
  769. sapi_module.input_filter = input_filter;
  770. sapi_module.input_filter_init = input_filter_init;
  771. return SUCCESS;
  772. }
  773. SAPI_API int sapi_flush(TSRMLS_D)
  774. {
  775. if (sapi_module.flush) {
  776. sapi_module.flush(SG(server_context));
  777. return SUCCESS;
  778. } else {
  779. return FAILURE;
  780. }
  781. }
  782. SAPI_API struct stat *sapi_get_stat(TSRMLS_D)
  783. {
  784. if (sapi_module.get_stat) {
  785. return sapi_module.get_stat(TSRMLS_C);
  786. } else {
  787. if (!SG(request_info).path_translated || (VCWD_STAT(SG(request_info).path_translated, &SG(global_stat)) == -1)) {
  788. return NULL;
  789. }
  790. return &SG(global_stat);
  791. }
  792. }
  793. SAPI_API char *sapi_getenv(char *name, size_t name_len TSRMLS_DC)
  794. {
  795. if (sapi_module.getenv) {
  796. char *value, *tmp = sapi_module.getenv(name, name_len TSRMLS_CC);
  797. if (tmp) {
  798. value = estrdup(tmp);
  799. } else {
  800. return NULL;
  801. }
  802. sapi_module.input_filter(PARSE_ENV, name, &value, strlen(value), NULL TSRMLS_CC);
  803. return value;
  804. }
  805. return NULL;
  806. }
  807. SAPI_API int sapi_get_fd(int *fd TSRMLS_DC)
  808. {
  809. if (sapi_module.get_fd) {
  810. return sapi_module.get_fd(fd TSRMLS_CC);
  811. } else {
  812. return FAILURE;
  813. }
  814. }
  815. SAPI_API int sapi_force_http_10(TSRMLS_D)
  816. {
  817. if (sapi_module.force_http_10) {
  818. return sapi_module.force_http_10(TSRMLS_C);
  819. } else {
  820. return FAILURE;
  821. }
  822. }
  823. SAPI_API int sapi_get_target_uid(uid_t *obj TSRMLS_DC)
  824. {
  825. if (sapi_module.get_target_uid) {
  826. return sapi_module.get_target_uid(obj TSRMLS_CC);
  827. } else {
  828. return FAILURE;
  829. }
  830. }
  831. SAPI_API int sapi_get_target_gid(gid_t *obj TSRMLS_DC)
  832. {
  833. if (sapi_module.get_target_gid) {
  834. return sapi_module.get_target_gid(obj TSRMLS_CC);
  835. } else {
  836. return FAILURE;
  837. }
  838. }
  839. SAPI_API time_t sapi_get_request_time(TSRMLS_D)
  840. {
  841. if(SG(global_request_time)) return SG(global_request_time);
  842. if (sapi_module.get_request_time && SG(server_context)) {
  843. SG(global_request_time) = sapi_module.get_request_time(TSRMLS_C);
  844. } else {
  845. SG(global_request_time) = time(0);
  846. }
  847. return SG(global_request_time);
  848. }
  849. SAPI_API void sapi_terminate_process(TSRMLS_D) {
  850. if (sapi_module.terminate_process) {
  851. sapi_module.terminate_process(TSRMLS_C);
  852. }
  853. }
  854. /*
  855. * Local variables:
  856. * tab-width: 4
  857. * c-basic-offset: 4
  858. * End:
  859. * vim600: sw=4 ts=4 fdm=marker
  860. * vim<600: sw=4 ts=4
  861. */