Hcs.Plug.Static

hcs · API reference

Static file serving plug.

Serves files from a directory with optional directory listing, index file lookup, and ETag support.

type config = {
  index : string list; (* Index files to try for directories *)
  listing : bool; (* Enable directory listing *)
  dotfiles : bool; (* Serve dotfiles *)
}
val default_config : config
val string_starts_with : prefix:string -> string -> bool

Check if string starts with prefix

val extension : string -> string

Get file extension

val mime_type_of_extension : string -> string

Map file extension to MIME type

val html_escape : string -> string

HTML escape

val directory_listing : path:string -> (String.t * bool * int) list -> string

Generate directory listing HTML

val normalize_path : string -> string option

Normalize and validate URL path to prevent directory traversal

val generate_etag : String.t -> string

Generate ETag from content

val server : 
  ?config:config ->
  [> Eio.Fs.dir_ty ] Eio.Path.t ->
  Server.request ->
  Server.response

Create a static file server handler (not middleware).

Use this when static files are the only thing being served. root should be a directory capability already narrowed to the static root. Symlinks under root are rejected rather than followed.

val create : 
  ?index:string list ->
  ?with_etag:bool ->
  [> Eio.Fs.dir_ty ] Eio.Path.t ->
  (Server.request -> Server.response) ->
  Server.request ->
  Server.response

Create static file serving plug.

Serves files from root directory, falls back to next handler if not found.

parameter index Index files to try (default: "index.html") parameter with_etag Add ETag headers (default: true) parameter root Directory capability for the static root. Symlinks under this root are rejected rather than followed.