SNode.C
Loading...
Searching...
No Matches
MimeTypes.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "web/http/MimeTypes.h"
43
44#ifndef DOXYGEN_SHOULD_SKIP_THIS
45
46#include "log/Logger.h"
47
48#include <filesystem>
49#include <utility>
50
51#endif /* DOXYGEN_SHOULD_SKIP_THIS */
52
53namespace web::http {
54
55#ifdef HAS_LIBMAGIC
56 magic_t MimeTypes::magic;
57#endif
58
59 std::map<std::string, std::string> MimeTypes::mimeType = {
60 {".dwg", "application/acad"},
61 {".asd", "application/astound"},
62 {".asn", "application/astound"},
63 {".tsp", "application/dsptype"},
64 {".dxf", "application/dxf"},
65 {".reg", "application/force-download"},
66 {".spl", "application/futuresplash"},
67 {".gz", "application/gzip"},
68 {".js", "application/javascript"},
69 {".json", "application/json"},
70 {".ptlk", "application/listenup"},
71 {".hqx", "application/mac-binhex40"},
72 {".mbd", "application/mbedlet"},
73 {".mif", "application/mif"},
74 {".xls", "application/msexcel"},
75 {".xla", "application/msexcel"},
76 {".hlp", "application/mshelp"},
77 {".chm", "application/mshelp"},
78 {".ppt", "application/mspowerpoint"},
79 {".ppz", "application/mspowerpoint"},
80 {".pps", "application/mspowerpoint"},
81 {".pot", "application/mspowerpoint"},
82 {".doc", "application/msword"},
83 {".dot", "application/msword"},
84 {".bin", "application/octet-stream"},
85 {".file", "application/octet-stream"},
86 {".com", "application/octet-stream"},
87 {".class", "application/octet-stream"},
88 {".ini", "application/octet-stream"},
89 {".oda", "application/oda"},
90 {".pdf", "application/pdf"},
91 {".ai", "application/postscript"},
92 {".eps", "application/postscript"},
93 {".ps", "application/postscript"},
94 {".rtc", "application/rtc"},
95 {".rtf", "application/rtf"},
96 {".smp", "application/studiom"},
97 {".tbk", "application/toolbook"},
98 {".vmd", "application/vocaltec-media-desc"},
99 {".vmf", "application/vocaltec-media-file"},
100 {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
101 {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
102 {".xml", "application/xml"},
103 {".bcpio", "application/x-bcpio"},
104 {".z", "application/x-compress"},
105 {".cpio", "application/x-cpio"},
106 {".csh", "application/x-csh"},
107 {".dcr", "application/x-director"},
108 {".dir", "application/x-director"},
109 {".dxr", "application/x-director"},
110 {".dvi", "application/x-dvi"},
111 {".evy", "application/x-envoy"},
112 {".gtar", "application/x-gtar"},
113 {".hdf", "application/x-hdf"},
114 {".php", "application/x-httpd-php"},
115 {".phtml", "application/x-httpd-php"},
116 {".latex", "application/x-latex"},
117 {".bin", "application/x-macbinary"},
118 {".mif", "application/x-mif"},
119 {".nc", "application/x-netcdf"},
120 {".cdf", "application/x-netcdf"},
121 {".nsc", "application/x-nschat"},
122 {".sh", "application/x-sh"},
123 {".shar", "application/x-shar"},
124 {".swf", "application/x-shockwave-flash"},
125 {".cab", "application/x-shockwave-flash"},
126 {".spr", "application/x-sprite"},
127 {".sprite", "application/x-sprite"},
128 {".sit", "application/x-stuffit"},
129 {".sca", "application/x-supercard"},
130 {".sv4cpio", "application/x-sv4cpio"},
131 {".sv4crc", "application/x-sv4crc"},
132 {".tar", "application/x-tar"},
133 {".tcl", "application/x-tcl"},
134 {".tex", "application/x-tex"},
135 {".texinfo", "application/x-texinfo"},
136 {".texi", "application/x-texinfo"},
137 {".t", "application/x-troff"},
138 {".tr", "application/x-troff"},
139 {".roff", "application/x-troff"},
140 {".man", "application/x-troff-man"},
141 {".troff", "application/x-troff-man"},
142 {".me", "application/x-troff-me"},
143 {".troff", "application/x-troff-me"},
144 {".me", "application/x-troff-ms"},
145 {".troff", "application/x-troff-ms"},
146 {".ustar", "application/x-ustar"},
147 {".src", "application/x-wais-source"},
148 {".zip", "application/zip"},
149 {".au", "audio/basic"},
150 {".snd", "audio/basic"},
151 {".es", "audio/echospeech"},
152 {".mp3", "audio/mpeg"},
153 {".mp4", "audio/mp4"},
154 {".ogg", "audio/ogg"},
155 {".tsi", "audio/tsplayer"},
156 {".vox", "audio/voxware"},
157 {".wav", "audio/wav"},
158 {".aif", "audio/x-aiff"},
159 {".aiff", "audio/x-aiff"},
160 {".aifc", "audio/x-aiff"},
161 {".dus", "audio/x-dspeeh"},
162 {".cht", "audio/x-dspeeh"},
163 {".mid", "audio/x-midi"},
164 {".midi", "audio/x-midi"},
165 {".mp2", "audio/x-mpeg"},
166 {".ram", "audio/x-pn-realaudio"},
167 {".ra", "audio/x-pn-realaudio"},
168 {".rpm", "audio/x-pn-realaudio-plugin"},
169 {".stream", "audio/x-qt-stream"},
170 {".dwf", "drawing/x-dwf"},
171 {".bmp", "image/bmp"},
172 {".bmp", "image/x-bmp"},
173 {".bmp", "image/x-ms-bmp"},
174 {".cod", "image/cis-cod"},
175 {".ras", "image/cmu-raster"},
176 {".fif", "image/fif"},
177 {".gif", "image/gif"},
178 {".ief", "image/ief"},
179 {".jpeg", "image/jpeg"},
180 {".jpg", "image/jpeg"},
181 {".jpe", "image/jpeg"},
182 {".png", "image/png"},
183 {".svg", "image/svg+xml"},
184 {".tiff", "image/tiff"},
185 {".tif", "image/tiff"},
186 {".mcf", "image/vasa"},
187 {".wbmp", "image/vnd.wap.wbmp"},
188 {".fh4", "image/x-freehand"},
189 {".fh5", "image/x-freehand"},
190 {".fhc", "image/x-freehand"},
191 {".ico", "image/x-icon"},
192 {".pnm", "image/x-portable-anymap"},
193 {".pbm", "image/x-portable-bitmap"},
194 {".pgm", "image/x-portable-graymap"},
195 {".ppm", "image/x-portable-pixmap"},
196 {".rgb", "image/x-rgb"},
197 {".xwd", "image/x-windowdump"},
198 {".xbm", "image/x-xbitmap"},
199 {".xpm", "image/x-xpixmap"},
200 {".wrl", "model/vrml"},
201 {".ics", "text/calendar"},
202 {".csv", "text/comma-separated-values"},
203 {".css", "text/css"},
204 {".htm", "text/html"},
205 {".html", "text/html"},
206 {".shtml", "text/html"},
207 {".xhtml", "text/html"},
208 {".js", "text/javascript"},
209 {".txt", "text/plain"},
210 {".rtx", "text/richtext"},
211 {".rtf", "text/rtf"},
212 {".tsv", "text/tab-separated-values"},
213 {".wml", "text/vnd.wap.wml"},
214 {".wmlc", "application/vnd.wap.wmlc"},
215 {".wmls", "text/vnd.wap.wmlscript"},
216 {".wmlsc", "application/vnd.wap.wmlscriptc"},
217 {".xml", "text/xml"},
218 {".etx", "text/x-setext"},
219 {".sgm", "text/x-sgml"},
220 {".sgml", "text/x-sgml"},
221 {".talk", "text/x-speech"},
222 {".spc", "text/x-speech"},
223 {".mpeg", "video/mpeg"},
224 {".mpg", "video/mpeg"},
225 {".mpe", "video/mpeg"},
226 {".mp4", "video/mp4"},
227 {".ogg", "video/ogg"},
228 {".ogv", "video/ogg"},
229 {".qt", "video/quicktime"},
230 {".mov", "video/quicktime"},
231 {".viv", "video/vnd.vivo"},
232 {".vivo", "video/vnd.vivo"},
233 {".webm", "video/webm"},
234 {".avi", "video/x-msvideo"},
235 {".movie", "video/x-sgi-movie"},
236 {".vts", "workbook/formulaone"},
237 {".vtts", "workbook/formulaone"},
238 {".3dmf", "x-world/x-3dmf"},
239 {".3dm", "x-world/x-3dmf"},
240 {".qd3d", "x-world/x-3dmf"},
241 {".qd3", "x-world/x-3dmf"},
242 {".wrl", "x-world/x-vrml"}};
243
245#ifdef HAS_LIBMAGIC
246 MimeTypes::magic = magic_open(MAGIC_MIME);
247
248 if (magic_load(magic, nullptr) != 0) {
249 LOG(DEBUG) << "HTTP: Cannot load magic database - " + std::string(magic_error(magic));
250 magic_close(magic);
251 magic = nullptr;
252 }
253#endif
254 }
255
257#ifdef HAS_LIBMAGIC
258 magic_close(MimeTypes::magic);
259#endif
260 }
261
262 std::string MimeTypes::contentType(const std::string& file) {
263 const std::map<std::string, std::string>::iterator it = mimeType.find(std::filesystem::path(file).extension());
264
265 std::string type;
266
267 if (it != mimeType.end()) {
268 type = it->second;
269 }
270#ifdef HAS_LIBMAGIC
271 else if (magic != nullptr) {
272 const char* magicType = magic_file(MimeTypes::magic, file.c_str());
273 type = magicType != nullptr ? magicType : std::string();
274 }
275#endif
276
277 return !type.empty() ? type : "application/octet-stream";
278 }
279
280} // namespace web::http
static std::string contentType(const std::string &file)
static std::map< std::string, std::string > mimeType
Definition MimeTypes.h:76