You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

728 lines
20 KiB

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