Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Net::SSL , LWP::UserAgent & HTTP/1.1

Posted on 2004-04-20
4
Medium Priority
?
497 Views
Last Modified: 2008-03-10
Hi,

I have a script that goes to a https server, requests some information, does a while($sock->read($line, 1024) to get the information, then when all the info is got, carries on.

The problem that I have is that this works fine with an HTTP/1.0 request, since the connection is closed automatically once all the data has been sent. However with HTTP/1.1 , it seems that the connection is kept open, until a "Connection: close" is received.

My question to you: What is the easiest way to change the following so that my script works?

Relevant parts of script:

sub connect{
       my $self = shift;
       my $host = shift;
       my $port = 443;

       $sock = Net::SSL->new(
                             PeerAddr => $host,
                             PeerPort => $port,
                             Timeout => 15,
                            ) || warn "couldnt connect\n";
}

<SNIP>

       pct->connect($host);

       $sock || ($@ ||= "no Net::SSL connection established");

       my $error = $@;

       $error && die("Can't connect to $host:$port; $error; $!");

       $sock->print("$method $path HTTP/1.1\nHost: www.blah.net\n\n");

       while($sock->read($test, 1024)){
               $acc .= $test;
               print $test;
               }
       $sock->print("Connection: close\n\n");
       $sock->close;

       @out = split("\are",$acc);


       shift @out;


       foreach my $line (@out)
       {
               $line =~ s/^\n//s;
               if($line =~ /^\"/)
               {
                       $return[$count]=$line;
                       $count++;
               }
              
       }
      
Any help is greatly appreciated.

Kind regards,

Tom


0
Comment
Question by:dew-genen-ny
4 Comments
 
LVL 8

Accepted Solution

by:
davorg earned 1000 total points
ID: 10868831
You know you'd make this all a lot easier for yourself if you just rewrote it use LWP or WWW::Mechanize. There's really no need to be writing socket level code to make HTTP requests.

Dave...
0
 
LVL 20

Assisted Solution

by:jmcg
jmcg earned 1000 total points
ID: 10869858
If you're going to run your connections at the socket level, you have to be prepared to do the protocol interpretation at that level. On a HTTP 1.1 connection, the content should arrive with either a Content-Length header or using "chunked" transfer encoding, each chunk with its own length and a zero-length chunk to mark the end. Check out the spec (section 3.6.1) at

http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1

and the sample pseudo-code (appendix 19.4.6) at

http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.4.6

Just be wary of servers (usually because of improperly written CGI scripts) that send incorrect Content-Length headers.

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question