Skip to content

Commit

Permalink
New cookie API (#5420)
Browse files Browse the repository at this point in the history
* new cookie

* optimize set cookie method

* fix error

* optimize code

* optimize code

* refactor

* optimize code

* optimize code [2]

---------

Co-authored-by: NathanFreeman <[email protected]>
Co-authored-by: MARiA so cute <[email protected]>
  • Loading branch information
3 people authored Jul 24, 2024
1 parent 5ba0700 commit b21b8d9
Show file tree
Hide file tree
Showing 21 changed files with 924 additions and 174 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
39 changes: 39 additions & 0 deletions ext-src/php_swoole_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,43 @@ struct Context {
void free();
};

class Cookie {
private:
bool encode_;
smart_str buffer_ = {0};

protected:
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;

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 *);
void reset();
void toArray(zval *return_value);
zend_string *toString();
~Cookie();
};

} // namespace http

namespace http2 {
Expand Down Expand Up @@ -270,10 +307,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_get_cooke_safety(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
19 changes: 19 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,19 @@
<?php
namespace Swoole\Http {
class Cookie {
public function __construct(bool $encode = true) {}
public function withName(string $name): \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 {}
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 toArray(): array {}
public function toString(): string | false {}
public function reset(): void {}
}
}
55 changes: 55 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,55 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: a11e74ea8b1a4c77a3c10fbfbd94775c504ba812 */

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)
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_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_toArray, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Swoole_Http_Cookie_toString, 0, 0, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_reset, 0, 0, IS_VOID, 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|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 header(string $key, string|array $value, bool $format = true): bool {}
public function initHeader(): bool {}
public function isWritable(): bool {}
Expand Down
18 changes: 15 additions & 3 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: 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 @@ -30,9 +30,21 @@ 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_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)
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
Expand Down
Loading

0 comments on commit b21b8d9

Please sign in to comment.