From 86a48c6ca9c91ad345f3420bcdf0f9d5f4317d25 Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Wed, 21 Sep 2011 03:09:42 +0000 Subject: [PATCH] Fixed bug #55747 (request headers missed in $_SERVER) --- sapi/cli/php_cli_server.c | 45 ++++++++++++----------- sapi/cli/tests/php_cli_server.inc | 3 +- sapi/cli/tests/php_cli_server_004.phpt | 49 ++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 23 deletions(-) create mode 100644 sapi/cli/tests/php_cli_server_004.phpt diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c index ac669cac4f..d6889128dd 100644 --- a/sapi/cli/php_cli_server.c +++ b/sapi/cli/php_cli_server.c @@ -524,31 +524,33 @@ static void sapi_cli_server_register_variable(zval *track_vars_array, const char } } /* }}} */ +static int sapi_cli_server_register_client_headers(char **entry TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */ { + zval *track_vars_array = va_arg(args, zval *); + if (hash_key->nKeyLength) { + char *real_key, *key; + uint i; + key = estrndup(hash_key->arKey, hash_key->nKeyLength); + for(i=0; inKeyLength; i++) { + if (key[i] == '-') { + key[i] = '_'; + } else { + key[i] = toupper(key[i]); + } + } + spprintf(&real_key, 0, "%s_%s", "HTTP", key); + sapi_cli_server_register_variable(track_vars_array, real_key, *entry TSRMLS_CC); + efree(key); + efree(real_key); + } + + return ZEND_HASH_APPLY_KEEP; +} +/* }}} */ + static void sapi_cli_server_register_variables(zval *track_vars_array TSRMLS_DC) /* {{{ */ { php_cli_server_client *client = SG(server_context); sapi_cli_server_register_variable(track_vars_array, "DOCUMENT_ROOT", client->server->document_root TSRMLS_CC); - { - smart_str buf = { 0 }; - smart_str_appends(&buf, client->server->host); - smart_str_appendc(&buf, ':'); - smart_str_append_generic_ex(&buf, client->server->port, 0, int, _unsigned); - smart_str_0(&buf); - sapi_cli_server_register_variable(track_vars_array, "HTTP_HOST", buf.c TSRMLS_CC); - smart_str_free(&buf); - } - { - char **val; - if (SUCCESS == zend_hash_find(&client->request.headers, "Cookie", sizeof("Cookie"), (void**)&val)) { - sapi_cli_server_register_variable(track_vars_array, "HTTP_COOKIE", *val TSRMLS_CC); - } - } - { - char **val; - if (SUCCESS == zend_hash_find(&client->request.headers, "Referer", sizeof("Referer"), (void**)&val)) { - sapi_cli_server_register_variable(track_vars_array, "HTTP_REFERER", *val TSRMLS_CC); - } - } { char *tmp; if ((tmp = strrchr(client->addr_str, ':'))) { @@ -581,6 +583,7 @@ static void sapi_cli_server_register_variables(zval *track_vars_array TSRMLS_DC) if (client->request.query_string) { sapi_cli_server_register_variable(track_vars_array, "QUERY_STRING", client->request.query_string TSRMLS_CC); } + zend_hash_apply_with_arguments(&client->request.headers TSRMLS_CC, (apply_func_args_t)sapi_cli_server_register_client_headers, 1, track_vars_array); } /* }}} */ static void sapi_cli_server_log_message(char *msg TSRMLS_DC) /* {{{ */ diff --git a/sapi/cli/tests/php_cli_server.inc b/sapi/cli/tests/php_cli_server.inc index 3a4e8ebb17..ed861e26ab 100644 --- a/sapi/cli/tests/php_cli_server.inc +++ b/sapi/cli/tests/php_cli_server.inc @@ -24,7 +24,6 @@ function php_cli_server_start($code = 'echo "Hello world";') { }, $handle ); - - usleep(50000); + usleep(50000); } ?> diff --git a/sapi/cli/tests/php_cli_server_004.phpt b/sapi/cli/tests/php_cli_server_004.phpt new file mode 100644 index 0000000000..513840d9e0 --- /dev/null +++ b/sapi/cli/tests/php_cli_server_004.phpt @@ -0,0 +1,49 @@ +--TEST-- +Bug #55747 (request headers missed in $_SERVER) +--INI-- +allow_url_fopen=1 +--SKIPIF-- + +--FILE-- +$v) { if (!strncmp($k, "HTTP", 4)) var_dump( $k . ":" . $v); }'); + +list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS); +$port = intval($port)?:80; + +$fp = fsockopen($host, $port, $errno, $errstr, 0.5); +if (!$fp) { + die("connect failed"); +} + +if(fwrite($fp, <<
+--EXPECTF-- +HTTP/1.1 200 OK +Host: %s +Connection: closed +X-Powered-By: PHP/5.5.0-dev +Content-type: text/html + +string(19) "HTTP_HOST:localhost" +string(21) "HTTP_USER_AGENT:dummy" +string(15) "HTTP_CUSTOM:foo" -- 2.50.1