| 1 | #!perl -w |
| 2 | use strict; |
| 3 | use Test::More; |
| 4 | use HTTP::Parser::XS qw(:all); |
| 5 | |
| 6 | plan skip_all => 'PP has no static limitations' |
| 7 | if $HTTP::Parser::XS::BACKEND eq 'pp'; |
| 8 | |
| 9 | my $MAX_HEADER_LEN = 1024; |
| 10 | my $MAX_HEADERS = 128; |
| 11 | |
| 12 | my $crlf = "\015\012"; |
| 13 | |
| 14 | note 'request parser'; |
| 15 | # on success |
| 16 | my $name = 'x' x $MAX_HEADER_LEN; # OK |
| 17 | my $request = "GET / HTTP/1.1" . $crlf |
| 18 | . "$name: 42" . $crlf |
| 19 | . $crlf; |
| 20 | |
| 21 | my $env = {}; |
| 22 | my $ret = parse_http_request($request, $env); |
| 23 | cmp_ok $ret, '>', 0, 'parsing status (success)'; |
| 24 | is $env->{REQUEST_METHOD}, 'GET'; |
| 25 | is $env->{'HTTP_' . uc $name}, 42, 'very long name'; |
| 26 | |
| 27 | # on fail |
| 28 | $name = 'x' x ($MAX_HEADER_LEN + 1); |
| 29 | $request = "GET / HTTP/1.1" . $crlf |
| 30 | . "$name: 42" . $crlf |
| 31 | . $crlf; |
| 32 | |
| 33 | $env = {}; |
| 34 | $ret = parse_http_request($request, $env); |
| 35 | cmp_ok $ret, '==', -1, 'parsing status (fail)'; |
| 36 | is $env->{REQUEST_METHOD}, undef; |
| 37 | is $env->{'HTTP_' . uc $name}, undef, 'too long name'; |
| 38 | |
| 39 | # too many headers |
| 40 | |
| 41 | $request = "GET / HTTP/1.1" . $crlf |
| 42 | . join($crlf, map { "X$_: $_" } 0 .. $MAX_HEADERS) . $crlf |
| 43 | . $crlf; |
| 44 | |
| 45 | $env = {}; |
| 46 | $ret = parse_http_request($request, $env); |
| 47 | is $ret, -1, 'too many headers'; |
| 48 | |
| 49 | note 'response parser'; |
| 50 | # on success |
| 51 | $name = 'x' x $MAX_HEADER_LEN; |
| 52 | my $response = 'HTTP/1.1 200 OK' . $crlf |
| 53 | . "$name: 42" . $crlf |
| 54 | . $crlf; |
| 55 | ($ret, my $minor_version, my $status, my $message, my $headers) |
| 56 | = parse_http_response($response, HEADERS_AS_HASHREF); |
| 57 | |
| 58 | cmp_ok $ret, '>', 0, 'parsing status (success)'; |
| 59 | is $minor_version, 1; |
| 60 | is $status, 200; |
| 61 | is $message, 'OK'; |
| 62 | is $headers->{$name}, 42, 'very long name'; |
| 63 | |
| 64 | # on fail |
| 65 | $name = 'x' x ($MAX_HEADER_LEN + 1); |
| 66 | $response = 'HTTP/1.1 200 OK' . $crlf |
| 67 | . "$name: 42" . $crlf |
| 68 | . "foo: bar" . $crlf |
| 69 | . $crlf; |
| 70 | ($ret, $minor_version, $status, $message, $headers) |
| 71 | = parse_http_response($response, HEADERS_AS_HASHREF); |
| 72 | |
| 73 | cmp_ok $ret, '>', -1, 'parsing status (fail)'; |
| 74 | is_deeply $headers, { foo => 'bar' }, 'too long name is ignored' |
| 75 | or diag(explain($headers)); |
| 76 | |
| 77 | # too many headers |
| 78 | $response = 'HTTP/1.1 200 OK' . $crlf |
| 79 | . join($crlf, map { "X$_: $_" } 0 .. $MAX_HEADERS) . $crlf |
| 80 | . "foo: bar" . $crlf |
| 81 | . $crlf; |
| 82 | ($ret) = parse_http_response($response, HEADERS_AS_HASHREF); |
| 83 | is $ret, -1, 'too many headers (fail)'; |
| 84 | |
| 85 | |
| 86 | done_testing; |
| 87 |