WPILibC++  2019.3.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
wpi::HttpParser Class Reference

HTTP protocol parser. More...

#include <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...
 
StringRef Execute (StringRef in)
 Executes the parser. More...
 
unsigned int GetMajor () const
 Get HTTP major version.
 
unsigned int GetMinor () const
 Get HTTP minor version.
 
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.
 
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.
 
StringRef 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.
 
sig::Signal< StringRefurl
 URL callback. More...
 
sig::Signal< StringRefstatus
 Status callback. More...
 
sig::Signal< StringRef, StringRefheader
 Header field callback. More...
 
sig::Signal< bool > headersComplete
 Headers complete callback. More...
 
sig::Signal< StringRef, 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.
 

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.

Constructor & Destructor Documentation

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

Constructor.

Parameters
typeType of parser (request or response or both)

Member Function Documentation

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

StringRef wpi::HttpParser::Execute ( StringRef  in)
inline

Executes the parser.

An empty input is treated as EOF.

Parameters
ininput data
Returns
Trailing input data after the parse.
http_method wpi::HttpParser::GetMethod ( ) const
inline

Get HTTP method.

Valid only on requests.

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.

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.

StringRef wpi::HttpParser::GetUrl ( ) const
inline

Get URL.

Valid in and after the url callback has been called.

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

Determine if an error occurred.

Returns
False if no error.
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.
void wpi::HttpParser::Pause ( bool  paused)
inline

Pause the parser.

Parameters
pausedTrue 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.

Parameters
typeType of parser (request or response or both)
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
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

sig::Signal<StringRef, 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<uint64_t> wpi::HttpParser::chunkHeader

Chunk header callback.

The parameter to the callback is the chunk size.

sig::Signal<StringRef, StringRef> 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<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<StringRef> 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<StringRef> 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: