Malloy
Loading...
Searching...
No Matches
utils.hpp
1#pragma once
2
3#include <boost/asio/buffer.hpp>
4#include <boost/beast/core/bind_handler.hpp>
5#include <boost/beast/core/buffers_to_string.hpp>
6
7#include <filesystem>
8#include <fstream>
9
10namespace malloy
11{
12 using boost::asio::buffer;
13 using boost::beast::buffers_to_string;
14 using boost::beast::bind_front_handler;
15
22 [[nodiscard]]
23 static
24 inline
25 std::string
26 file_contents(const std::filesystem::path& path)
27 {
28 // Sanity check
29 if (!std::filesystem::is_regular_file(path))
30 return { };
31
32 // Open the file
33 // Note that we have to use binary mode as we want to return a string
34 // representing matching the bytes of the file on the file system.
35 std::ifstream file(path, std::ios::in | std::ios::binary);
36 if (!file.is_open())
37 return { };
38
39 // Read contents
40 std::string content{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
41
42 // Close the file
43 file.close();
44
45 return content;
46 }
47
54 [[nodiscard]]
55 static
56 inline
57 std::uint8_t
58 hex2dec(std::uint8_t c)
59 {
60 if (c >= '0' && c <= '9')
61 c -= '0';
62
63 else if (c >= 'a' && c <= 'f')
64 c -= 'a' - 10;
65
66 else if (c >= 'A' && c <= 'F')
67 c -= 'A' - 10;
68
69 return c;
70 }
71
83 [[nodiscard]]
84 inline
85 std::vector<std::string_view>
86 split(std::string_view str, std::string_view delimiter)
87 {
88 // Sanity check str
89 if (str.empty())
90 return { };
91
92 // Sanity check delimiter
93 if (delimiter.empty())
94 return { str };
95
96 // Split
97 std::vector<std::string_view> parts;
98 std::string_view::size_type pos = 0;
99 while (pos != std::string_view::npos) {
100 // Look for substring
101 const auto pos_found = str.find(delimiter, pos);
102
103 // Drop leading delimiters
104 if (pos_found == 0) {
105 pos += delimiter.size();
106 continue;
107 }
108
109 // Capture string
110 parts.emplace_back(str.substr(pos, pos_found-pos));
111
112 // Drop trailing delimiters
113 if (pos_found + delimiter.size() >= str.size())
114 break;
115
116 // Move on
117 if (pos_found == std::string_view::npos)
118 break;
119 pos = pos_found + delimiter.size();
120 }
121
122 return parts;
123 }
124
136 static
137 inline
138 void
139 url_decode(std::string& str)
140 {
141 size_t w = 0;
142 for (size_t r = 0 ; r < str.size() ; ++r) {
143 uint8_t v = str[r];
144 if (str[r] == '%') {
145 v = hex2dec(str[++r]) << 4;
146 v |= hex2dec(str[++r]);
147 }
148 str[w++] = v;
149 }
150 str.resize(w);
151 }
152
159 // ToDo: This is still a left-over from the very first day that malloy was started as a PoC. This needs some
160 // serious overhaul. See issue #4.
161 [[nodiscard]]
162 static
163 inline
164 std::string_view
165 mime_type(const std::filesystem::path& path)
166 {
167 // Extract file extension
168 const std::filesystem::path& ext = path.extension();
169
170 if (ext == ".7z") return "application/x-7z-compressed";
171 if (ext == ".bin") return "application/octet-stream";
172 if (ext == ".bmp") return "image/bmp";
173 if (ext == ".bz") return "application/x-bzip";
174 if (ext == ".bz2") return "application/x-bzip2";
175 if (ext == ".css") return "text/css";
176 if (ext == ".css.min") return "text/css";
177 if (ext == ".csv") return "text/csv";
178 if (ext == ".gif") return "image/gif";
179 if (ext == ".gz") return "application/gzip";
180 if (ext == ".ico") return "image/vnd.microsoft.icon";
181 if (ext == ".htm") return "text/html";
182 if (ext == ".html") return "text/html";
183 if (ext == ".mp3") return "audio/mpeg";
184 if (ext == ".mp4") return "video/mp4";
185 if (ext == ".mpeg") return "video/mpeg";
186 if (ext == ".rar") return "application/vnd.rar";
187 if (ext == ".php") return "text/html";
188 if (ext == ".txt") return "text/plain";
189 if (ext == ".js") return "application/javascript";
190 if (ext == ".json") return "application/json";
191 if (ext == ".xml") return "application/xml";
192 if (ext == ".swf") return "application/x-shockwave-flash";
193 if (ext == ".flv") return "video/x-flv";
194 if (ext == ".png") return "image/png";
195 if (ext == ".jpe") return "image/jpeg";
196 if (ext == ".jpeg") return "image/jpeg";
197 if (ext == ".jpg") return "image/jpeg";
198 if (ext == ".tiff") return "image/tiff";
199 if (ext == ".tif") return "image/tiff";
200 if (ext == ".svg") return "image/svg+xml";
201 if (ext == ".svgz") return "image/svg+xml";
202 if (ext == ".zip") return "application/zip";
203
204 // Recommended fall-back
205 return "application/octet-stream";
206 }
207
208}
Definition: controller.hpp:31
std::vector< std::string_view > split(std::string_view str, std::string_view delimiter)
Definition: utils.hpp:86