SNode.C
Loading...
Searching...
No Matches
web::http::server::RequestParser Class Reference

#include <RequestParser.h>

Inheritance diagram for web::http::server::RequestParser:
Collaboration diagram for web::http::server::RequestParser:

Public Member Functions

 RequestParser (core::socket::stream::SocketContext *socketContext, const std::function< void()> &onRequestStart, const std::function< void(Request &&)> &onRequestParsed, const std::function< void(int, const std::string &)> &onRequestParseError)
 
 RequestParser (const RequestParser &)=delete
 
RequestParseroperator= (const RequestParser &)=delete
 
- Public Member Functions inherited from web::http::Parser
 Parser (core::socket::stream::SocketContext *socketContext, const enum HTTPCompliance &compliance=HTTPCompliance::RFC2616|HTTPCompliance::RFC7230)
 
virtual ~Parser ()
 
std::size_t parse ()
 
void reset ()
 

Private Member Functions

virtual bool methodSupported (const std::string &method) const
 
void begin () override
 
void parseStartLine (const std::string &line) override
 
void analyzeHeader () override
 
void parsingFinished () override
 
void parseError (int code, const std::string &reason) override
 

Private Attributes

std::set< std::string > supportedMethods {"GET", "PUT", "POST", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH", "HEAD"}
 
Request request
 
std::function< void()> onRequestStart
 
std::function< void(Request &&)> onRequestParsed
 
std::function< void(int, const std::string &)> onRequestParseError
 

Additional Inherited Members

- Protected Types inherited from web::http::Parser
enum struct  HTTPCompliance : unsigned short {
  RFC1945 = 0x01 << 0 , RFC2616 = 0x01 << 1 , RFC7230 = 0x01 << 2 , RFC7231 = 0x01 << 3 ,
  RFC7232 = 0x01 << 4 , RFC7233 = 0x01 << 5 , RFC7234 = 0x01 << 6 , RFC7235 = 0x01 << 7 ,
  RFC7540 = 0x01 << 8 , RFC7541 = 0x01 << 9
}
 
enum struct  ParserState {
  BEGIN , FIRSTLINE , HEADER , BODY ,
  TRAILER , ERROR
}
 
- Protected Attributes inherited from web::http::Parser
enum web::http::Parser::HTTPCompliance hTTPCompliance
 
ParserState parserState = ParserState::BEGIN
 
TransferEncoding transferEncoding = TransferEncoding::HTTP10
 
CiStringMap< std::string > headers
 
std::vector< char > content
 
int httpMajor = 0
 
int httpMinor = 0
 
std::list< web::http::ContentDecoder * > decoderQueue
 
core::socket::stream::SocketContextsocketContext = nullptr
 
std::string line
 
std::size_t contentLength = 0
 
std::size_t contentLengthRead = 0
 
- Static Protected Attributes inherited from web::http::Parser
static const std::regex httpVersionRegex
 

Detailed Description

Definition at line 40 of file RequestParser.h.

Constructor & Destructor Documentation

◆ RequestParser() [1/2]

web::http::server::RequestParser::RequestParser ( core::socket::stream::SocketContext * socketContext,
const std::function< void()> & onRequestStart,
const std::function< void(Request &&)> & onRequestParsed,
const std::function< void(int, const std::string &)> & onRequestParseError )

Definition at line 34 of file RequestParser.cpp.

42 }
Parser(core::socket::stream::SocketContext *socketContext, const enum HTTPCompliance &compliance=HTTPCompliance::RFC2616|HTTPCompliance::RFC7230)
Definition Parser.cpp:41
core::socket::stream::SocketContext * socketContext
Definition Parser.h:105
std::function< void(int, const std::string &)> onRequestParseError
std::function< void(Request &&)> onRequestParsed
std::function< void()> onRequestStart

References RequestParser().

Referenced by RequestParser().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RequestParser() [2/2]

web::http::server::RequestParser::RequestParser ( const RequestParser & )
delete

Member Function Documentation

◆ analyzeHeader()

void web::http::server::RequestParser::analyzeHeader ( )
overrideprivatevirtual

Reimplemented from web::http::Parser.

Definition at line 89 of file RequestParser.cpp.

89 {
91
92 if (headers.contains("Connection")) {
93 const std::string& connection = headers["Connection"];
94 if (web::http::ciContains(connection, "keep-alive")) {
96 } else if (web::http::ciContains(connection, "close")) {
98 }
99 }
100
101 if (headers.contains("Cookie")) {
102 std::string& cookiesLine = headers["Cookie"];
103
104 while (!cookiesLine.empty()) {
105 std::string cookieLine;
106 std::tie(cookieLine, cookiesLine) = httputils::str_split(cookiesLine, ',');
107
108 while (!cookieLine.empty()) {
109 std::string cookie;
110 std::tie(cookie, cookieLine) = httputils::str_split(cookieLine, ';');
111
112 std::string cookieName;
113 std::string cookieValue;
114 std::tie(cookieName, cookieValue) = httputils::str_split(cookie, '=');
115
116 httputils::str_trimm(cookieName);
117 httputils::str_trimm(cookieValue);
118
119 request.cookies.emplace(cookieName, cookieValue);
120 }
121 }
122
123 headers.erase("Cookie");
124 }
125
128 } else {
130 }
131 }
ParserState parserState
Definition Parser.h:78
std::size_t contentLength
Definition Parser.h:121
CiStringMap< std::string > headers
Definition Parser.h:97
TransferEncoding transferEncoding
Definition Parser.h:80
virtual void analyzeHeader()
Definition Parser.cpp:138
CiStringMap< std::string > cookies
Definition Request.h:65
ConnectionState connectionState
Definition Request.h:53
std::pair< std::string, std::string > str_split(const std::string &base, char c_middle)
std::string & str_trimm(std::string &text)
bool ciContains(const std::string &str1, const std::string &str2)

References web::http::Parser::analyzeHeader(), web::http::Parser::BODY, web::http::Parser::parserState, and parsingFinished().

Here is the call graph for this function:

◆ begin()

void web::http::server::RequestParser::begin ( )
overrideprivatevirtual

Implements web::http::Parser.

Definition at line 48 of file RequestParser.cpp.

48 {
50 }

◆ methodSupported()

bool web::http::server::RequestParser::methodSupported ( const std::string & method) const
privatevirtual

Definition at line 44 of file RequestParser.cpp.

44 {
45 return supportedMethods.contains(method);
46 }
std::set< std::string > supportedMethods

◆ operator=()

RequestParser & web::http::server::RequestParser::operator= ( const RequestParser & )
delete

◆ parseError()

void web::http::server::RequestParser::parseError ( int code,
const std::string & reason )
overrideprivatevirtual

Implements web::http::Parser.

Definition at line 146 of file RequestParser.cpp.

146 {
147 onRequestParseError(code, reason);
148
149 reset();
150
152 }

References web::http::Parser::ERROR, web::http::Parser::parserState, and web::http::Parser::reset().

Here is the call graph for this function:

◆ parseStartLine()

void web::http::server::RequestParser::parseStartLine ( const std::string & line)
overrideprivatevirtual

Implements web::http::Parser.

Definition at line 52 of file RequestParser.cpp.

52 {
54
55 if (!line.empty()) {
56 std::string remaining;
57
58 std::tie(request.method, remaining) = httputils::str_split(line, ' ');
59 std::tie(request.url, request.httpVersion) = httputils::str_split(remaining, ' ');
60
61 std::string queriesLine;
62 std::tie(std::ignore, queriesLine) = httputils::str_split(request.url, '?');
63
65 parseError(400, "Bad request method: " + request.method);
66 } else if (request.url.empty() || request.url.front() != '/') {
67 parseError(400, "Malformed request");
68 } else {
69 std::smatch httpVersionMatch;
70 if (!std::regex_match(request.httpVersion, httpVersionMatch, httpVersionRegex)) {
71 parseError(400, "Wrong protocol-version: " + request.httpVersion);
72 } else {
73 httpMajor = std::stoi(httpVersionMatch.str(1));
74 httpMinor = std::stoi(httpVersionMatch.str(2));
75
76 while (!queriesLine.empty()) {
77 std::string query;
78
79 std::tie(query, queriesLine) = httputils::str_split(queriesLine, '&');
80 request.queries.insert(httputils::str_split(query, '='));
81 }
82 }
83 }
84 } else {
85 parseError(400, "Request-line empty");
86 }
87 }
static const std::regex httpVersionRegex
Definition Parser.h:82
std::string line
Definition Parser.h:120
void parseError(int code, const std::string &reason) override
virtual bool methodSupported(const std::string &method) const
CiStringMap< std::string > queries
Definition Request.h:63
std::string httpVersion
Definition Request.h:59

References web::http::Parser::HEADER, and web::http::Parser::parserState.

◆ parsingFinished()

void web::http::server::RequestParser::parsingFinished ( )
overrideprivatevirtual

Implements web::http::Parser.

Definition at line 133 of file RequestParser.cpp.

133 {
136 request.headers = std::move(Parser::headers);
137 request.body = std::move(content);
138
139 onRequestParsed(std::move(request));
140
141 reset();
142
144 }
std::vector< char > content
Definition Parser.h:98
std::vector< char > body
Definition Request.h:66
CiStringMap< std::string > headers
Definition Request.h:64

References web::http::Parser::BEGIN, web::http::Parser::parserState, and web::http::Parser::reset().

Referenced by analyzeHeader().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ onRequestParsed

std::function<void(Request&&)> web::http::server::RequestParser::onRequestParsed
private

Definition at line 72 of file RequestParser.h.

◆ onRequestParseError

std::function<void(int, const std::string&)> web::http::server::RequestParser::onRequestParseError
private

Definition at line 73 of file RequestParser.h.

◆ onRequestStart

std::function<void()> web::http::server::RequestParser::onRequestStart
private

Definition at line 71 of file RequestParser.h.

◆ request

Request web::http::server::RequestParser::request
private

Definition at line 68 of file RequestParser.h.

◆ supportedMethods

std::set<std::string> web::http::server::RequestParser::supportedMethods {"GET", "PUT", "POST", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH", "HEAD"}
private

Definition at line 52 of file RequestParser.h.

52{"GET", "PUT", "POST", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH", "HEAD"};

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