#! /usr/bin/perl -w use strict; use IO::Socket; use Test::More tests => 114; my $testname; my @request; my @response; my $configfile = 'lighttpd.conf'; my $lighttpd_path = '../src/lighttpd'; my $pidfile = '/tmp/lighttpd/lighttpd.pid'; my $pidoffile = '/tmp/lighttpd/pidof.pid'; sub pidof { my $prog = $_[0]; open F, "ps ax | grep $prog | awk '{ print \$1 }'|" or open F, "ps -ef | grep $prog | awk '{ print \$2 }'|" or return -1; my $pid = ; close F; return $pid; } sub stop_proc { open F, $pidfile or return -1; my $pid = ; close F; kill('TERM',$pid) or return -1; select(undef, undef, undef, 0.25); return 0; } sub start_proc { # kill old proc if necessary stop_proc; unlink($pidfile); system($lighttpd_path." -f ".$configfile); if (-e $pidfile) { return 0; } else { return -1; } } sub handle_http { my $EOL = "\015\012"; my $BLANK = $EOL x 2; my $port = 2048; my $host = "127.0.0.1"; my $remote = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $host, PeerPort => $port) or return -1; $remote->autoflush(1); foreach(@request) { # pipeline requests s/\r//g; s/\n/$EOL/g; print $remote $_.$BLANK; } my $lines = ""; # read everything while(<$remote>) { $lines .= $_; } close $remote; my $href; foreach $href (@response) { # first line is always response header my %resp_hdr; my $resp_body; my $resp_line; my $conditions = $_; for (my $ln = 0; defined $lines; $ln++) { (my $line, $lines) = split($EOL, $lines, 2); # header finished last if(length($line) == 0); if ($ln == 0) { # response header $resp_line = $line; } else { # response vars if ($line =~ /^([^:]+):\s*(.+)$/) { (my $h = $1) =~ tr/[A-Z]/[a-z]/; $resp_hdr{$h} = $2; } else { return -1; } } } # check length if (defined $resp_hdr{"content-length"}) { ($resp_body, $lines) = split("^.".$resp_hdr{"content-length"}, $lines, 2); } else { $resp_body = $lines; undef $lines; } # check conditions if ($resp_line =~ /^(HTTP\/1\.[01]) ([0-9]{3}) .+$/) { if ($href->{'HTTP-Protocol'} ne $1) { diag(sprintf("proto failed: expected '%s', got '%s'\n", $href->{'HTTP-Protocol'}, $1)); return -1; } if ($href->{'HTTP-Status'} ne $2) { diag(sprintf("status failed: expected '%s', got '%s'\n", $href->{'HTTP-Status'}, $2)); return -1; } } else { return -1; } if (defined $href->{'HTTP-Content'}) { if ($href->{'HTTP-Content'} ne $resp_body) { diag(sprintf("body failed: expected '%s', got '%s'\n", $href->{'HTTP-Content'}, $resp_body)); return -1; } } if (defined $href->{'-HTTP-Content'}) { if (defined $resp_body && $resp_body ne '') { diag(sprintf("body failed: expected empty body, got '%s'\n", $resp_body)); return -1; } } foreach (keys %{ $href }) { next if $_ eq 'HTTP-Protocol'; next if $_ eq 'HTTP-Status'; next if $_ eq 'HTTP-Content'; next if $_ eq '-HTTP-Content'; (my $k = $_) =~ tr/[A-Z]/[a-z]/; my $no_val = 0; if (substr($k, 0, 1) eq '+') { $k = substr($k, 1); $no_val = 1; } if (!defined $resp_hdr{$k}) { diag(sprintf("required header '%s' is missing\n", $k)); return -1; } if ($no_val == 0 && $href->{$_} ne $resp_hdr{$k}) { diag(sprintf("response-header failed: expected '%s', got '%s'\n", $href->{$_}, $resp_hdr{$k})); return -1; } } } # we should have sucked up everything return -1 if (defined $lines); return 0; } print "\nStart-Up\n"; ok(start_proc == 0, "Starting lighttpd") or die(); print "\nRequest Line\n"; @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'Valid HTTP/1.0 Request') or die(); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'missing Protocol'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'missing protocol + unknown method'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'missing protocol + unknown method + missing URI'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 501 } ); ok(handle_http == 0, 'unknown method'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 505 } ); ok(handle_http == 0, 'unknown protocol'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'absolute URI'); print "\nLow-Level Request-Header Parsing\n"; @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'whitespace after key'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'whitespace with-in key'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'no whitespace'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'line-folding'); print "\nLow-Level Request-Header Parsing - URI\n"; @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'URL-encoding'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 404 } ); ok(handle_http == 0, 'URL-encoding, %00'); print "\nLow-Level Request-Header Parsing - Host:\n"; @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'hostname'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'IPv4 address'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'IPv6 address'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'hostname + port'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'IPv4 address + port'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'IPv6 address + port'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'directory traversal'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'leading and trailing dot'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'trailing dot is ok'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'leading dot'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'two dots'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'broken port-number'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'negative port-number'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'port given but host missing'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'port and host are broken'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'allowed characters in host-name'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'leading dash'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'dot only'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'broken IPv4 address - non-digit'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'broken IPv4 address - too short'); print "\nLow-Level Request-Header Parsing - Content-Length:\n"; @request = ( < 'HTTP/1.0', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'negative Content-Length'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 413 } ); ok(handle_http == 0, 'Content-Length > max-request-size'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 411 } ); ok(handle_http == 0, 'Content-Length is empty'); print "\nLow-Level Request-Header Parsing - HTTP/1.1\n"; @request = ( < 'HTTP/1.1', 'HTTP-Status' => 400 } ); ok(handle_http == 0, 'Host missing'); print "\nLow-Level Response-Header Parsing - HTTP/1.1\n"; @request = ( < 'HTTP/1.1', 'HTTP-Status' => 200, '+Date' => '' } ); ok(handle_http == 0, 'Date header'); @request = ( < 'HTTP/1.1', 'HTTP-Status' => 400, 'Connection' => 'close' } ); ok(handle_http == 0, 'Host missing'); print "\nLow-Level Response-Header Parsing - Content-Length:\n"; @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => '6' } ); ok(handle_http == 0, 'Content-Length for text/html'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => '6' } ); ok(handle_http == 0, 'Content-Length for text/plain'); print "\nLow-Level Response-Header Parsing - Location:\n"; @request = ( < 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://localhost:2048/dummydir/' } ); ok(handle_http == 0, 'internal redirect in directory'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://localhost:2048/dummydir/?foo' } ); ok(handle_http == 0, 'internal redirect in directory + querystring'); print "\nBasic Request-Handling\n"; @request = ( < 'HTTP/1.0', 'HTTP-Status' => 404 } ); ok(handle_http == 0, 'file not found'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 404 } ); ok(handle_http == 0, 'file not found + querystring'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '12345'."\n", 'Content-Type' => 'text/plain' } ); ok(handle_http == 0, 'GET, content == 12345, mimetype text/plain'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '12345'."\n", 'Content-Type' => 'text/html' } ); ok(handle_http == 0, 'GET, content == 12345, mimetype text/html'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '12345'."\n", 'Content-Type' => 'application/octet-stream' } ); ok(handle_http == 0, 'GET, content == 12345, mimetype application/octet-stream'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 411 } ); ok(handle_http == 0, 'POST request, no Content-Length'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'POST request, empty request-body'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, '-HTTP-Content' => ''} ); ok(handle_http == 0, 'HEAD request, no content'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, '-HTTP-Content' => '', 'Content-Type' => 'text/html', 'Content-Length' => '6'} ); ok(handle_http == 0, 'HEAD request, mimetype text/html, content-length'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 404, '-HTTP-Content' => '' } ); ok(handle_http == 0, 'HEAD request, file-not-found, query-string'); @request = ( < 'HTTP/1.1', 'HTTP-Status' => 417, '-HTTP-Content' => ''} ); ok(handle_http == 0, 'Continue, Expect'); ## ranges @request = ( < 'HTTP/1.0', 'HTTP-Status' => 206, 'HTTP-Content' => '1234' } ); ok(handle_http == 0, 'GET, Range 0-3'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 206, 'HTTP-Content' => '45'."\n" } ); ok(handle_http == 0, 'GET, Range -3'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 206, 'HTTP-Content' => '45'."\n" } ); ok(handle_http == 0, 'GET, Range 3-'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 206, 'HTTP-Content' => < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'GET, Range 0--'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'GET, Range -2-3'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 416, 'HTTP-Content' => < 416 - Requested Range Not Satisfiable

416 - Requested Range Not Satisfiable

EOF } ); ok(handle_http == 0, 'GET, Range -0'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 416, 'HTTP-Content' => < 416 - Requested Range Not Satisfiable

416 - Requested Range Not Satisfiable

EOF } ); ok(handle_http == 0, 'GET, Range start out of range'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'larger headers'); print "\nmodules - mod_access\n"; @request = ( < 'HTTP/1.0', 'HTTP-Status' => 403 } ); ok(handle_http == 0, 'forbid access to ...~'); print "\nmodules - mod_auth\n"; @request = ( < 'HTTP/1.0', 'HTTP-Status' => 401 } ); ok(handle_http == 0, 'Missing Auth-token'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 401 } ); ok(handle_http == 0, 'Basic-Auth: Wrong Auth-token'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'Basic-Auth: Valid Auth-token'); # mod-cgi # print "\nmodules - mod_cgi\n"; @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'perl via cgi'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/cgi.pl' } ); ok(handle_http == 0, 'perl via cgi + pathinfo'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/foo' } ); ok(handle_http == 0, 'perl via cgi + pathinfo'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'NPH + perl, Bug #14'); print "\nmodules - mod_fastcgi\n"; SKIP: { skip "no PHP running on port 1026", 13 if pidof("php") == -1; @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'valid request'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 404 } ); ok(handle_http == 0, 'file not found'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'index-file handling'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 302, 'Location' => 'http://www.example.org:2048/' } ); ok(handle_http == 0, 'Status + Location via FastCGI'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, '$_SERVER["PHP_SELF"]'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/phpself.php' } ); ok(handle_http == 0, '$_SERVER["PHP_SELF"]'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ); ok(handle_http == 0, 'SERVER_NAME'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ); ok(handle_http == 0, 'SERVER_NAME'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ); ok(handle_http == 0, 'SERVER_NAME'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ); ok(handle_http == 0, 'SERVER_NAME'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'PATHINFO'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 404 } ); ok(handle_http == 0, 'PATHINFO on a directory'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/indexfile/index.php' } ); ok(handle_http == 0, 'PHP_SELF + Indexfile, Bug #3'); } print "\nmodules - mod_redirect\n"; @request = ( < 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://localhost:2048/' } ); ok(handle_http == 0, 'external redirect'); print "\nmodules - mod_compress\n"; @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, '+Vary' => '' } ); ok(handle_http == 0, 'Vary is set'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, '+Vary' => '', 'Content-Length' => '1288', '+Content-Encoding' => '' } ); ok(handle_http == 0, 'deflate - Content-Length and Content-Encoding is set'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, '+Vary' => '', '+Content-Encoding' => '' } ); ok(handle_http == 0, 'gzip - Content-Length and Content-Encoding is set'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, '+Vary' => '', '+Content-Encoding' => '' } ); ok(handle_http == 0, 'gzip, deflate - Content-Length and Content-Encoding is set'); print "\nmodules - mod_expire\n"; @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, '+Expires' => '' } ); ok(handle_http == 0, 'access'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, '+Expires' => '' } ); ok(handle_http == 0, 'modification'); print "\nmodules - mod_userdir\n"; # get current user @request = ( < 'HTTP/1.0', 'HTTP-Status' => 404 } ); ok(handle_http == 0, 'valid user'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://localhost:2048/~jan/' } ); ok(handle_http == 0, 'valid user + redirect'); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://www.example.org/~jan/' } ); ok(handle_http == 0, 'valid user + redirect'); print "\nclean up\n"; ok(stop_proc == 0, "Stopping lighttpd"); print "\nspecial config\n"; $configfile = 'fastcgi-10.conf'; ok(start_proc == 0, "Starting lighttpd with fastcgi-10.conf") or die(); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'zzz.example.org' } ); ok(handle_http == 0, 'FastCGI + Host'); ok(stop_proc == 0, "Stopping lighttpd"); $configfile = 'fastcgi-11.conf'; ok(start_proc == 0, "Starting lighttpd with fastcgi-11.conf") or die(); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'FastCGI - Auth'); ok(stop_proc == 0, "Stopping lighttpd"); $configfile = 'fastcgi-12.conf'; ok(start_proc == 0, "Starting lighttpd with fastcgi-12.conf") or die(); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 403 } ); ok(handle_http == 0, 'FastCGI - Auth'); ok(stop_proc == 0, "Stopping lighttpd"); $configfile = 'fastcgi-13.conf'; ok(start_proc == 0, "Starting lighttpd with fastcgi-13.conf") or die(); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200 } ); ok(handle_http == 0, 'FastCGI + local spawning'); ok(stop_proc == 0, "Stopping lighttpd"); $configfile = 'bug-06.conf'; ok(start_proc == 0, "Starting lighttpd with bug-06.conf") or die(); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/indexfile/index.php' } ); ok(handle_http == 0, 'Bug #6'); ok(stop_proc == 0, "Stopping lighttpd"); $configfile = 'bug-12.conf'; ok(start_proc == 0, "Starting lighttpd with bug-12.conf") or die(); @request = ( < 'HTTP/1.0', 'HTTP-Status' => 404, 'HTTP-Content' => '/indexfile/return-404.php' } ); ok(handle_http == 0, 'Bug #12'); ok(stop_proc == 0, "Stopping lighttpd");