package CallHttpdS;
use PDSUtil;
use Socket qw(:DEFAULT :crlf);
use IO::Socket::SSL;

require Exporter;
use vars       qw($VERSION @ISA @EXPORT);

# set the version for version checking
$VERSION     = 0.01;

@ISA         = qw(Exporter);
@EXPORT      = qw(&call_https);

my $debug = "";

$debug = PDSUtil::getEnvironmentParams('debug');

sub call_https {
    my ($method,$host_name,$cgi_name,$cgi_param,$timeout) = @_;
    my $time = 30;
    my $socket = "";
    my $response="";
    my $command = "";
    my $rin = "";


    PDSUtil::printLog ("INFO\t[CallHttpdS::call_https]\tPerforming a secure HTTPS request using SSL socket");	

    if ($debug eq "Y") {
        PDSUtil::printLog("[CallHttpdS::call_https]\t debug information");
        PDSUtil::printLog("[CallHttpdS::call_https]\tmethod      = $method");
        PDSUtil::printLog("[CallHttpdS::call_https]\thost_name   = $host_name");
        PDSUtil::printLog("[CallHttpdS::call_https]\tcgi_name    = $cgi_name");
        my $tmp_cgi_param=PDSUtil::PWEncrypt($cgi_param);
        PDSUtil::printLog("[CallHttpdS::call_https]\tcgi_param   = $tmp_cgi_param");
    }


    if ($host_name =~ /http:\/\/(.*)/) {
        $host_name = $1;
    }
    if ($host_name =~ /(.*):(\d*)/) {
        $host_name = $1;
        $port      = $2;
    }
    if ($cgi_name =~ /^\/(.*)/) {
        $cgi_name = $1;
    }
    if ($cgi_name =~ /(.*)\?$/) {
        $cgi_name = $1;
    }

    if ($method eq 'GET') {
        if (length $cgi_param) {
            $command =  "GET /$cgi_name?$cgi_param HTTP/1.0$CRLF" ;
        } else {
            $command =  "GET /$cgi_name HTTP/1.0$CRLF" ;
        }
        $command .=     "Connection: close$CRLF" .
                        "Accept: */*$CRLF" .
                        "Host: $host_name:$port$CRLF" .
                        "User-Agent: libwww-perl/5.63$CRLF$CRLF";

    } elsif ($method eq 'POST') {
        $command =      "POST /$cgi_name HTTP/1.0$CRLF" .
                        "Connection: close$CRLF" .
                        "Accept: */*$CRLF" .
                        "Host: $host_name:$port$CRLF" .
                        "User-Agent: libwww-perl/5.63$CRLF".
                        "Content-Length: " . length ($cgi_param) . "$CRLF$CRLF" .
                        "$cgi_param";

    } else {
	PDSUtil::printLog ("[CallHttpdS::call_https]\tError (call_https_timeout)");
	PDSUtil::printLog ("ERROR\t[CallHttpdS::call_https]\tUnsupported method-type $method");
        return "";
    }

    
    if($timeout and $timeout eq 'Y'){
        
	PDSUtil::printLog ("[CallHttpdS::call_https]\tTIMEOUT = TRUE");
        $socket = IO::Socket::SSL->new(     PeerAddr => $host_name,
                                            PeerPort => $port,
                                            SSL_verify_mode => 0,
                                            Timeout  => 1,
                                            Proto    => "tcp", );
    }
    
    else{
        $socket = IO::Socket::SSL->new(     PeerAddr => $host_name,
                                            PeerPort => $port,
                                            SSL_verify_mode => 0,
                                            Proto    => "tcp", );
    }


    if (!($socket)) {
	PDSUtil::printLog ("[CallHttpdS::call_https]\tError:Cannot make connection to server");
        return $response;
    }

    $socket->autoflush(1);
    print $socket $command;

    vec($rin, fileno($socket), 1) = 1;

    while (1) {
        $sel = select($rin, undef, undef, $time);
        if ($sel==0) {
            PDSUtil::printLog ( "[CallHttpdS::call_https]\tWarning : TIMEOUT occured ");
            return $response;
        } elsif ($sel==-1) {
            PDSUtil::printLog ("[CallHttpdS::call_https]\tWarning : Error in select()");
            return $response;
        }

        my $line = <$socket>;
        $response .= $line;
        if (length $line == 0) {
            last;
        }
    }

    close $socket;

    return $response;
}

1;
