2using System.Collections.Generic;
4using System.Security.Cryptography.X509Certificates;
6using System.Threading.Tasks;
71 params KeyValuePair<string, string>[] Headers)
73 return this.
GetAsync(Uri, Certificate, RemoteCertificateValidator, 60000, Headers);
93 public async Task<object>
GetAsync(Uri Uri, X509Certificate Certificate,
96 KeyValuePair<string, TemporaryStream> Rec = await this.
GetTempStreamAsync(Uri, Certificate, RemoteCertificateValidator, TimeoutMs, Headers);
97 string ContentType = Rec.Key;
107 if (File.
Length >
int.MaxValue)
108 throw new OutOfMemoryException(
"Resource too large.");
110 int Len = (int)File.
Length;
111 byte[] Bin =
new byte[Len];
112 if (await File.
ReadAsync(Bin, 0, Len) != Len)
113 throw new IOException(
"Unable to read from file.");
136 public Task<KeyValuePair<string, TemporaryStream>>
GetTempStreamAsync(Uri Uri, X509Certificate Certificate,
139 return this.
GetTempStreamAsync(Uri, Certificate, RemoteCertificateValidator, 60000, Headers);
156 public async Task<KeyValuePair<string, TemporaryStream>>
GetTempStreamAsync(Uri Uri, X509Certificate Certificate,
160 string BareJid = Uri.UserInfo +
"@" + Uri.Authority;
166 if (Proxy.DefaultXmppClient.Disposed || Proxy.ServerlessMessaging.Disposed)
167 throw new InvalidOperationException(
"Service is being shut down.");
169 if (
string.Compare(BareJid, Proxy.DefaultXmppClient.BareJID,
true) == 0 &&
170 Proxy.DefaultXmppClient.TryGetExtension(out
HttpxServer Server))
172 return await Server.GetLocalTempStreamAsync(Uri.PathAndQuery + Uri.Fragment);
187 throw new InvalidOperationException(
"Service is being shut down.");
192 return await Server.GetLocalTempStreamAsync(Uri.PathAndQuery + Uri.Fragment);
197 throw new InvalidOperationException(
"No HTTPX Extesion has been registered on the XMPP Client.");
201 if (
string.IsNullOrEmpty(Uri.UserInfo))
202 FullJid = BareJid = Uri.Authority;
205 BareJid = Uri.UserInfo +
"@" + Uri.Authority;
210 throw new ConflictException(
"No approved presence subscription with " + BareJid +
".");
217 LocalUrl = Uri.PathAndQuery + Uri.Fragment;
221 throw new InvalidOperationException(
"An HTTPX Proxy or XMPP Client Module Parameter has not been registered.");
223 List<HttpField> Headers2 =
new List<HttpField>();
224 bool HasHost =
false;
226 foreach (KeyValuePair<string, string> Header
in Headers)
228 switch (Header.Key.ToLower())
231 Headers2.Add(
new HttpField(
"Host", BareJid));
241 Headers2.Add(
new HttpField(Header.Key, Header.Value));
247 Headers2.Add(
new HttpField(
"Host", Uri.Authority));
255 Timer =
new Timer((P) =>
257 State.Done.TrySetResult(
false);
258 },
null, TimeoutMs, Timeout.Infinite);
263 throw new Exception(
"No HTTPX client available.");
269 State.HttpResponse = e.HttpResponse;
270 State.StatusCode = e.StatusCode;
271 State.StatusMessage = e.StatusMessage;
275 State.File = new TemporaryStream();
277 if (!(e.Data is null))
279 await State.File.WriteAsync(e.Data, 0, e.Data.Length);
280 State.Done.TrySetResult(true);
284 State.Done.TrySetResult(
true);
287 State.Done.TrySetException(e.StanzaError ??
new Exception(
"Unable to get resource."));
289 }, async (Sender, e) =>
291 await (State.File?.WriteAsync(e.Data, 0, e.Data.Length) ?? Task.CompletedTask);
293 State.Done?.TrySetResult(
true);
295 }, State, Headers2.ToArray());
297 if (!await State.Done.Task)
298 throw new TimeoutException(
"Request timed out.");
303 if (State.StatusCode >= 200 && State.StatusCode < 300)
308 return new KeyValuePair<string, TemporaryStream>(State.HttpResponse?.ContentType, Result);
312 string ContentType =
string.Empty;
315 if (State.File is
null)
319 int Len = (int)State.File.Length;
321 ContentType = State.HttpResponse.ContentType;
322 State.File.Position = 0;
323 Data =
new byte[Len];
324 await State.File.ReadAsync(Data, 0, Len);
327 throw GetExceptionObject(State.StatusCode, State.StatusMessage,
328 State.HttpResponse, Data, ContentType);
333 State.File?.Dispose();
336 if (!(State.HttpResponse is
null))
338 await State.HttpResponse.DisposeAsync();
339 State.HttpResponse =
null;
347 internal static Exception GetExceptionObject(
int StatusCode,
string StatusMessage,
391 case HTTP.NotImplementedException.Code:
throw new HTTP.NotImplementedException(Data, ContentType);
396 throw new HttpException(StatusCode, StatusMessage, Data, ContentType);
400 private static string[] GetMethods(
string Allow)
402 if (
string.IsNullOrEmpty(Allow))
403 return new string[0];
405 string[] Result = Allow.Split(
',');
406 int i, c = Result.Length;
408 for (i = 0; i < c; i++)
409 Result[i] = Result[i].Trim();
418 public TaskCompletionSource<bool> Done =
new TaskCompletionSource<bool>();
419 public string StatusMessage =
string.Empty;
420 public int StatusCode = 0;
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.
The server, while acting as a gateway or proxy, received an invalid response from the upstream server...
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
The request could not be completed due to a conflict with the current state of the resource....
The request failed due to failure of a previous request (e.g., a PROPPATCH).
The server understood the request, but is refusing to fulfill it. Authorization will not help and the...
The requested resource resides temporarily under a different URI. Since the redirection might be alte...
The server, while acting as a gateway or proxy, did not receive a timely response from the upstream s...
The requested resource is no longer available at the server and no forwarding address is known....
Base class of all HTTP Exceptions.
Base class for all HTTP fields.
Represets a response of an HTTP client request.
string GetFirstHeader(string FieldName)
Gets the first header value matching a given header field name.
string[] GetChallenges()
Gets available WWW-Authenticate challenges returned in the response.
The server is unable to store the representation needed to complete the request.
The server encountered an unexpected condition which prevented it from fulfilling the request.
The resource that is being accessed is locked.
The server detected an infinite loop while processing the request.
The server has not found anything matching the Request-URI. No indication is given of whether the con...
The request was directed at a server that is not able to produce a response (for example because a co...
The requested resource has been assigned a new permanent URI and any future references to this resour...
The client needs to authenticate to gain network access. Intended for use by intercepting proxies use...
The resource identified by the request is only capable of generating response entities which have con...
Further extensions to the request are required for the server to fulfil it.
The server has not found anything matching the Request-URI. No indication is given of whether the con...
If the client has performed a conditional GET request and access is allowed, but the document has not...
This means that the resource is now permanently located at another URI, specified by the Location: HT...
The precondition given in one or more of the request-header fields evaluated to false when it was tes...
The origin server requires the request to be conditional. Intended to prevent "the 'lost update' prob...
A server SHOULD return a response with this status code if a request included a Range request-header ...
The client did not produce a request within the time that the server was prepared to wait....
The response to the request can be found under a different URI and SHOULD be retrieved using a GET me...
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
The requested resource resides temporarily under a different URI. Since the redirection MAY be altere...
The user has sent too many requests in a given amount of time. Intended for use with rate limiting sc...
Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or ...
Defined in the internet draft "A New HTTP Status Code for Legally-restricted Resources"....
The request was well-formed but was unable to be followed due to semantic errors.
The client should switch to a different protocol such as TLS/1.0, given in the Upgrade header field.
The requested resource MUST be accessed through the proxy given by the Location field....
Transparent content negotiation for the request results in a circular reference.
Response to the HttpxProxy.GetClientAsync(Uri) method call.
string BareJid
Bare JID of entity hosting the resource.
HttpxClient HttpxClient
Corresponding HttpxClient object to use for the request..
string FullJid
Full JID of entity hosting the resource.
string LocalUrl
Local part of the URL
Task Request(string To, string Method, string LocalResource, EventHandlerAsync< HttpxResponseEventArgs > Callback, EventHandlerAsync< HttpxResponseDataEventArgs > DataCallback, object State, params HttpField[] Headers)
Performs an HTTP request.
Content Getter, retrieving content using the HTTPX URI Scheme.
string[] UriSchemes
Supported URI schemes.
const string HttpxUriScheme
httpx
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).
Task< object > GetAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, params KeyValuePair< string, string >[] Headers)
Gets a resource, using a Uniform Resource Identifier (or Locator).
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).
bool CanGet(Uri Uri, out Grade Grade)
If the getter is able to get a resource, given its URI.
async 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).
HttpxGetter()
Content Getter, retrieving content using the HTTPX URI Scheme.
Implements a Proxy resource that allows Web clients to fetch HTTP-based resources over HTTPX.
Maintains information about an item in the roster.
bool HasLastPresence
If the roster item has received presence from an online resource having the given bare JID.
string LastPresenceFullJid
Full JID of last resource sending online presence.
PresenceEventArgs LastPresence
Last presence received from a resource having this bare JID.
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
bool Disposed
If the client has been disposed.
bool TryGetExtension(Type Type, out IXmppExtension Extension)
Tries to get a registered extension of a specific type from the client.
RosterItem GetRosterItem(string BareJID)
Gets a roster item.
Static class that dynamically manages types and interfaces available in the runtime environment.
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
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 Task< int > ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Asynchronously reads a sequence of bytes from the current stream, advances the position within the st...
override long Length
When overridden in a derived class, gets the length in bytes of the stream.
Basic interface for Internet Content getters. A class implementing this interface and having a defaul...
delegate void RemoteCertificateEventHandler(object Sender, RemoteCertificateEventArgs e)
Delegate for remote certificate event handlers.