Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LocalContent.cs
1using System;
3using System.Threading.Tasks;
4using Waher.Content;
5using Waher.Events;
8
9namespace Waher.IoTGateway
10{
16 public static class LocalContent
17 {
23 public static bool IsLocal(string Uri)
24 {
25 return IsLocal(Uri, false);
26 }
27
34 public static bool IsLocal(string Uri, bool CheckExists)
35 {
36 if (!System.Uri.TryCreate(Uri, UriKind.RelativeOrAbsolute, out Uri Parsed))
37 return false;
38
39 return IsLocal(Parsed, CheckExists);
40 }
41
47 public static bool IsLocal(Uri Uri)
48 {
49 return IsLocal(Uri, false);
50 }
51
58 public static bool IsLocal(Uri Uri, bool CheckExists)
59 {
60 if (Uri.IsAbsoluteUri)
61 {
62 int[] Ports;
63
64 switch (Uri.Scheme.ToLower())
65 {
66 case "http":
68 break;
69
70 case "https":
72 break;
73
74 default:
75 return false;
76 }
77
78 if (Ports is null)
79 return false;
80
81 if (string.Compare(Uri.Host, "localhost", true) != 0 &&
82 !Gateway.IsDomain(Uri.Host, true))
83 {
84 return false;
85 }
86
87 if (Array.IndexOf(Ports, Uri.Port) < 0)
88 return false;
89 }
90
91 if (CheckExists)
92 {
93 HttpResource Resource;
94 string SubPath;
95 string s;
96
97 try
98 {
99 s = Uri.AbsolutePath;
100
101 if (!Gateway.HttpServer.TryGetResource(ref s, out Resource, out SubPath))
102 return false;
103 }
104 catch (Exception ex)
105 {
106 Log.Error("Attempt to check local resource failed with exception: " + ex.Message);
107 return false;
108 }
109
110 if (!string.IsNullOrEmpty(SubPath))
111 {
112 if (!Resource.HandlesSubPaths)
113 return false;
114
115 if (!Gateway.HttpServer.TryGetFileName(Uri.AbsolutePath, true, out _))
116 return false;
117 }
118 }
119
120 return true;
121 }
122
129 public static async Task<ContentResponse> GetAsync(Uri Uri, params KeyValuePair<string, string>[] Headers)
130 {
131 if (!IsLocal(Uri) || Gateway.HttpServer is null)
132 return await InternetContent.GetAsync(Uri, Gateway.Certificate, Headers);
133
134 using SessionVariables v = new SessionVariables();
135 Tuple<int, string, byte[]> T = await Gateway.HttpServer.GET(Uri.AbsolutePath, v);
136 int Code = T.Item1;
137 string ContentType = T.Item2;
138 byte[] Bin = T.Item3;
139
140 if (Code < 200 || Code >= 300)
141 return new ContentResponse(new HttpException(Code, HttpException.GetStatusMessage(Code), Bin, ContentType));
142
143 return await InternetContent.DecodeAsync(ContentType, Bin, Uri);
144 }
145
152 public static Task<ContentStreamResponse> GetTempStreamAsync(Uri Uri, params KeyValuePair<string, string>[] Headers)
153 {
154 return GetTempStreamAsync(Uri, null, Headers);
155 }
156
164 public static async Task<ContentStreamResponse> GetTempStreamAsync(Uri Uri, TemporaryStream Destination, params KeyValuePair<string, string>[] Headers)
165 {
166 if (!IsLocal(Uri) || Gateway.HttpServer is null)
167 return await InternetContent.GetTempStreamAsync(Uri, Gateway.Certificate, Headers);
168
169 using SessionVariables v = new SessionVariables();
170 Tuple<int, string, byte[]> T = await Gateway.HttpServer.GET(Uri.AbsolutePath, v);
171 int Code = T.Item1;
172 string ContentType = T.Item2;
173 byte[] Bin = T.Item3;
174
175 if (Code < 200 || Code >= 300)
176 return new ContentStreamResponse(new HttpException(Code, HttpException.GetStatusMessage(Code), Bin, ContentType));
177
178 bool DestinationCreated = false;
179
180 if (Destination is null)
181 {
182 Destination = new TemporaryStream();
183 DestinationCreated = true;
184 }
185
186 try
187 {
188 await Destination.WriteAsync(Bin, 0, Bin.Length);
189 }
190 catch (Exception ex)
191 {
192 if (DestinationCreated)
193 Destination.Dispose();
194
195 return new ContentStreamResponse(ex);
196 }
197
198 return new ContentStreamResponse(ContentType, Destination);
199 }
200 }
201}
Contains information about a response to a content request.
Contains information about a stream response to a content request.
Static class managing encoding and decoding of internet content.
static Task< ContentStreamResponse > GetTempStreamAsync(Uri Uri, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, given its URI.
static Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
static Task< ContentResponse > GetAsync(Uri Uri, params KeyValuePair< string, string >[] Headers)
Gets a resource, given its URI.
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Error(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an error event.
Definition: Log.cs:692
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static HttpServer HttpServer
HTTP Server
Definition: Gateway.cs:4118
static bool IsDomain(string DomainOrHost, bool IncludeAlternativeDomains)
If a domain or host name represents the gateway.
Definition: Gateway.cs:5174
static X509Certificate2 Certificate
Domain certificate.
Definition: Gateway.cs:3082
Static class that gives access to local content published by the gateway, without having to perform r...
Definition: LocalContent.cs:17
static bool IsLocal(Uri Uri)
Checks if a Uri is local to the gateway.
Definition: LocalContent.cs:47
static async Task< ContentStreamResponse > GetTempStreamAsync(Uri Uri, TemporaryStream Destination, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, given its URI.
static bool IsLocal(string Uri, bool CheckExists)
Checks if a Uri is local to the gateway.
Definition: LocalContent.cs:34
static bool IsLocal(Uri Uri, bool CheckExists)
Checks if a Uri is local to the gateway.
Definition: LocalContent.cs:58
static async Task< ContentResponse > GetAsync(Uri Uri, params KeyValuePair< string, string >[] Headers)
Gets a resource, given its URI.
static Task< ContentStreamResponse > GetTempStreamAsync(Uri Uri, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, given its URI.
static bool IsLocal(string Uri)
Checks if a Uri is local to the gateway.
Definition: LocalContent.cs:23
Base class of all HTTP Exceptions.
static string GetStatusMessage(int StatusCode)
Gets the default status message, given a status code.
Base class for all HTTP resources.
Definition: HttpResource.cs:23
int[] OpenHttpPorts
HTTP Ports successfully opened.
Definition: HttpServer.cs:768
bool TryGetResource(HttpRequest Request, out HttpResource Resource, out string SubPath)
Tries to get a resource from the server.
Definition: HttpServer.cs:1796
bool TryGetFileName(string LocalUrl, out string FileName)
Tries to get the full path of a file-based resource.
Definition: HttpServer.cs:2427
int[] OpenHttpsPorts
HTTPS Ports successfully opened.
Definition: HttpServer.cs:773
async Task< Tuple< int, string, byte[]> > GET(string LocalUrl, SessionVariables Session)
Performs an internal GET operation.
Definition: HttpServer.cs:2383
Collection of session variables.
Manages a temporary stream. Contents is kept in-memory, if below a memory threshold,...
override void Dispose(bool disposing)
Releases the unmanaged resources used by the System.IO.Stream and optionally releases the managed res...
override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Asynchronously writes a sequence of bytes to the current stream, advances the current position within...
Definition: ImplTypes.g.cs:58