{"id":13476604,"url":"https://github.com/inhabitedtype/httpaf","last_synced_at":"2025-04-04T10:09:35.318Z","repository":{"id":41117482,"uuid":"71267311","full_name":"inhabitedtype/httpaf","owner":"inhabitedtype","description":"A high performance, memory efficient, and scalable web server written in OCaml","archived":false,"fork":false,"pushed_at":"2024-05-27T12:28:21.000Z","size":782,"stargazers_count":541,"open_issues_count":27,"forks_count":45,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-03-28T09:08:24.736Z","etag":null,"topics":["http","ocaml"],"latest_commit_sha":null,"homepage":null,"language":"OCaml","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/inhabitedtype.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-18T16:15:41.000Z","updated_at":"2025-03-23T14:25:44.000Z","dependencies_parsed_at":"2024-09-29T06:40:41.017Z","dependency_job_id":"74a2d473-2c1a-415e-996d-89f7097db04f","html_url":"https://github.com/inhabitedtype/httpaf","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inhabitedtype%2Fhttpaf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inhabitedtype%2Fhttpaf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inhabitedtype%2Fhttpaf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inhabitedtype%2Fhttpaf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/inhabitedtype","download_url":"https://codeload.github.com/inhabitedtype/httpaf/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247157283,"owners_count":20893220,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["http","ocaml"],"created_at":"2024-07-31T16:01:32.461Z","updated_at":"2025-04-04T10:09:35.291Z","avatar_url":"https://github.com/inhabitedtype.png","language":"OCaml","readme":"# http/af\n\nhttp/af is a high-performance, memory-efficient, and scalable web server for\nOCaml. It implements the HTTP 1.1 specification with respect to parsing,\nserialization, and connection pipelining as a state machine that is agnostic to\nthe underlying IO mechanism, and is therefore portable across many platform.\nIt uses the [Angstrom][angstrom] and [Faraday][faraday] libraries to implement\nthe parsing and serialization layers of the HTTP standard, hence the name.\n\n[angstrom]: https://github.com/inhabitedtype/angstrom\n[faraday]: https://github.com/inhabitedtype/faraday\n[![Build Status](https://github.com/inhabitedtype/httpaf/workflows/build/badge.svg)](https://github.com/inhabitedtype/httpaf/actions?query=workflow%3A%22build%22)]\n\n## Installation\n\nInstall the library and its dependencies via [OPAM][opam]:\n\n[opam]: http://opam.ocaml.org/\n\n```bash\nopam install httpaf\n```\n\n## Usage\n\nHere is a Hello, World! program written using httpaf. It only responds to `GET`\nrequests to the `/hello/*` target. As it does not itself do any IO, it can be\nused with both the Async and Lwt runtimes. See the [`examples`][examples] directory for\nusage of the individual runtimes.\n\n[examples]: https://github.com/inhabitedtype/httpaf/tree/master/examples\n\n```ocaml\nopen Httpaf\nmodule String = Caml.String\n\nlet invalid_request reqd status body =\n  (* Responses without an explicit length or transfer-encoding are\n     close-delimited. *)\n  let headers = Headers.of_list [ \"Connection\", \"close\" ] in\n  Reqd.respond_with_string reqd (Response.create ~headers status) body\n;;\n\nlet request_handler reqd =\n  let { Request.meth; target; _ } = Reqd.request reqd in\n  match meth with\n  | `GET -\u003e\n    begin match String.split_on_char '/' target with\n    | \"\" :: \"hello\" :: rest -\u003e\n      let who =\n        match rest with\n        | [] -\u003e \"world\"\n        | who :: _ -\u003e who\n      in\n      let response_body = Printf.sprintf \"Hello, %s!\\n\" who in\n      (* Specify the length of the response. *)\n      let headers =\n        Headers.of_list\n          [ \"Content-length\", string_of_int (String.length response_body) ]\n      in\n      Reqd.respond_with_string reqd (Response.create ~headers `OK) response_body\n    | _ -\u003e\n      let response_body = Printf.sprintf \"%S not found\\n\" target in\n      invalid_request reqd `Not_found response_body\n    end\n  | meth -\u003e\n    let response_body =\n      Printf.sprintf \"%s is not an allowed method\\n\" (Method.to_string meth)\n    in\n    invalid_request reqd `Method_not_allowed response_body\n;;\n```\n\n## Performance\n\nThe reason for http/af's existence is [mirage/ocaml-cohttp#328][328], which\nhighlights the poor scalability of cohttp. This is due to a number of factors,\nincluding poor scheduling, excessive allocation, and starvation of the server's\naccept loop. Here is a comparison chart of the data from that issue, along with\ndata from an async-based http/af server. This server was run on a VM with 3\nvirtual cores, the host being circa 2015 MacBook Pro:\n\n[328]: https://github.com/mirage/ocaml-cohttp/issues/328\n\n![http/af comparsion to cohttp](https://raw.githubusercontent.com/inhabitedtype/httpaf/master/images/httpaf-comparison.png)\n\nThe http/af latency histogram, relative to the cohttp histograms, is pretty\nmuch flat along the x-axis. Here are some additional statistics from that run\n(with latencies in milliseconds):\n\n```\n#[Mean    =       27.719, StdDeviation   =       31.570]\n#[Max     =      263.424, Total count    =      1312140]\n#[Buckets =           27, SubBuckets     =         2048]\n----------------------------------------------------------\n  1709909 requests in 1.00m, 3.33GB read\n```\n\n## Development\n\nTo install development dependencies, pin the package from the root of the\nrepository:\n\n```bash\nopam pin add -n httpaf .\nopam install --deps-only httpaf\n```\n\nAfter this, you may install a development version of the library using the\ninstall command as usual.\n\nTests can be run via dune:\n\n```bash\ndune runtest\n```\n\n## License\n\nBSD3, see LICENSE files for its text.\n","funding_links":[],"categories":["OCaml","Networking"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finhabitedtype%2Fhttpaf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finhabitedtype%2Fhttpaf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finhabitedtype%2Fhttpaf/lists"}