Skip to content

Commit

Permalink
Slightly clean up cookies handling
Browse files Browse the repository at this point in the history
Make the property always an array with an empty array default.
Properly separate the array on modification to compensate.
  • Loading branch information
nikic committed Aug 20, 2021
1 parent de6cf68 commit 841d0b3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 54 deletions.
63 changes: 28 additions & 35 deletions ext/soap/php_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -822,37 +822,35 @@ int make_http_soap_request(zval *this_ptr,

/* Send cookies along with request */
cookies = Z_CLIENT_COOKIES_P(this_ptr);
if (Z_TYPE_P(cookies) == IS_ARRAY) {
ZEND_ASSERT(Z_TYPE_P(cookies) == IS_ARRAY);
if (zend_hash_num_elements(Z_ARRVAL_P(cookies)) != 0) {
zval *data;
zend_string *key;
uint32_t n = zend_hash_num_elements(Z_ARRVAL_P(cookies));
has_cookies = 1;
if (n > 0) {
smart_str_append_const(&soap_headers, "Cookie: ");
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(cookies), key, data) {
if (key && Z_TYPE_P(data) == IS_ARRAY) {
zval *value;

if ((value = zend_hash_index_find(Z_ARRVAL_P(data), 0)) != NULL &&
Z_TYPE_P(value) == IS_STRING) {
zval *tmp;
if (((tmp = zend_hash_index_find(Z_ARRVAL_P(data), 1)) == NULL ||
Z_TYPE_P(tmp) != IS_STRING ||
strncmp(phpurl->path?ZSTR_VAL(phpurl->path):"/",Z_STRVAL_P(tmp),Z_STRLEN_P(tmp)) == 0) &&
((tmp = zend_hash_index_find(Z_ARRVAL_P(data), 2)) == NULL ||
Z_TYPE_P(tmp) != IS_STRING ||
in_domain(ZSTR_VAL(phpurl->host),Z_STRVAL_P(tmp))) &&
(use_ssl || (tmp = zend_hash_index_find(Z_ARRVAL_P(data), 3)) == NULL)) {
smart_str_append(&soap_headers, key);
smart_str_appendc(&soap_headers, '=');
smart_str_append(&soap_headers, Z_STR_P(value));
smart_str_appendc(&soap_headers, ';');
}
smart_str_append_const(&soap_headers, "Cookie: ");
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(cookies), key, data) {
if (key && Z_TYPE_P(data) == IS_ARRAY) {
zval *value;

if ((value = zend_hash_index_find(Z_ARRVAL_P(data), 0)) != NULL &&
Z_TYPE_P(value) == IS_STRING) {
zval *tmp;
if (((tmp = zend_hash_index_find(Z_ARRVAL_P(data), 1)) == NULL ||
Z_TYPE_P(tmp) != IS_STRING ||
strncmp(phpurl->path?ZSTR_VAL(phpurl->path):"/",Z_STRVAL_P(tmp),Z_STRLEN_P(tmp)) == 0) &&
((tmp = zend_hash_index_find(Z_ARRVAL_P(data), 2)) == NULL ||
Z_TYPE_P(tmp) != IS_STRING ||
in_domain(ZSTR_VAL(phpurl->host),Z_STRVAL_P(tmp))) &&
(use_ssl || (tmp = zend_hash_index_find(Z_ARRVAL_P(data), 3)) == NULL)) {
smart_str_append(&soap_headers, key);
smart_str_appendc(&soap_headers, '=');
smart_str_append(&soap_headers, Z_STR_P(value));
smart_str_appendc(&soap_headers, ';');
}
}
} ZEND_HASH_FOREACH_END();
smart_str_append_const(&soap_headers, "\r\n");
}
}
} ZEND_HASH_FOREACH_END();
smart_str_append_const(&soap_headers, "\r\n");
}

http_context_headers(context, has_authorization, has_proxy_authorization, has_cookies, &soap_headers);
Expand Down Expand Up @@ -957,17 +955,12 @@ int make_http_soap_request(zval *this_ptr,
cookie_itt = ZSTR_VAL(http_headers);

while ((cookie_itt = get_http_header_value_nodup(cookie_itt, "Set-Cookie: ", &cookie_len))) {
char *cookie;
char *eqpos, *sempos;
zval *cookies = Z_CLIENT_COOKIES_P(this_ptr);
if (Z_TYPE_P(cookies) != IS_ARRAY) {
array_init(cookies);
}

cookie = estrndup(cookie_itt, cookie_len);
SEPARATE_ARRAY(cookies);

eqpos = strstr(cookie, "=");
sempos = strstr(cookie, ";");
char *cookie = estrndup(cookie_itt, cookie_len);
char *eqpos = strstr(cookie, "=");
char *sempos = strstr(cookie, ";");
if (eqpos != NULL && (sempos == NULL || sempos > eqpos)) {
smart_str name = {0};
int cookie_len;
Expand Down
19 changes: 4 additions & 15 deletions ext/soap/soap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2692,22 +2692,16 @@ PHP_METHOD(SoapClient, __doRequest)
PHP_METHOD(SoapClient, __setCookie)
{
zend_string *name, *val = NULL;
zval *this_ptr = ZEND_THIS;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S!", &name, &val) == FAILURE) {
RETURN_THROWS();
}

zval *cookies = Z_CLIENT_COOKIES_P(this_ptr);
zval *cookies = Z_CLIENT_COOKIES_P(ZEND_THIS);
SEPARATE_ARRAY(cookies);
if (val == NULL) {
if (Z_TYPE_P(cookies) == IS_ARRAY) {
zend_hash_del(Z_ARRVAL_P(cookies), name);
}
zend_hash_del(Z_ARRVAL_P(cookies), name);
} else {
if (Z_TYPE_P(cookies) != IS_ARRAY) {
array_init(cookies);
}

zval zcookie;
array_init(&zcookie);
add_index_str(&zcookie, 0, zend_string_copy(val));
Expand All @@ -2723,12 +2717,7 @@ PHP_METHOD(SoapClient, __getCookies)
RETURN_THROWS();
}

zval *cookies = Z_CLIENT_COOKIES_P(ZEND_THIS);
if (Z_TYPE_P(cookies) == IS_ARRAY) {
RETURN_ARR(zend_array_dup(Z_ARRVAL_P(cookies)));
} else {
array_init(return_value);
}
RETURN_COPY(Z_CLIENT_COOKIES_P(ZEND_THIS));
}
/* }}} */

Expand Down
2 changes: 1 addition & 1 deletion ext/soap/soap.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class SoapClient
private ?int $_ssl_method = null;
private int $_soap_version;
private ?int $_use_proxy = null;
private ?array $_cookies = null;
private array $_cookies = [];
private ?array $__default_headers = null;
private ?SoapFault $__soap_fault = null;
private ?string $__last_request = null;
Expand Down
6 changes: 3 additions & 3 deletions ext/soap/soap_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: a77aedba4916cdd6981f70dee9185c1ed7b0ab6e */
* Stub hash: ab057422ee42f574e7d00462f3bf173caf14ecc1 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_use_soap_error_handler, 0, 0, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, enable, _IS_BOOL, 0, "true")
Expand Down Expand Up @@ -598,9 +598,9 @@ static zend_class_entry *register_class_SoapClient(void)
zend_string_release(property__use_proxy_name);

zval property__cookies_default_value;
ZVAL_NULL(&property__cookies_default_value);
ZVAL_EMPTY_ARRAY(&property__cookies_default_value);
zend_string *property__cookies_name = zend_string_init("_cookies", sizeof("_cookies") - 1, 1);
zend_declare_typed_property(class_entry, property__cookies_name, &property__cookies_default_value, ZEND_ACC_PRIVATE, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ARRAY|MAY_BE_NULL));
zend_declare_typed_property(class_entry, property__cookies_name, &property__cookies_default_value, ZEND_ACC_PRIVATE, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ARRAY));
zend_string_release(property__cookies_name);

zval property___default_headers_default_value;
Expand Down

0 comments on commit 841d0b3

Please sign in to comment.