-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.h
More file actions
560 lines (421 loc) · 14.4 KB
/
Copy pathhttpserver.h
File metadata and controls
560 lines (421 loc) · 14.4 KB
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
554
555
556
557
558
/*
//========================================================
//HTTP SERVER (httpserver.h)
//========================================================
Developed by: Kamal Panthi
Email: kamal210@hotmail.com
The attached folder(tar.gz file) contains the following files
httpserver.c
httpserver.h
Makefile
Readme.txt
index.html
//To Compile type:"gcc -g httpserver.c -o httpserver"
//or Use Makefile to compile.
--> type make
//To run server type: "./httpserver portnumber"
//========================================================
//BROWSER RUNNING INFORMATION
//========================================================
From any browser type: http://hostaddress:portnumber/index.html
--> hostaddress is that host address where httpserver is running
--> portnumber is that port where httpserver is running
==>When the client from a new IP requests a page..the client receives a welcome page
and the IP address of that client is stored in server.
==>If registered client sends request for a page than welcome page is not displayed..the desired requested page is displayed
==>The logfile.html is created if it doesnot exist already,
==>The logfile.html saves the record of the request from the client browsers.
It stores the request arrival time, request resource and IP of requesting client.
==>This logfile can be viewed from client browsers by typing : http://hostaddress:portnumber/logfile.html
==>The server sleeps for 3 seconds on every 1024 bytes transferred to client
==>The server supports multiple client connections.
//========================================================
//ACKNOWLEDGEMENT:
//========================================================
http://guillaume.maillard.free.fr/libc/libc_403.html
http://publib.boulder.ibm.com/iseries/v5r1/ic2924/index.htm?info/apis/waitpid.htm
http://www.faqs.org/rfcs/rfc1945.html
http://www.paulgriffiths.net/program/c/webserv.php
Stevens, W.R.: Unix Network Programming, The Sockets Networking API, Vol. 1, 3rd Ed., Prentice Hall, 2004.
Internetworking with TCP/IP Vol. 3: Client-Server Programming and Application, Linux/POSIX Socket Version (Comer, D.E., Stevens, D.), 2000.
Stevens, W.R.: Unix Network Programming, The Sockets Networking API, Vol. 1, 3rd Ed., Prentice Hall, 2004.
Internetworking with TCP/IP Vol. 3: Client-Server Programming and Application, Linux/POSIX Socket Version (Comer, D.E., Stevens, D.), 2000.
//========================================================
*/
//==================Header Declarations===================
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/wait.h>
//================Global Variable Declaration=============
int PORT,BROWSERCOUNTER=0;
time_t CURRENTTIME;
struct tm *LOCALTIME;
char TEMPBUFFER[1024];
//=============simple web page displayed for newly CONN client only once through the lifetime==============
char welcomePage[1024]={"<html><body><h1>HTTP SERVER</h1><h4>Simple web page for first time connecting client</h4><p>HTTP server support RFC1945 standard and the following status code responses are implmented here.<br> <li>200 OK<li>400 Bad Request<li>404 Not Found<li>501 Not Implemented<br><h6>HTTP SERVER ABILITIES</h6><br><br>---Multiple simultaneus connections<br>---Every client responses are recorded in log file with the time and resource name<br>---When client(new IP) contacts to the server first time, it receives this welcome web page and the IP is saved to know the client for future references<br>---Log file is accessible through a browser as logfile.html<br>---After sending each packet of 1024 bytes the server sleeps for 3 seconds<br><br><br>Other Links: <br><a href=\"index.html\">Click here for Index Page</a><br><a href=\"logfile.html \">Click here for Log File</a></p></body></html>"};
//Other Files are: <br><b>index.html</b><br><b>logfile.html</b></p></body></html>"};
//=============Structure for storing browser IP==========================
//=======BROWSER ==========
typedef struct
{
char IP[16];//client IP
}browsers;
browsers BROWSERINSTANCE[100];//user instance for browsers
//==========ENUMERATED DATATYPES FOR METHODS===================
enum METHODS { GET, HEAD, UNSUPPORTED };//Method varieties
//==========ENUMERATED DATATYPES FOR TYPES===================
enum TYPES { SIMPLE, FULL };//Type varieties
//=============Structure for storing Client Request Information==========================
struct CLIENTIFORMATION
{
enum METHODS method;//method
enum TYPES type;//type
char *referer;//referer
char *useragent;//useragent
char *resource;//resource
int status;//status code
};
//==========================LIST OF FUNCTIONS=============================================
//=================Function to Convert lower case to uppercase================
int CONVERTTOUPPER(char * package);
//==================Function to write into file==========================
void WRITETOFILE(char* msg, char* file);
//=============Function to Read a line from a socket====================
ssize_t READSOCK(int FDSOCK, void *PACKAGE, size_t PACKAGELENGTH);
//==========Function to Write a line to a socket================
ssize_t WRITESOCK(int FDSOCK, const void *PACKAGE, size_t PACKAGELENGTH);
//===============FUNCTION to parse request header===================
int REQUESTPARSER(char * buffer, struct CLIENTIFORMATION * CLIENT_REQUEST);
//===========Function to Service an HTTP request===============
int SERVICING(int CONN, int FIRSTTIME);
//=================Function to Convert lower case to uppercase================
int CONVERTTOUPPER(char * package)
{
while ( *package )
{
*package = toupper(*package);
++package;
}
return 0;
}
//==================Function to write into file==========================
void WRITETOFILE(char* msg, char* file)
{
FILE *fp;
int fd;
fp = fopen(file,"a");//opening the file
fd= fileno(fp);//converting the file stream to int
flock(fd, LOCK_EX);//Locking the file for writing by single program
fprintf(fp,"%s\n",msg);//writing to file
flock(fd, LOCK_UN);//unlocking the file
fclose(fp);//vlosing the file
}
//=============Function to Read a line from a socket====================
ssize_t READSOCK(int FDSOCK, void *PACKAGE, size_t PACKAGELENGTH)
{
char c, *buffer;
ssize_t length, ReadLength;
buffer = PACKAGE;
for ( length = 1; length < PACKAGELENGTH; length++ )
{
if ( (ReadLength = read(FDSOCK, &c, 1)) == 1 )
{
*buffer++ = c;
if ( c == '\n' )
break;
}
else if ( ReadLength == 0 )
{
if ( length == 1 )
return 0;
else
break;
}
else
{
if ( errno == EINTR )
continue;
{
perror("Server- READSOCK() error");
exit(EXIT_FAILURE);
}
}
}
*buffer = 0;
return length;
}
//==========Function to Write a line to a socket================
ssize_t WRITESOCK(int FDSOCK, const void *PACKAGE, size_t PACKAGELENGTH)
{
size_t PACKAGELENGTHLEFT;
ssize_t PACKAGELENGTHWRITTEN;
const char *buffer;
buffer = PACKAGE;
PACKAGELENGTHLEFT = PACKAGELENGTH;
while ( PACKAGELENGTHLEFT > 0 )
{
if ( (PACKAGELENGTHWRITTEN = write(FDSOCK, buffer, PACKAGELENGTHLEFT)) <= 0 )
{
if ( errno == EINTR )
PACKAGELENGTHWRITTEN = 0;
/*else
{
perror("Server- WRITESOCK() error");
exit(EXIT_FAILURE);
}
*/
}
PACKAGELENGTHLEFT -= PACKAGELENGTHWRITTEN;
buffer += PACKAGELENGTHWRITTEN;
}
return PACKAGELENGTH;
}
//===============FUNCTION to parse request header===================
int REQUESTPARSER(char * buffer, struct CLIENTIFORMATION * CLIENT_REQUEST)
{
static int FIRSTLINEREQUEST = 1;
char *temp;
char *ENDPOINTER;
int HEADERLENGTH;
//==========If FIRSTLINEREQUEST == 0, it is the first line of the HTTP request=====
if ( FIRSTLINEREQUEST == 1 )//request line
{
if ( !strncmp(buffer, "GET ", 4) )
{
CLIENT_REQUEST->method = GET;
buffer += 4;
}
else if ( !strncmp(buffer, "HEAD ", 5) )
{
CLIENT_REQUEST->method = HEAD;
buffer += 5;
}
else
{
CLIENT_REQUEST->method = UNSUPPORTED;
CLIENT_REQUEST->status = 501;//NOT IMPLEMENTED
return -1;
}
//=======move to beginning of resource file=====
while ( *buffer && isspace(*buffer) )
buffer++;
ENDPOINTER = strchr(buffer, ' ');
if ( ENDPOINTER == NULL )
HEADERLENGTH = strlen(buffer);//calculating length
else
HEADERLENGTH = ENDPOINTER - buffer;
if ( HEADERLENGTH == 0 )
{
CLIENT_REQUEST->status = 400;//bad request
return -1;
}
CLIENT_REQUEST->resource = calloc(HEADERLENGTH + 1, sizeof(char));
strncpy(CLIENT_REQUEST->resource, buffer, HEADERLENGTH);//storing resource in CLIENT_REQUEST
if ( strstr(buffer, "HTTP/") )//if supplied the HTTP/ information it is FULL
CLIENT_REQUEST->type = FULL;//full request
else//else it is SIMPLE
CLIENT_REQUEST->type = SIMPLE;//simple request
FIRSTLINEREQUEST = 0;
return 0;
}
//if only when request is FULL
ENDPOINTER = strchr(buffer, ':');
if ( ENDPOINTER == NULL )
{
CLIENT_REQUEST->status = 400;//bad request
return -1;
}
temp = calloc( (ENDPOINTER - buffer) + 1, sizeof(char) );
strncpy(temp, buffer, (ENDPOINTER - buffer));
CONVERTTOUPPER(temp);
buffer = ENDPOINTER + 1;//increasing buffer to point to a value
while ( *buffer && isspace(*buffer) )
++buffer;
if ( *buffer == '\0' )
return 0;
if ( !strcmp(temp, "USER-AGENT") ) //user-agent
{
CLIENT_REQUEST->useragent = malloc( strlen(buffer) + 1 );
strcpy(CLIENT_REQUEST->useragent, buffer);
}
else if ( !strcmp(temp, "REFERER") ) //referer
{
CLIENT_REQUEST->referer = malloc( strlen(buffer) + 1 );
strcpy(CLIENT_REQUEST->referer, buffer);
}
free(temp);//free temp buffer
return 0;
}
//===========Function to Service an HTTP request===============
int SERVICING(int CONN, int FIRSTTIME)
{
struct CLIENTIFORMATION CLIENT_REQUEST;
int RESFILE = 0;
char buffer[1024] = {0};
int SELECTVALUE;
fd_set FDesc;
struct timeval TIMEOUT;
char RESOURCELOCATION[512];
//=======Initialize Struct=========
CLIENT_REQUEST.useragent = NULL;
CLIENT_REQUEST.referer = NULL;
CLIENT_REQUEST.resource = NULL;
CLIENT_REQUEST.method = UNSUPPORTED;
CLIENT_REQUEST.status = 200;
//========Get Request================
TIMEOUT.tv_sec = 500;//Timeout set to 5 seconds
TIMEOUT.tv_usec = 0;
do //If FULL request loop until a blank line is received or until select() times out, else If SIMPLE request loop only once.
{
//======file descriptors reset=====
FD_ZERO(&FDesc);
FD_SET (CONN, &FDesc);
SELECTVALUE = select(CONN + 1, &FDesc, NULL, NULL, &TIMEOUT);
if ( SELECTVALUE < 0 ) //if error in select()
{
perror("Server- select() error");
exit(EXIT_FAILURE);
}
else if ( SELECTVALUE == 0 ) //if timer expires
{
return -1;
}
else
{
//====reading an input line====
READSOCK(CONN, buffer, 1024 - 1);
//=====trimming a whitespace at the end of the line===
int m = strlen(buffer) - 1;
while ( !isalnum(buffer[m]) && m >= 0 )
buffer[m--] = '\0';
if ( buffer[0] == '\0' )
break;
if ( REQUESTPARSER(buffer, &CLIENT_REQUEST) )
break;
}
} while ( CLIENT_REQUEST.type != SIMPLE );
if ( CLIENT_REQUEST.status == 200 )//if client request status is OK i.e. 200
{
//===Cleaning urlengthcoded data from resource name==========
char asciinum[3] = {0};
int i = 0, c;
while ( CLIENT_REQUEST.resource[i] )
{
if ( CLIENT_REQUEST.resource[i] == '+' )
CLIENT_REQUEST.resource[i] = ' ';
else if ( CLIENT_REQUEST.resource[i] == '%' )
{
asciinum[0] = CLIENT_REQUEST.resource[i+1];
asciinum[1] = CLIENT_REQUEST.resource[i+2];
CLIENT_REQUEST.resource[i] = strtol(asciinum, NULL, 16);
c = i+1;
do
{
CLIENT_REQUEST.resource[c] = CLIENT_REQUEST.resource[c+2];
} while ( CLIENT_REQUEST.resource[2+(c++)] );
}
++i;
}
memset(TEMPBUFFER,0,1024);
strcat(TEMPBUFFER, "..");
strcat(TEMPBUFFER, CLIENT_REQUEST.resource);
strcat(TEMPBUFFER, "]<br>\n");
WRITETOFILE(TEMPBUFFER, "logfile.html");
//========making the requested file path========
memset(RESOURCELOCATION,0,512);
strcat(RESOURCELOCATION, "../c0346747_Kamal_Panthi");
strcat(RESOURCELOCATION, CLIENT_REQUEST.resource);
if ( (RESFILE = open(RESOURCELOCATION, O_RDONLY)) < 0 ) //open the file
{
if ( errno == EACCES )
{
CLIENT_REQUEST.status = 401;//unauthorized
}
else
{
CLIENT_REQUEST.status = 404;//not found
}
}
}
if ( CLIENT_REQUEST.type == FULL )//if client request is full
{
char ReqBuf[100];
sprintf(ReqBuf, "HTTP/1.0 %d OK\r\n", CLIENT_REQUEST.status);
WRITESOCK(CONN, ReqBuf, strlen(ReqBuf));
WRITESOCK(CONN, "Server: PGWebServ v0.1\r\n", 24);
WRITESOCK(CONN, "Content-Type: text/html\r\n", 25);
WRITESOCK(CONN, "\r\n", 2);
}
if ( CLIENT_REQUEST.status == 200 ) //when status is OK i.e. 200
{
if(FIRSTTIME==0)
{
if ( write(CONN, &welcomePage, 1024) < 1 )
{
perror("Server- write() error");
exit(EXIT_FAILURE);
}
}
else
{
char c;
int i, packsize=0;
while ( (i = read(RESFILE, &c, 1)) )
{
if ( i < 0 )
{
perror("Server- read() error");
exit(EXIT_FAILURE);
}
if ( write(CONN, &c, 1) < 1 )
{
perror("Server- write() error");
exit(EXIT_FAILURE);
}
packsize++;
if(packsize==1024)
{
packsize=0;
sleep(3);//after sending 1024 chars it sleeps for 3 seconds
}
}
}
}
else//if error occurred
{
char ERRORPACKAGE[100];
char STATUSCODE[3];
memset(STATUSCODE,0,3);
sprintf(STATUSCODE, "%d",CLIENT_REQUEST.status);
sprintf(ERRORPACKAGE, "<HTML>\n<HEAD>\n<TITLE>Server Error %d</TITLE>\n</HEAD>\n\n<BODY>\n<H1>Server Error %d</H1>\n<P>Error Occurred.</P>\n</BODY>\n</HTML>\n", CLIENT_REQUEST.status,CLIENT_REQUEST.status);
WRITESOCK(CONN, ERRORPACKAGE, strlen(ERRORPACKAGE));
}
if ( RESFILE > 0 )//if resource file is opened
if ( close(RESFILE) < 0 )
{
perror("Server- Close() error");
exit(EXIT_FAILURE);
}
// Free the allocated memory locations
if ( CLIENT_REQUEST.useragent )
free(CLIENT_REQUEST.useragent);
if ( CLIENT_REQUEST.referer )
free(CLIENT_REQUEST.referer);
if ( CLIENT_REQUEST.resource )
free(CLIENT_REQUEST.resource);
return 0;
}