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.

635 lines
18 KiB

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