Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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 video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

15 Experts available now in Live!

Get 1:1 Help Now