FastLED 3.9.15
Loading...
Searching...
No Matches
http_parser.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/shared_ptr.h"
4#include "fl/stl/span.h"
5#include "fl/stl/vector.h"
6#include "fl/stl/optional.h"
7#include "fl/stl/string.h"
8#include "fl/stl/flat_map.h"
9#include "fl/stl/stdint.h"
10#include "fl/stl/noexcept.h"
11
12// Forward declaration — breaks fl.stl+ -> fl.net+ link chain
13namespace fl { namespace net { namespace http { class ChunkedReader; } } }
14
15namespace fl {
16
17// HTTP request structure
19 fl::string method; // "GET", "POST", etc.
20 fl::string uri; // "/rpc"
21 fl::string version; // "HTTP/1.1"
23 fl::vector<u8> body; // Decoded body (if chunked, already decoded)
24};
25
26// HTTP response structure
34
39
40// HttpRequestParser: Parse HTTP/1.1 requests
42public:
45
46 // Feed raw bytes received from a socket into the parser (incremental/streaming)
47 void feed(fl::span<const u8> data);
48
49 // Check if request is complete
50 bool isComplete() const;
51
52 // Get parsed request as shared_ptr (zero-copy handoff, returns null if not complete)
54
55 // Reset state
56 void reset();
57
58 // State enum (public for debug access)
59 enum State {
60 READ_REQUEST_LINE, // "POST /rpc HTTP/1.1\r\n"
61 READ_HEADERS, // "Header: Value\r\n" ... "\r\n"
62 READ_BODY, // Body content (chunked or Content-Length)
63 COMPLETE // Request fully parsed
64 };
65
66 // Debug getters (public for testing)
67 State getState() const { return mState; }
68 size_t getBufferSize() const { return mBuffer.size(); }
69 size_t getContentLength() const { return mContentLength; }
70 bool getIsChunked() const { return mIsChunked; }
71
72private:
73
80
81 HttpRequest& req() { return *mRequest; }
82 const HttpRequest& req() const { return *mRequest; }
83
84 // Parse request line: "POST /rpc HTTP/1.1\r\n"
85 bool parseRequestLine();
86
87 // Parse headers: "Header: Value\r\n" ... "\r\n"
88 bool parseHeaders();
89
90 // Parse body (chunked or Content-Length)
91 void parseBody();
92
93 // Find CRLF in buffer
95
96 // Consume n bytes from buffer
97 void consume(size_t n);
98
99 // Get header value (case-insensitive)
100 fl::optional<fl::string> getHeader(const char* name) const;
101};
102
103// HttpResponseParser: Parse HTTP/1.1 responses
105public:
108
109 // Feed raw bytes received from a socket into the parser (incremental/streaming)
110 void feed(fl::span<const u8> data);
111
112 // Check if response is complete
113 bool isComplete() const;
114
115 // Get parsed response as shared_ptr (zero-copy handoff, returns null if not complete)
117
118 // Reset state
119 void reset();
120
121 // State enum (public for debug access)
122 enum State {
123 READ_STATUS_LINE, // "HTTP/1.1 200 OK\r\n"
124 READ_HEADERS, // "Header: Value\r\n" ... "\r\n"
125 READ_BODY, // Body content (chunked or Content-Length)
126 COMPLETE // Response fully parsed
127 };
128
129 // Debug getters (public for testing)
130 State getState() const { return mState; }
131 size_t getBufferSize() const { return mBuffer.size(); }
132 size_t getContentLength() const { return mContentLength; }
133 bool getIsChunked() const { return mIsChunked; }
134
135private:
136
143
145 const HttpResponse& resp() const { return *mResponse; }
146
147 // Parse status line: "HTTP/1.1 200 OK\r\n"
148 bool parseStatusLine();
149
150 // Parse headers: "Header: Value\r\n" ... "\r\n"
151 bool parseHeaders();
152
153 // Parse body (chunked or Content-Length)
154 void parseBody();
155
156 // Find CRLF in buffer
158
159 // Consume n bytes from buffer
160 void consume(size_t n);
161
162 // Get header value (case-insensitive)
163 fl::optional<fl::string> getHeader(const char* name) const;
164};
165
166} // namespace fl
fl::optional< fl::string > getHeader(const char *name) const
~HttpRequestParser() FL_NOEXCEPT
HttpRequest & req()
Definition http_parser.h:81
size_t getBufferSize() const
Definition http_parser.h:68
const HttpRequest & req() const
Definition http_parser.h:82
bool getIsChunked() const
Definition http_parser.h:70
fl::shared_ptr< net::http::ChunkedReader > mChunkedReader
Definition http_parser.h:77
fl::optional< size_t > findCRLF() const
fl::shared_ptr< HttpRequest > mRequest
Definition http_parser.h:76
fl::vector< u8 > mBuffer
Definition http_parser.h:75
size_t getContentLength() const
Definition http_parser.h:69
State getState() const
Definition http_parser.h:67
HttpRequestParser() FL_NOEXCEPT
HttpRequestPtrConst getRequest()
void feed(fl::span< const u8 > data)
const HttpResponse & resp() const
bool getIsChunked() const
fl::optional< size_t > findCRLF() const
size_t getContentLength() const
State getState() const
~HttpResponseParser() FL_NOEXCEPT
HttpResponse & resp()
size_t getBufferSize() const
fl::vector< u8 > mBuffer
HttpResponsePtrConst getResponse()
void feed(fl::span< const u8 > data)
fl::shared_ptr< HttpResponse > mResponse
fl::shared_ptr< net::http::ChunkedReader > mChunkedReader
fl::optional< fl::string > getHeader(const char *name) const
fl::shared_ptr< HttpResponse > HttpResponsePtr
Definition http_parser.h:36
unsigned char u8
Definition stdint.h:131
Optional< T > optional
Definition optional.h:16
fl::shared_ptr< const HttpResponse > HttpResponsePtrConst
Definition http_parser.h:38
fl::shared_ptr< const HttpRequest > HttpRequestPtrConst
Definition http_parser.h:37
fl::shared_ptr< HttpRequest > HttpRequestPtr
Definition http_parser.h:35
Base definition for an LED controller.
Definition crgb.hpp:179
fl::string method
Definition http_parser.h:19
fl::string version
Definition http_parser.h:28
fl::vector< u8 > body
Definition http_parser.h:32
fl::string uri
Definition http_parser.h:20
fl::vector< u8 > body
Definition http_parser.h:23
fl::flat_map< fl::string, fl::string, fl::StringFastLess > headers
Definition http_parser.h:22
fl::string version
Definition http_parser.h:21
fl::string reasonPhrase
Definition http_parser.h:30
fl::flat_map< fl::string, fl::string, fl::StringFastLess > headers
Definition http_parser.h:31
#define FL_NOEXCEPT