Skip to content

Commit

Permalink
fixes #14 and fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Alexandre committed Jul 27, 2017
1 parent caae468 commit 9287d72
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
### Fixed
* Fix tab character '/t' breaking dependencies.json parsing. [(#10)](https://github.com/nosinovacao/name-sdk/issues/9)
* Fix the table ui endpoint failing to load the manifest behind a HTTPS proxy. [(#9)](https://github.com/nosinovacao/name-sdk/issues/9)
* Fix NAME not retrying to fetch a service dependency manifest from the correct endpoint when the dependency returned a successful status code. [(#8)](https://github.com/nosinovacao/name-sdk/issues/8)
### Added
* A relevant error message is now shown when a service dependency does not have NAME installed. [(#14)](https://github.com/nosinovacao/name-sdk/issues/14)

## v1.0.0 - 2017-07-07
* Initial open source release.
Expand Down
48 changes: 27 additions & 21 deletions src/NAME/Service/ServiceVersionResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,12 @@ public ServiceVersionResolver(IConnectionStringProvider connectionStringProvider
/// <exception cref="VersionParsingException">The version from manifest cannot be parsed</exception>
public override async Task<IEnumerable<DependencyVersion>> GetVersions()
{
string serviceConnectionString;
if (!this._connectionStringProvider.TryGetConnectionString(out serviceConnectionString))
if (!this._connectionStringProvider.TryGetConnectionString(out string serviceConnectionString))
throw new ConnectionStringNotFoundException(this._connectionStringProvider.ToString());

var serviceUri = new Uri(serviceConnectionString);
var serviceUri = new Uri(serviceConnectionString + Constants.MANIFEST_ENDPOINT_HEADER_NAME);

string jsonContents = await this.GetManifest(serviceUri.ToString().TrimEnd('/') + Constants.MANIFEST_ENDPOINT, true, serviceUri, this.HopNumber)
string jsonContents = await this.GetManifest(serviceUri, true, this.HopNumber)
.ConfigureAwait(false);

DependencyVersion dependencyVersion;
Expand Down Expand Up @@ -130,11 +129,10 @@ public async Task GetDependantManifests(DependencyVersion rootDependency)
continue;
}

var uri = new Uri(dependencyUrl);
var endpoint = dependencyUrl.TrimEnd('/') + Constants.MANIFEST_ENDPOINT;
var uri = new Uri(dependencyUrl.TrimEnd('/') + Constants.MANIFEST_ENDPOINT);
try
{
var manifest = await this.GetManifest(endpoint, true, uri, nextHop);
var manifest = await this.GetManifest(uri, true, nextHop);

JsonNode manifestJsonNode = Json.Json.Parse(manifest);
JsonNode infrastructureDependencies = manifestJsonNode[InfrastructureDependenciesKey];
Expand All @@ -150,10 +148,9 @@ public async Task GetDependantManifests(DependencyVersion rootDependency)
}
}

private async Task<string> GetManifest(string endpoint, bool retry, Uri serviceUri, int hop)
private async Task<string> GetManifest(Uri endpointUri, bool retry, int hop)
{
string jsonContents;
HttpWebRequest request = this._webRequestFactory.Invoke(endpoint);
HttpWebRequest request = this._webRequestFactory.Invoke(endpointUri.AbsoluteUri);
request.ContentType = "application/json; charset=utf-8";
request.Headers[Constants.HOP_COUNT_HEADER_NAME] = hop.ToString();

Expand All @@ -179,10 +176,23 @@ private async Task<string> GetManifest(string endpoint, bool retry, Uri serviceU
throw new NAMEException($"{SupportedDependencies.Service}: The service returned an unsuccessfull status code: {response.StatusCode}.");
}

using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
var headerManifestEndpoint = response.Headers[Constants.MANIFEST_ENDPOINT_HEADER_NAME];
if (headerManifestEndpoint == null)
throw new NAMEException($"{SupportedDependencies.Service}: NAME is not installed.");

Uri uriFromHeader = new Uri(endpointUri, headerManifestEndpoint);

if (uriFromHeader != endpointUri && retry)
{
return await this.GetManifest(uriFromHeader, false, hop);
}
else
{
jsonContents = await reader.ReadToEndAsync().ConfigureAwait(false);
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
return await reader.ReadToEndAsync().ConfigureAwait(false);
}
}
}
}
Expand All @@ -200,12 +210,10 @@ private async Task<string> GetManifest(string endpoint, bool retry, Uri serviceU
if (retry)
{
var manifestEndpoint = response.Headers[Constants.MANIFEST_ENDPOINT_HEADER_NAME];
if (manifestEndpoint != null)
{
string baseUri = serviceUri.GetComponents(UriComponents.Scheme | UriComponents.StrongAuthority, UriFormat.Unescaped).TrimEnd('/');
baseUri += manifestEndpoint;
return await this.GetManifest(baseUri, false, serviceUri, hop);
}
if (manifestEndpoint == null)
throw new NAMEException($"{SupportedDependencies.Service}: NAME is not installed.");

return await this.GetManifest(new Uri(endpointUri, manifestEndpoint), false, hop);
}

if ((int)response.StatusCode == Constants.SERVICE_HOPS_ERROR_STATUS_CODE)
Expand All @@ -215,8 +223,6 @@ private async Task<string> GetManifest(string endpoint, bool retry, Uri serviceU
}
throw new DependencyNotReachableException(SupportedDependencies.Service.ToString(), ex);
}

return jsonContents;
}
}
}

0 comments on commit 9287d72

Please sign in to comment.