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 62 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 56 of file RequestParser.cpp.

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

References onRequestParsed, onRequestParseError, onRequestStart, and web::http::Parser::Parser().

Referenced by web::http::server::SocketContext::SocketContext().

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 111 of file RequestParser.cpp.

111 {
113
114 if (headers.contains("Connection")) {
115 const std::string& connection = headers["Connection"];
116 if (web::http::ciContains(connection, "keep-alive")) {
118 } else if (web::http::ciContains(connection, "close")) {
120 }
121 }
122
123 if (headers.contains("Cookie")) {
124 std::string& cookiesLine = headers["Cookie"];
125
126 while (!cookiesLine.empty()) {
127 std::string cookieLine;
128 std::tie(cookieLine, cookiesLine) = httputils::str_split(cookiesLine, ',');
129
130 while (!cookieLine.empty()) {
131 std::string cookie;
132 std::tie(cookie, cookieLine) = httputils::str_split(cookieLine, ';');
133
134 std::string cookieName;
135 std::string cookieValue;
136 std::tie(cookieName, cookieValue) = httputils::str_split(cookie, '=');
137
138 httputils::str_trimm(cookieName);
139 httputils::str_trimm(cookieValue);
140
141 request.cookies.emplace(cookieName, cookieValue);
142 }
143 }
144
145 headers.erase("Cookie");
146 }
147
150 } else {
152 }
153 }
ParserState parserState
Definition Parser.h:100
std::size_t contentLength
Definition Parser.h:143
CiStringMap< std::string > headers
Definition Parser.h:119
TransferEncoding transferEncoding
Definition Parser.h:102
virtual void analyzeHeader()
Definition Parser.cpp:160
CiStringMap< std::string > cookies
Definition Request.h:87
ConnectionState connectionState
Definition Request.h:75
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::Chunked, web::http::ciContains(), web::http::Close, web::http::server::Request::connectionState, web::http::Parser::contentLength, web::http::server::Request::cookies, web::http::Parser::headers, web::http::Keep, web::http::Parser::parserState, parsingFinished(), request, httputils::str_split(), httputils::str_trimm(), and web::http::Parser::transferEncoding.

Here is the call graph for this function:

◆ begin()

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

Implements web::http::Parser.

Definition at line 70 of file RequestParser.cpp.

70 {
72 }

References onRequestStart.

◆ methodSupported()

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

Definition at line 66 of file RequestParser.cpp.

66 {
67 return supportedMethods.contains(method);
68 }
std::set< std::string > supportedMethods

References supportedMethods.

Referenced by parseStartLine().

Here is the caller graph for this function:

◆ 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 168 of file RequestParser.cpp.

168 {
169 onRequestParseError(code, reason);
170
171 reset();
172
174 }

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

Referenced by parseStartLine().

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

◆ parseStartLine()

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

Implements web::http::Parser.

Definition at line 74 of file RequestParser.cpp.

74 {
76
77 if (!line.empty()) {
78 std::string remaining;
79
80 std::tie(request.method, remaining) = httputils::str_split(line, ' ');
81 std::tie(request.url, request.httpVersion) = httputils::str_split(remaining, ' ');
82
83 std::string queriesLine;
84 std::tie(std::ignore, queriesLine) = httputils::str_split(request.url, '?');
85
87 parseError(400, "Bad request method: " + request.method);
88 } else if (request.url.empty() || request.url.front() != '/') {
89 parseError(400, "Malformed request");
90 } else {
91 std::smatch httpVersionMatch;
92 if (!std::regex_match(request.httpVersion, httpVersionMatch, httpVersionRegex)) {
93 parseError(400, "Wrong protocol-version: " + request.httpVersion);
94 } else {
95 httpMajor = std::stoi(httpVersionMatch.str(1));
96 httpMinor = std::stoi(httpVersionMatch.str(2));
97
98 while (!queriesLine.empty()) {
99 std::string query;
100
101 std::tie(query, queriesLine) = httputils::str_split(queriesLine, '&');
102 request.queries.insert(httputils::str_split(query, '='));
103 }
104 }
105 }
106 } else {
107 parseError(400, "Request-line empty");
108 }
109 }
static const std::regex httpVersionRegex
Definition Parser.h:104
std::string line
Definition Parser.h:142
void parseError(int code, const std::string &reason) override
virtual bool methodSupported(const std::string &method) const
CiStringMap< std::string > queries
Definition Request.h:85
std::string httpVersion
Definition Request.h:81

References web::http::Parser::HEADER, web::http::Parser::httpMajor, web::http::Parser::httpMinor, web::http::server::Request::httpVersion, web::http::Parser::httpVersionRegex, web::http::server::Request::method, methodSupported(), parseError(), web::http::Parser::parserState, web::http::server::Request::queries, request, httputils::str_split(), and web::http::server::Request::url.

Here is the call graph for this function:

◆ parsingFinished()

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

Implements web::http::Parser.

Definition at line 155 of file RequestParser.cpp.

155 {
158 request.headers = std::move(Parser::headers);
159 request.body = std::move(content);
160
161 onRequestParsed(std::move(request));
162
163 reset();
164
166 }
std::vector< char > content
Definition Parser.h:120
std::vector< char > body
Definition Request.h:88
CiStringMap< std::string > headers
Definition Request.h:86

References web::http::Parser::BEGIN, web::http::server::Request::body, web::http::Parser::content, web::http::Parser::headers, web::http::server::Request::headers, web::http::Parser::httpMajor, web::http::server::Request::httpMajor, web::http::Parser::httpMinor, web::http::server::Request::httpMinor, onRequestParsed, web::http::Parser::parserState, request, 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 94 of file RequestParser.h.

Referenced by parsingFinished(), and RequestParser().

◆ onRequestParseError

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

Definition at line 95 of file RequestParser.h.

Referenced by parseError(), and RequestParser().

◆ onRequestStart

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

Definition at line 93 of file RequestParser.h.

Referenced by begin(), and RequestParser().

◆ request

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

Definition at line 90 of file RequestParser.h.

Referenced by analyzeHeader(), parseStartLine(), and parsingFinished().

◆ supportedMethods

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

Definition at line 74 of file RequestParser.h.

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

Referenced by methodSupported().


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