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

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

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

88 {
90
91 if (headers.contains("Connection")) {
92 const std::string& connection = headers["Connection"];
93 if (web::http::ciContains(connection, "keep-alive")) {
95 } else if (web::http::ciContains(connection, "close")) {
97 }
98 }
99 if (headers.contains("Set-Cookie")) {
100 std::string cookiesLine = headers["Set-Cookie"];
101
102 while (!cookiesLine.empty()) {
103 std::string cookieLine;
104 std::tie(cookieLine, cookiesLine) = httputils::str_split(cookiesLine, ',');
105
106 std::string cookieOptions;
107 std::string cookie;
108 std::tie(cookie, cookieOptions) = httputils::str_split(cookieLine, ';');
109
110 std::string cookieName;
111 std::string cookieValue;
112 std::tie(cookieName, cookieValue) = httputils::str_split(cookie, '=');
113
114 httputils::str_trimm(cookieName);
115 httputils::str_trimm(cookieValue);
116
117 std::map<std::string, CookieOptions>::iterator cookieElement;
118 bool inserted = false;
119 std::tie(cookieElement, inserted) = response.cookies.insert({cookieName, CookieOptions(cookieValue)});
120
121 while (!cookieOptions.empty()) {
122 std::string option;
123 std::tie(option, cookieOptions) = httputils::str_split(cookieOptions, ';');
124
125 std::string optionName;
126 std::string optionValue;
127 std::tie(optionName, optionValue) = httputils::str_split(option, '=');
128
129 httputils::str_trimm(optionName);
130 httputils::str_trimm(optionValue);
131
132 cookieElement->second.setOption(optionName, optionValue);
133 }
134 }
135
136 Parser::headers.erase("Set-Cookie");
137 }
138
141 } else {
143 }
144 }
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< CookieOptions > cookies
Definition Response.h:63
ConnectionState connectionState
Definition Response.h:52
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::client::ResponseParser::begin ( )
overrideprivatevirtual

Implements web::http::Parser.

Definition at line 55 of file ResponseParser.cpp.

55 {
57 }

◆ getResponse()

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

Definition at line 47 of file ResponseParser.cpp.

47 {
48 if (!decoderQueue.empty()) {
49 response.body = decoderQueue.back()->getContent();
50 }
51
52 return std::move(response);
53 }
std::list< web::http::ContentDecoder * > decoderQueue
Definition Parser.h:103
std::vector< char > body
Definition Response.h:64

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

159 {
160 onResponseParseError(code, reason);
161
162 reset();
163
165 }

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::client::ResponseParser::parseStartLine ( const std::string & line)
overrideprivatevirtual

Implements web::http::Parser.

Definition at line 59 of file ResponseParser.cpp.

59 {
61
62 if (!line.empty()) {
63 std::string remaining;
64
65 std::tie(response.httpVersion, remaining) = httputils::str_split(line, ' ');
66
67 std::smatch httpVersionMatch;
68 if (!std::regex_match(response.httpVersion, httpVersionMatch, httpVersionRegex)) {
69 parseError(400, "Wrong protocol version: " + response.httpVersion);
70 } else {
71 httpMajor = std::stoi(httpVersionMatch.str(1));
72 httpMinor = std::stoi(httpVersionMatch.str(2));
73
74 std::tie(response.statusCode, response.reason) = httputils::str_split(remaining, ' ');
76 if (response.reason.empty()) {
77 parseError(400, "No reason phrase");
78 }
79 } else {
80 parseError(400, "Unknown status code");
81 }
82 }
83 } else {
84 parseError(400, "Response line empty");
85 }
86 }
static const std::regex httpVersionRegex
Definition Parser.h:82
std::string line
Definition Parser.h:120
static bool contains(int status)
void parseError(int code, const std::string &reason) override

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

◆ parsingFinished()

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

Implements web::http::Parser.

Definition at line 146 of file ResponseParser.cpp.

146 {
149 response.headers = std::move(headers);
150 response.body = std::move(content);
151
152 onResponseParsed(std::move(response));
153
154 reset();
155
157 }
std::vector< char > content
Definition Parser.h:98
CiStringMap< std::string > headers
Definition Response.h:62

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

◆ onResponseParsed

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

Definition at line 69 of file ResponseParser.h.

◆ onResponseParseError

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

Definition at line 70 of file ResponseParser.h.

◆ onResponseStart

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

Definition at line 68 of file ResponseParser.h.

◆ response

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

Definition at line 65 of file ResponseParser.h.


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