Skip to content

Commit 7097eae

Browse files
authored
Improve Response::create() & Refactor HttpContext (swoole#3998)
* Improve Response::create() * support array * optimize code * optimize code [2] * optimize code * fix * fix 2 * fix 3 * optimize code * fix * rename * fix 3
1 parent 9e2568f commit 7097eae

13 files changed

+436
-232
lines changed

ext-src/php_swoole.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,6 @@ const zend_function_entry swoole_functions[] =
217217
};
218218
// clang-format on
219219

220-
#if PHP_MEMORY_DEBUG
221-
php_vmstat_t php_vmstat;
222-
#endif
223-
224220
zend_class_entry *swoole_exception_ce;
225221
zend_object_handlers swoole_exception_handlers;
226222

ext-src/php_swoole.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,12 @@ static sw_inline zend_bool ZVAL_IS_ARRAY(zval *v)
499499
}
500500
#endif
501501

502+
#ifndef ZVAL_IS_OBJECT
503+
static sw_inline zend_bool ZVAL_IS_OBJECT(zval *v) {
504+
return Z_TYPE_P(v) == IS_OBJECT;
505+
}
506+
#endif
507+
502508
static sw_inline zval *sw_malloc_zval()
503509
{
504510
return (zval *) emalloc(sizeof(zval));

ext-src/php_swoole_http.h

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ enum http_compress_method {
5454
};
5555

5656
namespace swoole {
57-
57+
class Server;
5858
#ifdef SW_USE_HTTP2
5959
class Coroutine;
6060
namespace http2 {
@@ -119,15 +119,15 @@ struct Response {
119119
struct Context {
120120
SessionId fd;
121121
uchar completed : 1;
122-
uchar end : 1;
123-
uchar send_header : 1;
122+
uchar end_ : 1;
123+
uchar send_header_ : 1;
124124
#ifdef SW_HAVE_COMPRESSION
125125
uchar enable_compression : 1;
126126
uchar accept_compression : 1;
127127
#endif
128128
uchar send_chunked : 1;
129129
uchar recv_chunked : 1;
130-
uchar send_trailer : 1;
130+
uchar send_trailer_ : 1;
131131
uchar keepalive : 1;
132132
uchar websocket : 1;
133133
#ifdef SW_HAVE_ZLIB
@@ -165,12 +165,36 @@ struct Context {
165165
size_t current_form_data_name_len;
166166
zval *current_multipart_header;
167167

168-
const char *upload_tmp_dir;
168+
std::string upload_tmp_dir;
169169

170170
void *private_data;
171171
bool (*send)(Context *ctx, const char *data, size_t length);
172172
bool (*sendfile)(Context *ctx, const char *file, uint32_t l_file, off_t offset, size_t length);
173173
bool (*close)(Context *ctx);
174+
175+
void init(Server *server);
176+
void init(coroutine::Socket *socket);
177+
void bind(Server *server);
178+
void bind(coroutine::Socket *socket);
179+
void copy(Context *ctx);
180+
bool parse_form_data(const char *boundary_str, int boundary_len);
181+
size_t parse(const char *data, size_t length);
182+
bool set_header(const char *, size_t, zval *, bool);
183+
bool set_header(const char *, size_t, const char *, size_t, bool);
184+
void end(zval *zdata, zval *return_value);
185+
void send_trailer(zval *return_value);
186+
String *get_write_buffer();
187+
188+
#ifdef SW_HAVE_COMPRESSION
189+
void set_compression_method(const char *accept_encoding, size_t length);
190+
const char *get_content_encoding();
191+
#endif
192+
193+
#ifdef SW_USE_HTTP2
194+
void http2_end(zval *zdata, zval *return_value);
195+
#endif
196+
197+
void free();
174198
};
175199

176200
} // namespace http
@@ -191,7 +215,7 @@ class Stream {
191215
~Stream();
192216

193217
bool send_header(size_t body_length, bool end_stream);
194-
bool send_body(swString *body, bool end_stream, size_t max_frame_size, off_t offset = 0, size_t length = 0);
218+
bool send_body(String *body, bool end_stream, size_t max_frame_size, off_t offset = 0, size_t length = 0);
195219
bool send_trailer();
196220

197221
void reset(uint32_t error_code);
@@ -240,8 +264,6 @@ extern swoole::String *swoole_zlib_buffer;
240264
swoole::http::Context *swoole_http_context_new(swoole::SessionId fd);
241265
swoole::http::Context *php_swoole_http_request_get_and_check_context(zval *zobject);
242266
swoole::http::Context *php_swoole_http_response_get_and_check_context(zval *zobject);
243-
void swoole_http_context_free(swoole::http::Context *ctx);
244-
void swoole_http_context_copy(swoole::http::Context *src, swoole::http::Context *dst);
245267

246268
static sw_inline zval *swoole_http_init_and_read_property(
247269
zend_class_entry *ce, zval *zobject, zval **zproperty_store_pp, const char *name, size_t name_len) {
@@ -254,25 +276,16 @@ static sw_inline zval *swoole_http_init_and_read_property(
254276
}
255277
return *zproperty_store_pp;
256278
}
257-
int swoole_http_parse_form_data(swoole::http::Context *ctx, const char *boundary_str, int boundary_len);
279+
258280
void swoole_http_parse_cookie(zval *array, const char *at, size_t length);
259281

260282
swoole::http::Context *php_swoole_http_request_get_context(zval *zobject);
261283
void php_swoole_http_request_set_context(zval *zobject, swoole::http::Context *context);
262284
swoole::http::Context *php_swoole_http_response_get_context(zval *zobject);
263285
void php_swoole_http_response_set_context(zval *zobject, swoole::http::Context *context);
264-
size_t swoole_http_requset_parse(swoole::http::Context *ctx, const char *data, size_t length);
265-
266-
bool swoole_http_response_set_header(swoole::http::Context *, const char *, size_t, zval *, bool);
267-
bool swoole_http_response_set_header(swoole::http::Context *, const char *, size_t, const char *, size_t, bool);
268-
269-
void swoole_http_response_end(swoole::http::Context *ctx, zval *zdata, zval *return_value);
270-
void swoole_http_response_send_trailer(swoole::http::Context *ctx, zval *return_value);
271286

272287
#ifdef SW_HAVE_COMPRESSION
273288
int swoole_http_response_compress(const char *data, size_t length, int method, int level);
274-
void swoole_http_get_compression_method(swoole::http::Context *ctx, const char *accept_encoding, size_t length);
275-
const char *swoole_http_get_content_encoding(swoole::http::Context *ctx);
276289
#endif
277290

278291
#ifdef SW_HAVE_ZLIB
@@ -296,8 +309,6 @@ static sw_inline nghttp2_mem *php_nghttp2_mem() {
296309
return &mem;
297310
}
298311

299-
void swoole_http2_response_end(swoole::http::Context *ctx, zval *zdata, zval *return_value);
300-
301312
namespace swoole {
302313
namespace http2 {
303314
//-----------------------------------namespace begin--------------------------------------------

ext-src/php_swoole_http_server.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ void swoole_websocket_onOpen(swoole::http::Context *ctx);
3535
void swoole_websocket_onRequest(swoole::http::Context *ctx);
3636
bool swoole_websocket_handshake(swoole::http::Context *ctx);
3737

38-
void swoole_http_server_init_context(swoole::Server *serv, swoole::http::Context *ctx);
39-
4038
#ifdef SW_USE_HTTP2
4139

4240
int swoole_http2_server_onFrame(swoole::Server *serv, swoole::Connection *conn, swoole::RecvData *req);

ext-src/swoole_http2_server.cc

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static bool swoole_http2_server_respond(http_context *ctx, String *body);
4040

4141
Http2Stream::Stream(Http2Session *client, uint32_t _id) {
4242
ctx = swoole_http_context_new(client->fd);
43-
swoole_http_context_copy(client->default_ctx, ctx);
43+
ctx->copy(client->default_ctx);
4444
ctx->http2 = true;
4545
ctx->stream = this;
4646
ctx->keepalive = true;
@@ -51,8 +51,8 @@ Http2Stream::Stream(Http2Session *client, uint32_t _id) {
5151

5252
Http2Stream::~Stream() {
5353
ctx->stream = nullptr;
54-
ctx->end = true;
55-
swoole_http_context_free(ctx);
54+
ctx->end_ = true;
55+
ctx->free();
5656
}
5757

5858
void Http2Stream::reset(uint32_t error_code) {
@@ -87,7 +87,7 @@ Http2Session::~Session() {
8787
nghttp2_hd_deflate_del(deflater);
8888
}
8989
if (default_ctx) {
90-
efree(default_ctx);
90+
delete default_ctx;
9191
}
9292
http2_sessions.erase(fd);
9393
}
@@ -172,7 +172,7 @@ static bool swoole_http2_is_static_file(Server *serv, http_context *ctx) {
172172
auto date_str_last_modified = handler.get_date_last_modified();
173173

174174
zval *zheader = ctx->request.zserver;
175-
swoole_http_response_set_header(ctx, ZEND_STRL("Last-Modified"), date_str.c_str(), date_str.length(), 0);
175+
ctx->set_header(ZEND_STRL("Last-Modified"), date_str.c_str(), date_str.length(), 0);
176176

177177
zval *zdate_if_modified_since = zend_hash_str_find(Z_ARR_P(zheader), ZEND_STRL("if-modified-since"));
178178
if (zdate_if_modified_since) {
@@ -244,7 +244,7 @@ static ssize_t http2_build_header(http_context *ctx, uchar *buffer, size_t body_
244244
char intbuf[2][16];
245245
int ret;
246246

247-
assert(ctx->send_header == 0);
247+
assert(ctx->send_header_ == 0);
248248

249249
// status code
250250
if (ctx->response.status == 0) {
@@ -314,7 +314,7 @@ static ssize_t http2_build_header(http_context *ctx, uchar *buffer, size_t body_
314314
// content encoding
315315
#ifdef SW_HAVE_COMPRESSION
316316
if (ctx->accept_compression) {
317-
const char *content_encoding = swoole_http_get_content_encoding(ctx);
317+
const char *content_encoding = ctx->get_content_encoding();
318318
headers.add(ZEND_STRL("content-encoding"), (char *) content_encoding, strlen(content_encoding));
319319
}
320320
#endif
@@ -353,7 +353,7 @@ static ssize_t http2_build_header(http_context *ctx, uchar *buffer, size_t body_
353353
return -1;
354354
}
355355

356-
ctx->send_header = 1;
356+
ctx->send_header_ = 1;
357357
return rv;
358358
}
359359

@@ -416,7 +416,7 @@ bool Http2Stream::send_header(size_t body_length, bool end_stream) {
416416
swoole_http_buffer->append(header_buffer, bytes);
417417

418418
if (!ctx->send(ctx, swoole_http_buffer->str, swoole_http_buffer->length)) {
419-
ctx->send_header = 0;
419+
ctx->send_header_ = 0;
420420
return false;
421421
}
422422

@@ -503,7 +503,7 @@ static bool swoole_http2_server_respond(http_context *ctx, String *body) {
503503
}
504504

505505
// The headers has already been sent, retries are no longer allowed (even if send body failed)
506-
ctx->end = 1;
506+
ctx->end_ = 1;
507507

508508
bool error = false;
509509

@@ -602,15 +602,15 @@ static bool http2_context_sendfile(http_context *ctx, const char *file, uint32_t
602602
}
603603

604604
const char *mimetype = swoole::mime_type::get(file).c_str();
605-
swoole_http_response_set_header(ctx, ZEND_STRL("content-type"), mimetype, strlen(mimetype), 0);
605+
ctx->set_header(ZEND_STRL("content-type"), mimetype, strlen(mimetype), 0);
606606

607607
bool end_stream = (ztrailer == nullptr);
608608
if (!stream->send_header(length, end_stream)) {
609609
return false;
610610
}
611611

612612
/* headers has already been sent, retries are no longer allowed (even if send body failed) */
613-
ctx->end = 1;
613+
ctx->end_ = 1;
614614

615615
bool error = false;
616616

@@ -725,7 +725,7 @@ static int http2_parse_header(Http2Session *client, http_context *ctx, int flags
725725
swWarn("invalid multipart/form-data body fd:%ld", ctx->fd);
726726
return SW_ERR;
727727
}
728-
swoole_http_parse_form_data(ctx, (char *) nv.value + nv.valuelen - boundary_len, boundary_len);
728+
ctx->parse_form_data((char *) nv.value + nv.valuelen - boundary_len, boundary_len);
729729
ctx->parser.data = ctx;
730730
}
731731
} else if (SW_STRCASEEQ((char *) nv.name, nv.namelen, "cookie")) {
@@ -738,7 +738,7 @@ static int http2_parse_header(Http2Session *client, http_context *ctx, int flags
738738
}
739739
#ifdef SW_HAVE_COMPRESSION
740740
else if (ctx->enable_compression && SW_STRCASEEQ((char *) nv.name, nv.namelen, "accept-encoding")) {
741-
swoole_http_get_compression_method(ctx, (char *) nv.value, nv.valuelen);
741+
ctx->set_compression_method((char *) nv.value, nv.valuelen);
742742
}
743743
#endif
744744
add_assoc_stringl_ex(zheader, (char *) nv.name, nv.namelen, (char *) nv.value, nv.valuelen);
@@ -996,8 +996,8 @@ int swoole_http2_server_onFrame(Server *serv, Connection *conn, RecvData *req) {
996996

997997
client->handle = swoole_http2_onRequest;
998998
if (!client->default_ctx) {
999-
client->default_ctx = (http_context *) ecalloc(1, sizeof(http_context));
1000-
swoole_http_server_init_context(serv, client->default_ctx);
999+
client->default_ctx = new http_context();
1000+
client->default_ctx->init(serv);
10011001
client->default_ctx->fd = session_id;
10021002
client->default_ctx->http2 = true;
10031003
client->default_ctx->stream = (Http2Stream *) -1;
@@ -1022,16 +1022,16 @@ void swoole_http2_server_session_free(Connection *conn) {
10221022
delete client;
10231023
}
10241024

1025-
void swoole_http2_response_end(http_context *ctx, zval *zdata, zval *return_value) {
1026-
swString http_body = {};
1025+
void http_context::http2_end(zval *zdata, zval *return_value) {
1026+
String http_body = {};
10271027
if (zdata) {
10281028
http_body.length = php_swoole_get_send_data(zdata, &http_body.str);
10291029
} else {
10301030
http_body.length = 0;
10311031
http_body.str = nullptr;
10321032
}
10331033

1034-
RETURN_BOOL(swoole_http2_server_respond(ctx, &http_body));
1034+
RETURN_BOOL(swoole_http2_server_respond(this, &http_body));
10351035
}
10361036

10371037
#endif

0 commit comments

Comments
 (0)