Skip to content

Commit 00597e1

Browse files
Kezinomihakezi
authored andcommitted
[issue #2373] Use memory streams for getBodyStream
1 parent 5b7225d commit 00597e1

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

ext-src/stubs/php_swoole_http_request.stub.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public static function create(array $options = []): Request {}
77
public function parse(string $data): int|false {}
88
public function isCompleted(): bool {}
99
public function getMethod(): string|false {}
10-
public function getContent(): string|false {}
11-
/** @return resource|false */
12-
public function getBodyStream(){}
13-
}
10+
public function getContent(): string|false {}
11+
/** @return resource|false */
12+
public function getBodyStream() {}
13+
}
1414
}

ext-src/swoole_http_request.cc

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ const zend_function_entry swoole_http_request_methods[] =
222222
PHP_ME(swoole_http_request, parse, arginfo_class_Swoole_Http_Request_parse, ZEND_ACC_PUBLIC)
223223
PHP_ME(swoole_http_request, isCompleted, arginfo_class_Swoole_Http_Request_isCompleted, ZEND_ACC_PUBLIC)
224224
PHP_ME(swoole_http_request, getMethod, arginfo_class_Swoole_Http_Request_getMethod, ZEND_ACC_PUBLIC)
225-
PHP_ME(swoole_http_request, getBodyStream, arginfo_class_Swoole_Http_Request_getBodyStream, ZEND_ACC_PUBLIC)
225+
PHP_ME(swoole_http_request, getBodyStream, arginfo_class_Swoole_Http_Request_getBodyStream, ZEND_ACC_PUBLIC)
226226
PHP_FE_END
227227
};
228228
// clang-format on
@@ -918,33 +918,37 @@ static PHP_METHOD(swoole_http_request, getBodyStream) {
918918
RETURN_FALSE;
919919
}
920920

921-
char tmp_file[SW_HTTP_UPLOAD_TMPDIR_SIZE];
922-
sw_snprintf(tmp_file, SW_HTTP_UPLOAD_TMPDIR_SIZE, "%s/swoole.request.XXXXXX", ctx->upload_tmp_dir.c_str());
923-
int fd = swoole_tmpfile(tmp_file);
924-
if (fd < 0) {
925-
RETURN_FALSE;
926-
}
927-
928921
HttpRequest *req = &ctx->request;
922+
const char *data = nullptr;
923+
size_t length = 0;
924+
929925
if (req->body_length > 0) {
930926
zval *zdata = &req->zdata;
931-
write(fd, Z_STRVAL_P(zdata) + Z_STRLEN_P(zdata) - req->body_length, req->body_length);
927+
data = Z_STRVAL_P(zdata) + Z_STRLEN_P(zdata) - req->body_length;
928+
length = req->body_length;
932929
} else if (req->chunked_body && req->chunked_body->length != 0) {
933-
write(fd, req->chunked_body->str, req->chunked_body->length);
930+
data = req->chunked_body->str;
931+
length = req->chunked_body->length;
934932
} else if (req->h2_data_buffer && req->h2_data_buffer->length != 0) {
935-
write(fd, req->h2_data_buffer->str, req->h2_data_buffer->length);
933+
data = req->h2_data_buffer->str;
934+
length = req->h2_data_buffer->length;
936935
}
937936

938-
lseek(fd, 0, SEEK_SET);
939-
php_stream *stream = php_swoole_create_stream_from_pipe(fd, "r+", nullptr);
937+
zend_string *buf = nullptr;
938+
if (data && length > 0) {
939+
buf = zend_string_init(data, length, 0);
940+
}
941+
php_stream *stream = php_stream_memory_open(TEMP_STREAM_READONLY, buf);
940942
if (!stream) {
941-
close(fd);
943+
if (buf) {
944+
zend_string_release(buf);
945+
}
942946
RETURN_FALSE;
943947
}
944-
zval *ztmpfiles = swoole_http_init_and_read_property(
945-
swoole_http_request_ce, ctx->request.zobject, &ctx->request.ztmpfiles, SW_ZSTR_KNOWN(SW_ZEND_STR_TMPFILES));
946-
add_next_index_string(ztmpfiles, tmp_file);
947948
php_stream_to_zval(stream, return_value);
949+
if (buf) {
950+
zend_string_release(buf);
951+
}
948952
}
949953

950954
static PHP_METHOD(swoole_http_request, getData) {

0 commit comments

Comments
 (0)