Skip to content

Commit 18b5bfe

Browse files
committed
Forbide response 20 with an empty mime type.
1 parent f78102e commit 18b5bfe

6 files changed

Lines changed: 24 additions & 25 deletions

File tree

mehari-eio/file.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
let not_found = Mehari.(response not_found "")
22

3-
let response_document ?mime path =
3+
let response_document ?(mime = Mehari.app_octet_stream) path =
44
try
55
let chunk_size = 16384 in
66
let body =
@@ -24,7 +24,7 @@ let response_document ?mime path =
2424
in
2525
loop ()))
2626
in
27-
Option.value mime ~default:Mehari.no_mime |> Mehari.response_body body
27+
Mehari.response_body body mime
2828
with Eio.Io _ -> not_found
2929

3030
include

mehari-lwt-unix/file.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,9 @@ let read_chunks path =
8989

9090
let not_found = Mehari_io.respond Mehari.not_found ""
9191

92-
let respond_document ?mime path =
92+
let respond_document ?(mime = Mehari.app_octet_stream) path =
9393
let* exists = Lwt_unix.file_exists path in
9494
if exists then
95-
let mime = Option.value mime ~default:Mehari.no_mime in
9695
let* chunks = read_chunks path in
9796
let* cs = chunks () in
9897
Mehari_io.respond_body (Mehari.seq (fun () -> cs)) mime

mehari/mehari.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ let page = Response.page
3232
let make_mime = Mime.make_mime
3333
let from_filename = Mime.from_filename
3434
let from_content = Mime.from_content
35-
let no_mime = Mime.no_mime
3635
let gemini = Mime.gemini
3736
let app_octet_stream = Mime.app_octet_stream
3837
let plaintext = Mime.plaintext

mehari/mehari.mli

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ val make_mime : ?charset:string -> string -> mime
205205
[charset]. Charset defaults to [utf-8] if mime type begins with
206206
[text/].
207207
208+
@raise Invalid_argument if [mime] is an empty string
209+
208210
@see < https://www.rfc-editor.org/rfc/rfc2046#section-4.1.2 >
209211
For a description of the "charset" parameter. *)
210212

@@ -220,9 +222,6 @@ val from_content : ?charset:string -> tree:Conan.Tree.t -> string -> mime option
220222
performing a mime lookup based on content [c]. [tree] is the tree used to
221223
build the MIME database. *)
222224

223-
val no_mime : mime
224-
(** Represents the absence of a mime. This is a shortcut for [make_mime ""]. *)
225-
226225
val gemini : ?charset:string -> ?lang:string list -> unit -> mime
227226
(** [gemini ?charset ?lang ()] is [text/gemini; charset=...; lang=...].
228227
@@ -373,8 +372,8 @@ module type FS = sig
373372
(** Same as {!val:Mehari.response} but respond with content of given
374373
[filename] and use given {!type:Mehari.mime} as mime type.
375374
If [filename] is not present on filesystem, responds with
376-
{!val:Mehari.not_found}. If [mime] parameter is not supplied, use
377-
{!val:Mehari.no_mime} as mime type. *)
375+
{!val:Mehari.not_found}. If [mime] parameter is not supplied, document is
376+
served as {!val:Mehari.app_octet_stream}. *)
378377

379378
val static :
380379
?handler:(dir_path -> handler) ->
@@ -399,6 +398,9 @@ module type FS = sig
399398
will be generated by calling [dir_listing [ filename; ... ] request].
400399
[index] is default on [index.gmi].
401400
401+
Mime type of the served ressource is guessed by checking file name.
402+
Note that file names of the form [*.gmi] will be served as [text/gemini].
403+
402404
[show_hidden] decides whether hidden files should be listed. It defaults to
403405
[false] for security reasons. *)
404406
end

mehari/mime.ml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
type t = { mime : string; charset : string option; lang : string list }
22

3-
let make_mime ?charset mime =
4-
{
5-
mime;
6-
charset =
7-
(match charset with
8-
| None when String.starts_with ~prefix:"text/" mime -> Some "utf-8"
9-
| _ -> None);
10-
lang = [];
11-
}
12-
13-
let no_mime = make_mime ""
3+
let make_mime ?charset = function
4+
| "" -> raise (Invalid_argument "Mehari.make_mime")
5+
| mime ->
6+
{
7+
mime;
8+
charset =
9+
(match charset with
10+
| None when String.starts_with ~prefix:"text/" mime -> Some "utf-8"
11+
| _ -> None);
12+
lang = [];
13+
}
1414

1515
let gemini ?charset ?(lang = []) () =
1616
{ (make_mime ?charset "text/gemini") with lang }

mehari/static.ml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@ module Make (Dir : DIR) (Addr : Types.T) :
5050
let fname = Request.param req 1 in
5151
let mime =
5252
match Mime.from_filename fname with
53-
| None when Filename.check_suffix fname ".gmi" -> Mime.gemini ()
54-
| None -> Mime.no_mime
55-
| Some m -> m
53+
| None when Filename.check_suffix fname ".gmi" -> Some (Mime.gemini ())
54+
| (None | Some _) as m -> m
5655
in
57-
Dir.response_document ~mime path
56+
Dir.response_document ?mime path
5857

5958
let parent_path =
6059
Re.(compile (seq [ Re.group (seq [ rep1 any; char '/' ]); rep1 any ]))

0 commit comments

Comments
 (0)