aboutsummaryrefslogtreecommitdiff
path: root/vendor/luasec/src/https.lua
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/luasec/src/https.lua')
-rw-r--r--vendor/luasec/src/https.lua147
1 files changed, 147 insertions, 0 deletions
diff --git a/vendor/luasec/src/https.lua b/vendor/luasec/src/https.lua
new file mode 100644
index 00000000..85cef361
--- /dev/null
+++ b/vendor/luasec/src/https.lua
@@ -0,0 +1,147 @@
1----------------------------------------------------------------------------
2-- LuaSec 1.3.2
3--
4-- Copyright (C) 2009-2023 PUC-Rio
5--
6-- Author: Pablo Musa
7-- Author: Tomas Guisasola
8---------------------------------------------------------------------------
9
10local socket = require("socket")
11local ssl = require("ssl")
12local ltn12 = require("ltn12")
13local http = require("socket.http")
14local url = require("socket.url")
15
16local try = socket.try
17
18--
19-- Module
20--
21local _M = {
22 _VERSION = "1.3.2",
23 _COPYRIGHT = "LuaSec 1.3.2 - Copyright (C) 2009-2023 PUC-Rio",
24 PORT = 443,
25 TIMEOUT = 60
26}
27
28-- TLS configuration
29local cfg = {
30 protocol = "any",
31 options = {"all", "no_sslv2", "no_sslv3", "no_tlsv1"},
32 verify = "none",
33}
34
35--------------------------------------------------------------------
36-- Auxiliar Functions
37--------------------------------------------------------------------
38
39-- Insert default HTTPS port.
40local function default_https_port(u)
41 return url.build(url.parse(u, {port = _M.PORT}))
42end
43
44-- Convert an URL to a table according to Luasocket needs.
45local function urlstring_totable(url, body, result_table)
46 url = {
47 url = default_https_port(url),
48 method = body and "POST" or "GET",
49 sink = ltn12.sink.table(result_table)
50 }
51 if body then
52 url.source = ltn12.source.string(body)
53 url.headers = {
54 ["content-length"] = #body,
55 ["content-type"] = "application/x-www-form-urlencoded",
56 }
57 end
58 return url
59end
60
61-- Forward calls to the real connection object.
62local function reg(conn)
63 local mt = getmetatable(conn.sock).__index
64 for name, method in pairs(mt) do
65 if type(method) == "function" then
66 conn[name] = function (self, ...)
67 return method(self.sock, ...)
68 end
69 end
70 end
71end
72
73-- Return a function which performs the SSL/TLS connection.
74local function tcp(params)
75 params = params or {}
76 -- Default settings
77 for k, v in pairs(cfg) do
78 params[k] = params[k] or v
79 end
80 -- Force client mode
81 params.mode = "client"
82 -- 'create' function for LuaSocket
83 return function ()
84 local conn = {}
85 conn.sock = try(socket.tcp())
86 local st = getmetatable(conn.sock).__index.settimeout
87 function conn:settimeout(...)
88 return st(self.sock, _M.TIMEOUT)
89 end
90 -- Replace TCP's connection function
91 function conn:connect(host, port)
92 try(self.sock:connect(host, port))
93 self.sock = try(ssl.wrap(self.sock, params))
94 self.sock:sni(host)
95 self.sock:settimeout(_M.TIMEOUT)
96 try(self.sock:dohandshake())
97 reg(self)
98 return 1
99 end
100 return conn
101 end
102end
103
104--------------------------------------------------------------------
105-- Main Function
106--------------------------------------------------------------------
107
108-- Make a HTTP request over secure connection. This function receives
109-- the same parameters of LuaSocket's HTTP module (except 'proxy' and
110-- 'redirect') plus LuaSec parameters.
111--
112-- @param url mandatory (string or table)
113-- @param body optional (string)
114-- @return (string if url == string or 1), code, headers, status
115--
116local function request(url, body)
117 local result_table = {}
118 local stringrequest = type(url) == "string"
119 if stringrequest then
120 url = urlstring_totable(url, body, result_table)
121 else
122 url.url = default_https_port(url.url)
123 end
124 if http.PROXY or url.proxy then
125 return nil, "proxy not supported"
126 elseif url.redirect then
127 return nil, "redirect not supported"
128 elseif url.create then
129 return nil, "create function not permitted"
130 end
131 -- New 'create' function to establish a secure connection
132 url.create = tcp(url)
133 local res, code, headers, status = http.request(url)
134 if res and stringrequest then
135 return table.concat(result_table), code, headers, status
136 end
137 return res, code, headers, status
138end
139
140--------------------------------------------------------------------------------
141-- Export module
142--
143
144_M.request = request
145_M.tcp = tcp
146
147return _M