File Coverage

blib/lib/App/DubiousHTTP/Tests/Chunked.pm
Criterion Covered Total %
statement 12 119 10.0
branch 0 82 0.0
condition 0 17 0.0
subroutine 4 7 57.1
pod 0 1 0.0
total 16 226 7.0


line stmt bran cond sub pod time code
1 1     1   3 use strict;
  1         1  
  1         24  
2 1     1   3 use warnings;
  1         1  
  1         24  
3             package App::DubiousHTTP::Tests::Chunked;
4 1     1   3 use App::DubiousHTTP::Tests::Common;
  1         1  
  1         121  
5 1     1   4 use Compress::Raw::Zlib;
  1         1  
  1         2064  
6              
7             SETUP(
8             'chunked',
9             "Variations of server side chunked encoding",
10             <<'DESC',
11             Various tests with invalid or uncommon forms of setting or not setting the
12             Transfer-Encoding: chunked header:
13            
14            
  • chunked is not defined for HTTP/1.0, but some systems still interpret the header for HTTP/1.0 responses
  • 15            
  • some systems do not support breaking HTTP header over multiple lines
  • 16            
  • some systems are happy if 'chunked' is matched somewhere in the header
  • 17            
  • some even interprete the existence of a Transfer-Encoding header as enough to expect chunked data
  • 18            
    19             DESC
    20              
    21             # ------------------------ Tests -----------------------------------
    22             [ 'VALID: basic tests' ],
    23             [ SHOULDBE_VALID, 'chunked' => 'simple and valid chunking'],
    24             [ MUSTBE_VALID, 'clen' => 'content-length header, not chunked'],
    25              
    26             [ 'VALID: modification of chunk size' ],
    27             [ UNCOMMON_VALID, '0size' => "chunks size prefixed with 0" ],
    28             [ UNCOMMON_VALID, '00size' => "chunks size prefixed with 00" ],
    29             [ UNCOMMON_VALID, 'ucsize' => "upper case size" ],
    30             [ UNCOMMON_VALID, '0ucsize' => "upper case size prefix with 0" ],
    31             [ INVALID, '32-size' => "negative size for 32bit uint" ],
    32             [ INVALID, '64-size' => "negative size for 64bit uint" ],
    33             [ INVALID, 'size-space' => "size followed by space" ],
    34             [ INVALID, 'size-tab' => "size followed by tab" ],
    35             [ INVALID, 'size-cr' => "size followed by " ],
    36             [ INVALID, 'size-lf' => "size followed by " ],
    37             [ INVALID, 'size-x' => "size followed by char 'x'" ],
    38             [ INVALID, 'size-\054' => "size followed by char ','" ],
    39             [ INVALID, 'size-\000' => "size followed by char \\000" ],
    40             [ INVALID, 'size-\013' => "size followed by char \\v" ],
    41             [ INVALID, 'size-\014' => "size followed by char \\f" ],
    42             [ INVALID, 'size-spacex' => "size followed by space and char 'x'" ],
    43             [ INVALID, 'space-size' => "size prefixed by space" ],
    44             [ INVALID, 'tab-size' => "size prefixed by tab" ],
    45             [ INVALID, 'cr-size' => "size prefixed by cr" ],
    46             [ INVALID, 'lf-size' => "size prefixed by lf" ],
    47             [ INVALID, 'crlf-size' => 'size prefixed by "\r\n"' ],
    48             [ INVALID, 'crlf-crlf-size' => 'size prefixed by "\r\n\r\n"' ],
    49             [ INVALID, 'crlf-x-crlf-size' => 'size prefixed by "\r\nx\r\n"' ],
    50             [ INVALID, 'x-size' => "size prefixed by char 'x'" ],
    51             [ INVALID, '\054-size' => "size prefixed by char ','" ],
    52             [ INVALID, '\073-size' => "size prefixed by char ';'" ],
    53             [ INVALID, '\000-size' => "size prefixed by char \\000" ],
    54             [ INVALID, '\013-size' => "size prefixed by char \\v" ],
    55             [ INVALID, '\014-size' => "size prefixed by char \\f" ],
    56             [ INVALID, 'xspace-size' => "size prefixed by char 'x' and space" ],
    57             [ INVALID, '\053-size' => "size prefixed by char '+'" ],
    58             [ INVALID, '\060\170-size' => "size prefixed by '0x'" ],
    59             [ UNCOMMON_VALID, 'final=00' => 'final chunk size "00"' ],
    60             [ UNCOMMON_VALID, 'final=00000000000000000000' => 'final chunk size "00000000000000000000"' ],
    61             [ INVALID, 'final=0x' => 'final chunk size "0x"' ],
    62             [ INVALID, 'final=Foo' => 'final chunk size "Foo"' ],
    63             [ INVALID, 'finalchunk=0\012' => 'final chunk just "0\n"' ],
    64             [ INVALID, 'finalchunk=0\015' => 'final chunk just "0\r"' ],
    65             [ INVALID, 'finalchunk=0' => 'final chunk just "0"' ],
    66             [ INVALID, 'finalchunk=0\012\012' => 'final chunk "0\n\n"' ],
    67             [ INVALID, 'finalchunk=0\012\040\012' => 'final chunk "0\n\n"' ],
    68             [ INVALID, 'finalchunk=0\012\015\012' => 'final chunk "0\n\r\n"' ],
    69             [ INVALID, 'finalchunk=0\012\015\015\012' => 'final chunk "0\n\r\r\n"' ],
    70             [ INVALID, 'finalchunk=0\015\012foobar\015\012' => 'final chunk "0\r\nfoobar\r\n"' ],
    71              
    72             [ 'VALID: (but uncommon) use of extensions in chunked header' ],
    73             [ UNCOMMON_VALID, 'chunk-ext-junk' => "chunked with some junk chunk extension" ],
    74             [ UNCOMMON_VALID, 'chunk-ext-chunk' => "chunked with some junk chunk extension looking like a chunk" ],
    75              
    76             [ 'VALID: combined with content-length' ],
    77             # according to RFC2616 TE chunked has preference to clen
    78             [ VALID, 'chunked,clen' => 'chunked first then content-length, served chunked'],
    79             [ VALID, 'clen,chunked' => 'content-length first then chunked, served chunked'],
    80             # but some still expect clen bytes
    81             # safari does not like it, so mark it as uncommon
    82             [ UNCOMMON_VALID, 'chunked,clen200' => 'chunking and content-length header with double length, served chunked'],
    83             [ UNCOMMON_VALID, 'chunked,clen50' => 'chunking and content-length header with half length, served chunked'],
    84             [ INVALID, 'addjunk,chunked,clen50' => 'content+junk, chunked, content-length header includes content only' ],
    85              
    86             [ 'INVALID: chunking is only allowed with HTTP/1.1' ],
    87             [ INVALID, 'chunked,http10' => 'Chunked Header and HTTP/1.0. Served chunked.'],
    88             [ INVALID, 'chunked,clen,http10' => 'Chunked Header and Content-length and HTTP/1.0. Served chunked.'],
    89             [ INVALID, 'clen,chunked,http10' => 'Content-length Header and Chunked and HTTP/1.0. Served chunked.'],
    90             [ INVALID, 'chunked,http10,gzip' => 'Chunked Header and HTTP/1.0. Served chunked with gzip.'],
    91             [ INVALID, 'chunked,clen,http10,gzip' => 'Chunked Header and Content-length and HTTP/1.0. Served chunked with gzip.'],
    92             [ INVALID, 'clen,chunked,http10,gzip' => 'Content-length Header and Chunked and HTTP/1.0. Served chunked with gzip.'],
    93              
    94             [ 'VALID: chunked header should be ignored with HTTP/1.0' ],
    95             [ UNCOMMON_VALID, 'chunked,http10,do_clen' => 'Chunked Header and HTTP/1.0. Not served chunked.'],
    96             [ UNCOMMON_VALID, 'chunked,clen,http10,do_clen' => 'Chunked Header and Content-length and HTTP/1.0. Not served chunked.'],
    97             [ UNCOMMON_VALID, 'clen,chunked,http10,do_clen' => 'Content-length Header and Chunked and HTTP/1.0. Not served chunked.'],
    98             [ UNCOMMON_VALID, 'chunked,http10,do_clen,gzip' => 'Chunked Header and HTTP/1.0. Not served chunked. Compressed with gzip.'],
    99             [ UNCOMMON_VALID, 'chunked,clen,http10,do_clen,gzip' => 'Chunked Header and Content-length and HTTP/1.0. Not served chunked. Compressed with gzip.'],
    100             [ UNCOMMON_VALID, 'clen,chunked,http10,do_clen,gzip' => 'Content-length Header and Chunked and HTTP/1.0. Not served chunked. Compressed with gzip.'],
    101              
    102             [ 'INVALID: chunking with invalid HTTP versions' ],
    103             [ INVALID, 'chunked,HTTP/1.2' => 'Chunked Header and HTTP/1.2. Served chunked.'],
    104             [ INVALID, 'chunked,clen,HTTP/1.2,do_clen' => 'Chunked Header and HTTP/1.2. Not served chunked.'],
    105             [ INVALID, 'chunked,HTTP/2.0' => 'Chunked Header and HTTP/2.0. Served chunked.'],
    106             [ INVALID, 'chunked,clen,HTTP/2.0,do_clen' => 'Chunked Header and HTTP/2.0. erved chunked.'],
    107             [ INVALID, 'chunked,HTTP/2.1' => 'Chunked Header and HTTP/2.1. Served chunked.'],
    108             [ INVALID, 'chunked,clen,HTTP/2.1,do_clen' => 'Chunked Header and HTTP/2.1. erved chunked.'],
    109             [ INVALID, 'chunked,HTTP/0.9' => 'Chunked Header and HTTP/0.9. Served chunked.'],
    110             [ INVALID, 'chunked,clen,HTTP/0.9,do_clen' => 'Chunked Header and HTTP/0.9. Not served chunked.'],
    111             [ INVALID, 'chunked,HTTP/1.01' => 'Chunked Header and HTTP/1.01. Served chunked.'],
    112             [ INVALID, 'chunked,clen,HTTP/1.01,do_clen' => 'Chunked Header and HTTP/1.01. Not served chunked.'],
    113             [ INVALID, 'chunked,HTTP/1.10' => 'Chunked Header and HTTP/1.10. Served chunked.'],
    114             [ INVALID, 'chunked,clen,HTTP/1.10,do_clen' => 'Chunked Header and HTTP/1.10. Not served chunked.'],
    115             [ INVALID, 'chunked,http/1.1' => 'Chunked Header and http/1.1. Served chunked.'],
    116             [ INVALID, 'chunked,clen,http/1.1,do_clen' => 'Chunked Header and http/1.1. Not served chunked.'],
    117             [ INVALID, 'chunked,http/1.0' => 'Chunked Header and http/1.0. Served chunked.'],
    118             [ INVALID, 'chunked,clen,http/1.0,do_clen' => 'Chunked Header and http/1.0. Not served chunked.'],
    119              
    120             [ INVALID, 'chunked,HTTP/0.1,gzip' => 'Chunked Header and HTTP/0.1. Served chunked and with gzip.'],
    121             [ INVALID, 'chunked,clen,HTTP/0.1,gzip,do_clen' => 'Chunked Header and HTTP/0.1. Not served chunked but with gzip.'],
    122             [ INVALID, 'chunked,HTTP/01.1,gzip' => 'Chunked Header and HTTP/01.1. Served chunked and with gzip.'],
    123             [ INVALID, 'chunked,clen,HTTP/01.1,gzip,do_clen' => 'Chunked Header and HTTP/01.1. Not served chunked but with gzip.'],
    124             [ INVALID, 'chunked,HTTP/11.01,gzip' => 'Chunked Header and HTTP/11.01. Served chunked and with gzip.'],
    125             [ INVALID, 'chunked,clen,HTTP/11.01,gzip,do_clen' => 'Chunked Header and HTTP/11.01. Not served chunked but with gzip.'],
    126             [ INVALID, 'chunked,HTTP/11.10,gzip' => 'Chunked Header and HTTP/11.10. Served chunked and with gzip.'],
    127             [ INVALID, 'chunked,clen,HTTP/11.10,gzip,do_clen' => 'Chunked Header and HTTP/11.10. Not served chunked but with gzip.'],
    128             [ INVALID, 'chunked,HTTP/9.9,gzip' => 'Chunked Header and HTTP/9.9. Served chunked and with gzip.'],
    129             [ INVALID, 'chunked,clen,HTTP/9.9,gzip,do_clen' => 'Chunked Header and HTTP/9.9. Not served chunked but with gzip.'],
    130              
    131             [ 'VALID: valid variations on "chunked" value' ],
    132             [ VALID, 'chUnked' => 'mixed case "chUnked", served chunked'],
    133             [ UNCOMMON_VALID,'nl-chunked' => "chunked header with continuation line, served chunked"],
    134             [ UNCOMMON_VALID,'chunkednl-' => "chunked header followed by empty with continuation line, served chunked"],
    135             [ UNCOMMON_VALID,'nl-nl-chunked' => "chunked header with double continuation line, served chunked"],
    136             [ UNCOMMON_VALID,'nl-nl-chunked-nl-' => "chunked header with double continuation line and continuation afterwareds, served chunked"],
    137              
    138             [ 'INVALID: invalid variations on "chunked" value' ],
    139             [ INVALID, 'chu' => '"chu" not "chunked"'],
    140             [ INVALID, 'chunked-semicolon' => '"Transfer-Encoding: chunked;"' ],
    141             [ INVALID, 'xchunked' => '"xchunked" not "chunked"'],
    142             [ INVALID, 'chunkedx' => '"chunkedx" not "chunked"'],
    143             [ INVALID, 'chunked-x' => '"chunked x" not "chunked"'],
    144             [ INVALID, 'x-chunked' => '"x chunked" not "chunked"'],
    145             [ UNCOMMON_INVALID, 'chunked-x,do_clen' => '"chunked x" not "chunked", not served chunked'],
    146             [ UNCOMMON_INVALID, 'x-chunked,do_clen' => '"x chunked" not "chunked", not served chunked'],
    147             [ INVALID, 'x-nl-chunked' => '"x-folding-chunked" not "chunked"'],
    148             [ INVALID, 'chunked-nl-x' => '"chunked-folding-x" not "chunked"'],
    149             [ INVALID, 'rfc2047,do_chunked' => 'rfc2047/base64 encoded "chunked", served chunked' ],
    150             [ UNCOMMON_VALID, 'rfc2047,do_clen' => 'rfc2047/base64 encoded "chunked", not served chunked' ],
    151             [ UNCOMMON_VALID, 'rfc2047,clen,do_clen' => 'rfc2047/base64 encoded "chunked" and content-length, not served chunked' ],
    152             [ INVALID,'nl-chunked,do_clen' => "chunked header with continuation line. Not served chunked."],
    153             [ INVALID,'chunkednl-,do_clen' => "chunked header followed by empty continuation line. Not served chunked."],
    154             [ INVALID,'nl-nl-chunked,do_clen' => "chunked header with double continuation line, not served chunked"],
    155             [ INVALID,'crchunked,do_chunked' => "Transfer-Encoding:chunked. Served chunked."],
    156             [ INVALID,'crchunked,do_clen' => "Transfer-Encoding:chunked. Not served chunked."],
    157             [ INVALID,'cr-chunked,do_chunked' => "Transfer-Encoding:chunked. Served chunked."],
    158             [ INVALID,'cr-chunked,do_clen' => "Transfer-Encoding:chunked. Not served chunked."],
    159             [ INVALID,'chunkedcr-,do_chunked' => "Transfer-Encoding:chunked. Served chunked."],
    160             [ INVALID,'chunkedcr-,do_clen' => "Transfer-Encoding:chunked. Not served chunked."],
    161             [ INVALID,'ce-chunked,do_chunked' => "Content-encoding chunked instead of Transfer-encoding. Served chunked."],
    162              
    163             [ 'INVALID: hiding with another Transfer-Encoding header' ],
    164             [ INVALID, 'xte,chunked,do_chunked' => "double Transfer-Encoding: first junk, last chunked. Served chunked." ],
    165             [ INVALID, 'xte,chunked,do_chunked,gzip' => "double Transfer-Encoding: first junk, last chunked. Served chunked and gzipped." ],
    166             [ INVALID, 'chunked,xte,do_chunked' => "double Transfer-Encoding: first chunked, last junk. Served chunked." ],
    167             [ INVALID, 'chunked,xte,do_chunked,gzip' => "double Transfer-Encoding: first chunked, last junk. Served chunked and gzipped." ],
    168             [ INVALID, 'xte,chunked,xte,do_chunked' => "triple Transfer-Encoding: first junk, then chunked, then junk again. Served chunked." ],
    169             [ INVALID, 'xte,chunked,do_clen' => "double Transfer-Encoding: first junk, last chunked. Not served chunked." ],
    170             [ INVALID, 'chunked,xte,do_clen' => "double Transfer-Encoding: first chunked, last junk. Not served chunked." ],
    171             [ INVALID, 'chunked,xte,clen,do_chunked' => "double Transfer-Encoding: first chunked, last junk. Also Content-length header. Served chunked." ],
    172             [ INVALID, 'xte,chunked,clen,do_chunked' => "double Transfer-Encoding: first junk, last chunked. Also Content-length header. Served chunked." ],
    173             [ INVALID, 'xte,chunked,xte,clen,do_chunked' => "triple Transfer-Encoding: first junk, then chunked, then junk again. Also Content-length header. Served chunked." ],
    174             [ INVALID, 'chunked,xte,clen,do_clen' => "double Transfer-Encoding: first chunked, last junk. Also Content-length header. Not served chunked." ],
    175             [ INVALID, 'chunked,xte,clen,do_clen,gzip' => "double Transfer-Encoding: first chunked, last junk. Also Content-length header. Not served chunked. Compressed with gzip." ],
    176             [ INVALID, 'xte,chunked,clen,do_clen' => "double Transfer-Encoding: first junk, last chunked. Also Content-length header. Not served chunked." ],
    177             [ INVALID, 'xte,chunked,clen,do_clen,gzip' => "double Transfer-Encoding: first junk, last chunked. Also Content-length header. Not served chunked. Compressed with gzip." ],
    178             [ INVALID, 'chunked,clen,do_clen' => 'chunking and content-length, not served chunked'],
    179             [ INVALID, 'chunked,clen,do_clen,gzip' => 'chunking and content-length, not served chunked. Compressed with gzip.'],
    180              
    181             [ 'INVALID: hiding the Transfer-Encoding header' ],
    182             [ INVALID, 'space-colon-chunked,do_chunked' => '"Transfer-Encoding:", served chunked' ],
    183             [ INVALID, 'tab-colon-chunked,do_chunked' => '"Transfer-Encoding:", served chunked' ],
    184             [ INVALID, 'cr-colon-chunked,do_chunked' => '"Transfer-Encoding:", served chunked' ],
    185             [ UNCOMMON_INVALID, 'space-colon-chunked,do_clen' => '"Transfer-Encoding:", not served chunked' ],
    186             [ UNCOMMON_INVALID, 'tab-colon-chunked,do_clen' => '"Transfer-Encoding:", not served chunked' ],
    187             [ UNCOMMON_INVALID, 'cr-colon-chunked,do_clen' => '"Transfer-Encoding:", not served chunked' ],
    188             [ INVALID, 'colon-colon-chunked,do_chunked' => '"Transfer-Encoding::", served chunked' ],
    189             [ UNCOMMON_INVALID, 'colon-colon-chunked,do_clen' => '"Transfer-Encoding::", not served chunked' ],
    190             [ INVALID, 'cronly-chunked,do_chunked' => 'Transfer-Encoding with only as line delimiter before, served chunked' ],
    191             [ INVALID, 'crxonly-chunked,do_chunked' => 'Only as line delimiter followed by "xTransfer-Encoding", served chunked' ],
    192             [ UNCOMMON_INVALID, 'cronly-chunked,do_clen' => 'Transfer-Encoding with only as line delimiter before, not served chunked' ],
    193             [ UNCOMMON_INVALID, 'lfonly-chunked,do_chunked' => 'Transfer-Encoding with only as line delimiter before, served chunked' ],
    194             [ INVALID, 'lfonly-chunked,do_clen' => 'Transfer-Encoding with only as line delimiter before, not served chunked' ],
    195              
    196             [ 'INVALID: invalid chunks' ],
    197             [ INVALID, 'chunk-lf' => "chunk with LF as delimiter instead of CRLF" ],
    198             [ INVALID, 'chunk-cr' => "chunk with CR as delimiter instead of CRLF" ],
    199             [ INVALID, 'chunk-crcr' => "chunk with CRCR as delimiter instead of CRLF" ],
    200             [ INVALID, 'chunk-lflf' => "chunk with LFLF as delimiter instead of CRLF" ],
    201             [ INVALID, 'chunk-lfcr' => "chunk with LFCR as delimiter instead of CRLF" ],
    202             [ INVALID, 'nofinal' => 'missing final chunk' ],
    203             [ INVALID, 'eof-inchunk' => 'eof inside some chunk' ],
    204             [ INVALID, 'space-before-chunks' => 'space before chunks start' ],
    205             [ INVALID, 'lf-before-chunks' => ' before chunks start' ],
    206             [ INVALID, 'cr-before-chunks' => ' before chunks start' ],
    207             [ INVALID, 'crlf-before-chunks' => ' before chunks start' ],
    208             );
    209              
    210              
    211             sub make_response {
    212 0     0 0   my ($self,$page,$spec) = @_;
    213 0 0         return make_index_page() if $page eq '';
    214 0 0         my ($hdr,$data) = content($page,$self->ID."-".$spec) or die "unknown page $page";
    215 0           my $version = 'HTTP/1.1';
    216 0           my ($te,@chunks,%chunkmod,$clen);
    217 0           my $sizefmt = '%x';
    218 0           my $before_chunks = '';
    219 0           my $final = '0';
    220 0           my $finalchunk;
    221 0           for (split(',',$spec)) {
    222 0 0         if ( m{^(x|-|nl|lf|cr)*chunked(x|-|nl|lf|cr)*$}i ) {
        0          
        0          
        0          
        0          
        0          
        0          
        0          
        0          
        0          
        0          
        0          
        0          
        0          
        0          
        0          
        0          
        0          
        0          
        0          
        0          
        0          
        0          
    223 0           s{-}{ }g;
    224 0           s{nl}{\r\n}g;
    225 0           s{lf}{\n}g;
    226 0           s{cr}{\r}g;
    227 0           $hdr .= "Transfer-Encoding: $_\r\nConnection: close\r\n";
    228             } elsif ( m{^(space|tab|cr|colon)-colon-chunked$} ) {
    229 0           my $c = $1;
    230 0           $c =~s{space}{ }g;
    231 0           $c =~s{colon}{:}g;
    232 0           $c =~s{tab}{\t}g;
    233 0           $c =~s{cr}{\r}g;
    234 0           $te = 'chunked';
    235 0           $hdr .= "Connection: close\r\nTransfer-Encoding$c: chunked\r\n"
    236             } elsif ( my ($crlf) = m {^((?:lf|cr|x)+)only-chunked$} ) {
    237 0           $te = 'chunked';
    238 0 0         $hdr = "X-Foo: bar" if $hdr !~s{\r\n\z}{};
    239 0           $crlf =~s{lf}{\n}g;
    240 0           $crlf =~s{cr}{\r}g;
    241 0           $hdr .= $crlf . "Transfer-Encoding: chunked\r\nConnection: close\r\n";
    242             } elsif ( $_ eq '1chunk' ) {
    243 0           $hdr .= "Transfer-Encoding: chunked\r\n";
    244 0           @chunks = $data;
    245             } elsif ( $_ eq 'chu' ) {
    246 0           $hdr .= "Transfer-Encoding: chu\r\nConnection: close\r\n"
    247             } elsif ( $_ eq 'ce-chunked' ) {
    248 0           $hdr .= "Content-Encoding: chunked\r\nConnection: close\r\n"
    249             } elsif ( $_ =~ m{^clen(\d+)?$} ) {
    250 0   0       $clen = $1 || 100;
    251             } elsif ( $_ eq 'http10' ) {
    252 0           $version = "HTTP/1.0";
    253             } elsif ( $_ =~m{^HTTP/\S+}i ) {
    254 0           $version = $_;
    255             } elsif ( $_ eq 'do_clen' ) {
    256 0           $te = 'clen'
    257             } elsif ( $_ eq 'do_chunked' ) {
    258 0           $te = 'chunked'
    259             } elsif ( $_ eq 'chunked-semicolon' ) {
    260 0           $hdr .= "Transfer-Encoding: chunked;\r\nConnection: close\r\n"
    261             } elsif ( $_ eq 'rfc2047' ) {
    262 0           $hdr .= "Transfer-Encoding: =?UTF-8?B?Y2h1bmtlZAo=?=\r\nConnection: close\r\n";
    263             } elsif ( $_ eq 'xte' ) {
    264 0           $hdr .= "Transfer-Encoding: lalala\r\nConnection: close\r\n";
    265             } elsif ( m{^(chunk-ext-|nofinal$|eof-inchunk$)} ) {
    266 0           $hdr .= "Transfer-Encoding: chunked\r\nConnection: close\r\n";
    267 0           $chunkmod{$_} = 1;
    268             } elsif ( my ($eol) = m{^chunk-((?:lf|cr)+)$} ) {
    269 0           $hdr .= "Transfer-Encoding: chunked\r\nConnection: close\r\n";
    270 0           $eol =~s{cr}{\r}g;
    271 0           $eol =~s{lf}{\n}g;
    272 0           $chunkmod{lineend} = $eol;
    273             } elsif (m{^(32|64)-size\z}) {
    274 0 0         my $o = ($1 == 64) ? 'ffffffff':'';
    275 0     0     $sizefmt = sub { sprintf("-$o%08x", 1+(0xffffffff & ~shift())) };
      0            
    276 0           $hdr .= "Transfer-Encoding: chunked\r\nConnection: close\r\n";
    277             } elsif ( m{^(-|space|cr|lf|tab|x|\\[0-7]{3})*(0*)(uc)?size(-|space|cr|lf|tab|x|\\[0-7]{3})*$}) {
    278 0           $hdr .= "Transfer-Encoding: chunked\r\nConnection: close\r\n";
    279 0 0         @chunks = ( $data =~m{(.{1,15})}smg,'') if ! @chunks;
    280 0           s{ucsize}{%X};
    281 0           s{size}{%x};
    282 0           s{\\r}{\r}g;
    283 0           s{\\n}{\n}g;
    284 0           s{-}{}g;
    285 0           s{space}{ }g;
    286 0           s{tab}{\t}g;
    287 0           s{cr}{\r}g;
    288 0           s{lf}{\n}g;
    289 0           s{\\([0-7]{3})}{ chr(oct($1)) }eg;
      0            
    290 0           $sizefmt = $_;
    291             } elsif (m{((?:space|tab|cr|lf)*)-before-chunks}) {
    292 0           $hdr .= "Transfer-Encoding: chunked\r\nConnection: close\r\n";
    293 0           $before_chunks = $1;
    294 0           $before_chunks =~ s{space}{ }g;
    295 0           $before_chunks =~ s{tab}{\t}g;
    296 0           $before_chunks =~ s{cr}{\r}g;
    297 0           $before_chunks =~ s{lf}{\n}g;
    298             } elsif (m{^final=(.*)$}) {
    299 0           $hdr .= "Transfer-Encoding: chunked\r\nConnection: close\r\n";
    300 0           $final = $1;
    301             } elsif (m{^finalchunk=(.*)$}) {
    302 0           $hdr .= "Transfer-Encoding: chunked\r\nConnection: close\r\n";
    303 0           (my $d = $1 ) =~ s{\\([0-7]{3})}{ chr(oct($1)) }eg;
      0            
    304 0           $finalchunk = $d;
    305             } elsif ( $_ eq 'addjunk' ) {
    306 0           $data .= "x" x length($data);
    307             } elsif ( $_ eq 'gzip' ) {
    308 0           $data = _compress($data,'gzip');
    309 0           $hdr .= "Content-Encoding: gzip\r\n";
    310             } else {
    311 0           die $_
    312             }
    313             }
    314 0 0         $hdr .= "Content-length: ". int($clen/100*length($data)) ."\r\n"
    315             if defined $clen;
    316 0           $hdr = "$version 200 ok\r\n$hdr";
    317 0 0 0       $te ||= $hdr =~m{^Transfer-Encoding:}im ? 'chunked':'clen';
    318 0 0 0       @chunks = ( $data =~m{(.{1,5})}smg,'') if $te eq 'chunked' && ! @chunks;
    319 0 0         if (@chunks) {
    320 0           @chunks = map { [ length($_), $_ ] } @chunks;
      0            
    321 0   0       my $nl = $chunkmod{lineend} || "\r\n";
    322 0 0         if ($chunkmod{'chunk-ext-chunk'}) {
        0          
    323 0           $_->[2] = sprintf("; %s %x","x" x $_->[0],$_->[0]) for @chunks;
    324             } elsif ($chunkmod{'chunk-ext-junk'}) {
    325 0           $_->[2] = "; foobar" for @chunks;
    326             }
    327 0 0 0       pop @chunks if $chunkmod{nofinal} && ! $chunks[-1][0];
    328              
    329 0           my $end = '';
    330 0 0         if ($chunkmod{'eof-inchunk'}) {
    331 0 0         pop @chunks if ! $chunks[-1][0]; # remove final chunk
    332 0           my $last = pop(@chunks);
    333 0   0       $end = sprintf("%x%s%s%s",$last->[0]+10,$last->[2]||'',$nl,$last->[1]);
    334             }
    335              
    336 0 0         $finalchunk = "$final$nl$nl" if ! defined $finalchunk;
    337             $data = join("",map {
    338 0 0 0       $_->[0] ? sprintf("%s%s%s%s%s",
      0 0          
    339             ref($sizefmt) ? $sizefmt->($_->[0]): sprintf($sizefmt,$_->[0]), # size
    340             $_->[2] || '', # ext
    341             $nl,
    342             $_->[1],
    343             $nl
    344             ) : $finalchunk
    345             } @chunks).$end;
    346             }
    347 0           return "$hdr\r\n$before_chunks$data";
    348             }
    349              
    350             sub _compress {
    351 0     0     my ($data,$w) = @_;
    352 0 0         my $zlib = Compress::Raw::Zlib::Deflate->new(
    353             -WindowBits => $w eq 'gzip' ? WANT_GZIP : -MAX_WBITS(),
    354             -AppendOutput => 1,
    355             );
    356 0           my $newdata = '';
    357 0           $zlib->deflate( $data, $newdata);
    358 0           $zlib->flush($newdata,Z_FINISH);
    359 0           return $newdata;
    360             }
    361              
    362             1;