SAPI.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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. #ifdef ZTS
  33. #include "TSRM.h"
  34. #endif
  35. #ifdef HAVE_SYS_TIME_H
  36. #include <sys/time.h>
  37. #elif defined(PHP_WIN32)
  38. #include "win32/time.h"
  39. #endif
  40. #include "rfc1867.h"
  41. #ifdef PHP_WIN32
  42. #define STRCASECMP stricmp
  43. #else
  44. #define STRCASECMP strcasecmp
  45. #endif
  46. #include "php_content_types.h"
  47. #ifdef ZTS
  48. SAPI_API int sapi_globals_id;
  49. #else
  50. sapi_globals_struct sapi_globals;
  51. #endif
  52. static void sapi_globals_ctor(sapi_globals_struct *sapi_globals TSRMLS_DC)
  53. {
  54. memset(sapi_globals, 0, sizeof(*sapi_globals));
  55. zend_hash_init_ex(&sapi_globals->known_post_content_types, 5, NULL, NULL, 1, 0);
  56. php_setup_sapi_content_types(TSRMLS_C);
  57. }
  58. static void sapi_globals_dtor(sapi_globals_struct *sapi_globals TSRMLS_DC)
  59. {
  60. zend_hash_destroy(&sapi_globals->known_post_content_types);
  61. }
  62. /* True globals (no need for thread safety) */
  63. SAPI_API sapi_module_struct sapi_module;
  64. SAPI_API void sapi_startup(sapi_module_struct *sf)
  65. {
  66. #ifdef ZEND_SIGNALS
  67. zend_signal_startup();
  68. #endif
  69. sf->ini_entries = NULL;
  70. sapi_module = *sf;
  71. #ifdef ZTS
  72. ts_allocate_id(&sapi_globals_id, sizeof(sapi_globals_struct), (ts_allocate_ctor) sapi_globals_ctor, (ts_allocate_dtor) sapi_globals_dtor);
  73. # ifdef PHP_WIN32
  74. _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
  75. # endif
  76. #else
  77. sapi_globals_ctor(&sapi_globals);
  78. #endif
  79. #ifdef PHP_WIN32
  80. tsrm_win32_startup();
  81. #endif
  82. reentrancy_startup();
  83. }
  84. SAPI_API void sapi_shutdown(void)
  85. {
  86. #ifdef ZTS
  87. ts_free_id(sapi_globals_id);
  88. #else
  89. sapi_globals_dtor(&sapi_globals);
  90. #endif
  91. reentrancy_shutdown();
  92. #ifdef PHP_WIN32
  93. tsrm_win32_shutdown();
  94. #endif
  95. }
  96. SAPI_API void sapi_free_header(sapi_header_struct *sapi_header)
  97. {
  98. efree(sapi_header->header);
  99. }
  100. /* {{{ proto bool header_register_callback(mixed callback)
  101. call a header function */
  102. PHP_FUNCTION(header_register_callback)
  103. {
  104. zval *callback_func;
  105. char *callback_name;
  106. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &callback_func) == FAILURE) {
  107. return;
  108. }
  109. if (!zend_is_callable(callback_func, 0, &callback_name TSRMLS_CC)) {
  110. efree(callback_name);
  111. RETURN_FALSE;
  112. }
  113. efree(callback_name);
  114. if (SG(callback_func)) {
  115. zval_ptr_dtor(&SG(callback_func));
  116. SG(fci_cache) = empty_fcall_info_cache;
  117. }
  118. SG(callback_func) = callback_func;
  119. Z_ADDREF_P(SG(callback_func));
  120. RETURN_TRUE;
  121. }
  122. /* }}} */
  123. static void sapi_run_header_callback(TSRMLS_D)
  124. {
  125. int error;
  126. zend_fcall_info fci;
  127. char *callback_name = NULL;
  128. char *callback_error = NULL;
  129. zval *retval_ptr = NULL;
  130. if (zend_fcall_info_init(SG(callback_func), 0, &fci, &SG(fci_cache), &callback_name, &callback_error TSRMLS_CC) == SUCCESS) {
  131. fci.retval_ptr_ptr = &retval_ptr;
  132. error = zend_call_function(&fci, &SG(fci_cache) TSRMLS_CC);
  133. if (error == FAILURE) {
  134. goto callback_failed;
  135. } else if (retval_ptr) {
  136. zval_ptr_dtor(&retval_ptr);
  137. }
  138. } else {
  139. callback_failed:
  140. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the sapi_header_callback");
  141. }
  142. if (callback_name) {
  143. efree(callback_name);
  144. }
  145. if (callback_error) {
  146. efree(callback_error);
  147. }
  148. }
  149. SAPI_API void sapi_handle_post(void *arg TSRMLS_DC)
  150. {
  151. if (SG(request_info).post_entry && SG(request_info).content_type_dup) {
  152. SG(request_info).post_entry->post_handler(SG(request_info).content_type_dup, arg TSRMLS_CC);
  153. efree(SG(request_info).content_type_dup);
  154. SG(request_info).content_type_dup = NULL;
  155. }
  156. }
  157. static void sapi_read_post_data(TSRMLS_D)
  158. {
  159. sapi_post_entry *post_entry;
  160. uint content_type_length = strlen(SG(request_info).content_type);
  161. char *content_type = estrndup(SG(request_info).content_type, content_type_length);
  162. char *p;
  163. char oldchar=0;
  164. void (*post_reader_func)(TSRMLS_D) = NULL;
  165. /* dedicated implementation for increased performance:
  166. * - Make the content type lowercase
  167. * - Trim descriptive data, stay with the content-type only
  168. */
  169. for (p=content_type; p<content_type+content_type_length; p++) {
  170. switch (*p) {
  171. case ';':
  172. case ',':
  173. case ' ':
  174. content_type_length = p-content_type;
  175. oldchar = *p;
  176. *p = 0;
  177. break;
  178. default:
  179. *p = tolower(*p);
  180. break;
  181. }
  182. }
  183. /* now try to find an appropriate POST content handler */
  184. if (zend_hash_find(&SG(known_post_content_types), content_type,
  185. content_type_length+1, (void **) &post_entry) == SUCCESS) {
  186. /* found one, register it for use */
  187. SG(request_info).post_entry = post_entry;
  188. post_reader_func = post_entry->post_reader;
  189. } else {
  190. /* fallback */
  191. SG(request_info).post_entry = NULL;
  192. if (!sapi_module.default_post_reader) {
  193. /* no default reader ? */
  194. SG(request_info).content_type_dup = NULL;
  195. sapi_module.sapi_error(E_WARNING, "Unsupported content type: '%s'", content_type);
  196. return;
  197. }
  198. }
  199. if (oldchar) {
  200. *(p-1) = oldchar;
  201. }
  202. SG(request_info).content_type_dup = content_type;
  203. if(post_reader_func) {
  204. post_reader_func(TSRMLS_C);
  205. }
  206. if(sapi_module.default_post_reader) {
  207. sapi_module.default_post_reader(TSRMLS_C);
  208. }
  209. }
  210. SAPI_API int sapi_read_post_block(char *buffer, size_t buflen TSRMLS_DC)
  211. {
  212. int read_bytes;
  213. if (!sapi_module.read_post) {
  214. return -1;
  215. }
  216. read_bytes = sapi_module.read_post(buffer, buflen TSRMLS_CC);
  217. if (read_bytes > 0) {
  218. /* gogo */
  219. SG(read_post_bytes) += read_bytes;
  220. }
  221. if (read_bytes < buflen) {
  222. /* done */
  223. SG(post_read) = 1;
  224. }
  225. return read_bytes;
  226. }
  227. SAPI_API SAPI_POST_READER_FUNC(sapi_read_standard_form_data)
  228. {
  229. if ((SG(post_max_size) > 0) && (SG(request_info).content_length > SG(post_max_size))) {
  230. php_error_docref(NULL TSRMLS_CC, E_WARNING, "POST Content-Length of %ld bytes exceeds the limit of %ld bytes",
  231. SG(request_info).content_length, SG(post_max_size));
  232. return;
  233. }
  234. SG(request_info).request_body = php_stream_temp_create_ex(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE, PG(upload_tmp_dir));
  235. if (sapi_module.read_post) {
  236. int read_bytes;
  237. for (;;) {
  238. char buffer[SAPI_POST_BLOCK_SIZE];
  239. read_bytes = sapi_read_post_block(buffer, SAPI_POST_BLOCK_SIZE TSRMLS_CC);
  240. if (read_bytes > 0) {
  241. if (php_stream_write(SG(request_info).request_body, buffer, read_bytes) != read_bytes) {
  242. /* if parts of the stream can't be written, purge it completely */
  243. php_stream_truncate_set_size(SG(request_info).request_body, 0);
  244. php_error_docref(NULL TSRMLS_CC, E_WARNING, "POST data can't be buffered; all data discarded");
  245. break;
  246. }
  247. }
  248. if ((SG(post_max_size) > 0) && (SG(read_post_bytes) > SG(post_max_size))) {
  249. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Actual POST length does not match Content-Length, and exceeds %ld bytes", SG(post_max_size));
  250. break;
  251. }
  252. if (read_bytes < SAPI_POST_BLOCK_SIZE) {
  253. /* done */
  254. break;
  255. }
  256. }
  257. php_stream_rewind(SG(request_info).request_body);
  258. }
  259. }
  260. static inline char *get_default_content_type(uint prefix_len, uint *len TSRMLS_DC)
  261. {
  262. char *mimetype, *charset, *content_type;
  263. uint mimetype_len, charset_len;
  264. if (SG(default_mimetype)) {
  265. mimetype = SG(default_mimetype);
  266. mimetype_len = strlen(SG(default_mimetype));
  267. } else {
  268. mimetype = SAPI_DEFAULT_MIMETYPE;
  269. mimetype_len = sizeof(SAPI_DEFAULT_MIMETYPE) - 1;
  270. }
  271. if (SG(default_charset)) {
  272. charset = SG(default_charset);
  273. charset_len = strlen(SG(default_charset));
  274. } else {
  275. charset = SAPI_DEFAULT_CHARSET;
  276. charset_len = sizeof(SAPI_DEFAULT_CHARSET) - 1;
  277. }
  278. if (*charset && strncasecmp(mimetype, "text/", 5) == 0) {
  279. char *p;
  280. *len = prefix_len + mimetype_len + sizeof("; charset=") - 1 + charset_len;
  281. content_type = (char*)emalloc(*len + 1);
  282. p = content_type + prefix_len;
  283. memcpy(p, mimetype, mimetype_len);
  284. p += mimetype_len;
  285. memcpy(p, "; charset=", sizeof("; charset=") - 1);
  286. p += sizeof("; charset=") - 1;
  287. memcpy(p, charset, charset_len + 1);
  288. } else {
  289. *len = prefix_len + mimetype_len;
  290. content_type = (char*)emalloc(*len + 1);
  291. memcpy(content_type + prefix_len, mimetype, mimetype_len + 1);
  292. }
  293. return content_type;
  294. }
  295. SAPI_API char *sapi_get_default_content_type(TSRMLS_D)
  296. {
  297. uint len;
  298. return get_default_content_type(0, &len TSRMLS_CC);
  299. }
  300. SAPI_API void sapi_get_default_content_type_header(sapi_header_struct *default_header TSRMLS_DC)
  301. {
  302. uint len;
  303. default_header->header = get_default_content_type(sizeof("Content-type: ")-1, &len TSRMLS_CC);
  304. default_header->header_len = len;
  305. memcpy(default_header->header, "Content-type: ", sizeof("Content-type: ") - 1);
  306. }
  307. /*
  308. * Add charset on content-type header if the MIME type starts with
  309. * "text/", the default_charset directive is not empty and
  310. * there is not already a charset option in there.
  311. *
  312. * If "mimetype" is non-NULL, it should point to a pointer allocated
  313. * with emalloc(). If a charset is added, the string will be
  314. * re-allocated and the new length is returned. If mimetype is
  315. * unchanged, 0 is returned.
  316. *
  317. */
  318. SAPI_API size_t sapi_apply_default_charset(char **mimetype, size_t len TSRMLS_DC)
  319. {
  320. char *charset, *newtype;
  321. size_t newlen;
  322. charset = SG(default_charset) ? SG(default_charset) : SAPI_DEFAULT_CHARSET;
  323. if (*mimetype != NULL) {
  324. if (*charset && strncmp(*mimetype, "text/", 5) == 0 && strstr(*mimetype, "charset=") == NULL) {
  325. newlen = len + (sizeof(";charset=")-1) + strlen(charset);
  326. newtype = emalloc(newlen + 1);
  327. PHP_STRLCPY(newtype, *mimetype, newlen + 1, len);
  328. strlcat(newtype, ";charset=", newlen + 1);
  329. strlcat(newtype, charset, newlen + 1);
  330. efree(*mimetype);
  331. *mimetype = newtype;
  332. return newlen;
  333. }
  334. }
  335. return 0;
  336. }
  337. SAPI_API void sapi_activate_headers_only(TSRMLS_D)
  338. {
  339. if (SG(request_info).headers_read == 1)
  340. return;
  341. SG(request_info).headers_read = 1;
  342. zend_llist_init(&SG(sapi_headers).headers, sizeof(sapi_header_struct),
  343. (void (*)(void *)) sapi_free_header, 0);
  344. SG(sapi_headers).send_default_content_type = 1;
  345. /* SG(sapi_headers).http_response_code = 200; */
  346. SG(sapi_headers).http_status_line = NULL;
  347. SG(sapi_headers).mimetype = NULL;
  348. SG(read_post_bytes) = 0;
  349. SG(request_info).request_body = NULL;
  350. SG(request_info).current_user = NULL;
  351. SG(request_info).current_user_length = 0;
  352. SG(request_info).no_headers = 0;
  353. SG(request_info).post_entry = NULL;
  354. SG(global_request_time) = 0;
  355. /*
  356. * It's possible to override this general case in the activate() callback,
  357. * if necessary.
  358. */
  359. if (SG(request_info).request_method && !strcmp(SG(request_info).request_method, "HEAD")) {
  360. SG(request_info).headers_only = 1;
  361. } else {
  362. SG(request_info).headers_only = 0;
  363. }
  364. if (SG(server_context)) {
  365. SG(request_info).cookie_data = sapi_module.read_cookies(TSRMLS_C);
  366. if (sapi_module.activate) {
  367. sapi_module.activate(TSRMLS_C);
  368. }
  369. }
  370. if (sapi_module.input_filter_init ) {
  371. sapi_module.input_filter_init(TSRMLS_C);
  372. }
  373. }
  374. /*
  375. * Called from php_request_startup() for every request.
  376. */
  377. SAPI_API void sapi_activate(TSRMLS_D)
  378. {
  379. zend_llist_init(&SG(sapi_headers).headers, sizeof(sapi_header_struct), (void (*)(void *)) sapi_free_header, 0);
  380. SG(sapi_headers).send_default_content_type = 1;
  381. /*
  382. SG(sapi_headers).http_response_code = 200;
  383. */
  384. SG(sapi_headers).http_status_line = NULL;
  385. SG(sapi_headers).mimetype = NULL;
  386. SG(headers_sent) = 0;
  387. SG(callback_run) = 0;
  388. SG(callback_func) = NULL;
  389. SG(read_post_bytes) = 0;
  390. SG(request_info).request_body = NULL;
  391. SG(request_info).current_user = NULL;
  392. SG(request_info).current_user_length = 0;
  393. SG(request_info).no_headers = 0;
  394. SG(request_info).post_entry = NULL;
  395. SG(request_info).proto_num = 1000; /* Default to HTTP 1.0 */
  396. SG(global_request_time) = 0;
  397. SG(post_read) = 0;
  398. /* It's possible to override this general case in the activate() callback, if necessary. */
  399. if (SG(request_info).request_method && !strcmp(SG(request_info).request_method, "HEAD")) {
  400. SG(request_info).headers_only = 1;
  401. } else {
  402. SG(request_info).headers_only = 0;
  403. }
  404. SG(rfc1867_uploaded_files) = NULL;
  405. /* Handle request method */
  406. if (SG(server_context)) {
  407. if (PG(enable_post_data_reading)
  408. && SG(request_info).content_type
  409. && SG(request_info).request_method
  410. && !strcmp(SG(request_info).request_method, "POST")) {
  411. /* HTTP POST may contain form data to be processed into variables
  412. * depending on given content type */
  413. sapi_read_post_data(TSRMLS_C);
  414. } else {
  415. SG(request_info).content_type_dup = NULL;
  416. }
  417. /* Cookies */
  418. SG(request_info).cookie_data = sapi_module.read_cookies(TSRMLS_C);
  419. if (sapi_module.activate) {
  420. sapi_module.activate(TSRMLS_C);
  421. }
  422. }
  423. if (sapi_module.input_filter_init) {
  424. sapi_module.input_filter_init(TSRMLS_C);
  425. }
  426. }
  427. static void sapi_send_headers_free(TSRMLS_D)
  428. {
  429. if (SG(sapi_headers).http_status_line) {
  430. efree(SG(sapi_headers).http_status_line);
  431. SG(sapi_headers).http_status_line = NULL;
  432. }
  433. }
  434. SAPI_API void sapi_deactivate(TSRMLS_D)
  435. {
  436. zend_llist_destroy(&SG(sapi_headers).headers);
  437. if (SG(request_info).request_body) {
  438. SG(request_info).request_body = NULL;
  439. } else if (SG(server_context)) {
  440. if (!SG(post_read)) {
  441. /* make sure we've consumed all request input data */
  442. char dummy[SAPI_POST_BLOCK_SIZE];
  443. int read_bytes;
  444. do {
  445. read_bytes = sapi_read_post_block(dummy, SAPI_POST_BLOCK_SIZE TSRMLS_CC);
  446. } while (SAPI_POST_BLOCK_SIZE == read_bytes);
  447. }
  448. }
  449. if (SG(request_info).auth_user) {
  450. efree(SG(request_info).auth_user);
  451. }
  452. if (SG(request_info).auth_password) {
  453. efree(SG(request_info).auth_password);
  454. }
  455. if (SG(request_info).auth_digest) {
  456. efree(SG(request_info).auth_digest);
  457. }
  458. if (SG(request_info).content_type_dup) {
  459. efree(SG(request_info).content_type_dup);
  460. }
  461. if (SG(request_info).current_user) {
  462. efree(SG(request_info).current_user);
  463. }
  464. if (sapi_module.deactivate) {
  465. sapi_module.deactivate(TSRMLS_C);
  466. }
  467. if (SG(rfc1867_uploaded_files)) {
  468. destroy_uploaded_files_hash(TSRMLS_C);
  469. }
  470. if (SG(sapi_headers).mimetype) {
  471. efree(SG(sapi_headers).mimetype);
  472. SG(sapi_headers).mimetype = NULL;
  473. }
  474. sapi_send_headers_free(TSRMLS_C);
  475. SG(sapi_started) = 0;
  476. SG(headers_sent) = 0;
  477. SG(callback_run) = 0;
  478. if (SG(callback_func)) {
  479. zval_ptr_dtor(&SG(callback_func));
  480. }
  481. SG(request_info).headers_read = 0;
  482. SG(global_request_time) = 0;
  483. }
  484. SAPI_API void sapi_initialize_empty_request(TSRMLS_D)
  485. {
  486. SG(server_context) = NULL;
  487. SG(request_info).request_method = NULL;
  488. SG(request_info).auth_digest = SG(request_info).auth_user = SG(request_info).auth_password = NULL;
  489. SG(request_info).content_type_dup = NULL;
  490. }
  491. static int sapi_extract_response_code(const char *header_line)
  492. {
  493. int code = 200;
  494. const char *ptr;
  495. for (ptr = header_line; *ptr; ptr++) {
  496. if (*ptr == ' ' && *(ptr + 1) != ' ') {
  497. code = atoi(ptr + 1);
  498. break;
  499. }
  500. }
  501. return code;
  502. }
  503. static void sapi_update_response_code(int ncode TSRMLS_DC)
  504. {
  505. /* if the status code did not change, we do not want
  506. to change the status line, and no need to change the code */
  507. if (SG(sapi_headers).http_response_code == ncode) {
  508. return;
  509. }
  510. if (SG(sapi_headers).http_status_line) {
  511. efree(SG(sapi_headers).http_status_line);
  512. SG(sapi_headers).http_status_line = NULL;
  513. }
  514. SG(sapi_headers).http_response_code = ncode;
  515. }
  516. /*
  517. * since zend_llist_del_element only remove one matched item once,
  518. * we should remove them by ourself
  519. */
  520. static void sapi_remove_header(zend_llist *l, char *name, uint len) {
  521. sapi_header_struct *header;
  522. zend_llist_element *next;
  523. zend_llist_element *current=l->head;
  524. while (current) {
  525. header = (sapi_header_struct *)(current->data);
  526. next = current->next;
  527. if (header->header_len > len && header->header[len] == ':'
  528. && !strncasecmp(header->header, name, len)) {
  529. if (current->prev) {
  530. current->prev->next = next;
  531. } else {
  532. l->head = next;
  533. }
  534. if (next) {
  535. next->prev = current->prev;
  536. } else {
  537. l->tail = current->prev;
  538. }
  539. sapi_free_header(header);
  540. efree(current);
  541. --l->count;
  542. }
  543. current = next;
  544. }
  545. }
  546. SAPI_API int sapi_add_header_ex(char *header_line, uint header_line_len, zend_bool duplicate, zend_bool replace TSRMLS_DC)
  547. {
  548. sapi_header_line ctr = {0};
  549. int r;
  550. ctr.line = header_line;
  551. ctr.line_len = header_line_len;
  552. r = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD,
  553. &ctr TSRMLS_CC);
  554. if (!duplicate)
  555. efree(header_line);
  556. return r;
  557. }
  558. static void sapi_header_add_op(sapi_header_op_enum op, sapi_header_struct *sapi_header TSRMLS_DC)
  559. {
  560. if (!sapi_module.header_handler ||
  561. (SAPI_HEADER_ADD & sapi_module.header_handler(sapi_header, op, &SG(sapi_headers) TSRMLS_CC))) {
  562. if (op == SAPI_HEADER_REPLACE) {
  563. char *colon_offset = strchr(sapi_header->header, ':');
  564. if (colon_offset) {
  565. char sav = *colon_offset;
  566. *colon_offset = 0;
  567. sapi_remove_header(&SG(sapi_headers).headers, sapi_header->header, strlen(sapi_header->header));
  568. *colon_offset = sav;
  569. }
  570. }
  571. zend_llist_add_element(&SG(sapi_headers).headers, (void *) sapi_header);
  572. } else {
  573. sapi_free_header(sapi_header);
  574. }
  575. }
  576. SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg TSRMLS_DC)
  577. {
  578. sapi_header_struct sapi_header;
  579. char *colon_offset;
  580. char *header_line;
  581. uint header_line_len;
  582. int http_response_code;
  583. if (SG(headers_sent) && !SG(request_info).no_headers) {
  584. const char *output_start_filename = php_output_get_start_filename(TSRMLS_C);
  585. int output_start_lineno = php_output_get_start_lineno(TSRMLS_C);
  586. if (output_start_filename) {
  587. sapi_module.sapi_error(E_WARNING, "Cannot modify header information - headers already sent by (output started at %s:%d)",
  588. output_start_filename, output_start_lineno);
  589. } else {
  590. sapi_module.sapi_error(E_WARNING, "Cannot modify header information - headers already sent");
  591. }
  592. return FAILURE;
  593. }
  594. switch (op) {
  595. case SAPI_HEADER_SET_STATUS:
  596. sapi_update_response_code((int)(zend_intptr_t) arg TSRMLS_CC);
  597. return SUCCESS;
  598. case SAPI_HEADER_ADD:
  599. case SAPI_HEADER_REPLACE:
  600. case SAPI_HEADER_DELETE: {
  601. sapi_header_line *p = arg;
  602. if (!p->line || !p->line_len) {
  603. return FAILURE;
  604. }
  605. header_line = p->line;
  606. header_line_len = p->line_len;
  607. http_response_code = p->response_code;
  608. break;
  609. }
  610. case SAPI_HEADER_DELETE_ALL:
  611. if (sapi_module.header_handler) {
  612. sapi_module.header_handler(&sapi_header, op, &SG(sapi_headers) TSRMLS_CC);
  613. }
  614. zend_llist_clean(&SG(sapi_headers).headers);
  615. return SUCCESS;
  616. default:
  617. return FAILURE;
  618. }
  619. header_line = estrndup(header_line, header_line_len);
  620. /* cut off trailing spaces, linefeeds and carriage-returns */
  621. if (header_line_len && isspace(header_line[header_line_len-1])) {
  622. do {
  623. header_line_len--;
  624. } while(header_line_len && isspace(header_line[header_line_len-1]));
  625. header_line[header_line_len]='\0';
  626. }
  627. if (op == SAPI_HEADER_DELETE) {
  628. if (strchr(header_line, ':')) {
  629. efree(header_line);
  630. sapi_module.sapi_error(E_WARNING, "Header to delete may not contain colon.");
  631. return FAILURE;
  632. }
  633. if (sapi_module.header_handler) {
  634. sapi_header.header = header_line;
  635. sapi_header.header_len = header_line_len;
  636. sapi_module.header_handler(&sapi_header, op, &SG(sapi_headers) TSRMLS_CC);
  637. }
  638. sapi_remove_header(&SG(sapi_headers).headers, header_line, header_line_len);
  639. efree(header_line);
  640. return SUCCESS;
  641. } else {
  642. /* new line/NUL character safety check */
  643. int i;
  644. for (i = 0; i < header_line_len; i++) {
  645. /* RFC 7230 ch. 3.2.4 deprecates folding support */
  646. if (header_line[i] == '\n' || header_line[i] == '\r') {
  647. efree(header_line);
  648. sapi_module.sapi_error(E_WARNING, "Header may not contain "
  649. "more than a single header, new line detected");
  650. return FAILURE;
  651. }
  652. if (header_line[i] == '\0') {
  653. efree(header_line);
  654. sapi_module.sapi_error(E_WARNING, "Header may not contain NUL bytes");
  655. return FAILURE;
  656. }
  657. }
  658. }
  659. sapi_header.header = header_line;
  660. sapi_header.header_len = header_line_len;
  661. /* Check the header for a few cases that we have special support for in SAPI */
  662. if (header_line_len>=5
  663. && !strncasecmp(header_line, "HTTP/", 5)) {
  664. /* filter out the response code */
  665. sapi_update_response_code(sapi_extract_response_code(header_line) TSRMLS_CC);
  666. /* sapi_update_response_code doesn't free the status line if the code didn't change */
  667. if (SG(sapi_headers).http_status_line) {
  668. efree(SG(sapi_headers).http_status_line);
  669. }
  670. SG(sapi_headers).http_status_line = header_line;
  671. return SUCCESS;
  672. } else {
  673. colon_offset = strchr(header_line, ':');
  674. if (colon_offset) {
  675. *colon_offset = 0;
  676. if (!STRCASECMP(header_line, "Content-Type")) {
  677. char *ptr = colon_offset+1, *mimetype = NULL, *newheader;
  678. size_t len = header_line_len - (ptr - header_line), newlen;
  679. while (*ptr == ' ') {
  680. ptr++;
  681. len--;
  682. }
  683. /* Disable possible output compression for images */
  684. if (!strncmp(ptr, "image/", sizeof("image/")-1)) {
  685. zend_alter_ini_entry("zlib.output_compression", sizeof("zlib.output_compression"), "0", sizeof("0") - 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  686. }
  687. mimetype = estrdup(ptr);
  688. newlen = sapi_apply_default_charset(&mimetype, len TSRMLS_CC);
  689. if (!SG(sapi_headers).mimetype){
  690. SG(sapi_headers).mimetype = estrdup(mimetype);
  691. }
  692. if (newlen != 0) {
  693. newlen += sizeof("Content-type: ");
  694. newheader = emalloc(newlen);
  695. PHP_STRLCPY(newheader, "Content-type: ", newlen, sizeof("Content-type: ")-1);
  696. strlcat(newheader, mimetype, newlen);
  697. sapi_header.header = newheader;
  698. sapi_header.header_len = newlen - 1;
  699. efree(header_line);
  700. }
  701. efree(mimetype);
  702. SG(sapi_headers).send_default_content_type = 0;
  703. } else if (!STRCASECMP(header_line, "Content-Length")) {
  704. /* Script is setting Content-length. The script cannot reasonably
  705. * know the size of the message body after compression, so it's best
  706. * do disable compression altogether. This contributes to making scripts
  707. * portable between setups that have and don't have zlib compression
  708. * enabled globally. See req #44164 */
  709. zend_alter_ini_entry("zlib.output_compression", sizeof("zlib.output_compression"),
  710. "0", sizeof("0") - 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  711. } else if (!STRCASECMP(header_line, "Location")) {
  712. if ((SG(sapi_headers).http_response_code < 300 ||
  713. SG(sapi_headers).http_response_code > 399) &&
  714. SG(sapi_headers).http_response_code != 201) {
  715. /* Return a Found Redirect if one is not already specified */
  716. if (http_response_code) { /* user specified redirect code */
  717. sapi_update_response_code(http_response_code TSRMLS_CC);
  718. } else if (SG(request_info).proto_num > 1000 &&
  719. SG(request_info).request_method &&
  720. strcmp(SG(request_info).request_method, "HEAD") &&
  721. strcmp(SG(request_info).request_method, "GET")) {
  722. sapi_update_response_code(303 TSRMLS_CC);
  723. } else {
  724. sapi_update_response_code(302 TSRMLS_CC);
  725. }
  726. }
  727. } else if (!STRCASECMP(header_line, "WWW-Authenticate")) { /* HTTP Authentication */
  728. sapi_update_response_code(401 TSRMLS_CC); /* authentication-required */
  729. }
  730. if (sapi_header.header==header_line) {
  731. *colon_offset = ':';
  732. }
  733. }
  734. }
  735. if (http_response_code) {
  736. sapi_update_response_code(http_response_code TSRMLS_CC);
  737. }
  738. sapi_header_add_op(op, &sapi_header TSRMLS_CC);
  739. return SUCCESS;
  740. }
  741. SAPI_API int sapi_send_headers(TSRMLS_D)
  742. {
  743. int retval;
  744. int ret = FAILURE;
  745. if (SG(headers_sent) || SG(request_info).no_headers || SG(callback_run)) {
  746. return SUCCESS;
  747. }
  748. /* Success-oriented. We set headers_sent to 1 here to avoid an infinite loop
  749. * in case of an error situation.
  750. */
  751. if (SG(sapi_headers).send_default_content_type && sapi_module.send_headers) {
  752. sapi_header_struct default_header;
  753. uint len;
  754. SG(sapi_headers).mimetype = get_default_content_type(0, &len TSRMLS_CC);
  755. default_header.header_len = sizeof("Content-type: ") - 1 + len;
  756. default_header.header = emalloc(default_header.header_len + 1);
  757. memcpy(default_header.header, "Content-type: ", sizeof("Content-type: ") - 1);
  758. memcpy(default_header.header + sizeof("Content-type: ") - 1, SG(sapi_headers).mimetype, len + 1);
  759. sapi_header_add_op(SAPI_HEADER_ADD, &default_header TSRMLS_CC);
  760. SG(sapi_headers).send_default_content_type = 0;
  761. }
  762. if (SG(callback_func) && !SG(callback_run)) {
  763. SG(callback_run) = 1;
  764. sapi_run_header_callback(TSRMLS_C);
  765. }
  766. SG(headers_sent) = 1;
  767. if (sapi_module.send_headers) {
  768. retval = sapi_module.send_headers(&SG(sapi_headers) TSRMLS_CC);
  769. } else {
  770. retval = SAPI_HEADER_DO_SEND;
  771. }
  772. switch (retval) {
  773. case SAPI_HEADER_SENT_SUCCESSFULLY:
  774. ret = SUCCESS;
  775. break;
  776. case SAPI_HEADER_DO_SEND: {
  777. sapi_header_struct http_status_line;
  778. char buf[255];
  779. if (SG(sapi_headers).http_status_line) {
  780. http_status_line.header = SG(sapi_headers).http_status_line;
  781. http_status_line.header_len = strlen(SG(sapi_headers).http_status_line);
  782. } else {
  783. http_status_line.header = buf;
  784. http_status_line.header_len = slprintf(buf, sizeof(buf), "HTTP/1.0 %d X", SG(sapi_headers).http_response_code);
  785. }
  786. sapi_module.send_header(&http_status_line, SG(server_context) TSRMLS_CC);
  787. }
  788. zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t) sapi_module.send_header, SG(server_context) TSRMLS_CC);
  789. if(SG(sapi_headers).send_default_content_type) {
  790. sapi_header_struct default_header;
  791. sapi_get_default_content_type_header(&default_header TSRMLS_CC);
  792. sapi_module.send_header(&default_header, SG(server_context) TSRMLS_CC);
  793. sapi_free_header(&default_header);
  794. }
  795. sapi_module.send_header(NULL, SG(server_context) TSRMLS_CC);
  796. ret = SUCCESS;
  797. break;
  798. case SAPI_HEADER_SEND_FAILED:
  799. SG(headers_sent) = 0;
  800. ret = FAILURE;
  801. break;
  802. }
  803. sapi_send_headers_free(TSRMLS_C);
  804. return ret;
  805. }
  806. SAPI_API int sapi_register_post_entries(sapi_post_entry *post_entries TSRMLS_DC)
  807. {
  808. sapi_post_entry *p=post_entries;
  809. while (p->content_type) {
  810. if (sapi_register_post_entry(p TSRMLS_CC) == FAILURE) {
  811. return FAILURE;
  812. }
  813. p++;
  814. }
  815. return SUCCESS;
  816. }
  817. SAPI_API int sapi_register_post_entry(sapi_post_entry *post_entry TSRMLS_DC)
  818. {
  819. if (SG(sapi_started) && EG(in_execution)) {
  820. return FAILURE;
  821. }
  822. return zend_hash_add(&SG(known_post_content_types),
  823. post_entry->content_type, post_entry->content_type_len+1,
  824. (void *) post_entry, sizeof(sapi_post_entry), NULL);
  825. }
  826. SAPI_API void sapi_unregister_post_entry(sapi_post_entry *post_entry TSRMLS_DC)
  827. {
  828. if (SG(sapi_started) && EG(in_execution)) {
  829. return;
  830. }
  831. zend_hash_del(&SG(known_post_content_types), post_entry->content_type,
  832. post_entry->content_type_len+1);
  833. }
  834. SAPI_API int sapi_register_default_post_reader(void (*default_post_reader)(TSRMLS_D) TSRMLS_DC)
  835. {
  836. if (SG(sapi_started) && EG(in_execution)) {
  837. return FAILURE;
  838. }
  839. sapi_module.default_post_reader = default_post_reader;
  840. return SUCCESS;
  841. }
  842. SAPI_API int sapi_register_treat_data(void (*treat_data)(int arg, char *str, zval *destArray TSRMLS_DC) TSRMLS_DC)
  843. {
  844. if (SG(sapi_started) && EG(in_execution)) {
  845. return FAILURE;
  846. }
  847. sapi_module.treat_data = treat_data;
  848. return SUCCESS;
  849. }
  850. 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), unsigned int (*input_filter_init)(TSRMLS_D) TSRMLS_DC)
  851. {
  852. if (SG(sapi_started) && EG(in_execution)) {
  853. return FAILURE;
  854. }
  855. sapi_module.input_filter = input_filter;
  856. sapi_module.input_filter_init = input_filter_init;
  857. return SUCCESS;
  858. }
  859. SAPI_API int sapi_flush(TSRMLS_D)
  860. {
  861. if (sapi_module.flush) {
  862. sapi_module.flush(SG(server_context));
  863. return SUCCESS;
  864. } else {
  865. return FAILURE;
  866. }
  867. }
  868. SAPI_API struct stat *sapi_get_stat(TSRMLS_D)
  869. {
  870. if (sapi_module.get_stat) {
  871. return sapi_module.get_stat(TSRMLS_C);
  872. } else {
  873. if (!SG(request_info).path_translated || (VCWD_STAT(SG(request_info).path_translated, &SG(global_stat)) == -1)) {
  874. return NULL;
  875. }
  876. return &SG(global_stat);
  877. }
  878. }
  879. SAPI_API char *sapi_getenv(char *name, size_t name_len TSRMLS_DC)
  880. {
  881. if (!strncasecmp(name, "HTTP_PROXY", name_len)) {
  882. /* Ugly fix for HTTP_PROXY issue, see bug #72573 */
  883. return NULL;
  884. }
  885. if (sapi_module.getenv) {
  886. char *value, *tmp = sapi_module.getenv(name, name_len TSRMLS_CC);
  887. if (tmp) {
  888. value = estrdup(tmp);
  889. } else {
  890. return NULL;
  891. }
  892. if (sapi_module.input_filter) {
  893. sapi_module.input_filter(PARSE_STRING, name, &value, strlen(value), NULL TSRMLS_CC);
  894. }
  895. return value;
  896. }
  897. return NULL;
  898. }
  899. SAPI_API int sapi_get_fd(int *fd TSRMLS_DC)
  900. {
  901. if (sapi_module.get_fd) {
  902. return sapi_module.get_fd(fd TSRMLS_CC);
  903. } else {
  904. return FAILURE;
  905. }
  906. }
  907. SAPI_API int sapi_force_http_10(TSRMLS_D)
  908. {
  909. if (sapi_module.force_http_10) {
  910. return sapi_module.force_http_10(TSRMLS_C);
  911. } else {
  912. return FAILURE;
  913. }
  914. }
  915. SAPI_API int sapi_get_target_uid(uid_t *obj TSRMLS_DC)
  916. {
  917. if (sapi_module.get_target_uid) {
  918. return sapi_module.get_target_uid(obj TSRMLS_CC);
  919. } else {
  920. return FAILURE;
  921. }
  922. }
  923. SAPI_API int sapi_get_target_gid(gid_t *obj TSRMLS_DC)
  924. {
  925. if (sapi_module.get_target_gid) {
  926. return sapi_module.get_target_gid(obj TSRMLS_CC);
  927. } else {
  928. return FAILURE;
  929. }
  930. }
  931. SAPI_API double sapi_get_request_time(TSRMLS_D)
  932. {
  933. if(SG(global_request_time)) return SG(global_request_time);
  934. if (sapi_module.get_request_time && SG(server_context)) {
  935. SG(global_request_time) = sapi_module.get_request_time(TSRMLS_C);
  936. } else {
  937. struct timeval tp = {0};
  938. if (!gettimeofday(&tp, NULL)) {
  939. SG(global_request_time) = (double)(tp.tv_sec + tp.tv_usec / 1000000.00);
  940. } else {
  941. SG(global_request_time) = (double)time(0);
  942. }
  943. }
  944. return SG(global_request_time);
  945. }
  946. SAPI_API void sapi_terminate_process(TSRMLS_D) {
  947. if (sapi_module.terminate_process) {
  948. sapi_module.terminate_process(TSRMLS_C);
  949. }
  950. }
  951. /*
  952. * Local variables:
  953. * tab-width: 4
  954. * c-basic-offset: 4
  955. * End:
  956. * vim600: sw=4 ts=4 fdm=marker
  957. * vim<600: sw=4 ts=4
  958. */