Skip to content

Commit 34d2cc9

Browse files
committed
qjs fetch.
1 parent 18d3170 commit 34d2cc9

9 files changed

+3561
-110
lines changed

external/qjs_query_string_module.c

-6
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,6 @@ qjs_query_string_decode(JSContext *cx, const u_char *start, size_t size)
252252

253253
ret = qjs_string_create_chb(cx, &chain);
254254

255-
njs_chb_destroy(&chain);
256-
257255
return ret;
258256
}
259257

@@ -281,8 +279,6 @@ qjs_query_string_escape(JSContext *cx, JSValueConst this_val, int argc,
281279

282280
ret = qjs_string_create_chb(cx, &chain);
283281

284-
njs_chb_destroy(&chain);
285-
286282
JS_FreeCString(cx, (char *) str.start);
287283

288284
return ret;
@@ -824,8 +820,6 @@ qjs_query_string_stringify_internal(JSContext *cx, JSValue obj, njs_str_t *sep,
824820

825821
ret = qjs_string_create_chb(cx, &chain);
826822

827-
njs_chb_destroy(&chain);
828-
829823
return ret;
830824

831825
fail:

nginx/config

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ NJS_SRCS="$ngx_addon_dir/ngx_js.c \
1414
$ngx_addon_dir/ngx_js_shared_dict.c"
1515

1616
QJS_DEPS=""
17-
QJS_SRCS=""
17+
QJS_SRCS="$ngx_addon_dir/ngx_qjs_fetch.c"
1818

1919
NJS_OPENSSL_LIB=
2020
NJS_XSLT_LIB=

nginx/ngx_http_js_module.c

+33-48
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,7 @@ static JSClassDef ngx_http_qjs_headers_out_class = {
11331133
qjs_module_t *njs_http_qjs_addon_modules[] = {
11341134
&ngx_qjs_ngx_module,
11351135
&ngx_qjs_ngx_shared_dict_module,
1136+
&ngx_qjs_ngx_fetch_module,
11361137
/*
11371138
* Shared addons should be in the same order and the same positions
11381139
* in all nginx modules.
@@ -5133,7 +5134,7 @@ ngx_http_qjs_ext_internal_redirect(JSContext *cx, JSValueConst this_val,
51335134
"internalRedirect cannot be called while filtering");
51345135
}
51355136

5136-
if (ngx_qjs_string(ctx->engine, argv[0], &ctx->redirect_uri) != NGX_OK) {
5137+
if (ngx_qjs_string(cx, argv[0], &ctx->redirect_uri) != NGX_OK) {
51375138
return JS_EXCEPTION;
51385139
}
51395140

@@ -5412,7 +5413,7 @@ ngx_http_qjs_ext_return(JSContext *cx, JSValueConst this_val,
54125413
ctx = ngx_http_get_module_ctx(r, ngx_http_js_module);
54135414

54145415
if (status < NGX_HTTP_BAD_REQUEST || !JS_IsNullOrUndefined(argv[1])) {
5415-
if (ngx_qjs_string(ctx->engine, argv[1], &body) != NGX_OK) {
5416+
if (ngx_qjs_string(cx, argv[1], &body) != NGX_OK) {
54165417
return JS_ThrowOutOfMemory(cx);
54175418
}
54185419

@@ -5515,7 +5516,7 @@ ngx_http_qjs_ext_send(JSContext *cx, JSValueConst this_val,
55155516
ll = &out;
55165517

55175518
for (n = 0; n < (ngx_uint_t) argc; n++) {
5518-
if (ngx_qjs_string(ctx->engine, argv[n], &s) != NGX_OK) {
5519+
if (ngx_qjs_string(cx, argv[n], &s) != NGX_OK) {
55195520
return JS_ThrowTypeError(cx, "failed to convert arg");
55205521
}
55215522

@@ -5578,7 +5579,7 @@ ngx_http_qjs_ext_send_buffer(JSContext *cx, JSValueConst this_val,
55785579
return JS_ThrowTypeError(cx, "cannot send buffer while not filtering");
55795580
}
55805581

5581-
if (ngx_qjs_string(ctx->engine, argv[0], &buffer) != NGX_OK) {
5582+
if (ngx_qjs_string(cx, argv[0], &buffer) != NGX_OK) {
55825583
return JS_ThrowTypeError(cx, "failed get buffer arg");
55835584
}
55845585

@@ -5735,7 +5736,7 @@ ngx_http_qjs_subrequest_done(ngx_http_request_t *r, void *data, ngx_int_t rc)
57355736
reply = JS_DupValue(cx, ngx_qjs_arg(sctx->args[0]));
57365737
}
57375738

5738-
rc = ngx_qjs_call((ngx_js_ctx_t *) ctx, event->function, &reply, 1);
5739+
rc = ngx_qjs_call(cx, event->function, &reply, 1);
57395740

57405741
JS_FreeValue(cx, reply);
57415742
ngx_js_del_event(ctx, event);
@@ -5786,7 +5787,7 @@ ngx_http_qjs_ext_subrequest(JSContext *cx, JSValueConst this_val,
57865787
"the primary request");
57875788
}
57885789

5789-
if (ngx_qjs_string(ctx->engine, argv[0], &uri) != NGX_OK) {
5790+
if (ngx_qjs_string(cx, argv[0], &uri) != NGX_OK) {
57905791
return JS_ThrowTypeError(cx, "failed to convert uri arg");
57915792
}
57925793

@@ -5812,7 +5813,7 @@ ngx_http_qjs_ext_subrequest(JSContext *cx, JSValueConst this_val,
58125813
arg = argv[1];
58135814

58145815
if (JS_IsString(arg)) {
5815-
if (ngx_qjs_string(ctx->engine, arg, &args) != NGX_OK) {
5816+
if (ngx_qjs_string(cx, arg, &args) != NGX_OK) {
58165817
return JS_ThrowTypeError(cx, "failed to convert args");
58175818
}
58185819

@@ -5833,7 +5834,7 @@ ngx_http_qjs_ext_subrequest(JSContext *cx, JSValueConst this_val,
58335834
}
58345835

58355836
if (!JS_IsUndefined(value)) {
5836-
rc = ngx_qjs_string(ctx->engine, value, &args);
5837+
rc = ngx_qjs_string(cx, value, &args);
58375838
JS_FreeValue(cx, value);
58385839

58395840
if (rc != NGX_OK) {
@@ -5857,7 +5858,7 @@ ngx_http_qjs_ext_subrequest(JSContext *cx, JSValueConst this_val,
58575858
}
58585859

58595860
if (!JS_IsUndefined(value)) {
5860-
rc = ngx_qjs_string(ctx->engine, value, &method_name);
5861+
rc = ngx_qjs_string(cx, value, &method_name);
58615862
JS_FreeValue(cx, value);
58625863

58635864
if (rc != NGX_OK) {
@@ -5884,7 +5885,7 @@ ngx_http_qjs_ext_subrequest(JSContext *cx, JSValueConst this_val,
58845885
}
58855886

58865887
if (!JS_IsUndefined(value)) {
5887-
rc = ngx_qjs_string(ctx->engine, value, &body_arg);
5888+
rc = ngx_qjs_string(cx, value, &body_arg);
58885889
JS_FreeValue(cx, value);
58895890

58905891
if (rc != NGX_OK) {
@@ -6233,7 +6234,6 @@ ngx_http_qjs_variables_set_property(JSContext *cx, JSValueConst obj,
62336234
u_char *lowcase_key;
62346235
ngx_str_t name, s;
62356236
ngx_uint_t key;
6236-
ngx_http_js_ctx_t *ctx;
62376237
ngx_http_request_t *r;
62386238
ngx_http_variable_t *v;
62396239
ngx_http_variable_value_t *vv;
@@ -6279,9 +6279,7 @@ ngx_http_qjs_variables_set_property(JSContext *cx, JSValueConst obj,
62796279
return -1;
62806280
}
62816281

6282-
ctx = ngx_http_get_module_ctx(r, ngx_http_js_module);
6283-
6284-
if (ngx_qjs_string(ctx->engine, value, &s) != NGX_OK) {
6282+
if (ngx_qjs_string(cx, value, &s) != NGX_OK) {
62856283
return -1;
62866284
}
62876285

@@ -6514,15 +6512,10 @@ ngx_http_qjs_header_generic(JSContext *cx, ngx_http_request_t *r,
65146512
pdesc->setter = JS_UNDEFINED;
65156513
pdesc->value = qjs_string_create_chb(cx, &chain);
65166514
if (JS_IsException(pdesc->value)) {
6517-
ret = -1;
6518-
goto done;
6515+
return -1;
65196516
}
65206517
}
65216518

6522-
done:
6523-
6524-
njs_chb_destroy(&chain);
6525-
65266519
return ret;
65276520
}
65286521

@@ -6692,15 +6685,14 @@ ngx_http_qjs_headers_out_handler(JSContext *cx, ngx_http_request_t *r,
66926685
ngx_str_t *name, JSPropertyDescriptor *pdesc, JSValue *value,
66936686
unsigned flags)
66946687
{
6695-
u_char *p;
6696-
int64_t length;
6697-
uint32_t i;
6698-
ngx_int_t rc;
6699-
ngx_str_t s;
6700-
JSValue v;
6701-
ngx_list_part_t *part;
6702-
ngx_table_elt_t *header, *h, **ph;
6703-
ngx_http_js_ctx_t *ctx;
6688+
u_char *p;
6689+
int64_t length;
6690+
uint32_t i;
6691+
ngx_int_t rc;
6692+
ngx_str_t s;
6693+
JSValue v;
6694+
ngx_list_part_t *part;
6695+
ngx_table_elt_t *header, *h, **ph;
67046696

67056697
if (flags & NJS_HEADER_GET) {
67066698
return ngx_http_qjs_header_generic(cx, r, &r->headers_out.headers, NULL,
@@ -6758,7 +6750,6 @@ ngx_http_qjs_headers_out_handler(JSContext *cx, ngx_http_request_t *r,
67586750
}
67596751

67606752
ph = &header;
6761-
ctx = ngx_http_get_module_ctx(r, ngx_http_js_module);
67626753

67636754
for (i = 0; i < (uint32_t) length; i++) {
67646755
if (JS_IsArray(cx, *value)) {
@@ -6768,7 +6759,7 @@ ngx_http_qjs_headers_out_handler(JSContext *cx, ngx_http_request_t *r,
67686759
}
67696760
}
67706761

6771-
rc = ngx_qjs_string(ctx->engine, v, &s);
6762+
rc = ngx_qjs_string(cx, v, &s);
67726763

67736764
if (JS_IsArray(cx, *value)) {
67746765
JS_FreeValue(cx, v);
@@ -6828,15 +6819,14 @@ ngx_http_qjs_headers_out_special_handler(JSContext *cx, ngx_http_request_t *r,
68286819
ngx_str_t *name, JSPropertyDescriptor *pdesc, JSValue *value,
68296820
unsigned flags, ngx_table_elt_t **hh)
68306821
{
6831-
u_char *p;
6832-
uint32_t length;
6833-
JSValue len, setval;
6834-
ngx_str_t s;
6835-
ngx_uint_t i, rc;
6836-
ngx_list_t *headers;
6837-
ngx_list_part_t *part;
6838-
ngx_table_elt_t *header, *h;
6839-
ngx_http_js_ctx_t *ctx;
6822+
u_char *p;
6823+
uint32_t length;
6824+
JSValue len, setval;
6825+
ngx_str_t s;
6826+
ngx_uint_t i, rc;
6827+
ngx_list_t *headers;
6828+
ngx_list_part_t *part;
6829+
ngx_table_elt_t *header, *h;
68406830

68416831
if (flags & NJS_HEADER_GET) {
68426832
return ngx_http_qjs_headers_out_handler(cx, r, name, pdesc, NULL,
@@ -6870,9 +6860,7 @@ ngx_http_qjs_headers_out_special_handler(JSContext *cx, ngx_http_request_t *r,
68706860
setval = JS_UNDEFINED;
68716861
}
68726862

6873-
ctx = ngx_http_get_module_ctx(r, ngx_http_js_module);
6874-
6875-
rc = ngx_qjs_string(ctx->engine, setval, &s);
6863+
rc = ngx_qjs_string(cx, setval, &s);
68766864

68776865
if (value != NULL && JS_IsArray(cx, *value)) {
68786866
JS_FreeValue(cx, setval);
@@ -7050,11 +7038,10 @@ ngx_http_qjs_headers_out_content_type(JSContext *cx, ngx_http_request_t *r,
70507038
ngx_str_t *name, JSPropertyDescriptor *pdesc, JSValue *value,
70517039
unsigned flags)
70527040
{
7053-
uint32_t length;
7041+
uint32_t length;
70547042
JSValue len, setval;
70557043
ngx_int_t rc;
70567044
ngx_str_t *hdr, s;
7057-
ngx_http_js_ctx_t *ctx;
70587045

70597046
if (flags & NJS_HEADER_GET) {
70607047
hdr = &r->headers_out.content_type;
@@ -7108,9 +7095,7 @@ ngx_http_qjs_headers_out_content_type(JSContext *cx, ngx_http_request_t *r,
71087095
setval = *value;
71097096
}
71107097

7111-
ctx = ngx_http_get_module_ctx(r, ngx_http_js_module);
7112-
7113-
rc = ngx_qjs_string(ctx->engine, setval, &s);
7098+
rc = ngx_qjs_string(cx, setval, &s);
71147099

71157100
if (JS_IsArray(cx, *value)) {
71167101
JS_FreeValue(cx, setval);

0 commit comments

Comments
 (0)