]>
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 */
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"
31 /* forward declarations */
32 static void mimeFreeMemory(void);
33 static char const *mimeGetIcon(const char *fn
);
35 class MimeIcon
: public StoreClient
37 MEMPROXY_CLASS(MimeIcon
);
40 explicit MimeIcon(const char *aName
);
42 void setName(char const *);
43 char const * getName() const;
47 virtual void created(StoreEntry
*);
56 MEMPROXY_CLASS(MimeEntry
);
59 explicit MimeEntry(const char *aPattern
, const regex_t
&compiledPattern
,
60 const char *aContentType
,
61 const char *aContentEncoding
, const char *aTransferMode
,
62 bool optionViewEnable
, bool optionDownloadEnable
,
63 const char *anIconName
);
67 regex_t compiled_pattern
;
68 const char *content_type
;
69 const char *content_encoding
;
77 static MimeEntry
*MimeTable
= NULL
;
78 static MimeEntry
**MimeTableTail
= &MimeTable
;
81 mimeGetEntry(const char *fn
, int skip_encodings
)
85 char *name
= xstrdup(fn
);
90 for (m
= MimeTable
; m
; m
= m
->next
) {
91 if (regexec(&m
->compiled_pattern
, name
, 0, 0, 0) == 0)
99 else if (strcmp(m
->content_type
, dash_str
))
101 else if (!strcmp(m
->content_encoding
, dash_str
))
104 /* Assume we matched /\.\w$/ and cut off the last extension */
105 if ((t
= strrchr(name
, '.'))) {
108 /* What? A encoding without a extension? */
118 MimeIcon::MimeIcon(const char *aName
) :
119 icon_(xstrdup(aName
))
121 url_
= xstrdup(internalLocalUri("/squid-internal-static/icons/", icon_
));
124 MimeIcon::~MimeIcon()
131 MimeIcon::setName(char const *aString
)
135 icon_
= xstrdup(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);
153 if (!strcmp(m
->theIcon
.getName(), dash_str
))
156 return m
->theIcon
.getName();
160 mimeGetIconURL(const char *fn
)
162 char const *icon
= mimeGetIcon(fn
);
167 if (Config
.icons
.use_short_names
) {
170 mb
.appendf("/squid-internal-static/icons/%s", icon
);
173 return internalLocalUri("/squid-internal-static/icons/", icon
);
178 mimeGetContentType(const char *fn
)
180 MimeEntry
*m
= mimeGetEntry(fn
, 1);
185 if (!strcmp(m
->content_type
, dash_str
))
188 return m
->content_type
;
192 mimeGetContentEncoding(const char *fn
)
194 MimeEntry
*m
= mimeGetEntry(fn
, 0);
199 if (!strcmp(m
->content_encoding
, dash_str
))
202 return m
->content_encoding
;
206 mimeGetTransferMode(const char *fn
)
208 MimeEntry
*m
= mimeGetEntry(fn
, 0);
209 return m
? m
->transfer_mode
: 'I';
213 mimeGetDownloadOption(const char *fn
)
215 MimeEntry
*m
= mimeGetEntry(fn
, 1);
216 return m
? m
->download_option
: 0;
220 mimeGetViewOption(const char *fn
)
222 MimeEntry
*m
= mimeGetEntry(fn
, 0);
223 return m
!= 0 ? m
->view_option
: false;
226 /* Initializes/reloads the mime table
227 * Note: Due to Solaris STDIO problems the caller should NOT
228 * call mimeFreeMemory on reconfigure. This way, if STDIO
229 * fails we at least have the old copy loaded.
232 mimeInit(char *filename
)
236 char chopbuf
[BUFSIZ
];
248 int re_flags
= REG_EXTENDED
| REG_NOSUB
| REG_ICASE
;
250 if (filename
== NULL
)
253 if ((fp
= fopen(filename
, "r")) == NULL
) {
254 debugs(25, DBG_IMPORTANT
, "mimeInit: " << filename
<< ": " << xstrerror());
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, DBG_IMPORTANT
, "Finished loading MIME types and icons.");
343 while ((m
= MimeTable
)) {
348 MimeTableTail
= &MimeTable
;
354 const char *type
= mimeGetContentType(icon_
);
357 fatal("Unknown icon format while reading mime.conf\n");
359 StoreEntry::getPublic(this, url_
, Http::METHOD_GET
);
363 MimeIcon::created(StoreEntry
*newEntry
)
365 /* if the icon is already in the store, do nothing */
366 if (!newEntry
->isNull())
368 // XXX: if a 204 is cached due to earlier load 'failure' we should try to reload.
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
;
374 static char path
[MAXPATHLEN
];
376 if (snprintf(path
, sizeof(path
)-1, "%s/%s", Config
.icons
.directory
, icon_
) < 0) {
377 debugs(25, DBG_CRITICAL
, "ERROR: icon file '" << Config
.icons
.directory
<< "/" << icon_
<< "' path is longer than " << MAXPATHLEN
<< " bytes");
378 status
= Http::scNoContent
;
383 if (status
== Http::scOkay
&& (fd
= file_open(path
, O_RDONLY
| O_BINARY
)) < 0) {
385 debugs(25, DBG_CRITICAL
, "ERROR: opening icon file " << path
<< ": " << xstrerr(xerrno
));
386 status
= Http::scNoContent
;
391 if (status
== Http::scOkay
&& fstat(fd
, &sb
) < 0) {
393 debugs(25, DBG_CRITICAL
, "ERROR: opening icon file " << path
<< " FD " << fd
<< ", fstat error " << xstrerr(xerrno
));
395 status
= Http::scNoContent
;
398 // fill newEntry with a canned 2xx response object
400 flags
.cachable
= true;
401 StoreEntry
*e
= storeCreateEntry(url_
,url_
,flags
,Http::METHOD_GET
);
403 EBIT_SET(e
->flags
, ENTRY_SPECIAL
);
406 HttpRequest
*r
= HttpRequest::CreateFromUrl(url_
);
409 fatalf("mimeLoadIcon: cannot parse internal URL: %s", url_
);
411 e
->mem_obj
->request
= r
;
412 HTTPMSGLOCK(e
->mem_obj
->request
);
414 HttpReply
*reply
= new HttpReply
;
416 if (status
== Http::scNoContent
)
417 reply
->setHeaders(status
, NULL
, NULL
, 0, -1, -1);
419 reply
->setHeaders(status
, NULL
, mimeGetContentType(icon_
), sb
.st_size
, sb
.st_mtime
, -1);
420 reply
->cache_control
= new HttpHdrCc();
421 reply
->cache_control
->maxAge(86400);
422 reply
->header
.putCc(reply
->cache_control
);
423 e
->replaceHttpReply(reply
);
425 if (status
== Http::scOkay
) {
426 /* read the file into the buffer and append it to store */
428 char *buf
= (char *)memAllocate(MEM_4K_BUF
);
429 while ((n
= FD_READ_METHOD(fd
, buf
, sizeof(*buf
))) > 0)
433 memFree(buf
, MEM_4K_BUF
);
439 e
->unlock("MimeIcon::created");
440 debugs(25, 3, "Loaded icon " << url_
);
443 MimeEntry::~MimeEntry()
447 xfree(content_encoding
);
448 regfree(&compiled_pattern
);
451 MimeEntry::MimeEntry(const char *aPattern
, const regex_t
&compiledPattern
,
452 const char *aContentType
, const char *aContentEncoding
,
453 const char *aTransferMode
, bool optionViewEnable
,
454 bool optionDownloadEnable
, const char *anIconName
) :
455 pattern(xstrdup(aPattern
)),
456 compiled_pattern(compiledPattern
),
457 content_type(xstrdup(aContentType
)),
458 content_encoding(xstrdup(aContentEncoding
)),
459 view_option(optionViewEnable
),
460 download_option(optionDownloadEnable
),
461 theIcon(anIconName
), next(NULL
)
463 if (!strcasecmp(aTransferMode
, "ascii"))
465 else if (!strcasecmp(aTransferMode
, "text"))