Skip to content

Commit

Permalink
fix for HEAD request and content-length handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rawhat committed Dec 24, 2024
1 parent 4054d00 commit f1fd730
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Unreleased
- Properly handle `content-length` and drop body on response when appropriate

## v4.0.2
- Replace `birl` with manual date methods (Thank you, @giacomocavalieri)

Expand Down
38 changes: 28 additions & 10 deletions src/mist/internal/http.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,16 @@ pub fn maybe_keep_alive(resp: Response(any)) -> Response(any) {
}
}

fn maybe_drop_body(
resp: Response(BytesTree),
is_head_request: Bool,
) -> Response(BytesTree) {
case is_head_request {
True -> response.set_body(resp, bytes_tree.new())
False -> resp
}
}

pub fn add_content_length(
when when: Bool,
length length: Int,
Expand All @@ -554,20 +564,28 @@ pub fn add_default_headers(
is_head_response: Bool,
) -> Response(BytesTree) {
let body_size = bytes_tree.byte_size(resp.body)

let include_content_length = case resp.status {
n if n >= 100 && n <= 199 -> False
n if n == 204 -> False
n if n == 304 -> False
_ -> True
let #(_existing_content_length, headers) =
resp.headers
|> list.key_pop("content-length")
|> result.lazy_unwrap(fn() { #("", resp.headers) })

let resp = case resp.status, body_size {
// explicitly drop
n, _ if n >= 100 && n <= 199 -> Response(..resp, headers:)
// explicitly drop
n, _ if n == 204 -> Response(..resp, headers:)
// don't add, don't drop
n, 0 if n == 304 -> resp
// don't add, don't drop
_, 0 if is_head_response == True -> resp
// explicitly overwrite
_, _ ->
response.set_header(resp, "content-length", int.to_string(body_size))
}

resp
|> add_date_header
|> add_content_length(
when: include_content_length && !is_head_response,
length: body_size,
)
|> maybe_drop_body(is_head_response)
}

fn is_continue(req: Request(Connection)) -> Bool {
Expand Down

0 comments on commit f1fd730

Please sign in to comment.