Scott Hanselman wrote today about P3P and adding headers, which got me thinking. For this site (however dead it is) I can control the actual server, so adding the P3P headers was easy. However I have a community site bubbling under and if it takes off it will be need to be moved somewhere professional and not my attic. With professional hosting comes that lack of control and the need to have a better way of adding headers, and Scott made a throw away comment that sparked a day of fun;
If you don't have access to your IIS instance or your ISP doesn't want to help you out, you can also add these HTTP Headers programmatically using an HttpModule.
So I present to you idunno.Web.HttpHeaders. This is a configurable HttpModule, allowing you to use web.config to specify what headers and values you wish added to requests. I will freely admit to taking the shell from Blowery's HTTP Compression module and it works pretty much the same. Add the right lines to your web.config and away you go.
First add the references to the configuration section <configSections>
<sectionGroup name="idunno.web">
<section name="httpHeaders"
type="idunno.Web.HttpHeaders.SectionHandler, idunno.Web.HttpHeaders"/>
</sectionGroup>
</configSections>
Then add the configuration section
<idunno.web>
<httpHeaders>
<headers>
<add name="X-Test" value="pigeon" />
<remove name="X-Powered-By" />
</headers>
<excludedMimeTypes>
<add type="image/jpeg"/>
<add type="image/gif"/>
</excludedMimeTypes>
<excludedPaths>
<add path="NoCustomerHeaders.aspx"/>
</excludedPaths>
</httpHeaders>
</idunno.web>
And finally add the module into the pipeline
<system.web>
...
<httpModules>
<add name="HttpHeadersModule"
type="idunno.Web.HttpHeaders.HttpModule, idunno.Web.HttpHeaders"/>
</httpModules>
...
</system.web>
Note that if you use the same name as an existing header your value should overwrite the existing value.