This repository has been archived by the owner on Mar 20, 2019. It is now read-only.
/
XriIdentifier.cs
93 lines (83 loc) · 3.01 KB
/
XriIdentifier.cs
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
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) {
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 = "http://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 ServiceEndpoint Discover() {
return downloadXrds().CreateServiceEndpoint(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;
}
}
}