]>
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;
45 void created(StoreEntry
*newEntry
);
54 MEMPROXY_CLASS(MimeEntry
);
57 explicit MimeEntry(const char *aPattern
, const regex_t
&compiledPattern
,
58 const char *aContentType
,
59 const char *aContentEncoding
, const char *aTransferMode
,
60 bool optionViewEnable
, bool optionDownloadEnable
,
61 const char *anIconName
);
65 regex_t compiled_pattern
;
66 const char *content_type
;
67 const char *content_encoding
;
75 static MimeEntry
*MimeTable
= NULL
;
76 static MimeEntry
**MimeTableTail
= &MimeTable
;
79 mimeGetEntry(const char *fn
, int skip_encodings
)
83 char *name
= xstrdup(fn
);
88 for (m
= MimeTable
; m
; m
= m
->next
) {
89 if (regexec(&m
->compiled_pattern
, name
, 0, 0, 0) == 0)
97 else if (strcmp(m
->content_type
, dash_str
))
99 else if (!strcmp(m
->content_encoding
, dash_str
))
102 /* Assume we matched /\.\w$/ and cut off the last extension */
103 if ((t
= strrchr(name
, '.'))) {
106 /* What? A encoding without a extension? */
116 MimeIcon::MimeIcon(const char *aName
) :
117 icon_(xstrdup(aName
))
119 url_
= xstrdup(internalLocalUri("/squid-internal-static/icons/", icon_
));
122 MimeIcon::~MimeIcon()
129 MimeIcon::setName(char const *aString
)
133 icon_
= xstrdup(aString
);
134 url_
= xstrdup(internalLocalUri("/squid-internal-static/icons/", icon_
));
138 MimeIcon::getName() const
144 mimeGetIcon(const char *fn
)
146 MimeEntry
*m
= mimeGetEntry(fn
, 1);
151 if (!strcmp(m
->theIcon
.getName(), dash_str
))
154 return m
->theIcon
.getName();
158 mimeGetIconURL(const char *fn
)
160 char const *icon
= mimeGetIcon(fn
);
165 if (Config
.icons
.use_short_names
) {
168 mb
.appendf("/squid-internal-static/icons/%s", icon
);
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
) {
252 debugs(25, DBG_IMPORTANT
, "mimeInit: " << filename
<< ": " << xstrerror());
257 setmode(fileno(fp
), O_TEXT
);
262 while (fgets(buf
, BUFSIZ
, fp
)) {
263 if ((t
= strchr(buf
, '#')))
266 if ((t
= strchr(buf
, '\r')))
269 if ((t
= strchr(buf
, '\n')))
275 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
<< ")");
314 if (regcomp(&re
, pattern
, re_flags
) != 0) {
315 debugs(25, DBG_IMPORTANT
, "mimeInit: regcomp error: '" << buf
<< "'");
319 m
= new MimeEntry(pattern
,re
,type
,encoding
,mode
,view_option
,
320 download_option
,icon
);
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_
);
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())
371 LOCAL_ARRAY(char, path
, MAXPATHLEN
);
374 snprintf(path
, MAXPATHLEN
, "%s/%s", Config
.icons
.directory
, icon_
);
376 fd
= file_open(path
, O_RDONLY
| O_BINARY
);
378 debugs(25, DBG_CRITICAL
, "Problem opening icon file " << path
<< ": " << xstrerror());
381 if (fstat(fd
, &sb
) < 0) {
382 debugs(25, DBG_CRITICAL
, "Problem opening icon file. Fd: " << fd
<< ", fstat error " << xstrerror());
387 flags
.cachable
= true;
388 StoreEntry
*e
= storeCreateEntry(url_
,url_
,flags
,Http::METHOD_GET
);
390 EBIT_SET(e
->flags
, ENTRY_SPECIAL
);
393 HttpRequest
*r
= HttpRequest::CreateFromUrl(url_
);
396 fatal("mimeLoadIcon: cannot parse internal URL");
398 e
->mem_obj
->request
= r
;
399 HTTPMSGLOCK(e
->mem_obj
->request
);
401 HttpReply
*reply
= new HttpReply
;
403 reply
->setHeaders(Http::scOkay
, NULL
, mimeGetContentType(icon_
), sb
.st_size
, sb
.st_mtime
, -1);
404 reply
->cache_control
= new HttpHdrCc();
405 reply
->cache_control
->maxAge(86400);
406 reply
->header
.putCc(reply
->cache_control
);
407 e
->replaceHttpReply(reply
);
409 /* read the file into the buffer and append it to store */
410 buf
= (char *)memAllocate(MEM_4K_BUF
);
411 while ((n
= FD_READ_METHOD(fd
, buf
, 4096)) > 0)
418 e
->unlock("MimeIcon::created");
419 memFree(buf
, MEM_4K_BUF
);
420 debugs(25, 3, "Loaded icon " << url_
);
423 MimeEntry::~MimeEntry()
427 xfree(content_encoding
);
428 regfree(&compiled_pattern
);
431 MimeEntry::MimeEntry(const char *aPattern
, const regex_t
&compiledPattern
,
432 const char *aContentType
, const char *aContentEncoding
,
433 const char *aTransferMode
, bool optionViewEnable
,
434 bool optionDownloadEnable
, const char *anIconName
) :
435 pattern(xstrdup(aPattern
)),
436 compiled_pattern(compiledPattern
),
437 content_type(xstrdup(aContentType
)),
438 content_encoding(xstrdup(aContentEncoding
)),
439 view_option(optionViewEnable
),
440 download_option(optionDownloadEnable
),
441 theIcon(anIconName
), next(NULL
)
443 if (!strcasecmp(aTransferMode
, "ascii"))
445 else if (!strcasecmp(aTransferMode
, "text"))