Reading http chunked body

Here is my implementation of Chunked Transfer Coding. I find it quite elegant compared to other snippets i’ve seen.

###################################################
# {{{ read body chunked
###################################################
sub getbodychunked
{
	my $sh = shift; # the socket handle should be passed
	my ($in, $body);

	for ($in = < $sh>; defined $in; $in = < $sh>)
	{
		$in = trim $in;
		my $chunk = hex $in;
		while (defined $in && $chunk > 0)
		{
			$chunk -= read $sh, $in, $chunk;
			$body .= $in;
		}
	}
	return $body;
}

This entry was posted on Monday, November 14th, 2005 at 21:40 and is filed under Perl. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.