Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
WebHostMetaDataXml.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
7
9{
16 {
17 public WebHostMetaDataXml()
18 : base("/.well-known/host-meta")
19 {
20 }
21
22 public override bool HandlesSubPaths => false;
23 public override bool UserSessions => false;
24 public bool AllowsGET => true;
25
26 public Task GET(HttpRequest Request, HttpResponse Response)
27 {
28 string ContentType = XmlCodec.DefaultContentType;
29
30 if (!(Request.Header.Accept is null))
31 {
32 ContentType = Request.Header.Accept.GetBestAlternative(XmlCodec.XmlContentTypes);
33 if (string.IsNullOrEmpty(ContentType))
34 throw new NotAcceptableException();
35 }
36
37 StringBuilder Xml = new StringBuilder();
38 int[] HttpsPorts = Gateway.GetConfigPorts("HTTPS");
39 int[] HttpPorts = Gateway.GetConfigPorts("HTTP");
40
41 Xml.Append("<?xml version='1.0'?><XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>");
42
43 if (HttpsPorts.Length > 0 || HttpPorts.Length > 0)
44 {
45 AppendLink(Xml, HttpsPorts, HttpPorts, Gateway.Domain, "urn:xmpp:alt-connections:xbosh", "http", "/http-bind");
46 AppendLink(Xml, HttpsPorts, HttpPorts, Gateway.Domain, "urn:xmpp:alt-connections:websocket", "ws", "/xmpp-websocket");
47 }
48
49 Xml.Append("</XRD>");
50
51 Response.ContentType = ContentType;
52 return Response.Write(Xml.ToString());
53 }
54
55 public static string BoshLink(string Domain)
56 {
57 StringBuilder sb = new StringBuilder();
58 int[] HttpsPorts = Gateway.GetConfigPorts("HTTPS");
59 int[] HttpPorts = Gateway.GetConfigPorts("HTTP");
60
61 AppendLink(sb, HttpsPorts, HttpPorts, Domain, "http", "/http-bind");
62
63 return sb.ToString();
64 }
65
66 public static string WebSocketLink(string Domain)
67 {
68 StringBuilder sb = new StringBuilder();
69 int[] HttpsPorts = Gateway.GetConfigPorts("HTTPS");
70 int[] HttpPorts = Gateway.GetConfigPorts("HTTP");
71
72 AppendLink(sb, HttpsPorts, HttpPorts, Domain, "ws", "/xmpp-websocket");
73
74 return sb.ToString();
75 }
76
77 private static void AppendLink(StringBuilder Xml, int[] HttpsPorts, int[] HttpPorts,
78 string Domain, string Type, string Protocol, string Resource)
79 {
80 Xml.Append("<Link rel='");
81 Xml.Append(Type);
82 Xml.Append("' href='");
83 AppendLink(Xml, HttpsPorts, HttpPorts, Domain, Protocol, Resource);
84 Xml.Append("'/>");
85 }
86
87 private static void AppendLink(StringBuilder Xml, int[] HttpsPorts, int[] HttpPorts,
88 string Domain, string Protocol, string Resource)
89 {
90 Xml.Append(Protocol);
91
92 if (HttpsPorts.Length > 0)
93 {
94 Xml.Append("s://");
95 Xml.Append(Domain);
96
97 if (Array.IndexOf(HttpsPorts, HttpServer.DefaultHttpsPort) < 0)
98 {
99 Xml.Append(':');
100 Xml.Append(HttpsPorts[0].ToString());
101 }
102 }
103 else
104 {
105 Xml.Append("://");
106 Xml.Append(Domain);
107
108 if (Array.IndexOf(HttpPorts, HttpServer.DefaultHttpPort) < 0)
109 {
110 Xml.Append(':');
111 Xml.Append(HttpPorts[0].ToString());
112 }
113 }
114
115 Xml.Append(Resource);
116 }
117
118 }
119}
XML encoder/decoder.
Definition: XmlCodec.cs:16
static readonly string[] XmlContentTypes
XML content types.
Definition: XmlCodec.cs:37
const string DefaultContentType
Default content type for XML documents.
Definition: XmlCodec.cs:27
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:2354
static int[] GetConfigPorts(string Protocol)
Gets the port numbers defined for a given protocol in the configuration file.
Definition: Gateway.cs:2420
HttpFieldAccept Accept
Accept HTTP Field header. (RFC 2616, §14.1)
Represents an HTTP request.
Definition: HttpRequest.cs:18
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:134
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task Write(byte[] Data)
Returns binary data in the response.
Implements an HTTP server.
Definition: HttpServer.cs:36
const int DefaultHttpPort
Default HTTP Port (80).
Definition: HttpServer.cs:40
const int DefaultHttpsPort
Default HTTPS port (443).
Definition: HttpServer.cs:45
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
The resource identified by the request is only capable of generating response entities which have con...
Web Host Meta Data in XML format, as defined in XEP-0156 and RFC 6415: https://xmpp....
Task GET(HttpRequest Request, HttpResponse Response)
Executes the GET method on the resource.
GET Interface for HTTP resources.
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.