sapi_apache2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. | Authors: Sascha Schumann <sascha@schumann.cx> |
  14. | Parts based on Apache 1.3 SAPI module by |
  15. | Rasmus Lerdorf and Zeev Suraski |
  16. +----------------------------------------------------------------------+
  17. */
  18. #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
  19. #include "php.h"
  20. #ifdef strcasecmp
  21. # undef strcasecmp
  22. #endif
  23. #ifdef strncasecmp
  24. # undef strncasecmp
  25. #endif
  26. #include "php_main.h"
  27. #include "php_ini.h"
  28. #include "php_variables.h"
  29. #include "SAPI.h"
  30. #include <fcntl.h>
  31. #include "zend_smart_str.h"
  32. #include "ext/standard/php_standard.h"
  33. #include "apr_strings.h"
  34. #include "ap_config.h"
  35. #include "util_filter.h"
  36. #include "httpd.h"
  37. #include "http_config.h"
  38. #include "http_request.h"
  39. #include "http_core.h"
  40. #include "http_protocol.h"
  41. #include "http_log.h"
  42. #include "http_main.h"
  43. #include "util_script.h"
  44. #include "http_core.h"
  45. #include "ap_mpm.h"
  46. #include "php_apache.h"
  47. /* UnixWare define shutdown to _shutdown, which causes problems later
  48. * on when using a structure member named shutdown. Since this source
  49. * file does not use the system call shutdown, it is safe to #undef it.
  50. */
  51. #undef shutdown
  52. #define PHP_MAGIC_TYPE "application/x-httpd-php"
  53. #define PHP_SOURCE_MAGIC_TYPE "application/x-httpd-php-source"
  54. #define PHP_SCRIPT "php-script"
  55. /* A way to specify the location of the php.ini dir in an apache directive */
  56. char *apache2_php_ini_path_override = NULL;
  57. #if defined(PHP_WIN32) && defined(ZTS)
  58. ZEND_TSRMLS_CACHE_DEFINE()
  59. #endif
  60. static size_t
  61. php_apache_sapi_ub_write(const char *str, size_t str_length)
  62. {
  63. request_rec *r;
  64. php_struct *ctx;
  65. ctx = SG(server_context);
  66. r = ctx->r;
  67. if (ap_rwrite(str, str_length, r) < 0) {
  68. php_handle_aborted_connection();
  69. }
  70. return str_length; /* we always consume all the data passed to us. */
  71. }
  72. static int
  73. php_apache_sapi_header_handler(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers)
  74. {
  75. php_struct *ctx;
  76. char *val, *ptr;
  77. ctx = SG(server_context);
  78. switch (op) {
  79. case SAPI_HEADER_DELETE:
  80. apr_table_unset(ctx->r->headers_out, sapi_header->header);
  81. return 0;
  82. case SAPI_HEADER_DELETE_ALL:
  83. apr_table_clear(ctx->r->headers_out);
  84. return 0;
  85. case SAPI_HEADER_ADD:
  86. case SAPI_HEADER_REPLACE:
  87. val = strchr(sapi_header->header, ':');
  88. if (!val) {
  89. return 0;
  90. }
  91. ptr = val;
  92. *val = '\0';
  93. do {
  94. val++;
  95. } while (*val == ' ');
  96. if (!strcasecmp(sapi_header->header, "content-type")) {
  97. if (ctx->content_type) {
  98. efree(ctx->content_type);
  99. }
  100. ctx->content_type = estrdup(val);
  101. } else if (!strcasecmp(sapi_header->header, "content-length")) {
  102. apr_off_t clen = 0;
  103. if (APR_SUCCESS != apr_strtoff(&clen, val, (char **) NULL, 10)) {
  104. /* We'll fall back to strtol, since that's what we used to
  105. * do anyway. */
  106. clen = (apr_off_t) strtol(val, (char **) NULL, 10);
  107. }
  108. ap_set_content_length(ctx->r, clen);
  109. } else if (op == SAPI_HEADER_REPLACE) {
  110. apr_table_set(ctx->r->headers_out, sapi_header->header, val);
  111. } else {
  112. apr_table_add(ctx->r->headers_out, sapi_header->header, val);
  113. }
  114. *ptr = ':';
  115. return SAPI_HEADER_ADD;
  116. default:
  117. return 0;
  118. }
  119. }
  120. static int
  121. php_apache_sapi_send_headers(sapi_headers_struct *sapi_headers)
  122. {
  123. php_struct *ctx = SG(server_context);
  124. const char *sline = SG(sapi_headers).http_status_line;
  125. ctx->r->status = SG(sapi_headers).http_response_code;
  126. /* httpd requires that r->status_line is set to the first digit of
  127. * the status-code: */
  128. if (sline && strlen(sline) > 12 && strncmp(sline, "HTTP/1.", 7) == 0 && sline[8] == ' ') {
  129. ctx->r->status_line = apr_pstrdup(ctx->r->pool, sline + 9);
  130. ctx->r->proto_num = 1000 + (sline[7]-'0');
  131. if ((sline[7]-'0') == 0) {
  132. apr_table_set(ctx->r->subprocess_env, "force-response-1.0", "true");
  133. }
  134. }
  135. /* call ap_set_content_type only once, else each time we call it,
  136. configured output filters for that content type will be added */
  137. if (!ctx->content_type) {
  138. ctx->content_type = sapi_get_default_content_type();
  139. }
  140. ap_set_content_type(ctx->r, apr_pstrdup(ctx->r->pool, ctx->content_type));
  141. efree(ctx->content_type);
  142. ctx->content_type = NULL;
  143. return SAPI_HEADER_SENT_SUCCESSFULLY;
  144. }
  145. static apr_size_t
  146. php_apache_sapi_read_post(char *buf, size_t count_bytes)
  147. {
  148. apr_size_t len, tlen=0;
  149. php_struct *ctx = SG(server_context);
  150. request_rec *r;
  151. apr_bucket_brigade *brigade;
  152. r = ctx->r;
  153. brigade = ctx->brigade;
  154. len = count_bytes;
  155. /*
  156. * This loop is needed because ap_get_brigade() can return us partial data
  157. * which would cause premature termination of request read. Therefore we
  158. * need to make sure that if data is available we fill the buffer completely.
  159. */
  160. while (ap_get_brigade(r->input_filters, brigade, AP_MODE_READBYTES, APR_BLOCK_READ, len) == APR_SUCCESS) {
  161. apr_brigade_flatten(brigade, buf, &len);
  162. apr_brigade_cleanup(brigade);
  163. tlen += len;
  164. if (tlen == count_bytes || !len) {
  165. break;
  166. }
  167. buf += len;
  168. len = count_bytes - tlen;
  169. }
  170. return tlen;
  171. }
  172. static zend_stat_t*
  173. php_apache_sapi_get_stat(void)
  174. {
  175. php_struct *ctx = SG(server_context);
  176. #ifdef PHP_WIN32
  177. ctx->finfo.st_uid = 0;
  178. ctx->finfo.st_gid = 0;
  179. #else
  180. ctx->finfo.st_uid = ctx->r->finfo.user;
  181. ctx->finfo.st_gid = ctx->r->finfo.group;
  182. #endif
  183. ctx->finfo.st_dev = ctx->r->finfo.device;
  184. ctx->finfo.st_ino = ctx->r->finfo.inode;
  185. ctx->finfo.st_atime = apr_time_sec(ctx->r->finfo.atime);
  186. ctx->finfo.st_mtime = apr_time_sec(ctx->r->finfo.mtime);
  187. ctx->finfo.st_ctime = apr_time_sec(ctx->r->finfo.ctime);
  188. ctx->finfo.st_size = ctx->r->finfo.size;
  189. ctx->finfo.st_nlink = ctx->r->finfo.nlink;
  190. return &ctx->finfo;
  191. }
  192. static char *
  193. php_apache_sapi_read_cookies(void)
  194. {
  195. php_struct *ctx = SG(server_context);
  196. const char *http_cookie;
  197. http_cookie = apr_table_get(ctx->r->headers_in, "cookie");
  198. /* The SAPI interface should use 'const char *' */
  199. return (char *) http_cookie;
  200. }
  201. static char *
  202. php_apache_sapi_getenv(const char *name, size_t name_len)
  203. {
  204. php_struct *ctx = SG(server_context);
  205. const char *env_var;
  206. if (ctx == NULL) {
  207. return NULL;
  208. }
  209. env_var = apr_table_get(ctx->r->subprocess_env, name);
  210. return (char *) env_var;
  211. }
  212. static void
  213. php_apache_sapi_register_variables(zval *track_vars_array)
  214. {
  215. php_struct *ctx = SG(server_context);
  216. const apr_array_header_t *arr = apr_table_elts(ctx->r->subprocess_env);
  217. char *key, *val;
  218. size_t new_val_len;
  219. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  220. if (!val) {
  221. val = "";
  222. }
  223. if (sapi_module.input_filter(PARSE_SERVER, key, &val, strlen(val), &new_val_len)) {
  224. php_register_variable_safe(key, val, new_val_len, track_vars_array);
  225. }
  226. APR_ARRAY_FOREACH_CLOSE()
  227. if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &ctx->r->uri, strlen(ctx->r->uri), &new_val_len)) {
  228. php_register_variable_safe("PHP_SELF", ctx->r->uri, new_val_len, track_vars_array);
  229. }
  230. }
  231. static void
  232. php_apache_sapi_flush(void *server_context)
  233. {
  234. php_struct *ctx;
  235. request_rec *r;
  236. ctx = server_context;
  237. /* If we haven't registered a server_context yet,
  238. * then don't bother flushing. */
  239. if (!server_context) {
  240. return;
  241. }
  242. r = ctx->r;
  243. sapi_send_headers();
  244. r->status = SG(sapi_headers).http_response_code;
  245. SG(headers_sent) = 1;
  246. if (ap_rflush(r) < 0 || r->connection->aborted) {
  247. php_handle_aborted_connection();
  248. }
  249. }
  250. static void php_apache_sapi_log_message(const char *msg, int syslog_type_int)
  251. {
  252. php_struct *ctx;
  253. int aplog_type = APLOG_ERR;
  254. ctx = SG(server_context);
  255. switch (syslog_type_int) {
  256. #if LOG_EMERG != LOG_CRIT
  257. case LOG_EMERG:
  258. aplog_type = APLOG_EMERG;
  259. break;
  260. #endif
  261. #if LOG_ALERT != LOG_CRIT
  262. case LOG_ALERT:
  263. aplog_type = APLOG_ALERT;
  264. break;
  265. #endif
  266. case LOG_CRIT:
  267. aplog_type = APLOG_CRIT;
  268. break;
  269. case LOG_ERR:
  270. aplog_type = APLOG_ERR;
  271. break;
  272. case LOG_WARNING:
  273. aplog_type = APLOG_WARNING;
  274. break;
  275. case LOG_NOTICE:
  276. aplog_type = APLOG_NOTICE;
  277. break;
  278. #if LOG_INFO != LOG_NOTICE
  279. case LOG_INFO:
  280. aplog_type = APLOG_INFO;
  281. break;
  282. #endif
  283. #if LOG_NOTICE != LOG_DEBUG
  284. case LOG_DEBUG:
  285. aplog_type = APLOG_DEBUG;
  286. break;
  287. #endif
  288. }
  289. if (ctx == NULL) { /* we haven't initialized our ctx yet, oh well */
  290. ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, NULL, "%s", msg);
  291. } else {
  292. ap_log_rerror(APLOG_MARK, aplog_type, 0, ctx->r, "%s", msg);
  293. }
  294. }
  295. static void php_apache_sapi_log_message_ex(const char *msg, request_rec *r)
  296. {
  297. if (r) {
  298. ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, msg, r->filename);
  299. } else {
  300. php_apache_sapi_log_message(msg, -1);
  301. }
  302. }
  303. static double php_apache_sapi_get_request_time(void)
  304. {
  305. php_struct *ctx = SG(server_context);
  306. return ((double) ctx->r->request_time) / 1000000.0;
  307. }
  308. extern zend_module_entry php_apache_module;
  309. static int php_apache2_startup(sapi_module_struct *sapi_module)
  310. {
  311. if (php_module_startup(sapi_module, &php_apache_module, 1)==FAILURE) {
  312. return FAILURE;
  313. }
  314. return SUCCESS;
  315. }
  316. static sapi_module_struct apache2_sapi_module = {
  317. "apache2handler",
  318. "Apache 2.0 Handler",
  319. php_apache2_startup, /* startup */
  320. php_module_shutdown_wrapper, /* shutdown */
  321. NULL, /* activate */
  322. NULL, /* deactivate */
  323. php_apache_sapi_ub_write, /* unbuffered write */
  324. php_apache_sapi_flush, /* flush */
  325. php_apache_sapi_get_stat, /* get uid */
  326. php_apache_sapi_getenv, /* getenv */
  327. php_error, /* error handler */
  328. php_apache_sapi_header_handler, /* header handler */
  329. php_apache_sapi_send_headers, /* send headers handler */
  330. NULL, /* send header handler */
  331. php_apache_sapi_read_post, /* read POST data */
  332. php_apache_sapi_read_cookies, /* read Cookies */
  333. php_apache_sapi_register_variables,
  334. php_apache_sapi_log_message, /* Log message */
  335. php_apache_sapi_get_request_time, /* Request Time */
  336. NULL, /* Child Terminate */
  337. STANDARD_SAPI_MODULE_PROPERTIES
  338. };
  339. static apr_status_t php_apache_server_shutdown(void *tmp)
  340. {
  341. apache2_sapi_module.shutdown(&apache2_sapi_module);
  342. sapi_shutdown();
  343. #ifdef ZTS
  344. tsrm_shutdown();
  345. #endif
  346. return APR_SUCCESS;
  347. }
  348. static apr_status_t php_apache_child_shutdown(void *tmp)
  349. {
  350. apache2_sapi_module.shutdown(&apache2_sapi_module);
  351. #if defined(ZTS) && !defined(PHP_WIN32)
  352. tsrm_shutdown();
  353. #endif
  354. return APR_SUCCESS;
  355. }
  356. static void php_apache_add_version(apr_pool_t *p)
  357. {
  358. if (PG(expose_php)) {
  359. ap_add_version_component(p, "PHP/" PHP_VERSION);
  360. }
  361. }
  362. static int php_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
  363. {
  364. #ifndef ZTS
  365. int threaded_mpm;
  366. ap_mpm_query(AP_MPMQ_IS_THREADED, &threaded_mpm);
  367. if(threaded_mpm) {
  368. ap_log_error(APLOG_MARK, APLOG_CRIT, 0, 0, "Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP.");
  369. return DONE;
  370. }
  371. #endif
  372. /* When this is NULL, apache won't override the hard-coded default
  373. * php.ini path setting. */
  374. apache2_php_ini_path_override = NULL;
  375. return OK;
  376. }
  377. static int
  378. php_apache_server_startup(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
  379. {
  380. void *data = NULL;
  381. const char *userdata_key = "apache2hook_post_config";
  382. /* Apache will load, unload and then reload a DSO module. This
  383. * prevents us from starting PHP until the second load. */
  384. apr_pool_userdata_get(&data, userdata_key, s->process->pool);
  385. if (data == NULL) {
  386. /* We must use set() here and *not* setn(), otherwise the
  387. * static string pointed to by userdata_key will be mapped
  388. * to a different location when the DSO is reloaded and the
  389. * pointers won't match, causing get() to return NULL when
  390. * we expected it to return non-NULL. */
  391. apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
  392. return OK;
  393. }
  394. /* Set up our overridden path. */
  395. if (apache2_php_ini_path_override) {
  396. apache2_sapi_module.php_ini_path_override = apache2_php_ini_path_override;
  397. }
  398. #ifdef ZTS
  399. php_tsrm_startup();
  400. # ifdef PHP_WIN32
  401. ZEND_TSRMLS_CACHE_UPDATE();
  402. # endif
  403. #endif
  404. zend_signal_startup();
  405. sapi_startup(&apache2_sapi_module);
  406. if (apache2_sapi_module.startup(&apache2_sapi_module) != SUCCESS) {
  407. return DONE;
  408. }
  409. apr_pool_cleanup_register(pconf, NULL, php_apache_server_shutdown, apr_pool_cleanup_null);
  410. php_apache_add_version(pconf);
  411. return OK;
  412. }
  413. static apr_status_t php_server_context_cleanup(void *data_)
  414. {
  415. void **data = data_;
  416. *data = NULL;
  417. return APR_SUCCESS;
  418. }
  419. static int php_apache_request_ctor(request_rec *r, php_struct *ctx)
  420. {
  421. char *content_length;
  422. const char *auth;
  423. SG(sapi_headers).http_response_code = !r->status ? HTTP_OK : r->status;
  424. SG(request_info).content_type = apr_table_get(r->headers_in, "Content-Type");
  425. SG(request_info).query_string = apr_pstrdup(r->pool, r->args);
  426. SG(request_info).request_method = r->method;
  427. SG(request_info).proto_num = r->proto_num;
  428. SG(request_info).request_uri = apr_pstrdup(r->pool, r->uri);
  429. SG(request_info).path_translated = apr_pstrdup(r->pool, r->filename);
  430. r->no_local_copy = 1;
  431. content_length = (char *) apr_table_get(r->headers_in, "Content-Length");
  432. if (content_length) {
  433. SG(request_info).content_length = ZEND_ATOL(content_length);
  434. } else {
  435. SG(request_info).content_length = 0;
  436. }
  437. apr_table_unset(r->headers_out, "Content-Length");
  438. apr_table_unset(r->headers_out, "Last-Modified");
  439. apr_table_unset(r->headers_out, "Expires");
  440. apr_table_unset(r->headers_out, "ETag");
  441. auth = apr_table_get(r->headers_in, "Authorization");
  442. php_handle_auth_data(auth);
  443. if (SG(request_info).auth_user == NULL && r->user) {
  444. SG(request_info).auth_user = estrdup(r->user);
  445. }
  446. ctx->r->user = apr_pstrdup(ctx->r->pool, SG(request_info).auth_user);
  447. return php_request_startup();
  448. }
  449. static void php_apache_request_dtor(request_rec *r)
  450. {
  451. php_request_shutdown(NULL);
  452. }
  453. static void php_apache_ini_dtor(request_rec *r, request_rec *p)
  454. {
  455. if (strcmp(r->protocol, "INCLUDED")) {
  456. zend_try { zend_ini_deactivate(); } zend_end_try();
  457. } else {
  458. typedef struct {
  459. HashTable config;
  460. } php_conf_rec;
  461. zend_string *str;
  462. php_conf_rec *c = ap_get_module_config(r->per_dir_config, &php_module);
  463. ZEND_HASH_FOREACH_STR_KEY(&c->config, str) {
  464. zend_restore_ini_entry(str, ZEND_INI_STAGE_SHUTDOWN);
  465. } ZEND_HASH_FOREACH_END();
  466. }
  467. if (p) {
  468. ((php_struct *)SG(server_context))->r = p;
  469. } else {
  470. apr_pool_cleanup_run(r->pool, (void *)&SG(server_context), php_server_context_cleanup);
  471. }
  472. }
  473. static int php_handler(request_rec *r)
  474. {
  475. php_struct * volatile ctx;
  476. void *conf;
  477. apr_bucket_brigade * volatile brigade;
  478. apr_bucket *bucket;
  479. apr_status_t rv;
  480. request_rec * volatile parent_req = NULL;
  481. #ifdef ZTS
  482. /* initial resource fetch */
  483. (void)ts_resource(0);
  484. # ifdef PHP_WIN32
  485. ZEND_TSRMLS_CACHE_UPDATE();
  486. # endif
  487. #endif
  488. #define PHPAP_INI_OFF php_apache_ini_dtor(r, parent_req);
  489. conf = ap_get_module_config(r->per_dir_config, &php_module);
  490. /* apply_config() needs r in some cases, so allocate server_context early */
  491. ctx = SG(server_context);
  492. if (ctx == NULL || (ctx && ctx->request_processed && !strcmp(r->protocol, "INCLUDED"))) {
  493. normal:
  494. ctx = SG(server_context) = apr_pcalloc(r->pool, sizeof(*ctx));
  495. /* register a cleanup so we clear out the SG(server_context)
  496. * after each request. Note: We pass in the pointer to the
  497. * server_context in case this is handled by a different thread.
  498. */
  499. apr_pool_cleanup_register(r->pool, (void *)&SG(server_context), php_server_context_cleanup, apr_pool_cleanup_null);
  500. ctx->r = r;
  501. ctx = NULL; /* May look weird to null it here, but it is to catch the right case in the first_try later on */
  502. } else {
  503. parent_req = ctx->r;
  504. ctx->r = r;
  505. }
  506. apply_config(conf);
  507. if (strcmp(r->handler, PHP_MAGIC_TYPE) && strcmp(r->handler, PHP_SOURCE_MAGIC_TYPE) && strcmp(r->handler, PHP_SCRIPT)) {
  508. /* Check for xbithack in this case. */
  509. if (!AP2(xbithack) || strcmp(r->handler, "text/html") || !(r->finfo.protection & APR_UEXECUTE)) {
  510. PHPAP_INI_OFF;
  511. return DECLINED;
  512. }
  513. }
  514. /* Give a 404 if PATH_INFO is used but is explicitly disabled in
  515. * the configuration; default behaviour is to accept. */
  516. if (r->used_path_info == AP_REQ_REJECT_PATH_INFO
  517. && r->path_info && r->path_info[0]) {
  518. PHPAP_INI_OFF;
  519. return HTTP_NOT_FOUND;
  520. }
  521. /* handle situations where user turns the engine off */
  522. if (!AP2(engine)) {
  523. PHPAP_INI_OFF;
  524. return DECLINED;
  525. }
  526. if (r->finfo.filetype == 0) {
  527. php_apache_sapi_log_message_ex("script '%s' not found or unable to stat", r);
  528. PHPAP_INI_OFF;
  529. return HTTP_NOT_FOUND;
  530. }
  531. if (r->finfo.filetype == APR_DIR) {
  532. php_apache_sapi_log_message_ex("attempt to invoke directory '%s' as script", r);
  533. PHPAP_INI_OFF;
  534. return HTTP_FORBIDDEN;
  535. }
  536. /* Setup the CGI variables if this is the main request */
  537. if (r->main == NULL ||
  538. /* .. or if the sub-request environment differs from the main-request. */
  539. r->subprocess_env != r->main->subprocess_env
  540. ) {
  541. /* setup standard CGI variables */
  542. ap_add_common_vars(r);
  543. ap_add_cgi_vars(r);
  544. }
  545. zend_first_try {
  546. if (ctx == NULL) {
  547. brigade = apr_brigade_create(r->pool, r->connection->bucket_alloc);
  548. ctx = SG(server_context);
  549. ctx->brigade = brigade;
  550. if (php_apache_request_ctor(r, ctx)!=SUCCESS) {
  551. zend_bailout();
  552. }
  553. } else {
  554. if (!parent_req) {
  555. parent_req = ctx->r;
  556. }
  557. if (parent_req && parent_req->handler &&
  558. strcmp(parent_req->handler, PHP_MAGIC_TYPE) &&
  559. strcmp(parent_req->handler, PHP_SOURCE_MAGIC_TYPE) &&
  560. strcmp(parent_req->handler, PHP_SCRIPT)) {
  561. if (php_apache_request_ctor(r, ctx)!=SUCCESS) {
  562. zend_bailout();
  563. }
  564. }
  565. /*
  566. * check if coming due to ErrorDocument
  567. * We make a special exception of 413 (Invalid POST request) as the invalidity of the request occurs
  568. * during processing of the request by PHP during POST processing. Therefore we need to re-use the exiting
  569. * PHP instance to handle the request rather then creating a new one.
  570. */
  571. if (parent_req && parent_req->status != HTTP_OK && parent_req->status != 413 && strcmp(r->protocol, "INCLUDED")) {
  572. parent_req = NULL;
  573. goto normal;
  574. }
  575. ctx->r = r;
  576. brigade = ctx->brigade;
  577. }
  578. if (AP2(last_modified)) {
  579. ap_update_mtime(r, r->finfo.mtime);
  580. ap_set_last_modified(r);
  581. }
  582. /* Determine if we need to parse the file or show the source */
  583. if (strncmp(r->handler, PHP_SOURCE_MAGIC_TYPE, sizeof(PHP_SOURCE_MAGIC_TYPE) - 1) == 0) {
  584. zend_syntax_highlighter_ini syntax_highlighter_ini;
  585. php_get_highlight_struct(&syntax_highlighter_ini);
  586. highlight_file((char *)r->filename, &syntax_highlighter_ini);
  587. } else {
  588. zend_file_handle zfd;
  589. zend_stream_init_filename(&zfd, (char *) r->filename);
  590. zfd.primary_script = 1;
  591. if (!parent_req) {
  592. php_execute_script(&zfd);
  593. } else {
  594. zend_execute_scripts(ZEND_INCLUDE, NULL, 1, &zfd);
  595. }
  596. zend_destroy_file_handle(&zfd);
  597. apr_table_set(r->notes, "mod_php_memory_usage",
  598. apr_psprintf(ctx->r->pool, "%" APR_SIZE_T_FMT, zend_memory_peak_usage(1)));
  599. }
  600. } zend_end_try();
  601. if (!parent_req) {
  602. php_apache_request_dtor(r);
  603. ctx->request_processed = 1;
  604. apr_brigade_cleanup(brigade);
  605. bucket = apr_bucket_eos_create(r->connection->bucket_alloc);
  606. APR_BRIGADE_INSERT_TAIL(brigade, bucket);
  607. rv = ap_pass_brigade(r->output_filters, brigade);
  608. if (rv != APR_SUCCESS || r->connection->aborted) {
  609. zend_first_try {
  610. php_handle_aborted_connection();
  611. } zend_end_try();
  612. }
  613. apr_brigade_cleanup(brigade);
  614. apr_pool_cleanup_run(r->pool, (void *)&SG(server_context), php_server_context_cleanup);
  615. } else {
  616. ctx->r = parent_req;
  617. }
  618. return OK;
  619. }
  620. static void php_apache_child_init(apr_pool_t *pchild, server_rec *s)
  621. {
  622. apr_pool_cleanup_register(pchild, NULL, php_apache_child_shutdown, apr_pool_cleanup_null);
  623. }
  624. #ifdef ZEND_SIGNALS
  625. static void php_apache_signal_init(apr_pool_t *pchild, server_rec *s)
  626. {
  627. zend_signal_init();
  628. }
  629. #endif
  630. void php_ap2_register_hook(apr_pool_t *p)
  631. {
  632. ap_hook_pre_config(php_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
  633. ap_hook_post_config(php_apache_server_startup, NULL, NULL, APR_HOOK_MIDDLE);
  634. ap_hook_handler(php_handler, NULL, NULL, APR_HOOK_MIDDLE);
  635. #ifdef ZEND_SIGNALS
  636. ap_hook_child_init(php_apache_signal_init, NULL, NULL, APR_HOOK_MIDDLE);
  637. #endif
  638. ap_hook_child_init(php_apache_child_init, NULL, NULL, APR_HOOK_MIDDLE);
  639. }