Skip to content

Commit

Permalink
Move site prefix handling to a middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
aantron committed Dec 13, 2021
1 parent 2529b9a commit 3da5e23
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 37 deletions.
3 changes: 1 addition & 2 deletions src/dream.ml
Expand Up @@ -67,10 +67,9 @@ include Dream_pure.Formats
(* TODO Restore the ability to test with a prefix and re-enable the
corresponding tests. *)
let test ?(prefix = "") handler request =
ignore prefix;
let app =
content_length
@@ chop_site_prefix
@@ with_site_prefix prefix
@@ handler
in

Expand Down
18 changes: 9 additions & 9 deletions src/dream.mli
Expand Up @@ -1973,7 +1973,6 @@ val run :
?stop:unit promise ->
?debug:bool ->
?error_handler:error_handler ->
?prefix:string ->
?https:bool ->
?certificate_file:string ->
?key_file:string ->
Expand Down Expand Up @@ -2004,8 +2003,6 @@ val run :
low-level errors. See {!section-errors} and example
{{:https://github.com/aantron/dream/tree/master/example/9-error#files}
[9-error]} \[{{:http://dream.as/9-error} playground}\].
- [~prefix] is a site prefix for applications that are not running at the
root ([/]) of their domain. The default is ["/"], for no prefix.
- [~https:true] enables HTTPS. You should also specify [~certificate_file]
and [~key_file]. However, for development, Dream includes an insecure
compiled-in
Expand Down Expand Up @@ -2036,7 +2033,6 @@ val serve :
?stop:unit promise ->
?debug:bool ->
?error_handler:error_handler ->
?prefix:string ->
?https:bool ->
?certificate_file:string ->
?key_file:string ->
Expand Down Expand Up @@ -2071,7 +2067,6 @@ val serve :
@@ Dream.lowercase_headers
@@ Dream.content_length
@@ Dream.catch_errors
@@ Dream.chop_site_prefix
@@ my_app
]}
Expand All @@ -2098,10 +2093,15 @@ val catch_errors : middleware
(** Forwards exceptions, rejections, and [4xx], [5xx] responses from the
application to the error handler. See {!section-errors}. *)

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



Expand Down
14 changes: 1 addition & 13 deletions src/http/http.ml
Expand Up @@ -651,7 +651,6 @@ let built_in_middleware =
Dream__middleware.Lowercase_headers.lowercase_headers;
Dream__middleware.Content_length.content_length;
Dream__middleware.Catch.catch_errors;
Dream__middleware.Site_prefix.chop_site_prefix;
]


Expand Down Expand Up @@ -743,27 +742,20 @@ let serve_with_details
let is_localhost interface =
interface = "localhost" || interface = "127.0.0.1"

(* TODO Validate the prefix here. *)
let serve_with_maybe_https
caller_function_for_error_messages
~interface
~port
~stop
?debug
~error_handler
~prefix
~https
?certificate_file ?key_file
?certificate_string ?key_string
~builtins
user's_dream_handler =

let prefix =
prefix
|> Dream_pure.Formats.from_path
|> Dream_pure.Formats.drop_trailing_slash
in
let app = Dream.new_app (Error_handler.app error_handler) prefix in
let app = Dream.new_app (Error_handler.app error_handler) in

try%lwt
begin match debug with
Expand Down Expand Up @@ -915,7 +907,6 @@ let serve
?(stop = never)
?debug
?(error_handler = Error_handler.default)
?(prefix = "")
?(https = false)
?certificate_file
?key_file
Expand All @@ -929,7 +920,6 @@ let serve
~stop
?debug
~error_handler
~prefix
~https:(if https then `OpenSSL else `No)
?certificate_file
?key_file
Expand All @@ -946,7 +936,6 @@ let run
?(stop = never)
?debug
?(error_handler = Error_handler.default)
?(prefix = "")
?(https = false)
?certificate_file
?key_file
Expand Down Expand Up @@ -1025,7 +1014,6 @@ let run
~stop
?debug
~error_handler
~prefix
~https:(if https then `OpenSSL else `No)
?certificate_file ?key_file
?certificate_string:None ?key_string:None
Expand Down
9 changes: 7 additions & 2 deletions src/middleware/site_prefix.ml
Expand Up @@ -24,8 +24,13 @@ let rec match_site_prefix prefix path =


(* TODO The path and prefix representations and accessors need a cleanup. *)
let chop_site_prefix next_handler request =
let prefix = Dream.site_prefix request in
let with_site_prefix prefix =
let prefix =
prefix
|> Dream_pure.Formats.from_path
|> Dream_pure.Formats.drop_trailing_slash
in
fun next_handler request ->
match match_site_prefix prefix (Dream.path request) with
| None ->
(* TODO Streams. *)
Expand Down
5 changes: 1 addition & 4 deletions src/pure/dream_pure.mli
Expand Up @@ -140,9 +140,6 @@ val prefix : request -> string
val internal_prefix : request -> string list
val path : request -> string list
val version : request -> int * int
val site_prefix : request -> string list
(* TODO This will be moved out of dream-pure and become just a server-side
middleware.. *)
val with_client : string -> request -> request
val with_method_ : [< method_ ] -> request -> request
val with_prefix : string list -> request -> request
Expand Down Expand Up @@ -394,7 +391,7 @@ type error = {

type error_handler = error -> response option promise

val new_app : (error -> response Lwt.t) -> string list -> app
val new_app : (error -> response Lwt.t) -> app
val app : request -> app
val debug : app -> bool
val set_debug : bool -> app -> unit
Expand Down
9 changes: 2 additions & 7 deletions src/pure/inmost.ml
Expand Up @@ -73,7 +73,6 @@ and app = {
mutable app_debug : bool;
mutable https : bool;
error_handler : error -> response Lwt.t;
site_prefix : string list;
}

and error_handler = error -> response option Lwt.t
Expand Down Expand Up @@ -129,14 +128,10 @@ let app_error_handler app =
let set_https https app =
app.https <- https

let site_prefix request =
request.specific.app.site_prefix

let new_app error_handler site_prefix = {
let new_app error_handler = {
app_debug = false;
https = false;
error_handler;
site_prefix;
}

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

0 comments on commit 3da5e23

Please sign in to comment.