09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#!/usr/bin/perl -w
b8d55029467b9f139d081cf38c6d06ff45e742a9nd#
031b91a62d25106ae69d4693475c79618dd5e884fielding# Licensed to the Apache Software Foundation (ASF) under one or more
031b91a62d25106ae69d4693475c79618dd5e884fielding# contributor license agreements. See the NOTICE file distributed with
031b91a62d25106ae69d4693475c79618dd5e884fielding# this work for additional information regarding copyright ownership.
031b91a62d25106ae69d4693475c79618dd5e884fielding# The ASF licenses this file to You under the Apache License, Version 2.0
031b91a62d25106ae69d4693475c79618dd5e884fielding# (the "License"); you may not use this file except in compliance with
031b91a62d25106ae69d4693475c79618dd5e884fielding# the License. You may obtain a copy of the License at
b8d55029467b9f139d081cf38c6d06ff45e742a9nd#
b8d55029467b9f139d081cf38c6d06ff45e742a9nd# http://www.apache.org/licenses/LICENSE-2.0
b8d55029467b9f139d081cf38c6d06ff45e742a9nd#
b8d55029467b9f139d081cf38c6d06ff45e742a9nd# Unless required by applicable law or agreed to in writing, software
b8d55029467b9f139d081cf38c6d06ff45e742a9nd# distributed under the License is distributed on an "AS IS" BASIS,
b8d55029467b9f139d081cf38c6d06ff45e742a9nd# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b8d55029467b9f139d081cf38c6d06ff45e742a9nd# See the License for the specific language governing permissions and
b8d55029467b9f139d081cf38c6d06ff45e742a9nd# limitations under the License.
b8d55029467b9f139d081cf38c6d06ff45e742a9nd#
b8d55029467b9f139d081cf38c6d06ff45e742a9nd#
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}