Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
WebHostMetaDataJson.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
7
9{
16 {
17 public WebHostMetaDataJson()
18 : base("/.well-known/host-meta.json")
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 = JsonCodec.DefaultContentType;
29
30 if (!(Request.Header.Accept is null))
31 {
32 ContentType = Request.Header.Accept.GetBestAlternative(JsonCodec.JsonContentTypes);
33 if (string.IsNullOrEmpty(ContentType))
34 throw new NotAcceptableException();
35 }
36
37 StringBuilder Json = new StringBuilder();
38 int[] HttpsPorts = Gateway.GetConfigPorts("HTTPS");
39 int[] HttpPorts = Gateway.GetConfigPorts("HTTP");
40
41 Json.Append("{\"links\": [");
42
43 if (HttpsPorts.Length > 0 || HttpPorts.Length > 0)
44 {
45 this.AppendLink(Json, HttpsPorts, HttpPorts, "urn:xmpp:alt-connections:xbosh", "http", "/http-bind");
46 this.AppendLink(Json, HttpsPorts, HttpPorts, "urn:xmpp:alt-connections:websocket", "ws", "/xmpp-websocket");
47 }
48
49 Json.Append("]}");
50
51 Response.ContentType = ContentType;
52 return Response.Write(Json.ToString());
53 }
54
55 private void AppendLink(StringBuilder Json, int[] HttpsPorts, int[] HttpPorts,
56 string Type, string Protocol, string Resource)
57 {
58 Json.Append("{\"rel\":\"");
59 Json.Append(Type);
60 Json.Append("\", \"href\":\"");
61 Json.Append(Protocol);
62
63 if (HttpsPorts.Length > 0)
64 {
65 Json.Append("s://");
66 Json.Append(Gateway.Domain);
67
68 if (Array.IndexOf(HttpsPorts, HttpServer.DefaultHttpsPort) < 0)
69 {
70 Json.Append(':');
71 Json.Append(HttpsPorts[0].ToString());
72 }
73 }
74 else
75 {
76 Json.Append("://");
77 Json.Append(Gateway.Domain);
78
79 if (Array.IndexOf(HttpPorts, HttpServer.DefaultHttpPort) < 0)
80 {
81 Json.Append(':');
82 Json.Append(HttpPorts[0].ToString());
83 }
84 }
85
86 Json.Append(Resource);
87 Json.Append("\"}");
88 }
89
90 }
91}
const string DefaultContentType
application/json
Definition: JsonCodec.cs:19
static readonly string[] JsonContentTypes
JSON content types.
Definition: JsonCodec.cs:36
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 JSON 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.