1 package Net::HTTPS::Any;
5 use vars qw(@EXPORT_OK $ssl_module $skip_NetSSLeay);
10 @EXPORT_OK = qw( https_get https_post );
17 die if defined($skip_NetSSLeay) && $skip_NetSSLeay;
19 Net::SSLeay->VERSION(1.30);
22 # qw(get_https post_https make_form make_headers);
23 $ssl_module = 'Net::SSLeay';
28 require LWP::UserAgent;
29 require HTTP::Request::Common;
30 require Crypt::SSLeay;
32 #import HTTP::Request::Common qw(GET POST);
33 $ssl_module = 'Crypt::SSLeay';
37 unless ($ssl_module) {
38 die "One of Net::SSLeay (v1.30 or later)"
39 . " or Crypt::SSLeay (+LWP) is required";
46 Net::HTTPS::Any - Simple HTTPS class using whatever underlying module is available
54 our $VERSION = '0.09';
58 use Net::HTTPS::Any qw(https_get https_post);
60 ( $page, $response, %reply_headers )
62 { 'host' => 'secure.sisd.com',
64 'path' => '/freeside/index.html',
65 'args' => { 'field' => 'value' },
66 #'args' => [ 'field'=>'value' ], #order preserved
70 ( $page, $response, %reply_headers )
72 'host' => 'secure.sisd.com',
74 'path' => '/freeside/index.html',
75 'args' => { 'field' => 'value' },
76 #'args' => [ 'field'=>'value' ], #order preserved
83 This is a simple wrapper around either of the two available SSL
86 It depends on Net::SSLeay _or_ ( Crypt::SSLeay and LWP::UserAgent ).
90 =head2 https_get HASHREF | FIELD => VALUE, ...
92 Accepts parameters as either a hashref or a list of fields and values.
104 =item headers (hashref)
106 For example: { 'X-Header1' => 'value', ... }
110 For example: 'text/namevalue',
114 CGI arguments, eitehr as a hashref or a listref. In the latter case, ordering
115 is preserved (see L<Tie::IxHash> to do so when passing a hashref).
119 Returns a list consisting of the page content as a string, the HTTP
120 response code and message (i.e. "200 OK" or "404 Not Found"), and a list of
121 key/value pairs representing the HTTP response headers.
126 my $opts = ref($_[0]) ? shift : { @_ }; #hashref or list
128 # accept a hashref or a list (keep it ordered)
130 if ( exists($opts->{'args'}) && ref($opts->{'args'}) eq 'HASH' ) {
132 } elsif ( exists($opts->{'args'}) && ref($opts->{'args'}) eq 'ARRAY' ) {
133 tie my %hash, 'Tie::IxHash', @{ $opts->{'args'} };
137 $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
141 if ( ref( $opts->{headers} ) eq "HASH" ) {
142 %headers = %{ $opts->{headers} };
144 $headers{'Host'} ||= $opts->{'host'};
146 my $path = $opts->{'path'};
147 if ( keys %$post_data ) {
150 map { uri_escape($_) . '=' . uri_escape( $post_data->{$_} ) }
154 if ( $ssl_module eq 'Net::SSLeay' ) {
156 import Net::SSLeay qw(get_https make_headers);
157 my $headers = make_headers(%headers);
159 my( $res_page, $res_code, @res_headers ) =
160 get_https( $opts->{'host'},
165 $opts->{"Content-Type"},
168 return ( $res_page, $res_code, @res_headers );
170 } elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
172 import HTTP::Request::Common qw(GET);
174 my $url = 'https://' . $opts->{'host'};
175 $url .= ':' . $opts->{'port'}
176 unless $opts->{'port'} == 443;
179 my $ua = new LWP::UserAgent;
180 foreach my $hdr ( keys %headers ) {
181 $ua->default_header( $hdr => $headers{$hdr} );
183 my $res = $ua->request( GET($url) );
185 my @res_headers = map { $_ => $res->header($_) }
186 $res->header_field_names;
188 return ( $res->content, $res->code. ' '. $res->message, @res_headers );
191 die "unknown SSL module $ssl_module";
196 =head2 https_post HASHREF | FIELD => VALUE, ...
198 Accepts parameters as either a hashref or a list of fields and values.
210 =item headers (hashref)
212 For example: { 'X-Header1' => 'value', ... }
216 For example: 'text/namevalue',
220 CGI arguments, eitehr as a hashref or a listref. In the latter case, ordering
221 is preserved (see L<Tie::IxHash> to do so when passing a hashref).
225 Raw content (overrides args). A simple scalar containing the raw content.
230 Returns a list consisting of the page content as a string, the HTTP
231 response code and message (i.e. "200 OK" or "404 Not Found"), and a list of
232 key/value pairs representing the HTTP response headers.
237 my $opts = ref($_[0]) ? shift : { @_ }; #hashref or list
239 # accept a hashref or a list (keep it ordered). or a scalar of content.
241 if ( exists($opts->{'args'}) && ref($opts->{'args'}) eq 'HASH' ) {
243 } elsif ( exists($opts->{'args'}) && ref($opts->{'args'}) eq 'ARRAY' ) {
244 tie my %hash, 'Tie::IxHash', @{ $opts->{'args'} };
247 if ( exists $opts->{'content'} ) {
248 $post_data = $opts->{'content'};
251 $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
255 if ( ref( $opts->{headers} ) eq "HASH" ) {
256 %headers = %{ $opts->{headers} };
258 $headers{'Host'} ||= $opts->{'host'};
260 if ( $ssl_module eq 'Net::SSLeay' ) {
262 import Net::SSLeay qw(post_https make_headers make_form);
263 my $headers = make_headers(%headers);
265 my $raw_data = ref($post_data) ? make_form(%$post_data) : $post_data;
267 my( $res_page, $res_code, @res_headers ) =
268 post_https( $opts->{'host'},
273 $opts->{"Content-Type"},
276 return ( $res_page, $res_code, @res_headers );
278 } elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
280 import HTTP::Request::Common qw(POST);
282 my $url = 'https://' . $opts->{'host'};
283 $url .= ':' . $opts->{'port'}
284 unless $opts->{'port'} == 443;
285 $url .= $opts->{'path'};
287 my $ua = new LWP::UserAgent;
288 foreach my $hdr ( keys %headers ) {
289 $ua->default_header( $hdr => $headers{$hdr} );
293 if ( ref($post_data) ) {
294 $res = $ua->request( POST( $url, [%$post_data] ) );
297 my $req = new HTTP::Request( 'POST' => $url );
298 $req->content_type( $opts->{"Content-Type"} );
299 $req->content($post_data);
300 $res = $ua->request($req);
303 my @res_headers = map { $_ => $res->header($_) }
304 $res->header_field_names;
306 return ( $res->content, $res->code. ' '. $res->message, @res_headers );
309 die "unknown SSL module $ssl_module";
316 Ivan Kohler, C<< <ivan-net-https-any at freeside.biz> >>
320 Please report any bugs or feature requests to C<bug-net-https-any at rt.cpan.org>, or through
321 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-HTTPS-Any>. I will be notified, and then you'll
322 automatically be notified of progress on your bug as I make changes.
326 You can find documentation for this module with the perldoc command.
328 perldoc Net::HTTPS::Any
331 You can also look for information at:
335 =item * RT: CPAN's request tracker
337 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-HTTPS-Any>
339 =item * AnnoCPAN: Annotated CPAN documentation
341 L<http://annocpan.org/dist/Net-HTTPS-Any>
345 L<http://cpanratings.perl.org/d/Net-HTTPS-Any>
349 L<http://search.cpan.org/dist/Net-HTTPS-Any>
354 =head1 ACKNOWLEDGEMENTS
357 =head1 COPYRIGHT & LICENSE
359 Copyright 2008 Freeside Internet Services, Inc. (http://freeside.biz/)
362 This program is free software; you can redistribute it and/or modify it
363 under the same terms as Perl itself.