blib/lib/XML/RPC/UA/Curl.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 90 CC 8 SCAR 28.3
CC
8
Cov
0%
call
42.0
new
12.0
async
2.0
CRAP 17.0
1 package XML::RPC::UA::Curl;
2  
3 use strict;
4 use warnings;
5 use base 'XML::RPC::UA';
6 use HTTP::Response;
7 use WWW::Curl::Easy;
8 use Carp;
9  
10 use XML::RPC::Fast ();
11 our $VERSION = $XML::RPC::Fast::VERSION;
12  
13 =head1 NAME
14  
15 XML::RPC::UA::Curl - XML::RPC useragent, using Curl
16  
17 =head1 SYNOPSIS
18  
19 use XML::RPC::Fast;
20 use XML::RPC::UA::Curl;
21
22 my $rpc = XML::RPC::Fast->new(
23 $uri,
24 ua => XML::RPC::UA::Curl->new(
25 ua => 'YourApp/0.1',
26 timeout => 3,
27 ),
28 );
29  
30 =head1 DESCRIPTION
31  
32 Default syncronous useragent for L<XML::RPC::Fast>
33  
34 =head1 IMPLEMENTED METHODS
35  
36 =head2 new
37  
38 =head2 async = 0
39  
40 =head2 call
41  
42 =head1 SEE ALSO
43  
44 =over 4
45  
46 =item * L<XML::RPC::UA>
47  
48 Base class (also contains documentation)
49  
50 =item * L<XML::RPC::UA::AnyEvent>
51  
52 =back
53  
54 =cut
55  
56 sub async { 0 }
57  
58 sub new {
59 my $pkg = shift;
60 my %args = @_;
61 my $useragent = delete $args{ua} || 'XML-RPC-Fast/'.$XML::RPC::Fast::VERSION;
62 my $ua = WWW::Curl::Easy->new;
63 $ua->setopt(CURLOPT_TIMEOUT, (exists $args{timeout} ? defined $args{timeout} ? $args{timeout} : 0 : 10) );
64 return bless {
65 lwp => $ua,
66 ua => $useragent,
67 }, $pkg;
68 }
69  
70 sub call {
71 my $self = shift;
72 my ($method, $url) = splice @_,0,2;
73 my %args = @_;
74 $args{cb} or croak "cb required for useragent @{[%args]}";
75 #warn "call";
76 if( utf8::is_utf8($args{body}) ) {
77 carp "got an utf8 body: $args{body}";
78 utf8::encode($args{body});
79 }
80 if (uc $method eq 'POST') {
81 $self->{lwp}->setopt(CURLOPT_POST, 1)
82 } elsif (uc $method eq 'GET') {
83 $self->{lwp}->setopt(CURLOPT_HTTPGET, 1)
84 }
85 $self->{lwp}->setopt(CURLOPT_URL, $url);
86 {
87 use bytes;
88 my $headers = [
89 'Content-Type: text/xml',
90 "UserAgent: $self->{ua}",
91 (map { "$_: $args{headers}{$_}" } keys %{$args{headers}}),
92 'Content-Length:' . length($args{body})
93 ];
94 $self->{lwp}->setopt(CURLOPT_HTTPHEADER, $headers);
95 }
96 $self->{lwp}->setopt(CURLOPT_POSTFIELDS, $args{body});
97 $self->{lwp}->setopt(CURLOPT_VERBOSE) if $ENV{RPC_DEBUG};
98 my $response_body;
99 my $response = $self->{lwp}->setopt(CURLOPT_WRITEDATA,\$response_body);
100 my $res = $self->{lwp}->perform();
101 if ($res != 0) {
102 die $self->{lwp}->strerror($res);
103 }
104 #warn sprintf "http call lasts %0.3fs",time - $start if DEBUG_TIMES;
105 $args{cb}( HTTP::Response->new(
106 $self->{lwp}->getinfo(CURLINFO_HTTP_CODE),
107 '',
108 [],
109 $response_body,
110 ) );
111 }
112  
113 =head1 COPYRIGHT & LICENSE
114  
115 Copyright (c) 2008-2011 Mons Anderson, Andrii Kostenko.
116  
117 This program is free software; you can redistribute it and/or modify it
118 under the same terms as Perl itself.
119  
120 =head1 AUTHOR
121  
122 Mons Anderson, C<< <mons@cpan.org> >>, Andrii Kostenko C<< <andrey@kostenko.name> >>
123  
124 =cut
125  
126 1;