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 41 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 71 of file Chunked.cpp.

71 {
72 }

Member Function Documentation

◆ begin()

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

Definition at line 193 of file Chunked.cpp.

193 {
194 return chunk.begin();
195 }
std::vector< char > chunk
Definition Chunked.h:64

◆ end()

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

Definition at line 197 of file Chunked.cpp.

197 {
198 return chunk.end();
199 }

◆ isComplete()

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

Definition at line 189 of file Chunked.cpp.

189 {
190 return completed;
191 }

References completed.

◆ isError()

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

Definition at line 185 of file Chunked.cpp.

185 {
186 return error;
187 }

References error.

◆ 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 74 of file Chunked.cpp.

74 {
75 std::size_t consumed = 0;
76 std::size_t ret = 0;
77 std::size_t pos = 0;
78
79 const static int maxChunkLenTotalS = sizeof(std::size_t) * 2;
80
81 switch (state) {
82 case -1: // Re-init
83 CR = false;
84 LF = false;
85 chunkLenTotalS.clear();
86 chunkLenTotal = 0;
87 chunkLenRead = 0;
88
89 completed = false;
90 error = false;
91
92 state = 0;
93
94 [[fallthrough]];
95 case 0: // ChunklLenS
96 do {
97 char ch = '\0';
98
99 ret = socketContext->readFromPeer(&ch, 1);
100 if (ret > 0) {
101 consumed++;
102
103 if (CR) {
104 if (ch == '\n') {
105 LF = true;
106 } else {
107 error = true;
108 }
109 } else if (ch == '\r') {
110 CR = true;
111 } else {
112 chunkLenTotalS += ch;
113 }
114 }
115 } while (!error && ret > 0 && !(CR && LF) && chunkLenTotalS.size() <= maxChunkLenTotalS);
116
117 if (!(CR && LF)) {
118 error = !error ? chunkLenTotalS.size() > maxChunkLenTotalS : error;
119
120 if (error) {
121 state = -1;
122 }
123
124 break;
125 }
126
127 CR = false;
128 LF = false;
129
130 try {
131 chunkLenTotal = std::stoul(chunkLenTotalS, &pos, 16);
132 chunk.resize(chunkLenTotal);
133
134 state = 1;
135 } catch (std::invalid_argument&) {
136 error = true;
137 break;
138 }
139
140 [[fallthrough]];
141 case 1: // ChunkData
142 do {
144 chunkLenRead += ret;
145 consumed += ret;
146 } while (ret > 0 && (chunkLenTotal != chunkLenRead));
147
149 break;
150 }
151
152 state = 2;
153
154 [[fallthrough]];
155 case 2: // Closing "\r\n"
156 char ch = '\0';
157
158 ret = socketContext->readFromPeer(&ch, 1);
159 consumed += ret;
160
161 if (ret > 0) {
162 if (CR || ch == '\r') {
163 if (CR && ch == '\n') {
164 LF = true;
165 completed = true;
166 state = -1;
167 } else if (CR) {
168 error = true;
169 state = -1;
170 } else {
171 CR = true;
172 }
173 } else {
174 error = true;
175 state = -1;
176 }
177 }
178
179 break;
180 }
181
182 return consumed;
183 }
std::size_t readFromPeer(char *chunk, std::size_t chunklen) const final
const core::socket::stream::SocketContext * socketContext
Definition Chunked.h:91

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

Here is the call graph for this function:

◆ size()

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

Definition at line 201 of file Chunked.cpp.

201 {
202 return chunk.size();
203 }

Member Data Documentation

◆ chunk

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

Definition at line 64 of file Chunked.h.

◆ chunkLenRead

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

Definition at line 68 of file Chunked.h.

Referenced by read().

◆ chunkLenTotal

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

Definition at line 67 of file Chunked.h.

Referenced by read().

◆ chunkLenTotalS

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

Definition at line 66 of file Chunked.h.

Referenced by read().

◆ completed

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

Definition at line 76 of file Chunked.h.

Referenced by isComplete(), and read().

◆ CR

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

Definition at line 72 of file Chunked.h.

Referenced by read().

◆ error

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

Definition at line 75 of file Chunked.h.

Referenced by isError(), and read().

◆ LF

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

Definition at line 73 of file Chunked.h.

Referenced by read().

◆ state

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

Definition at line 70 of file Chunked.h.

Referenced by read().


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