Gzip compression with MVC3 issue on IIS 6 - asp.net

I have some doubts about this server response stream compression on MVC. Actually im using my own action filter attribute for compress.
i attached this CompressFilter into my "Home" action of my HomeController that loads the whole home page, but when i check on firebug i dont see the content-encoding:gzip, even the size is too high 18 KBytes. The url is http://goo.gl/5v5yD and this is the request/response headers:
Response headers
-----------------
Date Sat, 17 Mar 2012 18:58:49 GMT
Server Microsoft-IIS/6.0
X-Powered-By ASP.NET
X-AspNet-Version 4.0.30319
X-AspNetMvc-Version 3.0
Cache-Control private, max-age=43200
Expires Sun, 18 Mar 2012 06:58:48 GMT
Last-Modified Sat, 17 Mar 2012 18:58:48 GMT
Content-Type text/html; charset=utf-8
Transfer-Encoding chunked
Request headers
-----------------
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
Accept text/html,application/xhtml+xml,application/xml;q=0.9,q=0.8
Accept-Language es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection keep-alive
Cookie __utma=72740111.1981468378.1331490472.1331490472.1331490472.1; __utmz=72740111.1331490472.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
This is the code of my compression filter:
public class CompressionFilter : ActionFilterAttribute
{
const CompressionMode compress = CompressionMode.Compress;
public override void OnActionExecuting(ActionExecutingContext 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);
}
}
}
Do you know why is not working the compression? im start thinking that maybe is better to try to compress the response using an HttpFilter instead an ActionFilter.

Are you sure? Did you fix it? Maybe your page wasn't refreshed. Ctrl-F5 will do a full refresh. I get the correct response.
FireFox FireBug:
Date Sat, 17 Mar 2012 19:29:58 GMT
Server Microsoft-IIS/6.0
X-Powered-By ASP.NET
X-AspNet-Version 4.0.30319
X-AspNetMvc-Version 3.0
Content-Encoding gzip
Cache-Control private, max-age=43200
Expires Sun, 18 Mar 2012 07:29:58 GMT
Last-Modified Sat, 17 Mar 2012 19:29:58 GMT
Content-Type text/html; charset=utf-8
Content-Length 4710
Chrome Debug:
Cache-Control:private, max-age=43200
Content-Encoding:gzip
Content-Length:4710
Content-Type:text/html; charset=utf-8
Date:Sat, 17 Mar 2012 19:27:20 GMT
Expires:Sun, 18 Mar 2012 07:27:20 GMT
Last-Modified:Sat, 17 Mar 2012 19:27:20 GMT
Server:Microsoft-IIS/6.0
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0
X-Powered-By:ASP.NET

Related

"accept-ranges" header set to "none" instead of "bytes"

I was getting following response after making a curl request to an endpoint
HTTP/2 200
date: Mon, 24 Jun 2019 10:11:39 GMT
content-type: text/html; charset=UTF-8
content-length: 1952
server: nginx
x-powered-by: Express
cache-control: public, max-age=0
last-modified: Tue, 21 May 2019 10:11:37 GMT
vary: Accept-Encoding
x-frame-options: SAMEORIGIN
strict-transport-security: max-age=31536000
x-content-type-options: nosniff
x-xss-protection: 1; mode=block;
accept-ranges: bytes
Ultimately, I don't want accept-ranges: bytes , instead it must be "accept-ranges: none"
I tried following in the nginx configurations
server {
.....
.....
.....
location / {
......
......
max_ranges 0;
proxy_force_ranges on;
......
}
}
What could be the possible issue ?
Did you tried removing "proxy_force_ranges on" ? It enables byte-range support for responses from the proxied server regardless of the “Accept-Ranges” field in these responses.

Getting JSON Tag Value into a text field in ASP.NET

I need to get the "uxfauthorization" tag value into a text box in ASP.NET. I am using curl to send the json request to server and getting below response from the server. I want "uxfauthorization" key value to be in text box so that user can view the authorization key.
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, must-revalidate
Date: Tue, 08 Dec 2015 15:16:45 GMT
Pragma: no-cache
Content-Length: 22
Content-Type: application/json
Expires: -1
SET-COOKIE:WIRELESS_SECURITY_TOKEN=HvsvzzAyux0oXBENeLPwtQ**___currentencryptionkey___36wByp bpGkrOz/CQ6pTbGt44O+PpWvB0gu8hr12vIYa32j2Qg3Na3eylq5h4dZLeAmA6T6kZ4hevGJUjqXeFN8 2DIyEdaO7MVEqiemiGB90F7J0KXfiUdRjw5E5XkV9a; path=/; HttpOnly;
uxf_lastLoginDate: 1449587736928
uxfauthorization:UXF_CSRFToken:csrfcurrentencryptionkey___dpU+veeOOvcV/0mrZKt3V8c4tI677cvo0hodpmXppOQ= UXF_SessionToken:HvsvzzAyux0oXBENeLPwtQ==___currentencryptionkey___36wBypbpGkrOz/CQ6pTbGt44O+PpWvB0gu8hr12vIYa32j2Qg3Na3eylq5h4dZLeAmA6T6kZ4hevGJUjqXeFN82DIyEdaO7MVEqiemiGB90F7J0KXfiUdRjw5E5XkV9a
X-XSS-Protection: 1; mode=block
X-Powered-By: Servlet/3.0 JSP/2.2
{"__EmptyObject__":{}}
Thanks In Advance.
You can use regular expression to parse the value from the text. It can be something like below:
String pattern = #"uxfauthorization:(.*?)";
Regex re = new Regex(pattern);
MatchCollection matches = re.Matches(your_curl_response_here);
foreach(Match match in matches){
// use the match.ToString() for your need
}

Download of xls working in IE/Firefox but named as aspx page in Chrome

I'm having an issue with Chrome renaming an export file to the default of the page name where the export is being initiated from. I've gone through all related forum posts I could find and have tried all suggestions - I'm not seeing any recent posts within the past couple of years.
My code for the export (before any modification attempts) is as follows:
public static void ExportToSpreadsheet(object items, string name)
{
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearHeaders();
response.Charset = "";
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Context-Disposition", "attachment;filename=\"" + name + "\"");
using (StringWriter sw = new StringWriter())
{
using (System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw))
{
System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
dg.DataSource = items;
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
The above code works flawlessly in IE/Firefox. The resulting result headers received (per Chrome net-internals) is:
Cache-Control: private
Content-Type: application/vnd.ms-excel
Server: Microsoft-IIS/8.0
Context-Disposition: attachment;filename="DataExport.xls"
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcTXlfRGF0YVxBVFNNXERldjQwXHBvcnRhbFxDR0lfQXV0b21hdGlvbl9GcmFtZXdvcmtcQXV0b21hdGlvbl9EZWNrLmFzcHg=?=
X-Powered-By: ASP.NET
Date: Fri, 05 Dec 2014 19:37:05 GMT
Content-Length: 86493
I've tried several updates that includes hard coding the filename to "test.xls", clearing content of the response, setting buffer to true, setting CacheControl and Pragma to no-cache, and overriding VerifyRenderingInServerForm() in the page's code-behind. The response headers after applying all these changes are as follows:
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/vnd.ms-excel
Expires: -1
Server: Microsoft-IIS/8.0
Context-Disposition: attachment; filename="test.xls"
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcTXlfRGF0YVxBVFNNXERldjQwXHBvcnRhbFxDR0lfQXV0b21hdGlvbl9GcmFtZXdvcmtcQXV0b21hdGlvbl9EZWNrLmFzcHg=?=
X-Powered-By: ASP.NET
Date: Fri, 05 Dec 2014 19:10:23 GMT
Content-Length: 86493
This still produces the same results where Chrome is not recognizing/accepting the filename and defaults back to the pagename as the downloaded filename.
Any recommendations for how to fix this issue would be greatly appreciated.
I'm using Chrome Version 39.0.2171.71 m

Cache the dynamic image served by http handler

I have developed a http handler to serve images dynamically..
I have a respective last modified date to each image.
So I have implemented http header caching, as below :
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
DateTime lastModified = Convert.ToDateTime("2/1/2013 12:00:00 AM");
string eTag = "test.JPG" + lastModified.ToString();
// set cache info
context.Response.Cache.VaryByHeaders["If-Modified-Since"] = true;
context.Response.Cache.VaryByHeaders["If-None-Match"] = true;
context.Response.Cache.SetLastModified(lastModified);
context.Response.Cache.SetETag(eTag);
//Set cache control header
context.Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
Byte[] imageBytes = CreateImage();
context.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);
}
Note : Here lastModified will be fetched dynamically for each image, also the name will be different for each image.
Response Headers
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: image/jpeg
Last-Modified: Thu, 31 Jan 2013 18:30:00 GMT
Etag: test.JPG2/1/2013 12:00:00 AM
Vary: If-Modified-Since, If-None-Match
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RTpcTGVhcm5pbmdcQ2FjaGVcR2V0SW1hZ2UuYXNoeA==?=
X-Powered-By: ASP.NET
Date: Mon, 08 Jul 2013 11:22:26 GMT
Content-Length: 384411
Request Headers
GET /GetImage.ashx HTTP/1.1
Host: localhost:50432
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Question
This page is getting cached properly..But the next time when I am loading the page with different lastModified value, which makes the etag different too..
But the page is not hitting the handler at all..It is always showing the image from cache...
And as it is not hitting the handler, how can I check lastModified value and serve accordingly..

HttpClient request to local IIS 8.0 does not produce expected headers in the response

I'm making the following request to a local website running in IIS
var httpRequestMessage = new HttpRequestMessage();
httpRequestMessage.RequestUri = new Uri("http://localhost:8081/");
httpRequestMessage.Method = HttpMethod.Get;
var response = new HttpClient().SendAsync(httpRequestMessage).Result;
This produces the following response headers:
HTTP/1.1 200 OK
Accept-Ranges: bytes
Date: Mon, 03 Jun 2013 22:34:25 GMT
ETag: "50c7472eb342ce1:0"
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET
An identical request made via Fiddler produces the following response headers (I've highlighted the differences):
HTTP/1.1 200 OK
Content-Type: text/html
Last-Modified: Fri, 26 Apr 2013 19:20:58 GMT
Accept-Ranges: bytes
ETag: "50c7472eb342ce1:0"
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET
Date: Mon, 03 Jun 2013 22:29:34 GMT
Content-Length: 10
Why is there a difference in response headers?
Am I using HttpClient correctly (aside from the fact I am calling Send synchronously)?
TL;DR;
To access all response headers you need to read both HttpResponseMessage.Headers and HttpResponseMessage.Content.Headers properties.
Long(er) answer:
This, basically:
var response = new HttpClient().GetAsync("http://uri/").Result;
var allHeaders = response.Headers.Union(response.Content.Headers);
foreach (var header in allHeaders)
{
// do stuff
}
I see two issues with this:
The Headers property is not appropriately named: it should really be SomeHeaders or AllHeadersExceptContentHeaders. (I mean, really, when you see a property named Headers, do you expect it to return all headers or some headers? I am pretty sure they are in violation of their own framework design guidelines on this one.)
The MSDN page does not mention at any point the fact this is a subset of all headers and developers should also inspect Content.Headers.

Resources