annotate h2_proxy_protocol.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 236d038dc04a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
876
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
1 #!/usr/bin/perl
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
2
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
3 # (C) Sergey Kandaurov
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
4 # (C) Nginx, Inc.
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
5
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
6 # Tests for HTTP/2 protocol with proxy_protocol.
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
7
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
8 ###############################################################################
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
9
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
10 use warnings;
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
11 use strict;
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
12
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
13 use Test::More;
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
14
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
15 use Socket qw/ CRLF /;
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
16
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
17 BEGIN { use FindBin; chdir($FindBin::Bin); }
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
18
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
19 use lib 'lib';
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
20 use Test::Nginx;
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
21 use Test::Nginx::HTTP2;
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
22
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
23 ###############################################################################
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
24
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
25 select STDERR; $| = 1;
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
26 select STDOUT; $| = 1;
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
27
1899
a0ee073760c5 Tests: updated HTTP/2 tests with invalid PROXY protocol.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
28 my $t = Test::Nginx->new()->has(qw/http http_v2 realip/)->plan(3)
876
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
29 ->write_file_expand('nginx.conf', <<'EOF');
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
30
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
31 %%TEST_GLOBALS%%
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
32
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
33 daemon off;
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
34
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
35 events {
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
36 }
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
37
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
38 http {
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
39 %%TEST_GLOBALS_HTTP%%
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
40
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
41 server {
974
882267679006 Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 952
diff changeset
42 listen 127.0.0.1:8080 proxy_protocol http2;
876
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
43 server_name localhost;
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
44
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
45 location /pp {
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
46 set_real_ip_from 127.0.0.1/32;
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
47 real_ip_header proxy_protocol;
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
48 alias %%TESTDIR%%/t.html;
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
49 add_header X-PP $remote_addr;
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
50 }
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
51 }
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
52 }
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
53
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
54 EOF
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
55
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
56 $t->write_file('t.html', 'SEE-THIS');
1900
236d038dc04a Tests: suppress "listen .. http2;" deprecation warnings.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1899
diff changeset
57
236d038dc04a Tests: suppress "listen .. http2;" deprecation warnings.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1899
diff changeset
58 # suppress deprecation warning
236d038dc04a Tests: suppress "listen .. http2;" deprecation warnings.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1899
diff changeset
59
236d038dc04a Tests: suppress "listen .. http2;" deprecation warnings.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1899
diff changeset
60 open OLDERR, ">&", \*STDERR; close STDERR;
876
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
61 $t->run();
1900
236d038dc04a Tests: suppress "listen .. http2;" deprecation warnings.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1899
diff changeset
62 open STDERR, ">&", \*OLDERR;
876
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
63
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
64 ###############################################################################
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
65
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
66 my $proxy = 'PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678' . CRLF;
974
882267679006 Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 952
diff changeset
67 my $s = Test::Nginx::HTTP2->new(port(8080), proxy => $proxy);
948
4dc302d8e04f Tests: changed HTTP2 package to act as a class.
Sergey Kandaurov <pluknet@nginx.com>
parents: 946
diff changeset
68 my $sid = $s->new_stream({ path => '/pp' });
4dc302d8e04f Tests: changed HTTP2 package to act as a class.
Sergey Kandaurov <pluknet@nginx.com>
parents: 946
diff changeset
69 my $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
876
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
70
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
71 my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
72 ok($frame, 'PROXY HEADERS frame');
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
73 is($frame->{headers}->{'x-pp'}, '192.0.2.1', 'PROXY remote addr');
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
74
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
75 # invalid PROXY protocol string
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
76
1899
a0ee073760c5 Tests: updated HTTP/2 tests with invalid PROXY protocol.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
77 TODO: {
a0ee073760c5 Tests: updated HTTP/2 tests with invalid PROXY protocol.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
78 local $TODO = 'not yet' unless $t->has_version('1.25.1');
876
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
79
1899
a0ee073760c5 Tests: updated HTTP/2 tests with invalid PROXY protocol.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
80 $proxy = 'BOGUS TCP4 192.0.2.1 192.0.2.2 1234 5678' . CRLF;
a0ee073760c5 Tests: updated HTTP/2 tests with invalid PROXY protocol.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
81 ok(!http($proxy), 'PROXY invalid protocol');
a0ee073760c5 Tests: updated HTTP/2 tests with invalid PROXY protocol.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
82
a0ee073760c5 Tests: updated HTTP/2 tests with invalid PROXY protocol.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
83 }
876
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
84
a6abbfed42c0 Tests: split HTTP/2 tests, HTTP2 package introduced.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
85 ###############################################################################