http://www.perlmonks.org?node_id=534516


in reply to LWP::UserAgent hacking

Here's a method that will work for chunked responses, too:
sub send_request { my($self, $request, $arg, $size) = @_; my $content_length = 0; if (ref $arg && ref $arg eq "CODE") { my $original_arg = $arg; $arg = sub { $content_length += length($_[0]); return &$original_arg(@_); }; } my $t0 = [Time::HiRes::gettimeofday]; my $response = $self->SUPER::send_request($request,$arg,$size); my $elapsed = Time::HiRes::tv_interval($t0); $content_length = length($response->content) unless $content_length; my $header_length = length($response->headers_as_string); print STDERR sprintf "%s %f %d %d %d\n", $request->uri, $elapsed, $content_length, $header_length, $content_length+$header_length; return $response; }