-
Notifications
You must be signed in to change notification settings - Fork 1
/
chibiWeb.c
290 lines (243 loc) · 7.12 KB
/
chibiWeb.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
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <signal.h>
#include "chibiWeb.h"
#include "request.h"
#include "response.h"
#include "chibiWebDefs.h"
#include "tsQueue.h"
#define POOL_SIZE 4
#define BUFSIZE 1000
typedef struct pHandle {
char* path;
char* localPath;
Handler handler;
} PathHandle;
Handler h = NULL;
TSQueue *filePaths, *paths, *workQ;
// use volatile to prevent compiler caching
static volatile int keepRunning = TRUE;
/* Copy s1 and s2 into a new str and return it */
char* strcatcpy(char* s1, char* s2) {
char* str = (char*) malloc(strlen(s1) + strlen(s2) + 1);
if (!str) return NULL;
sprintf(str, "%s%s", s1, s2);
return str;
}
void pathhandle_free(void* ph) {
PathHandle* p = (PathHandle*) ph;
free(p->path);
if (p->localPath) free(p->localPath);
free(p);
}
int find_path(void *a, void *b) {
char *path = (char *) a;
PathHandle *phb = (PathHandle *) b;
return !strcmp(path, phb->path);
}
void interruptHandler(int dummy) {
keepRunning = FALSE;
}
void setup_signal_handler(){
// use sigaction to stop blocking calls from restarting after interrupts
struct sigaction a;
a.sa_handler = interruptHandler;
a.sa_flags = 0;
sigemptyset(&a.sa_mask);
sigaction(SIGINT, &a, NULL);
}
int chibi_init() {
setup_signal_handler();
filePaths = tsq_create();
if (!filePaths) {
printf("Failed to init filePaths queue\n");
return FALSE;
}
paths = tsq_create();
if (!paths) {
printf("Failed to init paths queue\n");
return FALSE;
}
workQ = tsq_create();
if (!workQ) {
printf("Failed to init work queue\n");
return FALSE;
}
return TRUE;
}
Response *serveFile(Request *req) {
PathHandle* ph = (PathHandle *) tsq_find(filePaths, find_path, req->root);
if (!ph) {
printf("serveFile: %s\n", req->root);
return 0;
}
req->file = strcatcpy(ph->localPath, req->file);
printf("Resolved filepath: %s\n", req->file);
if (!req->file) return 0;
return response_new_file(STATUS_200_OK, req->file);
}
int serve(char *path, char* localPath, Handler handler, TSQueue *tsq) {
PathHandle *p = (PathHandle *) malloc(sizeof(PathHandle));
if (p == NULL) return 0;
// copy over paths
p->path = strdup(path);
if (localPath) p->localPath = strdup(localPath);
p->handler = handler;
if (tsq_put(tsq, p)) return 1;
// put failed, free and bail
pathhandle_free(p);
return 0;
}
int chibi_serve(char *path, Handler handler) {
return serve(path, NULL, handler, paths);
}
int chibi_serveFiles(char *path, char *localPath) {
return serve(path, localPath, serveFile, filePaths);
}
int transferFile(int clientfd, int fd) {
char buf[BUFSIZE];
int bytesRead = 0;
int bytesSent = 0;
char* p = NULL;
printf("transferFile: starting transfer(%d -> %d)\n", fd, clientfd);
while(1) {
// read a chunk
bytesRead = read(fd, buf, BUFSIZE);
if (bytesRead <= 0) break;
p = buf;
while (bytesRead > 0) {
// write a chunk
bytesSent = write(clientfd, p, bytesRead);
if (bytesSent <= 0) break;
bytesRead -= bytesSent;
p += bytesSent;
}
}
if (bytesRead < 0) perror("transferFile: READ ERROR\n");
if (bytesSent < 0) perror("transferFile: WRITE ERROR\n");
close(fd);
printf("transferFile: transfer complete(%d -> %d)\n", fd, clientfd);
return bytesSent;
}
void *workerThread(void *workQueue) {
TSQueue *wq = (TSQueue *) workQueue;
int *clientfd = NULL;
Request *req = NULL;
Response *resp = NULL;
char request[REQUEST_SIZE];
int len = 0;
while(keepRunning) {
clientfd = tsq_get(wq);
/* Check for stop sentinel */
if (*clientfd == -1) break;
/* Otherwise got work */
printf("Thread %p got work\n", pthread_self());
memset(request, '\0', REQUEST_SIZE);
len = recv(*clientfd, request, REQUEST_SIZE, 0);
if (len <= 0) {
free(clientfd);
continue;
}
printf("LEN:%d\n", len);
req = request_parse(request);
/* find matching path for request */
PathHandle *ph = (PathHandle *) tsq_find(filePaths, find_path, req->root);
if (ph != NULL) {
resp = serveFile(req);
} else {
printf("No matching filepath\n");
/* Call client response handler */
ph = (PathHandle *) tsq_find(paths, find_path, req->path);
if (ph != NULL) resp = ph->handler(req);
else resp = NULL;
}
if (resp == NULL) {
resp = response_new(STATUS_404_NOT_FOUND, "", 0);
}
/* Write response back to client */
write(*clientfd, resp->msg, resp->len);
if (resp->isFile) transferFile(*clientfd, resp->fd);
close(*clientfd);
request_free(req);
response_free(resp);
free(clientfd);
}
pthread_exit(0);
}
int chibi_run(int port, int poolSize) {
printf("chibiWeb starting...\n");
pthread_t workerPool[POOL_SIZE];
int listenfd;
struct sockaddr_in serv_addr;
int socket_yes = 1;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '\0', sizeof serv_addr);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
printf("> opening socket on port: %d\n", port);
// set SO_REUSEADDR to prevent "Address already in use"
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &socket_yes, sizeof socket_yes) == -1) {
perror("setsockopt");
exit(1);
}
if (bind(listenfd, (struct sockaddr*) &serv_addr, sizeof serv_addr) == -1) {
perror("bind");
exit(1);
}
printf("> bound socket\n");
printf("> creating workers...");
for (int i = 0; i < POOL_SIZE; i++) {
pthread_create(&workerPool[i], NULL, workerThread, (void*) workQ);
}
printf(" created %d\n", POOL_SIZE);
printf("> listening on: http://127.0.0.1:%d\n", port);
listen(listenfd, LISTEN_WAITERS);
int *clientfd;
// main loop for receiving incoming requests
while(keepRunning) {
clientfd = (int *) malloc(sizeof(int));
if (clientfd == NULL) break;
*clientfd = accept(listenfd, (struct sockaddr*) NULL, NULL);
// check for signal interrupt
if (*clientfd == -1 & errno == EINTR) break;
// got a valid fd, add to queue to service
tsq_put(workQ, clientfd);
}
printf("> exiting\n");
void *result = NULL;
printf("> stopping threads...");
/* fill workQ with stop sentinels */
int stopSentinel = -1;
for (int i = 0; i < POOL_SIZE; i++) {
tsq_put(workQ, &stopSentinel);
}
/* Wait on threads to pick up sentinel and stop gracefully */
for (int i = 0; i < POOL_SIZE; i++) {
pthread_join(workerPool[i], &result);
printf(" [%d]", i);
}
printf("done\n");
printf("> freeing resources...");
// free data stores
tsq_destroy(workQ, NULL);
tsq_destroy(paths, &pathhandle_free);
tsq_destroy(filePaths, &pathhandle_free);
// free/close file descriptors
if (clientfd) free(clientfd);
close(listenfd);
printf("done\n");
printf("Shutdown complete\n");
return 0;
}