rust-http-box
What is http-box?
http-box is a push oriented HTTP parser with the goal of remaining fast and bare bones. There are several HTTP client and server libraries available, but http-box attempts to stay as far away from abstract as possible, giving the developer absolute and full control over how HTTP data is processed.
Features
- New: HTTP/2 support
- Push oriented and will process a single byte at a time
- Callback oriented with the ability to break out of the parser loop
- Headers are normalized to lower-case
- Error handling is a breeze
- Parse HTTP phases separately:
- Head
- Request / Response
- Headers
- Body
- Multipart
- Headers
- Data
- Chunk Transfer-Encoded
- Chunk Length
- Extensions
- Chunk Data
- Trailers
- URL encoded
- Parameters
- Multipart
- Head
- Zero copy philosophy
- DoS protection is easily supported
- Fast!
- Use with any networking library
API Documentation
https://docs.rs/http-box/0.1.1/http_box/
Quick Docs
Parser
Parser is the guts of the library. It provides only necessary components for parsing HTTP data.
HttpHandler
Implementing HttpHandler is how you provide a custom callback implementation. It is optional to provide multiple implementations based on which type of data is being parsed: head, chunked transfer-encoded, multipart, URL encoded, etc. It is also suggested since it lends itself to clarity.
Callbacks
In a typical application, callbacks receive arguments that are complete pieces of data. However, Parser parses data, and because of this, it must operate one byte at a type. Moreoever, the data being parsed is often coming from a network connection, and is received as incomplete pieces of data. To stick to the zero-copy philosophy, and to avoid buffering, callbacks are executed as frequent as necessary.
Tracking State
Sometimes multiple states need to work together to produce a single result. A good example of this is when headers are being parsed. The callback for the header name may be called multiple times in order to receive the full header name. And the same is true for the header value. It isn't until the header value is complete, that the header name/value pair can be stored.
This is where the State enum comes into play. You can use this to track the current state when a callback is executed. There is nothing mysterious about this enum. It's a helper type with the objective of simplifying state tracking.
Example
extern crate http_box;
use ;
use HashMap;
// provide a container for the data we wish to track
// implement the callbacks we are listening for