forked from sysprog21/khttpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server.c
435 lines (384 loc) · 14 KB
/
http_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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/err.h>
#include <linux/fs.h>
#include <linux/sched/signal.h>
#include <linux/string.h>
#include <linux/tcp.h>
#include <linux/workqueue.h>
#include "http_parser.h"
#include "http_server.h"
#define CRLF "\r\n"
#define CMWQ_MODE 0
#define PATH "/home/oslab/xueyang/linux2023/khttpd"
#define HTTP_RESPONSE_200_DUMMY \
"" \
"HTTP/1.1 200 OK" CRLF "Server: " KBUILD_MODNAME CRLF \
"Content-Type: text/html" CRLF "Connection: Close" CRLF \
"Transfer-Encoding: chunked" CRLF CRLF
#define HTTP_RESPONSE_200_KEEPALIVE_DUMMY \
"" \
"HTTP/1.1 200 OK" CRLF "Server: " KBUILD_MODNAME CRLF \
"Content-Type: text/html" CRLF "Connection: Keep-Alive" CRLF \
"Transfer-Encoding: chunked" CRLF CRLF
#define HTTP_RESPONSE_501 \
"" \
"HTTP/1.1 501 Not Implemented" CRLF "Server: " KBUILD_MODNAME CRLF \
"Content-Type: text/plain" CRLF "Content-Length: 21" CRLF \
"Connection: Close" CRLF CRLF "501 Not Implemented" CRLF
#define HTTP_RESPONSE_501_KEEPALIVE \
"" \
"HTTP/1.1 501 Not Implemented" CRLF "Server: " KBUILD_MODNAME CRLF \
"Content-Type: text/plain" CRLF "Content-Length: 21" CRLF \
"Connection: KeepAlive" CRLF CRLF "501 Not Implemented" CRLF
/**
* * If CRLF must be included in chunk data, the len of CRLF is 2.
* * The len of chunk data is len of char array + 2 * the number of CRLF.
*/
#define HTTP_RESPONSE_DIRECTORY_LIST_BEGIN \
"" \
"7B" CRLF "<html><head><style>" CRLF \
"body{font-family: monospace; font-size: 15px;}" CRLF \
"td {padding: 1.5px 6px;}" CRLF "</style></head><body><table>" CRLF
#define HTTP_RESPONSE_DIRECTORY_LIST_END \
"" \
"16" CRLF "</table></body></html>" CRLF "0" CRLF CRLF
#define RECV_BUFFER_SIZE 4096
struct http_request {
struct socket *socket;
enum http_method method;
char request_url[128];
int complete;
struct dir_context dir_context;
};
extern struct workqueue_struct *http_server_wq;
struct httpd_service daemon = {.is_stopped = false};
static int http_server_recv(struct socket *sock, char *buf, size_t size)
{
struct kvec iov = {.iov_base = (void *) buf, .iov_len = size};
struct msghdr msg = {.msg_name = 0,
.msg_namelen = 0,
.msg_control = NULL,
.msg_controllen = 0,
.msg_flags = 0};
return kernel_recvmsg(sock, &msg, &iov, 1, size, msg.msg_flags);
}
static int http_server_send(struct socket *sock, const char *buf, size_t size)
{
struct msghdr msg = {.msg_name = NULL,
.msg_namelen = 0,
.msg_control = NULL,
.msg_controllen = 0,
.msg_flags = 0};
int done = 0;
while (done < size) {
struct kvec iov = {
.iov_base = (void *) ((char *) buf + done),
.iov_len = size - done,
};
int length = kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
if (length < 0) {
pr_err("write error: %d\n", length);
break;
}
done += length;
}
return done;
}
static void http_server_send_header(struct socket *sock,
int status,
const char *status_msg,
const char *content_type,
int keep_alive,
int content_length)
{
char buf[256] = {0};
snprintf(buf, sizeof(buf),
"HTTP/1.1 %d %s" CRLF "Server: " KBUILD_MODNAME CRLF
"Content-Type: %s" CRLF "Content-Length: %d" CRLF
"Connection: %s" CRLF CRLF,
status, status_msg, content_type, content_length,
keep_alive ? "Keep-Alive" : "Close");
http_server_send(sock, buf, strlen(buf));
}
static int http_server_trace_dir(struct dir_context *dir_context,
const char *name,
int namelen,
loff_t offset,
u64 ino,
unsigned int d_type)
{
if (strcmp(name, ".") && strcmp(name, "..")) {
struct http_request *request =
container_of(dir_context, struct http_request, dir_context);
char buf[256] = {0};
snprintf(buf, sizeof(buf),
"%x\r\n<tr><td><a href=\"%s\">%s</a></td></tr>\r\n",
33 + (namelen << 1), name, name);
http_server_send(request->socket, buf, strlen(buf));
}
return 0;
}
static inline int read_file(struct file *fp, char *buf)
{
return kernel_read(fp, buf, fp->f_inode->i_size, 0);
}
static void handle_directory(struct http_request *request, int keep_alive)
{
char *response;
char absolute_path[100];
struct file *fp;
request->dir_context.actor = http_server_trace_dir;
if (request->method != HTTP_GET) {
response = keep_alive ? HTTP_RESPONSE_501_KEEPALIVE : HTTP_RESPONSE_501;
http_server_send(request->socket, response, strlen(response));
return;
}
/* extern struct file *filp_open(const char *, int, umode_t); */
memcpy(absolute_path, PATH, strlen(PATH));
memcpy(absolute_path + strlen(PATH), request->request_url,
strlen(request->request_url));
absolute_path[strlen(PATH) + strlen(request->request_url)] = '\0';
fp = filp_open(absolute_path, O_RDONLY, 0);
if (IS_ERR(fp)) {
pr_err("open error: %s %ld\n", absolute_path, PTR_ERR(fp));
return;
} else {
printk("open success: %s\n", absolute_path);
}
if (S_ISDIR(fp->f_inode->i_mode)) {
response = keep_alive ? HTTP_RESPONSE_200_KEEPALIVE_DUMMY
: HTTP_RESPONSE_200_DUMMY;
http_server_send(request->socket, response, strlen(response));
response = HTTP_RESPONSE_DIRECTORY_LIST_BEGIN;
http_server_send(request->socket, response, strlen(response));
iterate_dir(fp, &request->dir_context);
response = HTTP_RESPONSE_DIRECTORY_LIST_END;
http_server_send(request->socket, response, strlen(response));
} else {
/* is a file */
char *read_data = kmalloc(fp->f_inode->i_size, GFP_KERNEL);
int ret = read_file(fp, read_data);
if (ret < 0) {
pr_err("read file error: %d\n", ret);
return;
}
http_server_send_header(request->socket, 200, "OK", "text/plain",
keep_alive, ret);
http_server_send(request->socket, read_data, ret);
kfree(read_data);
}
filp_close(fp, NULL);
}
static int http_server_response(struct http_request *request, int keep_alive)
{
handle_directory(request, keep_alive);
return 0;
}
static int http_parser_callback_message_begin(http_parser *parser)
{
struct http_request *request = parser->data;
struct socket *socket = request->socket;
memset(request, 0x00, sizeof(struct http_request));
request->socket = socket;
return 0;
}
static int http_parser_callback_request_url(http_parser *parser,
const char *p,
size_t len)
{
struct http_request *request = parser->data;
strncat(request->request_url, p, len);
return 0;
}
static int http_parser_callback_header_field(http_parser *parser,
const char *p,
size_t len)
{
return 0;
}
static int http_parser_callback_header_value(http_parser *parser,
const char *p,
size_t len)
{
return 0;
}
static int http_parser_callback_headers_complete(http_parser *parser)
{
struct http_request *request = parser->data;
request->method = parser->method;
return 0;
}
static int http_parser_callback_body(http_parser *parser,
const char *p,
size_t len)
{
return 0;
}
static int http_parser_callback_message_complete(http_parser *parser)
{
struct http_request *request = parser->data;
http_server_response(request, http_should_keep_alive(parser));
request->complete = 1;
return 0;
}
#if CMWQ_MODE > 0
static void http_server_worker(struct work_struct *work)
{
char *buf;
struct khttpd *worker = container_of(work, struct khttpd, khttpd_work);
struct socket *socket = worker->sock;
struct http_request request;
struct http_parser parser;
struct http_parser_settings setting = {
.on_message_begin = http_parser_callback_message_begin,
.on_url = http_parser_callback_request_url,
.on_header_field = http_parser_callback_header_field,
.on_header_value = http_parser_callback_header_value,
.on_headers_complete = http_parser_callback_headers_complete,
.on_body = http_parser_callback_body,
.on_message_complete = http_parser_callback_message_complete};
allow_signal(SIGKILL);
allow_signal(SIGTERM);
buf = kzalloc(RECV_BUFFER_SIZE, GFP_KERNEL);
if (!buf) {
pr_err("can't allocate memory!\n");
return;
}
request.socket = socket;
http_parser_init(&parser, HTTP_REQUEST);
parser.data = &request;
while (!daemon.is_stopped) {
int ret;
ret = http_server_recv(socket, buf, RECV_BUFFER_SIZE - 1);
if (ret <= 0) {
pr_err("read error: %d\n", ret);
break;
}
http_parser_execute(&parser, &setting, buf, ret);
if (request.complete && !http_should_keep_alive(&parser))
break;
memset(buf, 0, RECV_BUFFER_SIZE);
}
kernel_sock_shutdown(socket, SHUT_RDWR);
sock_release(socket);
kfree(buf);
pr_info("http_server_worker exit\n");
}
static struct work_struct *create_work(struct socket *socket)
{
struct khttpd *work = kmalloc(sizeof(struct khttpd), GFP_KERNEL);
if (!work)
return NULL;
work->sock = socket;
INIT_WORK(&work->khttpd_work, http_server_worker);
list_add(&work->list, &daemon.worker_list);
return &work->khttpd_work;
}
static void free_work(void)
{
struct khttpd *safe, *next;
list_for_each_entry_safe (safe, next, &daemon.worker_list, list) {
kernel_sock_shutdown(safe->sock, SHUT_RDWR);
flush_work(&safe->khttpd_work);
sock_release(safe->sock);
kfree(safe);
}
}
#else
struct task_struct *my_kthread_run(int (*threadfn)(void *data),
void *data,
const char *namefmt,
...)
{
// Call the original kthread_run function
return kthread_run(threadfn, data, namefmt);
}
static int http_server_worker(void *arg)
{
char *buf;
struct http_parser parser;
struct http_parser_settings setting = {
.on_message_begin = http_parser_callback_message_begin,
.on_url = http_parser_callback_request_url,
.on_header_field = http_parser_callback_header_field,
.on_header_value = http_parser_callback_header_value,
.on_headers_complete = http_parser_callback_headers_complete,
.on_body = http_parser_callback_body,
.on_message_complete = http_parser_callback_message_complete};
struct http_request request;
struct socket *socket = (struct socket *) arg;
allow_signal(SIGKILL);
allow_signal(SIGTERM);
buf = kzalloc(RECV_BUFFER_SIZE, GFP_KERNEL);
if (!buf) {
pr_err("can't allocate memory!\n");
return -1;
}
request.socket = socket;
http_parser_init(&parser, HTTP_REQUEST);
parser.data = &request;
while (!kthread_should_stop()) {
/* kernel_recvmsg will return the numbers of bytes received */
int ret = http_server_recv(socket, buf, RECV_BUFFER_SIZE - 1);
if (ret <= 0) {
if (ret)
pr_err("recv error: %d\n", ret);
break;
}
http_parser_execute(&parser, &setting, buf, ret);
if (request.complete && !http_should_keep_alive(&parser))
break;
memset(buf, 0, RECV_BUFFER_SIZE);
}
kernel_sock_shutdown(socket, SHUT_RDWR);
sock_release(socket);
kfree(buf);
return 0;
}
#endif
int http_server_daemon(void *arg)
{
struct socket *client_socket;
struct http_server_param *param = (struct http_server_param *) arg;
#if CMWQ_MODE > 0
struct work_struct *work;
#else
struct task_struct *worker;
#endif
allow_signal(SIGKILL);
allow_signal(SIGTERM);
INIT_LIST_HEAD(&daemon.worker_list);
while (!kthread_should_stop()) {
int err = kernel_accept(param->listen_socket, &client_socket, 0);
if (err < 0) {
if (signal_pending(current))
break;
pr_err("kernel_accept() error: %d\n", err);
continue;
}
#if CMWQ_MODE > 0
if (unlikely(!(work = create_work(client_socket)))) {
printk(KERN_ERR MODULE_NAME
": create work error, connection closed\n");
kernel_sock_shutdown(client_socket, SHUT_RDWR);
sock_release(client_socket);
continue;
}
/* start server worker */
queue_work(http_server_wq, work);
#else
worker =
my_kthread_run(http_server_worker, client_socket, KBUILD_MODNAME);
if (IS_ERR(worker)) {
pr_err("can't create more worker process\n");
continue;
}
#endif
}
#if CMWQ_MODE > 0
printk(MODULE_NAME ": daemon shutdown in progress...\n");
daemon.is_stopped = true;
free_work();
#endif
return 0;
}