@@ -499,8 +499,8 @@ fn parse_cookie_list(cookie_string) {
499
499
case string.split_once(string.trim(pair), "=") {
500
500
Ok(tuple("", _)) -> Error(Nil)
501
501
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)
504
504
try _ = check_token(bit_string.from_string(key))
505
505
try _ = check_token(bit_string.from_string(value))
506
506
Ok(tuple(key, value))
@@ -512,14 +512,16 @@ fn parse_cookie_list(cookie_string) {
512
512
}
513
513
514
514
/// Fetch the cookies sent in a request.
515
+ ///
516
+ /// Note badly formed cookie pairs will be ignored.
515
517
pub fn get_req_cookies(req) -> List(tuple(String, String)) {
516
518
let Request(headers: headers, ..) = req
517
519
518
520
headers
519
521
|> list.filter_map(
520
522
fn(header) {
521
- let tuple(key , value) = header
522
- case key {
523
+ let tuple(name , value) = header
524
+ case name {
523
525
"cookie" -> Ok(parse_cookie_list(value))
524
526
_ -> Error(Nil)
525
527
}
@@ -528,7 +530,10 @@ pub fn get_req_cookies(req) -> List(tuple(String, String)) {
528
530
|> list.flatten()
529
531
}
530
532
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) {
532
537
let Request(
533
538
method: method,
534
539
headers: headers,
@@ -539,7 +544,7 @@ pub fn set_req_cookie(req, key, value) {
539
544
path: path,
540
545
query: query,
541
546
) = req
542
- let new_cookie_string = string.join([key , value], "=")
547
+ let new_cookie_string = string.join([name , value], "=")
543
548
544
549
let tuple(cookies_string, headers) = case list.key_pop(headers, "cookie") {
545
550
Ok(tuple(cookies_string, headers)) -> {
@@ -564,14 +569,20 @@ pub fn set_req_cookie(req, key, value) {
564
569
)
565
570
}
566
571
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) {
568
576
prepend_resp_header(
569
577
resp,
570
578
"set-cookie",
571
- cookie.set_cookie_string(key , value, attributes),
579
+ cookie.set_cookie_string(name , value, attributes),
572
580
)
573
581
}
574
582
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 ) )
577
588
}
0 commit comments