Skip to content

[6.0][WIP]new cookie #5401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
109 changes: 109 additions & 0 deletions ext-src/php_swoole_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,113 @@ 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;
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);
}
}
};

} // namespace http

namespace http2 {
Expand Down Expand Up @@ -270,10 +377,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 withName(string $name): \Swoole\Http\Cookie {}
public function withValue(string $value = '', bool $encode = true): \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 {}
public function withSecure(bool $secure = false): \Swoole\Http\Cookie {}
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 {}
}
}
52 changes: 52 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,52 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 3a257967f878f1186db07db30ea2caf29bb97fa7 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Http_Cookie___construct, 0, 0, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withName, 0, 1, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
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)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, expires, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withPath, 0, 0, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, path, IS_STRING, 0, "\'/\'")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withDomain, 0, 0, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, domain, IS_STRING, 0, "\'\'")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withSecure, 0, 0, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, secure, _IS_BOOL, 0, "false")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withHttpOnly, 0, 0, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, httpOnly, _IS_BOOL, 0, "false")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withSameSite, 0, 0, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, sameSite, IS_STRING, 0, "\'\'")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withPriority, 0, 0, Swoole\\Http\\Cookie, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, priority, IS_STRING, 0, "\'\'")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withPartitioned, 0, 0, Swoole\\Http\\Cookie, 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()
5 changes: 3 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,9 @@ 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(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 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
7 changes: 6 additions & 1 deletion 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: f823bb9df8c0deca0edc159f68fce6bcec8291ed */

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 Down Expand Up @@ -30,10 +30,15 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_cooki
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()

#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_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_header, 0, 2, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
ZEND_ARG_TYPE_MASK(0, value, MAY_BE_STRING|MAY_BE_ARRAY, NULL)
Expand Down
Loading
Loading