-
Notifications
You must be signed in to change notification settings - Fork 0
/
40proxy-protocol.t
153 lines (140 loc) · 3.7 KB
/
40proxy-protocol.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use strict;
use warnings;
use File::Temp qw(tempdir);
use IO::Socket::INET;
use IO::Socket::SSL;
use Net::EmptyPort qw(check_port empty_port);
use Test::More;
use t::Util;
my $tempdir = tempdir(CLEANUP => 1);
my $port = empty_port();
sub spawn_h2o {
my ($proxy_protocol, $ssl) = @_;
open my $fh, ">", "$tempdir/h2o.conf"
or die "failed to create file:$tempdir/h2o.conf:$!";
print $fh <<"EOT";
handshake-timeout: 3
hosts:
default:
access-log:
format: "%h"
path: $tempdir/access_log
paths:
/:
file.dir: @{[ DOC_ROOT ]}
listen:
host: 127.0.0.1
port: $port
proxy-protocol: @{[$proxy_protocol ? "ON" : "OFF"]}
EOT
if ($ssl) {
print $fh <<"EOT";
ssl:
key-file: examples/h2o/server.key
certificate-file: examples/h2o/server.crt
EOT
}
close $fh;
spawn_server(
argv => [ bindir() . "/h2o", "-c", "$tempdir/h2o.conf" ],
is_ready => sub {
check_port($port);
},
);
}
sub fetch {
my $req = shift;
my $conn = IO::Socket::INET->new(
PeerHost => q(127.0.0.1),
PeerPort => $port,
Proto => q(tcp),
) or die "failed to connect to host:$!";
$conn->write($req);
$conn->read(my $buf, 1048576);
$buf;
}
sub fetch_ssl {
my ($pre, $req) = @_;
my $conn = IO::Socket::INET->new(
PeerHost => q(127.0.0.1),
PeerPort => $port,
SSL_startHandshake => 0,
) or die "failed to connect to host:$!";
$conn->write($pre);
IO::Socket::SSL->start_SSL($conn, SSL_verify_mode => 0)
or die $SSL_ERROR;
$conn->write($req);
$conn->read(my $buf, 1048576);
$buf;
}
sub last_log {
open my $fh, "<", "$tempdir/access_log"
or die "failed to open file:$tempdir/access_log:$!";
my $last;
while (<$fh>) {
$last = $_;
}
chomp $last;
$last;
}
sub test_timeout {
local $@;
my $gotsig = 0;
local $SIG{ALRM} = sub {
$gotsig = 1;
die "gotsig";
};
alarm(5);
eval { fetch("") };
alarm(0);
ok ! $gotsig;
}
subtest "http" => sub {
my $guard = spawn_h2o(1, 0);
subtest "with proxy" => sub {
my $resp = fetch("PROXY TCP4 1.2.3.4 5.6.7.8 1234 9999\r\nGET / HTTP/1.0\r\n\r\n");
like $resp, qr{^HTTP/1.1 200 OK\r\n}s;
is last_log(), "1.2.3.4";
};
subtest "without proxy" => sub {
my $resp = fetch("GET / HTTP/1.0\r\n\r\n");
like $resp, qr{^HTTP/1.1 200 OK\r\n}s;
is last_log(), "127.0.0.1";
};
subtest "timeout" => sub {
test_timeout();
};
};
subtest "https" => sub {
my $guard = spawn_h2o(1, 1);
subtest "with proxy" => sub {
my $resp = fetch_ssl("PROXY TCP4 1.2.3.4 5.6.7.8 1234 9999\r\n", "GET / HTTP/1.0\r\n\r\n");
like $resp, qr{^HTTP/1.1 200 OK\r\n}s;
is last_log(), "1.2.3.4";
};
subtest "without proxy" => sub {
my $resp = fetch_ssl("", "GET / HTTP/1.0\r\n\r\n");
like $resp, qr{^HTTP/1.1 200 OK\r\n}s;
is last_log(), "127.0.0.1";
};
subtest "timeout" => sub {
test_timeout();
};
};
subtest "off" => sub {
my $guard = spawn_h2o(0, 0);
subtest "with proxy" => sub {
my $resp = fetch("PROXY TCP4 1.2.3.4 5.6.7.8 1234 9999\r\nGET / HTTP/1.0\r\n\r\n");
unlike $resp, qr{^HTTP/1.1 200 OK\r\n}s;
};
subtest "without proxy" => sub {
my $resp = fetch("GET / HTTP/1.0\r\n\r\n");
like $resp, qr{^HTTP/1.1 200 OK\r\n}s;
};
};
subtest "https handshake timeout" => sub {
# timeout test for PROXY:OFF over HTTPS is implemented here since it is easier to do so
my $guard = spawn_h2o(0, 1);
test_timeout();
};
done_testing;