Skip to content

Commit 5df6776

Browse files
committed
optimize code
1 parent 5838b4c commit 5df6776

File tree

6 files changed

+23
-28
lines changed

6 files changed

+23
-28
lines changed

ext-src/php_swoole_http.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,11 @@ struct Context {
214214
};
215215

216216
class Cookie {
217-
private:
217+
private:
218218
bool encode_;
219219
smart_str buffer_ = {0};
220-
protected:
220+
221+
protected:
221222
zend_string *name = nullptr;
222223
zend_string *value = nullptr;
223224
zend_string *path = nullptr;
@@ -228,7 +229,8 @@ class Cookie {
228229
zend_bool secure = false;
229230
zend_bool httpOnly = false;
230231
zend_bool partitioned = false;
231-
public:
232+
233+
public:
232234
Cookie(bool _encode = true) {
233235
encode_ = _encode;
234236
}
@@ -242,10 +244,9 @@ class Cookie {
242244
Cookie *withDomain(zend_string *);
243245
Cookie *withSameSite(zend_string *);
244246
Cookie *withPriority(zend_string *);
245-
zend_string *create();
246247
void reset();
247248
void toArray(zval *return_value);
248-
void toString(zval *return_value);
249+
zend_string *toString();
249250
~Cookie();
250251
};
251252

ext-src/stubs/php_swoole_http_cookie.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function withSameSite(string $sameSite = ''): \Swoole\Http\Cookie {}
1313
public function withPriority(string $priority = ''): \Swoole\Http\Cookie {}
1414
public function withPartitioned(bool $partitioned = false): \Swoole\Http\Cookie {}
1515
public function toArray(): array {}
16-
public function toString(): string {}
16+
public function toString(): string | false {}
1717
public function reset(): void {}
1818
}
1919
}

ext-src/stubs/php_swoole_http_cookie_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: e23852c332ef2c62b86048d36b2ae15a7d8a0de6 */
2+
* Stub hash: a11e74ea8b1a4c77a3c10fbfbd94775c504ba812 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Http_Cookie___construct, 0, 0, 0)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, encode, _IS_BOOL, 0, "true")
@@ -48,7 +48,7 @@ ZEND_END_ARG_INFO()
4848
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_toArray, 0, 0, IS_ARRAY, 0)
4949
ZEND_END_ARG_INFO()
5050

51-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_toString, 0, 0, IS_STRING, 0)
51+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Swoole_Http_Cookie_toString, 0, 0, MAY_BE_STRING|MAY_BE_FALSE)
5252
ZEND_END_ARG_INFO()
5353

5454
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_reset, 0, 0, IS_VOID, 0)

ext-src/swoole_http_cookie.cc

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ HttpCookie *HttpCookie::withPartitioned(zend_bool _partitioned) {
174174
return this;
175175
}
176176

177-
zend_string *HttpCookie::create() {
177+
zend_string *HttpCookie::toString() {
178178
zend_string *date = nullptr;
179179
if (name == nullptr || ZSTR_LEN(name) == 0) {
180180
php_swoole_error(E_WARNING, "The name cannot be empty");
181181
return nullptr;
182182
}
183183

184-
if (strpbrk(ZSTR_VAL(name), "=" ILLEGAL_COOKIE_CHARACTER) != nullptr) {
184+
if (strpbrk(ZSTR_VAL(name), "=" ILLEGAL_COOKIE_CHARACTER) != nullptr) {
185185
php_swoole_error(E_WARNING, "The name cannot contain \"=\", " ILLEGAL_COOKIE_CHARACTER_PRINT);
186186
return nullptr;
187187
}
@@ -334,15 +334,6 @@ void HttpCookie::toArray(zval *return_value) {
334334
add_assoc_bool(return_value, "partitioned", partitioned);
335335
}
336336

337-
void HttpCookie::toString(zval *return_value) {
338-
auto cookie_str = create();
339-
if (!cookie_str) {
340-
reset();
341-
RETURN_FALSE;
342-
}
343-
ZVAL_STR(return_value, cookie_str);
344-
}
345-
346337
HttpCookie::~Cookie() {
347338
reset();
348339
}
@@ -371,7 +362,7 @@ static PHP_METHOD(swoole_http_cookie, __construct) {
371362

372363
#define PHP_METHOD_HTTP_COOKIE_WITH_BOOL(field) \
373364
zend_bool field = false; \
374-
HttpCookie *cookie = php_swoole_http_get_cookie(ZEND_THIS); \
365+
HttpCookie *cookie = php_swoole_http_get_cooke_safety(ZEND_THIS); \
375366
\
376367
ZEND_PARSE_PARAMETERS_START(0, 1) \
377368
Z_PARAM_OPTIONAL \
@@ -431,13 +422,19 @@ static PHP_METHOD(swoole_http_cookie, withPartitioned) {
431422
}
432423

433424
static PHP_METHOD(swoole_http_cookie, toString) {
434-
php_swoole_http_get_cookie(ZEND_THIS)->toString(return_value);
425+
auto cookie = php_swoole_http_get_cooke_safety(ZEND_THIS);
426+
auto cookie_str = cookie->toString();
427+
if (!cookie_str) {
428+
cookie->reset();
429+
RETURN_FALSE;
430+
}
431+
ZVAL_STR(return_value, cookie_str);
435432
}
436433

437434
static PHP_METHOD(swoole_http_cookie, toArray) {
438-
php_swoole_http_get_cookie(ZEND_THIS)->toArray(return_value);
435+
php_swoole_http_get_cooke_safety(ZEND_THIS)->toArray(return_value);
439436
}
440437

441438
static PHP_METHOD(swoole_http_cookie, reset) {
442-
php_swoole_http_get_cookie(ZEND_THIS)->reset();
439+
php_swoole_http_get_cooke_safety(ZEND_THIS)->reset();
443440
}

ext-src/swoole_http_response.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -966,11 +966,8 @@ static PHP_METHOD(swoole_http_response, sendfile) {
966966

967967
static bool inline php_swoole_http_response_create_cookie(HttpCookie *cookie, zval *zobject) {
968968
HttpContext *ctx = php_swoole_http_response_get_and_check_context(zobject);
969-
if (UNEXPECTED(!ctx)) {
970-
return false;
971-
}
972969

973-
zend_string *cookie_str = cookie->create();
970+
zend_string *cookie_str = cookie->toString();
974971
if (!cookie_str) {
975972
cookie->reset();
976973
return false;

tests/swoole_http_server_coro/check_cookie_crlf.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ $pm->childFirst();
5454
$pm->run();
5555
?>
5656
--EXPECTF--
57-
Warning: Swoole\Http\Response::rawcookie(): Cookie value cannot contain ",", ";", " ", "\t", "\r", "\n", "\013", or "\014" in %s
57+
Warning: Swoole\Http\Response::rawcookie(): The value cannot contain ",", ";", " ", "\t", "\r", "\n", "\013", or "\014" in %s
5858
DONE

0 commit comments

Comments
 (0)