| 1 |
--[[[ |
| 2 |
-- Just a test for TCP API |
| 3 |
--]] |
| 4 |
|
| 5 |
local rspamd_tcp = require "rspamd_tcp" |
| 6 |
local logger = require "rspamd_logger" |
| 7 |
local tcp_sync = require "lua_tcp_sync" |
| 8 |
|
| 9 |
-- [[ old fashioned callback api ]] |
| 10 |
local function http_simple_tcp_async_symbol(task) |
| 11 |
logger.errx(task, 'http_tcp_symbol: begin') |
| 12 |
local function http_get_cb(err, data, conn) |
| 13 |
logger.errx(task, 'http_get_cb: got reply: %s, error: %s, conn: %s', data, err, conn) |
| 14 |
task:insert_result('HTTP_ASYNC_RESPONSE_2', 1.0, data) |
| 15 |
end |
| 16 |
local function http_read_post_cb(err, conn) |
| 17 |
logger.errx(task, 'http_read_post_cb: write done: error: %s, conn: %s', err, conn) |
| 18 |
conn:add_read(http_get_cb) |
| 19 |
end |
| 20 |
local function http_read_cb(err, data, conn) |
| 21 |
logger.errx(task, 'http_read_cb: got reply: %s, error: %s, conn: %s', data, err, conn) |
| 22 |
conn:add_write(http_read_post_cb, "POST /request2 HTTP/1.1\r\nHost: 127.0.0.1:18080\r\n\r\n") |
| 23 |
task:insert_result('HTTP_ASYNC_RESPONSE', 1.0, data or err) |
| 24 |
end |
| 25 |
rspamd_tcp:request({ |
| 26 |
task = task, |
| 27 |
callback = http_read_cb, |
| 28 |
host = '127.0.0.1', |
| 29 |
data = {'GET /request HTTP/1.1\r\nHost: 127.0.0.1:18080\r\nConnection: keep-alive\r\n\r\n'}, |
| 30 |
read = true, |
| 31 |
port = 18080, |
| 32 |
}) |
| 33 |
end |
| 34 |
|
| 35 |
local function http_simple_tcp_ssl_symbol(task) |
| 36 |
logger.errx(task, 'ssl_tcp_symbol: begin') |
| 37 |
local function ssl_get_cb(err, data, conn) |
| 38 |
logger.errx(task, 'ssl_get_cb: got reply: %s, error: %s, conn: %s', data, err, conn) |
| 39 |
task:insert_result('TCP_SSL_RESPONSE_2', 1.0, tostring(data):gsub('%s', '')) |
| 40 |
end |
| 41 |
local function ssl_read_post_cb(err, conn) |
| 42 |
logger.errx(task, 'ssl_read_post_cb: write done: error: %s, conn: %s', err, conn) |
| 43 |
conn:add_read(ssl_get_cb) |
| 44 |
end |
| 45 |
local function ssl_read_cb(err, data, conn) |
| 46 |
logger.errx(task, 'ssl_read_cb: got reply: %s, error: %s, conn: %s', data, err, conn) |
| 47 |
conn:add_write(ssl_read_post_cb, "test2\n") |
| 48 |
task:insert_result('TCP_SSL_RESPONSE', 1.0, tostring(data):gsub('%s', '')) |
| 49 |
end |
| 50 |
rspamd_tcp:request({ |
| 51 |
task = task, |
| 52 |
callback = ssl_read_cb, |
| 53 |
host = '127.0.0.1', |
| 54 |
data = {'test\n'}, |
| 55 |
read = true, |
| 56 |
ssl = true, |
| 57 |
ssl_noverify = true, |
| 58 |
port = 18081, |
| 59 |
}) |
| 60 |
end |
| 61 |
|
| 62 |
local function http_large_tcp_ssl_symbol(task) |
| 63 |
local data = {} |
| 64 |
|
| 65 |
local function ssl_get_cb(err, rep, conn) |
| 66 |
logger.errx(task, 'ssl_get_cb: got reply: %s, error: %s, conn: %s', rep, err, conn) |
| 67 |
task:insert_result('TCP_SSL_LARGE_2', 1.0) |
| 68 |
end |
| 69 |
local function ssl_read_post_cb(err, conn) |
| 70 |
logger.errx(task, 'ssl_large_read_post_cb: write done: error: %s, conn: %s', err, conn) |
| 71 |
conn:add_read(ssl_get_cb) |
| 72 |
end |
| 73 |
local function ssl_read_cb(err, rep, conn) |
| 74 |
logger.errx(task, 'ssl_large_read_cb: got reply: %s, error: %s, conn: %s', rep, err, conn) |
| 75 |
conn:add_write(ssl_read_post_cb, 'foo\n') |
| 76 |
task:insert_result('TCP_SSL_LARGE', 1.0) |
| 77 |
end |
| 78 |
|
| 79 |
if task:get_queue_id() == 'SSL Large TCP request' then |
| 80 |
logger.errx(task, 'ssl_large_tcp_symbol: begin') |
| 81 |
for i = 1,2 do |
| 82 |
local st = {} |
| 83 |
for j=1,300000 do |
| 84 |
st[j] = 't' |
| 85 |
end |
| 86 |
data[i] = table.concat(st) |
| 87 |
end |
| 88 |
data[#data + 1] = '\n' |
| 89 |
|
| 90 |
rspamd_tcp:request({ |
| 91 |
task = task, |
| 92 |
callback = ssl_read_cb, |
| 93 |
host = '127.0.0.1', |
| 94 |
data = data, |
| 95 |
read = true, |
| 96 |
ssl = true, |
| 97 |
stop_pattern = '\n', |
| 98 |
ssl_noverify = true, |
| 99 |
port = 18081, |
| 100 |
timeout = 20, |
| 101 |
}) |
| 102 |
else |
| 103 |
logger.errx(task, 'ssl_large_tcp_symbol: skip') |
| 104 |
end |
| 105 |
end |
| 106 |
|
| 107 |
local function http_simple_tcp_symbol(task) |
| 108 |
logger.errx(task, 'connect_sync, before') |
| 109 |
|
| 110 |
local err |
| 111 |
local is_ok, connection = tcp_sync.connect { |
| 112 |
task = task, |
| 113 |
host = '127.0.0.1', |
| 114 |
timeout = 20, |
| 115 |
port = 18080, |
| 116 |
} |
| 117 |
|
| 118 |
if not is_ok then |
| 119 |
task:insert_result('HTTP_SYNC_WRITE_ERROR', 1.0, connection) |
| 120 |
logger.errx(task, 'write error: %1', connection) |
| 121 |
end |
| 122 |
|
| 123 |
logger.errx(task, 'connect_sync %1, %2', is_ok, tostring(connection)) |
| 124 |
|
| 125 |
is_ok, err = connection:write('GET /request HTTP/1.1\r\nHost: 127.0.0.1:18080\r\nConnection: keep-alive\r\n\r\n') |
| 126 |
|
| 127 |
logger.errx(task, 'write %1, %2', is_ok, err) |
| 128 |
if not is_ok then |
| 129 |
task:insert_result('HTTP_SYNC_WRITE_ERROR', 1.0, err) |
| 130 |
logger.errx(task, 'write error: %1', err) |
| 131 |
end |
| 132 |
|
| 133 |
local data |
| 134 |
local got_content = '' |
| 135 |
repeat |
| 136 |
is_ok, data = connection:read_once(); |
| 137 |
logger.errx(task, 'read_once: is_ok: %1, data: %2', is_ok, data) |
| 138 |
if not is_ok then |
| 139 |
task:insert_result('HTTP_SYNC_ERROR', 1.0, data) |
| 140 |
return |
| 141 |
else |
| 142 |
got_content = got_content .. data |
| 143 |
end |
| 144 |
if got_content:find('hello') then |
| 145 |
-- dummy_http.py responds with either hello world or hello post |
| 146 |
break |
| 147 |
end |
| 148 |
until false |
| 149 |
|
| 150 |
task:insert_result('HTTP_SYNC_RESPONSE', 1.0, got_content) |
| 151 |
|
| 152 |
is_ok, err = connection:write("POST /request HTTP/1.1\r\nHost: 127.0.0.1:18080\r\n\r\n") |
| 153 |
logger.errx(task, 'write[2] %1, %2', is_ok, err) |
| 154 |
|
| 155 |
got_content = '' |
| 156 |
repeat |
| 157 |
is_ok, data = connection:read_once(); |
| 158 |
logger.errx(task, 'read_once[2]: is_ok %1, data: %2', is_ok, data) |
| 159 |
if not is_ok then |
| 160 |
task:insert_result('HTTP_SYNC_ERROR_2', 1.0, data) |
| 161 |
return |
| 162 |
else |
| 163 |
got_content = got_content .. data |
| 164 |
end |
| 165 |
if got_content:find('hello') then |
| 166 |
break |
| 167 |
end |
| 168 |
until false |
| 169 |
|
| 170 |
task:insert_result('HTTP_SYNC_RESPONSE_2', 1.0, data) |
| 171 |
|
| 172 |
connection:close() |
| 173 |
end |
| 174 |
|
| 175 |
local function http_tcp_symbol(task) |
| 176 |
local url = tostring(task:get_request_header('url')) |
| 177 |
local method = tostring(task:get_request_header('method')) |
| 178 |
|
| 179 |
if url == 'nil' then |
| 180 |
return |
| 181 |
end |
| 182 |
|
| 183 |
local err |
| 184 |
local is_ok, connection = tcp_sync.connect { |
| 185 |
task = task, |
| 186 |
host = '127.0.0.1', |
| 187 |
timeout = 20, |
| 188 |
port = 18080, |
| 189 |
} |
| 190 |
|
| 191 |
logger.errx(task, 'connect_sync %1, %2', is_ok, tostring(connection)) |
| 192 |
if not is_ok then |
| 193 |
logger.errx(task, 'connect error: %1', connection) |
| 194 |
return |
| 195 |
end |
| 196 |
|
| 197 |
is_ok, err = connection:write(string.format('%s %s HTTP/1.1\r\nHost: 127.0.0.1:18080\r\nConnection: close\r\n\r\n', method:upper(), url)) |
| 198 |
|
| 199 |
logger.errx(task, 'write %1, %2', is_ok, err) |
| 200 |
if not is_ok then |
| 201 |
logger.errx(task, 'write error: %1', err) |
| 202 |
return |
| 203 |
end |
| 204 |
|
| 205 |
local content_length, content |
| 206 |
|
| 207 |
while true do |
| 208 |
local header_line |
| 209 |
is_ok, header_line = connection:read_until("\r\n") |
| 210 |
if not is_ok then |
| 211 |
logger.errx(task, 'failed to get header: %1', header_line) |
| 212 |
return |
| 213 |
end |
| 214 |
|
| 215 |
if header_line == "" then |
| 216 |
logger.errx(task, 'headers done') |
| 217 |
break |
| 218 |
end |
| 219 |
|
| 220 |
local value |
| 221 |
local header = header_line:gsub("([%w-]+): (.*)", |
| 222 |
function (h, v) value = v; return h:lower() end) |
| 223 |
|
| 224 |
logger.errx(task, 'parsed header: %1 -> "%2"', header, value) |
| 225 |
|
| 226 |
if header == "content-length" then |
| 227 |
content_length = tonumber(value) |
| 228 |
end |
| 229 |
|
| 230 |
end |
| 231 |
|
| 232 |
if content_length then |
| 233 |
is_ok, content = connection:read_bytes(content_length) |
| 234 |
if is_ok then |
| 235 |
task:insert_result('HTTP_SYNC_CONTENT_' .. method, 1.0, content) |
| 236 |
end |
| 237 |
else |
| 238 |
is_ok, content = connection:read_until_eof() |
| 239 |
if is_ok then |
| 240 |
task:insert_result('HTTP_SYNC_EOF_' .. method, 1.0, content) |
| 241 |
end |
| 242 |
end |
| 243 |
logger.errx(task, '(is_ok: %1) content [%2 bytes] %3', is_ok, content_length, content) |
| 244 |
end |
| 245 |
|
| 246 |
rspamd_config:register_symbol({ |
| 247 |
name = 'SIMPLE_TCP_ASYNC_TEST', |
| 248 |
score = 1.0, |
| 249 |
callback = http_simple_tcp_async_symbol, |
| 250 |
no_squeeze = true |
| 251 |
}) |
| 252 |
rspamd_config:register_symbol({ |
| 253 |
name = 'SIMPLE_TCP_ASYNC_SSL_TEST', |
| 254 |
score = 1.0, |
| 255 |
callback = http_simple_tcp_ssl_symbol, |
| 256 |
no_squeeze = true |
| 257 |
}) |
| 258 |
rspamd_config:register_symbol({ |
| 259 |
name = 'LARGE_TCP_ASYNC_SSL_TEST', |
| 260 |
score = 1.0, |
| 261 |
callback = http_large_tcp_ssl_symbol, |
| 262 |
no_squeeze = true |
| 263 |
}) |
| 264 |
rspamd_config:register_symbol({ |
| 265 |
name = 'SIMPLE_TCP_TEST', |
| 266 |
score = 1.0, |
| 267 |
callback = http_simple_tcp_symbol, |
| 268 |
no_squeeze = true, |
| 269 |
flags = 'coro', |
| 270 |
}) |
| 271 |
|
| 272 |
rspamd_config:register_symbol({ |
| 273 |
name = 'HTTP_TCP_TEST', |
| 274 |
score = 1.0, |
| 275 |
callback = http_tcp_symbol, |
| 276 |
no_squeeze = true, |
| 277 |
flags = 'coro', |
| 278 |
}) |
| 279 |
-- ]] |