Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

815 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

12 Experts available now in Live!

Get 1:1 Help Now