Add timeout to https module

Glocal attribute https.TIMEOUT controls connection tiemout.

Sample:
  https.TIMEOUT = 5  -- seconds
  https.request()
This commit is contained in:
Bruno Silvestre 2018-07-02 10:40:14 -03:00
parent 28e247dbc5
commit 953a363a59

View File

@ -21,6 +21,7 @@ local _M = {
_VERSION = "0.7", _VERSION = "0.7",
_COPYRIGHT = "LuaSec 0.7 - Copyright (C) 2009-2018 PUC-Rio", _COPYRIGHT = "LuaSec 0.7 - Copyright (C) 2009-2018 PUC-Rio",
PORT = 443, PORT = 443,
TIMEOUT = 60
} }
-- TLS configuration -- TLS configuration
@ -83,13 +84,14 @@ local function tcp(params)
conn.sock = try(socket.tcp()) conn.sock = try(socket.tcp())
local st = getmetatable(conn.sock).__index.settimeout local st = getmetatable(conn.sock).__index.settimeout
function conn:settimeout(...) function conn:settimeout(...)
return st(self.sock, ...) return st(self.sock, _M.TIMEOUT)
end end
-- Replace TCP's connection function -- Replace TCP's connection function
function conn:connect(host, port) function conn:connect(host, port)
try(self.sock:connect(host, port)) try(self.sock:connect(host, port))
self.sock = try(ssl.wrap(self.sock, params)) self.sock = try(ssl.wrap(self.sock, params))
self.sock:sni(host) self.sock:sni(host)
self.sock:settimeout(_M.TIMEOUT)
try(self.sock:dohandshake()) try(self.sock:dohandshake())
reg(self, getmetatable(self.sock)) reg(self, getmetatable(self.sock))
return 1 return 1