Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Jul 23, 2024
1 parent efdf152 commit 5838b4c
Show file tree
Hide file tree
Showing 12 changed files with 402 additions and 421 deletions.
119 changes: 24 additions & 95 deletions ext-src/php_swoole_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ struct Context {
void free();
};

struct Cookie {
class Cookie {
private:
bool encode_;
smart_str buffer_ = {0};
protected:
zend_string *name = nullptr;
zend_string *value = nullptr;
zend_string *path = nullptr;
Expand All @@ -224,100 +228,25 @@ 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() {
zend_string *date = nullptr;
if (!value) {
smart_str_append(&buffer, name);
smart_str_appends(&buffer, "=deleted; expires=");

date = php_format_date((char *) ZEND_STRL("D, d-M-Y H:i:s T"), 1, 0);
smart_str_append(&buffer, date);
smart_str_appends(&buffer, "; Max-Age=0");
zend_string_free(date);

smart_str_0(&buffer);
return buffer.s;
}

smart_str_append(&buffer, name);
smart_str_appendc(&buffer, '=');
smart_str_append(&buffer, value);

if (expires > 0) {
smart_str_appends(&buffer, "; expires=");
date = php_format_date((char *) ZEND_STRL("D, d-M-Y H:i:s T"), expires, 0);
smart_str_append(&buffer, date);
smart_str_appends(&buffer, "; Max-Age=");

double diff = difftime(expires, php_time());
smart_str_append_long(&buffer, (zend_long) (diff >= 0 ? diff : 0));
zend_string_free(date);
}

if (path && ZSTR_LEN(path) > 0) {
smart_str_appends(&buffer, "; path=");
smart_str_append(&buffer, path);
}

if (domain && ZSTR_LEN(domain) > 0) {
smart_str_appends(&buffer, "; domain=");
smart_str_append(&buffer, domain);
}

if (secure) {
smart_str_appends(&buffer, "; secure");
}

if (httpOnly) {
smart_str_appends(&buffer, "; HttpOnly");
}

if (sameSite && ZSTR_LEN(sameSite) > 0) {
smart_str_appends(&buffer, "; SameSite=");
smart_str_append(&buffer, sameSite);
}

if (priority && ZSTR_LEN(priority) > 0) {
smart_str_appends(&buffer, "; Priority=");
smart_str_append(&buffer, priority);
}

if (partitioned) {
smart_str_appends(&buffer, "; Partitioned");
}

smart_str_0(&buffer);
return buffer.s;
}

~Cookie() {
if (name) {
zend_string_release(name);
}

if (value) {
zend_string_release(value);
}

if (path) {
zend_string_release(path);
}

if (domain) {
zend_string_release(domain);
}

if (sameSite) {
zend_string_release(sameSite);
}

if (priority) {
zend_string_release(priority);
}
public:
Cookie(bool _encode = true) {
encode_ = _encode;
}
Cookie *withName(zend_string *);
Cookie *withExpires(zend_long);
Cookie *withSecure(zend_bool);
Cookie *withHttpOnly(zend_bool);
Cookie *withPartitioned(zend_bool);
Cookie *withValue(zend_string *);
Cookie *withPath(zend_string *);
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);
~Cookie();
};

} // namespace http
Expand Down Expand Up @@ -382,7 +311,7 @@ extern zend_class_entry *swoole_http_cookie_ce;
swoole::http::Context *swoole_http_context_new(swoole::SessionId fd);
swoole::http::Context *php_swoole_http_request_get_and_check_context(zval *zobject);
swoole::http::Context *php_swoole_http_response_get_and_check_context(zval *zobject);
swoole::http::Cookie *php_swoole_http_response_get_and_check_cookie(zval *zobject);
swoole::http::Cookie *php_swoole_http_get_cooke_safety(zval *zobject);

/**
* These class properties cannot be modified by the user before assignment, such as Swoole\\Http\\Request.
Expand Down
9 changes: 5 additions & 4 deletions ext-src/stubs/php_swoole_http_cookie.stub.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace Swoole\Http {
class Cookie {
public function __construct() {}
public function __construct(bool $encode = true) {}
public function withName(string $name): \Swoole\Http\Cookie {}
public function withValue(string $value = '', bool $encode = true): \Swoole\Http\Cookie {}
public function withValue(string $value = ''): \Swoole\Http\Cookie {}
public function withExpires(int $expires = 0): \Swoole\Http\Cookie {}
public function withPath(string $path = '/'): \Swoole\Http\Cookie {}
public function withDomain(string $domain = ''): \Swoole\Http\Cookie {}
Expand All @@ -12,7 +12,8 @@ public function withHttpOnly(bool $httpOnly = false): \Swoole\Http\Cookie {}
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 getCookie(): array {}
public function reset(): bool {}
public function toArray(): array {}
public function toString(): string {}
public function reset(): void {}
}
}
11 changes: 7 additions & 4 deletions ext-src/stubs/php_swoole_http_cookie_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 3a257967f878f1186db07db30ea2caf29bb97fa7 */
* Stub hash: e23852c332ef2c62b86048d36b2ae15a7d8a0de6 */

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")
ZEND_END_ARG_INFO()

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

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withValue, 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_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withExpires, 0, 0, Swoole\\Http\\Cookie, 0)
Expand Down Expand Up @@ -45,8 +45,11 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withPart
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, partitioned, _IS_BOOL, 0, "false")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_getCookie, 0, 0, IS_ARRAY, 0)
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_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_reset, 0, 0, _IS_BOOL, 0)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_reset, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO()
3 changes: 1 addition & 2 deletions ext-src/stubs/php_swoole_http_response.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ 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(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 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 {}
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
19 changes: 13 additions & 6 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: f823bb9df8c0deca0edc159f68fce6bcec8291ed */
* Stub hash: 8797137fe315a9dec64e349bad8bd72529895bf9 */

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,7 +21,7 @@ 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_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_OBJ_TYPE_MASK(0, name_or_object, Swoole\\Http\\Cookie, MAY_BE_STRING, NULL)
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, "\'/\'")
Expand All @@ -33,10 +33,17 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_cooki
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_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_rawcookie, 0, 1, _IS_BOOL, 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()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_header, 0, 2, _IS_BOOL, 0)
Expand Down
Loading

0 comments on commit 5838b4c

Please sign in to comment.