10 Einträge, 1 Seite |
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#
# provide subclassed UserAgent to override on_connect, on_failure and
# on_return methods
#
package myUA;
use Exporter();
use LWP::Parallel::UserAgent qw(:CALLBACK);
@ISA = qw(LWP::Parallel::UserAgent Exporter);
@EXPORT = @LWP::Parallel::UserAgent::EXPORT_OK;
# redefine methods: on_connect gets called whenever we're about to
# make a a connection
sub on_connect {
my ($self, $request, $response, $entry) = @_;
print "Connecting to ",$request->url,"\n";
}
# on_failure gets called whenever a connection fails right away
# (either we timed out, or failed to connect to this address before,
# or it's a duplicate). Please note that non-connection based
# errors, for example requests for non-existant pages, will NOT call
# on_failure since the response from the server will be a well
# formed HTTP response!
sub on_failure {
my ($self, $request, $response, $entry) = @_;
print "Failed to connect to ",$request->url,"\n\t",
$response->code, ", ", $response->message,"\n"
if $response;
}
# on_return gets called whenever a connection (or its callback)
# returns EOF (or any other terminating status code available for
# callback functions). Please note that on_return gets called for
# any successfully terminated HTTP connection! This does not imply
# that the response sent from the server is a success!
sub on_return {
my ($self, $request, $response, $entry) = @_;
if ($response->is_success) {
print "\n\nWoa! Request to ",$request->url," returned code ", $response->code,
": ", $response->message, "\n";
print $response->content;
} else {
print "\n\nBummer! Request to ",$request->url," returned code ", $response->code,
": ", $response->message, "\n";
# print $response->error_as_HTML;
}
return;
}
package main;
use HTTP::Request;
# shortcut for demo URLs
my $url = "http://localhost/";
my $reqs = [
HTTP::Request->new('GET', $url),
HTTP::Request->new('GET', $url."homes/marclang/"),
];
my $pua = myUA->new();
foreach my $req (@$reqs) {
print "Registering '".$req->url."'\n";
$pua->register ($req);
}
my $entries = $pua->wait(); # responses will be caught by on_return, etc
10 Einträge, 1 Seite |