annotate proxy_set_body.t @ 1961:fe6f22da53ec default tip

Tests: tests for usage of discarded body. The client_max_body_size limit should be ignored when the request body is already discarded. In HTTP/1.x, this is done by checking the r->discard_body flag when the body is being discarded, and because r->headers_in.content_length_n is 0 when it's already discarded. This, however, does not happen with HTTP/2 and HTTP/3, and therefore "error_page 413" does not work without relaxing the limit. Further, with proxy_pass, r->headers_in.content_length_n is used to determine length of the request body, and therefore is not correct if discarding of the request body isn't yet complete. While discarding the request body, r->headers_in.content_length_n contains the rest of the body to discard (or, in case of chunked request body, the rest of the current chunk to discard). Similarly, the $content_length variable uses r->headers_in.content_length if available, and also incorrect. The $content_length variable is used when proxying with fastcgi_pass, grpc_pass, and uwsgi_pass (scgi_pass uses the value calculated based on the actual request body buffers, and therefore works correctly).
author Maxim Dounin <mdounin@mdounin.ru>
date Sat, 27 Apr 2024 18:55:50 +0300
parents 882267679006
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
283
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
1 #!/usr/bin/perl
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
2
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
3 # (C) Maxim Dounin
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
4
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
5 # Tests for proxy_set_body.
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
6
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
7 ###############################################################################
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
8
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
9 use warnings;
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
10 use strict;
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
11
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
12 use Test::More;
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
13
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
14 BEGIN { use FindBin; chdir($FindBin::Bin); }
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
15
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
16 use lib 'lib';
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
17 use Test::Nginx;
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
18
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
19 ###############################################################################
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
20
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
21 select STDERR; $| = 1;
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
22 select STDOUT; $| = 1;
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
23
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
24 my $t = Test::Nginx->new()->has(qw/http proxy rewrite/)->plan(2)
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
25 ->write_file_expand('nginx.conf', <<'EOF');
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
26
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
27 %%TEST_GLOBALS%%
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
28
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
29 daemon off;
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
30
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
31 events {
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
32 }
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
33
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
34 http {
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
35 %%TEST_GLOBALS_HTTP%%
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
36
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
37 server {
974
882267679006 Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 952
diff changeset
38 listen 127.0.0.1:8080;
283
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
39 server_name localhost;
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
40
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
41 location / {
974
882267679006 Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 952
diff changeset
42 proxy_pass http://127.0.0.1:8080/body;
283
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
43 proxy_set_body "body";
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
44 }
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
45
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
46 location /p1 {
974
882267679006 Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 952
diff changeset
47 proxy_pass http://127.0.0.1:8080/x1;
283
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
48 proxy_set_body "body";
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
49 }
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
50
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
51 location /p2 {
974
882267679006 Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 952
diff changeset
52 proxy_pass http://127.0.0.1:8080/body;
283
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
53 proxy_set_body "body two";
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
54 }
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
55
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
56 location /x1 {
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
57 add_header X-Accel-Redirect /p2;
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
58 return 204;
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
59 }
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
60
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
61 location /body {
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
62 add_header X-Body $request_body;
974
882267679006 Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 952
diff changeset
63 proxy_pass http://127.0.0.1:8080/empty;
283
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
64 }
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
65
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
66 location /empty {
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
67 return 204;
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
68 }
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
69 }
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
70 }
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
71
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
72 EOF
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
73
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
74 $t->run();
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
75
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
76 ###############################################################################
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
77
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
78 like(http_get('/'), qr/X-Body: body/, 'proxy_set_body');
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
79 like(http_get('/p1'), qr/X-Body: body two/, 'proxy_set_body twice');
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
80
36d24870ccb2 Tests: proxy_set_body tests added.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
81 ###############################################################################