#! /bin/perl
#
# url.pl        --- recognize, parse and retrieve URLs
#
# NB: If this package interests you, you should probably
# have a look at Roy Fielding's libwww-perl packages:
# http://www.ics.uci.edu/WebSoft/libwww-perl/
#
# This package and friends can be found at:
# http://cui_www.unige.ch/ftp/PUBLIC/oscar/scripts/README.html
# or ftp: cui.unige.ch:/PUBLIC/oscar/scripts/
#
# This package contains:
#
# url'get:      parse an URL and perform an http get
# url'do_ftp:   perform an ftp request and return the result
#
# Oscar Nierstrasz 26/8/93 oscar@cui.unige.ch
#
# 25/3/94 -- moved subroutines into packages `http' and `html'
# 5/4/94 -- commented out ftp stuff (problems at various sites)
#
# For a more complete treatment of all kinds of URLs,
# see `Jack Lund's urlget in:
# http://cui_www.unige.ch/ftp/PUBLIC/oscar/scripts/README.html
# Or: ftp://cui.unige.ch:/PUBLIC/oscar/scripts/

package url;

# Where to find libraries:
unshift(@INC, "/user/u1/oscar/Cmd/PerlLib");

# Gene Spafford's ftp package (and using the chat package).
# Added ftp'grab -- a variant of ftp'get that returns its result
# rather than writing to a local file.
# Get this from a perl archive or from: cui.unige.ch:/PUBLIC/oscar/scripts/
# require "ftplib.pl";
require "http.pl";

# Should be replaced ...
# require "gopher.pl";

# parse an URL, issue the request and return the result
sub get {
        local($url,$version) = @_;
        ($type,$host,$port,$path,$request) =
                &html'parse($type,$host,$port,$path,$url);
        if ($host) {
                if ($type eq "http") { &http'get($host,$port,$request,$version); }
                # elsif ($type eq "gopher") { &gopher'get($host,$port,$request); }
                # elsif ($type eq "ftp") { &do_ftp($host,$request); }
                else { print STDERR "url'get: $type requests unimplemented\n"; }
        }
        else {
                undef;
        }
}

# default ports
sub defport {
        local($type) = @_;
        if ($type eq "http") { 80; }
        elsif ($type eq "gopher") { 70; }
        else { undef; }
}

# ftp'grab is a version of ftp'get that returns the page
# retrieved rather than writing it to a local file.
# Perhaps not so nice for big files, but what the heck.
# sub do_ftp {
#       local($host,$file) = @_;
#       &ftp'open($host, "ftp", "$user@$thishost") || &fail;
#       &ftp'type("i") || &fail;
#       $page = &ftp'grab($file) || &fail;
#       &ftp'close;
#       $page;
# }
# 
# sub fail {
#       $save = &ftp'error;
#       &ftp'close;
#       die $save;
# }

1;

