/usr/share/perl5/Bio/Root/HTTPget.pm is in libbio-perl-perl 1.6.924-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | #
# BioPerl module for fallback HTTP get operations.
#
# Module is proxy-aware
#
# Please direct questions and support issues to <bioperl-l@bioperl.org>
#
# Cared for by Chris Dagdigian <dag@sonsorol.org>
# but all of the good stuff was written by
# Lincoln Stein.
#
# You may distribute this module under the same terms as perl itself
# POD documentation - main docs before the code
=head1 NAME
Bio::Root::HTTPget - module for fallback HTTP get operations when
LWP:: is unavailable
=head1 SYNOPSIS
use Bio::Root::HTTPget;
my $web = Bio::Root::HTTPget->new();
my $response = $web->get('http://localhost');
$response = $web->get('http://localhost/images');
$response = eval { $web->get('http://fred:secret@localhost/ladies_only/')
} or warn $@;
$response = eval { $web->get('http://jeff:secret@localhost/ladies_only/')
} or warn $@;
$response = $web->get('http://localhost/images/navauthors.gif');
$response = $web->get(-url=>'http://www.google.com',
-proxy=>'http://www.modperl.com');
=head1 DESCRIPTION
This is basically an last-chance module for doing network HTTP get
requests in situations where more advanced external CPAN modules such
as LWP:: are not installed.
The particular reason this module was developed was so that the Open
Bio Database Access code can fallback to fetching the default registry
files from http://open-bio.org/registry/ without having to depend on
external dependencies like Bundle::LWP for network HTTP access.
The core of this module was written by Lincoln Stein. It can handle proxies
and HTTP-based proxy authentication.
=head1 FEEDBACK
=head2 Mailing Lists
User feedback is an integral part of the evolution of this
and other Bioperl modules. Send your comments and suggestions preferably
to one of the Bioperl mailing lists.
Your participation is much appreciated.
bioperl-l@bioperl.org - General discussion
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
=head2 Reporting Bugs
Report bugs to the Bioperl bug tracking system to help us keep track
the bugs and their resolution. Bug reports can be submitted via the
web:
https://github.com/bioperl/bioperl-live/issues
=head1 AUTHOR - Lincoln Stein
# Please direct questions and support issues to I<bioperl-l@bioperl.org>
Cared for by Chris Dagdigian <dag@sonsorol.org>
=head1 APPENDIX
The rest of the documentation details each of the object
methods. Internal methods are usually preceded with a _
=cut
# Let the code begin...
package Bio::Root::HTTPget;
use strict;
use warnings;
use IO::Socket qw(:DEFAULT :crlf);
use base qw(Bio::Root::Root);
{
# default attributes, in case used as a class/sub call
my %attributes;
=head2 get
Title : get
Usage : my $resp = get(-url => $url);
Function:
Returns : string
Args : -url => URL to HTTPGet
-proxy => proxy to use
-user => username for proxy or authentication
-pass => password for proxy or authentication
-timeout => timeout
=cut
sub get {
my $self;
if($_[0] && (ref($_[0]) or $_[0] =~ /^Bio::/)) {
$self = shift;
}
my ($url,$proxy,$timeout,$auth_user,$auth_pass) =
__PACKAGE__->_rearrange([qw(URL PROXY TIMEOUT USER PASS)],@_);
my $dest = $proxy || $url;
my ($host,$port,$path,$user,$pass)
= _http_parse_url($dest) or __PACKAGE__->throw("invalid URL $url");
$auth_user ||= $user;
$auth_pass ||= $pass;
if ($self) {
unless ($proxy) {
$proxy = $self->proxy;
}
unless ($auth_user) {
($auth_user, $auth_pass) = $self->authentication;
}
}
$path = $url if $proxy;
# set up the connection
my $socket = _http_connect($host,$port) or __PACKAGE__->throw("can't connect: $@");
# the request
print $socket "GET $path HTTP/1.0$CRLF";
print $socket "User-Agent: Bioperl fallback fetcher/1.0$CRLF";
# Support virtual hosts
print $socket "HOST: $host$CRLF";
if ($auth_user && $auth_pass) { # authentication information
my $token = _encode_base64("$auth_user:$auth_pass");
print $socket "Authorization: Basic $token$CRLF";
}
print $socket "$CRLF";
# read the response
my $response;
{
local $/ = "$CRLF$CRLF";
$response = <$socket>;
}
my ($status_line,@other_lines) = split $CRLF,$response;
my ($stat_code,$stat_msg) = $status_line =~ m!^HTTP/1\.[01] (\d+) (.+)!
or __PACKAGE__->throw("invalid response from web server: got $response");
my %headers = map {/^(\S+): (.+)/} @other_lines;
if ($stat_code == 302 || $stat_code == 301) { # redirect
my $location = $headers{Location} or
__PACKAGE__->throw("invalid redirect: no Location header");
return get(-url => $location, -proxy => $proxy, -timeout => $timeout, -user => $auth_user, -pass => $auth_pass); # recursive call
}
elsif ($stat_code == 401) { # auth required
my $auth_required = $headers{'WWW-Authenticate'};
$auth_required =~ /^Basic realm="([^\"]+)"/
or __PACKAGE__->throw("server requires unknown type of".
" authentication: $auth_required");
__PACKAGE__->throw("request failed: $status_line, realm = $1");
}
elsif ($stat_code != 200) {
__PACKAGE__->throw("request failed: $status_line");
}
$response = '';
while (1) {
my $bytes = read($socket,$response,2048,length $response);
last unless $bytes > 0;
}
$response;
}
=head2 getFH
Title : getFH
Usage :
Function:
Example :
Returns : string
Args :
=cut
sub getFH {
my $self;
if($_[0] && (ref($_[0]) or $_[0] =~ /^Bio::/)) {
$self = shift;
}
my ($url,$proxy,$timeout,$auth_user,$auth_pass) =
__PACKAGE__->_rearrange([qw(URL PROXY TIMEOUT USER PASS)],@_);
my $dest = $proxy || $url;
my ($host,$port,$path,$user,$pass)
= _http_parse_url($dest) or __PACKAGE__->throw("invalid URL $url");
$auth_user ||= $user;
$auth_pass ||= $pass;
$path = $url if $proxy;
# set up the connection
my $socket = _http_connect($host,$port) or __PACKAGE__->throw("can't connect: $@");
# the request
print $socket "GET $path HTTP/1.0$CRLF";
print $socket "User-Agent: Bioperl fallback fetcher/1.0$CRLF";
# Support virtual hosts
print $socket "HOST: $host$CRLF";
if ($auth_user && $auth_pass) { # authentication information
my $token = _encode_base64("$auth_user:$auth_pass");
print $socket "Authorization: Basic $token$CRLF";
}
print $socket "$CRLF";
# read the response
my $response;
{
local $/ = "$CRLF$CRLF";
$response = <$socket>;
}
my ($status_line,@other_lines) = split $CRLF,$response;
my ($stat_code,$stat_msg) = $status_line =~ m!^HTTP/1\.[01] (\d+) (.+)!
or __PACKAGE__->throw("invalid response from web server: got $response");
my %headers = map {/^(\S+): (.+)/} @other_lines;
if ($stat_code == 302 || $stat_code == 301) { # redirect
my $location = $headers{Location} or
__PACKAGE__->throw("invalid redirect: no Location header");
return getFH(-url => $location, -proxy => $proxy, -timeout => $timeout, -user => $auth_user, -pass => $auth_pass); # recursive call
}
elsif ($stat_code == 401) { # auth required
my $auth_required = $headers{'WWW-Authenticate'};
$auth_required =~ /^Basic realm="([^\"]+)"/
or __PACKAGE__->throw("server requires unknown type of ".
"authentication: $auth_required");
__PACKAGE__->throw("request failed: $status_line, realm = $1");
}
elsif ($stat_code != 200) {
__PACKAGE__->throw("request failed: $status_line");
}
# Now that we are reasonably sure the socket and request
# are OK we pass the socket back as a filehandle so it can
# be processed by the caller...
$socket;
}
=head2 _http_parse_url
Title :
Usage :
Function:
Example :
Returns :
Args :
=cut
sub _http_parse_url {
my $self;
if($_[0] && (ref($_[0]) or $_[0] =~ /^Bio::/)) {
$self = shift;
}
my $url = shift;
my ($user,$pass,$hostent,$path) =
$url =~ m!^http://(?:([^:]+):([^:]+)@)?([^/]+)(/?[^\#]*)! or return;
$path ||= '/';
my ($host,$port) = split(':',$hostent);
return ($host,$port||80,$path,$user,$pass);
}
=head2 _http_connect
Title :
Usage :
Function:
Example :
Returns :
Args :
=cut
sub _http_connect {
my ($host,$port,$timeout) = @_;
my $sock = IO::Socket::INET->new(Proto => 'tcp',
Type => SOCK_STREAM,
PeerHost => $host,
PeerPort => $port,
Timeout => $timeout,
);
$sock;
}
=head2 _encode_base64
Title :
Usage :
Function:
Example :
Returns :
Args :
=cut
sub _encode_base64 {
my $self;
if($_[0] && (ref($_[0]) or $_[0] =~ /^Bio::/)) {
$self = shift;
}
my $res = "";
my $eol = $_[1];
$eol = "\n" unless defined $eol;
pos($_[0]) = 0; # ensure start at the beginning
$res = join '', map( pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
$res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
# fix padding at the end
my $padding = (3 - length($_[0]) % 3) % 3;
$res =~ s/.{$padding}$/'=' x $padding/e if $padding;
# break encoded string into lines of no more than 76 characters each
if (length $eol) {
$res =~ s/(.{1,76})/$1$eol/g;
}
return $res;
}
=head2 proxy
Title : proxy
Usage : $httpproxy = $db->proxy('http') or
$db->proxy(['http','ftp'], 'http://myproxy' )
Function: Get/Set a proxy for use of proxy. Defaults to environment variable
http_proxy if present.
Returns : a string indicating the proxy
Args : $protocol : string for the protocol to set/get
$proxyurl : url of the proxy to use for the specified protocol
$username : username (if proxy requires authentication)
$password : password (if proxy requires authentication)
=cut
sub proxy {
my $self;
if($_[0] && (ref($_[0]) or $_[0] =~ /^Bio::/)) {
$self = shift;
}
my ($protocol,$proxy,$username,$password) = @_;
my $atts = ref($self) ? $self : \%attributes;
$protocol ||= 'http';
if (!$proxy) {
if (defined $ENV{http_proxy}) {
$proxy = $ENV{http_proxy};
if ($proxy =~ /\@/) {
($username, $password, $proxy) = $proxy =~ m{http://(\S+):(\S+)\@(\S+)};
$proxy = 'http://'.$proxy;
}
}
}
if (defined $proxy) {
# default to class method call
__PACKAGE__->authentication($username, $password)
if ($username && $password);
$atts->{'_proxy'}->{$protocol} = $proxy;
}
return $atts->{'_proxy'}->{$protocol};
}
=head2 clear_proxy
Title : clear_proxy
Usage : my $old_prozy = $db->clear_proxy('http')
Function: Unsets (clears) the proxy for the protocol indicated
Returns : a string indicating the old proxy value
Args : $protocol : string for the protocol to clear
=cut
sub clear_proxy {
my $self;
if($_[0] && (ref($_[0]) or $_[0] =~ /^Bio::/)) {
$self = shift;
}
my ($protocol) = @_;
my $atts = ref($self) ? $self : \%attributes;
$protocol ||= 'http';
delete $atts->{'_proxy'}->{$protocol};
}
=head2 authentication
Title : authentication
Usage : $db->authentication($user,$pass)
Function: Get/Set authentication credentials
Returns : Array of user/pass
Args : Array or user/pass
=cut
sub authentication {
my $self;
if($_[0] && (ref($_[0]) or $_[0] =~ /^Bio::/)) {
$self = shift;
}
my $atts = ref($self) ? $self : \%attributes;
if (@_) {
my ($u,$p) = @_;
my $atts = ref($self) ? $self : \%attributes;
$atts->{'_authentication'} = [ $u,$p];
}
return @{$atts->{'_authentication'} || []};
}
}
1;
|