Flecs: include/flecs/addons/http.h Source File
Description: A fast entity component system (ECS) for C & C++

| Flecs v4.0 A fast entity component system (ECS) for C & C++

Loading...
Searching...
No Matches
http.h
Go to the documentation of this file.
1
20#ifdef FLECS_HTTP
21
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
38#define ECS_HTTP_HEADER_COUNT_MAX (32)
39
41#define ECS_HTTP_QUERY_PARAM_COUNT_MAX (32)
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
48 typedef struct ecs_http_server_t ecs_http_server_t;
49
51 typedef struct {
52 uint64_t id;
53 ecs_http_server_t *server;
54
55 char host[128];
56 char port[16];
57} ecs_http_connection_t;
58
60 typedef struct {
61 const char *key;
62 const char *value;
63} ecs_http_key_value_t;
64
66 typedef enum {
67 EcsHttpGet,
68 EcsHttpPost,
69 EcsHttpPut,
70 EcsHttpDelete,
71 EcsHttpOptions,
72 EcsHttpMethodUnsupported
73} ecs_http_method_t;
74
76 typedef struct {
77 uint64_t id;
78
79 ecs_http_method_t method;
80 char *path;
81 char *body;
82 ecs_http_key_value_t headers[ECS_HTTP_HEADER_COUNT_MAX];
83 ecs_http_key_value_t params[ECS_HTTP_HEADER_COUNT_MAX];
84 int32_t header_count;
85 int32_t param_count;
86
87 ecs_http_connection_t *conn;
88} ecs_http_request_t;
89
91 typedef struct {
92 int code;
93 ecs_strbuf_t body;
94 const char* status;
95 const char* content_type;
96 ecs_strbuf_t headers;
97} ecs_http_reply_t;
98
99#define ECS_HTTP_REPLY_INIT \
100 (ecs_http_reply_t){200, ECS_STRBUF_INIT, "OK", "application/json", ECS_STRBUF_INIT}
101
102/* Global HTTP statistics. */
103extern int64_t ecs_http_request_received_count;
104extern int64_t ecs_http_request_invalid_count;
105extern int64_t ecs_http_request_handled_ok_count;
106extern int64_t ecs_http_request_handled_error_count;
107extern int64_t ecs_http_request_not_handled_count;
108extern int64_t ecs_http_request_preflight_count;
109extern int64_t ecs_http_send_ok_count;
110extern int64_t ecs_http_send_error_count;
111extern int64_t ecs_http_busy_count;
117 typedef bool (*ecs_http_reply_action_t)(
118 const ecs_http_request_t* request,
119 ecs_http_reply_t *reply,
120 void *ctx);
121
123 typedef struct {
124 ecs_http_reply_action_t callback;
125 void *ctx;
126 uint16_t port;
127 const char *ipaddr;
128 int32_t send_queue_wait_ms;
129 double cache_timeout;
130 double cache_purge_timeout;
131} ecs_http_server_desc_t;
132
139FLECS_API
140 ecs_http_server_t* ecs_http_server_init(
141 const ecs_http_server_desc_t *desc);
142
148FLECS_API
149 void ecs_http_server_fini(
150 ecs_http_server_t* server);
151
158FLECS_API
159 int ecs_http_server_start(
160 ecs_http_server_t* server);
161
168FLECS_API
169 void ecs_http_server_dequeue(
170 ecs_http_server_t* server,
171 ecs_ftime_t delta_time);
172
178FLECS_API
179 void ecs_http_server_stop(
180 ecs_http_server_t* server);
181
192FLECS_API
193 int ecs_http_server_http_request(
194 ecs_http_server_t* srv,
195 const char *req,
196 ecs_size_t len,
197 ecs_http_reply_t *reply_out);
198
200FLECS_API
201 int ecs_http_server_request(
202 ecs_http_server_t* srv,
203 const char *method,
204 const char *req,
205 ecs_http_reply_t *reply_out);
206
208FLECS_API
209 void* ecs_http_server_ctx(
210 ecs_http_server_t* srv);
211
218FLECS_API
219 const char* ecs_http_get_header(
220 const ecs_http_request_t* req,
221 const char* name);
222
229FLECS_API
230 const char* ecs_http_get_param(
231 const ecs_http_request_t* req,
232 const char* name);
233
234#ifdef __cplusplus
235}
236#endif
237
240#endif // FLECS_HTTP_H
241
242#endif // FLECS_HTTP
ecs_http_request_not_handled_count
int64_t ecs_http_request_not_handled_count
Total number of HTTP requests with an unknown endpoint.
ecs_http_get_param
FLECS_API const char * ecs_http_get_param(const ecs_http_request_t *req, const char *name)
Find query parameter in request.
ecs_http_request_invalid_count
int64_t ecs_http_request_invalid_count
Total number of invalid HTTP requests.
ecs_http_send_error_count
int64_t ecs_http_send_error_count
Total number of HTTP replies that failed to send.
ecs_http_reply_action_t
bool(* ecs_http_reply_action_t)(const ecs_http_request_t *request, ecs_http_reply_t *reply, void *ctx)
Request callback.
Definition http.h:117
ecs_http_request_received_count
int64_t ecs_http_request_received_count
Total number of HTTP requests received.
ecs_http_server_start
FLECS_API int ecs_http_server_start(ecs_http_server_t *server)
Start server.
ecs_http_server_stop
FLECS_API void ecs_http_server_stop(ecs_http_server_t *server)
Stop server.
ecs_http_busy_count
int64_t ecs_http_busy_count
Total number of HTTP busy replies.
ecs_http_request_handled_error_count
int64_t ecs_http_request_handled_error_count
Total number of HTTP requests with errors.
ecs_http_server_request
FLECS_API int ecs_http_server_request(ecs_http_server_t *srv, const char *method, const char *req, ecs_http_reply_t *reply_out)
Convenience wrapper around ecs_http_server_http_request().
ecs_http_server_dequeue
FLECS_API void ecs_http_server_dequeue(ecs_http_server_t *server, ecs_ftime_t delta_time)
Process server requests.
ecs_http_request_preflight_count
int64_t ecs_http_request_preflight_count
Total number of preflight HTTP requests received.
ecs_http_get_header
FLECS_API const char * ecs_http_get_header(const ecs_http_request_t *req, const char *name)
Find header in request.
ECS_HTTP_HEADER_COUNT_MAX
#define ECS_HTTP_HEADER_COUNT_MAX
Maximum number of headers in request.
Definition http.h:38
ecs_http_method_t
ecs_http_method_t
Supported request methods.
Definition http.h:66
ecs_http_send_ok_count
int64_t ecs_http_send_ok_count
Total number of HTTP replies successfully sent.
ecs_http_server_fini
FLECS_API void ecs_http_server_fini(ecs_http_server_t *server)
Destroy server.
ecs_http_server_t
struct ecs_http_server_t ecs_http_server_t
HTTP server.
Definition http.h:48
ecs_http_server_init
FLECS_API ecs_http_server_t * ecs_http_server_init(const ecs_http_server_desc_t *desc)
Create server.
ecs_http_request_handled_ok_count
int64_t ecs_http_request_handled_ok_count
Total number of successful HTTP requests.
ecs_http_server_ctx
FLECS_API void * ecs_http_server_ctx(ecs_http_server_t *srv)
Get context provided in ecs_http_server_desc_t.
ecs_http_server_http_request
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.
ecs_ftime_t
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
ecs_http_connection_t
A connection manages communication with the remote host.
Definition http.h:51
ecs_http_key_value_t
Helper type used for headers & URL query parameters.
Definition http.h:60
ecs_http_reply_t
An HTTP reply.
Definition http.h:91
ecs_http_reply_t::code
int code
default = 200
Definition http.h:92
ecs_http_reply_t::headers
ecs_strbuf_t headers
default = ""
Definition http.h:96
ecs_http_reply_t::body
ecs_strbuf_t body
default = ""
Definition http.h:93
ecs_http_reply_t::content_type
const char * content_type
default = application/json
Definition http.h:95
ecs_http_reply_t::status
const char * status
default = OK
Definition http.h:94
ecs_http_request_t
An HTTP request.
Definition http.h:76
ecs_http_server_desc_t
Used with ecs_http_server_init().
Definition http.h:123
ecs_http_server_desc_t::ipaddr
const char * ipaddr
Interface to listen on (optional)
Definition http.h:127
ecs_http_server_desc_t::send_queue_wait_ms
int32_t send_queue_wait_ms
Send queue wait time when empty.
Definition http.h:128
ecs_http_server_desc_t::port
uint16_t port
HTTP port.
Definition http.h:126
ecs_http_server_desc_t::cache_purge_timeout
double cache_purge_timeout
Cache purge timeout (for purging cache entries)
Definition http.h:130
ecs_http_server_desc_t::ctx
void * ctx
Passed to callback (optional)
Definition http.h:125
ecs_http_server_desc_t::cache_timeout
double cache_timeout
Cache invalidation timeout (0 disables caching)
Definition http.h:129
ecs_http_server_desc_t::callback
ecs_http_reply_action_t callback
Function called for each request
Definition http.h:124