Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
ChunkedDecoder Class Reference

#include <ChunkedDecoder.h>

+ Collaboration diagram for ChunkedDecoder:

Public Member Functions

void feed (const std::string &data, size_t maxLength=MAX_HTTP_BODY_LENGTH)
 
bool feedToFile (const std::string &data, std::function< bool(const char *data, size_t len)> writeFn, size_t maxLength=MAX_HTTP_BODY_LENGTH)
 Feed data to the decoder, writing it to a file using the provided write function.
 
std::string getBuffer () const
 Get the current buffer content.
 
bool wasTruncated () const
 Set if the content-length is longer than the maximum value allowed.
 
std::string getDecoded ()
 
bool isComplete () const
 

Private Member Functions

void parseChunks (size_t maxLength)
 

Private Attributes

std::string buffer
 
std::string decoded
 
bool complete = false
 
bool truncated = false
 
size_t totalDecoded = 0
 

Detailed Description

Definition at line 5 of file ChunkedDecoder.h.

Member Function Documentation

◆ feed()

void ChunkedDecoder::feed ( const std::string &  data,
size_t  maxLength = MAX_HTTP_BODY_LENGTH 
)

Definition at line 4 of file ChunkedDecoder.cpp.

4 {
5 buffer += data;
6 parseChunks(maxLength);
7}
std::string buffer
void parseChunks(size_t maxLength)

References buffer, and parseChunks().

Referenced by HttpParser::receiveChunkedBodyToString().

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

◆ feedToFile()

bool ChunkedDecoder::feedToFile ( const std::string &  data,
std::function< bool(const char *data, size_t len)>  writeFn,
size_t  maxLength = MAX_HTTP_BODY_LENGTH 
)

This function will write the data to a file using the provided write function. If the data exceeds maxLength, it will be truncated and truncated flag will be set.

Parameters
dataThe data to feed to the decoder.
writeFnThe function to write data to a file.
maxLengthThe maximum length of data to process (default is MAX_HTTP_BODY_LENGTH).
Returns
true if all data was written successfully, false if it was truncated.
Note
The write function should return true if the data was written successfully, false otherwise. If the write function returns false, the decoder will stop processing further data. If the data exceeds maxLength, it will be truncated and the truncated flag will be set.

Definition at line 9 of file ChunkedDecoder.cpp.

12{
13 buffer += data;
14 std::istringstream stream(buffer);
15 std::string line;
16 truncated = false;
17
18 while (std::getline(stream, line)) {
19 if (line.empty()) continue;
20
21 int chunkSize = 0;
22 std::istringstream chunkHeader(line);
23 chunkHeader >> std::hex >> chunkSize;
24
25 if (chunkSize == 0) {
26 complete = true;
27 buffer.clear();
28 return true;
29 }
30
31 if ((int)stream.rdbuf()->in_avail() < chunkSize + 2) {
32 return true; // wait for more input
33 }
34
35 char* chunkData = new char[chunkSize];
36 stream.read(chunkData, chunkSize);
37
38 size_t available = maxLength - totalDecoded;
39 size_t toWrite = std::min((size_t)chunkSize, available);
40
41 if (!writeFn(chunkData, toWrite)) {
42 delete[] chunkData;
43 return false;
44 }
45
46 delete[] chunkData;
47 totalDecoded += toWrite;
48
49 if (chunkSize > (int)available) {
50 truncated = true;
51 complete = true;
52 return true;
53 }
54
55 std::getline(stream, line); // Consume CRLF
56 }
57
58 buffer.clear();
59 return true;
60}

References buffer, complete, totalDecoded, and truncated.

Referenced by HttpParser::receiveChunkedBodyToFile().

+ Here is the caller graph for this function:

◆ getBuffer()

std::string ChunkedDecoder::getBuffer ( ) const
inline


This returns the raw buffer content that has been fed to the decoder.

Returns
The current buffer content as a string.

Definition at line 34 of file ChunkedDecoder.h.

34{ return buffer; }

References buffer.

◆ getDecoded()

std::string ChunkedDecoder::getDecoded ( )

Definition at line 63 of file ChunkedDecoder.cpp.

63 {
64 return decoded;
65}
std::string decoded

References decoded.

Referenced by HttpParser::receiveChunkedBodyToString().

+ Here is the caller graph for this function:

◆ isComplete()

bool ChunkedDecoder::isComplete ( ) const

Definition at line 67 of file ChunkedDecoder.cpp.

67 {
68 return complete;
69}

References complete.

Referenced by HttpParser::receiveChunkedBodyToFile(), and HttpParser::receiveChunkedBodyToString().

+ Here is the caller graph for this function:

◆ parseChunks()

void ChunkedDecoder::parseChunks ( size_t  maxLength)
private

Definition at line 71 of file ChunkedDecoder.cpp.

71 {
72 std::istringstream stream(buffer);
73 std::string line;
74 std::string tempDecoded;
75 truncated = false;
76
77 while (std::getline(stream, line)) {
78 if (line.empty()) continue;
79
80 int chunkSize = 0;
81 std::istringstream chunkHeader(line);
82 chunkHeader >> std::hex >> chunkSize;
83
84 if (chunkSize == 0) {
85 complete = true;
86 break;
87 }
88
89 char* chunk = new char[chunkSize];
90 stream.read(chunk, chunkSize);
91
92 size_t remaining = maxLength - tempDecoded.size();
93 if (chunkSize > remaining) {
94 tempDecoded.append(chunk, remaining);
95 truncated = true;
96 delete[] chunk;
97 break;
98 }
99
100 tempDecoded.append(chunk, chunkSize);
101 delete[] chunk;
102
103 std::getline(stream, line); // Consume CRLF
104 }
105
106 decoded = std::move(tempDecoded);
107}

References buffer, complete, decoded, and truncated.

Referenced by feed().

+ Here is the caller graph for this function:

◆ wasTruncated()

bool ChunkedDecoder::wasTruncated ( ) const
inline

This checks if the decoder has processed all chunks and is ready to return the decoded data.

Returns
true if decoding is complete, false otherwise.

Definition at line 42 of file ChunkedDecoder.h.

42{ return truncated; }

References truncated.

Referenced by HttpParser::receiveChunkedBodyToFile(), and HttpParser::receiveChunkedBodyToString().

+ Here is the caller graph for this function:

Member Data Documentation

◆ buffer

std::string ChunkedDecoder::buffer
private

Definition at line 47 of file ChunkedDecoder.h.

Referenced by feed(), feedToFile(), getBuffer(), and parseChunks().

◆ complete

bool ChunkedDecoder::complete = false
private

Definition at line 49 of file ChunkedDecoder.h.

Referenced by feedToFile(), isComplete(), and parseChunks().

◆ decoded

std::string ChunkedDecoder::decoded
private

Definition at line 48 of file ChunkedDecoder.h.

Referenced by getDecoded(), and parseChunks().

◆ totalDecoded

size_t ChunkedDecoder::totalDecoded = 0
private

Definition at line 51 of file ChunkedDecoder.h.

Referenced by feedToFile().

◆ truncated

bool ChunkedDecoder::truncated = false
private

Definition at line 50 of file ChunkedDecoder.h.

Referenced by feedToFile(), parseChunks(), and wasTruncated().


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