|
| 1 | +## http.cookie |
| 2 | + |
| 3 | +A module for working with cookies. |
| 4 | + |
| 5 | +### `bake(name, value, expiry_time, domain, path, secure_only, http_only, same_site)` <!-- --> {#http.cookie.bake} |
| 6 | + |
| 7 | +Returns a string suitable for use in a `Set-Cookie` header with the passed parameters. |
| 8 | + |
| 9 | + |
| 10 | +### `parse_cookie(cookie)` <!-- --> {#http.cookie.parse_cookie} |
| 11 | + |
| 12 | +Parses the `Cookie` header contents `cookie`. |
| 13 | + |
| 14 | +Returns a table containing `name` and `value` pairs as strings. |
| 15 | + |
| 16 | + |
| 17 | +### `parse_setcookie(setcookie)` <!-- --> {#http.cookie.parse_setcookie} |
| 18 | + |
| 19 | +Parses the `Set-Cookie` header contents `setcookie`. |
| 20 | + |
| 21 | +Returns `name`, `value` and `params` where: |
| 22 | + |
| 23 | + - `name` is a string containing the cookie name |
| 24 | + - `value` is a string containing the cookie value |
| 25 | + - `params` is the a table where the keys are cookie attribute names and values are cookie attribute values |
| 26 | + |
| 27 | + |
| 28 | +### `new_store()` <!-- --> {#http.cookie.new_store} |
| 29 | + |
| 30 | +Creates a new cookie store. |
| 31 | + |
| 32 | +Cookies are unique for a tuple of domain, path and name; |
| 33 | +although multiple cookies with the same name may exist in a request due to overlapping paths or domains. |
| 34 | + |
| 35 | + |
| 36 | +### `store.psl` <!-- --> {#http.cookie.store.psl} |
| 37 | + |
| 38 | +A [lua-psl](https://github.com/daurnimator/lua-psl) object to use for checking against the Public Suffix List. |
| 39 | +Set the field to `false` to skip checking the suffix list. |
| 40 | + |
| 41 | +Defaults to the [latest](https://rockdaboot.github.io/libpsl/libpsl-Public-Suffix-List-functions.html#psl-latest) PSL on the system. If lua-psl is not installed then it will be `nil`. |
| 42 | + |
| 43 | + |
| 44 | +### `store.time()` <!-- --> {#http.cookie.store.time} |
| 45 | + |
| 46 | +A function used by the `store` to get the current time for expiries and such. |
| 47 | + |
| 48 | +Defaults to a function based on [`os.time`](https://www.lua.org/manual/5.3/manual.html#pdf-os.time). |
| 49 | + |
| 50 | + |
| 51 | +### `store.max_cookie_length` <!-- --> {#http.cookie.store.max_cookie_length} |
| 52 | + |
| 53 | +The default maximum cookie length for store methods such as `:lookup()`. |
| 54 | + |
| 55 | +Defaults to infinity (no maximum size). |
| 56 | + |
| 57 | + |
| 58 | +### `store:store(req_domain, req_path, req_is_http, req_is_secure, req_site_for_cookies, name, value, params)` <!-- --> {#http.cookie.store:store} |
| 59 | + |
| 60 | +Attempts to add a cookie to the `store`. |
| 61 | + |
| 62 | + - `req_domain` is the domain that the cookie was obtained from |
| 63 | + - `req_path` is the path that the cookie was obtained from |
| 64 | + - `req_is_http` is a boolean flag indicating if the cookie was obtained from a "non-HTTP" API |
| 65 | + - `req_is_secure` is a boolean flag indicating if the cookie was obtained from a "secure" protocol |
| 66 | + - `req_site_for_cookies` is a string containing the host that should be considered as the "site for cookies" (See [RFC 6265bis-02 Section 5.2](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-02#section-5.2)), can be `nil` if unknown. |
| 67 | + - `name` is a string containing the cookie name |
| 68 | + - `value` is a string containing the cookie value |
| 69 | + - `params` is the a table where the keys are cookie attribute names and values are cookie attribute values |
| 70 | + |
| 71 | +Returns a boolean indicating if a cookie was stored. |
| 72 | + |
| 73 | + |
| 74 | +### `store:store_from_request(req_headers, resp_headers, req_host, req_site_for_cookies)` <!-- --> {#http.cookie.store:store_from_request} |
| 75 | + |
| 76 | +Attempt to store any cookies found in the response headers. |
| 77 | + |
| 78 | + - `req_headers` is the [*http.headers*](#http.headers) object for the outgoing request |
| 79 | + - `resp_headers` is the [*http.headers*](#http.headers) object received in response |
| 80 | + - `req_host` is the host that your query was directed at (only used if `req_headers` is missing a `Host` header) |
| 81 | + - `req_site_for_cookies` is a string containing the host that should be considered as the "site for cookies" (See [RFC 6265bis-02 Section 5.2](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-02#section-5.2)), can be `nil` if unknown. |
| 82 | + |
| 83 | + |
| 84 | +### `store:get(domain, path, name)` <!-- --> {#http.cookie.store:get} |
| 85 | + |
| 86 | +Returns the cookie value for the cookie stored for the passed `domain`, `path` and `name`. |
| 87 | + |
| 88 | + |
| 89 | +### `store:remove(domain, path, name)` <!-- --> {#http.cookie.store:remove} |
| 90 | + |
| 91 | +Deletes the cookie stored for the passed `domain`, `path` and `name`. |
| 92 | + |
| 93 | +If `name` is `nil` or not passed then all cookies for the `domain` and `path` are removed. |
| 94 | + |
| 95 | +If `path` is `nil` or not passed (in addition to `name`) then all cookies for the `domain` are removed. |
| 96 | + |
| 97 | + |
| 98 | +### `store:lookup(domain, path, is_http, is_secure, is_safe_method, site_for_cookies, is_top_level, max_cookie_length)` <!-- --> {#http.cookie.store:lookup} |
| 99 | + |
| 100 | +Finds cookies visible to suitable for passing to an entity. |
| 101 | + |
| 102 | + - `domain` is the domain that will be sent the cookie |
| 103 | + - `path` is the path that will be sent the cookie |
| 104 | + - `is_http` is a boolean flag indicating if the destination is a "non-HTTP" API |
| 105 | + - `is_secure` is a boolean flag indicating if the destination will be communicated with over a "secure" protocol |
| 106 | + - `is_safe_method` is a boolean flag indicating if the cookie will be sent via a safe HTTP method (See also [http.util.is_safe_method](#http.util.is_safe_method)) |
| 107 | + - `site_for_cookies` is a string containing the host that should be considered as the "site for cookies" (See [RFC 6265bis-02 Section 5.2](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-02#section-5.2)), can be `nil` if unknown. |
| 108 | + - `is_top_level` is a boolean flag indicating if this request is a "top level" request (See [RFC 6265bis-02 Section 5.2](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-02#section-5.2)) |
| 109 | + - `max_cookie_length` is the maximum cookie length to allow (See also [`store.max_cookie_length`](#http.cookie.store.max_cookie_length)) |
| 110 | + |
| 111 | +Returns a string suitable for use in a `Cookie` header. |
| 112 | + |
| 113 | + |
| 114 | +### `store:lookup_for_request(headers, host, site_for_cookies, is_top_level, max_cookie_length)` <!-- --> {#http.cookie.store:lookup_for_request} |
| 115 | + |
| 116 | +Finds cookies suitable for adding to a request. |
| 117 | + |
| 118 | + - `headers` is the [*http.headers*](#http.headers) object for the outgoing request |
| 119 | + - `host` is the host that your query was directed at (only used if `headers` is missing a `Host` header) |
| 120 | + - `site_for_cookies` is a string containing the host that should be considered as the "site for cookies" (See [RFC 6265bis-02 Section 5.2](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-02#section-5.2)), can be `nil` if unknown. |
| 121 | + - `is_top_level` is a boolean flag indicating if this request is a "top level" request (See [RFC 6265bis-02 Section 5.2](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-02#section-5.2)) |
| 122 | + - `max_cookie_length` is the maximum cookie length to allow (See also [`store.max_cookie_length`](#http.cookie.store.max_cookie_length)) |
| 123 | + |
| 124 | +Returns a string suitable for use in a `Cookie` header. |
| 125 | + |
| 126 | + |
| 127 | +### `store:clean_due()` <!-- --> {#http.cookie.store:clean_due} |
| 128 | + |
| 129 | +Returns the number of seconds until the next cookie in the `store` expires. |
| 130 | + |
| 131 | + |
| 132 | +### `store:clean()` <!-- --> {#http.cookie.store:clean} |
| 133 | + |
| 134 | +Remove all expired cookies from the `store`. |
| 135 | + |
| 136 | + |
| 137 | +### `store:load_from_file(file)` <!-- --> {#http.cookie.store:load_from_file} |
| 138 | + |
| 139 | +Loads cookie data from the file object `file` into `store`. |
| 140 | +The file should be in the Netscape Cookiejar format. |
| 141 | +Invalid lines in the file are ignored. |
| 142 | + |
| 143 | +Returns `true` on success or passes along `nil, err, errno` if a `:read` call fails. |
| 144 | + |
| 145 | + |
| 146 | +### `store:save_to_file(file)` <!-- --> {#http.cookie.store:save_to_file} |
| 147 | + |
| 148 | +Writes the cookie data from `store` into the file object `file` in the Netscape Cookiejar format. |
| 149 | +`file` is not `seek`-ed or truncated before writing. |
| 150 | + |
| 151 | +Returns `true` on success or passes along `nil, err, errno` if a `:write` call fails. |
0 commit comments