]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/mime.cc
2 * DEBUG: section 25 MIME Parsing and Internal Icons
3 * AUTHOR: Harvest Derived
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
37 #include "HttpHdrCc.h"
38 #include "HttpReply.h"
39 #include "HttpRequest.h"
43 #include "MemObject.h"
45 #include "RequestFlags.h"
46 #include "SquidConfig.h"
48 #include "StoreClient.h"
54 /* forward declarations */
55 static void mimeFreeMemory(void);
56 static char const *mimeGetIcon(const char *fn
);
58 class MimeIcon
: public StoreClient
61 explicit MimeIcon(const char *aName
);
63 void setName(char const *);
64 char const * getName() const;
66 void created(StoreEntry
*newEntry
);
67 MEMPROXY_CLASS(MimeIcon
);
73 MEMPROXY_CLASS_INLINE(MimeIcon
);
78 explicit MimeEntry(const char *aPattern
, const regex_t
&compiledPattern
,
79 const char *aContentType
,
80 const char *aContentEncoding
, const char *aTransferMode
,
81 bool optionViewEnable
, bool optionDownloadEnable
,
82 const char *anIconName
);
84 MEMPROXY_CLASS(MimeEntry
);
87 regex_t compiled_pattern
;
88 const char *content_type
;
89 const char *content_encoding
;
96 MEMPROXY_CLASS_INLINE(MimeEntry
);
98 static MimeEntry
*MimeTable
= NULL
;
99 static MimeEntry
**MimeTableTail
= &MimeTable
;
102 mimeGetEntry(const char *fn
, int skip_encodings
)
106 char *name
= xstrdup(fn
);
111 for (m
= MimeTable
; m
; m
= m
->next
) {
112 if (regexec(&m
->compiled_pattern
, name
, 0, 0, 0) == 0)
120 else if (strcmp(m
->content_type
, dash_str
))
122 else if (!strcmp(m
->content_encoding
, dash_str
))
125 /* Assume we matched /\.\w$/ and cut off the last extension */
126 if ((t
= strrchr(name
, '.'))) {
129 /* What? A encoding without a extension? */
139 MimeIcon::MimeIcon(const char *aName
) :
140 icon_(xstrdup(aName
))
142 url_
= xstrdup(internalLocalUri("/squid-internal-static/icons/", icon_
));
145 MimeIcon::~MimeIcon()
152 MimeIcon::setName(char const *aString
)
156 icon_
= xstrdup(aString
);
157 url_
= xstrdup(internalLocalUri("/squid-internal-static/icons/", icon_
));
161 MimeIcon::getName() const
167 mimeGetIcon(const char *fn
)
169 MimeEntry
*m
= mimeGetEntry(fn
, 1);
174 if (!strcmp(m
->theIcon
.getName(), dash_str
))
177 return m
->theIcon
.getName();
181 mimeGetIconURL(const char *fn
)
183 char const *icon
= mimeGetIcon(fn
);
188 if (Config
.icons
.use_short_names
) {
191 mb
.Printf("/squid-internal-static/icons/%s", icon
);
194 return internalLocalUri("/squid-internal-static/icons/", icon
);
199 mimeGetContentType(const char *fn
)
201 MimeEntry
*m
= mimeGetEntry(fn
, 1);
206 if (!strcmp(m
->content_type
, dash_str
))
209 return m
->content_type
;
213 mimeGetContentEncoding(const char *fn
)
215 MimeEntry
*m
= mimeGetEntry(fn
, 0);
220 if (!strcmp(m
->content_encoding
, dash_str
))
223 return m
->content_encoding
;
227 mimeGetTransferMode(const char *fn
)
229 MimeEntry
*m
= mimeGetEntry(fn
, 0);
230 return m
? m
->transfer_mode
: 'I';
234 mimeGetDownloadOption(const char *fn
)
236 MimeEntry
*m
= mimeGetEntry(fn
, 1);
237 return m
? m
->download_option
: 0;
241 mimeGetViewOption(const char *fn
)
243 MimeEntry
*m
= mimeGetEntry(fn
, 0);
244 return m
!= 0 ? m
->view_option
: false;
247 /* Initializes/reloads the mime table
248 * Note: Due to Solaris STDIO problems the caller should NOT
249 * call mimeFreeMemory on reconfigure. This way, if STDIO
250 * fails we at least have the old copy loaded.
253 mimeInit(char *filename
)
257 char chopbuf
[BUFSIZ
];
269 int re_flags
= REG_EXTENDED
| REG_NOSUB
| REG_ICASE
;
271 if (filename
== NULL
)
274 if ((fp
= fopen(filename
, "r")) == NULL
) {
275 debugs(25, DBG_IMPORTANT
, "mimeInit: " << filename
<< ": " << xstrerror());
280 setmode(fileno(fp
), O_TEXT
);
285 while (fgets(buf
, BUFSIZ
, fp
)) {
286 if ((t
= strchr(buf
, '#')))
289 if ((t
= strchr(buf
, '\r')))
292 if ((t
= strchr(buf
, '\n')))
298 xstrncpy(chopbuf
, buf
, BUFSIZ
);
300 if ((pattern
= strtok(chopbuf
, w_space
)) == NULL
) {
301 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
305 if ((type
= strtok(NULL
, w_space
)) == NULL
) {
306 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
310 if ((icon
= strtok(NULL
, w_space
)) == NULL
) {
311 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
315 if ((encoding
= strtok(NULL
, w_space
)) == NULL
) {
316 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
320 if ((mode
= strtok(NULL
, w_space
)) == NULL
) {
321 debugs(25, DBG_IMPORTANT
, "mimeInit: parse error: '" << buf
<< "'");
328 while ((option
= strtok(NULL
, w_space
)) != NULL
) {
329 if (!strcmp(option
, "+download"))
331 else if (!strcmp(option
, "+view"))
334 debugs(25, DBG_IMPORTANT
, "mimeInit: unknown option: '" << buf
<< "' (" << option
<< ")");
337 if (regcomp(&re
, pattern
, re_flags
) != 0) {
338 debugs(25, DBG_IMPORTANT
, "mimeInit: regcomp error: '" << buf
<< "'");
342 m
= new MimeEntry(pattern
,re
,type
,encoding
,mode
,view_option
,
343 download_option
,icon
);
347 MimeTableTail
= &m
->next
;
349 debugs(25, 5, "mimeInit: added '" << buf
<< "'");
354 for (m
= MimeTable
; m
!= NULL
; m
= m
->next
)
356 debugs(25, DBG_IMPORTANT
, "Finished loading MIME types and icons.");
364 while ((m
= MimeTable
)) {
369 MimeTableTail
= &MimeTable
;
375 const char *type
= mimeGetContentType(icon_
);
378 fatal("Unknown icon format while reading mime.conf\n");
380 StoreEntry::getPublic(this, url_
, Http::METHOD_GET
);
384 MimeIcon::created (StoreEntry
*newEntry
)
386 /* if the icon is already in the store, do nothing */
387 if (!newEntry
->isNull())
394 LOCAL_ARRAY(char, path
, MAXPATHLEN
);
397 snprintf(path
, MAXPATHLEN
, "%s/%s", Config
.icons
.directory
, icon_
);
399 fd
= file_open(path
, O_RDONLY
| O_BINARY
);
401 debugs(25, DBG_CRITICAL
, "Problem opening icon file " << path
<< ": " << xstrerror());
404 if (fstat(fd
, &sb
) < 0) {
405 debugs(25, DBG_CRITICAL
, "Problem opening icon file. Fd: " << fd
<< ", fstat error " << xstrerror());
410 flags
.cachable
= true;
411 StoreEntry
*e
= storeCreateEntry(url_
,url_
,flags
,Http::METHOD_GET
);
413 EBIT_SET(e
->flags
, ENTRY_SPECIAL
);
416 HttpRequest
*r
= HttpRequest::CreateFromUrl(url_
);
419 fatal("mimeLoadIcon: cannot parse internal URL");
421 e
->mem_obj
->request
= r
;
422 HTTPMSGLOCK(e
->mem_obj
->request
);
424 HttpReply
*reply
= new HttpReply
;
426 reply
->setHeaders(Http::scOkay
, NULL
, mimeGetContentType(icon_
), sb
.st_size
, sb
.st_mtime
, -1);
427 reply
->cache_control
= new HttpHdrCc();
428 reply
->cache_control
->maxAge(86400);
429 reply
->header
.putCc(reply
->cache_control
);
430 e
->replaceHttpReply(reply
);
432 /* read the file into the buffer and append it to store */
433 buf
= (char *)memAllocate(MEM_4K_BUF
);
434 while ((n
= FD_READ_METHOD(fd
, buf
, 4096)) > 0)
441 e
->unlock("MimeIcon::created");
442 memFree(buf
, MEM_4K_BUF
);
443 debugs(25, 3, "Loaded icon " << url_
);
446 MimeEntry::~MimeEntry()
450 xfree(content_encoding
);
451 regfree(&compiled_pattern
);
454 MimeEntry::MimeEntry(const char *aPattern
, const regex_t
&compiledPattern
,
455 const char *aContentType
, const char *aContentEncoding
,
456 const char *aTransferMode
, bool optionViewEnable
,
457 bool optionDownloadEnable
, const char *anIconName
) :
458 pattern(xstrdup(aPattern
)),
459 compiled_pattern(compiledPattern
),
460 content_type(xstrdup(aContentType
)),
461 content_encoding(xstrdup(aContentEncoding
)),
462 view_option(optionViewEnable
),
463 download_option(optionViewEnable
),
464 theIcon(anIconName
), next(NULL
)
466 if (!strcasecmp(aTransferMode
, "ascii"))
468 else if (!strcasecmp(aTransferMode
, "text"))