-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb-server-persistent.cpp
More file actions
181 lines (168 loc) · 4.67 KB
/
web-server-persistent.cpp
File metadata and controls
181 lines (168 loc) · 4.67 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
#include <string>
#include <thread>
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <algorithm>
#include <time.h>
#include "httpRequest.h"
#include "httpResponse.h"
std::string directory;
void doStuff(int socketFd);
int timeout[1024];
// Web server script
// Initiates a server
// Arguments: ./webserver hostname port directory
// Defaults: localhost 4000 .
int main(int argc, char* argv[])
{
fd_set readFds;
fd_set errFds;
fd_set watchFds;
FD_ZERO(&readFds);
FD_ZERO(&errFds);
FD_ZERO(&watchFds);
for (int i = 0; i < 1024; i++){
timeout[i] = 0;
}
struct sockaddr_in serverAddress;
bzero((char*) &serverAddress, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddress.sin_port = htons(4000);
directory = ".";
if(4 == argc) {
// store this IP address in sa:
inet_pton(AF_INET, argv[1], &(serverAddress.sin_addr));
serverAddress.sin_port = htons(atoi(argv[2]));
directory = std::string(argv[3]);
}
// Create a webserver on hostname:port
// that serves files from directory
int listenFd = socket(AF_INET, SOCK_STREAM, 0);
int maxFd = listenFd;
if(listenFd < 0)
{
std::cerr << "Cannot open socket" << std::endl;
return -1;
}
FD_SET(listenFd, &watchFds);
int yes = 1;
if (setsockopt(listenFd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1){
perror("setsockopt");
return 1;
}
int res = bind(listenFd, (struct sockaddr*)&serverAddress, sizeof(serverAddress));
if(res < 0) {
std::cerr << "Cannot bind" << std::endl;
return -1;
}
if(listen(listenFd, 10) < 0) {
std::cerr << "Cannot listen" << std::endl;
return -1;
}
struct timeval tv;
while (true) {
int nReadyFds = 0;
readFds = watchFds;
errFds = watchFds;
tv.tv_sec = 3;
tv.tv_usec = 0;
time_t curr;
time(&curr);
for (int i = 0; i <= maxFd; i++){
if (i != listenFd && timeout[i] != 0 && timeout[i] <= curr){
timeout[i] = 0;
close(i);
FD_CLR(i, &watchFds);
}
}
if ((nReadyFds = select(maxFd + 1, &readFds, NULL, &errFds, &tv)) == -1)
{
perror ("Cannot select");
return -1;
}
if (nReadyFds != 0){
for (int fd = 0; fd <= maxFd; fd++){
if (FD_ISSET(fd, &readFds)){//get one for reading
if(fd == listenFd) {
struct sockaddr_in client_addr;
socklen_t client_addr_size = sizeof(client_addr);
int clientFd = accept(fd, (struct sockaddr*)&client_addr, &client_addr_size);
time_t current;
time(¤t);
timeout[clientFd] = current + 30;
if (clientFd == -1){
perror("Cannot accept");
return -1;
}
char ipstr[INET_ADDRSTRLEN] = {'\0'};
inet_ntop(client_addr.sin_family, &client_addr.sin_addr, ipstr, sizeof(ipstr));
time(¤t);
timeout[clientFd] = current + 30;
if (maxFd < clientFd){
maxFd = clientFd;
}
FD_SET (clientFd, &watchFds);
}
else { //normal socket
time_t now;
time(&now);
timeout[fd] = now + 30;
doStuff(fd);
FD_CLR(fd, &watchFds);
}
}
}
}
time_t cur;
time(&cur);
for (int i = 0; i < maxFd; i++){
if (i != listenFd && timeout[i] <= cur){
close(i);
FD_CLR(i, &watchFds);
}
}
}
return 0;
}
// Handle a connection from a client
void doStuff(int socketFd) {
// Get the request
std::string request = "";
char buf[8193] = {0};
if(recv(socketFd, buf, 8192, 0) > 0) {
request.append(buf);
}
// Parse the request
HttpResponse response;
// Get the url from request
size_t start = request.find(' ');
size_t end = request.find(' ', start + 1);
std::string version = request.substr(end + 1, 8);
HttpRequest req;
if (request != "") {
req.consume(request);
}
if(req.invalidRequest() || version.substr(0, 4) != "HTTP") {
response.setStatus("400 Invalid Request");
}
else if ("HTTP/1.0" != version) {
response.setStatus("505 Version not supported");
}
else {
std::string url = request.substr(start + 1, end - start - 1);
response.setUrl(directory + "/" + url);
response.setMessage();
}
std::string blob = response.encode();
// Form a response
// Send back the response
send(socketFd, blob.c_str(), blob.length() + 1, 0);
}