]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/mime.cc
2 * Copyright (C) 1996-2015 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 "base/RegexPattern.h"
16 #include "HttpHdrCc.h"
17 #include "HttpReply.h"
18 #include "HttpRequest.h"
21 #include "MemObject.h"
23 #include "RequestFlags.h"
24 #include "SquidConfig.h"
26 #include "StoreClient.h"
32 /* forward declarations */
33 static void mimeFreeMemory(void);
34 static const SBuf
mimeGetIcon(const char *fn
);
36 class MimeIcon
: public StoreClient
38 MEMPROXY_CLASS(MimeIcon
);
41 explicit MimeIcon(const char *aName
);
43 void setName(char const *);
48 virtual void created(StoreEntry
*);
57 MEMPROXY_CLASS(MimeEntry
);
60 MimeEntry(const char *aPattern
, const decltype(RegexPattern::flags
) &reFlags
,
61 const char *aContentType
,
62 const char *aContentEncoding
, const char *aTransferMode
,
63 bool optionViewEnable
, bool optionDownloadEnable
,
64 const char *anIconName
);
65 MimeEntry(const MimeEntry
&) = delete;
66 MimeEntry(const MimeEntry
&&) = delete;
70 const char *content_type
;
71 const char *content_encoding
;
79 static MimeEntry
*MimeTable
= NULL
;
80 static MimeEntry
**MimeTableTail
= &MimeTable
;
83 mimeGetEntry(const char *fn
, int skip_encodings
)
87 char *name
= xstrdup(fn
);
92 for (m
= MimeTable
; m
; m
= m
->next
) {
93 if (m
->pattern
.match(name
))
101 else if (strcmp(m
->content_type
, dash_str
))
103 else if (!strcmp(m
->content_encoding
, dash_str
))
106 /* Assume we matched /\.\w$/ and cut off the last extension */
107 if ((t
= strrchr(name
, '.'))) {
110 /* What? A encoding without a extension? */
120 MimeIcon::MimeIcon(const char *aName
) :
123 url_
= xstrdup(internalLocalUri("/squid-internal-static/icons/", icon_
));
126 MimeIcon::~MimeIcon()
132 MimeIcon::setName(char const *aString
)
136 url_
= xstrdup(internalLocalUri("/squid-internal-static/icons/", icon_
));
140 MimeIcon::getName() const
146 mimeGetIcon(const char *fn
)
148 MimeEntry
*m
= mimeGetEntry(fn
, 1);
150 if (!m
|| !m
->theIcon
.getName().cmp(dash_str
))
153 return m
->theIcon
.getName();
157 mimeGetIconURL(const char *fn
)
159 SBuf
icon(mimeGetIcon(fn
));
164 if (Config
.icons
.use_short_names
) {
167 mb
.append("/squid-internal-static/icons/");
171 return internalLocalUri("/squid-internal-static/icons/", icon
);
176 mimeGetContentType(const char *fn
)
178 MimeEntry
*m
= mimeGetEntry(fn
, 1);
183 if (!strcmp(m
->content_type
, dash_str
))
186 return m
->content_type
;
190 mimeGetContentEncoding(const char *fn
)
192 MimeEntry
*m
= mimeGetEntry(fn
, 0);
197 if (!strcmp(m
->content_encoding
, dash_str
))
200 return m
->content_encoding
;
204 mimeGetTransferMode(const char *fn
)
206 MimeEntry
*m
= mimeGetEntry(fn
, 0);
207 return m
? m
->transfer_mode
: 'I';
211 mimeGetDownloadOption(const char *fn
)
213 MimeEntry
*m
= mimeGetEntry(fn
, 1);
214 return m
? m
->download_option
: 0;
218 mimeGetViewOption(const char *fn
)
220 MimeEntry
*m
= mimeGetEntry(fn
, 0);
221 return m
!= 0 ? m
->view_option
: false;
224 /* Initializes/reloads the mime table
225 * Note: Due to Solaris STDIO problems the caller should NOT
226 * call mimeFreeMemory on reconfigure. This way, if STDIO
227 * fails we at least have the old copy loaded.
230 mimeInit(char *filename
)
234 char chopbuf
[BUFSIZ
];
245 if (filename
== NULL
)
248 if ((fp
= fopen(filename
, "r")) == NULL
) {
249 debugs(25, DBG_IMPORTANT
, "mimeInit: " << filename
<< ": " << xstrerror());
254 setmode(fileno(fp
), O_TEXT
);
259 const auto re_flags
= std::regex::extended
| std::regex::nosubs
| std::regex::icase
;
261 while (fgets(buf
, BUFSIZ
, fp
)) {
262 if ((t
= strchr(buf
, '#')))
265 if ((t
= strchr(buf
, '\r')))
268 if ((t
= strchr(buf
, '\n')))
274 xstrncpy(chopbuf
, buf
, BUFSIZ
);
277 if ((pattern
= strtok(chopbuf
, w_space
)) == NULL
) {
278 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
282 if ((type
= strtok(NULL
, w_space
)) == NULL
) {
283 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
287 if ((icon
= strtok(NULL
, w_space
)) == NULL
) {
288 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
292 if ((encoding
= strtok(NULL
, w_space
)) == NULL
) {
293 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
297 if ((mode
= strtok(NULL
, w_space
)) == NULL
) {
298 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
305 while ((option
= strtok(NULL
, w_space
)) != NULL
) {
306 if (!strcmp(option
, "+download"))
308 else if (!strcmp(option
, "+view"))
311 debugs(25, DBG_IMPORTANT
, "mimeInit: unknown option: '" << buf
<< "' (" << option
<< ")");
315 m
= new MimeEntry(pattern
, re_flags
, type
, encoding
, mode
, view_option
, download_option
, icon
);
317 } catch (std::regex_error
&e
) {
318 debugs(25, DBG_IMPORTANT
, "mimeInit: invalid regular expression: '" << buf
<< "'");
324 MimeTableTail
= &m
->next
;
326 debugs(25, 5, "mimeInit: added '" << buf
<< "'");
331 for (m
= MimeTable
; m
!= NULL
; m
= m
->next
)
333 debugs(25, DBG_IMPORTANT
, "Finished loading MIME types and icons.");
341 while ((m
= MimeTable
)) {
346 MimeTableTail
= &MimeTable
;
352 const char *type
= mimeGetContentType(icon_
.c_str());
355 fatal("Unknown icon format while reading mime.conf\n");
357 StoreEntry::getPublic(this, url_
, Http::METHOD_GET
);
361 MimeIcon::created(StoreEntry
*newEntry
)
363 /* if the icon is already in the store, do nothing */
364 if (!newEntry
->isNull())
366 // XXX: if a 204 is cached due to earlier load 'failure' we should try to reload.
368 // default is a 200 object with image data.
369 // set to the backup value of 204 on image loading errors
370 Http::StatusCode status
= Http::scOkay
;
372 static char path
[MAXPATHLEN
];
374 if (snprintf(path
, sizeof(path
)-1, "%s/" SQUIDSBUFPH
, Config
.icons
.directory
, SQUIDSBUFPRINT(icon_
)) < 0) {
375 debugs(25, DBG_CRITICAL
, "ERROR: icon file '" << Config
.icons
.directory
<< "/" << icon_
<< "' path is longer than " << MAXPATHLEN
<< " bytes");
376 status
= Http::scNoContent
;
381 if (status
== Http::scOkay
&& (fd
= file_open(path
, O_RDONLY
| O_BINARY
)) < 0) {
383 debugs(25, DBG_CRITICAL
, "ERROR: opening icon file " << path
<< ": " << xstrerr(xerrno
));
384 status
= Http::scNoContent
;
389 if (status
== Http::scOkay
&& fstat(fd
, &sb
) < 0) {
391 debugs(25, DBG_CRITICAL
, "ERROR: opening icon file " << path
<< " FD " << fd
<< ", fstat error " << xstrerr(xerrno
));
393 status
= Http::scNoContent
;
396 // fill newEntry with a canned 2xx response object
398 flags
.cachable
= true;
399 StoreEntry
*e
= storeCreateEntry(url_
,url_
,flags
,Http::METHOD_GET
);
401 EBIT_SET(e
->flags
, ENTRY_SPECIAL
);
404 HttpRequest
*r
= HttpRequest::CreateFromUrl(url_
);
407 fatalf("mimeLoadIcon: cannot parse internal URL: %s", url_
);
409 e
->mem_obj
->request
= r
;
410 HTTPMSGLOCK(e
->mem_obj
->request
);
412 HttpReply
*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
);
423 if (status
== Http::scOkay
) {
424 /* read the file into the buffer and append it to store */
426 char *buf
= (char *)memAllocate(MEM_4K_BUF
);
427 while ((n
= FD_READ_METHOD(fd
, buf
, sizeof(*buf
))) > 0)
431 memFree(buf
, MEM_4K_BUF
);
437 e
->unlock("MimeIcon::created");
438 debugs(25, 3, "Loaded icon " << url_
);
441 MimeEntry::~MimeEntry()
444 xfree(content_encoding
);
447 MimeEntry::MimeEntry(const char *aPattern
, const decltype(RegexPattern::flags
) &reFlags
,
448 const char *aContentType
, const char *aContentEncoding
,
449 const char *aTransferMode
, bool optionViewEnable
,
450 bool optionDownloadEnable
, const char *anIconName
) :
451 pattern(reFlags
, aPattern
),
452 content_type(xstrdup(aContentType
)),
453 content_encoding(xstrdup(aContentEncoding
)),
454 view_option(optionViewEnable
),
455 download_option(optionDownloadEnable
),
459 if (!strcasecmp(aTransferMode
, "ascii"))
461 else if (!strcasecmp(aTransferMode
, "text"))