Skip to content

Commit b54b23d

Browse files
committed
v4.1.1
1 parent e530a0f commit b54b23d

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v4.1.1 - 2025-07-22
4+
5+
- Fixed a bug where `set_cookie` would fail to overwrite an existing cookie.
6+
37
## v4.1.0 - 2025-07-06
48

59
- Updated for latest `gleam_stdlib`.

gleam.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "gleam_http"
2-
version = "4.1.0"
2+
version = "4.1.1"
33
licences = ["Apache-2.0"]
44
description = "Types and functions for Gleam HTTP clients and servers"
55
gleam = ">= 1.0.0"

src/gleam/http/request.gleam

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,22 +243,29 @@ pub fn set_path(req: Request(body), path: String) -> Request(body) {
243243
Request(..req, path: path)
244244
}
245245

246-
/// Send a cookie with a request
246+
/// Set a cookie on a request, replacing any previous cookie with that name.
247+
///
248+
/// All cookies stored in a single header named `cookie`. There should be
249+
/// at most one header with the name `cookie`, otherwise this function cannot
250+
/// guarentee that previous cookies with the same name are replaced.
247251
///
248-
/// Multiple cookies are added to the same cookie header.
249252
pub fn set_cookie(req: Request(body), name: String, value: String) {
250-
let new_cookie_string = string.join([name, value], "=")
253+
// Get the cookies
254+
let #(cookies, headers) =
255+
list.key_pop(req.headers, "cookie") |> result.unwrap(#("", req.headers))
251256

252-
let #(cookies_string, headers) = case list.key_pop(req.headers, "cookie") {
253-
Ok(#(cookies_string, headers)) -> {
254-
let cookies_string =
255-
string.join([cookies_string, new_cookie_string], "; ")
256-
#(cookies_string, headers)
257-
}
258-
Error(Nil) -> #(new_cookie_string, req.headers)
259-
}
257+
// Parse them
258+
let cookies =
259+
string.split(cookies, ";")
260+
|> list.filter_map(fn(c) { string.trim_start(c) |> string.split_once("=") })
261+
262+
// Set the new cookie, replacing any previous one with the same name
263+
let cookies =
264+
list.key_set(cookies, name, value)
265+
|> list.map(fn(pair) { pair.0 <> "=" <> pair.1 })
266+
|> string.join("; ")
260267

261-
Request(..req, headers: [#("cookie", cookies_string), ..headers])
268+
Request(..req, headers: [#("cookie", cookies), ..headers])
262269
}
263270

264271
/// Fetch the cookies sent in a request.

test/gleam/http/request_test.gleam

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,21 @@ pub fn set_req_cookies_test() {
451451
|> should.equal(Ok("k1=v1; k2=v2"))
452452
}
453453

454+
pub fn set_req_cookies_overwrite_test() {
455+
let request =
456+
request.new()
457+
|> request.set_cookie("k1", "k1")
458+
|> request.set_cookie("k2", "k2")
459+
|> request.set_cookie("k3", "k3")
460+
|> request.set_cookie("k4", "k4")
461+
|> request.set_cookie("k2", "k2-updated")
462+
|> request.set_cookie("k4", "k4-updated")
463+
464+
request
465+
|> request.get_header("cookie")
466+
|> should.equal(Ok("k1=k1; k2=k2-updated; k3=k3; k4=k4-updated"))
467+
}
468+
454469
pub fn remove_cookie_from_request_test() {
455470
let req =
456471
request.new()

0 commit comments

Comments
 (0)