$ show mochi/mochiweb
Mochiweb is a lightweight, battle-tested HTTP server library written in Erlang. It provides a full HTTP/1.1 server with support for chunked transfer encoding, compression (gzip, zstd), WebSocket upgrades, and multipart form parsing. The library is designed as a collection of composable, parameterized modules, with a core socket server (`mochiweb_socket_server`) that manages an acceptor pool and delegates request handling to user-provided callback modules. This architecture has made Mochiweb the foundation for numerous Erlang web frameworks, including Nitrogen, Chicago Boss, and Webmachine. Key entry points include `mochiweb_http:start/1` for starting a plain HTTP listener and `mochiweb_socket_server:start/3` for custom socket servers. Request handling flows through `mochiweb_request` (parsing, body streaming, file serving) and `mochiweb_response` (header encoding, streaming, compression). The library also ships with standalone utilities: `mochijson2` for JSON encoding/decoding, `mochiweb_html` for loose HTML tokenization, `mochiweb_util` for URL/header parsing, and `mochiweb_headers` for case-insensitive header dictionaries. While the core is production-grade and actively maintained, some internal modules use deprecated OTP APIs (e.g., `httpd_util:rfc1123_date/0`, `{ssl_imp, new}`) and the state format in `mochiweb_socket_server` includes backward-compatibility shims. The example projects and test suite are comprehensive, covering edge cases from RFC 7230 and WebSocket protocols. Overall, Mochiweb remains a solid choice for embedding an HTTP server in Erlang applications, with several components (especially `mochijson2` and `mochiweb_util`) that are widely reused in isolation.
The LLM identified 3 files worth highlighting for reuse:
A standalone, high-performance JSON encoder/decoder that supports multiple output formats (proplist, struct, eep18, map) and custom handlers for special values. Its encoder is among the fastest in the Erlang ecosystem, and the decoder handles UTF-8 safely. This module is widely extracted and used in projects that need JSON processing without the full Mochiweb dependency.
A collection of well-crafted utility functions for URL splitting, query string parsing, cookie/header parsing, content negotiation, and safe relative path resolution. Each function is self-contained, documented, and handles edge cases (e.g., quoted strings, multiple cookies). The module demonstrates idiomatic Erlang pattern matching and binary processing, and can be extracted as a standalone utility library.
An HTML tokenizer/parser that accepts loose, real-world HTML (including unclosed tags, missing attributes, and invalid entities). It produces a structured token list that can be used for scraping, transformation, or sanitization. The parser is self-contained and handles character references, CDATA, and comments. This module demonstrates a clean state-machine design and is reusable for any HTML processing task in Erlang.