-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
308 lines (256 loc) · 6.79 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
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define BASE_DIR "public"
#define MAXLINE 2000
#define LISTENQ 8 // Max number of client connections
typedef struct
{
char *code;
char *content_type;
char *file;
} http_response;
int open_listenfd(char *port);
void do_work(int connfd);
char *get_request(int fd);
char *parse_uri(const char *request);
http_response create_response(char *uri);
char *get_full_path(const char *path);
int main(int argc, char **argv)
{
int listenfd, connfd;
struct sockaddr_storage clientaddr;
socklen_t clientlen;
if (argc != 2)
{
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(1);
}
listenfd = open_listenfd(argv[1]);
while (1)
{
char hostname[MAXLINE];
char port[MAXLINE];
pid_t pid;
clientlen = sizeof(clientaddr);
connfd = accept(listenfd, (struct sockaddr *)&clientaddr, &clientlen);
getnameinfo((struct sockaddr *)&clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, 0);
printf("Accepted connection from (%s %s)\n", hostname, port);
pid = fork();
if (pid == -1)
{
printf("forking failed\n");
close(connfd);
return -1;
}
// Child process;
if (pid == 0)
{
do_work(connfd);
close(connfd);
break;
}
else
close(connfd);
}
close(listenfd);
return 0;
}
int open_listenfd(char *port)
{
int listenfd, optval = 1;
struct addrinfo hints, *listp, *p;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_socktype = SOCK_STREAM; /* Open a connection */
hints.ai_flags |= AI_PASSIVE | AI_ADDRCONFIG; /* On any IP address */
getaddrinfo(NULL, port, &hints, &listp);
for (p = listp; p; p = p->ai_next)
{
/* Create a socket descriptor */
if ((listenfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0)
continue; /* Socket failed, try the next one */
/* Eliminates "Address already in use" error from bind */
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof(int));
/* Bind to the server */
if (bind(listenfd, p->ai_addr, p->ai_addrlen) == 0)
break; /* Success */
close(listenfd);
}
/* Cleanup */
freeaddrinfo(listp);
if (!p) /* No address worked */
return -1;
/* Make it a listening socket ready to accept connection requests */
if (listen(listenfd, LISTENQ) < 0)
{
close(listenfd);
return -1;
}
{
char buf[MAXLINE];
getnameinfo(p->ai_addr, p->ai_addrlen, buf, MAXLINE, NULL, 0, 0);
printf("Listening at %s\n", buf);
}
return listenfd;
}
void do_work(int connfd)
{
int n = 0, file_fd;
char *request, *uri, *path;
char buffer[MAXLINE];
http_response response;
request = get_request(connfd);
uri = parse_uri(request);
printf("URI %s\n", uri);
free(request);
response = create_response(uri);
path = get_full_path(response.file);
printf("File: %s\n", path);
printf("Header: %s\n", response.code);
send(connfd, response.code, strlen(response.code), 0);
send(connfd, response.content_type, strlen(response.content_type), 0);
send(connfd, "\r\n", strlen("\r\n"), 0);
file_fd = open(path, 0, 0);
while (1)
{
n = read(file_fd, buffer, MAXLINE);
if (n == 0)
break;
send(connfd, buffer, n, 0);
}
free(uri);
free(path);
}
char *get_request(int fd)
{
FILE *sstream;
size_t size = 1, line_size = 1, line_usable_size = 1;
char *request = malloc(sizeof(char) * size);
char *line = malloc(sizeof(char) * size);
if ((sstream = fdopen(fd, "r")) == NULL)
{
fprintf(stderr, "Error allocating memory while getting request\n");
exit(1);
}
*request = '\0';
while ((line_size = getline(&line, &line_usable_size, sstream)) > 0)
{
// If the line read is blank and has only carraige return
if (strcmp(line, "\r\n") == 0)
break;
request = realloc(request, size + line_size);
size += line_size;
strcat(request, line);
}
free(line);
return request;
}
#define VERB_GET "GET "
#define VERB_GET_LEN strlen(VERB_GET)
#define HTTP_PARAM "?"
#define HTTP_PROTOCOL " HTTP/1"
char *parse_uri(const char *request)
{
char *request_uri;
char *pos_get = strstr(request, VERB_GET);
char *pos_param = strstr(pos_get, HTTP_PARAM);
char *pos_http = strstr(pos_get, HTTP_PROTOCOL);
char *pos_start, *pos_end;
size_t len;
if (!pos_get || !pos_http)
{
fprintf(stderr, "Type of request not supported\n");
exit(1);
}
pos_start = pos_get + VERB_GET_LEN;
pos_end = pos_param && (pos_param > pos_start && pos_param < pos_http) ? pos_param : pos_http;
len = pos_end - pos_start;
request_uri = malloc(len + 1);
for (int i = 0; i < len; ++i)
{
request_uri[i] = pos_start[i];
}
request_uri[len] = '\0';
return request_uri;
}
#define BASE_DIR_LEN strlen(BASE_DIR)
char *get_full_path(const char *path)
{
size_t size = (BASE_DIR_LEN + strlen(path) + 1) * sizeof(char);
char *full_path = malloc(size);
strcpy(full_path, BASE_DIR);
strcat(full_path, path);
return full_path;
}
const char *get_extension(const char *path)
{
const char *dot = strrchr(path, '.');
if (!dot)
return NULL;
return dot + 1;
}
#define HTTP_200 "HTTP/1.1 200 OK\r\n"
#define HTTP_400 "HTTP/1.1 400 Bad Request\r\n"
#define HTTP_404 "HTTP/1.1 404 Not Found\r\n"
#define CONTENT_TYPE(type) "Content-Type:" type "charset=UTF-8\r\n"
#define BAD_STRING ".."
http_response create_response(char *uri)
{
http_response response;
char *path;
if (strcmp(uri, "/") == 0)
path = "/index.html";
else
path = uri;
if (strcmp(uri, BAD_STRING) == 0)
{
response.code = HTTP_400;
response.content_type = CONTENT_TYPE("text/html;");
response.file = "/400.html";
}
else
{
char *full_path = get_full_path(path);
FILE *exists = fopen(full_path, "r");
if (exists)
{
const char *ext = get_extension(path);
response.code = HTTP_200;
response.file = path;
if (strcmp(ext, "html") == 0)
response.content_type = CONTENT_TYPE("text/html;");
else if (strcmp(ext, "js") == 0)
response.content_type = CONTENT_TYPE("text/javascript;");
else if (strcmp(ext, "css") == 0)
response.content_type = CONTENT_TYPE("text/css;");
else if (strcmp(ext, "ico") == 0)
response.content_type = CONTENT_TYPE("image/icon;");
else if (strcmp(ext, "jpg") == 0)
response.content_type = CONTENT_TYPE("image/jpg;");
else if (strcmp(ext, "jpeg") == 0)
response.content_type = CONTENT_TYPE("image/jpeg;");
else if (strcmp(ext, "png") == 0)
response.content_type = CONTENT_TYPE("image/png;");
else if (strcmp(ext, "gif") == 0)
response.content_type = CONTENT_TYPE("image/gif;");
else if (strcmp(ext, "pdf") == 0)
response.content_type = CONTENT_TYPE("application/pdf;");
else
response.content_type = CONTENT_TYPE("application/octet-stream;");
fclose(exists);
}
else
{
response.code = HTTP_404;
response.content_type = CONTENT_TYPE("text/html;");
response.file = "/404.html";
}
free(full_path);
}
return response;
}