com php-src: Merge branch 'master' into issue-67910: README.PARAMETER_PARSING_API build/build.mk configu re.in ext/interbase/php_ibase_udf.c
ext/intl/collat or/collator_sort.c ext/mssql/php_mssql.c ext/mysqli /mysqli_warning.c sapi/apache/mod_php7.c sapi/apa che/mod_php7.h
sapi/apache/php_apache_http.h sa pi/apache2filter/php_apache.h sapi/apache2filter/sapi _apache2.c sapi/apache_hooks/mod_php7.h sapi/ caudium/caudium.c
sapi/isapi/php7isapi.c sapi/nsap

From: Date: Sat, 20 Sep 2014 08:09:21 +0000
Subject: com php-src: Merge branch 'master' into issue-67910: README.PARAMETER_PARSING_API build/build.mk configu re.in ext/interbase/php_ibase_udf.c
ext/intl/collat or/collator_sort.c ext/mssql/php_mssql.c ext/mysqli /mysqli_warning.c sapi/apache/mod_php7.c sapi/apa che/mod_php7.h
sapi/apache/php_apache_http.h sa pi/apache2filter/php_apache.h sapi/apache2filter/sapi _apache2.c sapi/apache_hooks/mod_php7.h sapi/ caudium/caudium.c
sapi/isapi/php7isapi.c sapi/nsap
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-81810@lists.php.net to get a copy of this message
Commit: 8eb7e7bf7f63f49e97f112871df47c6d6213e715 Author: Florian MARGAINE <florian@margaine.com> Sat, 20 Sep 2014 10:09:21 +0200 Parents: 58ed832fc99d5c5aeac7849819bbc4e0dc90d698 95836a350420cabc2e8e5f5c45131fba46504a3b Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=8eb7e7bf7f63f49e97f112871df47c6d6213e715 Log: Merge branch 'master' into issue-67910 Conflicts: README.PARAMETER_PARSING_API ext/gmp/tests/001.phpt Changed paths: MM README.PARAMETER_PARSING_API MM build/build.mk MM configure.in MM ext/interbase/php_ibase_udf.c MM ext/intl/collator/collator_sort.c MM ext/mssql/php_mssql.c MM ext/mysqli/mysqli_warning..c MA sapi/apache/mod_php7.c MA sapi/apache/mod_php7.h MM sapi/apache/php_apache_http.h MM sapi/apache2filter/php_apache.h MM sapi/apache2filter/sapi_apache2.c MA sapi/apache_hooks/mod_php7.h MM sapi/caudium/caudium.c MA sapi/isapi/php7isapi.c MM sapi/nsapi/nsapi.c MM sapi/pi3web/pi3web_sapi.c MM sapi/roxen/roxen.c MM win32/build/Makefile MM win32/build/config.w32

diff --cc README.PARAMETER_PARSING_API index 446b581,ab62502..46e190e --- a/README.PARAMETER_PARSING_API +++ b/README.PARAMETER_PARSING_API @@@ -227,7 -221,4 +221,3 @@@ if (zend_parse_parameters(ZEND_NUM_ARGS for (i = 0; i < num_varargs; i++) { /* do something with varargs[i] */ } -- - if (varargs) { - efree(varargs); - } diff --cc sapi/apache/mod_php7.c index 582de52,0000000..6875869 mode 100644,000000..100644 --- a/sapi/apache/mod_php7.c +++ b/sapi/apache/mod_php7.c @@@ -1,1050 -1,0 +1,1050 @@@ +/* + +----------------------------------------------------------------------+ - | PHP Version 5 | ++ | PHP Version 7 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 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: Rasmus Lerdorf <rasmus@php.net> | + | (with helpful hints from Dean Gaudet <dgaudet@arctic.org> | + | PHP 4.0 patches by Zeev Suraski <zeev@zend.com> | + +----------------------------------------------------------------------+ + */ +/* $Id$ */ + +#include "php_apache_http.h" +#include "http_conf_globals.h" + +#ifdef NETWARE +#define SIGPIPE SIGINT +#endif + +#undef shutdown + +/* {{{ Prototypes + */ +int apache_php_module_main(request_rec *r, int display_source_mode TSRMLS_DC); +static void php_save_umask(void); +static void php_restore_umask(void); +static int sapi_apache_read_post(char *buffer, uint count_bytes TSRMLS_DC); +static char *sapi_apache_read_cookies(TSRMLS_D); +static int sapi_apache_header_handler(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers TSRMLS_DC); +static int sapi_apache_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC); +static int send_php(request_rec *r, int display_source_mode, char *filename); +static int send_parsed_php(request_rec * r); +static int send_parsed_php_source(request_rec * r); +static int php_xbithack_handler(request_rec * r); +static void php_init_handler(server_rec *s, pool *p); +/* }}} */ + +#if MODULE_MAGIC_NUMBER >= 19970728 +static void php_child_exit_handler(server_rec *s, pool *p); +#endif + +#if MODULE_MAGIC_NUMBER > 19961007 +#define CONST_PREFIX const +#else +#define CONST_PREFIX +#endif +static CONST_PREFIX char *php_apache_value_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode); +static CONST_PREFIX char *php_apache_value_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2); +static CONST_PREFIX char *php_apache_admin_value_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2); +static CONST_PREFIX char *php_apache_flag_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2); +static CONST_PREFIX char *php_apache_flag_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode); +static CONST_PREFIX char *php_apache_admin_flag_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2); + +/* ### these should be defined in mod_php7.h or somewhere else */ +#define USE_PATH 1 +#define IGNORE_URL 2 +#define MAX_STATUS_LENGTH sizeof("xxxx LONGEST POSSIBLE STATUS DESCRIPTION") + +module MODULE_VAR_EXPORT php7_module; + +int saved_umask; +static unsigned char apache_php_initialized; + +typedef struct _php_per_dir_entry { + char *key; + char *value; + uint key_length; + uint value_length; + int type; + char htaccess; +} php_per_dir_entry; + +/* some systems are missing these from their header files */ + +/* {{{ php_save_umask + */ +static void php_save_umask(void) +{ + saved_umask = umask(077); + umask(saved_umask); +} +/* }}} */ + +/* {{{ sapi_apache_ub_write + */ +static int sapi_apache_ub_write(const char *str, uint str_length TSRMLS_DC) +{ + int ret=0; + + if (SG(server_context)) { + ret = rwrite(str, str_length, (request_rec *) SG(server_context)); + } + if (ret != str_length) { + php_handle_aborted_connection(); + } + return ret; +} +/* }}} */ + +/* {{{ sapi_apache_flush + */ +static void sapi_apache_flush(void *server_context TSRMLS_DC) +{ + if (server_context) { +#if MODULE_MAGIC_NUMBER > 19970110 + rflush((request_rec *) server_context); +#else + bflush((request_rec *) server_context->connection->client); +#endif + } +} +/* }}} */ + +/* {{{ sapi_apache_read_post + */ +static int sapi_apache_read_post(char *buffer, uint count_bytes TSRMLS_DC) +{ + int total_read_bytes=0, read_bytes; + request_rec *r = (request_rec *) SG(server_context); + void (*handler)(int); + + /* + * This handles the situation where the browser sends a Expect: 100-continue header + * and needs to receive confirmation from the server on whether or not it can send + * the rest of the request. RFC 2616 + * + */ + if (!SG(read_post_bytes) && !ap_should_client_block(r)) { + return total_read_bytes; + } + + handler = signal(SIGPIPE, SIG_IGN); + while (total_read_bytes<count_bytes) { + hard_timeout("Read POST information", r); /* start timeout timer */ + read_bytes = get_client_block(r, buffer+total_read_bytes, count_bytes-total_read_bytes); + reset_timeout(r); + if (read_bytes<=0) { + break; + } + total_read_bytes += read_bytes; + } + signal(SIGPIPE, handler); + return total_read_bytes; +} +/* }}} */ + +/* {{{ sapi_apache_read_cookies + */ +static char *sapi_apache_read_cookies(TSRMLS_D) +{ + return (char *) table_get(((request_rec *) SG(server_context))->subprocess_env, "HTTP_COOKIE"); +} +/* }}} */ + +/* {{{ sapi_apache_header_handler + */ +static int sapi_apache_header_handler(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers TSRMLS_DC) +{ + char *header_name, *header_content, *p; + request_rec *r = (request_rec *) SG(server_context); + if(!r) { + return 0; + } + + switch(op) { + case SAPI_HEADER_DELETE_ALL: + clear_table(r->headers_out); + return 0; + + case SAPI_HEADER_DELETE: + table_unset(r->headers_out, sapi_header->header); + return 0; + + case SAPI_HEADER_ADD: + case SAPI_HEADER_REPLACE: + header_name = sapi_header->header; + + header_content = p = strchr(header_name, ':'); + if (!p) { + return 0; + } + + *p = 0; + do { + header_content++; + } while (*header_content==' '); + + if (!strcasecmp(header_name, "Content-Type")) { + r->content_type = pstrdup(r->pool, header_content); + } else if (!strcasecmp(header_name, "Content-Length")) { + ap_set_content_length(r, strtol(header_content, (char **)NULL, 10)); + } else if (!strcasecmp(header_name, "Set-Cookie")) { + table_add(r->headers_out, header_name, header_content); + } else if (op == SAPI_HEADER_REPLACE) { + table_set(r->headers_out, header_name, header_content); + } else { + table_add(r->headers_out, header_name, header_content); + } + + *p = ':'; /* a well behaved header handler shouldn't change its original arguments */ + + return SAPI_HEADER_ADD; + + default: + return 0; + } +} +/* }}} */ + +/* {{{ sapi_apache_send_headers + */ +static int sapi_apache_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) +{ + request_rec *r = SG(server_context); + const char *sline = SG(sapi_headers).http_status_line; + int sline_len; + + if(r == NULL) { /* server_context is not here anymore */ + return SAPI_HEADER_SEND_FAILED; + } + + r->status = SG(sapi_headers).http_response_code; + + /* httpd requires that r->status_line is set to the first digit of + * the status-code: */ + if (sline && ((sline_len = strlen(sline)) > 12) && strncmp(sline, "HTTP/1.", 7) == 0 && sline[8] == ' ' && sline[12] == ' ') { + if ((sline_len - 9) > MAX_STATUS_LENGTH) { + r->status_line = ap_pstrndup(r->pool, sline + 9, MAX_STATUS_LENGTH); + } else { + r->status_line = ap_pstrndup(r->pool, sline + 9, sline_len - 9); + } + } + + if(r->status==304) { + send_error_response(r,0); + } else { + send_http_header(r); + } + return SAPI_HEADER_SENT_SUCCESSFULLY; +} +/* }}} */ + +/* {{{ sapi_apache_register_server_variables + */ +static void sapi_apache_register_server_variables(zval *track_vars_array TSRMLS_DC) +{ + register int i; + array_header *arr = table_elts(((request_rec *) SG(server_context))->subprocess_env); + table_entry *elts = (table_entry *) arr->elts; + zval *path_translated; + HashTable *symbol_table; + unsigned int new_val_len; + + for (i = 0; i < arr->nelts; i++) { + char *val; + int val_len; + + if (elts[i].val) { + val = elts[i].val; + } else { + val = ""; + } + val_len = strlen(val); + if (sapi_module.input_filter(PARSE_SERVER, elts[i].key, &val, val_len, &new_val_len TSRMLS_CC)) { + php_register_variable_safe(elts[i].key, val, new_val_len, track_vars_array TSRMLS_CC); + } + } + + /* If PATH_TRANSLATED doesn't exist, copy it from SCRIPT_FILENAME */ + if (track_vars_array) { + symbol_table = Z_ARRVAL_P(track_vars_array); + } else { + symbol_table = NULL; + } + if (symbol_table + && !zend_hash_str_exists(symbol_table, "PATH_TRANSLATED", sizeof("PATH_TRANSLATED")-1) + && (path_translated = zend_hash_str_find(symbol_table, "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME")-1)) != NULL) { + php_register_variable("PATH_TRANSLATED", Z_STRVAL_P(path_translated), track_vars_array TSRMLS_CC); + } + + if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &((request_rec *) SG(server_context))->uri, strlen(((request_rec *) SG(server_context))->uri), &new_val_len TSRMLS_CC)) { + php_register_variable("PHP_SELF", ((request_rec *) SG(server_context))->uri, track_vars_array TSRMLS_CC); + } +} +/* }}} */ + +/* {{{ php_apache_startup + */ +static int php_apache_startup(sapi_module_struct *sapi_module) +{ + if (php_module_startup(sapi_module, &apache_module_entry, 1) == FAILURE) { + return FAILURE; + } else { + return SUCCESS; + } +} +/* }}} */ + +/* {{{ php_apache_log_message + */ +static void php_apache_log_message(char *message TSRMLS_DC) +{ + if (SG(server_context)) { +#if MODULE_MAGIC_NUMBER >= 19970831 + aplog_error(NULL, 0, APLOG_ERR | APLOG_NOERRNO, ((request_rec *) SG(server_context))->server, "%s", message); +#else + log_error(message, ((request_rec *) SG(server_context))->server); +#endif + } else { + fprintf(stderr, "%s\n", message); + } +} +/* }}} */ + +/* {{{ php_apache_request_shutdown + */ +static void php_apache_request_shutdown(void *dummy) +{ + TSRMLS_FETCH(); + + php_output_set_status(PHP_OUTPUT_DISABLED TSRMLS_CC); + if (AP(in_request)) { + AP(in_request) = 0; + php_request_shutdown(dummy); + } + SG(server_context) = NULL; + /* + * The server context (request) is NOT invalid by the time + * run_cleanups() is called + */ +} +/* }}} */ + +/* {{{ php_apache_sapi_activate + */ +static int php_apache_sapi_activate(TSRMLS_D) +{ + request_rec *r = (request_rec *) SG(server_context); + + /* + * For the Apache module version, this bit of code registers a cleanup + * function that gets triggered when our request pool is destroyed. + * We need this because at any point in our code we can be interrupted + * and that may happen before we have had time to free our memory. + * The php_request_shutdown function needs to free all outstanding allocated + * memory. + */ + block_alarms(); + register_cleanup(r->pool, NULL, php_apache_request_shutdown, php_request_shutdown_for_exec); + AP(in_request)=1; + unblock_alarms(); + + /* Override the default headers_only value - sometimes "GET" requests should actually only + * send headers. + */ + SG(request_info).headers_only = r->header_only; + return SUCCESS; +} +/* }}} */ + +/* {{{ php_apache_get_stat + */ +static struct stat *php_apache_get_stat(TSRMLS_D) +{ + return &((request_rec *) SG(server_context))->finfo; +} +/* }}} */ + +/* {{{ php_apache_getenv + */ +static char *php_apache_getenv(char *name, size_t name_len TSRMLS_DC) +{ + if (SG(server_context) == NULL) { + return NULL; + } + + return (char *) table_get(((request_rec *) SG(server_context))->subprocess_env, name); +} +/* }}} */ + +/* {{{ sapi_apache_get_fd + */ +static int sapi_apache_get_fd(int *nfd TSRMLS_DC) +{ +#if PHP_APACHE_HAVE_CLIENT_FD + request_rec *r = SG(server_context); + int fd; + + fd = r->connection->client->fd; + + if (fd >= 0) { + if (nfd) *nfd = fd; + return SUCCESS; + } +#endif + return FAILURE; +} +/* }}} */ + +/* {{{ sapi_apache_force_http_10 + */ +static int sapi_apache_force_http_10(TSRMLS_D) +{ + request_rec *r = SG(server_context); + + r->proto_num = HTTP_VERSION(1,0); + + return SUCCESS; +} +/* }}} */ + +/* {{{ sapi_apache_get_target_uid + */ +static int sapi_apache_get_target_uid(uid_t *obj TSRMLS_DC) +{ + *obj = ap_user_id; + return SUCCESS; +} +/* }}} */ + +/* {{{ sapi_apache_get_target_gid + */ +static int sapi_apache_get_target_gid(gid_t *obj TSRMLS_DC) +{ + *obj = ap_group_id; + return SUCCESS; +} +/* }}} */ + +/* {{{ php_apache_get_request_time + */ +static double php_apache_get_request_time(TSRMLS_D) +{ + return (double) ((request_rec *)SG(server_context))->request_time; +} +/* }}} */ + +/* {{{ sapi_apache_child_terminate + */ +static void sapi_apache_child_terminate(TSRMLS_D) +{ +#ifndef MULTITHREAD + ap_child_terminate((request_rec *)SG(server_context)); +#endif +} +/* }}} */ + +/* {{{ sapi_module_struct apache_sapi_module + */ +static sapi_module_struct apache_sapi_module = { + "apache", /* name */ + "Apache", /* pretty name */ + + php_apache_startup, /* startup */ + php_module_shutdown_wrapper, /* shutdown */ + + php_apache_sapi_activate, /* activate */ + NULL, /* deactivate */ + + sapi_apache_ub_write, /* unbuffered write */ + sapi_apache_flush, /* flush */ + php_apache_get_stat, /* get uid */ + php_apache_getenv, /* getenv */ + + php_error, /* error handler */ + + sapi_apache_header_handler, /* header handler */ + sapi_apache_send_headers, /* send headers handler */ + NULL, /* send header handler */ + + sapi_apache_read_post, /* read POST data */ + sapi_apache_read_cookies, /* read Cookies */ + + sapi_apache_register_server_variables, /* register server variables */ + php_apache_log_message, /* Log message */ + php_apache_get_request_time, /* Get request time */ + sapi_apache_child_terminate, + + NULL, /* php.ini path override */ + +#ifdef PHP_WIN32 + NULL, + NULL, +#else + block_alarms, /* Block interruptions */ + unblock_alarms, /* Unblock interruptions */ +#endif + + NULL, /* default post reader */ + NULL, /* treat data */ + NULL, /* exe location */ + 0, /* ini ignore */ + 0, /* ini ignore cwd */ + sapi_apache_get_fd, + sapi_apache_force_http_10, + sapi_apache_get_target_uid, + sapi_apache_get_target_gid +}; +/* }}} */ + +/* {{{ php_restore_umask + */ +static void php_restore_umask(void) +{ + umask(saved_umask); +} +/* }}} */ + +/* {{{ init_request_info + */ +static void init_request_info(TSRMLS_D) +{ + request_rec *r = ((request_rec *) SG(server_context)); + char *content_length = (char *) table_get(r->subprocess_env, "CONTENT_LENGTH"); + const char *authorization=NULL; + char *tmp, *tmp_user; + + SG(request_info).query_string = r->args; + SG(request_info).path_translated = r->filename; + SG(request_info).request_uri = r->uri; + SG(request_info).request_method = (char *)r->method; + SG(request_info).content_type = (char *) table_get(r->subprocess_env, "CONTENT_TYPE"); + SG(request_info).content_length = (content_length ? atol(content_length) : 0); + SG(sapi_headers).http_response_code = r->status; + SG(request_info).proto_num = r->proto_num; + + if (r->headers_in) { + authorization = table_get(r->headers_in, "Authorization"); + } + + SG(request_info).auth_user = NULL; + SG(request_info).auth_password = NULL; + SG(request_info).auth_digest = NULL; + + if (authorization) { + char *p = getword(r->pool, &authorization, ' '); + if (!strcasecmp(p, "Basic")) { + tmp = uudecode(r->pool, authorization); + tmp_user = getword_nulls_nc(r->pool, &tmp, ':'); + if (tmp_user) { + r->connection->user = pstrdup(r->connection->pool, tmp_user); + r->connection->ap_auth_type = "Basic"; + SG(request_info).auth_user = estrdup(tmp_user); + } + if (tmp) { + SG(request_info).auth_password = estrdup(tmp); + } + } else if (!strcasecmp(p, "Digest")) { + r->connection->ap_auth_type = "Digest"; + SG(request_info).auth_digest = estrdup(authorization); + } + } +} +/* }}} */ + +/* {{{ php_apache_alter_ini_entries + */ +static int php_apache_alter_ini_entries(php_per_dir_entry *per_dir_entry TSRMLS_DC) +{ + zend_string *key = STR_INIT(per_dir_entry->key, per_dir_entry->key_length, 0); - zend_alter_ini_entry(key, per_dir_entry->value, per_dir_entry->value_length, per_dir_entry->type, per_dir_entry->htaccess?PHP_INI_STAGE_HTACCESS:PHP_INI_STAGE_ACTIVATE); ++ zend_alter_ini_entry_chars(key, per_dir_entry->value, per_dir_entry->value_length, per_dir_entry->type, per_dir_entry->htaccess?PHP_INI_STAGE_HTACCESS:PHP_INI_STAGE_ACTIVATE); + STR_RELEASE(key); + return 0; +} +/* }}} */ + +/* {{{ php_apache_get_default_mimetype + */ +static char *php_apache_get_default_mimetype(request_rec *r TSRMLS_DC) +{ + + char *mimetype; + if (SG(default_mimetype) || SG(default_charset)) { + /* Assume output will be of the default MIME type. Individual + scripts may change this later. */ + char *tmpmimetype; + tmpmimetype = sapi_get_default_content_type(TSRMLS_C); + mimetype = pstrdup(r->pool, tmpmimetype); + efree(tmpmimetype); + } else { + mimetype = SAPI_DEFAULT_MIMETYPE "; charset=" SAPI_DEFAULT_CHARSET; + } + return mimetype; +} +/* }}} */ + +/* {{{ send_php + */ +static int send_php(request_rec *r, int display_source_mode, char *filename) +{ + int retval; + HashTable *per_dir_conf; + TSRMLS_FETCH(); + + if (AP(in_request)) { + zend_file_handle fh; + + fh.filename = r->filename; + fh.opened_path = NULL; + fh.free_filename = 0; + fh.type = ZEND_HANDLE_FILENAME; + + zend_execute_scripts(ZEND_INCLUDE TSRMLS_CC, NULL, 1, &fh); + return OK; + } + + SG(server_context) = r; + + zend_first_try { + + /* Make sure file exists */ + if (filename == NULL && r->finfo.st_mode == 0) { + return DECLINED; + } + + per_dir_conf = (HashTable *) get_module_config(r->per_dir_config, &php7_module); + if (per_dir_conf) { + zend_hash_apply((HashTable *) per_dir_conf, (apply_func_t) php_apache_alter_ini_entries TSRMLS_CC); + } + + /* If PHP parser engine has been turned off with an "engine off" + * directive, then decline to handle this request + */ + if (!AP(engine)) { + r->content_type = php_apache_get_default_mimetype(r TSRMLS_CC); + zend_try { + zend_ini_deactivate(TSRMLS_C); + } zend_end_try(); + return DECLINED; + } + if (filename == NULL) { + filename = r->filename; + } + + /* Apache 1.2 has a more complex mechanism for reading POST data */ +#if MODULE_MAGIC_NUMBER > 19961007 + if ((retval = setup_client_block(r, REQUEST_CHUNKED_DECHUNK))) { + zend_try { + zend_ini_deactivate(TSRMLS_C); + } zend_end_try(); + return retval; + } +#endif + + if (AP(last_modified)) { +#if MODULE_MAGIC_NUMBER < 19970912 + if ((retval = set_last_modified(r, r->finfo.st_mtime))) { + zend_try { + zend_ini_deactivate(TSRMLS_C); + } zend_end_try(); + return retval; + } +#else + update_mtime (r, r->finfo.st_mtime); + set_last_modified(r); + set_etag(r); +#endif + } + /* Assume output will be of the default MIME type. Individual + scripts may change this later in the request. */ + r->content_type = php_apache_get_default_mimetype(r TSRMLS_CC); + + /* Init timeout */ + hard_timeout("send", r); + + php_save_umask(); + add_common_vars(r); + add_cgi_vars(r); + + init_request_info(TSRMLS_C); + apache_php_module_main(r, display_source_mode TSRMLS_CC); + + /* Done, restore umask, turn off timeout, close file and return */ + php_restore_umask(); + kill_timeout(r); + } zend_end_try(); + + return OK; +} +/* }}} */ + +/* {{{ send_parsed_php + */ +static int send_parsed_php(request_rec * r) +{ + int result = send_php(r, 0, NULL); + TSRMLS_FETCH(); + + ap_table_setn(r->notes, "mod_php_memory_usage", + ap_psprintf(r->pool, "%lu", zend_memory_peak_usage(1 TSRMLS_CC))); + + return result; +} +/* }}} */ + +/* {{{ send_parsed_php_source + */ +static int send_parsed_php_source(request_rec * r) +{ + return send_php(r, 1, NULL); +} +/* }}} */ + +/* {{{ destroy_per_dir_entry + */ +static void destroy_per_dir_entry(zval *zv) +{ + php_per_dir_entry *per_dir_entry = Z_PTR_P(zv); + + free(per_dir_entry->key); + free(per_dir_entry->value); + free(per_dir_entry); +} +/* }}} */ + +/* {{{ copy_per_dir_entry + */ +static void copy_per_dir_entry(zval *zv) +{ + php_per_dir_entry *old_per_dir_entry = Z_PTR_P(zv); + php_per_dir_entry *new_per_dir_entry = malloc(sizeof(php_per_dir_entry)); + + memcpy(new_per_dir_entry, old_per_dir_entry, sizeof(php_per_dir_entry)); + Z_PTR_P(zv) = new_per_dir_entry; + + new_per_dir_entry->key = (char *) malloc(old_per_dir_entry->key_length+1); + memcpy(new_per_dir_entry->key, old_per_dir_entry->key, old_per_dir_entry->key_length); + new_per_dir_entry->key[new_per_dir_entry->key_length] = 0; + + new_per_dir_entry->value = (char *) malloc(old_per_dir_entry->value_length+1); + memcpy(new_per_dir_entry->value, old_per_dir_entry->value, old_per_dir_entry->value_length); + new_per_dir_entry->value[new_per_dir_entry->value_length] = 0; +} +/* }}} */ + +/* {{{ should_overwrite_per_dir_entry + */ +static zend_bool should_overwrite_per_dir_entry(HashTable *target_ht, zval *zv, zend_hash_key *hash_key, void *pData) +{ + php_per_dir_entry *new_per_dir_entry = Z_PTR_P(zv); + php_per_dir_entry *orig_per_dir_entry; + + if ((orig_per_dir_entry = zend_hash_find_ptr(target_ht, hash_key->key)) == NULL) { + return 1; /* does not exist in dest, copy from source */ + } + + if (orig_per_dir_entry->type==PHP_INI_SYSTEM + && new_per_dir_entry->type!=PHP_INI_SYSTEM) { + return 0; + } else { + return 1; + } +} +/* }}} */ + +/* {{{ php_destroy_per_dir_info + */ +static void php_destroy_per_dir_info(HashTable *per_dir_info) +{ + zend_hash_destroy(per_dir_info); + free(per_dir_info); +} +/* }}} */ + +/* {{{ php_create_dir + */ +static void *php_create_dir(pool *p, char *dummy) +{ + HashTable *per_dir_info; + + per_dir_info = (HashTable *) malloc(sizeof(HashTable)); + zend_hash_init_ex(per_dir_info, 5, NULL, destroy_per_dir_entry, 1, 0); + register_cleanup(p, (void *) per_dir_info, (void (*)(void *)) php_destroy_per_dir_info, (void (*)(void *)) zend_hash_destroy); + + return per_dir_info; +} +/* }}} */ + +/* {{{ php_merge_dir + */ +static void *php_merge_dir(pool *p, void *basev, void *addv) +{ + /* This function *must* not modify addv or basev */ + HashTable *new; + + /* need a copy of addv to merge */ + new = php_create_dir(p, "php_merge_dir"); + zend_hash_copy(new, (HashTable *) basev, copy_per_dir_entry); + + zend_hash_merge_ex(new, (HashTable *) addv, copy_per_dir_entry, should_overwrite_per_dir_entry, NULL); + return new; +} +/* }}} */ + +/* {{{ php_apache_value_handler_ex + */ +static CONST_PREFIX char *php_apache_value_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode) +{ + php_per_dir_entry per_dir_entry; + + if (!apache_php_initialized) { + apache_php_initialized = 1; +#ifdef ZTS + tsrm_startup(1, 1, 0, NULL); +#endif + sapi_startup(&apache_sapi_module); + php_apache_startup(&apache_sapi_module); + } + per_dir_entry.type = mode; + per_dir_entry.htaccess = ((cmd->override & (RSRC_CONF|ACCESS_CONF)) == 0); + + if (strcasecmp(arg2, "none") == 0) { + arg2 = ""; + } + + per_dir_entry.key_length = strlen(arg1); + per_dir_entry.value_length = strlen(arg2); + + per_dir_entry.key = (char *) malloc(per_dir_entry.key_length+1); + memcpy(per_dir_entry.key, arg1, per_dir_entry.key_length); + per_dir_entry.key[per_dir_entry.key_length] = 0; + + per_dir_entry.value = (char *) malloc(per_dir_entry.value_length+1); + memcpy(per_dir_entry.value, arg2, per_dir_entry.value_length); + per_dir_entry.value[per_dir_entry.value_length] = 0; + + zend_hash_str_update_mem(conf, per_dir_entry.key, per_dir_entry.key_length, &per_dir_entry, sizeof(php_per_dir_entry)); + return NULL; +} +/* }}} */ + +/* {{{ php_apache_value_handler + */ +static CONST_PREFIX char *php_apache_value_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2) +{ + return php_apache_value_handler_ex(cmd, conf, arg1, arg2, PHP_INI_PERDIR); +} +/* }}} */ + +/* {{{ php_apache_admin_value_handler + */ +static CONST_PREFIX char *php_apache_admin_value_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2) +{ + return php_apache_value_handler_ex(cmd, conf, arg1, arg2, PHP_INI_SYSTEM); +} +/* }}} */ + +/* {{{ php_apache_flag_handler_ex + */ +static CONST_PREFIX char *php_apache_flag_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode) +{ + char bool_val[2]; + + if (!strcasecmp(arg2, "On") || (arg2[0] == '1' && arg2[1] == '\0')) { + bool_val[0] = '1'; + } else { + bool_val[0] = '0'; + } + bool_val[1] = 0; + + return php_apache_value_handler_ex(cmd, conf, arg1, bool_val, mode); +} +/* }}} */ + +/* {{{ php_apache_flag_handler + */ +static CONST_PREFIX char *php_apache_flag_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2) +{ + return php_apache_flag_handler_ex(cmd, conf, arg1, arg2, PHP_INI_PERDIR); +} +/* }}} */ + +/* {{{ php_apache_admin_flag_handler + */ +static CONST_PREFIX char *php_apache_admin_flag_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2) +{ + return php_apache_flag_handler_ex(cmd, conf, arg1, arg2, PHP_INI_SYSTEM); +} +/* }}} */ + +/* {{{ php_apache_phpini_set + */ +static CONST_PREFIX char *php_apache_phpini_set(cmd_parms *cmd, HashTable *conf, char *arg) +{ + if (apache_sapi_module.php_ini_path_override) { + return "Only first PHPINIDir directive honored per configuration tree - subsequent ones ignored"; + } + apache_sapi_module.php_ini_path_override = ap_server_root_relative(cmd->pool, arg); + return NULL; +} +/* }}} */ + +/* {{{ int php_xbithack_handler(request_rec * r) + */ +static int php_xbithack_handler(request_rec * r) +{ + HashTable *per_dir_conf; + TSRMLS_FETCH(); + + if (!(r->finfo.st_mode & S_IXUSR)) { + return DECLINED; + } + per_dir_conf = (HashTable *) get_module_config(r->per_dir_config, &php7_module); + if (per_dir_conf) { + zend_hash_apply((HashTable *) per_dir_conf, (apply_func_t) php_apache_alter_ini_entries TSRMLS_CC); + } + if(!AP(xbithack)) { + zend_try { + zend_ini_deactivate(TSRMLS_C); + } zend_end_try(); + return DECLINED; + } + return send_parsed_php(r); +} +/* }}} */ + +/* {{{ apache_php_module_shutdown_wrapper + */ +static void apache_php_module_shutdown_wrapper(void) +{ + apache_php_initialized = 0; + apache_sapi_module.shutdown(&apache_sapi_module); + +#if MODULE_MAGIC_NUMBER >= 19970728 + /* This function is only called on server exit if the apache API + * child_exit handler exists, so shutdown globally + */ + sapi_shutdown(); +#endif + +#ifdef ZTS + tsrm_shutdown(); +#endif +} +/* }}} */ + +#if MODULE_MAGIC_NUMBER >= 19970728 +/* {{{ php_child_exit_handler + */ +static void php_child_exit_handler(server_rec *s, pool *p) +{ +/* apache_php_initialized = 0; */ + apache_sapi_module.shutdown(&apache_sapi_module); + +#ifdef ZTS + tsrm_shutdown(); +#endif +} +/* }}} */ +#endif + +/* {{{ void php_init_handler(server_rec *s, pool *p) + */ +static void php_init_handler(server_rec *s, pool *p) +{ + register_cleanup(p, NULL, (void (*)(void *))apache_php_module_shutdown_wrapper, (void (*)(void *))php_module_shutdown_for_exec); + if (!apache_php_initialized) { + apache_php_initialized = 1; +#ifdef ZTS + tsrm_startup(1, 1, 0, NULL); +#endif + sapi_startup(&apache_sapi_module); + php_apache_startup(&apache_sapi_module); + } +#if MODULE_MAGIC_NUMBER >= 19980527 + { + TSRMLS_FETCH(); + if (PG(expose_php)) { + ap_add_version_component("PHP/" PHP_VERSION); + } + } +#endif +} +/* }}} */ + +/* {{{ handler_rec php_handlers[] + */ +handler_rec php_handlers[] = +{ + {"application/x-httpd-php", send_parsed_php}, + {"application/x-httpd-php-source", send_parsed_php_source}, + {"text/html", php_xbithack_handler}, + {NULL} +}; +/* }}} */ + +/* {{{ command_rec php_commands[] + */ +command_rec php_commands[] = +{ + {"php_value", php_apache_value_handler, NULL, OR_OPTIONS, TAKE2, "PHP Value Modifier"}, + {"php_flag", php_apache_flag_handler, NULL, OR_OPTIONS, TAKE2, "PHP Flag Modifier"}, + {"php_admin_value", php_apache_admin_value_handler, NULL, ACCESS_CONF|RSRC_CONF, TAKE2, "PHP Value Modifier (Admin)"}, + {"php_admin_flag", php_apache_admin_flag_handler, NULL, ACCESS_CONF|RSRC_CONF, TAKE2, "PHP Flag Modifier (Admin)"}, + {"PHPINIDir", php_apache_phpini_set, NULL, RSRC_CONF, TAKE1, "Directory containing the php.ini file"}, + {NULL} +}; +/* }}} */ + +/* {{{ odule MODULE_VAR_EXPORT php7_module + */ +module MODULE_VAR_EXPORT php7_module = +{ + STANDARD_MODULE_STUFF, + php_init_handler, /* initializer */ + php_create_dir, /* per-directory config creator */ + php_merge_dir, /* dir merger */ + NULL, /* per-server config creator */ + NULL, /* merge server config */ + php_commands, /* command table */ + php_handlers, /* handlers */ + NULL, /* filename translation */ + NULL, /* check_user_id */ + NULL, /* check auth */ + NULL, /* check access */ + NULL, /* type_checker */ + NULL, /* fixups */ + NULL /* logger */ +#if MODULE_MAGIC_NUMBER >= 19970103 + , NULL /* header parser */ +#endif +#if MODULE_MAGIC_NUMBER >= 19970719 + , NULL /* child_init */ +#endif +#if MODULE_MAGIC_NUMBER >= 19970728 + , php_child_exit_handler /* child_exit */ +#endif +#if MODULE_MAGIC_NUMBER >= 19970902 + , NULL /* post read-request */ +#endif +}; +/* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --cc sapi/apache/mod_php7.h index de8aa1b,0000000..947d8b4 mode 100644,000000..100644 --- a/sapi/apache/mod_php7.h +++ b/sapi/apache/mod_php7.h @@@ -1,60 -1,0 +1,60 @@@ +/* + +----------------------------------------------------------------------+ - | PHP Version 5 | ++ | PHP Version 7 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 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. | + +----------------------------------------------------------------------+ + | Author: Rasmus Lerdorf <rasmus@php.net> | + +----------------------------------------------------------------------+ + */ +/* $Id$ */ + +#ifndef MOD_PHP7_H +#define MOD_PHP7_H + +#if !defined(WIN32) && !defined(WINNT) +#ifndef MODULE_VAR_EXPORT +#define MODULE_VAR_EXPORT +#endif +#endif + +typedef struct { + long engine; + long last_modified; + long xbithack; + long terminate_child; + zend_bool in_request; +} php_apache_info_struct; + +extern zend_module_entry apache_module_entry; + +#ifdef ZTS +extern int php_apache_info_id; +#define AP(v) TSRMG(php_apache_info_id, php_apache_info_struct *, v) +#else +extern php_apache_info_struct php_apache_info; +#define AP(v) (php_apache_info.v) +#endif + +/* fix for gcc4 visibility patch */ +#ifndef PHP_WIN32 +# undef MODULE_VAR_EXPORT +# define MODULE_VAR_EXPORT PHPAPI +#endif + +#endif /* MOD_PHP7_H */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + */ diff --cc sapi/apache_hooks/mod_php7.h index 612aab5,0000000..325ae1f mode 100644,000000..100644 --- a/sapi/apache_hooks/mod_php7.h +++ b/sapi/apache_hooks/mod_php7.h @@@ -1,88 -1,0 +1,88 @@@ +/* + +----------------------------------------------------------------------+ - | PHP Version 5 | ++ | PHP Version 7 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 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. | + +----------------------------------------------------------------------+ + | Author: Rasmus Lerdorf <rasmus@php.net> | + +----------------------------------------------------------------------+ + */ +/* $Id$ */ + +#ifndef MOD_PHP7_H +#define MOD_PHP7_H + +#if !defined(WIN32) && !defined(WINNT) +#ifndef MODULE_VAR_EXPORT +#define MODULE_VAR_EXPORT +#endif +#endif + +typedef struct { + long engine; + long last_modified; + long xbithack; + long terminate_child; + long setup_env; + long current_hook; + zend_bool in_request; + zend_bool apache_config_loaded; + zend_bool headers_sent; +} php_apache_info_struct; + +typedef struct _php_handler { + long type; + long stage; + char *name; +} php_handler; + +#define AP_HANDLER_TYPE_FILE 0 +#define AP_HANDLER_TYPE_METHOD 1 + +extern zend_module_entry apache_module_entry; + +#ifdef ZTS +extern int php_apache_info_id; +#define AP(v) TSRMG(php_apache_info_id, php_apache_info_struct *, v) +#else +extern php_apache_info_struct php_apache_info; +#define AP(v) (php_apache_info.v) +#endif + +/* defines for the various stages of the apache request */ +#define AP_WAITING_FOR_REQUEST 0 +#define AP_POST_READ 1 +#define AP_URI_TRANS 2 +#define AP_HEADER_PARSE 3 +#define AP_ACCESS_CONTROL 4 +#define AP_AUTHENTICATION 5 +#define AP_AUTHORIZATION 6 +#define AP_TYPE_CHECKING 7 +#define AP_FIXUP 8 +#define AP_RESPONSE 9 +#define AP_LOGGING 10 +#define AP_CLEANUP 11 + + +/* fix for gcc4 visibility patch */ +#ifndef PHP_WIN32 +# undef MODULE_VAR_EXPORT +# define MODULE_VAR_EXPORT PHPAPI +#endif + +#endif /* MOD_PHP7_H */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + */ diff --cc sapi/isapi/php7isapi.c index d83c81a,0000000..27eb1c9 mode 100644,000000..100644 --- a/sapi/isapi/php7isapi.c +++ b/sapi/isapi/php7isapi.c @@@ -1,973 -1,0 +1,973 @@@ +/* + +----------------------------------------------------------------------+ - | PHP Version 5 | ++ | PHP Version 7 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 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: Zeev Suraski <zeev@zend.com> | + | Ben Mansell <ben@zeus.com> (Zeus Support) | + +----------------------------------------------------------------------+ + */ +/* $Id$ */ + +#include "php.h" +#include <httpext.h> +#include <httpfilt.h> +#include <httpext.h> +#include "php_main.h" +#include "SAPI.h" +#include "php_globals.h" +#include "ext/standard/info.h" +#include "php_variables.h" +#include "php_ini.h" + +#ifdef PHP_WIN32 +# include <process.h> +#else +# define __try +# define __except(val) +# define __declspec(foo) +#endif + + +#ifdef WITH_ZEUS +# include "httpext.h" +# include <errno.h> +# define GetLastError() errno +#endif + +#ifdef PHP_WIN32 +#define PHP_ENABLE_SEH +#endif + +/* +uncomment the following lines to turn off +exception trapping when running under a debugger + +#ifdef _DEBUG +#undef PHP_ENABLE_SEH +#endif +*/ + +#define MAX_STATUS_LENGTH sizeof("xxxx LONGEST POSSIBLE STATUS DESCRIPTION") +#define ISAPI_SERVER_VAR_BUF_SIZE 1024 +#define ISAPI_POST_DATA_BUF 1024 + +static zend_bool bFilterLoaded=0; +static zend_bool bTerminateThreadsOnError=0; + +static char *isapi_special_server_variable_names[] = { + "ALL_HTTP", + "HTTPS", +#ifndef WITH_ZEUS + "SCRIPT_NAME", +#endif + NULL +}; + +#define NUM_SPECIAL_VARS (sizeof(isapi_special_server_variable_names)/sizeof(char *)) +#define SPECIAL_VAR_ALL_HTTP 0 +#define SPECIAL_VAR_HTTPS 1 +#define SPECIAL_VAR_PHP_SELF 2 + +static char *isapi_server_variable_names[] = { + "AUTH_PASSWORD", + "AUTH_TYPE", + "AUTH_USER", + "CONTENT_LENGTH", + "CONTENT_TYPE", + "PATH_TRANSLATED", + "QUERY_STRING", + "REMOTE_ADDR", + "REMOTE_HOST", + "REMOTE_USER", + "REQUEST_METHOD", + "SERVER_NAME", + "SERVER_PORT", + "SERVER_PROTOCOL", + "SERVER_SOFTWARE", +#ifndef WITH_ZEUS + "APPL_MD_PATH", + "APPL_PHYSICAL_PATH", + "INSTANCE_ID", + "INSTANCE_META_PATH", + "LOGON_USER", + "REQUEST_URI", + "URL", +#else + "DOCUMENT_ROOT", +#endif + NULL +}; + + +static char *isapi_secure_server_variable_names[] = { + "CERT_COOKIE", + "CERT_FLAGS", + "CERT_ISSUER", + "CERT_KEYSIZE", + "CERT_SECRETKEYSIZE", + "CERT_SERIALNUMBER", + "CERT_SERVER_ISSUER", + "CERT_SERVER_SUBJECT", + "CERT_SUBJECT", + "HTTPS_KEYSIZE", + "HTTPS_SECRETKEYSIZE", + "HTTPS_SERVER_ISSUER", + "HTTPS_SERVER_SUBJECT", + "SERVER_PORT_SECURE", +#ifdef WITH_ZEUS + "SSL_CLIENT_CN", + "SSL_CLIENT_EMAIL", + "SSL_CLIENT_OU", + "SSL_CLIENT_O", + "SSL_CLIENT_L", + "SSL_CLIENT_ST", + "SSL_CLIENT_C", + "SSL_CLIENT_I_CN", + "SSL_CLIENT_I_EMAIL", + "SSL_CLIENT_I_OU", + "SSL_CLIENT_I_O", + "SSL_CLIENT_I_L", + "SSL_CLIENT_I_ST", + "SSL_CLIENT_I_C", +#endif + NULL +}; + + +static void php_info_isapi(ZEND_MODULE_INFO_FUNC_ARGS) +{ + char **p; + char variable_buf[ISAPI_SERVER_VAR_BUF_SIZE]; + DWORD variable_len; + char **all_variables[] = { + isapi_server_variable_names, + isapi_special_server_variable_names, + isapi_secure_server_variable_names, + NULL + }; + char ***server_variable_names; + LPEXTENSION_CONTROL_BLOCK lpECB; + + lpECB = (LPEXTENSION_CONTROL_BLOCK) SG(server_context); + + php_info_print_table_start(); + php_info_print_table_header(2, "Server Variable", "Value"); + server_variable_names = all_variables; + while (*server_variable_names) { + p = *server_variable_names; + while (*p) { + variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + if (lpECB->GetServerVariable(lpECB->ConnID, *p, variable_buf, &variable_len) + && variable_buf[0]) { + php_info_print_table_row(2, *p, variable_buf); + } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + char *tmp_variable_buf; + + tmp_variable_buf = (char *) emalloc(variable_len); + if (lpECB->GetServerVariable(lpECB->ConnID, *p, tmp_variable_buf, &variable_len) + && variable_buf[0]) { + php_info_print_table_row(2, *p, tmp_variable_buf); + } + efree(tmp_variable_buf); + } + p++; + } + server_variable_names++; + } + php_info_print_table_end(); +} + + +static zend_module_entry php_isapi_module = { + STANDARD_MODULE_HEADER, + "ISAPI", + NULL, + NULL, + NULL, + NULL, + NULL, + php_info_isapi, + NULL, + STANDARD_MODULE_PROPERTIES +}; + + +static int sapi_isapi_ub_write(const char *str, uint str_length TSRMLS_DC) +{ + DWORD num_bytes = str_length; + LPEXTENSION_CONTROL_BLOCK ecb; + + ecb = (LPEXTENSION_CONTROL_BLOCK) SG(server_context); + if (ecb->WriteClient(ecb->ConnID, (char *) str, &num_bytes, HSE_IO_SYNC) == FALSE) { + php_handle_aborted_connection(); + } + return num_bytes; +} + + +static int sapi_isapi_header_handler(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers TSRMLS_DC) +{ + return SAPI_HEADER_ADD; +} + + + +static void accumulate_header_length(sapi_header_struct *sapi_header, uint *total_length TSRMLS_DC) +{ + *total_length += sapi_header->header_len+2; +} + + +static void concat_header(sapi_header_struct *sapi_header, char **combined_headers_ptr TSRMLS_DC) +{ + memcpy(*combined_headers_ptr, sapi_header->header, sapi_header->header_len); + *combined_headers_ptr += sapi_header->header_len; + **combined_headers_ptr = '\r'; + (*combined_headers_ptr)++; + **combined_headers_ptr = '\n'; + (*combined_headers_ptr)++; +} + + +static int sapi_isapi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) +{ + uint total_length = 2; /* account for the trailing \r\n */ + char *combined_headers, *combined_headers_ptr; + LPEXTENSION_CONTROL_BLOCK lpECB = (LPEXTENSION_CONTROL_BLOCK) SG(server_context); + HSE_SEND_HEADER_EX_INFO header_info; + sapi_header_struct default_content_type; + char *status_buf = NULL; + + /* Obtain headers length */ + if (SG(sapi_headers).send_default_content_type) { + sapi_get_default_content_type_header(&default_content_type TSRMLS_CC); + accumulate_header_length(&default_content_type, (void *) &total_length TSRMLS_CC); + } + zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t) accumulate_header_length, (void *) &total_length TSRMLS_CC); + + /* Generate headers */ + combined_headers = (char *) emalloc(total_length+1); + combined_headers_ptr = combined_headers; + if (SG(sapi_headers).send_default_content_type) { + concat_header(&default_content_type, (void *) &combined_headers_ptr TSRMLS_CC); + sapi_free_header(&default_content_type); /* we no longer need it */ + } + zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t) concat_header, (void *) &combined_headers_ptr TSRMLS_CC); + *combined_headers_ptr++ = '\r'; + *combined_headers_ptr++ = '\n'; + *combined_headers_ptr = 0; + + switch (SG(sapi_headers).http_response_code) { + case 200: + header_info.pszStatus = "200 OK"; + break; + case 302: + header_info.pszStatus = "302 Moved Temporarily"; + break; + case 401: + header_info.pszStatus = "401 Authorization Required"; + break; + default: { + const char *sline = SG(sapi_headers).http_status_line; + int sline_len; + + /* httpd requires that r->status_line is set to the first digit of + * the status-code: */ + if (sline && ((sline_len = strlen(sline)) > 12) && strncmp(sline, "HTTP/1.", 7) == 0 && sline[8] == ' ') { + if ((sline_len - 9) > MAX_STATUS_LENGTH) { + status_buf = estrndup(sline + 9, MAX_STATUS_LENGTH); + } else { + status_buf = estrndup(sline + 9, sline_len - 9); + } + } else { + status_buf = emalloc(MAX_STATUS_LENGTH + 1); + snprintf(status_buf, MAX_STATUS_LENGTH, "%d Undescribed", SG(sapi_headers).http_response_code); + } + header_info.pszStatus = status_buf; + break; + } + } + header_info.cchStatus = strlen(header_info.pszStatus); + header_info.pszHeader = combined_headers; + header_info.cchHeader = total_length; + header_info.fKeepConn = FALSE; + lpECB->dwHttpStatusCode = SG(sapi_headers).http_response_code; + + lpECB->ServerSupportFunction(lpECB->ConnID, HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL); + + efree(combined_headers); + if (status_buf) { + efree(status_buf); + } + return SAPI_HEADER_SENT_SUCCESSFULLY; +} + + +static int php_isapi_startup(sapi_module_struct *sapi_module) +{ + if (php_module_startup(sapi_module, &php_isapi_module, 1)==FAILURE) { + return FAILURE; + } else { + bTerminateThreadsOnError = (zend_bool) INI_INT("isapi.terminate_threads_on_error"); + return SUCCESS; + } +} + + +static int sapi_isapi_read_post(char *buffer, uint count_bytes TSRMLS_DC) +{ + LPEXTENSION_CONTROL_BLOCK lpECB = (LPEXTENSION_CONTROL_BLOCK) SG(server_context); + DWORD read_from_buf=0; + DWORD read_from_input=0; + DWORD total_read=0; + + if ((DWORD) SG(read_post_bytes) < lpECB->cbAvailable) { + read_from_buf = MIN(lpECB->cbAvailable-SG(read_post_bytes), count_bytes); + memcpy(buffer, lpECB->lpbData+SG(read_post_bytes), read_from_buf); + total_read += read_from_buf; + } + if (read_from_buf<count_bytes + && (SG(read_post_bytes)+read_from_buf) < lpECB->cbTotalBytes) { + DWORD cbRead=0, cbSize; + + read_from_input = MIN(count_bytes-read_from_buf, lpECB->cbTotalBytes-SG(read_post_bytes)-read_from_buf); + while (cbRead < read_from_input) { + cbSize = read_from_input - cbRead; + if (!lpECB->ReadClient(lpECB->ConnID, buffer+read_from_buf+cbRead, &cbSize) || cbSize==0) { + break; + } + cbRead += cbSize; + } + total_read += cbRead; + } + return total_read; +} + + +static char *sapi_isapi_read_cookies(TSRMLS_D) +{ + LPEXTENSION_CONTROL_BLOCK lpECB = (LPEXTENSION_CONTROL_BLOCK) SG(server_context); + char variable_buf[ISAPI_SERVER_VAR_BUF_SIZE]; + DWORD variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + + if (lpECB->GetServerVariable(lpECB->ConnID, "HTTP_COOKIE", variable_buf, &variable_len)) { + return estrndup(variable_buf, variable_len); + } else if (GetLastError()==ERROR_INSUFFICIENT_BUFFER) { + char *tmp_variable_buf = (char *) emalloc(variable_len+1); + + if (lpECB->GetServerVariable(lpECB->ConnID, "HTTP_COOKIE", tmp_variable_buf, &variable_len)) { + tmp_variable_buf[variable_len] = 0; + return tmp_variable_buf; + } else { + efree(tmp_variable_buf); + } + } + return STR_EMPTY_ALLOC(); +} + + +#ifdef WITH_ZEUS + +static void sapi_isapi_register_zeus_ssl_variables(LPEXTENSION_CONTROL_BLOCK lpECB, zval *track_vars_array TSRMLS_DC) +{ + char static_variable_buf[ISAPI_SERVER_VAR_BUF_SIZE]; + DWORD variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + char static_cons_buf[ISAPI_SERVER_VAR_BUF_SIZE]; + /* + * We need to construct the /C=.../ST=... + * DN's for SSL_CLIENT_DN and SSL_CLIENT_I_DN + */ + strcpy( static_cons_buf, "/C=" ); + if( lpECB->GetServerVariable( lpECB->ConnID, "SSL_CLIENT_C", static_variable_buf, &variable_len ) && static_variable_buf[0] ) { + strlcat( static_cons_buf, static_variable_buf, ISAPI_SERVER_VAR_BUF_SIZE); + } + strlcat( static_cons_buf, "/ST=", ISAPI_SERVER_VAR_BUF_SIZE); + variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + if( lpECB->GetServerVariable( lpECB->ConnID, "SSL_CLIENT_ST", static_variable_buf, &variable_len ) && static_variable_buf[0] ) { + strlcat( static_cons_buf, static_variable_buf, ISAPI_SERVER_VAR_BUF_SIZE ); + } + php_register_variable( "SSL_CLIENT_DN", static_cons_buf, track_vars_array TSRMLS_CC ); + + strcpy( static_cons_buf, "/C=" ); + variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + if( lpECB->GetServerVariable( lpECB->ConnID, "SSL_CLIENT_I_C", static_variable_buf, &variable_len ) && static_variable_buf[0] ) { + strlcat( static_cons_buf, static_variable_buf, ISAPI_SERVER_VAR_BUF_SIZE ); + } + strlcat( static_cons_buf, "/ST=", ISAPI_SERVER_VAR_BUF_SIZE); + variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + if( lpECB->GetServerVariable( lpECB->ConnID, "SSL_CLIENT_I_ST", static_variable_buf, &variable_len ) && static_variable_buf[0] ) { + strlcat( static_cons_buf, static_variable_buf, ISAPI_SERVER_VAR_BUF_SIZE ); + } + php_register_variable( "SSL_CLIENT_I_DN", static_cons_buf, track_vars_array TSRMLS_CC ); +} + +static void sapi_isapi_register_zeus_variables(LPEXTENSION_CONTROL_BLOCK lpECB, zval *track_vars_array TSRMLS_DC) +{ + char static_variable_buf[ISAPI_SERVER_VAR_BUF_SIZE]; + DWORD variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + DWORD scriptname_len = ISAPI_SERVER_VAR_BUF_SIZE; + DWORD pathinfo_len = 0; + char *strtok_buf = NULL; + + /* Get SCRIPT_NAME, we use this to work out which bit of the URL + * belongs in PHP's version of PATH_INFO + */ + lpECB->GetServerVariable(lpECB->ConnID, "SCRIPT_NAME", static_variable_buf, &scriptname_len); + + /* Adjust Zeus' version of PATH_INFO, set PHP_SELF, + * and generate REQUEST_URI + */ + if ( lpECB->GetServerVariable(lpECB->ConnID, "PATH_INFO", static_variable_buf, &variable_len) && static_variable_buf[0] ) { + + /* PHP_SELF is just PATH_INFO */ + php_register_variable( "PHP_SELF", static_variable_buf, track_vars_array TSRMLS_CC ); + + /* Chop off filename to get just the 'real' PATH_INFO' */ + pathinfo_len = variable_len - scriptname_len; + php_register_variable( "PATH_INFO", static_variable_buf + scriptname_len - 1, track_vars_array TSRMLS_CC ); + /* append query string to give url... extra byte for '?' */ + if ( strlen(lpECB->lpszQueryString) + variable_len + 1 < ISAPI_SERVER_VAR_BUF_SIZE ) { + /* append query string only if it is present... */ + if ( strlen(lpECB->lpszQueryString) ) { + static_variable_buf[ variable_len - 1 ] = '?'; + strcpy( static_variable_buf + variable_len, lpECB->lpszQueryString ); + } + php_register_variable( "URL", static_variable_buf, track_vars_array TSRMLS_CC ); + php_register_variable( "REQUEST_URI", static_variable_buf, track_vars_array TSRMLS_CC ); + } + } + + /* Get and adjust PATH_TRANSLATED to what PHP wants */ + variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + if ( lpECB->GetServerVariable(lpECB->ConnID, "PATH_TRANSLATED", static_variable_buf, &variable_len) && static_variable_buf[0] ) { + static_variable_buf[ variable_len - pathinfo_len - 1 ] = '\0'; + php_register_variable( "PATH_TRANSLATED", static_variable_buf, track_vars_array TSRMLS_CC ); + } + + /* Bring in the AUTHENTICATION stuff as needed */ + variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + if ( lpECB->GetServerVariable(lpECB->ConnID, "AUTH_USER", static_variable_buf, &variable_len) && static_variable_buf[0] ) { + php_register_variable( "PHP_AUTH_USER", static_variable_buf, track_vars_array TSRMLS_CC ); + } + variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + if ( lpECB->GetServerVariable(lpECB->ConnID, "AUTH_PASSWORD", static_variable_buf, &variable_len) && static_variable_buf[0] ) { + php_register_variable( "PHP_AUTH_PW", static_variable_buf, track_vars_array TSRMLS_CC ); + } + variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + if ( lpECB->GetServerVariable(lpECB->ConnID, "AUTH_TYPE", static_variable_buf, &variable_len) && static_variable_buf[0] ) { + php_register_variable( "AUTH_TYPE", static_variable_buf, track_vars_array TSRMLS_CC ); + } + + /* And now, for the SSL variables (if applicable) */ + variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + if ( lpECB->GetServerVariable(lpECB->ConnID, "CERT_COOKIE", static_variable_buf, &variable_len) && static_variable_buf[0] ) { + sapi_isapi_register_zeus_ssl_variables( lpECB, track_vars_array TSRMLS_CC ); + } + /* Copy some of the variables we need to meet Apache specs */ + variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + if ( lpECB->GetServerVariable(lpECB->ConnID, "SERVER_SOFTWARE", static_variable_buf, &variable_len) && static_variable_buf[0] ) { + php_register_variable( "SERVER_SIGNATURE", static_variable_buf, track_vars_array TSRMLS_CC ); + } +} +#else + +static void sapi_isapi_register_iis_variables(LPEXTENSION_CONTROL_BLOCK lpECB, zval *track_vars_array TSRMLS_DC) +{ + char static_variable_buf[ISAPI_SERVER_VAR_BUF_SIZE]; + char path_info_buf[ISAPI_SERVER_VAR_BUF_SIZE]; + DWORD variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + DWORD scriptname_len = ISAPI_SERVER_VAR_BUF_SIZE; + DWORD pathinfo_len = 0; + HSE_URL_MAPEX_INFO humi; + + /* Get SCRIPT_NAME, we use this to work out which bit of the URL + * belongs in PHP's version of PATH_INFO. SCRIPT_NAME also becomes PHP_SELF. + */ + lpECB->GetServerVariable(lpECB->ConnID, "SCRIPT_NAME", static_variable_buf, &scriptname_len); + php_register_variable("SCRIPT_FILENAME", SG(request_info).path_translated, track_vars_array TSRMLS_CC); + + /* Adjust IIS' version of PATH_INFO, set PHP_SELF, + * and generate REQUEST_URI + * Get and adjust PATH_TRANSLATED to what PHP wants + */ + if ( lpECB->GetServerVariable(lpECB->ConnID, "PATH_INFO", static_variable_buf, &variable_len) && static_variable_buf[0] ) { + + /* Chop off filename to get just the 'real' PATH_INFO' */ + php_register_variable( "ORIG_PATH_INFO", static_variable_buf, track_vars_array TSRMLS_CC ); + pathinfo_len = variable_len - scriptname_len; + strncpy(path_info_buf, static_variable_buf + scriptname_len - 1, sizeof(path_info_buf)-1); + php_register_variable( "PATH_INFO", path_info_buf, track_vars_array TSRMLS_CC ); + /* append query string to give url... extra byte for '?' */ + if ( strlen(lpECB->lpszQueryString) + variable_len + 1 < ISAPI_SERVER_VAR_BUF_SIZE ) { + /* append query string only if it is present... */ + if ( strlen(lpECB->lpszQueryString) ) { + static_variable_buf[ variable_len - 1 ] = '?'; + strcpy( static_variable_buf + variable_len, lpECB->lpszQueryString ); + } + php_register_variable( "URL", static_variable_buf, track_vars_array TSRMLS_CC ); + php_register_variable( "REQUEST_URI", static_variable_buf, track_vars_array TSRMLS_CC ); + } + variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + if ( lpECB->GetServerVariable(lpECB->ConnID, "PATH_TRANSLATED", static_variable_buf, &variable_len) && static_variable_buf[0] ) { + php_register_variable( "ORIG_PATH_TRANSLATED", static_variable_buf, track_vars_array TSRMLS_CC ); + } + if (lpECB->ServerSupportFunction(lpECB->ConnID, HSE_REQ_MAP_URL_TO_PATH_EX, path_info_buf, &pathinfo_len, (LPDWORD) &humi)) { + /* Remove trailing \ */ + if (humi.lpszPath[variable_len-2] == '\\') { + humi.lpszPath[variable_len-2] = 0; + } + php_register_variable("PATH_TRANSLATED", humi.lpszPath, track_vars_array TSRMLS_CC); + } + } + + static_variable_buf[0] = '/'; + static_variable_buf[1] = 0; + variable_len = 2; + if (lpECB->ServerSupportFunction(lpECB->ConnID, HSE_REQ_MAP_URL_TO_PATH_EX, static_variable_buf, &variable_len, (LPDWORD) &humi)) { + /* Remove trailing \ */ + if (humi.lpszPath[variable_len-2] == '\\') { + humi.lpszPath[variable_len-2] = 0; + } + php_register_variable("DOCUMENT_ROOT", humi.lpszPath, track_vars_array TSRMLS_CC); + } + + if (!SG(request_info).auth_user || !SG(request_info).auth_password || + !SG(request_info).auth_user[0] || !SG(request_info).auth_password[0]) { + variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + if (lpECB->GetServerVariable(lpECB->ConnID, "HTTP_AUTHORIZATION", static_variable_buf, &variable_len) + && static_variable_buf[0]) { + php_handle_auth_data(static_variable_buf TSRMLS_CC); + } + } + + if (SG(request_info).auth_user) { + php_register_variable("PHP_AUTH_USER", SG(request_info).auth_user, track_vars_array TSRMLS_CC ); + } + if (SG(request_info).auth_password) { + php_register_variable("PHP_AUTH_PW", SG(request_info).auth_password, track_vars_array TSRMLS_CC ); + } +} +#endif + +static void sapi_isapi_register_server_variables2(char **server_variables, LPEXTENSION_CONTROL_BLOCK lpECB, zval *track_vars_array, char **recorded_values TSRMLS_DC) +{ + char **p=server_variables; + DWORD variable_len; + char static_variable_buf[ISAPI_SERVER_VAR_BUF_SIZE]; + char *variable_buf; + + while (*p) { + variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + if (lpECB->GetServerVariable(lpECB->ConnID, *p, static_variable_buf, &variable_len) + && static_variable_buf[0]) { + php_register_variable(*p, static_variable_buf, track_vars_array TSRMLS_CC); + if (recorded_values) { + recorded_values[p-server_variables] = estrndup(static_variable_buf, variable_len); + } + } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + variable_buf = (char *) emalloc(variable_len+1); + if (lpECB->GetServerVariable(lpECB->ConnID, *p, variable_buf, &variable_len) + && variable_buf[0]) { + php_register_variable(*p, variable_buf, track_vars_array TSRMLS_CC); + } + if (recorded_values) { + recorded_values[p-server_variables] = variable_buf; + } else { + efree(variable_buf); + } + } else { /* for compatibility with Apache SAPIs */ + php_register_variable(*p, "", track_vars_array TSRMLS_CC); + } + p++; + } +} + + +static void sapi_isapi_register_server_variables(zval *track_vars_array TSRMLS_DC) +{ + DWORD variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + char *variable; + char *strtok_buf = NULL; + char *isapi_special_server_variables[NUM_SPECIAL_VARS]; + LPEXTENSION_CONTROL_BLOCK lpECB; + + lpECB = (LPEXTENSION_CONTROL_BLOCK) SG(server_context); + + /* Register the special ISAPI variables */ + memset(isapi_special_server_variables, 0, sizeof(isapi_special_server_variables)); + sapi_isapi_register_server_variables2(isapi_special_server_variable_names, lpECB, track_vars_array, isapi_special_server_variables TSRMLS_CC); + if (SG(request_info).cookie_data) { + php_register_variable("HTTP_COOKIE", SG(request_info).cookie_data, track_vars_array TSRMLS_CC); + } + + /* Register the standard ISAPI variables */ + sapi_isapi_register_server_variables2(isapi_server_variable_names, lpECB, track_vars_array, NULL TSRMLS_CC); + + if (isapi_special_server_variables[SPECIAL_VAR_HTTPS] + && (atoi(isapi_special_server_variables[SPECIAL_VAR_HTTPS]) + || !strcasecmp(isapi_special_server_variables[SPECIAL_VAR_HTTPS], "on")) + ) { + /* Register SSL ISAPI variables */ + sapi_isapi_register_server_variables2(isapi_secure_server_variable_names, lpECB, track_vars_array, NULL TSRMLS_CC); + } + + if (isapi_special_server_variables[SPECIAL_VAR_HTTPS]) { + efree(isapi_special_server_variables[SPECIAL_VAR_HTTPS]); + } + + +#ifdef WITH_ZEUS + sapi_isapi_register_zeus_variables(lpECB, track_vars_array TSRMLS_CC); +#else + sapi_isapi_register_iis_variables(lpECB, track_vars_array TSRMLS_CC); +#endif + + /* PHP_SELF support */ + if (isapi_special_server_variables[SPECIAL_VAR_PHP_SELF]) { + php_register_variable("PHP_SELF", isapi_special_server_variables[SPECIAL_VAR_PHP_SELF], track_vars_array TSRMLS_CC); + efree(isapi_special_server_variables[SPECIAL_VAR_PHP_SELF]); + } + + if (isapi_special_server_variables[SPECIAL_VAR_ALL_HTTP]) { + /* Register the internal bits of ALL_HTTP */ + variable = php_strtok_r(isapi_special_server_variables[SPECIAL_VAR_ALL_HTTP], "\r\n", &strtok_buf); + while (variable) { + char *colon = strchr(variable, ':'); + + if (colon) { + char *value = colon+1; + + while (*value==' ') { + value++; + } + *colon = 0; + php_register_variable(variable, value, track_vars_array TSRMLS_CC); + *colon = ':'; + } + variable = php_strtok_r(NULL, "\r\n", &strtok_buf); + } + efree(isapi_special_server_variables[SPECIAL_VAR_ALL_HTTP]); + } +} + + +static sapi_module_struct isapi_sapi_module = { + "isapi", /* name */ + "ISAPI", /* pretty name */ + + php_isapi_startup, /* startup */ + php_module_shutdown_wrapper, /* shutdown */ + + NULL, /* activate */ + NULL, /* deactivate */ + + sapi_isapi_ub_write, /* unbuffered write */ + NULL, /* flush */ + NULL, /* get uid */ + NULL, /* getenv */ + + php_error, /* error handler */ + + sapi_isapi_header_handler, /* header handler */ + sapi_isapi_send_headers, /* send headers handler */ + NULL, /* send header handler */ + + sapi_isapi_read_post, /* read POST data */ + sapi_isapi_read_cookies, /* read Cookies */ + + sapi_isapi_register_server_variables, /* register server variables */ + NULL, /* Log message */ + NULL, /* Get request time */ + NULL, /* Child terminate */ + + STANDARD_SAPI_MODULE_PROPERTIES +}; + + +BOOL WINAPI GetFilterVersion(PHTTP_FILTER_VERSION pFilterVersion) +{ + bFilterLoaded = 1; + pFilterVersion->dwFilterVersion = HTTP_FILTER_REVISION; + strcpy(pFilterVersion->lpszFilterDesc, isapi_sapi_module.pretty_name); + pFilterVersion->dwFlags= (SF_NOTIFY_AUTHENTICATION | SF_NOTIFY_PREPROC_HEADERS); + return TRUE; +} + + +DWORD WINAPI HttpFilterProc(PHTTP_FILTER_CONTEXT pfc, DWORD notificationType, LPVOID pvNotification) +{ + TSRMLS_FETCH(); + + switch (notificationType) { + case SF_NOTIFY_PREPROC_HEADERS: + SG(request_info).auth_user = NULL; + SG(request_info).auth_password = NULL; + SG(request_info).auth_digest = NULL; + break; + case SF_NOTIFY_AUTHENTICATION: { + char *auth_user = ((HTTP_FILTER_AUTHENT *) pvNotification)->pszUser; + char *auth_password = ((HTTP_FILTER_AUTHENT *) pvNotification)->pszPassword; + + if (auth_user && auth_user[0]) { + SG(request_info).auth_user = estrdup(auth_user); + } + if (auth_password && auth_password[0]) { + SG(request_info).auth_password = estrdup(auth_password); + } + return SF_STATUS_REQ_HANDLED_NOTIFICATION; + } + break; + } + return SF_STATUS_REQ_NEXT_NOTIFICATION; +} + + +static void init_request_info(LPEXTENSION_CONTROL_BLOCK lpECB TSRMLS_DC) +{ + DWORD variable_len = ISAPI_SERVER_VAR_BUF_SIZE; + char static_variable_buf[ISAPI_SERVER_VAR_BUF_SIZE]; +#ifndef WITH_ZEUS + HSE_URL_MAPEX_INFO humi; +#endif + + SG(request_info).request_method = lpECB->lpszMethod; + SG(request_info).query_string = lpECB->lpszQueryString; + SG(request_info).request_uri = lpECB->lpszPathInfo; + SG(request_info).content_type = lpECB->lpszContentType; + SG(request_info).content_length = lpECB->cbTotalBytes; + SG(sapi_headers).http_response_code = 200; /* I think dwHttpStatusCode is invalid at this stage -RL */ + if (!bFilterLoaded) { /* we don't have valid ISAPI Filter information */ + SG(request_info).auth_user = SG(request_info).auth_password = SG(request_info).auth_digest = NULL; + } + +#ifdef WITH_ZEUS + /* PATH_TRANSLATED can contain extra PATH_INFO stuff after the + * file being loaded, so we must use SCRIPT_FILENAME instead + */ + if(lpECB->GetServerVariable(lpECB->ConnID, "SCRIPT_FILENAME", static_variable_buf, &variable_len)) { + SG(request_info).path_translated = estrdup(static_variable_buf); + } else +#else + /* happily, IIS gives us SCRIPT_NAME which is correct (without PATH_INFO stuff) + so we can just map that to the physical path and we have our filename */ + + lpECB->GetServerVariable(lpECB->ConnID, "SCRIPT_NAME", static_variable_buf, &variable_len); + if (lpECB->ServerSupportFunction(lpECB->ConnID, HSE_REQ_MAP_URL_TO_PATH_EX, static_variable_buf, &variable_len, (LPDWORD) &humi)) { + SG(request_info).path_translated = estrdup(humi.lpszPath); + } else +#endif + /* if mapping fails, default to what the server tells us */ + SG(request_info).path_translated = estrdup(lpECB->lpszPathTranslated); + + /* some server configurations allow '..' to slip through in the + translated path. We'll just refuse to handle such a path. */ + if (strstr(SG(request_info).path_translated,"..")) { + SG(sapi_headers).http_response_code = 404; + efree(SG(request_info).path_translated); + SG(request_info).path_translated = NULL; + } +} + + +static void php_isapi_report_exception(char *message, int message_len TSRMLS_DC) +{ + if (!SG(headers_sent)) { + HSE_SEND_HEADER_EX_INFO header_info; + LPEXTENSION_CONTROL_BLOCK lpECB = (LPEXTENSION_CONTROL_BLOCK) SG(server_context); + + header_info.pszStatus = "500 Internal Server Error"; + header_info.cchStatus = strlen(header_info.pszStatus); + header_info.pszHeader = "Content-Type: text/html\r\n\r\n"; + header_info.cchHeader = strlen(header_info.pszHeader); + + lpECB->dwHttpStatusCode = 500; + lpECB->ServerSupportFunction(lpECB->ConnID, HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL); + SG(headers_sent)=1; + } + sapi_isapi_ub_write(message, message_len TSRMLS_CC); +} + + +BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) +{ + pVer->dwExtensionVersion = HSE_VERSION; +#ifdef WITH_ZEUS + strncpy( pVer->lpszExtensionDesc, isapi_sapi_module.name, HSE_MAX_EXT_DLL_NAME_LEN); +#else + lstrcpyn(pVer->lpszExtensionDesc, isapi_sapi_module.name, HSE_MAX_EXT_DLL_NAME_LEN); +#endif + return TRUE; +} + + +static void my_endthread() +{ +#ifdef PHP_WIN32 + if (bTerminateThreadsOnError) { + _endthread(); + } +#endif +} + +#ifdef PHP_WIN32 +/* ep is accessible only in the context of the __except expression, + * so we have to call this function to obtain it. + */ +BOOL exceptionhandler(LPEXCEPTION_POINTERS *e, LPEXCEPTION_POINTERS ep) +{ + *e=ep; + return TRUE; +} +#endif + +DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) +{ + zend_file_handle file_handle; + zend_bool stack_overflown=0; + int retval = FAILURE; +#ifdef PHP_ENABLE_SEH + LPEXCEPTION_POINTERS e; +#endif + TSRMLS_FETCH(); + + zend_first_try { +#ifdef PHP_ENABLE_SEH + __try { +#endif + init_request_info(lpECB TSRMLS_CC); + SG(server_context) = lpECB; + + php_request_startup(TSRMLS_C); + + file_handle.filename = SG(request_info).path_translated; + file_handle.free_filename = 0; + file_handle.type = ZEND_HANDLE_FILENAME; + file_handle.opened_path = NULL; + + /* open the script here so we can 404 if it fails */ + if (file_handle.filename) + retval = php_fopen_primary_script(&file_handle TSRMLS_CC); + + if (!file_handle.filename || retval == FAILURE) { + SG(sapi_headers).http_response_code = 404; + PUTS("No input file specified.\n"); + } else { + php_execute_script(&file_handle TSRMLS_CC); + } + + if (SG(request_info).cookie_data) { + efree(SG(request_info).cookie_data); + } + if (SG(request_info).path_translated) + efree(SG(request_info).path_translated); +#ifdef PHP_ENABLE_SEH + } __except(exceptionhandler(&e, GetExceptionInformation())) { + char buf[1024]; + if (_exception_code()==EXCEPTION_STACK_OVERFLOW) { + LPBYTE lpPage; + static SYSTEM_INFO si; + static MEMORY_BASIC_INFORMATION mi; + static DWORD dwOldProtect; + + GetSystemInfo(&si); + + /* Get page ESP is pointing to */ + _asm mov lpPage, esp; + + /* Get stack allocation base */ + VirtualQuery(lpPage, &mi, sizeof(mi)); + + /* Go to the page below the current page */ + lpPage = (LPBYTE) (mi.BaseAddress) - si.dwPageSize; + + /* Free pages below current page */ + if (!VirtualFree(mi.AllocationBase, (LPBYTE)lpPage - (LPBYTE) mi.AllocationBase, MEM_DECOMMIT)) { + _endthread(); + } + + /* Restore the guard page */ + if (!VirtualProtect(lpPage, si.dwPageSize, PAGE_GUARD | PAGE_READWRITE, &dwOldProtect)) { + _endthread(); + } + + CG(unclean_shutdown)=1; + _snprintf(buf, sizeof(buf)-1,"PHP has encountered a Stack overflow"); + php_isapi_report_exception(buf, strlen(buf) TSRMLS_CC); + } else if (_exception_code()==EXCEPTION_ACCESS_VIOLATION) { + _snprintf(buf, sizeof(buf)-1,"PHP has encountered an Access Violation at %p", e->ExceptionRecord->ExceptionAddress); + php_isapi_report_exception(buf, strlen(buf) TSRMLS_CC); + my_endthread(); + } else { + _snprintf(buf, sizeof(buf)-1,"PHP has encountered an Unhandled Exception Code %d at %p", e->ExceptionRecord->ExceptionCode , e->ExceptionRecord->ExceptionAddress); + php_isapi_report_exception(buf, strlen(buf) TSRMLS_CC); + my_endthread(); + } + } +#endif +#ifdef PHP_ENABLE_SEH + __try { + php_request_shutdown(NULL); + } __except(EXCEPTION_EXECUTE_HANDLER) { + my_endthread(); + } +#else + php_request_shutdown(NULL); +#endif + } zend_catch { + zend_try { + php_request_shutdown(NULL); + } zend_end_try(); + return HSE_STATUS_ERROR; + } zend_end_try(); + + return HSE_STATUS_SUCCESS; +} + + + +__declspec(dllexport) BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) +{ + switch (fdwReason) { + case DLL_PROCESS_ATTACH: +#ifdef WITH_ZEUS + tsrm_startup(128, 1, TSRM_ERROR_LEVEL_CORE, "TSRM.log"); +#else + tsrm_startup(128, 1, TSRM_ERROR_LEVEL_CORE, "C:\\TSRM.log"); +#endif + sapi_startup(&isapi_sapi_module); + if (isapi_sapi_module.startup) { + isapi_sapi_module.startup(&sapi_module); + } + break; + case DLL_THREAD_ATTACH: + break; + case DLL_THREAD_DETACH: + ts_free_thread(); + break; + case DLL_PROCESS_DETACH: + if (isapi_sapi_module.shutdown) { + isapi_sapi_module.shutdown(&sapi_module); + } + sapi_shutdown(); + tsrm_shutdown(); + break; + } + return TRUE; +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + */
« previous php.cvs (#81810) next »