Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
aantron committed Jul 7, 2024
1 parent 64daabf commit a532c4a
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 57 deletions.
6 changes: 3 additions & 3 deletions example/c-cookie/cookie.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let () =
Dream.run
let () = Eio_main.run @@ fun env ->
Dream.run env
@@ Dream.set_secret "foo"
@@ Dream.logger
@@ fun request ->
Expand All @@ -13,4 +13,4 @@ let () =
let response = Dream.response "Set language preference; come again!" in
Dream.add_header response "Content-Type" Dream.text_html;
Dream.set_cookie response request "ui.language" "ut-OP";
Lwt.return response
response
2 changes: 1 addition & 1 deletion example/c-cookie/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(executable
(name cookie)
(libraries dream))
(libraries dream eio_main))
3 changes: 1 addition & 2 deletions example/d-form/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(executable
(name form)
(libraries dream)
(preprocess (pps lwt_ppx)))
(libraries dream eio_main))

(rule
(targets form.ml)
Expand Down
6 changes: 3 additions & 3 deletions example/d-form/form.eml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ let show_form ?message request =
</body>
</html>

let () =
Dream.run
let () = Eio_main.run @@ fun env ->
Dream.run env
@@ Dream.logger
@@ Dream.memory_sessions
@@ Dream.router [
Expand All @@ -28,7 +28,7 @@ let () =

Dream.post "/"
(fun request ->
match%lwt Dream.form request with
match Dream.form request with
| `Ok ["message", message] ->
Dream.html (show_form ~message request)
| _ ->
Expand Down
4 changes: 2 additions & 2 deletions example/e-json/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(executable
(name json)
(libraries dream)
(preprocess (pps lwt_ppx ppx_yojson_conv)))
(libraries dream eio_main)
(preprocess (pps ppx_yojson_conv)))
8 changes: 3 additions & 5 deletions example/e-json/json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ type message_object = {
message : string;
} [@@deriving yojson]

let () =
Dream.run
let () = Eio_main.run @@ fun env ->
Dream.run env
@@ Dream.logger
@@ Dream.origin_referrer_check
@@ Dream.router [

Dream.post "/"
(fun request ->
let%lwt body = Dream.body request in

let message_object =
body
Dream.body request
|> Yojson.Safe.from_string
|> message_object_of_yojson
in
Expand Down
2 changes: 1 addition & 1 deletion example/f-static/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(executable
(name static)
(libraries dream))
(libraries dream eio_main))
6 changes: 3 additions & 3 deletions example/f-static/static.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let () =
Dream.run
let () = Eio_main.run @@ fun env ->
Dream.run env
@@ Dream.logger
@@ Dream.router [
Dream.get "/static/**" (Dream.static ".")
Dream.get "/static/**" (Dream.static (Eio.Stdenv.cwd env))
]
6 changes: 2 additions & 4 deletions src/dream.mli
Original file line number Diff line number Diff line change
Expand Up @@ -1463,9 +1463,7 @@ val no_route : route

(** {1 Static files} *)

val static :
?loader:(string -> string -> handler) ->
string -> handler
val static : _ Eio.Path.t -> handler
(** Serves static files from a local directory. See example
{{:https://github.com/aantron/dream/tree/master/example/f-static#files}
[f-static]}.
Expand Down Expand Up @@ -1495,7 +1493,7 @@ val static :
{{:https://github.com/aantron/dream/tree/master/example/w-one-binary#files}
[w-one-binary]} for a loader that serves files from memory instead. *)

val from_filesystem : string -> string -> handler
val from_filesystem : _ Eio.Path.t -> string -> handler
(** [Dream.from_filesystem local_directory path request] responds with a file
from the file system found at [local_directory ^ "/" ^ path].
If such a file does not exist, it responds with [404 Not Found].
Expand Down
14 changes: 6 additions & 8 deletions src/http/driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,8 @@ let to_dream_method : Cohttp_http.Method.t -> Method.method_ = function
| `CONNECT -> `CONNECT
| `Other method_ -> `Method method_

(* TODO Adapt
let to_httpaf_status status =
Status.status_to_int status |> Httpaf.Status.of_code
let to_h2_status status =
Status.status_to_int status |> H2.Status.of_code
*)
let to_cohttp_status status =
Status.status_to_int status |> Cohttp_http.Status.of_int

let sha1 s =
s
Expand Down Expand Up @@ -190,7 +185,10 @@ let wrap_handler
(Stream_adapter.create response,
Eio.Flow.Pi.source (module Stream_adapter)) in

Cohttp_eio.Server.respond ~status:`OK ~body:response_body ()
Cohttp_eio.Server.respond
~status:(to_cohttp_status (Message.status response))
~headers:(Cohttp.Header.of_list (Message.all_headers response))
~body:response_body ()
in

(* Call the user's handler. If it raises an exception or returns a promise
Expand Down
37 changes: 12 additions & 25 deletions src/unix/static.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,15 @@ let mime_lookup filename =
["Content-Type", content_type]

let from_filesystem local_root path _ =
(* TODO Restore.
let file = Filename.concat local_root path in
Lwt.catch
(fun () ->
Lwt_io.(with_file ~mode:Input file) (fun channel ->
let%lwt content = Lwt_io.read channel in
let (/) = Eio.Path.(/) in
let file = local_root / path in
(* TODO Indentation below. *)
try
let content = Eio.Path.load file in
Message.response
~headers:(mime_lookup path) (Stream.string content) Stream.null
|> Lwt.return))
(fun _exn ->
with _exn ->
Message.response ~status:`Not_Found Stream.empty Stream.null
|> Lwt.return)
*)
ignore local_root;
ignore path;
assert false

(* TODO Add ETag handling. *)
(* TODO Add Content-Length handling? *)
Expand Down Expand Up @@ -77,21 +70,20 @@ let validate_path request =
else
None

let static ?(loader = from_filesystem) local_root = fun request ->
(* TODO Restore.
let static local_root = fun request ->
if not @@ Method.methods_equal (Message.method_ request) `GET then
Message.response ~status:`Not_Found Stream.empty Stream.null
|> Lwt.return

else
match validate_path request with
| None ->
Message.response ~status:`Not_Found Stream.empty Stream.null
|> Lwt.return

| Some path ->
let%lwt response = loader local_root path request in
(* TODO Using from_filesystem because of row type unification problems in
the phantom type parameters of Eio path capabilities -- a completely
artificial regression. *)
let response = from_filesystem local_root path request in
if not (Message.has_header response "Content-Type") then begin
match Message.status response with
| `OK
Expand All @@ -103,9 +95,4 @@ let static ?(loader = from_filesystem) local_root = fun request ->
| _ ->
()
end;
Lwt.return response
*)
ignore loader;
ignore local_root;
ignore request;
assert false
response

0 comments on commit a532c4a

Please sign in to comment.