-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server_t.c
170 lines (140 loc) · 3.31 KB
/
http_server_t.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
/*
* 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_t.h"
#include "http_common.h"
/*
* The main program function for the running of the function
*/
int main(int argc, char *argv[])
{
struct threadArgs *tArgs = calloc(10, sizeof(struct threadArgs));
struct sockaddr_in clientaddr;
socklen_t addrlen;
char c;
resume = 1;
hup = 0;
pthread_t *threads = calloc(10, sizeof(threads));
FILE *logFile = stdout;
struct sigaction pSig;
memset(&pSig, '\0', sizeof(pSig));
pSig.sa_handler = handler;
sigemptyset(&pSig.sa_mask);
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);
if (!argv[1])
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);
addrlen = 0;
create(threads, tArgs, clientaddr, addrlen, logFile);
// ACCEPT connections
while (resume) {
if (hup) {
destroy(threads);
close(listenfd);
die = 0;
parser(PORT, logFile);
set_port(PORT, ROOT, logFile);
addrlen = 0;
create(threads, tArgs, clientaddr, addrlen, logFile);
hup = 0;
}
}
destroy(threads);
free(threads);
free(tArgs);
free(ROOT);
free(PORT);
fclose(logFile);
return 0;
}
void create(pthread_t *threads, struct threadArgs *tArgs,
struct sockaddr_in clientaddr, socklen_t addrlen,
FILE *logFile)
{
int i;
int rc;
for (i = 0; i < 10; i++) {
tArgs[i].arg1 = i;
tArgs[i].arg2 = clientaddr;
tArgs[i].arg3 = addrlen;
tArgs[i].arg4 = &logFile;
rc = pthread_create(&threads[i], NULL,
thread_loop, (void *)&tArgs[i]);
if (rc) {
printf("ERROR; pthread_create(): %d\n", rc);
exit(-1);
}
}
}
void destroy(pthread_t *threads)
{
shutdown(listenfd, SHUT_RDWR);
int i;
for (i = 0; i < 10; ++i)
pthread_join(threads[i], NULL);
}
void *thread_loop(void *argum)
{
struct threadArgs *tArgs = argum;
int slot = tArgs->arg1;
struct sockaddr_in clientaddr = tArgs->arg2;
socklen_t addrlen = tArgs->arg3;
FILE **logFile = tArgs->arg4;
addrlen = sizeof(clientaddr);
fd_set set;
FD_ZERO(&set);
while (!die) {
clients[slot] = accept(listenfd,
(struct sockaddr *) &clientaddr, &addrlen);
if (errno == EINTR)
shutdown(listenfd, SHUT_RDWR);
if (clients[slot] >= 0) {
FD_SET(clients[slot], &set);
while (fcntl(clients[slot], F_GETFD) >= 0)
respond(slot, *logFile);
}
FD_ZERO(&set);
}
pthread_exit(NULL);
}