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;
}

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>