@@ -14,6 +14,7 @@ local str_byte = string.byte
14
14
local str_char = string.char
15
15
local ipairs = ipairs
16
16
local pairs = pairs
17
+ local unpack = unpack
17
18
local re_match = ngx .re .match
18
19
local type = type
19
20
local tab_insert = table.insert
@@ -56,6 +57,60 @@ local function choose_endpoint(self)
56
57
end
57
58
58
59
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
+
59
114
local function http_request_uri (self , http_cli , method , uri , body , headers , keepalive )
60
115
local endpoint , err = choose_endpoint (self )
61
116
if not endpoint then
@@ -70,7 +125,7 @@ local function http_request_uri(self, http_cli, method, uri, body, headers, keep
70
125
end
71
126
72
127
local res
73
- res , err = http_cli : request_uri ( full_uri , {
128
+ res , err = request_uri_via_unix_socket ( http_cli , full_uri , {
74
129
method = method ,
75
130
body = body ,
76
131
headers = headers ,
@@ -79,6 +134,7 @@ local function http_request_uri(self, http_cli, method, uri, body, headers, keep
79
134
ssl_cert_path = self .ssl_cert_path ,
80
135
ssl_key_path = self .ssl_key_path ,
81
136
ssl_server_name = self .sni ,
137
+ unix_socket_proxy = self .unix_socket_proxy ,
82
138
})
83
139
84
140
if err then
@@ -199,6 +255,7 @@ function _M.new(opts)
199
255
local serializer = opts .serializer
200
256
local extra_headers = opts .extra_headers
201
257
local sni = opts .sni
258
+ local unix_socket_proxy = opts .unix_socket_proxy
202
259
203
260
if not typeof .uint (timeout ) then
204
261
return nil , ' opts.timeout must be unsigned integer'
@@ -228,6 +285,10 @@ function _M.new(opts)
228
285
return nil , ' opts.password must be string or ignore'
229
286
end
230
287
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
+
231
292
local endpoints = {}
232
293
local http_hosts
233
294
if type (http_host ) == ' string' then -- signle node
@@ -243,12 +304,25 @@ function _M.new(opts)
243
304
return nil , " invalid http host: " .. host .. " , err: " .. (err or " not matched" )
244
305
end
245
306
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
+
246
319
tab_insert (endpoints , {
247
320
full_prefix = host .. utils .normalize (api_prefix ),
248
321
http_host = host ,
249
322
scheme = m [1 ],
250
323
host = m [2 ] or " 127.0.0.1" ,
251
- port = m [3 ] or " 2379" ,
324
+ address = addr ,
325
+ port = port ,
252
326
api_prefix = api_prefix ,
253
327
})
254
328
end
@@ -282,6 +356,7 @@ function _M.new(opts)
282
356
ssl_key_path = opts .ssl_key_path ,
283
357
extra_headers = extra_headers ,
284
358
sni = sni ,
359
+ unix_socket_proxy = unix_socket_proxy ,
285
360
},
286
361
mt )
287
362
end
@@ -538,7 +613,7 @@ local function http_request_chunk(self, http_cli)
538
613
local ok
539
614
ok , err = http_cli :connect ({
540
615
scheme = endpoint .scheme ,
541
- host = endpoint .host ,
616
+ host = endpoint .address ,
542
617
port = endpoint .port ,
543
618
ssl_verify = self .ssl_verify ,
544
619
ssl_cert_path = self .ssl_cert_path ,
@@ -625,6 +700,10 @@ local function request_chunk(self, method, path, opts, timeout)
625
700
return nil , err
626
701
end
627
702
703
+ if self .unix_socket_proxy then
704
+ headers [" Host" ] = endpoint .host
705
+ end
706
+
628
707
local res
629
708
res , err = http_cli :request ({
630
709
method = method ,
0 commit comments