line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Ubic::UA; |
2
|
|
|
|
|
|
|
$Ubic::UA::VERSION = '1.60'; |
3
|
|
|
|
|
|
|
# ABSTRACT: tiny http client |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
680
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
22
|
|
7
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
18
|
|
8
|
1
|
|
|
1
|
|
369
|
use IO::Socket; |
|
1
|
|
|
|
|
10583
|
|
|
1
|
|
|
|
|
3
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
12
|
0
|
|
|
|
|
|
my %arg = @_; |
13
|
|
|
|
|
|
|
|
14
|
0
|
|
|
|
|
|
my $self = {}; |
15
|
0
|
|
0
|
|
|
|
$self->{timeout} = $arg{timeout} || 10; |
16
|
0
|
|
|
|
|
|
return bless $self => $class; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub get { |
20
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
21
|
0
|
|
|
|
|
|
my ($url) = @_; |
22
|
|
|
|
|
|
|
|
23
|
0
|
0
|
|
|
|
|
unless ($url) { |
24
|
0
|
|
|
|
|
|
return { error => 'Url not specified' }; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
my ($scheme, $authority, $path, $query, undef) = $url |
28
|
|
|
|
|
|
|
=~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $socket = IO::Socket::INET->new( |
31
|
|
|
|
|
|
|
PeerAddr => $authority, |
32
|
|
|
|
|
|
|
Proto => 'tcp', |
33
|
|
|
|
|
|
|
Timeout => $self->{timeout} |
34
|
0
|
0
|
|
|
|
|
) or return {error => $@}; |
35
|
0
|
|
|
|
|
|
$socket->autoflush(1); |
36
|
0
|
0
|
|
|
|
|
$path .= '?' . $query if $query; |
37
|
0
|
|
|
|
|
|
print $socket "GET $path HTTP/1.0\r\n\r\n"; |
38
|
0
|
|
|
|
|
|
my @arr = <$socket>; |
39
|
0
|
|
|
|
|
|
close $socket; |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
my $status_line = shift @arr; |
42
|
0
|
|
|
|
|
|
my @headers; |
43
|
0
|
|
|
|
|
|
while (my $line = shift @arr) { |
44
|
0
|
0
|
|
|
|
|
last if $line eq "\r\n"; |
45
|
0
|
|
|
|
|
|
$line =~ s/\r\n//; |
46
|
0
|
|
|
|
|
|
push @headers, $line; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
0
|
0
|
|
|
|
|
if ($status_line =~ /^HTTP\/([0-9\.]+)\s+([0-9]{3})\s+(?=([^\r\n]*))?/i) { |
50
|
0
|
|
|
|
|
|
my ($ver, $code, $status) = ($1, $2, $3); |
51
|
|
|
|
|
|
|
return { |
52
|
0
|
|
|
|
|
|
ver => $ver, |
53
|
|
|
|
|
|
|
code => $code, |
54
|
|
|
|
|
|
|
status => $status, |
55
|
|
|
|
|
|
|
body => join('', @arr), |
56
|
|
|
|
|
|
|
headers => \@headers, |
57
|
|
|
|
|
|
|
}; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
else { |
60
|
0
|
|
|
|
|
|
return { error => 'Invalid http response' }; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
1; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
__END__ |