4using System.Threading.Tasks;
27 private readonly
string userVariable;
28 private readonly
string[] userPrivileges;
42 this.webServer = WebServer;
43 this.userVariable = UserVariable;
44 this.userPrivileges = UserPrivileges;
71 int i = s.IndexOf(
'/');
76 bool Forbidden =
false;
87 if (Session is
null ||
89 !(v.ValueObject is
IUser User))
95 foreach (
string Privilege
in this.userPrivileges)
97 if (!User.HasPrivilege(Privilege))
122 if (Alternative is
null)
128 string[] AllowedCollections = Item?.
Groups;
129 int MaxCount =
int.MaxValue;
136 if (!
int.TryParse(s, out i) || i <= 0)
147 string[] RequestedCollections = s.Split(
',');
149 if (!(AllowedCollections is
null))
151 foreach (
string Collection
in RequestedCollections)
153 if (Array.IndexOf(AllowedCollections, Collection) < 0)
161 AllowedCollections = RequestedCollections;
164 if (!(AllowedCollections is
null) && AllowedCollections.Length == 0)
170 this.ProcessRequest(Response, Alternative, AllowedCollections, Last, MaxCount);
173 private async
void ProcessRequest(
HttpResponse Response,
string Alternative,
string[] AllowedCollections,
174 string Last,
int MaxCount)
178 IEnumerable<BlockReference> Blocks;
180 if (AllowedCollections is
null)
182 if (
string.IsNullOrEmpty(Last))
189 Dictionary<string, bool> Collections =
new Dictionary<string, bool>();
191 foreach (
string Collection
in AllowedCollections)
192 Collections[Collection] =
true;
195 (Ref) => Collections.ContainsKey(Ref.Collection));
197 if (
string.IsNullOrEmpty(Last))
206 StringBuilder sb =
new StringBuilder();
212 Response.StatusCode = 200;
213 Response.StatusMessage =
"OK";
214 Response.ContentType = Alternative;
216 sb.Append(
"<blockReferences xmlns=\"");
217 sb.Append(NeuroLedgerClient.NeuroLedgerNamespace);
220 await Response.
Write(sb.ToString());
226 sb.Append(
"<ref id='");
230 sb.Append(Convert.ToBase64String(Ref.
Digest));
232 sb.Append(Convert.ToBase64String(Ref.
Signature));
234 if (!(Ref.
Link is
null))
237 sb.Append(Convert.ToBase64String(Ref.
Link));
247 if (Ref.
Updated != DateTime.MinValue)
253 if (Ref.
Expires != DateTime.MaxValue)
262 sb.Append(Ref.
Status.ToString());
266 sb.Append(
"httpx://");
267 sb.Append(this.client.BareJID);
271 sb.Append(Ref.
Bytes.ToString());
274 await Response.
Write(sb.ToString());
277 await Response.
Write(
"</blockReferences>");
283 Response.StatusCode = 200;
284 Response.StatusMessage =
"OK";
285 Response.ContentType = Alternative;
287 List<KeyValuePair<string, object>> Properties =
new List<KeyValuePair<string, object>>();
290 await Response.
Write(
"[");
295 Properties.Add(
new KeyValuePair<string, object>(
"id", Ref.
ObjectId));
296 Properties.Add(
new KeyValuePair<string, object>(
"d", Convert.ToBase64String(Ref.
Digest)));
297 Properties.Add(
new KeyValuePair<string, object>(
"s", Convert.ToBase64String(Ref.
Signature)));
298 Properties.Add(
new KeyValuePair<string, object>(
"cn", Ref.
Collection));
299 Properties.Add(
new KeyValuePair<string, object>(
"cr", Ref.
Creator));
300 Properties.Add(
new KeyValuePair<string, object>(
"ct", Ref.
Created));
301 Properties.Add(
new KeyValuePair<string, object>(
"b", Ref.
Bytes));
303 if (!(Ref.
Link is
null))
304 Properties.Add(
new KeyValuePair<string, object>(
"l", Convert.ToBase64String(Ref.
Link)));
306 if (Ref.
Updated != DateTime.MinValue)
307 Properties.Add(
new KeyValuePair<string, object>(
"u", Ref.
Updated));
309 if (Ref.
Expires != DateTime.MaxValue)
310 Properties.Add(
new KeyValuePair<string, object>(
"x", Ref.
Expires));
313 Properties.Add(
new KeyValuePair<string, object>(
"t", Ref.
Status.ToString()));
317 sb.Append(
"httpx://");
318 sb.Append(this.client.BareJID);
322 Properties.Add(
new KeyValuePair<string, object>(
"r", sb.ToString()));
327 sb.Append(
JSON.
Encode(Properties.ToArray(),
false));
330 sb.Append(
"," +
JSON.
Encode(Properties.ToArray(),
false));
332 await Response.
Write(sb.ToString());
335 await Response.
Write(
']');
Static class that does BASE64URL encoding (using URL and filename safe alphabet), as defined in RFC46...
static string Encode(byte[] Data)
Converts a binary block of data to a Base64URL-encoded string.
Helps with common JSON-related tasks.
static string Encode(string s)
Encodes a string for inclusion in JSON.
const string DefaultContentType
application/json
const string DefaultContentType
Default content type for XML documents.
const string SchemaContentType
Default content type for XML schema documents.
Helps with common XML-related tasks.
static string Encode(string s)
Encodes a string for use in XML.
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
The server understood the request, but is refusing to fulfill it. Authorization will not help and the...
Base class for all asynchronous HTTP resources. An asynchronous resource responds outside of the meth...
Represents an HTTP request.
HttpRequestHeader Header
Request header.
string RemoteEndPoint
Remote end-point.
static string GetSessionId(HttpRequest Request, HttpResponse Response)
Gets the session ID used for a request.
const string HttpSessionID
The Cookie Key for HTTP Session Identifiers: "HttpSessionID"
string ResourceName
Name of resource.
Represets a response of an HTTP client request.
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
Task Write(byte[] Data)
Returns binary data in the response.
Implements an HTTP server.
The resource identified by the request is only capable of generating response entities which have con...
Provides authenticated and authorized clients with lists of available blocks.
BlockListResource(string ResourceName, XmppClient Client, HttpServer WebServer, string UserVariable, params string[] UserPrivileges)
Provides authenticated and authorized clients with binary blocks.
override bool HandlesSubPaths
If the resource handles sub-paths.
bool AllowsGET
If the GET method is allowed.
async Task GET(HttpRequest Request, HttpResponse Response)
Executes the GET method on the resource.
override bool UserSessions
If the resource uses user sessions.
Maintains information about an item in the roster.
SubscriptionState State
roup Current subscription state.
string[] Groups
Any groups the roster item belongs to.
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Static interface for database persistence. In order to work, a database provider has to be assigned t...
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
This filter selects objects that conform to all child-filters provided.
Custom filter used to filter objects using an external expression.
This filter selects objects that have a named field greater than a given value.
Contains a reference to a block in the ledger.
ulong Bytes
Size of block, in bytes.
byte[] Digest
Digest of block
byte[] Signature
Signature of block
Contains information about a variable.
virtual bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
GET Interface for HTTP resources.
Basic interface for a user.
SubscriptionState
State of a presence subscription.
BlockStatus
Status of the block.