Skip to content

Commit

Permalink
new cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFreeman committed Jul 9, 2024
1 parent 936402e commit 5568707
Show file tree
Hide file tree
Showing 26 changed files with 817 additions and 175 deletions.
1 change: 1 addition & 0 deletions ext-src/php_swoole.cc
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ PHP_MINIT_FUNCTION(swoole) {
php_swoole_server_port_minit(module_number);
php_swoole_http_request_minit(module_number);
php_swoole_http_response_minit(module_number);
php_swoole_http_cookie_minit(module_number);
php_swoole_http_server_minit(module_number);
php_swoole_http_server_coro_minit(module_number);
php_swoole_websocket_server_minit(module_number);
Expand Down
108 changes: 108 additions & 0 deletions ext-src/php_swoole_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,112 @@ struct Context {
void free();
};

struct Cookie {
zend_string *name = nullptr;
zend_string *value = nullptr;
zend_string *path = nullptr;
zend_string *domain = nullptr;
zend_string *sameSite = nullptr;
zend_string *priority = nullptr;
zend_long expires = 0;
zend_bool secure = false;
zend_bool httpOnly = false;
zend_bool partitioned = false;
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);
}
}
};

} // namespace http

namespace http2 {
Expand Down Expand Up @@ -270,10 +376,12 @@ class Session {
extern zend_class_entry *swoole_http_server_ce;
extern zend_class_entry *swoole_http_request_ce;
extern zend_class_entry *swoole_http_response_ce;
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);

/**
* These class properties cannot be modified by the user before assignment, such as Swoole\\Http\\Request.
Expand Down
1 change: 1 addition & 0 deletions ext-src/php_swoole_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ void php_swoole_server_minit(int module_number);
void php_swoole_server_port_minit(int module_number);
void php_swoole_http_request_minit(int module_number);
void php_swoole_http_response_minit(int module_number);
void php_swoole_http_cookie_minit(int module_number);
void php_swoole_http_server_minit(int module_number);
void php_swoole_http_server_coro_minit(int module_number);
void php_swoole_websocket_server_minit(int module_number);
Expand Down
18 changes: 18 additions & 0 deletions ext-src/stubs/php_swoole_http_cookie.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
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 getCookie(): array {}
public function reset(): bool {}
}
}
51 changes: 51 additions & 0 deletions ext-src/stubs/php_swoole_http_cookie_arginfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: a14fec26e023c94118c7074dec7f462f8b3a84f0 */

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_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_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_STRING, 0, "\'\'")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setExpires, 0, 0, IS_VOID, 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_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_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_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_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_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_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_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_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_reset, 0, 0, _IS_BOOL, 0)
ZEND_END_ARG_INFO()
4 changes: 2 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,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 {}
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 {}
public function cookie(\Swoole\Http\Cookie $cookie): bool {}
public function rawcookie(\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
12 changes: 2 additions & 10 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: f233694bac2a3ab5469d8ffd95d4d44f5ce9c340 */
* Stub hash: 8d9d9ceaf03c44dc001ced33ab773ca091ec3996 */

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,15 +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_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_OBJ_INFO(0, cookie, Swoole\\Http\\Cookie, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_Swoole_Http_Response_rawcookie arginfo_class_Swoole_Http_Response_cookie
Expand Down
Loading

0 comments on commit 5568707

Please sign in to comment.