FETCH(3)
π FETCH(3) β Library Functions Manual π
okay bestie, welcome to FETCH(3) from the Library Functions Manual. this is the whole file transfer situation and it is a lot, but we're gonna get through it together. no cap.
β¨ NAME β¨
meet the cast: fetchMakeURL, fetchParseURL, fetchFreeURL, fetchXGetURL, fetchGetURL, fetchPutURL, fetchStatURL, fetchListURL, fetchXGet, fetchGet, fetchPut, fetchStat, fetchList, fetchXGetFile, fetchGetFile, fetchPutFile, fetchStatFile, fetchListFile, fetchXGetHTTP, fetchGetHTTP, fetchPutHTTP, fetchStatHTTP, fetchListHTTP, fetchReqHTTP, fetchXGetFTP, fetchGetFTP, fetchPutFTP, fetchStatFTP, fetchListFTP β these are the file transfer functions. iconic lineup, honestly.
π LIBRARY π
File Transfer Library for URLs (libfetch, -lfetch). she's serving URLs and she's linked in. π«Ά
π¦ SYNOPSIS π¦
okay, deep breath, here's the whole signature situation, exactly as it is:
#include <sys/param.h>
#include <stdio.h>
#include <fetch.h>
struct url *
fetchMakeURL(const char *scheme, const char *host, int port,
const char *doc, const char *user, const char *pwd);
struct url *
fetchParseURL(const char *URL);
void
fetchFreeURL(struct url *u);
FILE *
fetchXGetURL(const char *URL, struct url_stat *us, const char *flags);
FILE *
fetchGetURL(const char *URL, const char *flags);
FILE *
fetchPutURL(const char *URL, const char *flags);
int
fetchStatURL(const char *URL, struct url_stat *us, const char *flags);
struct url_ent *
fetchListURL(const char *URL, const char *flags);
FILE *
fetchXGet(struct url *u, struct url_stat *us, const char *flags);
FILE *
fetchGet(struct url *u, const char *flags);
FILE *
fetchPut(struct url *u, const char *flags);
int
fetchStat(struct url *u, struct url_stat *us, const char *flags);
struct url_ent *
fetchList(struct url *u, const char *flags);
FILE *
fetchXGetFile(struct url *u, struct url_stat *us, const char *flags);
FILE *
fetchGetFile(struct url *u, const char *flags);
FILE *
fetchPutFile(struct url *u, const char *flags);
int
fetchStatFile(struct url *u, struct url_stat *us, const char *flags);
struct url_ent *
fetchListFile(struct url *u, const char *flags);
FILE *
fetchXGetHTTP(struct url *u, struct url_stat *us, const char *flags);
FILE *
fetchGetHTTP(struct url *u, const char *flags);
FILE *
fetchPutHTTP(struct url *u, const char *flags);
int
fetchStatHTTP(struct url *u, struct url_stat *us, const char *flags);
struct url_ent *
fetchListHTTP(struct url *u, const char *flags);
FILE *
fetchReqHTTP(struct url *u, const char *method, const char *flags,
const char *content_type, const char *body);
FILE *
fetchXGetFTP(struct url *u, struct url_stat *us, const char *flags);
FILE *
fetchGetFTP(struct url *u, const char *flags);
FILE *
fetchPutFTP(struct url *u, const char *flags);
int
fetchStatFTP(struct url *u, struct url_stat *us, const char *flags);
struct url_ent *
fetchListFTP(struct url *u, const char *flags);
π DESCRIPTION π
so these functions are the high-level library glow-up for retrieving and uploading files using Uniform Resource Locators, aka URLs. yes bestie, the whole URL-to-file pipeline.
fetchParseURL() takes a URL as a null-terminated string and splits it into its components according to the Common Internet Scheme Syntax detailed in RFC1738. the regular expression that produces this syntax is:
<scheme>:(//(<user>(:<pwd>)?@)?<host>(:<port>)?)?/(<document>)?
if the URL does not seem to begin with a scheme name, this syntax is assumed instead:
((<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. lowkey minimal.
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;
time_t ims_time;
};
the ims_time field stores the time value for If-Modified-Since HTTP requests. very "only send me the fresh stuff" of it.
the pointer returned by fetchMakeURL() or fetchParseURL() should be freed using fetchFreeURL(). do not ghost that cleanup, bestie. π§Ή
fetchXGetURL(), fetchGetURL(), and fetchPutURL() constitute the recommended interface to the fetch library. they examine the URL passed to them to determine the transfer method, then call the appropriate lower-level functions to perform the actual transfer. fetchXGetURL() also returns the remote document's metadata in the url_stat structure pointed to by the us argument. main character behavior tbh.
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. so yes, you gotta keep reading. π
fetchStatURL() attempts to obtain the requested document's metadata and fill in the structure pointed to by its 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. it's giving fallback values, and honestly? valid.
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[PATH_MAX];
struct url_stat stat;
};
the list is terminated by an entry with an empty name. that's the little "we're done here" marker. β¨
the pointer returned by fetchListURL() should be freed using free(). again: cleanup era. π«Ά
fetchXGet(), fetchGet(), fetchPut() and fetchStat() are similar to fetchXGetURL(), 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. same vibes, different input format.
all of the fetchXGetXXX(), 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 fetchXGetXXX() or fetchGetXXX() functions is read-only, and that a stream returned by one of the fetchPutXXX() functions is write-only. so likeβ¦ don't try to make fetch happen in the wrong direction. π
πΈ FILE SCHEME πΈ
fetchXGetFile(), 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. local file realness.
fetchXGetFile() and fetchGetFile() do not accept any flags. none. zero. the flag budget is spent.
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. so a is literally the "add it, don't erase it" icon. π
π FTP SCHEME π
fetchXGetFTP(), fetchGetFTP() and fetchPutFTP() implement the FTP protocol as described in RFC959. old-school but still showing up.
if the P (not passive) flag is specified, an active (rather than passive) connection will be attempted. yes, P is the "not passive" one β the capital letter is doing important work here. π
the p flag is supported for compatibility with earlier versions where active connections were the default. it has precedence over the P flag, so if both are specified, fetchMakeURL will use a passive connection. lowercase p said "I win actually." π
if the l (low) flag is specified, data sockets will be allocated in the low (or default) port range instead of the high port range (see ip(4)). low port range behavior, as advertised.
if the d (direct) flag is specified, fetchXGetFTP(), fetchGetFTP() and fetchPutFTP() will use a direct connection even if a proxy server is defined. direct means direct, bestie.
if no user name or password is given, the fetch library will attempt an anonymous login, with user name "anonymous" and password "anonymous@<hostname>". anonymous but make it specified. πΆοΈ
π HTTP SCHEME π
the fetchXGetHTTP(), fetchGetHTTP(), fetchPutHTTP() and fetchReqHTTP() functions implement the HTTP/1.1 protocol. with a little luck, there is even a chance that they comply with RFC2616 and RFC2617. the confidence? honestly kind of iconic.
if the d (direct) flag is specified, fetchXGetHTTP(), fetchGetHTTP() and fetchPutHTTP() will use a direct connection even if a proxy server is defined. same direct-flag energy as FTP.
if the i (if-modified-since) flag is specified, and the ims_time field is set in struct url, then fetchXGetHTTP() and fetchGetHTTP() will send a conditional If-Modified-Since HTTP header to only fetch the content if it is newer than ims_time. that's the "don't send me stale content" button. β°
the function fetchReqHTTP() can be used to make requests with an arbitrary HTTP verb, including POST, DELETE, CONNECT, OPTIONS, TRACE or PATCH. this can be done by setting the argument method to the intended verb, such as 'POST', and body to the content. yes, arbitrary verb rights. π₯
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. important: it is not implemented. do not expect it to slay. π
π HTTPS SCHEME π
based on HTTP SCHEME. yes, she's HTTP's more secure cousin.
the CA bundle used for peer verification can be changed by setting the environment variables SSL_CA_CERT_FILE to point to a concatenated bundle of trusted certificates and SSL_CA_CERT_PATH to point to a directory containing hashes of trusted CAs (see verify(1)). trust configuration but make it environment-variable chic.
a certificate revocation list (CRL) can be used by setting the environment variable SSL_CRL_FILE (see crl(1)). CRL rights.
peer verification can be disabled by setting the environment variable SSL_NO_VERIFY_PEER. note that this also disables CRL checking.
β οΈ bestie, that's a real "turn off the safety rails" move. the note is right there: SSL_NO_VERIFY_PEER also disables CRL checking. be normal about it.
by default the service identity is verified according to the rules detailed in RFC6125 (also known as hostname verification). this feature can be disabled by setting the environment variable SSL_NO_VERIFY_HOSTNAME. again: disabling verification is a choice. π
client certificate based authentication is supported. the environment variable SSL_CLIENT_CERT_FILE should be set to point to a file containing key and client certificate to be used in PEM format. when a PEM-format key is in a separate file from the client certificate, the environment variable SSL_CLIENT_KEY_FILE can be set to point to the key file. in case the key uses a password, the user will be prompted on standard input. certificate auth, but you might have to type a password. classic.
by default libfetch allows TLSv1 and newer when negotiating the connecting with the remote peer. you can change this behavior by setting the SSL_NO_TLS1, SSL_NO_TLS1_1, SSL_NO_TLS1_2 and SSL_NO_TLS1_3 environment variables to disable TLS 1.0, 1.1, 1.2 and 1.3 respectively. TLS negotiation, but make it adjustable. π
π AUTHENTICATION π
Okay so auth time, bestie. Besides setting the right environment variables and putting the user name and password in the URL or the struct url, the calling program can define an authentication function. The prototype is literally this:
int myAuthMethod(struct url *u)
Your callback should fill in the user and pwd fields in the provided struct url and return 0 on success, or any other value to indicate failure.
To register the authentication callback, just set fetchAuthMethod to point at it. That callback gets used whenever a site requires authentication and the appropriate environment variables arenβt set.
π§ͺ This interface is experimental and may be subject to change.
π RETURN VALUES π
fetchParseURL() returns a pointer to a struct url holding the individual components of the URL. If it canβt 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 you can use 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, like "File is not available (404 Not Found)".
π ENVIRONMENT π
FETCH_BIND_ADDRESSβ Specifies a hostname or IP address to which sockets used for outgoing connections will be bound.FTP_LOGINβ Default FTP login if none was provided in the URL.FTP_PASSIVE_MODEβ If set tono, forces the FTP code to use active mode. If set to any other value, forces passive mode even if the application requested active mode.FTP_PASSWORDβ Default FTP password if the remote server requests one and none was provided in the URL.FTP_PROXYβ URL of the proxy to use for FTP requests. The document part is ignored. FTP and HTTP proxies are supported; if no scheme is specified, FTP is assumed. If the proxy is an FTP proxy, libfetch will senduser@hostas user name to the proxy, whereuseris the real user name, andhostis the name of the FTP server. If this variable is set to an empty string, no proxy will be used for FTP requests, even if theHTTP_PROXYvariable is set.ftp_proxyβ Same asFTP_PROXY, for compatibility.HTTP_ACCEPTβ Specifies the value of theAcceptheader for HTTP requests. If empty, noAcceptheader is sent. The default is*/*.HTTP_AUTHβ Specifies HTTP authorization parameters as a colon-separated list of items. The first and second item are the authorization scheme and realm respectively; further items are scheme-dependent. Currently, thebasicanddigestauthorization methods are supported. Both methods require two parameters: the user name and password, in that order. This variable is only used if the server requires authorization and no user name or password was specified in the URL.HTTP_PROXYβ URL of the proxy to use for HTTP requests. The document part is ignored. Only HTTP proxies are supported for HTTP requests. If no port number is specified, the default is3128. Note that this proxy will also be used for FTP documents, unless theFTP_PROXYvariable is set.http_proxyβ Same asHTTP_PROXY, for compatibility.HTTP_PROXY_AUTHβ Specifies authorization parameters for the HTTP proxy in the same format as theHTTP_AUTHvariable. This variable is used if and only if connected to an HTTP proxy, and is ignored if a user and/or a password were specified in the proxy URL.HTTP_REFERERβ Specifies the referrer URL to use for HTTP requests. If set toauto, the document URL will be used as referrer URL.HTTP_USER_AGENTβ Specifies theUser-Agentstring to use for HTTP requests. This can be useful when working with HTTP origin or proxy servers that differentiate between user agents. If defined but empty, noUser-Agentheader is sent.NETRCβ Specifies a file to use instead of~/.netrcto look up login names and passwords for FTP and HTTP sites as well as HTTP proxies. Seeftp(1)for a description of the file format.NO_PROXYβ Either a single*, which disables the use of proxies altogether, or a comma- or whitespace-separated list of hosts for which proxies should not be used.no_proxyβ Same asNO_PROXY, for compatibility.SOCKS5_PROXYβ Uses SOCKS version 5 to make connection. The format must be the IP or hostname followed by a colon for the port. IPv6 addresses must enclose the address in brackets. If no port is specified, the default is1080. This setting will supercede a connection to anHTTP_PROXY.SSL_CA_CERT_FILEβ CA certificate bundle containing trusted CA certificates. Default value: See HTTPS SCHEME above.SSL_CA_CERT_PATHβ Path containing trusted CA hashes.SSL_CLIENT_CERT_FILEβ PEM encoded client certificate/key which will be used in client certificate authentication.SSL_CLIENT_KEY_FILEβ PEM encoded client key in case key and client certificate are stored separately.SSL_CRL_FILEβ File containing certificate revocation list.SSL_NO_TLS1β Do not allow TLS version 1.0 when negotiating the connection.SSL_NO_TLS1_1β Do not allow TLS version 1.1 when negotiating the connection.SSL_NO_TLS1_2β Do not allow TLS version 1.2 when negotiating the connection.SSL_NO_TLS1_3β Do not allow TLS version 1.3 when negotiating the connection.SSL_NO_VERIFY_HOSTNAMEβ If set, do not verify that the hostname matches the subject of the certificate presented by the server.SSL_NO_VERIFY_PEERβ If set, do not verify the peer certificate against trusted CAs.
β¨ EXAMPLES β¨
To access a proxy server on proxy.example.com port 8080, set the HTTP_PROXY environment variable in a manner similar to this:
HTTP_PROXY=http://proxy.example.com:8080
If the proxy server requires authentication, there are two options available for passing the authentication data. The first method is by using the proxy URL:
HTTP_PROXY=http://<user>:<pwd>@proxy.example.com:8080
The second method is by using the HTTP_PROXY_AUTH environment variable:
HTTP_PROXY=http://proxy.example.com:8080
HTTP_PROXY_AUTH=basic:*:<user>:<pwd>
To disable the use of a proxy for an HTTP server running on the local host, define NO_PROXY as follows:
NO_PROXY=localhost,127.0.0.1
To use a SOCKS5 proxy, set the SOCKS5_PROXY environment variable to a valid host or IP followed by an optional colon and the port. IPv6 addresses must be enclosed in brackets. The following are examples of valid settings:
SOCKS5_PROXY=proxy.example.com
SOCKS5_PROXY=proxy.example.com:1080
SOCKS5_PROXY=192.0.2.0
SOCKS5_PROXY=198.51.100.0:1080
SOCKS5_PROXY=[2001:db8::1]
SOCKS5_PROXY=[2001:db8::2]:1080
Access HTTPS website without any certificate verification whatsoever:
SSL_NO_VERIFY_PEER=1
SSL_NO_VERIFY_HOSTNAME=1
Access HTTPS website using client certificate based authentication and a private CA:
SSL_CLIENT_CERT_FILE=/path/to/client.pem
SSL_CA_CERT_FILE=/path/to/myca.pem
π¦ SEE ALSO π¦
fetch(1),ip(4)- J. Postel and J. K. Reynolds, File Transfer Protocol, October 1985, RFC959.
- P. Deutsch, A. Emtage, and A. Marine., How to Use Anonymous FTP, May 1994, RFC1635.
- T. Berners-Lee, L. Masinter, and M. McCahill, Uniform Resource Locators (URL), December 1994, RFC1738.
- R. Fielding, J. Gettys, J. Mogul, H. Frystyk, L. Masinter, P. Leach, and T. Berners-Lee, Hypertext Transfer Protocol -- HTTP/1.1, January 1999, RFC2616.
- J. Franks, P. Hallam-Baker, J. Hostetler, S. Lawrence, P. Leach, A. Luotonen, and L. Stewart, HTTP Authentication: Basic and Digest Access Authentication, June 1999, RFC2617.
πΈ HISTORY πΈ
The fetch library first appeared in FreeBSD 3.0.
π AUTHORS π
The fetch library was mostly written by Dag-Erling SmΓΈrgrav <des@FreeBSD.org> with numerous suggestions and contributions from Jordan K. Hubbard <jkh@FreeBSD.org>, Eugene Skepner <eu@qub.com>, Hajimu Umemoto <ume@FreeBSD.org>, Henry Whincup <henry@techiebod.com>, Jukka A. Ukkonen <jau@iki.fi>, Jean-FranΓ§ois Dockes <jf@dockes.org>, Michael Gmelin <freebsd@grem.de> and others. It replaces the older ftpio library written by Poul-Henning Kamp <phk@FreeBSD.org> and Jordan K. Hubbard <jkh@FreeBSD.org>.
This manual page was written by Dag-Erling SmΓΈrgrav <des@FreeBSD.org> and Michael Gmelin <freebsd@grem.de>. π
π BUGS π
Okay bestie, gather round, because this is the "nobody's perfect" era of the page π and honestly? The library is serving potential, not polish. Let's get into it.
- Some parts of the library are not yet implemented π« The most notable examples of this are
fetchPutHTTP(),fetchListHTTP(),fetchListFTP()and FTP proxy support. So like⦠those exist in spirit. Not in code. Vibes only ⨠- There is no way to select a proxy at run-time other than setting the
HTTP_PROXYorFTP_PROXYenvironment variables as appropriate. That's the whole menu, babes π libfetchdoes not understand or obey305 (Use Proxy)replies. It simply does not acknowledge the redirect's existence. Iconic behaviour, honestly π- 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
202means"Command not implemented, superfluous at this site"in an FTP context and"Accepted"in an HTTP context. Same number, two completely different personalities. Main character energy, ambiguous backstory π fetchStatFTP()does not check that the result of anMDTMcommand is a valid date ποΈ It's giving "trust falls but make it timestamps."- In case password protected keys are used for client certificate based authentication, the user is prompted for the password on each and every fetch operation π Every. Single. One. Not a one-time thing. Not a "remember me" thing. Just vibes and typing.
- The man page is incomplete, poorly written and produces badly formatted text π (Yes, this page is roasting itself. We respect the self-awareness, fr fr π)
- The error reporting mechanism is unsatisfactory πΈ So vague. So unhelpful. Not slay.
- Some parts of the code are not fully reentrant π¦ Just a little bit of "please don't do that from two places at once," iykyk.
And that's the tea, bestie βπ«Ά You've officially read the whole thing β every scheme, every variable, every single caveat, no cap π This little page is courtesy of Debian, dated January 22, 2026, so go forth and fetch responsibly, you gorgeous nerd πβ¨
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 1998-2013 Dag-Erling SmΓΈrgrav
- Copyright (c) 2013-2016 Michael Gmelin <freebsd@grem.de>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms