WPILibC++ 2023.4.3-108-ge5452e3
|
HTTP protocol parser. More...
#include <wpinet/HttpParser.h>
Public Types | |
enum | Type { kRequest = HTTP_REQUEST , kResponse = HTTP_RESPONSE , kBoth = HTTP_BOTH } |
Public Member Functions | |
HttpParser (Type type) | |
Constructor. More... | |
void | Reset (Type type) |
Reset the parser to initial state. More... | |
void | SetMaxLength (size_t len) |
Set the maximum accepted length for URLs, field names, and field values. More... | |
std::string_view | Execute (std::string_view in) |
Executes the parser. More... | |
unsigned int | GetMajor () const |
Get HTTP major version. More... | |
unsigned int | GetMinor () const |
Get HTTP minor version. More... | |
unsigned int | GetStatusCode () const |
Get HTTP status code. More... | |
http_method | GetMethod () const |
Get HTTP method. More... | |
bool | HasError () const |
Determine if an error occurred. More... | |
http_errno | GetError () const |
Get error number. More... | |
void | Abort () |
Abort the parse. More... | |
bool | IsUpgrade () const |
Determine if an upgrade header was present and the parser has exited because of that. More... | |
bool | ShouldKeepAlive () const |
If this returns false in the headersComplete or messageComplete callback, then this should be the last message on the connection. More... | |
void | Pause (bool paused) |
Pause the parser. More... | |
bool | IsBodyFinal () const |
Checks if this is the final chunk of the body. More... | |
std::string_view | GetUrl () const |
Get URL. More... | |
Static Public Member Functions | |
static uint32_t | GetParserVersion () |
Returns the library version. More... | |
Public Attributes | |
sig::Signal | messageBegin |
Message begin callback. More... | |
sig::Signal< std::string_view > | url |
URL callback. More... | |
sig::Signal< std::string_view > | status |
Status callback. More... | |
sig::Signal< std::string_view, std::string_view > | header |
Header field callback. More... | |
sig::Signal< bool > | headersComplete |
Headers complete callback. More... | |
sig::Signal< std::string_view, bool > | body |
Body data callback. More... | |
sig::Signal< bool > | messageComplete |
Headers complete callback. More... | |
sig::Signal< uint64_t > | chunkHeader |
Chunk header callback. More... | |
sig::Signal | chunkComplete |
Chunk complete callback. More... | |
HTTP protocol parser.
Performs incremental parsing with callbacks for each part of the HTTP protocol. As this is incremental, it's suitable for use with event based frameworks that provide arbitrary chunks of data.
|
explicit |
Constructor.
type | Type of parser (request or response or both) |
|
inline |
Abort the parse.
Call this from a callback handler to indicate an error. This will result in GetError() returning one of the callback-related errors (e.g. HPE_CB_message_begin).
|
inline |
Executes the parser.
An empty input is treated as EOF.
in | input data |
|
inline |
Get error number.
|
inline |
Get HTTP major version.
|
inline |
Get HTTP method.
Valid only on requests.
|
inline |
Get HTTP minor version.
|
static |
Returns the library version.
Bits 16-23 contain the major version number, bits 8-15 the minor version number and bits 0-7 the patch level.
|
inline |
Get HTTP status code.
Valid only on responses. Valid in and after the OnStatus() callback has been called.
|
inline |
Get URL.
Valid in and after the url callback has been called.
|
inline |
Determine if an error occurred.
|
inline |
Checks if this is the final chunk of the body.
|
inline |
Determine if an upgrade header was present and the parser has exited because of that.
Should be checked when Execute() returns in addition to checking GetError().
|
inline |
Pause the parser.
paused | True to pause, false to unpause. |
void wpi::HttpParser::Reset | ( | Type | type | ) |
Reset the parser to initial state.
This allows reusing the same parser object from request to request.
type | Type of parser (request or response or both) |
|
inline |
Set the maximum accepted length for URLs, field names, and field values.
The default is 1024.
len | maximum length |
|
inline |
If this returns false in the headersComplete or messageComplete callback, then this should be the last message on the connection.
If you are the server, respond with the "Connection: close" header. If you are the client, close the connection.
sig::Signal<std::string_view, bool> wpi::HttpParser::body |
Body data callback.
The parameters to the callback is the data chunk and whether this is the final chunk of data in the message. Note this callback will be called multiple times arbitrarily (e.g. it's possible that it may be called with just a few characters at a time).
sig::Signal wpi::HttpParser::chunkComplete |
Chunk complete callback.
sig::Signal<uint64_t> wpi::HttpParser::chunkHeader |
Chunk header callback.
The parameter to the callback is the chunk size.
sig::Signal<std::string_view, std::string_view> wpi::HttpParser::header |
Header field callback.
The parameters to the callback are the field name and field value.
sig::Signal<bool> wpi::HttpParser::headersComplete |
Headers complete callback.
The parameter to the callback is whether the connection should be kept alive. If this is false, then this should be the last message on the connection. If you are the server, respond with the "Connection: close" header. If you are the client, close the connection.
sig::Signal wpi::HttpParser::messageBegin |
Message begin callback.
sig::Signal<bool> wpi::HttpParser::messageComplete |
Headers complete callback.
The parameter to the callback is whether the connection should be kept alive. If this is false, then this should be the last message on the connection. If you are the server, respond with the "Connection: close" header. If you are the client, close the connection.
sig::Signal<std::string_view> wpi::HttpParser::status |
Status callback.
The parameter to the callback is the complete status string. GetStatusCode() can be used to get the numeric status code.
sig::Signal<std::string_view> wpi::HttpParser::url |
URL callback.
The parameter to the callback is the complete URL string.