Skip to content

Commit 390a2b8

Browse files
CrowdHailerlpil
authored andcommitted
handle spaces within av pair
1 parent d09c183 commit 390a2b8

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

src/gleam/http.gleam

+3-1
Original file line numberDiff line numberDiff line change
@@ -499,6 +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)
502504
try _ = check_token(bit_string.from_string(key))
503505
try _ = check_token(bit_string.from_string(value))
504506
Ok(tuple(key, value))
@@ -571,5 +573,5 @@ pub fn set_resp_cookie(resp, key, value, attributes) {
571573
}
572574

573575
pub fn expire_resp_cookie(resp, key, attributes) {
574-
set_resp_cookie(resp, key, "", cookie.expire_attributes(attributes))
576+
set_resp_cookie(resp, key, "", cookie.expire_attributes(attributes))
575577
}

src/gleam/http/cookie.gleam

+28-28
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub type SameSitePolicy {
1313

1414
pub type Attributes {
1515
Attributes(
16-
// Expires is deprecated, we can still serialize a value for max compatibility
1716
max_age: Option(Int),
1817
domain: Option(String),
1918
path: Option(String),
@@ -47,34 +46,33 @@ pub fn default_attributes() {
4746
}
4847

4948
fn same_site_to_string(policy) {
50-
case policy {
51-
Lax -> "Lax"
52-
Strict -> "Strict"
53-
None -> "None"
54-
}
49+
case policy {
50+
Lax -> "Lax"
51+
Strict -> "Strict"
52+
None -> "None"
53+
}
5554
}
5655

5756
pub fn expire_attributes(attributes) {
58-
let Attributes(
59-
max_age: _max_age,
60-
domain: domain,
61-
path: path,
62-
secure: secure,
63-
http_only: http_only,
64-
same_site: same_site,
65-
) = attributes
66-
Attributes(
67-
max_age: Some(0),
68-
domain: domain,
69-
path: path,
70-
secure: secure,
71-
http_only: http_only,
72-
same_site: same_site,
73-
)
57+
let Attributes(
58+
max_age: _max_age,
59+
domain: domain,
60+
path: path,
61+
secure: secure,
62+
http_only: http_only,
63+
same_site: same_site,
64+
) = attributes
65+
Attributes(
66+
max_age: Some(0),
67+
domain: domain,
68+
path: path,
69+
secure: secure,
70+
http_only: http_only,
71+
same_site: same_site,
72+
)
7473
}
7574

76-
77-
pub fn attributes_to_list(attributes) {
75+
fn attributes_to_list(attributes) {
7876
let Attributes(
7977
max_age: max_age,
8078
domain: domain,
@@ -84,9 +82,13 @@ pub fn attributes_to_list(attributes) {
8482
same_site: same_site,
8583
) = attributes
8684
[
85+
// Expires is a deprecated attribute for cookies, it has been replaced with MaxAge
86+
// MaxAge is widely supported and so Expires values are not set.
87+
// Only when deleting cookies is the exception made to use the old format,
88+
// to ensure complete clearup of cookies if required by an application.
8789
case max_age {
88-
Some(0) -> Some(["expires=Thu, 01 Jan 1970 00:00:00 GMT"])
89-
_ -> option.None
90+
Some(0) -> Some(["Expires=Thu, 01 Jan 1970 00:00:00 GMT"])
91+
_ -> option.None
9092
},
9193
option.map(max_age, fn(max_age) { ["MaxAge=", int.to_string(max_age)] }),
9294
option.map(domain, fn(domain) { ["Domain=", domain] }),
@@ -112,5 +114,3 @@ pub fn set_cookie_string(key, value, attributes) {
112114
|> list.map(string.join(_, ""))
113115
|> string.join("; ")
114116
}
115-
// // Plug sets secure true automatically if request/conn is https
116-
// // https://github.com/elixir-plug/plug/blob/v1.10.3/lib/plug/conn.ex#L1464

test/gleam/http_test.gleam

+7-5
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ pub fn get_req_cookies_test() {
793793
|> should.equal([tuple("k1", "v1"), tuple("k2", "v2")])
794794

795795
http.default_req()
796-
|> http.prepend_req_header("cookie", " k1=v1 ; k2=v2 ")
796+
|> http.prepend_req_header("cookie", " k1 = v1 ; k2=v2 ")
797797
|> http.get_req_cookies()
798798
|> should.equal([tuple("k1", "v1"), tuple("k2", "v2")])
799799

@@ -855,8 +855,10 @@ pub fn set_resp_cookie_test() {
855855
}
856856

857857
pub fn expire_resp_cookie_test() {
858-
http.response(200)
859-
|> http.expire_resp_cookie("k1" , cookie.default_attributes())
860-
|> http.get_resp_header("set-cookie")
861-
|> should.equal(Ok("k1=; expires=Thu, 01 Jan 1970 00:00:00 GMT; MaxAge=0; Path=/; HttpOnly"))
858+
http.response(200)
859+
|> http.expire_resp_cookie("k1", cookie.default_attributes())
860+
|> http.get_resp_header("set-cookie")
861+
|> should.equal(
862+
Ok("k1=; expires=Thu, 01 Jan 1970 00:00:00 GMT; MaxAge=0; Path=/; HttpOnly"),
863+
)
862864
}

0 commit comments

Comments
 (0)