diff --git a/http/client.lua b/http/client.lua index 5000d675..2eee8aa8 100644 --- a/http/client.lua +++ b/http/client.lua @@ -84,11 +84,30 @@ local function negotiate(s, options, timeout) end local function connect(options, timeout) + local bind = options.bind + if bind ~= nil then + assert(type(bind) == "string") + local bind_address, bind_port = bind:match("^(.-):(%d+)$") + if bind_address then + bind_port = tonumber(bind_port, 10) + else + bind_address = bind + end + local ipv6 = bind_address:match("^%[([:%x]+)%]$") + if ipv6 then + bind_address = ipv6 + end + bind = { + address = bind_address; + port = bind_port; + } + end local s, err, errno = ca.fileresult(cs.connect { family = options.family; host = options.host; port = options.port; path = options.path; + bind = bind; sendname = false; v6only = options.v6only; nodelay = true; diff --git a/http/request.lua b/http/request.lua index b4b4fd06..31a5460d 100644 --- a/http/request.lua +++ b/http/request.lua @@ -107,6 +107,7 @@ function request_methods:clone() return setmetatable({ host = self.host; port = self.port; + bind = self.bind; tls = self.tls; ctx = self.ctx; sendname = self.sendname; @@ -467,6 +468,7 @@ function request_methods:go(timeout) connection, err, errno = client.connect({ host = host; port = port; + bind = self.bind; tls = tls; ctx = self.ctx; sendname = self.sendname;