|
using System.Configuration;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Configuration;
namespace MinifyContent
{
class ResponseStream : MemoryStream
{
Stream outputStream;
string outputContentType;
public ResponseStream(HttpResponse response)
: this(response.Filter, response.ContentType)
{ }
public ResponseStream(Stream stream)
: this(stream, "text/html")
{ }
public ResponseStream(Stream stream, string contentType)
{
outputStream = stream;
outputContentType = contentType;
}
public override void Write(byte[] buffer, int offset, int count)
{
//convert byte array to string
string bufferString = UTF8Encoding.UTF8.GetString(buffer);
//loop all the expressions to minify content
foreach (Expressions.Expression expr in Expressions.ExpressionCollection.Instance.Collections())
{
//Check if expression can be ignored in CSS case
if (outputContentType == "text/css" && expr.EscapeInCss)
continue;
//TODO: Javascript stuff!!!
//if custom handler is define then execute it.
if (expr.ReplaceHandler == null)
bufferString = expr.RegularExpression.Replace(bufferString, expr.ReplaceWith);
else
bufferString = expr.ReplaceHandler(expr, bufferString);
}
//write the content with converting in bytes
outputStream.Write(UTF8Encoding.UTF8.GetBytes(bufferString), offset, UTF8Encoding.UTF8.GetByteCount(bufferString));
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Mehul Thakkar is having 8 yrs of experience in IT industry. He is having good command over Ms .Net and Ms Sql Server