don't cause uninitialized value warnings when debugging is off...
[Net-HTTPS-Any.git] / lib / Net / HTTPS / Any.pm
index c65c7d9..d42b618 100644 (file)
@@ -2,11 +2,12 @@ package Net::HTTPS::Any;
 
 use warnings;
 use strict;
-use vars qw(@EXPORT_OK $ssl_module $skip_NetSSLeay);
+use vars qw(@ISA @EXPORT_OK $ssl_module $skip_NetSSLeay);
 use Exporter;
 use URI::Escape;
 use Tie::IxHash;
 
+@ISA = qw( Exporter );
 @EXPORT_OK = qw( https_get https_post );
 
 BEGIN {
@@ -81,7 +82,8 @@ our $VERSION = '0.09';
 =head1 DESCRIPTION
 
 This is a simple wrapper around either of the two available SSL
-modules.
+modules.  It offers a unified API for send GET and POST requests over HTTPS
+and receiving responses.
 
 It depends on Net::SSLeay _or_ ( Crypt::SSLeay and LWP::UserAgent ).
 
@@ -114,6 +116,8 @@ For example: 'text/namevalue',
 CGI arguments, eitehr as a hashref or a listref.  In the latter case, ordering
 is preserved (see L<Tie::IxHash> to do so when passing a hashref).
 
+=item debug
+
 =back
 
 Returns a list consisting of the page content as a string, the HTTP
@@ -128,13 +132,14 @@ sub https_get {
     # accept a hashref or a list (keep it ordered)
     my $post_data = {};
     if (      exists($opts->{'args'}) && ref($opts->{'args'}) eq 'HASH'  ) {
-        $post_data = shift;
+        $post_data = $opts->{'args'};
     } elsif ( exists($opts->{'args'}) && ref($opts->{'args'}) eq 'ARRAY' ) {
         tie my %hash, 'Tie::IxHash', @{ $opts->{'args'} };
         $post_data = \%hash;
     }
 
-    $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
+    $opts->{'port'} ||= 443;
+    #$opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
 
     ### XXX referer!!!
     my %headers = ();
@@ -156,6 +161,9 @@ sub https_get {
         import Net::SSLeay qw(get_https make_headers);
         my $headers = make_headers(%headers);
 
+        $Net::SSLeay::trace = $opts->{'debug'}
+          if exists $opts->{'debug'} && $opts->{'debug'};
+
         my( $res_page, $res_code, @res_headers ) =
           get_https( $opts->{'host'},
                      $opts->{'port'},
@@ -180,6 +188,7 @@ sub https_get {
         foreach my $hdr ( keys %headers ) {
             $ua->default_header( $hdr => $headers{$hdr} );
         }
+        $ENV{HTTPS_DEBUG} = $opts->{'debug'} if exists $opts->{'debug'};
         my $res = $ua->request( GET($url) );
 
         my @res_headers = map { $_ => $res->header($_) }
@@ -239,7 +248,7 @@ sub https_post {
     # accept a hashref or a list (keep it ordered).  or a scalar of content.
     my $post_data = '';
     if (      exists($opts->{'args'}) && ref($opts->{'args'}) eq 'HASH'  ) {
-        $post_data = shift;
+        $post_data = $opts->{'args'};
     } elsif ( exists($opts->{'args'}) && ref($opts->{'args'}) eq 'ARRAY' ) {
         tie my %hash, 'Tie::IxHash', @{ $opts->{'args'} };
         $post_data = \%hash;
@@ -248,6 +257,7 @@ sub https_post {
         $post_data = $opts->{'content'};
     }
 
+    $opts->{'port'} ||= 443;
     $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
 
     ### XXX referer!!!
@@ -258,12 +268,15 @@ sub https_post {
     $headers{'Host'} ||= $opts->{'host'};
 
     if ( $ssl_module eq 'Net::SSLeay' ) {
-
+        
         import Net::SSLeay qw(post_https make_headers make_form);
         my $headers = make_headers(%headers);
 
         my $raw_data = ref($post_data) ? make_form(%$post_data) : $post_data;
 
+        $Net::SSLeay::trace = $opts->{'debug'}
+          if exists $opts->{'debug'} && $opts->{'debug'};
+
         my( $res_page, $res_code, @res_headers ) =
           post_https( $opts->{'host'},
                       $opts->{'port'},
@@ -289,6 +302,8 @@ sub https_post {
             $ua->default_header( $hdr => $headers{$hdr} );
         }
 
+        $ENV{HTTPS_DEBUG} = $opts->{'debug'} if exists $opts->{'debug'};
+
         my $res;
         if ( ref($post_data) ) {
             $res = $ua->request( POST( $url, [%$post_data] ) );