From 6b2fce430b4cdb063b5b4e5382515d384c90272b Mon Sep 17 00:00:00 2001 From: Igor Sysoev Date: Mon, 3 Dec 2007 10:05:19 +0000 Subject: [PATCH] allow to use IP addresses without defined resolver --- src/core/ngx_resolver.c | 28 +++++++++++++++++----------- src/core/ngx_resolver.h | 3 +++ src/http/ngx_http_core_module.c | 7 +++++++ src/http/ngx_http_upstream.c | 15 ++++++++------- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/core/ngx_resolver.c b/src/core/ngx_resolver.c index 045ded702..94d729c58 100644 --- a/src/core/ngx_resolver.c +++ b/src/core/ngx_resolver.c @@ -99,11 +99,6 @@ ngx_resolver_create(ngx_peer_addr_t *addr, ngx_log_t *log) return NULL; } - uc = ngx_calloc(sizeof(ngx_udp_connection_t), log); - if (uc == NULL) { - return NULL; - } - r->event = ngx_calloc(sizeof(ngx_event_t), log); if (r->event == NULL) { return NULL; @@ -138,8 +133,6 @@ ngx_resolver_create(ngx_peer_addr_t *addr, ngx_log_t *log) r->event->log = log; r->ident = -1; - r->udp_connection = uc; - r->resend_timeout = 5; r->expire = 30; r->valid = 300; @@ -147,10 +140,19 @@ ngx_resolver_create(ngx_peer_addr_t *addr, ngx_log_t *log) r->log = log; r->log_level = NGX_LOG_ALERT; - uc->sockaddr = addr->sockaddr; - uc->socklen = addr->socklen; - uc->server = addr->name; - uc->log = log; + if (addr) { + uc = ngx_calloc(sizeof(ngx_udp_connection_t), log); + if (uc == NULL) { + return NULL; + } + + r->udp_connection = uc; + + uc->sockaddr = addr->sockaddr; + uc->socklen = addr->socklen; + uc->server = addr->name; + uc->log = log; + } return r; } @@ -177,6 +179,10 @@ ngx_resolve_start(ngx_resolver_t *r, ngx_resolver_ctx_t *temp) } } + if (r->udp_connection == NULL) { + return NGX_NO_RESOLVER; + } + ctx = ngx_resolver_calloc(r, sizeof(ngx_resolver_ctx_t)); if (ctx) { diff --git a/src/core/ngx_resolver.h b/src/core/ngx_resolver.h index e69f42087..b1b8ede2c 100644 --- a/src/core/ngx_resolver.h +++ b/src/core/ngx_resolver.h @@ -26,6 +26,9 @@ #define NGX_RESOLVE_TIMEDOUT NGX_ETIMEDOUT +#define NGX_NO_RESOLVER (void *) -1 + + typedef struct { ngx_connection_t *connection; struct sockaddr *sockaddr; diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c index fea5cb5b5..b882f1c2f 100644 --- a/src/http/ngx_http_core_module.c +++ b/src/http/ngx_http_core_module.c @@ -2594,6 +2594,13 @@ ngx_http_core_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) if (conf->resolver == NULL) { conf->resolver = prev->resolver; + + if (conf->resolver == NULL) { + conf->resolver = ngx_resolver_create(NULL, cf->cycle->new_log); + if (conf->resolver == NULL) { + return NGX_OK; + } + } } ngx_conf_merge_path_value(conf->client_body_temp_path, diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c index 7901bb667..e51638b56 100644 --- a/src/http/ngx_http_upstream.c +++ b/src/http/ngx_http_upstream.c @@ -398,13 +398,6 @@ ngx_http_upstream_init(ngx_http_request_t *r) } } - if (clcf->resolver == NULL) { - ngx_log_error(NGX_LOG_ERR, c->log, 0, - "no resolver defined to resolve %V", host); - ngx_http_finalize_request(r, NGX_HTTP_BAD_GATEWAY); - return; - } - temp.name = *host; ctx = ngx_resolve_start(clcf->resolver, &temp); @@ -413,6 +406,14 @@ ngx_http_upstream_init(ngx_http_request_t *r) return; } + if (ctx == NGX_NO_RESOLVER) { + ngx_log_error(NGX_LOG_ERR, c->log, 0, + "no resolver defined to resolve %V", host); + + ngx_http_finalize_request(r, NGX_HTTP_BAD_GATEWAY); + return; + } + ctx->name = *host; ctx->type = NGX_RESOLVE_A; ctx->handler = ngx_http_upstream_resolve_handler;