2using System.Collections.Generic;
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(
'/');
73 s = s.Substring(0, i);
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))
118 ??
throw new NotAcceptableException(
"List can be returned in XML or JSON formats only. Choose which one using the Accept header.");
120 string[] AllowedCollections = Item?.
Groups;
121 int MaxCount =
int.MaxValue;
128 if (!
int.TryParse(s, out i) || i <= 0)
136 string[] RequestedCollections = s.Split(
',');
138 if (!(AllowedCollections is
null))
140 foreach (
string Collection
in RequestedCollections)
142 if (Array.IndexOf(AllowedCollections, Collection) < 0)
147 AllowedCollections = RequestedCollections;
150 if (!(AllowedCollections is
null) && AllowedCollections.Length == 0)
153 this.ProcessRequest(Response, Alternative, AllowedCollections, Last, MaxCount);
155 return Task.CompletedTask;
158 private async
void ProcessRequest(
HttpResponse Response,
string Alternative,
string[] AllowedCollections,
159 string Last,
int MaxCount)
163 IEnumerable<BlockReference> Blocks;
165 if (AllowedCollections is
null)
167 if (
string.IsNullOrEmpty(Last))
174 Dictionary<string, bool> Collections =
new Dictionary<string, bool>();
176 foreach (
string Collection
in AllowedCollections)
177 Collections[Collection] =
true;
180 (Ref) => Collections.ContainsKey(Ref.Collection));
182 if (
string.IsNullOrEmpty(Last))
191 StringBuilder sb =
new StringBuilder();
197 Response.StatusCode = 200;
198 Response.ContentType = Alternative;
200 sb.Append(
"<blockReferences xmlns=\"");
201 sb.Append(NeuroLedgerClient.NeuroLedgerNamespace);
204 await Response.
Write(sb.ToString());
210 sb.Append(
"<ref id='");
214 sb.Append(Convert.ToBase64String(Ref.
Digest));
216 sb.Append(Convert.ToBase64String(Ref.
Signature));
218 if (!(Ref.
Link is
null))
221 sb.Append(Convert.ToBase64String(Ref.
Link));
231 if (Ref.
Updated != DateTime.MinValue)
237 if (Ref.
Expires != DateTime.MaxValue)
246 sb.Append(Ref.
Status.ToString());
250 sb.Append(
"httpx://");
251 sb.Append(this.client.BareJID);
255 sb.Append(Ref.
Bytes.ToString());
258 await Response.
Write(sb.ToString());
261 await Response.
Write(
"</blockReferences>");
267 Response.StatusCode = 200;
268 Response.ContentType = Alternative;
270 List<KeyValuePair<string, object>> Properties =
new List<KeyValuePair<string, object>>();
273 await Response.
Write(
"[");
278 Properties.Add(
new KeyValuePair<string, object>(
"id", Ref.
ObjectId));
279 Properties.Add(
new KeyValuePair<string, object>(
"d", Convert.ToBase64String(Ref.
Digest)));
280 Properties.Add(
new KeyValuePair<string, object>(
"s", Convert.ToBase64String(Ref.
Signature)));
281 Properties.Add(
new KeyValuePair<string, object>(
"cn", Ref.
Collection));
282 Properties.Add(
new KeyValuePair<string, object>(
"cr", Ref.
Creator));
283 Properties.Add(
new KeyValuePair<string, object>(
"ct", Ref.
Created));
284 Properties.Add(
new KeyValuePair<string, object>(
"b", Ref.
Bytes));
286 if (!(Ref.
Link is
null))
287 Properties.Add(
new KeyValuePair<string, object>(
"l", Convert.ToBase64String(Ref.
Link)));
289 if (Ref.
Updated != DateTime.MinValue)
290 Properties.Add(
new KeyValuePair<string, object>(
"u", Ref.
Updated));
292 if (Ref.
Expires != DateTime.MaxValue)
293 Properties.Add(
new KeyValuePair<string, object>(
"x", Ref.
Expires));
296 Properties.Add(
new KeyValuePair<string, object>(
"t", Ref.
Status.ToString()));
300 sb.Append(
"httpx://");
301 sb.Append(this.client.BareJID);
305 Properties.Add(
new KeyValuePair<string, object>(
"r", sb.ToString()));
310 sb.Append(
JSON.
Encode(Properties.ToArray(),
false));
313 sb.Append(
"," +
JSON.
Encode(Properties.ToArray(),
false));
315 await Response.
Write(sb.ToString());
318 await Response.
Write(
']');
323 throw new NotAcceptableException(
"Desired format not acceptable. Use the Accept header field to select either text/xml or application/json.");
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...
async 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.
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.