1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
# $Id: GHTTP.pm,v 1.11 2002/03/25 09:25:53 matt Exp $
package HTTP::GHTTP;
use strict;
use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);
$VERSION = '1.07';
require Exporter;
require DynaLoader;
@ISA = qw(Exporter DynaLoader);
bootstrap HTTP::GHTTP $VERSION;
@EXPORT_OK = qw(
get
METHOD_GET
METHOD_POST
METHOD_OPTIONS
METHOD_HEAD
METHOD_PUT
METHOD_DELETE
METHOD_TRACE
METHOD_CONNECT
METHOD_PROPFIND
METHOD_PROPPATCH
METHOD_MKCOL
METHOD_COPY
METHOD_MOVE
METHOD_LOCK
METHOD_UNLOCK
);
%EXPORT_TAGS = (
methods => [qw(
METHOD_GET
METHOD_POST
METHOD_OPTIONS
METHOD_HEAD
METHOD_PUT
METHOD_DELETE
METHOD_TRACE
METHOD_CONNECT
METHOD_PROPFIND
METHOD_PROPPATCH
METHOD_MKCOL
METHOD_COPY
METHOD_MOVE
METHOD_LOCK
METHOD_UNLOCK
)],
);
sub new {
my $class = shift;
my $r = $class->_new();
bless $r, $class;
if (@_) {
my $uri = shift;
die "Blank uri not supported" unless length($uri);
$r->set_uri($uri);
while(@_) {
my ($header, $value) = splice(@_, 0, 2);
$r->set_header($header, $value);
}
}
return $r;
}
sub get {
die "get() requires a URI as a parameter" unless @_;
my $r = __PACKAGE__->new(@_);
$r->process_request;
return $r->get_body();
}
sub get_socket {
my $self = shift;
require IO::Handle;
my $sockno = $self->_get_socket();
my $sock = IO::Handle->new_from_fd($sockno, "r")
|| die "Cannot open socket: $!";
return $sock;
}
1;
__END__
=head1 NAME
HTTP::GHTTP - Perl interface to the gnome ghttp library
=head1 SYNOPSIS
use HTTP::GHTTP;
my $r = HTTP::GHTTP->new();
$r->set_uri("http://axkit.org/");
$r->process_request;
print $r->get_body;
=head1 DESCRIPTION
This is a fairly low level interface to the Gnome project's libghttp,
which allows you to process HTTP requests to HTTP servers. There also
exists a slightly higher level interface - a simple get() function
which takes a URI as a parameter. This is not exported by default, you
have to ask for it explicitly.
=head1 API
=head2 HTTP::GHTTP->new([$uri, [%headers]])
Constructor function - creates a new GHTTP object. If supplied a URI
it will automatically call set_uri for you. If you also supply a list
of key/value pairs it will set those as headers:
my $r = HTTP::GHTTP->new(
"http://axkit.com/",
Connection => "close");
=head2 $r->set_uri($uri)
This sets the URI for the request
=head2 $r->set_header($header, $value)
This sets an outgoing HTTP request header
=head2 $r->set_type($type)
This sets the request type. The request types themselves are constants
that are not exported by default. To export them, specify the :methods
option in the import list:
use HTTP::GHTTP qw/:methods/;
my $r = HTTP::GHTTP->new();
$r->set_uri('http://axkit.com/');
$r->set_type(METHOD_HEAD);
...
The available methods are:
METHOD_GET
METHOD_POST
METHOD_OPTIONS
METHOD_HEAD
METHOD_PUT
METHOD_DELETE
METHOD_TRACE
METHOD_CONNECT
METHOD_PROPFIND
METHOD_PROPPATCH
METHOD_MKCOL
METHOD_COPY
METHOD_MOVE
METHOD_LOCK
METHOD_UNLOCK
Some of these are for DAV.
=head2 $r->set_body($body)
This sets the body of a request, useful in POST and some of the DAV
request types.
=head2 $r->process_request()
This sends the actual request to the server
=head2 $r->get_status()
This returns 2 values, a status code (numeric) and a status reason
phrase. A simple example of the return values would be (200, "OK").
=head2 $r->get_header($header)
This gets the value of an incoming HTTP response header
=head2 $r->get_headers()
Returns a list of all the response header names in the order they
came back. This method is only available in libghttp 1.08 and later -
perl Makefile.PL should have reported whether it found it or not.
my @headers = $r->get_headers;
print join("\n",
map { "$_: " . $r->get_header($_) } @headers), "\n\n";
=head2 $r->get_body()
This gets the body of the response
=head2 $r->get_error()
If the response failed for some reason, this returns a textual error
=head2 $r->set_authinfo($user, $password)
This sets an outgoing username and password for simple HTTP
authentication
=head2 $r->set_proxy($proxy)
This sets your proxy server, use the form "http://proxy:port"
=head2 $r->set_proxy_authinfo($user, $password)
If you have set a proxy and your proxy requires a username and password
you can set it with this.
=head2 $r->prepare()
This is a low level interface useful only when doing async downloads.
See L<ASYNC OPERATION>.
=head2 $r->process()
This is a low level interface useful only when doing async downloads.
See L<ASYNC OPERATION>.
process returns undef for error, 1 for "in progress", and zero for
"complete".
=head2 $r->get_socket()
Returns an IO::Handle object that is the currently in progress socket.
Useful only when doing async downloads. There appears to be some corruption
when using the socket to retrieve file contents on more recent libghttp's.
=head2 $r->current_status()
This is only useful in async mode. It returns 3 values: The current
processing stage (0 = none, 1 = request, 2 = response headers,
3 = response), the number of bytes read, and the number of bytes total.
=head2 $r->set_async()
This turns async mode on. There is no corresponding unset function.
=head2 $r->set_chunksize($bytes)
Sets the download (and upload) chunk size in bytes for use in async
mode. This may be a useful value to set for slow modems, or perhaps
for a download progress bar, or just to allow periodic writes.
=head2 get($uri, [%headers])
This does everything automatically for you, retrieving the body at
the remote URI. Optionally pass in headers.
=head1 ASYNC OPERATION
Its possible to use an asynchronous mode of operation with ghttp. Here's
a brief example of how:
my $r = HTTP::GHTTP->new("http://axkit.org/");
$r->set_async;
$r->set_chunksize(1);
$r->prepare;
my $status;
while ($status = $r->process) {
# do something
# you can do $r->get_body in here if you want to
# but it always returns the entire body.
}
die "An error occured" unless defined $status;
print $r->get_body;
Doing timeouts is an exercise for the reader (hint: lookup select() in
perlfunc).
Note also that $sock above is an IO::Handle, not an IO::Socket, although
you can probably get away with re-blessing it. Also note that by calling
$r->get_socket() you load IO::Handle, which probably brings a lot of
code with it, thereby obliterating a lot of the use for libghttp. So
use at your own risk :-)
=head1 AUTHOR
Matt Sergeant, matt@sergeant.org
=head1 LICENSE
This is free software, you may use it and distribute it under the
same terms as Perl itself. Please be aware though that libghttp is
licensed under the terms of the LGPL, a copy of which can be found
in the libghttp distribution.
=head1 BUGS
Probably many - this is my first adventure into XS.
libghttp doesn't support SSL. When libghttp does support SSL, so will
HTTP::GHTTP. The author of libghttp, Chris Blizzard <blizzard@redhat.com>
is looking for patches to support SSL, so get coding!
=head1 BENCHMARKS
Benchmarking this sort of thing is often difficult, and I don't want
to offend anyone. But as well as being lightweight (HTTP::GHTTP is
about 4 times less code than either LWP::UserAgent, or HTTP::Lite), it
is also in my tests significantly faster. Here are my benchmark
results requesting http://localhost/ (the Apache "Successful Install"
page):
Benchmark: timing 1000 iterations of ghttp, lite, lwp...
ghttp: 8 wallclock secs ( 0.96 usr + 1.16 sys = 2.12 CPU)
lite: 21 wallclock secs ( 3.00 usr + 3.44 sys = 6.44 CPU)
lwp: 18 wallclock secs ( 9.76 usr + 1.59 sys = 11.35 CPU)
=cut
|