Debian Codegraph @

lualib/lua_tcp_sync.lua

raw
1 local rspamd_tcp = require "rspamd_tcp"
2 local lua_util = require "lua_util"
3
4 local exports = {}
5 local N = 'tcp_sync'
6
7 local tcp_sync = { _conn = nil, _data = '', _eof = false, _addr = '' }
8 local metatable = {
9 __tostring = function(self)
10 return "class {tcp_sync connect to: " .. self._addr .. "}"
11 end
12 }
13
14 function tcp_sync.new(connection)
15 local self = {}
16
17 for name, method in pairs(tcp_sync) do
18 if name ~= 'new' then
19 self[name] = method
20 end
21 end
22
23 self._conn = connection
24
25 setmetatable(self, metatable)
26
27 return self
28 end
29
30 --[[[
31 -- @method tcp_sync.read_once()
32 --
33 -- Acts exactly like low-level tcp_sync.read_once()
34 -- the only exception is that if there is some pending data,
35 -- it's returned immediately and no underlying call is performed
36 --
37 -- @return
38 -- true, {data} if everything is fine
39 -- false, {error message} otherwise
40 --
41 --]]
42 function tcp_sync:read_once()
43 local is_ok, data
44 if self._data:len() > 0 then
45 data = self._data
46 self._data = nil
47 return true, data
48 end
49
50 is_ok, data = self._conn:read_once()
51
52 return is_ok, data
53 end
54
55 --[[[
56 -- @method tcp_sync.read_until(pattern)
57 --
58 -- Reads data from the connection until pattern is found
59 -- returns all bytes before the pattern
60 --
61 -- @param {pattern} Read data until pattern is found
62 -- @return
63 -- true, {data} if everything is fine
64 -- false, {error message} otherwise
65 -- @example
66 --
67 --]]
68 function tcp_sync:read_until(pattern)
69 repeat
70 local pos_start, pos_end = self._data:find(pattern, 1, true)
71 if pos_start then
72 local data = self._data:sub(1, pos_start - 1)
73 self._data = self._data:sub(pos_end + 1)
74 return true, data
75 end
76
77 local is_ok, more_data = self._conn:read_once()
78 if not is_ok then
79 return is_ok, more_data
80 end
81
82 self._data = self._data .. more_data
83 until false
84 end
85
86 --[[[
87 -- @method tcp_sync.read_bytes(n)
88 --
89 -- Reads {n} bytes from the stream
90 --
91 -- @param {n} Number of bytes to read
92 -- @return
93 -- true, {data} if everything is fine
94 -- false, {error message} otherwise
95 --
96 --]]
97 function tcp_sync:read_bytes(n)
98 repeat
99 if self._data:len() >= n then
100 local data = self._data:sub(1, n)
101 self._data = self._data:sub(n + 1)
102 return true, data
103 end
104
105 local is_ok, more_data = self._conn:read_once()
106 if not is_ok then
107 return is_ok, more_data
108 end
109
110 self._data = self._data .. more_data
111 until false
112 end
113
114 --[[[
115 -- @method tcp_sync.read_until_eof(n)
116 --
117 -- Reads stream until EOF is reached
118 --
119 -- @return
120 -- true, {data} if everything is fine
121 -- false, {error message} otherwise
122 --
123 --]]
124 function tcp_sync:read_until_eof()
125 while not self:eof() do
126 local is_ok, more_data = self._conn:read_once()
127 if not is_ok then
128 if self:eof() then
129 -- this error is EOF (connection terminated)
130 -- exactly what we were waiting for
131 break
132 end
133 return is_ok, more_data
134 end
135 self._data = self._data .. more_data
136 end
137
138 local data = self._data
139 self._data = ''
140 return true, data
141 end
142
143 --[[[
144 -- @method tcp_sync.write(n)
145 --
146 -- Writes data into the stream.
147 --
148 -- @return
149 -- true if everything is fine
150 -- false, {error message} otherwise
151 --
152 --]]
153 function tcp_sync:write(data)
154 return self._conn:write(data)
155 end
156
157 --[[[
158 -- @method tcp_sync.close()
159 --
160 -- Closes the connection. If the connection was created with task,
161 -- this method is called automatically as soon as the task is done
162 -- Calling this method helps to prevent connections leak.
163 -- The object is finally destroyed by garbage collector.
164 --
165 -- @return
166 --
167 --]]
168 function tcp_sync:close()
169 return self._conn:close()
170 end
171
172 --[[[
173 -- @method tcp_sync.eof()
174 --
175 -- @return
176 -- true if last "read" operation ended with EOF
177 -- false otherwise
178 --
179 --]]
180 function tcp_sync:eof()
181 if not self._eof and self._conn:eof() then
182 self._eof = true
183 end
184 return self._eof
185 end
186
187 --[[[
188 -- @function tcp_sync.shutdown(n)
189 --
190 -- half-close socket
191 --
192 -- @return
193 --
194 --]]
195 function tcp_sync:shutdown()
196 return self._conn:shutdown()
197 end
198
199 exports.connect = function(args)
200 local is_ok, connection = rspamd_tcp.connect_sync(args)
201 if not is_ok then
202 return is_ok, connection
203 end
204
205 local instance = tcp_sync.new(connection)
206 instance._addr = string.format("%s:%s", tostring(args.host), tostring(args.port))
207
208 lua_util.debugm(N, args.task, 'Connected to %s', instance._addr)
209
210 return true, instance
211 end
212
213 return exports