Skip to content
Flecs v4.1
http.h
Go to the documentation of this file.
1/**
2 * @file addons/http.h
3 * @brief HTTP addon.
4 *
5 * Minimalistic HTTP server that can receive and reply to simple HTTP requests.
6 * The main goal of this addon is to enable remotely connecting to a running
7 * Flecs application (for example, with a web-based UI) and request and visualize
8 * data from the ECS world.
9 *
10 * Each server instance creates a single thread used for receiving requests.
11 * Received requests are enqueued and handled when the application calls
12 * ecs_http_server_dequeue(). This increases the latency of request handling vs.
13 * responding directly in the receive thread, but is better suited for
14 * retrieving data from ECS applications, as requests can be processed by an ECS
15 * system without having to lock the world.
16 *
17 * This server is intended to be used in a development environment.
18 */
19
20#ifdef FLECS_HTTP
21
22/**
23 * @defgroup c_addons_http Http
24 * @ingroup c_addons
25 * Simple HTTP server used for serving up the REST API.
26 *
27 * @{
28 */
29
30#if !defined(FLECS_OS_API_IMPL) && !defined(FLECS_NO_OS_API_IMPL)
31#define FLECS_OS_API_IMPL
32#endif
33
34#ifndef FLECS_HTTP_H
35#define FLECS_HTTP_H
36
37/** Maximum number of headers in a request. */
38#define ECS_HTTP_HEADER_COUNT_MAX (32)
39
40/** Maximum number of query parameters in a request. */
41#define ECS_HTTP_QUERY_PARAM_COUNT_MAX (32)
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47/** HTTP server. */
49
50/** A connection manages communication with the remote host. */
51typedef struct {
52 uint64_t id; /**< Connection ID. */
53 ecs_http_server_t *server; /**< Server. */
54
55 char host[128]; /**< Remote host. */
56 char port[16]; /**< Remote port. */
58
59/** Helper type used for headers and URL query parameters. */
60typedef struct {
61 const char *key; /**< Key. */
62 const char *value; /**< Value. */
64
65/** Supported request methods. */
66typedef enum {
67 EcsHttpGet,
68 EcsHttpPost,
69 EcsHttpPut,
70 EcsHttpDelete,
71 EcsHttpOptions,
72 EcsHttpMethodUnsupported
74
75/** An HTTP request. */
76typedef struct {
77 uint64_t id; /**< Request ID. */
78
79 ecs_http_method_t method; /**< Request method. */
80 char *path; /**< Request path. */
81 char *body; /**< Request body. */
84 int32_t header_count; /**< Number of headers. */
85 int32_t param_count; /**< Number of query parameters. */
86
87 ecs_http_connection_t *conn; /**< Connection. */
89
90/** An HTTP reply. */
91typedef struct {
92 int code; /**< default = 200. */
93 ecs_strbuf_t body; /**< default = "". */
94 const char* status; /**< default = OK. */
95 const char* content_type; /**< default = application/json. */
96 ecs_strbuf_t headers; /**< default = "". */
98
99/** Default initializer for ecs_http_reply_t. */
100#define ECS_HTTP_REPLY_INIT \
101 (ecs_http_reply_t){200, ECS_STRBUF_INIT, "OK", "application/json", ECS_STRBUF_INIT}
102
103/** Global HTTP statistics. */
104extern int64_t ecs_http_request_received_count; /**< Total number of HTTP requests received. */
105extern int64_t ecs_http_request_invalid_count; /**< Total number of invalid HTTP requests. */
106extern int64_t ecs_http_request_handled_ok_count; /**< Total number of successful HTTP requests. */
107extern int64_t ecs_http_request_handled_error_count; /**< Total number of HTTP requests with errors. */
108extern int64_t ecs_http_request_not_handled_count; /**< Total number of HTTP requests with an unknown endpoint. */
109extern int64_t ecs_http_request_preflight_count; /**< Total number of preflight HTTP requests received. */
110extern int64_t ecs_http_send_ok_count; /**< Total number of HTTP replies successfully sent. */
111extern int64_t ecs_http_send_error_count; /**< Total number of HTTP replies that failed to send. */
112extern int64_t ecs_http_busy_count; /**< Total number of HTTP busy replies. */
113
114/** Request callback.
115 * Invoked for each valid request. The function should populate the reply and
116 * return true. When the function returns false, the server will reply with a
117 * 404 (Not found) code. */
119 const ecs_http_request_t* request,
120 ecs_http_reply_t *reply,
121 void *ctx);
122
123/** Used with ecs_http_server_init(). */
124typedef struct {
125 ecs_http_reply_action_t callback; /**< Function called for each request. */
126 void *ctx; /**< Passed to callback (optional). */
127 uint16_t port; /**< HTTP port. */
128 const char *ipaddr; /**< Interface to listen on (optional). */
129 int32_t send_queue_wait_ms; /**< Send queue wait time when empty. */
130 double cache_timeout; /**< Cache invalidation timeout (0 disables caching). */
131 double cache_purge_timeout; /**< Cache purge timeout (for purging cache entries). */
133
134/** Create a server.
135 * Use ecs_http_server_start() to start receiving requests.
136 *
137 * @param desc Server configuration parameters.
138 * @return The new server, or NULL if creation failed.
139 */
140FLECS_API
142 const ecs_http_server_desc_t *desc);
143
144/** Destroy a server.
145 * This operation will stop the server if it was still running.
146 *
147 * @param server The server to destroy.
148 */
149FLECS_API
151 ecs_http_server_t* server);
152
153/** Start a server.
154 * After this operation, the server will be able to accept requests.
155 *
156 * @param server The server to start.
157 * @return Zero if successful, non-zero if failed.
158 */
159FLECS_API
161 ecs_http_server_t* server);
162
163/** Process server requests.
164 * This operation invokes the reply callback for each received request. No new
165 * requests will be enqueued while processing requests.
166 *
167 * @param server The server for which to process requests.
168 * @param delta_time The time passed since the last call to dequeue.
169 */
170FLECS_API
172 ecs_http_server_t* server,
173 ecs_ftime_t delta_time);
174
175/** Stop a server.
176 * After this operation, no new requests can be received.
177 *
178 * @param server The server.
179 */
180FLECS_API
182 ecs_http_server_t* server);
183
184/** Emulate a request.
185 * The request string must be a valid HTTP request. A minimal example:
186 *
187 * GET /entity/flecs/core/World?label=true HTTP/1.1
188 *
189 * @param srv The server.
190 * @param req The request.
191 * @param len The length of the request (optional).
192 * @param reply_out The reply (out parameter).
193 * @return Zero if success, non-zero if failed.
194 */
195FLECS_API
198 const char *req,
199 ecs_size_t len,
200 ecs_http_reply_t *reply_out);
201
202/** Convenience wrapper around ecs_http_server_http_request().
203 *
204 * @param srv The server.
205 * @param method The HTTP method (e.g., "GET").
206 * @param req The request path.
207 * @param body The request body (optional).
208 * @param reply_out The reply (out parameter).
209 * @return Zero if success, non-zero if failed.
210 */
211FLECS_API
214 const char *method,
215 const char *req,
216 const char *body,
217 ecs_http_reply_t *reply_out);
218
219/** Get context provided in ecs_http_server_desc_t.
220 *
221 * @param srv The server.
222 * @return The context.
223 */
224FLECS_API
226 ecs_http_server_t* srv);
227
228/** Find a header in a request.
229 *
230 * @param req The request.
231 * @param name Name of the header to find.
232 * @return The header value, or NULL if not found.
233*/
234FLECS_API
236 const ecs_http_request_t* req,
237 const char* name);
238
239/** Find a query parameter in a request.
240 *
241 * @param req The request.
242 * @param name The parameter name.
243 * @return The decoded parameter value, or NULL if not found.
244 */
245FLECS_API
247 const ecs_http_request_t* req,
248 const char* name);
249
250#ifdef __cplusplus
251}
252#endif
253
254/** @} */
255
256#endif // FLECS_HTTP_H
257
258#endif // FLECS_HTTP
int64_t ecs_http_request_not_handled_count
Total number of HTTP requests with an unknown endpoint.
FLECS_API const char * ecs_http_get_param(const ecs_http_request_t *req, const char *name)
Find a query parameter in a request.
int64_t ecs_http_request_invalid_count
Total number of invalid HTTP requests.
int64_t ecs_http_send_error_count
Total number of HTTP replies that failed to send.
int64_t ecs_http_request_received_count
Global HTTP statistics.
FLECS_API int ecs_http_server_start(ecs_http_server_t *server)
Start a server.
FLECS_API void ecs_http_server_stop(ecs_http_server_t *server)
Stop a server.
int64_t ecs_http_busy_count
Total number of HTTP busy replies.
int64_t ecs_http_request_handled_error_count
Total number of HTTP requests with errors.
FLECS_API void ecs_http_server_dequeue(ecs_http_server_t *server, ecs_ftime_t delta_time)
Process server requests.
int64_t ecs_http_request_preflight_count
Total number of preflight HTTP requests received.
FLECS_API const char * ecs_http_get_header(const ecs_http_request_t *req, const char *name)
Find a header in a request.
#define ECS_HTTP_HEADER_COUNT_MAX
Maximum number of headers in a request.
Definition http.h:38
ecs_http_method_t
Supported request methods.
Definition http.h:66
int64_t ecs_http_send_ok_count
Total number of HTTP replies successfully sent.
FLECS_API void ecs_http_server_fini(ecs_http_server_t *server)
Destroy a server.
#define ECS_HTTP_QUERY_PARAM_COUNT_MAX
Maximum number of query parameters in a request.
Definition http.h:41
struct ecs_http_server_t ecs_http_server_t
HTTP server.
Definition http.h:48
bool(*) ecs_http_reply_action_t(const ecs_http_request_t *request, ecs_http_reply_t *reply, void *ctx)
Request callback.
Definition http.h:118
FLECS_API int ecs_http_server_request(ecs_http_server_t *srv, const char *method, const char *req, const char *body, ecs_http_reply_t *reply_out)
Convenience wrapper around ecs_http_server_http_request().
FLECS_API ecs_http_server_t * ecs_http_server_init(const ecs_http_server_desc_t *desc)
Create a server.
int64_t ecs_http_request_handled_ok_count
Total number of successful HTTP requests.
FLECS_API void * ecs_http_server_ctx(ecs_http_server_t *srv)
Get context provided in ecs_http_server_desc_t.
FLECS_API int ecs_http_server_http_request(ecs_http_server_t *srv, const char *req, ecs_size_t len, ecs_http_reply_t *reply_out)
Emulate a request.
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
A connection manages communication with the remote host.
Definition http.h:51
uint64_t id
Connection ID.
Definition http.h:52
char host[128]
Remote host.
Definition http.h:55
ecs_http_server_t * server
Server.
Definition http.h:53
char port[16]
Remote port.
Definition http.h:56
Helper type used for headers and URL query parameters.
Definition http.h:60
const char * value
Value.
Definition http.h:62
const char * key
Key.
Definition http.h:61
An HTTP reply.
Definition http.h:91
int code
default = 200.
Definition http.h:92
ecs_strbuf_t headers
default = "".
Definition http.h:96
ecs_strbuf_t body
default = "".
Definition http.h:93
const char * content_type
default = application/json.
Definition http.h:95
const char * status
default = OK.
Definition http.h:94
An HTTP request.
Definition http.h:76
int32_t param_count
Number of query parameters.
Definition http.h:85
int32_t header_count
Number of headers.
Definition http.h:84
ecs_http_key_value_t params[(32)]
Request query parameters.
Definition http.h:83
ecs_http_method_t method
Request method.
Definition http.h:79
ecs_http_connection_t * conn
Connection.
Definition http.h:87
ecs_http_key_value_t headers[(32)]
Request headers.
Definition http.h:82
uint64_t id
Request ID.
Definition http.h:77
char * path
Request path.
Definition http.h:80
char * body
Request body.
Definition http.h:81
Used with ecs_http_server_init().
Definition http.h:124
const char * ipaddr
Interface to listen on (optional).
Definition http.h:128
int32_t send_queue_wait_ms
Send queue wait time when empty.
Definition http.h:129
uint16_t port
HTTP port.
Definition http.h:127
double cache_purge_timeout
Cache purge timeout (for purging cache entries).
Definition http.h:131
void * ctx
Passed to callback (optional).
Definition http.h:126
double cache_timeout
Cache invalidation timeout (0 disables caching).
Definition http.h:130
ecs_http_reply_action_t callback
Function called for each request.
Definition http.h:125