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
 
Response && getResponse ()
 
- 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 59 of file ResponseParser.cpp.

67 }
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 110 of file ResponseParser.cpp.

110 {
112
113 if (headers.contains("Connection")) {
114 const std::string& connection = headers["Connection"];
115 if (web::http::ciContains(connection, "keep-alive")) {
117 } else if (web::http::ciContains(connection, "close")) {
119 }
120 }
121 if (headers.contains("Set-Cookie")) {
122 std::string cookiesLine = headers["Set-Cookie"];
123
124 while (!cookiesLine.empty()) {
125 std::string cookieLine;
126 std::tie(cookieLine, cookiesLine) = httputils::str_split(cookiesLine, ',');
127
128 std::string cookieOptions;
129 std::string cookie;
130 std::tie(cookie, cookieOptions) = httputils::str_split(cookieLine, ';');
131
132 std::string cookieName;
133 std::string cookieValue;
134 std::tie(cookieName, cookieValue) = httputils::str_split(cookie, '=');
135
136 httputils::str_trimm(cookieName);
137 httputils::str_trimm(cookieValue);
138
139 std::map<std::string, CookieOptions>::iterator cookieElement;
140 bool inserted = false;
141 std::tie(cookieElement, inserted) = response.cookies.insert({cookieName, CookieOptions(cookieValue)});
142
143 while (!cookieOptions.empty()) {
144 std::string option;
145 std::tie(option, cookieOptions) = httputils::str_split(cookieOptions, ';');
146
147 std::string optionName;
148 std::string optionValue;
149 std::tie(optionName, optionValue) = httputils::str_split(option, '=');
150
151 httputils::str_trimm(optionName);
152 httputils::str_trimm(optionValue);
153
154 cookieElement->second.setOption(optionName, optionValue);
155 }
156 }
157
158 Parser::headers.erase("Set-Cookie");
159 }
160
163 } else {
165 }
166 }
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:85
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 77 of file ResponseParser.cpp.

77 {
79 }

References onResponseStart.

◆ getResponse()

Response && web::http::client::ResponseParser::getResponse ( )

Definition at line 69 of file ResponseParser.cpp.

69 {
70 if (!decoderQueue.empty()) {
71 response.body = decoderQueue.back()->getContent();
72 }
73
74 return std::move(response);
75 }
std::list< web::http::ContentDecoder * > decoderQueue
Definition Parser.h:125
std::vector< char > body
Definition Response.h:86

References web::http::client::Response::body, web::http::Parser::decoderQueue, web::http::ContentDecoder::getContent(), and response.

Referenced by web::http::client::SocketContext::onDisconnected().

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

◆ 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 181 of file ResponseParser.cpp.

181 {
182 onResponseParseError(code, reason);
183
184 reset();
185
187 }

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 81 of file ResponseParser.cpp.

81 {
83
84 if (!line.empty()) {
85 std::string remaining;
86
87 std::tie(response.httpVersion, remaining) = httputils::str_split(line, ' ');
88
89 std::smatch httpVersionMatch;
90 if (!std::regex_match(response.httpVersion, httpVersionMatch, httpVersionRegex)) {
91 parseError(400, "Wrong protocol version: " + response.httpVersion);
92 } else {
93 httpMajor = std::stoi(httpVersionMatch.str(1));
94 httpMinor = std::stoi(httpVersionMatch.str(2));
95
96 std::tie(response.statusCode, response.reason) = httputils::str_split(remaining, ' ');
98 if (response.reason.empty()) {
99 parseError(400, "No reason phrase");
100 }
101 } else {
102 parseError(400, "Unknown status code");
103 }
104 }
105 } else {
106 parseError(400, "Response line empty");
107 }
108 }
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::Parser::httpMajor, web::http::Parser::httpMinor, web::http::client::Response::httpVersion, web::http::Parser::httpVersionRegex, parseError(), web::http::Parser::parserState, web::http::client::Response::reason, response, web::http::client::Response::statusCode, and httputils::str_split().

Here is the call graph for this function:

◆ parsingFinished()

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

Implements web::http::Parser.

Definition at line 168 of file ResponseParser.cpp.

168 {
171 response.headers = std::move(headers);
172 response.body = std::move(content);
173
174 onResponseParsed(std::move(response));
175
176 reset();
177
179 }
std::vector< char > content
Definition Parser.h:120
CiStringMap< std::string > headers
Definition Response.h:84

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

Referenced by parseError(), and ResponseParser().

◆ onResponseStart

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

Definition at line 90 of file ResponseParser.h.

Referenced by begin(), and ResponseParser().

◆ response

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

Definition at line 87 of file ResponseParser.h.

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


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