-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
619 lines (515 loc) · 19.5 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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#include <asm-generic/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
// errors
#define FILE_NOT_FOUND -2
// HTTP Status code
#define HTTP_OK 200
#define HTTP_NOT_FOUND 404
#define HTTP_PARTIAL_CONTENT 206
#define HTTP_INTERNAL_ERROR 500
// todo: take this as an argument
#define PORT 8082
#define BUFFER_SIZE 309715200 // 300 MiB
// NOTE: choose appropriate buffer size, If the amount of data you want to
// receive/send in HTTP request-response cycle is greater than 200 MiB, then
// increase. #define BUFFER_SIZE 104857600 // 100 MiB #define BUFFER_SIZE
// 16106127360 // 15 GiB
// Header constansts
#define HOST_HEADER "HOST"
#define AUTHORIZATION_HEADER "Authorization"
#define CONTENT_TYPE_HEADER "Content-Type"
#define CONTENT_LENGTH_HEADER "Content-Length"
/* Note about HTTTP Request and Response
A sample GET request:
GET /test HTTP/1.1\r\n
Host: localhost:8082\r\n
User-Agent: curl/7.81.0\r\n
Authorization: as \r\n
\r\n
A sample POST request:
POST /path HTTP/1.0\r\n
Content-Type: text/plain\r\n
Content-Length: 12\r\n
\r\n
query_string
A sample response:
HTTP/1.1 200 OK\r\n
Access-Control-Allow-Origin: *\r\n
Server: abc\r\n
Content-Length: 200\r\n
\r\n
body
According to the HTTP RFC, the first line of the request is the request line
and the rest of the lines are headers followed by an optional body. Each of
the lines are separated by \r\n. The headers are separated from the body by
the \r\n.
If the payload is present, the Content-Length header is used to determine the
size.
*/
// Ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests
struct HTTPRangeRequest {
long long start;
long long end;
char range_type[30];
};
struct HTTPRangeResponse {
long long start;
long long end;
long long total_length;
};
struct HTTPRequest {
// Method details
char method[30]; // "GET" or "POST"
char uri[200]; // "/index.html" things before '?' (used for GET request)
char protocol[30]; // "HTTP/1.1"
// Headers
char host_hdr[30]; // "localhost:8082"
char authorization_hdr[30]; // <secret>
char content_type_hdr[30]; // "application/json"
long long content_length_hdr; // 38 (used for POST request)
char range_hdr_raw[30]; // "0-100" (used for range request)
struct HTTPRangeRequest range_hdr; // 0-100 (used for range request)
// Payload
char payload[BUFFER_SIZE]; // { "title":"foo","body":"bar", "id": 1}
};
struct HTTPResponse {
char protocol[30]; // "HTTP/1.1"
int status_code; // 200
char status_message[30]; // "OK"
// Headers
char content_type_hdr[30]; // "application/json"
char accept_ranges_hdr[30]; // "bytes"
long long content_length_hdr; // 38 (sent back in GET response)
struct HTTPRangeResponse range_hdr; // 0-100 (used for range request)
// Payload
char payload[BUFFER_SIZE]; // { "title":"foo","body":"bar", "id": 1}
};
struct HTTPResponseMetadata {
char *response_string;
long response_length;
};
/* ---------------- util function ------------------------------*/
void printHTTPRequest(struct HTTPRequest *request) {
printf("-------------- HTTP Request -----------\n");
printf("Method: %s\n", request->method);
printf("URI: %s\n", request->uri);
printf("Protocol: %s\n", request->protocol);
printf("Host Header: %s\n", request->host_hdr);
printf("Authorization Header: %s\n", request->authorization_hdr);
printf("Content-Type Header: %s\n", request->content_type_hdr);
printf("Content-Length Header: %lld\n", request->content_length_hdr);
printf("Range: %lld - %lld\n", request->range_hdr.start,
request->range_hdr.end);
printf("Payload: %s\n", request->payload);
}
void printHTTPResponse(struct HTTPResponse *http_response) {
printf("-------------- HTTP Response -----------\n");
printf("%s %d %s\n", http_response->protocol, http_response->status_code,
http_response->status_message);
printf("Content-Length: %lld\n", http_response->content_length_hdr);
printf("Accept-Ranges: %s\n", http_response->accept_ranges_hdr);
printf("Content-Type: %s\n", http_response->content_type_hdr);
printf("Content-Range: %lld-%lld/%lld\n", http_response->range_hdr.start,
http_response->range_hdr.end, http_response->range_hdr.total_length);
printf("Payload: %s\n", http_response->payload);
}
long long get_file_length(char *file_name) {
FILE *fp = fopen(file_name, "r");
if (fp == NULL) {
return FILE_NOT_FOUND;
}
int file_descriptor = fileno(fp);
if (file_descriptor < 0) {
perror("error getting file descriptor");
exit(EXIT_FAILURE);
}
// Get the file attributes
// This allows us to get the size of file without opening it
struct stat file_stat;
fstat(file_descriptor, &file_stat);
fclose(fp);
return file_stat.st_size;
}
size_t get_file_content(char *file_name, unsigned char *buffer,
long long file_size) {
FILE *fp = fopen(file_name, "rb");
if (fp == NULL) {
return FILE_NOT_FOUND;
}
size_t bytes_read = fread(buffer, 1, file_size, fp);
// The total bytes read should be same as the file size
if (bytes_read != file_size) {
perror("error reading file");
exit(EXIT_FAILURE);
}
fclose(fp);
return bytes_read;
}
size_t get_file_content_range(char *file_name, unsigned char *buffer,
long long start, long long range_length) {
FILE *fp = fopen(file_name, "rb");
if (fp == NULL) {
return FILE_NOT_FOUND;
}
fseek(fp, start, SEEK_SET);
size_t bytes_read = fread(buffer, 1, range_length, fp);
// The total bytes read should be same as the file size
fclose(fp);
return bytes_read;
}
void generate_http_response(int code, struct HTTPResponse *http_response) {
switch (code) {
case HTTP_OK:
http_response->status_code = 200;
strcpy(http_response->status_message, "OK");
strcpy(http_response->content_type_hdr, "application/octet-stream");
break;
case HTTP_PARTIAL_CONTENT:
http_response->status_code = 206;
strcpy(http_response->status_message, "Partial Content");
strcpy(http_response->content_type_hdr, "application/octet-stream");
break;
case HTTP_NOT_FOUND:
http_response->status_code = 404;
strcpy(http_response->status_message, "Not Found");
break;
default:
http_response->status_code = 500;
strcpy(http_response->status_message, "Internal Server Error");
break;
}
}
/* ---------------- util function ------------------------------*/
long long construct_http_response_string(struct HTTPResponse *http_response,
struct HTTPRequest *http_request,
char *response_string) {
if (response_string == NULL) {
perror("Failed to allocate memory");
exit(EXIT_FAILURE);
}
// Initialize the response string
response_string[0] = '\0';
// Add the status line
sprintf(response_string, "%s %d %s\r\n", http_response->protocol,
http_response->status_code, http_response->status_message);
// Add headers if they are not empty
char content_length[64];
sprintf(content_length, "Content-Length: %lld\r\n",
http_response->content_length_hdr);
strcat(response_string, content_length);
if (strlen(http_response->accept_ranges_hdr) > 0) {
strcat(response_string, "Accept-Ranges: ");
strcat(response_string, http_response->accept_ranges_hdr);
strcat(response_string, "\r\n");
}
if (strlen(http_response->content_type_hdr) > 0) {
strcat(response_string, "Content-Type: ");
strcat(response_string, http_response->content_type_hdr);
strcat(response_string, "\r\n");
}
if (http_response->status_code == HTTP_PARTIAL_CONTENT) {
char content_range[64];
sprintf(content_range, "Content-Range: bytes %lld-%lld/%lld\r\n",
http_response->range_hdr.start, http_response->range_hdr.end,
http_response->range_hdr.total_length);
strcat(response_string, content_range);
}
// End headers section
strcat(response_string, "\r\n");
long long response_length = strlen(response_string);
// Add the payload only when the method is GET
if (strcasecmp(http_request->method, "GET") == 0)
for (long long i = 0; i < http_response->content_length_hdr; i++) {
response_string[response_length] =
(unsigned char)http_response->payload[i];
response_length++;
}
return response_length;
}
struct HTTPRangeRequest *parse_http_range_request_hdr(char *range_hdr) {
struct HTTPRangeRequest *range_request =
malloc(sizeof(struct HTTPRangeRequest));
char *range = malloc(strlen(range_hdr) * sizeof(char));
strcpy(range, range_hdr);
char *range_type = strtok(range, "=");
char *range_start = strtok(NULL, "-");
char *range_end = strtok(NULL, "");
range_request->start = atoll(range_start);
range_request->end = atoll(range_end);
strcpy(range_request->range_type, range_type);
return range_request;
}
// Parse the raw HTTP request and populate the HTTPRequest struct
void parse_http_request(char *buffer, struct HTTPRequest *http_request) {
char *method, *uri, *prot, *payload;
int payload_size;
// Parse the request line
// GET /test HTTP/1.1\r\n
method = strtok(buffer, " ");
uri = strtok(NULL, " ");
// uri = uri + 1; // remove the leading '/'
prot = strtok(NULL, " \r\n");
strcpy(http_request->method, method);
strcpy(http_request->uri, uri);
strcpy(http_request->protocol, prot);
bool headers_done = false;
char *payload_body;
// Parse headers
// Note: Currently we only support the headers mentioned in the Header
// constants
while (!headers_done) {
char *key, *value, *next;
// Extract the key and value from the header
key = strtok(NULL, "\r\n: \t");
value = strtok(NULL, "\r\n");
while (*value && *value == ' ')
value++;
if (strcasecmp(key, HOST_HEADER) == 0) {
strcpy(http_request->host_hdr, value);
} else if (strcasecmp(key, AUTHORIZATION_HEADER) == 0) {
strcpy(http_request->authorization_hdr, value);
} else if (strcasecmp(key, CONTENT_TYPE_HEADER) == 0) {
strcpy(http_request->content_type_hdr, value);
} else if (strcasecmp(key, CONTENT_LENGTH_HEADER) == 0) {
http_request->content_length_hdr = atoi(value);
} else if (strcasecmp(key, "Range") == 0) {
strcpy(http_request->range_hdr_raw, value);
}
// we want to keep checking if the next line after header is \r\n which
// indicates the end of headers and start of payload body
next = value + strlen(value) + 2;
if (next[0] == '\r' && next[1] == '\n') {
payload_body = next;
headers_done = true;
}
}
// The pointer is now at the start of the payload body
payload_body = strtok(NULL, "\0");
// The headers and payload body is seperated by "\r\n\r\n", hence move the
// pointer that much forward
payload_body = payload_body + 3;
strcpy(http_request->payload, payload_body);
// TODO: Maybe strtok_r can be used to make this function reentrant?
// Since strtok is not a reentrant function, we weren't able to parse the
// range header in the same loop. Hence, we parse it here
if (strlen(http_request->range_hdr_raw) > 0) {
http_request->range_hdr =
*parse_http_range_request_hdr(http_request->range_hdr_raw);
printf("Range: %lld - %lld\n", http_request->range_hdr.start,
http_request->range_hdr.end);
}
}
int process_head_http_request(struct HTTPRequest *http_request,
struct HTTPResponse *http_response) {
// Read the file length. Store it in content length. If file not found return
// 404
long file_length = get_file_length(http_request->uri);
strcpy(http_response->protocol, http_request->protocol);
if (file_length == FILE_NOT_FOUND) {
return HTTP_NOT_FOUND;
}
http_response->content_length_hdr = file_length;
strcpy(http_response->accept_ranges_hdr, "bytes");
strcpy(http_response->content_type_hdr, "application/octet-stream");
return HTTP_OK;
}
int proccess_get_http_request(struct HTTPRequest *http_request,
struct HTTPResponse *http_response) {
strcpy(http_response->protocol, http_request->protocol);
// Read the file length. Store it in content length. If file not found return
// 404
long file_length = get_file_length(http_request->uri);
if (file_length == FILE_NOT_FOUND) {
return HTTP_NOT_FOUND;
}
http_response->content_length_hdr = file_length;
// Read the file content and store it in the payload
unsigned char *file_content = malloc(file_length * sizeof(char));
size_t bytes_read =
get_file_content(http_request->uri, file_content, file_length);
// Add a null terminator to the end of the file content
file_content[bytes_read] = '\0';
// The contents of the file should be treated as binary and as such should be
// copied as is. Hence, we use memcpy instead of strcpy
memcpy(http_response->payload, file_content, bytes_read);
free(file_content);
return HTTP_OK;
}
int process_get_http_range_request(struct HTTPRequest *http_request,
struct HTTPResponse *http_response) {
strcpy(http_response->protocol, http_request->protocol);
// Validate the range requests
if (http_request->range_hdr.start < 0 || http_request->range_hdr.end < 0) {
return HTTP_INTERNAL_ERROR;
}
if (http_request->range_hdr.start > http_request->range_hdr.end) {
return HTTP_INTERNAL_ERROR;
}
long file_length = get_file_length(http_request->uri);
if (file_length == FILE_NOT_FOUND) {
return HTTP_NOT_FOUND;
}
http_response->range_hdr.total_length = file_length;
long request_bytes_length =
http_request->range_hdr.end - http_request->range_hdr.start + 1;
// TODO: What do we do, if the range request is beyond the BUFFER_LENGTH
// allowed?
unsigned char *partial_file_content =
malloc(request_bytes_length * sizeof(char));
size_t bytes_read = get_file_content_range(
http_request->uri, partial_file_content, http_request->range_hdr.start,
request_bytes_length);
// Add a null terminator to the end of the file content
partial_file_content[bytes_read] = '\0';
// The contents of the file should be treated as binary and as such should be
// copied as is. Hence, we use memcpy instead of strcpy
memcpy(http_response->payload, partial_file_content, bytes_read);
// Set the "Content-Range" and "Content-Length" header
http_response->range_hdr.start = http_request->range_hdr.start;
http_response->range_hdr.end = http_request->range_hdr.end;
http_response->content_length_hdr = bytes_read;
free(partial_file_content);
return HTTP_PARTIAL_CONTENT;
}
void process_http_request(struct HTTPRequest *http_request,
struct HTTPResponse *http_response) {
int response_code;
// 1. Check the http method and process accordingly
// 2. If GET, then fetch the file and send it back
if (strcasecmp(http_request->method, "HEAD") == 0) {
response_code = process_head_http_request(http_request, http_response);
} else if (strcasecmp(http_request->method, "GET") == 0) {
if (strlen(http_request->range_hdr_raw) > 0) {
response_code =
process_get_http_range_request(http_request, http_response);
} else {
response_code = proccess_get_http_request(http_request, http_response);
}
} else {
response_code = HTTP_NOT_FOUND;
}
generate_http_response(response_code, http_response);
return;
}
void send_http_request(int client_fd, struct HTTPResponse *http_response,
struct HTTPRequest *http_request) {
// Allocate a buffer for the response string
char *response_string = malloc(BUFFER_SIZE); // Adjust size as needed
long total_response_bytes = construct_http_response_string(
http_response, http_request, response_string);
long bytes_sent = send(client_fd, response_string, total_response_bytes, 0);
if (bytes_sent < 0) {
perror("error sending data to client");
exit(EXIT_FAILURE);
}
free(response_string);
}
// Handle the client request
void *handle_client_request(void *arg) {
int client_fd = *((int *)arg);
char *buffer = (char *)malloc(BUFFER_SIZE * sizeof(char));
int bytes_recvd;
char *method, *uri, *prot, *payload;
int payload_size;
struct HTTPRequest *http_request = malloc(sizeof(struct HTTPRequest));
struct HTTPResponse *http_response = malloc(sizeof(struct HTTPResponse));
// Get the data from client and store it in buffer
// FIXME: Current assumption is that, we get the entire request in a single
// packet. But this is not true. We need to keep reading from the socket until
// we get the entire request or the `/r/n/r/n` which indicates the end of the
// request.
// REF:
// https://stackoverflow.com/questions/49821687/how-to-determine-if-i-received-entire-message-from-recv-calls
bytes_recvd = recv(client_fd, buffer, BUFFER_SIZE, 0);
// Validate the received bytes
if (bytes_recvd < 0) {
perror("error reading data from socket");
exit(EXIT_FAILURE);
}
if (bytes_recvd == 0) {
perror("client disconnected upexpectedly");
exit(EXIT_FAILURE);
}
// Add a null terminator to know that we have reached the end of the HTTP
// request This is useful for getting the payload body below.
buffer[bytes_recvd] = '\0';
parse_http_request(buffer, http_request);
printHTTPRequest(http_request);
process_http_request(http_request, http_response);
printHTTPResponse(http_response);
send_http_request(client_fd, http_response, http_request);
printf("\nResponse sent\n");
printf("-------------------------------\n\n");
close(client_fd);
free(arg);
free(buffer);
free(http_request);
free(http_response);
return NULL;
}
int main(int argc, char *argv[]) {
int server_fd, client_fd;
struct sockaddr_in server_addr;
// 1. Create server socket
server_fd = socket(AF_INET, SOCK_STREAM, 0);
int option = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
if (server_fd < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 2. Configure the socket
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY; // the host address
server_addr.sin_port = htons(PORT); // convert to network byte order
// 3. bind socket to port
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) <
0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// 4. listen for connections
if (listen(server_fd, 5) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
printf("Server is listening on port %d\n", PORT);
// infinite loop to listen on connection
while (1) {
// client info
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
// accept client connection
client_fd =
accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_len);
int *client_fd_ptr = malloc(sizeof(int));
*client_fd_ptr = client_fd;
if (client_fd < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// create a new thread to handle client request
// TODO: Use thread pool, to avoid creating new thread for each request
pthread_t thread_id;
if (pthread_create(&thread_id, NULL, handle_client_request,
client_fd_ptr) != 0) {
perror("error creating thread");
free(client_fd_ptr);
close(client_fd);
}
pthread_detach(thread_id);
}
close(client_fd);
close(server_fd);
return 0;
}