Skip to content
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

Auxiliary changes for upcoming QuickJS patches. #764

Merged
merged 5 commits into from
Jul 19, 2024
Merged
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
185 changes: 98 additions & 87 deletions nginx/ngx_http_js_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,24 @@ typedef struct {


typedef struct {
ngx_http_request_t *request;
njs_opaque_value_t callbacks[2];
} ngx_http_js_cb_t;
ngx_str_t name;
unsigned flags;
uintptr_t handler;
} ngx_http_js_header_t;


typedef struct {
njs_str_t name;
#if defined(nginx_version) && (nginx_version >= 1023000)
unsigned flags;
njs_int_t (*handler)(njs_vm_t *vm, ngx_http_request_t *r,
unsigned flags, njs_str_t *name,
njs_value_t *setval, njs_value_t *retval);
#else
njs_int_t (*handler)(njs_vm_t *vm, ngx_http_request_t *r,
ngx_list_t *headers, njs_str_t *name,
njs_value_t *setval, njs_value_t *retval);
typedef njs_int_t (*njs_http_js_header_handler_t)(njs_vm_t *vm,
ngx_http_request_t *r, unsigned flags, njs_str_t *name, njs_value_t *setval,
njs_value_t *retval);
typedef njs_int_t (*njs_http_js_header_handler122_t)(njs_vm_t *vm,
ngx_http_request_t *r, ngx_list_t *headers, njs_str_t *name,
njs_value_t *setval, njs_value_t *retval);

#endif
} ngx_http_js_header_t;

typedef struct {
ngx_str_t name;
ngx_uint_t value;
} ngx_http_js_entry_t;


static ngx_int_t ngx_http_js_content_handler(ngx_http_request_t *r);
Expand Down Expand Up @@ -895,6 +894,25 @@ njs_module_t *njs_http_js_addon_modules[] = {
};


static ngx_http_js_entry_t ngx_http_methods[] = {
{ ngx_string("GET"), NGX_HTTP_GET },
{ ngx_string("POST"), NGX_HTTP_POST },
{ ngx_string("HEAD"), NGX_HTTP_HEAD },
{ ngx_string("OPTIONS"), NGX_HTTP_OPTIONS },
{ ngx_string("PROPFIND"), NGX_HTTP_PROPFIND },
{ ngx_string("PUT"), NGX_HTTP_PUT },
{ ngx_string("MKCOL"), NGX_HTTP_MKCOL },
{ ngx_string("DELETE"), NGX_HTTP_DELETE },
{ ngx_string("COPY"), NGX_HTTP_COPY },
{ ngx_string("MOVE"), NGX_HTTP_MOVE },
{ ngx_string("PROPPATCH"), NGX_HTTP_PROPPATCH },
{ ngx_string("LOCK"), NGX_HTTP_LOCK },
{ ngx_string("UNLOCK"), NGX_HTTP_UNLOCK },
{ ngx_string("PATCH"), NGX_HTTP_PATCH },
{ ngx_string("TRACE"), NGX_HTTP_TRACE },
};


static ngx_int_t
ngx_http_js_content_handler(ngx_http_request_t *r)
{
Expand Down Expand Up @@ -1613,33 +1631,41 @@ ngx_http_js_ext_header_out(njs_vm_t *vm, njs_object_prop_t *prop,

static ngx_http_js_header_t headers_out[] = {
#if defined(nginx_version) && (nginx_version < 1023000)
{ njs_str("Age"), ngx_http_js_header_single },
{ njs_str("Content-Type"), ngx_http_js_content_type122 },
{ njs_str("Content-Length"), ngx_http_js_content_length122 },
{ njs_str("Content-Encoding"), ngx_http_js_content_encoding122 },
{ njs_str("Date"), ngx_http_js_date122 },
{ njs_str("Etag"), ngx_http_js_header_single },
{ njs_str("Expires"), ngx_http_js_header_single },
{ njs_str("Last-Modified"), ngx_http_js_last_modified122 },
{ njs_str("Location"), ngx_http_js_location122 },
{ njs_str("Server"), ngx_http_js_server122 },
{ njs_str("Set-Cookie"), ngx_http_js_header_array },
{ njs_str("Retry-After"), ngx_http_js_header_single },
{ njs_str(""), ngx_http_js_header_generic },

#define header(name, h) { njs_str(name), 0, (uintptr_t) h }
header("Age", ngx_http_js_header_single),
header("Content-Type", ngx_http_js_content_type122),
header("Content-Length", ngx_http_js_content_length122),
header("Content-Encoding", ngx_http_js_content_encoding122),
header("Date", ngx_http_js_date122),
header("Etag", ngx_http_js_header_single),
header("Expires", ngx_http_js_header_single),
header("Last-Modified", ngx_http_js_last_modified122),
header("Location", ngx_http_js_location122),
header("Server", ngx_http_js_server122),
header("Set-Cookie", ngx_http_js_header_array),
header("Retry-After", ngx_http_js_header_single),
header("", ngx_http_js_header_generic),
#undef header

#else
{ njs_str("Age"), NJS_HEADER_SINGLE, ngx_http_js_header_out },
{ njs_str("Content-Encoding"), 0, ngx_http_js_content_encoding },
{ njs_str("Content-Length"), 0, ngx_http_js_content_length },
{ njs_str("Content-Type"), 0, ngx_http_js_content_type },
{ njs_str("Date"), 0, ngx_http_js_date },
{ njs_str("Etag"), NJS_HEADER_SINGLE, ngx_http_js_header_out },
{ njs_str("Expires"), NJS_HEADER_SINGLE, ngx_http_js_header_out },
{ njs_str("Last-Modified"), 0, ngx_http_js_last_modified },
{ njs_str("Location"), 0, ngx_http_js_location },
{ njs_str("Server"), 0, ngx_http_js_server },
{ njs_str("Set-Cookie"), NJS_HEADER_ARRAY, ngx_http_js_header_out },
{ njs_str("Retry-After"), NJS_HEADER_SINGLE, ngx_http_js_header_out },
{ njs_str(""), 0, ngx_http_js_header_out },

#define header(name, fl, h) { njs_str(name), fl, (uintptr_t) h }
header("Age", NJS_HEADER_SINGLE, ngx_http_js_header_out),
header("Content-Encoding", 0, ngx_http_js_content_encoding),
header("Content-Length", 0, ngx_http_js_content_length),
header("Content-Type", 0, ngx_http_js_content_type),
header("Date", 0, ngx_http_js_date),
header("Etag", NJS_HEADER_SINGLE, ngx_http_js_header_out),
header("Expires", NJS_HEADER_SINGLE, ngx_http_js_header_out),
header("Last-Modified", 0, ngx_http_js_last_modified),
header("Location", 0, ngx_http_js_location),
header("Server", 0, ngx_http_js_server),
header("Set-Cookie", NJS_HEADER_ARRAY, ngx_http_js_header_out),
header("Retry-After", NJS_HEADER_SINGLE, ngx_http_js_header_out),
header("", 0, ngx_http_js_header_out),
#undef header

#endif
};

Expand Down Expand Up @@ -1667,18 +1693,20 @@ ngx_http_js_ext_header_out(njs_vm_t *vm, njs_object_prop_t *prop,
" headers were already sent", &name);
}

for (h = headers_out; h->name.length > 0; h++) {
if (h->name.length == name.length
&& ngx_strncasecmp(h->name.start, name.start, name.length) == 0)
for (h = headers_out; h->name.len > 0; h++) {
if (h->name.len == name.length
&& ngx_strncasecmp(h->name.data, name.start, name.length) == 0)
{
break;
}
}

#if defined(nginx_version) && (nginx_version < 1023000)
return h->handler(vm, r, &r->headers_out.headers, &name, setval, retval);
return ((njs_http_js_header_handler122_t) h->handler)(vm, r,
&r->headers_out.headers, &name, setval, retval);
#else
return h->handler(vm, r, h->flags, &name, setval, retval);
return ((njs_http_js_header_handler_t) h->handler)(vm, r, h->flags, &name,
setval, retval);
#endif
}

Expand Down Expand Up @@ -2746,18 +2774,20 @@ ngx_http_js_ext_header_in(njs_vm_t *vm, njs_object_prop_t *prop,
ngx_http_js_header_t *h;

static ngx_http_js_header_t headers_in[] = {
{ njs_str("Content-Type"), ngx_http_js_header_single },
{ njs_str("Cookie"), ngx_http_js_header_cookie },
{ njs_str("ETag"), ngx_http_js_header_single },
{ njs_str("From"), ngx_http_js_header_single },
{ njs_str("Max-Forwards"), ngx_http_js_header_single },
{ njs_str("Referer"), ngx_http_js_header_single },
{ njs_str("Proxy-Authorization"), ngx_http_js_header_single },
{ njs_str("User-Agent"), ngx_http_js_header_single },
#define header(name, h) { njs_str(name), 0, (uintptr_t) h }
header("Content-Type", ngx_http_js_header_single),
header("Cookie", ngx_http_js_header_cookie),
header("ETag", ngx_http_js_header_single),
header("From", ngx_http_js_header_single),
header("Max-Forwards", ngx_http_js_header_single),
header("Referer", ngx_http_js_header_single),
header("Proxy-Authorization", ngx_http_js_header_single),
header("User-Agent", ngx_http_js_header_single),
#if (NGX_HTTP_X_FORWARDED_FOR)
{ njs_str("X-Forwarded-For"), ngx_http_js_header_x_forwarded_for },
header("X-Forwarded-For", ngx_http_js_header_x_forwarded_for),
#endif
{ njs_str(""), ngx_http_js_header_generic },
header("", ngx_http_js_header_generic),
#undef header
};

r = njs_vm_external(vm, ngx_http_js_request_proto_id, value);
Expand All @@ -2778,15 +2808,16 @@ ngx_http_js_ext_header_in(njs_vm_t *vm, njs_object_prop_t *prop,
return NJS_DECLINED;
}

for (h = headers_in; h->name.length > 0; h++) {
if (h->name.length == name.length
&& ngx_strncasecmp(h->name.start, name.start, name.length) == 0)
for (h = headers_in; h->name.len > 0; h++) {
if (h->name.len == name.length
&& ngx_strncasecmp(h->name.data, name.start, name.length) == 0)
{
break;
}
}

return h->handler(vm, r, &r->headers_in.headers, &name, setval, retval);
return ((njs_http_js_header_handler122_t) h->handler)(vm, r,
&r->headers_in.headers, &name, setval, retval);
}


Expand Down Expand Up @@ -3063,27 +3094,6 @@ ngx_http_js_ext_subrequest(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
ngx_http_request_body_t *rb;
ngx_http_post_subrequest_t *ps;

static const struct {
ngx_str_t name;
ngx_uint_t value;
} methods[] = {
{ ngx_string("GET"), NGX_HTTP_GET },
{ ngx_string("POST"), NGX_HTTP_POST },
{ ngx_string("HEAD"), NGX_HTTP_HEAD },
{ ngx_string("OPTIONS"), NGX_HTTP_OPTIONS },
{ ngx_string("PROPFIND"), NGX_HTTP_PROPFIND },
{ ngx_string("PUT"), NGX_HTTP_PUT },
{ ngx_string("MKCOL"), NGX_HTTP_MKCOL },
{ ngx_string("DELETE"), NGX_HTTP_DELETE },
{ ngx_string("COPY"), NGX_HTTP_COPY },
{ ngx_string("MOVE"), NGX_HTTP_MOVE },
{ ngx_string("PROPPATCH"), NGX_HTTP_PROPPATCH },
{ ngx_string("LOCK"), NGX_HTTP_LOCK },
{ ngx_string("UNLOCK"), NGX_HTTP_UNLOCK },
{ ngx_string("PATCH"), NGX_HTTP_PATCH },
{ ngx_string("TRACE"), NGX_HTTP_TRACE },
};

static const njs_str_t args_key = njs_str("args");
static const njs_str_t method_key = njs_str("method");
static const njs_str_t body_key = njs_str("body");
Expand Down Expand Up @@ -3118,7 +3128,7 @@ ngx_http_js_ext_subrequest(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
callback = NULL;

method = 0;
methods_max = sizeof(methods) / sizeof(methods[0]);
methods_max = sizeof(ngx_http_methods) / sizeof(ngx_http_methods[0]);

args_arg.length = 0;
args_arg.start = NULL;
Expand Down Expand Up @@ -3166,8 +3176,9 @@ ngx_http_js_ext_subrequest(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
}

while (method < methods_max) {
if (method_name.length == methods[method].name.len
&& ngx_memcmp(method_name.start, methods[method].name.data,
if (method_name.length == ngx_http_methods[method].name.len
&& ngx_memcmp(method_name.start,
ngx_http_methods[method].name.data,
method_name.length)
== 0)
{
Expand Down Expand Up @@ -3277,8 +3288,8 @@ ngx_http_js_ext_subrequest(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
}

if (method != methods_max) {
sr->method = methods[method].value;
sr->method_name = methods[method].name;
sr->method = ngx_http_methods[method].value;
sr->method_name = ngx_http_methods[method].name;

} else {
sr->method = NGX_HTTP_UNKNOWN;
Expand Down
9 changes: 5 additions & 4 deletions nginx/t/js_headers.t
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,12 @@ $t->write_file('test.js', <<EOF);
r.headersOut['Foo'] = 'xxx';
}

r.return(200, `B:\${njs.dump(r.headersOut.foo)}`);
r.return(200, `B:\${r.headersOut.foo}`);
}

function hdr_out_single(r) {
r.headersOut.ETag = ['a', 'b'];
r.return(200, `B:\${njs.dump(r.headersOut.etag)}`);
r.return(200, `B:\${r.headersOut.etag}`);
}

function hdr_out_set_cookie(r) {
Expand All @@ -403,7 +403,8 @@ $t->write_file('test.js', <<EOF);
r.headersOut['Set-Cookie'] = 'e';
r.headersOut['Set-Cookie'] = ['c', '', null, 'd', 'f'];

r.return(200, `B:\${njs.dump(r.headersOut['Set-Cookie'])}`);
var cookies = r.headersOut['Set-Cookie'];
r.return(200, `B:\${cookies} \${Array.isArray(cookies)}`);
}

function ihdr_out(r) {
Expand Down Expand Up @@ -519,7 +520,7 @@ like(http_get('/hdr_out_single'), qr/B:a/,
'r.headersOut single get');
like(http_get('/hdr_out_set_cookie'), qr/Set-Cookie: c\r\nSet-Cookie: d/,
'set_cookie');
like(http_get('/hdr_out_set_cookie'), qr/B:\['c','d','f']/,
like(http_get('/hdr_out_set_cookie'), qr/B:c,d,f true/,
'set_cookie2');
unlike(http_get('/hdr_out_set_cookie'), qr/Set-Cookie: [abe]/,
'set_cookie3');
Expand Down
14 changes: 8 additions & 6 deletions nginx/t/js_subrequests.t
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ $t->write_file('test.js', <<EOF);
}

function sr(r) {
subrequest_fn(r, ['/p/sub2'], ['uri', 'status'])
subrequest_fn(r, ['/p/sub2'], ['status'])
}

function sr_pr(r) {
Expand Down Expand Up @@ -386,11 +386,11 @@ $t->write_file('test.js', <<EOF);
}

function sr_unavail(req) {
subrequest_fn(req, ['/unavail'], ['uri', 'status']);
subrequest_fn(req, ['/unavail'], ['status']);
}

function sr_unavail_pr(req) {
subrequest_fn_pr(req, ['/unavail'], ['uri', 'status']);
subrequest_fn_pr(req, ['/unavail'], ['status']);
}

function sr_unsafe(r) {
Expand Down Expand Up @@ -455,18 +455,20 @@ $t->write_file('test.js', <<EOF);

function sr_out_of_order(r) {
subrequest_fn(r, ['/p/delayed', '/p/sub1', '/unknown'],
['uri', 'status']);
['status']);
}

function collect(replies, props, total, reply) {
reply.log(`subrequest handler: \${reply.uri} status: \${reply.status}`)

var rep = {};
props.push('uri');
props.forEach(p => {rep[p] = reply[p]});

replies.push(rep);

if (replies.length == total) {
replies.sort((a, b) => a.uri < b.uri ? -1 : 1);
reply.parent.return(200, JSON.stringify(replies));
}
}
Expand Down Expand Up @@ -546,9 +548,9 @@ is(get_json('/sr_js_in_subrequest'), '["JS-SUB"]', 'sr_js_in_subrequest');
is(get_json('/sr_unavail'), '[{"status":502,"uri":"/unavail"}]',
'sr_unavail');
is(get_json('/sr_out_of_order'),
'[{"status":404,"uri":"/unknown"},' .
'[{"status":200,"uri":"/p/delayed"},' .
'{"status":206,"uri":"/p/sub1"},' .
'{"status":200,"uri":"/p/delayed"}]',
'{"status":404,"uri":"/unknown"}]',
'sr_multi');

is(get_json('/sr_pr'), '{"h":"xxx"}', 'sr_promise');
Expand Down
4 changes: 2 additions & 2 deletions src/njs.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

#include <njs_auto_config.h>

#define NJS_VERSION "0.8.5"
#define NJS_VERSION_NUMBER 0x000805
#define NJS_VERSION "0.8.6"
#define NJS_VERSION_NUMBER 0x000806


#include <string.h>
Expand Down