forked from jonashaag/bjoern
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.c
358 lines (307 loc) · 9.6 KB
/
server.c
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#ifdef WANT_SIGINT_HANDLING
# include <sys/signal.h>
#endif
#include <ev.h>
#include "portable_sendfile.h"
#include "common.h"
#include "wsgi.h"
#include "server.h"
#define LISTEN_BACKLOG 1024
#define READ_BUFFER_SIZE 64*1024
#define Py_XCLEAR(obj) do { if(obj) { Py_DECREF(obj); obj = NULL; } } while(0)
#define GIL_LOCK(n) PyGILState_STATE _gilstate_##n = PyGILState_Ensure()
#define GIL_UNLOCK(n) PyGILState_Release(_gilstate_##n)
static const char* http_error_messages[4] = {
NULL, /* Error codes start at 1 because 0 means "no error" */
"HTTP/1.1 400 Bad Request\r\n\r\n",
"HTTP/1.1 406 Length Required\r\n\r\n",
"HTTP/1.1 500 Internal Server Error\r\n\r\n"
};
typedef void ev_io_callback(struct ev_loop*, ev_io*, const int);
#if WANT_SIGINT_HANDLING
typedef void ev_signal_callback(struct ev_loop*, ev_signal*, const int);
static ev_signal_callback ev_signal_on_sigint;
#endif
static ev_io_callback ev_io_on_request;
static ev_io_callback ev_io_on_read;
static ev_io_callback ev_io_on_write;
static bool send_chunk(Request*);
static bool do_sendfile(Request*);
static bool handle_nonzero_errno(Request*);
static struct { int fd; const char* filename; } sockinfo;
void server_run(void)
{
struct ev_loop* mainloop = ev_default_loop(0);
ev_io accept_watcher;
ev_io_init(&accept_watcher, ev_io_on_request, sockinfo.fd, EV_READ);
ev_io_start(mainloop, &accept_watcher);
#if WANT_SIGINT_HANDLING
ev_signal signal_watcher;
ev_signal_init(&signal_watcher, ev_signal_on_sigint, SIGINT);
ev_signal_start(mainloop, &signal_watcher);
#endif
/* This is the program main loop */
Py_BEGIN_ALLOW_THREADS
ev_loop(mainloop, 0);
ev_default_destroy();
Py_END_ALLOW_THREADS
}
static void cleanup(void) {
close(sockinfo.fd);
if(sockinfo.filename)
unlink(sockinfo.filename);
}
#if WANT_SIGINT_HANDLING
static void
ev_signal_on_sigint(struct ev_loop* mainloop, ev_signal* watcher, const int events)
{
/* Clean up and shut down this thread.
* (Shuts down the Python interpreter if this is the main thread) */
cleanup();
ev_unloop(mainloop, EVUNLOOP_ALL);
PyErr_SetInterrupt();
}
#endif
bool server_init(const char* hostaddr, const int port)
{
if(!port) {
/* Unix socket */
if((sockinfo.fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
return false;
struct sockaddr_un sockaddr;
sockaddr.sun_family = PF_UNIX;
strcpy(sockaddr.sun_path, hostaddr);
/* Use @ for abstract (Linux) */
if(hostaddr[0] == '@')
sockaddr.sun_path[0] = '\0';
else
sockinfo.filename = hostaddr;
if(bind(sockinfo.fd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) < 0)
goto err;
}
else {
/* IP bind */
if((sockinfo.fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
return false;
struct sockaddr_in sockaddr;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = PF_INET;
inet_pton(AF_INET, hostaddr, &sockaddr.sin_addr);
sockaddr.sin_port = htons(port);
/* Set SO_REUSEADDR t make the IP address available for reuse */
int optval = true;
setsockopt(sockinfo.fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
if(bind(sockinfo.fd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) < 0)
goto err;
}
if(listen(sockinfo.fd, LISTEN_BACKLOG) < 0)
goto err;
DBG("Listening on %s:%d...", hostaddr, port);
return true;
err:
cleanup();
return false;
}
static void
ev_io_on_request(struct ev_loop* mainloop, ev_io* watcher, const int events)
{
int client_fd;
struct sockaddr_in sockaddr;
socklen_t addrlen;
addrlen = sizeof(struct sockaddr_in);
client_fd = accept(watcher->fd, (struct sockaddr*)&sockaddr, &addrlen);
if(client_fd < 0) {
DBG("Could not accept() client: errno %d", errno);
return;
}
int flags = fcntl(client_fd, F_GETFL, 0);
if(fcntl(client_fd, F_SETFL, (flags < 0 ? 0 : flags) | O_NONBLOCK) == -1) {
DBG("Could not set_nonblocking() client %d: errno %d", client_fd, errno);
return;
}
GIL_LOCK(0);
Request* request = Request_new(client_fd, inet_ntoa(sockaddr.sin_addr));
GIL_UNLOCK(0);
DBG_REQ(request, "Accepted client %s:%d on fd %d",
inet_ntoa(sockaddr.sin_addr), ntohs(sockaddr.sin_port), client_fd);
ev_io_init(&request->ev_watcher, &ev_io_on_read,
client_fd, EV_READ);
ev_io_start(mainloop, &request->ev_watcher);
}
static void
ev_io_on_read(struct ev_loop* mainloop, ev_io* watcher, const int events)
{
static char read_buf[READ_BUFFER_SIZE];
Request* request = REQUEST_FROM_WATCHER(watcher);
ssize_t read_bytes = read(
request->client_fd,
read_buf,
READ_BUFFER_SIZE
);
GIL_LOCK(0);
if(read_bytes <= 0) {
if(errno != EAGAIN && errno != EWOULDBLOCK) {
if(read_bytes == 0)
DBG_REQ(request, "Client disconnected");
else
DBG_REQ(request, "Hit errno %d while read()ing", errno);
close(request->client_fd);
ev_io_stop(mainloop, &request->ev_watcher);
Request_free(request);
}
goto out;
}
Request_parse(request, read_buf, (size_t)read_bytes);
if(request->state.error_code) {
DBG_REQ(request, "Parse error");
request->current_chunk = PyBytes_FromString(
http_error_messages[request->state.error_code]);
assert(request->iterator == NULL);
}
else if(request->state.parse_finished) {
if(!wsgi_call_application(request)) {
assert(PyErr_Occurred());
PyErr_Print();
assert(!request->state.chunked_response);
Py_XCLEAR(request->iterator);
request->current_chunk = PyBytes_FromString(
http_error_messages[HTTP_SERVER_ERROR]);
}
} else {
/* Wait for more data */
goto out;
}
ev_io_stop(mainloop, &request->ev_watcher);
ev_io_init(&request->ev_watcher, &ev_io_on_write,
request->client_fd, EV_WRITE);
ev_io_start(mainloop, &request->ev_watcher);
out:
GIL_UNLOCK(0);
return;
}
/* XXX too many gotos */
static void
ev_io_on_write(struct ev_loop* mainloop, ev_io* watcher, const int events)
{
Request* request = REQUEST_FROM_WATCHER(watcher);
GIL_LOCK(0);
if(request->state.use_sendfile) {
/* sendfile */
if(request->current_chunk) {
/* current_chunk contains the HTTP headers */
if(send_chunk(request))
goto out;
assert(request->current_chunk_p == 0);
/* abuse current_chunk_p to store the file fd */
request->current_chunk_p = PyObject_AsFileDescriptor(request->iterable);
goto out;
}
if(do_sendfile(request))
goto out;
} else {
/* iterable */
if(send_chunk(request))
goto out;
if(request->iterator) {
PyObject* next_chunk;
next_chunk = wsgi_iterable_get_next_chunk(request);
if(next_chunk) {
if(request->state.chunked_response) {
request->current_chunk = wrap_http_chunk_cruft_around(next_chunk);
Py_DECREF(next_chunk);
} else {
request->current_chunk = next_chunk;
}
assert(request->current_chunk_p == 0);
goto out;
} else {
if(PyErr_Occurred()) {
PyErr_Print();
/* We can't do anything graceful here because at least one
* chunk is already sent... just close the connection */
DBG_REQ(request, "Exception in iterator, can not recover");
ev_io_stop(mainloop, &request->ev_watcher);
close(request->client_fd);
Request_free(request);
goto out;
}
Py_CLEAR(request->iterator);
}
}
if(request->state.chunked_response) {
/* We have to send a terminating empty chunk + \r\n */
request->current_chunk = PyBytes_FromString("0\r\n\r\n");
assert(request->current_chunk_p == 0);
request->state.chunked_response = false;
goto out;
}
}
ev_io_stop(mainloop, &request->ev_watcher);
if(request->state.keep_alive) {
DBG_REQ(request, "done, keep-alive");
Request_clean(request);
Request_reset(request);
ev_io_init(&request->ev_watcher, &ev_io_on_read,
request->client_fd, EV_READ);
ev_io_start(mainloop, &request->ev_watcher);
} else {
DBG_REQ(request, "done, close");
close(request->client_fd);
Request_free(request);
}
out:
GIL_UNLOCK(0);
}
static bool
send_chunk(Request* request)
{
Py_ssize_t bytes_sent;
assert(request->current_chunk != NULL);
assert(!(request->current_chunk_p == PyBytes_GET_SIZE(request->current_chunk)
&& PyBytes_GET_SIZE(request->current_chunk) != 0));
bytes_sent = write(
request->client_fd,
PyBytes_AS_STRING(request->current_chunk) + request->current_chunk_p,
PyBytes_GET_SIZE(request->current_chunk) - request->current_chunk_p
);
if(bytes_sent == -1)
return handle_nonzero_errno(request);
request->current_chunk_p += bytes_sent;
if(request->current_chunk_p == PyBytes_GET_SIZE(request->current_chunk)) {
Py_CLEAR(request->current_chunk);
request->current_chunk_p = 0;
return false;
}
return true;
}
static bool
do_sendfile(Request* request)
{
Py_ssize_t bytes_sent = portable_sendfile(
request->client_fd,
request->current_chunk_p /* current_chunk_p stores the file fd */
);
if(bytes_sent == -1)
return handle_nonzero_errno(request);
return bytes_sent != 0;
}
static bool
handle_nonzero_errno(Request* request)
{
if(errno == EAGAIN || errno == EWOULDBLOCK) {
/* Try again later */
return true;
} else {
/* Serious transmission failure. Hang up. */
fprintf(stderr, "Client %d hit errno %d\n", request->client_fd, errno);
Py_XDECREF(request->current_chunk);
Py_XCLEAR(request->iterator);
request->state.keep_alive = false;
return false;
}
}