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
49
51typedef struct {
52 uint64_t id;
53 ecs_http_server_t *server;
54
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
76typedef struct {
77 uint64_t id;
78
79 ecs_http_method_t method;
80 char *path;
81 char *body;
84 int32_t header_count;
85 int32_t param_count;
86
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
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;
109extern int64_t ecs_http_send_ok_count;
110extern int64_t ecs_http_send_error_count;
111extern int64_t ecs_http_busy_count;
118 const ecs_http_request_t* request,
119 ecs_http_reply_t *reply,
120 void *ctx);
121
132
139FLECS_API
141 const ecs_http_server_desc_t *desc);
142
148FLECS_API
150 ecs_http_server_t* server);
151
158FLECS_API
160 ecs_http_server_t* server);
161
168FLECS_API
170 ecs_http_server_t* server,
171 ecs_ftime_t delta_time);
172
178FLECS_API
180 ecs_http_server_t* server);
181
192FLECS_API
195 const char *req,
196 ecs_size_t len,
197 ecs_http_reply_t *reply_out);
198
200FLECS_API
203 const char *method,
204 const char *req,
205 ecs_http_reply_t *reply_out);
206
208FLECS_API
210 ecs_http_server_t* srv);
211
218FLECS_API
220 const ecs_http_request_t* req,
221 const char* name);
222
229FLECS_API
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
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 query parameter in 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:117
int64_t ecs_http_request_received_count
Total number of HTTP requests received.
FLECS_API int ecs_http_server_start(ecs_http_server_t *server)
Start server.
FLECS_API void ecs_http_server_stop(ecs_http_server_t *server)
Stop 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 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().
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 header in request.
#define ECS_HTTP_HEADER_COUNT_MAX
Maximum number of headers in 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 server.
struct ecs_http_server_t ecs_http_server_t
HTTP server.
Definition http.h:48
FLECS_API ecs_http_server_t * ecs_http_server_init(const ecs_http_server_desc_t *desc)
Create 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
Helper type used for headers & URL query parameters.
Definition http.h:60
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
Used with ecs_http_server_init().
Definition http.h:123
const char * ipaddr
Interface to listen on (optional)
Definition http.h:127
int32_t send_queue_wait_ms
Send queue wait time when empty.
Definition http.h:128
uint16_t port
HTTP port.
Definition http.h:126
double cache_purge_timeout
Cache purge timeout (for purging cache entries)
Definition http.h:130
void * ctx
Passed to callback (optional)
Definition http.h:125
double cache_timeout
Cache invalidation timeout (0 disables caching)
Definition http.h:129
ecs_http_reply_action_t callback
Function called for each request
Definition http.h:124