Skip to content

Commit

Permalink
optimize http response (#5574)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFreeman authored Nov 20, 2024
1 parent 433fc03 commit af29dac
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 35 deletions.
45 changes: 15 additions & 30 deletions ext-src/swoole_http_response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -750,17 +750,6 @@ void HttpContext::write(zval *zdata, zval *return_value) {
}

void HttpContext::end(zval *zdata, zval *return_value) {
struct {
char *str;
size_t length;
} http_body;
if (zdata) {
http_body.length = php_swoole_get_send_data(zdata, &http_body.str);
} else {
http_body.length = 0;
http_body.str = nullptr;
}

if (send_chunked) {
if (zdata && Z_STRLEN_P(zdata) > 0) {
zval retval;
Expand All @@ -782,6 +771,9 @@ void HttpContext::end(zval *zdata, zval *return_value) {
}
send_chunked = 0;
} else {
char *data = nullptr;
size_t length = zdata ? php_swoole_get_send_data(zdata, &data) : 0;

String *http_buffer = get_write_buffer();
http_buffer->clear();

Expand Down Expand Up @@ -812,39 +804,32 @@ void HttpContext::end(zval *zdata, zval *return_value) {
}
#endif

build_header(http_buffer, http_body.str, http_body.length);
build_header(http_buffer, data, length);

char *send_body_str;
size_t send_body_len;

if (http_body.length > 0) {
if (length > 0) {
#ifdef SW_HAVE_COMPRESSION
if (content_compressed) {
send_body_str = zlib_buffer->str;
send_body_len = zlib_buffer->length;
} else
#endif
{
send_body_str = http_body.str;
send_body_len = http_body.length;
data = zlib_buffer->str;
length = zlib_buffer->length;
}
#endif
// send twice to reduce memory copy
if (send_body_len < swoole_pagesize()) {
if (http_buffer->append(send_body_str, send_body_len) < 0) {
send_header_ = 0;
RETURN_FALSE;
}
} else {
if (length > SW_HTTP_MAX_APPEND_DATA) {
if (!send(this, http_buffer->str, http_buffer->length)) {
send_header_ = 0;
RETURN_FALSE;
}
if (!send(this, send_body_str, send_body_len)) {
if (!send(this, data, length)) {
end_ = 1;
close(this);
RETURN_FALSE;
}
goto _skip_copy;
} else {
if (http_buffer->append(data, length) < 0) {
send_header_ = 0;
RETURN_FALSE;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/swoole_http_server/chunk.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ $pm->childFunc = function () use ($pm) {
$http->on("request", function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
$data = str_split(file_get_contents(TEST_IMAGE), 8192);
foreach ($data as $chunk) {
$response->write($chunk);
Assert::true($response->write($chunk));
}
$response->end();
Assert::true($response->end());
});

$http->start();
Expand Down
6 changes: 3 additions & 3 deletions tests/swoole_http_server/trailer.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ $pm->childFunc = function () use ($pm) {
$http->on('request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
$response->header('trailer', 'Content-MD5');
$data = 'hello world';
$response->write($data);
$response->trailer('Content-MD5', md5($data));
$response->end();
Assert::true($response->write($data));
Assert::true($response->trailer('Content-MD5', md5($data)));
Assert::true($response->end());
});
$http->start();
};
Expand Down

0 comments on commit af29dac

Please sign in to comment.