Solved

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

Posted on 2004-04-20
4
482 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 250 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 250 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now