summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId/XriIdentifier.cs
blob: b412e797c6c3a040c09698dc375b44e21b8dfa38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using DotNetOpenId.RelyingParty;
using DotNetOpenId.Yadis;
using System.IO;
using System.Xml;

namespace DotNetOpenId {
	class XriIdentifier : Identifier {
		internal static readonly char[] GlobalContextSymbols = { '=', '@', '+', '$', '!' };
		const string xriScheme = "xri://";

		public XriIdentifier(string xri) {
			if (!IsValidXri(xri))
				throw new FormatException(string.Format(CultureInfo.CurrentCulture,
					Strings.InvalidXri, xri));
			OriginalXri = xri;
			CanonicalXri = canonicalizeXri(xri);
		}

		/// <summary>
		/// The original XRI supplied to the constructor.
		/// </summary>
		public string OriginalXri { get; private set; }
		/// <summary>
		/// The canonical form of the XRI string.
		/// </summary>
		public string CanonicalXri { get; private set; }

		/// <summary>
		/// Tests whether a given string represents a valid XRI format.
		/// </summary>
		internal static bool IsValidXri(string xri) {
			if (string.IsNullOrEmpty(xri)) throw new ArgumentNullException("xri");
			// TODO: better validation code here
			return xri.IndexOfAny(GlobalContextSymbols) == 0
				|| xri.StartsWith("(", StringComparison.Ordinal)
				|| xri.StartsWith(xriScheme, StringComparison.OrdinalIgnoreCase);
		}

		/// <summary>
		/// Takes any valid form of XRI string and returns the canonical form of the same XRI.
		/// </summary>
		static string canonicalizeXri(string xri) {
			xri = xri.Trim();
			if (xri.StartsWith(xriScheme, StringComparison.OrdinalIgnoreCase))
				xri = xri.Substring(xriScheme.Length);
			return xri;
		}

		/// <summary>
		/// The magic URL that will provide us an XRDS document for a given XRI identifier.
		/// </summary>
		/// <remarks>
		/// We use application/xrd+xml instead of application/xrds+xml because it gets
		/// xri.net to automatically give us exactly the right XRD element for community i-names
		/// automatically, saving us having to choose which one to use out of the result.
		/// </remarks>
		const string xriResolverProxy = "https://xri.net/{0}?_xrd_r=application/xrd%2Bxml;sep=false";
		/// <summary>
		/// Resolves the XRI to a URL from which an XRDS document may be downloaded.
		/// </summary>
		protected virtual Uri XrdsUrl {
			get {
				return new Uri(string.Format(CultureInfo.InvariantCulture, 
					xriResolverProxy, this));
			}
		}

		XrdsDocument downloadXrds() {
			var xrdsResponse = UntrustedWebRequest.Request(XrdsUrl);
			return new XrdsDocument(XmlReader.Create(xrdsResponse.ResponseStream));
		}

		internal override IEnumerable<ServiceEndpoint> Discover() {
			return downloadXrds().CreateServiceEndpoints(this, this);
		}

		/// <summary>
		/// Performs discovery on THIS identifier, but generates <see cref="ServiceEndpoint"/>
		/// instances that treat another given identifier as the user-supplied identifier.
		/// </summary>
		internal IEnumerable<ServiceEndpoint> Discover(XriIdentifier userSuppliedIdentifier) {
			return downloadXrds().CreateServiceEndpoints(this, userSuppliedIdentifier);
		}

		internal override Identifier TrimFragment() {
			return this;
		}

		public override bool Equals(object obj) {
			XriIdentifier other = obj as XriIdentifier;
			if (other == null) return false;
			return this.CanonicalXri == other.CanonicalXri;
		}
		public override int GetHashCode() {
			return CanonicalXri.GetHashCode();
		}

		public override string ToString() {
			return CanonicalXri;
		}
	}
}