SAPI.c 31 KB

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