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

sub GetURL {
    # Returns headers and document
    local($url) = @_;
    local($mysite, $mypath, $myport);
    local($doc, $handle, $headers, $document);

    ($mysite, $mypath, $myport) = &ParseURL($url);
    print("Getting $mypath from $mysite ($myport)\n") if $main'debug;
    $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);

    $doc=~/\n\n/;
    $headers = $`;
    $document = $';

    ($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:');
    ($site, $port)=split(':', $siteport);
    $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;
