/src/wt/src/web/UriUtils.C
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2012 Emweb bv, Herent, Belgium. |
3 | | * |
4 | | * See the LICENSE file for terms of use. |
5 | | */ |
6 | | |
7 | | #include "UriUtils.h" |
8 | | #include "Wt/Utils.h" |
9 | | |
10 | | #include "Wt/WException.h" |
11 | | |
12 | | #include <boost/algorithm/string.hpp> |
13 | | |
14 | | namespace Wt { |
15 | | |
16 | | DataUri::DataUri(const std::string& uriString) |
17 | 0 | { |
18 | 0 | parse(uriString); |
19 | 0 | } |
20 | | |
21 | | bool DataUri::isDataUri(const std::string& uriString) |
22 | 0 | { |
23 | 0 | return boost::starts_with(uriString, "data:"); |
24 | 0 | } |
25 | | |
26 | | void DataUri::parse(const std::string& uriString) |
27 | 0 | { |
28 | 0 | std::size_t dataEndPos = uriString.find("data:") + 5; |
29 | 0 | std::size_t commaPos = uriString.find(","); |
30 | 0 | if (commaPos == std::string::npos) |
31 | 0 | commaPos = dataEndPos; |
32 | |
|
33 | 0 | mimeType = uriString.substr(dataEndPos, commaPos - dataEndPos); |
34 | |
|
35 | 0 | std::string d = uriString.substr(commaPos + 1); |
36 | |
|
37 | 0 | #ifndef WT_TARGET_JAVA |
38 | 0 | d = Utils::base64Decode(d); |
39 | 0 | data = std::vector<unsigned char>(d.begin(), d.end()); |
40 | | #else |
41 | | data = Utils::base64Decode(d); |
42 | | #endif // WT_TARGET_JAVA |
43 | |
|
44 | 0 | if (!boost::ends_with(mimeType, ";base64") || data.empty()) |
45 | 0 | throw WException("Ill formed data URI: " + uriString); |
46 | 0 | else |
47 | 0 | mimeType = mimeType.substr(0, mimeType.find(";")); |
48 | 0 | } |
49 | | } |