Skip to content

Commit

Permalink
Improve 413 response handling
Browse files Browse the repository at this point in the history
Previously, when the server decided too much data sent with the client's
request, it would immediately send a 413 response and close the socket. The
client side kept sending incoming data as the socket was closed with unread
data in it. When this happens the connection was reset instead of going through
a regular close sequence. The client, specifically the replicator client,
detected the connection reset event before it had a chance to process the 413
response.

Fixes #1211
  • Loading branch information
nickva committed Mar 23, 2018
1 parent 97e14a8 commit cecf228
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/couch/src/couch_httpd.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,17 @@ before_response(Req0, Code0, Headers0, Args0) ->

respond_(#httpd{mochi_req = MochiReq}, Code, Headers, _Args, start_response) ->
MochiReq:start_response({Code, Headers});
respond_(#httpd{mochi_req = MochiReq}, 413, Headers, Args, Type) ->
% Special handling for the 413 response. Make sure the socket is closed as
% we don't know how much data was read before the error was thrown. Also
% drain all the data in the receive buffer to avoid connction being reset
% before the 413 response is parsed by the client. This is still racy, it
% just increases the chances of 413 being detected correctly by the client
% (rather than getting a brutal TCP reset).
erlang:put(mochiweb_request_force_close, true),
Result = MochiReq:Type({413, Headers, Args}),
catch MochiReq:recv(0, 0),
Result;
respond_(#httpd{mochi_req = MochiReq}, Code, Headers, Args, Type) ->
MochiReq:Type({Code, Headers, Args}).

Expand Down

0 comments on commit cecf228

Please sign in to comment.