Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2004-04-20
4
486 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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…

808 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