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.

718 lines
20 KiB

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