blib/lib/XAS/Lib/Curl/HTTP.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« blib/lib/XAS/Lib/Modules/Alerts.pm ↑ Summary blib/lib/XAS/Lib/SSH/Client.pm »
1 package XAS::Lib::Curl::HTTP;
2  
3 our $VERSION = '0.01';
4  
5 1 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use WWW::Curl::Easy;
Subroutine BEGIN: called CC=1 SCAR=2.6
7  
8 use XAS::Class
9 debug => 0,
10 version => $VERSION,
11 base => 'XAS::Base',
12 accessors => 'curl',
13 mutators => 'retcode',
14 utils => 'dotid',
15 vars => {
16 PARAMS => {
17 -keep_alive => { optional => 1, default => 1 },
18 -followlocation => { optional => 1, default => 1 },
19 -max_redirects => { optional => 1, default => 3 },
20 -ssl_verify_peer => { optional => 1, default => 1 },
21 -ssl_verify_host => { optional => 1, default => 1 },
22 -timeout => { optional => 1, default => 60 },
23 -connect_timeout => { optional => 1, default => 300 },
24 -ssl_cacert => { optional => 1, default => undef },
25 -ssl_keypasswd => { optional => 1, default => undef },
26 -proxy_url => { optional => 1, default => undef },
27 -ssl_cert => { optional => 1, default => undef, depends => [ 'ssl_key' ] },
28 -ssl_key => { optional => 1, default => undef, depends => [ 'ssl_cert' ] },
29 -password => { optional => 1, default => undef, depends => [ 'username' ] },
30 -username => { optional => 1, default => undef, depends => [ 'password' ] },
31 -proxy_password => { optional => 1, default => undef, depends => [ 'proxy_username' ] },
32 -proxy_username => { optional => 1, default => undef, depends => [ 'proxy_password' ] },
33 -auth_method => { optional => 1, default => 'noauth', regex => qr/any|noauth|basic|digest|ntlm|negotiate/ },
34 -proxy_auth => { optional => 1, default => 'noauth', regex => qr/any|noauth|basic|digest|ntlm|negotiate/ },
35 }
36 },
37 ;
38  
39 # ----------------------------------------------------------------------
40 # Public Methods
41 # ----------------------------------------------------------------------
42  
43 sub request {
44 my $self = shift;
45 my ($request) = $self->validate_params(\@_, [
46 { isa => 'HTTP::Request' }
47 ]);
48  
49 my @body;
50 my @head;
51 my $response = undef;
52 my $header = $request->headers->as_string("\n");
53 my @headers = split("\n", $header);
54  
55 $self->curl->setopt(CURLOPT_URL, $request->uri);
56 $self->curl->setopt(CURLOPT_HTTPHEADER, \@headers) if (scalar(@headers));
57  
58 # I/O for the request
59  
60 $self->curl->setopt(CURLOPT_WRITEDATA, \@body);
61 $self->curl->setopt(CURLOPT_HEADERDATA, \@head);
62 $self->curl->setopt(CURLOPT_READFUNCTION, \&_read_callback);
63 $self->curl->setopt(CURLOPT_WRITEFUNCTION, \&_write_callback);
64 $self->curl->setopt(CURLOPT_HEADERFUNCTION, \&_write_callback);
65  
66 # other options depending on request type
67  
68 if ($request->method eq 'GET') {
69  
70 $self->curl->setopt(CURLOPT_HTTPGET, 1);
71  
72 } elsif ($request->method eq 'POST') {
73  
74 use bytes;
75  
76 my $content = $request->content;
77  
78 $self->curl->setopt(CURLOPT_POST, 1);
79 $self->curl->setopt(CURLOPT_POSTFIELDSIZE, length($content));
80 $self->curl->setopt(CURLOPT_COPYPOSTFIELDS, $content);
81  
82 } elsif ($request->method eq 'PUT') {
83  
84 use bytes;
85  
86 my $content = $request->content;
87  
88 $self->curl->setopt(CURLOPT_UPLOAD, 1);
89 $self->curl->setopt(CURLOPT_READDATA, \$content);
90 $self->curl->setopt(CURLOPT_INFILESIZE, length($content));
91  
92 } elsif ($request->method eq 'HEAD') {
93  
94 $self->curl->setopt(CURLOPT_NOBODY, 1);
95  
96 } else {
97  
98 $self->curl->setopt(CURLOPT_CUSTOMREQUEST, uc $request->method);
99  
100 }
101  
102 # perform the request and create the response
103  
104 if (($self->{retcode} = $self->curl->perform) == 0) {
105  
106 my @temp;
107 my $message;
108 my $content;
109  
110 # there may be multiple responses within head, we only
111 # want the last one. so search backwards until a HTTP header
112 # is found.
113  
114 while (my $line = pop(@head)) {
115  
116 push(@temp, $line);
117 last if ($line =~ /^HTTP\//);
118  
119 }
120  
121 $content = join('', reverse(@temp), @body);
122  
123 # now let HTTP::Response figure it all out...
124  
125 $response = HTTP::Response->parse($content);
126  
127 # do some fixups
128  
129 $message = $response->message;
130 $response->message($message) if ($message =~ s/\r//g);
131 $response->request($request);
132  
133 } else {
134  
135 $self->throw_msg(
136 dotid($self->class) . '.request.curl',
137 'curl',
138 $self->retcode, lc($self->curl->strerror($self->retcode))
139 );
140  
141 }
142  
143 return $response;
144  
145 }
146  
147 # ----------------------------------------------------------------------
148 # Private Methods
149 # ----------------------------------------------------------------------
150  
151 sub _read_callback {
152 my ($maxlength, $pointer) = @_;
153  
154 my $data = substr($$pointer, 0, $maxlength);
155  
156 $$pointer =
157 length($$pointer) > $maxlength
158 ? scalar substr($$pointer, $maxlength)
159 : '';
160  
161 return $data;
162  
163 }
164  
165 sub _write_callback {
166 my ($data, $pointer) = @_;
167  
168 push(@{$pointer}, $data);
169  
170 return length($data);
171  
172 }
173  
174 sub init {
175 my $class = shift;
176  
177 my $self = $class->SUPER::init(@_);
178  
179 my $authen = 0;
180 my $timeout = $self->timeout * 1000;
181 my $protocols = (CURLPROTO_HTTP & CURLPROTO_HTTPS);
182 my $connect_timeout = $self->timeout * 1000;
183  
184 $self->{curl} = WWW::Curl::Easy->new();
185  
186 # basic options
187  
188 $self->curl->setopt(CURLOPT_HEADER, 0);
189 $self->curl->setopt(CURLOPT_VERBOSE, $self->xdebug);
190 $self->curl->setopt(CURLOPT_MAXREDIRS, $self->max_redirects);
191 $self->curl->setopt(CURLOPT_PROTOCOLS, $protocols);
192 $self->curl->setopt(CURLOPT_NOPROGRESS, 1);
193 $self->curl->setopt(CURLOPT_TIMEOUT_MS, $timeout);
194 $self->curl->setopt(CURLOPT_FORBID_REUSE, $self->keep_alive);
195 $self->curl->setopt(CURLOPT_FOLLOWLOCATION, $self->followlocation);
196 $self->curl->setopt(CURLOPT_CONNECTTIMEOUT_MS, $connect_timeout);
197  
198 # setup authentication
199  
200 $authen = CURLAUTH_ANY if ($self->auth_method eq 'any');
201 $authen = CURLAUTH_NTLM if ($self->auth_method eq 'ntlm');
202 $authen = CURLAUTH_BASIC if ($self->auth_method eq 'basic');
203 $authen = CURLAUTH_DIGEST if ($self->auth_method eq 'digest');
204 $authen = CURLAUTH_GSSNEGOTIATE if ($self->auth_method eq 'negotitate');
205  
206 $self->curl->setopt(CURLOPT_HTTPAUTH, $authen);
207  
208 if ($self->username) {
209  
210 $self->curl->setopt(CURLOPT_USERNAME, $self->username);
211 $self->curl->setopt(CURLOPT_PASSWORD, $self->password);
212  
213 }
214  
215 # setup proxy stuff
216  
217 if ($self->proxy_url) {
218  
219 $authen = 0;
220  
221 $authen = CURLAUTH_ANY if ($self->proxy_auth eq 'any');
222 $authen = CURLAUTH_NTLM if ($self->proxy_auth eq 'ntlm');
223 $authen = CURLAUTH_BASIC if ($self->proxy_auth eq 'basic');
224 $authen = CURLAUTH_DIGEST if ($self->proxy_auth eq 'digest');
225 $authen = CURLAUTH_GSSNEGOTIATE if ($self->proxy_auth eq 'negotitate');
226  
227 $self->curl->setopt(CURLOPT_PROXY, $self->proxy_url);
228 $self->curl->setopt(CURLOPT_PROXYAUTH, $authen);
229 $self->curl->setopt(CURLOPT_PROXYUSERNAME, $self->proxy_username);
230 $self->curl->setopt(CURLOPT_PROXYPASSWORD, $self->proxy_password);
231  
232 }
233  
234 # set up the SSL stuff
235  
236 $self->curl->setopt(CURLOPT_SSL_VERIFYPEER, $self->ssl_verify_peer);
237 $self->curl->setopt(CURLOPT_SSL_VERIFYHOST, $self->ssl_verify_host);
238  
239 if ($self->ssl_keypasswd) {
240  
241 $self->curl->setop(CURLOPT_KEYPASSWD, $self->ssl_keypasswd);
242  
243 }
244  
245 if ($self->ssl_cacert) {
246  
247 $self->curl->setopt(CURLOPT_CAINFO, $self->ssl_cacert);
248  
249 }
250  
251 if ($self->ssl_cert) {
252  
253 $self->curl->setopt(CURLOPT_SSLCERT, $self->ssl_cert);
254 $self->curl->setopt(CURLOPT_SSLKEY, $self->ssl_key);
255  
256 }
257  
258 return $self;
259  
260 }
261  
262 1;
263  
264 __END__
265  
266 =head1 NAME
267  
268 XAS::Lib::Curl::HTTP - A class for the XAS environment
269  
270 =head1 SYNOPSIS
271  
272 use HTTP::Request;
273 use XAS::Lib::Curl::HTTP;
274  
275 my $response;
276 my $request = HTTP::Request->new(GET => 'http://scm.kesteb.us/trac');
277 my $ua = XAS::Lib::Curl::HTTP->new();
278  
279 $response = $ua->request($request);
280 print $response->content;
281  
282 =head1 DESCRIPTION
283  
284 This module uses L<libcurl|http://curl.haxx.se/libcurl/> as the HTTP engine
285 to make requests from a web server.
286  
287 =head1 METHODS
288  
289 All true/false values use 0/1 as the indicator.
290  
291 =head2 new
292  
293 This method initializes the module and takes the following parameters:
294  
295 =over 4
296  
297 =item B<-keep_alive>
298  
299 A toggle to tell curl to forbid the reuse of sockets, defaults to true.
300  
301 =item B<-followlocation>
302  
303 A toggle to tell curl to follow redirects, defaults to true.
304  
305 =item B<-max_redirects>
306  
307 The number of redirects to follow, defaults to 3.
308  
309 =item B<-timeout>
310  
311 The timeout for the connection, defaults to 60 seconds.
312  
313 =item B<-connect_timeout>
314  
315 The timeout for the initial connection, defaults to 300 seconds.
316  
317 =item B<-auth_method>
318  
319 The authentication method to use, defaults to 'noauth'. Possible
320 values are 'any', 'basic', 'digest', 'ntlm', 'negotiate'. If a username
321 and password are supplied, curl defaults to 'basic'.
322  
323 =item B<-password>
324  
325 An optional password to use, implies a username. Wither the password is
326 actually used, depends on -auth_method.
327  
328 =item B<-username>
329  
330 An optional username to use, implies a password.
331  
332 =item B<-ssl_cacert>
333  
334 An optional CA cerificate to use.
335  
336 =item B<-ssl_keypasswd>
337  
338 An optional password for a signed cerificate.
339  
340 =item B<-ssl_cert>
341  
342 An optional certificate to use.
343  
344 =item B<-ssl_key>
345  
346 An optional key for a certificate to use.
347  
348 =item B<-ssl_verify_host>
349  
350 Wither to verify the host certifcate, defaults to true.
351  
352 =item B<-ssl_verify_peer>
353  
354 Wither to verify the peer certificate, defaults to true.
355  
356 =item B<-proxy_url>
357  
358 The url of a proxy that needs to be transversed.
359  
360 =item B<-proxy_auth>
361  
362 The authentication method to use, defaults to 'noauth'. Possible
363 values are 'any', 'basic', 'digest', 'ntlm', 'negotiate'. If a proxy
364 username and a proxy password are supplied, curl defaults to 'basic'.
365  
366 =item B<-proxy_password>
367  
368 An optional password to use, implies a username. Wither the password is
369 actually used, depends on -proxy_auth.
370  
371 =item B<-proxy_username>
372  
373 An optional username to use, implies a password.
374  
375 =back
376  
377 =head2 request($request)
378  
379 This method sends the requset to the web server. The request will return
380 a L<HTTP::Response|https://metacpan.org/pod/HTTP::Response> object. It takes the following parameters:
381  
382 =over 4
383  
384 =item B<$request>
385  
386 A L<HTTP::Request|https://metacpan.org/pod/HTTP::Request> object.
387  
388 =back
389  
390 =head1 SEE ALSO
391  
392 =over 4
393  
394 =item L<XAS|XAS>
395  
396 =item L<WWW::Curl|https://metacpan.org/pod/WWW::Curl>
397  
398 =item L<libcurl|http://curl.haxx.se/libcurl/>
399  
400 =back
401  
402 =head1 AUTHOR
403  
404 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
405  
406 =head1 COPYRIGHT AND LICENSE
407  
408 Copyright (C) 2014 Kevin L. Esteb
409  
410 This is free software; you can redistribute it and/or modify it under
411 the terms of the Artistic License 2.0. For details, see the full text
412 of the license at http://www.perlfoundation.org/artistic_license_2_0.
413  
414 =cut
« blib/lib/XAS/Lib/Modules/Alerts.pm ↑ Summary blib/lib/XAS/Lib/SSH/Client.pm »