Skip to content

Commit 5838b4c

Browse files
committed
refactor
1 parent efdf152 commit 5838b4c

12 files changed

+402
-421
lines changed

ext-src/php_swoole_http.h

Lines changed: 24 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,11 @@ struct Context {
213213
void free();
214214
};
215215

216-
struct Cookie {
216+
class Cookie {
217+
private:
218+
bool encode_;
219+
smart_str buffer_ = {0};
220+
protected:
217221
zend_string *name = nullptr;
218222
zend_string *value = nullptr;
219223
zend_string *path = nullptr;
@@ -224,100 +228,25 @@ struct Cookie {
224228
zend_bool secure = false;
225229
zend_bool httpOnly = false;
226230
zend_bool partitioned = false;
227-
zend_bool encode = true;
228-
smart_str buffer = {0};
229-
230-
zend_string *create() {
231-
zend_string *date = nullptr;
232-
if (!value) {
233-
smart_str_append(&buffer, name);
234-
smart_str_appends(&buffer, "=deleted; expires=");
235-
236-
date = php_format_date((char *) ZEND_STRL("D, d-M-Y H:i:s T"), 1, 0);
237-
smart_str_append(&buffer, date);
238-
smart_str_appends(&buffer, "; Max-Age=0");
239-
zend_string_free(date);
240-
241-
smart_str_0(&buffer);
242-
return buffer.s;
243-
}
244-
245-
smart_str_append(&buffer, name);
246-
smart_str_appendc(&buffer, '=');
247-
smart_str_append(&buffer, value);
248-
249-
if (expires > 0) {
250-
smart_str_appends(&buffer, "; expires=");
251-
date = php_format_date((char *) ZEND_STRL("D, d-M-Y H:i:s T"), expires, 0);
252-
smart_str_append(&buffer, date);
253-
smart_str_appends(&buffer, "; Max-Age=");
254-
255-
double diff = difftime(expires, php_time());
256-
smart_str_append_long(&buffer, (zend_long) (diff >= 0 ? diff : 0));
257-
zend_string_free(date);
258-
}
259-
260-
if (path && ZSTR_LEN(path) > 0) {
261-
smart_str_appends(&buffer, "; path=");
262-
smart_str_append(&buffer, path);
263-
}
264-
265-
if (domain && ZSTR_LEN(domain) > 0) {
266-
smart_str_appends(&buffer, "; domain=");
267-
smart_str_append(&buffer, domain);
268-
}
269-
270-
if (secure) {
271-
smart_str_appends(&buffer, "; secure");
272-
}
273-
274-
if (httpOnly) {
275-
smart_str_appends(&buffer, "; HttpOnly");
276-
}
277-
278-
if (sameSite && ZSTR_LEN(sameSite) > 0) {
279-
smart_str_appends(&buffer, "; SameSite=");
280-
smart_str_append(&buffer, sameSite);
281-
}
282-
283-
if (priority && ZSTR_LEN(priority) > 0) {
284-
smart_str_appends(&buffer, "; Priority=");
285-
smart_str_append(&buffer, priority);
286-
}
287-
288-
if (partitioned) {
289-
smart_str_appends(&buffer, "; Partitioned");
290-
}
291-
292-
smart_str_0(&buffer);
293-
return buffer.s;
294-
}
295-
296-
~Cookie() {
297-
if (name) {
298-
zend_string_release(name);
299-
}
300-
301-
if (value) {
302-
zend_string_release(value);
303-
}
304-
305-
if (path) {
306-
zend_string_release(path);
307-
}
308-
309-
if (domain) {
310-
zend_string_release(domain);
311-
}
312-
313-
if (sameSite) {
314-
zend_string_release(sameSite);
315-
}
316-
317-
if (priority) {
318-
zend_string_release(priority);
319-
}
231+
public:
232+
Cookie(bool _encode = true) {
233+
encode_ = _encode;
320234
}
235+
Cookie *withName(zend_string *);
236+
Cookie *withExpires(zend_long);
237+
Cookie *withSecure(zend_bool);
238+
Cookie *withHttpOnly(zend_bool);
239+
Cookie *withPartitioned(zend_bool);
240+
Cookie *withValue(zend_string *);
241+
Cookie *withPath(zend_string *);
242+
Cookie *withDomain(zend_string *);
243+
Cookie *withSameSite(zend_string *);
244+
Cookie *withPriority(zend_string *);
245+
zend_string *create();
246+
void reset();
247+
void toArray(zval *return_value);
248+
void toString(zval *return_value);
249+
~Cookie();
321250
};
322251

323252
} // namespace http
@@ -382,7 +311,7 @@ extern zend_class_entry *swoole_http_cookie_ce;
382311
swoole::http::Context *swoole_http_context_new(swoole::SessionId fd);
383312
swoole::http::Context *php_swoole_http_request_get_and_check_context(zval *zobject);
384313
swoole::http::Context *php_swoole_http_response_get_and_check_context(zval *zobject);
385-
swoole::http::Cookie *php_swoole_http_response_get_and_check_cookie(zval *zobject);
314+
swoole::http::Cookie *php_swoole_http_get_cooke_safety(zval *zobject);
386315

387316
/**
388317
* These class properties cannot be modified by the user before assignment, such as Swoole\\Http\\Request.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
namespace Swoole\Http {
33
class Cookie {
4-
public function __construct() {}
4+
public function __construct(bool $encode = true) {}
55
public function withName(string $name): \Swoole\Http\Cookie {}
6-
public function withValue(string $value = '', bool $encode = true): \Swoole\Http\Cookie {}
6+
public function withValue(string $value = ''): \Swoole\Http\Cookie {}
77
public function withExpires(int $expires = 0): \Swoole\Http\Cookie {}
88
public function withPath(string $path = '/'): \Swoole\Http\Cookie {}
99
public function withDomain(string $domain = ''): \Swoole\Http\Cookie {}
@@ -12,7 +12,8 @@ public function withHttpOnly(bool $httpOnly = false): \Swoole\Http\Cookie {}
1212
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 {}
15-
public function getCookie(): array {}
16-
public function reset(): bool {}
15+
public function toArray(): array {}
16+
public function toString(): string {}
17+
public function reset(): void {}
1718
}
1819
}

ext-src/stubs/php_swoole_http_cookie_arginfo.h

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

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Http_Cookie___construct, 0, 0, 0)
5+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, encode, _IS_BOOL, 0, "true")
56
ZEND_END_ARG_INFO()
67

78
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withName, 0, 1, Swoole\\Http\\Cookie, 0)
@@ -10,7 +11,6 @@ ZEND_END_ARG_INFO()
1011

1112
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withValue, 0, 0, Swoole\\Http\\Cookie, 0)
1213
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_STRING, 0, "\'\'")
13-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, encode, _IS_BOOL, 0, "true")
1414
ZEND_END_ARG_INFO()
1515

1616
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withExpires, 0, 0, Swoole\\Http\\Cookie, 0)
@@ -45,8 +45,11 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withPart
4545
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, partitioned, _IS_BOOL, 0, "false")
4646
ZEND_END_ARG_INFO()
4747

48-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_getCookie, 0, 0, IS_ARRAY, 0)
48+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_toArray, 0, 0, IS_ARRAY, 0)
49+
ZEND_END_ARG_INFO()
50+
51+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_toString, 0, 0, IS_STRING, 0)
4952
ZEND_END_ARG_INFO()
5053

51-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_reset, 0, 0, _IS_BOOL, 0)
54+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_reset, 0, 0, IS_VOID, 0)
5255
ZEND_END_ARG_INFO()

ext-src/stubs/php_swoole_http_response.stub.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ public function write(string $content): bool {}
55
public function end(?string $content = null): bool {}
66
public function sendfile(string $filename, int $offset = 0, int $length = 0): bool {}
77
public function redirect(string $location, int $http_code = 302): bool {}
8-
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 {}
8+
public function cookie(\Swoole\Http\Cookie|string $name_or_object , string $value = '', int $expires = 0 , string $path = '/', string $domain = '', bool $secure = false , bool $httponly = false, string $samesite = '', string $priority = '', bool $partitioned = false): bool {}
99
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 {}
10-
public function objectCookie(\Swoole\Http\Cookie $cookie): bool {}
1110
public function header(string $key, string|array $value, bool $format = true): bool {}
1211
public function initHeader(): bool {}
1312
public function isWritable(): bool {}

ext-src/stubs/php_swoole_http_response_arginfo.h

Lines changed: 13 additions & 6 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: f823bb9df8c0deca0edc159f68fce6bcec8291ed */
2+
* Stub hash: 8797137fe315a9dec64e349bad8bd72529895bf9 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_write, 0, 1, _IS_BOOL, 0)
55
ZEND_ARG_TYPE_INFO(0, content, IS_STRING, 0)
@@ -21,7 +21,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_redir
2121
ZEND_END_ARG_INFO()
2222

2323
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_cookie, 0, 1, _IS_BOOL, 0)
24-
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
24+
ZEND_ARG_OBJ_TYPE_MASK(0, name_or_object, Swoole\\Http\\Cookie, MAY_BE_STRING, NULL)
2525
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_STRING, 0, "\'\'")
2626
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, expires, IS_LONG, 0, "0")
2727
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, path, IS_STRING, 0, "\'/\'")
@@ -33,10 +33,17 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_cooki
3333
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, partitioned, _IS_BOOL, 0, "false")
3434
ZEND_END_ARG_INFO()
3535

36-
#define arginfo_class_Swoole_Http_Response_rawcookie arginfo_class_Swoole_Http_Response_cookie
37-
38-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_objectCookie, 0, 1, _IS_BOOL, 0)
39-
ZEND_ARG_OBJ_INFO(0, cookie, Swoole\\Http\\Cookie, 0)
36+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_rawcookie, 0, 1, _IS_BOOL, 0)
37+
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
38+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_STRING, 0, "\'\'")
39+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, expires, IS_LONG, 0, "0")
40+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, path, IS_STRING, 0, "\'/\'")
41+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, domain, IS_STRING, 0, "\'\'")
42+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, secure, _IS_BOOL, 0, "false")
43+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, httponly, _IS_BOOL, 0, "false")
44+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, samesite, IS_STRING, 0, "\'\'")
45+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, priority, IS_STRING, 0, "\'\'")
46+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, partitioned, _IS_BOOL, 0, "false")
4047
ZEND_END_ARG_INFO()
4148

4249
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_header, 0, 2, _IS_BOOL, 0)

0 commit comments

Comments
 (0)