-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Implemented FR #65917 (getallheaders() is not supported by the built-in web server) #496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,6 +133,7 @@ typedef struct php_cli_server_request { | |
char *query_string; | ||
size_t query_string_len; | ||
HashTable headers; | ||
HashTable headers_original_case; | ||
char *content; | ||
size_t content_len; | ||
const char *ext; | ||
|
@@ -435,6 +436,75 @@ static const char *get_mime_type(const char *ext, size_t ext_len) /* {{{ */ | |
return NULL; | ||
} /* }}} */ | ||
|
||
PHP_FUNCTION(apache_request_headers) /* {{{ */ | ||
{ | ||
php_cli_server_client *client; | ||
HashTable *headers; | ||
char *key; | ||
uint key_len; | ||
char **value_pointer; | ||
HashPosition pos; | ||
|
||
if (zend_parse_parameters_none() == FAILURE) { | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use zppn:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, right. Will fix. |
||
|
||
client = SG(server_context); | ||
headers = &client->request.headers_original_case; | ||
|
||
array_init_size(return_value, zend_hash_num_elements(headers)); | ||
|
||
zend_hash_internal_pointer_reset_ex(headers, &pos); | ||
while (zend_hash_get_current_data_ex(headers, (void **)&value_pointer, &pos) == SUCCESS) { | ||
zend_hash_get_current_key_ex(headers, &key, &key_len, NULL, 0, &pos); | ||
add_assoc_string_ex(return_value, key, key_len, *value_pointer, 1); | ||
zend_hash_move_forward_ex(headers, &pos); | ||
} | ||
} | ||
/* }}} */ | ||
|
||
static void add_response_header(sapi_header_struct *h, zval *return_value TSRMLS_DC) /* {{{ */ | ||
{ | ||
char *s, *p; | ||
int len; | ||
ALLOCA_FLAG(use_heap) | ||
|
||
if (h->header_len > 0) { | ||
p = strchr(h->header, ':'); | ||
len = p - h->header; | ||
if (p && (len > 0)) { | ||
while (len > 0 && (h->header[len-1] == ' ' || h->header[len-1] == '\t')) { | ||
len--; | ||
} | ||
if (len) { | ||
s = do_alloca(len + 1, use_heap); | ||
memcpy(s, h->header, len); | ||
s[len] = 0; | ||
do { | ||
p++; | ||
} while (*p == ' ' || *p == '\t'); | ||
add_assoc_stringl_ex(return_value, s, len+1, p, h->header_len - (p - h->header), 1); | ||
free_alloca(s, use_heap); | ||
} | ||
} | ||
} | ||
} | ||
/* }}} */ | ||
|
||
PHP_FUNCTION(apache_response_headers) /* {{{ */ | ||
{ | ||
if (zend_parse_parameters_none() == FAILURE) { | ||
return; | ||
} | ||
|
||
if (!&SG(sapi_headers).headers) { | ||
RETURN_FALSE; | ||
} | ||
array_init(return_value); | ||
zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t)add_response_header, return_value TSRMLS_CC); | ||
} | ||
/* }}} */ | ||
|
||
/* {{{ cli_server module | ||
*/ | ||
|
||
|
@@ -479,9 +549,15 @@ zend_module_entry cli_server_module_entry = { | |
}; | ||
/* }}} */ | ||
|
||
ZEND_BEGIN_ARG_INFO(arginfo_no_args, 0) | ||
ZEND_END_ARG_INFO() | ||
|
||
const zend_function_entry server_additional_functions[] = { | ||
PHP_FE(cli_set_process_title, arginfo_cli_set_process_title) | ||
PHP_FE(cli_get_process_title, arginfo_cli_get_process_title) | ||
PHP_FE(apache_request_headers, arginfo_no_args) | ||
PHP_FE(apache_response_headers, arginfo_no_args) | ||
PHP_FALIAS(getallheaders, apache_request_headers, arginfo_no_args) | ||
{NULL, NULL, NULL} | ||
}; | ||
|
||
|
@@ -1300,6 +1376,7 @@ static int php_cli_server_request_ctor(php_cli_server_request *req) /* {{{ */ | |
req->query_string = NULL; | ||
req->query_string_len = 0; | ||
zend_hash_init(&req->headers, 0, NULL, (void(*)(void*))char_ptr_dtor_p, 1); | ||
zend_hash_init(&req->headers_original_case, 0, NULL, NULL, 1); | ||
req->content = NULL; | ||
req->content_len = 0; | ||
req->ext = NULL; | ||
|
@@ -1325,6 +1402,7 @@ static void php_cli_server_request_dtor(php_cli_server_request *req) /* {{{ */ | |
pefree(req->query_string, 1); | ||
} | ||
zend_hash_destroy(&req->headers); | ||
zend_hash_destroy(&req->headers_original_case); | ||
if (req->content) { | ||
pefree(req->content, 1); | ||
} | ||
|
@@ -1569,6 +1647,7 @@ static int php_cli_server_client_read_request_on_header_value(php_http_parser *p | |
{ | ||
char *header_name = zend_str_tolower_dup(client->current_header_name, client->current_header_name_len); | ||
zend_hash_add(&client->request.headers, header_name, client->current_header_name_len + 1, &value, sizeof(char *), NULL); | ||
zend_hash_add(&client->request.headers_original_case, client->current_header_name, client->current_header_name_len + 1, &value, sizeof(char *), NULL); | ||
efree(header_name); | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--TEST-- | ||
Implement Req #65917 (getallheaders() is not supported by the built-in web server) | ||
--SKIPIF-- | ||
<?php | ||
include "skipif.inc"; | ||
?> | ||
--FILE-- | ||
<?php | ||
include "php_cli_server.inc"; | ||
php_cli_server_start(<<<'PHP' | ||
header('Bar-Foo: Foo'); | ||
var_dump(getallheaders()); | ||
var_dump(apache_request_headers()); | ||
var_dump(apache_response_headers()); | ||
PHP | ||
); | ||
|
||
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} | ||
Foo-Bar: Bar | ||
|
||
|
||
HEADER | ||
)) { | ||
while (!feof($fp)) { | ||
echo fgets($fp); | ||
} | ||
} | ||
|
||
fclose($fp); | ||
?> | ||
--EXPECTF-- | ||
HTTP/1.1 200 OK | ||
Host: %s | ||
Connection: close | ||
X-Powered-By: %s | ||
Bar-Foo: Foo | ||
Content-type: text/html | ||
|
||
array(2) { | ||
["Host"]=> | ||
string(9) "localhost" | ||
["Foo-Bar"]=> | ||
string(3) "Bar" | ||
} | ||
array(2) { | ||
["Host"]=> | ||
string(9) "localhost" | ||
["Foo-Bar"]=> | ||
string(3) "Bar" | ||
} | ||
array(3) { | ||
["X-Powered-By"]=> | ||
string(13) "P%s" | ||
["Bar-Foo"]=> | ||
string(3) "Foo" | ||
["Content-type"]=> | ||
string(9) "text/html" | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By convention we use
under_score
variable names rather thancamelCase
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, OK. I'll fix that as well. I should possibly deduplicate the
apache_response_headers
implementation as well, which is copied from FastCGI.