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

use strict;
use vars qw(@ISA);

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

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

our $CPAN;

unless ($CPAN) {
    # Try to find local CPAN mirror via $CPAN::Config
    eval {
	require CPAN::Config;
	if($CPAN::Config) {
	    my $urls = $CPAN::Config-&gt;{urllist};
	    if (ref($urls) eq "ARRAY") {
		my $file;
		for (@$urls) {
		    if (/^file:/) {
			$file = $_;
			last;
		    }
		}

		if ($file) {
		    $CPAN = $file;
		}
		else {
		    $CPAN = $urls-&gt;[0];
		}
	    }
	}
    };

    $CPAN ||= "http://cpan.org/";  # last resort
}

# ensure that we don't chop of last part
$CPAN .= "/" unless $CPAN =~ m,/$,;


sub request {
    my($self, $request, $proxy, $arg, $size) = @_;
    # check proxy
    if (defined $proxy)
    {
	return HTTP::Response-&gt;new(&amp;HTTP::Status::RC_BAD_REQUEST,
				   'You can not proxy with cpan');
    }

    # check method
    my $method = $request-&gt;method;
    unless ($method eq 'GET' || $method eq 'HEAD') {
	return HTTP::Response-&gt;new(&amp;HTTP::Status::RC_BAD_REQUEST,
				   'Library does not allow method ' .
				   "$method for 'cpan:' URLs");
    }

    my $path = $request-&gt;uri-&gt;path;
    $path =~ s,^/,,;

    my $response = HTTP::Response-&gt;new(&amp;HTTP::Status::RC_FOUND);
    $response-&gt;header("Location" =&gt; URI-&gt;new_abs($path, $CPAN));
    $response;
}

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