resolv.conf: fixed a bug that when a newline character is present at the end of the resolv.conf file, the parser incorrectly included such newline in the parsed address.

Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
This commit is contained in:
Datong Sun 2018-03-29 16:57:17 -07:00 committed by Yichun Zhang (agentzh)
parent 395e039e8e
commit ff89bf3ea1
2 changed files with 48 additions and 4 deletions

View File

@ -1,5 +1,5 @@
diff --git a/src/core/ngx_resolver.c b/src/core/ngx_resolver.c
index cd55520c..d8dc49e8 100644
index cd55520c..df55a484 100644
--- a/src/core/ngx_resolver.c
+++ b/src/core/ngx_resolver.c
@@ -5,6 +5,7 @@
@ -25,7 +25,7 @@ index cd55520c..d8dc49e8 100644
typedef struct {
u_char ident_hi;
@@ -131,6 +140,182 @@ static ngx_resolver_node_t *ngx_resolver_lookup_addr6(ngx_resolver_t *r,
@@ -131,6 +140,189 @@ static ngx_resolver_node_t *ngx_resolver_lookup_addr6(ngx_resolver_t *r,
#endif
@ -127,7 +127,14 @@ index cd55520c..d8dc49e8 100644
+ ngx_memzero(&u, sizeof(ngx_url_t));
+
+ u.url.data = buf + address;
+ u.url.len = (i == n - 1) ? n - address : i - address;
+
+ if (i == n - 1 && buf[i] != CR && buf[i] != LF) {
+ u.url.len = n - address;
+
+ } else {
+ u.url.len = i - address;
+ }
+
+ u.default_port = 53;
+
+ /* IPv6? */
@ -208,7 +215,7 @@ index cd55520c..d8dc49e8 100644
ngx_resolver_t *
ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n)
{
@@ -246,6 +431,37 @@ ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n)
@@ -246,6 +438,37 @@ ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n)
}
#endif

View File

@ -343,3 +343,40 @@ done
--- error_log
IPv6 resolver address is too long: "2001:4860:4860::8888:2001:4860:4860::8888:2001"
unable to parse local resolver
=== TEST 11: new line at the end of the file
--- config
resolver local=../html/resolv.conf ipv6=off;
resolver_timeout 5s;
location /t {
content_by_lua_block {
local sock = ngx.socket.tcp()
local ok, err = sock:connect("openresty.org", 80)
if not ok then
ngx.say("failed to connect to openresty.org: ", err)
return
end
ngx.say("successfully connected to openresty.org")
sock:close()
}
}
--- user_files eval
">>> resolv.conf
domain example.com
nameserver 8.8.8.8
nameserver 8.8.4.4
"
--- request
GET /t
--- response_body
successfully connected to openresty.org
--- no_error_log
[error]
[crit]
--- grep_error_log eval: qr/parsed a resolver: ".+"/
--- grep_error_log_out
parsed a resolver: "8.8.8.8"
parsed a resolver: "8.8.4.4"