# W4libV1.0 for W4v3.0, June 1994
# Copyright Matthew Gray <mkgray@mit.edu> 1994

require '/afs/athena/user/m/k/mkgray/perl/chat2.pl';;
package url;

sub main'get_url {
    # Returns headers and document
    local($url) = @_;
    print("######Get $url\n") if $main'debug;
    local($mysite, $mypath, $myport);
    local($doc, $handle, $headers, $document);

    ($mysite, $mypath, $myport) = &ParseURL($url);
    $doc = ''; $headers = ''; $document = '';
    $handle =&chat'open_port($mysite, $myport) || do {print("Failed following $mysite:$myport\n");return;};
    &chat'print("GET $mypath HTTP/1.0\nUser-Agent: WWWWanderer v3.0 by Matthew Gray <mkgray@mit.edu>\n\n");
    $doc=&listen(30);
    &chat'close($handle);

    if($doc=~/\n\r?\n\r?/){
	$headers = $`;
	$document = $';
    }
    else{
	$doc =~ s/ /\./g;
	print("^^^^^^^^^^^^^WTF?\n$doc\n^^^^^^^^^^^^^^^^^^^^\n");
    }
    print("################Got $mysite:$myport, $mypath\n$headers\n###########################\n$document\n###########################") if ($main'debug && $headers !~ /HTTP/);

    ($headers, $document);
}

sub ConstructURL {
    local($current, $rellink) = @_;
    local($url);

    if($rellink =~ m,://,){
	$url = $rellink;
    }
    elsif($rellink =~ m,^/,){
	$current =~ /http:\/\/[^\/]+/;
	$url = $&.$rellink;
    }
    else{
	(@parts) = split(/\//, $current);
	$np = ($current=~/\/$/) ? $#parts : $#parts-1;
	$url = join('/', (@parts[0..$np], $rellink));
    }
    $url;
}

sub ParseURL {
    # Returns site, path, port
    local($url) = @_;
    
    ($proto, $garbage, $siteport, @path) = split('/', $url);
    next if ($proto ne 'http:');
    $port = 0;
    ($site, $port)=split(':', $siteport);
    if(!$port){
	$site = $siteport; $port = 80;
    }
    $pn = 0;
    for (@path){
        if($_ eq '..'){
            splice(@path, $pn-1, 3);
            $pn -=2;
        }
        if($_ eq '.'){
            splice(@path, $pn, 1);
            $pn -=1;
        }
        $pn++;
    }
    $path = '/'.join('/', @path);
    return($site, $path, $port);
}


#
# Chat Utility Routine
#
sub listen {
    local($secs) = @_;
    local($return,$tmp) = "";
    while (length($tmp = &chat'expect($secs, '(.|\n)+', '$&'))) {
        print $tmp if $trace;
        $return .= $tmp;
        (return $return) if (length($return) > 100000);
    }
    $return;
}


1;
