-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpserver.c
333 lines (282 loc) · 8.35 KB
/
httpserver.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
#include "helper_funcs_socket.h"
#include "connection.h"
#include "response.h"
#include "request.h"
#include "queue.h"
#include "rwlock.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <err.h>
#include <errno.h>
int num_threads = 4;
void handle_connection(int);
void handle_get(conn_t *);
void handle_put(conn_t *);
void handle_unsupported(conn_t *);
typedef struct Node {
char *key;
rwlock_t *rwlock;
struct Node *next;
} node_t;
queue_t *q;
node_t *list_head;
pthread_mutex_t m;
node_t *new_Node(char *k, node_t *nxt) {
node_t *node = (node_t *) malloc(sizeof(node_t));
node->key = strdup(k);
node->rwlock = rwlock_new(N_WAY, 1);
node->next = nxt;
return node;
}
int listSearch(char *key) {
node_t *curr = list_head;
while (curr != NULL && curr->key != NULL) {
if (strcmp(curr->key, key) == 0) {
return 1;
}
curr = curr->next;
}
return 0;
}
void lockNode(char *key, bool reader) {
node_t *curr = list_head;
// To lock a rwlock that already exists
while (curr->next != NULL) {
if (strcmp(curr->key, key) == 0) {
if (reader) {
reader_lock(curr->rwlock);
return;
} else {
writer_lock(curr->rwlock);
return;
}
}
curr = curr->next;
}
}
void unlockNode(char *key, bool reader) {
node_t *curr = list_head;
while (curr->next != NULL) {
if (strcmp(curr->key, key) == 0) {
if (reader) {
reader_unlock(curr->rwlock);
return;
} else {
writer_unlock(curr->rwlock);
return;
}
}
curr = curr->next;
}
}
void insertNode(char *key) {
if (list_head == NULL) {
list_head = new_Node(key, NULL);
// fprintf(stderr, "Makes new head correctly?\n");
return;
}
node_t *curr = list_head;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = new_Node(key, NULL);
}
void deleteList(node_t **head) {
while ((*head) != NULL) {
node_t *tempNext = (*head)->next;
rwlock_delete(&((*head)->rwlock));
free((*head));
(*head) = tempNext;
}
}
void fake_flock(char *URI, int mode, int oper) {
// Mode 0 == LOCK_SH
// Mode 1 == LOCK_EX
// Mode 2 == LOCK_UN
// First check if URI exists in list
int exists = listSearch(URI);
// fprintf(stderr, "Does the URI: %s exist? %d\n", URI, exists);
if (exists == 0) {
// fprintf(stderr, "Attempts to add node.\n");
insertNode(URI);
// fprintf(stderr, "Inserts Node successfully?\n");
}
if (mode == 0) {
lockNode(URI, true);
// fprintf(stderr, "Locks Node for reading successfully?\n");
} else if (mode == 1) {
lockNode(URI, false);
// fprintf(stderr, "Locks Node for writing successfully?\n");
} else {
if (oper == 0) {
unlockNode(URI, true);
// fprintf(stderr, "Unlocks Node from reading successfully?\n");
} else {
unlockNode(URI, false);
// fprintf(stderr, "Unlocks Node from writing successfully?\n");
}
}
}
void *worker_thread() {
while (1) {
uintptr_t connfd = 0;
queue_pop(q, (void **) &connfd);
handle_connection(connfd);
close(connfd);
}
return (void *) 1;
}
void handle_memory(int signal) {
queue_delete(&q);
pthread_mutex_destroy(&m);
deleteList(&list_head);
exit(signal);
}
int main(int argc, char **argv) {
if (argc < 3) {
warnx("wrong arguments: %s port_num", argv[0]);
fprintf(stderr, "invalid arguments");
return EXIT_FAILURE;
}
int c;
while ((c = getopt(argc, argv, "t:")) != -1) {
switch (c) {
case 't': num_threads = atoi(optarg); break;
default: fprintf(stderr, "usage: -t <threadcount> %s <port\n", argv[0]);
}
}
char *endptr = NULL;
size_t port = (size_t) strtoull(argv[optind], &endptr, 10);
if (endptr && *endptr != '\0') {
warnx("invalid port number: %s", argv[optind]);
return EXIT_FAILURE;
}
// Thread crap that was done in section
// Dispatcher
signal(SIGPIPE, SIG_IGN);
// Thread threads[num_threads];
pthread_t threads[num_threads];
q = queue_new(num_threads);
for (int i = 0; i < num_threads; i++) {
pthread_create(&threads[i], NULL, worker_thread, (void *) ((uintptr_t) i));
}
pthread_mutex_init(&m, NULL);
signal(SIGINT, handle_memory);
signal(SIGTERM, handle_memory);
Listener_Socket sock;
listener_init(&sock, port);
while (1) {
uintptr_t connfd = listener_accept(&sock);
queue_push(q, (void *) connfd);
}
pthread_mutex_destroy(&m);
queue_delete(&q);
deleteList(&list_head);
return EXIT_SUCCESS;
}
void handle_connection(int connfd) {
conn_t *conn = conn_new(connfd);
const Response_t *res = conn_parse(conn);
if (res != NULL) {
conn_send_response(conn, res);
} else {
const Request_t *req = conn_get_request(conn);
if (req == &REQUEST_GET) {
handle_get(conn);
} else if (req == &REQUEST_PUT) {
handle_put(conn);
} else {
handle_unsupported(conn);
}
}
conn_delete(&conn);
}
void handle_get(conn_t *conn) {
char *URI = conn_get_uri(conn);
const Response_t *res = NULL;
int fd = open(URI, O_RDONLY);
if (fd == -1) {
if (errno == EACCES) {
fprintf(stderr, "%s,%s,%d,%s\n", "GET", URI, 403, conn_get_header(conn, "Request-Id"));
res = &RESPONSE_FORBIDDEN;
conn_send_response(conn, res);
return;
} else if (errno == ENOENT) {
fprintf(stderr, "%s,%s,%d,%s\n", "GET", URI, 404, conn_get_header(conn, "Request-Id"));
res = &RESPONSE_NOT_FOUND;
conn_send_response(conn, res);
return;
} else {
fprintf(stderr, "%s,%s,%d,%s\n", "GET", URI, 500, conn_get_header(conn, "Request-Id"));
res = &RESPONSE_INTERNAL_SERVER_ERROR;
conn_send_response(conn, res);
return;
}
}
fake_flock(URI, 0, 0);
// flock(fd, LOCK_SH);
struct stat buf1;
fstat(fd, &buf1);
if (S_ISDIR(buf1.st_mode)) {
res = &RESPONSE_FORBIDDEN;
fake_flock(URI, 2, 0);
// flock(fd, LOCK_UN);
close(fd);
conn_send_response(conn, res);
return;
}
conn_send_file(conn, fd, buf1.st_size);
fprintf(stderr, "%s,%s,%d,%s\n", "GET", URI, 200, conn_get_header(conn, "Request-Id"));
fake_flock(URI, 2, 0);
// flock(fd, LOCK_UN);
close(fd);
return;
}
void handle_put(conn_t *conn) {
char *URI = conn_get_uri(conn);
const Response_t *res = NULL;
pthread_mutex_lock(&m);
bool exists = access(URI, F_OK) == 0;
int fd = open(URI, O_CREAT | O_TRUNC | O_WRONLY, 0600);
if (fd < 0) {
if (errno == EACCES || errno == EISDIR || errno == ENOENT) {
fprintf(stderr, "%s,%s,%d,%s\n", "PUT", URI, 403, conn_get_header(conn, "Request-Id"));
res = &RESPONSE_FORBIDDEN;
conn_send_response(conn, res);
return;
} else {
fprintf(stderr, "%s,%s,%d,%s\n", "PUT", URI, 500, conn_get_header(conn, "Request-Id"));
res = &RESPONSE_INTERNAL_SERVER_ERROR;
conn_send_response(conn, res);
return;
}
}
fake_flock(URI, 1, 1);
// flock(fd, LOCK_EX);
pthread_mutex_unlock(&m);
res = conn_recv_file(conn, fd);
if (exists && res == NULL) {
res = &RESPONSE_OK;
fprintf(stderr, "%s,%s,%d,%s\n", "PUT", URI, 200, conn_get_header(conn, "Request-Id"));
} else if (!exists && res == NULL) {
res = &RESPONSE_CREATED;
fprintf(stderr, "%s,%s,%d,%s\n", "PUT", URI, 201, conn_get_header(conn, "Request-Id"));
}
conn_send_response(conn, res);
fake_flock(URI, 2, 1);
// flock(fd, LOCK_UN);
close(fd);
return;
}
void handle_unsupported(conn_t *conn) {
const Response_t *res = NULL;
res = &RESPONSE_NOT_IMPLEMENTED;
conn_send_response(conn, res);
fprintf(stderr, "%s,%s,%d,%s\n", "", "", 501, "0");
}