/// <summary> /// Compresses the specified instance. /// </summary> /// <param name="instance">The instance.</param> public static void Compress(this HttpContextBase instance) { Invariant.IsNotNull(instance, "instance"); HttpRequestBase httpRequest = instance.Request; // IE6 if ((httpRequest.Browser.MajorVersion < 7) && httpRequest.Browser.IsBrowser("IE")) { return; } // No explicit format is specified, so we have to detect the format from the // accepted mime types. // Webkit sends wrongly ordered accept types, so we have to identify it // more info http://www.gethifi.com/blog/webkit-team-admits-accept-header-error bool isDefectiveBrowser = instance.Request.Browser.IsBrowser("chrome") || instance.Request.Browser.IsBrowser("safari"); string preferedAcceptEncoding = QValueSorter.Sort(httpRequest.AcceptTypes, isDefectiveBrowser).FirstOrDefault(); if (string.IsNullOrEmpty(preferedAcceptEncoding)) { return; } HttpResponseBase httpResponse = instance.Response; Action <string> compress = encoding => { if (!string.IsNullOrEmpty(httpResponse.RedirectLocation)) { return; } try { httpResponse.AppendHeader("Content-encoding", encoding); httpResponse.Filter = encoding.Equals("deflate") ? new DeflateStream(httpResponse.Filter, CompressionMode.Compress) : new GZipStream(httpResponse.Filter, CompressionMode.Compress) as Stream; } catch (HttpException) { } }; if (preferedAcceptEncoding.Equals("*", StringComparison.Ordinal) || preferedAcceptEncoding.Equals("gzip", StringComparison.OrdinalIgnoreCase)) { compress("gzip"); } else if (preferedAcceptEncoding.Equals("deflate", StringComparison.OrdinalIgnoreCase)) { compress("deflate"); } }
public virtual IResponder FindMatching([NotNull] ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } string action = context.RouteData.ActionName(); // Filter the responders which does not has this action in exclude list. IEnumerable <IResponder> filtered = Items.Where(r => r.CanRespondToAction(action)).ToList(); string format = context.HttpContext.Request.QueryString[QueryStringFormatParameterName]; IResponder responder = filtered.FirstOrDefault(r => r.CanRespondToFormat(format)); // Found matched responder, no need to go further if (responder != null) { return(responder); } // If explicit format is specified but no matching responder exist, do not need to continue if (!string.IsNullOrWhiteSpace(format)) { return(null); } // No explicit format is specified, so we have to detect the format from the // accepted mime types. // Webkit sends wrongly ordered accept types, so we have to identify it // more info http://www.gethifi.com/blog/webkit-team-admits-accept-header-error bool isDefectiveBrowser = context.HttpContext.Request.Browser.IsBrowser("chrome") || context.HttpContext.Request.Browser.IsBrowser("safari"); IEnumerable <string> mimeTypes = QValueSorter.Sort(context.HttpContext.Request.AcceptTypes, isDefectiveBrowser); foreach (string mimeType in mimeTypes) { string type = mimeType; responder = filtered.FirstOrDefault(r => r.CanRespondToMimeType(type)); if (responder != null) { break; } } return(responder); }
/// <summary> /// Compresses the specified instance. /// </summary> /// <param name="instance">The instance.</param> public static void Compress([NotNull] this HttpContextBase instance) { Invariant.IsNotNull(instance, "instance"); HttpRequestBase httpRequest = instance.Request; // IE6 if ((httpRequest.Browser.MajorVersion < 7) && httpRequest.Browser.IsBrowser("IE")) { return; } string preferedAcceptEncoding = QValueSorter.Sort(httpRequest.Headers["Accept-Encoding"]).FirstOrDefault(); if (string.IsNullOrEmpty(preferedAcceptEncoding)) { return; } HttpResponseBase httpResponse = instance.Response; Action <string> compress = encoding => { if (!string.IsNullOrEmpty(httpResponse.RedirectLocation)) { return; } try { httpResponse.AppendHeader("Content-encoding", encoding); httpResponse.Filter = encoding.Equals("deflate") ? new DeflateStream(httpResponse.Filter, CompressionMode.Compress) : new GZipStream(httpResponse.Filter, CompressionMode.Compress) as Stream; } catch (HttpException) { } }; if (preferedAcceptEncoding.Equals("*", StringComparison.Ordinal) || preferedAcceptEncoding.Equals("gzip", StringComparison.OrdinalIgnoreCase)) { compress("gzip"); } else if (preferedAcceptEncoding.Equals("deflate", StringComparison.OrdinalIgnoreCase)) { compress("deflate"); } }