1 package Net::HTTPS::Any;
5 use vars qw(@ISA @EXPORT_OK $ssl_module $skip_NetSSLeay);
10 @ISA = qw( Exporter );
11 @EXPORT_OK = qw( https_get https_post );
18 die if defined($skip_NetSSLeay) && $skip_NetSSLeay;
20 Net::SSLeay->VERSION(1.30);
23 # qw(get_https post_https make_form make_headers);
24 $ssl_module = 'Net::SSLeay';
29 require LWP::UserAgent;
30 require HTTP::Request::Common;
31 require Crypt::SSLeay;
33 #import HTTP::Request::Common qw(GET POST);
34 $ssl_module = 'Crypt::SSLeay';
38 unless ($ssl_module) {
39 die "One of Net::SSLeay (v1.30 or later)"
40 . " or Crypt::SSLeay (+LWP) is required";
47 Net::HTTPS::Any - Simple HTTPS class using whatever underlying module is available
55 our $VERSION = '0.09';
59 use Net::HTTPS::Any qw(https_get https_post);
61 ( $page, $response, %reply_headers )
63 { 'host' => 'secure.sisd.com',
65 'path' => '/freeside/index.html',
66 'args' => { 'field' => 'value' },
67 #'args' => [ 'field'=>'value' ], #order preserved
71 ( $page, $response, %reply_headers )
73 'host' => 'secure.sisd.com',
75 'path' => '/freeside/index.html',
76 'args' => { 'field' => 'value' },
77 #'args' => [ 'field'=>'value' ], #order preserved
84 This is a simple wrapper around either of the two available SSL
85 modules. It offers a unified API for send GET and POST requests over HTTPS
86 and receiving responses.
88 It depends on Net::SSLeay _or_ ( Crypt::SSLeay and LWP::UserAgent ).
92 =head2 https_get HASHREF | FIELD => VALUE, ...
94 Accepts parameters as either a hashref or a list of fields and values.
106 =item headers (hashref)
108 For example: { 'X-Header1' => 'value', ... }
112 For example: 'text/namevalue',
116 CGI arguments, eitehr as a hashref or a listref. In the latter case, ordering
117 is preserved (see L<Tie::IxHash> to do so when passing a hashref).
123 Returns a list consisting of the page content as a string, the HTTP
124 response code and message (i.e. "200 OK" or "404 Not Found"), and a list of
125 key/value pairs representing the HTTP response headers.
130 my $opts = ref($_[0]) ? shift : { @_ }; #hashref or list
132 # accept a hashref or a list (keep it ordered)
134 if ( exists($opts->{'args'}) && ref($opts->{'args'}) eq 'HASH' ) {
135 $post_data = $opts->{'args'};
136 } elsif ( exists($opts->{'args'}) && ref($opts->{'args'}) eq 'ARRAY' ) {
137 tie my %hash, 'Tie::IxHash', @{ $opts->{'args'} };
141 $opts->{'port'} ||= 443;
142 #$opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
146 if ( ref( $opts->{headers} ) eq "HASH" ) {
147 %headers = %{ $opts->{headers} };
149 $headers{'Host'} ||= $opts->{'host'};
151 my $path = $opts->{'path'};
152 if ( keys %$post_data ) {
155 map { uri_escape($_) . '=' . uri_escape( $post_data->{$_} ) }
159 if ( $ssl_module eq 'Net::SSLeay' ) {
161 import Net::SSLeay qw(get_https make_headers);
162 my $headers = make_headers(%headers);
164 $Net::SSLeay::trace = $opts->{'debug'} if exists $opts->{'debug'};
166 my( $res_page, $res_code, @res_headers ) =
167 get_https( $opts->{'host'},
172 $opts->{"Content-Type"},
175 return ( $res_page, $res_code, @res_headers );
177 } elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
179 import HTTP::Request::Common qw(GET);
181 my $url = 'https://' . $opts->{'host'};
182 $url .= ':' . $opts->{'port'}
183 unless $opts->{'port'} == 443;
186 my $ua = new LWP::UserAgent;
187 foreach my $hdr ( keys %headers ) {
188 $ua->default_header( $hdr => $headers{$hdr} );
190 $ENV{HTTPS_DEBUG} = $opts->{'debug'} if exists $opts->{'debug'};
191 my $res = $ua->request( GET($url) );
193 my @res_headers = map { $_ => $res->header($_) }
194 $res->header_field_names;
196 return ( $res->content, $res->code. ' '. $res->message, @res_headers );
199 die "unknown SSL module $ssl_module";
204 =head2 https_post HASHREF | FIELD => VALUE, ...
206 Accepts parameters as either a hashref or a list of fields and values.
218 =item headers (hashref)
220 For example: { 'X-Header1' => 'value', ... }
224 For example: 'text/namevalue',
228 CGI arguments, eitehr as a hashref or a listref. In the latter case, ordering
229 is preserved (see L<Tie::IxHash> to do so when passing a hashref).
233 Raw content (overrides args). A simple scalar containing the raw content.
238 Returns a list consisting of the page content as a string, the HTTP
239 response code and message (i.e. "200 OK" or "404 Not Found"), and a list of
240 key/value pairs representing the HTTP response headers.
245 my $opts = ref($_[0]) ? shift : { @_ }; #hashref or list
247 # accept a hashref or a list (keep it ordered). or a scalar of content.
249 if ( exists($opts->{'args'}) && ref($opts->{'args'}) eq 'HASH' ) {
250 $post_data = $opts->{'args'};
251 } elsif ( exists($opts->{'args'}) && ref($opts->{'args'}) eq 'ARRAY' ) {
252 tie my %hash, 'Tie::IxHash', @{ $opts->{'args'} };
255 if ( exists $opts->{'content'} ) {
256 $post_data = $opts->{'content'};
259 $opts->{'port'} ||= 443;
260 $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
264 if ( ref( $opts->{headers} ) eq "HASH" ) {
265 %headers = %{ $opts->{headers} };
267 $headers{'Host'} ||= $opts->{'host'};
269 if ( $ssl_module eq 'Net::SSLeay' ) {
271 import Net::SSLeay qw(post_https make_headers make_form);
272 my $headers = make_headers(%headers);
274 my $raw_data = ref($post_data) ? make_form(%$post_data) : $post_data;
276 $Net::SSLeay::trace = $opts->{'debug'} if exists $opts->{'debug'};
278 my( $res_page, $res_code, @res_headers ) =
279 post_https( $opts->{'host'},
284 $opts->{"Content-Type"},
287 return ( $res_page, $res_code, @res_headers );
289 } elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
291 import HTTP::Request::Common qw(POST);
293 my $url = 'https://' . $opts->{'host'};
294 $url .= ':' . $opts->{'port'}
295 unless $opts->{'port'} == 443;
296 $url .= $opts->{'path'};
298 my $ua = new LWP::UserAgent;
299 foreach my $hdr ( keys %headers ) {
300 $ua->default_header( $hdr => $headers{$hdr} );
303 $ENV{HTTPS_DEBUG} = $opts->{'debug'} if exists $opts->{'debug'};
306 if ( ref($post_data) ) {
307 $res = $ua->request( POST( $url, [%$post_data] ) );
310 my $req = new HTTP::Request( 'POST' => $url );
311 $req->content_type( $opts->{"Content-Type"} );
312 $req->content($post_data);
313 $res = $ua->request($req);
316 my @res_headers = map { $_ => $res->header($_) }
317 $res->header_field_names;
319 return ( $res->content, $res->code. ' '. $res->message, @res_headers );
322 die "unknown SSL module $ssl_module";
329 Ivan Kohler, C<< <ivan-net-https-any at freeside.biz> >>
333 Please report any bugs or feature requests to C<bug-net-https-any at rt.cpan.org>, or through
334 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-HTTPS-Any>. I will be notified, and then you'll
335 automatically be notified of progress on your bug as I make changes.
339 You can find documentation for this module with the perldoc command.
341 perldoc Net::HTTPS::Any
344 You can also look for information at:
348 =item * RT: CPAN's request tracker
350 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-HTTPS-Any>
352 =item * AnnoCPAN: Annotated CPAN documentation
354 L<http://annocpan.org/dist/Net-HTTPS-Any>
358 L<http://cpanratings.perl.org/d/Net-HTTPS-Any>
362 L<http://search.cpan.org/dist/Net-HTTPS-Any>
367 =head1 ACKNOWLEDGEMENTS
370 =head1 COPYRIGHT & LICENSE
372 Copyright 2008 Freeside Internet Services, Inc. (http://freeside.biz/)
375 This program is free software; you can redistribute it and/or modify it
376 under the same terms as Perl itself.