/
serverSelect.c
376 lines (340 loc) · 9.59 KB
/
serverSelect.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
/* A simple server in the internet domain using TCP
The port number is passed as an argument
This version runs forever, forking off a separate
process for each connection
*/
#include <stdio.h>
#include <sys/types.h> // definitions of a number of data types used in socket.h and netinet/in.h
#include <sys/socket.h> // definitions of structures needed for sockets, e.g. sockaddr
#include <netinet/in.h> // constants and structures needed for internet domain addresses, e.g. sockaddr_in
#include <stdlib.h>
#include <strings.h>
#include <sys/wait.h> /* for the waitpid() system call */
#include <signal.h> /* signal name macros, and the kill() prototype */
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#define BUF_SIZE 256
//Request Info
typedef struct {
char *Method;
char *Path;
char *IPaddr;
char *PortNo;
char *Protocol;}ReqInfo;
void sigchld_handler(int s)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
void dostuff(int); /* function prototype */
void error(char *msg)
{
perror(msg);
exit(1);
}
//Main Using select()
int main(int argc, char *argv[])
{
/*********************/
fd_set readfds;
fd_set allfds;
int fdmax;
FD_ZERO(&readfds);
FD_ZERO(&allfds);
int n,m;
int nready;
int i;int maxi=0;
/*********************/
int sockfd, newsockfd, portno, pid;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
struct sigaction sa; // for signal SIGCHLD
/**********************/
int client[FD_SETSIZE];
for(i=0;i<FD_SETSIZE;i++)
client[i]=-1;
/*********************/
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
/******************************/
FD_SET(sockfd, &allfds);
fdmax = sockfd;
/******************************/
/****** Kill Zombie Processes ******/
sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
/*********************************/
/*******change for select()*******/
while (1) {
readfds = allfds;
nready=select(fdmax+1, &readfds, NULL, NULL, NULL);
if(nready<0)
{
perror("select");
exit(-1);
}
if(nready==0)
continue;
if(FD_ISSET(sockfd,&readfds))//new client coming
{
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
for(i=0;i<FD_SETSIZE;i++)
{
if(client[i]<0)
{
client[i]=newsockfd;
if(i>maxi) maxi=i;
break;
}
}
if(i==FD_SETSIZE)
{
fprintf(stderr,"too many clients\n");
exit(-1);
}
FD_SET(newsockfd,&allfds);
if(newsockfd>fdmax) fdmax=newsockfd;
if(--nready<=0) continue;
}
for(i=0;i<=maxi;i++)
{
newsockfd = client[i];
if(newsockfd==-1) continue;
if(FD_ISSET(newsockfd,&readfds))
{
dostuff(newsockfd);
close(newsockfd);
client[i]=-1;
FD_CLR(newsockfd,&allfds);
if(--nready<=0) break;
}
}
} /* end of while */
return 0; /* we never get here */
}
//GET Info(FilePath,IP,Portno) IP,Portno not used here
ReqInfo parseRequest(char *Request)
{ ReqInfo internalData;
char *needle ="HTTP/";
char *temp = Request;
char *buf = strstr(temp, needle);
internalData.Protocol=buf;
char *token = NULL;
token = strtok(Request," ");
internalData.Method = token;
if(token!=NULL)
{
token = strtok(NULL," ");
}
internalData.Path = token;
if(token!=NULL)
{
token = strtok(NULL,":");
if(token!=NULL)
{
token = strtok(NULL,":");
}
}
internalData.IPaddr = token;
if(token!=NULL)
{
token = strtok(NULL,":");
}
internalData.PortNo = token;
return internalData;
}
//header lines
int IsValidHeader(ReqInfo request)
{
if((strcmp("GET",request.Method)==0)||(strcmp("POST",request.Method)==0))
if((strcmp("HTTP/1.1\r\nHost",request.Protocol)==0)||(strcmp("HTTP/1.0\r\nHost",request.Protocol)==0))
return 1;
return 0;
}
//header-ResponseTime
char *CurrentTime()
{
time_t timep;
time(&timep);
return ctime(&timep);
}
//header-FileLastModified
char *LastModified(char* FilePath)
{
if(FilePath[0]=='/')
FilePath++;
struct stat buf;
int rtn = stat(FilePath,&buf);
if(rtn!=0)
{
return NULL;
}
time_t t = buf.st_mtime;
char *output;
output = ctime(&t);
return output;
}
//header-FileLength
int Length(char* FilePath)
{
if(FilePath[0]=='/')
FilePath++;
struct stat buf;
int rtn = stat(FilePath,&buf);
if(rtn!=0)
{ return -1;}
int len = (int)(buf.st_size);
return len;
}
//header-Filetype
char *FileType(char* FilePath)
{
char *p;
p= rindex(FilePath,'.'); //reverse index find '.'
if(strcmp(p,".txt")==0)
return "text/html";
else if(strcmp(p,".html")==0)
return "text/html";
else if(strcmp(p,".htm")==0)//for old system
return "text/html";
else if(strcmp(p,".jpg")==0)
return "image/jpg";
else if(strcmp(p,".gif")==0)
return "image/gif";
else if(strcmp(p,".mp4")==0)
return "video/mp4";
else if(strcmp(p,".png")==0)
return "image/png";
else if(strcmp(p,".pdf")==0)
return "pdf/application";
else return "";
}
//Construct Response Header
char *constructHeader(char* FilePath)
{
FILE *fp;
fp=fopen(FilePath+1,"r");
if(fp==NULL)
return NULL;
else
{
char *header =malloc(sizeof(char)*BUF_SIZE);
memset(header,0,sizeof(char)*BUF_SIZE);
strcpy(header,"HTTP/1.1 200 OK\nConnection: close\nDate: ");
char *time = CurrentTime();
strcat(header,time);
strcat(header,"Last-Modified: ");
char *modtime = LastModified(FilePath);
strcat(header,modtime);
strcat(header,"Content-Length: ");
int leng = Length(FilePath);
char len[32];
sprintf(len,"%d",leng);
strcat(header,len);
strcat(header,"\nContent-Type: ");
char *type = FileType(FilePath);
strcat(header,type);
strcat(header,"\r\n\r\n");
return header;
free(header);
}
fclose(fp);
}
//Send Data With Header to Client
void datasend(int sock,char* URL)
{
int m,n=BUF_SIZE,hlen;//hlen is header length
FILE *fp;
char *head=constructHeader(URL);
char wbuf[BUF_SIZE];
bzero(wbuf,BUF_SIZE);
fp=fopen(URL+1,"rb");
hlen=strlen(head);
printf("%s\n",head);
strcpy(wbuf,head);
//printf("%d",hlen);
//printf("%d",m);
m=fread(wbuf+hlen,1,BUF_SIZE-hlen,fp);//combine header and data
if (m < 0) error("ERROR read from file");
m=send (sock,wbuf,hlen+m,0);
if (m < 0) error("ERROR writing to socket");
//printf("%s",wbuf);
bzero(wbuf,BUF_SIZE);
while (n == BUF_SIZE)
{n=fread(wbuf,1,BUF_SIZE,fp);
//printf("%d",n);
if (n < 0) error("ERROR read from file");
m=send(sock,wbuf,BUF_SIZE,0);
//printf("%s",wbuf);
//printf("%d",m);
if (m < 0) error("ERROR writing to socket");
bzero(wbuf,BUF_SIZE);
//if(feof(fp)==0) break;
}
fclose(fp);
}
/******** DOSTUFF() *********************
There is a separate instance of this function
for each connection. It handles all communication
once a connnection has been established.
*****************************************/
void dostuff (int sock)
{
int n,m;
char buffer[BUF_SIZE],request[BUF_SIZE];
bzero(buffer,BUF_SIZE);
ReqInfo internalData;
n = recv(sock,buffer,BUF_SIZE-1,0);
if (n < 0) error("ERROR reading from socket");
strcpy(request,buffer);
internalData = parseRequest(request);
//printf("%s,%s\n",internalData.Method,internalData.Protocol);
printf("Here is the message:\n%s",buffer);
bzero(buffer,BUF_SIZE);
//PART A: Dump Request Message to the Console
while (n == BUF_SIZE-1)
{
n = recv(sock,buffer,BUF_SIZE-1,0);
if (n < 0) error("ERROR reading from socket");
printf("%s",buffer);
bzero(buffer,BUF_SIZE);
}
if(!IsValidHeader(internalData))
{
m = send(sock,"HTTP/1.1 400 Bad Request\r\n\r\n 400 Bad Request",sizeof("HTTP/1.1 400 Bad Request\r\n\r\n 400 Bad Request")-1,0);
printf("HTTP/1.1 400 Bad Request\r\n\r\n");
if (m < 0) error("ERROR writing to socket");
}
else if(Length(internalData.Path)<0)
{
m = send(sock,"HTTP/1.1 404 Not Found\r\n\r\n 404 Not found",sizeof("HTTP/1.1 404 Not Found\r\n\r\n 404 Not found")-1,0);
printf("HTTP/1.1 404 Not Found\r\n\r\n");
if (m < 0) error("ERROR writing to socket");
//printf("%d",m);
fprintf(stderr,"file cannot open\n");
}
else
{datasend(sock,internalData.Path);}
}