Turning off Pretty Printing in ASP.Net

5/9/2008

Have you ever noticed that when a page is sent in ASP.Net, that it comes in a "pretty printing" sort of format? Ever wanted to get rid of those extra spaces, tabs, etc. to save some bandwidth? It's actually really easy to accomplish. All that you need to do is change the stream that that page uses for output. This can be done by writing your own Stream and changing the response's filter to a new instance of that stream.

The Stream

The stream code is as follows:

   1: /*
   2: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
   3: 
   4: Permission is hereby granted, free of charge, to any person obtaining a copy
   5: of this software and associated documentation files (the "Software"), to deal
   6: in the Software without restriction, including without limitation the rights
   7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   8: copies of the Software, and to permit persons to whom the Software is
   9: furnished to do so, subject to the following conditions:
  10: 
  11: The above copyright notice and this permission notice shall be included in
  12: all copies or substantial portions of the Software.
  13: 
  14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20: THE SOFTWARE.*/
  21:  
  22: #region Usings
  23: using System;
  24: using System.IO;
  25: using System.Text;
  26: using System.Text.RegularExpressions;
  27: #endregion
  28:  
  29: namespace Site
  30: {
  31:     /// <summary>
  32:     /// Removes "pretty printing"
  33:     /// </summary>
  34:     public class UglyStream : Stream
  35:     {
  36:         #region Variables
  37:         private string Compression;
  38:         private Stream StreamUsing;
  39:         private string FinalString;
  40:         #endregion
  41:  
  42:         #region Functions and Properties
  43:  
  44:         /// <summary>
  45:         /// Constructor
  46:         /// </summary>
  47:         /// <param name="StreamUsing">The stream for the page</param>
  48:         /// <param name="Compression">The compression we're using (gzip or deflate)</param>
  49:         public UglyStream(Stream StreamUsing, string Compression)
  50:         {
  51:             this.Compression = Compression;
  52:             this.StreamUsing = StreamUsing;
  53:         }
  54:  
  55:         /// <summary>
  56:         /// Doesn't deal with reading
  57:         /// </summary>
  58:         public override bool CanRead
  59:         {
  60:             get { return false; }
  61:         }
  62:  
  63:         /// <summary>
  64:         /// No seeking
  65:         /// </summary>
  66:         public override bool CanSeek
  67:         {
  68:             get { return false; }
  69:         }
  70:  
  71:         /// <summary>
  72:         /// Can write out though
  73:         /// </summary>
  74:         public override bool CanWrite
  75:         {
  76:             get { return true; }
  77:         }
  78:  
  79:         /// <summary>
  80:         /// Nothing to flush
  81:         /// </summary>
  82:         public override void Flush()
  83:         {
  84:             try
  85:             {
  86:                 FinalString = Regex.Replace(FinalString, "/// <.+>", "");
  87:                 FinalString = Regex.Replace(FinalString, @">[\s\S]*?<", new MatchEvaluator(Evaluate));
  88:                 byte[] data = Encoding.ASCII.GetBytes(FinalString);
  89:                 if (Compression.Equals("deflate"))
  90:                 {
  91:                     data = Utilities.Compression.Deflate.Compress(data);
  92:                 }
  93:                 else
  94:                 {
  95:                     data = Utilities.Compression.GZip.Compress(data);
  96:                 }
  97:                 StreamUsing.Write(data, 0, data.Length);
  98:                 FinalString = "";
  99:             }
 100:             catch { throw; }
 101:         }
 102:  
 103:         /// <summary>
 104:         /// Don't worry about
 105:         /// </summary>
 106:         public override long Length
 107:         {
 108:             get { throw new NotImplementedException(); }
 109:         }
 110:  
 111:         /// <summary>
 112:         /// No position to take care of
 113:         /// </summary>
 114:         public override long Position
 115:         {
 116:             get
 117:             {
 118:                 throw new NotImplementedException();
 119:             }
 120:             set
 121:             {
 122:                 throw new NotImplementedException();
 123:             }
 124:         }
 125:  
 126:         /// <summary>
 127:         /// Don't worry about
 128:         /// </summary>
 129:         /// <param name="buffer"></param>
 130:         /// <param name="offset"></param>
 131:         /// <param name="count"></param>
 132:         /// <returns></returns>
 133:         public override int Read(byte[] buffer, int offset, int count)
 134:         {
 135:             throw new NotImplementedException();
 136:         }
 137:  
 138:         /// <summary>
 139:         /// Once again not implemented
 140:         /// </summary>
 141:         /// <param name="offset"></param>
 142:         /// <param name="origin"></param>
 143:         /// <returns></returns>
 144:         public override long Seek(long offset, SeekOrigin origin)
 145:         {
 146:             throw new NotImplementedException();
 147:         }
 148:  
 149:         /// <summary>
 150:         /// Don't worry about
 151:         /// </summary>
 152:         /// <param name="value"></param>
 153:         public override void SetLength(long value)
 154:         {
 155:             throw new NotImplementedException();
 156:         }
 157:  
 158:         /// <summary>
 159:         /// Actually writes out the data
 160:         /// </summary>
 161:         /// <param name="buffer">the page's data in byte form</param>
 162:         /// <param name="offset">offset of the data</param>
 163:         /// <param name="count">the amount of data</param>
 164:         public override void Write(byte[] buffer, int offset, int count)
 165:         {
 166:             try
 167:             {
 168:                 byte[] data = new byte[count];
 169:                 Buffer.BlockCopy(buffer, offset, data, 0, count);
 170:                 string inputstring = Encoding.ASCII.GetString(data);
 171:                 FinalString += inputstring;
 172:             }
 173:             catch { throw; }
 174:         }
 175:  
 176:         /// <summary>
 177:         /// Evaluates whether the text has spaces, page breaks, etc. and removes them.
 178:         /// </summary>
 179:         /// <param name="m"></param>
 180:         /// <returns></returns>
 181:         protected string Evaluate(Match Matcher)
 182:         {
 183:             try
 184:             {
 185:                 string MyString = Matcher.ToString();
 186:                 MyString = Regex.Replace(MyString, @"\r\n\s*", "");
 187:                 return MyString;
 188:             }
 189:             catch { throw; }
 190:         }
 191:  
 192:         #endregion
 193:     }
 194: }

That's all there is to it. Note that it uses some compression code from my utility library. Specifically it uses my gzip and deflate classes. So you may wish to go and grab those classes or the whole library (or you can just strip that code, the choice is yours). Anyway, you may notice that most of the items aren't even implemented, the only ones that matter are Flush and Write. Both of these have to be implemented. Other than that, who cares. Then all that is left to do is on the page we wish to use it, we add:

   1: Response.Filter=new UglyStream(Response.Filter,"deflate");

And voil??, we have turned off pretty printing. You can even do this in an HttpModule and just set the filter on each page that is requested. I'm actually a bit surprised that this sort of stuff isn't done by default. The viewstate compression, HTTP compression, pretty printing, etc. or at least give us an easy switch in the config file or something. Maybe it is there and I've just been too lazy to find it, but I'm guessing no... Anyway, try out the code, leave feedback, and happy coding.



Comments