-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathweb-server.cpp
More file actions
279 lines (247 loc) · 8.04 KB
/
web-server.cpp
File metadata and controls
279 lines (247 loc) · 8.04 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
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "string-utils.h"
#include <map>
#include <netinet/in.h>
#include <regex>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <thread>
#include <unistd.h>
#include <vector>
#include <unistd.h>
#include <fcntl.h>
using namespace std;
#include "web-server.h"
Request Request::from_string(string s) {
Request r;
auto lines = split(s, "\r\n");
auto request_line = lines[0];
auto request_parts = split(request_line, ' ');
const static std::regex request_regex(R"(^(\w+)\s+(\S+)\s+(.*)$)");
std::smatch matches;
std::regex_match(request_line, matches, request_regex);
if (matches.size() != 4) {
throw string("bad request line \""+s+"\"");
}
r.method = matches[1];
r.uri = matches[2];
r.http_version = matches[3];
const static std::regex header_regex(R"(^(\S*)\:\s*(\S.*)\s*$)");
for (int i = 1; i < lines.size(); ++i) {
auto &line = lines[i];
regex_match(line, matches, header_regex);
if (matches.size() != 3) {
break; // end of headers
}
r.headers[matches[1]] = matches[2];
}
// break apart the command line parameters, if any example /my/path?color=green&fruit=apple
auto uri_and_params = split(r.uri,'?');
if(uri_and_params.size()==2) {
r.uri=uri_and_params[0];
auto params_string = uri_and_params[1];
for(auto param_setting : split(params_string,'&')) {
auto name_value = split(param_setting,'=');
if(name_value.size()==2) {
r.params[name_value[0]]=name_value[1];
}
}
}
return r;
}
string Request::to_string() const {
stringstream ss;
ss << "method: " << method << endl;
ss << "uri: " << uri << endl;
ss << "http_version: " << http_version << endl;
for (auto &it : headers) {
ss << it.first << ": " << it.second << endl;
}
ss << endl;
return ss.str();
}
Response::Response(int fd) : fd(fd) {
}
void Response::add_header(string name, string value) {
headers[name] = value;
}
void Response::write_status(int code, string description) {
if(status_written) {
throw string("Error: Response::write_status called twice in same response");
}
string s = (string) "HTTP/1.1 " + to_string(code) + " " + description + "\r\n";
write(s.c_str(), s.size());
status_written = true;
}
void Response::enable_multipart() {
multipart = true;
close_requested = true;
}
// writes content-length and bytes, writes good status if no status has been sent
// does nothing if connection is closed
void Response::write_content(string mime_type, const char *bytes, size_t byte_count) {
if(is_closed()) {
return;
}
if(!status_written) {
write_status();
}
if (multipart) {
if (results_sent == 0) {
headers["Content-type"] = "multipart/x-mixed-replace;boundary=--boundarydonotcross";
headers["Connection"] = "close";
write_headers();
}
write(multipart_boundary);
headers.clear();
headers["Content-type"] = mime_type;
write_headers();
write(bytes, byte_count);
} else {
if(results_sent > 0) {
throw string("write_content called multiple times in non-multipart response");
}
headers["Content-type"] = mime_type;
headers["Content-length"] = to_string(byte_count);
write_headers();
write(bytes, byte_count);
}
++results_sent;
}
// marks end of multi-part response,
// calls after first time ignored
void Response::end() {
if(end_written) {
return;
}
if (multipart && fd > 0) {
write(multipart_final_boundary);
}
end_written = true;
}
bool Response::is_closed() {
char buff[2];
if (fd <= 0 || (recv(fd, buff, 1, MSG_PEEK | MSG_DONTWAIT) == 0)) {
return true;
}
return false;
}
void Response::write_headers() {
for (auto header : headers) {
string bytes = header.first + ": " + header.second + "\r\n";
write(bytes);
}
write("\r\n");
}
void Response::write(string s) { write(s.c_str(), s.length()); }
void Response::write(const char *bytes, size_t byte_count) {
if (fd > 0) {
int i = 0;
while (i < byte_count) {
int written = send(fd, bytes + i, byte_count - i, MSG_NOSIGNAL);
if (written == -1) {
throw string("cannot send, client closed");
}
i += written;
}
}
}
typedef std::function<void(const Request &, Response &)> Handler;
void connection_thread(int client_socket_fd, map<string, Handler> *handler_map, Handler *default_handler) {
bool trace=false;
try {
if(trace) cout << "connected to client socket " << client_socket_fd << endl;
const size_t buffer_size = 2000;
char buffer[buffer_size];
// read request
fd_set readset;
struct timeval timeout;
while (true) {
timeout.tv_sec = 0;
timeout.tv_usec = 10000;
FD_ZERO(&readset);
FD_SET(client_socket_fd, &readset);
int result = select(client_socket_fd + 1, &readset, NULL, NULL, &timeout);
if (result > 0) {
bzero(buffer, buffer_size);
auto count_received = recv(client_socket_fd, buffer, buffer_size - 1, MSG_DONTWAIT);
buffer[count_received] = 0; // terminate string
if (count_received == 0) {
if(trace) cout << "client disconnected " << client_socket_fd << endl;
break;
}
Request request = Request::from_string(buffer);
if(trace) cout << request.method << " " << request.uri << endl;
Response response(client_socket_fd);
string handler_key = request.method + request.uri;
auto it = handler_map->find(handler_key);
if (it != handler_map->end()) {
it->second(request, response);
} else {
(*default_handler)(request, response);
}
if (response.close_requested) {
if(trace) cout << "response requested close" << endl;
break;
}
}
}
} catch (string s) {
if(trace) cout << "Error in connection thread: " << s << endl;
}
if(trace) cout << "closing connection " << client_socket_fd << endl;
close(client_socket_fd);
if(trace) cout << "leaving connection thread" << endl;
}
void WebServer::add_handler(string method, string uri, Handler handler) { handler_map[method + uri] = handler; }
void WebServer::run(int port) {
int server_socket_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (server_socket_fd < 0)
throw(string("ERROR opening socket"));
linger lin;
lin.l_onoff = 0;
lin.l_linger = 0;
setsockopt(server_socket_fd, SOL_SOCKET, SO_LINGER, (const char *)&lin, sizeof(int));
int enable = 1;
if (setsockopt(server_socket_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
throw(string("setsockopt(SO_REUSEADDR) failed"));
struct sockaddr_in serv_addr;
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
if (bind(server_socket_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
throw(string("ERROR on binding"));
listen(server_socket_fd, max_connection_count);
cout << "web-server listening on port " << port << endl;
while (true) {
struct timeval tv;
tv.tv_sec = 10;
tv.tv_usec = 0;
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(server_socket_fd, &readfds);
int select_result = select(server_socket_fd + 1, &readfds, NULL, NULL, &tv);
if (select_result > 0) {
auto client_socket_fd = accept(server_socket_fd, NULL, NULL);
if (client_socket_fd > 0) {
std::thread t(connection_thread, client_socket_fd, &handler_map, &default_handler);
pthread_setname_np(t.native_handle(), "web-server-worker");
t.detach();
} else {
static int count = 0;
++count;
cout << "select return " << select_result <<", and no client. client_socket_fd=" << client_socket_fd << endl;
cout << "trying to read server_socket_fd to see if things clear up" << endl;
char buff[80];
int bytes_read = recv(server_socket_fd, buff, 80, MSG_PEEK | MSG_DONTWAIT);
cout << "recv returned " << bytes_read << ", this has happened " << count << " times." << endl;
}
}
}
}