//-----------------------------------------------------------------------
//
// Copyright (c) Scott Hanselman, Andrew Arnott. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Yadis {
using System;
using System.IO;
using System.Net.Mime;
using System.Web.UI.HtmlControls;
using System.Xml;
using DotNetOpenAuth.Messaging;
///
/// 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 CachedDirectWebResponse htmlFallback;
///
/// Initializes a new instance of the class.
///
/// The user-supplied identifier.
/// The initial response.
/// The final response.
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;
}
}
///
/// 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 ContentType 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; }
///
/// Reverts to the HTML response after the XRDS response didn't work out.
///
internal void TryRevertToHtmlResponse() {
if (this.htmlFallback != null) {
this.ApplyHtmlResponse(this.htmlFallback);
this.htmlFallback = null;
}
}
///
/// Applies the HTML response to the object.
///
/// The initial response.
private void ApplyHtmlResponse(CachedDirectWebResponse initialResponse) {
this.ContentType = initialResponse.ContentType;
this.ResponseText = initialResponse.GetResponseString();
this.IsXrds = this.ContentType != null && this.ContentType.MediaType == ContentTypes.Xrds;
}
}
}