mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-16 05:00:24 +00:00
Add cowboy_req:inform/2,3
User code can now send as many 1xx responses as necessary.
This commit is contained in:
parent
f4331f7c16
commit
f3d6b05b86
11 changed files with 206 additions and 1 deletions
|
@ -495,6 +495,13 @@ commands(State, Stream=#stream{local=idle}, [{error_response, StatusCode, Header
|
|||
commands(State, Stream, [{response, StatusCode, Headers, Body}|Tail]);
|
||||
commands(State, Stream, [{error_response, _, _, _}|Tail]) ->
|
||||
commands(State, Stream, Tail);
|
||||
%% Send an informational response.
|
||||
commands(State=#state{socket=Socket, transport=Transport, encode_state=EncodeState0},
|
||||
Stream=#stream{id=StreamID, local=idle}, [{inform, StatusCode, Headers0}|Tail]) ->
|
||||
Headers = Headers0#{<<":status">> => status(StatusCode)},
|
||||
{HeaderBlock, EncodeState} = headers_encode(Headers, EncodeState0),
|
||||
Transport:send(Socket, cow_http2:headers(StreamID, fin, HeaderBlock)),
|
||||
commands(State#state{encode_state=EncodeState}, Stream, Tail);
|
||||
%% Send response headers.
|
||||
%%
|
||||
%% @todo Kill the stream if it sent a response when one has already been sent.
|
||||
|
|
|
@ -71,6 +71,8 @@
|
|||
-export([set_resp_body/2]).
|
||||
%% @todo set_resp_body/3 with a ContentType or even Headers argument, to set content headers.
|
||||
-export([has_resp_body/1]).
|
||||
-export([inform/2]).
|
||||
-export([inform/3]).
|
||||
-export([reply/2]).
|
||||
-export([reply/3]).
|
||||
-export([reply/4]).
|
||||
|
@ -685,6 +687,18 @@ has_resp_body(_) ->
|
|||
delete_resp_header(Name, Req=#{resp_headers := RespHeaders}) ->
|
||||
Req#{resp_headers => maps:remove(Name, RespHeaders)}.
|
||||
|
||||
-spec inform(cowboy:http_status(), req()) -> ok.
|
||||
inform(Status, Req) ->
|
||||
inform(Status, #{}, Req).
|
||||
|
||||
-spec inform(cowboy:http_status(), cowboy:http_headers(), req()) -> ok.
|
||||
inform(_, _, #{has_sent_resp := _}) ->
|
||||
error(function_clause); %% @todo Better error message.
|
||||
inform(Status, Headers, #{pid := Pid, streamid := StreamID})
|
||||
when is_integer(Status); is_binary(Status) ->
|
||||
Pid ! {{Pid, StreamID}, {inform, Status, Headers}},
|
||||
ok.
|
||||
|
||||
-spec reply(cowboy:http_status(), Req) -> Req when Req::req().
|
||||
reply(Status, Req) ->
|
||||
reply(Status, #{}, Req).
|
||||
|
@ -699,7 +713,7 @@ reply(Status, Headers, Req) ->
|
|||
-spec reply(cowboy:http_status(), cowboy:http_headers(), resp_body(), Req)
|
||||
-> Req when Req::req().
|
||||
reply(_, _, _, #{has_sent_resp := _}) ->
|
||||
error(function_clause);
|
||||
error(function_clause); %% @todo Better error message.
|
||||
reply(Status, Headers, {sendfile, _, 0, _}, Req)
|
||||
when is_integer(Status); is_binary(Status) ->
|
||||
do_reply(Status, Headers#{
|
||||
|
|
|
@ -119,6 +119,8 @@ info(_StreamID, {read_body_timeout, Ref}, State=#state{pid=Pid, read_body_ref=Re
|
|||
info(_StreamID, {read_body_timeout, _}, State) ->
|
||||
{[], State};
|
||||
%% Response.
|
||||
info(_StreamID, Inform = {inform, _, _}, State) ->
|
||||
{[Inform], State};
|
||||
info(_StreamID, Response = {response, _, _, _}, State) ->
|
||||
{[Response], State};
|
||||
info(_StreamID, Headers = {headers, _, _}, State) ->
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue