package tiny_httpd

  1. Overview
  2. Docs

Serving static content from directories

This module provides the same functionality as the "http_of_dir" tool. It exposes a directory (and its subdirectories), with the optional ability to delete or upload files.

  • since 0.11
type dir_behavior =
  1. | Index
    (*

    Redirect to index.html if present, else fails.

    *)
  2. | Lists
    (*

    Lists content of directory. Be careful of security implications.

    *)
  3. | Index_or_lists
    (*

    Redirect to index.html if present and lists content otherwise. This is useful for tilde ("~") directories and other per-user behavior, but be mindful of security implications

    *)
  4. | Forbidden
    (*

    Forbid access to directory. This is suited for serving assets, for example.

    *)

behavior of static directory.

This controls what happens when the user requests the path to a directory rather than a file.

type hidden

Type used to prevent users from building a config directly. Use default_config or config instead.

type config = {
  1. mutable download : bool;
    (*

    Is downloading files allowed?

    *)
  2. mutable dir_behavior : dir_behavior;
    (*

    Behavior when serving a directory and not a file

    *)
  3. mutable delete : bool;
    (*

    Is deleting a file allowed? (with method DELETE)

    *)
  4. mutable upload : bool;
    (*

    Is uploading a file allowed? (with method PUT)

    *)
  5. mutable max_upload_size : int;
    (*

    If upload is true, this is the maximum size in bytes for uploaded files.

    *)
  6. _rest : hidden;
    (*

    Just ignore this field.

    *)
}

configuration for static file handlers. This might get more fields over time.

val default_config : unit -> config

default configuration: { download=true ; dir_behavior=Forbidden ; delete=false ; upload=false ; max_upload_size = 10 * 1024 * 1024 }

val config : ?download:bool -> ?dir_behavior:dir_behavior -> ?delete:bool -> ?upload:bool -> ?max_upload_size:int -> unit -> config

Build a config from default_config.

  • since 0.12
val add_dir_path : config:config -> dir:string -> prefix:string -> Tiny_httpd_core.Server.t -> unit

add_dirpath ~config ~dir ~prefix server adds route handle to the server to serve static files in dir when url starts with prefix, using the given configuration config.

module type VFS = sig ... end

Virtual file system.

val vfs_of_dir : string -> (module VFS)

vfs_of_dir dir makes a virtual file system that reads from the disk.

  • since 0.12
val add_vfs : config:config -> vfs:(module VFS) -> prefix:string -> Tiny_httpd_core.Server.t -> unit

Similar to add_dir_path but using a virtual file system instead.

  • since 0.12
module Embedded_fs : sig ... end

An embedded file system, as a list of files with (relative) paths. This is useful in combination with the "tiny-httpd-mkfs" tool, which embeds the files it's given into a OCaml module.

OCaml

Innovation. Community. Security.