简体   繁体   中英

Erlang Cowboy change server signature in HTTP headers

Can someone tell me how to change default server signature in Erlang Cowboy Framework (which is "Cowboy") to a custom one, in all requests? I mean the value for the key "server" in HTTP response headers.

Kind regards, Leandro

The best way to achieve this would be by using onresponse hook

cowboy:start_http accepts a list of arguments in which you can supply onrequest and onresponse hooks . The basic syntax is very simple. It is just a tuple consisting on an atom and the name of the function.

   {onresponse, fun custom_onresponse/4}

Within this onresponse function you can modify the headers. For your special case you want to remove Server header. So you custom_onresponse will look like this

custom_onresponse(StatusCode,Headers,Body,Req)-> 
                Headers2 = 
                lists:delete({<<"server">>,<<"Cowboy">>},Headers),
                {ok,Req2} = cowboy_req:reply(StatusCode,Headers2,Body,Req),
                Req2.

To replace it you can use keyreplace function like so

Headers2 = 
lists:keyreplace(<<"server">>,1,Headers,{<<"server">>,<<"Your_Header">>})

There is also an example provided in the examples section of cowboy repo. Hope this helps.

它由手册条目的第三个代码片段说明。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM