annotate stream_realip.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 f3ba4c74de31
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1028
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1 #!/usr/bin/perl
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
2
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
3 # (C) Sergey Kandaurov
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
4 # (C) Nginx, Inc.
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
5
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
6 # Tests for stream realip module, server side proxy protocol.
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
7
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
8 ###############################################################################
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
9
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
10 use warnings;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
11 use strict;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
12
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
13 use Test::More;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
14
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
15 use IO::Select;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
16 use Socket qw/ $CRLF /;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
17
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
18 BEGIN { use FindBin; chdir($FindBin::Bin); }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
19
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
20 use lib 'lib';
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
21 use Test::Nginx;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
22 use Test::Nginx::Stream qw/ stream /;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
23
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
24 ###############################################################################
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
25
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
26 select STDERR; $| = 1;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
27 select STDOUT; $| = 1;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
28
1170
cf14cfe9ec8c Tests: dropped obsolete ipv6 prerequisite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1055
diff changeset
29 my $t = Test::Nginx->new()->has(qw/stream stream_return stream_realip/)
1028
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
30 ->write_file_expand('nginx.conf', <<'EOF');
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
31
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
32 %%TEST_GLOBALS%%
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
33
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
34 daemon off;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
35
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
36 events {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
37 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
38
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
39 stream {
1609
f3ba4c74de31 Tests: added TEST_GLOBALS_STREAM variable support.
Andrei Belov <defan@nginx.com>
parents: 1251
diff changeset
40 %%TEST_GLOBALS_STREAM%%
f3ba4c74de31 Tests: added TEST_GLOBALS_STREAM variable support.
Andrei Belov <defan@nginx.com>
parents: 1251
diff changeset
41
1028
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
42 server {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
43 listen 127.0.0.1:8083 proxy_protocol;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
44 listen 127.0.0.1:8084;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
45 return $proxy_protocol_addr:$proxy_protocol_port;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
46 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
47
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
48 server {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
49 listen 127.0.0.1:8085 proxy_protocol;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
50 proxy_pass 127.0.0.1:8081;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
51 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
52
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
53 server {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
54 listen 127.0.0.1:8086 proxy_protocol;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
55 listen [::1]:%%PORT_8086%% proxy_protocol;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
56 return "$remote_addr:$remote_port:
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
57 $realip_remote_addr:$realip_remote_port";
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
58
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
59 set_real_ip_from ::1;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
60 set_real_ip_from 127.0.0.2;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
61 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
62
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
63 server {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
64 listen 127.0.0.1:8087;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
65 proxy_pass [::1]:%%PORT_8086%%;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
66 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
67
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
68 server {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
69 listen 127.0.0.1:8088 proxy_protocol;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
70 listen [::1]:%%PORT_8088%% proxy_protocol;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
71 return "$remote_addr:$remote_port:
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
72 $realip_remote_addr:$realip_remote_port";
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
73
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
74 set_real_ip_from 127.0.0.1;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
75 set_real_ip_from ::2;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
76 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
77
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
78 server {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
79 listen 127.0.0.1:8089;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
80 proxy_pass [::1]:%%PORT_8088%%;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
81 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
82 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
83
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
84 EOF
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
85
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
86 $t->run_daemon(\&stream_daemon);
1251
766bcbb632ee Tests: removed TODO and try_run() checks for legacy versions.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1190
diff changeset
87 $t->try_run('no inet6 support')->plan(8);
1028
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
88 $t->waitforsocket('127.0.0.1:' . port(8081));
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
89
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
90 ###############################################################################
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
91
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
92 is(pp_get(8083, "PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"),
1028
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
93 '192.0.2.1:1234', 'server');
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
94
1030
094e1247740f Tests: simplified stream_realip.t and unbroke it on win32.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1028
diff changeset
95 is(stream('127.0.0.1:' . port(8084))->read(), ':', 'server off');
1028
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
96
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
97 is(pp_get(8085, "PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}close"),
1028
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
98 'close', 'server payload');
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
99
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
100 like(pp_get(8086, "PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"),
1028
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
101 qr/^(\Q127.0.0.1:\E\d+):\s+\1$/, 'server ipv6 realip - no match');
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
102
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
103 like(pp_get(8087, "PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"),
1028
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
104 qr/\Q192.0.2.1:1234:\E\s+\Q::1:\E\d+/, 'server ipv6 realip');
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
105
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
106 like(pp_get(8088, "PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"),
1028
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
107 qr/\Q192.0.2.1:1234:\E\s+\Q127.0.0.1:\E\d+/, 'server ipv4 realip');
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
108
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
109 like(pp_get(8089, "PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"),
1028
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
110 qr/^(::1:\d+):\s+\1$/, 'server ipv4 realip - no match');
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
111
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
112 like(pp_get(8088, "PROXY UNKNOWN TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"),
1028
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
113 qr/^(\Q127.0.0.1:\E\d+):\s+\1$/, 'server unknown');
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
114
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
115 ###############################################################################
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
116
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
117 sub pp_get {
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
118 my ($port, $proxy) = @_;
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
119 stream(PeerPort => port($port))->io($proxy);
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
120 }
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
121
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
122 ###############################################################################
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
123
1028
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
124 sub stream_daemon {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
125 my $server = IO::Socket::INET->new(
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
126 Proto => 'tcp',
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
127 LocalAddr => '127.0.0.1:' . port(8081),
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
128 Listen => 5,
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
129 Reuse => 1
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
130 )
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
131 or die "Can't create listening socket: $!\n";
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
132
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
133 my $sel = IO::Select->new($server);
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
134
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
135 local $SIG{PIPE} = 'IGNORE';
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
136
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
137 while (my @ready = $sel->can_read) {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
138 foreach my $fh (@ready) {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
139 if ($server == $fh) {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
140 my $new = $fh->accept;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
141 $new->autoflush(1);
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
142 $sel->add($new);
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
143
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
144 } elsif (stream_handle_client($fh)) {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
145 $sel->remove($fh);
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
146 $fh->close;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
147 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
148 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
149 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
150 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
151
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
152 sub stream_handle_client {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
153 my ($client) = @_;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
154
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
155 log2c("(new connection $client)");
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
156
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
157 $client->sysread(my $buffer, 65536) or return 1;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
158
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
159 log2i("$client $buffer");
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
160
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
161 log2o("$client $buffer");
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
162
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
163 $client->syswrite($buffer);
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
164
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
165 return $buffer =~ /close/;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
166 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
167
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
168 sub log2i { Test::Nginx::log_core('|| <<', @_); }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
169 sub log2o { Test::Nginx::log_core('|| >>', @_); }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
170 sub log2c { Test::Nginx::log_core('||', @_); }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
171
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
172 ###############################################################################