-
Notifications
You must be signed in to change notification settings - Fork 76
/
fastcgi_body.t
183 lines (128 loc) · 3.7 KB
/
fastcgi_body.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/perl
# (C) Maxim Dounin
# Test for fastcgi backend with chunked request body.
###############################################################################
use warnings;
use strict;
use Test::More;
BEGIN { use FindBin; chdir($FindBin::Bin); }
use lib 'lib';
use Test::Nginx;
###############################################################################
select STDERR; $| = 1;
select STDOUT; $| = 1;
my $t = Test::Nginx->new()->has(qw/http fastcgi/)->plan(5)
->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
server {
listen 127.0.0.1:8080;
server_name localhost;
location / {
fastcgi_pass 127.0.0.1:8081;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param CONTENT_LENGTH $content_length;
}
}
}
EOF
$t->run_daemon(\&fastcgi_daemon);
$t->run()->waitforsocket('127.0.0.1:' . port(8081));
###############################################################################
like(http_get('/'), qr/X-Body: _eos\x0d?$/ms, 'fastcgi no body');
like(http_get_length('/', ''), qr/X-Body: _eos\x0d?$/ms, 'fastcgi empty body');
like(http_get_length('/', 'foobar'), qr/X-Body: foobar_eos\x0d?$/ms,
'fastcgi body');
like(http(<<EOF), qr/X-Body: foobar_eos\x0d?$/ms, 'fastcgi chunked');
GET / HTTP/1.1
Host: localhost
Connection: close
Transfer-Encoding: chunked
6
foobar
0
EOF
like(http(<<EOF), qr/X-Body: _eos\x0d?$/ms, 'fastcgi empty chunked');
GET / HTTP/1.1
Host: localhost
Connection: close
Transfer-Encoding: chunked
0
EOF
###############################################################################
sub http_get_length {
my ($url, $body) = @_;
my $length = length $body;
return http(<<EOF);
GET $url HTTP/1.1
Host: localhost
Connection: close
Content-Length: $length
$body
EOF
}
###############################################################################
# Simple FastCGI responder implementation.
# http://www.fastcgi.com/devkit/doc/fcgi-spec.html
sub fastcgi_read_record($) {
my ($buf) = @_;
my $h;
return undef unless length $$buf;
@{$h}{qw/ version type id clen plen /} = unpack("CCnnC", $$buf);
$h->{content} = substr $$buf, 8, $h->{clen};
$h->{padding} = substr $$buf, 8 + $h->{clen}, $h->{plen};
$$buf = substr $$buf, 8 + $h->{clen} + $h->{plen};
return $h;
}
sub fastcgi_respond($$$$) {
my ($socket, $version, $id, $body) = @_;
# stdout
$socket->write(pack("CCnnCx", $version, 6, $id, length($body), 0));
$socket->write($body);
# close stdout
$socket->write(pack("CCnnCx", $version, 6, $id, 0, 0));
# end request
$socket->write(pack("CCnnCx", $version, 3, $id, 8, 0));
$socket->write(pack("NCxxx", 0, 0));
}
sub fastcgi_daemon {
my $server = IO::Socket::INET->new(
Proto => 'tcp',
LocalAddr => '127.0.0.1:' . port(8081),
Listen => 5,
Reuse => 1
)
or die "Can't create listening socket: $!\n";
local $SIG{PIPE} = 'IGNORE';
while (my $client = $server->accept()) {
$client->autoflush(1);
Test::Nginx::log_core('||', "fastcgi connection");
$client->sysread(my $buf, 1024) or next;
my ($version, $id);
my $body = '';
while (my $h = fastcgi_read_record(\$buf)) {
$version = $h->{version};
$id = $h->{id};
Test::Nginx::log_core('||', "fastcgi record: "
. " $h->{version}, $h->{type}, $h->{id}, "
. "'$h->{content}'");
if ($h->{type} == 5) {
$body .= $h->{content} if $h->{clen} > 0;
# count stdin end-of-stream
$body .= '_eos' if $h->{clen} == 0;
}
}
# respond
fastcgi_respond($client, $version, $id, <<EOF);
Location: http://localhost/redirect
Content-Type: text/html
X-Body: $body
SEE-THIS
EOF
}
}
###############################################################################