Debian Codegraph @

test/functional/lua/http_early_response.lua

raw
1 --[[
2 Test HTTP client behavior when server sends early responses.
3
4 This tests edge cases where a server responds before the client has finished
5 sending the request body (which is allowed by HTTP/1.1 spec).
6
7 The test server (dummy_http_early_response.py) runs on port 18083.
8 ]]
9
10 local rspamd_http = require "rspamd_http"
11 local rspamd_logger = require "rspamd_logger"
12
13 -- Register all possible result symbols upfront
14 -- These are the symbols that will be inserted based on HTTP response codes
15 local result_symbols = {
16 -- Test 1: Early reply
17 'HTTP_EARLY_REPLY_ERROR',
18 'HTTP_EARLY_REPLY_200',
19 'HTTP_EARLY_REPLY_413',
20 'HTTP_EARLY_REPLY_500',
21 -- Test 2: Early 413
22 'HTTP_EARLY_413_ERROR',
23 'HTTP_EARLY_413_413',
24 'HTTP_EARLY_413_200',
25 -- Test 3: Keepalive early
26 'HTTP_KEEPALIVE_EARLY_ERROR',
27 'HTTP_KEEPALIVE_EARLY_200',
28 -- Test 4: Coroutine early
29 'HTTP_EARLY_CORO_ERROR',
30 'HTTP_EARLY_CORO_200',
31 -- Test 5: Normal
32 'HTTP_NORMAL_ERROR',
33 'HTTP_NORMAL_200',
34 -- Test 6: Keepalive sequential
35 'HTTP_KEEPALIVE_SEQ_ERRORS',
36 'HTTP_KEEPALIVE_SEQ_SUCCESS',
37 -- Test 7: Keepalive stress
38 'HTTP_EARLY_KEEPALIVE_STRESS',
39 -- Test 8: Immediate close
40 'HTTP_IMMEDIATE_CLOSE_ERROR',
41 'HTTP_IMMEDIATE_CLOSE_413',
42 'HTTP_IMMEDIATE_CLOSE_200',
43 -- Test 9: Slow response
44 'HTTP_SLOW_RESPONSE_ERROR',
45 'HTTP_SLOW_RESPONSE_200',
46 -- Test 10: Rapid close
47 'HTTP_RAPID_CLOSE_RESULTS',
48 -- Test 11: Block and reply
49 'HTTP_BLOCK_REPLY_ERROR',
50 'HTTP_BLOCK_REPLY_413',
51 'HTTP_BLOCK_REPLY_200',
52 -- Test 12: Block and reply coro
53 'HTTP_BLOCK_REPLY_CORO_ERROR',
54 'HTTP_BLOCK_REPLY_CORO_413',
55 'HTTP_BLOCK_REPLY_CORO_200',
56 -- Test 13: Block slow
57 'HTTP_BLOCK_SLOW_ERROR',
58 'HTTP_BLOCK_SLOW_503',
59 'HTTP_BLOCK_SLOW_200',
60 -- Test 14: Instant reply (before headers even read)
61 'HTTP_INSTANT_REPLY_ERROR',
62 'HTTP_INSTANT_REPLY_413',
63 }
64
65 -- Register all result symbols as virtual symbols
66 for _, sym_name in ipairs(result_symbols) do
67 rspamd_config:register_symbol({
68 name = sym_name,
69 score = 0.0,
70 type = 'virtual',
71 parent = -1, -- Will be set properly by parent registration
72 })
73 end
74
75 -- Test 1: Early reply - server responds before reading body
76 local function test_early_reply(task)
77 rspamd_logger.errx(task, 'test_early_reply: starting')
78
79 -- Create a large body to increase chance of race condition
80 local body_parts = {}
81 for i = 1, 1000 do
82 body_parts[i] = string.format("line %d: some test data that we are sending\n", i)
83 end
84 local large_body = table.concat(body_parts)
85
86 local function callback(err, code, body)
87 if err then
88 rspamd_logger.errx(task, 'test_early_reply callback error: %s', err)
89 task:insert_result('HTTP_EARLY_REPLY_ERROR', 1.0, err)
90 else
91 rspamd_logger.errx(task, 'test_early_reply callback success: code=%s body=%s', code, body)
92 task:insert_result('HTTP_EARLY_REPLY_' .. tostring(code), 1.0, body)
93 end
94 end
95
96 rspamd_http.request({
97 url = 'http://127.0.0.1:18083/early-reply',
98 task = task,
99 method = 'post',
100 body = large_body,
101 callback = callback,
102 timeout = 5,
103 })
104 end
105
106 -- Test 2: Early 413 error - server sends error before reading body
107 local function test_early_error_413(task)
108 rspamd_logger.errx(task, 'test_early_error_413: starting')
109
110 -- Create a large body
111 local body_parts = {}
112 for i = 1, 1000 do
113 body_parts[i] = string.format("line %d: large body data\n", i)
114 end
115 local large_body = table.concat(body_parts)
116
117 local function callback(err, code, body)
118 if err then
119 rspamd_logger.errx(task, 'test_early_error_413 callback error: %s', err)
120 task:insert_result('HTTP_EARLY_413_ERROR', 1.0, err)
121 else
122 rspamd_logger.errx(task, 'test_early_error_413 callback success: code=%s body=%s', code, body)
123 task:insert_result('HTTP_EARLY_413_' .. tostring(code), 1.0, body)
124 end
125 end
126
127 rspamd_http.request({
128 url = 'http://127.0.0.1:18083/early-error-413',
129 task = task,
130 method = 'post',
131 body = large_body,
132 callback = callback,
133 timeout = 5,
134 })
135 end
136
137 -- Test 3: Keep-alive with early response
138 local function test_keepalive_early(task)
139 rspamd_logger.errx(task, 'test_keepalive_early: starting')
140
141 local body = "test body for keepalive"
142
143 local function callback(err, code, body_response)
144 if err then
145 rspamd_logger.errx(task, 'test_keepalive_early callback error: %s', err)
146 task:insert_result('HTTP_KEEPALIVE_EARLY_ERROR', 1.0, err)
147 else
148 rspamd_logger.errx(task, 'test_keepalive_early callback success: code=%s', code)
149 task:insert_result('HTTP_KEEPALIVE_EARLY_' .. tostring(code), 1.0, body_response)
150 end
151 end
152
153 rspamd_http.request({
154 url = 'http://127.0.0.1:18083/keepalive-early',
155 task = task,
156 method = 'post',
157 body = body,
158 callback = callback,
159 timeout = 5,
160 keepalive = true,
161 })
162 end
163
164 -- Test 4: Coroutine-based early reply test
165 local function test_early_reply_coro(task)
166 rspamd_logger.errx(task, 'test_early_reply_coro: starting')
167
168 local body_parts = {}
169 for i = 1, 500 do
170 body_parts[i] = string.format("coro line %d: test data\n", i)
171 end
172 local body = table.concat(body_parts)
173
174 local err, response = rspamd_http.request({
175 url = 'http://127.0.0.1:18083/early-reply',
176 task = task,
177 method = 'post',
178 body = body,
179 timeout = 5,
180 })
181
182 if err then
183 rspamd_logger.errx(task, 'test_early_reply_coro error: %s', err)
184 task:insert_result('HTTP_EARLY_CORO_ERROR', 1.0, err)
185 else
186 rspamd_logger.errx(task, 'test_early_reply_coro success: code=%s', response.code)
187 task:insert_result('HTTP_EARLY_CORO_' .. tostring(response.code), 1.0, response.content)
188 end
189 end
190
191 -- Test 5: Multiple requests to normal endpoint (baseline)
192 local function test_normal_request(task)
193 rspamd_logger.errx(task, 'test_normal_request: starting')
194
195 local function callback(err, code, body)
196 if err then
197 rspamd_logger.errx(task, 'test_normal_request callback error: %s', err)
198 task:insert_result('HTTP_NORMAL_ERROR', 1.0, err)
199 else
200 rspamd_logger.errx(task, 'test_normal_request callback success: code=%s', code)
201 task:insert_result('HTTP_NORMAL_' .. tostring(code), 1.0, body)
202 end
203 end
204
205 rspamd_http.request({
206 url = 'http://127.0.0.1:18083/request',
207 task = task,
208 method = 'post',
209 body = 'normal test body',
210 callback = callback,
211 timeout = 5,
212 })
213 end
214
215 -- Main test symbol that runs all tests
216 local function http_early_response_tests(task)
217 local test_type = tostring(task:get_request_header('test-type') or 'all')
218
219 rspamd_logger.errx(task, 'http_early_response_tests: test_type=%s', test_type)
220
221 if test_type == 'early-reply' or test_type == 'all' then
222 test_early_reply(task)
223 end
224
225 if test_type == 'early-413' or test_type == 'all' then
226 test_early_error_413(task)
227 end
228
229 if test_type == 'keepalive-early' or test_type == 'all' then
230 test_keepalive_early(task)
231 end
232
233 if test_type == 'early-coro' or test_type == 'all' then
234 test_early_reply_coro(task)
235 end
236
237 if test_type == 'normal' or test_type == 'all' then
238 test_normal_request(task)
239 end
240 end
241
242 rspamd_config:register_symbol({
243 name = 'HTTP_EARLY_RESPONSE_TEST',
244 score = 1.0,
245 callback = http_early_response_tests,
246 no_squeeze = true,
247 flags = 'coro'
248 })
249
250 -- Test 6: Sequential keepalive requests (stress test for keepalive pool)
251 local function test_keepalive_sequential(task)
252 rspamd_logger.errx(task, 'test_keepalive_sequential: starting')
253
254 local success_count = 0
255 local error_count = 0
256 local errors = {}
257
258 -- Make 3 sequential requests using keepalive
259 for i = 1, 3 do
260 local err, response = rspamd_http.request({
261 url = 'http://127.0.0.1:18083/keepalive-normal',
262 task = task,
263 method = 'post',
264 body = string.format('request %d body', i),
265 timeout = 5,
266 keepalive = true,
267 })
268
269 if err then
270 error_count = error_count + 1
271 errors[#errors + 1] = string.format('req%d: %s', i, err)
272 rspamd_logger.errx(task, 'keepalive request %d error: %s', i, err)
273 else
274 success_count = success_count + 1
275 rspamd_logger.errx(task, 'keepalive request %d success: code=%s', i, response.code)
276 end
277 end
278
279 if error_count > 0 then
280 task:insert_result('HTTP_KEEPALIVE_SEQ_ERRORS', 1.0, table.concat(errors, '; '))
281 end
282 task:insert_result('HTTP_KEEPALIVE_SEQ_SUCCESS', 1.0, tostring(success_count))
283 end
284
285 rspamd_config:register_symbol({
286 name = 'HTTP_KEEPALIVE_SEQUENTIAL_TEST',
287 score = 1.0,
288 callback = test_keepalive_sequential,
289 no_squeeze = true,
290 flags = 'coro'
291 })
292
293 -- Test 7: Stress test with early responses + keepalive
294 local function test_early_keepalive_stress(task)
295 rspamd_logger.errx(task, 'test_early_keepalive_stress: starting')
296
297 local results = {}
298
299 -- Mix of normal and early-response requests
300 local endpoints = {
301 '/request',
302 '/early-reply',
303 '/request',
304 '/keepalive-early',
305 '/request',
306 }
307
308 for i, endpoint in ipairs(endpoints) do
309 local body_parts = {}
310 for j = 1, 100 do
311 body_parts[j] = string.format("stress test line %d-%d\n", i, j)
312 end
313
314 local err, response = rspamd_http.request({
315 url = 'http://127.0.0.1:18083' .. endpoint,
316 task = task,
317 method = 'post',
318 body = table.concat(body_parts),
319 timeout = 5,
320 keepalive = true,
321 })
322
323 if err then
324 results[#results + 1] = string.format('%s:err:%s', endpoint, err)
325 else
326 results[#results + 1] = string.format('%s:ok:%d', endpoint, response.code)
327 end
328 end
329
330 task:insert_result('HTTP_EARLY_KEEPALIVE_STRESS', 1.0, table.concat(results, '|'))
331 end
332
333 rspamd_config:register_symbol({
334 name = 'HTTP_EARLY_KEEPALIVE_STRESS_TEST',
335 score = 1.0,
336 callback = test_early_keepalive_stress,
337 no_squeeze = true,
338 flags = 'coro'
339 })
340
341 -- Test 8: Aggressive immediate close with large body
342 -- This should trigger actual failures - server closes socket while client writes
343 local function test_immediate_close_large(task)
344 rspamd_logger.errx(task, 'test_immediate_close_large: starting')
345
346 -- Create a VERY large body that won't fit in socket buffer
347 -- This forces the client to block on write, at which point server closes
348 local body_parts = {}
349 for i = 1, 10000 do -- ~500KB body
350 body_parts[i] = string.format("line %05d: this is padding data to make the body very large and exceed socket buffers\n", i)
351 end
352 local large_body = table.concat(body_parts)
353 rspamd_logger.errx(task, 'test_immediate_close_large: body size = %d bytes', #large_body)
354
355 local function callback(err, code, body)
356 if err then
357 rspamd_logger.errx(task, 'test_immediate_close_large callback error: %s', err)
358 -- Error is EXPECTED here - we want to see how client handles it
359 task:insert_result('HTTP_IMMEDIATE_CLOSE_ERROR', 1.0, err)
360 else
361 rspamd_logger.errx(task, 'test_immediate_close_large callback success: code=%s', code)
362 -- Success means client received the 413 response despite server closing
363 task:insert_result('HTTP_IMMEDIATE_CLOSE_' .. tostring(code), 1.0, body)
364 end
365 end
366
367 rspamd_http.request({
368 url = 'http://127.0.0.1:18083/immediate-close-413',
369 task = task,
370 method = 'post',
371 body = large_body,
372 callback = callback,
373 timeout = 10,
374 })
375 end
376
377 rspamd_config:register_symbol({
378 name = 'HTTP_IMMEDIATE_CLOSE_TEST',
379 score = 1.0,
380 callback = test_immediate_close_large,
381 no_squeeze = true,
382 flags = 'coro'
383 })
384
385 -- Test 9: Slow response with large body - tests race condition
386 local function test_slow_response_large(task)
387 rspamd_logger.errx(task, 'test_slow_response_large: starting')
388
389 local body_parts = {}
390 for i = 1, 5000 do -- ~250KB body
391 body_parts[i] = string.format("slow test line %05d: padding data for the test\n", i)
392 end
393 local body = table.concat(body_parts)
394
395 local err, response = rspamd_http.request({
396 url = 'http://127.0.0.1:18083/slow-response-no-drain',
397 task = task,
398 method = 'post',
399 body = body,
400 timeout = 10,
401 })
402
403 if err then
404 rspamd_logger.errx(task, 'test_slow_response_large error: %s', err)
405 task:insert_result('HTTP_SLOW_RESPONSE_ERROR', 1.0, err)
406 else
407 rspamd_logger.errx(task, 'test_slow_response_large success: code=%s', response.code)
408 task:insert_result('HTTP_SLOW_RESPONSE_' .. tostring(response.code), 1.0, response.content)
409 end
410 end
411
412 rspamd_config:register_symbol({
413 name = 'HTTP_SLOW_RESPONSE_TEST',
414 score = 1.0,
415 callback = test_slow_response_large,
416 no_squeeze = true,
417 flags = 'coro'
418 })
419
420 -- Test 10: Multiple rapid requests to immediate-close endpoint
421 -- This stresses the connection handling and cleanup
422 local function test_rapid_close_requests(task)
423 rspamd_logger.errx(task, 'test_rapid_close_requests: starting')
424
425 local results = {}
426 local body = string.rep("x", 10000) -- 10KB body
427
428 for i = 1, 5 do
429 local err, response = rspamd_http.request({
430 url = 'http://127.0.0.1:18083/immediate-close-413',
431 task = task,
432 method = 'post',
433 body = body,
434 timeout = 5,
435 })
436
437 if err then
438 results[#results + 1] = string.format('req%d:err:%s', i, err)
439 else
440 results[#results + 1] = string.format('req%d:ok:%d', i, response.code)
441 end
442 end
443
444 task:insert_result('HTTP_RAPID_CLOSE_RESULTS', 1.0, table.concat(results, '|'))
445 end
446
447 rspamd_config:register_symbol({
448 name = 'HTTP_RAPID_CLOSE_TEST',
449 score = 1.0,
450 callback = test_rapid_close_requests,
451 no_squeeze = true,
452 flags = 'coro'
453 })
454
455 -- Test 11: TRUE early response test with buffer-exceeding body
456 -- This test sends a body larger than socket buffers (~256KB+) to ensure
457 -- the client actually blocks on write while server sends response
458 local function test_block_and_reply(task)
459 rspamd_logger.errx(task, 'test_block_and_reply: starting')
460
461 -- Create body larger than socket buffers (256KB+ for localhost)
462 -- macOS has 128KB send + 128KB receive buffers
463 local body_size = 512 * 1024 -- 512KB should definitely exceed buffers
464 local chunk = string.rep("X", 1024) -- 1KB chunk
465 local body_parts = {}
466 for i = 1, body_size / 1024 do
467 body_parts[i] = chunk
468 end
469 local huge_body = table.concat(body_parts)
470 rspamd_logger.errx(task, 'test_block_and_reply: body size = %d bytes', #huge_body)
471
472 local function callback(err, code, body)
473 if err then
474 rspamd_logger.errx(task, 'test_block_and_reply error: %s', err)
475 task:insert_result('HTTP_BLOCK_REPLY_ERROR', 1.0, err)
476 else
477 rspamd_logger.errx(task, 'test_block_and_reply success: code=%s body=%s', code, body)
478 -- This is the IDEAL outcome - we got the early 413 response!
479 task:insert_result('HTTP_BLOCK_REPLY_' .. tostring(code), 1.0, body)
480 end
481 end
482
483 rspamd_http.request({
484 url = 'http://127.0.0.1:18083/block-and-reply',
485 task = task,
486 method = 'post',
487 body = huge_body,
488 callback = callback,
489 timeout = 10,
490 })
491 end
492
493 rspamd_config:register_symbol({
494 name = 'HTTP_BLOCK_REPLY_TEST',
495 score = 1.0,
496 callback = test_block_and_reply,
497 no_squeeze = true,
498 flags = 'coro'
499 })
500
501 -- Test 12: Coroutine version of block-and-reply
502 local function test_block_and_reply_coro(task)
503 rspamd_logger.errx(task, 'test_block_and_reply_coro: starting')
504
505 -- 512KB body
506 local huge_body = string.rep("Y", 512 * 1024)
507
508 local err, response = rspamd_http.request({
509 url = 'http://127.0.0.1:18083/block-and-reply',
510 task = task,
511 method = 'post',
512 body = huge_body,
513 timeout = 10,
514 })
515
516 if err then
517 rspamd_logger.errx(task, 'test_block_and_reply_coro error: %s', err)
518 task:insert_result('HTTP_BLOCK_REPLY_CORO_ERROR', 1.0, err)
519 else
520 rspamd_logger.errx(task, 'test_block_and_reply_coro success: code=%s', response.code)
521 task:insert_result('HTTP_BLOCK_REPLY_CORO_' .. tostring(response.code), 1.0, response.content)
522 end
523 end
524
525 rspamd_config:register_symbol({
526 name = 'HTTP_BLOCK_REPLY_CORO_TEST',
527 score = 1.0,
528 callback = test_block_and_reply_coro,
529 no_squeeze = true,
530 flags = 'coro'
531 })
532
533 -- Test 13: Slow block test - server waits even longer
534 local function test_block_slow(task)
535 rspamd_logger.errx(task, 'test_block_slow: starting')
536
537 -- 1MB body to really fill things up
538 local huge_body = string.rep("Z", 1024 * 1024)
539
540 local err, response = rspamd_http.request({
541 url = 'http://127.0.0.1:18083/block-and-reply-slow',
542 task = task,
543 method = 'post',
544 body = huge_body,
545 timeout = 15, -- Longer timeout since server waits 1 second
546 })
547
548 if err then
549 rspamd_logger.errx(task, 'test_block_slow error: %s', err)
550 task:insert_result('HTTP_BLOCK_SLOW_ERROR', 1.0, err)
551 else
552 rspamd_logger.errx(task, 'test_block_slow success: code=%s', response.code)
553 task:insert_result('HTTP_BLOCK_SLOW_' .. tostring(response.code), 1.0, response.content)
554 end
555 end
556
557 rspamd_config:register_symbol({
558 name = 'HTTP_BLOCK_SLOW_TEST',
559 score = 1.0,
560 callback = test_block_slow,
561 no_squeeze = true,
562 flags = 'coro'
563 })
564
565 -- Test 14: Instant reply - server responds BEFORE reading headers
566 -- This is the most aggressive early response test
567 local function test_instant_reply(task)
568 rspamd_logger.errx(task, 'test_instant_reply: starting')
569
570 -- 512KB body - server will respond before even reading our headers
571 local huge_body = string.rep("Z", 512 * 1024)
572
573 local err, response = rspamd_http.request({
574 url = 'http://127.0.0.1:18083/instant-reply',
575 task = task,
576 method = 'post',
577 body = huge_body,
578 timeout = 10,
579 })
580
581 if err then
582 rspamd_logger.errx(task, 'test_instant_reply error: %s', err)
583 task:insert_result('HTTP_INSTANT_REPLY_ERROR', 1.0, err)
584 else
585 rspamd_logger.errx(task, 'test_instant_reply success: code=%s', response.code)
586 task:insert_result('HTTP_INSTANT_REPLY_' .. tostring(response.code), 1.0, response.content)
587 end
588 end
589
590 rspamd_config:register_symbol({
591 name = 'HTTP_INSTANT_REPLY_TEST',
592 score = 1.0,
593 callback = test_instant_reply,
594 no_squeeze = true,
595 flags = 'coro'
596 })