SNode.C
Loading...
Searching...
No Matches
web::http::decoder::Chunked::Chunk Class Reference
Collaboration diagram for web::http::decoder::Chunked::Chunk:

Public Member Functions

 Chunk ()=default
 
 Chunk (const Chunk &)=delete
 
 Chunk (Chunk &&) noexcept=default
 
Chunkoperator= (const Chunk &)=delete
 
Chunkoperator= (Chunk &&) noexcept=default
 
 ~Chunk ()
 
std::size_t read (const core::socket::stream::SocketContext *socketContext)
 
bool isError () const
 
bool isComplete () const
 
std::vector< char >::iterator begin ()
 
std::vector< char >::iterator end ()
 
std::size_t size ()
 

Private Attributes

std::vector< char > chunk
 
std::string chunkLenTotalS
 
std::size_t chunkLenTotal = 0
 
std::size_t chunkLenRead = 0
 
int state = 0
 
bool CR = false
 
bool LF = false
 
bool error = false
 
bool completed = false
 

Detailed Description

Definition at line 63 of file Chunked.h.

Constructor & Destructor Documentation

◆ Chunk() [1/3]

web::http::decoder::Chunked::Chunk::Chunk ( )
default

◆ Chunk() [2/3]

web::http::decoder::Chunked::Chunk::Chunk ( const Chunk )
delete

◆ Chunk() [3/3]

web::http::decoder::Chunked::Chunk::Chunk ( Chunk &&  )
defaultnoexcept

◆ ~Chunk()

web::http::decoder::Chunked::Chunk::~Chunk ( )

Definition at line 93 of file Chunked.cpp.

93 {
94 }

Member Function Documentation

◆ begin()

std::vector< char >::iterator web::http::decoder::Chunked::Chunk::begin ( )

Definition at line 215 of file Chunked.cpp.

215 {
216 return chunk.begin();
217 }
std::vector< char > chunk
Definition Chunked.h:86

References chunk.

Referenced by web::http::decoder::Chunked::read().

Here is the caller graph for this function:

◆ end()

std::vector< char >::iterator web::http::decoder::Chunked::Chunk::end ( )

Definition at line 219 of file Chunked.cpp.

219 {
220 return chunk.end();
221 }

References chunk.

Referenced by web::http::decoder::Chunked::read().

Here is the caller graph for this function:

◆ isComplete()

bool web::http::decoder::Chunked::Chunk::isComplete ( ) const
inline

Definition at line 211 of file Chunked.cpp.

211 {
212 return completed;
213 }

References completed.

Referenced by web::http::decoder::Chunked::read().

Here is the caller graph for this function:

◆ isError()

bool web::http::decoder::Chunked::Chunk::isError ( ) const
inline

Definition at line 207 of file Chunked.cpp.

207 {
208 return error;
209 }

References error.

Referenced by web::http::decoder::Chunked::read().

Here is the caller graph for this function:

◆ operator=() [1/2]

Chunk & web::http::decoder::Chunked::Chunk::operator= ( Chunk &&  )
defaultnoexcept

◆ operator=() [2/2]

Chunk & web::http::decoder::Chunked::Chunk::operator= ( const Chunk )
delete

◆ read()

std::size_t web::http::decoder::Chunked::Chunk::read ( const core::socket::stream::SocketContext socketContext)
inline

Definition at line 96 of file Chunked.cpp.

96 {
97 std::size_t consumed = 0;
98 std::size_t ret = 0;
99 std::size_t pos = 0;
100
101 const static int maxChunkLenTotalS = sizeof(std::size_t) * 2;
102
103 switch (state) {
104 case -1: // Re-init
105 CR = false;
106 LF = false;
107 chunkLenTotalS.clear();
108 chunkLenTotal = 0;
109 chunkLenRead = 0;
110
111 completed = false;
112 error = false;
113
114 state = 0;
115
116 [[fallthrough]];
117 case 0: // ChunklLenS
118 do {
119 char ch = '\0';
120
121 ret = socketContext->readFromPeer(&ch, 1);
122 if (ret > 0) {
123 consumed++;
124
125 if (CR) {
126 if (ch == '\n') {
127 LF = true;
128 } else {
129 error = true;
130 }
131 } else if (ch == '\r') {
132 CR = true;
133 } else {
134 chunkLenTotalS += ch;
135 }
136 }
137 } while (!error && ret > 0 && !(CR && LF) && chunkLenTotalS.size() <= maxChunkLenTotalS);
138
139 if (!(CR && LF)) {
140 error = !error ? chunkLenTotalS.size() > maxChunkLenTotalS : error;
141
142 if (error) {
143 state = -1;
144 }
145
146 break;
147 }
148
149 CR = false;
150 LF = false;
151
152 try {
153 chunkLenTotal = std::stoul(chunkLenTotalS, &pos, 16);
154 chunk.resize(chunkLenTotal);
155
156 state = 1;
157 } catch (std::invalid_argument&) {
158 error = true;
159 break;
160 }
161
162 [[fallthrough]];
163 case 1: // ChunkData
164 do {
166 chunkLenRead += ret;
167 consumed += ret;
168 } while (ret > 0 && (chunkLenTotal != chunkLenRead));
169
171 break;
172 }
173
174 state = 2;
175
176 [[fallthrough]];
177 case 2: // Closing "\r\n"
178 char ch = '\0';
179
180 ret = socketContext->readFromPeer(&ch, 1);
181 consumed += ret;
182
183 if (ret > 0) {
184 if (CR || ch == '\r') {
185 if (CR && ch == '\n') {
186 LF = true;
187 completed = true;
188 state = -1;
189 } else if (CR) {
190 error = true;
191 state = -1;
192 } else {
193 CR = true;
194 }
195 } else {
196 error = true;
197 state = -1;
198 }
199 }
200
201 break;
202 }
203
204 return consumed;
205 }
std::size_t readFromPeer(char *chunk, std::size_t chunklen) const final
const core::socket::stream::SocketContext * socketContext
Definition Chunked.h:113

References chunk, chunkLenRead, chunkLenTotal, chunkLenTotalS, completed, CR, error, LF, core::socket::stream::SocketContext::readFromPeer(), and state.

Referenced by web::http::decoder::Chunked::read().

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

◆ size()

std::size_t web::http::decoder::Chunked::Chunk::size ( )

Definition at line 223 of file Chunked.cpp.

223 {
224 return chunk.size();
225 }

References chunk.

Referenced by web::http::decoder::Chunked::read().

Here is the caller graph for this function:

Member Data Documentation

◆ chunk

std::vector<char> web::http::decoder::Chunked::Chunk::chunk
private

Definition at line 86 of file Chunked.h.

Referenced by begin(), end(), read(), and size().

◆ chunkLenRead

std::size_t web::http::decoder::Chunked::Chunk::chunkLenRead = 0
private

Definition at line 90 of file Chunked.h.

Referenced by read().

◆ chunkLenTotal

std::size_t web::http::decoder::Chunked::Chunk::chunkLenTotal = 0
private

Definition at line 89 of file Chunked.h.

Referenced by read().

◆ chunkLenTotalS

std::string web::http::decoder::Chunked::Chunk::chunkLenTotalS
private

Definition at line 88 of file Chunked.h.

Referenced by read().

◆ completed

bool web::http::decoder::Chunked::Chunk::completed = false
private

Definition at line 98 of file Chunked.h.

Referenced by isComplete(), and read().

◆ CR

bool web::http::decoder::Chunked::Chunk::CR = false
private

Definition at line 94 of file Chunked.h.

Referenced by read().

◆ error

bool web::http::decoder::Chunked::Chunk::error = false
private

Definition at line 97 of file Chunked.h.

Referenced by isError(), and read().

◆ LF

bool web::http::decoder::Chunked::Chunk::LF = false
private

Definition at line 95 of file Chunked.h.

Referenced by read().

◆ state

int web::http::decoder::Chunked::Chunk::state = 0
private

Definition at line 92 of file Chunked.h.

Referenced by read().


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