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.

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