Skip to content

Commit dbf556f

Browse files
committed
Fetch: refactored out ngx_js_http_resolve().
1 parent 78b55af commit dbf556f

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

Diff for: nginx/ngx_js_fetch.c

+41-18
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ static ngx_js_http_t *ngx_js_http_alloc(njs_vm_t *vm, ngx_pool_t *pool,
166166
static void ngx_js_http_resolve_done(ngx_js_http_t *http);
167167
static void ngx_js_http_close_peer(ngx_js_http_t *http);
168168
static void ngx_js_http_destructor(ngx_js_event_t *event);
169-
static void ngx_js_resolve_handler(ngx_resolver_ctx_t *ctx);
169+
static ngx_resolver_ctx_t *ngx_js_http_resolve(ngx_js_http_t *http,
170+
ngx_resolver_t *r, ngx_str_t *host, in_port_t port, ngx_msec_t timeout);
171+
static void ngx_js_http_resolve_handler(ngx_resolver_ctx_t *ctx);
170172
static njs_int_t ngx_js_fetch_promissified_result(njs_vm_t *vm,
171173
njs_value_t *result, njs_int_t rc, njs_value_t *retval);
172174
static void ngx_js_http_fetch_done(ngx_js_http_t *http,
@@ -832,7 +834,9 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
832834
}
833835

834836
if (u.addrs == NULL) {
835-
ctx = ngx_resolve_start(ngx_external_resolver(vm, external), NULL);
837+
ctx = ngx_js_http_resolve(http, ngx_external_resolver(vm, external),
838+
&u.host, u.port,
839+
ngx_external_resolver_timeout(vm, external));
836840
if (ctx == NULL) {
837841
njs_vm_memory_error(vm);
838842
return NJS_ERROR;
@@ -843,21 +847,6 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
843847
goto fail;
844848
}
845849

846-
http->ctx = ctx;
847-
http->port = u.port;
848-
849-
ctx->name = u.host;
850-
ctx->handler = ngx_js_resolve_handler;
851-
ctx->data = http;
852-
ctx->timeout = ngx_external_resolver_timeout(vm, external);
853-
854-
ret = ngx_resolve_name(http->ctx);
855-
if (ret != NGX_OK) {
856-
http->ctx = NULL;
857-
njs_vm_memory_error(vm);
858-
return NJS_ERROR;
859-
}
860-
861850
njs_value_assign(retval, njs_value_arg(&http->promise));
862851

863852
return NJS_OK;
@@ -1341,8 +1330,42 @@ ngx_js_http_error(ngx_js_http_t *http, const char *fmt, ...)
13411330
}
13421331

13431332

1333+
static ngx_resolver_ctx_t *
1334+
ngx_js_http_resolve(ngx_js_http_t *http, ngx_resolver_t *r, ngx_str_t *host,
1335+
in_port_t port, ngx_msec_t timeout)
1336+
{
1337+
ngx_int_t ret;
1338+
ngx_resolver_ctx_t *ctx;
1339+
1340+
ctx = ngx_resolve_start(r, NULL);
1341+
if (ctx == NULL) {
1342+
return NULL;
1343+
}
1344+
1345+
if (ctx == NGX_NO_RESOLVER) {
1346+
return ctx;
1347+
}
1348+
1349+
http->ctx = ctx;
1350+
http->port = port;
1351+
1352+
ctx->name = *host;
1353+
ctx->handler = ngx_js_http_resolve_handler;
1354+
ctx->data = http;
1355+
ctx->timeout = timeout;
1356+
1357+
ret = ngx_resolve_name(ctx);
1358+
if (ret != NGX_OK) {
1359+
http->ctx = NULL;
1360+
return NULL;
1361+
}
1362+
1363+
return ctx;
1364+
}
1365+
1366+
13441367
static void
1345-
ngx_js_resolve_handler(ngx_resolver_ctx_t *ctx)
1368+
ngx_js_http_resolve_handler(ngx_resolver_ctx_t *ctx)
13461369
{
13471370
u_char *p;
13481371
size_t len;

0 commit comments

Comments
 (0)