Skip to content

Instantly share code, notes, and snippets.

@tylerhunt
Created December 1, 2021 19:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tylerhunt/753ac0c3f4f5e3160597b53647b0d9a7 to your computer and use it in GitHub Desktop.
Cowboy lowercases all the headers by default. This stream handler can be used to capitalize the headers. The implementation is in Elixir, and a configuration example showing its use in a Phoenix application is shown.
defmodule AppWeb.CapitalizeHeaders do
def init(stream_id, req, opts) do
:cowboy_stream_h.init(stream_id, req, opts)
end
def data(stream_id, is_fin, data, state) do
:cowboy_stream_h.data(stream_id, is_fin, data, state)
end
def info(stream_id, {:response, _status, headers, _body} = info, state) do
info = put_elem(info, 2, capitalize_headers(headers))
:cowboy_stream_h.info(stream_id, info, state)
end
def terminate(stream_id, reason, state) do
:cowboy_stream_h.terminate(stream_id, reason, state)
end
def early_error(stream_id, reason, partial_req, resp, opts) do
:cowboy_stream_h.early_error(stream_id, reason, partial_req, resp, opts)
end
defp capitalize_headers(headers) when is_map(headers) do
Map.new(headers, fn {key, value} -> {capitalize_header(key), value} end)
end
defp capitalize_header("etag"), do: "ETag"
defp capitalize_header("www-authenticate"), do: "WWW-Authenticate"
defp capitalize_header(header), do: :cowboy_bstr.capitalize_token(header)
end
config :app, AppWeb.Endpoint,
http: [
stream_handlers: [
AppWeb.CapitalizeHeaders,
:cowboy_telemetry_h,
:cowboy_stream_h,
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment