-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-server.c
299 lines (270 loc) · 7.5 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/select.h>
//max number of bytes for words
#define SIZE 100
int parse_get_request(int sock_fd, char** body, int num_lines){
//split first line of request
int rc;
char* words[SIZE];
char* tok = strtok(body[0], " ");
int i = 0;
words[i] = tok;
while(tok != NULL) {
i++;
tok = strtok(NULL, " ");
words[i] = tok;
}
//construct the path
char* path = malloc(SIZE);
char abs_path[SIZE];
char* server_root = getcwd(abs_path, SIZE);
strncpy(path, server_root, SIZE);
if(strncmp(words[1], "/", SIZE) == 0) {
strcat(path, "/index.html");
} else {
strcat(path, words[1]);
}
//check if resource exists
char* response = malloc(SIZE);
if(access(path, F_OK) != -1) {
strncpy(response, words[2], strlen(words[2] + 1));
strcat(response, " 200 OK\n\n\0");
//open file to extract lines
FILE* file;
file = fopen(path, "r");
char** text = malloc(SIZE);
if(file) {
char* line = NULL;
size_t linecap = 0;
ssize_t linelen = 0;
while((linelen = getline(&line, &linecap, file)) > 0) {
*text = calloc(SIZE, SIZE);
strncpy(*text, line, SIZE);
strcat(response, *text);
}
} else {
perror("error opening file!");
}
rc = 1;
} else {
strncpy(response, words[2], strlen(words[2] + 1));
strcat(response, " 404 Not Found\n\0");
printf("%s 404 File Not Found\n", words[2]);
rc = 0;
}
write(sock_fd, response, strlen(response) + 1);
return rc;
}
int parse_head_request(int sock_fd, char** body, int num_lines) {
//split the words of the first line of the request
char* words[SIZE];
char* tok = strtok(body[0], " ");
int i = 0;
int rc = 0;
words[i] = tok;
while(tok != NULL) {
i++;
tok = strtok(NULL, " ");
words[i] = tok;
}
//construct path
char* path = malloc(SIZE);
char abs_path[SIZE];
char* server_root = getcwd(abs_path, SIZE);
strncpy(path, server_root, SIZE);
if(strncmp(words[1], "/", SIZE) == 0) {
strcat(path, "/index.html");
} else {
strcat(path, words[1]);
}
//check if resource exists
char* response = malloc(SIZE * 5);
if(access(path, F_OK) != -1) {
//get current time and date
time_t current_time = time(NULL);
if(current_time == (time_t) - 1) {
fprintf(stderr, "error computing time\n");
return 0;
}
struct tm* local_time = localtime(¤t_time);
if(local_time == NULL) {
fprintf(stderr, "error converting time\n");
return 0;
}
char buff[SIZE];
strftime(buff, SIZE, "Date: %A, %d %B %Y %T %Z\n\0", local_time);
//get file information
struct stat attribs;
int stat_rc = stat(path, &attribs);
if(stat_rc > 0) {
fprintf(stderr, "error getting file attribs\n");
return 0;
}
char date[SIZE];
struct tm* access_time = localtime(&(attribs.st_atime));
strftime(date, SIZE, "Last accessed: %A, %d %B %Y %T %Z\n\0", access_time);
off_t size = attribs.st_size;
char content_length[SIZE] = "Content-Length: \0";
//print status line as 200 OK
strncpy(response, words[2], strlen(words[2] + 1));
strcat(response, " 200 OK\n\0");
strcat(response, buff);
strcat(response, date);
strcat(response, content_length);
char size_str[8];
snprintf(size_str, 8, "%lld\n", size);
strcat(response, size_str);
rc = 1;
} else {
strncpy(response, words[2], strlen(words[2] + 1));
strcat(response, " 404 Not Found\n\0");
printf("%s 404 Not Found\n", words[2]);
rc = 0;
}
write(sock_fd, response, strlen(response));
return rc;
}
int parse_request(char* buff, int sock_fd) {
//TODO: fix the order of the read_from_client, parse_request.. functions
char* buff_copy = calloc(1, SIZE);
strncpy(buff_copy, buff, SIZE);
int num_lines = 0;
char* newline = strtok(buff_copy, "\n");
while(newline != NULL) {
num_lines +=1;
newline = strtok(NULL, "\n");
}
char** lines = calloc(num_lines, sizeof(char *));
int i = 0;
char* substr = strtok(buff, "\n");
lines[i] = substr;
while(substr != NULL){
i++;
substr = strtok(NULL, "\n");
lines[i] = substr;
}
//since strtok modifies its argument, I'm creating a copy of lines to figure
//out the type(GET, POST, HEAD) of request
char** str = calloc(num_lines, sizeof(char *));
for (int x = 0; x < i; x++) {
str[x] = calloc(SIZE, sizeof(char));
strncpy(str[x], lines[x], SIZE * sizeof(char));
}
//determine type of request from the first word of the message
//should probably do it in a more organized fashion
char* tok = strtok(str[0], " ");
if(strcmp(tok, "GET") == 0) {
if(parse_get_request(sock_fd, lines, num_lines)) {
return 1;
} else {
return 0;
}
} else if (strcmp(tok, "HEAD") == 0) {
parse_head_request(sock_fd, lines, num_lines);
} else {
perror("invalid request lol");
return 0;
}
printf("finished parsing\n");
return 1;
}
int open_socket() {
//SOCK_STREAM -> TCP/IP type of communication
int sock_fd = socket(PF_INET, SOCK_STREAM, 0);
if (sock_fd < 0) {
perror("error opening socket");
exit(1);
}
return sock_fd;
}
int bind_socket(int sock_fd, int port) {
struct sockaddr_in sock_addr;
//AF_INET: address family of IPv4
sock_addr.sin_family = AF_INET;
//htons/l convert from host byte order to network byte order
sock_addr.sin_port = htons(port);
sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int bind_rc = bind(sock_fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
if (bind_rc < 0) {
perror("error binding socket");
exit(1);
}
return bind_rc;
}
void listen_socket(int sock_fd, int num_connections) {
if (listen(sock_fd, num_connections) < 0) {
perror("error listening");
exit(1);
}
}
int accept_connection(int sock_fd) {
struct sockaddr_in clien_addr;
//haven't made up my mind over using memset here
//memset(&clien_addr, 0, sizeof(struct sockaddr_in));
int clien_len;
int accepted_socket_fd = accept(sock_fd, (struct sockaddr*)&clien_addr, (socklen_t*)&clien_len);
if (accepted_socket_fd < 0) {
perror("error accepting connection");
exit(1);
}
return accepted_socket_fd;
}
int read_from_client(int socket_fd) {
char buff[512];
int num_bytes = read(socket_fd, buff, 512);
printf("%s\r\n", buff);
if (num_bytes < 0) {
perror("error reading from client");
exit(1);
}
if (num_bytes == 0) {
return -1;
}
parse_request(buff, socket_fd);
return num_bytes;
}
void close_socket(int sock_fd) {
if (close(sock_fd) < 0) {
perror("error closing socket");
exit(1);
}
}
int main(int argc, char* argv[]) {
fd_set active_set, read_set;
int port = atoi(argv[1]);
int sock_fd = open_socket();
bind_socket(sock_fd, port);
listen_socket(sock_fd, 5);
FD_ZERO(&active_set);
FD_SET(sock_fd, &active_set);
while(1) {
read_set = active_set;
if (select(FD_SETSIZE, &read_set, NULL, NULL, NULL) < 0) {
perror("select error");
exit(EXIT_FAILURE);
}
for (int i = 0; i < FD_SETSIZE; i++) {
if (FD_ISSET(i, &read_set)) {
if (i == sock_fd) {
int accepted_fd = accept_connection(sock_fd);
FD_SET(accepted_fd, &active_set);
} else {
if (read_from_client(i) < 0) {
close_socket(i);
FD_CLR(i, &active_set);
}
}
}
}
}
return 0;
}