forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.cs
More file actions
470 lines (403 loc) · 12.9 KB
/
State.cs
File metadata and controls
470 lines (403 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using SkiaSharp;
using Waher.Content.Xml;
using Waher.Events;
using Waher.Networking.HTTP;
using Waher.Script;
using Waher.Script.Abstraction.Elements;
using Waher.Script.Exceptions;
using Waher.Script.Graphs;
using Waher.Script.Objects;
using Waher.Script.Objects.Matrices;
using Waher.Security;
using Waher.Security.LoginMonitor;
namespace Waher.WebService.Script
{
/// <summary>
/// Internal script execution state.
/// </summary>
internal class State
{
private static readonly Dictionary<string, State> expressions = new Dictionary<string, State>();
private readonly string tag;
private readonly Stopwatch watch = new Stopwatch();
private readonly int timeout;
private readonly Variables variables;
private readonly StringBuilder printOutput;
private Expression expression;
private HttpRequest request;
private HttpResponse response;
private IUser user;
private Timer watchdog = null;
private Thread thread = null;
private int counter;
private bool previewing;
/// <summary>
/// Internal script execution state.
/// </summary>
/// <param name="Request">HTTP Request object.</param>
/// <param name="Response">HTTP Response object for request.</param>
/// <param name="Tag">Client-side tag.</param>
/// <param name="Timeout">Timeout, in milliseconds.</param>
/// <param name="User">User executing the script.</param>
public State(HttpRequest Request, HttpResponse Response, string Tag, int Timeout, IUser User)
{
this.request = Request;
this.response = Response;
this.tag = Tag;
this.timeout = Timeout;
this.user = User;
this.expression = null;
this.previewing = false;
this.counter = 0;
this.variables = new Variables();
Request.Session.CopyTo(this.variables);
this.variables["Request"] = Request;
this.variables["Response"] = Response;
this.printOutput = new StringBuilder();
this.variables.ConsoleOut = new StringWriter(this.printOutput);
}
internal static bool TryGetState(string Tag, out State State)
{
lock (expressions)
{
return expressions.TryGetValue(Tag, out State);
}
}
internal void SetExpression(Expression Expression)
{
if (!(this.expression is null))
throw new InvalidOperationException("Expression already set.");
this.expression = Expression;
this.expression.Tag = this;
this.expression.OnPreview += Expression_OnPreview;
}
private void Expression_OnPreview(object Sender, PreviewEventArgs e)
{
if (!(this.response?.HeaderSent ?? true))
{
this.previewing = true;
Task.Run(() => this.SendResponse(e.Preview, true));
}
}
internal void SetRequestResponse(HttpRequest Request, HttpResponse Response, IUser User)
{
this.request = Request;
this.response = Response;
this.user = User;
}
/// <summary>
/// Starts the watch.
/// </summary>
public void Start()
{
if (!(this.thread is null))
throw new InvalidOperationException("Evaluation already started.");
this.thread = new Thread(this.Execute);
this.watchdog = new Timer(this.WatchdogTimer, null, 1000, 1000);
lock (expressions)
{
expressions[this.tag] = this;
}
this.thread.Start();
}
/// <summary>
/// Stops the watch.
/// </summary>
public void Stop()
{
this.watch.Stop();
}
/// <summary>
/// Elapsed milliseconds
/// </summary>
public double Milliseconds
{
get => (this.watch.ElapsedTicks * 1000.0) / Stopwatch.Frequency;
}
private async void Execute(object P)
{
IElement Result;
try
{
try
{
this.watch.Start();
Result = this.expression.Root.Evaluate(this.variables);
}
catch (ScriptReturnValueException ex)
{
Result = ex.ReturnValue;
}
catch (ScriptAbortedException)
{
this.variables.CancelAbort();
Result = new ObjectValue(new TimeoutException("Script forcefully aborted. You can control the timeout threshold, by setting the Timeout variable to the number of milliseconds to use."));
}
catch (Exception ex)
{
Result = new ObjectValue(ex);
}
finally
{
this.watch.Stop();
Timer Temp = this.watchdog;
this.watchdog = null;
Temp?.Dispose();
KeyValuePair<string, object>[] Tags = await LoginAuditor.Annotate(this.request.RemoteEndPoint,
new KeyValuePair<string, object>("RemoteEndPoint", this.request.RemoteEndPoint),
new KeyValuePair<string, object>("Script", this.expression.Script),
new KeyValuePair<string, object>("Milliseconds", this.Milliseconds));
Log.Notice("Script evaluated.", this.request.Resource.ResourceName, this.user.UserName, "ScriptEval", Tags);
}
if (!this.response.HeaderSent)
{
lock (expressions)
{
expressions.Remove(this.tag);
}
await this.SendResponse(Result, false);
}
this.variables.CopyTo(this.request.Session);
}
catch (ThreadAbortException)
{
if (!this.response.HeaderSent)
{
lock (expressions)
{
expressions.Remove(this.tag);
}
await this.SendResponse(new ObjectValue(
new TimeoutException("Script forcefully aborted. You can control the timeout threshold, by setting the Timeout variable to the number of milliseconds to use.")),
false);
}
}
catch (Exception ex)
{
Log.Critical(ex);
}
finally
{
Timer Temp = this.watchdog;
this.watchdog = null;
Temp?.Dispose();
}
}
private async void WatchdogTimer(object State)
{
try
{
double ms = this.Milliseconds;
this.counter++;
if (!this.response.HeaderSent && !this.previewing)
{
this.response.SetHeader("X-More", "1");
this.response.ContentType = "text/html";
await this.response.Write("<p><font style=\"color:green\"><code>" + new string('.', this.counter) + "</code></font></p>");
await this.response.SendResponse();
this.response.Dispose();
}
if (ms >= this.timeout)
{
Timer Temp = this.watchdog;
this.watchdog = null;
Temp?.Dispose();
MethodInfo AbortInternal = null;
foreach (MethodInfo MI in this.thread.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance))
{
if (MI.Name == "AbortInternal" && MI.GetParameters().Length == 0)
{
AbortInternal = MI;
break;
}
}
if (AbortInternal is null)
this.variables?.Abort();
else
{
try
{
AbortInternal.Invoke(this.thread, new object[0]);
}
catch (Exception)
{
this.variables?.Abort();
}
}
Log.Warning("Long-running script forcefully terminated.", this.request.Resource.ResourceName, this.user.UserName, "ScriptAbort",
new KeyValuePair<string, object>("RemoteEndPoint", this.request.RemoteEndPoint),
new KeyValuePair<string, object>("Script", this.expression.Script),
new KeyValuePair<string, object>("Milliseconds", ms));
}
else if (this.counter == 5) // 5 sceonds
{
Log.Notice("Long-running script.", this.request.Resource.ResourceName, this.user.UserName, "ScriptLong",
new KeyValuePair<string, object>("RemoteEndPoint", this.request.RemoteEndPoint),
new KeyValuePair<string, object>("Script", this.expression.Script),
new KeyValuePair<string, object>("Milliseconds", ms));
}
}
catch (Exception)
{
// Ignore
}
}
internal async Task SendResponse(IElement Result, bool More)
{
this.variables["Ans"] = Result;
byte[] Bin;
object Obj;
string s;
if (Result is Graph G)
{
GraphSettings Settings = new GraphSettings();
Tuple<int, int> Size;
double d;
if ((Size = G.RecommendedBitmapSize) != null)
{
Settings.Width = Size.Item1;
Settings.Height = Size.Item2;
Settings.MarginLeft = (int)Math.Round(15.0 * Settings.Width / 640);
Settings.MarginRight = Settings.MarginLeft;
Settings.MarginTop = (int)Math.Round(15.0 * Settings.Height / 480);
Settings.MarginBottom = Settings.MarginTop;
Settings.LabelFontSize = 12.0 * Settings.Height / 480;
}
else
{
if (this.variables.TryGetVariable("GraphWidth", out Variable v) && (Obj = v.ValueObject) is double && (d = (double)Obj) >= 1)
{
Settings.Width = (int)Math.Round(d);
Settings.MarginLeft = (int)Math.Round(15 * d / 640);
Settings.MarginRight = Settings.MarginLeft;
}
else if (!this.variables.ContainsVariable("GraphWidth"))
this.variables["GraphWidth"] = (double)Settings.Width;
if (this.variables.TryGetVariable("GraphHeight", out v) && (Obj = v.ValueObject) is double && (d = (double)Obj) >= 1)
{
Settings.Height = (int)Math.Round(d);
Settings.MarginTop = (int)Math.Round(15 * d / 480);
Settings.MarginBottom = Settings.MarginTop;
Settings.LabelFontSize = 12 * d / 480;
}
else if (!this.variables.ContainsVariable("GraphHeight"))
this.variables["GraphHeight"] = (double)Settings.Height;
}
using (SKImage Bmp = G.CreateBitmap(Settings, out object[] States))
{
string Tag = Guid.NewGuid().ToString();
SKData Data = Bmp.Encode(SKEncodedImageFormat.Png, 100);
Bin = Data.ToArray();
s = Convert.ToBase64String(Bin, 0, Bin.Length);
s = "<figure><img border=\"2\" width=\"" + Settings.Width.ToString() + "\" height=\"" + Settings.Height.ToString() +
"\" src=\"data:image/png;base64," + s + "\" onclick=\"GraphClicked(this,event,'" + Tag + "');\" /></figure>";
Data.Dispose();
if (!(this.variables["Graphs"] is Dictionary<string, KeyValuePair<Graph, object[]>> Graphs))
{
Graphs = new Dictionary<string, KeyValuePair<Graph, object[]>>();
this.variables["Graphs"] = Graphs;
}
lock (Graphs)
{
Graphs[Tag] = new KeyValuePair<Graph, object[]>(G, States);
}
}
}
else if (Result.AssociatedObjectValue is SKImage Img)
{
SKData Data = Img.Encode(SKEncodedImageFormat.Png, 100);
Bin = Data.ToArray();
s = Convert.ToBase64String(Bin, 0, Bin.Length);
s = "<figure><img border=\"2\" width=\"" + Img.Width.ToString() + "\" height=\"" + Img.Height.ToString() +
"\" src=\"data:image/png;base64," + s + "\" /></figure>";
Data.Dispose();
}
else if (Result.AssociatedObjectValue is Exception ex)
{
ex = Log.UnnestException(ex);
if (ex is AggregateException ex2)
{
StringBuilder sb2 = new StringBuilder();
foreach (Exception ex3 in ex2.InnerExceptions)
{
sb2.Append("<p><font style=\"color:red;font-weight:bold\"><code>");
sb2.Append(this.FormatText(XML.HtmlValueEncode(ex3.Message)));
sb2.Append("</code></font></p>");
}
s = sb2.ToString();
}
else
s = "<p><font style=\"color:red;font-weight:bold\"><code>" + this.FormatText(XML.HtmlValueEncode(ex.Message)) + "</code></font></p>";
}
else if (Result is ObjectMatrix M && M.ColumnNames != null)
{
StringBuilder Html = new StringBuilder();
s = Result.ToString();
Html.Append("<div class='clickable' onclick='SetScript(this);'><code style='display:none'>");
Html.Append(XML.Encode(s));
Html.Append("</code><table><thead><tr>");
foreach (string Name in M.ColumnNames)
{
Html.Append("<th>");
Html.Append(this.FormatText(XML.HtmlValueEncode(Name)));
Html.Append("</th>");
}
Html.Append("</tr></thead><tbody>");
int x, y;
for (y = 0; y < M.Rows; y++)
{
Html.Append("<tr>");
for (x = 0; x < M.Columns; x++)
{
Html.Append("<td>");
object Item = M.GetElement(x, y).AssociatedObjectValue;
if (!(Item is null))
{
if (Item is string s3)
Html.Append(this.FormatText(XML.HtmlValueEncode(s3)));
else
Html.Append(this.FormatText(XML.HtmlValueEncode(Expression.ToString(Item))));
}
Html.Append("</td>");
}
Html.Append("</tr>");
}
Html.Append("</tbody></table></div>");
s = Html.ToString();
}
else
{
s = Result.ToString();
s = "<div class='clickable' onclick='SetScript(this);'><code style='display:none'>" + XML.Encode(s) +
"</code><p><font style=\"color:red\"><code>" + this.FormatText(XML.HtmlValueEncode(s)) + "</code></font></p></div>";
}
string s2 = this.printOutput.ToString();
if (!string.IsNullOrEmpty(s2))
s = "<p><font style=\"color:blue\"><code>" + this.FormatText(XML.HtmlValueEncode(s2)) + "</code></font></p>" + s;
Bin = Encoding.UTF8.GetBytes(s);
this.response.ContentType = "text/html; charset=utf-8";
this.response.ContentLength = Bin.Length; // To avoid chunked transfer.
this.response.SetHeader("X-More", More ? "1" : "0");
await this.response.Write(Bin);
await this.response.SendResponse();
this.response.Dispose();
}
private string FormatText(string s)
{
return s.
Replace("\r\n", "\n").
Replace("\n", "<br/>").
Replace("\r", "<br/>").
Replace("\t", " ").
Replace(" ", " ");
}
}
}