-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
executable file
·165 lines (136 loc) · 6.21 KB
/
server.cpp
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
/*
* Web-server setup using Simple Web Server library
* The code in this file is modified from:
* https://gitlab.com/eidheim/Simple-Web-Server/blob/master/http_examples.cpp
*
* (C) 2020 Laurens Bosman, CMPT383 Fall2020
* Released under the MIT license
* email [email protected]
*/
#include "client_http.hpp"
#include "server_http.hpp"
#include <future>
// Added for the json-example
#define BOOST_SPIRIT_THREADSAFE
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
// needed for the index.html
#include <algorithm>
#include <boost/filesystem.hpp>
#include <fstream>
#include <vector>
#include "dataProcessing.cpp"
using namespace std;
using HttpServer = SimpleWeb::Server<SimpleWeb::HTTP>;
using HttpClient = SimpleWeb::Client<SimpleWeb::HTTP>;
// Function for activating the server
int startServer(int port) {
HttpServer server;
server.config.port = port;
// Anonymous function for serving the default homepage.
// Defaults to index.html
// This is where you start out when calling "localhost:8080" after startup
server.default_resource["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
try {
auto web_root_path = boost::filesystem::canonical("web");
auto path = boost::filesystem::canonical(web_root_path / request->path);
// Check if path is within web_root_path
if(distance(web_root_path.begin(), web_root_path.end()) > distance(path.begin(), path.end()) ||
!equal(web_root_path.begin(), web_root_path.end(), path.begin()))
throw invalid_argument("path must be within root path");
if(boost::filesystem::is_directory(path))
path /= "index.html";
SimpleWeb::CaseInsensitiveMultimap header;
auto ifs = make_shared<ifstream>();
ifs->open(path.string(), ifstream::in | ios::binary | ios::ate);
if(*ifs) {
auto length = ifs->tellg();
ifs->seekg(0, ios::beg);
header.emplace("Content-Length", to_string(length));
response->write(header);
// Trick to define a recursive function within this scope (for example purposes)
class FileServer {
public:
static void read_and_send(const shared_ptr<HttpServer::Response> &response, const shared_ptr<ifstream> &ifs) {
// Read and send 128 KB at a time
static vector<char> buffer(131072); // Safe when server is running on one thread
streamsize read_length;
if((read_length = ifs->read(&buffer[0], static_cast<streamsize>(buffer.size())).gcount()) > 0) {
response->write(&buffer[0], read_length);
if(read_length == static_cast<streamsize>(buffer.size())) {
response->send([response, ifs](const SimpleWeb::error_code &ec) {
if(!ec)
read_and_send(response, ifs);
else
cerr << "Connection interrupted" << endl;
});
}
}
}
};
FileServer::read_and_send(response, ifs);
}
else
throw invalid_argument("could not read file");
}
catch(const exception &e) {
response->write(SimpleWeb::StatusCode::client_error_bad_request, "Could not open path " + request->path + ": " + e.what());
}
};
//main POST code, receives data from the js query
//returns positive or negative result to the frontend
server.resource["^/json$"]["POST"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
try {
boost::property_tree::ptree pt;
read_json(request->content, pt);
auto data = pt.get<string>("name");
auto dataType = pt.get<string>("dataType");
cout << "Data: " << data << endl;
DataProcessor processor = DataProcessor(data, dataType);
processor.storeData();
data = processor.calcSentiment();
cout << "Result: " << data << endl;
*response << "HTTP/1.1 200 OK\r\n"
<< "Content-Length: " << data.length() << "\r\n\r\n"
<< data;
}
catch(const exception &e) {
*response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n"
<< e.what();
}
};
// POST code for receiving corrections from the frontend if a user submits it
server.resource["^/sent$"]["POST"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
try {
boost::property_tree::ptree pt;
read_json(request->content, pt);
auto data = pt.get<string>("data");
auto dType = pt.get<string>("dataType");
auto sent = pt.get<string>("sentiment");
cout << endl << "Correction" << endl;
cout << "Data: " << data << endl;
cout << "Data type: " << dType << endl;
cout << "Supposed to be: " << sent << endl << endl;
DataProcessor processor = DataProcessor(data, dType, sent);
processor.storeCorrection();
processor.updateModel();
*response << "HTTP/1.1 200 OK\r\n";
}
catch(const exception &e) {
*response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n"
<< e.what();
}
};
// Start server and receive assigned port when server is listening for requests
promise<unsigned short> server_port;
thread server_thread([&server, &server_port]() {
// Start server
server.start([&server_port](unsigned short port) {
server_port.set_value(port);
});
});
cout << "Server listening on port " << server_port.get_future().get() << endl
<< endl;
server_thread.join();
return 0;
}