Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Forked from pmhsfelix/gist:3949044
Created October 24, 2012 21:43
Show Gist options
  • Save tugberkugurlu/3949100 to your computer and use it in GitHub Desktop.
Save tugberkugurlu/3949100 to your computer and use it in GitHub Desktop.
Canonicalized Header and Resource String
private static string[] GetCanonicalizedHeaders(HttpRequestHeaders headers)
{
return headers
.Where(p => p.Key.StartsWith("x-ms-", StringComparison.InvariantCultureIgnoreCase))
.Select(p => new { Name = p.Key.ToLower(), Value = p.Value.First() })
.OrderBy(p => p.Name)
.Select(p => string.Format("{0}:{1}", p.Name, p.Value))
.ToArray();
}
private string[] CanonicalizedResource(Uri requestUri)
{
var path = requestUri.AbsolutePath;
var query = requestUri.Query.Length <= 1 ? new string[0]
: requestUri.Query.Substring(1).Split('&')
.Select(p =>
{
var s = p.Split('=');
return new
{
Name = Uri.UnescapeDataString(s[0].ToLower()),
Value = Uri.UnescapeDataString(s.Length > 1 ? s[1] : "")
.Replace('+', ' ')
};
})
.GroupBy(p => p.Name)
.OrderBy(g => g.Key)
.Select(g => string.Format("{0}:{1}",
g.Key,
g.OrderBy(s => s.Value)
.Select(s => s.Value)
.Aggregate((a, s) => JoinWith(',', a, s))));
return new[] { "/" + _account + path }.Concat(query).ToArray();
}
private static string JoinWith(char sep, string left, string right)
{
return string.IsNullOrWhiteSpace(left) ? right : left + sep + right;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment