@@ -209,6 +209,7 @@ static PHP_METHOD(swoole_http_request, parse);
209
209
static PHP_METHOD (swoole_http_request, isCompleted);
210
210
static PHP_METHOD (swoole_http_request, getMethod);
211
211
static PHP_METHOD (swoole_http_request, getContent);
212
+ static PHP_METHOD (swoole_http_request, getBodyStream);
212
213
SW_EXTERN_C_END
213
214
214
215
// clang-format off
@@ -221,6 +222,7 @@ const zend_function_entry swoole_http_request_methods[] =
221
222
PHP_ME (swoole_http_request, parse, arginfo_class_Swoole_Http_Request_parse, ZEND_ACC_PUBLIC)
222
223
PHP_ME (swoole_http_request, isCompleted, arginfo_class_Swoole_Http_Request_isCompleted, ZEND_ACC_PUBLIC)
223
224
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)
224
226
PHP_FE_END
225
227
};
226
228
// clang-format on
@@ -910,6 +912,41 @@ static PHP_METHOD(swoole_http_request, getContent) {
910
912
RETURN_EMPTY_STRING ();
911
913
}
912
914
915
+ static PHP_METHOD (swoole_http_request, getBodyStream) {
916
+ HttpContext *ctx = php_swoole_http_request_get_and_check_context (ZEND_THIS);
917
+ if (UNEXPECTED (!ctx)) {
918
+ RETURN_FALSE;
919
+ }
920
+
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
+
928
+ HttpRequest *req = &ctx->request ;
929
+ if (req->body_length > 0 ) {
930
+ zval *zdata = &req->zdata ;
931
+ write (fd, Z_STRVAL_P (zdata) + Z_STRLEN_P (zdata) - req->body_length , req->body_length );
932
+ } else if (req->chunked_body && req->chunked_body ->length != 0 ) {
933
+ write (fd, req->chunked_body ->str , req->chunked_body ->length );
934
+ } 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 );
936
+ }
937
+
938
+ lseek (fd, 0 , SEEK_SET);
939
+ php_stream *stream = php_swoole_create_stream_from_pipe (fd, " r+" , nullptr );
940
+ if (!stream) {
941
+ close (fd);
942
+ RETURN_FALSE;
943
+ }
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);
947
+ php_stream_to_zval (stream, return_value);
948
+ }
949
+
913
950
static PHP_METHOD (swoole_http_request, getData) {
914
951
HttpContext *ctx = php_swoole_http_request_get_and_check_context (ZEND_THIS);
915
952
if (UNEXPECTED (!ctx)) {
0 commit comments