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 49 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 51 of file Parser.h.

51 : unsigned short {
52 RFC1945 = 0x01 << 0, // HTTP 1.0
53 RFC2616 = 0x01 << 1, // HTTP 1.1
54 RFC7230 = 0x01 << 2, // Message Syntax and Routing
55 RFC7231 = 0x01 << 3, // Semantics and Content
56 RFC7232 = 0x01 << 4, // Conditional Requests
57 RFC7233 = 0x01 << 5, // Range Requests
58 RFC7234 = 0x01 << 6, // Caching
59 RFC7235 = 0x01 << 7, // Authentication
60 RFC7540 = 0x01 << 8, // HTTP 2.0
61 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 41 of file Parser.cpp.

42 : hTTPCompliance(compliance)
46 }
web::http::decoder::Fields headerDecoder
Definition Parser.h:108
core::socket::stream::SocketContext * socketContext
Definition Parser.h:105
web::http::decoder::Fields trailerDecoder
Definition Parser.h:111

◆ ~Parser()

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

Definition at line 48 of file Parser.cpp.

48 {
49 reset();
50 }

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 138 of file Parser.cpp.

138 {
139 if (headers.contains("Content-Length")) {
140 contentLength = std::stoul(headers["Content-Length"]);
143 }
144 if (headers.contains("Transfer-Encoding")) {
145 const std::string& encoding = headers["Transfer-Encoding"];
146
147 if (web::http::ciContains(encoding, "chunked")) {
150
151 if (headers.contains("Trailer")) {
152 std::string trailers = headers["Trailer"];
153
154 while (!trailers.empty()) {
155 std::string trailerField;
156 std::tie(trailerField, trailers) = httputils::str_split(trailers, ',');
157 httputils::str_trimm(trailerField);
158 trailerFieldsExpected.insert(trailerField);
159 trailerField.clear();
160 }
162 }
163 }
164 if (web::http::ciContains(encoding, "compressed")) {
165 // decoderQueue.emplace_back(new web::http::decoder::Compress(socketContext));
166 }
167 if (web::http::ciContains(encoding, "deflate")) {
168 // decoderQueue.emplace_back(new web::http::decoder::Deflate(socketContext));
169 }
170 if (web::http::ciContains(encoding, "gzip")) {
171 // decoderQueue.emplace_back(new web::http::decoder::GZip(socketContext));
172 }
173 }
174 if (decoderQueue.empty()) {
177 }
178
179 if (headers.contains("Content-Encoding")) {
180 const std::string& encoding = headers["Content-Encoding"];
181
182 if (web::http::ciContains(encoding, "compressed")) {
183 // decoderQueue.emplace_back(new web::http::decoder::Compress(socketContext));
184 }
185 if (web::http::ciContains(encoding, "deflate")) {
186 // decoderQueue.emplace_back(new web::http::decoder::Deflate(socketContext));
187 }
188 if (web::http::ciContains(encoding, "gzip")) {
189 // decoderQueue.emplace_back(new web::http::decoder::GZip(socketContext));
190 }
191 if (web::http::ciContains(encoding, "br")) {
192 // decoderQueue.emplace_back(new web::http::decoder::Br(socketContext));
193 }
194 }
195 }
std::size_t contentLength
Definition Parser.h:121
CiStringMap< std::string > headers
Definition Parser.h:97
std::list< web::http::ContentDecoder * > decoderQueue
Definition Parser.h:103
TransferEncoding transferEncoding
Definition Parser.h:80
std::set< std::string > trailerFieldsExpected
Definition Parser.h:110
void setFieldsExpected(std::set< std::string > fieldsExpected)
Definition Fields.cpp:44
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 70 of file Parser.cpp.

70 {
71 std::size_t ret = 0;
72 std::size_t consumed = 0;
73
74 do {
75 switch (parserState) {
77 begin();
79 [[fallthrough]];
81 ret = readStartLine();
82 break;
84 ret = readHeader();
85 break;
87 ret = readContent();
88 break;
90 ret = readTrailer();
91 break;
93 break;
94 }
95 consumed += ret;
97
98 return consumed;
99 }
ParserState parserState
Definition Parser.h:78
std::size_t readContent()
Definition Parser.cpp:197
virtual void begin()=0
std::size_t readTrailer()
Definition Parser.cpp:220
std::size_t readStartLine()
Definition Parser.cpp:101
std::size_t readHeader()
Definition Parser.cpp:125

◆ 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 197 of file Parser.cpp.

197 {
198 ContentDecoder* contentDecoder = decoderQueue.front();
199
200 const std::size_t consumed = contentDecoder->read();
201
202 if (contentDecoder->isComplete()) {
203 contentDecoder = decoderQueue.back();
204
205 std::vector<char> chunk = contentDecoder->getContent();
206 content.insert(content.end(), chunk.begin(), chunk.end());
207
208 if (transferEncoding == TransferEncoding::Chunked && headers.contains("Trailer")) {
210 } else {
212 }
213 } else if (contentDecoder->isError()) {
214 parseError(400, "Wrong content encoding");
215 }
216
217 return consumed;
218 }
virtual void parseError(int code, const std::string &reason)=0
virtual void parsingFinished()=0
std::vector< char > content
Definition Parser.h:98

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 125 of file Parser.cpp.

125 {
126 const std::size_t consumed = headerDecoder.read();
127
128 if (headerDecoder.isError()) {
130 } else if (headerDecoder.isComplete()) {
131 headers = std::move(headerDecoder.getHeader());
133 }
134
135 return consumed;
136 }
virtual void analyzeHeader()
Definition Parser.cpp:138
std::string getErrorReason()
Definition Fields.cpp:153
web::http::CiStringMap< std::string > && getHeader()
Definition Fields.cpp:137

References analyzeHeader().

Here is the call graph for this function:

◆ readStartLine()

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

Definition at line 101 of file Parser.cpp.

101 {
102 std::size_t consumed = 0;
103 std::size_t ret = 0;
104
105 do {
106 char ch = 0;
107 ret = socketContext->readFromPeer(&ch, 1);
108
109 if (ret > 0) {
110 consumed += ret;
111 if (ch == '\r' || ch == '\n') {
112 if (ch == '\n') {
114 line.clear();
115 }
116 } else {
117 line += ch;
118 }
119 }
120 } while (ret > 0 && parserState == ParserState::FIRSTLINE);
121
122 return consumed;
123 }
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:120

◆ readTrailer()

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

Definition at line 220 of file Parser.cpp.

220 {
221 const std::size_t consumed = trailerDecoder.read();
222
223 if (trailerDecoder.isError()) {
225 } else if (trailerDecoder.isComplete()) {
227 headers.insert(trailer.begin(), trailer.end());
229 }
230
231 return consumed;
232 }

References parsingFinished().

Here is the call graph for this function:

◆ reset()

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

Definition at line 52 of file Parser.cpp.

52 {
54 headers.clear();
55 content.clear();
56 httpMinor = 0;
57 httpMajor = 0;
58 line.clear();
59 contentLength = 0;
61
62 for (ContentDecoder* contentDecoder : decoderQueue) {
63 delete contentDecoder;
64 }
65 decoderQueue.clear();
66
68 }
std::size_t contentLengthRead
Definition Parser.h:122

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 238 of file Parser.cpp.

238 {
239 return static_cast<enum Parser::HTTPCompliance>(static_cast<unsigned short>(c1) & static_cast<unsigned short>(c2));
240 }

◆ operator|

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

Definition at line 234 of file Parser.cpp.

234 {
235 return static_cast<enum Parser::HTTPCompliance>(static_cast<unsigned short>(c1) | static_cast<unsigned short>(c2));
236 }

Member Data Documentation

◆ content

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

Definition at line 98 of file Parser.h.

◆ contentLength

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

Definition at line 121 of file Parser.h.

◆ contentLengthRead

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

Definition at line 122 of file Parser.h.

◆ decoderQueue

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

Definition at line 103 of file Parser.h.

◆ headerDecoder

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

Definition at line 108 of file Parser.h.

◆ headers

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

Definition at line 97 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 100 of file Parser.h.

Referenced by reset().

◆ httpMinor

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

Definition at line 101 of file Parser.h.

Referenced by reset().

◆ httpVersionRegex

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

Definition at line 82 of file Parser.h.

◆ line

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

Definition at line 120 of file Parser.h.

◆ parserState

◆ socketContext

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

Definition at line 105 of file Parser.h.

◆ trailerDecoder

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

Definition at line 111 of file Parser.h.

◆ trailerFieldsExpected

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

Definition at line 110 of file Parser.h.

◆ transferEncoding

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

Definition at line 80 of file Parser.h.


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