File: connection_common.lua

package info (click to toggle)
lua-http 0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,100 kB
  • sloc: makefile: 60; sh: 16
file content (110 lines) | stat: -rw-r--r-- 2,159 bytes parent folder | download | duplicates (2)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
local cqueues = require "cqueues"
local ca = require "cqueues.auxlib"
local ce = require "cqueues.errno"

local connection_methods = {}

local function onerror(socket, op, why, lvl) -- luacheck: ignore 212
	local err = string.format("%s: %s", op, ce.strerror(why))
	if op == "starttls" then
		local ssl = socket:checktls()
		if ssl and ssl.getVerifyResult then
			local code, msg = ssl:getVerifyResult()
			if code ~= 0 then
				err = err .. ":" .. msg
			end
		end
	end
	if why == ce.ETIMEDOUT then
		if op == "fill" or op == "read" then
			socket:clearerr("r")
		elseif op == "flush" then
			socket:clearerr("w")
		end
	end
	return err, why
end

function connection_methods:pollfd()
	if self.socket == nil then
		return nil
	end
	return self.socket:pollfd()
end

function connection_methods:events()
	if self.socket == nil then
		return nil
	end
	return self.socket:events()
end

function connection_methods:timeout()
	if self.socket == nil then
		return nil
	end
	return self.socket:timeout()
end

function connection_methods:onidle_() -- luacheck: ignore 212
end

function connection_methods:onidle(...)
	local old_handler = self.onidle_
	if select("#", ...) > 0 then
		self.onidle_ = ...
	end
	return old_handler
end

function connection_methods:connect(timeout)
	if self.socket == nil then
		return nil
	end
	local ok, err, errno = self.socket:connect(timeout)
	if not ok then
		return nil, err, errno
	end
	return true
end

function connection_methods:checktls()
	if self.socket == nil then
		return nil
	end
	return self.socket:checktls()
end

function connection_methods:localname()
	if self.socket == nil then
		return nil
	end
	return ca.fileresult(self.socket:localname())
end

function connection_methods:peername()
	if self.socket == nil then
		return nil
	end
	return ca.fileresult(self.socket:peername())
end

-- Primarily used for testing
function connection_methods:flush(timeout)
	return self.socket:flush("n", timeout)
end

function connection_methods:close()
	self:shutdown()
	if self.socket then
		cqueues.poll()
		cqueues.poll()
		self.socket:close()
	end
	return true
end

return {
	onerror = onerror;
	methods = connection_methods;
}