Re: an HTTP client upon a buffer-based IO, please?
Gisle Aas <[email protected]> Thu, 6 May 2010 21:55:47 +0200
| Newsgroups | gmane.comp.lang.perl.modules.lwp |
|---|---|
| Message-ID | <[email protected]> |
On Tue, May 4, 2010 at 12:33, Ivan Shmakov <[email protected]> wrote: > 1. The questions > > Do I understand it correctly that the most of the libwww HTTP > implementation is tied together by the means of the > LWP::Protocol::http module, while LWP::UserAgent just invokes > scheme-specific protocol handlers? That's right. > I need for the libwww HTTP client to use two scalar values as > the read and write buffers for communication instead of a, say, > IO::Socket::INET6 instance. Do I understand it correctly that > in order to do that I will have to: There are many ways to hook into the protocol machinery. I tried to set it up just by creating my own http-handler and ::Socket package that subclass Net::HTTP::Methods, and then just filled in the missing methods that I found my test program invoked. I then ended up with the attached script. I hope you find it instructive. Regards, Gisle
http-scalar.pl
(text/x-perl-script, 965 B)
#!perl -w
use strict;
my $read_buf = "HTTP/1.0 200 OK\n\nHi there\n";
my $write_buf = "";
use LWP::UserAgent;
use LWP::Protocol;
LWP::Protocol::implementor('http', 'MyProtocolHandler');
my $ua = LWP::UserAgent->new;
$ua->get("http://www.example.com")->dump;
use Data::Dump; dd $write_buf;
exit;
BEGIN {
package MyProtocolHandler;
use base qw(LWP::Protocol::http);
package MyProtocolHandler::Socket;
use base qw(Net::HTTP::Methods);
require Symbol;
sub http_connect {
return 1;
}
sub syswrite {
my($self, $buf, $len) = @_;
$write_buf .= substr($buf, 0, $len);
return $len;
}
sub sysread {
my $self = shift;
my $len = $_[1];
$len = length($read_buf) if $len > length($read_buf);
$_[0] = substr($read_buf, 0, $len, "");
return $len;
}
sub peerhost {
undef;
}
sub increment_response_count {
my $self = shift;
return ++${*$self}{'myhttp_response_count'};
}
sub ping {
1;
}
}