annotate ssl_proxy_upgrade.t @ 585:5bb19f7448b5

Tests: Upgrade handling tests with http ssl module.
author Sergey Kandaurov <pluknet@nginx.com>
date Wed, 20 May 2015 14:44:14 +0300
parents
children 53b7c3c33a79
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
585
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1 #!/usr/bin/perl
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
2
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
3 # (C) Maxim Dounin
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
4 # (C) Sergey Kandaurov
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
5 # (C) Nginx, Inc.
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
6
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
7 # Tests for http proxy upgrade support with http ssl module.
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
8 # In contrast to proxy_websocket.t, this test doesn't try to use binary
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
9 # WebSocket protocol, but uses simple plain text protocol instead.
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
10
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
11 ###############################################################################
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
12
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
13 use warnings;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
14 use strict;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
15
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
16 use Test::More;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
17
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
18 use IO::Poll;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
19 use IO::Select;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
20 use Socket qw/ CRLF /;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
21
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
22 BEGIN { use FindBin; chdir($FindBin::Bin); }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
23
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
24 use lib 'lib';
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
25 use Test::Nginx;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
26
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
27 ###############################################################################
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
28
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
29 select STDERR; $| = 1;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
30 select STDOUT; $| = 1;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
31
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
32 eval { require IO::Socket::SSL; };
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
33 plan(skip_all => 'IO::Socket::SSL not installed') if $@;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
34 eval { IO::Socket::SSL::SSL_VERIFY_NONE(); };
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
35 plan(skip_all => 'IO::Socket::SSL too old') if $@;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
36
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
37 my $t = Test::Nginx->new()->has(qw/http proxy http_ssl/)->has_daemon('openssl')
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
38 ->write_file_expand('nginx.conf', <<'EOF')->plan(30);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
39
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
40 %%TEST_GLOBALS%%
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
41
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
42 daemon off;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
43
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
44 events {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
45 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
46
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
47 http {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
48 %%TEST_GLOBALS_HTTP%%
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
49
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
50 log_format test "$bytes_sent $body_bytes_sent";
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
51 access_log %%TESTDIR%%/cc.log test;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
52
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
53 server {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
54 listen 127.0.0.1:8080 ssl;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
55 server_name localhost;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
56
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
57 ssl_certificate_key localhost.key;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
58 ssl_certificate localhost.crt;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
59
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
60 location / {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
61 proxy_pass http://127.0.0.1:8081;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
62 proxy_http_version 1.1;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
63 proxy_set_header Upgrade $http_upgrade;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
64 proxy_set_header Connection "Upgrade";
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
65 proxy_read_timeout 2s;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
66 send_timeout 2s;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
67 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
68 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
69 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
70
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
71 EOF
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
72
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
73 $t->write_file('openssl.conf', <<EOF);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
74 [ req ]
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
75 default_bits = 2048
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
76 encrypt_key = no
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
77 distinguished_name = req_distinguished_name
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
78 [ req_distinguished_name ]
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
79 EOF
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
80
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
81 my $d = $t->testdir();
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
82
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
83 foreach my $name ('localhost') {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
84 system('openssl req -x509 -new '
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
85 . "-config '$d/openssl.conf' -subj '/CN=$name/' "
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
86 . "-out '$d/$name.crt' -keyout '$d/$name.key' "
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
87 . ">>$d/openssl.out 2>&1") == 0
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
88 or die "Can't create certificate for $name: $!\n";
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
89 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
90
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
91 $t->run_daemon(\&upgrade_fake_daemon);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
92 $t->run();
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
93
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
94 $t->waitforsocket('127.0.0.1:8081')
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
95 or die "Can't start test backend";
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
96
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
97 ###############################################################################
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
98
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
99 # establish connection
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
100
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
101 my @r;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
102 my $s = upgrade_connect();
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
103 ok($s, "handshake");
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
104
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
105 SKIP: {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
106 skip "handshake failed", 22 unless $s;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
107
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
108 # send a frame
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
109
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
110 upgrade_write($s, 'foo');
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
111 is(upgrade_read($s), 'bar', "upgrade response");
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
112
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
113 # send some big frame
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
114
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
115 upgrade_write($s, 'foo' x 16384);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
116 like(upgrade_read($s), qr/^(bar){16384}$/, "upgrade big response");
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
117
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
118 # send multiple frames
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
119
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
120 for my $i (1 .. 10) {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
121 upgrade_write($s, ('foo' x 16384) . $i);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
122 upgrade_write($s, 'bazz' . $i);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
123 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
124
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
125 for my $i (1 .. 10) {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
126 like(upgrade_read($s), qr/^(bar){16384}\d+$/, "upgrade $i");
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
127 is(upgrade_read($s), 'bazz' . $i, "upgrade small $i");
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
128 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
129 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
130
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
131 push @r, $s ? ${*$s}->{_upgrade_private}->{r} : 'failed';
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
132 undef $s;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
133
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
134 # establish connection with some pipelined data
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
135 # and make sure they are correctly passed upstream
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
136
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
137 $s = upgrade_connect(message => "foo");
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
138 ok($s, "handshake pipelined");
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
139
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
140 SKIP: {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
141 skip "handshake failed", 2 unless $s;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
142
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
143 is(upgrade_read($s), "bar", "response pipelined");
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
144
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
145 upgrade_write($s, "foo");
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
146 is(upgrade_read($s), "bar", "next to pipelined");
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
147 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
148
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
149 push @r, $s ? ${*$s}->{_upgrade_private}->{r} : 'failed';
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
150 undef $s;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
151
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
152 # connection should not be upgraded unless upgrade was actually
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
153 # requested and allowed by configuration
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
154
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
155 $s = upgrade_connect(noheader => 1);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
156 ok(!$s, "handshake noupgrade");
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
157
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
158 # bytes sent on upgraded connection, fixed in c2f309fb7ad2 (1.7.11)
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
159 # verify with 1) data actually read by client, 2) expected data from backend
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
160
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
161 $t->stop();
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
162
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
163 open my $f, '<', "$d/cc.log" or die "Can't open cc.log: $!";
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
164
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
165 is($f->getline(), shift (@r) . " 540793\n", 'log - bytes');
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
166 is($f->getline(), shift (@r) . " 22\n", 'log - bytes pipelined');
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
167 is($f->getline(), "0 0\n", 'log - bytes noupgrade');
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
168
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
169 ###############################################################################
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
170
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
171 sub upgrade_connect {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
172 my (%opts) = @_;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
173
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
174 my $s = IO::Socket::SSL->new(
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
175 Proto => 'tcp',
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
176 PeerAddr => '127.0.0.1:8080',
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
177 SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
178 )
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
179 or die "Can't connect to nginx: $!\n";
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
180
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
181 # send request, $h->to_string
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
182
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
183 my $buf = "GET / HTTP/1.1" . CRLF
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
184 . "Host: localhost" . CRLF
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
185 . ($opts{noheader} ? '' : "Upgrade: foo" . CRLF)
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
186 . "Connection: Upgrade" . CRLF . CRLF;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
187
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
188 $buf .= $opts{message} . CRLF if defined $opts{message};
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
189
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
190 local $SIG{PIPE} = 'IGNORE';
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
191
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
192 log_out($buf);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
193 $s->syswrite($buf);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
194
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
195 # read response
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
196
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
197 my $got = '';
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
198 $buf = '';
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
199
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
200 while (1) {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
201 $buf = upgrade_getline($s);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
202 last unless defined $buf and length $buf;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
203 log_in($buf);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
204 $got .= $buf;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
205 last if $got =~ /\x0d?\x0a\x0d?\x0a$/;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
206 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
207
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
208 # parse server response
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
209
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
210 return if $got !~ m!HTTP/1.1 101!;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
211
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
212 # make sure next line is "handshaked"
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
213
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
214 $buf = upgrade_read($s);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
215
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
216 return if !defined $buf or $buf ne 'handshaked';
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
217 return $s;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
218 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
219
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
220 sub upgrade_getline {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
221 my ($s) = @_;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
222 my ($h, $buf, $line);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
223
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
224 ${*$s}->{_upgrade_private} ||= { b => '', r => 0 };
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
225 $h = ${*$s}->{_upgrade_private};
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
226
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
227 if ($h->{b} =~ /^(.*?\x0a)(.*)/ms) {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
228 $h->{b} = $2;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
229 return $1;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
230 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
231
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
232 $s->blocking(0);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
233 while (IO::Select->new($s)->can_read(1.5)) {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
234 my $n = $s->sysread($buf, 1024);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
235 last unless $n;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
236
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
237 $h->{b} .= $buf;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
238 $h->{r} += $n;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
239
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
240 if ($h->{b} =~ /^(.*?\x0a)(.*)/ms) {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
241 $h->{b} = $2;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
242 return $1;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
243 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
244 };
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
245 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
246
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
247 sub upgrade_write {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
248 my ($s, $message) = @_;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
249
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
250 $message = $message . CRLF;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
251
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
252 local $SIG{PIPE} = 'IGNORE';
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
253
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
254 $s->blocking(0);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
255 while (IO::Select->new($s)->can_write(1.5)) {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
256 my $n = $s->syswrite($message);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
257 last unless $n;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
258 $message = substr($message, $n);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
259 last unless length $message;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
260 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
261
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
262 if (length $message) {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
263 $s->close();
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
264 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
265 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
266
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
267 sub upgrade_read {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
268 my ($s) = @_;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
269 my $m = upgrade_getline($s);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
270 $m =~ s/\x0d?\x0a// if defined $m;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
271 log_in($m);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
272 return $m;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
273 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
274
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
275 ###############################################################################
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
276
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
277 sub upgrade_fake_daemon {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
278 my $server = IO::Socket::INET->new(
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
279 Proto => 'tcp',
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
280 LocalAddr => '127.0.0.1:8081',
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
281 Listen => 5,
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
282 Reuse => 1
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
283 )
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
284 or die "Can't create listening socket: $!\n";
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
285
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
286 while (my $client = $server->accept()) {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
287 upgrade_handle_client($client);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
288 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
289 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
290
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
291 sub upgrade_handle_client {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
292 my ($client) = @_;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
293
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
294 $client->autoflush(1);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
295 $client->blocking(0);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
296
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
297 my $poll = IO::Poll->new;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
298
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
299 my $handshake = 1;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
300 my $unfinished = '';
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
301 my $buffer = '';
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
302 my $n;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
303
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
304 log2c("(new connection $client)");
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
305
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
306 while (1) {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
307 $poll->mask($client => ($buffer ? POLLIN|POLLOUT : POLLIN));
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
308 my $p = $poll->poll(0.5);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
309 log2c("(poll $p)");
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
310
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
311 foreach my $reader ($poll->handles(POLLIN)) {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
312 $n = $client->sysread(my $chunk, 65536);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
313 return unless $n;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
314
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
315 log2i($chunk);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
316
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
317 if ($handshake) {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
318 $buffer .= $chunk;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
319 next unless $buffer =~ /\x0d?\x0a\x0d?\x0a$/;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
320
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
321 log2c("(handshake done)");
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
322
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
323 $handshake = 0;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
324 $buffer = 'HTTP/1.1 101 Switching' . CRLF
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
325 . 'Upgrade: foo' . CRLF
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
326 . 'Connection: Upgrade' . CRLF . CRLF
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
327 . 'handshaked' . CRLF;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
328
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
329 log2o($buffer);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
330
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
331 next;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
332 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
333
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
334 $unfinished .= $chunk;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
335
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
336 if ($unfinished =~ m/\x0d?\x0a\z/) {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
337 $unfinished =~ s/foo/bar/g;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
338 log2o($unfinished);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
339 $buffer .= $unfinished;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
340 $unfinished = '';
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
341 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
342 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
343
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
344 foreach my $writer ($poll->handles(POLLOUT)) {
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
345 next unless length $buffer;
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
346 $n = $writer->syswrite($buffer);
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
347 substr $buffer, 0, $n, '';
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
348 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
349 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
350 }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
351
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
352 sub log2i { Test::Nginx::log_core('|| <<', @_); }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
353 sub log2o { Test::Nginx::log_core('|| >>', @_); }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
354 sub log2c { Test::Nginx::log_core('||', @_); }
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
355
5bb19f7448b5 Tests: Upgrade handling tests with http ssl module.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
356 ###############################################################################