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) ||
41 Request.Header.Referer is null)
42 {
43 throw new BadRequestException("Missing parameters.");
44 }
45
46 CaseInsensitiveString Contact = s;
47 Uri Uri = new Uri(Request.Header.Referer.Value);
48
49 s = Uri.AbsolutePath;
50 if (s.StartsWith("/"))
51 s = s[1..];
52
53 int i = s.IndexOf('/');
54 if (i < 0)
55 i = s.Length;
56
57 CaseInsensitiveString NodeName = s[..i];
58
59 PubSubNode Node = await XmppServerModule.PubSub.GetNodeAsync(CaseInsensitiveString.Empty, NodeName, null, XmppAddress.Empty, null)
60 ?? throw new NotFoundException("Node not found: " + NodeName);
61
62 if (!Node.PublishOnWeb)
63 throw new BadRequestException("Node is not a web node.");
64
65 s = s[i..];
66 if (string.IsNullOrEmpty(s) || s == "/")
67 {
68 if ((Node.Creator != Contact) &&
69 (Node.Contact is null || Array.IndexOf(Node.Contact, Contact) < 0) &&
70 (Node.Publishers is null || Array.IndexOf(Node.Publishers, Contact) < 0) &&
71 (Node.PublishersOnly is null || Array.IndexOf(Node.PublishersOnly, Contact) < 0))
72 {
73 throw new ForbiddenException(Request, "Access to avatar not granted: " + Contact + " is not a publisher of " + NodeName + ".");
74 }
75 }
76 else
77 {
78 PubSubItem Item = await XmppServerModule.PubSub.LoadItem(CaseInsensitiveString.Empty, NodeName, s);
79 if (Item is null && s.StartsWith("/"))
80 Item = await XmppServerModule.PubSub.LoadItem(CaseInsensitiveString.Empty, NodeName, s[1..]);
81
82 if (Item is null)
83 {
84 string s2 = HttpUtility.UrlDecode(s);
85
86 if (s != s2)
87 {
88 s = s2;
89 Item = await XmppServerModule.PubSub.LoadItem(CaseInsensitiveString.Empty, NodeName, s);
90
91 if (Item is null && s.StartsWith("/"))
92 Item = await XmppServerModule.PubSub.LoadItem(CaseInsensitiveString.Empty, NodeName, s[1..]);
93 }
94
95 if (Item is null)
96 throw new NotFoundException("Item not found on node: " + s);
97 }
98
99 if (Contact != Item.Publisher)
100 throw new ForbiddenException(Request, Contact + " not the publisher of " + s);
101 }
102
103 HttpRequestHeader Header = Request.Header;
105
106 if (!(Header.IfNoneMatch is null) && Header.IfNoneMatch.Value == Avatar.Hash)
107 {
108 Response.StatusCode = 304;
109 Response.StatusMessage = "Not Modified";
110 }
111 else
112 {
113 Response.StatusCode = 200;
114 Response.StatusMessage = "OK";
115 Response.ContentType = Avatar.ContentType;
116 Response.SetHeader("ETag", "\"" + Avatar.Hash + "\"");
117 await Response.Write(true, Avatar.Binary);
118 }
119 }
120
121 }
122}
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static AvatarClient AvatarClient
XMPP Concentrator Server.
Definition: Gateway.cs:4058
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.
Represents an HTTP request.
Definition: HttpRequest.cs:22
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:182
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Task Write(byte[] Data)
Returns binary data in the response.
void SetHeader(string FieldName, string Value)
Sets a custom header field value.
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...
async Task< Avatar > GetAvatarAsync(string BareJid)
Gets an avatar.
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....
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.