SNode.C
Loading...
Searching...
No Matches
web::http::client::ResponseParser Class Reference

#include <ResponseParser.h>

Inheritance diagram for web::http::client::ResponseParser:
Collaboration diagram for web::http::client::ResponseParser:

Public Member Functions

 ResponseParser (core::socket::stream::SocketContext *socketContext, const std::function< void()> &onResponseStart, const std::function< void(Response &)> &onResponseParsed, const std::function< void(int, const std::string &)> &onResponseParseError)
 
 ResponseParser (const ResponseParser &)=delete
 
ResponseParseroperator= (const ResponseParser &)=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

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

Response response
 
std::function< void()> onResponseStart
 
std::function< void(Response &)> onResponseParsed
 
std::function< void(int, const std::string &)> onResponseParseError
 

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 63 of file ResponseParser.h.

Constructor & Destructor Documentation

◆ ResponseParser() [1/2]

web::http::client::ResponseParser::ResponseParser ( core::socket::stream::SocketContext socketContext,
const std::function< void()> &  onResponseStart,
const std::function< void(Response &)> &  onResponseParsed,
const std::function< void(int, const std::string &)> &  onResponseParseError 
)

Definition at line 58 of file ResponseParser.cpp.

66 }
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(Response &)> onResponseParsed
std::function< void()> onResponseStart
std::function< void(int, const std::string &)> onResponseParseError

References onResponseParsed, onResponseParseError, onResponseStart, and web::http::Parser::Parser().

Referenced by apps::http::getResponseParser(), and web::http::client::SocketContext::SocketContext().

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

◆ ResponseParser() [2/2]

web::http::client::ResponseParser::ResponseParser ( const ResponseParser )
delete

Member Function Documentation

◆ analyzeHeader()

void web::http::client::ResponseParser::analyzeHeader ( )
overrideprivatevirtual

Reimplemented from web::http::Parser.

Definition at line 107 of file ResponseParser.cpp.

107 {
109
110 if (headers.contains("Connection")) {
111 const std::string& connection = headers["Connection"];
112 if (web::http::ciContains(connection, "keep-alive")) {
114 } else if (web::http::ciContains(connection, "close")) {
116 }
117 }
118 if (headers.contains("Set-Cookie")) {
119 std::string cookiesLine = headers["Set-Cookie"];
120
121 while (!cookiesLine.empty()) {
122 std::string cookieLine;
123 std::tie(cookieLine, cookiesLine) = httputils::str_split(cookiesLine, ',');
124
125 std::string cookieOptions;
126 std::string cookie;
127 std::tie(cookie, cookieOptions) = httputils::str_split(cookieLine, ';');
128
129 std::string cookieName;
130 std::string cookieValue;
131 std::tie(cookieName, cookieValue) = httputils::str_split(cookie, '=');
132
133 httputils::str_trimm(cookieName);
134 httputils::str_trimm(cookieValue);
135
136 std::map<std::string, CookieOptions>::iterator cookieElement;
137 bool inserted = false;
138 std::tie(cookieElement, inserted) = response.cookies.insert({cookieName, CookieOptions(cookieValue)});
139
140 while (!cookieOptions.empty()) {
141 std::string option;
142 std::tie(option, cookieOptions) = httputils::str_split(cookieOptions, ';');
143
144 std::string optionName;
145 std::string optionValue;
146 std::tie(optionName, optionValue) = httputils::str_split(option, '=');
147
148 httputils::str_trimm(optionName);
149 httputils::str_trimm(optionValue);
150
151 cookieElement->second.setOption(optionName, optionValue);
152 }
153 }
154
155 Parser::headers.erase("Set-Cookie");
156 }
157
160 } else {
162 }
163 }
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< CookieOptions > cookies
Definition Response.h:84
ConnectionState connectionState
Definition Response.h:74
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::ciContains(), web::http::Close, web::http::client::Response::connectionState, web::http::Parser::contentLength, web::http::CookieOptions::CookieOptions(), web::http::client::Response::cookies, web::http::Parser::headers, web::http::Identity, web::http::Keep, web::http::Parser::parserState, parsingFinished(), response, web::http::CookieOptions::setOption(), httputils::str_split(), httputils::str_trimm(), and web::http::Parser::transferEncoding.

Here is the call graph for this function:

◆ begin()

void web::http::client::ResponseParser::begin ( )
overrideprivatevirtual

Implements web::http::Parser.

Definition at line 68 of file ResponseParser.cpp.

68 {
70 }

References onResponseStart.

◆ operator=()

ResponseParser & web::http::client::ResponseParser::operator= ( const ResponseParser )
delete

◆ parseError()

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

Implements web::http::Parser.

Definition at line 178 of file ResponseParser.cpp.

178 {
179 onResponseParseError(code, reason);
180
181 reset();
182
184 }

References web::http::Parser::ERROR, onResponseParseError, 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::client::ResponseParser::parseStartLine ( const std::string &  line)
overrideprivatevirtual

Implements web::http::Parser.

Definition at line 72 of file ResponseParser.cpp.

72 {
74
75 if (!line.empty()) {
76 std::string remaining;
77
78 std::tie(response.httpVersion, remaining) = httputils::str_split(line, ' ');
79
80 std::smatch httpVersionMatch;
81 if (!std::regex_match(response.httpVersion, httpVersionMatch, httpVersionRegex)) {
82 parseError(400, "Wrong protocol version: " + response.httpVersion);
83 } else {
84 httpMajor = std::stoi(httpVersionMatch.str(1));
85 httpMinor = std::stoi(httpVersionMatch.str(2));
86
87 if (httpMinor == 0) {
89 } else if (httpMinor == 1) {
91 }
92
93 std::tie(response.statusCode, response.reason) = httputils::str_split(remaining, ' ');
95 if (response.reason.empty()) {
96 parseError(400, "No reason phrase");
97 }
98 } else {
99 parseError(400, "Unknown status code");
100 }
101 }
102 } else {
103 parseError(400, "Response line empty");
104 }
105 }
static const std::regex httpVersionRegex
Definition Parser.h:104
std::string line
Definition Parser.h:142
static bool contains(int status)
void parseError(int code, const std::string &reason) override

References web::http::StatusCode::contains(), web::http::Parser::HEADER, web::http::HTTP10, web::http::Parser::httpMajor, web::http::Parser::httpMinor, web::http::client::Response::httpVersion, web::http::Parser::httpVersionRegex, web::http::Identity, parseError(), web::http::Parser::parserState, web::http::client::Response::reason, response, web::http::client::Response::statusCode, httputils::str_split(), and web::http::Parser::transferEncoding.

Here is the call graph for this function:

◆ parsingFinished()

void web::http::client::ResponseParser::parsingFinished ( )
overrideprivatevirtual

Implements web::http::Parser.

Definition at line 165 of file ResponseParser.cpp.

165 {
168 response.headers = std::move(headers);
169 response.body = std::move(content);
170
172
173 reset();
174
176 }
std::vector< char > content
Definition Parser.h:120
CiStringMap< std::string > headers
Definition Response.h:83
std::vector< char > body
Definition Response.h:85

References web::http::Parser::BEGIN, web::http::client::Response::body, web::http::Parser::content, web::http::client::Response::headers, web::http::Parser::headers, web::http::client::Response::httpMajor, web::http::Parser::httpMajor, web::http::client::Response::httpMinor, web::http::Parser::httpMinor, onResponseParsed, web::http::Parser::parserState, web::http::Parser::reset(), and response.

Referenced by analyzeHeader().

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

Member Data Documentation

◆ onResponseParsed

std::function<void(Response&)> web::http::client::ResponseParser::onResponseParsed
private

Definition at line 89 of file ResponseParser.h.

Referenced by parsingFinished(), and ResponseParser().

◆ onResponseParseError

std::function<void(int, const std::string&)> web::http::client::ResponseParser::onResponseParseError
private

Definition at line 90 of file ResponseParser.h.

Referenced by parseError(), and ResponseParser().

◆ onResponseStart

std::function<void()> web::http::client::ResponseParser::onResponseStart
private

Definition at line 88 of file ResponseParser.h.

Referenced by begin(), and ResponseParser().

◆ response

Response web::http::client::ResponseParser::response
private

Definition at line 85 of file ResponseParser.h.

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


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