Skip to content

Commit

Permalink
Optimize performance, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Aug 27, 2024
1 parent f4df7c9 commit c7996c7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
37 changes: 34 additions & 3 deletions ext-src/swoole_http_client_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -768,19 +768,50 @@ void Client::set_basic_auth(const std::string &username, const std::string &pass
}
}

static zend_always_inline zval *sw_zend_symtable_str_add(
HashTable *ht, const char *str, size_t len, zend_ulong idx, bool numeric_key, zval *pData) {
if (numeric_key) {
return zend_hash_index_add(ht, idx, pData);
} else {
return zend_hash_str_add(ht, str, len, pData);
}
}

static zend_always_inline zval *sw_zend_symtable_str_find(
HashTable *ht, const char *str, size_t len, zend_ulong idx, bool numeric_key) {
if (numeric_key) {
return zend_hash_index_find(ht, idx);
} else {
return zend_hash_str_find(ht, str, len);
}
}

static zend_always_inline zval *sw_zend_symtable_str_update(
HashTable *ht, const char *str, size_t len, zend_ulong idx, bool numeric_key, zval *pData) {
if (numeric_key) {
return zend_hash_index_update(ht, idx, pData);
} else {
return zend_hash_str_update(ht, str, len, pData);
}
}

void Client::add_header(const char *key, size_t key_len, const char *str, size_t length) {
zval *zheaders =
sw_zend_read_and_convert_property_array(swoole_http_client_coro_ce, zobject, ZEND_STRL("headers"), 0);
zend_array *array = Z_ARRVAL_P(zheaders);

zval zvalue_new;
ZVAL_STRINGL(&zvalue_new, str, length);
zval *zresult = zend_hash_str_add(array, key, key_len, &zvalue_new);

zend_ulong idx;
bool numeric_key = ZEND_HANDLE_NUMERIC_STR(key, key_len, idx);

zval *zresult = sw_zend_symtable_str_add(array, key, key_len, idx, numeric_key, &zvalue_new);
/**
* Adding failed, indicating that this header already exists and must be converted to an array
*/
if (!zresult) {
zval *zvalue_found = zend_hash_str_find(array, key, key_len);
zval *zvalue_found = sw_zend_symtable_str_find(array, key, key_len, idx, numeric_key);
if (ZVAL_IS_ARRAY(zvalue_found)) {
add_next_index_zval(zvalue_found, &zvalue_new);
} else {
Expand All @@ -789,7 +820,7 @@ void Client::add_header(const char *key, size_t key_len, const char *str, size_t
Z_ADDREF_P(zvalue_found);
add_next_index_zval(&zvalue_array, zvalue_found);
add_next_index_zval(&zvalue_array, &zvalue_new);
zend_hash_str_update(array, key, key_len, &zvalue_array);
sw_zend_symtable_str_update(array, key, key_len, idx, numeric_key, &zvalue_array);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/swoole_http_server_coro/close_socket.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ $pm->childFunc = function () use ($pm) {
foreach (array_merge([
'Content-Type' => 'application/octet-stream',
'X-Accel-Buffering' => 'no',
'server' => 'webserver/1.0'
'X-Server' => 'webserver/1.0'
], $headers) as $k => $v) {
$socket->send("{$k}: {$v}\r\n");
}
Expand Down

0 comments on commit c7996c7

Please sign in to comment.