| 1 | package Ubic::UA; | ||||||||||||
| 2 | 29 | $Ubic::UA::VERSION = '1.59'; | |||||||||||
| 3 | # ABSTRACT: tiny http client | ||||||||||||
| 4 | |||||||||||||
| 5 | |||||||||||||
| 6 | 29 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 29 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 29 | ▶ | use IO::Socket; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | sub new { | ||||||||||||
| 11 | 0 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 12 | 0 | my %arg = @_; | |||||||||||
| 13 | |||||||||||||
| 14 | 0 | my $self = {}; | |||||||||||
| 15 | 0 | ▶ | $self->{timeout} = $arg{timeout} || 10; | ||||||||||
|
Condition0%0/2
$arg{'timeout'} || 10
MC/DC0%0/1
$arg{'timeout'} || 10
$arg{'timeout'}
Truth table0%0/2
A
A= $arg{'timeout'}
| |||||||||||||
| 16 | 0 | return bless $self => $class; | |||||||||||
| 17 | } | ||||||||||||
| 18 | |||||||||||||
| 19 | sub get { | ||||||||||||
| 20 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
get: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||
| 21 | 0 | my ($url) = @_; | |||||||||||
| 22 | |||||||||||||
| 23 | 0 | ▶ | unless ($url) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | ▶ | ) or return {error => $@}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | 0 | $socket->autoflush(1); | |||||||||||
| 36 | 0 | ▶ | $path .= '?' . $query if $query; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | 0 | ▶ | last if $line eq "\r\n"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | 0 | $line =~ s/\r\n//; | |||||||||||
| 46 | 0 | push @headers, $line; | |||||||||||
| 47 | } | ||||||||||||
| 48 | |||||||||||||
| 49 | 0 | ▶ | if ($status_line =~ /^HTTP\/([0-9\.]+)\s+([0-9]{3})\s+(?=([^\r\n]*))?/i) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | 29 | 1; | |||||||||||
| 66 | |||||||||||||
| 67 | __END__ | ||||||||||||