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.

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