Skip to content

Commit f328099

Browse files
committed
Tidying: simplify end-of-file handling
1 parent 8c158e6 commit f328099

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

eio/tls_eio.ml

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ exception Tls_failure of Tls.Engine.failure
55

66
module Raw = struct
77

8+
(* We could replace [`Eof] with [`Error End_of_file] and then use
9+
a regular [result] type here. *)
810
type t = {
911
flow : Flow.two_way ;
1012
mutable state : [ `Active of Tls.Engine.state
@@ -17,7 +19,9 @@ module Raw = struct
1719
let read_t t cs =
1820
try Flow.read t.flow cs
1921
with
20-
| End_of_file -> 0
22+
| End_of_file as ex ->
23+
t.state <- `Eof;
24+
raise ex
2125
| exn ->
2226
(match t.state with
2327
| `Error _ | `Eof -> ()
@@ -43,12 +47,12 @@ module Raw = struct
4347
| Ok (state', `Response resp, `Data data) ->
4448
let state' = match state' with
4549
| `Ok tls -> `Active tls
46-
| `Eof -> `Eof
50+
| `Eof -> raise End_of_file
4751
| `Alert a -> `Error (Tls_alert a)
4852
in
4953
t.state <- state' ;
5054
Option.iter (try_write_t t) resp;
51-
`Ok data
55+
data
5256

5357
| Error (alert, `Response resp) ->
5458
t.state <- `Error (Tls_failure alert) ;
@@ -57,14 +61,13 @@ module Raw = struct
5761

5862
match t.state with
5963
| `Error e -> raise e
60-
| `Eof -> `Eof
64+
| `Eof -> raise End_of_file
6165
| `Active _ ->
6266
let n = read_t t t.recv_buf in
6367
match (t.state, n) with
64-
| (`Active _ , 0) -> t.state <- `Eof ; `Eof
6568
| (`Active tls, n) -> handle tls (Cstruct.sub t.recv_buf 0 n)
6669
| (`Error e, _) -> raise e
67-
| (`Eof, _) -> `Eof
70+
| (`Eof, _) -> raise End_of_file
6871

6972
let rec read t buf =
7073

@@ -81,9 +84,8 @@ module Raw = struct
8184
| Some res -> writeout res
8285
| None ->
8386
match read_react t with
84-
| `Eof -> raise End_of_file
85-
| `Ok None -> read t buf
86-
| `Ok (Some res) -> writeout res
87+
| None -> read t buf
88+
| Some res -> writeout res
8789

8890
let writev t css =
8991
match t.state with
@@ -115,9 +117,8 @@ module Raw = struct
115117
| `Active tls when not (Tls.Engine.handshake_in_progress tls) ->
116118
t
117119
| _ ->
118-
match read_react t with
119-
| `Eof -> raise End_of_file
120-
| `Ok cs -> push_linger t cs; drain_handshake t
120+
let cs = read_react t in
121+
push_linger t cs; drain_handshake t
121122

122123
let reneg ?authenticator ?acceptable_cas ?cert ?(drop = true) t =
123124
match t.state with
@@ -147,7 +148,7 @@ module Raw = struct
147148
match t.state with
148149
| `Active tls ->
149150
let (_, buf) = Tls.Engine.send_close_notify tls in
150-
t.state <- `Eof ;
151+
t.state <- `Eof ; (* XXX: this looks wrong - we're only trying to close the sending side *)
151152
write_t t buf
152153
| _ -> ()
153154

0 commit comments

Comments
 (0)