SNode.C
Loading...
Searching...
No Matches
web::http::decoder::Fields Class Reference

#include <Fields.h>

Collaboration diagram for web::http::decoder::Fields:

Public Member Functions

 Fields (core::socket::stream::SocketContext *socketContext, std::set< std::string > fieldsExpected={})
 
 Fields (Fields &)=delete
 
 Fields (Fields &&)=delete
 
Fieldsoperator= (Fields &)=delete
 
Fieldsoperator= (Fields &&)=delete
 
void setFieldsExpected (std::set< std::string > fieldsExpected)
 
std::size_t read ()
 
web::http::CiStringMap< std::string > && getHeader ()
 
bool isComplete () const
 
bool isError () const
 
int getErrorCode () const
 
std::string getErrorReason ()
 

Private Member Functions

void splitLine (const std::string &line)
 

Private Attributes

core::socket::stream::SocketContextsocketContext
 
web::http::CiStringMap< std::string > fields
 
std::set< std::string > fieldsExpected
 
std::string line
 
std::size_t maxLineLength
 
bool completed = false
 
int errorCode = 0
 
std::string errorReason
 
char lastButOne = '\0'
 
char last = '\0'
 

Detailed Description

Definition at line 61 of file Fields.h.

Constructor & Destructor Documentation

◆ Fields() [1/3]

web::http::decoder::Fields::Fields ( core::socket::stream::SocketContext socketContext,
std::set< std::string >  fieldsExpected = {} 
)
explicit

Definition at line 60 of file Fields.cpp.

62 , fieldsExpected(std::move(fieldsExpected))
64 }
#define MAX_LINE_LENGTH
Definition Fields.cpp:55
core::socket::stream::SocketContext * socketContext
Definition Fields.h:85
std::size_t maxLineLength
Definition Fields.h:91
std::set< std::string > fieldsExpected
Definition Fields.h:88

References fieldsExpected, maxLineLength, and socketContext.

Referenced by web::http::Parser::Parser().

Here is the caller graph for this function:

◆ Fields() [2/3]

web::http::decoder::Fields::Fields ( Fields )
delete

◆ Fields() [3/3]

web::http::decoder::Fields::Fields ( Fields &&  )
delete

Member Function Documentation

◆ getErrorCode()

int web::http::decoder::Fields::getErrorCode ( ) const

Definition at line 171 of file Fields.cpp.

171 {
172 return errorCode;
173 }

References errorCode.

Referenced by web::http::Parser::readHeader(), and web::http::Parser::readTrailer().

Here is the caller graph for this function:

◆ getErrorReason()

std::string web::http::decoder::Fields::getErrorReason ( )

Definition at line 175 of file Fields.cpp.

175 {
176 return errorReason;
177 }
std::string errorReason
Definition Fields.h:95

References errorReason.

Referenced by web::http::Parser::readHeader(), and web::http::Parser::readTrailer().

Here is the caller graph for this function:

◆ getHeader()

web::http::CiStringMap< std::string > && web::http::decoder::Fields::getHeader ( )

Definition at line 159 of file Fields.cpp.

159 {
160 return std::move(fields);
161 }
web::http::CiStringMap< std::string > fields
Definition Fields.h:87

References fields.

Referenced by web::http::Parser::readHeader(), and web::http::Parser::readTrailer().

Here is the caller graph for this function:

◆ isComplete()

bool web::http::decoder::Fields::isComplete ( ) const

Definition at line 163 of file Fields.cpp.

163 {
164 return completed;
165 }

References completed.

Referenced by web::http::Parser::readHeader(), and web::http::Parser::readTrailer().

Here is the caller graph for this function:

◆ isError()

bool web::http::decoder::Fields::isError ( ) const

Definition at line 167 of file Fields.cpp.

167 {
168 return errorCode != 0;
169 }

References errorCode.

Referenced by web::http::Parser::readHeader(), and web::http::Parser::readTrailer().

Here is the caller graph for this function:

◆ operator=() [1/2]

Fields & web::http::decoder::Fields::operator= ( Fields &&  )
delete

◆ operator=() [2/2]

Fields & web::http::decoder::Fields::operator= ( Fields )
delete

◆ read()

std::size_t web::http::decoder::Fields::read ( )

Definition at line 70 of file Fields.cpp.

70 {
71 std::size_t consumed = 0;
72
73 if (completed || errorCode != 0) {
74 completed = false;
75 fields.clear();
76 errorCode = 0;
77 errorReason = "";
78 }
79
80 std::size_t ret = 0;
81 do {
82 char ch = '\0';
83
84 ret = socketContext->readFromPeer(&ch, 1);
85 consumed += ret;
86
87 if (ret > 0) {
88 if (!line.empty() || ch != ' ') {
89 line += ch;
90
91 if (maxLineLength == 0 || line.size() <= maxLineLength) {
93 last = ch;
94
95 if (lastButOne == '\r' && last == '\n') {
96 line.pop_back(); // Remove \n
97 line.pop_back(); // Remove \r
98
99 completed = line.empty();
100 if (!completed) {
102
103 if (!fieldsExpected.empty() && fields.size() > fieldsExpected.size()) {
104 errorCode = 400;
105 errorReason = "Too many fields";
106 }
107 } else if (!fieldsExpected.empty() && fields.size() < fieldsExpected.size()) {
108 errorCode = 400;
109 errorReason = "Too view fields";
110
111 completed = false;
112 }
113 line.clear();
114 lastButOne = '\0';
115 last = '\0';
116 }
117 } else {
118 errorCode = 431;
119 errorReason = "Line too long: " + line;
120 }
121 } else {
122 errorCode = 400;
123 errorReason = "Header Folding";
124 }
125 }
126 } while (ret > 0 && !completed && errorCode == 0);
127
128 return consumed;
129 }
std::size_t readFromPeer(char *chunk, std::size_t chunklen) const final
void splitLine(const std::string &line)
Definition Fields.cpp:131

References completed, errorCode, errorReason, fields, fieldsExpected, last, lastButOne, line, maxLineLength, core::socket::stream::SocketContext::readFromPeer(), socketContext, and splitLine().

Referenced by web::http::Parser::readHeader(), and web::http::Parser::readTrailer().

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

◆ setFieldsExpected()

void web::http::decoder::Fields::setFieldsExpected ( std::set< std::string >  fieldsExpected)

Definition at line 66 of file Fields.cpp.

66 {
67 this->fieldsExpected = std::move(fieldsExpected);
68 }

References fieldsExpected.

Referenced by web::http::Parser::analyzeHeader().

Here is the caller graph for this function:

◆ splitLine()

void web::http::decoder::Fields::splitLine ( const std::string &  line)
private

Definition at line 131 of file Fields.cpp.

131 {
132 auto [headerFieldName, value] = httputils::str_split(line, ':');
133
134 if (headerFieldName.empty()) {
135 errorCode = 400;
136 errorReason = "Header field empty";
137 } else if ((std::isblank(headerFieldName.back()) != 0) || (std::isblank(headerFieldName.front()) != 0)) {
138 errorCode = 400;
139 errorReason = "White space before or after field";
140 } else if (value.empty()) {
141 errorCode = 400;
142 errorReason = "Value of field \"" + headerFieldName + "\" empty";
143 } else {
144 if (fieldsExpected.empty() || fieldsExpected.contains(headerFieldName)) {
146
147 if (!fields.contains(headerFieldName)) {
148 fields.emplace(headerFieldName, value);
149 } else {
150 fields[headerFieldName] += "," + value;
151 }
152 } else if (!fieldsExpected.empty() && !fieldsExpected.contains(headerFieldName)) {
153 errorCode = 400;
154 errorReason = "Field '" + headerFieldName + "' not in expected fields";
155 }
156 }
157 }
std::pair< std::string, std::string > str_split(const std::string &base, char c_middle)
std::string & str_trimm(std::string &text)

References errorCode, errorReason, fields, fieldsExpected, httputils::str_split(), and httputils::str_trimm().

Referenced by read().

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

Member Data Documentation

◆ completed

bool web::http::decoder::Fields::completed = false
private

Definition at line 93 of file Fields.h.

Referenced by isComplete(), and read().

◆ errorCode

int web::http::decoder::Fields::errorCode = 0
private

Definition at line 94 of file Fields.h.

Referenced by getErrorCode(), isError(), read(), and splitLine().

◆ errorReason

std::string web::http::decoder::Fields::errorReason
private

Definition at line 95 of file Fields.h.

Referenced by getErrorReason(), read(), and splitLine().

◆ fields

web::http::CiStringMap<std::string> web::http::decoder::Fields::fields
private

Definition at line 87 of file Fields.h.

Referenced by getHeader(), read(), and splitLine().

◆ fieldsExpected

std::set<std::string> web::http::decoder::Fields::fieldsExpected
private

Definition at line 88 of file Fields.h.

Referenced by Fields(), read(), setFieldsExpected(), and splitLine().

◆ last

char web::http::decoder::Fields::last = '\0'
private

Definition at line 98 of file Fields.h.

Referenced by read().

◆ lastButOne

char web::http::decoder::Fields::lastButOne = '\0'
private

Definition at line 97 of file Fields.h.

Referenced by read().

◆ line

std::string web::http::decoder::Fields::line
private

Definition at line 90 of file Fields.h.

Referenced by read().

◆ maxLineLength

std::size_t web::http::decoder::Fields::maxLineLength
private

Definition at line 91 of file Fields.h.

Referenced by Fields(), and read().

◆ socketContext

core::socket::stream::SocketContext* web::http::decoder::Fields::socketContext
private

Definition at line 85 of file Fields.h.

Referenced by Fields(), and read().


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