Overview
httpz includes a production-ready static file server built with OCaml’s Async library. The server demonstrates httpz’s capabilities while providing a fast, feature-rich solution for serving static content.The server can handle up to 10,000 concurrent connections with efficient zero-copy bigstring I/O.
Key Features
Async I/O
Non-blocking concurrent connection handling using OCaml Async
Zero-Copy I/O
Direct bigstring I/O without intermediate copying
MIME Types
Automatic content type detection for common file extensions
Security
Directory traversal protection and path normalization
Advanced HTTP Features
The server implements several modern HTTP/1.1 features:Range Requests (HTTP 206)
Supports byte-range requests for partial content delivery, enabling:- Resume interrupted downloads
- Stream specific portions of media files
- Efficient large file serving
Conditional Requests
The server supports efficient caching with ETags and conditional GETs:- ETag generation: Weak ETags based on file modification time and size
- If-None-Match: Returns 304 Not Modified when content hasn’t changed
- Last-Modified: Provides modification timestamp headers
Keep-Alive Connections
Persistent connections are automatically managed for improved performance:MIME Type Detection
The server automatically detects content types for common file extensions:Web Content
Web Content
- HTML:
text/html - CSS:
text/css - JavaScript:
application/javascript - JSON:
application/json
Images
Images
- PNG:
image/png - JPEG:
image/jpeg - GIF:
image/gif - SVG:
image/svg+xml - ICO:
image/x-icon
Fonts
Fonts
- WOFF:
font/woff - WOFF2:
font/woff2 - TTF:
font/ttf
Source Code
Source Code
- OCaml:
text/x-ocaml - C/C++:
text/x-c - Python:
text/x-python - Shell:
text/x-shellscript
Security Features
Directory Traversal Protection
The server normalizes paths and ensures all file access stays within the served root directory:Realpath Verification
Before serving files, the server verifies they’re within the allowed root:Directory Index
When accessing a directory, the server automatically looks forindex.html:
Performance Characteristics
The server supports up to 10,000 concurrent connections with a backlog of 128 pending connections.
Buffer Configuration
- Request buffer: 32KB (configurable via
Httpz.buffer_size) - Response buffer: 64KB for headers
- Zero-copy streaming: File contents streamed directly to writer
Next Steps
Configuration
Learn how to configure the server with command-line options
HTTP Parser
Understand the underlying zero-allocation parser