Skip to content

Commit

Permalink
workaround case-insensitive headers because we lack access to the res…
Browse files Browse the repository at this point in the history
…ponse headers
  • Loading branch information
shiftkey committed Apr 9, 2020
1 parent 272c2d0 commit e87b851
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
25 changes: 17 additions & 8 deletions Octokit/Http/ApiInfoParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ internal static class ApiInfoParser
static readonly Regex _linkRelRegex = new Regex("rel=\"(next|prev|first|last)\"", regexOptions);
static readonly Regex _linkUriRegex = new Regex("<(.+)>", regexOptions);

static KeyValuePair<string, string> LookupHeader(IDictionary<string, string> headers, string key)
{
return headers.FirstOrDefault(h => string.Equals(h.Key, key, StringComparison.OrdinalIgnoreCase));
}

public static ApiInfo ParseResponseHeaders(IDictionary<string, string> responseHeaders)
{
Ensure.ArgumentNotNull(responseHeaders, nameof(responseHeaders));
Expand All @@ -25,28 +30,32 @@ public static ApiInfo ParseResponseHeaders(IDictionary<string, string> responseH
var acceptedOauthScopes = new List<string>();
string etag = null;

if (responseHeaders.ContainsKey("X-Accepted-OAuth-Scopes"))
var acceptedOauthScopesKey = LookupHeader(responseHeaders, "X-Accepted-OAuth-Scopes");
if (!acceptedOauthScopesKey.Equals(default(KeyValuePair<string, string>)))
{
acceptedOauthScopes.AddRange(responseHeaders["X-Accepted-OAuth-Scopes"]
acceptedOauthScopes.AddRange(acceptedOauthScopesKey.Value
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim()));
}

if (responseHeaders.ContainsKey("X-OAuth-Scopes"))
var oauthScopesKey = LookupHeader(responseHeaders, "X-OAuth-Scopes");
if (!oauthScopesKey.Equals(default(KeyValuePair<string, string>)))
{
oauthScopes.AddRange(responseHeaders["X-OAuth-Scopes"]
oauthScopes.AddRange(oauthScopesKey.Value
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim()));
}

if (responseHeaders.ContainsKey("ETag"))
var etagKey = LookupHeader(responseHeaders, "ETag");
if (!etagKey.Equals(default(KeyValuePair<string, string>)))
{
etag = responseHeaders["ETag"];
etag = etagKey.Value;
}

if (responseHeaders.ContainsKey("Link"))
var linkKey = LookupHeader(responseHeaders, "Link");
if (!linkKey.Equals(default(KeyValuePair<string, string>)))
{
var links = responseHeaders["Link"].Split(',');
var links = linkKey.Value.Split(',');
foreach (var link in links)
{
var relMatch = _linkRelRegex.Match(link);
Expand Down
21 changes: 19 additions & 2 deletions Octokit/Http/RateLimit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
#if !NO_SERIALIZABLE
using System.Runtime.Serialization;
#endif
Expand Down Expand Up @@ -65,11 +66,27 @@ public RateLimit(int limit, int remaining, long resetAsUtcEpochSeconds)
[Parameter(Key = "reset")]
public long ResetAsUtcEpochSeconds { get; private set; }

static KeyValuePair<string, string> LookupHeader(IDictionary<string, string> headers, string key)
{
return headers.FirstOrDefault(h => string.Equals(h.Key, key, StringComparison.OrdinalIgnoreCase));
}

static long GetHeaderValueAsInt32Safe(IDictionary<string, string> responseHeaders, string key)
{
string value;
long result;
return !responseHeaders.TryGetValue(key, out value) || value == null || !long.TryParse(value, out result)

var foundKey = LookupHeader(responseHeaders, key);
if (foundKey.Equals(default(KeyValuePair<string, string>)))
{
return 0;
}

if (string.IsNullOrWhiteSpace(foundKey.Value))
{
return 0;
}

return !long.TryParse(foundKey.Value, out result)
? 0
: result;
}
Expand Down

0 comments on commit e87b851

Please sign in to comment.