FastLED 3.9.15
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() FL_NOEXCEPT = default;
51
53 const string& method() const { return mMethod; }
54
56 const string& path() const { return mPath; }
57
59 const string& body() const { return mBody; }
60
62 const string& http_version() const { return mHttpVersion; }
63
67 optional<string> header(const string& name) const;
68
72 optional<string> query(const string& param) const;
73
75 bool is_get() const { return mMethod == "GET"; }
76
78 bool is_post() const { return mMethod == "POST"; }
79
81 bool has_body() const { 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);
103
108 Response& header(const string& name, const string& value);
109
113 Response& body(const string& content);
114
118 Response& json(const class json& data);
119
123 static Response ok(const string& body = "");
124
127 static Response not_found();
128
132 static Response bad_request(const string& message);
133
137 static Response internal_error(const string& message);
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;
147};
148
152
167class Server : public EngineEvents::Listener {
168public:
171
174
175 // Non-copyable
176 Server(const Server&) FL_NOEXCEPT = delete;
177 Server& operator=(const Server&) FL_NOEXCEPT = delete;
178
182 bool start(int port = 8080);
183
185 void stop();
186
191 void route(const string& method, const string& path, RouteHandler handler);
192
196 void get(const string& path, RouteHandler handler);
197
201 void post(const string& path, RouteHandler handler);
202
206 void put(const string& path, RouteHandler handler);
207
211 void del(const string& path, RouteHandler handler);
212
217 size_t update();
218
220 bool is_running() const { return mRunning; }
221
223 int port() const { return mPort; }
224
226 string last_error() const { return mLastError; }
227
228private:
229 // EngineEvents::Listener implementation
230 void onExit() 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);
262 optional<RouteHandler> find_handler(const string& method, const string& path) const;
263 void close_client(size_t index);
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);
272#endif
273};
274
275} // namespace http
276} // namespace asio
277
278// Convenience type aliases for common use
282
283} // namespace fl
Request() FL_NOEXCEPT=default
const string & http_version() const
Get HTTP version (e.g., "HTTP/1.1")
Definition server.h:62
const string & path() const
Get request path (e.g., "/", "/api/status")
Definition server.h:56
optional< string > header(const string &name) const
Get header value by name (case-insensitive)
const string & body() const
Get request body (for POST/PUT requests)
Definition server.h:59
bool is_get() const
Check if request is GET.
Definition server.h:75
map< string, string > mHeaders
Definition server.h:90
const string & method() const
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
Check if request has a body.
Definition server.h:81
optional< string > query(const string &param) const
Get query parameter value by name.
bool is_post() const
Check if request is POST.
Definition server.h:78
HTTP request object (immutable, passed by const reference)
Definition server.h:48
static Response not_found()
Factory method for 404 Not Found response.
Response & json(const class json &data)
Set JSON response body with automatic Content-Type header.
static Response internal_error(const string &message)
Factory method for 500 Internal Server Error response.
Response & status(int code)
Set HTTP status code.
Response & body(const string &content)
Set response body.
static Response ok(const string &body="")
Factory method for 200 OK response.
Response & header(const string &name, const string &value)
Add HTTP header.
friend class Server
Definition server.h:140
map< string, string > mHeaders
Definition server.h:144
static Response bad_request(const string &message)
Factory method for 400 Bad Request response.
HTTP response builder (fluent interface)
Definition server.h:95
void del(const string &path, RouteHandler handler)
Convenience method for DELETE routes.
optional< Request > read_request(ClientConnection &client)
void get(const string &path, RouteHandler handler)
Convenience method for GET routes.
bool is_running() const
Check if server is running.
Definition server.h:220
vector< ClientConnection > mClientSockets
Definition server.h:252
void put(const string &path, RouteHandler handler)
Convenience method for PUT routes.
~Server() FL_NOEXCEPT
Destructor (stops server if running)
vector< RouteEntry > mRoutes
Definition server.h:251
int port() const
Get server port.
Definition server.h:223
void stop()
Stop server and close all connections.
bool send_response(int client_fd, const Response &response)
void post(const string &path, RouteHandler handler)
Convenience method for POST routes.
string last_error() const
Get last error message.
Definition server.h:226
bool start(int port=8080)
Start server on specified port.
Server() FL_NOEXCEPT
Constructor.
size_t update()
Update server (process pending requests non-blocking)
fl::unique_ptr< ServerAsyncRunner > mAsyncRunner
Definition server.h:255
optional< RouteHandler > find_handler(const string &method, const string &path) const
void route(const string &method, const string &path, RouteHandler handler)
Register route handler using fl::function.
bool setup_listen_socket(int port)
void close_client(size_t index)
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
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
#define FL_NOEXCEPT