-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
436 lines (364 loc) · 12.8 KB
/
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
#include "server.h"
#include <pthread.h>
const char *FMT = "HTTP/1.1 200 OK\r\n"
"Content-length: %ld\r\n"
"Content-Type: %s\r\n"
"\r\n"
"%s";
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET)
{
return &(((struct sockaddr_in *)sa)->sin_addr);
}
return &(((struct sockaddr_in6 *)sa)->sin6_addr);
}
//easy way to handle fails.
void check(int value, char *err_str)
{
if (value < 0)
{
perror(err_str);
exit(EXIT_FAILURE);
}
}
/// Deprecated
char *humanize(Route *route, pid_t *pid)
{
char *req_type = translate_reqtype(route->type);
int len_str = snprintf(NULL, 0, "<html>\n<head>\n<title>Hello Process %d !</title>\n</head>\n<body>\n<h1>%s Request</h1>\n<p> Host: %s \n<p> User-Agent: %s \n<p> Looking for: <b>%s</b> \n<p>Answered by PID: %d \n</body>\n</html>\n",
*pid, req_type, route->host, route->user_string, route->route, *pid); // write a variadic function for this.
char *buf = malloc(len_str + 1); //TODO Free
snprintf(buf, len_str + 1, "<html>\n<head>\n<title>Hello Process %d !</title>\n</head>\n<body>\n<h1>%s Request</h1>\n<p> Host: %s \n<p> User-Agent: %s \n<p> Looking for: <b>%s</b> \n<p>Answered by PID: %d \n</body>\n</html>\n",
*pid, req_type, route->host, route->user_string, route->route, *pid);
int output = snprintf(NULL, 0, FMT, strlen(buf), "text/html", buf);
char *out = malloc(output + 1);
snprintf(out, output + 1, FMT, strlen(buf), "text/html", buf);
free(buf);
return out;
}
void handle_new_conn(int *p_accepted_socket, void *additional_args)
{
int accepted_socket = *p_accepted_socket;
free(p_accepted_socket);
char *from = (char *)additional_args;
printf(">> Incoming message from %s : \n", from);
//pid_t child_pid = getpid(); // underlying size is an int, so #10 chars max.
char message[BUFF_SIZE] = {0};
long value = read(accepted_socket, message, BUFF_SIZE); //don't use pread socket is not "seekable"
if (value < 0)
{
close(accepted_socket);
perror("Error reading incoming msg");
exit(EXIT_FAILURE);
}
Route *route = parse_http(message);
FileOut *file_out = calloc(1, sizeof(FileOut));
int success = read_file(route, file_out);
switch (success)
{
case -1: //error locating file
break;
case -2: //file extension not supported
break;
case 1: //works!
write_file(file_out, accepted_socket);
free_fout(file_out);
break;
}
// char *output = humanize(route, &child_pid);
// int write_result = write(accepted_socket, output, strlen(output));
// if (write_result < 0)
// {
// close(accepted_socket);
// perror("Error writing outgoing msg");
// exit(EXIT_FAILURE);
// }
close(accepted_socket);
//free(output);
free(route);
}
Route *parse_route(char *token, Route *req)
{
char *request;
int i = 0;
while ((request = strsep(&token, " ")) != NULL)
{
if (i == 0)
{
// req type
if (strcmp(request, "GET") == 0)
{
req->type = GET;
}
else if (strcmp(request, "POST") == 0)
req->type = POST;
else if (strcmp(request, "PUT") == 0)
req->type = PUT;
}
else if (i == 1)
{
req->route = request;
}
i++;
}
return req;
}
char *translate_reqtype(ReqType type)
{
switch (type)
{
case GET:
return "GET";
case POST:
return "POST";
case PUT:
return "PUT";
}
return "OTHER";
}
void pretty_print_route(Route *route)
{
char *type = translate_reqtype(route->type);
printf("Route :\n\t%s\n\t%s\n\t%s\n", type, route->route, route->host);
return;
}
Route *parse_http(char *input)
{
char *cp = strdup(input);
char *token;
//first line is request type, resource uri and http protocol
Route *route = malloc(sizeof(Route));
int i = 0;
while ((token = strtok_r(cp, "\n", &cp)))
{
if (i == 0)
{
route = parse_route(token, route);
}
else if (i == 1)
{
char *space = strchr(token, 32); //32 is space character
if (space != NULL)
route->host = space + 1; // add one to get the next char over don't want to get the extra space
}
else if (i == 2)
route->user_string = strchr(token, 32) + 1;
i += 1;
}
return route;
}
int main()
{
struct addrinfo hints, *res;
struct sockaddr_storage incoming_addr; // this is to make it ipv4 or ipv6 agnostic
int server_file_d, accepted;
///instantiate epoll instance
struct epoll_event ev, events[BACKLOG];
int epollfd, ndfs;
epollfd = epoll_create1(0); // TODO: close
check(epollfd, "Could not create epoll file descriptor");
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; //only ipv4
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
check(getaddrinfo(NULL, "8080", &hints, &res), "Error with instantiating addrinfo struct");
// Create Socket, bind to it
// If Protocol is 0 it's automatically selected
check(server_file_d = socket(res->ai_family, res->ai_socktype, res->ai_protocol), "Cannot create the socket"); //change to PF_INET for the sake of "correctedness"
//setsockopt to be able to reuse to an unclosed socket...usually happens if you close and re-start the server
int yes = 1;
check(setsockopt(server_file_d, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)), "Could not reuse the unclosed socket.");
/*
binding to socket requires a socketaddr struct,
struct is a generic, the actual definition is determined byt the type of network
IP networking:
https://pubs.opengroup.org/onlinepubs/009695399/basedefs/netinet/in.h.html
struct sockaddr_in
{
__uint8_t sin_len; //note this is specific to linux...not portable
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
char sin_zero[8]; //technically removed...see link
};
NOTE: you do not have to pack it by hand, use the helper func "getaddrinfo" and use the result ptr.
*/
check(bind(server_file_d, res->ai_addr, res->ai_addrlen), "Failed to bind socket to address/port");
setnonblocking(server_file_d); //set listening socket to nonblocking
//Now listen to the bound port
check(listen(server_file_d, BACKLOG), "Failed to listen");
//listen doesn't mean accept
ev.events = EPOLLIN;
ev.data.fd = server_file_d;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, server_file_d, &ev) == -1)
{
perror("Error with epoll CTL call on server_file_d");
close(server_file_d);
free(res);
exit(EXIT_FAILURE);
//exit everything
}
socklen_t addrlen = sizeof(incoming_addr);
while (1)
{
//todo replace code w/ ASYNC version
printf("\n~~~~~~~~~Waiting to accept a new conn~~~~~~~~\n\n\n");
ndfs = epoll_wait(epollfd, events, BACKLOG, -1); // checks to see if any FD are ready to talk
if (ndfs == -1)
{
perror("Epoll wait error");
free(res);
close(server_file_d);
exit(EXIT_FAILURE);
}
for (int n = 0; n < ndfs; ++n) // iterate over the ones that are ready to talk, at this point you don't know if it is inbound or outbound
{
if (events[n].data.fd == server_file_d) //if the fd is the listening socket, then it is inbound
{
//if any events are ready to talk, then check if they're the server_file_d
for (;;)
{
accepted = accept(server_file_d, get_in_addr((struct sockaddr *)&incoming_addr), &addrlen);
// if -1, and the errno var is set, we are done processing everything
if ((accepted == -1) && (errno == EAGAIN || errno == EWOULDBLOCK))
{
break;
}
else
{ //otherwise accept the connection, set it to non-blocking(default is blocking) then set the fd and events to check for in the ev struct(resusing it over and over again), then add it into the epollfd via the ctl call.
printf("Accepted connection on %d \n", accepted);
setnonblocking(accepted);
ev.data.fd = accepted;
ev.events = EPOLLIN;
check(epoll_ctl(epollfd, EPOLL_CTL_ADD, accepted, &ev), "COULD NOT ADD TO EPOLL"); // look into alternate closing?
}
}
}
else
{
//check to see what client wants.
//this means it's a client socket ready to communicate
int sock = events[n].data.fd;
char buffer[BUFF_SIZE];
memset(&buffer, '\0', BUFF_SIZE * sizeof(char));
check(read(sock, &buffer, sizeof(buffer)), "Recv call done with -1");
printf("%s\n", buffer);
memset(&buffer, '\0', BUFF_SIZE * sizeof(char));
FILE *fd = fdopen(sock, "r+");
fprintf(fd, "hey there stranger"); //todo inject hand conn function in here.
fflush(fd);
fclose(fd);
close(sock);
}
}
}
close(server_file_d);
return 0;
}
void *handle_conn_wrapper(void *arg)
{
thread_args *args = (thread_args *)arg;
handle_new_conn(args->socket, args->host_name);
free(args);
pthread_exit(NULL);
}
/// read file function takes p_Route and p_FileOut
///
/// return -1 if file not found
/// return -2 if file not supported
/// return 1 if found and supported
int read_file(Route *route, FileOut *file_out)
{
//route requested resource = file name inside of files dir
struct stat sbuf;
char files[] = "./files";
char *fp = realpath(files, NULL);
char *c_fp = malloc(strlen(fp) + strlen(route->route) + 1); //TODO FREE
sprintf(c_fp, "%s%s", fp, route->route);
printf("\n\nFile path %s\n", c_fp);
if (stat(c_fp, &sbuf) < 0)
{
perror("Error locating file");
free(c_fp);
return -1;
}
unsigned char *buffer = calloc(sbuf.st_size, sizeof(unsigned char)); //TODO Free
FILE *f_to_read = fopen(c_fp, "rb");
fread(buffer, sizeof(unsigned char), sbuf.st_size, f_to_read);
fflush(stdout);
printf("\nSize of file : %ld\n", sbuf.st_size);
printf("\n%s\n", buffer);
file_out->buffer = buffer;
file_out->fp = sbuf;
char *ftype = parse_file_type(c_fp);
if (ftype == NULL)
{
perror("Error file not supported");
free(c_fp);
free(buffer);
fclose(f_to_read);
return -2;
}
file_out->filetype = ftype;
fflush(stdout);
free(c_fp);
fclose(f_to_read);
return 1;
}
char *parse_file_type(char *input)
{
if (strstr(input, ".html") != NULL)
{
char *out = calloc(sizeof(char), strlen(HTML_FILE));
strcpy(out, HTML_FILE);
return out;
}
if (strstr(input, ".pdf") != NULL)
{
char *out = calloc(sizeof(char), strlen(PDF_FILE));
strcpy(out, PDF_FILE);
return out;
}
if (strstr(input, ".jpg") != NULL)
{
char *out = calloc(sizeof(char), strlen(IMG_JPEG_FILE));
strcpy(out, IMG_JPEG_FILE);
return out;
}
if (strstr(input, ".jpeg") != NULL)
{
char *out = calloc(sizeof(char), strlen(IMG_JPEG_FILE));
strcpy(out, IMG_JPEG_FILE);
return out;
}
return NULL;
}
void free_fout(FileOut *out)
{
free(out->filetype);
free(out->buffer);
free(out);
}
void write_file(FileOut *out, int accepted_socket)
{
//saw an implementation w/ mmap being used...thinking of using that
FILE *sockfd = fdopen(accepted_socket, "r+");
fprintf(sockfd, "HTTP/1.1 200 OK\n");
fprintf(sockfd, "Server: My Simple TCP Server\n");
fprintf(sockfd, "Content-length: %d\n", (int)out->fp.st_size);
fprintf(sockfd, "Content-type: %s\n", out->filetype);
fprintf(sockfd, "\r\n");
fwrite(out->buffer, sizeof(unsigned char), out->fp.st_size, sockfd); //should check to see if all of the content is written, there is no guarantee that fwrite will write ALL of the data out.
fflush(sockfd);
fclose(sockfd);
}
void setnonblocking(int fd)
{
// get flags for current fd
int flags = fcntl(fd, F_GETFL);
check(flags, "Error getting flags for current fd");
//set flag to non-blocking by |=
int result = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
check(result, "Error setting the flag to non-blocking");
}