WPILibC++ 2024.3.2
wpi::HttpParser Class Reference

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_viewurl
 URL callback. More...
 
sig::Signal< std::string_viewstatus
 Status callback. More...
 
sig::Signal< std::string_view, std::string_viewheader
 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...
 

Detailed Description

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.

Member Enumeration Documentation

◆ Type

Enumerator
kRequest 
kResponse 
kBoth 

Constructor & Destructor Documentation

◆ HttpParser()

wpi::HttpParser::HttpParser ( Type  type)
explicit

Constructor.

Parameters
typeType of parser (request or response or both)

Member Function Documentation

◆ Abort()

void wpi::HttpParser::Abort ( )
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).

◆ Execute()

std::string_view wpi::HttpParser::Execute ( std::string_view  in)
inline

Executes the parser.

An empty input is treated as EOF.

Parameters
ininput data
Returns
Trailing input data after the parse.

◆ GetError()

http_errno wpi::HttpParser::GetError ( ) const
inline

Get error number.

◆ GetMajor()

unsigned int wpi::HttpParser::GetMajor ( ) const
inline

Get HTTP major version.

◆ GetMethod()

http_method wpi::HttpParser::GetMethod ( ) const
inline

Get HTTP method.

Valid only on requests.

◆ GetMinor()

unsigned int wpi::HttpParser::GetMinor ( ) const
inline

Get HTTP minor version.

◆ GetParserVersion()

static uint32_t wpi::HttpParser::GetParserVersion ( )
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.

◆ GetStatusCode()

unsigned int wpi::HttpParser::GetStatusCode ( ) const
inline

Get HTTP status code.

Valid only on responses. Valid in and after the OnStatus() callback has been called.

◆ GetUrl()

std::string_view wpi::HttpParser::GetUrl ( ) const
inline

Get URL.

Valid in and after the url callback has been called.

◆ HasError()

bool wpi::HttpParser::HasError ( ) const
inline

Determine if an error occurred.

Returns
False if no error.

◆ IsBodyFinal()

bool wpi::HttpParser::IsBodyFinal ( ) const
inline

Checks if this is the final chunk of the body.

◆ IsUpgrade()

bool wpi::HttpParser::IsUpgrade ( ) const
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().

Returns
True if upgrade header, false otherwise.

◆ Pause()

void wpi::HttpParser::Pause ( bool  paused)
inline

Pause the parser.

Parameters
pausedTrue to pause, false to unpause.

◆ Reset()

void wpi::HttpParser::Reset ( Type  type)

Reset the parser to initial state.

This allows reusing the same parser object from request to request.

Parameters
typeType of parser (request or response or both)

◆ SetMaxLength()

void wpi::HttpParser::SetMaxLength ( size_t  len)
inline

Set the maximum accepted length for URLs, field names, and field values.

The default is 1024.

Parameters
lenmaximum length

◆ ShouldKeepAlive()

bool wpi::HttpParser::ShouldKeepAlive ( ) const
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.

Member Data Documentation

◆ body

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

◆ chunkComplete

sig::Signal wpi::HttpParser::chunkComplete

Chunk complete callback.

◆ chunkHeader

sig::Signal<uint64_t> wpi::HttpParser::chunkHeader

Chunk header callback.

The parameter to the callback is the chunk size.

◆ header

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.

◆ headersComplete

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.

◆ messageBegin

sig::Signal wpi::HttpParser::messageBegin

Message begin callback.

◆ messageComplete

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.

◆ status

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.

◆ url

sig::Signal<std::string_view> wpi::HttpParser::url

URL callback.

The parameter to the callback is the complete URL string.


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