Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
orbit::http::Http1Parser Class Reference

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_
 

Detailed Description

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:

  • HeadersComplete, only when a body follows: method, target, headers and cookies are known; the body has not been read. The caller may install a body handler (to stream it) or send "100 Continue" first, then feeds the rest.
  • MessageComplete: the request is whole. The caller handles it, then calls next() before feeding the bytes that follow (a pipelined 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.

Member Enumeration Documentation

◆ Event

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().

Constructor & Destructor Documentation

◆ Http1Parser() [1/3]

orbit::http::Http1Parser::Http1Parser ( Limits  limits)
explicit

◆ Http1Parser() [2/3]

orbit::http::Http1Parser::Http1Parser ( )
inline

◆ ~Http1Parser()

orbit::http::Http1Parser::~Http1Parser ( )

◆ Http1Parser() [3/3]

orbit::http::Http1Parser::Http1Parser ( const Http1Parser &  )
delete

Member Function Documentation

◆ chunked()

bool orbit::http::Http1Parser::chunked ( ) const

Transfer-Encoding: chunked.

◆ error_reason()

const std::string & orbit::http::Http1Parser::error_reason ( ) const

◆ error_status()

int orbit::http::Http1Parser::error_status ( ) const

400, 413, 431 or 501 after Error.

◆ expect_continue()

bool orbit::http::Http1Parser::expect_continue ( ) const

"Expect: 100-continue" was sent.

◆ feed()

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.

◆ has_body()

bool orbit::http::Http1Parser::has_body ( ) const

A body follows the headers (chunked or Content-Length > 0).

◆ keep_alive()

bool orbit::http::Http1Parser::keep_alive ( ) const

The connection may carry another request.

◆ next()

void orbit::http::Http1Parser::next ( )

After MessageComplete: discards the request and readies the parser for the next one on the same connection.

◆ operator=()

Http1Parser & orbit::http::Http1Parser::operator= ( const Http1Parser &  )
delete

◆ request()

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).

◆ set_body_handler()

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().

◆ set_body_limit()

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.

◆ upgrade()

bool orbit::http::Http1Parser::upgrade ( ) const

At MessageComplete: the request asks to switch protocols.

Member Data Documentation

◆ impl_

std::unique_ptr<Impl> orbit::http::Http1Parser::impl_
private

The documentation for this class was generated from the following file: