summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/t/50reverse-proxy-proxy-protocol.t
blob: 0bb1a821efdc2d31e602db0f3918dadd6d120f39 (plain)
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
use strict;
use warnings;
use IO::Socket::INET;
use Test::TCP;
use Net::EmptyPort qw(check_port empty_port);
use Test::More;
use Scope::Guard qw(guard);
use t::Util;

my $upstream_port = empty_port();

my $listen = IO::Socket::INET->new(
    LocalAddr => '127.0.0.1',
    LocalPort => $upstream_port,
    Listen => 5,
) or die "failed to listen to 127.0.0.1:$upstream_port:$!";

my $upstream_guard = do {
    my $pid = fork;
    die "fork failed:$!"
        unless defined $pid;
    if ($pid == 0) {
        # server process
        while (1) {
            if (my $conn = $listen->accept) {
                sysread $conn, my $buf, 4096;
print STDERR "**** $buf";
                syswrite $conn, "HTTP/1.1 200 OK\r\nconnection: close\r\n\r\n$buf";
print STDERR "**** yeoh";
                $conn->close;
            }
        }
    }
    guard {
        kill 'TERM', $pid;
    };
};

my $server = spawn_h2o(<< "EOT");
hosts:
  default:
    paths:
      "/":
        proxy.reverse.url: http://127.0.0.1:$upstream_port/
        proxy.proxy-protocol: ON
        proxy.timeout.keepalive: 0
EOT

run_with_curl($server, sub {
    my ($proto, $port, $curl_cmd) = @_;
    my $resp = `$curl_cmd --silent $proto://127.0.0.1:$port/hello`;
    like $resp, qr{^PROXY TCP4 127\.0\.0\.1 127\.0\.0\.1 [0-9]{1,5} $port\r\nGET /hello HTTP/1\.}is;
});

done_testing;