|
Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
|
Incremental HTTP/1.1 request parser, built on llhttp (the parser Node.js uses). More...
#include <Http1Parser.hpp>
Classes | |
| struct | Limits |
Public Types | |
| enum class | Event { NeedMore , HeadersComplete , MessageComplete , Error } |
Public Member Functions | |
| Http1Parser (Limits limits) | |
| Http1Parser () | |
| ~Http1Parser () | |
| Http1Parser (const Http1Parser &)=delete | |
| Http1Parser & | operator= (const Http1Parser &)=delete |
| Event | feed (std::string_view data, size_t &consumed) |
| void | next () |
| HttpRequest & | request () |
| void | set_body_handler (std::function< void(std::string_view)> handler) |
| void | set_body_limit (std::function< size_t(const HttpRequest &)> limit_for) |
| bool | expect_continue () const |
| "Expect: 100-continue" was sent. | |
| bool | chunked () const |
| Transfer-Encoding: chunked. | |
| bool | has_body () const |
| A body follows the headers (chunked or Content-Length > 0). | |
| bool | keep_alive () const |
| The connection may carry another request. | |
| bool | upgrade () const |
| At MessageComplete: the request asks to switch protocols. | |
| int | error_status () const |
| 400, 413, 431 or 501 after Error. | |
| const std::string & | error_reason () const |
Private Attributes | |
| std::unique_ptr< Impl > | impl_ |
Incremental HTTP/1.1 request parser, built on llhttp (the parser Node.js uses).
Bytes are fed as they arrive, in pieces of any size; nothing is rescanned. Parsing is strict (RFC 9112, no lenient flags), so ambiguous framing that could smuggle a request is rejected. The resulting HttpRequest owns its header names, values and body.
The parser pauses up to twice per request:
Limits are enforced as bytes arrive: an over-long request line or header section fails before its terminating CRLF is seen, and a Content-Length above the body limit fails before any of the body is read.
|
strong |
| Enumerator | |
|---|---|
| NeedMore | All input consumed; feed more. |
| HeadersComplete | Paused after the header section of a request with a body. |
| MessageComplete | Paused after a whole request; call next(). |
| Error | See error_status() and error_reason(). |
|
explicit |
|
inline |
| orbit::http::Http1Parser::~Http1Parser | ( | ) |
|
delete |
| bool orbit::http::Http1Parser::chunked | ( | ) | const |
Transfer-Encoding: chunked.
| const std::string & orbit::http::Http1Parser::error_reason | ( | ) | const |
| int orbit::http::Http1Parser::error_status | ( | ) | const |
400, 413, 431 or 501 after Error.
| bool orbit::http::Http1Parser::expect_continue | ( | ) | const |
"Expect: 100-continue" was sent.
| Event orbit::http::Http1Parser::feed | ( | std::string_view | data, |
| size_t & | consumed | ||
| ) |
Parses from data, stopping at the next pause. consumed is set to the number of bytes used; the rest must be fed again later.
| bool orbit::http::Http1Parser::has_body | ( | ) | const |
A body follows the headers (chunked or Content-Length > 0).
| bool orbit::http::Http1Parser::keep_alive | ( | ) | const |
The connection may carry another request.
| void orbit::http::Http1Parser::next | ( | ) |
After MessageComplete: discards the request and readies the parser for the next one on the same connection.
|
delete |
| HttpRequest & orbit::http::Http1Parser::request | ( | ) |
The request being parsed. Valid from HeadersComplete (or, without a body, MessageComplete); its body is set at MessageComplete (empty when a body handler took the body).
| void orbit::http::Http1Parser::set_body_handler | ( | std::function< void(std::string_view)> | handler | ) |
Receives the body as it arrives instead of buffering it. Set after HeadersComplete; cleared by next().
| void orbit::http::Http1Parser::set_body_limit | ( | std::function< size_t(const HttpRequest &)> | limit_for | ) |
Chooses the body size limit per request, from its method, path and headers, in place of Limits::max_body_size (e.g. none for a route that streams large uploads). Called once the headers are parsed, before a declared Content-Length is checked. Stays set across requests.
| bool orbit::http::Http1Parser::upgrade | ( | ) | const |
At MessageComplete: the request asks to switch protocols.
|
private |