Erlang Cowboy如何为静态文件添加响应头

xj3cbfub  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(186)

我想将Strict-Transport-Security标头添加到Web应用程序中。
设置如下

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}}
)

在提供静态文件时,在哪里添加HSTS或其他自定义头文件?

ca1c2owp

ca1c2owp1#

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.

相关问题