laruence                                 Wed, 21 Sep 2011 03:09:42 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=317074

Log:
Fixed bug #55747 (request headers missed in $_SERVER)

Bug: https://bugs.php.net/55747 (Open) request headers missed in $_SERVER
      
Changed paths:
    U   php/php-src/branches/PHP_5_4/NEWS
    U   php/php-src/branches/PHP_5_4/sapi/cli/php_cli_server.c
    A   php/php-src/branches/PHP_5_4/sapi/cli/tests/php_cli_server_004.phpt
    U   php/php-src/trunk/sapi/cli/php_cli_server.c
    U   php/php-src/trunk/sapi/cli/tests/php_cli_server.inc
    A   php/php-src/trunk/sapi/cli/tests/php_cli_server_004.phpt

Modified: php/php-src/branches/PHP_5_4/NEWS
===================================================================
--- php/php-src/branches/PHP_5_4/NEWS	2011-09-20 23:17:11 UTC (rev 317073)
+++ php/php-src/branches/PHP_5_4/NEWS	2011-09-21 03:09:42 UTC (rev 317074)
@@ -14,6 +14,7 @@
 - CLI SAPI:
   . Fixed bug #55726 (Changing the working directory makes router script
     inaccessible). (Laruence)
+  . Fixed bug #55747 (request headers missed in $_SERVER). (Laruence)

 15 Sep 2011, PHP 5.4.0 Beta
 - General improvements:

Modified: php/php-src/branches/PHP_5_4/sapi/cli/php_cli_server.c
===================================================================
--- php/php-src/branches/PHP_5_4/sapi/cli/php_cli_server.c	2011-09-20 23:17:11 UTC (rev 317073)
+++ php/php-src/branches/PHP_5_4/sapi/cli/php_cli_server.c	2011-09-21 03:09:42 UTC (rev 317074)
@@ -524,32 +524,34 @@
 	}
 } /* }}} */

+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; i<hash_key->nKeyLength; 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, ':'))) {
 			char addr[64], port[8];
@@ -581,6 +583,7 @@
 	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) /* {{{ */

Added: php/php-src/branches/PHP_5_4/sapi/cli/tests/php_cli_server_004.phpt
===================================================================
--- php/php-src/branches/PHP_5_4/sapi/cli/tests/php_cli_server_004.phpt	                        (rev 0)
+++ php/php-src/branches/PHP_5_4/sapi/cli/tests/php_cli_server_004.phpt	2011-09-21 03:09:42 UTC (rev 317074)
@@ -0,0 +1,49 @@
+--TEST--
+Bug #55747 (request headers missed in $_SERVER)
+--INI--
+allow_url_fopen=1
+--SKIPIF--
+<?php
+include "skipif.inc";
+if (substr(PHP_OS, 0, 3) == 'WIN') {
+    die ("skip not for Windows");
+}
+?>
+--FILE--
+<?php
+include "php_cli_server.inc";
+php_cli_server_start('foreach($_SERVER as $k=>$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, <<<HEADER
+GET / HTTP/1.1
+Host:{$host}
+User-Agent:dummy
+Custom:foo
+
+
+HEADER
+)) {
+	while (!feof($fp)) {
+		echo fgets($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"

Modified: php/php-src/trunk/sapi/cli/php_cli_server.c
===================================================================
--- php/php-src/trunk/sapi/cli/php_cli_server.c	2011-09-20 23:17:11 UTC (rev 317073)
+++ php/php-src/trunk/sapi/cli/php_cli_server.c	2011-09-21 03:09:42 UTC (rev 317074)
@@ -524,32 +524,34 @@
 	}
 } /* }}} */

+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; i<hash_key->nKeyLength; 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, ':'))) {
 			char addr[64], port[8];
@@ -581,6 +583,7 @@
 	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) /* {{{ */

Modified: php/php-src/trunk/sapi/cli/tests/php_cli_server.inc
===================================================================
--- php/php-src/trunk/sapi/cli/tests/php_cli_server.inc	2011-09-20 23:17:11 UTC (rev 317073)
+++ php/php-src/trunk/sapi/cli/tests/php_cli_server.inc	2011-09-21 03:09:42 UTC (rev 317074)
@@ -24,7 +24,6 @@
         },
         $handle
     );
-
-    usleep(50000);
+	usleep(50000);
 }
 ?>

Added: php/php-src/trunk/sapi/cli/tests/php_cli_server_004.phpt
===================================================================
--- php/php-src/trunk/sapi/cli/tests/php_cli_server_004.phpt	                        (rev 0)
+++ php/php-src/trunk/sapi/cli/tests/php_cli_server_004.phpt	2011-09-21 03:09:42 UTC (rev 317074)
@@ -0,0 +1,49 @@
+--TEST--
+Bug #55747 (request headers missed in $_SERVER)
+--INI--
+allow_url_fopen=1
+--SKIPIF--
+<?php
+include "skipif.inc";
+if (substr(PHP_OS, 0, 3) == 'WIN') {
+    die ("skip not for Windows");
+}
+?>
+--FILE--
+<?php
+include "php_cli_server.inc";
+php_cli_server_start('foreach($_SERVER as $k=>$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, <<<HEADER
+GET / HTTP/1.1
+Host:{$host}
+User-Agent:dummy
+Custom:foo
+
+
+HEADER
+)) {
+	while (!feof($fp)) {
+		echo fgets($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"
-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to