-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo_server.c
553 lines (484 loc) · 15.5 KB
/
echo_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
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <ctype.h>
#include <strings.h>
#include <pthread.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <time.h>
#include <arpa/inet.h>
#define ISspace(x) isspace((int)(x))
#define BUFSIZE 1024
#define SERVER_STRING "Server: Liso/1.0 \r\n"
char *logFile;
char *www;
void accept_request(int);
void bad_request(int);
void cat(int, FILE *);
void cannot_execute(int);
void error_die(const char *);
void execute_cgi(int, const char *, const char *, const char *);
int get_line(int, char *, int);
void headers(int, const char *, int);
void not_found(int);
void serve_file(int, const char *, char*);
int startup(u_short *);
void unimplemented(int);
void error(char *msg);
char *get_filename_ext(const char *);
char * getType(char *);
/*
* error - wrapper for perror
*/
void error(char *msg) {
perror(msg);
exit(1);
}
void unimplemented(int client)
{
char buf[1024];
sprintf(buf, "HTTP/1.0 501 Method Not Implemented\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, SERVER_STRING);
send(client, buf, strlen(buf), 0);
sprintf(buf, "Content-Type: text/html\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "<HTML><HEAD><TITLE>Method Not Implemented\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "</TITLE></HEAD>\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "<BODY><P>HTTP request method not supported.\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "</BODY></HTML>\r\n");
send(client, buf, strlen(buf), 0);
}
/* Not Found message*/
void not_found(int client)
{
char buf[1024];
sprintf(buf, "HTTP/1.0 404 NOT FOUND\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, SERVER_STRING);
send(client, buf, strlen(buf), 0);
sprintf(buf, "Content-Type: text/html\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "<HTML><TITLE>Not Found</TITLE>\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "<BODY><P>The server could not fulfill\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "your request because the resource specified\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "is unavailable or nonexistent.\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "</BODY></HTML>\r\n");
send(client, buf, strlen(buf), 0);
}
/**********************************************************************/
/* Send the entire content of a file trough a socket
/**********************************************************************/
void cat(int client, FILE *resource)
{
char buffer[1024];
size_t s1=1024;
int fd=fileno(resource);
int nread;
while(1){
if((nread=read(fd,buffer,1024))<=0){
break;
}
send(client, buffer, nread, 0);
}
}
/**********************************************************************/
/* Get extension of a file */
/**********************************************************************/
char *get_filename_ext(const char *filename) {
char *dot = strrchr(filename, '.');
if(!dot || dot == filename) return "";
return dot + 1;
}
/**********************************************************************/
/* Logging */
/**********************************************************************/
void save_logs(const char *s)
{
FILE *log_file = fopen(logFile, "a"); //append
if (log_file == NULL)
{
printf("Error accessing the file!\n");
exit(1);
}
time_t localTime;
struct tm *Tm;
localTime = time(NULL);
Tm = localtime(&localTime);
fprintf(log_file, "%04d-%02d-%02d %02d:%02d:%02d - ",
Tm->tm_year+1900,
Tm->tm_mon+1,
Tm->tm_mday,
Tm->tm_hour-2,
Tm->tm_min,
Tm->tm_sec
);
fprintf(log_file, "%s\n", s);
fclose(log_file);
}
/**********************************************************************/
/* Return the informations to fill an HTTP header
/**********************************************************************/
void headers(int client, const char *filename, int size)
{
char buf[1024];
(void)filename;
char outstr[200];
time_t t;
struct tm *tmp;
t = time(NULL);
char *extension;
char *type;
tmp = localtime(&t);
struct stat attrib;
char date[200];
//Caluclate last modification date
stat(filename, &attrib);
strftime(date,sizeof(date), "%a, %d %b %Y %H:%M:%S %Z", localtime(&(attrib.st_ctime)));
if (tmp == NULL) {
perror("localtime");
exit(EXIT_FAILURE);
}
//Calculate local time
if (strftime(outstr, sizeof(outstr), "%a, %d %b %Y %H:%M:%S %Z", tmp) == 0) {
fprintf(stderr, "strftime returned 0");
exit(EXIT_FAILURE);
}/* could use filename to determine file type */
extension=get_filename_ext(filename);
type=getType(extension);
strcpy(buf, "HTTP/1.0 200 OK\r\n");
send(client, buf, strlen(buf), 0);
strcpy(buf, SERVER_STRING);
send(client, buf, strlen(buf), 0);
sprintf(buf, "Content-Type: %s\r\n",getType(extension));
send(client, buf, strlen(buf), 0);
strcpy(buf, "Connection: keep alive\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf,"Date: %s\r\n",outstr);
send(client, buf, strlen(buf), 0);
sprintf(buf,"Content-Length: %d\r\n",size);
send(client, buf, strlen(buf), 0);
sprintf(buf,"Last-Modified: %s\r\n",date);
send(client, buf, strlen(buf), 0);
strcpy(buf, "\r\n");
send(client, buf, strlen(buf), 0);
}
/**********************************************************************/
/* This function reply the user with the same message in the request */
/**********************************************************************/
void echo(int client)
{
char buf[1024];
recv(client, buf, BUFSIZE, 0);
send(client, buf, strlen(buf), 0);
return;
}
/**********************************************************************/
/* Send a regular file to the client. Use headers, and report
* errors to client if they occur.
/**********************************************************************/
void serve_file(int client, const char *filename, char *method)
{
FILE *resource = NULL;
int numchars = 1;
char buf[1024];
struct stat st;
int size;
buf[0] = 'A'; buf[1] = '\0';
while ((numchars > 0) && strcmp("\n", buf)) /* read & discard headers */
numchars = get_line(client, buf, sizeof(buf));
resource = fopen(filename, "r");
if (resource == NULL)
not_found(client);
else
{
stat(filename, &st);
size = st.st_size;
headers(client, filename,size);
if(strcmp(method,"GET")==0)
cat(client, resource);
}
fclose(resource);
}
/***********************************************************************/
/* Loops through the URL and gets the appropriate HTTP fileType values */
/**********************************************************************/
char * getType(char *extension){
FILE *resource = NULL;
int numchars = 1;
char buf[1024];
char ext[1024];
static char format[1024];
fprintf(stdout,"Path %s\n",extension);
if(strcasecmp(extension,"html")==0){
return "text/html";
}
else if(strcasecmp(extension,"css")==0){
return "text/css";
}
else if(strcasecmp(extension,"png")==0){
return "image/png";
}
else if(strcasecmp(extension,"jpg")==0){
return "image/jpeg";
}
else if(strcasecmp(extension,"gif")==0){
return "image/gif";
}
else{
fprintf(stdout,"error\n");
return "text/html";
}
}
/**********************************************************************/
/* A request has caused a call to accept() on the server port to
* return. Process the request appropriately.
* Parameters: the socket connected to the client */
/**********************************************************************/
void accept_request(int client)
{
char buf[1024];
int numchars;
char method[255];
char url[255];
char path[512];
size_t i, j;
struct stat st;
int cgi = 0; /* becomes true if server decides this is a CGI
* program */
// echo(client);
char *query_string = NULL;
numchars = get_line(client, buf, sizeof(buf));
i = 0; j = 0;
//Get The Method
while (!ISspace(buf[j]) && (i < sizeof(method) - 1))
{
method[i] = buf[j];
i++; j++;
}
method[i] = '\0';
//NOT GET OR POST
if (strcasecmp(method, "GET") && strcasecmp(method, "POST") && strcasecmp(method, "HEAD"))
{
unimplemented(client);
return;
}
if (strcasecmp(method, "POST") == 0){
fprintf(stdout,"RECEIVED A %s\n",method);
char buf[BUFSIZE];
sprintf(buf, "HTTP/1.1 200 No Content\r\n");
sprintf(buf, "%sServer: Liso/1.0\r\n", buf);
sprintf(buf, "%sContent-Length: 0\r\n", buf);
sprintf(buf, "%sContent-Type: text/html\r\n", buf);
send(client, buf, strlen(buf), 0);
return;
}
i = 0;
//Retreive the URL
while (ISspace(buf[j]) && (j < sizeof(buf)))
j++;
while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < sizeof(buf)))
{
url[i] = buf[j];
i++; j++;
}
url[i] = '\0';
//GET, HEAD METHODS
if (strcasecmp(method, "GET") == 0 || strcasecmp(method, "HEAD") == 0)
{
fprintf(stdout,"RECEIVED A %s\n",method);
query_string = url;
while ((*query_string != '?') && (*query_string != '\0'))
query_string++;
if (*query_string == '?')
{
cgi = 1;
*query_string = '\0';
query_string++;
}
}
// fprintf(stdout,"%s\n",url);
// sprintf(path, "%s%s",www, url);
sprintf(path, "www%s", url);
if (path[strlen(path) - 1] == '/')
strcat(path, "index.html");
if (stat(path, &st) == -1) {
while ((numchars > 0) && strcmp("\n", buf)) /* read & discard headers */
numchars = get_line(client, buf, sizeof(buf));
not_found(client);
}
else
{
if ((st.st_mode & S_IFMT) == S_IFDIR)
strcat(path, "/index.html");
//check the type of file
if ((st.st_mode & S_IXUSR) ||
(st.st_mode & S_IXGRP) ||
(st.st_mode & S_IXOTH) )
cgi = 1;
//Is it readable?
if(st.st_mode & S_IRUSR) {
serve_file(client, path,method);
}else{
fprintf(stdout,"Do not have permission to read the file\n");
}
}
//echo(client);
close(client);
return;
}
int get_line(int sock, char *buf, int size)
{
int i = 0;
char c = '\0';
int n;
while ((i < size - 1) && (c != '\n'))
{
n = recv(sock, &c, 1, 0);
/* DEBUG printf("%02X\n", c); */
if (n > 0)
{
if (c == '\r')
{
n = recv(sock, &c, 1, MSG_PEEK);
/* DEBUG printf("%02X\n", c); */
if ((n > 0) && (c == '\n'))
recv(sock, &c, 1, 0);
else
c = '\n';
}
buf[i] = c;
i++;
}
else
c = '\n';
}
buf[i] = '\0';
return(i);
}
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);
}
int main(int argc, char **argv)
{
char *portnum;
//variables are dynamically assigned
portnum = argv[1];
logFile = argv[2];
www = argv[3];
save_logs("Server Started - Main Loop");
fprintf(stdout,"RECEIVED portnum: %s\n",portnum);
fprintf(stdout,"RECEIVED logfile: %s\n",logFile);
fprintf(stdout,"RECEIVED www path: %s\n",www);
/**********************************************************************/
/* Code in courtesy of Beej Guide on select */
/**********************************************************************/
fd_set mainFD; // main FD list
fd_set tempFD; // temp FD list
int FDmax; // max FD int
int listener; //listening socket FD
int newlyAcceptedFD; // newest accepted socket FD
struct sockaddr_storage clientAddress;
socklen_t addressLength;
char clientIP[INET6_ADDRSTRLEN];
int status=1; // for setsockopt() SO_REUSEADDR, below
int i, j, sckt;
struct addrinfo instanceInfo, *ai, *p;
FD_ZERO(&mainFD); // clear the mainFD
FD_ZERO(&tempFD); // clear the tempFD
memset(&instanceInfo, 0, sizeof instanceInfo); // clear data stored under instanceInfo
instanceInfo.ai_family = AF_UNSPEC;
instanceInfo.ai_socktype = SOCK_STREAM;
instanceInfo.ai_flags = AI_PASSIVE;
if ((sckt = getaddrinfo(NULL, portnum, &instanceInfo, &ai)) != 0) {
exit(1);
}
for(p = ai; p != NULL; p = p->ai_next) {
listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &status, sizeof(int));
if (bind(listener, p->ai_addr, p->ai_addrlen) < 0) {
close(listener);
continue;
}
break;
}
//report binding error
if (p == NULL) {
fprintf(stderr, "Error Message: binding error\n");
save_logs("Error Message: binding error");
exit(1);
}
freeaddrinfo(ai); // free when finished
// listen
if (listen(listener, 20) == -1) {
perror("listen");
exit(1);
}
FD_SET(listener, &mainFD); // register the listener to the mainFD
FDmax = listener; // get the max FD which is the listener
while(1) {
tempFD = mainFD;
if (select(FDmax+1, &tempFD, NULL, NULL, NULL) == -1) {
save_logs("server: closed");
perror("select");
exit(1);
}
for(i = 0; i <= FDmax; i++) {
if (FD_ISSET(i, &tempFD)) {
if (i == listener) {
addressLength = sizeof clientAddress;
newlyAcceptedFD = accept(listener,
(struct sockaddr *)&clientAddress,
&addressLength);
if (newlyAcceptedFD == -1) {
perror("accept");
} else {
FD_SET(newlyAcceptedFD, &mainFD);
if (newlyAcceptedFD > FDmax) { //update max
FDmax = newlyAcceptedFD;
}
printf("server: new connection from %s on "
"socket %d\n",
inet_ntop(clientAddress.ss_family,
get_in_addr((struct sockaddr*)&clientAddress),
clientIP, INET6_ADDRSTRLEN),
newlyAcceptedFD);
save_logs("server: new connection started");
}
} else {
accept_request(i);
FD_CLR(i, &mainFD); //deregister
}
}
}
}
}