Skip to content

Commit f46734a

Browse files
committed
rework cors
1 parent 69ee109 commit f46734a

26 files changed

+223
-231
lines changed

src/common/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(library
22
(name ezAPI)
33
(public_name ez_api)
4-
(modules arg param req path mime meth err security service doc url error_codes multipart ezAPI)
4+
(modules misc arg param req path mime meth err security service doc url error_codes multipart ezAPI)
55
(libraries lwt ezEncoding ezDebug ezLwtSys uuidm))
66

77
(library

src/common/ezAPI.ml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
(* *)
99
(**************************************************************************)
1010

11+
include Misc
1112
module Arg = Arg
1213
module Url = Url
1314
module Security = Security
@@ -84,7 +85,7 @@ let encode_params s params =
8485
let params =
8586
List.map (fun (param, v) ->
8687
if not (List.exists (fun p -> p.param_id = param.param_id) (Service.params s))
87-
then Printf.kprintf warning "unknown argument %S" param.param_id;
88+
then Printf.ksprintf warning "unknown argument %S" param.param_id;
8889
match v with
8990
| I n -> param.param_id, [string_of_int n]
9091
| S s -> param.param_id, [s]
@@ -108,36 +109,36 @@ let forge2 url s arg1 arg2 params = forge url s ((Req.dummy, arg1), arg2) param
108109
let raw_service :
109110
type i. ?section:Doc.section -> ?name:string -> ?descr:string -> ?meth:Meth.t ->
110111
input:i io -> output:'o io -> ?errors:'e Err.case list -> ?params:Param.t list ->
111-
?security:'s list -> ?access_control:(string * string) list -> ?register:bool -> ?hide:bool ->
112+
?security:'s list -> ?headers:StringSet.t StringMap.t -> ?register:bool -> ?hide:bool ->
112113
?input_example:i -> ?output_example:'o -> (Req.t, 'a) Path.t ->
113114
('a, i, 'o, 'e, 's) service =
114115
fun ?section ?name ?descr ?meth ~input ~output ?(errors=[]) ?(params=[])
115-
?(security=[]) ?access_control ?register ?hide ?input_example ?output_example path ->
116+
?(security=[]) ?headers ?register ?hide ?input_example ?output_example path ->
116117
let meth = match meth, input with
117118
| None, Empty -> `GET
118119
| None, _ -> `POST
119120
| Some m, _ -> m in
120121
let s = Service.make ~meth ~input ~output
121-
~errors ~params ~security ?access_control path in
122+
~errors ~params ~security ?headers path in
122123
let doc = Doc.make ?name ?descr ?section ?input_example ?output_example ?hide ?register s in
123124
{ s; doc }
124125

125126
let post_service ?section ?name ?descr ?(meth=`POST)
126127
~input ~output ?errors ?params
127-
?security ?register ?access_control ?input_example ?output_example
128+
?security ?register ?headers ?input_example ?output_example
128129
path =
129130
raw_service ?section ?name ?descr ~input:(Json input) ~output:(Json output)
130-
?errors ~meth ?params ?security ?access_control ?register ?input_example ?output_example path
131+
?errors ~meth ?params ?security ?headers ?register ?input_example ?output_example path
131132

132133
let service ?section ?name ?descr ?(meth=`GET) ~output ?errors ?params
133-
?security ?access_control ?register ?output_example path =
134+
?security ?headers ?register ?output_example path =
134135
raw_service ?section ?name ?descr ~input:Empty ~output:(Json output)
135-
?errors ~meth ?params ?security ?access_control ?register ?output_example path
136+
?errors ~meth ?params ?security ?headers ?register ?output_example path
136137

137138
let ws_service ?section ?name ?descr ~input ~output ?errors ?params
138-
?security ?access_control ?register ?output_example path =
139+
?security ?headers ?register ?output_example path =
139140
raw_service ?section ?name ?descr ~input ~output
140-
?errors ~meth:`GET ?params ?security ?access_control ?register ?output_example path
141+
?errors ~meth:`GET ?params ?security ?headers ?register ?output_example path
141142

142143
let register service =
143144
Doc.register service.doc;

src/common/js/ezDebug.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
open Js_of_ocaml
1212

13-
let printf fmt = Printf.kprintf (fun s -> Console.console##debug (Js.string s)) fmt
13+
let printf fmt = Printf.ksprintf (fun s -> Console.console##debug (Js.string s)) fmt
1414
let log s = Console.console##log (Js.string s)

src/common/meth.ml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,3 @@ let of_string s : [> all | `Other of string ] = match s with
3030
| "DELETE" -> `DELETE
3131
| "OPTIONS" -> `OPTIONS
3232
| s -> `Other s
33-
34-
let headers l =
35-
let meths = String.concat "," @@ List.map to_string l in
36-
[ "access-control-allow-methods", meths ]

src/common/misc.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module StringSet = Set.Make(String)
2+
module StringMap = Map.Make(String)

src/common/multipart.ml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
open Misc
2+
13
type 'content form_data = {
24
fo_name: string;
35
fo_content: 'content;
46
fo_filename: string option;
5-
fo_headers: string list Req.StringMap.t;
7+
fo_headers: string list StringMap.t;
68
}
79

810
let make ?(headers=[]) ?filename ~name content = {
911
fo_name=name; fo_content=content; fo_filename=filename;
10-
fo_headers=List.fold_left (fun acc (k, v) -> Req.StringMap.add k v acc) Req.StringMap.empty headers;
12+
fo_headers=List.fold_left (fun acc (k, v) -> StringMap.add k v acc) StringMap.empty headers;
1113
}
1214

1315
let split sep s =
@@ -24,25 +26,25 @@ let split sep s =
2426
let process_header ?(debug=false) map s = match String.split_on_char ':' s with
2527
| [ k; v ] ->
2628
let k, v = String.lowercase_ascii (String.trim k), String.trim v in
27-
Req.StringMap.add k (String.split_on_char ',' v) map
29+
StringMap.add k (String.split_on_char ',' v) map
2830
| _ ->
2931
if debug then Format.eprintf "malformed header: %s@." s;
3032
map
3133

3234
let make_form_data ?debug ?(index=0) ~headers contents =
33-
let headers = List.fold_left (process_header ?debug) Req.StringMap.empty headers in
35+
let headers = List.fold_left (process_header ?debug) StringMap.empty headers in
3436
let unquote s =
3537
let n = String.length s in
3638
if n > 1 && String.get s 0 = '"' && String.get s (n-1) = '"' then String.sub s 1 (n-2)
3739
else s in
3840
let fo_content = String.concat "\r\n" contents in
3941
let default = Format.sprintf "part%d" index in
40-
let fo_name, fo_filename, fo_headers = match Req.StringMap.find_opt "content-disposition" headers with
42+
let fo_name, fo_filename, fo_headers = match StringMap.find_opt "content-disposition" headers with
4143
| Some [ cd ] ->
4244
let _, params = Req.header_params ?debug cd in
4345
Option.value ~default (List.find_map (fun (k, v) -> if k="name" then Some (unquote v) else None) params),
4446
List.find_map (fun (k, v) -> if k="filename" then Some (unquote v) else None) params,
45-
Req.StringMap.remove "content-disposition" headers
47+
StringMap.remove "content-disposition" headers
4648
| _ -> default, None, headers in
4749
{ fo_name; fo_filename; fo_headers; fo_content }
4850

@@ -55,7 +57,7 @@ let get_boundary_from_content_type ?debug ct =
5557
| Some b -> Ok b
5658

5759
let get_boundary ?debug headers =
58-
match Req.StringMap.find_opt "content-type" headers with
60+
match StringMap.find_opt "content-type" headers with
5961
| Some [ ct ] -> get_boundary_from_content_type ?debug ct
6062
| _ -> Error "content-type header not found"
6163

@@ -98,7 +100,7 @@ let produce_form_data ~boundary b data =
98100
Buffer.add_string b ("content-disposition: form-data; name=\"" ^ data.fo_name ^ "\"");
99101
Option.iter (fun filename -> Buffer.add_string b ("; filename=\"" ^ filename ^ "\"")) data.fo_filename;
100102
Buffer.add_string b "\r\n";
101-
Req.StringMap.iter (fun k lv ->
103+
StringMap.iter (fun k lv ->
102104
Buffer.add_string b (k ^ ":");
103105
let () = match lv with
104106
| [] -> ()

src/common/req.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
(* *)
99
(**************************************************************************)
1010

11-
module StringMap = Map.Make(String)
11+
open Misc
1212

1313
type version = [ `HTTP_1_0 | `HTTP_1_1 ]
1414

src/common/security.ml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
(* *)
99
(**************************************************************************)
1010

11+
open Misc
12+
1113
type uninhabited = |
1214
type none = [ `Nosecurity of uninhabited ]
1315
type 'a apikey = { ref_name : string; name : 'a }
@@ -44,22 +46,15 @@ let params (l : [< scheme ] list) =
4446
| `Header _ | `Cookie _ -> acc
4547
) [] l
4648

47-
module StringSet = Set.Make(String)
48-
4949
let headers (sec : [< scheme ] list) =
5050
List.fold_left (fun headers -> function
5151
| `Nosecurity _ -> headers
52-
| `Basic _ | `Bearer _ -> StringSet.add "Authorization" headers
52+
| `Basic _ | `Bearer _ -> StringSet.add "authorization" headers
5353
| `Query _ -> headers
5454
| `Header { name; _ } -> StringSet.add name headers
55-
| `Cookie _ -> StringSet.add "Cookie" headers
55+
| `Cookie _ -> StringSet.add "cookie" headers
5656
) StringSet.empty sec
5757

58-
let header s =
59-
match StringSet.elements s with
60-
| [] -> []
61-
| l -> ["access-control-allow-headers", String.concat ", " l]
62-
6358
let make_authorization_headers ~kind s =
6459
[ "authorization", kind ^ " " ^ s ]
6560

src/common/service.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ type ('args, 'input, 'output, 'error, 'security) t = {
5151
meth : Meth.t;
5252
params : Param.t list;
5353
security: ([< Security.scheme ] as 'security) list;
54-
access_control : (string * string) list
54+
headers : Misc.StringSet.t Misc.StringMap.t;
5555
}
5656

5757
let make =
5858
fun ?(meth : Meth.t =`GET) ?(params=[]) ?(security=[]) ?(errors=[])
59-
?(access_control=[]) ~input ~output path ->
60-
{ path ; input ; output; errors; meth; params; security; access_control }
59+
?(headers=Misc.StringMap.empty) ~input ~output path ->
60+
{ path ; input ; output; errors; meth; params; security; headers }
6161

6262
let input s = s.input
6363
let output s = s.output
@@ -72,7 +72,7 @@ let meth s = s.meth
7272
let path s = s.path
7373
let security s = s.security
7474
let params s = s.params
75-
let access_control s = s.access_control
75+
let headers s = s.headers
7676

7777
let error s ~code = Err.get ~code s.errors
7878

src/common/unix/ezDebug.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
(* *)
99
(**************************************************************************)
1010

11-
let printf fmt = Printf.kprintf (fun s -> Format.eprintf "%s@." s) fmt
11+
let printf fmt = Printf.ksprintf (fun s -> Format.eprintf "%s@." s) fmt
1212
let log = prerr_endline

0 commit comments

Comments
 (0)