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 fetchGetURL , 32.Nm fetchPutURL , 33.Nm fetchStatURL , 34.Nm fetchListURL , 35.Nm fetchParseURL , 36.Nm fetchGet , 37.Nm fetchPut , 38.Nm fetchStat , 39.Nm fetchList , 40.Nm fetchGetFile , 41.Nm fetchPutFile , 42.Nm fetchStatFile , 43.Nm fetchListFile , 44.Nm fetchGetHTTP , 45.Nm fetchPutHTTP , 46.Nm fetchStatHTTP , 47.Nm fetchListHTTP , 48.Nm fetchGetFTP , 49.Nm fetchPutFTP 50.Nm fetchStatFTP 51.Nm fetchListFTP , 52.Nd file transfer library 53.Sh SYNOPSIS 54.Fd #include <sys/param.h> 55.Fd #include <stdio.h> 56.Fd #include <fetch.h> 57.Ft FILE * 58.Fn fetchGetURL "char *URL" "char *flags" 59.Ft FILE * 60.Fn fetchPutURL "char *URL" "char *flags" 61.Ft int 62.Fn fetchStatURL "char *URL" "struct url_stat *us" "char *flags" 63.Ft struct url_ent * 64.Fn fetchListURL "char *URL" "char *flags" 65.Ft struct url * 66.Fn fetchParseURL "char *URL" 67.Ft FILE * 68.Fn fetchGet "struct url *URL" "char *flags" 69.Ft FILE * 70.Fn fetchPut "struct url *URL" "char *flags" 71.Ft int 72.Fn fetchStat "struct url *URL" "struct url_stat *us" "char *flags" 73.Ft struct url_ent * 74.Fn fetchList "struct url *" "char *flags" 75.Ft FILE * 76.Fn fetchGetFile "struct url *u" "char *flags" 77.Ft FILE * 78.Fn fetchPutFile "struct url *u" "char *flags" 79.Ft int 80.Fn fetchStatFile "struct url *URL" "struct url_stat *us" "char *flags" 81.Ft struct url_ent * 82.Fn fetchListFile "struct url *" "char *flags" 83.Ft FILE * 84.Fn fetchGetHTTP "struct url *u" "char *flags" 85.Ft FILE * 86.Fn fetchPutHTTP "struct url *u" "char *flags" 87.Ft int 88.Fn fetchStatHTTP "struct url *URL" "struct url_stat *us" "char *flags" 89.Ft struct url_ent * 90.Fn fetchListHTTP "struct url *" "char *flags" 91.Ft FILE * 92.Fn fetchGetFTP "struct url *u" "char *flags" 93.Ft FILE * 94.Fn fetchPutFTP "struct url *u" "char *flags" 95.Ft int 96.Fn fetchStatFTP "struct url *URL" "struct url_stat *us" "char *flags" 97.Ft struct url_ent * 98.Fn fetchListFTP "struct url *" "char *flags" 99.Sh DESCRIPTION 100.Pp 101These functions implement a high-level library for retrieving and 102uploading files using Uniform Resource Locators (URLs). 103.Pp 104.Fn fetchGetURL 105and 106.Fn fetchPutURL 107constitute the recommended interface to the 108.Nm fetch 109library. 110They examine the URL passed to them to determine the transfer 111method, and call the appropriate lower-level functions to perform the 112actual transfer. 113The 114.Fa flags 115argument is a string of characters which specify transfer options. 116The 117meaning of the individual flags is scheme-dependent, and is detailed 118in the appropriate section below. 119.Pp 120.Fn fetchStatURL 121attempts to obtain the requested document's metadata and fill in the 122structure pointed to by it's second argument. 123The 124.Fa url_stat 125structure is defined as follows in 126.Aq Pa fetch.h : 127.Bd -literal 128struct url_stat { 129 off_t size; 130 time_t atime; 131 time_t mtime; 132}; 133.Ed 134.Pp 135.Fn fetchListURL 136attempts to list the contents of the directory pointed to by the URL 137provided. 138If successful, it returns a malloced array of 139.Fa url_ent 140structures. 141The 142.Fa url_ent 143structure is defined as follows in 144.Aq Pa fetch.h : 145.Bd -literal 146struct url_ent { 147 char name[MAXPATHLEN]; 148 struct url_stat stat; 149}; 150.Ed 151.Pp 152The list is terminated by an entry with an empty name. 153.Pp 154The pointer returned by 155.Fn fetchListURL 156should be freed using 157.Fn free . 158.Pp 159.Fn fetchParseURL 160takes a URL in the form of a null-terminated string and splits it into 161its components function according to the Common Internet Scheme Syntax 162detailed in RFC1738. A regular expression which produces this syntax 163is: 164.Bd -literal 165 <scheme>:(//(<user>(:<pwd>)?@)?<host>(:<port>)?)?/(<document>)? 166.Ed 167.Pp 168Note that some components of the URL are not necessarily relevant to 169all URL schemes. 170For instance, the file scheme only needs the <scheme> 171and <document> components. 172.Pp 173The pointer returned by 174.Fn fetchParseURL 175should be freed using 176.Fn free . 177.Pp 178.Fn fetchGet , 179.Fn fetchPut 180and 181.Fn fetchStat 182are similar to 183.Fn fetchGetURL , 184.Fn fetchPutURL 185and 186.Fn fetchStatURL , 187except that they expect a pre-parsed URL in the form of a pointer to 188a 189.Fa struct url 190rather than a string. 191.Pp 192All of the 193.Fn fetchGetXXX 194and 195.Fn fetchPutXXX 196functions return a pointer to a stream which can be used to read or 197write data from or to the requested document, respectively. 198Note that 199although the implementation details of the individual access methods 200vary, it can generally be assumed that a stream returned by one of the 201.Fn fetchGetXXX 202functions is read-only, and that a stream returned by one of the 203.Fn fetchPutXXX 204functions is write-only. 205.Sh FILE SCHEME 206.Fn fetchGetFile 207and 208.Fn fetchPutFile 209provide access to documents which are files in a locally mounted file 210system. 211Only the <document> component of the URL is used. 212.Pp 213.Fn fetchGetFile 214does not accept any flags. 215.Pp 216.Fn fetchPutFile 217accepts the 218.Fa a 219(append to file) flag. 220If that flag is specified, the data written to 221the stream returned by 222.Fn fetchPutFile 223will be appended to the previous contents of the file, instead of 224replacing them. 225.Sh FTP SCHEME 226.Fn fetchGetFTP 227and 228.Fn fetchPutFTP 229implement the FTP protocol as described in RFC959. 230.Pp 231If the 232.Fa p 233(passive) flag is specified, a passive (rather than active) connection 234will be attempted. 235.Pp 236If the 237.Fa h 238(high) flag is specified, data sockets will be allocated in the high 239port range (see 240.Xr ip 4 ). 241.Pp 242If the 243.Fa d 244(direct) flag is specified, 245.Fn fetchGetFTP 246and 247.Fn fetchPutFTP 248will use a direct connection even if a proxy server is defined. 249.Pp 250If no user name or password is given, the 251.Nm fetch 252library will attempt an anonymous login, with user name "ftp" and 253password "ftp". 254.Sh HTTP SCHEME 255The 256.Fn fetchGetHTTP 257and 258.Fn fetchPutHTTP 259functions implement the HTTP/1.1 protocol. 260With a little luck, there's 261even a chance that they comply with RFC2068. 262.Pp 263If the 264.Fa d 265(direct) flag is specified, 266.Fn fetchGetHTTP 267and 268.Fn fetchPutHTTP 269will use a direct connection even if a proxy server is defined. 270.Pp 271Since there seems to be no good way of implementing the HTTP PUT 272method in a manner consistent with the rest of the 273.Nm fetch 274library, 275.Fn fetchPutHTTP 276is currently unimplemented. 277.Sh RETURN VALUES 278.Fn fetchParseURL 279returns a pointer to a 280.Fa struct url 281containing the individual components of the URL. 282If it is 283unable to allocate memory, or the URL is syntactically incorrect, 284.Fn fetchParseURL 285returns a NULL pointer. 286.Pp 287The 288.Fn fetchStat 289functions return 0 on success and -1 on failure. 290.Pp 291All other functions return a stream pointer which may be used to 292access the requested document, or NULL if an error occurred. 293.Pp 294.Nm Libfetch 295uses the Common Error Library 296.Nm ( libcom_err ) 297to report errors. 298The error code passed to 299.Fn com_err 300is one of: 301.Bl -tag -width Er 302.It Bq Er FETCH_ABORT 303Operation aborted 304.It Bq Er FETCH_AUTH 305Authentication failed 306.It Bq Er FETCH_DOWN 307Service unavailable 308.It Bq Er FETCH_EXISTS 309File exists 310.It Bq Er FETCH_FULL 311File system full 312.It Bq Er FETCH_INFO 313Informational response 314.It Bq Er FETCH_MEMORY 315Insufficient memory 316.It Bq Er FETCH_MOVED 317File has moved 318.It Bq Er FETCH_NETWORK 319Network error 320.It Bq Er FETCH_OK 321No error 322.It Bq Er FETCH_PROTO 323Protocol error 324.It Bq Er FETCH_RESOLV 325Resolver error 326.It Bq Er FETCH_SERVER 327Server error 328.It Bq Er FETCH_TEMP 329Temporary error 330.It Bq Er FETCH_TIMEOUT 331Operation timed out 332.It Bq Er FETCH_UNAVAIL 333File is not available 334.It Bq Er FETCH_UNKNOWN 335Unknown error 336.It Bq Er FETCH_URL 337Invalid URL 338.El 339.Pp 340The accompanying error message includes a protocol-specific error code 341and message, e.g. "File is not available (404 Not Found)" 342.Sh ENVIRONMENT 343The FTP and HTTP related functions use the 344.Ev HTTP_PROXY 345and 346.Ev FTP_PROXY 347environment variables, respectively, as the address of a proxy server 348to use for transferring files. 349.Sh SEE ALSO 350.Xr com_err 3 , 351.Xr fetch 1 , 352.Xr ftpio 3 , 353.Xr ip 4 . 354.Rs 355.%A T. Berners-Lee, L. Masinter & M. McCahill 356.%D December 1994 357.%T Uniform Resource Locators (URL) 358.%O RFC1738 359.Re 360.Rs 361.%A R. Fielding, J. Gettys, J. Mogul, H. Frystyk, T. Berners-Lee 362.%D Januray 1997 363.%B Hypertext Transfer Protocol -- HTTP/1.1 364.%O RFC2068 365.Re 366.Rs 367.%A J. Postel, J. K. Reynolds 368.%D October 1985 369.%B File Transfer Protocol 370.%O RFC959 371.Re 372.Sh NOTES 373The 374.Nm fetch 375library uses the Common Error library, and applications which link 376with 377.Nm libfetch 378must therefore also link with 379.Nm libcom_err . 380.Sh HISTORY 381The 382.Nm fetch 383library first appeared in 384.Fx 3.0 . 385.Sh AUTHORS 386The 387.Nm fetch 388library was mostly written by 389.An Dag-Erling Co�dan Sm�rgrav Aq des@FreeBSD.org 390with numerous suggestions from 391.An Jordan K. Hubbard Aq jkh@FreeBSD.org , 392.An Eugene Skepner Aq eu@qub.com 393and other FreeBSD developers. 394It replaces the older 395.Nm ftpio 396library written by 397.An Poul-Henning Kamp Aq pkh@FreeBSD.org 398and 399.An Jordan K. Hubbard Aq jkh@FreeBSD.org . 400.Pp 401This manual page was written by 402.An Dag-Erling Co�dan Sm�rgrav Aq des@FreeBSD.org 403.Sh BUGS 404Some parts of the library are not yet implemented. 405The most notable 406examples of this are 407.Fn fetchPutHTTP , 408.Fn fetchStatHTTP , 409.Fn fetchListHTTP , 410.Fn fetchListFTP , 411and FTP proxy support. 412.Pp 413There's no way to select a proxy at run-time other than setting the 414.Ev HTTP_PROXY 415or 416.Ev FTP_PROXY 417environment variables as appropriate. 418There is also no way to stop the 419FTP and HTTP functions from trying to use a proxy if these variables 420are set. 421.Pp 422HTTP authentication doesn't work. 423I'm not sure that's a bug in my 424code; as far as I can determine, 425.Nm libfetch 426handles HTTP/1.1 basic authentication correctly as outlined in 427RFC2068, but I haven't been able to find an HTTP server that honors 428the Authentication: header field. 429Also, 430.Nm libfetch 431does not attempt to interpret and respond to authentication requests 432from the HTTP server. 433.Pp 434No attempt is made to encode spaces etc. within URLs. 435Spaces in the 436document part of an URLshould be replaced with "%20" in HTTP URLs and 437"\\ " in FTP URLs. 438.Pp 439Error numbers are unique only within a certain context; the error 440codes used for FTP and HTTP overlap, as do those used for resolver and 441system errors. 442For instance, error code 202 means "Command not 443implemented, superfluous at this site" in an FTP context and 444"Accepted" in an HTTP context. 445.Pp 446.Fn fetchStatFTP 447does not check that the result of an MDTM command is a valid date. 448.Pp 449The HTTP code needs a complete rewrite, or at least a serious cleanup. 450.Pp 451The man page is poorly written and produces badly formatted text. 452.Pp 453Tons of other stuff. 454