Skip to content

Commit

Permalink
operate ptr directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFreeman committed Aug 21, 2023
1 parent e6ba93e commit 2887a2f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
1 change: 0 additions & 1 deletion ext-src/php_swoole_cxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
_(SW_ZEND_STR_COOKIES, "cookies") \
_(SW_ZEND_STR_DOWNLOAD_FILE, "downloadFile") \
_(SW_ZEND_STR_DOWNLOAD_OFFSET, "downloadOffset") \
_(SW_ZEND_STR_SERVER, "server") \
_(SW_ZEND_STR_HEADER, "header") \
_(SW_ZEND_STR_GET, "get") \
_(SW_ZEND_STR_POST, "post") \
Expand Down
20 changes: 20 additions & 0 deletions ext-src/php_swoole_http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,23 @@ static inline void http_server_add_server_array(HashTable *ht, zend_string *key,
ZVAL_STR(&tmp, value);
zend_hash_add(ht, key, &tmp);
}

static inline void http_server_set_object_fd_property(zend_object *object, zend_class_entry *ce, long fd) {
// fd always the first property for swoole_http_request and swoole_http_response.
Bucket *bucket = &((&ce->properties_info)->arData[0]);
zend_property_info *property_info = (zend_property_info *) Z_PTR_P(&bucket->val);
zval *property = OBJ_PROP(object, property_info->offset);
ZVAL_LONG(property, fd);
}

static inline void http_server_init_array_property(
zend_class_entry *ce, zend_object *object, zval **zproperty_store_pp, int pos, int size) {
if (UNEXPECTED(!*zproperty_store_pp)) {
Bucket *bucket = &((&ce->properties_info)->arData[pos]);
zend_property_info *property_info = (zend_property_info *) Z_PTR_P(&bucket->val);
zval *property = OBJ_PROP(object, property_info->offset);
array_init_size(property, size);
*zproperty_store_pp = (zval *) (zproperty_store_pp + 1);
**zproperty_store_pp = *property;
}
}
5 changes: 3 additions & 2 deletions ext-src/swoole_http_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,11 @@ void php_swoole_http_request_minit(int module_number) {
HttpRequestObject,
std);

// Notice: Do not change the order, see http_server_set_object_fd_property and http_server_init_array_property
zend_declare_property_long(swoole_http_request_ce, ZEND_STRL("fd"), 0, ZEND_ACC_PUBLIC);
zend_declare_property_long(swoole_http_request_ce, ZEND_STRL("streamId"), 0, ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_request_ce, ZEND_STRL("header"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_request_ce, ZEND_STRL("server"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_request_ce, ZEND_STRL("header"), ZEND_ACC_PUBLIC);
zend_declare_property_long(swoole_http_request_ce, ZEND_STRL("streamId"), 0, ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_request_ce, ZEND_STRL("cookie"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_request_ce, ZEND_STRL("get"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_request_ce, ZEND_STRL("files"), ZEND_ACC_PUBLIC);
Expand Down
1 change: 1 addition & 0 deletions ext-src/swoole_http_response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ void php_swoole_http_response_minit(int module_number) {
HttpResponseObject,
std);

// Notice: Do not change the order, see http_server_set_object_fd_property.
zend_declare_property_long(swoole_http_response_ce, ZEND_STRL("fd"), 0, ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_response_ce, ZEND_STRL("socket"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_response_ce, ZEND_STRL("header"), ZEND_ACC_PUBLIC);
Expand Down
19 changes: 6 additions & 13 deletions ext-src/swoole_http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,12 @@ HttpContext *swoole_http_context_new(SessionId fd) {
object_init_ex(zresponse_object, swoole_http_response_ce);
php_swoole_http_response_set_context(zresponse_object, ctx);

zval tmp;
ZVAL_LONG(&tmp, fd);
zend_update_property_ex(swoole_http_request_ce, SW_Z8_OBJ_P(zrequest_object), SW_ZSTR_KNOWN(SW_ZEND_STR_FD), &tmp);
zend_update_property_ex(
swoole_http_response_ce, SW_Z8_OBJ_P(zresponse_object), SW_ZSTR_KNOWN(SW_ZEND_STR_FD), &tmp);

swoole_http_init_and_read_property(swoole_http_request_ce,
zrequest_object,
&ctx->request.zserver,
SW_ZSTR_KNOWN(SW_ZEND_STR_SERVER),
HT_MIN_SIZE << 1);
swoole_http_init_and_read_property(
swoole_http_request_ce, zrequest_object, &ctx->request.zheader, SW_ZSTR_KNOWN(SW_ZEND_STR_HEADER));
http_server_set_object_fd_property(SW_Z8_OBJ_P(zrequest_object), swoole_http_request_ce, fd);
http_server_set_object_fd_property(SW_Z8_OBJ_P(zresponse_object), swoole_http_response_ce, fd);
http_server_init_array_property(
swoole_http_request_ce, SW_Z8_OBJ_P(zrequest_object), &ctx->request.zserver, 1, HT_MIN_SIZE << 1);
http_server_init_array_property(
swoole_http_request_ce, SW_Z8_OBJ_P(zrequest_object), &ctx->request.zheader, 2, HT_MIN_SIZE);
ctx->fd = fd;

return ctx;
Expand Down

0 comments on commit 2887a2f

Please sign in to comment.