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#include "web/http/MimeTypes.h"
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include "log/Logger.h"
25
26#include <filesystem>
27#include <utility>
28
29#endif /* DOXYGEN_SHOULD_SKIP_THIS */
30
31namespace web::http {
32
33#ifdef HAS_LIBMAGIC
34 magic_t MimeTypes::magic;
35#endif
36
37 std::map<std::string, std::string> MimeTypes::mimeType = {
38 {".dwg", "application/acad"},
39 {".asd", "application/astound"},
40 {".asn", "application/astound"},
41 {".tsp", "application/dsptype"},
42 {".dxf", "application/dxf"},
43 {".reg", "application/force-download"},
44 {".spl", "application/futuresplash"},
45 {".gz", "application/gzip"},
46 {".js", "application/javascript"},
47 {".json", "application/json"},
48 {".ptlk", "application/listenup"},
49 {".hqx", "application/mac-binhex40"},
50 {".mbd", "application/mbedlet"},
51 {".mif", "application/mif"},
52 {".xls", "application/msexcel"},
53 {".xla", "application/msexcel"},
54 {".hlp", "application/mshelp"},
55 {".chm", "application/mshelp"},
56 {".ppt", "application/mspowerpoint"},
57 {".ppz", "application/mspowerpoint"},
58 {".pps", "application/mspowerpoint"},
59 {".pot", "application/mspowerpoint"},
60 {".doc", "application/msword"},
61 {".dot", "application/msword"},
62 {".bin", "application/octet-stream"},
63 {".file", "application/octet-stream"},
64 {".com", "application/octet-stream"},
65 {".class", "application/octet-stream"},
66 {".ini", "application/octet-stream"},
67 {".oda", "application/oda"},
68 {".pdf", "application/pdf"},
69 {".ai", "application/postscript"},
70 {".eps", "application/postscript"},
71 {".ps", "application/postscript"},
72 {".rtc", "application/rtc"},
73 {".rtf", "application/rtf"},
74 {".smp", "application/studiom"},
75 {".tbk", "application/toolbook"},
76 {".vmd", "application/vocaltec-media-desc"},
77 {".vmf", "application/vocaltec-media-file"},
78 {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
79 {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
80 {".xml", "application/xml"},
81 {".bcpio", "application/x-bcpio"},
82 {".z", "application/x-compress"},
83 {".cpio", "application/x-cpio"},
84 {".csh", "application/x-csh"},
85 {".dcr", "application/x-director"},
86 {".dir", "application/x-director"},
87 {".dxr", "application/x-director"},
88 {".dvi", "application/x-dvi"},
89 {".evy", "application/x-envoy"},
90 {".gtar", "application/x-gtar"},
91 {".hdf", "application/x-hdf"},
92 {".php", "application/x-httpd-php"},
93 {".phtml", "application/x-httpd-php"},
94 {".latex", "application/x-latex"},
95 {".bin", "application/x-macbinary"},
96 {".mif", "application/x-mif"},
97 {".nc", "application/x-netcdf"},
98 {".cdf", "application/x-netcdf"},
99 {".nsc", "application/x-nschat"},
100 {".sh", "application/x-sh"},
101 {".shar", "application/x-shar"},
102 {".swf", "application/x-shockwave-flash"},
103 {".cab", "application/x-shockwave-flash"},
104 {".spr", "application/x-sprite"},
105 {".sprite", "application/x-sprite"},
106 {".sit", "application/x-stuffit"},
107 {".sca", "application/x-supercard"},
108 {".sv4cpio", "application/x-sv4cpio"},
109 {".sv4crc", "application/x-sv4crc"},
110 {".tar", "application/x-tar"},
111 {".tcl", "application/x-tcl"},
112 {".tex", "application/x-tex"},
113 {".texinfo", "application/x-texinfo"},
114 {".texi", "application/x-texinfo"},
115 {".t", "application/x-troff"},
116 {".tr", "application/x-troff"},
117 {".roff", "application/x-troff"},
118 {".man", "application/x-troff-man"},
119 {".troff", "application/x-troff-man"},
120 {".me", "application/x-troff-me"},
121 {".troff", "application/x-troff-me"},
122 {".me", "application/x-troff-ms"},
123 {".troff", "application/x-troff-ms"},
124 {".ustar", "application/x-ustar"},
125 {".src", "application/x-wais-source"},
126 {".zip", "application/zip"},
127 {".au", "audio/basic"},
128 {".snd", "audio/basic"},
129 {".es", "audio/echospeech"},
130 {".mp3", "audio/mpeg"},
131 {".mp4", "audio/mp4"},
132 {".ogg", "audio/ogg"},
133 {".tsi", "audio/tsplayer"},
134 {".vox", "audio/voxware"},
135 {".wav", "audio/wav"},
136 {".aif", "audio/x-aiff"},
137 {".aiff", "audio/x-aiff"},
138 {".aifc", "audio/x-aiff"},
139 {".dus", "audio/x-dspeeh"},
140 {".cht", "audio/x-dspeeh"},
141 {".mid", "audio/x-midi"},
142 {".midi", "audio/x-midi"},
143 {".mp2", "audio/x-mpeg"},
144 {".ram", "audio/x-pn-realaudio"},
145 {".ra", "audio/x-pn-realaudio"},
146 {".rpm", "audio/x-pn-realaudio-plugin"},
147 {".stream", "audio/x-qt-stream"},
148 {".dwf", "drawing/x-dwf"},
149 {".bmp", "image/bmp"},
150 {".bmp", "image/x-bmp"},
151 {".bmp", "image/x-ms-bmp"},
152 {".cod", "image/cis-cod"},
153 {".ras", "image/cmu-raster"},
154 {".fif", "image/fif"},
155 {".gif", "image/gif"},
156 {".ief", "image/ief"},
157 {".jpeg", "image/jpeg"},
158 {".jpg", "image/jpeg"},
159 {".jpe", "image/jpeg"},
160 {".png", "image/png"},
161 {".svg", "image/svg+xml"},
162 {".tiff", "image/tiff"},
163 {".tif", "image/tiff"},
164 {".mcf", "image/vasa"},
165 {".wbmp", "image/vnd.wap.wbmp"},
166 {".fh4", "image/x-freehand"},
167 {".fh5", "image/x-freehand"},
168 {".fhc", "image/x-freehand"},
169 {".ico", "image/x-icon"},
170 {".pnm", "image/x-portable-anymap"},
171 {".pbm", "image/x-portable-bitmap"},
172 {".pgm", "image/x-portable-graymap"},
173 {".ppm", "image/x-portable-pixmap"},
174 {".rgb", "image/x-rgb"},
175 {".xwd", "image/x-windowdump"},
176 {".xbm", "image/x-xbitmap"},
177 {".xpm", "image/x-xpixmap"},
178 {".wrl", "model/vrml"},
179 {".ics", "text/calendar"},
180 {".csv", "text/comma-separated-values"},
181 {".css", "text/css"},
182 {".htm", "text/html"},
183 {".html", "text/html"},
184 {".shtml", "text/html"},
185 {".xhtml", "text/html"},
186 {".js", "text/javascript"},
187 {".txt", "text/plain"},
188 {".rtx", "text/richtext"},
189 {".rtf", "text/rtf"},
190 {".tsv", "text/tab-separated-values"},
191 {".wml", "text/vnd.wap.wml"},
192 {".wmlc", "application/vnd.wap.wmlc"},
193 {".wmls", "text/vnd.wap.wmlscript"},
194 {".wmlsc", "application/vnd.wap.wmlscriptc"},
195 {".xml", "text/xml"},
196 {".etx", "text/x-setext"},
197 {".sgm", "text/x-sgml"},
198 {".sgml", "text/x-sgml"},
199 {".talk", "text/x-speech"},
200 {".spc", "text/x-speech"},
201 {".mpeg", "video/mpeg"},
202 {".mpg", "video/mpeg"},
203 {".mpe", "video/mpeg"},
204 {".mp4", "video/mp4"},
205 {".ogg", "video/ogg"},
206 {".ogv", "video/ogg"},
207 {".qt", "video/quicktime"},
208 {".mov", "video/quicktime"},
209 {".viv", "video/vnd.vivo"},
210 {".vivo", "video/vnd.vivo"},
211 {".webm", "video/webm"},
212 {".avi", "video/x-msvideo"},
213 {".movie", "video/x-sgi-movie"},
214 {".vts", "workbook/formulaone"},
215 {".vtts", "workbook/formulaone"},
216 {".3dmf", "x-world/x-3dmf"},
217 {".3dm", "x-world/x-3dmf"},
218 {".qd3d", "x-world/x-3dmf"},
219 {".qd3", "x-world/x-3dmf"},
220 {".wrl", "x-world/x-vrml"}};
221
222 MimeTypes::MimeTypes() {
223#ifdef HAS_LIBMAGIC
224 MimeTypes::magic = magic_open(MAGIC_MIME);
225
226 if (magic_load(magic, nullptr) != 0) {
227 LOG(DEBUG) << "HTTP: Cannot load magic database - " + std::string(magic_error(magic));
228 magic_close(magic);
229 magic = nullptr;
230 }
231#endif
232 }
233
234 MimeTypes::~MimeTypes() {
235#ifdef HAS_LIBMAGIC
236 magic_close(MimeTypes::magic);
237#endif
238 }
239
240 std::string MimeTypes::contentType(const std::string& file) {
241 const std::map<std::string, std::string>::iterator it = mimeType.find(std::filesystem::path(file).extension());
242
243 std::string type;
244
245 if (it != mimeType.end()) {
246 type = it->second;
247 }
248#ifdef HAS_LIBMAGIC
249 else if (magic != nullptr) {
250 const char* magicType = magic_file(MimeTypes::magic, file.c_str());
251 type = magicType != nullptr ? magicType : std::string();
252 }
253#endif
254
255 return !type.empty() ? type : "application/octet-stream";
256 }
257
258} // namespace web::http