987f92
Adapted for 7.2.7 from 7.3 by remi
987f92
987f92
987f92
From 0ea4013f101d64fbeb9221260b36e98f10ed1ddd Mon Sep 17 00:00:00 2001
987f92
From: Remi Collet <remi@remirepo.net>
987f92
Date: Wed, 4 Jul 2018 08:48:38 +0200
987f92
Subject: [PATCH] Fixed bug #62596 add getallheaders (apache_request_headers)
987f92
 missing function in FPM add sapi_add_request_header in public API (was
987f92
 add_request_header) fix arginfo for fastcgi_finish_request fucntion
987f92
987f92
---
987f92
 main/SAPI.c                       | 50 +++++++++++++++++++++++++++++
987f92
 main/SAPI.h                       |  1 +
987f92
 sapi/cgi/cgi_main.c               | 51 +----------------------------
987f92
 sapi/fpm/fpm/fpm_main.c           | 25 ++++++++++++++-
987f92
 sapi/fpm/tests/getallheaders.phpt | 67 +++++++++++++++++++++++++++++++++++++++
987f92
 5 files changed, 143 insertions(+), 51 deletions(-)
987f92
 create mode 100644 sapi/fpm/tests/getallheaders.phpt
987f92
987f92
diff --git a/main/SAPI.c b/main/SAPI.c
987f92
index b6c3329..7e0c7c8 100644
987f92
--- a/main/SAPI.c
987f92
+++ b/main/SAPI.c
987f92
@@ -1104,6 +1104,56 @@ SAPI_API void sapi_terminate_process(void) {
987f92
 	}
987f92
 }
987f92
 
987f92
+SAPI_API void sapi_add_request_header(char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg) /* {{{ */
987f92
+{
987f92
+	zval *return_value = (zval*)arg;
987f92
+	char *str = NULL;
987f92
+
987f92
+	ALLOCA_FLAG(use_heap)
987f92
+
987f92
+	if (var_len > 5 &&
987f92
+	    var[0] == 'H' &&
987f92
+	    var[1] == 'T' &&
987f92
+	    var[2] == 'T' &&
987f92
+	    var[3] == 'P' &&
987f92
+	    var[4] == '_') {
987f92
+
987f92
+		char *p;
987f92
+
987f92
+		var_len -= 5;
987f92
+		p = var + 5;
987f92
+		var = str = do_alloca(var_len + 1, use_heap);
987f92
+		*str++ = *p++;
987f92
+		while (*p) {
987f92
+			if (*p == '_') {
987f92
+				*str++ = '-';
987f92
+				p++;
987f92
+				if (*p) {
987f92
+					*str++ = *p++;
987f92
+				}
987f92
+			} else if (*p >= 'A' && *p <= 'Z') {
987f92
+				*str++ = (*p++ - 'A' + 'a');
987f92
+			} else {
987f92
+				*str++ = *p++;
987f92
+			}
987f92
+		}
987f92
+		*str = 0;
987f92
+	} else if (var_len == sizeof("CONTENT_TYPE")-1 &&
987f92
+	           memcmp(var, "CONTENT_TYPE", sizeof("CONTENT_TYPE")-1) == 0) {
987f92
+		var = "Content-Type";
987f92
+	} else if (var_len == sizeof("CONTENT_LENGTH")-1 &&
987f92
+	           memcmp(var, "CONTENT_LENGTH", sizeof("CONTENT_LENGTH")-1) == 0) {
987f92
+		var = "Content-Length";
987f92
+	} else {
987f92
+		return;
987f92
+	}
987f92
+	add_assoc_stringl_ex(return_value, var, var_len, val, val_len);
987f92
+	if (str) {
987f92
+		free_alloca(var, use_heap);
987f92
+	}
987f92
+}
987f92
+/* }}} */
987f92
+
987f92
 /*
987f92
  * Local variables:
987f92
  * tab-width: 4
987f92
diff --git a/main/SAPI.h b/main/SAPI.h
987f92
index f829fd7..4b8e223 100644
987f92
--- a/main/SAPI.h
987f92
+++ b/main/SAPI.h
987f92
@@ -151,6 +151,7 @@ SAPI_API void sapi_shutdown(void);
987f92
 SAPI_API void sapi_activate(void);
987f92
 SAPI_API void sapi_deactivate(void);
987f92
 SAPI_API void sapi_initialize_empty_request(void);
987f92
+SAPI_API void sapi_add_request_header(char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg);
987f92
 END_EXTERN_C()
987f92
 
987f92
 /*
987f92
diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c
987f92
index 2e9cefe..350846d 100644
987f92
--- a/sapi/cgi/cgi_main.c
987f92
+++ b/sapi/cgi/cgi_main.c
987f92
@@ -1591,54 +1591,6 @@ PHP_FUNCTION(apache_child_terminate) /*
987f92
 }
987f92
 /* }}} */
987f92
 
987f92
-static void add_request_header(char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg) /* {{{ */
987f92
-{
987f92
-	zval *return_value = (zval*)arg;
987f92
-	char *str = NULL;
987f92
-	char *p;
987f92
-	ALLOCA_FLAG(use_heap)
987f92
-
987f92
-	if (var_len > 5 &&
987f92
-	    var[0] == 'H' &&
987f92
-	    var[1] == 'T' &&
987f92
-	    var[2] == 'T' &&
987f92
-	    var[3] == 'P' &&
987f92
-	    var[4] == '_') {
987f92
-
987f92
-		var_len -= 5;
987f92
-		p = var + 5;
987f92
-		var = str = do_alloca(var_len + 1, use_heap);
987f92
-		*str++ = *p++;
987f92
-		while (*p) {
987f92
-			if (*p == '_') {
987f92
-				*str++ = '-';
987f92
-				p++;
987f92
-				if (*p) {
987f92
-					*str++ = *p++;
987f92
-				}
987f92
-			} else if (*p >= 'A' && *p <= 'Z') {
987f92
-				*str++ = (*p++ - 'A' + 'a');
987f92
-			} else {
987f92
-				*str++ = *p++;
987f92
-			}
987f92
-		}
987f92
-		*str = 0;
987f92
-	} else if (var_len == sizeof("CONTENT_TYPE")-1 &&
987f92
-	           memcmp(var, "CONTENT_TYPE", sizeof("CONTENT_TYPE")-1) == 0) {
987f92
-		var = "Content-Type";
987f92
-	} else if (var_len == sizeof("CONTENT_LENGTH")-1 &&
987f92
-	           memcmp(var, "CONTENT_LENGTH", sizeof("CONTENT_LENGTH")-1) == 0) {
987f92
-		var = "Content-Length";
987f92
-	} else {
987f92
-		return;
987f92
-	}
987f92
-	add_assoc_stringl_ex(return_value, var, var_len, val, val_len);
987f92
-	if (str) {
987f92
-		free_alloca(var, use_heap);
987f92
-	}
987f92
-}
987f92
-/* }}} */
987f92
-
987f92
 PHP_FUNCTION(apache_request_headers) /* {{{ */
987f92
 {
987f92
 	if (zend_parse_parameters_none()) {
987f92
@@ -1648,7 +1600,7 @@ PHP_FUNCTION(apache_request_headers) /*
987f92
 	if (fcgi_is_fastcgi()) {
987f92
 		fcgi_request *request = (fcgi_request*) SG(server_context);
987f92
 
987f92
-		fcgi_loadenv(request, add_request_header, return_value);
987f92
+		fcgi_loadenv(request, sapi_add_request_header, return_value);
987f92
 	} else {
987f92
 		char buf[128];
987f92
 		char **env, *p, *q, *var, *val, *t = buf;
987f92
diff --git a/sapi/fpm/fpm/fpm_main.c b/sapi/fpm/fpm/fpm_main.c
987f92
index 3256660..e815be4 100644
987f92
--- a/sapi/fpm/fpm/fpm_main.c
987f92
+++ b/sapi/fpm/fpm/fpm_main.c
987f92
@@ -1533,6 +1533,10 @@ PHP_FUNCTION(fastcgi_finish_request) /* {{{ */
987f92
 {
987f92
 	fcgi_request *request = (fcgi_request*) SG(server_context);
987f92
 
987f92
+	if (zend_parse_parameters_none() == FAILURE) {
987f92
+		return;
987f92
+	}
987f92
+
987f92
 	if (!fcgi_is_closed(request)) {
987f92
 		php_output_end_all();
987f92
 		php_header();
987f92
@@ -1547,8 +1551,27 @@ PHP_FUNCTION(fastcgi_finish_request) /* {{{ */
987f92
 }
987f92
 /* }}} */
987f92
 
987f92
+ZEND_BEGIN_ARG_INFO(cgi_fcgi_sapi_no_arginfo, 0)
987f92
+ZEND_END_ARG_INFO()
987f92
+
987f92
+PHP_FUNCTION(apache_request_headers) /* {{{ */
987f92
+{
987f92
+	fcgi_request *request;
987f92
+
987f92
+	if (zend_parse_parameters_none() == FAILURE) {
987f92
+		return;
987f92
+	}
987f92
+
987f92
+	array_init(return_value);
987f92
+	if ((request = (fcgi_request*) SG(server_context))) {
987f92
+		fcgi_loadenv(request, sapi_add_request_header, return_value);
987f92
+	}
987f92
+} /* }}} */
987f92
+
987f92
 static const zend_function_entry cgi_fcgi_sapi_functions[] = {
987f92
-	PHP_FE(fastcgi_finish_request,              NULL)
987f92
+	PHP_FE(fastcgi_finish_request,                    cgi_fcgi_sapi_no_arginfo)
987f92
+	PHP_FE(apache_request_headers,                    cgi_fcgi_sapi_no_arginfo)
987f92
+	PHP_FALIAS(getallheaders, apache_request_headers, cgi_fcgi_sapi_no_arginfo)
987f92
 	PHP_FE_END
987f92
 };
987f92
 
987f92
diff --git a/sapi/fpm/tests/getallheaders.phpt b/sapi/fpm/tests/getallheaders.phpt
987f92
new file mode 100644
987f92
index 0000000..b41f1c6
987f92
--- /dev/null
987f92
+++ b/sapi/fpm/tests/getallheaders.phpt
987f92
@@ -0,0 +1,67 @@
987f92
+--TEST--
987f92
+FPM: Function getallheaders basic test
987f92
+--SKIPIF--
987f92
+
987f92
+--FILE--
987f92
+
987f92
+
987f92
+require_once "tester.inc";
987f92
+
987f92
+$cfg = <<
987f92
+[global]
987f92
+error_log = {{FILE:LOG}}
987f92
+[unconfined]
987f92
+listen = {{ADDR}}
987f92
+pm = dynamic
987f92
+pm.max_children = 5
987f92
+pm.start_servers = 1
987f92
+pm.min_spare_servers = 1
987f92
+pm.max_spare_servers = 3
987f92
+EOT;
987f92
+
987f92
+$code = <<
987f92
+
987f92
+echo "Test Start\n";
987f92
+var_dump(getallheaders());
987f92
+echo "Test End\n";
987f92
+EOT;
987f92
+
987f92
+$headers = [];
987f92
+$tester = new FPM\Tester($cfg, $code);
987f92
+$tester->start();
987f92
+$tester->expectLogStartNotices();
987f92
+$tester->request(
987f92
+		'', 
987f92
+		[
987f92
+			'HTTP_X_FOO' => 'BAR',
987f92
+			'HTTP_FOO'   => 'foo'
987f92
+		]
987f92
+	)->expectBody(
987f92
+		[
987f92
+			'Test Start',
987f92
+			'array(4) {',
987f92
+			'  ["Foo"]=>',
987f92
+			'  string(3) "foo"',
987f92
+			'  ["X-Foo"]=>',
987f92
+			'  string(3) "BAR"',
987f92
+			'  ["Content-Length"]=>',
987f92
+			'  string(1) "0"',
987f92
+			'  ["Content-Type"]=>',
987f92
+			'  string(0) ""',
987f92
+			'}',
987f92
+			'Test End',
987f92
+		]
987f92
+	);
987f92
+$tester->terminate();
987f92
+$tester->expectLogTerminatingNotices();
987f92
+$tester->close();
987f92
+
987f92
+?>
987f92
+Done
987f92
+--EXPECT--
987f92
+Done
987f92
+--CLEAN--
987f92
+
987f92
+require_once "tester.inc";
987f92
+FPM\Tester::clean();
987f92
+?>
987f92
-- 
987f92
2.1.4
987f92