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