Skip to content

Instantly share code, notes, and snippets.

@jbouy
Created August 27, 2013 03:22
Show Gist options
  • Save jbouy/6349313 to your computer and use it in GitHub Desktop.
Save jbouy/6349313 to your computer and use it in GitHub Desktop.
/// <summary>
/// Determines if GZip is supported
/// </summary>
/// <returns></returns>
public static bool IsGZipSupported()
{
string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
if (!string.IsNullOrEmpty(AcceptEncoding) &&
(AcceptEncoding.Contains("gzip") || AcceptEncoding.Contains("deflate")))
return true;
return false;
}
/// <summary>
/// Sets up the current page or handler to use GZip through a Response.Filter
/// IMPORTANT:
/// You have to call this method before any output is generated!
/// </summary>
public static void GZipEncodePage()
{
HttpResponse Response = HttpContext.Current.Response;
if (IsGZipSupported())
{
string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
if (AcceptEncoding.Contains("deflate"))
{
Response.Filter = new System.IO.Compression.DeflateStream(Response.Filter,
System.IO.Compression.CompressionMode.Compress);
Response.Headers.Remove("Content-Encoding");
Response.AppendHeader("Content-Encoding", "deflate");
}
else
{
Response.Filter = new System.IO.Compression.GZipStream(Response.Filter,
System.IO.Compression.CompressionMode.Compress);
Response.Headers.Remove("Content-Encoding");
Response.AppendHeader("Content-Encoding", "gzip");
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
WebUtils.GZipEncodePage();
Entry = WebLogFactory.GetEntry();
var entries = Entry.GetLastEntries(App.Configuration.ShowEntryCount, "pk,Title,SafeTitle,Body,Entered,Feedback,Location,ShowTopAd", "TEntries");
if (entries == null)
throw new ApplicationException("Couldn't load WebLog Entries: " + Entry.ErrorMessage);
this.repEntries.DataSource = entries;
this.repEntries.DataBind();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment