Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Jul 23, 2024
1 parent 5838b4c commit 5df6776
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 28 deletions.
11 changes: 6 additions & 5 deletions ext-src/php_swoole_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,11 @@ struct Context {
};

class Cookie {
private:
private:
bool encode_;
smart_str buffer_ = {0};
protected:

protected:
zend_string *name = nullptr;
zend_string *value = nullptr;
zend_string *path = nullptr;
Expand All @@ -228,7 +229,8 @@ class Cookie {
zend_bool secure = false;
zend_bool httpOnly = false;
zend_bool partitioned = false;
public:

public:
Cookie(bool _encode = true) {
encode_ = _encode;
}
Expand All @@ -242,10 +244,9 @@ class Cookie {
Cookie *withDomain(zend_string *);
Cookie *withSameSite(zend_string *);
Cookie *withPriority(zend_string *);
zend_string *create();
void reset();
void toArray(zval *return_value);
void toString(zval *return_value);
zend_string *toString();
~Cookie();
};

Expand Down
2 changes: 1 addition & 1 deletion ext-src/stubs/php_swoole_http_cookie.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function withSameSite(string $sameSite = ''): \Swoole\Http\Cookie {}
public function withPriority(string $priority = ''): \Swoole\Http\Cookie {}
public function withPartitioned(bool $partitioned = false): \Swoole\Http\Cookie {}
public function toArray(): array {}
public function toString(): string {}
public function toString(): string | false {}
public function reset(): void {}
}
}
4 changes: 2 additions & 2 deletions ext-src/stubs/php_swoole_http_cookie_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: e23852c332ef2c62b86048d36b2ae15a7d8a0de6 */
* Stub hash: a11e74ea8b1a4c77a3c10fbfbd94775c504ba812 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Http_Cookie___construct, 0, 0, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, encode, _IS_BOOL, 0, "true")
Expand Down Expand Up @@ -48,7 +48,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_toArray, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_toString, 0, 0, IS_STRING, 0)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Swoole_Http_Cookie_toString, 0, 0, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_reset, 0, 0, IS_VOID, 0)
Expand Down
27 changes: 12 additions & 15 deletions ext-src/swoole_http_cookie.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ HttpCookie *HttpCookie::withPartitioned(zend_bool _partitioned) {
return this;
}

zend_string *HttpCookie::create() {
zend_string *HttpCookie::toString() {
zend_string *date = nullptr;
if (name == nullptr || ZSTR_LEN(name) == 0) {
php_swoole_error(E_WARNING, "The name cannot be empty");
return nullptr;
}

if (strpbrk(ZSTR_VAL(name), "=" ILLEGAL_COOKIE_CHARACTER) != nullptr) {
if (strpbrk(ZSTR_VAL(name), "=" ILLEGAL_COOKIE_CHARACTER) != nullptr) {
php_swoole_error(E_WARNING, "The name cannot contain \"=\", " ILLEGAL_COOKIE_CHARACTER_PRINT);
return nullptr;
}
Expand Down Expand Up @@ -334,15 +334,6 @@ void HttpCookie::toArray(zval *return_value) {
add_assoc_bool(return_value, "partitioned", partitioned);
}

void HttpCookie::toString(zval *return_value) {
auto cookie_str = create();
if (!cookie_str) {
reset();
RETURN_FALSE;
}
ZVAL_STR(return_value, cookie_str);
}

HttpCookie::~Cookie() {
reset();
}
Expand Down Expand Up @@ -371,7 +362,7 @@ static PHP_METHOD(swoole_http_cookie, __construct) {

#define PHP_METHOD_HTTP_COOKIE_WITH_BOOL(field) \
zend_bool field = false; \
HttpCookie *cookie = php_swoole_http_get_cookie(ZEND_THIS); \
HttpCookie *cookie = php_swoole_http_get_cooke_safety(ZEND_THIS); \
\
ZEND_PARSE_PARAMETERS_START(0, 1) \
Z_PARAM_OPTIONAL \
Expand Down Expand Up @@ -431,13 +422,19 @@ static PHP_METHOD(swoole_http_cookie, withPartitioned) {
}

static PHP_METHOD(swoole_http_cookie, toString) {
php_swoole_http_get_cookie(ZEND_THIS)->toString(return_value);
auto cookie = php_swoole_http_get_cooke_safety(ZEND_THIS);
auto cookie_str = cookie->toString();
if (!cookie_str) {
cookie->reset();
RETURN_FALSE;
}
ZVAL_STR(return_value, cookie_str);
}

static PHP_METHOD(swoole_http_cookie, toArray) {
php_swoole_http_get_cookie(ZEND_THIS)->toArray(return_value);
php_swoole_http_get_cooke_safety(ZEND_THIS)->toArray(return_value);
}

static PHP_METHOD(swoole_http_cookie, reset) {
php_swoole_http_get_cookie(ZEND_THIS)->reset();
php_swoole_http_get_cooke_safety(ZEND_THIS)->reset();
}
5 changes: 1 addition & 4 deletions ext-src/swoole_http_response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -966,11 +966,8 @@ static PHP_METHOD(swoole_http_response, sendfile) {

static bool inline php_swoole_http_response_create_cookie(HttpCookie *cookie, zval *zobject) {
HttpContext *ctx = php_swoole_http_response_get_and_check_context(zobject);
if (UNEXPECTED(!ctx)) {
return false;
}

zend_string *cookie_str = cookie->create();
zend_string *cookie_str = cookie->toString();
if (!cookie_str) {
cookie->reset();
return false;
Expand Down
2 changes: 1 addition & 1 deletion tests/swoole_http_server_coro/check_cookie_crlf.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ $pm->childFirst();
$pm->run();
?>
--EXPECTF--
Warning: Swoole\Http\Response::rawcookie(): Cookie value cannot contain ",", ";", " ", "\t", "\r", "\n", "\013", or "\014" in %s
Warning: Swoole\Http\Response::rawcookie(): The value cannot contain ",", ";", " ", "\t", "\r", "\n", "\013", or "\014" in %s
DONE

0 comments on commit 5df6776

Please sign in to comment.