Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
IsContent.cs
1using System;
2using System.IO;
3using System.Threading.Tasks;
4using System.Xml;
7using Waher.Events;
9
11{
15 public class IsContent : WafComparison
16 {
17 private readonly StringAttribute manifestFile;
18
22 public IsContent()
23 : base()
24 {
25 }
26
33 public IsContent(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document)
34 : base(Xml, Parent, Document)
35 {
36 this.manifestFile = new StringAttribute(Xml, "manifestFile");
37 }
38
42 public override string LocalName => nameof(IsContent);
43
51 public override WafAction Create(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document) => new IsContent(Xml, Parent, Document);
52
58 public override async Task<WafResult?> Review(ProcessingState State)
59 {
60 if (!State.Request.Server.TryGetFileName(State.Request.Header.ResourcePart,
61 false, out string ResourceFileName))
62 {
63 return null;
64 }
65
66 string ManifestFileName = await this.manifestFile.EvaluateAsync(State.Variables, string.Empty);
67 string Key = ResourceFileName + "|C|" + ManifestFileName;
68
69 if (!State.TryGetCachedObject(Key, out bool IsMatch))
70 {
71 if (!File.Exists(ManifestFileName))
72 ManifestFileName = Path.Combine(State.Firewall.AppDataFolder, ManifestFileName);
73
74 if (ResourceFileName.StartsWith(State.Firewall.AppDataFolder, StringComparison.OrdinalIgnoreCase))
75 ResourceFileName = ResourceFileName[State.Firewall.AppDataFolder.Length..];
76
77 if (ResourceFileName.StartsWith('/'))
78 ResourceFileName = ResourceFileName[1..];
79
80 IsMatch = CheckMatch(ManifestFileName, ResourceFileName);
81
82 State.AddToCache(Key, IsMatch, fiveMinutes);
83 }
84
85 return IsMatch ? await this.ReviewChildren(State) : null;
86 }
87
88 private static bool CheckMatch(string ManifestFileName, string ContentFileName)
89 {
90 try
91 {
92 if (!File.Exists(ManifestFileName))
93 return false;
94
95 XmlDocument Doc = XML.LoadFromFile(ManifestFileName);
96
97 if (Doc.DocumentElement is null ||
98 Doc.DocumentElement.LocalName != "Module" ||
99 Doc.DocumentElement.NamespaceURI != "http://waher.se/Schema/ModuleManifest.xsd")
100 {
101 return false;
102 }
103
104 XmlElement Loop = Doc.DocumentElement;
105 string[] Parts = ContentFileName.Split(pathSeparators);
106 int i, c = Parts.Length;
107
108 for (i = string.IsNullOrEmpty(Parts[0]) ? 1 : 0; i < c; i++)
109 {
110 XmlElement Found = null;
111
112 foreach (XmlNode N in Loop.ChildNodes)
113 {
114 if (!(N is XmlElement E))
115 continue;
116
117 if (i == c - 1)
118 {
119 if (E.LocalName == "Content" &&
120 E.HasAttribute("fileName") &&
121 E.GetAttribute("fileName").Equals(Parts[i], StringComparison.CurrentCultureIgnoreCase))
122 {
123 Found = E;
124 break;
125 }
126 }
127 else
128 {
129 if (E.LocalName == "Folder" &&
130 E.HasAttribute("name") &&
131 E.GetAttribute("name").Equals(Parts[i], StringComparison.CurrentCultureIgnoreCase))
132 {
133 Found = E;
134 break;
135 }
136 }
137 }
138
139 if (Found is null)
140 return false;
141 else
142 Loop = Found;
143 }
144
145 return true;
146 }
147 catch (Exception ex)
148 {
149 Log.Exception(ex, ManifestFileName);
150 return false;
151 }
152 }
153
154 private static readonly char[] pathSeparators = new char[] { '/', '\\' };
155 }
156}
Helps with common XML-related tasks.
Definition: XML.cs:21
static XmlDocument LoadFromFile(string FileName)
Loads an XML document from a file.
Definition: XML.cs:1808
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
Checks if the request is encrypted.
Definition: IsContent.cs:16
override string LocalName
XML Local Name for the XML element defining the action.
Definition: IsContent.cs:42
IsContent(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document)
Checks if the request is encrypted.
Definition: IsContent.cs:33
IsContent()
Checks if the request is encrypted.
Definition: IsContent.cs:22
override async Task< WafResult?> Review(ProcessingState State)
Reviews the processing state, and returns a WAF result, if any.
Definition: IsContent.cs:58
override WafAction Create(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document)
Creates a WAF action from its XML definition.
Abstract base class for Web Application Firewall comparisons.
Definition: WafComparison.cs:9
Contains the current state of a review process.
Variables Variables
Set of variables.
void AddToCache(string Key, object Value)
Adds a value to the cache.
WebApplicationFirewall Firewall
Reference to the current Web Application Firewall object.
HttpRequest Request
Current HTTP Request
Abstract base class for Web Application Firewall actions.
Definition: WafAction.cs:17
WebApplicationFirewall Document
Web Application Firewall document.
Definition: WafAction.cs:60
static readonly TimeSpan fiveMinutes
Five minutes time span.
Definition: WafAction.cs:131
async Task< WafResult?> ReviewChildren(ProcessingState State)
Reviews the processing state, and returns a WAF result, if any.
Definition: WafActions.cs:72
Web Application Firewall for HttpServer.
string AppDataFolder
Application data folder, where content files are stored.