From 77f16307b5a3cdd22668ef44cd3cfe139c31ddd7 Mon Sep 17 00:00:00 2001 From: Sergey Kharkhardin Date: Thu, 14 Mar 2019 23:14:01 +0300 Subject: [PATCH] code review: fixed several review comments. --- lib/ngx/resolver.lua | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/ngx/resolver.lua b/lib/ngx/resolver.lua index 0442848af..19965c42a 100644 --- a/lib/ngx/resolver.lua +++ b/lib/ngx/resolver.lua @@ -2,7 +2,6 @@ local base = require "resty.core.base" local get_request = base.get_request local ffi = require "ffi" local C = ffi.C -local ffi_new = ffi.new local ffi_str = ffi.string local ffi_gc = ffi.gc local FFI_OK = base.FFI_OK @@ -33,7 +32,8 @@ typedef struct { unsigned ipv6:1; } ngx_http_lua_resolver_ctx_t; -int ngx_http_lua_ffi_resolve(ngx_http_lua_resolver_ctx_t *ctx, const char *hostname); +int ngx_http_lua_ffi_resolve(ngx_http_lua_resolver_ctx_t *ctx, + const char *hostname); void ngx_http_lua_ffi_resolver_destroy(ngx_http_lua_resolver_ctx_t *ctx); ]] @@ -47,18 +47,22 @@ local mt = { local Ctx = ffi.metatype("ngx_http_lua_resolver_ctx_t", mt) function _M.resolve(hostname, ipv4, ipv6) - assert(type(hostname) == "string", "hostname must be string") - assert(ipv4 == nil or type(ipv4) == "boolean", "ipv4 must be boolean or nil") - assert(ipv6 == nil or type(ipv6) == "boolean", "ipv6 must be boolean or nil") - local buf = get_string_buf(BUF_SIZE) local buf_size = get_size_ptr() buf_size[0] = BUF_SIZE - local ctx = Ctx({get_request(), buf, buf_size}) + local ctx = Ctx() + ctx.request = get_request() + ctx.buf = buf + ctx.buf_size = buf_size + + if ipv4 == nil or ipv4 then + ctx.ipv4 = 1 + end - ctx.ipv4 = ipv4 == nil and 1 or ipv4 - ctx.ipv6 = ipv6 == nil and 0 or ipv6 + if ipv6 then + ctx.ipv6 = 1 + end local rc = C.ngx_http_lua_ffi_resolve(ctx, hostname)