Skip to content

Commit

Permalink
Catch parse errors and hand them to error_handler
Browse files Browse the repository at this point in the history
refs #18
  • Loading branch information
anmonteiro committed Dec 9, 2018
1 parent ea1b913 commit 6d3405a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/parse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,17 @@ module Reader = struct
ignore (read_with_more t Bigstringaf.empty ~off:0 ~len:0 Complete : int);
;;

let is_parse_failure t =
match t.parse_state with
| Fail _ -> true
| _ -> false

let next t =
match t.parse_state with
| Done ->
if t.closed
then `Close
else `Read
| Fail _ -> `Close
| Fail failure -> `Error failure
| Partial _ -> `Read
end
3 changes: 2 additions & 1 deletion lib/server_connection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ let set_error_and_handle ?request t error =
t.error_handler ?request error (fun headers ->
Writer.write_response writer (Response.create ~headers status);
Body.of_faraday (Writer.faraday writer));
shutdown_writer t;
end

let report_exn t exn =
Expand All @@ -219,7 +220,7 @@ let advance_request_queue_if_necessary t =
else if not (Reqd.requires_input reqd)
then shutdown_reader t
end
end else if Reader.is_closed t.reader
end else if Reader.is_closed t.reader && (not (Reader.is_parse_failure t.reader))
then shutdown t

let _next_read_operation t =
Expand Down
1 change: 1 addition & 0 deletions lib_test/simulator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ let body_to_strings = function
;;

let case_to_strings = function
| `Raw r, body -> r @ (body_to_strings body)
| `Request r, body -> [request_to_string r] @ (body_to_strings body)
| `Response r, body -> [response_to_string r] @ (body_to_strings body)

Expand Down
10 changes: 9 additions & 1 deletion lib_test/test_httpaf_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let single_get =
, Simulator.test_server
~handler: (basic_handler "")
~input: [(`Request (Request.create `GET "/")), `Empty]
~output: [(`Response (Response.create `OK) ), `Empty]
~output: [(`Response (Response.create `OK)), `Empty]
; "single GET, close connection"
, `Quick
, Simulator.test_server
Expand Down Expand Up @@ -67,6 +67,14 @@ let single_get =
, `Fixed ["This is a test ... that involves multiple chunks"] ];
Alcotest.(check bool "got eof" !got_eof true);
end
; "single GET, malformed request"
, `Quick
, Simulator.test_server
~handler: (basic_handler "")
~input: [ `Raw [ "GET / HTTP/1.1\r\nconnection: close\r\nX-Other-Header : shouldnt_have_space_before_colon\r\n\r\n" ]
, `Empty
]
~output: [(`Response (Response.create `Bad_request)), `Fixed ["400"]]
]
;;

Expand Down

0 comments on commit 6d3405a

Please sign in to comment.