@@ -19,19 +19,10 @@ function httpc.dns(server,port)
1919 dns .server (server ,port )
2020end
2121
22-
2322local function check_protocol (host )
24- local protocol = host :match ( " ^[Hh][Tt][Tt][Pp][Ss]? ://" )
23+ local protocol , hostname = host :match " ^(%a+) ://(.*) "
2524 if protocol then
26- host = string.gsub (host , " ^" .. protocol , " " )
27- protocol = string.lower (protocol )
28- if protocol == " https://" then
29- return " https" , host
30- elseif protocol == " http://" then
31- return " http" , host
32- else
33- error (string.format (" Invalid protocol: %s" , protocol ))
34- end
25+ return string.lower (protocol ), hostname
3526 else
3627 return " http" , host
3728 end
@@ -65,16 +56,30 @@ local function gen_interface(protocol, fd, hostname)
6556 end
6657end
6758
59+ local default_port = {
60+ http = 80 ,
61+ https = 443 ,
62+ }
63+
6864local function connect (host , timeout )
69- local protocol
65+ local protocol , port
7066 protocol , host = check_protocol (host )
71-
72- local hostname , port
67+ if not host :find " :%d+$" then
68+ port = default_port [protocol ] or error (" Invalid protocol: " .. protocol )
69+ end
7370 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*)$"
71+ local hostname
72+ if port then -- without :%d+$
73+ -- if ipv6, contains colons
74+ -- if ipv4, end with a digit
75+ if host :find " ^[^:]-[^%d]$" then
76+ hostname = host
77+ end
78+ else -- with :%d+$ (port)
79+ -- hostname string (ends with ":%d+") must begin with a substring that doesn't contain colon "[^:]"
80+ -- and end with a character that is not a colon or a digit "[^%d%]]".
81+ hostname , port = host :match " ^([^:]-[^%d%]]):(%d+)$"
82+ end
7883 if hostname then
7984 local msg
8085 host , msg = dns .resolve (hostname )
@@ -84,15 +89,10 @@ local function connect(host, timeout)
8489 end
8590 end
8691
87- if port == " " or (port == nil and not host :find " :%d+$" ) then
88- port = protocol == " http" and 80 or protocol == " https" and 443
89- end
90-
9192 local fd = socket .connect (host , port , timeout )
9293 if not fd then
9394 error (string.format (" %s connect error host:%s, port:%s, timeout:%s" , protocol , host , port , timeout ))
9495 end
95- -- print("protocol hostname port", protocol, hostname, port)
9696 local interface = gen_interface (protocol , fd , hostname )
9797 if timeout then
9898 skynet .timeout (timeout , function ()
0 commit comments