Skip to content

Commit 3da5e23

Browse files
committed
Move site prefix handling to a middleware
1 parent 2529b9a commit 3da5e23

File tree

6 files changed

+21
-37
lines changed

6 files changed

+21
-37
lines changed

src/dream.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ include Dream_pure.Formats
6767
(* TODO Restore the ability to test with a prefix and re-enable the
6868
corresponding tests. *)
6969
let test ?(prefix = "") handler request =
70-
ignore prefix;
7170
let app =
7271
content_length
73-
@@ chop_site_prefix
72+
@@ with_site_prefix prefix
7473
@@ handler
7574
in
7675

src/dream.mli

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,7 +1973,6 @@ val run :
19731973
?stop:unit promise ->
19741974
?debug:bool ->
19751975
?error_handler:error_handler ->
1976-
?prefix:string ->
19771976
?https:bool ->
19781977
?certificate_file:string ->
19791978
?key_file:string ->
@@ -2004,8 +2003,6 @@ val run :
20042003
low-level errors. See {!section-errors} and example
20052004
{{:https://github.com/aantron/dream/tree/master/example/9-error#files}
20062005
[9-error]} \[{{:http://dream.as/9-error} playground}\].
2007-
- [~prefix] is a site prefix for applications that are not running at the
2008-
root ([/]) of their domain. The default is ["/"], for no prefix.
20092006
- [~https:true] enables HTTPS. You should also specify [~certificate_file]
20102007
and [~key_file]. However, for development, Dream includes an insecure
20112008
compiled-in
@@ -2036,7 +2033,6 @@ val serve :
20362033
?stop:unit promise ->
20372034
?debug:bool ->
20382035
?error_handler:error_handler ->
2039-
?prefix:string ->
20402036
?https:bool ->
20412037
?certificate_file:string ->
20422038
?key_file:string ->
@@ -2071,7 +2067,6 @@ val serve :
20712067
@@ Dream.lowercase_headers
20722068
@@ Dream.content_length
20732069
@@ Dream.catch_errors
2074-
@@ Dream.chop_site_prefix
20752070
@@ my_app
20762071
]}
20772072
@@ -2098,10 +2093,15 @@ val catch_errors : middleware
20982093
(** Forwards exceptions, rejections, and [4xx], [5xx] responses from the
20992094
application to the error handler. See {!section-errors}. *)
21002095

2101-
val chop_site_prefix : middleware
2102-
(** Removes {!Dream.run} [~prefix] from the path in each request, and adds it to
2103-
the request prefix. Responds with [502 Bad Gateway] if the path does not
2104-
have the expected prefix. *)
2096+
val with_site_prefix : string -> middleware
2097+
(** Removes the given prefix from the path in each request, and adds it to the
2098+
request prefix. Responds with [502 Bad Gateway] if the path does not have
2099+
the expected prefix.
2100+
2101+
This is for applications that are not running at the root ([/]) of their
2102+
domain. The default is ["/"], for no prefix. After [with_site_prefix],
2103+
routing is done relative to the prefix, and the prefix is also necessary for
2104+
emitting secure cookies. *)
21052105

21062106

21072107

src/http/http.ml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,6 @@ let built_in_middleware =
651651
Dream__middleware.Lowercase_headers.lowercase_headers;
652652
Dream__middleware.Content_length.content_length;
653653
Dream__middleware.Catch.catch_errors;
654-
Dream__middleware.Site_prefix.chop_site_prefix;
655654
]
656655

657656

@@ -743,27 +742,20 @@ let serve_with_details
743742
let is_localhost interface =
744743
interface = "localhost" || interface = "127.0.0.1"
745744

746-
(* TODO Validate the prefix here. *)
747745
let serve_with_maybe_https
748746
caller_function_for_error_messages
749747
~interface
750748
~port
751749
~stop
752750
?debug
753751
~error_handler
754-
~prefix
755752
~https
756753
?certificate_file ?key_file
757754
?certificate_string ?key_string
758755
~builtins
759756
user's_dream_handler =
760757

761-
let prefix =
762-
prefix
763-
|> Dream_pure.Formats.from_path
764-
|> Dream_pure.Formats.drop_trailing_slash
765-
in
766-
let app = Dream.new_app (Error_handler.app error_handler) prefix in
758+
let app = Dream.new_app (Error_handler.app error_handler) in
767759

768760
try%lwt
769761
begin match debug with
@@ -915,7 +907,6 @@ let serve
915907
?(stop = never)
916908
?debug
917909
?(error_handler = Error_handler.default)
918-
?(prefix = "")
919910
?(https = false)
920911
?certificate_file
921912
?key_file
@@ -929,7 +920,6 @@ let serve
929920
~stop
930921
?debug
931922
~error_handler
932-
~prefix
933923
~https:(if https then `OpenSSL else `No)
934924
?certificate_file
935925
?key_file
@@ -946,7 +936,6 @@ let run
946936
?(stop = never)
947937
?debug
948938
?(error_handler = Error_handler.default)
949-
?(prefix = "")
950939
?(https = false)
951940
?certificate_file
952941
?key_file
@@ -1025,7 +1014,6 @@ let run
10251014
~stop
10261015
?debug
10271016
~error_handler
1028-
~prefix
10291017
~https:(if https then `OpenSSL else `No)
10301018
?certificate_file ?key_file
10311019
?certificate_string:None ?key_string:None

src/middleware/site_prefix.ml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ let rec match_site_prefix prefix path =
2424

2525

2626
(* TODO The path and prefix representations and accessors need a cleanup. *)
27-
let chop_site_prefix next_handler request =
28-
let prefix = Dream.site_prefix request in
27+
let with_site_prefix prefix =
28+
let prefix =
29+
prefix
30+
|> Dream_pure.Formats.from_path
31+
|> Dream_pure.Formats.drop_trailing_slash
32+
in
33+
fun next_handler request ->
2934
match match_site_prefix prefix (Dream.path request) with
3035
| None ->
3136
(* TODO Streams. *)

src/pure/dream_pure.mli

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,6 @@ val prefix : request -> string
140140
val internal_prefix : request -> string list
141141
val path : request -> string list
142142
val version : request -> int * int
143-
val site_prefix : request -> string list
144-
(* TODO This will be moved out of dream-pure and become just a server-side
145-
middleware.. *)
146143
val with_client : string -> request -> request
147144
val with_method_ : [< method_ ] -> request -> request
148145
val with_prefix : string list -> request -> request
@@ -394,7 +391,7 @@ type error = {
394391

395392
type error_handler = error -> response option promise
396393

397-
val new_app : (error -> response Lwt.t) -> string list -> app
394+
val new_app : (error -> response Lwt.t) -> app
398395
val app : request -> app
399396
val debug : app -> bool
400397
val set_debug : bool -> app -> unit

src/pure/inmost.ml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ and app = {
7373
mutable app_debug : bool;
7474
mutable https : bool;
7575
error_handler : error -> response Lwt.t;
76-
site_prefix : string list;
7776
}
7877

7978
and error_handler = error -> response option Lwt.t
@@ -129,14 +128,10 @@ let app_error_handler app =
129128
let set_https https app =
130129
app.https <- https
131130

132-
let site_prefix request =
133-
request.specific.app.site_prefix
134-
135-
let new_app error_handler site_prefix = {
131+
let new_app error_handler = {
136132
app_debug = false;
137133
https = false;
138134
error_handler;
139-
site_prefix;
140135
}
141136

142137
type 'a promise = 'a Lwt.t
@@ -475,7 +470,7 @@ let request
475470
specific = {
476471
(* TODO Is there a better fake error handler? Maybe this function should
477472
come after the response constructors? *)
478-
app = new_app (fun _ -> assert false) [];
473+
app = new_app (fun _ -> assert false);
479474
request_client = client;
480475
method_;
481476
target;

0 commit comments

Comments
 (0)