Skip to content

Commit 3a79779

Browse files
committed
QuickJS: removed extra copy of body argument in ngx.fetch().
1 parent f9d4a5b commit 3a79779

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

nginx/ngx_qjs_fetch.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -805,10 +805,12 @@ ngx_qjs_fetch_response_ctor(JSContext *cx, JSValueConst new_target, int argc,
805805
JSValueConst *argv)
806806
{
807807
int ret;
808+
size_t byte_offset, byte_length;
808809
u_char *p, *end;
809-
JSValue init, value, body, proto, obj;
810+
JSValue init, value, body, proto, obj, buf;
810811
ngx_str_t bd;
811812
ngx_int_t rc;
813+
const char *str;
812814
ngx_pool_t *pool;
813815
ngx_js_ctx_t *ctx;
814816
ngx_js_response_t *response;
@@ -913,12 +915,39 @@ ngx_qjs_fetch_response_ctor(JSContext *cx, JSValueConst new_target, int argc,
913915
body = argv[0];
914916

915917
if (!JS_IsNullOrUndefined(body)) {
916-
if (ngx_qjs_string(cx, pool, body, &bd) != NGX_OK) {
917-
return JS_ThrowInternalError(cx, "invalid Response body");
918+
str = NULL;
919+
if (JS_IsString(body)) {
920+
goto string;
921+
}
922+
923+
buf = JS_GetTypedArrayBuffer(cx, body, &byte_offset, &byte_length, NULL);
924+
if (!JS_IsException(buf)) {
925+
bd.data = JS_GetArrayBuffer(cx, &bd.len, buf);
926+
927+
JS_FreeValue(cx, buf);
928+
929+
if (bd.data != NULL) {
930+
bd.data += byte_offset;
931+
bd.len = byte_length;
932+
}
933+
934+
} else {
935+
936+
string:
937+
str = JS_ToCStringLen(cx, &bd.len, body);
938+
if (str == NULL) {
939+
return JS_EXCEPTION;
940+
}
941+
942+
bd.data = (u_char *) str;
918943
}
919944

920945
njs_chb_append(&response->chain, bd.data, bd.len);
921946

947+
if (str != NULL) {
948+
JS_FreeCString(cx, str);
949+
}
950+
922951
if (JS_IsString(body)) {
923952
rc = ngx_qjs_headers_append(cx, &response->headers,
924953
(u_char *) "Content-Type",

0 commit comments

Comments
 (0)