Module Webs

module Webs: sig .. end
Web service interface.

Webs defines a generic, low-level, interface for web services implemented in OCaml. The interaction between the service and the HTTP web server that runs it is mediated by a connector whose details depend on the web server. Webs defines a generic connector interface that connectors are encouraged to reuse.

Consult the basics and examples of use. Open the module to use it, it defines only modules and types in your scope.

Webs is inspired by WAI.

Release 0.0.0-3-g2bbb3e4 - Daniel B├╝nzli <daniel.buenzl i@erratique.ch>



Dictionaries


type dict 
The type for type-safe heterogenous dictionaries.
module Dict: sig .. end
Dictionaries.

Services


type req 
The type for HTTP requests.
type resp 
The type for HTTP responses.
type service = req -> resp 
The type for services. Maps HTTP requests to responses.
type layer = service -> service 
The type for layers. Maps services to services.
module HTTP: sig .. end
HTTP types, fragment codecs and constants.
module Req: sig .. end
HTTP requests.
module Resp: sig .. end
HTTP response.

Connectors


module Connector: sig .. end
Generic web server connector interface.
type connector = Connector.t 
The type for generic webserver connectors.

Basics

A web service is simply a function mapping HTTP requests to HTTP responses. Here is an example of a simple service that entices to revolt on any GET request and errors on any other method:

open Webs

let revolt r = match Req.meth r with
| `GET ->
    let text = "text/plain; charset=UTF-8" in
    let headers = HTTP.H.(empty |> def content_type text) in
    Resp.v HTTP.s200_ok headers (Resp.string_body "Revolt!\n")
| _ ->
    let headers = HTTP.H.(empty |> def allow (HTTP.encode_meth `GET)) in
    Resp.v HTTP.s405_not_allowed headers Resp.empty_body


Basics

A web service is simply a function mapping HTTP requests to HTTP responses. Here is an example of a simple service that entices to revolt on any GET request and errors on any other method:

open Webs

let revolt r = match Req.meth r with
| `GET ->
    let text = "text/plain; charset=UTF-8" in
    let headers = HTTP.H.(empty |> def content_type text) in
    Resp.v HTTP.s200_ok headers (Resp.string_body "Revolt!\n")
| _ ->
    let headers = HTTP.H.(empty |> def allow (HTTP.encode_meth `GET)) in
    Resp.v HTTP.s405_not_allowed headers Resp.empty_body


Examples

Simple service via CGI

Timing the response

Forward req and analyse return headers, if content-type = text/html, add comment with time.