1*6236dae4SAndroid Build Coastguard Worker<!-- 2*6236dae4SAndroid Build Coastguard WorkerCopyright (C) Daniel Stenberg, <[email protected]>, et al. 3*6236dae4SAndroid Build Coastguard Worker 4*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl 5*6236dae4SAndroid Build Coastguard Worker--> 6*6236dae4SAndroid Build Coastguard Worker 7*6236dae4SAndroid Build Coastguard Worker# HTTP Cookies 8*6236dae4SAndroid Build Coastguard Worker 9*6236dae4SAndroid Build Coastguard Worker## Cookie overview 10*6236dae4SAndroid Build Coastguard Worker 11*6236dae4SAndroid Build Coastguard Worker Cookies are `name=contents` pairs that an HTTP server tells the client to 12*6236dae4SAndroid Build Coastguard Worker hold and then the client sends back those to the server on subsequent 13*6236dae4SAndroid Build Coastguard Worker requests to the same domains and paths for which the cookies were set. 14*6236dae4SAndroid Build Coastguard Worker 15*6236dae4SAndroid Build Coastguard Worker Cookies are either "session cookies" which typically are forgotten when the 16*6236dae4SAndroid Build Coastguard Worker session is over which is often translated to equal when browser quits, or 17*6236dae4SAndroid Build Coastguard Worker the cookies are not session cookies they have expiration dates after which 18*6236dae4SAndroid Build Coastguard Worker the client throws them away. 19*6236dae4SAndroid Build Coastguard Worker 20*6236dae4SAndroid Build Coastguard Worker Cookies are set to the client with the Set-Cookie: header and are sent to 21*6236dae4SAndroid Build Coastguard Worker servers with the Cookie: header. 22*6236dae4SAndroid Build Coastguard Worker 23*6236dae4SAndroid Build Coastguard Worker For a long time, the only spec explaining how to use cookies was the 24*6236dae4SAndroid Build Coastguard Worker original [Netscape spec from 1994](https://curl.se/rfc/cookie_spec.html). 25*6236dae4SAndroid Build Coastguard Worker 26*6236dae4SAndroid Build Coastguard Worker In 2011, [RFC 6265](https://www.ietf.org/rfc/rfc6265.txt) was finally 27*6236dae4SAndroid Build Coastguard Worker published and details how cookies work within HTTP. In 2016, an update which 28*6236dae4SAndroid Build Coastguard Worker added support for prefixes was 29*6236dae4SAndroid Build Coastguard Worker [proposed](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-prefixes-00), 30*6236dae4SAndroid Build Coastguard Worker and in 2017, another update was 31*6236dae4SAndroid Build Coastguard Worker [drafted](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-alone-01) 32*6236dae4SAndroid Build Coastguard Worker to deprecate modification of 'secure' cookies from non-secure origins. Both 33*6236dae4SAndroid Build Coastguard Worker of these drafts have been incorporated into a proposal to 34*6236dae4SAndroid Build Coastguard Worker [replace](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-11) 35*6236dae4SAndroid Build Coastguard Worker RFC 6265. Cookie prefixes and secure cookie modification protection has been 36*6236dae4SAndroid Build Coastguard Worker implemented by curl. 37*6236dae4SAndroid Build Coastguard Worker 38*6236dae4SAndroid Build Coastguard Worker curl considers `http://localhost` to be a *secure context*, meaning that it 39*6236dae4SAndroid Build Coastguard Worker allows and uses cookies marked with the `secure` keyword even when done over 40*6236dae4SAndroid Build Coastguard Worker plain HTTP for this host. curl does this to match how popular browsers work 41*6236dae4SAndroid Build Coastguard Worker with secure cookies. 42*6236dae4SAndroid Build Coastguard Worker 43*6236dae4SAndroid Build Coastguard Worker## Super cookies 44*6236dae4SAndroid Build Coastguard Worker 45*6236dae4SAndroid Build Coastguard Worker A single cookie can be set for a domain that matches multiple hosts. Like if 46*6236dae4SAndroid Build Coastguard Worker set for `example.com` it gets sent to both `aa.example.com` as well as 47*6236dae4SAndroid Build Coastguard Worker `bb.example.com`. 48*6236dae4SAndroid Build Coastguard Worker 49*6236dae4SAndroid Build Coastguard Worker A challenge with this concept is that there are certain domains for which 50*6236dae4SAndroid Build Coastguard Worker cookies should not be allowed at all, because they are *Public 51*6236dae4SAndroid Build Coastguard Worker Suffixes*. Similarly, a client never accepts cookies set directly for the 52*6236dae4SAndroid Build Coastguard Worker top-level domain like for example `.com`. Cookies set for *too broad* 53*6236dae4SAndroid Build Coastguard Worker domains are generally referred to as *super cookies*. 54*6236dae4SAndroid Build Coastguard Worker 55*6236dae4SAndroid Build Coastguard Worker If curl is built with PSL (**Public Suffix List**) support, it detects and 56*6236dae4SAndroid Build Coastguard Worker discards cookies that are specified for such suffix domains that should not 57*6236dae4SAndroid Build Coastguard Worker be allowed to have cookies. 58*6236dae4SAndroid Build Coastguard Worker 59*6236dae4SAndroid Build Coastguard Worker if curl is *not* built with PSL support, it has no ability to stop super 60*6236dae4SAndroid Build Coastguard Worker cookies. 61*6236dae4SAndroid Build Coastguard Worker 62*6236dae4SAndroid Build Coastguard Worker## Cookies saved to disk 63*6236dae4SAndroid Build Coastguard Worker 64*6236dae4SAndroid Build Coastguard Worker Netscape once created a file format for storing cookies on disk so that they 65*6236dae4SAndroid Build Coastguard Worker would survive browser restarts. curl adopted that file format to allow 66*6236dae4SAndroid Build Coastguard Worker sharing the cookies with browsers, only to see browsers move away from that 67*6236dae4SAndroid Build Coastguard Worker format. Modern browsers no longer use it, while curl still does. 68*6236dae4SAndroid Build Coastguard Worker 69*6236dae4SAndroid Build Coastguard Worker The Netscape cookie file format stores one cookie per physical line in the 70*6236dae4SAndroid Build Coastguard Worker file with a bunch of associated meta data, each field separated with 71*6236dae4SAndroid Build Coastguard Worker TAB. That file is called the cookie jar in curl terminology. 72*6236dae4SAndroid Build Coastguard Worker 73*6236dae4SAndroid Build Coastguard Worker When libcurl saves a cookie jar, it creates a file header of its own in 74*6236dae4SAndroid Build Coastguard Worker which there is a URL mention that links to the web version of this document. 75*6236dae4SAndroid Build Coastguard Worker 76*6236dae4SAndroid Build Coastguard Worker## Cookie file format 77*6236dae4SAndroid Build Coastguard Worker 78*6236dae4SAndroid Build Coastguard Worker The cookie file format is text based and stores one cookie per line. Lines 79*6236dae4SAndroid Build Coastguard Worker that start with `#` are treated as comments. An exception is lines that 80*6236dae4SAndroid Build Coastguard Worker start with `#HttpOnly_`, which is a prefix for cookies that have the 81*6236dae4SAndroid Build Coastguard Worker `HttpOnly` attribute set. 82*6236dae4SAndroid Build Coastguard Worker 83*6236dae4SAndroid Build Coastguard Worker Each line that specifies a single cookie consists of seven text fields 84*6236dae4SAndroid Build Coastguard Worker separated with TAB characters. A valid line must end with a newline 85*6236dae4SAndroid Build Coastguard Worker character. 86*6236dae4SAndroid Build Coastguard Worker 87*6236dae4SAndroid Build Coastguard Worker### Fields in the file 88*6236dae4SAndroid Build Coastguard Worker 89*6236dae4SAndroid Build Coastguard Worker Field number, what type and example data and the meaning of it: 90*6236dae4SAndroid Build Coastguard Worker 91*6236dae4SAndroid Build Coastguard Worker 0. string `example.com` - the domain name 92*6236dae4SAndroid Build Coastguard Worker 1. boolean `FALSE` - include subdomains 93*6236dae4SAndroid Build Coastguard Worker 2. string `/foobar/` - path 94*6236dae4SAndroid Build Coastguard Worker 3. boolean `TRUE` - send/receive over HTTPS only 95*6236dae4SAndroid Build Coastguard Worker 4. number `1462299217` - expires at - seconds since Jan 1st 1970, or 0 96*6236dae4SAndroid Build Coastguard Worker 5. string `person` - name of the cookie 97*6236dae4SAndroid Build Coastguard Worker 6. string `daniel` - value of the cookie 98*6236dae4SAndroid Build Coastguard Worker 99*6236dae4SAndroid Build Coastguard Worker## Cookies with curl the command line tool 100*6236dae4SAndroid Build Coastguard Worker 101*6236dae4SAndroid Build Coastguard Worker curl has a full cookie "engine" built in. If you just activate it, you can 102*6236dae4SAndroid Build Coastguard Worker have curl receive and send cookies exactly as mandated in the specs. 103*6236dae4SAndroid Build Coastguard Worker 104*6236dae4SAndroid Build Coastguard Worker Command line options: 105*6236dae4SAndroid Build Coastguard Worker 106*6236dae4SAndroid Build Coastguard Worker `-b, --cookie` 107*6236dae4SAndroid Build Coastguard Worker 108*6236dae4SAndroid Build Coastguard Worker tell curl a file to read cookies from and start the cookie engine, or if it 109*6236dae4SAndroid Build Coastguard Worker is not a file it passes on the given string. `-b name=var` works and so does 110*6236dae4SAndroid Build Coastguard Worker `-b cookiefile`. 111*6236dae4SAndroid Build Coastguard Worker 112*6236dae4SAndroid Build Coastguard Worker `-j, --junk-session-cookies` 113*6236dae4SAndroid Build Coastguard Worker 114*6236dae4SAndroid Build Coastguard Worker when used in combination with -b, it skips all "session cookies" on load so 115*6236dae4SAndroid Build Coastguard Worker as to appear to start a new cookie session. 116*6236dae4SAndroid Build Coastguard Worker 117*6236dae4SAndroid Build Coastguard Worker `-c, --cookie-jar` 118*6236dae4SAndroid Build Coastguard Worker 119*6236dae4SAndroid Build Coastguard Worker tell curl to start the cookie engine and write cookies to the given file 120*6236dae4SAndroid Build Coastguard Worker after the request(s) 121*6236dae4SAndroid Build Coastguard Worker 122*6236dae4SAndroid Build Coastguard Worker## Cookies with libcurl 123*6236dae4SAndroid Build Coastguard Worker 124*6236dae4SAndroid Build Coastguard Worker libcurl offers several ways to enable and interface the cookie engine. These 125*6236dae4SAndroid Build Coastguard Worker options are the ones provided by the native API. libcurl bindings may offer 126*6236dae4SAndroid Build Coastguard Worker access to them using other means. 127*6236dae4SAndroid Build Coastguard Worker 128*6236dae4SAndroid Build Coastguard Worker `CURLOPT_COOKIE` 129*6236dae4SAndroid Build Coastguard Worker 130*6236dae4SAndroid Build Coastguard Worker Is used when you want to specify the exact contents of a cookie header to 131*6236dae4SAndroid Build Coastguard Worker send to the server. 132*6236dae4SAndroid Build Coastguard Worker 133*6236dae4SAndroid Build Coastguard Worker `CURLOPT_COOKIEFILE` 134*6236dae4SAndroid Build Coastguard Worker 135*6236dae4SAndroid Build Coastguard Worker Tell libcurl to activate the cookie engine, and to read the initial set of 136*6236dae4SAndroid Build Coastguard Worker cookies from the given file. Read-only. 137*6236dae4SAndroid Build Coastguard Worker 138*6236dae4SAndroid Build Coastguard Worker `CURLOPT_COOKIEJAR` 139*6236dae4SAndroid Build Coastguard Worker 140*6236dae4SAndroid Build Coastguard Worker Tell libcurl to activate the cookie engine, and when the easy handle is 141*6236dae4SAndroid Build Coastguard Worker closed save all known cookies to the given cookie jar file. Write-only. 142*6236dae4SAndroid Build Coastguard Worker 143*6236dae4SAndroid Build Coastguard Worker `CURLOPT_COOKIELIST` 144*6236dae4SAndroid Build Coastguard Worker 145*6236dae4SAndroid Build Coastguard Worker Provide detailed information about a single cookie to add to the internal 146*6236dae4SAndroid Build Coastguard Worker storage of cookies. Pass in the cookie as an HTTP header with all the 147*6236dae4SAndroid Build Coastguard Worker details set, or pass in a line from a Netscape cookie file. This option can 148*6236dae4SAndroid Build Coastguard Worker also be used to flush the cookies etc. 149*6236dae4SAndroid Build Coastguard Worker 150*6236dae4SAndroid Build Coastguard Worker `CURLOPT_COOKIESESSION` 151*6236dae4SAndroid Build Coastguard Worker 152*6236dae4SAndroid Build Coastguard Worker Tell libcurl to ignore all cookies it is about to load that are session 153*6236dae4SAndroid Build Coastguard Worker cookies. 154*6236dae4SAndroid Build Coastguard Worker 155*6236dae4SAndroid Build Coastguard Worker `CURLINFO_COOKIELIST` 156*6236dae4SAndroid Build Coastguard Worker 157*6236dae4SAndroid Build Coastguard Worker Extract cookie information from the internal cookie storage as a linked 158*6236dae4SAndroid Build Coastguard Worker list. 159*6236dae4SAndroid Build Coastguard Worker 160*6236dae4SAndroid Build Coastguard Worker## Cookies with JavaScript 161*6236dae4SAndroid Build Coastguard Worker 162*6236dae4SAndroid Build Coastguard Worker These days a lot of the web is built up by JavaScript. The web browser loads 163*6236dae4SAndroid Build Coastguard Worker complete programs that render the page you see. These JavaScript programs 164*6236dae4SAndroid Build Coastguard Worker can also set and access cookies. 165*6236dae4SAndroid Build Coastguard Worker 166*6236dae4SAndroid Build Coastguard Worker Since curl and libcurl are plain HTTP clients without any knowledge of or 167*6236dae4SAndroid Build Coastguard Worker capability to handle JavaScript, such cookies are not detected or used. 168*6236dae4SAndroid Build Coastguard Worker 169*6236dae4SAndroid Build Coastguard Worker Often, if you want to mimic what a browser does on such websites, you can 170*6236dae4SAndroid Build Coastguard Worker record web browser HTTP traffic when using such a site and then repeat the 171*6236dae4SAndroid Build Coastguard Worker cookie operations using curl or libcurl. 172