Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
HttpxDeleter.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Security.Cryptography.X509Certificates;
5using System.Threading;
6using System.Threading.Tasks;
7using Waher.Content;
11
13{
21 {
28 public HttpxDeleter()
29 {
30 }
31
35 public override string[] UriSchemes => new string[] { HttpxGetter.HttpxUriScheme };
36
43 public override bool CanDelete(Uri Uri, out Grade Grade)
44 {
45 switch (Uri.Scheme)
46 {
48 Grade = Grade.Ok;
49 return true;
50
51 default:
52 Grade = Grade.NotAtAll;
53 return false;
54 }
55 }
56
74 public override async Task<object> DeleteAsync(Uri Uri, X509Certificate Certificate,
75 RemoteCertificateEventHandler RemoteCertificateValidator, int TimeoutMs, params KeyValuePair<string, string>[] Headers)
76 {
78 string BareJid;
79 string FullJid;
80 string LocalUrl;
81
82 if (Types.TryGetModuleParameter("HTTPX", out object Obj) && Obj is HttpxProxy Proxy)
83 {
84 if (Proxy.DefaultXmppClient.Disposed || Proxy.ServerlessMessaging.Disposed)
85 throw new InvalidOperationException("Service is being shut down.");
86
87 GetClientResponse Rec = await Proxy.GetClientAsync(Uri);
88
89 BareJid = Rec.BareJid;
90 FullJid = Rec.FullJid;
92 LocalUrl = Rec.LocalUrl;
93 }
94 else if (Types.TryGetModuleParameter("XMPP", out Obj) && Obj is XmppClient XmppClient)
95 {
97 throw new InvalidOperationException("Service is being shut down.");
98
99 if (!XmppClient.TryGetExtension(out HttpxClient HttpxClient2))
100 throw new InvalidOperationException("No HTTPX Extesion has been registered on the XMPP Client.");
101
102 HttpxClient = HttpxClient2;
103
104 if (string.IsNullOrEmpty(Uri.UserInfo))
105 FullJid = BareJid = Uri.Authority;
106 else
107 {
108 BareJid = Uri.UserInfo + "@" + Uri.Authority;
109
110 RosterItem Item = XmppClient.GetRosterItem(BareJid);
111
112 if (Item is null)
113 throw new ConflictException("No approved presence subscription with " + BareJid + ".");
114 else if (!Item.HasLastPresence || !Item.LastPresence.IsOnline)
115 throw new ServiceUnavailableException(BareJid + " is not online.");
116 else
117 FullJid = Item.LastPresenceFullJid;
118 }
119
120 LocalUrl = Uri.PathAndQuery + Uri.Fragment;
121 }
122 else
123 throw new InvalidOperationException("An HTTPX Proxy or XMPP Client Module Parameter has not been registered.");
124
125 List<HttpField> Headers2 = new List<HttpField>();
126 bool HasHost = false;
127
128 foreach (KeyValuePair<string, string> Header in Headers)
129 {
130 switch (Header.Key.ToLower())
131 {
132 case "host":
133 Headers2.Add(new HttpField("Host", BareJid));
134 HasHost = true;
135 break;
136
137 case "cookie":
138 case "set-cookie":
139 // Do not forward cookies.
140 break;
141
142 default:
143 Headers2.Add(new HttpField(Header.Key, Header.Value));
144 break;
145 }
146 }
147
148 if (!HasHost)
149 Headers2.Add(new HttpField("Host", Uri.Authority));
150
151 State State = null;
152 Timer Timer = null;
153
154 try
155 {
156 State = new State();
157 Timer = new Timer((P) =>
158 {
159 State.Done.TrySetResult(false);
160 }, null, TimeoutMs, Timeout.Infinite);
161
162 // TODO: Transport public part of Client certificate, if provided.
163
164 await HttpxClient.Request(FullJid, "DELETE", LocalUrl, 1.1, Headers2, null, async (Sender, e) =>
165 {
166 if (e.Ok)
167 {
168 State.HttpResponse = e.HttpResponse;
169 State.StatusCode = e.StatusCode;
170 State.StatusMessage = e.StatusMessage;
171
172 if (e.HasData)
173 {
174 if (State.Data is null)
175 State.Data = new MemoryStream();
176
177 if (!(e.Data is null))
178 {
179 await State.Data.WriteAsync(e.Data, 0, e.Data.Length);
180 State.Done.TrySetResult(true);
181 }
182 }
183 else
184 State.Done.TrySetResult(true);
185 }
186 else
187 State.Done.TrySetException(e.StanzaError ?? new Exception("Unable to get resource."));
188
189 }, async (Sender, e) =>
190 {
191 if (State.Data is null)
192 State.Data = new MemoryStream();
193
194 await State.Data.WriteAsync(e.Data, 0, e.Data.Length);
195 if (e.Last)
196 State.Done.TrySetResult(true);
197
198 }, State);
199
200 if (!await State.Done.Task)
201 throw new TimeoutException("Request timed out.");
202
203 Timer.Dispose();
204 Timer = null;
205
206 if (State.StatusCode >= 200 && State.StatusCode < 300)
207 return new KeyValuePair<byte[], string>(State.Data?.ToArray(), State.HttpResponse?.ContentType);
208 else
209 {
210 string ContentType = string.Empty;
211 byte[] EncodedData = State.Data?.ToArray();
212
213 throw HttpxGetter.GetExceptionObject(State.StatusCode, State.StatusMessage,
214 State.HttpResponse, EncodedData, ContentType);
215 }
216 }
217 finally
218 {
219 State.Data?.Dispose();
220 State.Data = null;
221
222 if (!(State.HttpResponse is null))
223 {
224 await State.HttpResponse.DisposeAsync();
225 State.HttpResponse = null;
226 }
227
228 Timer?.Dispose();
229 Timer = null;
230 }
231 }
232
233 private class State
234 {
235 public HttpResponse HttpResponse = null;
236 public MemoryStream Data = null;
237 public TaskCompletionSource<bool> Done = new TaskCompletionSource<bool>();
238 public string StatusMessage = string.Empty;
239 public int StatusCode = 0;
240 }
241
242 }
243}
Abstract base class for deleters.
Definition: DeleterBase.cs:13
The request could not be completed due to a conflict with the current state of the resource....
Base class for all HTTP fields.
Definition: HttpField.cs:7
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
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.
Task Request(string To, string Method, string LocalResource, EventHandlerAsync< HttpxResponseEventArgs > Callback, EventHandlerAsync< HttpxResponseDataEventArgs > DataCallback, object State, params HttpField[] Headers)
Performs an HTTP request.
Definition: HttpxClient.cs:252
Content Deleter, deleting content using the HTTPX URI Scheme.
Definition: HttpxDeleter.cs:21
override string[] UriSchemes
Supported URI schemes.
Definition: HttpxDeleter.cs:35
override async Task< object > DeleteAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Deletes a resource, using a Uniform Resource Identifier (or Locator).
Definition: HttpxDeleter.cs:74
HttpxDeleter()
Content Deleter, deleting content using the HTTPX URI Scheme.
Definition: HttpxDeleter.cs:28
override bool CanDelete(Uri Uri, out Grade Grade)
If the deleter is able to delete to a resource, given its URI.
Definition: HttpxDeleter.cs:43
Content Getter, retrieving content using the HTTPX URI Scheme.
Definition: HttpxGetter.cs:21
Implements a Proxy resource that allows Web clients to fetch HTTP-based resources over HTTPX.
Definition: HttpxProxy.cs:19
Maintains information about an item in the roster.
Definition: RosterItem.cs:75
bool HasLastPresence
If the roster item has received presence from an online resource having the given bare JID.
Definition: RosterItem.cs:425
string LastPresenceFullJid
Full JID of last resource sending online presence.
Definition: RosterItem.cs:343
PresenceEventArgs LastPresence
Last presence received from a resource having this bare JID.
Definition: RosterItem.cs:356
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
bool Disposed
If the client has been disposed.
Definition: XmppClient.cs:1192
bool TryGetExtension(Type Type, out IXmppExtension Extension)
Tries to get a registered extension of a specific type from the client.
Definition: XmppClient.cs:7318
RosterItem GetRosterItem(string BareJID)
Gets a roster item.
Definition: XmppClient.cs:4522
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:583
delegate void RemoteCertificateEventHandler(object Sender, RemoteCertificateEventArgs e)
Delegate for remote certificate event handlers.
Grade
Grade enumeration
Definition: Grade.cs:7