| 1 | #!/usr/bin/perl
|
|---|
| 2 |
|
|---|
| 3 | # (C) Maxim Dounin
|
|---|
| 4 |
|
|---|
| 5 | # Tests for http proxy and prematurely closed connections. Incomplete
|
|---|
| 6 | # responses shouldn't loose information about their incompleteness.
|
|---|
| 7 |
|
|---|
| 8 | # In particular, incomplete responses:
|
|---|
| 9 | #
|
|---|
| 10 | # - shouldn't be cached
|
|---|
| 11 | #
|
|---|
| 12 | # - if a response is sent using chunked transfer encoding,
|
|---|
| 13 | # final chunk shouldn't be sent
|
|---|
| 14 |
|
|---|
| 15 | ###############################################################################
|
|---|
| 16 |
|
|---|
| 17 | use warnings;
|
|---|
| 18 | use strict;
|
|---|
| 19 |
|
|---|
| 20 | use Test::More;
|
|---|
| 21 |
|
|---|
| 22 | use Socket qw/ CRLF /;
|
|---|
| 23 |
|
|---|
| 24 | BEGIN { use FindBin; chdir($FindBin::Bin); }
|
|---|
| 25 |
|
|---|
| 26 | use lib 'lib';
|
|---|
| 27 | use Test::Nginx;
|
|---|
| 28 |
|
|---|
| 29 | ###############################################################################
|
|---|
| 30 |
|
|---|
| 31 | select STDERR; $| = 1;
|
|---|
| 32 | select STDOUT; $| = 1;
|
|---|
| 33 |
|
|---|
| 34 | plan(skip_all => 'win32') if $^O eq 'MSWin32';
|
|---|
| 35 |
|
|---|
| 36 | my $t = Test::Nginx->new()->has(qw/http proxy cache/)->plan(1)
|
|---|
| 37 | ->write_file_expand('nginx.conf', <<'EOF');
|
|---|
| 38 |
|
|---|
| 39 | %%TEST_GLOBALS%%
|
|---|
| 40 |
|
|---|
| 41 | daemon off;
|
|---|
| 42 |
|
|---|
| 43 | events {
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | http {
|
|---|
| 47 | %%TEST_GLOBALS_HTTP%%
|
|---|
| 48 |
|
|---|
| 49 | proxy_cache_path %%TESTDIR%%/cache levels=1:2
|
|---|
| 50 | keys_zone=one:1m;
|
|---|
| 51 |
|
|---|
| 52 | server {
|
|---|
| 53 | listen 127.0.0.1:8080;
|
|---|
| 54 | server_name localhost;
|
|---|
| 55 |
|
|---|
| 56 | location / {
|
|---|
| 57 | proxy_pass http://127.0.0.1:8081;
|
|---|
| 58 | proxy_cache one;
|
|---|
| 59 | proxy_buffer_size 1k;
|
|---|
| 60 | proxy_buffers 4 1k;
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | EOF
|
|---|
| 66 |
|
|---|
| 67 | $t->run_daemon(\&http_daemon);
|
|---|
| 68 | $t->run()->waitforsocket('127.0.0.1:8081');
|
|---|
| 69 |
|
|---|
| 70 | ###############################################################################
|
|---|
| 71 |
|
|---|
| 72 | TODO: {
|
|---|
| 73 | local $TODO = 'not yet' unless $t->has_version('1.5.3');
|
|---|
| 74 |
|
|---|
| 75 | # if disk buffering fails for some reason, there should be
|
|---|
| 76 | # no final chunk
|
|---|
| 77 |
|
|---|
| 78 | chmod(0000, $t->testdir() . '/proxy_temp');
|
|---|
| 79 | like(http_get_11('/proxy/big.html', sleep => 0.5),
|
|---|
| 80 | qr/X(?!.*\x0d\x0a?0\x0d\x0a?)/s, 'no proxy temp');
|
|---|
| 81 |
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | ###############################################################################
|
|---|
| 85 |
|
|---|
| 86 | sub http_get_11 {
|
|---|
| 87 | my ($uri, %extra) = @_;
|
|---|
| 88 |
|
|---|
| 89 | return http(
|
|---|
| 90 | "GET $uri HTTP/1.1" . CRLF .
|
|---|
| 91 | "Connection: close" . CRLF .
|
|---|
| 92 | "Host: localhost" . CRLF . CRLF,
|
|---|
| 93 | %extra
|
|---|
| 94 | );
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | ###############################################################################
|
|---|
| 98 |
|
|---|
| 99 | sub http_daemon {
|
|---|
| 100 | my $server = IO::Socket::INET->new(
|
|---|
| 101 | Proto => 'tcp',
|
|---|
| 102 | LocalAddr => '127.0.0.1:8081',
|
|---|
| 103 | Listen => 5,
|
|---|
| 104 | Reuse => 1
|
|---|
| 105 | )
|
|---|
| 106 | or die "Can't create listening socket: $!\n";
|
|---|
| 107 |
|
|---|
| 108 | local $SIG{PIPE} = 'IGNORE';
|
|---|
| 109 |
|
|---|
| 110 | while (my $client = $server->accept()) {
|
|---|
| 111 | $client->autoflush(1);
|
|---|
| 112 |
|
|---|
| 113 | my $headers = '';
|
|---|
| 114 | my $uri = '';
|
|---|
| 115 |
|
|---|
| 116 | while (<$client>) {
|
|---|
| 117 | $headers .= $_;
|
|---|
| 118 | last if (/^\x0d?\x0a?$/);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i;
|
|---|
| 122 |
|
|---|
| 123 | print $client
|
|---|
| 124 | "HTTP/1.1 200 OK" . CRLF .
|
|---|
| 125 | "Content-Length: 1000010" . CRLF .
|
|---|
| 126 | "Cache-Control: max-age=300" . CRLF .
|
|---|
| 127 | "Connection: close" . CRLF .
|
|---|
| 128 | CRLF .
|
|---|
| 129 | ("X" x (1000*1000)) . "finished" . CRLF;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | ###############################################################################
|
|---|