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