package simple_httpd

  1. Overview
  2. Docs

Module Simple_httpd.DirSource

Module to server directory structure (real of virtual)

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.

Sourcetype 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.

Sourcetype 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.

    *)
}

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

Sourceval default_config : unit -> config

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

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

Build a config from default_config.

Sourceval add_dir_path : ?addresses:Address.t list -> ?filter:Input.t Filter.t -> ?prefix:string -> ?config:config -> dir:string -> 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. Method is always GET.

Virtual file system.

This is used to emulate a file system from pure OCaml functions and data, e.g. for resources bundled (totally of partially) inside the web server memory.

Sourcetype dynamic = Html.chaml

VFS allows to serve dynamic content produced using such a record

dynamic content can set then headers, cookies and input of the response. If initial headers contain Headers.Cache_Control or Headers.Etag.

Sourcetype 'a content =
  1. | String of string * string option
    (*

    String(s,Some sz) is a fixed string content. s is the content, sz, if provided is a compressed version of s produced by Camlzip.

    *)
  2. | Path of string * (string * int) option
    (*

    Path(fname,Some(fzname,size)) is a real file fname, possibly with a compressed version fzname of the given size

    *)
  3. | Dynamic of dynamic
    (*

    Dynamic content

    *)
  4. | Stream of Input.t
    (*

    Streamed content

    *)
  5. | Fd of Unix.file_descr
    (*

    Content with a file descriptor, that will be send using sendfile.

    *)
  6. | Dir of 'a
    (*

    Used internally

    *)

Here is the type of content that can be served by a VFS

Sourcetype file_info =
  1. | FI : {
    1. content : 'a content;
      (*

      content

      *)
    2. size : int option;
      (*

      size (must be accurate, if provided, will use chunked encoding if not provided.

      *)
    3. mtime : float option;
      (*

      modification time for caching

      *)
    4. headers : Headers.t;
      (*

      extra headers

      *)
    } -> file_info

Description of a file in a VFS

Sourcemodule type VFS = sig ... end
Sourceval vfs_of_dir : string -> (module VFS)

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

Sourceval add_vfs : ?addresses:Address.t list -> ?filter:Input.t Filter.t -> ?prefix:string -> ?config:config -> vfs:(module VFS) -> Server.t -> unit

Similar to add_dir_path but using a virtual file system instead.

Sourcemodule Embedded_fs : sig ... end

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