]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/mime.cc
2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 25 MIME Parsing and Internal Icons */
12 #include "DebugMessages.h"
16 #include "HttpHdrCc.h"
17 #include "HttpReply.h"
18 #include "HttpRequest.h"
21 #include "MemObject.h"
23 #include "SquidConfig.h"
25 #include "StoreClient.h"
33 /* forward declarations */
34 static void mimeFreeMemory(void);
35 static const SBuf
mimeGetIcon(const char *fn
);
37 class MimeIcon
: public StoreClient
39 MEMPROXY_CLASS(MimeIcon
);
42 explicit MimeIcon(const char *aName
);
44 void setName(char const *);
49 virtual LogTags
*loggingTags() const { return nullptr; } // no access logging/ACLs
50 virtual void fillChecklist(ACLFilledChecklist
&) const;
59 MEMPROXY_CLASS(MimeEntry
);
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
);
70 regex_t compiled_pattern
;
71 const char *content_type
;
72 const char *content_encoding
;
80 static MimeEntry
*MimeTable
= NULL
;
81 static MimeEntry
**MimeTableTail
= &MimeTable
;
84 mimeGetEntry(const char *fn
, int skip_encodings
)
88 char *name
= xstrdup(fn
);
93 for (m
= MimeTable
; m
; m
= m
->next
) {
94 if (regexec(&m
->compiled_pattern
, name
, 0, 0, 0) == 0)
102 else if (strcmp(m
->content_type
, dash_str
))
104 else if (!strcmp(m
->content_encoding
, dash_str
))
107 /* Assume we matched /\.\w$/ and cut off the last extension */
108 if ((t
= strrchr(name
, '.'))) {
111 /* What? A encoding without a extension? */
121 MimeIcon::MimeIcon(const char *aName
) :
127 MimeIcon::~MimeIcon()
133 MimeIcon::setName(char const *aString
)
137 url_
= xstrdup(internalLocalUri("/squid-internal-static/icons/", icon_
));
141 MimeIcon::getName() const
147 mimeGetIcon(const char *fn
)
149 MimeEntry
*m
= mimeGetEntry(fn
, 1);
151 if (!m
|| !m
->theIcon
.getName().cmp(dash_str
))
154 return m
->theIcon
.getName();
158 mimeGetIconURL(const char *fn
)
160 SBuf
icon(mimeGetIcon(fn
));
165 if (Config
.icons
.use_short_names
) {
168 mb
.append("/squid-internal-static/icons/");
172 return internalLocalUri("/squid-internal-static/icons/", icon
);
177 mimeGetContentType(const char *fn
)
179 MimeEntry
*m
= mimeGetEntry(fn
, 1);
184 if (!strcmp(m
->content_type
, dash_str
))
187 return m
->content_type
;
191 mimeGetContentEncoding(const char *fn
)
193 MimeEntry
*m
= mimeGetEntry(fn
, 0);
198 if (!strcmp(m
->content_encoding
, dash_str
))
201 return m
->content_encoding
;
205 mimeGetTransferMode(const char *fn
)
207 MimeEntry
*m
= mimeGetEntry(fn
, 0);
208 return m
? m
->transfer_mode
: 'I';
212 mimeGetDownloadOption(const char *fn
)
214 MimeEntry
*m
= mimeGetEntry(fn
, 1);
215 return m
? m
->download_option
: 0;
219 mimeGetViewOption(const char *fn
)
221 MimeEntry
*m
= mimeGetEntry(fn
, 0);
222 return m
!= 0 ? m
->view_option
: false;
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.
231 mimeInit(char *filename
)
235 char chopbuf
[BUFSIZ
];
247 int re_flags
= REG_EXTENDED
| REG_NOSUB
| REG_ICASE
;
249 if (filename
== NULL
)
252 if ((fp
= fopen(filename
, "r")) == NULL
) {
254 debugs(25, DBG_IMPORTANT
, "mimeInit: " << filename
<< ": " << xstrerr(xerrno
));
259 setmode(fileno(fp
), O_TEXT
);
264 while (fgets(buf
, BUFSIZ
, fp
)) {
265 if ((t
= strchr(buf
, '#')))
268 if ((t
= strchr(buf
, '\r')))
271 if ((t
= strchr(buf
, '\n')))
277 xstrncpy(chopbuf
, buf
, BUFSIZ
);
279 if ((pattern
= strtok(chopbuf
, w_space
)) == NULL
) {
280 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
284 if ((type
= strtok(NULL
, w_space
)) == NULL
) {
285 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
289 if ((icon
= strtok(NULL
, w_space
)) == NULL
) {
290 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
294 if ((encoding
= strtok(NULL
, w_space
)) == NULL
) {
295 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
299 if ((mode
= strtok(NULL
, w_space
)) == NULL
) {
300 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
307 while ((option
= strtok(NULL
, w_space
)) != NULL
) {
308 if (!strcmp(option
, "+download"))
310 else if (!strcmp(option
, "+view"))
313 debugs(25, DBG_IMPORTANT
, "mimeInit: unknown option: '" << buf
<< "' (" << option
<< ")");
316 if (regcomp(&re
, pattern
, re_flags
) != 0) {
317 debugs(25, DBG_IMPORTANT
, "mimeInit: regcomp error: '" << buf
<< "'");
321 m
= new MimeEntry(pattern
,re
,type
,encoding
,mode
,view_option
,
322 download_option
,icon
);
326 MimeTableTail
= &m
->next
;
328 debugs(25, 5, "mimeInit: added '" << buf
<< "'");
333 for (m
= MimeTable
; m
!= NULL
; m
= m
->next
)
335 debugs(25, Important(28), "Finished loading MIME types and icons.");
343 while ((m
= MimeTable
)) {
348 MimeTableTail
= &MimeTable
;
354 const char *type
= mimeGetContentType(icon_
.c_str());
357 fatal("Unknown icon format while reading mime.conf\n");
359 if (const auto e
= storeGetPublic(url_
, Http::METHOD_GET
)) {
360 // do not overwrite an already stored icon
361 e
->abandon(__FUNCTION__
);
365 // XXX: if a 204 is cached due to earlier load 'failure' we should try to reload.
367 // default is a 200 object with image data.
368 // set to the backup value of 204 on image loading errors
369 Http::StatusCode status
= Http::scOkay
;
371 static char path
[MAXPATHLEN
];
373 if (snprintf(path
, sizeof(path
)-1, "%s/" SQUIDSBUFPH
, Config
.icons
.directory
, SQUIDSBUFPRINT(icon_
)) < 0) {
374 debugs(25, DBG_CRITICAL
, "ERROR: icon file '" << Config
.icons
.directory
<< "/" << icon_
<< "' path is longer than " << MAXPATHLEN
<< " bytes");
375 status
= Http::scNoContent
;
380 if (status
== Http::scOkay
&& (fd
= file_open(path
, O_RDONLY
| O_BINARY
)) < 0) {
382 debugs(25, DBG_CRITICAL
, "ERROR: opening icon file " << path
<< ": " << xstrerr(xerrno
));
383 status
= Http::scNoContent
;
388 if (status
== Http::scOkay
&& fstat(fd
, &sb
) < 0) {
390 debugs(25, DBG_CRITICAL
, "ERROR: opening icon file " << path
<< " FD " << fd
<< ", fstat error " << xstrerr(xerrno
));
392 status
= Http::scNoContent
;
395 StoreEntry
*e
= storeCreatePureEntry(url_
, url_
, Http::METHOD_GET
);
396 e
->lock("MimeIcon::created");
397 EBIT_SET(e
->flags
, ENTRY_SPECIAL
);
398 const auto madePublic
= e
->setPublicKey();
399 assert(madePublic
); // nothing can block ENTRY_SPECIAL from becoming public
401 /* fill `e` with a canned 2xx response object */
403 const MasterXaction::Pointer mx
= new MasterXaction(XactionInitiator::initIcon
);
404 HttpRequestPointer
r(HttpRequest::FromUrlXXX(url_
, mx
));
406 fatalf("mimeLoadIcon: cannot parse internal URL: %s", url_
);
410 e
->mem_obj
->request
= r
;
412 HttpReplyPointer
reply(new HttpReply
);
414 if (status
== Http::scNoContent
)
415 reply
->setHeaders(status
, NULL
, NULL
, 0, -1, -1);
417 reply
->setHeaders(status
, NULL
, mimeGetContentType(icon_
.c_str()), sb
.st_size
, sb
.st_mtime
, -1);
418 reply
->cache_control
= new HttpHdrCc();
419 reply
->cache_control
->maxAge(86400);
420 reply
->header
.putCc(reply
->cache_control
);
421 e
->replaceHttpReply(reply
.getRaw());
423 if (status
== Http::scOkay
) {
424 /* read the file into the buffer and append it to store */
426 std::array
<char, 4096> buf
;
427 while ((n
= FD_READ_METHOD(fd
, buf
.data(), buf
.size())) > 0)
428 e
->append(buf
.data(), n
);
436 // MimeIcons are only loaded once, prevent accidental destruction
437 // e->unlock("MimeIcon::created");
438 debugs(25, 3, "Loaded icon " << url_
);
442 MimeIcon::fillChecklist(ACLFilledChecklist
&) const
444 // Unreachable: We never mayInitiateCollapsing() or startCollapsingOn().
448 MimeEntry::~MimeEntry()
452 xfree(content_encoding
);
453 regfree(&compiled_pattern
);
456 MimeEntry::MimeEntry(const char *aPattern
, const regex_t
&compiledPattern
,
457 const char *aContentType
, const char *aContentEncoding
,
458 const char *aTransferMode
, bool optionViewEnable
,
459 bool optionDownloadEnable
, const char *anIconName
) :
460 pattern(xstrdup(aPattern
)),
461 compiled_pattern(compiledPattern
),
462 content_type(xstrdup(aContentType
)),
463 content_encoding(xstrdup(aContentEncoding
)),
464 view_option(optionViewEnable
),
465 download_option(optionDownloadEnable
),
466 theIcon(anIconName
), next(NULL
)
468 if (!strcasecmp(aTransferMode
, "ascii"))
470 else if (!strcasecmp(aTransferMode
, "text"))