Navigation Menu

Skip to content

Commit

Permalink
Item14023: Remove getExternalResource fallback code
Browse files Browse the repository at this point in the history
LWP will now be a requirement for Foswiki.
  • Loading branch information
gac410 committed Feb 19, 2017
1 parent 615ca59 commit 4d72e01
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 327 deletions.
26 changes: 1 addition & 25 deletions UnitTestContrib/test/unit/NetTests.pm
Expand Up @@ -20,31 +20,7 @@ sub set_up {
$this->{net} = new Foswiki::Net();
}

sub LWP {
$expectedHeader = qr#text/html; charset=(utf-?8|iso-?8859-?1)#;

# Force re-eval
undef $Foswiki::Net::LWPAvailable;
}

sub Sockets {
$expectedHeader = qr#text/html#;
$Foswiki::Net::LWPAvailable = 0;
}

sub HTTPResponse {
$Foswiki::Net::noHTTPResponse = 0;
}

sub noHTTPResponse {
$Foswiki::Net::noHTTPResponse = 1;
}

sub fixture_groups {
return ( [ 'LWP', 'Sockets' ], [ 'HTTPResponse', 'noHTTPResponse' ] );
}

sub verify_getExternalResource {
sub test_getExternalResource {
my $this = shift;

# need a known, simple, robust URL to get
Expand Down
4 changes: 2 additions & 2 deletions core/lib/Foswiki/Contrib/core/DEPENDENCIES
Expand Up @@ -28,8 +28,8 @@ IO::Socket::SSL,>=1.80,cpan,Optional, for outbound SSL/TLS connections, eg. e-ma
Locale::Language,>=0,cpan,Optional, required if {UserInterfaceInternationalisation} is enabled in configuration.
Locale::Maketext::Lexicon,>=0,cpan,Optional, required if {UserInterfaceInternationalisation} is enabled in configuration.
Locale::Msgfmt,>=0,cpan,Optional, used to compress the language files in locale directory if enabled.
LWP,>=0,cpan,Optional, needed by the Configure Extensions installer,for external URL based INCLUDEs and URL item verification.
LWP::Protocol::https,>=0,cpan,Optional, needed by the Configure Extensions installer,for external URL based INCLUDEs and URL item verification.
LWP,>=0,cpan,Required, needed by the Configure Extensions installer,for external URL based INCLUDEs and URL item verification.
LWP::Protocol::https,>=0,cpan,Required, needed by the Configure Extensions installer,for external URL based INCLUDEs and URL item verification.
Mozilla::CA,>=20110904,cpan,Optional, SSL host verification for e-mail and other SSL/TLS connections.
Socket,>=2.001,cpan,Required, for base Foswiki.
URI,>=0,cpan,Required, used for Foswiki::Net URL parsing
Expand Down
1 change: 0 additions & 1 deletion core/lib/Foswiki/Contrib/core/MANIFEST
Expand Up @@ -598,7 +598,6 @@ lib/Foswiki/Merge.pm 0444
lib/Foswiki/Meta.pm 0444
lib/Foswiki/MetaCache.pm 0444
lib/Foswiki/Net.pm 0444
lib/Foswiki/Net/HTTPResponse.pm 0444
lib/Foswiki/Net/UserCredAgent.pm 0444
lib/Foswiki/OopsException.pm 0444
lib/Foswiki/PageCache.pm 0444
Expand Down
5 changes: 1 addition & 4 deletions core/lib/Foswiki/Func.pm
Expand Up @@ -303,10 +303,7 @@ the following subset of methods is available:
| =is_error()= |
| =is_redirect()= |
Note that if LWP is *not* available, this function:
1 can only really be trusted for HTTP/1.0 urls. If HTTP/1.1 or another
protocol is required, you are *strongly* recommended to =require LWP=.
1 Will not parse multipart content
Note that LWP is required for this function.
In the event of the server returning an error, then =is_error()= will return
true, =code()= will return a valid HTTP status code
Expand Down
177 changes: 2 additions & 175 deletions core/lib/Foswiki/Net.pm
Expand Up @@ -28,10 +28,6 @@ BEGIN {
}
}

our $LWPAvailable;
our $noHTTPResponse; # if set, forces local impl of HTTP::Response
our $SSLAvailable; # Set to defined false to prevent using SSL

# note that the session is *optional*
sub new {
my ( $class, $session ) = @_;
Expand Down Expand Up @@ -89,12 +85,6 @@ the following subset of methods is available:
| =is_error()= |
| =is_redirect()= |
Note that if LWP is *not* available, this function:
1 can only really be trusted for HTTP/1.0 urls. If HTTP/1.1 or another
protocol is required, you are *strongly* recommended to =require LWP=.
1 Will not parse multipart content
1 Will not process redirects (configure relies on this)
In the event of the server returning an error, then =is_error()= will return
true, =code()= will return a valid HTTP status code
as specified in RFC 2616 and RFC 2518, and =message()= will return the
Expand All @@ -121,176 +111,13 @@ sub getExternalResource {
my ( $this, $url, %options ) = @_;

require URI::URL;
require LWP;

my $uri = URI::URL->new($url);
my $proxyHost = $this->{PROXYHOST} || $Foswiki::cfg{PROXY}{HOST};
my $puri = $proxyHost ? URI::URL->new($proxyHost) : undef;

# Don't remove $LWPAvailable; it is required to disable LWP when unit
# testing
unless ( defined $LWPAvailable ) {
eval 'require LWP';
die $@ if $@;
$LWPAvailable = ($@) ? 0 : 1;
}
if ($LWPAvailable) {
return _GETUsingLWP( $this, $uri, $puri, %options );
}

# Fallback mechanism
if ( $uri->scheme() ne 'http' ) {
if ( $uri->scheme() eq 'https' && !defined $SSLAvailable ) {
eval 'require IO::Socket::SSL';
$SSLAvailable = $@ ? 0 : 1;
}
unless ( $uri->scheme() eq 'https' && $SSLAvailable ) {
require Foswiki::Net::HTTPResponse;
return new Foswiki::Net::HTTPResponse(
"LWP not available for handling protocol: $url");
}
}

my $method = $options{method} || 'GET';
my $response;

try {
my $sclass;
eval {
require IO::Socket::IP;
$sclass = 'IO::Socket::IP';
};
if ($@) {
require IO::Socket::INET;
$sclass = 'IO::Socket::INET';
}
my @ssloptions;
if ( $uri->scheme() eq 'https' ) {
$sclass = 'IO::Socket::SSL';
@ssloptions = (
SSL_hostname => $uri->host(),
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
);
}

$url = '/' unless ($url);

my $req = "$method $url HTTP/1.0\r\nHost: " . $uri->host();
if ( $uri->scheme() eq 'http' && $uri->port() == 80
|| $uri->scheme() eq 'https' && $uri->port() == 443 )
{
$req .= "\r\n";
}
else {
$req .= ":" . $uri->port() . "\r\n";
}

if ($puri) {
if ( !defined $puri->scheme()
|| $puri->scheme() eq 'https' && !$SSLAvailable )
{
require Foswiki::Net::HTTPResponse;
return new Foswiki::Net::HTTPResponse(
"Proxy settings are invalid, check configure ({PROXY}{HOST}"
);
}
elsif ( $puri->scheme() eq 'https' ) {
$puri->port() = 443 if ( !$puri->port() );
if ( !defined $SSLAvailable ) {
eval 'require IO::Socket::SSL';
$SSLAvailable = $@ ? 0 : 1;
}
$sclass = 'IO::Socket::SSL';
}
elsif ( !$puri->port() ) {
$puri->port(8080);
}
$req =
"$method $uri->scheme()://"
. $uri->host() . ":"
. $uri->port()
. "$url HTTP/1.0\r\n";
$uri->scheme( $puri->scheme() );
$uri->host( $puri->host() );
$uri->port( $puri->port() );
if ( $puri->can("userinfo") && defined $puri->userinfo() ) {
require MIME::Base64;
my $base64 =
MIME::Base64::encode_base64( $puri->userinfo(), '' );
$req .= "Proxy-Authorization: Basic $base64\r\n";
}
}

if ( $uri->can("userinfo") && defined $uri->userinfo() ) {
require MIME::Base64;
my $base64 = MIME::Base64::encode_base64( $uri->userinfo(), '' );
$req .= "Authorization: Basic $base64\r\n";
}

$req .= 'User-Agent: Foswiki::Net/' . $Foswiki::VERSION . "\r\n";
if ( $options{headers} ) {
while ( my ( $name, $value ) = each %{ $options{headers} } ) {
$name =~ s/_/-/g;
$req .= "$name: $value\r\n";
}
}

if ( defined $options{content} ) {

# Force body encoding to octets
$options{content} = Foswiki::encode_utf8( $options{content} );
$req .= 'Content-length: ' . length( $options{content} ) . "\r\n";
}

$req .= "\r\n";
$req .= $options{content} if defined $options{content};

my $sock = $sclass->new(
PeerAddr => $uri->host(),
PeerPort => $uri->port(),
Proto => 'tcp',
Timeout => 120,
@ssloptions,
);
unless ($sock) {
die "Unable to connect to "
. $uri->host() . ": $!"
. ( @ssloptions ? ' - ' . IO::Socket::SSL::errstr() : '' ) . "\n";
}
$sock->autoflush(1);

local $/ = undef;
print $sock $req;
my $result;
$result = <$sock>;
$result = '' unless ( defined $result );
unless ( close($sock) ) {
die "close failed: $!";
}

# No LWP, but may have HTTP::Response which would make life easier
# (it has a much more thorough parser)
eval 'require HTTP::Response' unless ($noHTTPResponse);
if ( $@ || $noHTTPResponse ) {

# Nope, no HTTP::Response, have to do things the hard way :-(
require Foswiki::Net::HTTPResponse;
$response = Foswiki::Net::HTTPResponse->parse($result);
}
else {
$response = HTTP::Response->parse($result);
}
}
catch Error with {
require Foswiki::Net::HTTPResponse;
$response = new Foswiki::Net::HTTPResponse(shift);
};
return $response;
}

sub _GETUsingLWP {
my ( $this, $uri, $puri, %options ) = @_;

my $request;

require HTTP::Request;
my $method = $options{method} || 'GET';
$request = HTTP::Request->new( $method => $uri->as_string() );
Expand Down
120 changes: 0 additions & 120 deletions core/lib/Foswiki/Net/HTTPResponse.pm

This file was deleted.

0 comments on commit 4d72e01

Please sign in to comment.