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