-
-
Save tugberkugurlu/3949100 to your computer and use it in GitHub Desktop.
Canonicalized Header and Resource String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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