mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
rewrite httpc connect, see #2125
This commit is contained in:
@@ -19,19 +19,10 @@ function httpc.dns(server,port)
|
|||||||
dns.server(server,port)
|
dns.server(server,port)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function check_protocol(host)
|
local function check_protocol(host)
|
||||||
local protocol = host:match("^[Hh][Tt][Tt][Pp][Ss]?://")
|
local protocol, hostname = host:match "^(%a+)://(.*)"
|
||||||
if protocol then
|
if protocol then
|
||||||
host = string.gsub(host, "^"..protocol, "")
|
return string.lower(protocol), hostname
|
||||||
protocol = string.lower(protocol)
|
|
||||||
if protocol == "https://" then
|
|
||||||
return "https", host
|
|
||||||
elseif protocol == "http://" then
|
|
||||||
return "http", host
|
|
||||||
else
|
|
||||||
error(string.format("Invalid protocol: %s", protocol))
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
return "http", host
|
return "http", host
|
||||||
end
|
end
|
||||||
@@ -65,16 +56,30 @@ local function gen_interface(protocol, fd, hostname)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function connect(host, timeout)
|
local default_port = {
|
||||||
local protocol
|
http = 80,
|
||||||
protocol, host = check_protocol(host)
|
https = 443,
|
||||||
|
}
|
||||||
|
|
||||||
local hostname, port
|
local function connect(host, timeout)
|
||||||
|
local protocol, port
|
||||||
|
protocol, host = check_protocol(host)
|
||||||
|
if not host:find ":%d+$" then
|
||||||
|
port = default_port[protocol] or error("Invalid protocol: " .. protocol)
|
||||||
|
end
|
||||||
if async_dns then
|
if async_dns then
|
||||||
-- hostname string (ends with ":?%d*") must begin with a substring that doesn't contain colon "[^:]"
|
local hostname
|
||||||
-- and end with a character that is not a colon or a digit "[^%d%]:]".
|
if port then -- without :%d+$
|
||||||
-- hostname not end with ".", pattern "%." can avoid splitting "127.0.0.1" into "127.0.0." and "1"
|
-- if ipv6, contains colons
|
||||||
hostname, port = host:match "^([^:]-[^%d%]:%.]):?(%d*)$"
|
-- if ipv4, end with a digit
|
||||||
|
if host:find "^[^:]-[^%d]$" then
|
||||||
|
hostname = host
|
||||||
|
end
|
||||||
|
else -- with :%d+$ (port)
|
||||||
|
-- hostname string (ends with ":%d+") must begin with a substring that doesn't contain colon "[^:]"
|
||||||
|
-- and end with a character that is not a colon or a digit "[^%d%]]".
|
||||||
|
hostname, port = host:match "^([^:]-[^%d%]]):(%d+)$"
|
||||||
|
end
|
||||||
if hostname then
|
if hostname then
|
||||||
local msg
|
local msg
|
||||||
host, msg = dns.resolve(hostname)
|
host, msg = dns.resolve(hostname)
|
||||||
@@ -84,15 +89,10 @@ local function connect(host, timeout)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if port == "" or (port == nil and not host:find ":%d+$") then
|
|
||||||
port = protocol=="http" and 80 or protocol=="https" and 443
|
|
||||||
end
|
|
||||||
|
|
||||||
local fd = socket.connect(host, port, timeout)
|
local fd = socket.connect(host, port, timeout)
|
||||||
if not fd then
|
if not fd then
|
||||||
error(string.format("%s connect error host:%s, port:%s, timeout:%s", protocol, host, port, timeout))
|
error(string.format("%s connect error host:%s, port:%s, timeout:%s", protocol, host, port, timeout))
|
||||||
end
|
end
|
||||||
-- print("protocol hostname port", protocol, hostname, port)
|
|
||||||
local interface = gen_interface(protocol, fd, hostname)
|
local interface = gen_interface(protocol, fd, hostname)
|
||||||
if timeout then
|
if timeout then
|
||||||
skynet.timeout(timeout, function()
|
skynet.timeout(timeout, function()
|
||||||
|
|||||||
Reference in New Issue
Block a user