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.

603 lines
17 KiB

27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 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
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
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP version 4.0 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2001 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.02 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license/2_02.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. #include <ctype.h>
  21. #include <sys/stat.h>
  22. #include "php.h"
  23. #include "SAPI.h"
  24. #ifdef ZTS
  25. #include "TSRM.h"
  26. #endif
  27. #include "rfc1867.h"
  28. #ifdef PHP_WIN32
  29. #define STRCASECMP stricmp
  30. #else
  31. #define STRCASECMP strcasecmp
  32. #endif
  33. #include "php_content_types.h"
  34. static HashTable known_post_content_types;
  35. SAPI_API void (*sapi_error)(int error_type, const char *message, ...);
  36. #ifdef ZTS
  37. SAPI_API int sapi_globals_id;
  38. #else
  39. sapi_globals_struct sapi_globals;
  40. #endif
  41. static void sapi_globals_ctor(sapi_globals_struct *sapi_globals TSRMLS_DC)
  42. {
  43. memset(sapi_globals, 0, sizeof(*sapi_globals));
  44. }
  45. /* True globals (no need for thread safety) */
  46. SAPI_API sapi_module_struct sapi_module;
  47. SAPI_API void (*sapi_error)(int error_type, const char *message, ...);
  48. SAPI_API void sapi_startup(sapi_module_struct *sf)
  49. {
  50. sapi_module = *sf;
  51. zend_hash_init_ex(&known_post_content_types, 5, NULL, NULL, 1, 0);
  52. #ifdef ZTS
  53. ts_allocate_id(&sapi_globals_id, sizeof(sapi_globals_struct), (ts_allocate_ctor) sapi_globals_ctor, NULL);
  54. #else
  55. sapi_globals_ctor(&sapi_globals TSRMLS_CC);
  56. #endif
  57. #ifdef VIRTUAL_DIR
  58. virtual_cwd_startup(); /* Could use shutdown to free the main cwd but it would just slow it down for CGI */
  59. #endif
  60. #ifdef PHP_WIN32
  61. tsrm_win32_startup();
  62. #endif
  63. reentrancy_startup();
  64. }
  65. SAPI_API void sapi_shutdown(void)
  66. {
  67. reentrancy_shutdown();
  68. #ifdef VIRTUAL_DIR
  69. virtual_cwd_shutdown();
  70. #endif
  71. #ifdef PHP_WIN32
  72. tsrm_win32_shutdown();
  73. #endif
  74. zend_hash_destroy(&known_post_content_types);
  75. }
  76. SAPI_API void sapi_free_header(sapi_header_struct *sapi_header)
  77. {
  78. efree(sapi_header->header);
  79. }
  80. SAPI_API void sapi_handle_post(void *arg TSRMLS_DC)
  81. {
  82. if (SG(request_info).post_entry && SG(request_info).content_type_dup) {
  83. SG(request_info).post_entry->post_handler(SG(request_info).content_type_dup, arg TSRMLS_CC);
  84. if (SG(request_info).post_data) {
  85. efree(SG(request_info).post_data);
  86. SG(request_info).post_data = NULL;
  87. }
  88. efree(SG(request_info).content_type_dup);
  89. SG(request_info).content_type_dup = NULL;
  90. }
  91. }
  92. static void sapi_read_post_data(TSRMLS_D)
  93. {
  94. sapi_post_entry *post_entry;
  95. uint content_type_length = strlen(SG(request_info).content_type);
  96. char *content_type = estrndup(SG(request_info).content_type, content_type_length);
  97. char *p;
  98. char oldchar=0;
  99. void (*post_reader_func)(TSRMLS_D);
  100. /* dedicated implementation for increased performance:
  101. * - Make the content type lowercase
  102. * - Trim descriptive data, stay with the content-type only
  103. */
  104. for (p=content_type; p<content_type+content_type_length; p++) {
  105. switch (*p) {
  106. case ';':
  107. case ',':
  108. case ' ':
  109. content_type_length = p-content_type;
  110. oldchar = *p;
  111. *p = 0;
  112. break;
  113. default:
  114. *p = tolower(*p);
  115. break;
  116. }
  117. }
  118. if (zend_hash_find(&known_post_content_types, content_type, content_type_length+1, (void **) &post_entry)==SUCCESS) {
  119. SG(request_info).post_entry = post_entry;
  120. post_reader_func = post_entry->post_reader;
  121. } else {
  122. if (!sapi_module.default_post_reader) {
  123. sapi_module.sapi_error(E_WARNING, "Unsupported content type: '%s'", content_type);
  124. return;
  125. }
  126. SG(request_info).post_entry = NULL;
  127. post_reader_func = sapi_module.default_post_reader;
  128. }
  129. if (oldchar) {
  130. *(p-1) = oldchar;
  131. }
  132. post_reader_func(TSRMLS_C);
  133. SG(request_info).content_type_dup = content_type;
  134. if(PG(always_populate_raw_post_data) && sapi_module.default_post_reader) {
  135. sapi_module.default_post_reader(TSRMLS_C);
  136. }
  137. }
  138. SAPI_POST_READER_FUNC(sapi_read_standard_form_data)
  139. {
  140. int read_bytes;
  141. int allocated_bytes=SAPI_POST_BLOCK_SIZE+1;
  142. if (SG(request_info).content_length > SG(post_max_size)) {
  143. php_error(E_WARNING, "POST Content-Length of %d bytes exceeds the limit of %d bytes",
  144. SG(request_info).content_length, SG(post_max_size));
  145. return;
  146. }
  147. SG(request_info).post_data = emalloc(allocated_bytes);
  148. for (;;) {
  149. read_bytes = sapi_module.read_post(SG(request_info).post_data+SG(read_post_bytes), SAPI_POST_BLOCK_SIZE TSRMLS_CC);
  150. if (read_bytes<=0) {
  151. break;
  152. }
  153. SG(read_post_bytes) += read_bytes;
  154. if (SG(read_post_bytes) > SG(post_max_size)) {
  155. php_error(E_WARNING, "Actual POST length does not match Content-Length, and exceeds %d bytes", SG(post_max_size));
  156. return;
  157. }
  158. if (read_bytes < SAPI_POST_BLOCK_SIZE) {
  159. break;
  160. }
  161. if (SG(read_post_bytes)+SAPI_POST_BLOCK_SIZE >= allocated_bytes) {
  162. allocated_bytes = SG(read_post_bytes)+SAPI_POST_BLOCK_SIZE+1;
  163. SG(request_info).post_data = erealloc(SG(request_info).post_data, allocated_bytes);
  164. }
  165. }
  166. SG(request_info).post_data[SG(read_post_bytes)] = 0; /* terminating NULL */
  167. SG(request_info).post_data_length = SG(read_post_bytes);
  168. }
  169. SAPI_API char *sapi_get_default_content_type(TSRMLS_D)
  170. {
  171. char *mimetype, *charset, *content_type;
  172. mimetype = SG(default_mimetype) ? SG(default_mimetype) : SAPI_DEFAULT_MIMETYPE;
  173. charset = SG(default_charset) ? SG(default_charset) : SAPI_DEFAULT_CHARSET;
  174. if (strncasecmp(mimetype, "text/", 5) == 0 && *charset) {
  175. int len = strlen(mimetype) + sizeof("; charset=") + strlen(charset);
  176. content_type = emalloc(len);
  177. snprintf(content_type, len, "%s; charset=%s", mimetype, charset);
  178. } else {
  179. content_type = estrdup(mimetype);
  180. }
  181. return content_type;
  182. }
  183. SAPI_API void sapi_get_default_content_type_header(sapi_header_struct *default_header TSRMLS_DC)
  184. {
  185. char *default_content_type = sapi_get_default_content_type(TSRMLS_C);
  186. int default_content_type_len = strlen(default_content_type);
  187. default_header->header_len = (sizeof("Content-type: ")-1) + default_content_type_len;
  188. default_header->header = emalloc(default_header->header_len+1);
  189. memcpy(default_header->header, "Content-type: ", sizeof("Content-type: "));
  190. memcpy(default_header->header+sizeof("Content-type: ")-1, default_content_type, default_content_type_len);
  191. default_header->header[default_header->header_len] = 0;
  192. efree(default_content_type);
  193. }
  194. /*
  195. * Add charset on content-type header if the MIME type starts with
  196. * "text/", the default_charset directive is not empty and
  197. * there is not already a charset option in there.
  198. *
  199. * If "mimetype" is non-NULL, it should point to a pointer allocated
  200. * with emalloc(). If a charset is added, the string will be
  201. * re-allocated and the new length is returned. If mimetype is
  202. * unchanged, 0 is returned.
  203. *
  204. */
  205. SAPI_API size_t sapi_apply_default_charset(char **mimetype, size_t len TSRMLS_DC)
  206. {
  207. char *charset, *newtype;
  208. size_t newlen;
  209. charset = SG(default_charset) ? SG(default_charset) : SAPI_DEFAULT_CHARSET;
  210. if (*charset && strncmp(*mimetype, "text/", 5) == 0 && strstr(*mimetype, "charset=") == NULL) {
  211. newlen = len + (sizeof(";charset=")-1) + strlen(charset);
  212. newtype = emalloc(newlen + 1);
  213. PHP_STRLCPY(newtype, *mimetype, newlen + 1, len);
  214. strlcat(newtype, ";charset=", newlen + 1);
  215. if (*mimetype != NULL) {
  216. efree(*mimetype);
  217. }
  218. *mimetype = newtype;
  219. return newlen;
  220. }
  221. return 0;
  222. }
  223. /*
  224. * Called from php_request_startup() for every request.
  225. */
  226. SAPI_API void sapi_activate(TSRMLS_D)
  227. {
  228. zend_llist_init(&SG(sapi_headers).headers, sizeof(sapi_header_struct), (void (*)(void *)) sapi_free_header, 0);
  229. SG(sapi_headers).send_default_content_type = 1;
  230. /*
  231. SG(sapi_headers).http_response_code = 200;
  232. */
  233. SG(sapi_headers).http_status_line = NULL;
  234. SG(headers_sent) = 0;
  235. SG(read_post_bytes) = 0;
  236. SG(request_info).post_data = NULL;
  237. SG(request_info).current_user = NULL;
  238. SG(request_info).current_user_length = 0;
  239. SG(request_info).no_headers = 0;
  240. /* It's possible to override this general case in the activate() callback, if
  241. * necessary.
  242. */
  243. if (SG(request_info).request_method && !strcmp(SG(request_info).request_method, "HEAD")) {
  244. SG(request_info).headers_only = 1;
  245. } else {
  246. SG(request_info).headers_only = 0;
  247. }
  248. SG(rfc1867_uploaded_files) = NULL;
  249. if (SG(server_context)) {
  250. if (SG(request_info).request_method
  251. && !strcmp(SG(request_info).request_method, "POST")) {
  252. if (!SG(request_info).content_type) {
  253. sapi_module.sapi_error(E_WARNING, "No content-type in POST request");
  254. SG(request_info).content_type_dup = NULL;
  255. } else {
  256. sapi_read_post_data(TSRMLS_C);
  257. }
  258. } else {
  259. SG(request_info).content_type_dup = NULL;
  260. }
  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. SAPI_API void sapi_deactivate(TSRMLS_D)
  268. {
  269. zend_llist_destroy(&SG(sapi_headers).headers);
  270. if (SG(request_info).post_data) {
  271. efree(SG(request_info).post_data);
  272. }
  273. if (SG(request_info).auth_user) {
  274. efree(SG(request_info).auth_user);
  275. }
  276. if (SG(request_info).auth_password) {
  277. efree(SG(request_info).auth_password);
  278. }
  279. if (SG(request_info).content_type_dup) {
  280. efree(SG(request_info).content_type_dup);
  281. }
  282. if (SG(request_info).current_user) {
  283. efree(SG(request_info).current_user);
  284. }
  285. if (sapi_module.deactivate) {
  286. sapi_module.deactivate(TSRMLS_C);
  287. }
  288. if (SG(rfc1867_uploaded_files)) {
  289. destroy_uploaded_files_hash(TSRMLS_C);
  290. }
  291. }
  292. SAPI_API void sapi_initialize_empty_request(TSRMLS_D)
  293. {
  294. SG(server_context) = NULL;
  295. SG(request_info).request_method = NULL;
  296. SG(request_info).auth_user = SG(request_info).auth_password = NULL;
  297. SG(request_info).content_type_dup = NULL;
  298. }
  299. static int sapi_extract_response_code(const char *header_line)
  300. {
  301. int code = 200;
  302. const char *ptr;
  303. for (ptr = header_line; *ptr; ptr++) {
  304. if (*ptr == ' ' && *(ptr + 1) != ' ') {
  305. code = atoi(ptr + 1);
  306. break;
  307. }
  308. }
  309. return code;
  310. }
  311. /* This function expects a *duplicated* string, that was previously emalloc()'d.
  312. * Pointers sent to this functions will be automatically freed by the framework.
  313. */
  314. SAPI_API int sapi_add_header_ex(char *header_line, uint header_line_len, zend_bool duplicate, zend_bool replace)
  315. {
  316. int retval, free_header = 0;
  317. sapi_header_struct sapi_header;
  318. char *colon_offset;
  319. TSRMLS_FETCH();
  320. if (SG(headers_sent) && !SG(request_info).no_headers) {
  321. char *output_start_filename = php_get_output_start_filename();
  322. int output_start_lineno = php_get_output_start_lineno();
  323. if (output_start_filename) {
  324. sapi_module.sapi_error(E_WARNING, "Cannot add header information - headers already sent by (output started at %s:%d)",
  325. output_start_filename, output_start_lineno);
  326. } else {
  327. sapi_module.sapi_error(E_WARNING, "Cannot add header information - headers already sent");
  328. }
  329. if (!duplicate) {
  330. efree(header_line);
  331. }
  332. return FAILURE;
  333. }
  334. if (duplicate) {
  335. header_line = estrndup(header_line, header_line_len);
  336. }
  337. /* cut of trailing spaces, linefeeds and carriage-returns */
  338. while(isspace(header_line[header_line_len-1]))
  339. header_line[--header_line_len]='\0';
  340. sapi_header.header = header_line;
  341. sapi_header.header_len = header_line_len;
  342. sapi_header.replace = replace;
  343. /* Check the header for a few cases that we have special support for in SAPI */
  344. if (header_line_len>=5
  345. && !strncasecmp(header_line, "HTTP/", 5)) {
  346. /* filter out the response code */
  347. SG(sapi_headers).http_response_code = sapi_extract_response_code(header_line);
  348. SG(sapi_headers).http_status_line = header_line;
  349. return SUCCESS;
  350. } else {
  351. colon_offset = strchr(header_line, ':');
  352. if (colon_offset) {
  353. *colon_offset = 0;
  354. if (!STRCASECMP(header_line, "Content-Type")) {
  355. char *ptr = colon_offset, *mimetype = NULL, *newheader;
  356. size_t len = header_line_len - (ptr - header_line), newlen;
  357. while (*ptr == ' ' && *ptr != '\0') {
  358. ptr++;
  359. }
  360. mimetype = estrdup(ptr);
  361. newlen = sapi_apply_default_charset(&mimetype, len TSRMLS_CC);
  362. if (newlen != 0) {
  363. newlen += sizeof("Content-type: ");
  364. newheader = emalloc(newlen);
  365. PHP_STRLCPY(newheader, "Content-type: ", newlen, sizeof("Content-type: ")-1);
  366. strlcat(newheader, mimetype, newlen);
  367. sapi_header.header = newheader;
  368. sapi_header.header_len = newlen - 1;
  369. colon_offset = strchr(newheader, ':');
  370. *colon_offset = '\0';
  371. free_header = 1;
  372. }
  373. efree(mimetype);
  374. SG(sapi_headers).send_default_content_type = 0;
  375. } else if (!STRCASECMP(header_line, "Location")) {
  376. if (SG(sapi_headers).http_response_code < 300 ||
  377. SG(sapi_headers).http_response_code > 307) {
  378. /* Return a Found Redirect if one is not already specified */
  379. SG(sapi_headers).http_response_code = 302;
  380. }
  381. } else if (!STRCASECMP(header_line, "WWW-Authenticate")) { /* HTTP Authentication */
  382. SG(sapi_headers).http_response_code = 401; /* authentication-required */
  383. }
  384. *colon_offset = ':';
  385. }
  386. }
  387. if (sapi_module.header_handler) {
  388. retval = sapi_module.header_handler(&sapi_header, &SG(sapi_headers) TSRMLS_CC);
  389. } else {
  390. retval = SAPI_HEADER_ADD;
  391. }
  392. if (retval & SAPI_HEADER_DELETE_ALL) {
  393. zend_llist_clean(&SG(sapi_headers).headers);
  394. }
  395. if (retval & SAPI_HEADER_ADD) {
  396. zend_llist_add_element(&SG(sapi_headers).headers, (void *) &sapi_header);
  397. }
  398. if (free_header) {
  399. efree(sapi_header.header);
  400. }
  401. return SUCCESS;
  402. }
  403. SAPI_API int sapi_send_headers()
  404. {
  405. int retval;
  406. int ret = FAILURE;
  407. TSRMLS_FETCH();
  408. if (SG(headers_sent) || SG(request_info).no_headers) {
  409. return SUCCESS;
  410. }
  411. /* Success-oriented. We set headers_sent to 1 here to avoid an infinite loop
  412. * in case of an error situation.
  413. */
  414. SG(headers_sent) = 1;
  415. if (sapi_module.send_headers) {
  416. retval = sapi_module.send_headers(&SG(sapi_headers) TSRMLS_CC);
  417. } else {
  418. retval = SAPI_HEADER_DO_SEND;
  419. }
  420. switch (retval) {
  421. case SAPI_HEADER_SENT_SUCCESSFULLY:
  422. ret = SUCCESS;
  423. break;
  424. case SAPI_HEADER_DO_SEND:
  425. if (SG(sapi_headers).http_status_line) {
  426. sapi_header_struct http_status_line;
  427. http_status_line.header = SG(sapi_headers).http_status_line;
  428. http_status_line.header_len = strlen(SG(sapi_headers).http_status_line);
  429. sapi_module.send_header(&http_status_line, SG(server_context));
  430. }
  431. zend_llist_apply_with_argument(&SG(sapi_headers).headers, (void (*)(void *, void *)) sapi_module.send_header, SG(server_context));
  432. if(SG(sapi_headers).send_default_content_type) {
  433. sapi_header_struct default_header;
  434. sapi_get_default_content_type_header(&default_header TSRMLS_CC);
  435. sapi_module.send_header(&default_header, SG(server_context));
  436. sapi_free_header(&default_header);
  437. }
  438. sapi_module.send_header(NULL, SG(server_context));
  439. ret = SUCCESS;
  440. break;
  441. case SAPI_HEADER_SEND_FAILED:
  442. SG(headers_sent) = 0;
  443. ret = FAILURE;
  444. break;
  445. }
  446. if (SG(sapi_headers).http_status_line) {
  447. efree(SG(sapi_headers).http_status_line);
  448. }
  449. return ret;
  450. }
  451. SAPI_API int sapi_register_post_entries(sapi_post_entry *post_entries)
  452. {
  453. sapi_post_entry *p=post_entries;
  454. while (p->content_type) {
  455. if (sapi_register_post_entry(p)==FAILURE) {
  456. return FAILURE;
  457. }
  458. p++;
  459. }
  460. return SUCCESS;
  461. }
  462. SAPI_API int sapi_register_post_entry(sapi_post_entry *post_entry)
  463. {
  464. 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);
  465. }
  466. SAPI_API void sapi_unregister_post_entry(sapi_post_entry *post_entry)
  467. {
  468. zend_hash_del(&known_post_content_types, post_entry->content_type, post_entry->content_type_len+1);
  469. }
  470. SAPI_API int sapi_register_default_post_reader(void (*default_post_reader)(TSRMLS_D))
  471. {
  472. sapi_module.default_post_reader = default_post_reader;
  473. return SUCCESS;
  474. }
  475. SAPI_API int sapi_flush()
  476. {
  477. if (sapi_module.flush) {
  478. TSRMLS_FETCH();
  479. sapi_module.flush(SG(server_context));
  480. return SUCCESS;
  481. } else {
  482. return FAILURE;
  483. }
  484. }
  485. SAPI_API struct stat *sapi_get_stat()
  486. {
  487. TSRMLS_FETCH();
  488. if (sapi_module.get_stat) {
  489. return sapi_module.get_stat(TSRMLS_C);
  490. } else {
  491. if (!SG(request_info).path_translated || (VCWD_STAT(SG(request_info).path_translated, &SG(global_stat))==-1)) {
  492. return NULL;
  493. }
  494. return &SG(global_stat);
  495. }
  496. }
  497. SAPI_API char *sapi_getenv(char *name, size_t name_len)
  498. {
  499. if (sapi_module.getenv) {
  500. TSRMLS_FETCH();
  501. return sapi_module.getenv(name, name_len TSRMLS_CC);
  502. } else {
  503. return NULL;
  504. }
  505. }
  506. /*
  507. * Local variables:
  508. * tab-width: 4
  509. * c-basic-offset: 4
  510. * End:
  511. * vim600: sw=4 ts=4 tw=78 fdm=marker
  512. * vim<600: sw=4 ts=4 tw=78
  513. */