@@ -92,7 +92,7 @@ let rec resolve :
92
92
(resolved_directory , lookup_error ) result Lwt. t =
93
93
fun prefix dir args path ->
94
94
match path, dir with
95
- | [] , dir ->
95
+ | [] , dir ->
96
96
Lwt. return_ok (Dir (dir, args))
97
97
| _name :: _path , { subdirs = None , None ; _ } -> Lwt. return_error `Not_found
98
98
| name :: path , { subdirs = Some static , None ; _ } ->
@@ -116,31 +116,36 @@ let rec resolve :
116
116
`Cannot_parse (arg.Arg. description, msg, name :: prefix)
117
117
118
118
(* Note : headers are merged with predefined headers *)
119
- let io_to_answer : type a. code:int -> headers:(string * string) list -> a io -> a -> string Answer.t =
120
- fun ~code ~headers io body ->
119
+ let io_to_answer : type a. code:int -> headers:(string * string) list -> a io -> a -> string Answer.t =
120
+ fun ~code ~headers io body ->
121
121
match io with
122
- | Empty -> {Answer. code; body= " " ; headers}
122
+ | Empty ->
123
+ let code = if code = - 1 then 204 else code in
124
+ {Answer. code; body= " " ; headers}
123
125
| Raw l ->
124
126
let content_type = match l with
125
127
| [] -> " application/octet-stream"
126
128
| h :: _ -> Mime. to_string h in
129
+ let code = if code = - 1 then 200 else code in
127
130
{Answer. code; body; headers= (" content-type" , content_type)::headers}
128
131
| Json enc ->
132
+ let code = if code = - 1 then 200 else code in
129
133
{Answer. code; body = EzEncoding. construct enc body;
130
134
headers= (" content-type" , " application/json" )::headers}
131
135
132
136
let ser_handler :
133
- type i o e . ?content_type :string -> access_control :(string * string ) list
137
+ type i o e . ?content_type :string -> access_control :(string * string ) list
134
138
-> ('a -> i -> (o , e ) result Answer. t Lwt. t ) -> 'a ->
135
- i io -> o io -> e Json_encoding. encoding ->
139
+ i io -> o io -> ( e -> int * string ) ->
136
140
string -> (string Answer. t , handler_error ) result Lwt. t =
137
141
fun ?content_type ~access_control handler args input output errors ->
138
- let handle_result {Answer. code; body; headers} =
142
+ let handle_result {Answer. code; body; headers} =
139
143
match body with
140
144
| Ok o -> io_to_answer ~code ~headers: (headers @ access_control) output o
141
145
| Error e ->
142
- {Answer. code; body = EzEncoding. construct errors e;
143
- headers= (" content-type" , " application/json" )::access_control }
146
+ let c, body = errors e in
147
+ let code = if code = - 1 then c else code in
148
+ {Answer. code; body; headers= (" content-type" , " application/json" )::access_control }
144
149
in
145
150
match input with
146
151
| Empty -> (fun _ ->
@@ -194,7 +199,7 @@ let lookup ?meth ?content_type dir r path : (lookup_ok, lookup_error) result Lwt
194
199
(* Todo : combine access control headers correctly. *)
195
200
let access_control = List. fold_left (fun acc (_ ,rs ) -> match rs with
196
201
| Http {service; _} when acc = [] -> Service. access_control service
197
- | _ -> acc) [] l in
202
+ | _ -> acc) [] l in
198
203
let meths = Meth. headers @@ List. map fst l in
199
204
let sec_set = List. fold_left (fun acc (_ , rs ) -> match rs with
200
205
| Http {service; _} -> Security.StringSet. union acc (Security. headers (Service. security service))
@@ -207,7 +212,7 @@ let lookup ?meth ?content_type dir r path : (lookup_ok, lookup_error) result Lwt
207
212
| _ , Some (Http {service; handler} ) ->
208
213
let input = Service. input service in
209
214
let output = Service. output service in
210
- let errors = Service. errors_encoding service in
215
+ let errors = Service. errors_handler service in
211
216
let access_control = Service. access_control service in
212
217
let h = ser_handler ?content_type ~access_control handler args input output errors in
213
218
Lwt. return_ok @@ `http h
0 commit comments