Skip to content

Commit

Permalink
optimize set cookie method
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFreeman committed Jul 18, 2024
1 parent 5568707 commit 9bb3530
Show file tree
Hide file tree
Showing 26 changed files with 383 additions and 251 deletions.
1 change: 1 addition & 0 deletions ext-src/php_swoole_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ struct Cookie {
zend_bool secure = false;
zend_bool httpOnly = false;
zend_bool partitioned = false;
zend_bool encode = true;
smart_str buffer = {0};

zend_string *create() {
Expand Down
20 changes: 10 additions & 10 deletions ext-src/stubs/php_swoole_http_cookie.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
namespace Swoole\Http {
class Cookie {
public function __construct() {}
public function setName(string $name): void {}
public function setValue(string $value = ''): void {}
public function setExpires(int $expires = 0): void {}
public function setPath(string $path = '/'): void {}
public function setDomain(string $domain = ''): void {}
public function setSecure(bool $secure = false): void {}
public function setHttpOnly(bool $httpOnly = false): void {}
public function setSameSite(string $sameSite = ''): void {}
public function setPriority(string $priority = ''): void {}
public function setPartitioned(bool $partitioned = false): void {}
public function setName(string $name): \Swoole\Http\Cookie {}
public function setValue(string $value = '', bool $encode = true): \Swoole\Http\Cookie {}
public function setExpires(int $expires = 0): \Swoole\Http\Cookie {}
public function setPath(string $path = '/'): \Swoole\Http\Cookie {}
public function setDomain(string $domain = ''): \Swoole\Http\Cookie {}
public function setSecure(bool $secure = false): \Swoole\Http\Cookie {}
public function setHttpOnly(bool $httpOnly = false): \Swoole\Http\Cookie {}
public function setSameSite(string $sameSite = ''): \Swoole\Http\Cookie {}
public function setPriority(string $priority = ''): \Swoole\Http\Cookie {}
public function setPartitioned(bool $partitioned = false): \Swoole\Http\Cookie {}
public function getCookie(): array {}
public function reset(): bool {}
}
Expand Down
23 changes: 12 additions & 11 deletions ext-src/stubs/php_swoole_http_cookie_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: a14fec26e023c94118c7074dec7f462f8b3a84f0 */
* Stub hash: 02aefd39f4852eea3258eea8edb7000fa86af163 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Http_Cookie___construct, 0, 0, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setName, 0, 1, IS_VOID, 0)
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_setName, 0, 1, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setValue, 0, 0, IS_VOID, 0)
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_setValue, 0, 0, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_STRING, 0, "\'\'")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, encode, _IS_BOOL, 0, "true")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setExpires, 0, 0, IS_VOID, 0)
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_setExpires, 0, 0, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, expires, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setPath, 0, 0, IS_VOID, 0)
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_setPath, 0, 0, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, path, IS_STRING, 0, "\'/\'")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setDomain, 0, 0, IS_VOID, 0)
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_setDomain, 0, 0, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, domain, IS_STRING, 0, "\'\'")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setSecure, 0, 0, IS_VOID, 0)
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_setSecure, 0, 0, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, secure, _IS_BOOL, 0, "false")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setHttpOnly, 0, 0, IS_VOID, 0)
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_setHttpOnly, 0, 0, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, httpOnly, _IS_BOOL, 0, "false")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setSameSite, 0, 0, IS_VOID, 0)
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_setSameSite, 0, 0, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, sameSite, IS_STRING, 0, "\'\'")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setPriority, 0, 0, IS_VOID, 0)
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_setPriority, 0, 0, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, priority, IS_STRING, 0, "\'\'")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setPartitioned, 0, 0, IS_VOID, 0)
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_setPartitioned, 0, 0, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, partitioned, _IS_BOOL, 0, "false")
ZEND_END_ARG_INFO()

Expand Down
5 changes: 3 additions & 2 deletions ext-src/stubs/php_swoole_http_response.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ public function write(string $content): bool {}
public function end(?string $content = null): bool {}
public function sendfile(string $filename, int $offset = 0, int $length = 0): bool {}
public function redirect(string $location, int $http_code = 302): bool {}
public function cookie(\Swoole\Http\Cookie $cookie): bool {}
public function rawcookie(\Swoole\Http\Cookie $cookie): bool {}
public function cookie(string $name, string $value = '', int $expires = 0 , string $path = '/', string $domain = '', bool $secure = false , bool $httponly = false, string $samesite = '', string $priority = '', bool $partitioned = false): bool {}
public function rawcookie(string $name, string $value = '', int $expires = 0 , string $path = '/', string $domain = '', bool $secure = false , bool $httponly = false, string $samesite = '', string $priority = '', bool $partitioned = false): bool {}
public function objectCookie(\Swoole\Http\Cookie $cookie): bool {}
public function header(string $key, string|array $value, bool $format = true): bool {}
public function initHeader(): bool {}
public function isWritable(): bool {}
Expand Down
17 changes: 15 additions & 2 deletions ext-src/stubs/php_swoole_http_response_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: 8d9d9ceaf03c44dc001ced33ab773ca091ec3996 */
* Stub hash: f823bb9df8c0deca0edc159f68fce6bcec8291ed */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_write, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, content, IS_STRING, 0)
Expand All @@ -21,11 +21,24 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_redir
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_cookie, 0, 1, _IS_BOOL, 0)
ZEND_ARG_OBJ_INFO(0, cookie, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_STRING, 0, "\'\'")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, expires, IS_LONG, 0, "0")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, path, IS_STRING, 0, "\'/\'")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, domain, IS_STRING, 0, "\'\'")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, secure, _IS_BOOL, 0, "false")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, httponly, _IS_BOOL, 0, "false")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, samesite, IS_STRING, 0, "\'\'")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, priority, IS_STRING, 0, "\'\'")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, partitioned, _IS_BOOL, 0, "false")
ZEND_END_ARG_INFO()

#define arginfo_class_Swoole_Http_Response_rawcookie arginfo_class_Swoole_Http_Response_cookie

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_objectCookie, 0, 1, _IS_BOOL, 0)
ZEND_ARG_OBJ_INFO(0, cookie, Swoole\\Http\\Cookie, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_header, 0, 2, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
ZEND_ARG_TYPE_MASK(0, value, MAY_BE_STRING|MAY_BE_ARRAY, NULL)
Expand Down
17 changes: 17 additions & 0 deletions ext-src/swoole_http_cookie.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ static PHP_METHOD(swoole_http_cookie, setHttpOnly);
static PHP_METHOD(swoole_http_cookie, setSameSite);
static PHP_METHOD(swoole_http_cookie, setPriority);
static PHP_METHOD(swoole_http_cookie, setPartitioned);
static PHP_METHOD(swoole_http_cookie, setUrlEncode);
static PHP_METHOD(swoole_http_cookie, getCookie);
static PHP_METHOD(swoole_http_cookie, reset);
SW_EXTERN_C_END
Expand Down Expand Up @@ -132,26 +133,32 @@ static PHP_METHOD(swoole_http_cookie, setName) {

zend_string_addref(name);
cookie->name = name;
RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
}

static PHP_METHOD(swoole_http_cookie, setValue) {
zend_string *value = nullptr;
zend_bool encode = true;
HttpCookie *cookie = php_swoole_http_get_cookie(ZEND_THIS);

ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_STR(value)
Z_PARAM_BOOL(encode)
ZEND_PARSE_PARAMETERS_END();

if (cookie->value) {
zend_string_release(cookie->value);
cookie->value = nullptr;
cookie->encode = true;
}

if (value && ZSTR_LEN(value) > 0) {
zend_string_addref(value);
cookie->value = value;
cookie->encode = encode;
}
RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
}

static PHP_METHOD(swoole_http_cookie, setExpires) {
Expand All @@ -164,6 +171,7 @@ static PHP_METHOD(swoole_http_cookie, setExpires) {
ZEND_PARSE_PARAMETERS_END();

cookie->expires = expires;
RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
}

static PHP_METHOD(swoole_http_cookie, setPath) {
Expand All @@ -182,6 +190,7 @@ static PHP_METHOD(swoole_http_cookie, setPath) {

zend_string_addref(path);
cookie->path = path;
RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
}

static PHP_METHOD(swoole_http_cookie, setDomain) {
Expand All @@ -200,6 +209,7 @@ static PHP_METHOD(swoole_http_cookie, setDomain) {

zend_string_addref(domain);
cookie->domain = domain;
RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
}

static PHP_METHOD(swoole_http_cookie, setSecure) {
Expand All @@ -212,6 +222,7 @@ static PHP_METHOD(swoole_http_cookie, setSecure) {
ZEND_PARSE_PARAMETERS_END();

cookie->secure = secure;
RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
}

static PHP_METHOD(swoole_http_cookie, setHttpOnly) {
Expand All @@ -224,6 +235,7 @@ static PHP_METHOD(swoole_http_cookie, setHttpOnly) {
ZEND_PARSE_PARAMETERS_END();

cookie->httpOnly = httpOnly;
RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
}

static PHP_METHOD(swoole_http_cookie, setSameSite) {
Expand All @@ -242,6 +254,7 @@ static PHP_METHOD(swoole_http_cookie, setSameSite) {

zend_string_addref(sameSite);
cookie->sameSite = sameSite;
RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
}

static PHP_METHOD(swoole_http_cookie, setPriority) {
Expand All @@ -260,6 +273,7 @@ static PHP_METHOD(swoole_http_cookie, setPriority) {

zend_string_addref(priority);
cookie->priority = priority;
RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
}

static PHP_METHOD(swoole_http_cookie, setPartitioned) {
Expand All @@ -272,6 +286,7 @@ static PHP_METHOD(swoole_http_cookie, setPartitioned) {
ZEND_PARSE_PARAMETERS_END();

cookie->partitioned = partitioned;
RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
}

static PHP_METHOD(swoole_http_cookie, getCookie) {
Expand All @@ -285,6 +300,7 @@ static PHP_METHOD(swoole_http_cookie, getCookie) {
cookie->domain ? add_assoc_str(return_value, "domain", cookie->path) : add_assoc_string(return_value, "domain", "");
cookie->sameSite ? add_assoc_str(return_value, "sameSite", cookie->name) : add_assoc_string(return_value, "sameSite", "");
cookie->priority ? add_assoc_str(return_value, "priority", cookie->name) : add_assoc_string(return_value, "priority", "");
add_assoc_bool(return_value, "encode", cookie->encode);
add_assoc_long(return_value, "expires", cookie->expires);
add_assoc_bool(return_value, "secure", cookie->secure);
add_assoc_bool(return_value, "httpOnly", cookie->httpOnly);
Expand All @@ -300,6 +316,7 @@ static PHP_METHOD(swoole_http_cookie, reset) {
cookie->secure = false;
cookie->httpOnly = false;
cookie->partitioned = false;
cookie->encode = true;

if (cookie->name) {
zend_string_release(cookie->name);
Expand Down
Loading

0 comments on commit 9bb3530

Please sign in to comment.