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.

572 lines
16 KiB

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