aboutsummaryrefslogtreecommitdiffstats
path: root/test/functional/lua/rspamadm/test_tcp_client.lua
blob: eb103db18feeb2db2a6b7156c64e1c07453c6f55 (plain)
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
local logger = require "rspamd_logger"
local tcp_sync = require "lua_tcp_sync"

local is_ok, connection = tcp_sync.connect {
  config = rspamd_config,
  ev_base = rspamadm_ev_base,
  session = rspamadm_session,
  host = '127.0.0.1',
  timeout = 20,
  port = 18080,
}
if not is_ok then
  logger.errx(rspamd_config, 'connect error: %1', connection)
  return
end
local err
is_ok, err = connection:write(string.format('POST /request HTTP/1.1\r\nConnection: close\r\n\r\n'))

logger.info('write %1, %2', is_ok, err)
if not is_ok then
  logger.errx(rspamd_config, 'write error: %1', err)
  return
end

local content_length, content

while true do
  local header_line
  is_ok, header_line = connection:read_until("\r\n")
  if not is_ok then
    logger.errx(rspamd_config, 'failed to get header: %1', header_line)
    return
  end

  if header_line == "" then
    logger.info('headers done')
    break
  end

  local value
  local header = header_line:gsub("([%w-]+): (.*)",
      function (h, v) value = v; return h:lower() end)

  logger.info('parsed header: %1 -> "%2"', header, value)

  if header == "content-length" then
    content_length = tonumber(value)
  end

end

if content_length then
  is_ok, content = connection:read_bytes(content_length)
  if is_ok then
  end
else
  is_ok, content = connection:read_until_eof()
  if is_ok then
  end
end
logger.info('(is_ok: %1) content [%2 bytes] %3', is_ok, content_length, content)


print(content)