check_chunked revision 09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1
394N/A#!/usr/bin/perl -w
394N/A
394N/A# This is meant to be used on the raw output of an HTTP/1.1 connection
394N/A# to check that the chunks are all correctly laid out. It's easiest
394N/A# to use a tool like netcat to generate the output. This script
394N/A# *insists* that \r exist in the output.
394N/A#
394N/A# You can find netcat at avian.org:/src/hacks/nc110.tgz.
394N/A
394N/Ause strict;
394N/A
394N/Amy $is_chunked = 0;
394N/A
394N/A# must toss headers
394N/Awhile(<>) {
394N/A if (/^Transfer-Encoding:\s+chunked/i) {
394N/A $is_chunked = 1;
394N/A }
394N/A last if ($_ eq "\r\n");
394N/A}
3996N/A
394N/A$is_chunked || die "wasn't chunked\n";
394N/A
394N/Afor(;;) {
394N/A $_ = <> || die "unexpected end of file!\n";
394N/A
394N/A m#^([0-9a-f]+) *\r$#i || die "bogus chunklen: $_";
394N/A
394N/A my $chunklen = hex($1);
394N/A
844N/A exit 0 if ($chunklen == 0);
844N/A
394N/A chop; chop;
394N/A print "$_ ";
1273N/A
394N/A my $data = '';
3661N/A read(ARGV, $data, $chunklen) == $chunklen || die "short read!\n";
3661N/A
3778N/A $_ = <> || die "unexpected end of file!\n";
3778N/A
3778N/A $_ eq "\r\n" || die "missing chunk trailer!\n";
3996N/A}
3996N/A