/src/httrack/src/htsmime.c
Line | Count | Source |
1 | | /* ------------------------------------------------------------ */ |
2 | | /* |
3 | | HTTrack Website Copier, Offline Browser for Windows and Unix |
4 | | Copyright (C) 2026 Xavier Roche and other contributors |
5 | | |
6 | | SPDX-License-Identifier: GPL-3.0-or-later |
7 | | |
8 | | This program is free software: you can redistribute it and/or modify |
9 | | it under the terms of the GNU General Public License as published by |
10 | | the Free Software Foundation, either version 3 of the License, or |
11 | | (at your option) any later version. |
12 | | |
13 | | This program is distributed in the hope that it will be useful, |
14 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | GNU General Public License for more details. |
17 | | |
18 | | You should have received a copy of the GNU General Public License |
19 | | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | | |
21 | | Ethical use: we kindly ask that you NOT use this software to harvest email |
22 | | addresses or to collect any other private information about people. Doing so |
23 | | would dishonor our work and waste the many hours we have spent on it. |
24 | | |
25 | | Please visit our Website: http://www.httrack.com |
26 | | */ |
27 | | |
28 | | /* ------------------------------------------------------------ */ |
29 | | /* File: htsmime.c subroutines: */ |
30 | | /* MIME type tables and extension lookups */ |
31 | | /* Author: Xavier Roche */ |
32 | | /* ------------------------------------------------------------ */ |
33 | | |
34 | | #include "htscore.h" |
35 | | #include "htsbase.h" |
36 | | |
37 | | /* Mime types kept as sent, since they often mean "unknown". */ |
38 | | static const char *const hts_mime_keep[] = {"application/octet-stream", |
39 | | "text/plain", "application/xml", |
40 | | "text/xml", ""}; |
41 | | |
42 | | /* bogus servers returns these mime types when the extension is seen within the |
43 | | * filename */ |
44 | | static const char *const hts_mime_bogus_multiple[] = { |
45 | | "application/x-wais-source", /* src (src.rpm) */ |
46 | | ""}; |
47 | | |
48 | | /* pas de type mime connu, mais extension connue */ |
49 | | static const char *const hts_ext_dynamic[] = { |
50 | | HTS_SPAN_WORDS("php3", "php", "php4", "php2", "cgi", "asp", "jsp", "pl", |
51 | | "cfm", "nsf" /* lotus */)}; |
52 | | |
53 | | /* types MIME |
54 | | note: application/octet-stream should not be used here |
55 | | */ |
56 | | static const char *const hts_mime[][2] = { |
57 | | {"application/acad", "dwg"}, |
58 | | {"application/arj", "arj"}, |
59 | | {"application/clariscad", "ccad"}, |
60 | | {"application/drafting", "drw"}, |
61 | | {"application/dxf", "dxf"}, |
62 | | {"application/excel", "xls"}, |
63 | | {"application/i-deas", "unv"}, |
64 | | {"application/iges", "isg"}, |
65 | | {"application/iges", "iges"}, |
66 | | {"application/mac-binhex40", "hqx"}, |
67 | | {"application/mac-compactpro", "cpt"}, |
68 | | {"application/msword", "doc"}, |
69 | | {"application/msword", "w6w"}, |
70 | | {"application/msword", "word"}, |
71 | | {"application/mswrite", "wri"}, |
72 | | /*{"application/octet-stream","dms"}, */ |
73 | | /*{"application/octet-stream","lzh"}, */ |
74 | | /*{"application/octet-stream","lha"}, */ |
75 | | /*{"application/octet-stream","bin"}, */ |
76 | | {"application/oda", "oda"}, |
77 | | {"application/pdf", "pdf"}, |
78 | | {"application/postscript", "ps"}, |
79 | | {"application/postscript", "ai"}, |
80 | | {"application/postscript", "eps"}, |
81 | | {"application/powerpoint", "ppt"}, |
82 | | {"application/pro_eng", "prt"}, |
83 | | {"application/pro_eng", "part"}, |
84 | | {"application/rtf", "rtf"}, |
85 | | {"application/set", "set"}, |
86 | | {"application/sla", "stl"}, |
87 | | {"application/smil", "smi"}, |
88 | | {"application/smil", "smil"}, |
89 | | {"application/smil", "sml"}, |
90 | | {"application/solids", "sol"}, |
91 | | {"application/STEP", "stp"}, |
92 | | {"application/STEP", "step"}, |
93 | | {"application/vda", "vda"}, |
94 | | {"application/x-authorware-map", "aam"}, |
95 | | {"application/x-authorware-seg", "aas"}, |
96 | | {"application/x-authorware-bin", "aab"}, |
97 | | {"application/x-bzip2", "bz2"}, |
98 | | {"application/x-cocoa", "cco"}, |
99 | | {"application/x-csh", "csh"}, |
100 | | {"application/x-director", "dir"}, |
101 | | {"application/x-director", "dcr"}, |
102 | | {"application/x-director", "dxr"}, |
103 | | {"application/x-mif", "mif"}, |
104 | | {"application/x-dvi", "dvi"}, |
105 | | {"application/x-gzip", "gz"}, |
106 | | {"application/x-gzip", "gzip"}, |
107 | | {"application/x-hdf", "hdf"}, |
108 | | {"application/x-javascript", "js"}, |
109 | | {"application/x-koan", "skp"}, |
110 | | {"application/x-koan", "skd"}, |
111 | | {"application/x-koan", "skt"}, |
112 | | {"application/x-koan", "skm"}, |
113 | | {"application/x-latex", "latex"}, |
114 | | {"application/x-netcdf", "nc"}, |
115 | | {"application/x-netcdf", "cdf"}, |
116 | | /* {"application/x-sh","sh"}, */ |
117 | | /* {"application/x-csh","csh"}, */ |
118 | | /* {"application/x-ksh","ksh"}, */ |
119 | | {"application/x-shar", "shar"}, |
120 | | {"application/x-stuffit", "sit"}, |
121 | | {"application/x-tcl", "tcl"}, |
122 | | {"application/x-tex", "tex"}, |
123 | | {"application/x-texinfo", "texinfo"}, |
124 | | {"application/x-texinfo", "texi"}, |
125 | | {"application/x-troff", "t"}, |
126 | | {"application/x-troff", "tr"}, |
127 | | {"application/x-troff", "roff"}, |
128 | | {"application/x-troff-man", "man"}, |
129 | | {"application/x-troff-me", "ms"}, |
130 | | {"application/x-wais-source", "src"}, |
131 | | {"application/zip", "zip"}, |
132 | | {"application/x-zip-compressed", "zip"}, |
133 | | {"application/x-bcpio", "bcpio"}, |
134 | | {"application/x-cdlink", "vcd"}, |
135 | | {"application/x-cpio", "cpio"}, |
136 | | {"application/x-gtar", "tgz"}, |
137 | | {"application/x-gtar", "gtar"}, |
138 | | {"application/x-shar", "shar"}, |
139 | | {"application/x-shockwave-flash", "swf"}, |
140 | | {"application/x-sv4cpio", "sv4cpio"}, |
141 | | {"application/x-sv4crc", "sv4crc"}, |
142 | | {"application/x-tar", "tar"}, |
143 | | {"application/x-ustar", "ustar"}, |
144 | | {"application/x-winhelp", "hlp"}, |
145 | | {"application/xml", "xml"}, |
146 | | {"audio/midi", "mid"}, |
147 | | {"audio/midi", "midi"}, |
148 | | {"audio/midi", "kar"}, |
149 | | {"audio/mpeg", "mp3"}, |
150 | | {"audio/mpeg", "mpga"}, |
151 | | {"audio/mpeg", "mp2"}, |
152 | | {"audio/basic", "au"}, |
153 | | {"audio/basic", "snd"}, |
154 | | {"audio/x-aiff", "aif"}, |
155 | | {"audio/x-aiff", "aiff"}, |
156 | | {"audio/x-aiff", "aifc"}, |
157 | | {"audio/x-pn-realaudio", "rm"}, |
158 | | {"audio/x-pn-realaudio", "ram"}, |
159 | | {"audio/x-pn-realaudio", "ra"}, |
160 | | {"audio/x-pn-realaudio-plugin", "rpm"}, |
161 | | {"audio/x-wav", "wav"}, |
162 | | {"chemical/x-pdb", "pdb"}, |
163 | | {"chemical/x-pdb", "xyz"}, |
164 | | {"drawing/x-dwf", "dwf"}, |
165 | | {"image/gif", "gif"}, |
166 | | {"image/ief", "ief"}, |
167 | | {"image/jpeg", "jpg"}, |
168 | | {"image/jpeg", "jpe"}, |
169 | | {"image/jpeg", "jpeg"}, |
170 | | {"image/pict", "pict"}, |
171 | | {"image/png", "png"}, |
172 | | {"image/tiff", "tiff"}, |
173 | | {"image/tiff", "tif"}, |
174 | | {"image/svg+xml", "svg"}, |
175 | | {"image/svg-xml", "svg"}, |
176 | | {"image/x-cmu-raster", "ras"}, |
177 | | {"image/x-freehand", "fh4"}, |
178 | | {"image/x-freehand", "fh7"}, |
179 | | {"image/x-freehand", "fh5"}, |
180 | | {"image/x-freehand", "fhc"}, |
181 | | {"image/x-freehand", "fh"}, |
182 | | {"image/x-portable-anymap", "pnm"}, |
183 | | {"image/x-portable-bitmap", "pgm"}, |
184 | | {"image/x-portable-pixmap", "ppm"}, |
185 | | {"image/x-rgb", "rgb"}, |
186 | | {"image/x-xbitmap", "xbm"}, |
187 | | {"image/x-xpixmap", "xpm"}, |
188 | | {"image/x-xwindowdump", "xwd"}, |
189 | | {"model/mesh", "msh"}, |
190 | | {"model/mesh", "mesh"}, |
191 | | {"model/mesh", "silo"}, |
192 | | {"multipart/x-zip", "zip"}, |
193 | | {"multipart/x-gzip", "gzip"}, |
194 | | {"text/css", "css"}, |
195 | | {"text/html", "html"}, |
196 | | {"text/html", "htm"}, |
197 | | {"text/plain", "txt"}, |
198 | | {"text/plain", "g"}, |
199 | | {"text/plain", "h"}, |
200 | | {"text/plain", "c"}, |
201 | | {"text/plain", "cc"}, |
202 | | {"text/plain", "hh"}, |
203 | | {"text/plain", "m"}, |
204 | | {"text/plain", "f90"}, |
205 | | {"text/richtext", "rtx"}, |
206 | | {"text/tab-separated-values", "tsv"}, |
207 | | {"text/x-setext", "etx"}, |
208 | | {"text/x-sgml", "sgml"}, |
209 | | {"text/x-sgml", "sgm"}, |
210 | | {"text/xml", "xml"}, |
211 | | {"text/xml", "dtd"}, |
212 | | {"video/mpeg", "mpeg"}, |
213 | | {"video/mpeg", "mpg"}, |
214 | | {"video/mpeg", "mpe"}, |
215 | | {"video/quicktime", "qt"}, |
216 | | {"video/quicktime", "mov"}, |
217 | | {"video/x-msvideo", "avi"}, |
218 | | {"video/x-sgi-movie", "movie"}, |
219 | | {"x-conference/x-cooltalk", "ice"}, |
220 | | /*{"application/x-httpd-cgi","cgi"}, */ |
221 | | {"x-world/x-vrml", "wrl"}, |
222 | | |
223 | | /* More from w3schools.com */ |
224 | | {"application/envoy", "evy"}, |
225 | | {"application/fractals", "fif"}, |
226 | | {"application/futuresplash", "spl"}, |
227 | | {"application/hta", "hta"}, |
228 | | {"application/internet-property-stream", "acx"}, |
229 | | {"application/msword", "dot"}, |
230 | | {"application/olescript", "axs"}, |
231 | | {"application/pics-rules", "prf"}, |
232 | | {"application/pkcs10", "p10"}, |
233 | | {"application/pkix-crl", "crl"}, |
234 | | {"application/set-payment-initiation", "setpay"}, |
235 | | {"application/set-registration-initiation", "setreg"}, |
236 | | {"application/vnd.ms-excel", "xls"}, |
237 | | {"application/vnd.ms-excel", "xla"}, |
238 | | {"application/vnd.ms-excel", "xlc"}, |
239 | | {"application/vnd.ms-excel", "xlm"}, |
240 | | {"application/vnd.ms-excel", "xlt"}, |
241 | | {"application/vnd.ms-excel", "xlw"}, |
242 | | {"application/vnd.ms-pkicertstore", "sst"}, |
243 | | {"application/vnd.ms-pkiseccat", "cat"}, |
244 | | {"application/vnd.ms-powerpoint", "ppt"}, |
245 | | {"application/vnd.ms-powerpoint", "pot"}, |
246 | | {"application/vnd.ms-powerpoint", "pps"}, |
247 | | {"application/vnd.ms-project", "mpp"}, |
248 | | {"application/vnd.ms-works", "wcm"}, |
249 | | {"application/vnd.ms-works", "wdb"}, |
250 | | {"application/vnd.ms-works", "wks"}, |
251 | | {"application/vnd.ms-works", "wps"}, |
252 | | {"application/vnd.oasis.opendocument.chart", "odc"}, |
253 | | {"application/vnd.oasis.opendocument.database", "odb"}, |
254 | | {"application/vnd.oasis.opendocument.formula", "odf"}, |
255 | | {"application/vnd.oasis.opendocument.graphics", "odg"}, |
256 | | {"application/vnd.oasis.opendocument.graphics-template", "otg"}, |
257 | | {"application/vnd.oasis.opendocument.image", "odi"}, |
258 | | {"application/vnd.oasis.opendocument.presentation", "odp"}, |
259 | | {"application/vnd.oasis.opendocument.presentation-template", "otp"}, |
260 | | {"application/vnd.oasis.opendocument.spreadsheet", "ods"}, |
261 | | {"application/vnd.oasis.opendocument.spreadsheet-template", "ots"}, |
262 | | {"application/vnd.oasis.opendocument.text", "odt"}, |
263 | | {"application/vnd.oasis.opendocument.text-master", "odm"}, |
264 | | {"application/vnd.oasis.opendocument.text-template", "ott"}, |
265 | | {"application/vnd.oasis.opendocument.text-web", "oth"}, |
266 | | {"application/" |
267 | | "vnd.openxmlformats-officedocument.presentationml.presentation", |
268 | | "pptx"}, |
269 | | {"application/vnd.openxmlformats-officedocument.presentationml.slide", |
270 | | "sldx"}, |
271 | | {"application/vnd.openxmlformats-officedocument.presentationml.slideshow", |
272 | | "ppsx"}, |
273 | | {"application/vnd.openxmlformats-officedocument.presentationml.template", |
274 | | "potx"}, |
275 | | {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
276 | | "xlsx"}, |
277 | | {"application/vnd.openxmlformats-officedocument.spreadsheetml.template", |
278 | | "xltx"}, |
279 | | {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
280 | | "docx"}, |
281 | | {"application/vnd.openxmlformats-officedocument.wordprocessingml.template", |
282 | | "dotx"}, |
283 | | {"application/x-compress", "z"}, |
284 | | {"application/x-compressed", "tgz"}, |
285 | | {"application/x-internet-signup", "ins"}, |
286 | | {"application/x-internet-signup", "isp"}, |
287 | | {"application/x-iphone", "iii"}, |
288 | | {"application/x-javascript", "js"}, |
289 | | {"application/x-msaccess", "mdb"}, |
290 | | {"application/x-mscardfile", "crd"}, |
291 | | {"application/x-msclip", "clp"}, |
292 | | {"application/x-msmediaview", "m13"}, |
293 | | {"application/x-msmediaview", "m14"}, |
294 | | {"application/x-msmediaview", "mvb"}, |
295 | | {"application/x-msmetafile", "wmf"}, |
296 | | {"application/x-msmoney", "mny"}, |
297 | | {"application/x-mspublisher", "pub"}, |
298 | | {"application/x-msschedule", "scd"}, |
299 | | {"application/x-msterminal", "trm"}, |
300 | | {"application/x-perfmon", "pma"}, |
301 | | {"application/x-perfmon", "pmc"}, |
302 | | {"application/x-perfmon", "pml"}, |
303 | | {"application/x-perfmon", "pmr"}, |
304 | | {"application/x-perfmon", "pmw"}, |
305 | | {"application/x-pkcs12", "p12"}, |
306 | | {"application/x-pkcs12", "pfx"}, |
307 | | {"application/x-pkcs7-certificates", "p7b"}, |
308 | | {"application/x-pkcs7-certificates", "spc"}, |
309 | | {"application/x-pkcs7-certreqresp", "p7r"}, |
310 | | {"application/x-pkcs7-mime", "p7c"}, |
311 | | {"application/x-pkcs7-mime", "p7m"}, |
312 | | {"application/x-pkcs7-signature", "p7s"}, |
313 | | {"application/x-troff-me", "me"}, |
314 | | {"application/x-x509-ca-cert", "cer"}, |
315 | | {"application/x-x509-ca-cert", "crt"}, |
316 | | {"application/x-x509-ca-cert", "der"}, |
317 | | {"application/ynd.ms-pkipko", "pko"}, |
318 | | {"audio/mid", "mid"}, |
319 | | {"audio/mid", "rmi"}, |
320 | | {"audio/mpeg", "mp3"}, |
321 | | {"audio/x-mpegurl", "m3u"}, |
322 | | {"image/bmp", "bmp"}, |
323 | | {"image/cis-cod", "cod"}, |
324 | | {"image/pipeg", "jfif"}, |
325 | | {"image/x-cmx", "cmx"}, |
326 | | {"image/x-icon", "ico"}, |
327 | | {"image/x-portable-bitmap", "pbm"}, |
328 | | {"message/rfc822", "mht"}, |
329 | | {"message/rfc822", "mhtml"}, |
330 | | {"message/rfc822", "nws"}, |
331 | | {"text/css", "css"}, |
332 | | {"text/h323", "323"}, |
333 | | {"text/html", "stm"}, |
334 | | {"text/iuls", "uls"}, |
335 | | {"text/plain", "bas"}, |
336 | | {"text/scriptlet", "sct"}, |
337 | | {"text/webviewhtml", "htt"}, |
338 | | {"text/x-component", "htc"}, |
339 | | {"text/x-vcard", "vcf"}, |
340 | | {"video/mpeg", "mp2"}, |
341 | | {"video/mpeg", "mpa"}, |
342 | | {"video/mpeg", "mpv2"}, |
343 | | {"video/x-la-asf", "lsf"}, |
344 | | {"video/x-la-asf", "lsx"}, |
345 | | {"video/x-ms-asf", "asf"}, |
346 | | {"video/x-ms-asf", "asr"}, |
347 | | {"video/x-ms-asf", "asx"}, |
348 | | {"video/x-ms-wmv", "wmv"}, |
349 | | {"x-world/x-vrml", "flr"}, |
350 | | {"x-world/x-vrml", "vrml"}, |
351 | | {"x-world/x-vrml", "wrz"}, |
352 | | {"x-world/x-vrml", "xaf"}, |
353 | | {"x-world/x-vrml", "xof"}, |
354 | | |
355 | | /* Various */ |
356 | | {"application/ogg", "ogg"}, |
357 | | |
358 | | {"application/x-java-vm", "class"}, |
359 | | {"application/x-bittorrent", "torrent"}, |
360 | | |
361 | | {"", ""}}; |
362 | | |
363 | | /* Post-2010 formats. Scanned after hts_mime[], so a legacy mapping wins. */ |
364 | | static const char *const hts_mime_modern[][2] = { |
365 | | {"image/webp", "webp"}, |
366 | | {"image/avif", "avif"}, |
367 | | {"image/heic", "heic"}, |
368 | | {"font/woff", "woff"}, |
369 | | {"font/woff2", "woff2"}, |
370 | | {"font/ttf", "ttf"}, |
371 | | {"font/otf", "otf"}, |
372 | | {"application/json", "json"}, |
373 | | {"application/ld+json", "jsonld"}, |
374 | | {"application/manifest+json", "webmanifest"}, |
375 | | {"application/wasm", "wasm"}, |
376 | | {"text/javascript", "js"}, |
377 | | {"text/javascript", "mjs"}, |
378 | | {"text/markdown", "md"}, |
379 | | {"video/mp4", "mp4"}, |
380 | | {"video/webm", "webm"}, |
381 | | {"video/ogg", "ogv"}, |
382 | | {"video/mp2t", "ts"}, |
383 | | {"application/vnd.apple.mpegurl", "m3u8"}, |
384 | | {"audio/mp4", "m4a"}, |
385 | | {"audio/aac", "aac"}, |
386 | | {"audio/ogg", "oga"}, |
387 | | {"audio/opus", "opus"}, |
388 | | {"audio/flac", "flac"}, |
389 | | {"audio/webm", "weba"}, |
390 | | {"application/x-7z-compressed", "7z"}, |
391 | | {"application/x-rar-compressed", "rar"}, |
392 | | {"application/zstd", "zst"}, |
393 | | {"", ""}}; |
394 | | |
395 | | // guess a local file's mime type (e.g. fil="toto.gif" -> s="image/gif") |
396 | | // returns 1 if a type was written to s, 0 otherwise |
397 | | hts_boolean guess_httptype_sized(httrackp *opt, char *s, size_t ssize, |
398 | 15.6k | const char *fil) { |
399 | 15.6k | return get_httptype_sized(opt, s, ssize, fil, 1); |
400 | 15.6k | } |
401 | | |
402 | | // deprecated variant; kept for ABI compatibility. Bounds to the implicit |
403 | | // contract the old callers relied on (a contenttype-sized buffer). |
404 | 0 | void guess_httptype(httrackp *opt, char *s, const char *fil) { |
405 | 0 | (void) get_httptype_sized(opt, s, HTS_MIMETYPE_SIZE, fil, 1); |
406 | 0 | } |
407 | | |
408 | | // first match in a NUL-terminated {mime,ext} table. key selects the lookup |
409 | | // column (0=mime, 1=ext); returns the other column, or NULL if no row matches |
410 | | // (a "*" partner means the row carries no value). |
411 | | static const char *hts_mime_lookup(const char *const (*table)[2], int key, |
412 | 231k | hts_span needle) { |
413 | 231k | int j; |
414 | | |
415 | 33.5M | for (j = 0; strnotempty(table[j][1]); j++) { |
416 | 33.3M | if (hts_span_eq_ci(hts_span_of(table[j][key]), needle) && |
417 | 72.6k | table[j][!key][0] != '*') |
418 | 72.6k | return table[j][!key]; |
419 | 33.3M | } |
420 | 158k | return NULL; |
421 | 231k | } |
422 | | |
423 | | // hts_mime_lookup() over the legacy table, then the modern one |
424 | 152k | static const char *hts_mime_find(int key, hts_span needle) { |
425 | 152k | const char *const found = hts_mime_lookup(hts_mime, key, needle); |
426 | | |
427 | 152k | return found != NULL ? found : hts_mime_lookup(hts_mime_modern, key, needle); |
428 | 152k | } |
429 | | |
430 | | // write the mime type for fil into s (capacity ssize) |
431 | | // flag: 1 to always return a type (the "application/..." / octet-stream |
432 | | // fallback) returns 1 if a type was written to s, 0 otherwise |
433 | | HTSEXT_API hts_boolean get_httptype_sized(httrackp *opt, char *s, size_t ssize, |
434 | 125k | const char *fil, hts_boolean flag) { |
435 | | // userdef wins. An empty rule ("--assume cgi=") reports "no type". |
436 | 125k | if (get_userhttptype(opt, hts_buf_at(s, ssize), fil)) { |
437 | 0 | return s[0] != '\0'; |
438 | 0 | } |
439 | | // regular tests |
440 | 125k | if (ishtml(opt, fil) == 1) { |
441 | 21.7k | strlcpybuff(s, "text/html", ssize); |
442 | 21.7k | return 1; |
443 | 103k | } else { |
444 | | /* Check html -> text/html */ |
445 | 103k | const char *a = hts_path_ext_dot(fil, "/"); |
446 | | |
447 | 103k | if (a != NULL && strlen(a) < 32) { |
448 | 62.1k | const char *mime; |
449 | | |
450 | 62.1k | a++; |
451 | 62.1k | mime = hts_mime_find(1, hts_span_of(a)); |
452 | 62.1k | if (mime != NULL) { |
453 | 35.8k | strlcpybuff(s, mime, ssize); |
454 | 35.8k | return 1; |
455 | 35.8k | } |
456 | | |
457 | 26.3k | if (flag) { |
458 | 4.34k | snprintf(s, ssize, "application/%s", a); |
459 | 4.34k | return 1; |
460 | 4.34k | } |
461 | 41.1k | } else { |
462 | 41.1k | if (flag) { |
463 | 0 | strlcpybuff(s, "application/octet-stream", ssize); |
464 | 0 | return 1; |
465 | 0 | } |
466 | 41.1k | } |
467 | 103k | } |
468 | 63.1k | return 0; |
469 | 125k | } |
470 | | |
471 | | // deprecated variant; kept for ABI compatibility. Bounds to the implicit |
472 | | // contract the old callers relied on (a contenttype-sized buffer). |
473 | | HTSEXT_API void get_httptype(httrackp *opt, char *s, const char *fil, |
474 | 0 | int flag) { |
475 | 0 | (void) get_httptype_sized(opt, s, HTS_MIMETYPE_SIZE, fil, flag); |
476 | 0 | } |
477 | | |
478 | | // get type of fil (php) |
479 | 281k | hts_boolean get_userhttptype(httrackp *opt, hts_buf s, const char *fil) { |
480 | 281k | if (s.p != NULL) { |
481 | 281k | assertf(s.size != 0); |
482 | 0 | s.p[0] = '\0'; |
483 | 281k | if (fil == NULL || *fil == '\0') |
484 | 0 | return HTS_FALSE; |
485 | 281k | if (StringLength(opt->mimedefs) > 0) { |
486 | | |
487 | | /* Check --assume foooo/foo/bar.cgi=text/html, then foo/bar.cgi=text/html, |
488 | | * then bar.cgi=text/html */ |
489 | | /* also: --assume baz,bar,foooo/foo/bar.cgi=text/html */ |
490 | | /* start from path beginning */ |
491 | 508k | do { |
492 | 508k | const char *next; |
493 | 508k | const char *mimedefs = |
494 | 508k | StringBuff(opt->mimedefs); /* loop through mime definitions : |
495 | | \nfoo=bar\nzoo=baz\n.. */ |
496 | | |
497 | 1.01M | while (*mimedefs != '\0') { |
498 | 508k | const char *segment = fil + 1; |
499 | | |
500 | 508k | if (*mimedefs == '\n') { |
501 | 508k | mimedefs++; |
502 | 508k | } |
503 | | /* compare current segment with user's definition */ |
504 | 508k | do { |
505 | 508k | int i; |
506 | | |
507 | | /* check current item */ |
508 | 508k | for (i = 0; mimedefs[i] != '\0' /* end of all defs */ |
509 | 0 | && mimedefs[i] != ' ' /* next item in left list */ |
510 | 0 | && mimedefs[i] != '=' /* end of left list */ |
511 | 0 | && mimedefs[i] != '\n' /* end of this def (?) */ |
512 | 0 | && mimedefs[i] == segment[i] /* same item */ |
513 | 508k | ; |
514 | 508k | i++) |
515 | 0 | ; |
516 | | /* success */ |
517 | 508k | if ((mimedefs[i] == '=' || mimedefs[i] == ' ') && |
518 | 0 | segment[i] == '\0') { |
519 | 0 | while (mimedefs[i] != 0 && mimedefs[i] != '\n' && |
520 | 0 | mimedefs[i] != '=') |
521 | 0 | i++; |
522 | 0 | if (mimedefs[i] == '=') { |
523 | 0 | size_t len; |
524 | |
|
525 | 0 | i++; |
526 | 0 | len = strcspn(&mimedefs[i], "\n"); |
527 | | /* clip, as a value that long matches no consumer (#1276) */ |
528 | 0 | const hts_boolean fit = |
529 | 0 | hts_buf_put_span_clip(s, hts_span_at(&mimedefs[i], len)); |
530 | |
|
531 | 0 | if (!fit) |
532 | 0 | hts_log_print(opt, LOG_WARNING, |
533 | 0 | "--assume value for '%s' is %d bytes, clipped " |
534 | 0 | "to %d", |
535 | 0 | fil, (int) len, (int) (s.size - 1)); |
536 | 0 | return HTS_TRUE; /* SUCCESS! */ |
537 | 0 | } |
538 | 0 | } |
539 | | /* next item in list */ |
540 | 508k | for (mimedefs += i; *mimedefs != '\0' && *mimedefs != '\n' && |
541 | 0 | *mimedefs != '=' && *mimedefs != ' '; |
542 | 508k | mimedefs++) |
543 | 0 | ; |
544 | 508k | if (*mimedefs == ' ') { |
545 | 0 | mimedefs++; |
546 | 0 | } |
547 | 508k | } while (*mimedefs != '\0' && *mimedefs != '\n' && *mimedefs != '='); |
548 | | /* next user-def */ |
549 | 508k | for (; *mimedefs != '\0' && *mimedefs != '\n'; mimedefs++) |
550 | 0 | ; |
551 | 508k | } |
552 | | /* shorten segment */ |
553 | 508k | next = strchr(fil + 1, '/'); |
554 | 508k | if (next == NULL) { |
555 | | /* ext tests */ |
556 | 460k | next = strchr(fil + 1, '.'); |
557 | 460k | } |
558 | 508k | fil = next; |
559 | 508k | } while (fil != NULL); |
560 | 281k | } |
561 | 281k | } |
562 | 281k | return HTS_FALSE; |
563 | 281k | } |
564 | | |
565 | | // give the file extension for a mime type (e.g. "image/gif" -> "gif") |
566 | | // returns 1 if an extension was found (and written to s), 0 otherwise |
567 | 0 | int give_mimext(hts_buf s, const char *st) { |
568 | 0 | int ok = 0; |
569 | 0 | const char *ext; |
570 | |
|
571 | 0 | st = hts_effective_mime(st); /* no declared type: derive an html ext */ |
572 | 0 | s.p[0] = '\0'; |
573 | 0 | ext = hts_mime_find(0, hts_span_of(st)); |
574 | 0 | if (ext != NULL) { |
575 | 0 | hts_buf_cpy(s, ext); |
576 | 0 | ok = 1; |
577 | 0 | } |
578 | | // wrap "x" mimetypes, such as: |
579 | | // application/x-mp3 |
580 | | // or |
581 | | // application/mp3 |
582 | 0 | if (!ok) { |
583 | 0 | const char *a = st; |
584 | |
|
585 | 0 | if (hts_skip_prefix_ci(&a, "application/x-") || |
586 | 0 | hts_skip_prefix_ci(&a, "application/")) { |
587 | 0 | if ((int) strlen(a) >= 1) { |
588 | 0 | if ((int) strlen(a) <= 4) { |
589 | 0 | hts_buf_cpy(s, a); |
590 | 0 | ok = 1; |
591 | 0 | } |
592 | 0 | } |
593 | 0 | } |
594 | 0 | } |
595 | 0 | return ok; |
596 | 0 | } |
597 | | |
598 | | // known extension? 0: no, 1: yes, 2: html |
599 | 89.8k | int is_knowntype(httrackp *opt, const char *fil) { |
600 | 89.8k | const char *mime; |
601 | | |
602 | 89.8k | if (!fil) |
603 | 0 | return 0; |
604 | 89.8k | mime = hts_mime_find(1, hts_fil_ext(fil)); |
605 | 89.8k | if (mime != NULL) |
606 | 36.8k | return is_html_mime_type(mime) ? 2 : 1; |
607 | | |
608 | | // Known by user? |
609 | 53.0k | return (is_userknowntype(opt, fil)); |
610 | 89.8k | } |
611 | | |
612 | | // known type?.. |
613 | | // 0 : no |
614 | | // 1 : yes |
615 | | // 2 : html |
616 | | // setdefs : set mime buffer: |
617 | | // file=(char*) "asp=text/html\nphp=text/html\n" |
618 | 53.0k | int is_userknowntype(httrackp *opt, const char *fil) { |
619 | 53.0k | char BIGSTK mime[1024]; |
620 | | |
621 | 53.0k | if (!fil) |
622 | 0 | return 0; |
623 | 53.0k | if (!strnotempty(fil)) |
624 | 21.6k | return 0; |
625 | 31.4k | if (!get_userhttptype(opt, HTS_BUF(mime), fil) || !strnotempty(mime)) |
626 | 31.4k | return 0; |
627 | 0 | else if (is_html_mime_type(mime)) |
628 | 0 | return 2; |
629 | 0 | else |
630 | 0 | return 1; |
631 | 31.4k | } |
632 | | |
633 | 63.1k | hts_boolean is_dyntype(hts_span ext) { |
634 | 63.1k | return hts_span_index_ci(ext, hts_ext_dynamic) >= 0; |
635 | 63.1k | } |
636 | | |
637 | | // types critiques qui ne doivent pas être changés car renvoyés par des serveurs |
638 | | // qui ne connaissent pas le type |
639 | 25.3k | hts_boolean may_unknown(httrackp *opt, const char *st) { |
640 | 25.3k | int j = 0; |
641 | | |
642 | | // types média |
643 | 25.3k | if (may_be_hypertext_mime(opt, st, "")) { |
644 | 1.89k | return 1; |
645 | 1.89k | } |
646 | 96.2k | while (strnotempty(hts_mime_keep[j])) { |
647 | 86.5k | if (hts_streq_ci(hts_mime_keep[j], st)) { |
648 | 13.7k | return 1; |
649 | 13.7k | } |
650 | 72.7k | j++; |
651 | 72.7k | } |
652 | 9.70k | return 0; |
653 | 23.4k | } |
654 | | |
655 | | /* returns 1 if the mime/filename seems bogus because of a badly recognized |
656 | | multiple extension, such as "application/x-wais-source" for |
657 | | "httrack-3.42-1.el5.src.rpm" */ |
658 | | static int may_bogus_multiple(httrackp *opt, const char *mime, |
659 | 0 | const char *filename) { |
660 | 0 | int j; |
661 | |
|
662 | 0 | for (j = 0; strnotempty(hts_mime_bogus_multiple[j]); j++) { |
663 | 0 | if (hts_streq_ci(hts_mime_bogus_multiple[j], |
664 | 0 | mime)) { /* found mime type in suspicious list */ |
665 | 0 | char ext[64]; |
666 | |
|
667 | 0 | if (give_mimext(HTS_BUF(ext), mime)) { /* we have an extension for that */ |
668 | 0 | const size_t ext_size = strlen(ext); |
669 | 0 | const char *file = strrchr(filename, '/'); /* fetch terminal filename */ |
670 | |
|
671 | 0 | if (file != NULL) { |
672 | 0 | int i; |
673 | |
|
674 | 0 | for (i = 0; file[i] != 0; i++) { |
675 | 0 | if (i > 0 && file[i - 1] == '.' && |
676 | 0 | hts_strncasecmp(&file[i], ext, ext_size) == 0 && |
677 | 0 | (file[i + ext_size] == 0 || file[i + ext_size] == '.' || |
678 | 0 | file[i + ext_size] == '?')) { |
679 | 0 | return 1; /* is ambiguous */ |
680 | 0 | } |
681 | 0 | } |
682 | 0 | } |
683 | 0 | } |
684 | 0 | return 0; |
685 | 0 | } |
686 | 0 | } |
687 | 0 | return 0; |
688 | 0 | } |
689 | | |
690 | | /* returns 1 when the filename extension may be bogus and must be kept */ |
691 | 0 | int may_unknown2(httrackp *opt, const char *mime, const char *filename) { |
692 | 0 | int ret = may_unknown(opt, mime); |
693 | |
|
694 | 0 | if (ret == 0) { |
695 | 0 | ret = may_bogus_multiple(opt, mime, filename); |
696 | 0 | } |
697 | 0 | return ret; |
698 | 0 | } |