Skip to content

Commit 0ce55d6

Browse files
authored
Add error message for broken response status line (#242)
* Add test to reproduce #241 * Add error message for broken response status line Fixes #241 * Add test for an explicit error message string See #241
1 parent f754eb7 commit 0ce55d6

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

lib/resty/http.lua

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,21 @@ local function _receive_status(sock)
362362
return nil, nil, nil, err
363363
end
364364

365-
return tonumber(str_sub(line, 10, 12)), tonumber(str_sub(line, 6, 8)), str_sub(line, 14)
365+
local version = tonumber(str_sub(line, 6, 8))
366+
if not version then
367+
return nil, nil, nil,
368+
"couldn't parse HTTP version from response status line: " .. line
369+
end
370+
371+
local status = tonumber(str_sub(line, 10, 12))
372+
if not status then
373+
return nil, nil, nil,
374+
"couldn't parse status code from response status line: " .. line
375+
end
376+
377+
local reason = str_sub(line, 14)
378+
379+
return status, version, reason
366380
end
367381

368382

t/01-basic.t

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,51 @@ OK
341341
--- no_error_log
342342
[error]
343343
[warn]
344+
345+
=== TEST 13: Should return error on invalid HTTP version in response status line
346+
--- http_config eval: $::HttpConfig
347+
--- config
348+
location = /a {
349+
content_by_lua_block {
350+
local http = require "resty.http"
351+
local httpc = http.new()
352+
local res, err = httpc:request_uri("http://127.0.0.1:12345")
353+
354+
assert(err == "couldn't parse HTTP version from response status line: TEAPOT/1.1 OMG")
355+
}
356+
}
357+
--- tcp_listen: 12345
358+
--- tcp_reply
359+
TEAPOT/1.1 OMG
360+
Server: Teapot
361+
362+
OK
363+
--- request
364+
GET /a
365+
--- no_error_log
366+
[error]
367+
[warn]
368+
369+
=== TEST 14: Should return error on invalid status code in response status line
370+
--- http_config eval: $::HttpConfig
371+
--- config
372+
location = /a {
373+
content_by_lua_block {
374+
local http = require "resty.http"
375+
local httpc = http.new()
376+
local res, err = httpc:request_uri("http://127.0.0.1:12345")
377+
378+
assert(err == "couldn't parse status code from response status line: HTTP/1.1 OMG")
379+
}
380+
}
381+
--- tcp_listen: 12345
382+
--- tcp_reply
383+
HTTP/1.1 OMG
384+
Server: Teapot
385+
386+
OK
387+
--- request
388+
GET /a
389+
--- no_error_log
390+
[error]
391+
[warn]

0 commit comments

Comments
 (0)