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
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
49
51typedef struct {
52 uint64_t id;
55 char host[128];
56 char port[16];
58
60typedef struct {
61 const char *key;
62 const char *value;
64
66typedef enum {
67 EcsHttpGet,
68 EcsHttpPost,
69 EcsHttpPut,
70 EcsHttpDelete,
71 EcsHttpOptions,
72 EcsHttpMethodUnsupported
74
89
91typedef struct {
92 int code;
93 ecs_strbuf_t body;
94 const char* status;
95 const char* content_type;
96 ecs_strbuf_t headers;
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;
110extern int64_t ecs_http_send_ok_count;
111extern int64_t ecs_http_send_error_count;
112extern int64_t ecs_http_busy_count;
119 const ecs_http_request_t* request,
120 ecs_http_reply_t *reply,
121 void *ctx);
122
133
140FLECS_API
142 const ecs_http_server_desc_t *desc);
143
149FLECS_API
151 ecs_http_server_t* server);
152
159FLECS_API
161 ecs_http_server_t* server);
162
170FLECS_API
172 ecs_http_server_t* server,
173 ecs_ftime_t delta_time);
174
180FLECS_API
182 ecs_http_server_t* server);
183
195FLECS_API
198 const char *req,
199 ecs_size_t len,
200 ecs_http_reply_t *reply_out);
201
211FLECS_API
214 const char *method,
215 const char *req,
216 const char *body,
217 ecs_http_reply_t *reply_out);
218
224FLECS_API
226 ecs_http_server_t* srv);
227
234FLECS_API
236 const ecs_http_request_t* req,
237 const char* name);
238
245FLECS_API
247 const ecs_http_request_t* req,
248 const char* name);
249
250#ifdef __cplusplus
251}
252#endif
253
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.
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
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
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
ecs_http_server_t * server
Server.
Definition http.h:53
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_method_t method
Request method.
Definition http.h:79
ecs_http_connection_t * conn
Connection.
Definition http.h:87
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