-
Notifications
You must be signed in to change notification settings - Fork 4
/
kweb.h
107 lines (90 loc) · 2.36 KB
/
kweb.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
#ifndef KWEB_H
#define KWEB_H
#include <pthread.h>
#include "kqueue.h"
#include "os.h"
#define VERSION "1.0"
#define ERROR 42
#define LOG 44
#define BUFSIZE 8192
#define FORBIDDEN 403
#define NOTFOUND 404
#define MAX_BURST 10
#define KWEB_SREAD_TIMEOUT (1000*30)
#define KWEB_SWRITE_TIMEOUT (-1)
#define RFC1123_TIME_LEN 29
enum {
REQ_NEW,
REQ_ALIVE
};
struct server_stats {
uint64_t tsc_freq;
};
struct http_request {
char rsp_header[256];
char static_buf[BUFSIZE+1];
char *buf;
int header_length;
int length;
};
struct http_connection {
struct kitem conn;
unsigned int burst_length;
int ref_count;
int socketfd;
int buf_length;
char buf[BUFSIZE+1];
mutex_t writelock;
int should_close;
/* TODO: these are linux specific, consider hiding them better */
int epollrfd;
int epollwfd;
};
enum {
FORBIDDEN_PAGE,
NOTFOUND_PAGE,
OK_HEADER,
URLCMD_PAGE,
};
extern struct tpool tpool;
void enqueue_connection_tail(struct kqueue *q, struct http_connection *c);
void enqueue_connection_head(struct kqueue *q, struct http_connection *c);
void http_server(struct kqueue *q, struct kitem *__c);
/* OS dependent, in linux.c or akaros.c */
void init_connection(struct http_connection *c);
void destroy_connection(struct http_connection *c);
ssize_t timed_read(struct http_connection *c, void *buf, size_t count);
ssize_t timed_write(struct http_connection *c, const char *buf, size_t count);
void dispatch_call(int call_fd, void *client_addr);
#ifndef DEBUG
#define logger(type, s1, s2, socket_fd) ({type;})
#else
void logger(int type, char *s1, char *s2, int socket_fd)
{
int fd;
char *logbuffer = malloc(BUFSIZE*2);
switch (type) {
case ERROR:
sprintf(logbuffer, "ERROR: %s:%s Errno=%d exiting pid=%d",
s1, s2, errno, getpid());
break;
case FORBIDDEN:
sprintf(logbuffer, "FORBIDDEN: %s:%s", s1, s2);
break;
case NOTFOUND:
sprintf(logbuffer, "NOT FOUND: %s:%s", s1, s2);
break;
case LOG:
sprintf(logbuffer, "INFO: %s:%s:%d", s1, s2, socket_fd);
break;
}
/* No checks here, nothing can be done with a failure anyway */
if((fd = open("kweb.log", O_CREAT | O_WRONLY | O_APPEND, 0643)) >= 0) {
write(fd, logbuffer, strlen(logbuffer));
write(fd, "\n", 1);
close(fd);
}
free(logbuffer);
}
#endif // DEBUG
#endif // KWEB_H