comparison ignore_invalid_headers.t @ 1522:d8684b300d22

Tests: ignore_invalid_headers, underscores_in_headers directives.
author Sergey Kandaurov <pluknet@nginx.com>
date Fri, 18 Oct 2019 13:33:34 +0300
parents
children 5ac6efbe5552
comparison
equal deleted inserted replaced
1521:b6699ffd9ddd 1522:d8684b300d22
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for ignore_invalid_headers, underscores_in_headers directives.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 use Socket qw/ CRLF /;
16 use MIME::Base64 qw/ encode_base64 decode_base64 /;
17
18 BEGIN { use FindBin; chdir($FindBin::Bin); }
19
20 use lib 'lib';
21 use Test::Nginx;
22
23 ###############################################################################
24
25 select STDERR; $| = 1;
26 select STDOUT; $| = 1;
27
28 my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(9)
29 ->write_file_expand('nginx.conf', <<'EOF');
30
31 %%TEST_GLOBALS%%
32
33 daemon off;
34
35 events {
36 }
37
38 http {
39 %%TEST_GLOBALS_HTTP%%
40
41 server {
42 listen 127.0.0.1:8080;
43 server_name localhost;
44
45 ignore_invalid_headers off;
46
47 location / {
48 proxy_pass http://127.0.0.1:8085;
49 }
50
51 location /v {
52 add_header X-Cookie $http_cookie;
53 }
54 }
55
56 server {
57 listen 127.0.0.1:8081;
58 server_name localhost;
59
60 location / {
61 proxy_pass http://127.0.0.1:8085;
62 }
63 }
64
65 server {
66 listen 127.0.0.1:8082;
67 server_name localhost;
68
69 underscores_in_headers on;
70
71 location / {
72 proxy_pass http://127.0.0.1:8085;
73 }
74 }
75 }
76
77 EOF
78
79 $t->write_file('index.html', '');
80 $t->write_file('v', '');
81 $t->run_daemon(\&http_daemon);
82 $t->run()->waitforsocket('127.0.0.1:' . port(8085));
83
84 ###############################################################################
85
86 my $us = 'GET / HTTP/1.0' . CRLF
87 . 'x_foo: x-bar' . CRLF . CRLF;
88 my $us2 = 'GET / HTTP/1.0' . CRLF
89 . '_foo: x-bar' . CRLF . CRLF;
90 my $bad = 'GET / HTTP/1.0' . CRLF
91 . 'x.foo: x-bar' . CRLF . CRLF;
92 my $bad2 = 'GET / HTTP/1.0' . CRLF
93 . '.foo: x-bar' . CRLF . CRLF;
94
95 # ignore_invalid_headers off;
96
97 like(get($us, 8080), qr/x-bar/, 'off - underscore');
98 like(get($us2, 8080), qr/x-bar/, 'off - underscore first');
99 like(get($bad, 8080), qr/x-bar/, 'off - bad');
100 like(get($bad2, 8080), qr/x-bar/, 'off - bad first');
101
102 # ignore_invalid_headers off; headers parsing post 8f55cb5c7e79
103
104 TODO: {
105 local $TODO = 'not yet' unless $t->has_version('1.17.5');
106
107 unlike(http('GET /v HTTP/1.0' . CRLF
108 . 'Host: localhost' . CRLF
109 . 'coo: foo' . CRLF
110 . '</kie>: x-bar' . CRLF . CRLF), qr/x-bar/, 'off - several');
111
112 }
113
114 # ignore_invalid_headers on;
115
116 unlike(get($us, 8081), qr/x-bar/, 'on - underscore');
117 unlike(get($us2, 8081), qr/x-bar/, 'on - underscore first');
118
119 # ignore_invalid_headers on; underscores_in_headers on;
120
121 like(get($us, 8082), qr/x-bar/, 'underscores_in_headers');
122 like(get($us2, 8082), qr/x-bar/, 'underscores_in_headers - first');
123
124 ###############################################################################
125
126 sub get {
127 my ($msg, $port) = @_;
128
129 my $s = IO::Socket::INET->new('127.0.0.1:' . port($port)) or die;
130 my ($headers) = http($msg, socket => $s) =~ /X-Headers: (\w+)/;
131 decode_base64($headers);
132 }
133
134 ###############################################################################
135
136 sub http_daemon {
137 my $once = 1;
138 my $server = IO::Socket::INET->new(
139 Proto => 'tcp',
140 LocalHost => '127.0.0.1:' . port(8085),
141 Listen => 5,
142 Reuse => 1
143 )
144 or die "Can't create listening socket: $!\n";
145
146 local $SIG{PIPE} = 'IGNORE';
147
148 while (my $client = $server->accept()) {
149 $client->autoflush(1);
150
151 my $headers = '';
152 my $uri = '';
153
154 while (<$client>) {
155 $headers .= $_;
156 last if (/^\x0d?\x0a?$/);
157 }
158
159 $headers = encode_base64($headers, "");
160
161 print $client <<EOF;
162 HTTP/1.1 200 OK
163 Connection: close
164 X-Headers: $headers
165
166 EOF
167
168 }
169 }
170
171 ###############################################################################