Repository navigation
Expand file tree
/
Copy pathclient.lua
More file actions
97 lines (89 loc) · 2.45 KB
/
Copy pathclient.lua
File metadata and controls
97 lines (89 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
local cs = require "cqueues.socket"
local ce = require "cqueues.errno"
local http_tls = require "http.tls"
local new_h1_connection = require "http.h1_connection".new
local new_h2_connection = require "http.h2_connection".new
local openssl_ctx = require "openssl.ssl.context"
-- Create a shared 'default' TLS contexts
local default_ctx = http_tls.new_client_context()
local default_h1_ctx
local default_h11_ctx
local default_h2_ctx = http_tls.new_client_context()
if http_tls.has_alpn then
default_ctx:setAlpnProtos({"h2", "http/1.1"})
default_h1_ctx = http_tls.new_client_context()
default_h11_ctx = http_tls.new_client_context()
default_h11_ctx:setAlpnProtos({"http/1.1"})
default_h2_ctx:setAlpnProtos({"h2"})
else
default_h1_ctx = default_ctx
default_h11_ctx = default_ctx
end
default_h2_ctx:setOptions(openssl_ctx.OP_NO_TLSv1 + openssl_ctx.OP_NO_TLSv1_1)
local function onerror(socket, op, why, lvl) -- luacheck: ignore 212
return string.format("%s: %s", op, ce.strerror(why)), why
end
local function negotiate(s, options, timeout)
s:onerror(onerror)
local tls = options.tls
local version = options.version
if tls then
if tls == true then
if version == nil then
tls = default_ctx
elseif version == 1 then
tls = default_h1_ctx
elseif version == 1.1 then
tls = default_h11_ctx
elseif version == 2 then
tls = default_h2_ctx
else
error("Unknown HTTP version: " .. tostring(version))
end
end
local ok, err, errno = s:starttls(tls, timeout)
if not ok then
return nil, err, errno
end
end
if version == nil then
if tls then
local ssl = s:checktls()
if http_tls.has_alpn and ssl:getAlpnSelected() == "h2" then
version = 2
else
version = 1.1
end
else
-- TODO: attempt upgrading http1 to http2
version = 1.1
end
end
if version < 2 then
return new_h1_connection(s, "client", version)
elseif version == 2 then
return new_h2_connection(s, "client", options.h2_settings)
else
error("Unknown HTTP version: " .. tostring(version))
end
end
local function connect(options, timeout)
-- TODO: https://github.com/wahern/cqueues/issues/124
local s, errno = cs.connect {
family = options.family;
host = options.host;
port = options.port;
path = options.path;
sendname = options.sendname;
v6only = options.v6only;
nodelay = true;
}
if s == nil then
return nil, ce.strerror(errno), errno
end
return negotiate(s, options, timeout)
end
return {
negotiate = negotiate;
connect = connect;
}