Anonymous avatar Anonymous committed f36ece2

useful error when exceeding MAX_RECV_BODY

Comments (0)

Files changed (2)

src/webmachine_request_srv.erl

 %% @doc Receive the body of the HTTP request (defined by Content-Length).
 %%      Will only receive up to the default max-body length
 do_recv_body(State) ->
-    read_whole_stream(recv_stream_body(State, ?MAX_RECV_BODY), []).
+    read_whole_stream(recv_stream_body(State, ?MAX_RECV_BODY), [], 0).
 
-read_whole_stream({Hunk,Next}, Acc0) ->
-    Acc = [Hunk|Acc0],
-    case Next of
-        done -> iolist_to_binary(lists:reverse(Acc));
-        _ -> read_whole_stream(Next(), Acc)
+read_whole_stream({Hunk,_}, _, SizeAcc)
+  when SizeAcc + byte_size(Hunk) > ?MAX_RECV_BODY -> 
+    {error, req_body_too_large};
+read_whole_stream({Hunk,Next}, Acc0, SizeAcc) ->
+    HunkSize = byte_size(Hunk),
+    if SizeAcc + HunkSize > ?MAX_RECV_BODY -> 
+            {error, req_body_too_large};
+       true ->
+            Acc = [Hunk|Acc0],
+            case Next of
+                done -> iolist_to_binary(lists:reverse(Acc));
+                _ -> read_whole_stream(Next(), Acc, SizeAcc + HunkSize)
+            end
     end.
 
 recv_stream_body(State = #state{reqdata=RD}, MaxHunkSize) ->
 
 range_parts({stream, {Hunk,Next}}, Ranges) ->
     % for now, streamed bodies are read in full for range requests
-    range_parts(read_whole_stream({Hunk,Next}, []), Ranges);
+    range_parts(read_whole_stream({Hunk,Next}, [], 0), Ranges);
 
 range_parts(Body0, Ranges) ->
     Body = iolist_to_binary(Body0),
 maybe_conflict_body(BodyResponse) ->
     case BodyResponse of
         stream_conflict ->
-            erlang:error("wrq:req_body and wrq:stream_req_body conflict");
+            exit("wrq:req_body and wrq:stream_req_body conflict");
+        {error, req_body_too_large} ->
+            exit("request body too large");
         _ ->
             BodyResponse
     end.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.