Created
June 17, 2015 17:13
-
-
Save phillip-haydon/9a71f6c6a7a0fa958862 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Startup | |
{ | |
public void Configuration(IAppBuilder app) | |
{ | |
app.Use(typeof(InjectDebugJsAndMinifyHtml)); | |
app.UseNancy(); | |
} | |
} | |
public class InjectDebugJsAndMinifyHtml : OwinMiddleware | |
{ | |
public InjectDebugJsAndMinifyHtml(OwinMiddleware next) | |
: base(next) | |
{ | |
} | |
public async override Task Invoke(IOwinContext context) | |
{ | |
if (context.Request.Accept == null) | |
{ | |
await Next.Invoke(context); | |
return; | |
} | |
if (context.Response.ContentType != "text/html" || !context.Request.Accept.Contains("text/html")) | |
{ | |
await Next.Invoke(context); | |
return; | |
} | |
var responseBuffer = new MemoryStream(); | |
var responseStream = new ContentStream(responseBuffer, context.Response.Body, context.Response.ContentType); | |
context.Response.Body = responseStream; | |
await Next.Invoke(context); | |
} | |
} | |
public class ContentStream : Stream | |
{ | |
protected readonly Stream Buffer; | |
protected readonly Stream Stream; | |
private readonly string contentType; | |
public ContentStream(Stream buffer, Stream stream, string contentType) | |
{ | |
Buffer = buffer; | |
Stream = stream; | |
this.contentType = contentType; | |
} | |
protected void WriteContent(byte[] buffer, int offset, int count) | |
{ | |
Buffer.Write(buffer, offset, count); | |
} | |
#region System.IO.Stream Overrides | |
public override bool CanRead | |
{ | |
get { return Stream.CanRead; } | |
} | |
public override bool CanSeek | |
{ | |
get { return Stream.CanSeek; } | |
} | |
public override bool CanWrite | |
{ | |
get { return Stream.CanWrite; } | |
} | |
public override void Flush() | |
{ | |
Stream.Flush(); | |
} | |
public override long Length | |
{ | |
get { return Stream.Length; } | |
} | |
public override long Position | |
{ | |
get { return Stream.Position; } | |
set { Stream.Position = value; } | |
} | |
public override int Read(byte[] buffer, int offset, int count) | |
{ | |
return Stream.Read(buffer, offset, count); | |
} | |
public override long Seek(long offset, SeekOrigin origin) | |
{ | |
return Stream.Seek(offset, origin); | |
} | |
public override void SetLength(long value) | |
{ | |
Stream.SetLength(value); | |
} | |
private static readonly HtmlContentCompressor Compressor = new HtmlContentCompressor(); | |
// ReSharper disable once RedundantAssignment | |
public override void Write(byte[] buffer, int offset, int count) | |
{ | |
if (contentType == "text/html") | |
{ | |
var str = Encoding.Default.GetString(buffer); | |
var compressed = Compressor.Compress(str); | |
buffer = Encoding.UTF8.GetBytes(compressed); | |
count = buffer.Length; | |
} | |
Stream.Write(buffer, offset, count); | |
} | |
#endregion | |
#region IDisposable Implementation | |
protected override void Dispose(bool disposing) | |
{ | |
Buffer.Dispose(); | |
base.Dispose(disposing); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment