HTTP Response Compression with Self Hosted

All caching returns an optimized response and with the latest v4.5.7 that’s on MyGet you can use the new [CompressResponse] Request Filter attribute to compress normal responses, so you could create a Service that returns a compressed response to clients that support it (e.g. browsers) with something like:

[Route("/compress/{Path*}")]
public class CompressFile
{
    public string Path { get; set; }
}

[CompressResponse]
public class CompressServices : Service
{
    public object Any(CompressFile request)
    {
        var file = VirtualFileSources.GetFile(request.Path);
        if (file == null)
            throw HttpError.NotFound(request.Path + " does not exist");

        return new HttpResult(file);
    }
}

Then call it with /compress/path/to.file.txt which will return a compressed file in clients that Accept gzip or deflate encoding.

I’ll also look into also making it configurable to compress static files as well.