0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 20:50:24 +00:00

Add functions for getting and setting response headers

This commit is contained in:
Johan Lövdahl 2016-11-13 15:39:40 +01:00 committed by Loïc Hoguin
parent 42c95b1584
commit 3f4e79d42f
No known key found for this signature in database
GPG key ID: 71366FF21851DF03
7 changed files with 231 additions and 1 deletions

View file

@ -59,8 +59,11 @@
%% Response.
-export([set_resp_cookie/3]).
-export([set_resp_cookie/4]).
-export([resp_header/2]).
-export([resp_header/3]).
-export([resp_headers/1]).
-export([set_resp_header/3]).
%% @todo set_resp_headers/2
-export([set_resp_headers/2]).
-export([has_resp_header/2]).
%% @todo resp_header
-export([delete_resp_header/2]).
@ -570,6 +573,31 @@ set_resp_header(Name, Value, Req=#{resp_headers := RespHeaders}) ->
set_resp_header(Name,Value, Req) ->
Req#{resp_headers => #{Name => Value}}.
-spec set_resp_headers(cowboy:http_headers(), Req)
-> Req when Req::req().
set_resp_headers(Headers, Req=#{resp_headers := RespHeaders}) ->
Req#{resp_headers => maps:merge(RespHeaders, Headers)};
set_resp_headers(Headers, Req) ->
Req#{resp_headers => Headers}.
-spec resp_header(binary(), req()) -> binary() | undefined.
resp_header(Name, Req) ->
resp_header(Name, Req, undefined).
-spec resp_header(binary(), req(), Default)
-> binary() | Default when Default::any().
resp_header(Name, #{resp_headers := Headers}, Default) ->
maps:get(Name, Headers, Default);
resp_header(_, #{}, Default) ->
Default.
-spec resp_headers(req()) -> cowboy:http_headers().
resp_headers(#{resp_headers := RespHeaders}) ->
RespHeaders;
resp_headers(#{}) ->
#{}.
-spec set_resp_body(resp_body(), Req) -> Req when Req::req().
set_resp_body(Body, Req) ->
Req#{resp_body => Body}.