-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-server.c
482 lines (355 loc) · 12.8 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
http-server.c
a program by Jack Damon
April, 2017
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#define MAXPENDING 5
static void die(const char *message)
{
perror(message);
exit(1);
}
int sendStatus(int clientSock, char *sendMessage);
int sendResponse(int clientSock, char *responseMessage);
int isDir(char *filepath);
int sendMdbResults(int client_sock, int mdb_lookup_sock, FILE *response, char *toSearch);
// ./http-server <server_port> <web_root> <mdb-lookup-host> <mdb-lookup-port>
// example call: ./http-server 8888 ~/html localhost 9999
int main(int argc, char **argv)
{
if (argc != 5){
fprintf(stderr, "%s\n", "usage: http-server <server_port> <web_root> <mdb-lookup-host> <mdb-lookup-port>");
exit(1);
}
// Make TCP connection to mdb-lookup-server
struct addrinfo hints, *res;
int mdb_lookup_sock;
// Loading up address structs with getaddrinfo()
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
getaddrinfo(argv[3], argv[4], &hints, &res);
// make a socket:
mdb_lookup_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if(mdb_lookup_sock < 0){
die("socket() failed");
}
int connectCheck;
connectCheck = connect(mdb_lookup_sock, res->ai_addr, res->ai_addrlen);
if(connectCheck < 0){
die("connect() failed");
}
FILE *response = fdopen(mdb_lookup_sock, "r");
freeaddrinfo(res);
// Open socket on a port
int server_sock, client_sock;
struct sockaddr_in echoServAddr, echoClntAddr;
unsigned short echoServPort = atoi(argv[1]);
unsigned int clntLen;
/* Create a socket for incoming connections */
if ((server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
die("socket() failed");
}
/* Construct local address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr));
echoServAddr.sin_family = AF_INET;
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
echoServAddr.sin_port = htons(echoServPort);
/* Bind to the local address */
if (bind(server_sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0){
die("bind() failed"); // CONTINUE TO BREAK LOOP
}
/* Mark the socket so it will listen for incoming connections */
if (listen(server_sock, MAXPENDING) < 0){
die("listen() failed");
}
while(1){
// wait for client to make a request
/* Set the size of the in-out parameter */
clntLen = sizeof(echoClntAddr);
/* Wait for a client to connect */
if((client_sock = accept(server_sock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0){
die("accept() failed");
}
/* clntSock is connected to a client */
char *ip = inet_ntoa(echoClntAddr.sin_addr);
// read the request (eg, this person wants page "index.html")
char recvBuffer[2048];
if (recv(client_sock, &recvBuffer, sizeof(recvBuffer), 0) < 0){
char *returnMessage = "500 Internal Service Error";
sendResponse(client_sock, returnMessage);
printf("Connected from %s, died before recieving request. %s\n", ip, returnMessage);
continue;
}
// Parse and check the request
//First, fix the request URI if it needs it
char *token_separators = "\t \r\n"; // tab, space, new line
char *method = strtok(recvBuffer, token_separators);
char *requestURI = strtok(NULL, token_separators);
char *httpVersionGrabber = strtok(NULL, token_separators);
char httpVersion[1024];
if (requestURI == NULL){
char *returnMessage = "400 Bad Request";
sendResponse(client_sock, returnMessage);
// Print request/response details to stdout
printf("%s \" \" %s\n", ip, returnMessage);
close(client_sock);
continue;
}
if(httpVersionGrabber != NULL){
strcpy(httpVersion, httpVersionGrabber);
}
if(!strstr(requestURI, "mdb-lookup")){
char pathToCheck[1024];
strcpy(pathToCheck, argv[2]);
strcat(pathToCheck, requestURI);
int dirCheck;
if(pathToCheck[strlen(pathToCheck)-1] != '/'){
if((dirCheck = isDir(pathToCheck))){
strcat(requestURI, "/index.html");
}
if (dirCheck < 0){
char *returnMessage = "404 Not Found";
sendResponse(client_sock, returnMessage);
close(client_sock);
continue;
}
}
if(requestURI[strlen(requestURI) -1] == '/'){
strcat(requestURI, "index.html");
}
if(!strstr(method, "GET")){
char *returnMessage = "501 Not Implemented";
sendResponse(client_sock, returnMessage);
// Print request/response details to stdout
printf("%s \"%s %s %s\" %s\n", ip, method, requestURI, httpVersion, returnMessage);
close(client_sock);
continue;
}
// Respond with 400 if request URI does not begin with "/"
if(requestURI[0] != '/'){
char *returnMessage = "400 Bad Request";
sendResponse(client_sock, returnMessage);
// Print request/response details to stdout
printf("%s \"%s %s %s\" %s\n", ip, method, requestURI, httpVersion, returnMessage);
close(client_sock);
continue;
}
// Respond with 400 if request URI contains "/.."
if(strstr(requestURI,"/..")){
char *returnMessage = "400 Bad Request";
sendResponse(client_sock, returnMessage);
// Print request/response details to stdout
printf("%s \"%s %s %s\" %s\n", ip, method, requestURI, httpVersion, returnMessage);
close(client_sock);
continue;
}
// Respond with 501 if protocol is not HTTP
if(!strstr(httpVersion,"HTTP")){
char *returnMessage = "501 Not Implemented";
sendResponse(client_sock, returnMessage);
// Print request/response details to stdout
printf("%s \"%s %s %s\" %s\n", ip, method, requestURI, httpVersion, returnMessage);
close(client_sock);
continue;
}
// Respond with 501 if HTTP version is not 1.0 or 1.1
if( (!strstr(httpVersion,"1.0")) && (!strstr(httpVersion,"1.1")) ){
char *returnMessage = "501 Not Implemented";
sendResponse(client_sock, returnMessage);
// Print request/response details to stdout
printf("%s \"%s %s %s\" %s\n", ip, method, requestURI, httpVersion, returnMessage);
close(client_sock);
continue;
}
char filepath[1024];
strcpy(filepath, argv[2]);
strcat(filepath, requestURI);
// find, read, and send "index.html"
FILE *toRead = fopen(filepath, "rb");
if (toRead == NULL){
char *returnMessage = "404 Not Found";
sendResponse(client_sock, returnMessage);
// Print request/response details to stdout
printf("%s \"%s %s %s\" %s\n", ip, method, requestURI, httpVersion, returnMessage);
close(client_sock);
continue;
}
char buffer[4096];
size_t n;
char *statusMessage = "200 OK";
while((n = fread(buffer, 1, sizeof(buffer), toRead)) > 0){
if(send(client_sock, buffer, n, 0) < 0){
die("send() failed");
}
}
printf("%s \"%s %s %s\" %s\n", ip, method, requestURI, httpVersion, statusMessage);
close(client_sock);
fclose(toRead);
} else {
const char *form =
"HTTP/1.0 200 OK\n"
"Content-Type: text/html\r\n\r\n"
"<h1>mdb-lookup</h1>\n"
"<p>\n"
"<form method=GET action=/mdb-lookup>\n"
"lookup: <input type=text name=key>\n"
"<input type=submit>\n"
"</form>\n"
"<p>\n";
int len, bytes_sent;
len = strlen(form);
bytes_sent = send(client_sock, form, len, 0);
if(bytes_sent != strlen(form)){
die("value returned by send() doesn't match value in len");
}
if (bytes_sent < 0){
die("send() failed");
}
if(strstr(requestURI, "mdb-lookup?key=")){
char reqCopy[1024];
strcpy(reqCopy, requestURI);
char *token_separators = "=";
char *firstHalf = strtok(requestURI, token_separators);
char *wordToSearch = strtok(NULL, token_separators);
int searchIsNull = 0;
if(wordToSearch == NULL){
searchIsNull = 1;
}
char *returnMessage = "200 OK";
if(searchIsNull){
char *newLineSearch = "\n";
printf("looking up[%s]: %s \"%s %s %s\" %s\n", "", ip, method, reqCopy, httpVersion, returnMessage);
sendMdbResults(client_sock, mdb_lookup_sock, response, newLineSearch);
if(firstHalf){
int x = 1;
x--;
}
} else {
printf("looking up[%s]: %s \"%s %s %s\" %s\n", wordToSearch, ip, method, reqCopy, httpVersion, returnMessage);
sendMdbResults(client_sock, mdb_lookup_sock, response, wordToSearch);
if(firstHalf){
int x = 1;
x--;
}
}
} else {
char *returnMessage = "200 OK";
printf("%s \"%s %s %s\" %s\n", ip, method, requestURI, httpVersion, returnMessage);
}
close(client_sock);
}
}
// send an html header, THEN send contents of "index.html"
fclose(response);
return 0;
}
int sendMdbResults(int client_sock, int mdb_lookup_sock, FILE *response, char *toSearch)
{
if(strcmp(toSearch, "\n")){
strcat(toSearch, "\n");
}
int len, bytes_sent;
len = strlen(toSearch);
bytes_sent = send(mdb_lookup_sock, toSearch, len, 0);
if(bytes_sent != strlen(toSearch)){
die("value returned by send() doesn't match value in len");
}
if (bytes_sent < 0){
die("send() failed");
}
char responseBuf[1024] = { 0 };
char *tableOpenTags = "<table border><tbody>";
len = strlen(tableOpenTags);
bytes_sent = send(client_sock, tableOpenTags, len, 0);
if(bytes_sent != strlen(tableOpenTags)){
die("value returned by send() doesn't match value in len");
}
if (bytes_sent < 0){
die("send() failed");
}
int color = 0;
char *s;
while((s = fgets(responseBuf, sizeof(responseBuf), response)) != NULL){
if(strlen(s) == 1){
break;
}
char tableItem[2048];
if(color == 0){
color = 1;
sprintf(tableItem, "<tr><td>%s</td></tr>", responseBuf);
} else {
color = 0;
sprintf(tableItem, "<tr><td bgcolor=\"yellow\">%s</td></tr>", responseBuf);
}
int len, bytes_sent;
len = strlen(tableItem);
bytes_sent = send(client_sock, tableItem, len, 0);
if(bytes_sent != strlen(tableItem)){
die("value returned by send() doesn't match value in len");
}
if (bytes_sent < 0){
die("send() failed");
}
}
char *tableCloseTags = "</tbody></table>";
len = strlen(tableCloseTags);
bytes_sent = send(client_sock, tableCloseTags, len, 0);
if(bytes_sent != strlen(tableCloseTags)){
die("value returned by send() doesn't match value in len");
}
if (bytes_sent < 0){
die("send() failed");
}
return 0;
}
/*
** Checks whether passed in filepath is a valid directory or not. On success returns positive non-zero, on failure returns -1.
*/
int isDir(char *filepath)
{
struct stat fileStat;
if(stat(filepath, &fileStat) != 0){
return -1;
}
return S_ISDIR(fileStat.st_mode);
}
/*
** Sends styilized response to indicated client socket. On success returns number of bytes sent, on failure returns -1.
*/
int sendResponse(int client_sock, char *responseMessage)
{
char response[1024];
sprintf(response, "HTTP/1.0 %s\r\nContent-Type: text/html\r\n\r\n<html><body><h1> %s </h1></body></html>\r\n\r\n", responseMessage, responseMessage);
int len, bytes_sent;
len = strlen(response);
bytes_sent = send(client_sock, response, len, 0);
if(bytes_sent != strlen(response)){
die("value returned by send() doesn't match value in len");
}
if (bytes_sent < 0){
die("send() failed");
}
return bytes_sent;
}
/*
** Sends status message to indicated client socket. On success returns number of bytes sent, on failure returns -1.
*/
int sendStatus(int client_sock, char *sendMessage)
{
// Send return message back to client
int len, bytes_sent;
len = strlen(sendMessage);
bytes_sent = send(client_sock, sendMessage, len, 0);
return(bytes_sent);
}