]> git.ipfire.org Git - thirdparty/squid.git/blob - src/mime.cc
5acdd8f26038502ec7863bce70b32487e8c09879
[thirdparty/squid.git] / src / mime.cc
1 /*
2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 25 MIME Parsing and Internal Icons */
10
11 #include "squid.h"
12 #include "fde.h"
13 #include "fs_io.h"
14 #include "globals.h"
15 #include "HttpHdrCc.h"
16 #include "HttpReply.h"
17 #include "HttpRequest.h"
18 #include "internal.h"
19 #include "MemBuf.h"
20 #include "MemObject.h"
21 #include "mime.h"
22 #include "SquidConfig.h"
23 #include "Store.h"
24 #include "StoreClient.h"
25
26 #include <array>
27
28 #if HAVE_SYS_STAT_H
29 #include <sys/stat.h>
30 #endif
31
32 /* forward declarations */
33 static void mimeFreeMemory(void);
34 static const SBuf mimeGetIcon(const char *fn);
35
36 class MimeIcon : public StoreClient
37 {
38 MEMPROXY_CLASS(MimeIcon);
39
40 public:
41 explicit MimeIcon(const char *aName);
42 ~MimeIcon();
43 void setName(char const *);
44 SBuf getName() const;
45 void load();
46
47 /* StoreClient API */
48 virtual void created(StoreEntry *);
49 virtual LogTags *loggingTags() { return nullptr; } // no access logging/ACLs
50 virtual void fillChecklist(ACLFilledChecklist &) const;
51
52 private:
53 SBuf icon_;
54 char *url_;
55 };
56
57 class MimeEntry
58 {
59 MEMPROXY_CLASS(MimeEntry);
60
61 public:
62 explicit MimeEntry(const char *aPattern, const regex_t &compiledPattern,
63 const char *aContentType,
64 const char *aContentEncoding, const char *aTransferMode,
65 bool optionViewEnable, bool optionDownloadEnable,
66 const char *anIconName);
67 ~MimeEntry();
68
69 const char *pattern;
70 regex_t compiled_pattern;
71 const char *content_type;
72 const char *content_encoding;
73 char transfer_mode;
74 bool view_option;
75 bool download_option;
76 MimeIcon theIcon;
77 MimeEntry *next;
78 };
79
80 static MimeEntry *MimeTable = NULL;
81 static MimeEntry **MimeTableTail = &MimeTable;
82
83 static MimeEntry *
84 mimeGetEntry(const char *fn, int skip_encodings)
85 {
86 MimeEntry *m;
87 char *t;
88 char *name = xstrdup(fn);
89
90 do {
91 t = NULL;
92
93 for (m = MimeTable; m; m = m->next) {
94 if (regexec(&m->compiled_pattern, name, 0, 0, 0) == 0)
95 break;
96 }
97
98 if (!skip_encodings)
99 (void) 0;
100 else if (m == NULL)
101 (void) 0;
102 else if (strcmp(m->content_type, dash_str))
103 (void) 0;
104 else if (!strcmp(m->content_encoding, dash_str))
105 (void) 0;
106 else {
107 /* Assume we matched /\.\w$/ and cut off the last extension */
108 if ((t = strrchr(name, '.'))) {
109 *t = '\0';
110 } else {
111 /* What? A encoding without a extension? */
112 m = NULL;
113 }
114 }
115 } while (t);
116
117 xfree(name);
118 return m;
119 }
120
121 MimeIcon::MimeIcon(const char *aName) :
122 url_(nullptr)
123 {
124 setName(aName);
125 }
126
127 MimeIcon::~MimeIcon()
128 {
129 xfree(url_);
130 }
131
132 void
133 MimeIcon::setName(char const *aString)
134 {
135 xfree(url_);
136 icon_ = aString;
137 url_ = xstrdup(internalLocalUri("/squid-internal-static/icons/", icon_));
138 }
139
140 SBuf
141 MimeIcon::getName() const
142 {
143 return icon_;
144 }
145
146 const SBuf
147 mimeGetIcon(const char *fn)
148 {
149 MimeEntry *m = mimeGetEntry(fn, 1);
150
151 if (!m || !m->theIcon.getName().cmp(dash_str))
152 return SBuf();
153
154 return m->theIcon.getName();
155 }
156
157 const char *
158 mimeGetIconURL(const char *fn)
159 {
160 SBuf icon(mimeGetIcon(fn));
161
162 if (icon.isEmpty())
163 return null_string;
164
165 if (Config.icons.use_short_names) {
166 static SBuf mb;
167 mb.clear();
168 mb.append("/squid-internal-static/icons/");
169 mb.append(icon);
170 return mb.c_str();
171 } else {
172 return internalLocalUri("/squid-internal-static/icons/", icon);
173 }
174 }
175
176 const char *
177 mimeGetContentType(const char *fn)
178 {
179 MimeEntry *m = mimeGetEntry(fn, 1);
180
181 if (m == NULL)
182 return NULL;
183
184 if (!strcmp(m->content_type, dash_str))
185 return NULL;
186
187 return m->content_type;
188 }
189
190 const char *
191 mimeGetContentEncoding(const char *fn)
192 {
193 MimeEntry *m = mimeGetEntry(fn, 0);
194
195 if (m == NULL)
196 return NULL;
197
198 if (!strcmp(m->content_encoding, dash_str))
199 return NULL;
200
201 return m->content_encoding;
202 }
203
204 char
205 mimeGetTransferMode(const char *fn)
206 {
207 MimeEntry *m = mimeGetEntry(fn, 0);
208 return m ? m->transfer_mode : 'I';
209 }
210
211 bool
212 mimeGetDownloadOption(const char *fn)
213 {
214 MimeEntry *m = mimeGetEntry(fn, 1);
215 return m ? m->download_option : 0;
216 }
217
218 bool
219 mimeGetViewOption(const char *fn)
220 {
221 MimeEntry *m = mimeGetEntry(fn, 0);
222 return m != 0 ? m->view_option : false;
223 }
224
225 /* Initializes/reloads the mime table
226 * Note: Due to Solaris STDIO problems the caller should NOT
227 * call mimeFreeMemory on reconfigure. This way, if STDIO
228 * fails we at least have the old copy loaded.
229 */
230 void
231 mimeInit(char *filename)
232 {
233 FILE *fp;
234 char buf[BUFSIZ];
235 char chopbuf[BUFSIZ];
236 char *t;
237 char *pattern;
238 char *icon;
239 char *type;
240 char *encoding;
241 char *mode;
242 char *option;
243 int view_option;
244 int download_option;
245 regex_t re;
246 MimeEntry *m;
247 int re_flags = REG_EXTENDED | REG_NOSUB | REG_ICASE;
248
249 if (filename == NULL)
250 return;
251
252 if ((fp = fopen(filename, "r")) == NULL) {
253 int xerrno = errno;
254 debugs(25, DBG_IMPORTANT, "mimeInit: " << filename << ": " << xstrerr(xerrno));
255 return;
256 }
257
258 #if _SQUID_WINDOWS_
259 setmode(fileno(fp), O_TEXT);
260 #endif
261
262 mimeFreeMemory();
263
264 while (fgets(buf, BUFSIZ, fp)) {
265 if ((t = strchr(buf, '#')))
266 *t = '\0';
267
268 if ((t = strchr(buf, '\r')))
269 *t = '\0';
270
271 if ((t = strchr(buf, '\n')))
272 *t = '\0';
273
274 if (buf[0] == '\0')
275 continue;
276
277 xstrncpy(chopbuf, buf, BUFSIZ);
278
279 if ((pattern = strtok(chopbuf, w_space)) == NULL) {
280 debugs(25, DBG_IMPORTANT, "mimeInit: parse error: '" << buf << "'");
281 continue;
282 }
283
284 if ((type = strtok(NULL, w_space)) == NULL) {
285 debugs(25, DBG_IMPORTANT, "mimeInit: parse error: '" << buf << "'");
286 continue;
287 }
288
289 if ((icon = strtok(NULL, w_space)) == NULL) {
290 debugs(25, DBG_IMPORTANT, "mimeInit: parse error: '" << buf << "'");
291 continue;
292 }
293
294 if ((encoding = strtok(NULL, w_space)) == NULL) {
295 debugs(25, DBG_IMPORTANT, "mimeInit: parse error: '" << buf << "'");
296 continue;
297 }
298
299 if ((mode = strtok(NULL, w_space)) == NULL) {
300 debugs(25, DBG_IMPORTANT, "mimeInit: parse error: '" << buf << "'");
301 continue;
302 }
303
304 download_option = 0;
305 view_option = 0;
306
307 while ((option = strtok(NULL, w_space)) != NULL) {
308 if (!strcmp(option, "+download"))
309 download_option = 1;
310 else if (!strcmp(option, "+view"))
311 view_option = 1;
312 else
313 debugs(25, DBG_IMPORTANT, "mimeInit: unknown option: '" << buf << "' (" << option << ")");
314 }
315
316 if (regcomp(&re, pattern, re_flags) != 0) {
317 debugs(25, DBG_IMPORTANT, "mimeInit: regcomp error: '" << buf << "'");
318 continue;
319 }
320
321 m = new MimeEntry(pattern,re,type,encoding,mode,view_option,
322 download_option,icon);
323
324 *MimeTableTail = m;
325
326 MimeTableTail = &m->next;
327
328 debugs(25, 5, "mimeInit: added '" << buf << "'");
329 }
330
331 fclose(fp);
332
333 for (m = MimeTable; m != NULL; m = m->next)
334 m->theIcon.load();
335 debugs(25, DBG_IMPORTANT, "Finished loading MIME types and icons.");
336 }
337
338 void
339 mimeFreeMemory(void)
340 {
341 MimeEntry *m;
342
343 while ((m = MimeTable)) {
344 MimeTable = m->next;
345 delete m;
346 }
347
348 MimeTableTail = &MimeTable;
349 }
350
351 void
352 MimeIcon::load()
353 {
354 const char *type = mimeGetContentType(icon_.c_str());
355
356 if (type == NULL)
357 fatal("Unknown icon format while reading mime.conf\n");
358
359 StoreEntry::getPublic(this, url_, Http::METHOD_GET);
360 }
361
362 void
363 MimeIcon::created(StoreEntry *newEntry)
364 {
365 /* if the icon is already in the store, do nothing */
366 if (newEntry)
367 return;
368 // XXX: if a 204 is cached due to earlier load 'failure' we should try to reload.
369
370 // default is a 200 object with image data.
371 // set to the backup value of 204 on image loading errors
372 Http::StatusCode status = Http::scOkay;
373
374 static char path[MAXPATHLEN];
375 *path = 0;
376 if (snprintf(path, sizeof(path)-1, "%s/" SQUIDSBUFPH, Config.icons.directory, SQUIDSBUFPRINT(icon_)) < 0) {
377 debugs(25, DBG_CRITICAL, "ERROR: icon file '" << Config.icons.directory << "/" << icon_ << "' path is longer than " << MAXPATHLEN << " bytes");
378 status = Http::scNoContent;
379 }
380
381 int fd = -1;
382 errno = 0;
383 if (status == Http::scOkay && (fd = file_open(path, O_RDONLY | O_BINARY)) < 0) {
384 int xerrno = errno;
385 debugs(25, DBG_CRITICAL, "ERROR: opening icon file " << path << ": " << xstrerr(xerrno));
386 status = Http::scNoContent;
387 }
388
389 struct stat sb;
390 errno = 0;
391 if (status == Http::scOkay && fstat(fd, &sb) < 0) {
392 int xerrno = errno;
393 debugs(25, DBG_CRITICAL, "ERROR: opening icon file " << path << " FD " << fd << ", fstat error " << xstrerr(xerrno));
394 file_close(fd);
395 status = Http::scNoContent;
396 }
397
398 StoreEntry *e = storeCreatePureEntry(url_, url_, Http::METHOD_GET);
399 e->lock("MimeIcon::created");
400 EBIT_SET(e->flags, ENTRY_SPECIAL);
401 const auto madePublic = e->setPublicKey();
402 assert(madePublic); // nothing can block ENTRY_SPECIAL from becoming public
403
404 /* fill `e` with a canned 2xx response object */
405
406 const MasterXaction::Pointer mx = new MasterXaction(XactionInitiator::initIcon);
407 HttpRequestPointer r(HttpRequest::FromUrlXXX(url_, mx));
408 if (!r)
409 fatalf("mimeLoadIcon: cannot parse internal URL: %s", url_);
410
411 e->buffer();
412
413 e->mem_obj->request = r;
414
415 HttpReplyPointer reply(new HttpReply);
416
417 if (status == Http::scNoContent)
418 reply->setHeaders(status, NULL, NULL, 0, -1, -1);
419 else
420 reply->setHeaders(status, NULL, mimeGetContentType(icon_.c_str()), sb.st_size, sb.st_mtime, -1);
421 reply->cache_control = new HttpHdrCc();
422 reply->cache_control->maxAge(86400);
423 reply->header.putCc(reply->cache_control);
424 e->replaceHttpReply(reply.getRaw());
425
426 if (status == Http::scOkay) {
427 /* read the file into the buffer and append it to store */
428 int n;
429 std::array<char, 4096> buf;
430 while ((n = FD_READ_METHOD(fd, buf.data(), buf.size())) > 0)
431 e->append(buf.data(), n);
432
433 file_close(fd);
434 }
435
436 e->flush();
437 e->complete();
438 e->timestampsSet();
439 // MimeIcons are only loaded once, prevent accidental destruction
440 // e->unlock("MimeIcon::created");
441 debugs(25, 3, "Loaded icon " << url_);
442 }
443
444 void
445 MimeIcon::fillChecklist(ACLFilledChecklist &) const
446 {
447 // Unreachable: We never mayInitiateCollapsing() or startCollapsingOn().
448 assert(false);
449 }
450
451 MimeEntry::~MimeEntry()
452 {
453 xfree(pattern);
454 xfree(content_type);
455 xfree(content_encoding);
456 regfree(&compiled_pattern);
457 }
458
459 MimeEntry::MimeEntry(const char *aPattern, const regex_t &compiledPattern,
460 const char *aContentType, const char *aContentEncoding,
461 const char *aTransferMode, bool optionViewEnable,
462 bool optionDownloadEnable, const char *anIconName) :
463 pattern(xstrdup(aPattern)),
464 compiled_pattern(compiledPattern),
465 content_type(xstrdup(aContentType)),
466 content_encoding(xstrdup(aContentEncoding)),
467 view_option(optionViewEnable),
468 download_option(optionDownloadEnable),
469 theIcon(anIconName), next(NULL)
470 {
471 if (!strcasecmp(aTransferMode, "ascii"))
472 transfer_mode = 'A';
473 else if (!strcasecmp(aTransferMode, "text"))
474 transfer_mode = 'A';
475 else
476 transfer_mode = 'I';
477 }
478