Skip to content

Instantly share code, notes, and snippets.

@bwatts
Forked from daanl/gist:5691791
Last active December 17, 2015 23:49
Show Gist options
  • Save bwatts/5691810 to your computer and use it in GitHub Desktop.
Save bwatts/5691810 to your computer and use it in GitHub Desktop.
public string GetCanonicalizedHeaders(HttpWebRequest request)
{
ArrayList headerNameList = new ArrayList();
StringBuilder sb = new StringBuilder();
foreach (string headerName in request.Headers.Keys)
{
if (headerName.ToLowerInvariant().StartsWith("x-ms-", StringComparison.Ordinal))
{
headerNameList.Add(headerName.ToLowerInvariant());
}
}
headerNameList.Sort();
foreach (string headerName in headerNameList)
{
StringBuilder builder = new StringBuilder(headerName);
string separator = ":";
foreach (string headerValue in GetHeaderValues(request.Headers, headerName))
{
string trimmedValue = headerValue.Replace("\r\n", String.Empty);
builder.Append(separator);
builder.Append(trimmedValue);
separator = ",";
}
sb.Append(builder.ToString());
sb.Append("\n");
}
return sb.ToString();
}
private string GetCanonicalizedHeaders(HttpClient httpClient)
{
var headers =
from header in httpClient.DefaultRequestHeaders
where header.Key.StartsWith("x-ms-", StringComparison.InvariantCultureIgnoreCase)
orderby header.Key
select new
{
Key = header.Key,
Values = header.Value.Select(value => value.Replace(Environment.Newline, ""))
};
var headerText = new StringBuilder();
foreach(var header in headers)
{
headerText.Append(header.Key);
var separator = ":";
foreach(var value in header.Values)
{
builder.Append(separator).Append(value);
separator = ",";
}
headerText.AppendLine();
}
return headerText.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment