Commits

Patrick Mézard committed 6971df6

Support <includedMimeTypes> rules similar to the excluded ones.

The included types are checked before the excluded ones.

  • Participants
  • Parent commits 117588e

Comments (0)

Files changed (4)

File HttpCompress/Settings.cs

 
     private Algorithms _preferredAlgorithm;
     private CompressionLevels _compressionLevel;
-    private Dictionary<String, bool> _excludedTypes;
+    private Dictionary<string, bool> _includedTypes;
+    private Dictionary<string, bool> _excludedTypes;
     private StringCollection _excludedPaths;
 
     /// <summary>
     private Settings() {
       _preferredAlgorithm = Algorithms.Default;
       _compressionLevel = CompressionLevels.Default;
-      _excludedTypes = new Dictionary<String, bool>();
+      _excludedTypes = new Dictionary<string, bool>();
+      _includedTypes = new Dictionary<string, bool>();
       _excludedPaths = new StringCollection();
       _excludedPaths.Add(".axd");
     }
         }catch(ArgumentException) { }
       }
 
-      ParseExcludedTypes(node.SelectSingleNode("excludedMimeTypes"));
+      ParseTypes(node.SelectSingleNode("includedMimeTypes"), _includedTypes);
+      ParseTypes(node.SelectSingleNode("excludedMimeTypes"), _excludedTypes);
       ParseExcludedPaths(node.SelectSingleNode("excludedPaths"));
 
     }
     /// <param name="mimetype">The MimeType to check.  Can include wildcards like image/* or */xml.</param>
     /// <returns>true if the mime type passed in is excluded from compression, false otherwise</returns>
     public bool IsExcludedMimeType(string mimetype) {
-      if(mimetype == null) return true;
-      return _excludedTypes.ContainsKey(mimetype.ToLower());
+      if(mimetype == null)
+        return true;
+      mimetype = mimetype.ToLower();
+      if(_includedTypes.Count > 0 && !_includedTypes.ContainsKey(mimetype))
+        return true;
+      return _excludedTypes.ContainsKey(mimetype);
     }
 
     /// <summary>
       return _excludedPaths.Contains(relUrl.ToLower());
     }
 
-    private void ParseExcludedTypes(XmlNode node) {
+    private void ParseTypes(XmlNode node, Dictionary<string, bool> types) {
       if(node == null) return;
 
       for(int i = 0; i < node.ChildNodes.Count; ++i) {
         switch(node.ChildNodes[i].LocalName) {
           case "add":
             if(node.ChildNodes[i].Attributes["type"] != null)
-              _excludedTypes[node.ChildNodes[i].Attributes["type"].Value.ToLower()] = true;
+              types[node.ChildNodes[i].Attributes["type"].Value.ToLower()] = true;
             break;
           case "delete":
             if(node.ChildNodes[i].Attributes["type"] != null) {
                String key = node.ChildNodes[i].Attributes["type"].Value.ToLower();
-               if(_excludedTypes.ContainsKey(key))
-                 _excludedTypes.Remove(key);
+               if(types.ContainsKey(key))
+                 types.Remove(key);
             }
             break;
         }

File Tests/SettingsTests.cs

       Assert.That(setting.IsExcludedPath("foo/bar.aspx"));
     }
 
+    [Test] public void IncludeExcludeType() {
+      Settings setting = new Settings(MakeNode(rman.GetString("IncludeExcludeTypes")));
+      Assert.That(!setting.IsExcludedMimeType("image/foo"));
+      Assert.That(setting.IsExcludedMimeType("image/bar"));
+      Assert.That(setting.IsExcludedMimeType("image/baz"));
+    }
+
     private System.Xml.XmlNode MakeNode(string xml) {
       System.Xml.XmlDocument dom = new System.Xml.XmlDocument();
       dom.LoadXml(xml);

File Tests/Tests.csproj

     <Compile Include="Utility.cs">
       <SubType>Code</SubType>
     </Compile>
-    <EmbeddedResource Include="XmlTestDocuments.resx" />
+    <EmbeddedResource Include="XmlTestDocuments.resx">
+      <SubType>Designer</SubType>
+    </EmbeddedResource>
     <Content Include="XmlTestDocuments.xsd" />
   </ItemGroup>
   <ItemGroup>

File Tests/XmlTestDocuments.resx

   &lt;/excludedPaths&gt;
 &lt;/httpCompressionModule&gt;</value>
     </data>
+    <data name="IncludeExcludeTypes">
+      <value>
+      &lt;httpCompressionModule&gt;
+      &lt;includedMimeTypes&gt;
+      &lt;add type="image/foo"/&gt;
+      &lt;add type="image/bar"/&gt;
+      &lt;/includedMimeTypes&gt;
+      &lt;excludedMimeTypes&gt;
+      &lt;add type="image/bar"/&gt;
+      &lt;/excludedMimeTypes&gt;
+      &lt;/httpCompressionModule&gt;
+      </value>
+    </data>
 </root>