I created a perl script that reads in a xml file, then encodes the contents of 
the file into a string, opens a TCP socket over port 80, then POSTs the string 
to a cgi on a non-SSL webserver. Everything works fine, and here is that code:


#!/usr/bin/perl
use Socket;
use CGI;

#- open xml file and read it in
open(INFILE,"</tmp/caaresultstest.xml");
my @rdata = <INFILE>;
my $rstr = '';
foreach $line (@rdata) {
        $rstr .= $line;
}
close(INFILE);

print CGI::escape(rstr);

#encode xml file contents string
my $encrstr = $rstr;
$encrstr =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
$encrstr = "caaresult=$encrstr";
my $rlen = length $encrstr;


if(!defined open_TCP('F','http://www.xyz.com','80')) {
        print "Error connecting to web server\n";
        exit(-1);
}

print F "POST /cgi-bin/mycgi.pl HTTP/1.0\n";
print F "Accept: */*\n";
print F "User-Agent: caaresults/1.0\n";
print F "Content-type: application/x-www-form-urlencoded\n";
print F "Content-length: $rlen\n";
print F "\n";
print F "$encrstr";

my $response = <F>;
my $data = '';
while(<F>) {
 $data .= $_;
}
close(F);
print $data;

############
# open_TCP #
############
# Given ($file_handle, $dest, $port) return 1 if successful, undef when
# unsuccessful.
#
# Input: $fileHandle is the name of the filehandle to use
#        $dest is the name of the destination computer,
#              either IP address or hostname
#        $port is the port number
# Output: successful network connection in file handle
#
use Socket;
sub open_TCP
{
  # get parameters
  my ($FS, $dest, $port) = @_;
  my $proto = getprotobyname('tcp');
  socket($FS, PF_INET, SOCK_STREAM, $proto);
  my $sin = sockaddr_in($port,inet_aton($dest));
  connect($FS,$sin) || return undef;
  my $old_fh = select($FS);
  $| = 1;                       # don't buffer output
  select($old_fh);
  1;
}



The problem is that now I have to do this same thing with a HTTPS server over 
port 443. I have used openssl in the past to communicate with HTTPS servers but 
I only did a GET, not a POST. Here is how I successfully used openssl in perl 
to do a GET:

#
#!/usr/bin/perl
$OPENSSL = "/var/local/httpd/etc/openssl";
$pw = encode_base64("$userID:$pwd");
$echo = "GET /index.html HTTP/1.0\nConnection: close\nAuthorization:Basic 
$pw\nHost: $host:$port\n\n";

print "About to open an SSL connection on $host";
open (CHECK, "echo \"$echo\" | $OPENSSL s_client -quiet -connect 
www.xyz.com:443 -ssl3 2>&1 |");
print "SSL connection opened on $host.  Processing return data.";

#print out index.html file we asked for
LINE: while (<CHECK>)
{
 print "$_" if (defined $opt_d);   # DEBUG
}
close (CHECK)


I tried to use the code above and change the GET to a POST and attach the 
string to send several different ways, but I could not get it to work. Can 
anyone post an example of how to do a POST using openssl in perl.

Thanks........Paul


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to