//----------------------------------------------------------------------- // // Copyright (c) Scott Hanselman, Andrew Arnott. All rights reserved. // //----------------------------------------------------------------------- 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; /// /// Contains the result of YADIS discovery. /// internal class DiscoveryResult { /// /// 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. /// private HttpResponseMessage htmlFallback; /// /// Prevents a default instance of the class from being created. /// private DiscoveryResult() { } /// /// Gets the URI of the original YADIS discovery request. /// This is the user supplied Identifier as given in the original /// YADIS discovery request. /// public Uri RequestUri { get; private set; } /// /// Gets the fully resolved (after redirects) URL of the user supplied Identifier. /// This becomes the ClaimedIdentifier. /// public Uri NormalizedUri { get; private set; } /// /// Gets the location the XRDS document was downloaded from, if different /// from the user supplied Identifier. /// public Uri YadisLocation { get; private set; } /// /// Gets the Content-Type associated with the . /// public MediaTypeHeaderValue ContentType { get; private set; } /// /// Gets the text in the final response. /// This may be an XRDS document or it may be an HTML document, /// as determined by the property. /// public string ResponseText { get; private set; } /// /// Gets a value indicating whether the /// represents an XRDS document. False if the response is an HTML document. /// public bool IsXrds { get; private set; } /// /// Initializes a new instance of the class. /// /// The request URI. /// The initial response. /// The final response. /// The newly initialized instance. internal static async Task 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; } /// /// Reverts to the HTML response after the XRDS response didn't work out. /// /// /// A task that completes with the asynchronous operation. /// internal async Task TryRevertToHtmlResponseAsync() { if (this.htmlFallback != null) { await this.ApplyHtmlResponseAsync(this.htmlFallback); this.htmlFallback = null; } } /// /// Applies the HTML response to the object. /// /// The initial response. /// /// A task that completes with the asynchronous operation. /// 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; } } }