xref: /dragonfly/lib/libfetch/fetch.3 (revision 984263bc)
1.\" Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.\" $FreeBSD: src/lib/libfetch/fetch.3,v 1.11.2.29 2003/06/13 14:36:58 trhodes Exp $
26.\"
27.Dd July 1, 1998
28.Dt FETCH 3
29.Os
30.Sh NAME
31.Nm fetchMakeURL ,
32.Nm fetchParseURL ,
33.Nm fetchFreeURL ,
34.Nm fetchXGetURL ,
35.Nm fetchGetURL ,
36.Nm fetchPutURL ,
37.Nm fetchStatURL ,
38.Nm fetchListURL ,
39.Nm fetchXGet ,
40.Nm fetchGet ,
41.Nm fetchPut ,
42.Nm fetchStat ,
43.Nm fetchList ,
44.Nm fetchXGetFile ,
45.Nm fetchGetFile ,
46.Nm fetchPutFile ,
47.Nm fetchStatFile ,
48.Nm fetchListFile ,
49.Nm fetchXGetHTTP ,
50.Nm fetchGetHTTP ,
51.Nm fetchPutHTTP ,
52.Nm fetchStatHTTP ,
53.Nm fetchListHTTP ,
54.Nm fetchXGetFTP ,
55.Nm fetchGetFTP ,
56.Nm fetchPutFTP ,
57.Nm fetchStatFTP ,
58.Nm fetchListFTP
59.Nd file transfer functions
60.Sh LIBRARY
61.Lb libfetch
62.Sh SYNOPSIS
63.In sys/param.h
64.In stdio.h
65.In fetch.h
66.Ft struct url *
67.Fn fetchMakeURL "const char *scheme" "const char *host" "int port" "const char *doc" "const char *user" "const char *pwd"
68.Ft struct url *
69.Fn fetchParseURL "const char *URL"
70.Ft void
71.Fn fetchFreeURL "struct url *u"
72.Ft FILE *
73.Fn fetchXGetURL "const char *URL" "struct url_stat *us" "const char *flags"
74.Ft FILE *
75.Fn fetchGetURL "const char *URL" "const char *flags"
76.Ft FILE *
77.Fn fetchPutURL "const char *URL" "const char *flags"
78.Ft int
79.Fn fetchStatURL "const char *URL" "struct url_stat *us" "const char *flags"
80.Ft struct url_ent *
81.Fn fetchListURL "const char *URL" "const char *flags"
82.Ft FILE *
83.Fn fetchXGet "struct url *u" "struct url_stat *us" "const char *flags"
84.Ft FILE *
85.Fn fetchGet "struct url *u" "const char *flags"
86.Ft FILE *
87.Fn fetchPut "struct url *u" "const char *flags"
88.Ft int
89.Fn fetchStat "struct url *u" "struct url_stat *us" "const char *flags"
90.Ft struct url_ent *
91.Fn fetchList "struct url *u" "const char *flags"
92.Ft FILE *
93.Fn fetchXGetFile "struct url *u" "struct url_stat *us" "const char *flags"
94.Ft FILE *
95.Fn fetchGetFile "struct url *u" "const char *flags"
96.Ft FILE *
97.Fn fetchPutFile "struct url *u" "const char *flags"
98.Ft int
99.Fn fetchStatFile "struct url *u" "struct url_stat *us" "const char *flags"
100.Ft struct url_ent *
101.Fn fetchListFile "struct url *u" "const char *flags"
102.Ft FILE *
103.Fn fetchXGetHTTP "struct url *u" "struct url_stat *us" "const char *flags"
104.Ft FILE *
105.Fn fetchGetHTTP "struct url *u" "const char *flags"
106.Ft FILE *
107.Fn fetchPutHTTP "struct url *u" "const char *flags"
108.Ft int
109.Fn fetchStatHTTP "struct url *u" "struct url_stat *us" "const char *flags"
110.Ft struct url_ent *
111.Fn fetchListHTTP "struct url *u" "const char *flags"
112.Ft FILE *
113.Fn fetchXGetFTP "struct url *u" "struct url_stat *us" "const char *flags"
114.Ft FILE *
115.Fn fetchGetFTP "struct url *u" "const char *flags"
116.Ft FILE *
117.Fn fetchPutFTP "struct url *u" "const char *flags"
118.Ft int
119.Fn fetchStatFTP "struct url *u" "struct url_stat *us" "const char *flags"
120.Ft struct url_ent *
121.Fn fetchListFTP "struct url *u" "const char *flags"
122.Sh DESCRIPTION
123These functions implement a high-level library for retrieving and
124uploading files using Uniform Resource Locators (URLs).
125.Pp
126.Fn fetchParseURL
127takes a URL in the form of a null-terminated string and splits it into
128its components function according to the Common Internet Scheme Syntax
129detailed in RFC1738.
130A regular expression which produces this syntax is:
131.Bd -literal
132    <scheme>:(//(<user>(:<pwd>)?@)?<host>(:<port>)?)?/(<document>)?
133.Ed
134.Pp
135If the URL does not seem to begin with a scheme name, the following
136syntax is assumed:
137.Bd -literal
138    ((<user>(:<pwd>)?@)?<host>(:<port>)?)?/(<document>)?
139.Ed
140.Pp
141Note that some components of the URL are not necessarily relevant to
142all URL schemes.
143For instance, the file scheme only needs the <scheme> and <document>
144components.
145.Pp
146.Fn fetchMakeURL
147and
148.Fn fetchParseURL
149return a pointer to a
150.Vt url
151structure, which is defined as follows in
152.Aq Pa fetch.h :
153.Bd -literal
154#define URL_SCHEMELEN 16
155#define URL_USERLEN 256
156#define URL_PWDLEN 256
157
158struct url {
159    char	 scheme[URL_SCHEMELEN+1];
160    char	 user[URL_USERLEN+1];
161    char	 pwd[URL_PWDLEN+1];
162    char	 host[MAXHOSTNAMELEN+1];
163    int		 port;
164    char	*doc;
165    off_t	 offset;
166    size_t	 length;
167};
168.Ed
169.Pp
170The pointer returned by
171.Fn fetchMakeURL
172or
173.Fn fetchParseURL
174should be freed using
175.Fn fetchFreeURL .
176.Pp
177.Fn fetchXGetURL ,
178.Fn fetchGetURL ,
179and
180.Fn fetchPutURL
181constitute the recommended interface to the
182.Nm fetch
183library.
184They examine the URL passed to them to determine the transfer
185method, and call the appropriate lower-level functions to perform the
186actual transfer.
187.Fn fetchXGetURL
188also returns the remote document's metadata in the
189.Vt url_stat
190structure pointed to by the
191.Fa us
192argument.
193.Pp
194The
195.Fa flags
196argument is a string of characters which specify transfer options.
197The
198meaning of the individual flags is scheme-dependent, and is detailed
199in the appropriate section below.
200.Pp
201.Fn fetchStatURL
202attempts to obtain the requested document's metadata and fill in the
203structure pointed to by its second argument.
204The
205.Vt url_stat
206structure is defined as follows in
207.Aq Pa fetch.h :
208.Bd -literal
209struct url_stat {
210    off_t	 size;
211    time_t	 atime;
212    time_t	 mtime;
213};
214.Ed
215.Pp
216If the size could not be obtained from the server, the
217.Fa size
218field is set to -1.
219If the modification time could not be obtained from the server, the
220.Fa mtime
221field is set to the epoch.
222If the access time could not be obtained from the server, the
223.Fa atime
224field is set to the modification time.
225.Pp
226.Fn fetchListURL
227attempts to list the contents of the directory pointed to by the URL
228provided.
229If successful, it returns a malloced array of
230.Vt url_ent
231structures.
232The
233.Vt url_ent
234structure is defined as follows in
235.Aq Pa fetch.h :
236.Bd -literal
237struct url_ent {
238    char         name[MAXPATHLEN];
239    struct url_stat stat;
240};
241.Ed
242.Pp
243The list is terminated by an entry with an empty name.
244.Pp
245The pointer returned by
246.Fn fetchListURL
247should be freed using
248.Fn free .
249.Pp
250.Fn fetchXGet ,
251.Fn fetchGet ,
252.Fn fetchPut
253and
254.Fn fetchStat
255are similar to
256.Fn fetchXGetURL ,
257.Fn fetchGetURL ,
258.Fn fetchPutURL
259and
260.Fn fetchStatURL ,
261except that they expect a pre-parsed URL in the form of a pointer to
262a
263.Vt struct url
264rather than a string.
265.Pp
266All of the
267.Fn fetchXGetXXX ,
268.Fn fetchGetXXX
269and
270.Fn fetchPutXXX
271functions return a pointer to a stream which can be used to read or
272write data from or to the requested document, respectively.
273Note that
274although the implementation details of the individual access methods
275vary, it can generally be assumed that a stream returned by one of the
276.Fn fetchXGetXXX
277or
278.Fn fetchGetXXX
279functions is read-only, and that a stream returned by one of the
280.Fn fetchPutXXX
281functions is write-only.
282.Sh FILE SCHEME
283.Fn fetchXGetFile ,
284.Fn fetchGetFile
285and
286.Fn fetchPutFile
287provide access to documents which are files in a locally mounted file
288system.
289Only the <document> component of the URL is used.
290.Pp
291.Fn fetchXGetFile
292and
293.Fn fetchGetFile
294do not accept any flags.
295.Pp
296.Fn fetchPutFile
297accepts the
298.Ql a
299(append to file) flag.
300If that flag is specified, the data written to
301the stream returned by
302.Fn fetchPutFile
303will be appended to the previous contents of the file, instead of
304replacing them.
305.Sh FTP SCHEME
306.Fn fetchXGetFTP ,
307.Fn fetchGetFTP
308and
309.Fn fetchPutFTP
310implement the FTP protocol as described in RFC959.
311.Pp
312If the
313.Ql p
314(passive) flag is specified, a passive (rather than active) connection
315will be attempted.
316.Pp
317If the
318.Ql l
319(low) flag is specified, data sockets will be allocated in the low (or
320default) port range instead of the high port range (see
321.Xr ip 4 ) .
322.Pp
323If the
324.Ql d
325(direct) flag is specified,
326.Fn fetchXGetFTP ,
327.Fn fetchGetFTP
328and
329.Fn fetchPutFTP
330will use a direct connection even if a proxy server is defined.
331.Pp
332If no user name or password is given, the
333.Nm fetch
334library will attempt an anonymous login, with user name "anonymous"
335and password "anonymous@<hostname>".
336.Sh HTTP SCHEME
337The
338.Fn fetchXGetHTTP ,
339.Fn fetchGetHTTP
340and
341.Fn fetchPutHTTP
342functions implement the HTTP/1.1 protocol.
343With a little luck, there's
344even a chance that they comply with RFC2616 and RFC2617.
345.Pp
346If the
347.Ql d
348(direct) flag is specified,
349.Fn fetchXGetHTTP ,
350.Fn fetchGetHTTP
351and
352.Fn fetchPutHTTP
353will use a direct connection even if a proxy server is defined.
354.Pp
355Since there seems to be no good way of implementing the HTTP PUT
356method in a manner consistent with the rest of the
357.Nm fetch
358library,
359.Fn fetchPutHTTP
360is currently unimplemented.
361.Sh AUTHENTICATION
362Apart from setting the appropriate environment variables and
363specifying the user name and password in the URL or the
364.Vt struct url ,
365the calling program has the option of defining an authentication
366function with the following prototype:
367.Pp
368.Ft int
369.Fn myAuthMethod "struct url *u"
370.Pp
371The callback function should fill in the
372.Fa user
373and
374.Fa pwd
375fields in the provided
376.Vt struct url
377and return 0 on success, or any other value to indicate failure.
378.Pp
379To register the authentication callback, simply set
380.Va fetchAuthMethod
381to point at it.
382The callback will be used whenever a site requires authentication and
383the appropriate environment variables aren't set.
384.Pp
385This interface is experimental and may be subject to change.
386.Sh RETURN VALUES
387.Fn fetchParseURL
388returns a pointer to a
389.Vt struct url
390containing the individual components of the URL.
391If it is
392unable to allocate memory, or the URL is syntactically incorrect,
393.Fn fetchParseURL
394returns a NULL pointer.
395.Pp
396The
397.Fn fetchStat
398functions return 0 on success and -1 on failure.
399.Pp
400All other functions return a stream pointer which may be used to
401access the requested document, or NULL if an error occurred.
402.Pp
403The following error codes are defined in
404.Aq Pa fetch.h :
405.Bl -tag -width 18n
406.It Bq Er FETCH_ABORT
407Operation aborted
408.It Bq Er FETCH_AUTH
409Authentication failed
410.It Bq Er FETCH_DOWN
411Service unavailable
412.It Bq Er FETCH_EXISTS
413File exists
414.It Bq Er FETCH_FULL
415File system full
416.It Bq Er FETCH_INFO
417Informational response
418.It Bq Er FETCH_MEMORY
419Insufficient memory
420.It Bq Er FETCH_MOVED
421File has moved
422.It Bq Er FETCH_NETWORK
423Network error
424.It Bq Er FETCH_OK
425No error
426.It Bq Er FETCH_PROTO
427Protocol error
428.It Bq Er FETCH_RESOLV
429Resolver error
430.It Bq Er FETCH_SERVER
431Server error
432.It Bq Er FETCH_TEMP
433Temporary error
434.It Bq Er FETCH_TIMEOUT
435Operation timed out
436.It Bq Er FETCH_UNAVAIL
437File is not available
438.It Bq Er FETCH_UNKNOWN
439Unknown error
440.It Bq Er FETCH_URL
441Invalid URL
442.El
443.Pp
444The accompanying error message includes a protocol-specific error code
445and message, e.g. "File is not available (404 Not Found)"
446.Sh ENVIRONMENT
447.Bl -tag -width ".Ev FETCH_BIND_ADDRESS"
448.It Ev FETCH_BIND_ADDRESS
449Specifies a hostname or IP address to which sockets used for outgoing
450connections will be bound.
451.It Ev FTP_LOGIN
452Default FTP login if none was provided in the URL.
453.It Ev FTP_PASSIVE_MODE
454If set to anything but
455.Ql no ,
456forces the FTP code to use passive mode.
457.It Ev FTP_PASSWORD
458Default FTP password if the remote server requests one and none was
459provided in the URL.
460.It Ev FTP_PROXY
461URL of the proxy to use for FTP requests.
462The document part is ignored.
463FTP and HTTP proxies are supported; if no scheme is specified, FTP is
464assumed.
465If the proxy is an FTP proxy,
466.Nm libfetch
467will send
468.Ql user@host
469as user name to the proxy, where
470.Ql user
471is the real user name, and
472.Ql host
473is the name of the FTP server.
474.Pp
475If this variable is set to an empty string, no proxy will be used for
476FTP requests, even if the
477.Ev HTTP_PROXY
478variable is set.
479.It Ev ftp_proxy
480Same as
481.Ev FTP_PROXY ,
482for compatibility.
483.It Ev HTTP_AUTH
484Specifies HTTP authorization parameters as a colon-separated list of
485items.
486The first and second item are the authorization scheme and realm
487respectively; further items are scheme-dependent.
488Currently, only basic authorization is supported.
489.Pp
490Basic authorization requires two parameters: the user name and
491password, in that order.
492.Pp
493This variable is only used if the server requires authorization and
494no user name or password was specified in the URL.
495.It Ev HTTP_PROXY
496URL of the proxy to use for HTTP requests.
497The document part is ignored.
498Only HTTP proxies are supported for HTTP requests.
499If no port number is specified, the default is 3128.
500.Pp
501Note that this proxy will also be used for FTP documents, unless the
502.Ev FTP_PROXY
503variable is set.
504.It Ev http_proxy
505Same as
506.Ev HTTP_PROXY ,
507for compatibility.
508.It Ev HTTP_PROXY_AUTH
509Specifies authorization parameters for the HTTP proxy in the same
510format as the
511.Ev HTTP_AUTH
512variable.
513.Pp
514This variable is used if and only if connected to an HTTP proxy, and
515is ignored if a user and/or a password were specified in the proxy
516URL.
517.It Ev HTTP_REFERER
518Specifies the referrer URL to use for HTTP requests.
519If set to
520.Dq auto ,
521the document URL will be used as referrer URL.
522.It Ev HTTP_USER_AGENT
523Specifies the User-Agent string to use for HTTP requests.
524This can be useful when working with HTTP origin or proxy servers that
525differentiate between user agents.
526.It Ev NETRC
527Specifies a file to use instead of
528.Pa ~/.netrc
529to look up login names and passwords for FTP sites.
530See
531.Xr ftp 1
532for a description of the file format.
533This feature is experimental.
534.El
535.Sh EXAMPLES
536To access a proxy server on
537.Pa proxy.example.com
538port 8080, set the
539.Ev HTTP_PROXY
540environment variable in a manner similar to this:
541.Pp
542.Dl HTTP_PROXY=http://proxy.example.com:8080
543.Pp
544If the proxy server requires authentication, there are
545two options available for passing the authentication data.
546The first method is by using the proxy URL:
547.Pp
548.Dl HTTP_PROXY=http://<user>:<pwd>@proxy.example.com:8080
549.Pp
550The second method is by using the
551.Ev HTTP_PROXY_AUTH
552environment variable:
553.Bd -literal -offset indent
554HTTP_PROXY=http://proxy.example.com:8080
555HTTP_PROXY_AUTH=basic:*:<user>:<pwd>
556.Ed
557.Sh SEE ALSO
558.Xr fetch 1 ,
559.Xr ftpio 3 ,
560.Xr ip 4
561.Rs
562.%A J. Postel
563.%A J. K. Reynolds
564.%D October 1985
565.%B File Transfer Protocol
566.%O RFC959
567.Re
568.Rs
569.%A P. Deutsch
570.%A A. Emtage
571.%A A. Marine.
572.%D May 1994
573.%T How to Use Anonymous FTP
574.%O RFC1635
575.Re
576.Rs
577.%A T. Berners-Lee
578.%A L. Masinter
579.%A M. McCahill
580.%D December 1994
581.%T Uniform Resource Locators (URL)
582.%O RFC1738
583.Re
584.Rs
585.%A R. Fielding
586.%A J. Gettys
587.%A J. Mogul
588.%A H. Frystyk
589.%A L. Masinter
590.%A P. Leach
591.%A T. Berners-Lee
592.%D January 1999
593.%B Hypertext Transfer Protocol -- HTTP/1.1
594.%O RFC2616
595.Re
596.Rs
597.%A J. Franks
598.%A P. Hallam-Baker
599.%A J. Hostetler
600.%A S. Lawrence
601.%A P. Leach
602.%A A. Luotonen
603.%A L. Stewart
604.%D June 1999
605.%B HTTP Authentication: Basic and Digest Access Authentication
606.%O RFC2617
607.Re
608.Sh HISTORY
609The
610.Nm fetch
611library first appeared in
612.Fx 3.0 .
613.Sh AUTHORS
614.An -nosplit
615The
616.Nm fetch
617library was mostly written by
618.An Dag-Erling Co\(:idan Sm\(/orgrav Aq des@FreeBSD.org
619with numerous suggestions from
620.An Jordan K. Hubbard Aq jkh@FreeBSD.org ,
621.An Eugene Skepner Aq eu@qub.com
622and other
623.Fx
624developers.
625It replaces the older
626.Nm ftpio
627library written by
628.An Poul-Henning Kamp Aq pkh@FreeBSD.org
629and
630.An Jordan K. Hubbard Aq jkh@FreeBSD.org .
631.Pp
632This manual page was written by
633.An Dag-Erling Co\(:idan Sm\(/orgrav Aq des@FreeBSD.org .
634.Sh BUGS
635Some parts of the library are not yet implemented.
636The most notable
637examples of this are
638.Fn fetchPutHTTP ,
639.Fn fetchListHTTP ,
640.Fn fetchListFTP
641and FTP proxy support.
642.Pp
643There's no way to select a proxy at run-time other than setting the
644.Ev HTTP_PROXY
645or
646.Ev FTP_PROXY
647environment variables as appropriate.
648.Pp
649.Nm libfetch
650does not understand or obey 305 (Use Proxy) replies.
651.Pp
652Error numbers are unique only within a certain context; the error
653codes used for FTP and HTTP overlap, as do those used for resolver and
654system errors.
655For instance, error code 202 means "Command not
656implemented, superfluous at this site" in an FTP context and
657"Accepted" in an HTTP context.
658.Pp
659.Fn fetchStatFTP
660does not check that the result of an MDTM command is a valid date.
661.Pp
662The man page is incomplete, poorly written and produces badly
663formatted text.
664.Pp
665The error reporting mechanism is unsatisfactory.
666.Pp
667Some parts of the code are not fully reentrant.
668