<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package LWP::Protocol::GHTTP;

# You can tell LWP to use this module for 'http' requests by running
# code like this before you make requests:
#
#    require LWP::Protocol::GHTTP;
#    LWP::Protocol::implementor('http', 'LWP::Protocol::GHTTP');
#

use strict;
use vars qw(@ISA);

require LWP::Protocol;
@ISA=qw(LWP::Protocol);

require HTTP::Response;
require HTTP::Status;

use HTTP::GHTTP qw(METHOD_GET METHOD_HEAD METHOD_POST);

my %METHOD =
(
 GET  =&gt; METHOD_GET,
 HEAD =&gt; METHOD_HEAD,
 POST =&gt; METHOD_POST,
);

sub request
{
    my($self, $request, $proxy, $arg, $size, $timeout) = @_;

    my $method = $request-&gt;method;
    unless (exists $METHOD{$method}) {
	return HTTP::Response-&gt;new(&amp;HTTP::Status::RC_BAD_REQUEST,
				   "Bad method '$method'");
    }

    my $r = HTTP::GHTTP-&gt;new($request-&gt;uri);

    # XXX what headers for repeated headers here?
    $request-&gt;headers-&gt;scan(sub { $r-&gt;set_header(@_)});

    $r-&gt;set_type($METHOD{$method});

    # XXX should also deal with subroutine content.
    my $cref = $request-&gt;content_ref;
    $r-&gt;set_body($$cref) if length($$cref);

    # XXX is this right
    $r-&gt;set_proxy($proxy-&gt;as_string) if $proxy;

    $r-&gt;process_request;

    my $response = HTTP::Response-&gt;new($r-&gt;get_status);

    # XXX How can get the headers out of $r??  This way is too stupid.
    my @headers;
    eval {
	# Wrapped in eval because this method is not always available
	@headers = $r-&gt;get_headers;
    };
    @headers = qw(Date Connection Server Content-type
                  Accept-Ranges Server
                  Content-Length Last-Modified ETag) if $@;
    for (@headers) {
	my $v = $r-&gt;get_header($_);
	$response-&gt;header($_ =&gt; $v) if defined $v;
    }

    return $self-&gt;collect_once($arg, $response, $r-&gt;get_body);
}

1;
</pre></body></html>