title: IIS7: Differences between static and dynamic content compression
body: <p>IIS supports two types of compression: <strong>static</strong> content compression and <strong>dynamic</strong> content compression. According to <strong>applicationHost.config</strong>, they are handled by different modules: DynamicCompressionModule (compdyn.dll) and StaticCompressionModule (compstat.dll), and they are configured to compress <strong>different types of requests</strong>. Also, I’m guessing that the dynamic compression does not cache the compressed requests as opposed the static compression (by default, the compressed files are saved to <code>%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files</code>).</p>
  
  <p><em>However, besides those obvious differences, I suspect that there is something else.</em> I think that they hook to the IIS pipeline to a slightly different way. Would anybody have an inside into the some more details?</p>
  
  <p>The way I found out was that <a href="https://stackoverflow.com/questions/5609479/custom-iis-module-conflicting-with-gzip">I was toying with a custom module for modifying CSS files on fly</a>. When the static compression was turned on (and set to handle the default set of files, i.e. also text/css), on cached request my custom module was served the already gzipped content. When I moved text/css to the list of dynamically compressed request, it all started working. But I would like to have a more solid proof that it is really the correct way to do it. Are there some other known consequences / problems?</p>
  
  <p><strong>Update:</strong> I think I may have a theory as to why it’s happening. It may not be 100% correct, but it at least it may explain the observed behavior. I think that the static compression module registers itself to the following events (among some others):</p>
  
  <pre><code>RQ_MAP_REQUEST_HANDLER
  RQ_EXECUTE_REQUEST_HANDLER
  </code></pre>
  
  <p>Then when a request for a static file is served, the static compression module in <a href="http://msdn.microsoft.com/en-us/library/ms693056%28v=VS.90%29.aspx" rel="nofollow noreferrer">OnMapRequestHandler</a> checks whether the file has been compressed before and whether the actual file has not been changed. If so, it will re-map the request to itself (returning the appropriate redirection using <a href="http://msdn.microsoft.com/en-us/library/ms692607.aspx" rel="nofollow noreferrer">IMapHandlerProvider</a>). When it later actually serves the response in <a href="http://msdn.microsoft.com/en-us/library/ms691104%28VS.90%29.aspx" rel="nofollow noreferrer">OnExecuteRequestHandler</a>, it sends the compressed file. If, on the other hand, the file has not been compressed before or if it has changed, it does not do the mapping redirect and lets the static content module serve the request and then later in <a href="http://msdn.microsoft.com/en-us/library/ms693033%28VS.90%29.aspx" rel="nofollow noreferrer">OnPostExecuteRequestHandler</a> compresses the content (and updates its cache). As mentioned above, I'm not saying that this is exactly what's happening (I don't know the source code), it may be only an approximation. Also, the dynamic compression module does not most likely do any of this. It simply compresses the outgoing responses sometimes after RQ_EXECUTE_REQUEST_HANDLER.</p>