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

macro to avoid duplicating code

This commit is contained in:
geeksilva97 2024-01-09 20:14:39 -03:00
parent b9990d0a93
commit 69dda7292b

View file

@ -15,6 +15,9 @@
-module(cowboy_req).
-define(INVALID_COOKIE_HEADER_ERROR, exit({response_error, invalid_header,
'The set-cookie header is special and must be set using cowboy_req:set_resp_cookie/3,4.'})).
%% Request.
-export([method/1]).
-export([version/1]).
@ -719,8 +722,7 @@ set_resp_cookie(Name, Value, Req, Opts) ->
-spec set_resp_header(binary(), iodata(), Req)
-> Req when Req::req().
set_resp_header(<<"set-cookie">>, _, _) ->
exit({response_error, invalid_header,
'The set-cookie header is special and must be set using cowboy_req:set_resp_cookie/3,4.'});
?INVALID_COOKIE_HEADER_ERROR;
set_resp_header(Name, Value, Req=#{resp_headers := RespHeaders}) ->
Req#{resp_headers => RespHeaders#{Name => Value}};
set_resp_header(Name,Value, Req) ->
@ -728,10 +730,8 @@ set_resp_header(Name,Value, Req) ->
-spec set_resp_headers(cowboy:http_headers(), Req)
-> Req when Req::req().
set_resp_headers(#{<<"set-cookie">> := _}, _) ->
exit({response_error, invalid_header,
'The set-cookie header is special and must be set using cowboy_req:set_resp_cookie/3,4.'});
?INVALID_COOKIE_HEADER_ERROR;
set_resp_headers(Headers, Req=#{resp_headers := RespHeaders}) ->
Req#{resp_headers => maps:merge(RespHeaders, Headers)};
set_resp_headers(Headers, Req) ->
@ -788,9 +788,8 @@ inform(Status, Req) ->
inform(_, _, #{has_sent_resp := _}) ->
exit({response_error, response_already_sent,
'The final response has already been sent.'});
inform(_, _, #{<<"set-cookie">> := _}) ->
exit({response_error, invalid_header,
'The set-cookie header is special and must be set using cowboy_req:set_resp_cookie/3,4.'});
inform(_, #{<<"set-cookie">> := _}, _) ->
?INVALID_COOKIE_HEADER_ERROR;
inform(Status, Headers, Req) when is_integer(Status); is_binary(Status) ->
cast({inform, Status, Headers}, Req).
@ -811,8 +810,7 @@ reply(_, _, _, #{has_sent_resp := _}) ->
exit({response_error, response_already_sent,
'The final response has already been sent.'});
reply(_, #{<<"set-cookie">> := _}, _, _) ->
exit({response_error, invalid_header,
'The set-cookie header is special and must be set using cowboy_req:set_resp_cookie/3,4.'});
?INVALID_COOKIE_HEADER_ERROR;
reply(Status, Headers, {sendfile, _, 0, _}, Req)
when is_integer(Status); is_binary(Status) ->
do_reply(Status, Headers#{
@ -871,8 +869,7 @@ stream_reply(_, _, #{has_sent_resp := _}) ->
exit({response_error, response_already_sent,
'The final response has already been sent.'});
stream_reply(_, #{<<"set-cookie">> := _}, _, _) ->
exit({response_error, invalid_header,
'The set-cookie header is special and must be set using cowboy_req:set_resp_cookie/3,4.'});
?INVALID_COOKIE_HEADER_ERROR;
%% 204 and 304 responses must NOT send a body. We therefore
%% transform the call to a full response and expect the user
%% to NOT call stream_body/3 afterwards. (RFC7230 3.3)
@ -924,9 +921,8 @@ stream_events(Events, IsFin, Req=#{has_sent_resp := headers}) ->
stream_body({data, self(), IsFin, cow_sse:events(Events)}, Req).
-spec stream_trailers(cowboy:http_headers(), req()) -> ok.
stream_trailers(<<"set-cookie">>, _) ->
exit({response_error, invalid_header,
'The set-cookie header is special and must be set using cowboy_req:set_resp_cookie/3,4.'});
stream_trailers(#{<<"set-cookie">> := _}, _) ->
?INVALID_COOKIE_HEADER_ERROR;
stream_trailers(Trailers, Req=#{has_sent_resp := headers}) ->
cast({trailers, Trailers}, Req).