FETCH(3)

NAME
fetchMakeURL, fetchParseURL, fetchFreeURL, fetchGetURL, fetchPutURL, fetchStatURL, fetchListURL, fetchGet, fetchPut, fetchStat, fetchList, fetchGetFile, fetchPutFile, fetchStatFile, fetchListFile, fetchGetHTTP, fetchPutHTTP, fetchStatHTTP, fetchListHTTP, fetchGetFTP, fetchPutFTP, fetchStatFTP, fetchListFTP - file transfer functions

LIBRARY
libfetch

SYNOPSIS
#include <sys/param.h>
#include <stdio.h>
#include <fetch.h>
struct url *
fetchMakeURL(char *scheme, char *host, int port, char *doc, char *user, char *pwd);
struct url *
fetchParseURL(char *URL);
void
fetchFreeURL(struct url *URL);
FILE *
fetchGetURL(char *URL, char *flags);
FILE *
fetchPutURL(char *URL, char *flags);
int
fetchStatURL(char *URL, struct url_stat *us, char *flags);
struct url_ent *
fetchListURL(char *URL, char *flags);
FILE *
fetchGet(struct url *URL, char *flags);
FILE *
fetchPut(struct url *URL, char *flags);
int
fetchStat(struct url *URL, struct url_stat *us, char *flags);
struct url_ent *
fetchList(struct url *, char *flags);
FILE *
fetchGetFile(struct url *u, char *flags);
FILE *
fetchPutFile(struct url *u, char *flags);
int
fetchStatFile(struct url *URL, struct url_stat *us, char *flags);
struct url_ent *
fetchListFile(struct url *, char *flags);
FILE *
fetchGetHTTP(struct url *u, char *flags);
FILE *
fetchPutHTTP(struct url *u, char *flags);
int
fetchStatHTTP(struct url *URL, struct url_stat *us, char *flags);
struct url_ent *
fetchListHTTP(struct url *, char *flags);
FILE *
fetchGetFTP(struct url *u, char *flags);
FILE *
fetchPutFTP(struct url *u, char *flags);
int
fetchStatFTP(struct url *URL, struct url_stat *us, char *flags);
struct url_ent *
fetchListFTP(struct url *, char *flags);

DESCRIPTION

These functions implement a high-level library for retrieving and uploading files using Uniform Resource Locators (URLs).

fetchParseURL() takes a URL in the form of a null-terminated string and splits it into its components function according to the Common Internet Scheme Syntax detailed in RFC1738. A regular expression which produces this syntax is:

        <scheme>:(//(<user>(:<pwd>)?@)?<host>(:<port>)?)?/(<document>)?

Note that some components of the URL are not necessarily relevant to all URL schemes. For instance, the file scheme only needs the <scheme> and <document> components.

fetchMakeURL() and fetchParseURL() return a pointer to a url structure, which is defined as follows in <fetch.h>:

    #define URL_SCHEMELEN 16
    #define URL_USERLEN 256
    #define URL_PWDLEN 256
    struct url {
        char	 scheme[URL_SCHEMELEN+1];
        char	 user[URL_USERLEN+1];
        char	 pwd[URL_PWDLEN+1];
        char	 host[MAXHOSTNAMELEN+1];
        int		 port;
        char	*doc;
        off_t	 offset;
        size_t	 length;
    };

The pointer returned by fetchMakeURL() or fetchParseURL() should be freed using fetchFreeURL().

fetchGetURL() and fetchPutURL() constitute the recommended interface to the fetch library. They examine the URL passed to them to determine the transfer method, and call the appropriate lower-level functions to perform the actual transfer. The flags argument is a string of characters which specify transfer options. The meaning of the individual flags is scheme-dependent, and is detailed in the appropriate section below.

fetchStatURL() attempts to obtain the requested document's metadata and fill in the structure pointed to by it's second argument. The url_stat structure is defined as follows in <fetch.h>:

    struct url_stat {
        off_t	 size;
        time_t	 atime;
        time_t	 mtime;
    };

If the size could not be obtained from the server, the size field is set to -1. If the modification time could not be obtained from the server, the mtime field is set to the epoch. If the access time could not be obtained from the server, the atime field is set to the modification time.

fetchListURL() attempts to list the contents of the directory pointed to by the URL provided. If successful, it returns a malloced array of url_ent structures. The url_ent structure is defined as follows in <fetch.h>:

    struct url_ent {
        char         name[MAXPATHLEN];
        struct url_stat stat;
    };

The list is terminated by an entry with an empty name.

The pointer returned by fetchListURL() should be freed using free().

fetchGet(), fetchPut() and fetchStat() are similar to fetchGetURL(), fetchPutURL() and fetchStatURL(), except that they expect a pre-parsed URL in the form of a pointer to a struct url rather than a string.

All of the fetchGetXXX() and fetchPutXXX() functions return a pointer to a stream which can be used to read or write data from or to the requested document, respectively. Note that although the implementation details of the individual access methods vary, it can generally be assumed that a stream returned by one of the fetchGetXXX() functions is read-only, and that a stream returned by one of the fetchPutXXX() functions is write-only.

FILE SCHEME
fetchGetFile() and fetchPutFile() provide access to documents which are files in a locally mounted file system. Only the <document> component of the URL is used.

fetchGetFile() does not accept any flags.

fetchPutFile() accepts the a (append to file) flag. If that flag is specified, the data written to the stream returned by fetchPutFile() will be appended to the previous contents of the file, instead of replacing them.

FTP SCHEME
fetchGetFTP() and fetchPutFTP() implement the FTP protocol as described in RFC959.

If the p (passive) flag is specified, a passive (rather than active) connection will be attempted.

If the h (high) flag is specified, data sockets will be allocated in the high port range (see ip(4) ).

If the d (direct) flag is specified, fetchGetFTP() and fetchPutFTP() will use a direct connection even if a proxy server is defined.

If no user name or password is given, the fetch library will attempt an anonymous login, with user name "ftp" and password "ftp".

HTTP SCHEME
The fetchGetHTTP() and fetchPutHTTP() functions implement the HTTP/1.1 protocol. With a little luck, there's even a chance that they comply with RFC2068.

If the d (direct) flag is specified, fetchGetHTTP() and fetchPutHTTP() will use a direct connection even if a proxy server is defined.

Since there seems to be no good way of implementing the HTTP PUT method in a manner consistent with the rest of the fetch library, fetchPutHTTP() is currently unimplemented.

RETURN VALUES
fetchParseURL() returns a pointer to a struct url containing the individual components of the URL. If it is unable to allocate memory, or the URL is syntactically incorrect, fetchParseURL() returns a NULL pointer.

The fetchStat() functions return 0 on success and -1 on failure.

All other functions return a stream pointer which may be used to access the requested document, or NULL if an error occurred.

The following error codes are defined in <fetch.h>:

[FETCH_ABORT]
    Operation aborted

[FETCH_AUTH]
    Authentication failed

[FETCH_DOWN]
    Service unavailable

[FETCH_EXISTS]
    File exists

[FETCH_FULL]
    File system full

[FETCH_INFO]
    Informational response

[FETCH_MEMORY]
    Insufficient memory

[FETCH_MOVED]
    File has moved

[FETCH_NETWORK]
    Network error

[FETCH_OK]
    No error

[FETCH_PROTO]
    Protocol error

[FETCH_RESOLV]
    Resolver error

[FETCH_SERVER]
    Server error

[FETCH_TEMP]
    Temporary error

[FETCH_TIMEOUT]
    Operation timed out

[FETCH_UNAVAIL]
    File is not available

[FETCH_UNKNOWN]
    Unknown error

[FETCH_URL]
    Invalid URL

The accompanying error message includes a protocol-specific error code and message, e.g. "File is not available (404 Not Found)"

ENVIRONMENT
The FTP and HTTP related functions use the HTTP_PROXY and FTP_PROXY environment variables, respectively, as the address of a proxy server to use for transferring files.

SEE ALSO
fetch(1), ftpio(3), ip(4). T. Berners-Lee, L. Masinter, M. McCahill, December 1994, Uniform Resource Locators (URL), RFC1738. R. Fielding, J. Gettys, J. Mogul, H. Frystyk, T. Berners-Lee, Januray 1997, Hypertext Transfer Protocol -- HTTP/1.1, RFC2068. J. Postel, J. K. Reynolds, October 1985, File Transfer Protocol, RFC959.

HISTORY
The fetch library first appeared in FreeBSD 3.0.

AUTHORS
The fetch library was mostly written by Dag-Erling Coïdan Smørgrav <des@FreeBSD.org> with numerous suggestions from Jordan K. Hubbard <jkh@FreeBSD.org>, Eugene Skepner <eu@qub.com> and other FreeBSD developers. It replaces the older ftpio library written by Poul-Henning Kamp <pkh@FreeBSD.org> and Jordan K. Hubbard <jkh@FreeBSD.org>.

This manual page was written by Dag-Erling Coïdan Smørgrav <des@FreeBSD.org>

BUGS
Some parts of the library are not yet implemented. The most notable examples of this are fetchPutHTTP(), fetchListHTTP(), fetchListFTP() and FTP proxy support.

There's no way to select a proxy at run-time other than setting the HTTP_PROXY or FTP_PROXY environment variables as appropriate. There is also no way to stop the FTP and HTTP functions from trying to use a proxy if these variables are set.

HTTP authentication doesn't work. I'm not sure that's a bug in my code; as far as I can determine, libfetch handles HTTP/1.1 basic authentication correctly as outlined in RFC2068, but I haven't been able to find an HTTP server that honors the Authentication: header field. Also, libfetch does not attempt to interpret and respond to authentication requests from the HTTP server.

No attempt is made to encode spaces etc. within URLs. Spaces in the document part of an URLshould be replaced with "%20" in HTTP URLs and "\ " in FTP URLs.

Error numbers are unique only within a certain context; the error codes used for FTP and HTTP overlap, as do those used for resolver and system errors. For instance, error code 202 means "Command not implemented, superfluous at this site" in an FTP context and "Accepted" in an HTTP context.

fetchStatFTP() does not check that the result of an MDTM command is a valid date.

The HTTP code needs a complete rewrite, or at least a serious cleanup.

The man page is poorly written and produces badly formatted text.

The error reporting mechanism is unsatisfactory.

Some parts of the code are not fully reentrant.

Tons of other stuff.