📄 sapi_apache2.c
字号:
/* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2007 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Sascha Schumann <sascha@schumann.cx> | | Parts based on Apache 1.3 SAPI module by | | Rasmus Lerdorf and Zeev Suraski | +----------------------------------------------------------------------+ *//* $Id: sapi_apache2.c,v 1.91.2.27.2.3 2007/01/01 09:46:51 sebastian Exp $ */#include <fcntl.h>#include "php.h"#include "php_main.h"#include "php_ini.h"#include "php_variables.h"#include "SAPI.h"#include "ext/standard/php_smart_str.h"#include "ext/standard/php_standard.h"#include "apr_strings.h"#include "ap_config.h"#include "util_filter.h"#include "httpd.h"#include "http_config.h"#include "http_request.h"#include "http_core.h"#include "http_protocol.h"#include "http_log.h"#include "http_main.h"#include "util_script.h"#include "http_core.h" #include "ap_mpm.h"#include "php_apache.h"/* UnixWare defines shutdown to _shutdown, which causes problems later * on when using a structure member named shutdown. Since this source * file does not use the system call shutdown, it is safe to #undef it. */#undef shutdown /* A way to specify the location of the php.ini dir in an apache directive */char *apache2_php_ini_path_override = NULL;static intphp_apache_sapi_ub_write(const char *str, uint str_length TSRMLS_DC){ apr_bucket *b; apr_bucket_brigade *bb; apr_bucket_alloc_t *ba; ap_filter_t *f; /* remaining output filters */ php_struct *ctx; ctx = SG(server_context); f = ctx->f; if (str_length == 0) return 0; ba = f->c->bucket_alloc; bb = apr_brigade_create(ctx->r->pool, ba); b = apr_bucket_transient_create(str, str_length, ba); APR_BRIGADE_INSERT_TAIL(bb, b);#if 0 /* Add a Flush bucket to the end of this brigade, so that * the transient buckets above are more likely to make it out * the end of the filter instead of having to be copied into * someone's setaside. */ b = apr_bucket_flush_create(ba); APR_BRIGADE_INSERT_TAIL(bb, b);#endif if (ap_pass_brigade(f->next, bb) != APR_SUCCESS || ctx->r->connection->aborted) { php_handle_aborted_connection(); } return str_length; /* we always consume all the data passed to us. */}static intphp_apache_sapi_header_handler(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers TSRMLS_DC){ php_struct *ctx; ap_filter_t *f; char *val; ctx = SG(server_context); f = ctx->r->output_filters; val = strchr(sapi_header->header, ':'); if (!val) { sapi_free_header(sapi_header); return 0; } *val = '\0'; do { val++; } while (*val == ' '); if (!strcasecmp(sapi_header->header, "content-type")) ctx->r->content_type = apr_pstrdup(ctx->r->pool, val); else if (sapi_header->replace) apr_table_set(ctx->r->headers_out, sapi_header->header, val); else apr_table_add(ctx->r->headers_out, sapi_header->header, val); sapi_free_header(sapi_header); return 0;}static intphp_apache_sapi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC){ php_struct *ctx = SG(server_context); ctx->r->status = SG(sapi_headers).http_response_code; return SAPI_HEADER_SENT_SUCCESSFULLY;}static intphp_apache_sapi_read_post(char *buf, uint count_bytes TSRMLS_DC){ int n; int to_read; php_struct *ctx = SG(server_context); to_read = ctx->post_len - ctx->post_idx; n = MIN(to_read, count_bytes); if (n > 0) { memcpy(buf, ctx->post_data + ctx->post_idx, n); ctx->post_idx += n; } else { if (ctx->post_data) free(ctx->post_data); ctx->post_data = NULL; } return n;}static struct stat*php_apache_sapi_get_stat(TSRMLS_D){ php_struct *ctx = SG(server_context); ctx->finfo.st_uid = ctx->r->finfo.user; ctx->finfo.st_gid = ctx->r->finfo.group; ctx->finfo.st_dev = ctx->r->finfo.device; ctx->finfo.st_ino = ctx->r->finfo.inode;#ifdef NETWARE ctx->finfo.st_atime.tv_sec = ctx->r->finfo.atime/1000000; ctx->finfo.st_mtime.tv_sec = ctx->r->finfo.mtime/1000000; ctx->finfo.st_ctime.tv_sec = ctx->r->finfo.ctime/1000000;#else ctx->finfo.st_atime = ctx->r->finfo.atime/1000000; ctx->finfo.st_mtime = ctx->r->finfo.mtime/1000000; ctx->finfo.st_ctime = ctx->r->finfo.ctime/1000000;#endif ctx->finfo.st_size = ctx->r->finfo.size; ctx->finfo.st_nlink = ctx->r->finfo.nlink; return &ctx->finfo;}static char *php_apache_sapi_read_cookies(TSRMLS_D){ php_struct *ctx = SG(server_context); const char *http_cookie; http_cookie = apr_table_get(ctx->r->headers_in, "cookie"); /* The SAPI interface should use 'const char *' */ return (char *) http_cookie;}static char *php_apache_sapi_getenv(char *name, size_t name_len TSRMLS_DC){ php_struct *ctx = SG(server_context); const char *env_var; env_var = apr_table_get(ctx->r->subprocess_env, name); return (char *) env_var;}static voidphp_apache_sapi_register_variables(zval *track_vars_array TSRMLS_DC){ php_struct *ctx = SG(server_context); const apr_array_header_t *arr = apr_table_elts(ctx->r->subprocess_env); char *key, *val; zval **path_translated_zv; APR_ARRAY_FOREACH_OPEN(arr, key, val) if (!val) val = empty_string; php_register_variable(key, val, track_vars_array TSRMLS_CC); APR_ARRAY_FOREACH_CLOSE() php_register_variable("PHP_SELF", ctx->r->uri, track_vars_array TSRMLS_CC); /* If PATH_TRANSLATED doesn't exist, copy it from SCRIPT_FILENAME */ if (!zend_hash_exists(Z_ARRVAL_P(track_vars_array), "PATH_TRANSLATED", sizeof("PATH_TRANSLATED")) && zend_hash_find(Z_ARRVAL_P(track_vars_array), "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME"), (void **) &path_translated_zv) == SUCCESS) { php_register_variable("PATH_TRANSLATED", Z_STRVAL_PP(path_translated_zv), track_vars_array TSRMLS_CC); }}static voidphp_apache_sapi_flush(void *server_context){ php_struct *ctx; apr_bucket_brigade *bb; apr_bucket_alloc_t *ba; apr_bucket *b; ap_filter_t *f; /* output filters */ TSRMLS_FETCH(); ctx = server_context; /* If we haven't registered a server_context yet, * then don't bother flushing. */ if (!server_context) return; sapi_send_headers(TSRMLS_C); ctx->r->status = SG(sapi_headers).http_response_code; SG(headers_sent) = 1; f = ctx->f; /* Send a flush bucket down the filter chain. The current default * handler seems to act on the first flush bucket, but ignores * all further flush buckets. */ ba = ctx->r->connection->bucket_alloc; bb = apr_brigade_create(ctx->r->pool, ba); b = apr_bucket_flush_create(ba); APR_BRIGADE_INSERT_TAIL(bb, b); if (ap_pass_brigade(f->next, bb) != APR_SUCCESS || ctx->r->connection->aborted) { php_handle_aborted_connection(); }}static void php_apache_sapi_log_message(char *msg){ php_struct *ctx; TSRMLS_FETCH(); ctx = SG(server_context); if (ctx == NULL) { /* we haven't initialized our ctx yet, oh well */ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, NULL, "%s", msg); } else { ap_log_error(APLOG_MARK, APLOG_ERR, 0, ctx->r->server, "%s", msg); }}static intphp_apache_disable_caching(ap_filter_t *f){ /* Identify PHP scripts as non-cacheable, thus preventing * Apache from sending a 304 status when the browser sends * If-Modified-Since header. */ f->r->no_local_copy = 1; return OK;}extern zend_module_entry php_apache_module;static int php_apache2_startup(sapi_module_struct *sapi_module){ if (php_module_startup(sapi_module, &php_apache_module, 1)==FAILURE) { return FAILURE; } return SUCCESS;}static sapi_module_struct apache2_sapi_module = { "apache2filter", "Apache 2.0 Filter", php_apache2_startup, /* startup */ php_module_shutdown_wrapper, /* shutdown */ NULL, /* activate */ NULL, /* deactivate */ php_apache_sapi_ub_write, /* unbuffered write */ php_apache_sapi_flush, /* flush */ php_apache_sapi_get_stat, /* get uid */ php_apache_sapi_getenv, /* getenv */ php_error, /* error handler */ php_apache_sapi_header_handler, /* header handler */ php_apache_sapi_send_headers, /* send headers handler */ NULL, /* send header handler */ php_apache_sapi_read_post, /* read POST data */ php_apache_sapi_read_cookies, /* read Cookies */ php_apache_sapi_register_variables, php_apache_sapi_log_message, /* Log message */ NULL, /* php_ini_path_override */ NULL, /* Block interruptions */ NULL, /* Unblock interruptions */ STANDARD_SAPI_MODULE_PROPERTIES};static int php_input_filter(ap_filter_t *f, apr_bucket_brigade *bb, ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes){ php_struct *ctx; long old_index; apr_bucket *b; const char *str; apr_size_t n; apr_status_t rv; TSRMLS_FETCH(); if (f->r->proxyreq) { return ap_get_brigade(f->next, bb, mode, block, readbytes); } ctx = SG(server_context); if (ctx == NULL) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -