]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Convert MimeEntry to use RegexPattern API (#1233)
authorAmos Jeffries <yadij@users.noreply.github.com>
Fri, 27 Jan 2023 20:04:05 +0000 (20:04 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Fri, 27 Jan 2023 20:04:14 +0000 (20:04 +0000)
Restore 318f2c8d3418a73946ae947d84f7696a381628b1
with modifications for current RegexPattern API.

src/mime.cc

index cf64743eaaacae78b87e35e015e8b331d3f5f860..4d535f7f5e13b2d225c6c73d11d6ce66e5f185be 100644 (file)
@@ -9,6 +9,7 @@
 /* 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"
@@ -63,15 +64,14 @@ class MimeEntry
     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;
@@ -95,7 +95,7 @@ mimeGetEntry(const char *fn, int skip_encodings)
         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;
         }
 
@@ -238,7 +238,6 @@ mimeInit(char *filename)
     char buf[BUFSIZ];
     char chopbuf[BUFSIZ];
     char *t;
-    char *pattern;
     char *icon;
     char *type;
     char *encoding;
@@ -246,9 +245,7 @@ mimeInit(char *filename)
     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;
@@ -266,70 +263,73 @@ mimeInit(char *filename)
     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);
@@ -451,18 +451,15 @@ MimeIcon::fillChecklist(ACLFilledChecklist &) const
 
 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),