Skip to content

Commit

Permalink
(inaka#123) Close the connection after receiving 204 responses
Browse files Browse the repository at this point in the history
In case of receiving an http answer with answer code 204, shotgun will
return the result from the server, but the connection is automatically
closed after receiving this answer.
  • Loading branch information
tothlac committed Dec 2, 2015
1 parent f045cdd commit 20bb919
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/shotgun.erl
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,12 @@ wait_response({gun_response, _Pid, _StreamRef, fin, StatusCode, Headers},
gen_fsm:reply(From, {ok, Response}),
queue:in(Response, Responses)
end,
{next_state, at_rest, State#{responses => NewResponses}, 0};
check_no_content_resp(StatusCode, State#{responses => NewResponses});
wait_response({gun_response, _Pid, _StreamRef, nofin, StatusCode, Headers},
#{from := From, stream := StreamRef, async := Async} = State) ->
StateName =
case lists:keyfind(<<"transfer-encoding">>, 1, Headers) of
{<<"transfer-encoding">>, <<"chunked">>} when Async == true->
{<<"transfer-encoding">>, <<"chunked">>} when Async =:= true->
Result = {ok, StreamRef},
gen_fsm:reply(From, Result),
receive_chunk;
Expand Down Expand Up @@ -548,7 +548,7 @@ receive_data({gun_data, _Pid, _StreamRef, fin, Data},
body => NewData
}},
gen_fsm:reply(From, Result),
{next_state, at_rest, State, 0};
check_no_content_resp(StatusCode, State);
receive_data({gun_error, _Pid, StreamRef, _Reason},
#{stream := StreamRef} = State) ->
{next_state, at_rest, State, 0}.
Expand All @@ -558,11 +558,12 @@ receive_data({gun_error, _Pid, StreamRef, _Reason},
-spec receive_chunk(term(), term()) -> term().
receive_chunk({'DOWN', _, _, _, _Reason}, _State) ->
error(incomplete);
receive_chunk({gun_data, _Pid, StreamRef, IsFin, Data}, State) ->
receive_chunk({gun_data, _Pid, StreamRef, IsFin, Data},
#{status_code := StatusCode} = State) ->
NewState = manage_chunk(IsFin, StreamRef, Data, State),
case IsFin of
fin ->
{next_state, at_rest, NewState, 0};
check_no_content_resp(StatusCode, NewState);
nofin ->
{next_state, receive_chunk, NewState}
end;
Expand Down Expand Up @@ -756,3 +757,12 @@ unexpected_event_warning(StateName, Event) ->
error_logger:warning_msg( "Unexpected event in state '~p': ~p~n"
, [StateName, Event]
).

%% @private
check_no_content_resp(StatusCode, State) ->
case StatusCode of
204 ->
{stop, no_content, State};
_ ->
{next_state, at_rest, State, 0}
end.

0 comments on commit 20bb919

Please sign in to comment.