Skip to content

Commit

Permalink
Gzip - fix good and bad examples (#8206)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradystroud authored Mar 27, 2024
1 parent b7781cc commit b698f97
Showing 1 changed file with 48 additions and 54 deletions.
102 changes: 48 additions & 54 deletions rules/do-you-use-gzip/rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,59 +49,53 @@ Refer to [Improve the Performance of ASP.NET MVC Web Application Using HTTP Comp

To implement this in ASP.NET MVC, we can utilize `ActionFilterAttribute` and override either `OnActionExecuting` or `OnResultExecuting` method. The below code snippet is being used to check whether the current request browser can accept GZIP/DEFLATE encoding by looking at Accept-Encoding request header. If it finds GZIP encoding in this header, then we would set gzip in Content-encoding in response header and if it supports DEFLATE, then this code would set deflate in Content-encoding.

```csharp
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HTTPCompression.ActionFilters
{
public class CompressAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext
filterContext)
{
HttpRequestBase request = filterContext.HttpContext.Request;

string acceptEncoding = request.Headers["Accept-Encoding"];

if (string.IsNullOrEmpty(acceptEncoding)) return;

acceptEncoding = acceptEncoding.ToUpperInvariant();

HttpResponseBase response = filterContext.HttpContext.Response;

if (acceptEncoding.Contains("GZIP"))
{
response.AppendHeader("Content-encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
else if (acceptEncoding.Contains("DEFLATE"))
{
response.AppendHeader("Content-encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
```csharp
using System;
using System.IO.Compression;
using System.Web.Mvc;

namespace HTTPCompression.ActionFilters;

public class CompressAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
HttpRequestBase request = filterContext.HttpContext.Request;
string acceptEncoding = request.Headers["Accept-Encoding"];

if (string.IsNullOrEmpty(acceptEncoding)) return;

acceptEncoding = acceptEncoding.ToUpperInvariant();
HttpResponseBase response = filterContext.HttpContext.Response;

if (acceptEncoding.Contains("GZIP"))
{
response.AppendHeader("Content-encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
else if (acceptEncoding.Contains("DEFLATE"))
{
response.AppendHeader("Content-encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
}
}
```

```csharp
[Compress]
public ActionResult About()
{
ViewBag.Message = "Your application description";
return View();
}
```

::: bad
Figure: Bad example - Files with large size and slow load time
:::

::: good
![Figure: Good example - Gzipped files with smaller size and faster load time](5.28.7.png)
:::
}

```

```csharp
[Compress]
public ActionResult About()
{
ViewBag.Message = "Your application description";
return View();
}
```

::: bad
Figure: Bad example - Files with large size and slow load time
:::

::: good
![Figure: Good example - Gzipped files with smaller size and faster load time](5.28.7.png)
:::

0 comments on commit b698f97

Please sign in to comment.