HTTP-COOKIES 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. Updated: July 3, 2012 (http://curl.haxx.se/docs/http-cookies.html)
  2. _ _ ____ _
  3. ___| | | | _ \| |
  4. / __| | | | |_) | |
  5. | (__| |_| | _ <| |___
  6. \___|\___/|_| \_\_____|
  7. HTTP Cookies
  8. 1. HTTP Cookies
  9. 1.1 Cookie overview
  10. 1.2 Cookies saved to disk
  11. 1.3 Cookies with curl the command line tool
  12. 1.4 Cookies with libcurl
  13. 1.5 Cookies with javascript
  14. ==============================================================================
  15. 1. HTTP Cookies
  16. 1.1 Cookie overview
  17. HTTP cookies are pieces of 'name=contents' snippets that a server tells the
  18. client to hold and then the client sends back those the server on subsequent
  19. requests to the same domains/paths for which the cookies were set.
  20. Cookies are either "session cookies" which typically are forgotten when the
  21. session is over which is often translated to equal when browser quits, or
  22. the cookies aren't session cookies they have expiration dates after which
  23. the client will throw them away.
  24. Cookies are set to the client with the Set-Cookie: header and are sent to
  25. servers with the Cookie: header.
  26. For a very long time, the only spec explaining how to use cookies was the
  27. original Netscape spec from 1994: http://curl.haxx.se/rfc/cookie_spec.html
  28. In 2011, RFC6265 (http://www.ietf.org/rfc/rfc6265.txt) was finally published
  29. and details how cookies work within HTTP.
  30. 1.2 Cookies saved to disk
  31. Netscape once created a file format for storing cookies on disk so that they
  32. would survive browser restarts. curl adopted that file format to allow
  33. sharing the cookies with browsers, only to see browsers move away from that
  34. format. Modern browsers no longer use it, while curl still does.
  35. The netscape cookie file format stores one cookie per physical line in the
  36. file with a bunch of associated meta data, each field separated with
  37. TAB. That file is called the cookiejar in curl terminology.
  38. When libcurl saves a cookiejar, it creates a file header of its own in which
  39. there is a URL mention that will link to the web version of this document.
  40. 1.3 Cookies with curl the command line tool
  41. curl has a full cookie "engine" built in. If you just activate it, you can
  42. have curl receive and send cookies exactly as mandated in the specs.
  43. Command line options:
  44. -b, --cookie
  45. tell curl a file to read cookies from and start the cookie engine, or if
  46. it isn't a file it will pass on the given string. -b name=var works and so
  47. does -b cookiefile.
  48. -j, --junk-session-cookies
  49. when used in combination with -b, it will skip all "session cookies" on
  50. load so as to appear to start a new cookie session.
  51. -c, --cookie-jar
  52. tell curl to start the cookie engine and write cookies to the given file
  53. after the request(s)
  54. 1.4 Cookies with libcurl
  55. libcurl offers several ways to enable and interface the cookie engine. These
  56. options are the ones provided by the native API. libcurl bindings may offer
  57. access to them using other means.
  58. CURLOPT_COOKIE
  59. Is used when you want to specify the exact contents of a cookie header to
  60. send to the server.
  61. CURLOPT_COOKIEFILE
  62. Tell libcurl to activate the cookie engine, and to read the initial set of
  63. cookies from the given file. Read-only.
  64. CURLOPT_COOKIEJAR
  65. Tell libcurl to activate the cookie engine, and when the easy handle is
  66. closed save all known cookies to the given cookiejar file. Write-only.
  67. CURLOPT_COOKIELIST
  68. Provide detailed information about a single cookie to add to the internal
  69. storage of cookies. Pass in the cookie as a HTTP header with all the
  70. details set, or pass in a line from a netscape cookie file. This option
  71. can also be used to flush the cookies etc.
  72. CURLINFO_COOKIELIST
  73. Extract cookie information from the internal cookie storage as a linked
  74. list.
  75. 1.5 Cookies with javascript
  76. These days a lot of the web is built up by javascript. The webbrowser loads
  77. complete programs that render the page you see. These javascript programs
  78. can also set and access cookies.
  79. Since curl and libcurl are plain HTTP clients without any knowledge of or
  80. capability to handle javascript, such cookies will not be detected or used.
  81. Often, if you want to mimic what a browser does on such web sites, you can
  82. record web browser HTTP traffic when using such a site and then repeat the
  83. cookie operations using curl or libcurl.