--- /home/bryan/Crypt-SSLeay-0.18/lib/Net/SSL.pm	Sat Nov 25 17:22:32 2000
+++ /home/bryan/SSL.pm	Wed Dec  6 13:12:30 2000
@@ -3,6 +3,9 @@
 use strict;
 use vars qw(@ISA $VERSION);
 
+use MIME::Base64;
+use Socket;
+
 require IO::Socket;
 @ISA=qw(IO::Socket::INET);
 my %REAL; # private to this package only
@@ -33,14 +36,75 @@
     $self->SUPER::configure($arg);
 }
 
+# This is really inefficient, but we only use it for reading the proxy response
+# so that doesn't really matter.
+
+sub _read_sock_line()
+{
+    my $sock = shift;
+    my $val="";
+    my $buf;
+    do {
+	$sock->SUPER::recv($buf, 1);
+	$val = $val . $buf;
+    } until ($buf eq "\n");
+
+    return $val;
+}
+
 sub connect
 {
     my $self = shift;
+    my $proxy = $self->proxy();
+
+    if ($proxy) {
+	my ($host, $port) = split(':',$proxy);
+	my $conn_ok = 0;
+	my $need_auth = 0;
+        my $auth_basic = 0;
+	my $realm = "";
+	my $length = 0;
+	my $line = "<noline>";
+
+	my $iaddr = gethostbyname($host);
+
+	return unless $self->SUPER::connect($port, $iaddr);
+
+	($port, $iaddr) = @_;
+	$host = gethostbyaddr($iaddr, AF_INET);
+
+	my $connect_string;
+	if ($ENV{"BASIC_USERNAME"} && $ENV{"BASIC_PASSWORD"}) {
+        	my $user = $ENV{"HTTPS_BASIC_USERNAME"};
+        	my $pass = $ENV{"HTTPS_BASIC_PASSWORD"};
+
+        	my $credentials = encode_base64("$user:$pass", "");
+        	$connect_string = "CONNECT $host:$port HTTP/1.0\r\n\Proxy-authorization: Basic $credentials\r\n\r\n";
+	}else{
+		$connect_string = "CONNECT $host:$port HTTP/1.0\r\n\r\n";
+	}
 
-    *$self->{io_socket_peername}=@_ == 1 ? $_[0] : IO::Socket::sockaddr_in(@_);    
-    if(!$self->SUPER::connect(@_)) {
-	# better to die than return here
-	die "Connect failed: $!";
+	$self->SUPER::send($connect_string);
+	do {
+            # not sure why getline doesn't work...
+	    $line = $self->_read_sock_line();
+	    chomp $line;
+
+            # search for HTTP/n.n 200 - this means we're good to go
+	    if ($line =~ /HTTP\/\d+\.\d+\s+200\s+/i) {
+		$conn_ok = 1;
+	    }
+
+	} until ($line =~ /^\s*$/);
+
+	return unless $conn_ok;
+
+    }else{
+        *$self->{io_socket_peername}=@_ == 1 ? $_[0] : IO::Socket::sockaddr_in(@_);    
+        if(!$self->SUPER::connect(@_)) {
+	    # better to die than return here
+	    die "Connect failed: $!";
+        }
     }
 
     my $ssl = Crypt::SSLeay::Conn->new(*$self->{'ssl_ctx'}, $self);
@@ -54,7 +118,6 @@
 	    $REAL{$self} = $REAL{$new_ssl} || $new_ssl;
 	    return $REAL{$self};
 	} elsif(*$self->{ssl_version} == 3) {
-# +           $self->die_with_error("SSL negotiation failed");
 	    my $arg = *$self->{ssl_arg};
 	    $arg->{SSL_Version} = 2;
 	    my $new_ssl = Net::SSL->new(%$arg);
@@ -68,6 +131,29 @@
 
     *$self->{'ssl_ssl'} = $ssl;
     $self;
+}
+
+# code copied from LWP::UserAgent
+sub proxy()
+{
+    my ($self, $url) = @_;
+
+    my $proxy_server = $ENV{"HTTPS_PROXY"};
+    my $no_proxy = $ENV{"NO_PROXY"};
+    my @no_proxy = split(/\s*,\s*/, $no_proxy);
+    $url = $HTTP::URI_CLASS->new($url) unless ref $url;
+
+
+    if (@no_proxy) {
+	if (my $host = eval { $url->host }) {
+	    for my $domain (@no_proxy) {
+		if ($host =~ /\Q$domain\E$/) {
+		    return;
+		}
+	    }
+	}
+    }
+    return $proxy_server;
 }
 
 sub accept
