SNode.C
Loading...
Searching...
No Matches
web::http::Parser Class Referenceabstract

#include <Parser.h>

Inheritance diagram for web::http::Parser:
Collaboration diagram for web::http::Parser:

Public Member Functions

 Parser (core::socket::stream::SocketContext *socketContext, const enum HTTPCompliance &compliance=HTTPCompliance::RFC2616|HTTPCompliance::RFC7230)
 
virtual ~Parser ()
 
std::size_t parse ()
 
void reset ()
 

Protected Types

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 Member Functions

virtual void analyzeHeader ()
 

Protected Attributes

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

static const std::regex httpVersionRegex
 

Private Member Functions

virtual void begin ()=0
 
virtual void parseStartLine (const std::string &line)=0
 
virtual void parseError (int code, const std::string &reason)=0
 
virtual void parsingFinished ()=0
 
std::size_t readStartLine ()
 
std::size_t readHeader ()
 
std::size_t readContent ()
 
std::size_t readTrailer ()
 

Private Attributes

web::http::decoder::Fields headerDecoder
 
std::set< std::string > trailerFieldsExpected
 
web::http::decoder::Fields trailerDecoder
 

Friends

enum HTTPCompliance operator| (const enum HTTPCompliance &c1, const enum HTTPCompliance &c2)
 
enum HTTPCompliance operator& (const enum HTTPCompliance &c1, const enum HTTPCompliance &c2)
 

Detailed Description

Definition at line 71 of file Parser.h.

Member Enumeration Documentation

◆ HTTPCompliance

enum struct web::http::Parser::HTTPCompliance : unsigned short
strongprotected
Enumerator
RFC1945 
RFC2616 
RFC7230 
RFC7231 
RFC7232 
RFC7233 
RFC7234 
RFC7235 
RFC7540 
RFC7541 

Definition at line 73 of file Parser.h.

73 : unsigned short {
74 RFC1945 = 0x01 << 0, // HTTP 1.0
75 RFC2616 = 0x01 << 1, // HTTP 1.1
76 RFC7230 = 0x01 << 2, // Message Syntax and Routing
77 RFC7231 = 0x01 << 3, // Semantics and Content
78 RFC7232 = 0x01 << 4, // Conditional Requests
79 RFC7233 = 0x01 << 5, // Range Requests
80 RFC7234 = 0x01 << 6, // Caching
81 RFC7235 = 0x01 << 7, // Authentication
82 RFC7540 = 0x01 << 8, // HTTP 2.0
83 RFC7541 = 0x01 << 9 // Header Compression
enum web::http::Parser::HTTPCompliance hTTPCompliance

◆ ParserState

Constructor & Destructor Documentation

◆ Parser()

web::http::Parser::Parser ( core::socket::stream::SocketContext * socketContext,
const enum HTTPCompliance & compliance = HTTPCompliance::RFC2616 | HTTPCompliance::RFC7230 )
explicit

Definition at line 63 of file Parser.cpp.

64 : hTTPCompliance(compliance)
68 }
web::http::decoder::Fields headerDecoder
Definition Parser.h:130
core::socket::stream::SocketContext * socketContext
Definition Parser.h:127
web::http::decoder::Fields trailerDecoder
Definition Parser.h:133

◆ ~Parser()

web::http::Parser::~Parser ( )
virtual

Definition at line 70 of file Parser.cpp.

70 {
71 reset();
72 }

References reset().

Here is the call graph for this function:

Member Function Documentation

◆ analyzeHeader()

void web::http::Parser::analyzeHeader ( )
protectedvirtual

Reimplemented in web::http::client::ResponseParser, and web::http::server::RequestParser.

Definition at line 160 of file Parser.cpp.

160 {
161 if (headers.contains("Content-Length")) {
162 contentLength = std::stoul(headers["Content-Length"]);
165 }
166 if (headers.contains("Transfer-Encoding")) {
167 const std::string& encoding = headers["Transfer-Encoding"];
168
169 if (web::http::ciContains(encoding, "chunked")) {
172
173 if (headers.contains("Trailer")) {
174 std::string trailers = headers["Trailer"];
175
176 while (!trailers.empty()) {
177 std::string trailerField;
178 std::tie(trailerField, trailers) = httputils::str_split(trailers, ',');
179 httputils::str_trimm(trailerField);
180 trailerFieldsExpected.insert(trailerField);
181 trailerField.clear();
182 }
184 }
185 }
186 if (web::http::ciContains(encoding, "compressed")) {
187 // decoderQueue.emplace_back(new web::http::decoder::Compress(socketContext));
188 }
189 if (web::http::ciContains(encoding, "deflate")) {
190 // decoderQueue.emplace_back(new web::http::decoder::Deflate(socketContext));
191 }
192 if (web::http::ciContains(encoding, "gzip")) {
193 // decoderQueue.emplace_back(new web::http::decoder::GZip(socketContext));
194 }
195 }
196 if (decoderQueue.empty()) {
199 }
200
201 if (headers.contains("Content-Encoding")) {
202 const std::string& encoding = headers["Content-Encoding"];
203
204 if (web::http::ciContains(encoding, "compressed")) {
205 // decoderQueue.emplace_back(new web::http::decoder::Compress(socketContext));
206 }
207 if (web::http::ciContains(encoding, "deflate")) {
208 // decoderQueue.emplace_back(new web::http::decoder::Deflate(socketContext));
209 }
210 if (web::http::ciContains(encoding, "gzip")) {
211 // decoderQueue.emplace_back(new web::http::decoder::GZip(socketContext));
212 }
213 if (web::http::ciContains(encoding, "br")) {
214 // decoderQueue.emplace_back(new web::http::decoder::Br(socketContext));
215 }
216 }
217 }
std::size_t contentLength
Definition Parser.h:143
CiStringMap< std::string > headers
Definition Parser.h:119
std::list< web::http::ContentDecoder * > decoderQueue
Definition Parser.h:125
TransferEncoding transferEncoding
Definition Parser.h:102
std::set< std::string > trailerFieldsExpected
Definition Parser.h:132
void setFieldsExpected(std::set< std::string > fieldsExpected)
Definition Fields.cpp:66
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)

Referenced by web::http::client::ResponseParser::analyzeHeader(), web::http::server::RequestParser::analyzeHeader(), and readHeader().

Here is the caller graph for this function:

◆ begin()

virtual void web::http::Parser::begin ( )
privatepure virtual

◆ parse()

std::size_t web::http::Parser::parse ( )

Definition at line 92 of file Parser.cpp.

92 {
93 std::size_t ret = 0;
94 std::size_t consumed = 0;
95
96 do {
97 switch (parserState) {
99 begin();
101 [[fallthrough]];
103 ret = readStartLine();
104 break;
106 ret = readHeader();
107 break;
109 ret = readContent();
110 break;
112 ret = readTrailer();
113 break;
115 break;
116 }
117 consumed += ret;
118 } while (ret > 0 && parserState != ParserState::BEGIN && parserState != ParserState::ERROR);
119
120 return consumed;
121 }
ParserState parserState
Definition Parser.h:100
std::size_t readContent()
Definition Parser.cpp:219
virtual void begin()=0
std::size_t readTrailer()
Definition Parser.cpp:242
std::size_t readStartLine()
Definition Parser.cpp:123
std::size_t readHeader()
Definition Parser.cpp:147

◆ parseError()

virtual void web::http::Parser::parseError ( int code,
const std::string & reason )
privatepure virtual

◆ parseStartLine()

virtual void web::http::Parser::parseStartLine ( const std::string & line)
privatepure virtual

◆ parsingFinished()

virtual void web::http::Parser::parsingFinished ( )
privatepure virtual

Implemented in web::http::client::ResponseParser, and web::http::server::RequestParser.

Referenced by readContent(), and readTrailer().

Here is the caller graph for this function:

◆ readContent()

std::size_t web::http::Parser::readContent ( )
private

Definition at line 219 of file Parser.cpp.

219 {
220 ContentDecoder* contentDecoder = decoderQueue.front();
221
222 const std::size_t consumed = contentDecoder->read();
223
224 if (contentDecoder->isComplete()) {
225 contentDecoder = decoderQueue.back();
226
227 std::vector<char> chunk = contentDecoder->getContent();
228 content.insert(content.end(), chunk.begin(), chunk.end());
229
230 if (transferEncoding == TransferEncoding::Chunked && headers.contains("Trailer")) {
232 } else {
234 }
235 } else if (contentDecoder->isError()) {
236 parseError(400, "Wrong content encoding");
237 }
238
239 return consumed;
240 }
virtual void parseError(int code, const std::string &reason)=0
virtual void parsingFinished()=0
std::vector< char > content
Definition Parser.h:120

References parserState, parsingFinished(), and TRAILER.

Here is the call graph for this function:

◆ readHeader()

std::size_t web::http::Parser::readHeader ( )
private

Definition at line 147 of file Parser.cpp.

147 {
148 const std::size_t consumed = headerDecoder.read();
149
150 if (headerDecoder.isError()) {
152 } else if (headerDecoder.isComplete()) {
153 headers = std::move(headerDecoder.getHeader());
155 }
156
157 return consumed;
158 }
virtual void analyzeHeader()
Definition Parser.cpp:160
std::string getErrorReason()
Definition Fields.cpp:175
web::http::CiStringMap< std::string > && getHeader()
Definition Fields.cpp:159

References analyzeHeader().

Here is the call graph for this function:

◆ readStartLine()

std::size_t web::http::Parser::readStartLine ( )
private

Definition at line 123 of file Parser.cpp.

123 {
124 std::size_t consumed = 0;
125 std::size_t ret = 0;
126
127 do {
128 char ch = 0;
129 ret = socketContext->readFromPeer(&ch, 1);
130
131 if (ret > 0) {
132 consumed += ret;
133 if (ch == '\r' || ch == '\n') {
134 if (ch == '\n') {
136 line.clear();
137 }
138 } else {
139 line += ch;
140 }
141 }
142 } while (ret > 0 && parserState == ParserState::FIRSTLINE);
143
144 return consumed;
145 }
std::size_t readFromPeer(char *chunk, std::size_t chunklen) const final
virtual void parseStartLine(const std::string &line)=0
std::string line
Definition Parser.h:142

◆ readTrailer()

std::size_t web::http::Parser::readTrailer ( )
private

Definition at line 242 of file Parser.cpp.

242 {
243 const std::size_t consumed = trailerDecoder.read();
244
245 if (trailerDecoder.isError()) {
247 } else if (trailerDecoder.isComplete()) {
249 headers.insert(trailer.begin(), trailer.end());
251 }
252
253 return consumed;
254 }

References parsingFinished().

Here is the call graph for this function:

◆ reset()

void web::http::Parser::reset ( )

Definition at line 74 of file Parser.cpp.

74 {
76 headers.clear();
77 content.clear();
78 httpMinor = 0;
79 httpMajor = 0;
80 line.clear();
81 contentLength = 0;
83
84 for (ContentDecoder* contentDecoder : decoderQueue) {
85 delete contentDecoder;
86 }
87 decoderQueue.clear();
88
90 }
std::size_t contentLengthRead
Definition Parser.h:144

References BEGIN, httpMajor, httpMinor, and parserState.

Referenced by web::http::client::ResponseParser::parseError(), web::http::server::RequestParser::parseError(), web::http::client::ResponseParser::parsingFinished(), web::http::server::RequestParser::parsingFinished(), and ~Parser().

Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ operator&

enum HTTPCompliance operator& ( const enum HTTPCompliance & c1,
const enum HTTPCompliance & c2 )
friend

Definition at line 260 of file Parser.cpp.

260 {
261 return static_cast<enum Parser::HTTPCompliance>(static_cast<unsigned short>(c1) & static_cast<unsigned short>(c2));
262 }

◆ operator|

enum HTTPCompliance operator| ( const enum HTTPCompliance & c1,
const enum HTTPCompliance & c2 )
friend

Definition at line 256 of file Parser.cpp.

256 {
257 return static_cast<enum Parser::HTTPCompliance>(static_cast<unsigned short>(c1) | static_cast<unsigned short>(c2));
258 }

Member Data Documentation

◆ content

std::vector<char> web::http::Parser::content
protected

Definition at line 120 of file Parser.h.

◆ contentLength

std::size_t web::http::Parser::contentLength = 0
protected

Definition at line 143 of file Parser.h.

◆ contentLengthRead

std::size_t web::http::Parser::contentLengthRead = 0
protected

Definition at line 144 of file Parser.h.

◆ decoderQueue

std::list<web::http::ContentDecoder*> web::http::Parser::decoderQueue
protected

Definition at line 125 of file Parser.h.

◆ headerDecoder

web::http::decoder::Fields web::http::Parser::headerDecoder
private

Definition at line 130 of file Parser.h.

◆ headers

CiStringMap<std::string> web::http::Parser::headers
protected

Definition at line 119 of file Parser.h.

◆ hTTPCompliance

enum web::http::Parser::HTTPCompliance web::http::Parser::hTTPCompliance
protected

◆ httpMajor

int web::http::Parser::httpMajor = 0
protected

Definition at line 122 of file Parser.h.

Referenced by reset().

◆ httpMinor

int web::http::Parser::httpMinor = 0
protected

Definition at line 123 of file Parser.h.

Referenced by reset().

◆ httpVersionRegex

const std::regex web::http::Parser::httpVersionRegex
staticprotected

Definition at line 104 of file Parser.h.

◆ line

std::string web::http::Parser::line
protected

Definition at line 142 of file Parser.h.

◆ parserState

◆ socketContext

core::socket::stream::SocketContext* web::http::Parser::socketContext = nullptr
protected

Definition at line 127 of file Parser.h.

◆ trailerDecoder

web::http::decoder::Fields web::http::Parser::trailerDecoder
private

Definition at line 133 of file Parser.h.

◆ trailerFieldsExpected

std::set<std::string> web::http::Parser::trailerFieldsExpected
private

Definition at line 132 of file Parser.h.

◆ transferEncoding

TransferEncoding web::http::Parser::transferEncoding = TransferEncoding::HTTP10
protected

Definition at line 102 of file Parser.h.


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