]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/mime.cc
2 * Copyright (C) 1996-2016 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 */
15 #include "HttpHdrCc.h"
16 #include "HttpReply.h"
17 #include "HttpRequest.h"
20 #include "MemObject.h"
22 #include "RequestFlags.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 void created(StoreEntry
*);
58 MEMPROXY_CLASS(MimeEntry
);
61 explicit MimeEntry(const char *aPattern
, const regex_t
&compiledPattern
,
62 const char *aContentType
,
63 const char *aContentEncoding
, const char *aTransferMode
,
64 bool optionViewEnable
, bool optionDownloadEnable
,
65 const char *anIconName
);
69 regex_t compiled_pattern
;
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 (regexec(&m
->compiled_pattern
, name
, 0, 0, 0) == 0)
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
) :
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
];
246 int re_flags
= REG_EXTENDED
| REG_NOSUB
| REG_ICASE
;
248 if (filename
== NULL
)
251 if ((fp
= fopen(filename
, "r")) == NULL
) {
253 debugs(25, DBG_IMPORTANT
, "mimeInit: " << filename
<< ": " << xstrerr(xerrno
));
258 setmode(fileno(fp
), O_TEXT
);
263 while (fgets(buf
, BUFSIZ
, fp
)) {
264 if ((t
= strchr(buf
, '#')))
267 if ((t
= strchr(buf
, '\r')))
270 if ((t
= strchr(buf
, '\n')))
276 xstrncpy(chopbuf
, buf
, BUFSIZ
);
278 if ((pattern
= strtok(chopbuf
, w_space
)) == NULL
) {
279 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
283 if ((type
= strtok(NULL
, w_space
)) == NULL
) {
284 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
288 if ((icon
= strtok(NULL
, w_space
)) == NULL
) {
289 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
293 if ((encoding
= strtok(NULL
, w_space
)) == NULL
) {
294 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
298 if ((mode
= strtok(NULL
, w_space
)) == NULL
) {
299 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
306 while ((option
= strtok(NULL
, w_space
)) != NULL
) {
307 if (!strcmp(option
, "+download"))
309 else if (!strcmp(option
, "+view"))
312 debugs(25, DBG_IMPORTANT
, "mimeInit: unknown option: '" << buf
<< "' (" << option
<< ")");
315 if (regcomp(&re
, pattern
, re_flags
) != 0) {
316 debugs(25, DBG_IMPORTANT
, "mimeInit: regcomp error: '" << buf
<< "'");
320 m
= new MimeEntry(pattern
,re
,type
,encoding
,mode
,view_option
,
321 download_option
,icon
);
325 MimeTableTail
= &m
->next
;
327 debugs(25, 5, "mimeInit: added '" << buf
<< "'");
332 for (m
= MimeTable
; m
!= NULL
; m
= m
->next
)
334 debugs(25, DBG_IMPORTANT
, "Finished loading MIME types and icons.");
342 while ((m
= MimeTable
)) {
347 MimeTableTail
= &MimeTable
;
353 const char *type
= mimeGetContentType(icon_
.c_str());
356 fatal("Unknown icon format while reading mime.conf\n");
358 StoreEntry::getPublic(this, url_
, Http::METHOD_GET
);
362 MimeIcon::created(StoreEntry
*newEntry
)
364 /* if the icon is already in the store, do nothing */
365 if (!newEntry
->isNull())
367 // XXX: if a 204 is cached due to earlier load 'failure' we should try to reload.
369 // default is a 200 object with image data.
370 // set to the backup value of 204 on image loading errors
371 Http::StatusCode status
= Http::scOkay
;
373 static char path
[MAXPATHLEN
];
375 if (snprintf(path
, sizeof(path
)-1, "%s/" SQUIDSBUFPH
, Config
.icons
.directory
, SQUIDSBUFPRINT(icon_
)) < 0) {
376 debugs(25, DBG_CRITICAL
, "ERROR: icon file '" << Config
.icons
.directory
<< "/" << icon_
<< "' path is longer than " << MAXPATHLEN
<< " bytes");
377 status
= Http::scNoContent
;
382 if (status
== Http::scOkay
&& (fd
= file_open(path
, O_RDONLY
| O_BINARY
)) < 0) {
384 debugs(25, DBG_CRITICAL
, "ERROR: opening icon file " << path
<< ": " << xstrerr(xerrno
));
385 status
= Http::scNoContent
;
390 if (status
== Http::scOkay
&& fstat(fd
, &sb
) < 0) {
392 debugs(25, DBG_CRITICAL
, "ERROR: opening icon file " << path
<< " FD " << fd
<< ", fstat error " << xstrerr(xerrno
));
394 status
= Http::scNoContent
;
397 // fill newEntry with a canned 2xx response object
399 flags
.cachable
= true;
400 StoreEntry
*e
= storeCreateEntry(url_
,url_
,flags
,Http::METHOD_GET
);
402 EBIT_SET(e
->flags
, ENTRY_SPECIAL
);
405 HttpRequest
*r
= HttpRequest::CreateFromUrl(url_
);
408 fatalf("mimeLoadIcon: cannot parse internal URL: %s", url_
);
410 e
->mem_obj
->request
= r
;
411 HTTPMSGLOCK(e
->mem_obj
->request
);
413 HttpReply
*reply
= new HttpReply
;
415 if (status
== Http::scNoContent
)
416 reply
->setHeaders(status
, NULL
, NULL
, 0, -1, -1);
418 reply
->setHeaders(status
, NULL
, mimeGetContentType(icon_
.c_str()), sb
.st_size
, sb
.st_mtime
, -1);
419 reply
->cache_control
= new HttpHdrCc();
420 reply
->cache_control
->maxAge(86400);
421 reply
->header
.putCc(reply
->cache_control
);
422 e
->replaceHttpReply(reply
);
424 if (status
== Http::scOkay
) {
425 /* read the file into the buffer and append it to store */
427 std::array
<char, 4096> buf
;
428 while ((n
= FD_READ_METHOD(fd
, buf
.data(), buf
.size())) > 0)
429 e
->append(buf
.data(), n
);
437 e
->unlock("MimeIcon::created");
438 debugs(25, 3, "Loaded icon " << url_
);
441 MimeEntry::~MimeEntry()
445 xfree(content_encoding
);
446 regfree(&compiled_pattern
);
449 MimeEntry::MimeEntry(const char *aPattern
, const regex_t
&compiledPattern
,
450 const char *aContentType
, const char *aContentEncoding
,
451 const char *aTransferMode
, bool optionViewEnable
,
452 bool optionDownloadEnable
, const char *anIconName
) :
453 pattern(xstrdup(aPattern
)),
454 compiled_pattern(compiledPattern
),
455 content_type(xstrdup(aContentType
)),
456 content_encoding(xstrdup(aContentEncoding
)),
457 view_option(optionViewEnable
),
458 download_option(optionDownloadEnable
),
459 theIcon(anIconName
), next(NULL
)
461 if (!strcasecmp(aTransferMode
, "ascii"))
463 else if (!strcasecmp(aTransferMode
, "text"))