-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server_s.c
150 lines (125 loc) · 2.89 KB
/
http_server_s.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
/*
* Code is pulled from:
* http://blog.abhijeetr.com/2010/04/very-simple-http-server-writen-in-c.html
* and modified to remove the fork command to make it single threaded
* instead of multi-threaded
*
* AUTHOR: Abhijeet Rastogi (http://www.google.com/profiles/abhijeet.1989)
*
* This is a very simple HTTP server. Default port is 10000 and ROOT for
* the server is your current working directory..
*
* You can provide command line arguments like:- $./a.aout -p [port] -v
*
* for ex.
* $./a.out -p 50000
* to start a server at port 50000
*/
/*
* Jordan McCaskill
* 1722645
* CMPT360
* Program to create a simple single threaded http server
*/
#include "http_server_s.h"
#include "http_common.h"
int main(int argc, char *argv[])
{
struct sockaddr_in clientaddr;
socklen_t addrlen = 0;
char c;
resume = 1;
hup = 0;
struct sigaction pSig;
//memset(&pSig, '\0', sizeof(pSig));
pSig.sa_handler = handler;
sigemptyset(&pSig.sa_mask);
pSig.sa_flags = SA_SIGINFO;
sigaction(SIGINT, &pSig, NULL);
sigaction(SIGHUP, &pSig, NULL);
//Default Values PATH = ~/ and PORT=10000
char *PORT = calloc(10, sizeof(char));
ROOT = calloc(99999, sizeof(char));
strcpy(ROOT, getenv("PWD"));
strncpy(PORT, "8000", 10);
FILE *logFile = stdout;
verbose = 0;
int slot = 0;
if (!argv[1])
logFile = parser(PORT, logFile);
//Parsing the command line arguments
while ((c = getopt(argc, argv, "p:v")) != -1)
switch (c) {
case 'v':
verbose = 1;
break;
case 'p':
strncpy(PORT, optarg, 10);
break;
case '?':
fprintf(stderr, "Wrong arguments given!!!\n");
exit(1);
default:
exit(1);
}
set_port(PORT, ROOT, logFile);
fd_set set, activeSet;
int retval;
int fdHigh = listenfd+1;
FD_ZERO(&activeSet);
FD_SET(listenfd, &activeSet);
// ACCEPT connections
while (resume) {
if (hup) {
shutdown(listenfd, SHUT_RDWR);
parser(PORT, logFile);
set_port(PORT, ROOT, logFile);
FD_ZERO(&activeSet);
FD_SET(listenfd, &activeSet);
hup = 0;
}
set = activeSet;
retval = select(fdHigh, &set, NULL, NULL, 0);
if (resume) {
if (retval == -1)
retval = -1;
else if (retval) {
returnVal(&fdHigh, &set, &activeSet,
&slot, &addrlen, &clientaddr, logFile);
}
}
}
free(ROOT);
fclose(logFile);
free(PORT);
return 0;
}
void returnVal(int *fdHigh, fd_set *set,
fd_set *activeSet, int *slot,
socklen_t *addrlen, struct sockaddr_in *clientaddr,
FILE *logFile)
{
int i;
for (i = 0; i < *fdHigh; ++i) {
if (FD_ISSET(i, set)) {
if (i == listenfd) {
*addrlen = sizeof(*clientaddr);
clients[*slot] = accept(listenfd,
(struct sockaddr *) clientaddr,
addrlen);
if (clients[*slot] < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
if (clients[*slot]+1 > *fdHigh)
*fdHigh = clients[*slot]+1;
FD_SET(clients[*slot], activeSet);
} else {
clients[*slot] = i;
respond(0, logFile);
close(i);
FD_CLR(i, activeSet);
}
}
}
}