summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2017-12-13 02:32:28 +0100
committerDaniil Baturin <daniil@baturin.org>2017-12-13 03:12:07 +0100
commit7373e8aea39cec0af507b20a8209bd4697e74674 (patch)
treed87db25a6e08d283b54d97476abe7f93d9f94cdf
parent5f7d6a60287bac8f633b9c4e5e9e3f6f3a45da85 (diff)
downloadvyconf-7373e8aea39cec0af507b20a8209bd4697e74674.tar.gz
vyconf-7373e8aea39cec0af507b20a8209bd4697e74674.zip
Handle malformed protobuf messages correctly.
-rw-r--r--src/vyconfd.ml25
1 files changed, 16 insertions, 9 deletions
diff --git a/src/vyconfd.ml b/src/vyconfd.ml
index 2670588..7a5d6c4 100644
--- a/src/vyconfd.ml
+++ b/src/vyconfd.ml
@@ -89,18 +89,25 @@ let rec handle_connection world ic oc () =
try%lwt
let%lwt req_msg = Message.read ic in
let%lwt req =
- let envelope = decode_request_envelope (Pbrt.Decoder.of_bytes req_msg) in
- Lwt.return (envelope.token, envelope.request)
+ try
+ let envelope = decode_request_envelope (Pbrt.Decoder.of_bytes req_msg) in
+ Lwt.return (Ok (envelope.token, envelope.request))
+ with Protobuf.Decoder.Failure e -> Lwt.return (Error (Protobuf.Decoder.error_to_string e))
in
let%lwt resp =
(match req with
- | _, Status -> response_tmpl
- | _, Setup_session r -> setup_session world r
- | None, _ -> {response_tmpl with status=Fail; output=(Some "Operation requires session token")}
- | Some t, Teardown _ -> teardown_session t
- | Some t, Configure r -> enter_conf_mode r t
- | Some t, Exit_configure -> exit_conf_mode world t
- | _ -> failwith "Unimplemented") |> return
+ | Error msg -> {response_tmpl with status=Fail; error=(Some (Printf.sprintf "Decoding error: %s" msg))}
+ | Ok req ->
+ begin
+ match req with
+ | _, Status -> response_tmpl
+ | _, Setup_session r -> setup_session world r
+ | None, _ -> {response_tmpl with status=Fail; output=(Some "Operation requires session token")}
+ | Some t, Teardown _ -> teardown_session t
+ | Some t, Configure r -> enter_conf_mode r t
+ | Some t, Exit_configure -> exit_conf_mode world t
+ | _ -> failwith "Unimplemented"
+ end) |> Lwt.return
in
let enc = Pbrt.Encoder.create () in
let%lwt () = encode_response resp enc |> return in