-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_server3.cc
446 lines (408 loc) · 9.39 KB
/
http_server3.cc
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
#include "minet_socket.h"
#include <stdlib.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/stat.h>
#define FILENAMESIZE 100
#define BUFSIZE 1024
typedef enum \
{NEW,READING_HEADERS,WRITING_RESPONSE,READING_FILE,WRITING_FILE,CLOSED} states;
typedef struct connection_s connection;
typedef struct connection_list_s connection_list;
struct connection_s
{
int sock;
int fd;
char filename[FILENAMESIZE+1];
char buf[BUFSIZE+1];
char *endheaders;
bool ok;
long filelen;
states state;
int headers_read,response_written,file_read,file_written;
connection *next;
};
struct connection_list_s
{
connection *first,*last;
};
void add_connection(int,connection_list *);
void insert_connection(int,connection_list *);
void init_connection(connection *con);
int writenbytes(int,char *,int);
int readnbytes(int,char *,int);
void read_headers(connection *);
void write_response(connection *);
void read_file(connection *);
void write_file(connection *);
int main(int argc,char *argv[])
{
int server_port;
int sock,sock2;
struct sockaddr_in sa,sa2;
int rc;
fd_set readlist,writelist;
connection_list connections;
connection *i;
int maxfd;
/* parse command line args */
if (argc != 3)
{
fprintf(stderr, "usage: http_server3 k|u port\n");
exit(-1);
}
server_port = atoi(argv[2]);
if (server_port < 1500)
{
fprintf(stderr,"INVALID PORT NUMBER: %d; can't be < 1500\n",server_port);
exit(-1);
}
/* initialize and make socket */
if (toupper(*(argv[1]))=='K') {
minet_init(MINET_KERNEL);
} else if (toupper(*(argv[1]))=='U') {
minet_init(MINET_USER);
} else {
fprintf(stderr, "First argument must be k or u\n");
exit(-1);
}
sock = minet_socket(SOCK_STREAM);
if (sock < 0)
{
minet_perror("couldn't make socket\n");
exit(-1);
}
/* set server address*/
memset(&sa, 0, sizeof sa);
sa.sin_port = htons(server_port);
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_family = AF_INET;
/* bind listening socket */
rc = minet_bind(sock,&sa);
if (rc < 0)
{
minet_perror("couldn't bind\n");
exit(-1);
}
/* start listening */
rc = minet_listen(sock,5);
if (rc < 0)
{
minet_perror("couldn't listen\n");
exit(-1);
}
/* connection handling loop */
connections.first = NULL;
connections.last = NULL;
maxfd = sock;
while(1)
{
/* create read and write lists */
FD_ZERO(&readlist);
FD_ZERO(&writelist);
FD_SET(sock,&readlist);
for (i = connections.first; i != NULL; i = i->next)
{
if (i->state == CLOSED)
continue;
else if ((i->state == READING_HEADERS)||
(i->state == NEW))
{
FD_SET(i->sock,&readlist);
if (i->sock > maxfd)
maxfd = i->sock;
}
else if ((i->state == WRITING_RESPONSE)||
(i->state == WRITING_FILE))
{
FD_SET(i->sock,&writelist);
if (i->sock > maxfd)
maxfd = i->sock;
}
else if (i->state == READING_FILE)
{
FD_SET(i->fd,&readlist);
if (i->fd > maxfd)
maxfd = i->fd;
}
}
/* do a select */
rc = select(maxfd+1,&readlist,&writelist,0,0);
while ((rc < 0) && (errno == EINTR))
rc = select(maxfd+1,&readlist,&writelist,0,0);
if (rc < 0)
{
minet_perror("select failed ");
continue;
}
/* process sockets that are ready */
for (i = connections.first; i != NULL; i=i->next)
{
if (i->state == CLOSED)
continue;
if (i->state == NEW)
{
init_connection(i);
i->state = READING_HEADERS;
read_headers(i);
}
else if (i->state == READING_HEADERS &&
FD_ISSET(i->sock,&readlist))
read_headers(i);
else if (i->state == WRITING_RESPONSE &&
FD_ISSET(i->sock,&writelist))
write_response(i);
else if (i->state == READING_FILE &&
FD_ISSET(i->fd,&readlist))
read_file(i);
else if (i->state == WRITING_FILE &&
FD_ISSET(i->sock,&writelist))
write_file(i);
}
if (FD_ISSET(sock,&readlist))
{
sock2 = minet_accept(sock,&sa2);
if (sock2 < 0)
minet_perror("couldn't accept ");
else
{
insert_connection(sock2,&connections);
fcntl(sock2,F_SETFL,O_NONBLOCK);
}
}
}
}
void read_headers(connection *con)
{
int sock2 = con->sock;
char *bptr,*endheaders;
struct stat filestat;
int rc;
char *buf = con->buf;
/* first read loop -- get request and headers*/
while ((rc = minet_read(sock2,buf+con->headers_read,BUFSIZE-con->headers_read)) > 0)
{
con->headers_read += rc;
con->buf[con->headers_read] = '\0';
endheaders = strstr(con->buf,"\r\n\r\n");
if (endheaders != NULL)
{
endheaders[2] = '\0';
endheaders += 4;
break;
}
}
if (rc < 0)
{
if (errno == EAGAIN)
return;
minet_perror("Error reading request\n");
minet_close(sock2);
return;
}
/* parse request to get file name */
/* Assumption: this is a GET request and filename contains no spaces*/
con->ok = true;
bptr = con->buf;
strsep(&bptr," ");
if (bptr == NULL) //Not a valid request
{
con->ok = false;
}
/* get file name and size, set to non-blocking */
else
{
/* get name */
bptr = strsep(&bptr," ");
if (strlen(bptr) > FILENAMESIZE)
{
fprintf(stderr,"Filename too big\n");
minet_close(sock2);
con->state = CLOSED;
return;
}
if (bptr[0] == '/')
bptr += 1;
strcpy(con->filename,bptr);
/* try opening the file */
con->fd = open(con->filename,O_RDONLY);
if (con->fd < 0)
con->ok = false;
else
{
/* set to non-blocking, get size */
fcntl(con->fd,F_SETFL,O_NONBLOCK);
rc = stat(con->filename,&filestat);
if (rc < 0)
{
perror("error getting file stat ");
//minet_close(sock2);
return;
}
else
con->filelen = filestat.st_size;
}
}
con->state = WRITING_RESPONSE;
write_response(con);
}
void write_response(connection *con)
{
int sock2 = con->sock;
int rc;
int written = con->response_written;
char *ok_response_f = "HTTP/1.0 200 OK\r\n"\
"Content-type: text/plain\r\n"\
"Content-length: %d \r\n\r\n";
char ok_response[100];
char *notok_response = "HTTP/1.0 404 FILE NOT FOUND\r\n"\
"Content-type: text/html\r\n\r\n"\
"<html><body bgColor=black text=white>\n"\
"<h2>404 FILE NOT FOUND</h2>\n"\
"</body></html>\n";
/* send response */
if (con->ok)
{
/* send headers */
sprintf(ok_response,ok_response_f,con->filelen);
rc = writenbytes(sock2,ok_response+written,strlen(ok_response)-written);
if (rc < 0)
{
if (errno == EAGAIN)
return;
minet_perror("error writing response ");
con->state = CLOSED;
minet_close(sock2);
return;
}
con->state = READING_FILE;
read_file(con);
}
else
{
rc = writenbytes(sock2,notok_response+written,strlen(notok_response)-written);
if (rc < 0)
{
if (errno == EAGAIN)
return;
minet_perror("error writing response ");
}
con->state = CLOSED;
minet_close(con->sock);
}
}
void read_file(connection *con)
{
int rc;
/* send file */
rc = read(con->fd,con->buf,BUFSIZE);
if (rc < 0)
{
if (errno == EAGAIN)
return;
fprintf(stderr,"error reading requested file %s\n",con->filename);
return;
}
else if (rc == 0)
{
con->state = CLOSED;
minet_close(con->sock);
}
else
{
con->file_read = rc;
con->state = WRITING_FILE;
write_file(con);
}
}
void write_file(connection *con)
{
int towrite = con->file_read;
int written = con->file_written;
int rc = writenbytes(con->sock, con->buf+written, towrite-written);
if (rc < 0)
{
if (errno == EAGAIN)
return;
minet_perror("error writing response ");
con->state = CLOSED;
minet_close(con->sock);
return;
}
else
{
con->file_written += rc;
if (con->file_written == towrite)
{
con->state = READING_FILE;
con->file_written = 0;
read_file(con);
}
else
printf("shouldn't happen\n");
}
}
int readnbytes(int fd,char *buf,int size)
{
int rc = 0;
int totalread = 0;
while ((rc = minet_read(fd,buf+totalread,size-totalread)) > 0)
totalread += rc;
if (rc < 0)
{
return -1;
}
else
return totalread;
}
int writenbytes(int fd,char *str,int size)
{
int rc = 0;
int totalwritten =0;
while ((rc = minet_write(fd,str+totalwritten,size-totalwritten)) > 0)
totalwritten += rc;
if (rc < 0)
return -1;
else
return totalwritten;
}
// inserts a connection in place of a closed connection
// if there are no closed connections, appends the connection
// to the end of the list
void insert_connection(int sock,connection_list *con_list)
{
connection *i;
for (i = con_list->first; i != NULL; i = i->next)
{
if (i->state == CLOSED)
{
i->sock = sock;
i->state = NEW;
return;
}
}
add_connection(sock,con_list);
}
void add_connection(int sock,connection_list *con_list)
{
connection *con = (connection *) malloc(sizeof(connection));
con->next = NULL;
con->state = NEW;
con->sock = sock;
if (con_list->first == NULL)
con_list->first = con;
if (con_list->last != NULL)
{
con_list->last->next = con;
con_list->last = con;
}
else
con_list->last = con;
}
void init_connection(connection *con)
{
con->headers_read = 0;
con->response_written = 0;
con->file_read = 0;
con->file_written = 0;
}