Skip to content

Commit

Permalink
Support cookie priority (#3854)
Browse files Browse the repository at this point in the history
* Support cookie priority

* Fix missing priority parameter declaration

Co-authored-by: codinghuang <[email protected]>
  • Loading branch information
matyhtf and huanghantao authored Nov 19, 2020
1 parent ff75951 commit d6476dc
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions ext-src/swoole_http_response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_response_cookie, 0, 0, 1)
ZEND_ARG_INFO(0, secure)
ZEND_ARG_INFO(0, httponly)
ZEND_ARG_INFO(0, samesite)
ZEND_ARG_INFO(0, priority)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_response_write, 0, 0, 1)
Expand Down Expand Up @@ -946,12 +947,12 @@ static PHP_METHOD(swoole_http_response, sendfile) {
}

static void php_swoole_http_response_cookie(INTERNAL_FUNCTION_PARAMETERS, const bool url_encode) {
char *name, *value = nullptr, *path = nullptr, *domain = nullptr, *samesite = nullptr;
char *name, *value = nullptr, *path = nullptr, *domain = nullptr, *samesite = nullptr, *priority = nullptr;
zend_long expires = 0;
size_t name_len, value_len = 0, path_len = 0, domain_len = 0, samesite_len = 0;
size_t name_len, value_len = 0, path_len = 0, domain_len = 0, samesite_len = 0, priority_len = 0;
zend_bool secure = 0, httponly = 0;

ZEND_PARSE_PARAMETERS_START(1, 8)
ZEND_PARSE_PARAMETERS_START(1, 9)
Z_PARAM_STRING(name, name_len)
Z_PARAM_OPTIONAL
Z_PARAM_STRING(value, value_len)
Expand All @@ -961,6 +962,7 @@ static void php_swoole_http_response_cookie(INTERNAL_FUNCTION_PARAMETERS, const
Z_PARAM_BOOL(secure)
Z_PARAM_BOOL(httponly)
Z_PARAM_STRING(samesite, samesite_len)
Z_PARAM_STRING(priority, priority_len)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

http_context *ctx = php_swoole_http_response_get_and_check_context(ZEND_THIS);
Expand Down Expand Up @@ -992,12 +994,12 @@ static void php_swoole_http_response_cookie(INTERNAL_FUNCTION_PARAMETERS, const
encoded_value = php_swoole_url_encode(value, value_len, &encoded_value_len);
cookie_size += encoded_value_len;
cookie = (char *) emalloc(cookie_size);
snprintf(cookie, cookie_size, "%s=%s", name, encoded_value);
sw_snprintf(cookie, cookie_size, "%s=%s", name, encoded_value);
efree(encoded_value);
} else {
cookie_size += value_len;
cookie = (char *) emalloc(cookie_size);
snprintf(cookie, cookie_size, "%s=%s", name, value);
sw_snprintf(cookie, cookie_size, "%s=%s", name, value);
}
if (expires > 0) {
strlcat(cookie, "; expires=", cookie_size);
Expand Down Expand Up @@ -1031,6 +1033,10 @@ static void php_swoole_http_response_cookie(INTERNAL_FUNCTION_PARAMETERS, const
strlcat(cookie, "; samesite=", cookie_size);
strlcat(cookie, samesite, cookie_size);
}
if (priority_len > 0) {
strlcat(cookie, "; priority=", cookie_size);
strlcat(cookie, priority, cookie_size);
}
add_next_index_stringl(
swoole_http_init_and_read_property(
swoole_http_response_ce, ctx->response.zobject, &ctx->response.zcookie, ZEND_STRL("cookie")),
Expand Down

0 comments on commit d6476dc

Please sign in to comment.