1#!/usr/bin/env perl 2BEGIN { 3 # add current source dir to the include-path 4 # we need this for make distcheck 5 (my $srcdir = $0) =~ s,/[^/]+$,/,; 6 unshift @INC, $srcdir; 7} 8 9use strict; 10use IO::Socket; 11use Test::More tests => 26; 12use LightyTest; 13 14my $tf = LightyTest->new(); 15my $has_pcre = $tf->has_feature("PCRE support"); 16$ENV{condition_include_file} = $has_pcre ? "var-include-sub.conf" : "nonexistent-glob*"; 17my $t; 18 19$ENV{"env_test"} = "good_env"; 20 21$tf->{CONFIGFILE} = 'condition.conf'; 22ok($tf->start_proc == 0, "Starting lighttpd") or die(); 23 24$t->{REQUEST} = ( <<EOF 25GET /index.html HTTP/1.0 26Host: www.example.org 27EOF 28 ); 29$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_1" } ]; 30ok($tf->handle_http($t) == 0, 'config deny'); 31 32$t->{REQUEST} = ( <<EOF 33GET /index.html HTTP/1.0 34Host: test1.example.org 35EOF 36 ); 37$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_2" } ]; 38ok($tf->handle_http($t) == 0, '2nd child of chaining'); 39 40$t->{REQUEST} = ( <<EOF 41GET /index.html HTTP/1.0 42Host: test2.example.org 43EOF 44 ); 45$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_3" } ]; 46ok($tf->handle_http($t) == 0, '3rd child of chaining'); 47 48$t->{REQUEST} = ( <<EOF 49GET /index.html HTTP/1.0 50Host: test3.example.org 51EOF 52 ); 53$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_5" } ]; 54ok($tf->handle_http($t) == 0, 'nesting'); 55 56$t->{REQUEST} = ( <<EOF 57GET /subdir/index.html HTTP/1.0 58Host: test4.example.org 59EOF 60 ); 61$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_7" } ]; 62ok($tf->handle_http($t) == 0, 'url subdir'); 63 64$t->{REQUEST} = ( <<EOF 65GET /subdir/../css/index.html HTTP/1.0 66Host: test4.example.org 67EOF 68 ); 69$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_6" } ]; 70ok($tf->handle_http($t) == 0, 'url subdir with path traversal'); 71 72$t->{REQUEST} = ( <<EOF 73GET / HTTP/1.0 74EOF 75 ); 76$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Server' => 'lighttpd-1.4.x' } ]; 77ok($tf->handle_http($t) == 0, 'condition: handle if before else branches'); 78 79$t->{REQUEST} = ( <<EOF 80GET /show/other/server-tag HTTP/1.0 81EOF 82 ); 83$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Server' => 'special tag' } ]; 84ok($tf->handle_http($t) == 0, 'condition: handle if before else branches #2'); 85 86 87## config includes 88 89$t->{REQUEST} = ( "GET /index.html HTTP/1.0\r\nHost: www.example.org\r\n" ); 90$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_1" } ]; 91ok($tf->handle_http($t) == 0, 'basic test'); 92 93SKIP: { 94 skip "skipping tests requiring PCRE", 15 unless $has_pcre; 95 96$t->{REQUEST} = ( <<EOF 97GET /rewrite/all/some+test%3axxx%20with%20space HTTP/1.0 98Host: test.example.org 99EOF 100 ); 101$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => '/some+test%3Axxx%20with%20space' } ]; 102ok($tf->handle_http($t) == 0, 'rewritten urls work with encoded path'); 103 104my $myvar = "good"; 105my $server_name = "test.example.org"; 106my $mystr = "string"; 107$mystr .= "_append"; 108my $tests = { 109 "include" => "/good_include", 110 "concat" => "/good_" . "concat", 111 "servername1" => "/good_" . $server_name, 112 "servername2" => $server_name . "/good_", 113 "servername3" => "/good_" . $server_name . "/", 114 "var.myvar" => "/good_var_myvar" . $myvar, 115 "myvar" => "/good_myvar" . $myvar, 116 "env" => "/" . $ENV{"env_test"}, 117 118 "number1" => "/good_number" . "1", 119 "number2" => "1" . "/good_number", 120 "array_append" => "/good_array_append", 121 "string_append" => "/good_" . $mystr, 122 "number_append" => "/good_" . "2", 123 124 "include_shell" => "/good_include_shell_" . "456" 125}; 126 127foreach my $test (keys %{ $tests }) { 128 my $expect = $tests->{$test}; 129 $t->{REQUEST} = ( <<EOF 130GET /$test HTTP/1.0 131Host: $server_name 132EOF 133 ); 134 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => $expect } ]; 135 ok($tf->handle_http($t) == 0, $test); 136} 137 138} 139 140ok($tf->stop_proc == 0, "Stopping lighttpd"); 141