Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/resty/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ function _M.request_uri(self, uri, params)
end


function _M.get_client_body_reader(_, chunksize, sock)
function _M.get_client_body_reader(_, chunksize, sock, headers)
chunksize = chunksize or 65536

if not sock then
Expand All @@ -994,8 +994,11 @@ function _M.get_client_body_reader(_, chunksize, sock)
end
end

local headers = ngx_req_get_headers()
local length = headers.content_length
if not headers then
headers = ngx_req_get_headers()
end

local length = headers["Content-Length"]
if length then
return _body_reader(sock, tonumber(length), chunksize)
elseif transfer_encoding_is_chunked(headers) then
Expand Down
26 changes: 18 additions & 8 deletions t/01-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ no_long_string();
run_tests();

__DATA__

=== TEST 1: Simple default get.
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -59,6 +60,7 @@ OK
[warn]



=== TEST 2: HTTP 1.0
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -95,6 +97,7 @@ OK
[warn]



=== TEST 3: Status code and reason phrase
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -136,6 +139,7 @@ OK
[warn]



=== TEST 4: Response headers
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -174,6 +178,7 @@ x-value
[warn]



=== TEST 5: Query
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -223,7 +228,8 @@ X-Header-B: 2
[warn]


=== TEST 7: HEAD has no body.

=== TEST 6: HEAD has no body.
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -260,7 +266,8 @@ GET /a
[warn]


=== TEST 8: Errors when not initialized

=== TEST 7: Errors when not initialized
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -300,7 +307,8 @@ not initialized
[warn]


=== TEST 12: Allow empty HTTP header values (RFC7230)

=== TEST 8: Allow empty HTTP header values (RFC7230)
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -342,7 +350,9 @@ OK
[error]
[warn]

=== TEST 13: Should return error on invalid HTTP version in response status line


=== TEST 9: Should return error on invalid HTTP version in response status line
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand All @@ -366,7 +376,9 @@ GET /a
[error]
[warn]

=== TEST 14: Should return error on invalid status code in response status line


=== TEST 10: Should return error on invalid status code in response status line
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand All @@ -392,7 +404,7 @@ GET /a



=== TEST 14: Empty query
=== TEST 11: Empty query
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -429,5 +441,3 @@ GET /a
--- no_error_log
[error]
[warn]


6 changes: 6 additions & 0 deletions t/02-chunked.t
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ no_long_string();
run_tests();

__DATA__

=== TEST 1: Non chunked.
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -67,6 +68,7 @@ GET /a
[warn]



=== TEST 2: Chunked. The number of chunks received when no max size is given proves the response was in fact chunked.
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -127,6 +129,7 @@ GET /a
[warn]



=== TEST 3: Chunked using read_body method.
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -175,6 +178,7 @@ GET /a
[warn]



=== TEST 4: Chunked. multiple-headers, mixed case
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -240,6 +244,7 @@ table
[warn]



=== TEST 5: transfer_encoding_is_chunked utility.
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -277,6 +282,7 @@ GET /a
[warn]



=== TEST 6: Don't send Content-Length if Transfer-Encoding is specified
--- http_config eval: $::HttpConfig
--- config
Expand Down
11 changes: 11 additions & 0 deletions t/03-requestbody.t
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ no_long_string();
run_tests();

__DATA__

=== TEST 1: POST form-urlencoded
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -69,6 +70,7 @@ c: 3
[warn]



=== TEST 2: POST form-urlencoded 1.0
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -118,6 +120,7 @@ c: 3
[warn]



=== TEST 3: 100 Continue does not end requset
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -164,6 +167,8 @@ c: 3
[error]
[warn]



=== TEST 4: Return non-100 status to user
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -205,6 +210,7 @@ Expectation Failed
[warn]



=== TEST 5: Return 100 Continue with headers
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -278,6 +284,7 @@ a=1&b=2&c=3
[warn]



=== TEST 6: Return 100 Continue without headers
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -351,6 +358,7 @@ a=1&b=2&c=3
[warn]



=== TEST 7: Non string request bodies are converted with correct length
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -393,6 +401,7 @@ mix123edtable
[warn]



=== TEST 8: Request body as iterator
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -430,6 +439,7 @@ foobar
[warn]



=== TEST 9: Request body as iterator, errors with missing length
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -465,6 +475,7 @@ Request body is a function but a length or chunked encoding is not specified
[warn]



=== TEST 10: Request body as iterator with chunked encoding
--- http_config eval: $::HttpConfig
--- config
Expand Down
2 changes: 2 additions & 0 deletions t/04-trailers.t
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ no_long_string();
run_tests();

__DATA__

=== TEST 1: Trailers. Check Content-MD5 generated after the body is sent matches up.
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -96,6 +97,7 @@ OK
[warn]



=== TEST 2: Advertised trailer does not exist, handled gracefully.
--- http_config eval: $::HttpConfig
--- config
Expand Down
33 changes: 23 additions & 10 deletions t/05-stream.t
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ no_long_string();
run_tests();

__DATA__

=== TEST 1: Chunked streaming body reader returns the right content length.
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -76,6 +77,7 @@ chunked
[warn]



=== TEST 2: Non-Chunked streaming body reader returns the right content length.
--- http_config eval: $::HttpConfig
--- config
Expand Down Expand Up @@ -131,7 +133,8 @@ nil
[warn]


=== TEST 2b: Non-Chunked streaming body reader, buffer size becomes nil

=== TEST 3b: Non-Chunked streaming body reader, buffer size becomes nil
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -184,7 +187,8 @@ nil
Buffer size not specified, bailing


=== TEST 3: HTTP 1.0 body reader with no max size returns the right content length.

=== TEST 4: HTTP 1.0 body reader with no max size returns the right content length.
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -240,7 +244,8 @@ nil
[warn]


=== TEST 4: HTTP 1.0 body reader with max chunk size returns the right content length.

=== TEST 5: HTTP 1.0 body reader with max chunk size returns the right content length.
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -298,7 +303,8 @@ nil
[warn]


=== TEST 4b: HTTP 1.0 body reader with no content length, stream works as expected.

=== TEST 6b: HTTP 1.0 body reader with no content length, stream works as expected.
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -365,7 +371,8 @@ GET /a
[warn]


=== TEST 5: Chunked streaming body reader with max chunk size returns the right content length.

=== TEST 7: Chunked streaming body reader with max chunk size returns the right content length.
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -421,7 +428,8 @@ chunked
[warn]


=== TEST 6: Request reader correctly reads body

=== TEST 8: Request reader correctly reads body
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -450,7 +458,9 @@ foobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarba
[error]
[warn]

=== TEST 7: Request reader correctly reads body in chunks


=== TEST 9: Request reader correctly reads body in chunks
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -484,7 +494,8 @@ foobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarba
[warn]


=== TEST 8: Request reader passes into client

=== TEST 10: Request reader passes into client
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -532,7 +543,8 @@ foobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarba
[warn]


=== TEST 9: Body reader is a function returning nil when no body is present.

=== TEST 11: Body reader is a function returning nil when no body is present.
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -569,7 +581,8 @@ GET /a
[warn]


=== TEST 10: Issue a notice (but do not error) if trying to read the request body in a subrequest

=== TEST 12: Issue a notice (but do not error) if trying to read the request body in a subrequest
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down
Loading
Loading