Skip to content

Commit 8b8520b

Browse files
CrowdHailerlpil
authored andcommitted
add documentation
1 parent 390a2b8 commit 8b8520b

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

src/gleam/http.gleam

+21-10
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,8 @@ fn parse_cookie_list(cookie_string) {
499499
case string.split_once(string.trim(pair), "=") {
500500
Ok(tuple("", _)) -> Error(Nil)
501501
Ok(tuple(key, value)) -> {
502-
let key = string.trim(key)
503-
let value = string.trim(value)
502+
let key = string.trim(key)
503+
let value = string.trim(value)
504504
try _ = check_token(bit_string.from_string(key))
505505
try _ = check_token(bit_string.from_string(value))
506506
Ok(tuple(key, value))
@@ -512,14 +512,16 @@ fn parse_cookie_list(cookie_string) {
512512
}
513513

514514
/// Fetch the cookies sent in a request.
515+
///
516+
/// Note badly formed cookie pairs will be ignored.
515517
pub fn get_req_cookies(req) -> List(tuple(String, String)) {
516518
let Request(headers: headers, ..) = req
517519

518520
headers
519521
|> list.filter_map(
520522
fn(header) {
521-
let tuple(key, value) = header
522-
case key {
523+
let tuple(name, value) = header
524+
case name {
523525
"cookie" -> Ok(parse_cookie_list(value))
524526
_ -> Error(Nil)
525527
}
@@ -528,7 +530,10 @@ pub fn get_req_cookies(req) -> List(tuple(String, String)) {
528530
|> list.flatten()
529531
}
530532

531-
pub fn set_req_cookie(req, key, value) {
533+
/// Send a cookie with a request
534+
///
535+
/// Multiple cookies are added to the same cookie header.
536+
pub fn set_req_cookie(req, name, value) {
532537
let Request(
533538
method: method,
534539
headers: headers,
@@ -539,7 +544,7 @@ pub fn set_req_cookie(req, key, value) {
539544
path: path,
540545
query: query,
541546
) = req
542-
let new_cookie_string = string.join([key, value], "=")
547+
let new_cookie_string = string.join([name, value], "=")
543548

544549
let tuple(cookies_string, headers) = case list.key_pop(headers, "cookie") {
545550
Ok(tuple(cookies_string, headers)) -> {
@@ -564,14 +569,20 @@ pub fn set_req_cookie(req, key, value) {
564569
)
565570
}
566571

567-
pub fn set_resp_cookie(resp, key, value, attributes) {
572+
/// Set a cookie value for a client
573+
///
574+
/// The attributes record is defined in `gleam/http/cookie`
575+
pub fn set_resp_cookie(resp, name, value, attributes) {
568576
prepend_resp_header(
569577
resp,
570578
"set-cookie",
571-
cookie.set_cookie_string(key, value, attributes),
579+
cookie.set_cookie_string(name, value, attributes),
572580
)
573581
}
574582

575-
pub fn expire_resp_cookie(resp, key, attributes) {
576-
set_resp_cookie(resp, key, "", cookie.expire_attributes(attributes))
583+
/// Expire a cookie value for a client
584+
///
585+
/// Not the attributes value should be the same as when the response cookie was set.
586+
pub fn expire_resp_cookie(resp, name, attributes) {
587+
set_resp_cookie(resp, name, "", cookie.expire_attributes(attributes))
577588
}

src/gleam/http/cookie.gleam

+15-5
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,30 @@ import gleam/list
33
import gleam/option.{Option, Some}
44
import gleam/string
55

6-
const epoch = "aa"
6+
const epoch = "Expires=Thu, 01 Jan 1970 00:00:00 GMT"
77

8+
/// Policy options for the SameSite cookie attribute
9+
///
10+
/// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
811
pub type SameSitePolicy {
912
Lax
1013
Strict
1114
None
1215
}
1316

17+
/// Attributes of a cookie when sent to a client in the `set-cookie` header.
1418
pub type Attributes {
1519
Attributes(
1620
max_age: Option(Int),
1721
domain: Option(String),
1822
path: Option(String),
1923
secure: Bool,
2024
http_only: Bool,
21-
// TODO I don't think it needs Option
2225
same_site: Option(SameSitePolicy),
2326
)
2427
}
2528

29+
/// Helper to create empty `Attributes` for a cookie.
2630
pub fn empty_attributes() {
2731
Attributes(
2832
max_age: option.None,
@@ -34,6 +38,7 @@ pub fn empty_attributes() {
3438
)
3539
}
3640

41+
/// Helper to create sensible default attributes for a set cookie.
3742
pub fn default_attributes() {
3843
Attributes(
3944
max_age: option.None,
@@ -53,6 +58,8 @@ fn same_site_to_string(policy) {
5358
}
5459
}
5560

61+
/// Update the MaxAge of a set of attributes to 0.
62+
/// This informes the client that the cookie should be expired.
5663
pub fn expire_attributes(attributes) {
5764
let Attributes(
5865
max_age: _max_age,
@@ -87,7 +94,7 @@ fn attributes_to_list(attributes) {
8794
// Only when deleting cookies is the exception made to use the old format,
8895
// to ensure complete clearup of cookies if required by an application.
8996
case max_age {
90-
Some(0) -> Some(["Expires=Thu, 01 Jan 1970 00:00:00 GMT"])
97+
Some(0) -> Some([epoch])
9198
_ -> option.None
9299
},
93100
option.map(max_age, fn(max_age) { ["MaxAge=", int.to_string(max_age)] }),
@@ -109,8 +116,11 @@ fn attributes_to_list(attributes) {
109116
|> list.filter_map(option.to_result(_, Nil))
110117
}
111118

112-
pub fn set_cookie_string(key, value, attributes) {
113-
[[key, "=", value], ..attributes_to_list(attributes)]
119+
/// Serialize a cookie and attributes.
120+
///
121+
/// This is the serialization of a cookie for the `set-cookie` header
122+
pub fn set_cookie_string(name, value, attributes) {
123+
[[name, "=", value], ..attributes_to_list(attributes)]
114124
|> list.map(string.join(_, ""))
115125
|> string.join("; ")
116126
}

test/gleam/http_test.gleam

+1-1
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,6 @@ pub fn expire_resp_cookie_test() {
859859
|> http.expire_resp_cookie("k1", cookie.default_attributes())
860860
|> http.get_resp_header("set-cookie")
861861
|> should.equal(
862-
Ok("k1=; expires=Thu, 01 Jan 1970 00:00:00 GMT; MaxAge=0; Path=/; HttpOnly"),
862+
Ok("k1=; Expires=Thu, 01 Jan 1970 00:00:00 GMT; MaxAge=0; Path=/; HttpOnly"),
863863
)
864864
}

0 commit comments

Comments
 (0)