Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DataGetter.cs
1using System;
2using System.Collections.Generic;
3using System.Security.Cryptography.X509Certificates;
4using System.Threading.Tasks;
7
9{
14 {
18 public DataGetter()
19 {
20 }
21
25 public string[] UriSchemes => new string[] { "data" };
26
33 public bool CanGet(Uri Uri, out Grade Grade)
34 {
35 switch (Uri.Scheme.ToLower())
36 {
37 case "data":
38 Grade = Grade.Ok;
39 return true;
40
41 default:
42 Grade = Grade.NotAtAll;
43 return false;
44 }
45 }
46
55 public Task<object> GetAsync(Uri Uri, X509Certificate Certificate,
56 RemoteCertificateEventHandler RemoteCertificateValidator, params KeyValuePair<string, string>[] Headers)
57 {
58 return this.GetAsync(Uri, Certificate, RemoteCertificateValidator, 60000, Headers);
59 }
60
70 public Task<object> GetAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator,
71 int TimeoutMs, params KeyValuePair<string, string>[] Headers)
72 {
73 KeyValuePair<byte[], string> P = Decode(Uri);
74 return InternetContent.DecodeAsync(P.Value, P.Key, Uri);
75 }
76
83 public static KeyValuePair<byte[], string> Decode(Uri Uri)
84 {
85 string s = Uri.OriginalString;
86 int i = s.IndexOf(':');
87 if (i < 0)
88 throw new ArgumentException("Invalid Data URI.", nameof(Uri));
89
90 int j = s.IndexOf(';', i + 1);
91 if (j < 0)
92 throw new ArgumentException("Content-Type not encoded in Data URI.", nameof(Uri));
93
94 string ContentType = s.Substring(i + 1, j - i - 1);
95
96 int k = s.IndexOf(',', j + 1);
97 if (k < 0)
98 throw new ArgumentException("Data not encoded in Data URI.", nameof(Uri));
99
100 byte[] Bin = Convert.FromBase64String(s.Substring(k + 1));
101
102 return new KeyValuePair<byte[], string>(Bin, ContentType);
103 }
104
113 public Task<KeyValuePair<string, TemporaryStream>> GetTempStreamAsync(Uri Uri, X509Certificate Certificate,
114 RemoteCertificateEventHandler RemoteCertificateValidator, params KeyValuePair<string, string>[] Headers)
115 {
116 return this.GetTempStreamAsync(Uri, Certificate, RemoteCertificateValidator, 60000, Headers);
117 }
118
128 public async Task<KeyValuePair<string, TemporaryStream>> GetTempStreamAsync(Uri Uri, X509Certificate Certificate,
129 RemoteCertificateEventHandler RemoteCertificateValidator, int TimeoutMs, params KeyValuePair<string, string>[] Headers)
130 {
131 KeyValuePair<byte[], string> P = Decode(Uri);
133 await f.WriteAsync(P.Key, 0, P.Key.Length);
134
135 return new KeyValuePair<string, TemporaryStream>(P.Value, f);
136 }
137
144 public bool CanHead(Uri Uri, out Grade Grade)
145 {
146 Grade = Grade.NotAtAll;
147 return false;
148 }
149
158 public Task<object> HeadAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator,
159 params KeyValuePair<string, string>[] Headers)
160 {
161 return Task.FromResult<object>(null);
162 }
163
173 public Task<object> HeadAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator,
174 int TimeoutMs, params KeyValuePair<string, string>[] Headers)
175 {
176 return Task.FromResult<object>(null);
177 }
178
179 }
180}
Gets data encoded into data URIs.
Definition: DataGetter.cs:14
static KeyValuePair< byte[], string > Decode(Uri Uri)
Decodes a Data URI.
Definition: DataGetter.cs:83
bool CanGet(Uri Uri, out Grade Grade)
If the getter is able to get a resource, given its URI.
Definition: DataGetter.cs:33
Task< object > HeadAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, params KeyValuePair< string, string >[] Headers)
Gets the headers of a resource, using a Uniform Resource Identifier (or Locator).
Definition: DataGetter.cs:158
Task< object > GetAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Gets a resource, using a Uniform Resource Identifier (or Locator).
Definition: DataGetter.cs:70
Task< object > HeadAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Gets the headers of a resource, using a Uniform Resource Identifier (or Locator).
Definition: DataGetter.cs:173
Task< KeyValuePair< string, TemporaryStream > > GetTempStreamAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, using a Uniform Resource Identifier (or Locator).
Definition: DataGetter.cs:113
DataGetter()
Gets data encoded into data URIs.
Definition: DataGetter.cs:18
string[] UriSchemes
Supported URI schemes.
Definition: DataGetter.cs:25
async Task< KeyValuePair< string, TemporaryStream > > GetTempStreamAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, using a Uniform Resource Identifier (or Locator).
Definition: DataGetter.cs:128
Task< object > GetAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, params KeyValuePair< string, string >[] Headers)
Gets a resource, using a Uniform Resource Identifier (or Locator).
Definition: DataGetter.cs:55
bool CanHead(Uri Uri, out Grade Grade)
If the getter is able to get headers of a resource, given its URI.
Definition: DataGetter.cs:144
Static class managing encoding and decoding of internet content.
static Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
Manages a temporary stream. Contents is kept in-memory, if below a memory threshold,...
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...
Basic interface for Internet Content getters. A class implementing this interface and having a defaul...
Basic interface for Internet Content headers. A class implementing this interface and having a defaul...
delegate void RemoteCertificateEventHandler(object Sender, RemoteCertificateEventArgs e)
Delegate for remote certificate event handlers.
Grade
Grade enumeration
Definition: Grade.cs:7