Lambda/HTTP/UserAgent.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 80 CC 9 SCAR 19.9
CC
9
Cov
0%
request
20.0
cookie_jar
6.0
conn_cache
6.0
CRAP 7.3
1 package IO::Lambda::HTTP::UserAgent;
2 use strict;
3 use warnings;
4 use IO::Lambda;
5 use IO::Lambda::HTTP::Client;
6 use HTTP::Cookies;
7 use LWP::ConnCache;
8
9 sub new
10 {
11 my ( $class, %opt ) = @_;
12 return bless {
13 cookie_jar => HTTP::Cookies-> new,
14 conn_cache => LWP::ConnCache-> new,
15 signature => "perl/IO-Lambda-HTTP v$IO::Lambda::VERSION",
16 protocol => 'HTTP/1.1',
17 timeout => 60,
18 %opt,
19 }, $class;
20 }
21
22 sub cookie_jar { $#_ ? $_[0]->{cookie_jar} = $_[1] : $_[0]->{cookie_jar} }
23 sub conn_cache { $#_ ? $_[0]->{conn_cache} = $_[1] : $_[0]->{conn_cache} }
24 sub signature { $#_ ? $_[0]->{signature } = $_[1] : $_[0]->{signature } }
25 sub protocol { $#_ ? $_[0]->{protocol } = $_[1] : $_[0]->{protocol } }
26 sub timeout { $#_ ? $_[0]->{timeout } = $_[1] : $_[0]->{timeout } }
27
28 sub request
29 {
30 my ( $self, $req, %xopt ) = @_;
31
32 my $keep_alive = 0;
33 my %headers;
34 $headers{'User-Agent'} = $self->signature;
35 if ( $self->protocol eq 'HTTP/1.1') {
36 unless ( $req-> protocol) {
37 $req-> protocol('HTTP/1.1');
38 }
39 $headers{Host} = $req-> uri-> host;
40 $headers{Connection} = 'Keep-Alive';
41 $headers{'Keep-Alive'} = 300;
42 $keep_alive = 1;
43 }
44
45 my $h = $req-> headers;
46 while ( my ($k, $v) = each %headers) {
47 $h-> header($k, $v) unless defined $h-> header($k);
48 }
49
50 my $class = $xopt{class} // 'IO::Lambda::HTTP::Client';
51 return $class->new($req,
52 %xopt,
53 cookie_jar => $self->cookie_jar,
54 conn_cache => $self->conn_cache,
55 keep_alive => $keep_alive,
56 timeout => $self->timeout,
57 );
58 }
59
60 1;
61
62 =pod
63
64 =head1 NAME
65
66 IO::Lambda::HTTP::UserAgent - common properties for http requests
67
68 =head1 DESCRIPTION
69
70 The module is a simple convenience wrapper for C<IO::Lambda::HTTP::Client> for shared properties
71 such as cookies, connection cache, etc.
72
73 =head1 SYNOPSIS
74
75 use IO::Lambda::HTTP::UserAgent;
76 use HTTP::Request;
77 my $ua = IO::Lambda::HTTP::UserAgent->new;
78 $ua->request( HTTP::Request->new( .. ) )->wait;
79
80
81 =head1 API
82
83 =over
84
85 =item new %OPTIONS
86
87 Creates a user agent instance
88
89 =item conn_cache $CACHE
90
91 Shared instance of a C<LWP::ConnCache> object
92
93 =item cookie_jar $JAR
94
95 Shared instance of a C<HTTP::Cookies> object
96
97 =item protocol $HTTP
98
99 Default is C<HTTP/1.1>
100
101 =item request HTTP::Request, %OPTIONS
102
103 Creates a lambda that would end when the request is finished.
104 The lambda returns either a C<HTTP::Response> object or an error string
105
106 Options:
107
108 =over
109
110 =item class $STRING = IO::Lambda::HTTP::Client
111
112 Sets class of a HTTP client.
113
114 =back
115
116 =item signature $STRING
117
118 The default C<User-Agent> header
119
120 =item timeout $INTEGER
121
122 Timeout for requests, default 60 seconds.
123
124 =back
125
126 =cut