@@ -14,6 +14,7 @@ local str_byte = string.byte
1414local str_char = string.char
1515local ipairs = ipairs
1616local pairs = pairs
17+ local unpack = unpack
1718local re_match = ngx .re .match
1819local type = type
1920local tab_insert = table.insert
@@ -56,6 +57,60 @@ local function choose_endpoint(self)
5657end
5758
5859
60+ local function request_uri_via_unix_socket (self , uri , params )
61+ local parsed_uri , err = self :parse_uri (uri , false )
62+ if not parsed_uri then
63+ return nil , err
64+ end
65+
66+ local path , query
67+ params .scheme , params .host , params .port , path , query = unpack (parsed_uri )
68+ if params .unix_socket_proxy then
69+ if not params .headers then
70+ params .headers = {}
71+ end
72+
73+ params .headers [" Host" ] = params .host
74+ params .host = params .unix_socket_proxy
75+ params .port = nil
76+ end
77+
78+ params .path = params .path or path
79+ params .query = params .query or query
80+ params .ssl_server_name = params .ssl_server_name or params .host
81+
82+ local res
83+ res , err = self :connect (params )
84+ if not res then
85+ return nil , err
86+ end
87+
88+ res , err = self :request (params )
89+ if not res then
90+ self :close ()
91+ return nil , err
92+ end
93+
94+ local body
95+ body , err = res :read_body ()
96+ if not body then
97+ self :close ()
98+ return nil , err
99+ end
100+
101+ res .body = body
102+
103+ if params .keepalive == false then
104+ self :close ()
105+
106+ else
107+ self :set_keepalive (params .keepalive_timeout , params .keepalive_pool )
108+ end
109+
110+ return res , nil
111+ end
112+
113+
59114local function http_request_uri (self , http_cli , method , uri , body , headers , keepalive )
60115 local endpoint , err = choose_endpoint (self )
61116 if not endpoint then
@@ -70,7 +125,7 @@ local function http_request_uri(self, http_cli, method, uri, body, headers, keep
70125 end
71126
72127 local res
73- res , err = http_cli : request_uri ( full_uri , {
128+ res , err = request_uri_via_unix_socket ( http_cli , full_uri , {
74129 method = method ,
75130 body = body ,
76131 headers = headers ,
@@ -79,6 +134,7 @@ local function http_request_uri(self, http_cli, method, uri, body, headers, keep
79134 ssl_cert_path = self .ssl_cert_path ,
80135 ssl_key_path = self .ssl_key_path ,
81136 ssl_server_name = self .sni ,
137+ unix_socket_proxy = self .unix_socket_proxy ,
82138 })
83139
84140 if err then
@@ -199,6 +255,7 @@ function _M.new(opts)
199255 local serializer = opts .serializer
200256 local extra_headers = opts .extra_headers
201257 local sni = opts .sni
258+ local unix_socket_proxy = opts .unix_socket_proxy
202259
203260 if not typeof .uint (timeout ) then
204261 return nil , ' opts.timeout must be unsigned integer'
@@ -228,6 +285,10 @@ function _M.new(opts)
228285 return nil , ' opts.password must be string or ignore'
229286 end
230287
288+ if unix_socket_proxy and not typeof .string (unix_socket_proxy ) then
289+ return nil , ' opts.unix_socket_proxy must be string or ignore'
290+ end
291+
231292 local endpoints = {}
232293 local http_hosts
233294 if type (http_host ) == ' string' then -- signle node
@@ -243,12 +304,25 @@ function _M.new(opts)
243304 return nil , " invalid http host: " .. host .. " , err: " .. (err or " not matched" )
244305 end
245306
307+ local addr
308+ if unix_socket_proxy then
309+ addr = unix_socket_proxy
310+ else
311+ addr = m [2 ] or " 127.0.0.1"
312+ end
313+
314+ local port
315+ if not unix_socket_proxy then
316+ port = m [3 ] or " 2379"
317+ end
318+
246319 tab_insert (endpoints , {
247320 full_prefix = host .. utils .normalize (api_prefix ),
248321 http_host = host ,
249322 scheme = m [1 ],
250323 host = m [2 ] or " 127.0.0.1" ,
251- port = m [3 ] or " 2379" ,
324+ address = addr ,
325+ port = port ,
252326 api_prefix = api_prefix ,
253327 })
254328 end
@@ -282,6 +356,7 @@ function _M.new(opts)
282356 ssl_key_path = opts .ssl_key_path ,
283357 extra_headers = extra_headers ,
284358 sni = sni ,
359+ unix_socket_proxy = unix_socket_proxy ,
285360 },
286361 mt )
287362end
@@ -538,7 +613,7 @@ local function http_request_chunk(self, http_cli)
538613 local ok
539614 ok , err = http_cli :connect ({
540615 scheme = endpoint .scheme ,
541- host = endpoint .host ,
616+ host = endpoint .address ,
542617 port = endpoint .port ,
543618 ssl_verify = self .ssl_verify ,
544619 ssl_cert_path = self .ssl_cert_path ,
@@ -625,6 +700,10 @@ local function request_chunk(self, method, path, opts, timeout)
625700 return nil , err
626701 end
627702
703+ if self .unix_socket_proxy then
704+ headers [" Host" ] = endpoint .host
705+ end
706+
628707 local res
629708 res , err = http_cli :request ({
630709 method = method ,
0 commit comments