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
|
# -*-Perl-*-
################################################################
###
### Http.pm
###
### Copyright (C) 1997 Internet Message Group
###
### This Perl5 library conforms
### GNU GENERAL PUBLIC LICENSE Version 2.
###
###
### Author: Internet Message Group <img@mew.org>
### Created: Apr 23, 1997
### Revised: @im_revised@
###
my $PM_VERSION = "IM::Http.pm @im_version@";
package IM::Http;
require 5.003;
require Exporter;
use IM::Util;
use IM::TcpTransaction;
use integer;
use strict;
use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw(http_process http_spec);
=head1 NAME
Http - HTTP handling package
=head1 SYNOPSIS
=head1 DESCRIPTION
=cut
use vars qw(*HTTPd);
########################
# HTTP access routines #
########################
# http_open(host, port, user, pass)
# host:
# port:
# user:
# pass:
# return value:
# 0: success
# -1: failure
#
sub http_open ($$) {
my ($host, $port) = @_;
my ($resp);
my (@host_list);
if ($port ne '' && $port != 0 && $port != 80) {
@host_list = ("$host/$port");
} else {
@host_list = ($host);
}
im_notice("opening HTTP session\n");
&tcp_logging(0);
*HTTPd = &connect_server(\@host_list, 'http', 0);
unless ($HTTPd) {
im_warn("connection failed.\n");
return -1;
}
return 0;
}
sub http_close () {
im_notice("closing HTTP session.\n");
close(HTTPd);
return 0;
}
sub http_get ($$$) {
my ($path, $user, $pass) = @_;
local ($_);
my (@Message);
im_notice("getting $path.\n");
&send_data(\*HTTPd, "GET $path HTTP/1.0", '');
if ($pass ne '') {
require IM::EncDec && import IM::EncDec;
my $cred = &b_encode_string("$user:$pass");
&send_data(\*HTTPd, "Authorization: Basic $cred",
'Authorization: ********');
}
&send_data(\*HTTPd, '', '');
@Message = ();
while (<HTTPd>) {
push (@Message, $_);
}
return \@Message;
}
# http_process(spec)
sub http_process ($;$$) {
my ($spec, $http_proxy, $no_proxy) = @_;
my ($msg, $rcode, $auth);
my ($user, $host, $port, $path);
my ($target_host, $target_port);
$http_proxy = '' if ($no_proxy && $spec =~ /$no_proxy/);
if ($http_proxy) {
im_notice("using proxy: $http_proxy\n");
if ($http_proxy =~ /(.*):(.*)/) {
$target_host = $1;
$target_port = $2;
} else {
$target_host = $http_proxy;
$target_port = '';
}
}
my $pass = '';
my $retry = 3;
my $first = 1;
my $found = 0;
while (1) {
($user, $host, $port, $path) = &http_spec($spec);
if ($http_proxy ne '') {
if ($port ne '' && $port != 0 && $port != 80) {
$path = "http://$host:$port$path";
} else {
$path = "http://$host$path";
}
} else {
$target_host = $host;
$target_port = $port;
}
return (-1) if (http_open($target_host, $target_port) < 0);
$msg = http_get($path, $user, $pass);
http_close();
im_debug("HTTP response for $spec follows\n") if (&debug('http'));
my $new_spec = 0;
$rcode = 0;
$auth = '';
$pass = '';
while ($_ = shift(@$msg)) {
s/\r?\n//;
if (/^HTTP\/\S+\s+(\d+)/i) {
$rcode = $1;
}
if (/^Location:\s*(.*)/i) {
$spec = $1;
$new_spec = 1;
}
if (/^WWW-Authenticate:\s*(.*)/i) {
$auth = $1;
}
im_debug("$_\n") if (&debug('http'));
last if (/^$/);
}
next if ($rcode == 302 && $new_spec);
if ($rcode == 401 && $auth =~ /Basic/i && $retry--) {
require IM::GetPass && import IM::GetPass;
if ($first) {
$first = 0;
if (&usepwagent()) {
$pass = &loadpass('http', $auth, $path, $user);
if ($pass ne '') {
$found = 1;
next;
}
}
if (&usepwfiles()) {
$pass = &findpass('http', $auth, $path, $user);
if ($pass ne '') {
$found = 1;
next;
}
}
}
# last if ($found && $NoPwQueryOnFail);
$pass = &getpass("Password: ");
next if ($pass ne '');
}
if ($rcode == 200 && $pass ne '' && &usepwagent()) {
&savepass('http', $auth, $path, $user, $pass);
}
last;
}
return (0, $msg);
}
# HTTP (--src=http://[user@]server[:port]/path)
sub http_spec ($) {
my $spec = shift;
if ($spec eq '') {
$spec = httphome();
}
$spec =~ s/^http://i;
my $host = 'localhost';
my $user = $ENV{'USER'} || $ENV{'LOGNAME'} || im_getlogin();
my $port = 0;
my $path;
if ($spec =~ m|^//([^/]+)(/.*)?|) {
my $s = $1;
$path = $2;
if ($s =~ /(.*)\@(.*)/) {
$user = $1;
$s = $2;
}
if ($s =~ /(.*):(.*)/) {
$host = $1;
$port = $2;
} else {
$host = $s;
}
} else {
$path = $spec;
}
return ($user, $host, $port, $path);
}
1;
|