]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libfetch/fetch.3
This commit was generated by cvs2svn to compensate for changes in r80484,
[FreeBSD/FreeBSD.git] / lib / libfetch / fetch.3
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$
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 .Fd #include <sys/param.h>
64 .Fd #include <stdio.h>
65 .Fd #include <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
123 These functions implement a high-level library for retrieving and
124 uploading files using Uniform Resource Locators (URLs).
125 .Pp
126 .Fn fetchParseURL
127 takes a URL in the form of a null-terminated string and splits it into
128 its components function according to the Common Internet Scheme Syntax
129 detailed in RFC1738.
130 A regular expression which produces this syntax is:
131 .Bd -literal
132     <scheme>:(//(<user>(:<pwd>)?@)?<host>(:<port>)?)?/(<document>)?
133 .Ed
134 .Pp
135 If the URL does not seem to begin with a scheme name, the following
136 syntax is assumed:
137 .Bd -literal
138     ((<user>(:<pwd>)?@)?<host>(:<port>)?)?/(<document>)?
139 .Ed
140 .Pp
141 Note that some components of the URL are not necessarily relevant to
142 all URL schemes.
143 For instance, the file scheme only needs the <scheme> and <document>
144 components.
145 .Pp
146 .Fn fetchMakeURL
147 and
148 .Fn fetchParseURL
149 return a pointer to a
150 .Vt url
151 structure, 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
158 struct 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
170 The pointer returned by
171 .Fn fetchMakeURL
172 or
173 .Fn fetchParseURL
174 should be freed using
175 .Fn fetchFreeURL .
176 .Pp
177 .Fn fetchXGetURL ,
178 .Fn fetchGetURL ,
179 and
180 .Fn fetchPutURL
181 constitute the recommended interface to the
182 .Nm fetch
183 library.
184 They examine the URL passed to them to determine the transfer
185 method, and call the appropriate lower-level functions to perform the
186 actual transfer.
187 .Fn fetchXGetURL
188 also returns the remote document's metadata in the
189 .Vt url_stat
190 structure pointed to by the
191 .Fa us
192 argument.
193 .Pp
194 The
195 .Fa flags
196 argument is a string of characters which specify transfer options.
197 The
198 meaning of the individual flags is scheme-dependent, and is detailed
199 in the appropriate section below.
200 .Pp
201 .Fn fetchStatURL
202 attempts to obtain the requested document's metadata and fill in the
203 structure pointed to by its second argument.
204 The
205 .Vt url_stat
206 structure is defined as follows in
207 .Aq Pa fetch.h :
208 .Bd -literal
209 struct url_stat {
210     off_t        size;
211     time_t       atime;
212     time_t       mtime;
213 };
214 .Ed
215 .Pp
216 If the size could not be obtained from the server, the
217 .Fa size
218 field is set to -1.
219 If the modification time could not be obtained from the server, the
220 .Fa mtime
221 field is set to the epoch.
222 If the access time could not be obtained from the server, the
223 .Fa atime
224 field is set to the modification time.
225 .Pp
226 .Fn fetchListURL
227 attempts to list the contents of the directory pointed to by the URL
228 provided.
229 If successful, it returns a malloced array of
230 .Vt url_ent
231 structures.
232 The
233 .Vt url_ent
234 structure is defined as follows in
235 .Aq Pa fetch.h :
236 .Bd -literal
237 struct url_ent {
238     char         name[MAXPATHLEN];
239     struct url_stat stat;
240 };
241 .Ed
242 .Pp
243 The list is terminated by an entry with an empty name.
244 .Pp
245 The pointer returned by
246 .Fn fetchListURL
247 should be freed using
248 .Fn free .
249 .Pp
250 .Fn fetchXGet ,
251 .Fn fetchGet ,
252 .Fn fetchPut
253 and
254 .Fn fetchStat
255 are similar to
256 .Fn fetchXGetURL ,
257 .Fn fetchGetURL ,
258 .Fn fetchPutURL
259 and
260 .Fn fetchStatURL ,
261 except that they expect a pre-parsed URL in the form of a pointer to
262 a
263 .Vt struct url
264 rather than a string.
265 .Pp
266 All of the
267 .Fn fetchXGetXXX ,
268 .Fn fetchGetXXX
269 and
270 .Fn fetchPutXXX
271 functions return a pointer to a stream which can be used to read or
272 write data from or to the requested document, respectively.
273 Note that
274 although the implementation details of the individual access methods
275 vary, it can generally be assumed that a stream returned by one of the
276 .Fn fetchXGetXXX
277 or
278 .Fn fetchGetXXX
279 functions is read-only, and that a stream returned by one of the
280 .Fn fetchPutXXX
281 functions is write-only.
282 .Sh FILE SCHEME
283 .Fn fetchXGetFile ,
284 .Fn fetchGetFile
285 and
286 .Fn fetchPutFile
287 provide access to documents which are files in a locally mounted file
288 system.
289 Only the <document> component of the URL is used.
290 .Pp
291 .Fn fetchXGetFile
292 and
293 .Fn fetchGetFile
294 do not accept any flags.
295 .Pp
296 .Fn fetchPutFile
297 accepts the
298 .Ql a
299 (append to file) flag.
300 If that flag is specified, the data written to
301 the stream returned by
302 .Fn fetchPutFile
303 will be appended to the previous contents of the file, instead of
304 replacing them.
305 .Sh FTP SCHEME
306 .Fn fetchXGetFTP ,
307 .Fn fetchGetFTP
308 and
309 .Fn fetchPutFTP
310 implement the FTP protocol as described in RFC959.
311 .Pp
312 If the
313 .Ql p
314 (passive) flag is specified, a passive (rather than active) connection
315 will be attempted.
316 .Pp
317 If the
318 .Ql l
319 (low) flag is specified, data sockets will be allocated in the low (or
320 default) port range instead of the hifh port range (see
321 .Xr ip 4 ) .
322 .Pp
323 If the
324 .Ql d
325 (direct) flag is specified,
326 .Fn fetchXGetFTP ,
327 .Fn fetchGetFTP
328 and
329 .Fn fetchPutFTP
330 will use a direct connection even if a proxy server is defined.
331 .Pp
332 If no user name or password is given, the
333 .Nm fetch
334 library will attempt an anonymous login, with user name "anonymous"
335 and password "anonymous@<hostname>".
336 .Sh HTTP SCHEME
337 The
338 .Fn fetchXGetHTTP ,
339 .Fn fetchGetHTTP
340 and
341 .Fn fetchPutHTTP
342 functions implement the HTTP/1.1 protocol.
343 With a little luck, there's
344 even a chance that they comply with RFC2616 and RFC2617.
345 .Pp
346 If the
347 .Ql d
348 (direct) flag is specified,
349 .Fn fetchXGetHTTP ,
350 .Fn fetchGetHTTP
351 and
352 .Fn fetchPutHTTP
353 will use a direct connection even if a proxy server is defined.
354 .Pp
355 Since there seems to be no good way of implementing the HTTP PUT
356 method in a manner consistent with the rest of the
357 .Nm fetch
358 library,
359 .Fn fetchPutHTTP
360 is currently unimplemented.
361 .Sh AUTHENTICATION
362 Apart from setting the appropriate environment variables and
363 specifying the user name and password in the URL or the
364 .Vt struct url ,
365 the calling program has the option of defining an authentication
366 function with the following prototype:
367 .Pp
368 .Ft int
369 .Fn myAuthMethod "struct url *u"
370 .Pp
371 The callback function should fill in the
372 .Fa user
373 and
374 .Fa pwd
375 fields in the provided
376 .Vt struct url
377 and return 0 on success, or any other value to indicate failure.
378 .Pp
379 To register the authentication callback, simply set
380 .Va fetchAuthMethod
381 to point at it.
382 The callback will be used whenever a site requires authentication and
383 the appropriate environment variables aren't set.
384 .Pp
385 This interface is experimental and may be subject to change.
386 .Sh RETURN VALUES
387 .Fn fetchParseURL
388 returns a pointer to a
389 .Vt struct url
390 containing the individual components of the URL.
391 If it is
392 unable to allocate memory, or the URL is syntactically incorrect,
393 .Fn fetchParseURL
394 returns a NULL pointer.
395 .Pp
396 The
397 .Fn fetchStat
398 functions return 0 on success and -1 on failure.
399 .Pp
400 All other functions return a stream pointer which may be used to
401 access the requested document, or NULL if an error occurred.
402 .Pp
403 The following error codes are defined in
404 .Aq Pa fetch.h :
405 .Bl -tag -width 18n
406 .It Bq Er FETCH_ABORT
407 Operation aborted
408 .It Bq Er FETCH_AUTH
409 Authentication failed
410 .It Bq Er FETCH_DOWN
411 Service unavailable
412 .It Bq Er FETCH_EXISTS
413 File exists
414 .It Bq Er FETCH_FULL
415 File system full
416 .It Bq Er FETCH_INFO
417 Informational response
418 .It Bq Er FETCH_MEMORY
419 Insufficient memory
420 .It Bq Er FETCH_MOVED
421 File has moved
422 .It Bq Er FETCH_NETWORK
423 Network error
424 .It Bq Er FETCH_OK
425 No error
426 .It Bq Er FETCH_PROTO
427 Protocol error
428 .It Bq Er FETCH_RESOLV
429 Resolver error
430 .It Bq Er FETCH_SERVER
431 Server error
432 .It Bq Er FETCH_TEMP
433 Temporary error
434 .It Bq Er FETCH_TIMEOUT
435 Operation timed out
436 .It Bq Er FETCH_UNAVAIL
437 File is not available
438 .It Bq Er FETCH_UNKNOWN
439 Unknown error
440 .It Bq Er FETCH_URL
441 Invalid URL
442 .El
443 .Pp
444 The accompanying error message includes a protocol-specific error code
445 and message, e.g. "File is not available (404 Not Found)"
446 .Sh ENVIRONMENT
447 .Bl -tag -width FTP_PASSIVE_MODE
448 .It Ev FTP_LOGIN
449 Default FTP login if none was provided in the URL.
450 .It Ev FTP_PASSIVE_MODE
451 If set to anything but
452 .Ql no ,
453 forces the FTP code to use passive mode.
454 .It Ev FTP_PASSWORD
455 Default FTP password if the remote server requests one and none was
456 provided in the URL.
457 .It Ev FTP_PROXY
458 URL of the proxy to use for FTP requests.
459 The document part is ignored.
460 FTP and HTTP proxies are supported; if no scheme is specified, FTP is
461 assumed.
462 If the proxy is an FTP proxy,
463 .Nm libfetch
464 will send
465 .Ql user@host
466 as user name to the proxy, where
467 .Ql user
468 is the real user name, and
469 .Ql host
470 is the name of the FTP server.
471 .Pp
472 If this variable is set to an empty string, no proxy will be used for
473 FTP requests, even if the
474 .Ev HTTP_PROXY
475 variable is set.
476 .It Ev ftp_proxy
477 Same as
478 .Ev FTP_PROXY ,
479 for compatibility.
480 .It Ev HTTP_AUTH
481 Specifies HTTP authorization parameters as a colon-separated list of
482 items.
483 The first and second item are the authorization scheme and realm
484 respectively; further items are scheme-dependent.
485 Currently, only basic authorization is supported.
486 .Pp
487 Basic authorization requires two parameters: the user name and
488 password, in that order.
489 .Pp
490 This variable is only used if the server requires authorization and
491 no user name or password was specified in the URL.
492 .It Ev HTTP_PROXY
493 URL of the proxy to use for HTTP requests.
494 The document part is ignored.
495 Only HTTP proxies are supported for HTTP requests.
496 If no port number is specified, the default is 3128.
497 .Pp
498 Note that this proxy will also be used for FTP documents, unless the
499 .Ev FTP_PROXY
500 variable is set.
501 .It Ev http_proxy
502 Same as
503 .Ev HTTP_PROXY ,
504 for compatibility.
505 .It Ev HTTP_PROXY_AUTH
506 Specifies authorization parameters for the HTTP proxy in the same
507 format as the
508 .Ev HTTP_AUTH
509 variable.
510 .Pp
511 This variable is used if and only if connected to an HTTP proxy, and
512 is ignored if a user and/or a password were specified in the proxy
513 URL.
514 .It Ev HTTP_USER_AGENT
515 Specifies the User-Agent string to use for HTTP requests.
516 This can be useful when working with HTTP origin or proxy servers that
517 differentiate between user agents.
518 .El
519 .Sh SEE ALSO
520 .Xr fetch 1 ,
521 .Xr ftpio 3 ,
522 .Xr ip 4
523 .Rs
524 .%A J. Postel
525 .%A J. K. Reynolds
526 .%D October 1985
527 .%B File Transfer Protocol
528 .%O RFC959
529 .Re
530 .Rs
531 .%A P. Deutsch
532 .%A A. Emtage
533 .%A A. Marine.
534 .%D May 1994
535 .%T How to Use Anonymous FTP
536 .%O RFC1635
537 .Re
538 .Rs
539 .%A T. Berners-Lee
540 .%A L. Masinter
541 .%A M. McCahill
542 .%D December 1994
543 .%T Uniform Resource Locators (URL)
544 .%O RFC1738
545 .Re
546 .Rs
547 .%A R. Fielding
548 .%A J. Gettys
549 .%A J. Mogul
550 .%A H. Frystyk
551 .%A L. Masinter
552 .%A P. Leach
553 .%A T. Berners-Lee
554 .%D Januray 1999
555 .%B Hypertext Transfer Protocol -- HTTP/1.1
556 .%O RFC2616
557 .Re
558 .Rs
559 .%A J. Franks
560 .%A P. Hallam-Baker
561 .%A J. Hostetler
562 .%A S. Lawrence
563 .%A P. Leach
564 .%A A. Luotonen
565 .%A L. Stewart
566 .%D June 1999
567 .%B HTTP Authentication: Basic and Digest Access Authentication
568 .%O RFC2617
569 .Re
570 .Sh HISTORY
571 The
572 .Nm fetch
573 library first appeared in
574 .Fx 3.0 .
575 .Sh AUTHORS
576 .An -nosplit
577 The
578 .Nm fetch
579 library was mostly written by
580 .An Dag-Erling Co\(:idan Sm\(/orgrav Aq des@FreeBSD.org
581 with numerous suggestions from
582 .An Jordan K. Hubbard Aq jkh@FreeBSD.org ,
583 .An Eugene Skepner Aq eu@qub.com
584 and other
585 .Fx
586 developers.
587 It replaces the older
588 .Nm ftpio
589 library written by
590 .An Poul-Henning Kamp Aq pkh@FreeBSD.org
591 and
592 .An Jordan K. Hubbard Aq jkh@FreeBSD.org .
593 .Pp
594 This manual page was written by
595 .An Dag-Erling Co\(:idan Sm\(/orgrav Aq des@FreeBSD.org .
596 .Sh BUGS
597 Some parts of the library are not yet implemented.
598 The most notable
599 examples of this are
600 .Fn fetchPutHTTP ,
601 .Fn fetchListHTTP ,
602 .Fn fetchListFTP
603 and FTP proxy support.
604 .Pp
605 There's no way to select a proxy at run-time other than setting the
606 .Ev HTTP_PROXY
607 or
608 .Ev FTP_PROXY
609 environment variables as appropriate.
610 .Pp
611 .Nm libfetch
612 does not understand or obey 305 (Use Proxy) replies.
613 .Pp
614 Error numbers are unique only within a certain context; the error
615 codes used for FTP and HTTP overlap, as do those used for resolver and
616 system errors.
617 For instance, error code 202 means "Command not
618 implemented, superfluous at this site" in an FTP context and
619 "Accepted" in an HTTP context.
620 .Pp
621 .Fn fetchStatFTP
622 does not check that the result of an MDTM command is a valid date.
623 .Pp
624 The man page is incomplete, poorly written and produces badly
625 formatted text.
626 .Pp
627 The error reporting mechanism is unsatisfactory.
628 .Pp
629 Some parts of the code are not fully reentrant.