Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
Http1Parser.hpp
Go to the documentation of this file.
1#pragma once
4
5#include <cstddef>
6#include <functional>
7#include <memory>
8#include <string>
9#include <string_view>
10
11namespace orbit::http {
12
35public:
36 struct Limits {
37 size_t max_request_line = 4096;
38 size_t max_header_bytes = 8192;
39 size_t max_headers = 100;
40 size_t max_body_size = 10 * 1024 * 1024;
41 };
42
43 enum class Event {
44 NeedMore,
47 Error,
48 };
49
50 explicit Http1Parser(Limits limits);
53 Http1Parser(const Http1Parser&) = delete;
55
58 Event feed(std::string_view data, size_t& consumed);
59
62 void next();
63
68
71 void set_body_handler(std::function<void(std::string_view)> handler);
72
77 void set_body_limit(std::function<size_t(const HttpRequest&)> limit_for);
78
79 bool expect_continue() const;
80 bool chunked() const;
81 bool has_body() const;
82 bool keep_alive() const;
83 bool upgrade() const;
84
85 int error_status() const;
86 const std::string& error_reason() const;
87
88private:
89 struct Impl;
90 std::unique_ptr<Impl> impl_;
91};
92
93} // namespace http
Incremental HTTP/1.1 request parser, built on llhttp (the parser Node.js uses).
Definition Http1Parser.hpp:34
void set_body_handler(std::function< void(std::string_view)> handler)
std::unique_ptr< Impl > impl_
Definition Http1Parser.hpp:90
Http1Parser(Limits limits)
bool keep_alive() const
The connection may carry another request.
Event
Definition Http1Parser.hpp:43
@ MessageComplete
Paused after a whole request; call next().
@ NeedMore
All input consumed; feed more.
@ Error
See error_status() and error_reason().
@ HeadersComplete
Paused after the header section of a request with a body.
Http1Parser()
Definition Http1Parser.hpp:51
Http1Parser(const Http1Parser &)=delete
HttpRequest & request()
bool chunked() const
Transfer-Encoding: chunked.
bool upgrade() const
At MessageComplete: the request asks to switch protocols.
int error_status() const
400, 413, 431 or 501 after Error.
void set_body_limit(std::function< size_t(const HttpRequest &)> limit_for)
bool expect_continue() const
"Expect: 100-continue" was sent.
bool has_body() const
A body follows the headers (chunked or Content-Length > 0).
Event feed(std::string_view data, size_t &consumed)
const std::string & error_reason() const
Http1Parser & operator=(const Http1Parser &)=delete
Definition Task.hpp:10
Definition Http1Parser.hpp:36
size_t max_headers
Definition Http1Parser.hpp:39
size_t max_body_size
Definition Http1Parser.hpp:40
size_t max_request_line
Definition Http1Parser.hpp:37
size_t max_header_bytes
Request line plus all header lines.
Definition Http1Parser.hpp:38
Represents an incoming HTTP request.
Definition HttpRequest.hpp:54