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;
3using System.IO;
4using System.Security.Cryptography.X509Certificates;
5using System.Threading;
6using System.Threading.Tasks;
7using Waher.Content;
9using Waher.Events;
12
14{
22 {
29 public HttpxDeleter()
30 {
31 }
32
36 public override string[] UriSchemes => new string[] { HttpxGetter.HttpxUriScheme };
37
44 public override bool CanDelete(Uri Uri, out Grade Grade)
45 {
46 switch (Uri.Scheme)
47 {
49 Grade = Grade.Ok;
50 return true;
51
52 default:
53 Grade = Grade.NotAtAll;
54 return false;
55 }
56 }
57
76 public override async Task<ContentResponse> DeleteAsync(Uri Uri, X509Certificate Certificate,
77 EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator, int TimeoutMs, params KeyValuePair<string, string>[] Headers)
78 {
80 string BareJid;
81 string FullJid;
82 string LocalUrl;
83
84 if (Types.TryGetModuleParameter("HTTPX", out HttpxProxy Proxy))
85 {
86 if (Proxy.DefaultXmppClient.Disposed || Proxy.ServerlessMessaging.Disposed)
87 return new ContentResponse(new InvalidOperationException("Service is being shut down."));
88
89 GetClientResponse Rec = await Proxy.GetClientAsync(Uri);
90
91 BareJid = Rec.BareJid;
92 FullJid = Rec.FullJid;
94 LocalUrl = Rec.LocalUrl;
95 }
97 {
99 return new ContentResponse(new InvalidOperationException("Service is being shut down."));
100
101 if (!XmppClient.TryGetExtension(out HttpxClient HttpxClient2))
102 return new ContentResponse(new InvalidOperationException("No HTTPX Extesion has been registered on the XMPP Client."));
103
104 HttpxClient = HttpxClient2;
105
106 if (string.IsNullOrEmpty(Uri.UserInfo))
107 FullJid = BareJid = Uri.Authority;
108 else
109 {
110 BareJid = Uri.UserInfo + "@" + Uri.Authority;
111
112 RosterItem Item = XmppClient.GetRosterItem(BareJid);
113
114 if (Item is null)
115 return new ContentResponse(new ConflictException("No approved presence subscription with " + BareJid + "."));
116 else if (!Item.HasLastPresence || !Item.LastPresence.IsOnline)
117 return new ContentResponse(new ServiceUnavailableException(BareJid + " is not online."));
118 else
119 FullJid = Item.LastPresenceFullJid;
120 }
121
122 LocalUrl = Uri.PathAndQuery + Uri.Fragment;
123 }
124 else
125 return new ContentResponse(new InvalidOperationException("An HTTPX Proxy or XMPP Client Module Parameter has not been registered."));
126
127 List<HttpField> Headers2 = new List<HttpField>();
128 bool HasHost = false;
129
130 foreach (KeyValuePair<string, string> Header in Headers)
131 {
132 switch (Header.Key.ToLower())
133 {
134 case "host":
135 Headers2.Add(new HttpField("Host", BareJid));
136 HasHost = true;
137 break;
138
139 case "cookie":
140 case "set-cookie":
141 // Do not forward cookies.
142 break;
143
144 default:
145 Headers2.Add(new HttpField(Header.Key, Header.Value));
146 break;
147 }
148 }
149
150 if (!HasHost)
151 Headers2.Add(new HttpField("Host", Uri.Authority));
152
153 State State = null;
154 Timer Timer = null;
155
156 try
157 {
158 State = new State();
159 Timer = new Timer((P) =>
160 {
161 State.Done.TrySetResult(false);
162 }, null, TimeoutMs, Timeout.Infinite);
163
164 // TODO: Transport public part of Client certificate, if provided.
165
166 await HttpxClient.Request(FullJid, "DELETE", LocalUrl, 1.1, Headers2, null, async (Sender, e) =>
167 {
168 if (e.Ok)
169 {
170 State.HttpResponse = e.HttpResponse;
171 State.StatusCode = e.StatusCode;
172 State.StatusMessage = e.StatusMessage;
173
174 if (e.HasData)
175 {
176 State.Data ??= new MemoryStream();
177
178 if (!(e.Data is null))
179 {
180 await State.Data.WriteAsync(e.Data, 0, e.Data.Length);
181 State.Done.TrySetResult(true);
182 }
183 }
184 else
185 State.Done.TrySetResult(true);
186 }
187 else
188 {
189 State.Done.TrySetException((Exception)e.StanzaError ??
190 new GenericException("Unable to delete resource.", null, Uri.OriginalString));
191 }
192
193 }, async (Sender, e) =>
194 {
195 State.Data ??= new MemoryStream();
196
197 await State.Data.WriteAsync(e.Data, 0, e.Data.Length);
198 if (e.Last)
199 State.Done.TrySetResult(true);
200
201 }, State);
202
203 if (!await State.Done.Task)
204 return new ContentResponse(new GenericException(new TimeoutException("Request timed out."), null, Uri.OriginalString));
205
206 Timer.Dispose();
207 Timer = null;
208
209 if (State.StatusCode >= 200 && State.StatusCode < 300)
210 {
211 byte[] Data = State.Data?.ToArray();
212 return new ContentResponse(State.HttpResponse?.ContentType, Data, Data);
213 }
214 else
215 {
216 string ContentType = string.Empty;
217 byte[] EncodedData = State.Data?.ToArray();
218
219 return new ContentResponse(HttpxGetter.GetExceptionObject(State.StatusCode, State.StatusMessage,
220 State.HttpResponse, EncodedData, ContentType));
221 }
222 }
223 finally
224 {
225 State.Data?.Dispose();
226 State.Data = null;
227
228 if (!(State.HttpResponse is null))
229 {
230 await State.HttpResponse.DisposeAsync();
231 State.HttpResponse = null;
232 }
233
234 Timer?.Dispose();
235 Timer = null;
236 }
237 }
238
239 private class State
240 {
241 public HttpResponse HttpResponse = null;
242 public MemoryStream Data = null;
243 public TaskCompletionSource<bool> Done = new TaskCompletionSource<bool>();
244 public string StatusMessage = string.Empty;
245 public int StatusCode = 0;
246 }
247
248 }
249}
Contains information about a response to a content request.
Abstract base class for deleters.
Definition: DeleterBase.cs:13
Generic exception, with meta-data for logging.
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:23
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:255
Content Deleter, deleting content using the HTTPX URI Scheme.
Definition: HttpxDeleter.cs:22
override string[] UriSchemes
Supported URI schemes.
Definition: HttpxDeleter.cs:36
override async Task< ContentResponse > DeleteAsync(Uri Uri, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Deletes a resource, using a Uniform Resource Identifier (or Locator).
Definition: HttpxDeleter.cs:76
HttpxDeleter()
Content Deleter, deleting content using the HTTPX URI Scheme.
Definition: HttpxDeleter.cs:29
override bool CanDelete(Uri Uri, out Grade Grade)
If the deleter is able to delete to a resource, given its URI.
Definition: HttpxDeleter.cs:44
Content Getter, retrieving content using the HTTPX URI Scheme.
Definition: HttpxGetter.cs:23
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:58
bool Disposed
If the client has been disposed.
Definition: XmppClient.cs:1173
bool TryGetExtension(Type Type, out IXmppExtension Extension)
Tries to get a registered extension of a specific type from the client.
Definition: XmppClient.cs:7391
RosterItem GetRosterItem(string BareJID)
Gets a roster item.
Definition: XmppClient.cs:4571
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:607
Grade
Grade enumeration
Definition: Grade.cs:7