/
http_server.h
206 lines (187 loc) · 5.4 KB
/
http_server.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* @file inet/http_server.h
*
* @section License
* Copyright (C) 2016, Erik Moqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERBITSTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
#ifndef __INET_HTTP_SERVER_H__
#define __INET_HTTP_SERVER_H__
#include "simba.h"
/**
* Request action types.
*/
enum http_server_request_action_t {
http_server_request_action_get_t = 0,
http_server_request_action_post_t = 1
};
/**
* Content type.
*/
enum http_server_content_type_t {
http_server_content_type_text_plain_t = 0,
http_server_content_type_text_html_t = 1
};
/**
* Response codes.
*/
enum http_server_response_code_t {
http_server_response_code_200_ok_t = 200,
http_server_response_code_404_not_found_t = 404
};
/**
* Connection state.
*/
enum http_server_connection_state_t {
http_server_connection_state_free_t = 0,
http_server_connection_state_allocated_t
};
/**
* HTTP request.
*/
struct http_server_request_t {
enum http_server_request_action_t action;
char path[64];
struct {
struct {
int present;
char value[32];
} sec_websocket_key;
struct {
int present;
char value[52];
} content_type;
struct {
int present;
long value;
} content_length;
} headers;
};
/**
* HTTP response.
*/
struct http_server_response_t {
int type;
enum http_server_response_code_t code;
struct {
int type;
const char *buf_p;
size_t size;
} content;
};
struct http_server_connection_t;
typedef int (*http_server_route_callback_t)(struct http_server_connection_t *connection_p,
struct http_server_request_t *request_p);
struct http_server_listener_t {
const char *address_p;
int port;
struct {
const char *name_p;
struct {
void *buf_p;
size_t size;
} stack;
struct thrd_t *id_p;
} thrd;
struct socket_t socket;
};
struct http_server_connection_t {
enum http_server_connection_state_t state;
struct {
const char *name_p;
struct {
void *buf_p;
size_t size;
} stack;
struct thrd_t *id_p;
} thrd;
struct http_server_t *self_p;
struct socket_t socket;
struct event_t events;
};
/**
* Call given callback for given path.
*/
struct http_server_route_t {
const char *path_p;
http_server_route_callback_t callback;
};
struct http_server_t {
const char *root_path_p;
const struct http_server_route_t *routes_p;
http_server_route_callback_t on_no_route;
struct http_server_listener_t *listener_p;
struct http_server_connection_t *connections_p;
struct event_t events;
};
/**
* Initialize given http server with given root path and maximum
* number of clients.
*
* @param[in] self_p Http server to initialize.
* @param[in] listener_p Listener.
* @param[in] connections_p A NULL terminated list of connections.
* @param[in] root_path_p Working directory for the connection threads.
* @param[in] routes_p An array of routes.
* @param[in] on_no_route Callback called for all requests without a
* matching route in route_p.
*
* @return zero(0) or negative error code.
*/
int http_server_init(struct http_server_t *self_p,
struct http_server_listener_t *listener_p,
struct http_server_connection_t *connections_p,
const char *root_path_p,
const struct http_server_route_t *routes_p,
http_server_route_callback_t on_no_route);
/**
* Start given HTTP server.
*
* Spawn the threads and start listening for connections.
*
* @param[in] self_p Http server.
*
* @return zero(0) or negative error code.
*/
int http_server_start(struct http_server_t *self_p);
/**
* Stop given HTTP server.
*
* Closes the listener and all open connections, and then kills the
* threads.
*
* @param[in] self_p Http server.
*
* @return zero(0) or negative error code.
*/
int http_server_stop(struct http_server_t *self_p);
/**
* Write given HTTP response to given connected client. This function
* should only be called from the route callbacks to respond to given
* request.
*
* @param[in] connection_p Current connection.
* @param[in] request_p Current request.
* @param[in] response_p Current response. If ``buf_p`` in the
* response to NULL this function will only
* write the HTTP header, including the size, to
* the socket. After this function returns write
* the payload by calling `socket_write()`.
*
* @return zero(0) or negative error code.
*/
int http_server_response_write(struct http_server_connection_t *connection_p,
struct http_server_request_t *request_p,
struct http_server_response_t *response_p);
#endif