Skip to content

Commit 5a6b5f7

Browse files
feature: add timer context support for get_client_body_reader.
When downloading a big file in the timer, you don't want to hold the whole file in the memory, write the file into the disk piece by piece.
1 parent 9cc19b3 commit 5a6b5f7

21 files changed

Lines changed: 316 additions & 22 deletions

lib/resty/http.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ function _M.request_uri(self, uri, params)
974974
end
975975

976976

977-
function _M.get_client_body_reader(_, chunksize, sock)
977+
function _M.get_client_body_reader(_, chunksize, sock, headers)
978978
chunksize = chunksize or 65536
979979

980980
if not sock then
@@ -994,8 +994,11 @@ function _M.get_client_body_reader(_, chunksize, sock)
994994
end
995995
end
996996

997-
local headers = ngx_req_get_headers()
998-
local length = headers.content_length
997+
if not headers then
998+
headers = ngx_req_get_headers()
999+
end
1000+
1001+
local length = headers["Content-Length"]
9991002
if length then
10001003
return _body_reader(sock, tonumber(length), chunksize)
10011004
elseif transfer_encoding_is_chunked(headers) then

t/01-basic.t

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ no_long_string();
2424
run_tests();
2525

2626
__DATA__
27+
2728
=== TEST 1: Simple default get.
2829
--- http_config eval: $::HttpConfig
2930
--- config
@@ -59,6 +60,7 @@ OK
5960
[warn]
6061

6162

63+
6264
=== TEST 2: HTTP 1.0
6365
--- http_config eval: $::HttpConfig
6466
--- config
@@ -95,6 +97,7 @@ OK
9597
[warn]
9698

9799

100+
98101
=== TEST 3: Status code and reason phrase
99102
--- http_config eval: $::HttpConfig
100103
--- config
@@ -136,6 +139,7 @@ OK
136139
[warn]
137140

138141

142+
139143
=== TEST 4: Response headers
140144
--- http_config eval: $::HttpConfig
141145
--- config
@@ -174,6 +178,7 @@ x-value
174178
[warn]
175179

176180

181+
177182
=== TEST 5: Query
178183
--- http_config eval: $::HttpConfig
179184
--- config
@@ -223,7 +228,8 @@ X-Header-B: 2
223228
[warn]
224229

225230

226-
=== TEST 7: HEAD has no body.
231+
232+
=== TEST 6: HEAD has no body.
227233
--- http_config eval: $::HttpConfig
228234
--- config
229235
location = /a {
@@ -260,7 +266,8 @@ GET /a
260266
[warn]
261267

262268

263-
=== TEST 8: Errors when not initialized
269+
270+
=== TEST 7: Errors when not initialized
264271
--- http_config eval: $::HttpConfig
265272
--- config
266273
location = /a {
@@ -300,7 +307,8 @@ not initialized
300307
[warn]
301308

302309

303-
=== TEST 12: Allow empty HTTP header values (RFC7230)
310+
311+
=== TEST 8: Allow empty HTTP header values (RFC7230)
304312
--- http_config eval: $::HttpConfig
305313
--- config
306314
location = /a {
@@ -342,7 +350,9 @@ OK
342350
[error]
343351
[warn]
344352

345-
=== TEST 13: Should return error on invalid HTTP version in response status line
353+
354+
355+
=== TEST 9: Should return error on invalid HTTP version in response status line
346356
--- http_config eval: $::HttpConfig
347357
--- config
348358
location = /a {
@@ -366,7 +376,9 @@ GET /a
366376
[error]
367377
[warn]
368378

369-
=== TEST 14: Should return error on invalid status code in response status line
379+
380+
381+
=== TEST 10: Should return error on invalid status code in response status line
370382
--- http_config eval: $::HttpConfig
371383
--- config
372384
location = /a {
@@ -392,7 +404,7 @@ GET /a
392404

393405

394406

395-
=== TEST 14: Empty query
407+
=== TEST 11: Empty query
396408
--- http_config eval: $::HttpConfig
397409
--- config
398410
location = /a {
@@ -429,5 +441,3 @@ GET /a
429441
--- no_error_log
430442
[error]
431443
[warn]
432-
433-

t/02-chunked.t

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ no_long_string();
2424
run_tests();
2525

2626
__DATA__
27+
2728
=== TEST 1: Non chunked.
2829
--- http_config eval: $::HttpConfig
2930
--- config
@@ -67,6 +68,7 @@ GET /a
6768
[warn]
6869

6970

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

129131

132+
130133
=== TEST 3: Chunked using read_body method.
131134
--- http_config eval: $::HttpConfig
132135
--- config
@@ -175,6 +178,7 @@ GET /a
175178
[warn]
176179

177180

181+
178182
=== TEST 4: Chunked. multiple-headers, mixed case
179183
--- http_config eval: $::HttpConfig
180184
--- config
@@ -240,6 +244,7 @@ table
240244
[warn]
241245

242246

247+
243248
=== TEST 5: transfer_encoding_is_chunked utility.
244249
--- http_config eval: $::HttpConfig
245250
--- config
@@ -277,6 +282,7 @@ GET /a
277282
[warn]
278283

279284

285+
280286
=== TEST 6: Don't send Content-Length if Transfer-Encoding is specified
281287
--- http_config eval: $::HttpConfig
282288
--- config

t/03-requestbody.t

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ no_long_string();
2424
run_tests();
2525

2626
__DATA__
27+
2728
=== TEST 1: POST form-urlencoded
2829
--- http_config eval: $::HttpConfig
2930
--- config
@@ -69,6 +70,7 @@ c: 3
6970
[warn]
7071

7172

73+
7274
=== TEST 2: POST form-urlencoded 1.0
7375
--- http_config eval: $::HttpConfig
7476
--- config
@@ -118,6 +120,7 @@ c: 3
118120
[warn]
119121

120122

123+
121124
=== TEST 3: 100 Continue does not end requset
122125
--- http_config eval: $::HttpConfig
123126
--- config
@@ -164,6 +167,8 @@ c: 3
164167
[error]
165168
[warn]
166169

170+
171+
167172
=== TEST 4: Return non-100 status to user
168173
--- http_config eval: $::HttpConfig
169174
--- config
@@ -205,6 +210,7 @@ Expectation Failed
205210
[warn]
206211

207212

213+
208214
=== TEST 5: Return 100 Continue with headers
209215
--- http_config eval: $::HttpConfig
210216
--- config
@@ -278,6 +284,7 @@ a=1&b=2&c=3
278284
[warn]
279285

280286

287+
281288
=== TEST 6: Return 100 Continue without headers
282289
--- http_config eval: $::HttpConfig
283290
--- config
@@ -351,6 +358,7 @@ a=1&b=2&c=3
351358
[warn]
352359

353360

361+
354362
=== TEST 7: Non string request bodies are converted with correct length
355363
--- http_config eval: $::HttpConfig
356364
--- config
@@ -393,6 +401,7 @@ mix123edtable
393401
[warn]
394402

395403

404+
396405
=== TEST 8: Request body as iterator
397406
--- http_config eval: $::HttpConfig
398407
--- config
@@ -430,6 +439,7 @@ foobar
430439
[warn]
431440

432441

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

467477

478+
468479
=== TEST 10: Request body as iterator with chunked encoding
469480
--- http_config eval: $::HttpConfig
470481
--- config

t/04-trailers.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ no_long_string();
2424
run_tests();
2525

2626
__DATA__
27+
2728
=== TEST 1: Trailers. Check Content-MD5 generated after the body is sent matches up.
2829
--- http_config eval: $::HttpConfig
2930
--- config
@@ -96,6 +97,7 @@ OK
9697
[warn]
9798
9899
100+
99101
=== TEST 2: Advertised trailer does not exist, handled gracefully.
100102
--- http_config eval: $::HttpConfig
101103
--- config

t/05-stream.t

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ no_long_string();
2424
run_tests();
2525

2626
__DATA__
27+
2728
=== TEST 1: Chunked streaming body reader returns the right content length.
2829
--- http_config eval: $::HttpConfig
2930
--- config
@@ -76,6 +77,7 @@ chunked
7677
[warn]
7778

7879

80+
7981
=== TEST 2: Non-Chunked streaming body reader returns the right content length.
8082
--- http_config eval: $::HttpConfig
8183
--- config
@@ -131,7 +133,8 @@ nil
131133
[warn]
132134

133135

134-
=== TEST 2b: Non-Chunked streaming body reader, buffer size becomes nil
136+
137+
=== TEST 3b: Non-Chunked streaming body reader, buffer size becomes nil
135138
--- http_config eval: $::HttpConfig
136139
--- config
137140
location = /a {
@@ -184,7 +187,8 @@ nil
184187
Buffer size not specified, bailing
185188

186189

187-
=== TEST 3: HTTP 1.0 body reader with no max size returns the right content length.
190+
191+
=== TEST 4: HTTP 1.0 body reader with no max size returns the right content length.
188192
--- http_config eval: $::HttpConfig
189193
--- config
190194
location = /a {
@@ -240,7 +244,8 @@ nil
240244
[warn]
241245

242246

243-
=== TEST 4: HTTP 1.0 body reader with max chunk size returns the right content length.
247+
248+
=== TEST 5: HTTP 1.0 body reader with max chunk size returns the right content length.
244249
--- http_config eval: $::HttpConfig
245250
--- config
246251
location = /a {
@@ -298,7 +303,8 @@ nil
298303
[warn]
299304

300305

301-
=== TEST 4b: HTTP 1.0 body reader with no content length, stream works as expected.
306+
307+
=== TEST 6b: HTTP 1.0 body reader with no content length, stream works as expected.
302308
--- http_config eval: $::HttpConfig
303309
--- config
304310
location = /a {
@@ -365,7 +371,8 @@ GET /a
365371
[warn]
366372

367373

368-
=== TEST 5: Chunked streaming body reader with max chunk size returns the right content length.
374+
375+
=== TEST 7: Chunked streaming body reader with max chunk size returns the right content length.
369376
--- http_config eval: $::HttpConfig
370377
--- config
371378
location = /a {
@@ -421,7 +428,8 @@ chunked
421428
[warn]
422429

423430

424-
=== TEST 6: Request reader correctly reads body
431+
432+
=== TEST 8: Request reader correctly reads body
425433
--- http_config eval: $::HttpConfig
426434
--- config
427435
location = /a {
@@ -450,7 +458,9 @@ foobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarba
450458
[error]
451459
[warn]
452460

453-
=== TEST 7: Request reader correctly reads body in chunks
461+
462+
463+
=== TEST 9: Request reader correctly reads body in chunks
454464
--- http_config eval: $::HttpConfig
455465
--- config
456466
location = /a {
@@ -484,7 +494,8 @@ foobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarba
484494
[warn]
485495

486496

487-
=== TEST 8: Request reader passes into client
497+
498+
=== TEST 10: Request reader passes into client
488499
--- http_config eval: $::HttpConfig
489500
--- config
490501
location = /a {
@@ -532,7 +543,8 @@ foobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarba
532543
[warn]
533544

534545

535-
=== TEST 9: Body reader is a function returning nil when no body is present.
546+
547+
=== TEST 11: Body reader is a function returning nil when no body is present.
536548
--- http_config eval: $::HttpConfig
537549
--- config
538550
location = /a {
@@ -569,7 +581,8 @@ GET /a
569581
[warn]
570582

571583

572-
=== TEST 10: Issue a notice (but do not error) if trying to read the request body in a subrequest
584+
585+
=== TEST 12: Issue a notice (but do not error) if trying to read the request body in a subrequest
573586
--- http_config eval: $::HttpConfig
574587
--- config
575588
location = /a {

0 commit comments

Comments
 (0)