0
|
1 /**
|
|
2 * Palantir - A Lightweight, RESTful DICOM Store
|
|
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
|
|
4 * Belgium
|
|
5 *
|
|
6 * This program is free software: you can redistribute it and/or
|
|
7 * modify it under the terms of the GNU General Public License as
|
|
8 * published by the Free Software Foundation, either version 3 of the
|
|
9 * License, or (at your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful, but
|
|
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
18 **/
|
|
19
|
|
20
|
|
21 #include "Toolbox.h"
|
|
22
|
|
23 #include "PalantirException.h"
|
|
24
|
|
25 #include <string.h>
|
|
26 #include <boost/filesystem.hpp>
|
|
27 #include <boost/filesystem/fstream.hpp>
|
|
28 #include <algorithm>
|
|
29
|
|
30 #if defined(_WIN32)
|
|
31 #include <windows.h>
|
|
32 #endif
|
|
33
|
|
34 #if defined(__linux)
|
10
|
35 #include <signal.h>
|
0
|
36 #include <unistd.h>
|
|
37 #endif
|
|
38
|
22
|
39 #include "../Resources/md5/md5.h"
|
|
40
|
0
|
41
|
|
42 namespace Palantir
|
|
43 {
|
|
44 static bool finish;
|
|
45
|
10
|
46 #if defined(_WIN32)
|
22
|
47 static BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType)
|
|
48 {
|
|
49 // http://msdn.microsoft.com/en-us/library/ms683242(v=vs.85).aspx
|
|
50 finish = true;
|
|
51 return true;
|
|
52 }
|
|
53 #else
|
0
|
54 static void SignalHandler(int)
|
|
55 {
|
|
56 finish = true;
|
|
57 }
|
22
|
58 #endif
|
0
|
59
|
|
60 void Toolbox::Sleep(uint32_t seconds)
|
|
61 {
|
|
62 #if defined(_WIN32)
|
|
63 ::Sleep(static_cast<DWORD>(seconds) * static_cast<DWORD>(1000));
|
|
64 #elif defined(__linux)
|
|
65 usleep(static_cast<uint64_t>(seconds) * static_cast<uint64_t>(1000000));
|
|
66 #else
|
|
67 #error Support your platform here
|
|
68 #endif
|
|
69 }
|
|
70
|
|
71 void Toolbox::USleep(uint64_t microSeconds)
|
|
72 {
|
|
73 #if defined(_WIN32)
|
8
|
74 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000)));
|
0
|
75 #elif defined(__linux)
|
|
76 usleep(microSeconds);
|
|
77 #else
|
|
78 #error Support your platform here
|
|
79 #endif
|
|
80 }
|
|
81
|
|
82
|
|
83 void Toolbox::ServerBarrier()
|
|
84 {
|
10
|
85 #if defined(_WIN32)
|
|
86 SetConsoleCtrlHandler(ConsoleControlHandler, true);
|
|
87 #else
|
0
|
88 signal(SIGINT, SignalHandler);
|
|
89 signal(SIGQUIT, SignalHandler);
|
|
90 #endif
|
|
91
|
|
92 finish = false;
|
|
93 while (!finish)
|
|
94 {
|
|
95 USleep(100000);
|
|
96 }
|
|
97
|
10
|
98 #if defined(_WIN32)
|
|
99 SetConsoleCtrlHandler(ConsoleControlHandler, false);
|
|
100 #else
|
0
|
101 signal(SIGINT, NULL);
|
|
102 signal(SIGQUIT, NULL);
|
|
103 #endif
|
|
104 }
|
|
105
|
|
106
|
|
107
|
|
108 void Toolbox::ToUpperCase(std::string& s)
|
|
109 {
|
|
110 std::transform(s.begin(), s.end(), s.begin(), toupper);
|
|
111 }
|
|
112
|
|
113
|
|
114 void Toolbox::ToLowerCase(std::string& s)
|
|
115 {
|
|
116 std::transform(s.begin(), s.end(), s.begin(), tolower);
|
|
117 }
|
|
118
|
|
119
|
|
120
|
|
121 void Toolbox::ReadFile(std::string& content,
|
|
122 const std::string& path)
|
|
123 {
|
|
124 boost::filesystem::ifstream f;
|
|
125 f.open(path, std::ifstream::in | std::ios::binary);
|
|
126 if (!f.good())
|
|
127 {
|
|
128 throw PalantirException("Unable to open a file");
|
|
129 }
|
|
130
|
|
131 // http://www.cplusplus.com/reference/iostream/istream/tellg/
|
|
132 f.seekg(0, std::ios::end);
|
|
133 std::streamsize size = f.tellg();
|
|
134 f.seekg(0, std::ios::beg);
|
|
135
|
|
136 content.resize(size);
|
|
137 if (size != 0)
|
|
138 {
|
|
139 f.read(reinterpret_cast<char*>(&content[0]), size);
|
|
140 }
|
|
141
|
|
142 f.close();
|
|
143 }
|
|
144
|
|
145
|
|
146 void Toolbox::RemoveFile(const std::string& path)
|
|
147 {
|
|
148 if (boost::filesystem::exists(path))
|
|
149 {
|
|
150 if (boost::filesystem::is_regular_file(path))
|
|
151 boost::filesystem::remove(path);
|
|
152 else
|
|
153 throw PalantirException("The path is not a regular file: " + path);
|
|
154 }
|
|
155 }
|
|
156
|
|
157
|
|
158
|
|
159 void Toolbox::SplitUriComponents(UriComponents& components,
|
|
160 const std::string& uri)
|
|
161 {
|
|
162 static const char URI_SEPARATOR = '/';
|
|
163
|
|
164 components.clear();
|
|
165
|
|
166 if (uri.size() == 0 ||
|
|
167 uri[0] != URI_SEPARATOR)
|
|
168 {
|
|
169 throw PalantirException(ErrorCode_UriSyntax);
|
|
170 }
|
|
171
|
|
172 // Count the number of slashes in the URI to make an assumption
|
|
173 // about the number of components in the URI
|
|
174 unsigned int estimatedSize = 0;
|
|
175 for (unsigned int i = 0; i < uri.size(); i++)
|
|
176 {
|
|
177 if (uri[i] == URI_SEPARATOR)
|
|
178 estimatedSize++;
|
|
179 }
|
|
180
|
|
181 components.reserve(estimatedSize - 1);
|
|
182
|
|
183 unsigned int start = 1;
|
|
184 unsigned int end = 1;
|
|
185 while (end < uri.size())
|
|
186 {
|
|
187 // This is the loop invariant
|
|
188 assert(uri[start - 1] == '/' && (end >= start));
|
|
189
|
|
190 if (uri[end] == '/')
|
|
191 {
|
|
192 components.push_back(std::string(&uri[start], end - start));
|
|
193 end++;
|
|
194 start = end;
|
|
195 }
|
|
196 else
|
|
197 {
|
|
198 end++;
|
|
199 }
|
|
200 }
|
|
201
|
|
202 if (start < uri.size())
|
|
203 {
|
|
204 components.push_back(std::string(&uri[start], end - start));
|
|
205 }
|
|
206 }
|
|
207
|
|
208
|
|
209 bool Toolbox::IsChildUri(const UriComponents& baseUri,
|
|
210 const UriComponents& testedUri)
|
|
211 {
|
|
212 if (testedUri.size() < baseUri.size())
|
|
213 {
|
|
214 return false;
|
|
215 }
|
|
216
|
|
217 for (size_t i = 0; i < baseUri.size(); i++)
|
|
218 {
|
|
219 if (baseUri[i] != testedUri[i])
|
|
220 return false;
|
|
221 }
|
|
222
|
|
223 return true;
|
|
224 }
|
|
225
|
|
226
|
|
227 std::string Toolbox::AutodetectMimeType(const std::string& path)
|
|
228 {
|
|
229 std::string contentType;
|
|
230 size_t lastDot = path.rfind('.');
|
|
231 size_t lastSlash = path.rfind('/');
|
|
232
|
|
233 if (lastDot == std::string::npos ||
|
|
234 (lastSlash != std::string::npos && lastDot < lastSlash))
|
|
235 {
|
|
236 // No trailing dot, unable to detect the content type
|
|
237 }
|
|
238 else
|
|
239 {
|
|
240 const char* extension = &path[lastDot + 1];
|
|
241
|
|
242 // http://en.wikipedia.org/wiki/Mime_types
|
|
243 // Text types
|
|
244 if (!strcmp(extension, "txt"))
|
|
245 contentType = "text/plain";
|
|
246 else if (!strcmp(extension, "html"))
|
|
247 contentType = "text/html";
|
|
248 else if (!strcmp(extension, "xml"))
|
|
249 contentType = "text/xml";
|
|
250 else if (!strcmp(extension, "css"))
|
|
251 contentType = "text/css";
|
|
252
|
|
253 // Application types
|
|
254 else if (!strcmp(extension, "js"))
|
|
255 contentType = "application/javascript";
|
|
256 else if (!strcmp(extension, "json"))
|
|
257 contentType = "application/json";
|
|
258 else if (!strcmp(extension, "pdf"))
|
|
259 contentType = "application/pdf";
|
|
260
|
|
261 // Images types
|
|
262 else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg"))
|
|
263 contentType = "image/jpeg";
|
|
264 else if (!strcmp(extension, "gif"))
|
|
265 contentType = "image/gif";
|
|
266 else if (!strcmp(extension, "png"))
|
|
267 contentType = "image/png";
|
|
268 }
|
|
269
|
|
270 return contentType;
|
|
271 }
|
|
272
|
|
273
|
|
274 std::string Toolbox::FlattenUri(const UriComponents& components,
|
|
275 size_t fromLevel)
|
|
276 {
|
|
277 if (components.size() <= fromLevel)
|
|
278 {
|
|
279 return "/";
|
|
280 }
|
|
281 else
|
|
282 {
|
|
283 std::string r;
|
|
284
|
|
285 for (size_t i = fromLevel; i < components.size(); i++)
|
|
286 {
|
|
287 r += "/" + components[i];
|
|
288 }
|
|
289
|
|
290 return r;
|
|
291 }
|
|
292 }
|
|
293
|
|
294
|
|
295
|
|
296 uint64_t Toolbox::GetFileSize(const std::string& path)
|
|
297 {
|
|
298 try
|
|
299 {
|
|
300 return static_cast<uint64_t>(boost::filesystem::file_size(path));
|
|
301 }
|
|
302 catch (boost::filesystem::filesystem_error)
|
|
303 {
|
|
304 throw PalantirException(ErrorCode_InexistentFile);
|
|
305 }
|
|
306 }
|
22
|
307
|
|
308
|
|
309 static char GetHexadecimalCharacter(uint8_t value)
|
|
310 {
|
|
311 assert(value < 16);
|
|
312
|
|
313 if (value < 10)
|
|
314 return value + '0';
|
|
315 else
|
|
316 return (value - 10) + 'a';
|
|
317 }
|
|
318
|
23
|
319
|
22
|
320 void Toolbox::ComputeMD5(std::string& result,
|
|
321 const std::string& data)
|
|
322 {
|
|
323 md5_state_s state;
|
|
324 md5_init(&state);
|
|
325
|
|
326 if (data.size() > 0)
|
|
327 {
|
|
328 md5_append(&state, reinterpret_cast<const md5_byte_t*>(&data[0]),
|
|
329 static_cast<int>(data.size()));
|
|
330 }
|
|
331
|
|
332 md5_byte_t actualHash[16];
|
|
333 md5_finish(&state, actualHash);
|
|
334
|
|
335 result.resize(32);
|
|
336 for (unsigned int i = 0; i < 16; i++)
|
|
337 {
|
|
338 result[2 * i] = GetHexadecimalCharacter(actualHash[i] / 16);
|
|
339 result[2 * i + 1] = GetHexadecimalCharacter(actualHash[i] % 16);
|
|
340 }
|
|
341 }
|
0
|
342 }
|