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
87 It depends on Net::SSLeay _or_ ( Crypt::SSLeay and LWP::UserAgent ).
91 =head2 https_get HASHREF | FIELD => VALUE, ...
93 Accepts parameters as either a hashref or a list of fields and values.
105 =item headers (hashref)
107 For example: { 'X-Header1' => 'value', ... }
111 For example: 'text/namevalue',
115 CGI arguments, eitehr as a hashref or a listref. In the latter case, ordering
116 is preserved (see L<Tie::IxHash> to do so when passing a hashref).
122 Returns a list consisting of the page content as a string, the HTTP
123 response code and message (i.e. "200 OK" or "404 Not Found"), and a list of
124 key/value pairs representing the HTTP response headers.
129 my $opts = ref($_[0]) ? shift : { @_ }; #hashref or list
131 # accept a hashref or a list (keep it ordered)
133 if ( exists($opts->{'args'}) && ref($opts->{'args'}) eq 'HASH' ) {
135 } elsif ( exists($opts->{'args'}) && ref($opts->{'args'}) eq 'ARRAY' ) {
136 tie my %hash, 'Tie::IxHash', @{ $opts->{'args'} };
140 $opts->{'port'} ||= 443;
141 $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
145 if ( ref( $opts->{headers} ) eq "HASH" ) {
146 %headers = %{ $opts->{headers} };
148 $headers{'Host'} ||= $opts->{'host'};
150 my $path = $opts->{'path'};
151 if ( keys %$post_data ) {
154 map { uri_escape($_) . '=' . uri_escape( $post_data->{$_} ) }
158 if ( $ssl_module eq 'Net::SSLeay' ) {
160 import Net::SSLeay qw(get_https make_headers);
161 my $headers = make_headers(%headers);
163 $Net::SSLeay::trace = $opts->{'debug'} if exists $opts->{'debug'};
165 my( $res_page, $res_code, @res_headers ) =
166 get_https( $opts->{'host'},
171 $opts->{"Content-Type"},
174 return ( $res_page, $res_code, @res_headers );
176 } elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
178 import HTTP::Request::Common qw(GET);
180 my $url = 'https://' . $opts->{'host'};
181 $url .= ':' . $opts->{'port'}
182 unless $opts->{'port'} == 443;
185 my $ua = new LWP::UserAgent;
186 foreach my $hdr ( keys %headers ) {
187 $ua->default_header( $hdr => $headers{$hdr} );
189 $ENV{HTTPS_DEBUG} = $opts->{'debug'} if exists $opts->{'debug'};
190 my $res = $ua->request( GET($url) );
192 my @res_headers = map { $_ => $res->header($_) }
193 $res->header_field_names;
195 return ( $res->content, $res->code. ' '. $res->message, @res_headers );
198 die "unknown SSL module $ssl_module";
203 =head2 https_post HASHREF | FIELD => VALUE, ...
205 Accepts parameters as either a hashref or a list of fields and values.
217 =item headers (hashref)
219 For example: { 'X-Header1' => 'value', ... }
223 For example: 'text/namevalue',
227 CGI arguments, eitehr as a hashref or a listref. In the latter case, ordering
228 is preserved (see L<Tie::IxHash> to do so when passing a hashref).
232 Raw content (overrides args). A simple scalar containing the raw content.
237 Returns a list consisting of the page content as a string, the HTTP
238 response code and message (i.e. "200 OK" or "404 Not Found"), and a list of
239 key/value pairs representing the HTTP response headers.
244 my $opts = ref($_[0]) ? shift : { @_ }; #hashref or list
246 # accept a hashref or a list (keep it ordered). or a scalar of content.
248 if ( exists($opts->{'args'}) && ref($opts->{'args'}) eq 'HASH' ) {
250 } elsif ( exists($opts->{'args'}) && ref($opts->{'args'}) eq 'ARRAY' ) {
251 tie my %hash, 'Tie::IxHash', @{ $opts->{'args'} };
254 if ( exists $opts->{'content'} ) {
255 $post_data = $opts->{'content'};
258 $opts->{'port'} ||= 443;
259 $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
263 if ( ref( $opts->{headers} ) eq "HASH" ) {
264 %headers = %{ $opts->{headers} };
266 $headers{'Host'} ||= $opts->{'host'};
268 if ( $ssl_module eq 'Net::SSLeay' ) {
270 import Net::SSLeay qw(post_https make_headers make_form);
271 my $headers = make_headers(%headers);
273 my $raw_data = ref($post_data) ? make_form(%$post_data) : $post_data;
275 $Net::SSLeay::trace = $opts->{'debug'} if exists $opts->{'debug'};
277 my( $res_page, $res_code, @res_headers ) =
278 post_https( $opts->{'host'},
283 $opts->{"Content-Type"},
286 return ( $res_page, $res_code, @res_headers );
288 } elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
290 import HTTP::Request::Common qw(POST);
292 my $url = 'https://' . $opts->{'host'};
293 $url .= ':' . $opts->{'port'}
294 unless $opts->{'port'} == 443;
295 $url .= $opts->{'path'};
297 my $ua = new LWP::UserAgent;
298 foreach my $hdr ( keys %headers ) {
299 $ua->default_header( $hdr => $headers{$hdr} );
302 $ENV{HTTPS_DEBUG} = $opts->{'debug'} if exists $opts->{'debug'};
305 if ( ref($post_data) ) {
306 $res = $ua->request( POST( $url, [%$post_data] ) );
309 my $req = new HTTP::Request( 'POST' => $url );
310 $req->content_type( $opts->{"Content-Type"} );
311 $req->content($post_data);
312 $res = $ua->request($req);
315 my @res_headers = map { $_ => $res->header($_) }
316 $res->header_field_names;
318 return ( $res->content, $res->code. ' '. $res->message, @res_headers );
321 die "unknown SSL module $ssl_module";
328 Ivan Kohler, C<< <ivan-net-https-any at freeside.biz> >>
332 Please report any bugs or feature requests to C<bug-net-https-any at rt.cpan.org>, or through
333 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-HTTPS-Any>. I will be notified, and then you'll
334 automatically be notified of progress on your bug as I make changes.
338 You can find documentation for this module with the perldoc command.
340 perldoc Net::HTTPS::Any
343 You can also look for information at:
347 =item * RT: CPAN's request tracker
349 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-HTTPS-Any>
351 =item * AnnoCPAN: Annotated CPAN documentation
353 L<http://annocpan.org/dist/Net-HTTPS-Any>
357 L<http://cpanratings.perl.org/d/Net-HTTPS-Any>
361 L<http://search.cpan.org/dist/Net-HTTPS-Any>
366 =head1 ACKNOWLEDGEMENTS
369 =head1 COPYRIGHT & LICENSE
371 Copyright 2008 Freeside Internet Services, Inc. (http://freeside.biz/)
374 This program is free software; you can redistribute it and/or modify it
375 under the same terms as Perl itself.