INTERNALS 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. _ _ ____ _
  2. ___| | | | _ \| |
  3. / __| | | | |_) | |
  4. | (__| |_| | _ <| |___
  5. \___|\___/|_| \_\_____|
  6. INTERNALS
  7. The project is split in two. The library and the client. The client part uses
  8. the library, but the library is designed to allow other applications to use
  9. it.
  10. The largest amount of code and complexity is in the library part.
  11. CVS
  12. ===
  13. All changes to the sources are committed to the CVS repository as soon as
  14. they're somewhat verified to work. Changes shall be commited as independently
  15. as possible so that individual changes can be easier spotted and tracked
  16. afterwards.
  17. Tagging shall be used extensively, and by the time we release new archives we
  18. should tag the sources with a name similar to the released version number.
  19. Portability
  20. ===========
  21. We write curl and libcurl to compile with C89 compilers. On 32bit and up
  22. machines. Most of libcurl assumes more or less POSIX compliance but that's
  23. not a requirement.
  24. We write libcurl to build and work with lots of third party tools, and we
  25. want it to remain functional and buildable with these and later versions
  26. (older versions may still work but is not what we work hard to maintain):
  27. OpenSSL 0.9.6
  28. GnuTLS 1.2
  29. zlib 1.1.4
  30. libssh2 0.16
  31. c-ares 1.5.0
  32. libidn 0.4.1
  33. *yassl 1.4.0 (http://curl.haxx.se/mail/lib-2008-02/0093.html)
  34. openldap 2.0
  35. MIT krb5 lib 1.2.4
  36. qsossl V5R2M0
  37. NSS 3.11.x
  38. Heimdal ?
  39. * = only partly functional, but that's due to bugs in the third party lib, not
  40. because of libcurl code
  41. On systems where configure runs, we aim at working on them all - if they have
  42. a suitable C compiler. On systems that don't run configure, we strive to keep
  43. curl running fine on:
  44. Windows 98
  45. AS/400 V5R2M0
  46. Symbian 9.1
  47. Windows CE ?
  48. TPF ?
  49. When writing code (mostly for generating stuff included in release tarballs)
  50. we use a few "build tools" and we make sure that we remain functional with
  51. these versions:
  52. GNU Libtool 1.4.2
  53. GNU Autoconf 2.57
  54. GNU Automake 1.7 (we currently avoid 1.10 due to Solaris-related bugs)
  55. GNU M4 1.4
  56. perl 4
  57. roffit 0.5
  58. groff ? (any version that supports "groff -Tps -man [in] [out]")
  59. ps2pdf (gs) ?
  60. Windows vs Unix
  61. ===============
  62. There are a few differences in how to program curl the unix way compared to
  63. the Windows way. The four perhaps most notable details are:
  64. 1. Different function names for socket operations.
  65. In curl, this is solved with defines and macros, so that the source looks
  66. the same at all places except for the header file that defines them. The
  67. macros in use are sclose(), sread() and swrite().
  68. 2. Windows requires a couple of init calls for the socket stuff.
  69. That's taken care of by the curl_global_init() call, but if other libs also
  70. do it etc there might be reasons for applications to alter that behaviour.
  71. 3. The file descriptors for network communication and file operations are
  72. not easily interchangable as in unix.
  73. We avoid this by not trying any funny tricks on file descriptors.
  74. 4. When writing data to stdout, Windows makes end-of-lines the DOS way, thus
  75. destroying binary data, although you do want that conversion if it is
  76. text coming through... (sigh)
  77. We set stdout to binary under windows
  78. Inside the source code, We make an effort to avoid '#ifdef [Your OS]'. All
  79. conditionals that deal with features *should* instead be in the format
  80. '#ifdef HAVE_THAT_WEIRD_FUNCTION'. Since Windows can't run configure scripts,
  81. we maintain two curl_config-win32.h files (one in lib/ and one in src/) that
  82. are supposed to look exactly as a curl_config.h file would have looked like on
  83. a Windows machine!
  84. Generally speaking: always remember that this will be compiled on dozens of
  85. operating systems. Don't walk on the edge.
  86. Library
  87. =======
  88. There are plenty of entry points to the library, namely each publicly defined
  89. function that libcurl offers to applications. All of those functions are
  90. rather small and easy-to-follow. All the ones prefixed with 'curl_easy' are
  91. put in the lib/easy.c file.
  92. curl_global_init_() and curl_global_cleanup() should be called by the
  93. application to initialize and clean up global stuff in the library. As of
  94. today, it can handle the global SSL initing if SSL is enabled and it can init
  95. the socket layer on windows machines. libcurl itself has no "global" scope.
  96. All printf()-style functions use the supplied clones in lib/mprintf.c. This
  97. makes sure we stay absolutely platform independent.
  98. curl_easy_init() allocates an internal struct and makes some initializations.
  99. The returned handle does not reveal internals. This is the 'SessionHandle'
  100. struct which works as an "anchor" struct for all curl_easy functions. All
  101. connections performed will get connect-specific data allocated that should be
  102. used for things related to particular connections/requests.
  103. curl_easy_setopt() takes three arguments, where the option stuff must be
  104. passed in pairs: the parameter-ID and the parameter-value. The list of
  105. options is documented in the man page. This function mainly sets things in
  106. the 'SessionHandle' struct.
  107. curl_easy_perform() does a whole lot of things:
  108. It starts off in the lib/easy.c file by calling Curl_perform() and the main
  109. work then continues in lib/url.c. The flow continues with a call to
  110. Curl_connect() to connect to the remote site.
  111. o Curl_connect()
  112. ... analyzes the URL, it separates the different components and connects to
  113. the remote host. This may involve using a proxy and/or using SSL. The
  114. Curl_resolv() function in lib/hostip.c is used for looking up host names
  115. (it does then use the proper underlying method, which may vary between
  116. platforms and builds).
  117. When Curl_connect is done, we are connected to the remote site. Then it is
  118. time to tell the server to get a document/file. Curl_do() arranges this.
  119. This function makes sure there's an allocated and initiated 'connectdata'
  120. struct that is used for this particular connection only (although there may
  121. be several requests performed on the same connect). A bunch of things are
  122. inited/inherited from the SessionHandle struct.
  123. o Curl_do()
  124. Curl_do() makes sure the proper protocol-specific function is called. The
  125. functions are named after the protocols they handle. Curl_ftp(),
  126. Curl_http(), Curl_dict(), etc. They all reside in their respective files
  127. (ftp.c, http.c and dict.c). HTTPS is handled by Curl_http() and FTPS by
  128. Curl_ftp().
  129. The protocol-specific functions of course deal with protocol-specific
  130. negotiations and setup. They have access to the Curl_sendf() (from
  131. lib/sendf.c) function to send printf-style formatted data to the remote
  132. host and when they're ready to make the actual file transfer they call the
  133. Curl_Transfer() function (in lib/transfer.c) to setup the transfer and
  134. returns.
  135. If this DO function fails and the connection is being re-used, libcurl will
  136. then close this connection, setup a new connection and re-issue the DO
  137. request on that. This is because there is no way to be perfectly sure that
  138. we have discovered a dead connection before the DO function and thus we
  139. might wrongly be re-using a connection that was closed by the remote peer.
  140. Some time during the DO function, the Curl_setup_transfer() function must
  141. be called with some basic info about the upcoming transfer: what socket(s)
  142. to read/write and the expected file tranfer sizes (if known).
  143. o Transfer()
  144. Curl_perform() then calls Transfer() in lib/transfer.c that performs the
  145. entire file transfer.
  146. During transfer, the progress functions in lib/progress.c are called at a
  147. frequent interval (or at the user's choice, a specified callback might get
  148. called). The speedcheck functions in lib/speedcheck.c are also used to
  149. verify that the transfer is as fast as required.
  150. o Curl_done()
  151. Called after a transfer is done. This function takes care of everything
  152. that has to be done after a transfer. This function attempts to leave
  153. matters in a state so that Curl_do() should be possible to call again on
  154. the same connection (in a persistent connection case). It might also soon
  155. be closed with Curl_disconnect().
  156. o Curl_disconnect()
  157. When doing normal connections and transfers, no one ever tries to close any
  158. connections so this is not normally called when curl_easy_perform() is
  159. used. This function is only used when we are certain that no more transfers
  160. is going to be made on the connection. It can be also closed by force, or
  161. it can be called to make sure that libcurl doesn't keep too many
  162. connections alive at the same time (there's a default amount of 5 but that
  163. can be changed with the CURLOPT_MAXCONNECTS option).
  164. This function cleans up all resources that are associated with a single
  165. connection.
  166. Curl_perform() is the function that does the main "connect - do - transfer -
  167. done" loop. It loops if there's a Location: to follow.
  168. When completed, the curl_easy_cleanup() should be called to free up used
  169. resources. It runs Curl_disconnect() on all open connectons.
  170. A quick roundup on internal function sequences (many of these call
  171. protocol-specific function-pointers):
  172. curl_connect - connects to a remote site and does initial connect fluff
  173. This also checks for an existing connection to the requested site and uses
  174. that one if it is possible.
  175. curl_do - starts a transfer
  176. curl_transfer() - transfers data
  177. curl_done - ends a transfer
  178. curl_disconnect - disconnects from a remote site. This is called when the
  179. disconnect is really requested, which doesn't necessarily have to be
  180. exactly after curl_done in case we want to keep the connection open for
  181. a while.
  182. HTTP(S)
  183. HTTP offers a lot and is the protocol in curl that uses the most lines of
  184. code. There is a special file (lib/formdata.c) that offers all the multipart
  185. post functions.
  186. base64-functions for user+password stuff (and more) is in (lib/base64.c) and
  187. all functions for parsing and sending cookies are found in (lib/cookie.c).
  188. HTTPS uses in almost every means the same procedure as HTTP, with only two
  189. exceptions: the connect procedure is different and the function used to read
  190. or write from the socket is different, although the latter fact is hidden in
  191. the source by the use of curl_read() for reading and curl_write() for writing
  192. data to the remote server.
  193. http_chunks.c contains functions that understands HTTP 1.1 chunked transfer
  194. encoding.
  195. An interesting detail with the HTTP(S) request, is the add_buffer() series of
  196. functions we use. They append data to one single buffer, and when the
  197. building is done the entire request is sent off in one single write. This is
  198. done this way to overcome problems with flawed firewalls and lame servers.
  199. FTP
  200. The Curl_if2ip() function can be used for getting the IP number of a
  201. specified network interface, and it resides in lib/if2ip.c.
  202. Curl_ftpsendf() is used for sending FTP commands to the remote server. It was
  203. made a separate function to prevent us programmers from forgetting that they
  204. must be CRLF terminated. They must also be sent in one single write() to make
  205. firewalls and similar happy.
  206. Kerberos
  207. The kerberos support is mainly in lib/krb4.c and lib/security.c.
  208. TELNET
  209. Telnet is implemented in lib/telnet.c.
  210. FILE
  211. The file:// protocol is dealt with in lib/file.c.
  212. LDAP
  213. Everything LDAP is in lib/ldap.c.
  214. GENERAL
  215. URL encoding and decoding, called escaping and unescaping in the source code,
  216. is found in lib/escape.c.
  217. While transfering data in Transfer() a few functions might get used.
  218. curl_getdate() in lib/parsedate.c is for HTTP date comparisons (and more).
  219. lib/getenv.c offers curl_getenv() which is for reading environment variables
  220. in a neat platform independent way. That's used in the client, but also in
  221. lib/url.c when checking the proxy environment variables. Note that contrary
  222. to the normal unix getenv(), this returns an allocated buffer that must be
  223. free()ed after use.
  224. lib/netrc.c holds the .netrc parser
  225. lib/timeval.c features replacement functions for systems that don't have
  226. gettimeofday() and a few support functions for timeval convertions.
  227. A function named curl_version() that returns the full curl version string is
  228. found in lib/version.c.
  229. Persistent Connections
  230. ======================
  231. The persistent connection support in libcurl requires some considerations on
  232. how to do things inside of the library.
  233. o The 'SessionHandle' struct returned in the curl_easy_init() call must never
  234. hold connection-oriented data. It is meant to hold the root data as well as
  235. all the options etc that the library-user may choose.
  236. o The 'SessionHandle' struct holds the "connection cache" (an array of
  237. pointers to 'connectdata' structs). There's one connectdata struct
  238. allocated for each connection that libcurl knows about. Note that when you
  239. use the multi interface, the multi handle will hold the connection cache
  240. and not the particular easy handle. This of course to allow all easy handles
  241. in a multi stack to be able to share and re-use connections.
  242. o This enables the 'curl handle' to be reused on subsequent transfers.
  243. o When we are about to perform a transfer with curl_easy_perform(), we first
  244. check for an already existing connection in the cache that we can use,
  245. otherwise we create a new one and add to the cache. If the cache is full
  246. already when we add a new connection, we close one of the present ones. We
  247. select which one to close dependent on the close policy that may have been
  248. previously set.
  249. o When the transfer operation is complete, we try to leave the connection
  250. open. Particular options may tell us not to, and protocols may signal
  251. closure on connections and then we don't keep it open of course.
  252. o When curl_easy_cleanup() is called, we close all still opened connections,
  253. unless of course the multi interface "owns" the connections.
  254. You do realize that the curl handle must be re-used in order for the
  255. persistent connections to work.
  256. multi interface/non-blocking
  257. ============================
  258. We make an effort to provide a non-blocking interface to the library, the
  259. multi interface. To make that interface work as good as possible, no
  260. low-level functions within libcurl must be written to work in a blocking
  261. manner.
  262. One of the primary reasons we introduced c-ares support was to allow the name
  263. resolve phase to be perfectly non-blocking as well.
  264. The ultimate goal is to provide the easy interface simply by wrapping the
  265. multi interface functions and thus treat everything internally as the multi
  266. interface is the single interface we have.
  267. The FTP and the SFTP/SCP protocols are thus perfect examples of how we adapt
  268. and adjust the code to allow non-blocking operations even on multi-stage
  269. protocols. The DICT, TELNET and TFTP are crappy examples and they are subject
  270. for rewrite in the future to better fit the libcurl protocol family.
  271. SSL libraries
  272. =============
  273. Originally libcurl supported SSLeay for SSL/TLS transports, but that was then
  274. extended to its successor OpenSSL but has since also been extended to several
  275. other SSL/TLS libraries and we expect and hope to further extend the support
  276. in future libcurl versions.
  277. To deal with this internally in the best way possible, we have a generic SSL
  278. function API as provided by the sslgen.[ch] system, and they are the only SSL
  279. functions we must use from within libcurl. sslgen is then crafted to use the
  280. appropriate lower-level function calls to whatever SSL library that is in
  281. use.
  282. Library Symbols
  283. ===============
  284. All symbols used internally in libcurl must use a 'Curl_' prefix if they're
  285. used in more than a single file. Single-file symbols must be made static.
  286. Public ("exported") symbols must use a 'curl_' prefix. (There are exceptions,
  287. but they are to be changed to follow this pattern in future versions.)
  288. Return Codes and Informationals
  289. ===============================
  290. I've made things simple. Almost every function in libcurl returns a CURLcode,
  291. that must be CURLE_OK if everything is OK or otherwise a suitable error code
  292. as the curl/curl.h include file defines. The very spot that detects an error
  293. must use the Curl_failf() function to set the human-readable error
  294. description.
  295. In aiding the user to understand what's happening and to debug curl usage, we
  296. must supply a fair amount of informational messages by using the Curl_infof()
  297. function. Those messages are only displayed when the user explicitly asks for
  298. them. They are best used when revealing information that isn't otherwise
  299. obvious.
  300. API/ABI
  301. =======
  302. We make an effort to not export or show internals or how internals work, as
  303. that makes it easier to keep a solid API/ABI over time. See docs/libcurl/ABI
  304. for our promise to users.
  305. Client
  306. ======
  307. main() resides in src/main.c together with most of the client code.
  308. src/hugehelp.c is automatically generated by the mkhelp.pl perl script to
  309. display the complete "manual" and the src/urlglob.c file holds the functions
  310. used for the URL-"globbing" support. Globbing in the sense that the {} and []
  311. expansion stuff is there.
  312. The client mostly messes around to setup its 'config' struct properly, then
  313. it calls the curl_easy_*() functions of the library and when it gets back
  314. control after the curl_easy_perform() it cleans up the library, checks status
  315. and exits.
  316. When the operation is done, the ourWriteOut() function in src/writeout.c may
  317. be called to report about the operation. That function is using the
  318. curl_easy_getinfo() function to extract useful information from the curl
  319. session.
  320. Recent versions may loop and do all this several times if many URLs were
  321. specified on the command line or config file.
  322. Memory Debugging
  323. ================
  324. The file lib/memdebug.c contains debug-versions of a few functions. Functions
  325. such as malloc, free, fopen, fclose, etc that somehow deal with resources
  326. that might give us problems if we "leak" them. The functions in the memdebug
  327. system do nothing fancy, they do their normal function and then log
  328. information about what they just did. The logged data can then be analyzed
  329. after a complete session,
  330. memanalyze.pl is the perl script present only present in CVS (not part of the
  331. release archives) that analyzes a log file generated by the memory tracking
  332. system. It detects if resources are allocated but never freed and other kinds
  333. of errors related to resource management.
  334. Internally, definition of preprocessor symbol DEBUGBUILD restricts code which
  335. is only compiled for debug enabled builds. And symbol CURLDEBUG is used to
  336. differentiate code which is _only_ used for memory tracking/debugging.
  337. Use -DCURLDEBUG when compiling to enable memory debugging, this is also
  338. switched on by running configure with --enable-curldebug. Use -DDEBUGBUILD
  339. when compiling to enable a debug build or run configure with --enable-debug.
  340. curl --version will list 'Debug' feature for debug enabled builds, and
  341. will list 'TrackMemory' feature for curl debug memory tracking capable
  342. builds. These features are independent and can be controlled when running
  343. the configure script. When --enable-debug is given both features will be
  344. enabled, unless some restriction prevents memory tracking from being used.
  345. Test Suite
  346. ==========
  347. Since November 2000, a test suite has evolved. It is placed in its own
  348. subdirectory directly off the root in the curl archive tree, and it contains
  349. a bunch of scripts and a lot of test case data.
  350. The main test script is runtests.pl that will invoke the two servers
  351. httpserver.pl and ftpserver.pl before all the test cases are performed. The
  352. test suite currently only runs on unix-like platforms.
  353. You'll find a complete description of the test case data files in the
  354. tests/README file.
  355. The test suite automatically detects if curl was built with the memory
  356. debugging enabled, and if it was it will detect memory leaks too.
  357. Building Releases
  358. =================
  359. There's no magic to this. When you consider everything stable enough to be
  360. released, run the 'maketgz' script (using 'make distcheck' will give you a
  361. pretty good view on the status of the current sources). maketgz prompts for
  362. version number of the client and the library before it creates a release
  363. archive. maketgz uses 'make dist' for the actual archive building, why you
  364. need to fill in the Makefile.am files properly for which files that should
  365. be included in the release archives.
  366. NOTE: you need to have curl checked out from CVS to be able to do a proper
  367. release build. The release tarballs do not have everything setup in order to
  368. do releases properly.