Skip to content

[#2373] Use memory streams for getBodyStream #5816

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions ext-src/stubs/php_swoole_http_request.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public function parse(string $data): int|false {}
public function isCompleted(): bool {}
public function getMethod(): string|false {}
public function getContent(): string|false {}
/** @return resource|false */
public function getBodyStream() {}
}
}
3 changes: 3 additions & 0 deletions ext-src/stubs/php_swoole_http_request_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ ZEND_END_ARG_INFO()
#define arginfo_class_Swoole_Http_Request_getMethod arginfo_class_Swoole_Http_Request_getData

#define arginfo_class_Swoole_Http_Request_getContent arginfo_class_Swoole_Http_Request_getData

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Swoole_Http_Request_getBodyStream, 0, 0, MAY_BE_RESOURCE|MAY_BE_FALSE)
ZEND_END_ARG_INFO()
41 changes: 41 additions & 0 deletions ext-src/swoole_http_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ static PHP_METHOD(swoole_http_request, parse);
static PHP_METHOD(swoole_http_request, isCompleted);
static PHP_METHOD(swoole_http_request, getMethod);
static PHP_METHOD(swoole_http_request, getContent);
static PHP_METHOD(swoole_http_request, getBodyStream);
SW_EXTERN_C_END

// clang-format off
Expand All @@ -221,6 +222,7 @@ const zend_function_entry swoole_http_request_methods[] =
PHP_ME(swoole_http_request, parse, arginfo_class_Swoole_Http_Request_parse, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_request, isCompleted, arginfo_class_Swoole_Http_Request_isCompleted, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_request, getMethod, arginfo_class_Swoole_Http_Request_getMethod, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_request, getBodyStream, arginfo_class_Swoole_Http_Request_getBodyStream, ZEND_ACC_PUBLIC)
PHP_FE_END
};
// clang-format on
Expand Down Expand Up @@ -910,6 +912,45 @@ static PHP_METHOD(swoole_http_request, getContent) {
RETURN_EMPTY_STRING();
}

static PHP_METHOD(swoole_http_request, getBodyStream) {
HttpContext *ctx = php_swoole_http_request_get_and_check_context(ZEND_THIS);
if (UNEXPECTED(!ctx)) {
RETURN_FALSE;
}

HttpRequest *req = &ctx->request;
const char *data = nullptr;
size_t length = 0;

if (req->body_length > 0) {
zval *zdata = &req->zdata;
data = Z_STRVAL_P(zdata) + Z_STRLEN_P(zdata) - req->body_length;
length = req->body_length;
} else if (req->chunked_body && req->chunked_body->length != 0) {
data = req->chunked_body->str;
length = req->chunked_body->length;
} else if (req->h2_data_buffer && req->h2_data_buffer->length != 0) {
data = req->h2_data_buffer->str;
length = req->h2_data_buffer->length;
}

zend_string *buf = nullptr;
if (data && length > 0) {
buf = zend_string_init(data, length, 0);
}
php_stream *stream = php_stream_memory_open(TEMP_STREAM_READONLY, buf);
if (!stream) {
if (buf) {
zend_string_release(buf);
}
RETURN_FALSE;
}
php_stream_to_zval(stream, return_value);
if (buf) {
zend_string_release(buf);
}
}

static PHP_METHOD(swoole_http_request, getData) {
HttpContext *ctx = php_swoole_http_request_get_and_check_context(ZEND_THIS);
if (UNEXPECTED(!ctx)) {
Expand Down
46 changes: 46 additions & 0 deletions tests/swoole_http_server/getBodyStream.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
swoole_http_server: getBodyStream
--SKIPIF--
<?php
require __DIR__ . '/../include/skipif.inc';
skip_if_in_valgrind();
?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use function Co\run;

$pm = new ProcessManager;
$pm->parentFunc = function ($pid) use ($pm) {
run(function () use ($pm) {
$randomData = get_safe_random();
$cli = new Co\http\Client(HTTP_SERVER_HOST, $pm->getFreePort(), false);
$cli->setMethod('POST');
$cli->setData($randomData);
$ok = $cli->execute('/getBodyStream');
Assert::assert($ok);
Assert::same($cli->statusCode, 200);
Assert::same($cli->errCode, 0);
Assert::same($cli->body, $randomData);
$pm->kill();
echo "DONE\n";
});
};
$pm->childFunc = function () use ($pm) {
$http = new Swoole\Http\Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE);
$http->set(['worker_num' => 1]);
$http->on('workerStart', function () use ($pm) {
$pm->wakeup();
});
$http->on('request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
$stream = $request->getBodyStream();
$response->end(stream_get_contents($stream));
});
$http->start();
};
$pm->childFirst();
$pm->run();
?>
--EXPECT--
DONE
4 changes: 2 additions & 2 deletions tests/swoole_server/close_reset.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ $pm->parentFunc = function ($pid) use ($pm) {
break;
}
$data .= $ret;
if (substr($ret, -2, 2) == "\r\n") {
if (substr($ret, -2, 2) === "\r\n") {
break;
}
}
Assert::lessThan(strlen($data), N);
Assert::lessThanEq(strlen($data), N + 2);
echo "DONE\n";
});
Swoole\Event::wait();
Expand Down
Loading