Skip to content

Commit bed4356

Browse files
zltfzhuyin.zhu
andauthored
fix: httpc支持ipv6 (#2125)
* fix: httpc支持ipv6 * httpc ip:port解析放在c层 * lua-socket.c listen增加ip:port字符串解析, 相关cluster代码修改 #2124 * socketchannel changehost port可置空 * socketchannel changehost恢复port判空 * httpc解析域名加端口号逻辑优化 * 注释中字符串模式内容格式优化 --------- Co-authored-by: zhuyin.zhu <zhuyin.zhu@bytedance.com>
1 parent 1bbdeb3 commit bed4356

File tree

7 files changed

+41
-39
lines changed

7 files changed

+41
-39
lines changed

lualib-src/lua-socket.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,14 @@ lshutdown(lua_State *L) {
484484

485485
static int
486486
llisten(lua_State *L) {
487-
const char * host = luaL_checkstring(L,1);
488-
int port = luaL_checkinteger(L,2);
487+
size_t sz = 0;
488+
const char * addr = luaL_checklstring(L, 1, &sz);
489+
char tmp[sz];
490+
int port = 0;
491+
const char * host = address_port(L, tmp, addr, 2, &port);
492+
if (port == 0) {
493+
return luaL_error(L, "Invalid port");
494+
}
489495
int backlog = luaL_optinteger(L,3,BACKLOG);
490496
struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1));
491497
int id = skynet_socket_listen(ctx, host,port,backlog);

lualib/http/httpc.lua

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,29 @@ end
6868
local function connect(host, timeout)
6969
local protocol
7070
protocol, host = check_protocol(host)
71-
local hostaddr, port = host:match"([^:]+):?(%d*)$"
72-
if port == "" then
73-
port = protocol=="http" and 80 or protocol=="https" and 443
74-
else
75-
port = tonumber(port)
71+
72+
local hostname, port
73+
if async_dns then
74+
-- hostname string (ends with ":?%d*") must begin with a substring that doesn't contain colon "[^:]"
75+
-- and end with a character that is not a colon or a digit "[^%d%]:]".
76+
-- hostname not end with ".", pattern "%." can avoid splitting "127.0.0.1" into "127.0.0." and "1"
77+
hostname, port = host:match "^([^:]-[^%d%]:%.]):?(%d*)$"
78+
if hostname then
79+
local msg
80+
host, msg = dns.resolve(hostname)
81+
if not host then
82+
error(string.format("%s dns resolve failed msg:%s", hostname, msg))
83+
end
84+
end
7685
end
77-
local hostname
78-
if not hostaddr:match(".*%d+$") then
79-
hostname = hostaddr
80-
if async_dns then
81-
hostaddr = dns.resolve(hostname)
82-
end
86+
87+
if port == "" or (port == nil and not host:find ":%d+$") then
88+
port = protocol=="http" and 80 or protocol=="https" and 443
8389
end
84-
local fd = socket.connect(hostaddr, port, timeout)
90+
91+
local fd = socket.connect(host, port, timeout)
8592
if not fd then
86-
error(string.format("%s connect error host:%s, port:%s, timeout:%s", protocol, hostaddr, port, timeout))
93+
error(string.format("%s connect error host:%s, port:%s, timeout:%s", protocol, host, port, timeout))
8794
end
8895
-- print("protocol hostname port", protocol, hostname, port)
8996
local interface = gen_interface(protocol, fd, hostname)

lualib/skynet/socket.lua

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,6 @@ function socket.disconnected(id)
407407
end
408408

409409
function socket.listen(host, port, backlog)
410-
if port == nil then
411-
host, port = string.match(host, "(.+):([^:]+)$")
412-
host = host:match("^%[(.-)%]$") or host
413-
port = tonumber(port)
414-
end
415410
local id = driver.listen(host, port, backlog)
416411
local s = {
417412
id = id,

lualib/skynet/socketchannel.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ socket_channel.error = socket_error
2626
function socket_channel.channel(desc)
2727
local c = {
2828
__host = assert(desc.host),
29-
__port = assert(desc.port),
29+
__port = desc.port,
3030
__backup = desc.backup,
3131
__auth = desc.auth,
3232
__response = desc.response, -- It's for session mode
@@ -328,7 +328,7 @@ local function connect_once(self)
328328
self.__overload = true
329329
overload(true)
330330
else
331-
skynet.error(string.format("WARNING: %d K bytes need to send out (fd = %d %s:%s)", size, id, self.__host, self.__port))
331+
skynet.error(string.format("WARNING: %d K bytes need to send out (fd = %d)", size, id), self.__host, self.__port)
332332
end
333333
end
334334
end
@@ -464,7 +464,7 @@ local function block_connect(self, once)
464464

465465
r = check_connection(self)
466466
if r == nil then
467-
skynet.error(string.format("Connect to %s:%d failed (%s)", self.__host, self.__port, err))
467+
skynet.error("Connect failed", err, self.__host, self.__port)
468468
error(socket_error)
469469
else
470470
return r
@@ -553,9 +553,9 @@ end
553553

554554
function channel:changehost(host, port)
555555
self.__host = host
556-
if port then
556+
if port then
557557
self.__port = port
558-
end
558+
end
559559
if not self.__closed then
560560
close_channel_socket(self)
561561
end

lualib/snax/gateserver.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ function gateserver.start(handler)
3939
function CMD.open( source, conf )
4040
assert(not socket)
4141
local address = conf.address or "0.0.0.0"
42-
local port = assert(conf.port)
42+
local port = conf.port
4343
maxclient = conf.maxclient or 1024
4444
nodelay = conf.nodelay
45-
skynet.error(string.format("Listen on %s:%d", address, port))
45+
skynet.error("Listen on", address, port)
4646
socket = socketdriver.listen(address, port, conf.backlog)
4747
listen_context.co = coroutine.running()
4848
listen_context.fd = socket

service/clusterd.lua

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@ local function open_channel(t, key)
3939
end
4040
local succ, err, c
4141
if address then
42-
local host, port = string.match(address, "(.+):([^:]+)$")
43-
host = host:match("^%[(.-)%]$") or host
4442
c = node_sender[key]
4543
if c == nil then
46-
c = skynet.newservice("clustersender", key, nodename, host, port)
44+
c = skynet.newservice("clustersender", key, nodename, address)
4745
if node_sender[key] then
4846
-- double check
4947
skynet.kill(c)
@@ -53,14 +51,14 @@ local function open_channel(t, key)
5351
end
5452
end
5553

56-
succ = pcall(skynet.call, c, "lua", "changenode", host, port)
54+
succ = pcall(skynet.call, c, "lua", "changenode", address)
5755

5856
if succ then
5957
t[key] = c
6058
ct.channel = c
6159
node_sender_closed[key] = nil
6260
else
63-
err = string.format("changenode [%s] (%s:%s) failed", key, host, port)
61+
err = string.format("changenode [%s] (%s:%s) failed", key, address)
6462
end
6563
elseif address == false then
6664
c = node_sender[key]
@@ -147,11 +145,7 @@ function command.listen(source, addr, port, maxclient)
147145
local gate = skynet.newservice("gate")
148146
if port == nil then
149147
local address = assert(node_address[addr], addr .. " is down")
150-
addr, port = string.match(address, "(.+):([^:]+)$")
151-
addr = addr:match("^%[(.-)%]$") or addr
152-
port = tonumber(port)
153-
assert(port ~= 0)
154-
skynet.call(gate, "lua", "open", { address = addr, port = port, maxclient = maxclient })
148+
skynet.call(gate, "lua", "open", { address = address, port = port, maxclient = maxclient })
155149
skynet.ret(skynet.pack(addr, port))
156150
else
157151
local realaddr, realport = skynet.call(gate, "lua", "open", { address = addr, port = port, maxclient = maxclient })

service/clustersender.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ end
6060

6161
function command.changenode(host, port)
6262
if not host then
63-
skynet.error(string.format("Close cluster sender %s:%d", channel.__host, channel.__port))
63+
skynet.error("Close cluster sender", channel.__host, channel.__port)
6464
channel:close()
6565
else
66-
channel:changehost(host, tonumber(port))
66+
channel:changehost(host, port)
6767
channel:connect(true)
6868
end
6969
skynet.ret(skynet.pack(nil))

0 commit comments

Comments
 (0)