Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PublisherAvatar.cs
1using System;
2using System.Threading.Tasks;
3using System.Web;
10
12{
14 {
15 public PublisherAvatar()
16 : base("/PublisherAvatar")
17 {
18 }
19
20 public override bool HandlesSubPaths
21 {
22 get
23 {
24 return false;
25 }
26 }
27
28 public override bool UserSessions
29 {
30 get
31 {
32 return true;
33 }
34 }
35
36 public bool AllowsGET => true;
37
38 public async Task GET(HttpRequest Request, HttpResponse Response)
39 {
40 if (!Request.Header.TryGetQueryParameter("c", out string s) || Request.Header.Referer is null)
41 throw new BadRequestException("Missing parameters.");
42
43 CaseInsensitiveString Contact = s;
44 Uri Uri = new Uri(Request.Header.Referer.Value);
45
46 s = Uri.AbsolutePath;
47 if (s.StartsWith("/"))
48 s = s.Substring(1);
49
50 int i = s.IndexOf('/');
51 if (i < 0)
52 i = s.Length;
53
54 CaseInsensitiveString NodeName = s.Substring(0, i);
55
56 PubSubNode Node = await XmppServerModule.PubSub.GetNodeAsync(CaseInsensitiveString.Empty, NodeName, null, XmppAddress.Empty, null)
57 ?? throw new NotFoundException("Node not found: " + NodeName);
58
59 if (!Node.PublishOnWeb)
60 throw new BadRequestException("Node is not a web node.");
61
62 s = s.Substring(i);
63 if (string.IsNullOrEmpty(s) || s == "/")
64 {
65 if ((Node.Creator != Contact) &&
66 (Node.Contact is null || Array.IndexOf(Node.Contact, Contact) < 0) &&
67 (Node.Publishers is null || Array.IndexOf(Node.Publishers, Contact) < 0) &&
68 (Node.PublishersOnly is null || Array.IndexOf(Node.PublishersOnly, Contact) < 0))
69 {
70 throw new ForbiddenException("Access to avatar not granted: " + Contact + " is not a publisher of " + NodeName + ".");
71 }
72 }
73 else
74 {
75 PubSubItem Item = await XmppServerModule.PubSub.LoadItem(CaseInsensitiveString.Empty, NodeName, s);
76 if (Item is null && s.StartsWith("/"))
77 Item = await XmppServerModule.PubSub.LoadItem(CaseInsensitiveString.Empty, NodeName, s.Substring(1));
78
79 if (Item is null)
80 {
81 string s2 = HttpUtility.UrlDecode(s);
82
83 if (s != s2)
84 {
85 s = s2;
86 Item = await XmppServerModule.PubSub.LoadItem(CaseInsensitiveString.Empty, NodeName, s);
87
88 if (Item is null && s.StartsWith("/"))
89 Item = await XmppServerModule.PubSub.LoadItem(CaseInsensitiveString.Empty, NodeName, s.Substring(1));
90 }
91
92 if (Item is null)
93 throw new NotFoundException("Item not found on node: " + s);
94 }
95
96 if (Contact != Item.Publisher)
97 throw new ForbiddenException(Contact + " not the publisher of " + s);
98 }
99
100 HttpRequestHeader Header = Request.Header;
101 Avatar Avatar = await Gateway.AvatarClient.GetAvatarAsync(Contact);
102
103 if (!(Header.IfNoneMatch is null) && Header.IfNoneMatch.Value == Avatar.Hash)
104 {
105 Response.StatusCode = 304;
106 Response.StatusMessage = "Not Modified";
107 }
108 else
109 {
110 Response.StatusCode = 200;
111 Response.ContentType = Avatar.ContentType;
112 Response.SetHeader("ETag", "\"" + Avatar.Hash + "\"");
113 await Response.Write(Avatar.Binary);
114 }
115 }
116
117 }
118}
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static Networking.XMPP.Avatar.AvatarClient AvatarClient
XMPP Concentrator Server.
Definition: Gateway.cs:3219
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...
Contains information about all fields in an HTTP request header.
HttpFieldReferer Referer
Referer HTTP Field header. (RFC 2616, §14.36)
bool TryGetQueryParameter(string QueryParameter, out string Value)
Tries to get the value of an individual query parameter, if available.
HttpFieldIfNoneMatch IfNoneMatch
If-None-Match HTTP Field header. (RFC 2616, §14.26)
Represents an HTTP request.
Definition: HttpRequest.cs:18
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:134
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
void SetHeader(string FieldName, string Value)
Sets a custom header field value.
async Task Write(byte[] Data)
Returns binary data in the response.
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
The server has not found anything matching the Request-URI. No indication is given of whether the con...
Contains information about an avatar.
Definition: Avatar.cs:13
string Hash
Hash digest of avatar.
Definition: Avatar.cs:90
string ContentType
Content-Type of binary encoding of avatar.
Definition: Avatar.cs:100
byte[] Binary
Binary encoding of avatar.
Definition: Avatar.cs:110
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
static readonly XmppAddress Empty
Empty address.
Definition: XmppAddress.cs:31
Represents a case-insensitive string.
static readonly CaseInsensitiveString Empty
Empty case-insensitive string
int IndexOf(CaseInsensitiveString value, StringComparison comparisonType)
Reports the zero-based index of the first occurrence of the specified string in the current System....
CaseInsensitiveString Substring(int startIndex, int length)
Retrieves a substring from this instance. The substring starts at a specified character position and ...
async Task< PubSubNode > GetNodeAsync(CaseInsensitiveString Service, CaseInsensitiveString NodeName, NodeAccessModel? AutoCreateAccess, XmppAddress From, CaseInsensitiveString Domain)
Gets a pubsub node.
CaseInsensitiveString Publisher
Publisher of content.
Definition: PubSubItem.cs:92
Defines a node on which items can be published.
Definition: PubSubNode.cs:19
bool PublishOnWeb
If the items published to the node should be available on the web or not.
Definition: PubSubNode.cs:654
CaseInsensitiveString[] PublishersOnly
Publisher-only affiliation.
Definition: PubSubNode.cs:595
CaseInsensitiveString[] Publishers
Publisher affiliation.
Definition: PubSubNode.cs:585
CaseInsensitiveString Creator
Creator of node.
Definition: PubSubNode.cs:123
CaseInsensitiveString[] Contact
The JIDs of those to contact with questions
Definition: PubSubNode.cs:243
async Task GET(HttpRequest Request, HttpResponse Response)
Executes the GET method on the resource.
bool AllowsGET
If the GET method is allowed.
Service Module hosting the XMPP broker and its components.
GET Interface for HTTP resources.