diff options
Diffstat (limited to 'src/DotNetOpenAuth.OpenId/Yadis/DiscoveryResult.cs')
-rw-r--r-- | src/DotNetOpenAuth.OpenId/Yadis/DiscoveryResult.cs | 79 |
1 files changed, 51 insertions, 28 deletions
diff --git a/src/DotNetOpenAuth.OpenId/Yadis/DiscoveryResult.cs b/src/DotNetOpenAuth.OpenId/Yadis/DiscoveryResult.cs index 06c6fc7..8266ff0 100644 --- a/src/DotNetOpenAuth.OpenId/Yadis/DiscoveryResult.cs +++ b/src/DotNetOpenAuth.OpenId/Yadis/DiscoveryResult.cs @@ -7,10 +7,15 @@ namespace DotNetOpenAuth.Yadis { using System; using System.IO; + using System.Net; + using System.Net.Http; + using System.Net.Http.Headers; using System.Net.Mime; + using System.Threading.Tasks; using System.Web.UI.HtmlControls; using System.Xml; using DotNetOpenAuth.Messaging; + using Validation; /// <summary> /// Contains the result of YADIS discovery. @@ -20,30 +25,12 @@ namespace DotNetOpenAuth.Yadis { /// The original web response, backed up here if the final web response is the preferred response to use /// in case it turns out to not work out. /// </summary> - private CachedDirectWebResponse htmlFallback; + private HttpResponseMessage htmlFallback; /// <summary> - /// Initializes a new instance of the <see cref="DiscoveryResult"/> class. + /// Prevents a default instance of the <see cref="DiscoveryResult" /> class from being created. /// </summary> - /// <param name="requestUri">The user-supplied identifier.</param> - /// <param name="initialResponse">The initial response.</param> - /// <param name="finalResponse">The final response.</param> - public DiscoveryResult(Uri requestUri, CachedDirectWebResponse initialResponse, CachedDirectWebResponse finalResponse) { - this.RequestUri = requestUri; - this.NormalizedUri = initialResponse.FinalUri; - if (finalResponse == null || finalResponse.Status != System.Net.HttpStatusCode.OK) { - this.ApplyHtmlResponse(initialResponse); - } else { - this.ContentType = finalResponse.ContentType; - this.ResponseText = finalResponse.GetResponseString(); - this.IsXrds = true; - if (initialResponse != finalResponse) { - this.YadisLocation = finalResponse.RequestUri; - } - - // Back up the initial HTML response in case the XRDS is not useful. - this.htmlFallback = initialResponse; - } + private DiscoveryResult() { } /// <summary> @@ -68,7 +55,7 @@ namespace DotNetOpenAuth.Yadis { /// <summary> /// Gets the Content-Type associated with the <see cref="ResponseText"/>. /// </summary> - public ContentType ContentType { get; private set; } + public MediaTypeHeaderValue ContentType { get; private set; } /// <summary> /// Gets the text in the final response. @@ -84,11 +71,42 @@ namespace DotNetOpenAuth.Yadis { public bool IsXrds { get; private set; } /// <summary> + /// Initializes a new instance of the <see cref="DiscoveryResult"/> class. + /// </summary> + /// <param name="requestUri">The request URI.</param> + /// <param name="initialResponse">The initial response.</param> + /// <param name="finalResponse">The final response.</param> + /// <returns>The newly initialized instance.</returns> + internal static async Task<DiscoveryResult> CreateAsync(Uri requestUri, HttpResponseMessage initialResponse, HttpResponseMessage finalResponse) { + var result = new DiscoveryResult(); + result.RequestUri = requestUri; + result.NormalizedUri = initialResponse.RequestMessage.RequestUri; + if (finalResponse == null || finalResponse.StatusCode != HttpStatusCode.OK) { + await result.ApplyHtmlResponseAsync(initialResponse); + } else { + result.ContentType = finalResponse.Content.Headers.ContentType; + result.ResponseText = await finalResponse.Content.ReadAsStringAsync(); + result.IsXrds = true; + if (initialResponse != finalResponse) { + result.YadisLocation = finalResponse.RequestMessage.RequestUri; + } + + // Back up the initial HTML response in case the XRDS is not useful. + result.htmlFallback = initialResponse; + } + + return result; + } + + /// <summary> /// Reverts to the HTML response after the XRDS response didn't work out. /// </summary> - internal void TryRevertToHtmlResponse() { + /// <returns> + /// A task that completes with the asynchronous operation. + /// </returns> + internal async Task TryRevertToHtmlResponseAsync() { if (this.htmlFallback != null) { - this.ApplyHtmlResponse(this.htmlFallback); + await this.ApplyHtmlResponseAsync(this.htmlFallback); this.htmlFallback = null; } } @@ -96,10 +114,15 @@ namespace DotNetOpenAuth.Yadis { /// <summary> /// Applies the HTML response to the object. /// </summary> - /// <param name="initialResponse">The initial response.</param> - private void ApplyHtmlResponse(CachedDirectWebResponse initialResponse) { - this.ContentType = initialResponse.ContentType; - this.ResponseText = initialResponse.GetResponseString(); + /// <param name="response">The initial response.</param> + /// <returns> + /// A task that completes with the asynchronous operation. + /// </returns> + private async Task ApplyHtmlResponseAsync(HttpResponseMessage response) { + Requires.NotNull(response, "response"); + + this.ContentType = response.Content.Headers.ContentType; + this.ResponseText = await response.Content.ReadAsStringAsync(); this.IsXrds = this.ContentType != null && this.ContentType.MediaType == ContentTypes.Xrds; } } |