annotate stream_realip.t @ 1427:eb1d883305ea

Tests: avoid edge cases in upstream random two test. Unavailable servers contribute to the number of attempts, if selected, before the balancer would fall back to the default round-robin method. This means that it's quite possible to get server with more connections. To facilitate with selecting two alive servers, down server was removed from the upstream configuration at the cost of slightly worse coverage.
author Sergey Kandaurov <pluknet@nginx.com>
date Thu, 10 Jan 2019 17:42:34 +0300
parents 766bcbb632ee
children f3ba4c74de31
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 {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
40 server {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
41 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
42 listen 127.0.0.1:8084;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
43 return $proxy_protocol_addr:$proxy_protocol_port;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
44 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
45
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
46 server {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
47 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
48 proxy_pass 127.0.0.1:8081;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
49 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
50
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
51 server {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
52 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
53 listen [::1]:%%PORT_8086%% proxy_protocol;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
54 return "$remote_addr:$remote_port:
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
55 $realip_remote_addr:$realip_remote_port";
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
56
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
57 set_real_ip_from ::1;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
58 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
59 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
60
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
61 server {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
62 listen 127.0.0.1:8087;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
63 proxy_pass [::1]:%%PORT_8086%%;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
64 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
65
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
66 server {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
67 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
68 listen [::1]:%%PORT_8088%% proxy_protocol;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
69 return "$remote_addr:$remote_port:
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
70 $realip_remote_addr:$realip_remote_port";
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
71
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
72 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
73 set_real_ip_from ::2;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
74 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
75
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
76 server {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
77 listen 127.0.0.1:8089;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
78 proxy_pass [::1]:%%PORT_8088%%;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
79 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
80 }
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 EOF
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 $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
85 $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
86 $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
87
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
88 ###############################################################################
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
89
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
90 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
91 '192.0.2.1:1234', 'server');
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
92
1030
094e1247740f Tests: simplified stream_realip.t and unbroke it on win32.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1028
diff changeset
93 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
94
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
95 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
96 'close', 'server payload');
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
97
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
98 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
99 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
100
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
101 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
102 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
103
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
104 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
105 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
106
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
107 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
108 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
109
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
110 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
111 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
112
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
113 ###############################################################################
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
114
1055
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
115 sub pp_get {
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
116 my ($port, $proxy) = @_;
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
117 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
118 }
8979f0d86c29 Tests: reduced diff to stream_ssl_realip.t, no functional changes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1030
diff changeset
119
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
1028
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
122 sub stream_daemon {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
123 my $server = IO::Socket::INET->new(
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
124 Proto => 'tcp',
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
125 LocalAddr => '127.0.0.1:' . port(8081),
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
126 Listen => 5,
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
127 Reuse => 1
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
128 )
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
129 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
130
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
131 my $sel = IO::Select->new($server);
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 local $SIG{PIPE} = 'IGNORE';
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 while (my @ready = $sel->can_read) {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
136 foreach my $fh (@ready) {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
137 if ($server == $fh) {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
138 my $new = $fh->accept;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
139 $new->autoflush(1);
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
140 $sel->add($new);
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
141
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
142 } elsif (stream_handle_client($fh)) {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
143 $sel->remove($fh);
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
144 $fh->close;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
145 }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
146 }
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 sub stream_handle_client {
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
151 my ($client) = @_;
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
152
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
153 log2c("(new connection $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 $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
156
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
157 log2i("$client $buffer");
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 log2o("$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 $client->syswrite($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 return $buffer =~ /close/;
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
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
166 sub log2i { Test::Nginx::log_core('|| <<', @_); }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
167 sub log2o { Test::Nginx::log_core('|| >>', @_); }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
168 sub log2c { Test::Nginx::log_core('||', @_); }
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
169
403709429c3b Tests: stream realip tests, listen proxy_protocol tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
170 ###############################################################################