user2317487
user2317487

Reputation:

Erlang Cowboy how to add response headers for static files

I want to add Strict-Transport-Security header into a web app.

Here is the setup

Dispatch = cowboy_router:compile([
    {'_', [
        {"/", cowboy_static, {file, env_file:filepath({data, "assets/index.html"})}}
    ]}
]),
{ok, _} = cowboy:start_tls(product_https,
    [
        {port, 8443},
        {certfile, env_file:filepath({etc, "ssl/cert.crt"})},
        {keyfile, env_file:filepath({etc, "ssl/key.key"})}
    ],
    #{env => #{dispatch => Dispatch}}
)

Where do I add the HSTS or other custom headers when serving static files?

Upvotes: 1

Views: 447

Answers (1)

user2317487
user2317487

Reputation:

Using middleware is the solution.

The setup will be:

Dispatch = cowboy_router:compile([
    {'_', [
        {"/", cowboy_static, {file, env_file:filepath({data, "assets/index.html"})}}
    ]}
]),
{ok, _} = cowboy:start_tls(product_https,
    [
        {port, 8443},
        {certfile, env_file:filepath({etc, "ssl/cert.crt"})},
        {keyfile, env_file:filepath({etc, "ssl/key.key"})}
    ],
    #{
        env => #{dispatch => Dispatch},
        middlewares => [cowboy_router, my_security_middleware, cowboy_handler]}
    }
)

And here is the middleware implementation

-module(my_security_middleware).
-behaviour(cowboy_middleware).

-export([execute/2]).

execute(Req, Env) ->
    Req2 = cowboy_req:set_resp_header(<<"aaaaa">>, <<"bbbbb">>, Req),
    {ok, Req2, Env}.

That will add header aaaaa: bbbbb to all request responses.

Upvotes: 2

Related Questions