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 const std::size_t& size = std::filesystem::file_size(path);
41 std::string content(size, '\0');
42 file.read(content.data(), size);
43
44 // Close the file
45 file.close();
46
47 return content;
48 }
49
56 [[nodiscard]]
57 static
58 inline
59 uint8_t
60 hex2dec(uint8_t c)
61 {
62 if (c >= '0' && c <= '9')
63 c -= '0';
64
65 else if (c >= 'a' && c <= 'f')
66 c -= 'a' - 10;
67
68 else if (c >= 'A' && c <= 'F')
69 c -= 'A' - 10;
70
71 return c;
72 }
73
85 [[nodiscard]]
86 inline
87 std::vector<std::string_view>
88 split(std::string_view str, std::string_view delimiter)
89 {
90 // Sanity check str
91 if (str.empty())
92 return { };
93
94 // Sanity check delimiter
95 if (delimiter.empty())
96 return { str };
97
98 // Split
99 std::vector<std::string_view> parts;
100 std::string_view::size_type pos = 0;
101 while (pos != std::string_view::npos) {
102 // Look for substring
103 const auto pos_found = str.find(delimiter, pos);
104
105 // Drop leading delimiters
106 if (pos_found == 0) {
107 pos += delimiter.size();
108 continue;
109 }
110
111 // Capture string
112 parts.emplace_back(str.substr(pos, pos_found-pos));
113
114 // Drop trailing delimiters
115 if (pos_found + delimiter.size() >= str.size())
116 break;
117
118 // Move on
119 if (pos_found == std::string_view::npos)
120 break;
121 pos = pos_found + delimiter.size();
122 }
123
124 return parts;
125 }
126
138 static
139 inline
140 void
141 url_decode(std::string& str)
142 {
143 size_t w = 0;
144 for (size_t r = 0 ; r < str.size() ; ++r) {
145 uint8_t v = str[r];
146 if (str[r] == '%') {
147 v = hex2dec(str[++r]) << 4;
148 v |= hex2dec(str[++r]);
149 }
150 str[w++] = v;
151 }
152 str.resize(w);
153 }
154
161 // ToDo: This is still a left-over from the very first day that malloy was started as a PoC. This needs some
162 // serious overhaul. See issue #4.
163 [[nodiscard]]
164 static
165 inline
166 std::string_view
167 mime_type(const std::filesystem::path& path)
168 {
169 // Extract file extension
170 const std::filesystem::path& ext = path.extension();
171
172 if (ext == ".7z") return "application/x-7z-compressed";
173 if (ext == ".bin") return "application/octet-stream";
174 if (ext == ".bmp") return "image/bmp";
175 if (ext == ".bz") return "application/x-bzip";
176 if (ext == ".bz2") return "application/x-bzip2";
177 if (ext == ".css") return "text/css";
178 if (ext == ".csv") return "text/csv";
179 if (ext == ".gif") return "image/gif";
180 if (ext == ".gz") return "application/gzip";
181 if (ext == ".ico") return "image/vnd.microsoft.icon";
182 if (ext == ".htm") return "text/html";
183 if (ext == ".html") return "text/html";
184 if (ext == ".mp3") return "audio/mpeg";
185 if (ext == ".mp4") return "video/mp4";
186 if (ext == ".mpeg") return "video/mpeg";
187 if (ext == ".rar") return "application/vnd.rar";
188 if (ext == ".php") return "text/html";
189 if (ext == ".txt") return "text/plain";
190 if (ext == ".js") return "application/javascript";
191 if (ext == ".json") return "application/json";
192 if (ext == ".xml") return "application/xml";
193 if (ext == ".swf") return "application/x-shockwave-flash";
194 if (ext == ".flv") return "video/x-flv";
195 if (ext == ".png") return "image/png";
196 if (ext == ".jpe") return "image/jpeg";
197 if (ext == ".jpeg") return "image/jpeg";
198 if (ext == ".jpg") return "image/jpeg";
199 if (ext == ".tiff") return "image/tiff";
200 if (ext == ".tif") return "image/tiff";
201 if (ext == ".svg") return "image/svg+xml";
202 if (ext == ".svgz") return "image/svg+xml";
203 if (ext == ".zip") return "application/zip";
204
205 // Recommended fall-back
206 return "application/octet-stream";
207 }
208
209}
Definition: controller.hpp:31
std::vector< std::string_view > split(std::string_view str, std::string_view delimiter)
Definition: utils.hpp:88