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
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <fcgiapp.h>
#include <chrono>
#include <iostream>
#include <functional>
#include <filesystem>
#include <mutex>
#include <random>
#include <regex>
#include <string>
#include <thread>
#include <unordered_map>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <ImageMagick-6/Magick++.h>
#include "config.h"
#include "file.h"
#include "qrcode.h"
#include "storage.h"
namespace pt = boost::property_tree;
using namespace std::string_literals;
namespace fs = std::filesystem;
namespace {
uint32_t checksum32(const std::string& s)
{
uint32_t result{0};
for (int i = 0; i < s.size(); i++) {
result = ((result >> 1) | ((result & 1) << 31)) ^ (s[i] & 0xFF);
}
return result & 0x7FFFFFFF;
}
fs::path data_path;
std::string generate_id()
{
static std::random_device r;
static std::default_random_engine e1(r());
static std::uniform_int_distribution<int> uniform_dist(0, 35);
// limit tries
for (int j = 0; j < 100000; j++) {
std::string result;
for (int i = 0; i < 6; i++) {
char c{static_cast<char>('0' + uniform_dist(e1))};
if (c > '9')
c = c - '9' + 'a';
result.push_back(c);
}
fs::path path{data_path / result};
if (!fs::exists(path))
return result;
}
return "endofcodes";
}
void usage() {
std::cout <<
"Usage: \n"
" whiteboard [-c]\n"
"\n"
"Options:\n"
" -c : Cleanup database according to timeout rules (config: maxage)\n"
"\n"
"Without options, whiteboard will be started as FCGI application"
<< std::endl;
}
std::mutex storage_mutex;
void storage_cleanup(Storage& storage)
{
while(true) {
{
std::lock_guard<std::mutex> lock(storage_mutex);
storage.cleanup();
}
std::this_thread::sleep_for(std::chrono::minutes(10));
}
}
} // namespace
// the actual main() for testability
int whiteboard(int argc, char* argv[])
{
Config config;
data_path = config.getDataPath();
Storage storage(config);
if (argc == 2) {
if (argv[1] == "-h"s || argv[1] == "-?"s) {
usage();
exit(0);
} else if (argv[1] == "-c"s) {
storage.cleanup();
exit(0);
}
}
std::thread storage_cleanup_thread(storage_cleanup, std::ref(storage));
Magick::InitializeMagick(NULL); // for qrcode.cpp
int result = FCGX_Init();
if (result != 0) { // error on init
fprintf(stderr, "Error: FCGX_Init()\n");
return 1;
}
result = FCGX_IsCGI();
if (result) {
fprintf(stderr, "Error: No FCGI environment available.\n");
return 1;
}
FCGX_Request request;
result = FCGX_InitRequest(&request, 0, 0);
if (result != 0) {
fprintf(stderr, "Error: FCGX_InitRequest()\n");
return 1;
}
while (FCGX_Accept_r(&request) >= 0) {
try {
std::lock_guard<std::mutex> lock(storage_mutex);
char* method = FCGX_GetParam("REQUEST_METHOD", request.envp);
// POST for server actions, changes
if (!strcmp(method, "POST")) {
size_t contentLength { std::stoul(FCGX_GetParam("CONTENT_LENGTH", request.envp)) };
std::string postData(contentLength, '\0'); // contentLength number of bytes, initialize with 0
if (FCGX_GetStr(postData.data(), contentLength, request.in) != static_cast<int>(contentLength)) {
throw std::runtime_error("Bad data read: Content length mismatch.\r\n");
}
// postData contains POST data
std::string contentType(FCGX_GetParam("CONTENT_TYPE", request.envp));
std::string xmlData = postData; // default: interpret whole POST data as xml request
pt::ptree xml;
std::istringstream ss{xmlData};
pt::xml_parser::read_xml(ss, xml);
std::string command {xml.get<std::string>("request.command")};
if (command == "modify") {
std::string id {xml.get<std::string>("request.id")};
std::string data {xml.get<std::string>("request.data")};
storage.setDocument(id, data);
FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out);
} else if (command == "getfile") {
std::string id {xml.get<std::string>("request.id")};
std::string filedata {storage.getDocument(id)};
if (filedata.size() > 30000000)
throw std::runtime_error("File too big");
FCGX_PutS("Content-Type: application/octet-stream\r\n", request.out);
FCGX_FPrintF(request.out, "Content-Length: %d\r\n\r\n", filedata.size());
FCGX_PutStr(filedata.c_str(), filedata.size(), request.out);
} else if (command == "checkupdate") {
std::string id {xml.get<std::string>("request.id")};
std::string checksum_s {xml.get<std::string>("request.checksum")};
uint32_t checksum{static_cast<uint32_t>(stoul(checksum_s))};
//std::cout << "Checksum JS: " << checksum_s << std::endl;
//std::cout << "Checksum C++: " << checksum32(filedata) << std::endl;
std::string filedata {storage.getDocument(id)};
if (checksum != checksum32(filedata)) {
//std::cout << "Sending change..." << std::endl;
FCGX_PutS("Content-Type: application/octet-stream\r\n", request.out);
FCGX_FPrintF(request.out, "Content-Length: %d\r\n\r\n", filedata.size());
FCGX_PutStr(filedata.c_str(), filedata.size(), request.out);
} else {
//std::cout << "No change..." << std::endl;
FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out);
FCGX_PutS("No change.\r\n", request.out);
}
} else if (command == "newid") {
FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out);
FCGX_PutS(generate_id().c_str(), request.out);
} else if (command == "qrcode") {
std::string url{xml.get<std::string>("request.url")};
if (url.size() > 1000)
throw std::runtime_error("URL too big");
std::string pngdata {getQRCode(url)};
FCGX_PutS("Content-Type: image/png\r\n", request.out);
FCGX_FPrintF(request.out, "Content-Length: %d\r\n\r\n", pngdata.size());
FCGX_PutStr(pngdata.c_str(), pngdata.size(), request.out);
} else {
throw std::runtime_error("Bad command: "s + command);
}
} else {
throw std::runtime_error("Unsupported method.\r\n");
}
} catch (const std::runtime_error& ex) {
FCGX_PutS("Status: 500 Internal Server Error\r\n", request.out);
FCGX_PutS("Content-Type: text/html\r\n\r\n", request.out);
FCGX_FPrintF(request.out, "Error: %s\r\n", ex.what());
} catch (const std::exception& ex) {
FCGX_PutS("Status: 500 Internal Server Error\r\n", request.out);
FCGX_PutS("Content-Type: text/html\r\n\r\n", request.out);
FCGX_FPrintF(request.out, "Unknown exception: %s\r\n", ex.what());
}
}
storage_cleanup_thread.join();
return 0;
}
|