File: get-callback

package info (click to toggle)
libwww-perl 6.78-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,008 kB
  • sloc: perl: 4,148; makefile: 10; sh: 6
file content (29 lines) | stat: -rw-r--r-- 589 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/perl
use strict;
use warnings;

use LWP::UserAgent ();

my $url = shift || "";
$url = "http://localhost:8080/$url" unless $url =~ /:/;

my $tot = 0;
my $ct_len;
my $ua = LWP::UserAgent->new;
my $res = $ua->request(HTTP::Request->new("GET", $url), \&data);

sub data {
    my ($data, $response) = @_;
    my $len = length $data;
    $tot += $len;
    print "Got $len bytes";
    unless ($ct_len) {
        $ct_len = $response->header("Content-Length");
    }
    if ($ct_len) {
        printf " (%.1f%% done)", $tot/$ct_len*100.0;
    }
    print "\n";
}

print $res->as_string;