FastLED 3.10.8
Loading...
Searching...
No Matches
server.h
Go to the documentation of this file.
1
29
30#pragma once
31
32#include "fl/stl/int.h"
33#include "fl/stl/unique_ptr.h"
34#include "fl/stl/function.h"
35#include "fl/stl/string.h"
36#include "fl/stl/vector.h"
37#include "fl/stl/optional.h"
38#include "fl/stl/map.h"
40#include "platforms/esp/is_esp.h" // ok platform headers - for FL_IS_ESP32 // IWYU pragma: keep
41#include "fl/stl/noexcept.h"
42
43namespace fl {
44namespace asio {
45namespace http {
46
48class Request {
49public:
50 Request() = default; // ok no noexcept: infer member exception contracts (#4773)
51
53 const string& method() const FL_NO_EXCEPT { return mMethod; }
54
56 const string& path() const FL_NO_EXCEPT { return mPath; }
57
59 const string& body() const FL_NO_EXCEPT { return mBody; }
60
62 const string& http_version() const FL_NO_EXCEPT { return mHttpVersion; }
63
67 optional<string> header(const string& name) const FL_NO_EXCEPT;
68
72 optional<string> query(const string& param) const FL_NO_EXCEPT;
73
75 bool is_get() const FL_NO_EXCEPT { return mMethod == "GET"; }
76
78 bool is_post() const FL_NO_EXCEPT { return mMethod == "POST"; }
79
81 bool has_body() const FL_NO_EXCEPT { return !mBody.empty(); }
82
83private:
84 friend class Server;
85
86 string mMethod;
87 string mPath;
89 string mBody;
92};
93
95class Response {
96public:
98
102 Response& status(int code) FL_NO_EXCEPT;
103
108 Response& header(const string& name, const string& value) FL_NO_EXCEPT;
109
113 Response& body(const string& content) FL_NO_EXCEPT;
114
118 Response& json(const class json& data) FL_NO_EXCEPT;
119
123 static Response ok(const string& body = "") FL_NO_EXCEPT;
124
128
132 static Response bad_request(const string& message) FL_NO_EXCEPT;
133
137 static Response internal_error(const string& message) FL_NO_EXCEPT;
138
139private:
140 friend class Server;
141
142 int mStatusCode = 200;
143 string mBody;
144 map<string, string> mHeaders;
145
146 string to_string() const FL_NO_EXCEPT;
147};
148
152
167class Server : public EngineEvents::Listener {
168public:
171
174
175 // Non-copyable
176 Server(const Server&) FL_NO_EXCEPT = delete;
177 Server& operator=(const Server&) FL_NO_EXCEPT = delete;
178
182 bool start(int port = 8080) FL_NO_EXCEPT;
183
185 void stop() FL_NO_EXCEPT;
186
191 void route(const string& method, const string& path, RouteHandler handler) FL_NO_EXCEPT;
192
196 void get(const string& path, RouteHandler handler) FL_NO_EXCEPT;
197
201 void post(const string& path, RouteHandler handler) FL_NO_EXCEPT;
202
206 void put(const string& path, RouteHandler handler) FL_NO_EXCEPT;
207
211 void del(const string& path, RouteHandler handler) FL_NO_EXCEPT;
212
217 size_t update() FL_NO_EXCEPT;
218
220 bool is_running() const FL_NO_EXCEPT { return mRunning; }
221
223 int port() const FL_NO_EXCEPT { return mPort; }
224
226 string last_error() const FL_NO_EXCEPT { return mLastError; }
227
228private:
229 // EngineEvents::Listener implementation
230 void onExit() FL_NO_EXCEPT override;
231
232 // Forward declaration for async integration helper
233 class ServerAsyncRunner;
234 struct RouteEntry {
235 string method;
236 string path;
238 };
239
241 int fd = -1;
243 string buffer;
244 };
245
246 int mPort = 0;
248 bool mRunning = false;
250
253
254 // Async system integration
256
261 bool send_response(int client_fd, const Response& response) FL_NO_EXCEPT;
262 optional<RouteHandler> find_handler(const string& method, const string& path) const FL_NO_EXCEPT;
263 void close_client(size_t index) FL_NO_EXCEPT;
265
266#ifdef FL_IS_ESP32
267public:
268 // ESP32: Static handler bridging esp_http_server callbacks to RouteHandlers.
269 // Declared public so the C callback wrapper can call it.
270 // Parameter is httpd_req_t* cast to void* to avoid ESP-IDF header dependency.
271 static int handle_esp_request(void* req) FL_NO_EXCEPT;
272#endif
273};
274
275} // namespace http
276} // namespace asio
277
278// Convenience type aliases for common use
282
283} // namespace fl
const string & path() const FL_NO_EXCEPT
Get request path (e.g., "/", "/api/status")
Definition server.h:56
optional< string > query(const string &param) const FL_NO_EXCEPT
Get query parameter value by name.
bool is_post() const FL_NO_EXCEPT
Check if request is POST.
Definition server.h:78
const string & body() const FL_NO_EXCEPT
Get request body (for POST/PUT requests)
Definition server.h:59
map< string, string > mHeaders
Definition server.h:90
optional< string > header(const string &name) const FL_NO_EXCEPT
Get header value by name (case-insensitive)
const string & method() const FL_NO_EXCEPT
Get HTTP method (e.g., "GET", "POST", "PUT", "DELETE")
Definition server.h:53
map< string, string > mQuery
Definition server.h:91
friend class Server
Definition server.h:84
bool has_body() const FL_NO_EXCEPT
Check if request has a body.
Definition server.h:81
const string & http_version() const FL_NO_EXCEPT
Get HTTP version (e.g., "HTTP/1.1")
Definition server.h:62
bool is_get() const FL_NO_EXCEPT
Check if request is GET.
Definition server.h:75
HTTP request object (immutable, passed by const reference)
Definition server.h:48
Response & header(const string &name, const string &value) FL_NO_EXCEPT
Add HTTP header.
Response & json(const class json &data) FL_NO_EXCEPT
Set JSON response body with automatic Content-Type header.
string to_string() const FL_NO_EXCEPT
Response & body(const string &content) FL_NO_EXCEPT
Set response body.
static Response bad_request(const string &message) FL_NO_EXCEPT
Factory method for 400 Bad Request response.
static Response ok(const string &body="") FL_NO_EXCEPT
Factory method for 200 OK response.
Response & status(int code) FL_NO_EXCEPT
Set HTTP status code.
static Response not_found() FL_NO_EXCEPT
Factory method for 404 Not Found response.
friend class Server
Definition server.h:140
map< string, string > mHeaders
Definition server.h:144
static Response internal_error(const string &message) FL_NO_EXCEPT
Factory method for 500 Internal Server Error response.
Response() FL_NO_EXCEPT
HTTP response builder (fluent interface)
Definition server.h:95
void accept_connections() FL_NO_EXCEPT
void stop() FL_NO_EXCEPT
Stop server and close all connections.
bool setup_listen_socket(int port) FL_NO_EXCEPT
vector< ClientConnection > mClientSockets
Definition server.h:252
void post(const string &path, RouteHandler handler) FL_NO_EXCEPT
Convenience method for POST routes.
Server() FL_NO_EXCEPT
Constructor.
void get(const string &path, RouteHandler handler) FL_NO_EXCEPT
Convenience method for GET routes.
bool start(int port=8080) FL_NO_EXCEPT
Start server on specified port.
vector< RouteEntry > mRoutes
Definition server.h:251
void put(const string &path, RouteHandler handler) FL_NO_EXCEPT
Convenience method for PUT routes.
void del(const string &path, RouteHandler handler) FL_NO_EXCEPT
Convenience method for DELETE routes.
void cleanup_stale_connections() FL_NO_EXCEPT
size_t process_requests() FL_NO_EXCEPT
bool send_response(int client_fd, const Response &response) FL_NO_EXCEPT
int port() const FL_NO_EXCEPT
Get server port.
Definition server.h:223
size_t update() FL_NO_EXCEPT
Update server (process pending requests non-blocking)
string last_error() const FL_NO_EXCEPT
Get last error message.
Definition server.h:226
void route(const string &method, const string &path, RouteHandler handler) FL_NO_EXCEPT
Register route handler using fl::function.
fl::unique_ptr< ServerAsyncRunner > mAsyncRunner
Definition server.h:255
optional< RouteHandler > find_handler(const string &method, const string &path) const FL_NO_EXCEPT
void close_client(size_t index) FL_NO_EXCEPT
~Server() FL_NO_EXCEPT
Destructor (stops server if running)
bool is_running() const FL_NO_EXCEPT
Check if server is running.
Definition server.h:220
optional< Request > read_request(ClientConnection &client) FL_NO_EXCEPT
HTTP Server class.
Definition server.h:167
Definition server.h:234
function< Response(const Request &)> RouteHandler
Route handler function signature Takes const reference to Request, returns Response by value.
Definition server.h:151
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
constexpr int type_rank< T >::value
asio::http::Request http_request
Definition server.h:280
MapRedBlackTree< Key, T, Compare, fl::allocator_slab< char > > map
Definition map.h:283
Optional< T > optional
Definition optional.h:16
asio::http::Response http_response
Definition server.h:281
asio::http::Server HttpServer
Definition server.h:279
Base definition for an LED controller.
Definition crgb.hpp:179