Skip to content

Commit 0717a45

Browse files
committed
Merge branch 'pick-local-port'
2 parents 1c82ccc + 426611c commit 0717a45

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

doc/modules/http.client.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ This function returns a new connection to an HTTP server. Once a connection has
3737
e.g. `"80"` or `80`
3838
- `path` (string): path to connect to (UNIX sockets)
3939
- `v6only` (boolean, optional): if the `IPV6_V6ONLY` flag should be set on the underlying socket.
40+
- `bind` (string, optional): the local outgoing address and optionally port to bind in the form of `"address[:port]"`, IPv6 addresses may be specified via square bracket notation. e.g. `"127.0.0.1"`, `"127.0.0.1:50000"`, `"[::1]:30000"`.
4041
- `timeout` (optional) is the maximum amount of time (in seconds) to allow for connection to be established.
4142
This includes time for DNS lookup, connection, TLS negotiation (if TLS enabled) and in the case of HTTP 2: settings exchange.
4243

doc/modules/http.request.md

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ The host this request should be sent to.
2222
The port this request should be sent to.
2323

2424

25+
### `request.bind` <!-- --> {#http.request.bind}
26+
27+
The local outgoing address and optionally port to bind in the form of `"address[:port]"`. Default is to allow the kernel to choose an address+port.
28+
29+
IPv6 addresses may be specified via square bracket notation. e.g. `"127.0.0.1"`, `"127.0.0.1:50000"`, `"[::1]:30000"`.
30+
31+
This option is rarely needed. Supplying an address can be used to manually select the network interface to make the request from, while supplying a port is only really used to interoperate with firewalls or devices that demand use of a certain port.
32+
33+
2534
### `request.tls` <!-- --> {#http.request.tls}
2635

2736
A boolean indicating if TLS should be used.

http/client.lua

+19
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,30 @@ local function negotiate(s, options, timeout)
8484
end
8585

8686
local function connect(options, timeout)
87+
local bind = options.bind
88+
if bind ~= nil then
89+
assert(type(bind) == "string")
90+
local bind_address, bind_port = bind:match("^(.-):(%d+)$")
91+
if bind_address then
92+
bind_port = tonumber(bind_port, 10)
93+
else
94+
bind_address = bind
95+
end
96+
local ipv6 = bind_address:match("^%[([:%x]+)%]$")
97+
if ipv6 then
98+
bind_address = ipv6
99+
end
100+
bind = {
101+
address = bind_address;
102+
port = bind_port;
103+
}
104+
end
87105
local s, err, errno = ca.fileresult(cs.connect {
88106
family = options.family;
89107
host = options.host;
90108
port = options.port;
91109
path = options.path;
110+
bind = bind;
92111
sendname = false;
93112
v6only = options.v6only;
94113
nodelay = true;

http/request.lua

+2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ function request_methods:clone()
107107
return setmetatable({
108108
host = self.host;
109109
port = self.port;
110+
bind = self.bind;
110111
tls = self.tls;
111112
ctx = self.ctx;
112113
sendname = self.sendname;
@@ -467,6 +468,7 @@ function request_methods:go(timeout)
467468
connection, err, errno = client.connect({
468469
host = host;
469470
port = port;
471+
bind = self.bind;
470472
tls = tls;
471473
ctx = self.ctx;
472474
sendname = self.sendname;

0 commit comments

Comments
 (0)