blob: 178df3b2fd1dd7d084b9e45a69bbff2cb3d252db [file] [log] [blame]
Maxim Dounin53eb4ff2008-09-26 18:28:02 +04001#!/usr/bin/perl
2
3# (C) Maxim Dounin
4
5# Test for http backend not closing connection properly after sending full
6# reply. This is in fact backend bug, but it seems common, and anyway
7# correct handling is required to support persistent connections.
8
Maxim Dounin140801b2008-09-30 22:58:25 +04009# There are actually 2 nginx problems here:
10#
11# 1. It doesn't send reply in-time even if got Content-Length and all the data.
12#
13# 2. If upstream times out some data may be left in input buffer and won't be
14# sent to downstream.
15
Maxim Dounin53eb4ff2008-09-26 18:28:02 +040016###############################################################################
17
18use warnings;
19use strict;
20
Maxim Douninff9b36a2009-12-26 16:06:17 +030021use Test::More;
Maxim Douninc68be762008-09-29 02:10:07 +040022
23use IO::Select;
Maxim Dounin53eb4ff2008-09-26 18:28:02 +040024
25BEGIN { use FindBin; chdir($FindBin::Bin); }
26
27use lib 'lib';
28use Test::Nginx;
29
30###############################################################################
31
32select STDERR; $| = 1;
33select STDOUT; $| = 1;
34
Maxim Douninff9b36a2009-12-26 16:06:17 +030035my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(4);
Maxim Dounin53eb4ff2008-09-26 18:28:02 +040036
37$t->write_file_expand('nginx.conf', <<'EOF');
38
Maxim Douninc6acedb2009-10-14 02:23:52 +040039%%TEST_GLOBALS%%
40
Maxim Dounin35773f62013-01-24 02:17:36 +040041daemon off;
Maxim Dounin53eb4ff2008-09-26 18:28:02 +040042
43events {
Maxim Dounin53eb4ff2008-09-26 18:28:02 +040044}
45
46http {
Maxim Douninc6acedb2009-10-14 02:23:52 +040047 %%TEST_GLOBALS_HTTP%%
Maxim Dounine3aaba52008-10-16 19:16:46 +040048
Maxim Dounin53eb4ff2008-09-26 18:28:02 +040049 server {
Maxim Dounin3c472202008-11-02 15:01:41 +030050 listen 127.0.0.1:8080;
Maxim Dounin53eb4ff2008-09-26 18:28:02 +040051 server_name localhost;
52
53 location / {
Maxim Dounin3c472202008-11-02 15:01:41 +030054 proxy_pass http://127.0.0.1:8081;
Sergey Kandaurov48036f22015-10-23 16:24:53 +030055 proxy_read_timeout 2s;
Maxim Dounin53eb4ff2008-09-26 18:28:02 +040056 }
Maxim Dounin140801b2008-09-30 22:58:25 +040057
58 location /uselen {
Maxim Dounin3c472202008-11-02 15:01:41 +030059 proxy_pass http://127.0.0.1:8081;
Maxim Dounin140801b2008-09-30 22:58:25 +040060
61 # test will wait only 2s for reply, we it will fail if
62 # Content-Length not used as a hint
Maxim Dounin35f59e22009-11-24 13:57:43 +030063
Maxim Dounin140801b2008-09-30 22:58:25 +040064 proxy_read_timeout 10s;
65 }
Maxim Dounin53eb4ff2008-09-26 18:28:02 +040066 }
67}
68
69EOF
70
71$t->run_daemon(\&http_noclose_daemon);
Maxim Dounin5f3f7ae2013-06-08 07:02:11 +040072$t->run()->waitforsocket('127.0.0.1:8081');
Maxim Dounin53eb4ff2008-09-26 18:28:02 +040073
74###############################################################################
75
Maxim Dounin323cf242008-10-11 10:58:43 +040076like(http_get('/'), qr/SEE-THIS/, 'request to bad backend');
77like(http_get('/multi'), qr/AND-THIS/, 'bad backend - multiple packets');
Maxim Dounin323cf242008-10-11 10:58:43 +040078like(http_get('/uselen'), qr/SEE-THIS/, 'content-length actually used');
Maxim Dounin53eb4ff2008-09-26 18:28:02 +040079
Maxim Douninebdcfc62012-06-03 14:47:10 +040080TODO: {
81local $TODO = 'not yet';
82local $SIG{__WARN__} = sub {};
83
84like(http_get('/nolen'), qr/SEE-THIS/, 'bad backend - no content length');
85
Maxim Dounin53eb4ff2008-09-26 18:28:02 +040086}
87
88###############################################################################
89
Maxim Dounin53eb4ff2008-09-26 18:28:02 +040090sub http_noclose_daemon {
91 my $server = IO::Socket::INET->new(
Maxim Douninb56a54c2009-04-12 06:46:21 +040092 Proto => 'tcp',
93 LocalAddr => '127.0.0.1:8081',
94 Listen => 5,
95 Reuse => 1
Maxim Dounin53eb4ff2008-09-26 18:28:02 +040096 )
Maxim Douninb56a54c2009-04-12 06:46:21 +040097 or die "Can't create listening socket: $!\n";
Maxim Dounin53eb4ff2008-09-26 18:28:02 +040098
Maxim Dounin5f3f7ae2013-06-08 07:02:11 +040099 local $SIG{PIPE} = 'IGNORE';
100
Maxim Dounin53eb4ff2008-09-26 18:28:02 +0400101 while (my $client = $server->accept()) {
Maxim Douninb56a54c2009-04-12 06:46:21 +0400102 $client->autoflush(1);
Maxim Dounin53eb4ff2008-09-26 18:28:02 +0400103
Maxim Douninc68be762008-09-29 02:10:07 +0400104 my $multi = 0;
Maxim Dounin140801b2008-09-30 22:58:25 +0400105 my $nolen = 0;
Maxim Douninc68be762008-09-29 02:10:07 +0400106
Maxim Douninb56a54c2009-04-12 06:46:21 +0400107 while (<$client>) {
Maxim Douninc68be762008-09-29 02:10:07 +0400108 $multi = 1 if /multi/;
Maxim Dounin140801b2008-09-30 22:58:25 +0400109 $nolen = 1 if /nolen/;
Maxim Douninb56a54c2009-04-12 06:46:21 +0400110 last if (/^\x0d?\x0a?$/);
111 }
Maxim Dounin53eb4ff2008-09-26 18:28:02 +0400112
Maxim Dounin140801b2008-09-30 22:58:25 +0400113 if ($nolen) {
Maxim Douninc68be762008-09-29 02:10:07 +0400114
Maxim Dounin140801b2008-09-30 22:58:25 +0400115 print $client <<'EOF';
Maxim Dounin53eb4ff2008-09-26 18:28:02 +0400116HTTP/1.1 200 OK
Maxim Dounin140801b2008-09-30 22:58:25 +0400117Connection: close
118
119TEST-OK-IF-YOU-SEE-THIS
120EOF
121 } elsif ($multi) {
122
Maxim Douninb56a54c2009-04-12 06:46:21 +0400123 print $client <<"EOF";
Maxim Dounin140801b2008-09-30 22:58:25 +0400124HTTP/1.1 200 OK
125Content-Length: 32
Maxim Dounin53eb4ff2008-09-26 18:28:02 +0400126Connection: close
127
128TEST-OK-IF-YOU-SEE-THIS
129EOF
Maxim Douninc68be762008-09-29 02:10:07 +0400130
Maxim Douninc68be762008-09-29 02:10:07 +0400131 select undef, undef, undef, 0.1;
132 print $client 'AND-THIS';
Maxim Dounin140801b2008-09-30 22:58:25 +0400133
134 } else {
135
Maxim Douninb56a54c2009-04-12 06:46:21 +0400136 print $client <<"EOF";
Maxim Dounin140801b2008-09-30 22:58:25 +0400137HTTP/1.1 200 OK
138Content-Length: 24
139Connection: close
140
141TEST-OK-IF-YOU-SEE-THIS
142EOF
Maxim Douninc68be762008-09-29 02:10:07 +0400143 }
144
145 my $select = IO::Select->new($client);
Maxim Douninb56a54c2009-04-12 06:46:21 +0400146 $select->can_read(10);
147 close $client;
Maxim Dounin53eb4ff2008-09-26 18:28:02 +0400148 }
149}
150
151###############################################################################