private static NameValueCollection GetRequiredHeaders(HttpRequest request)
{
// A useragent might be url encoded if SetOverrideBrowser was used in a previous request.
// This is checked by comparing the escaped string is different against the original.
NameValueCollection headers;
var escapedUA = request.UserAgent != null ?
Uri.UnescapeDataString(request.UserAgent).Replace('+', ' ') :
null;
if (escapedUA != request.UserAgent)
{
// Requests after SetOverrideBrowser are still expected to use the overriden alias.
// The override useragent is stored in cookie which ASP.NET unpacks automatically, however
// it is url encoded with pluses instead of spaces. This code transforms the useragent back,
// but a new NameValueCollection is needed as the original collection cannot be modified.
headers = new NameValueCollection(request.Headers.Count, request.Headers);
foreach (var componentHeader in ActiveProvider.DataSet.Components.SelectMany(i => i.HttpHeaders).Distinct())
{
if (headers[componentHeader] != null)
{
headers[componentHeader] = escapedUA;
break;
}
}
}
else
{
// No MVC defect so use the headers unaltered.
headers = request.Headers;
}
return headers;
}