Skip to content

Commit 9f57a66

Browse files
committed
Make request->headers store zend_string as IS_STRING zval instead as a IS_POINTER zval
1 parent a69d0ba commit 9f57a66

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

sapi/cli/php_cli_server.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ static bool php_cli_server_get_system_time(char *buf) {
269269
/* Destructor for php_cli_server_request->headers, this frees header value */
270270
static void char_ptr_dtor_p(zval *zv) /* {{{ */
271271
{
272-
// TODO free have zend_string* not as a pointer
273-
zend_string_release_ex(Z_PTR_P(zv), /* persistent */ true);
272+
zend_string_release_ex(Z_STR_P(zv), /* persistent */ true);
274273
} /* }}} */
275274

276275
static char *get_last_error(void) /* {{{ */
@@ -348,12 +347,12 @@ static void append_http_status_line(smart_str *buffer, int protocol_version, int
348347

349348
static void append_essential_headers(smart_str* buffer, php_cli_server_client *client, bool persistent) /* {{{ */
350349
{
351-
zend_string *val;
350+
zval *val;
352351
struct timeval tv = {0};
353352

354-
if (NULL != (val = zend_hash_str_find_ptr(&client->request.headers, "host", sizeof("host")-1))) {
353+
if (NULL != (val = zend_hash_str_find(&client->request.headers, "host", sizeof("host")-1))) {
355354
smart_str_appends_ex(buffer, "Host: ", persistent);
356-
smart_str_append_ex(buffer, val, persistent);
355+
smart_str_append_ex(buffer, Z_STR_P(val), persistent);
357356
smart_str_appends_ex(buffer, "\r\n", persistent);
358357
}
359358

@@ -382,25 +381,17 @@ static const char *get_mime_type(const php_cli_server *server, const char *ext,
382381
PHP_FUNCTION(apache_request_headers) /* {{{ */
383382
{
384383
php_cli_server_client *client;
385-
HashTable *headers;
386-
zend_string *key;
387-
zend_string *value;
388384
zval tmp;
389385

390386
if (zend_parse_parameters_none() == FAILURE) {
391387
RETURN_THROWS();
392388
}
393389

394390
client = SG(server_context);
395-
headers = &client->request.headers_original_case;
396391

397-
array_init_size(return_value, zend_hash_num_elements(headers));
398-
399-
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(headers, key, value) {
400-
// TODO There must be a better way
401-
ZVAL_STRING(&tmp, ZSTR_VAL(value));
402-
zend_symtable_update(Z_ARRVAL_P(return_value), key, &tmp);
403-
} ZEND_HASH_FOREACH_END();
392+
/* Need to copy the HashTable */
393+
ZVAL_ARR(&tmp,&client->request.headers_original_case);
394+
RETURN_COPY(&tmp);
404395
}
405396
/* }}} */
406397

@@ -573,11 +564,11 @@ static int sapi_cli_server_send_headers(sapi_headers_struct *sapi_headers) /* {{
573564
static char *sapi_cli_server_read_cookies(void) /* {{{ */
574565
{
575566
php_cli_server_client *client = SG(server_context);
576-
zend_string *val;
577-
if (NULL == (val = zend_hash_str_find_ptr(&client->request.headers, "cookie", sizeof("cookie")-1))) {
567+
zval *val;
568+
if (NULL == (val = zend_hash_str_find(&client->request.headers, "cookie", sizeof("cookie")-1))) {
578569
return NULL;
579570
}
580-
return ZSTR_VAL(val);
571+
return Z_STRVAL_P(val);
581572
} /* }}} */
582573

583574
static size_t sapi_cli_server_read_post(char *buf, size_t count_bytes) /* {{{ */
@@ -607,8 +598,12 @@ static void sapi_cli_server_register_variable(zval *track_vars_array, const char
607598
}
608599
} /* }}} */
609600

610-
static int sapi_cli_server_register_entry_cb(zend_string **entry, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */ {
601+
/* The entry zval will always contain a zend_string* */
602+
static int sapi_cli_server_register_entry_cb(zval *entry, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */ {
611603
zval *track_vars_array = va_arg(args, zval *);
604+
605+
ZEND_ASSERT(Z_TYPE_P(entry) == IS_STRING);
606+
612607
if (hash_key->key) {
613608
char *real_key, *key;
614609
uint32_t i;
@@ -623,9 +618,9 @@ static int sapi_cli_server_register_entry_cb(zend_string **entry, int num_args,
623618
spprintf(&real_key, 0, "%s_%s", "HTTP", key);
624619
if (strcmp(key, "CONTENT_TYPE") == 0 || strcmp(key, "CONTENT_LENGTH") == 0) {
625620
// TODO make a version specialized for zend_string?
626-
sapi_cli_server_register_variable(track_vars_array, key, ZSTR_VAL(*entry));
621+
sapi_cli_server_register_variable(track_vars_array, key, Z_STRVAL_P(entry));
627622
}
628-
sapi_cli_server_register_variable(track_vars_array, real_key, ZSTR_VAL(*entry));
623+
sapi_cli_server_register_variable(track_vars_array, real_key, Z_STRVAL_P(entry));
629624
efree(key);
630625
efree(real_key);
631626
}
@@ -1624,11 +1619,16 @@ static int php_cli_server_client_read_request_on_fragment(php_http_parser *parse
16241619

16251620
static void php_cli_server_client_save_header(php_cli_server_client *client)
16261621
{
1622+
/* Wrap header value in a zval to add is to the HashTable which acts as an array */
1623+
zval tmp;
1624+
ZVAL_STR(&tmp, client->current_header_value);
16271625
/* strip off the colon */
16281626
zend_string *perm_header_name = zend_string_dup(client->current_header_name, /* persistent */ true);
16291627
zend_string *lc_header_name = zend_string_tolower_ex(client->current_header_name, /* persistent */ true);
1630-
zend_hash_add_ptr(&client->request.headers, lc_header_name, client->current_header_value);
1631-
zend_hash_add_ptr(&client->request.headers_original_case, perm_header_name, client->current_header_value);
1628+
1629+
/* Add the wrapped zend_string to the HashTable */
1630+
zend_hash_add(&client->request.headers, lc_header_name, &tmp);
1631+
zend_hash_add(&client->request.headers_original_case, perm_header_name, &tmp);
16321632

16331633
zend_string_release_ex(client->current_header_name, /* persistent */ false);
16341634
zend_string_release_ex(lc_header_name, /* persistent */ true);
@@ -1850,7 +1850,7 @@ static size_t php_cli_server_client_send_through(php_cli_server_client *client,
18501850

18511851
static void php_cli_server_client_populate_request_info(const php_cli_server_client *client, sapi_request_info *request_info) /* {{{ */
18521852
{
1853-
zend_string *val;
1853+
zval *val;
18541854

18551855
request_info->request_method = php_http_method_str(client->request.request_method);
18561856
request_info->proto_num = client->request.protocol_version;
@@ -1859,8 +1859,8 @@ static void php_cli_server_client_populate_request_info(const php_cli_server_cli
18591859
request_info->query_string = client->request.query_string;
18601860
request_info->content_length = client->request.content_len;
18611861
request_info->auth_user = request_info->auth_password = request_info->auth_digest = NULL;
1862-
if (NULL != (val = zend_hash_str_find_ptr(&client->request.headers, "content-type", sizeof("content-type")-1))) {
1863-
request_info->content_type = ZSTR_VAL(val);
1862+
if (NULL != (val = zend_hash_str_find(&client->request.headers, "content-type", sizeof("content-type")-1))) {
1863+
request_info->content_type = Z_STRVAL_P(val);
18641864
}
18651865
} /* }}} */
18661866

@@ -2110,10 +2110,10 @@ static zend_result php_cli_server_begin_send_static(php_cli_server *server, php_
21102110
/* }}} */
21112111

21122112
static zend_result php_cli_server_request_startup(php_cli_server *server, php_cli_server_client *client) { /* {{{ */
2113-
zend_string *auth;
2113+
zval *auth;
21142114
php_cli_server_client_populate_request_info(client, &SG(request_info));
2115-
if (NULL != (auth = zend_hash_str_find_ptr(&client->request.headers, "authorization", sizeof("authorization")-1))) {
2116-
php_handle_auth_data(ZSTR_VAL(auth));
2115+
if (NULL != (auth = zend_hash_str_find(&client->request.headers, "authorization", sizeof("authorization")-1))) {
2116+
php_handle_auth_data(Z_STRVAL_P(auth));
21172117
}
21182118
SG(sapi_headers).http_response_code = 200;
21192119
if (FAILURE == php_request_startup()) {

0 commit comments

Comments
 (0)