FastLED 3.9.15
Loading...
Searching...
No Matches
chunked_encoding.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/span.h"
4#include "fl/stl/vector.h"
5#include "fl/stl/stdint.h"
6#include "fl/stl/noexcept.h"
7
8namespace fl {
9namespace net {
10namespace http {
11
12// Result of a ChunkedReader::readChunk() call
14 enum Status {
15 CHUNKED_NO_DATA, // No complete chunk available yet
16 CHUNKED_DATA, // Chunk data written to output span
17 CHUNKED_FINAL // Final (zero-length) chunk received, stream ended
18 };
19
21 fl::span<const u8> mData; // Subspan of caller's buffer containing the bytes read
22
25
26 bool hasData() const { return mStatus == CHUNKED_DATA; }
27 bool isFinal() const { return mStatus == CHUNKED_FINAL; }
28};
29
30// ChunkedReader: Parse HTTP/1.1 chunked transfer encoding
31// Format: <chunk-size-hex>\r\n<chunk-data>\r\n ... 0\r\n\r\n
33public:
35
36 // Feed raw bytes received from a socket into the parser (incremental/streaming)
37 void feed(fl::span<const u8> data);
38
39 // Check if complete chunk is available
40 bool hasChunk() const;
41
42 // Read next chunk into caller-provided buffer.
43 // On CHUNKED_DATA, result.mData is a subspan of `out` containing the bytes read.
44 // If the output span is too small, the chunk remains buffered (returns CHUNKED_NO_DATA).
46
47 // Peek the size of the next available chunk (0 if none)
48 size_t nextChunkSize() const;
49
50 // Check if final chunk (size 0) received
51 bool isFinal() const;
52
53 // Reset state
54 void reset();
55
56private:
57 enum State {
58 READ_SIZE, // Reading chunk size (hex + CRLF)
59 READ_DATA, // Reading chunk data
60 READ_TRAILER, // Reading trailing CRLF
61 STATE_FINAL // Final chunk received
62 };
63
66 size_t mChunkSize;
67 size_t mBytesRead;
68 fl::vector<fl::vector<u8>> mChunks; // Buffered complete chunks
69 fl::vector<u8> mCurrentChunk; // Chunk being assembled (not yet complete)
70
71 // Parse hex chunk size from buffer
72 bool parseChunkSize(size_t& outSize);
73
74 // Check if buffer contains CRLF at current position
75 bool hasCRLF() const;
76
77 // Consume n bytes from buffer
78 void consume(size_t n);
79};
80
81// ChunkedWriter: Format HTTP/1.1 chunked transfer encoding
83public:
85
86 // Write chunk into caller-provided buffer.
87 // Output format: <size-hex>\r\n<data>\r\n
88 // Returns number of bytes written, or 0 if output span is too small.
90
91 // Write final chunk into caller-provided buffer.
92 // Output format: 0\r\n\r\n (always 5 bytes)
93 // Returns number of bytes written, or 0 if output span is too small.
94 size_t writeFinal(fl::span<u8> out);
95
96 // Compute the output size needed for writeChunk with the given data length
97 static size_t chunkOverhead(size_t dataLen);
98
99 // Final chunk is always 5 bytes: "0\r\n\r\n"
100 static constexpr size_t FINAL_SIZE = 5;
101};
102
103} // namespace http
104} // namespace net
105} // namespace fl
void feed(fl::span< const u8 > data)
ChunkedReadResult readChunk(fl::span< u8 > out)
fl::vector< fl::vector< u8 > > mChunks
size_t writeChunk(fl::span< const u8 > data, fl::span< u8 > out)
static size_t chunkOverhead(size_t dataLen)
static constexpr size_t FINAL_SIZE
size_t writeFinal(fl::span< u8 > out)
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
ChunkedReadResult(Status s, fl::span< const u8 > d)