Debian Codegraph @

test/functional/lua/rspamadm/test_tcp_client.lua

raw
1 local logger = require "rspamd_logger"
2 local tcp_sync = require "lua_tcp_sync"
3
4 local is_ok, connection = tcp_sync.connect {
5 config = rspamd_config,
6 ev_base = rspamadm_ev_base,
7 session = rspamadm_session,
8 host = '127.0.0.1',
9 timeout = 20,
10 port = 18080,
11 }
12 if not is_ok then
13 logger.errx(rspamd_config, 'connect error: %1', connection)
14 return
15 end
16 local err
17 is_ok, err = connection:write(string.format('POST /request HTTP/1.1\r\nConnection: close\r\n\r\n'))
18
19 logger.info('write %1, %2', is_ok, err)
20 if not is_ok then
21 logger.errx(rspamd_config, 'write error: %1', err)
22 return
23 end
24
25 local content_length, content
26
27 while true do
28 local header_line
29 is_ok, header_line = connection:read_until("\r\n")
30 if not is_ok then
31 logger.errx(rspamd_config, 'failed to get header: %1', header_line)
32 return
33 end
34
35 if header_line == "" then
36 logger.info('headers done')
37 break
38 end
39
40 local value
41 local header = header_line:gsub("([%w-]+): (.*)",
42 function (h, v) value = v; return h:lower() end)
43
44 logger.info('parsed header: %1 -> "%2"', header, value)
45
46 if header == "content-length" then
47 content_length = tonumber(value)
48 end
49
50 end
51
52 if content_length then
53 is_ok, content = connection:read_bytes(content_length)
54 if is_ok then
55 end
56 else
57 is_ok, content = connection:read_until_eof()
58 if is_ok then
59 end
60 end
61 logger.info('(is_ok: %1) content [%2 bytes] %3', is_ok, content_length, content)
62
63
64 print(content)