/* DEBUG: section 25 MIME Parsing and Internal Icons */
#include "squid.h"
+#include "base/RegexPattern.h"
#include "debug/Messages.h"
#include "fde.h"
#include "fs_io.h"
MEMPROXY_CLASS(MimeEntry);
public:
- explicit MimeEntry(const char *aPattern, const regex_t &compiledPattern,
+ explicit MimeEntry(const SBuf &aPattern,
const char *aContentType,
const char *aContentEncoding, const char *aTransferMode,
bool optionViewEnable, bool optionDownloadEnable,
const char *anIconName);
~MimeEntry();
- const char *pattern;
- regex_t compiled_pattern;
+ RegexPattern pattern;
const char *content_type;
const char *content_encoding;
char transfer_mode;
t = nullptr;
for (m = MimeTable; m; m = m->next) {
- if (regexec(&m->compiled_pattern, name, 0, nullptr, 0) == 0)
+ if (m->pattern.match(name))
break;
}
char buf[BUFSIZ];
char chopbuf[BUFSIZ];
char *t;
- char *pattern;
char *icon;
char *type;
char *encoding;
char *option;
int view_option;
int download_option;
- regex_t re;
MimeEntry *m;
- int re_flags = REG_EXTENDED | REG_NOSUB | REG_ICASE;
if (filename == nullptr)
return;
mimeFreeMemory();
while (fgets(buf, BUFSIZ, fp)) {
- if ((t = strchr(buf, '#')))
- *t = '\0';
- if ((t = strchr(buf, '\r')))
- *t = '\0';
+ try {
- if ((t = strchr(buf, '\n')))
- *t = '\0';
+ if ((t = strchr(buf, '#')))
+ *t = '\0';
- if (buf[0] == '\0')
- continue;
+ if ((t = strchr(buf, '\r')))
+ *t = '\0';
- xstrncpy(chopbuf, buf, BUFSIZ);
+ if ((t = strchr(buf, '\n')))
+ *t = '\0';
- if ((pattern = strtok(chopbuf, w_space)) == nullptr) {
- debugs(25, DBG_IMPORTANT, "ERROR: mimeInit: parse failure: '" << buf << "'");
- continue;
- }
+ if (buf[0] == '\0')
+ continue;
- if ((type = strtok(nullptr, w_space)) == nullptr) {
- debugs(25, DBG_IMPORTANT, "ERROR: mimeInit: parse failure: '" << buf << "'");
- continue;
- }
+ xstrncpy(chopbuf, buf, BUFSIZ);
- if ((icon = strtok(nullptr, w_space)) == nullptr) {
- debugs(25, DBG_IMPORTANT, "ERROR: mimeInit: parse failure: '" << buf << "'");
- continue;
- }
+ const auto pattern = SBuf(strtok(chopbuf, w_space));
+ if (pattern.isEmpty()) {
+ debugs(25, DBG_IMPORTANT, "ERROR: mimeInit: parse failure: '" << buf << "'");
+ continue;
+ }
- if ((encoding = strtok(nullptr, w_space)) == nullptr) {
- debugs(25, DBG_IMPORTANT, "ERROR: mimeInit: parse failure: '" << buf << "'");
- continue;
- }
+ if ((type = strtok(nullptr, w_space)) == nullptr) {
+ debugs(25, DBG_IMPORTANT, "ERROR: mimeInit: parse failure: '" << buf << "'");
+ continue;
+ }
- if ((mode = strtok(nullptr, w_space)) == nullptr) {
- debugs(25, DBG_IMPORTANT, "ERROR: mimeInit: parse failure: '" << buf << "'");
- continue;
- }
+ if ((icon = strtok(nullptr, w_space)) == nullptr) {
+ debugs(25, DBG_IMPORTANT, "ERROR: mimeInit: parse failure: '" << buf << "'");
+ continue;
+ }
- download_option = 0;
- view_option = 0;
+ if ((encoding = strtok(nullptr, w_space)) == nullptr) {
+ debugs(25, DBG_IMPORTANT, "ERROR: mimeInit: parse failure: '" << buf << "'");
+ continue;
+ }
- while ((option = strtok(nullptr, w_space)) != nullptr) {
- if (!strcmp(option, "+download"))
- download_option = 1;
- else if (!strcmp(option, "+view"))
- view_option = 1;
- else
- debugs(25, DBG_IMPORTANT, "ERROR: mimeInit: unknown option: '" << buf << "' (" << option << ")");
- }
+ if ((mode = strtok(nullptr, w_space)) == nullptr) {
+ debugs(25, DBG_IMPORTANT, "ERROR: mimeInit: parse failure: '" << buf << "'");
+ continue;
+ }
- if (regcomp(&re, pattern, re_flags) != 0) {
- debugs(25, DBG_IMPORTANT, "ERROR: mimeInit: regcomp failure: '" << buf << "'");
- continue;
- }
+ download_option = 0;
+ view_option = 0;
+
+ while ((option = strtok(nullptr, w_space)) != nullptr) {
+ if (!strcmp(option, "+download"))
+ download_option = 1;
+ else if (!strcmp(option, "+view"))
+ view_option = 1;
+ else
+ debugs(25, DBG_IMPORTANT, "ERROR: mimeInit: unknown option: '" << buf << "' (" << option << ")");
+ }
- m = new MimeEntry(pattern,re,type,encoding,mode,view_option,
- download_option,icon);
+ m = new MimeEntry(SBuf(pattern),type,encoding,mode,view_option,download_option,icon);
- *MimeTableTail = m;
+ *MimeTableTail = m;
- MimeTableTail = &m->next;
+ MimeTableTail = &m->next;
- debugs(25, 5, "mimeInit: added '" << buf << "'");
+ debugs(25, 5, "mimeInit: added '" << buf << "'");
+
+ } catch(...) {
+ debugs(25, DBG_IMPORTANT, "ERROR: " << CurrentException);
+ continue;
+ }
}
fclose(fp);
MimeEntry::~MimeEntry()
{
- xfree(pattern);
xfree(content_type);
xfree(content_encoding);
- regfree(&compiled_pattern);
}
-MimeEntry::MimeEntry(const char *aPattern, const regex_t &compiledPattern,
+MimeEntry::MimeEntry(const SBuf &aPattern,
const char *aContentType, const char *aContentEncoding,
const char *aTransferMode, bool optionViewEnable,
bool optionDownloadEnable, const char *anIconName) :
- pattern(xstrdup(aPattern)),
- compiled_pattern(compiledPattern),
+ pattern(aPattern, REG_EXTENDED|REG_NOSUB|REG_ICASE),
content_type(xstrdup(aContentType)),
content_encoding(xstrdup(aContentEncoding)),
view_option(optionViewEnable),