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

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

Loading...
Searching...
No Matches
http.h
Go to the documentation of this file.
1
19
20#ifdef FLECS_HTTP
21
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
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_QUERY_PARAM_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
100#define ECS_HTTP_REPLY_INIT \
101 (ecs_http_reply_t){200, ECS_STRBUF_INIT, "OK", "application/json", ECS_STRBUF_INIT}
102
104extern int64_t ecs_http_request_received_count;
105extern int64_t ecs_http_request_invalid_count;
106extern int64_t ecs_http_request_handled_ok_count;
107extern int64_t ecs_http_request_handled_error_count;
108extern int64_t ecs_http_request_not_handled_count;
109extern int64_t ecs_http_request_preflight_count;
110extern int64_t ecs_http_send_ok_count;
111extern int64_t ecs_http_send_error_count;
112extern int64_t ecs_http_busy_count;
113
118 typedef bool (*ecs_http_reply_action_t)(
119 const ecs_http_request_t* request,
120 ecs_http_reply_t *reply,
121 void *ctx);
122
124 typedef struct {
125 ecs_http_reply_action_t callback;
126 void *ctx;
127 uint16_t port;
128 const char *ipaddr;
129 int32_t send_queue_wait_ms;
130 double cache_timeout;
131 double cache_purge_timeout;
132} ecs_http_server_desc_t;
133
140FLECS_API
141 ecs_http_server_t* ecs_http_server_init(
142 const ecs_http_server_desc_t *desc);
143
149FLECS_API
150 void ecs_http_server_fini(
151 ecs_http_server_t* server);
152
159FLECS_API
160 int ecs_http_server_start(
161 ecs_http_server_t* server);
162
170FLECS_API
171 void ecs_http_server_dequeue(
172 ecs_http_server_t* server,
173 ecs_ftime_t delta_time);
174
180FLECS_API
181 void ecs_http_server_stop(
182 ecs_http_server_t* server);
183
195FLECS_API
196 int ecs_http_server_http_request(
197 ecs_http_server_t* srv,
198 const char *req,
199 ecs_size_t len,
200 ecs_http_reply_t *reply_out);
201
211FLECS_API
212 int ecs_http_server_request(
213 ecs_http_server_t* srv,
214 const char *method,
215 const char *req,
216 const char *body,
217 ecs_http_reply_t *reply_out);
218
224FLECS_API
225 void* ecs_http_server_ctx(
226 ecs_http_server_t* srv);
227
234FLECS_API
235 const char* ecs_http_get_header(
236 const ecs_http_request_t* req,
237 const char* name);
238
245FLECS_API
246 const char* ecs_http_get_param(
247 const ecs_http_request_t* req,
248 const char* name);
249
250#ifdef __cplusplus
251}
252#endif
253
255
256#endif // FLECS_HTTP_H
257
258#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 a query parameter in a 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:118
ecs_http_request_received_count
int64_t ecs_http_request_received_count
Global HTTP statistics.
ecs_http_server_start
FLECS_API int ecs_http_server_start(ecs_http_server_t *server)
Start a server.
ecs_http_server_stop
FLECS_API void ecs_http_server_stop(ecs_http_server_t *server)
Stop a 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_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 a header in a request.
ECS_HTTP_HEADER_COUNT_MAX
#define ECS_HTTP_HEADER_COUNT_MAX
Maximum number of headers in a 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 a server.
ECS_HTTP_QUERY_PARAM_COUNT_MAX
#define ECS_HTTP_QUERY_PARAM_COUNT_MAX
Maximum number of query parameters in a request.
Definition http.h:41
ecs_http_server_t
struct ecs_http_server_t ecs_http_server_t
HTTP server.
Definition http.h:48
ecs_http_server_request
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().
ecs_http_server_init
FLECS_API ecs_http_server_t * ecs_http_server_init(const ecs_http_server_desc_t *desc)
Create a 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_connection_t::id
uint64_t id
Connection ID.
Definition http.h:52
ecs_http_connection_t::host
char host[128]
Remote host.
Definition http.h:55
ecs_http_connection_t::server
ecs_http_server_t * server
Server.
Definition http.h:53
ecs_http_connection_t::port
char port[16]
Remote port.
Definition http.h:56
ecs_http_key_value_t
Helper type used for headers and URL query parameters.
Definition http.h:60
ecs_http_key_value_t::value
const char * value
Value.
Definition http.h:62
ecs_http_key_value_t::key
const char * key
Key.
Definition http.h:61
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_request_t::param_count
int32_t param_count
Number of query parameters.
Definition http.h:85
ecs_http_request_t::header_count
int32_t header_count
Number of headers.
Definition http.h:84
ecs_http_request_t::params
ecs_http_key_value_t params[(32)]
Request query parameters.
Definition http.h:83
ecs_http_request_t::method
ecs_http_method_t method
Request method.
Definition http.h:79
ecs_http_request_t::conn
ecs_http_connection_t * conn
Connection.
Definition http.h:87
ecs_http_request_t::headers
ecs_http_key_value_t headers[(32)]
Request headers.
Definition http.h:82
ecs_http_request_t::id
uint64_t id
Request ID.
Definition http.h:77
ecs_http_request_t::path
char * path
Request path.
Definition http.h:80
ecs_http_request_t::body
char * body
Request body.
Definition http.h:81
ecs_http_server_desc_t
Used with ecs_http_server_init().
Definition http.h:124
ecs_http_server_desc_t::ipaddr
const char * ipaddr
Interface to listen on (optional).
Definition http.h:128
ecs_http_server_desc_t::send_queue_wait_ms
int32_t send_queue_wait_ms
Send queue wait time when empty.
Definition http.h:129
ecs_http_server_desc_t::port
uint16_t port
HTTP port.
Definition http.h:127
ecs_http_server_desc_t::cache_purge_timeout
double cache_purge_timeout
Cache purge timeout (for purging cache entries).
Definition http.h:131
ecs_http_server_desc_t::ctx
void * ctx
Passed to callback (optional).
Definition http.h:126
ecs_http_server_desc_t::cache_timeout
double cache_timeout
Cache invalidation timeout (0 disables caching).
Definition http.h:130
ecs_http_server_desc_t::callback
ecs_http_reply_action_t callback
Function called for each request.
Definition http.h:125