forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageResult.cs
More file actions
1109 lines (932 loc) · 43 KB
/
PageResult.cs
File metadata and controls
1109 lines (932 loc) · 43 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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using ServiceStack.Text;
using ServiceStack.Web;
namespace ServiceStack.Script
{
public interface IPageResult {}
// Render a Template Page to the Response OutputStream
public class PageResult : IPageResult, IStreamWriterAsync, IHasOptions, IDisposable
{
/// <summary>
/// The Page to Render
/// </summary>
public SharpPage Page { get; }
/// <summary>
/// The Code Page to Render
/// </summary>
public SharpCodePage CodePage { get; }
/// <summary>
/// Use specified Layout
/// </summary>
public SharpPage LayoutPage { get; set; }
/// <summary>
/// Use Layout with specified name
/// </summary>
public string Layout { get; set; }
/// <summary>
/// Render without any Layout
/// </summary>
public bool NoLayout { get; set; }
/// <summary>
/// Extract Model Properties into Scope Args
/// </summary>
public object Model { get; set; }
/// <summary>
/// Add additional Args available to all pages
/// </summary>
public Dictionary<string, object> Args { get; set; }
/// <summary>
/// Add additional script methods available to all pages
/// </summary>
public List<ScriptMethods> ScriptMethods { get; set; }
[Obsolete("Use ScriptMethods")] public List<ScriptMethods> TemplateFilters => ScriptMethods;
/// <summary>
/// Add additional script blocks available to all pages
/// </summary>
public List<ScriptBlock> ScriptBlocks { get; set; }
[Obsolete("Use ScriptBlocks")] public List<ScriptBlock> TemplateBlocks => ScriptBlocks;
/// <summary>
/// Add additional partials available to all pages
/// </summary>
public Dictionary<string, SharpPage> Partials { get; set; }
/// <summary>
/// Return additional HTTP Headers in HTTP Requests
/// </summary>
public IDictionary<string, string> Options { get; set; }
/// <summary>
/// Specify the Content-Type of the Response
/// </summary>
public string ContentType
{
get => Options.TryGetValue(HttpHeaders.ContentType, out string contentType) ? contentType : null;
set => Options[HttpHeaders.ContentType] = value;
}
/// <summary>
/// Transform the Page output using a chain of stream transformers
/// </summary>
public List<Func<Stream, Task<Stream>>> PageTransformers { get; set; }
/// <summary>
/// Transform the entire output using a chain of stream transformers
/// </summary>
public List<Func<Stream, Task<Stream>>> OutputTransformers { get; set; }
/// <summary>
/// Available transformers that can transform context filter stream outputs
/// </summary>
public Dictionary<string, Func<Stream, Task<Stream>>> FilterTransformers { get; set; }
/// <summary>
/// Don't allow access to specified filters
/// </summary>
public HashSet<string> ExcludeFiltersNamed { get; } = new HashSet<string>();
/// <summary>
/// The last error thrown by a filter
/// </summary>
public Exception LastFilterError { get; set; }
/// <summary>
/// The StackTrace where the Last Error Occured
/// </summary>
public string[] LastFilterStackTrace { get; set; }
/// <summary>
/// What argument errors should be binded to
/// </summary>
public string AssignExceptionsTo { get; set; }
/// <summary>
/// Whether to skip execution of all page filters and just write page string fragments
/// </summary>
public bool SkipFilterExecution { get; set; }
/// <summary>
/// Overrides Context to specify whether to Ignore or Continue executing filters on error
/// </summary>
public bool? SkipExecutingFiltersIfError { get; set; }
/// <summary>
/// Whether to always rethrow Exceptions
/// </summary>
public bool RethrowExceptions { get; set; }
/// <summary>
/// Immediately halt execution of the page
/// </summary>
public bool HaltExecution { get; set; }
/// <summary>
/// Whether to disable buffering output and render directly to OutputStream
/// </summary>
public bool DisableBuffering { get; set; }
/// <summary>
/// The Return value of the page (if any)
/// </summary>
public ReturnValue ReturnValue { get; set; }
private readonly Stack<string> stackTrace = new Stack<string>();
private PageResult(PageFormat format)
{
Args = new Dictionary<string, object>();
ScriptMethods = new List<ScriptMethods>();
ScriptBlocks = new List<ScriptBlock>();
Partials = new Dictionary<string, SharpPage>();
PageTransformers = new List<Func<Stream, Task<Stream>>>();
OutputTransformers = new List<Func<Stream, Task<Stream>>>();
FilterTransformers = new Dictionary<string, Func<Stream, Task<Stream>>>();
Options = new Dictionary<string, string>
{
{HttpHeaders.ContentType, format?.ContentType},
};
}
public PageResult(SharpPage page) : this(page?.Format)
{
Page = page ?? throw new ArgumentNullException(nameof(page));
}
public PageResult(SharpCodePage page) : this(page?.Format)
{
CodePage = page ?? throw new ArgumentNullException(nameof(page));
var hasRequest = (CodePage as IRequiresRequest)?.Request;
if (hasRequest != null)
Args[ScriptConstants.Request] = hasRequest;
}
//entry point
public async Task WriteToAsync(Stream responseStream, CancellationToken token = default)
{
if (OutputTransformers.Count == 0)
{
var bufferOutput = !DisableBuffering && !(responseStream is MemoryStream);
if (bufferOutput)
{
using (var ms = MemoryStreamFactory.GetStream())
{
await WriteToAsyncInternal(ms, token);
ms.Position = 0;
await ms.WriteToAsync(responseStream, token);
}
}
else
{
await WriteToAsyncInternal(responseStream, token);
}
return;
}
//If PageResult has any OutputFilters Buffer and chain stream responses to each
using (var ms = MemoryStreamFactory.GetStream())
{
stackTrace.Push("OutputTransformer");
await WriteToAsyncInternal(ms, token);
Stream stream = ms;
foreach (var transformer in OutputTransformers)
{
stream.Position = 0;
stream = await transformer(stream);
}
using (stream)
{
stream.Position = 0;
await stream.WriteToAsync(responseStream, token);
}
stackTrace.Pop();
}
}
internal async Task WriteToAsyncInternal(Stream outputStream, CancellationToken token)
{
await Init();
if (!NoLayout)
{
if (LayoutPage != null)
{
await LayoutPage.Init();
if (CodePage != null)
InitIfNewPage(CodePage);
if (Page != null)
await InitIfNewPage(Page);
}
else
{
if (Page != null)
{
await InitIfNewPage(Page);
if (Page.LayoutPage != null)
{
LayoutPage = Page.LayoutPage;
await LayoutPage.Init();
}
}
else if (CodePage != null)
{
InitIfNewPage(CodePage);
if (CodePage.LayoutPage != null)
{
LayoutPage = CodePage.LayoutPage;
await LayoutPage.Init();
}
}
}
}
else
{
if (Page != null)
{
await InitIfNewPage(Page);
}
else if (CodePage != null)
{
InitIfNewPage(CodePage);
}
}
token.ThrowIfCancellationRequested();
var pageScope = CreatePageContext(null, outputStream);
if (!NoLayout && LayoutPage != null)
{
// sync impl with WriteFragmentsAsync
stackTrace.Push("Layout: " + LayoutPage.VirtualPath);
foreach (var fragment in LayoutPage.PageFragments)
{
if (HaltExecution)
break;
if (fragment is PageStringFragment str)
{
await outputStream.WriteAsync(str.ValueUtf8, token);
}
else if (fragment is PageVariableFragment var && !ShouldSkipFilterExecution(var))
{
if (var.Binding?.Equals(ScriptConstants.Page) == true)
{
await WritePageAsync(Page, CodePage, pageScope, token);
if (HaltExecution)
HaltExecution = false; //break out of page but continue evaluating layout
}
else
{
await WriteVarAsync(pageScope, var, token);
}
}
else if (fragment is PageBlockFragment blockFragment && !ShouldSkipFilterExecution(blockFragment))
{
var block = GetBlock(blockFragment.Name);
await block.WriteAsync(pageScope, blockFragment, token);
}
}
stackTrace.Pop();
}
else
{
await WritePageAsync(Page, CodePage, pageScope, token);
}
}
internal async Task WriteFragmentsAsync(ScriptScopeContext scope, IEnumerable<PageFragment> fragments, string callTrace, CancellationToken token)
{
stackTrace.Push(callTrace);
foreach (var fragment in fragments)
{
if (HaltExecution)
break;
if (fragment is PageStringFragment str)
{
await scope.OutputStream.WriteAsync(str.ValueUtf8, token);
}
else if (fragment is PageVariableFragment var && !ShouldSkipFilterExecution(var))
{
await WriteVarAsync(scope, var, token);
}
else if (fragment is PageBlockFragment blockFragment && !ShouldSkipFilterExecution(blockFragment))
{
var block = GetBlock(blockFragment.Name);
await block.WriteAsync(scope, blockFragment, token);
}
}
stackTrace.Pop();
}
public bool ShouldSkipFilterExecution(PageVariableFragment var)
{
return HaltExecution || SkipFilterExecution && (var.Binding != null
? !ScriptConfig.OnlyEvaluateFiltersWhenSkippingPageFilterExecution.Contains(var.Binding)
: var.InitialExpression?.Name == null ||
!ScriptConfig.OnlyEvaluateFiltersWhenSkippingPageFilterExecution.Contains(var.InitialExpression.Name));
}
public bool ShouldSkipFilterExecution(PageBlockFragment var)
{
return HaltExecution || SkipFilterExecution;
}
public ScriptContext Context => Page?.Context ?? CodePage.Context;
public PageFormat Format => Page?.Format ?? CodePage.Format;
public string VirtualPath => Page?.VirtualPath ?? CodePage.VirtualPath;
private bool hasInit;
public async Task<PageResult> Init()
{
if (hasInit)
return this;
if (!Context.HasInit)
throw new NotSupportedException($"{Context.GetType().Name} has not been initialized. Call 'Init()' to initialize Script Context.");
if (Model != null)
{
var explodeModel = Model.ToObjectDictionary();
foreach (var entry in explodeModel)
{
Args[entry.Key] = entry.Value ?? JsNull.Value;
}
}
Args[ScriptConstants.Model] = Model ?? JsNull.Value;
foreach (var filter in ScriptMethods)
{
Context.InitMethod(filter);
}
foreach (var block in ScriptBlocks)
{
Context.InitBlock(block);
blocksMap[block.Name] = block;
}
if (Page != null)
{
await Page.Init();
InitPageArgs(Page.Args);
}
else
{
CodePage.Init();
InitPageArgs(CodePage.Args);
}
if (Layout != null && !NoLayout)
{
LayoutPage = Page != null
? Context.Pages.ResolveLayoutPage(Page, Layout)
: Context.Pages.ResolveLayoutPage(CodePage, Layout);
}
hasInit = true;
return this;
}
private void InitPageArgs(Dictionary<string, object> pageArgs)
{
if (pageArgs?.Count > 0)
{
NoLayout = (pageArgs.TryGetValue("ignore", out object ignore) && "template".Equals(ignore?.ToString())) ||
(pageArgs.TryGetValue("layout", out object layout) && "none".Equals(layout?.ToString()));
}
}
private Task InitIfNewPage(SharpPage page) => page != Page
? (Task) page.Init()
: TypeConstants.EmptyTask;
private void InitIfNewPage(SharpCodePage page)
{
if (page != CodePage)
page.Init();
}
private void AssertInit()
{
if (!hasInit)
throw new NotSupportedException("PageResult.Init() required for this operation.");
}
public Task WritePageAsync(SharpPage page, SharpCodePage codePage,
ScriptScopeContext scope, CancellationToken token = default(CancellationToken))
{
if (page != null)
return WritePageAsync(page, scope, token);
return WriteCodePageAsync(codePage, scope, token);
}
public async Task WritePageAsync(SharpPage page, ScriptScopeContext scope, CancellationToken token = default(CancellationToken))
{
if (PageTransformers.Count == 0)
{
await WritePageAsyncInternal(page, scope, token);
return;
}
//If PageResult has any PageFilters Buffer and chain stream responses to each
using (var ms = MemoryStreamFactory.GetStream())
{
stackTrace.Push("PageTransformer");
await WritePageAsyncInternal(page, new ScriptScopeContext(this, ms, scope.ScopedParams), token);
Stream stream = ms;
foreach (var transformer in PageTransformers)
{
stream.Position = 0;
stream = await transformer(stream);
}
using (stream)
{
stream.Position = 0;
await stream.WriteToAsync(scope.OutputStream, token);
}
stackTrace.Pop();
}
}
internal async Task WritePageAsyncInternal(SharpPage page, ScriptScopeContext scope, CancellationToken token = default(CancellationToken))
{
await page.Init(); //reload modified changes if needed
await WriteFragmentsAsync(scope, page.PageFragments, "Page: " + page.VirtualPath, token);
}
public async Task WriteCodePageAsync(SharpCodePage page, ScriptScopeContext scope, CancellationToken token = default(CancellationToken))
{
if (PageTransformers.Count == 0)
{
await WriteCodePageAsyncInternal(page, scope, token);
return;
}
//If PageResult has any PageFilters Buffer and chain stream responses to each
using (var ms = MemoryStreamFactory.GetStream())
{
await WriteCodePageAsyncInternal(page, new ScriptScopeContext(this, ms, scope.ScopedParams), token);
Stream stream = ms;
foreach (var transformer in PageTransformers)
{
stream.Position = 0;
stream = await transformer(stream);
}
using (stream)
{
stream.Position = 0;
await stream.WriteToAsync(scope.OutputStream, token);
}
}
}
internal Task WriteCodePageAsyncInternal(SharpCodePage page, ScriptScopeContext scope, CancellationToken token = default(CancellationToken))
{
page.Scope = scope;
if (!page.HasInit)
page.Init();
return page.WriteAsync(scope);
}
private string toDebugString(object instance)
{
using (JsConfig.With(new Config
{
ExcludeTypeInfo = true,
IncludeTypeInfo = false,
}))
{
if (instance is Dictionary<string, object> d)
return d.ToJsv();
if (instance is List<object> l)
return l.ToJsv();
if (instance is string s)
return '"' + s.Replace("\"", "\\\"") + '"';
return instance.ToJsv();
}
}
private async Task WriteVarAsync(ScriptScopeContext scope, PageVariableFragment var, CancellationToken token)
{
if (var.Binding != null)
stackTrace.Push($"Expression (binding): " + var.Binding);
else if (var.InitialExpression?.Name != null)
stackTrace.Push("Expression (filter): " + var.InitialExpression.Name);
else if (var.InitialValue != null)
stackTrace.Push($"Expression ({var.InitialValue.GetType().Name}): " + toDebugString(var.InitialValue).SubstringWithEllipsis(0, 200));
else
stackTrace.Push($"{var.Expression.GetType().Name}: " + var.Expression.ToRawString().SubstringWithEllipsis(0, 200));
var value = await EvaluateAsync(var, scope, token);
if (value != IgnoreResult.Value)
{
if (value != null)
{
var bytes = Format.EncodeValue(value).ToUtf8Bytes();
await scope.OutputStream.WriteAsync(bytes, token);
}
else
{
if (Context.OnUnhandledExpression != null)
{
var bytes = Context.OnUnhandledExpression(var);
if (bytes.Length > 0)
await scope.OutputStream.WriteAsync(bytes, token);
}
}
}
stackTrace.Pop();
}
private Func<Stream, Task<Stream>> GetFilterTransformer(string name)
{
return FilterTransformers.TryGetValue(name, out Func<Stream, Task<Stream>> fn)
? fn
: Context.FilterTransformers.TryGetValue(name, out fn)
? fn
: null;
}
private static Dictionary<string, object> GetPageParams(PageVariableFragment var)
{
Dictionary<string, object> scopedParams = null;
if (var != null && var.FilterExpressions.Length > 0)
{
if (var.FilterExpressions[0].Arguments.Length > 0)
{
var token = var.FilterExpressions[0].Arguments[0];
scopedParams = token.Evaluate(JS.CreateScope()) as Dictionary<string, object>;
}
}
return scopedParams;
}
private ScriptScopeContext CreatePageContext(PageVariableFragment var, Stream outputStream) => new ScriptScopeContext(this, outputStream, GetPageParams(var));
private async Task<object> EvaluateAsync(PageVariableFragment var, ScriptScopeContext scope, CancellationToken token=default(CancellationToken))
{
scope.ScopedParams[nameof(PageVariableFragment)] = var;
var value = var.Evaluate(scope);
if (value == null)
{
var handlesUnknownValue = Context.OnUnhandledExpression == null &&
var.FilterExpressions.Length > 0;
if (!handlesUnknownValue)
{
if (var.Expression is JsMemberExpression memberExpr)
{
//allow nested null bindings from an existing target to evaluate to an empty string
var targetValue = memberExpr.Object.Evaluate(scope);
if (targetValue != null)
return string.Empty;
}
if (var.Binding == null)
return null;
var hasFilterAsBinding = GetFilterAsBinding(var.Binding, out ScriptMethods filter);
if (hasFilterAsBinding != null)
{
value = InvokeFilter(hasFilterAsBinding, filter, new object[0], var.Binding);
}
else
{
var hasContextFilterAsBinding = GetContextFilterAsBinding(var.Binding, out filter);
if (hasContextFilterAsBinding != null)
{
value = InvokeFilter(hasContextFilterAsBinding, filter, new object[] { scope }, var.Binding);
}
else
{
return null;
}
}
}
}
if (value == JsNull.Value)
value = null;
value = EvaluateIfToken(value, scope);
for (var i = 0; i < var.FilterExpressions.Length; i++)
{
if (HaltExecution || value == StopExecution.Value)
break;
var expr = var.FilterExpressions[i];
try
{
var filterName = expr.Name;
var fnArgValues = JsCallExpression.EvaluateArgumentValues(scope, expr.Arguments);
var fnArgsLength = fnArgValues.Count;
var invoker = GetFilterInvoker(filterName, 1 + fnArgsLength, out ScriptMethods filter);
var contextFilterInvoker = invoker == null
? GetContextFilterInvoker(filterName, 2 + fnArgsLength, out filter)
: null;
var contextBlockInvoker = invoker == null && contextFilterInvoker == null
? GetContextBlockInvoker(filterName, 2 + fnArgsLength, out filter)
: null;
if (invoker == null && contextFilterInvoker == null && contextBlockInvoker == null)
{
if (i == 0)
return null; // ignore on server (i.e. assume it's on client) if first filter is missing
var errorMsg = CreateMissingFilterErrorMessage(filterName);
throw new NotSupportedException(errorMsg);
}
if (value is Task<object> valueObjectTask)
value = await valueObjectTask;
if (invoker != null)
{
fnArgValues.Insert(0, value);
var args = fnArgValues.ToArray();
value = InvokeFilter(invoker, filter, args, expr.Name);
}
else if (contextFilterInvoker != null)
{
fnArgValues.Insert(0, scope);
fnArgValues.Insert(1, value); // filter target
var args = fnArgValues.ToArray();
value = InvokeFilter(contextFilterInvoker, filter, args, expr.Name);
}
else
{
var hasFilterTransformers = var.FilterExpressions.Length + i > 1;
var useScope = hasFilterTransformers
? scope.ScopeWithStream(MemoryStreamFactory.GetStream())
: scope;
fnArgValues.Insert(0, useScope);
fnArgValues.Insert(1, value); // filter target
var args = fnArgValues.ToArray();
try
{
var taskResponse = (Task)contextBlockInvoker(filter, args);
await taskResponse;
if (hasFilterTransformers)
{
using (useScope.OutputStream)
{
var stream = useScope.OutputStream;
//If Context Filter has any Filter Transformers Buffer and chain stream responses to each
for (var exprIndex = i + 1; exprIndex < var.FilterExpressions.Length; exprIndex++)
{
stream.Position = 0;
contextBlockInvoker = GetContextBlockInvoker(var.FilterExpressions[exprIndex].Name, 1 + var.FilterExpressions[exprIndex].Arguments.Length, out filter);
if (contextBlockInvoker != null)
{
args[0] = useScope;
for (var cmdIndex = 0; cmdIndex < var.FilterExpressions[exprIndex].Arguments.Length; cmdIndex++)
{
var arg = var.FilterExpressions[exprIndex].Arguments[cmdIndex];
var varValue = arg.Evaluate(scope);
args[1 + cmdIndex] = varValue;
}
await (Task)contextBlockInvoker(filter, args);
}
else
{
var transformer = GetFilterTransformer(var.FilterExpressions[exprIndex].Name);
if (transformer == null)
throw new NotSupportedException($"Could not find FilterTransformer '{var.FilterExpressions[exprIndex].Name}' in page '{Page.VirtualPath}'");
stream = await transformer(stream);
useScope = useScope.ScopeWithStream(stream);
}
}
if (stream.CanRead)
{
stream.Position = 0;
await stream.WriteToAsync(scope.OutputStream, token);
}
}
}
}
catch (StopFilterExecutionException) { throw; }
catch (Exception ex)
{
var rethrow = ScriptConfig.FatalExceptions.Contains(ex.GetType());
var exResult = Format.OnExpressionException(this, ex);
if (exResult != null)
await scope.OutputStream.WriteAsync(Format.EncodeValue(exResult).ToUtf8Bytes(), token);
else if (rethrow)
throw;
throw new TargetInvocationException($"Failed to invoke filter '{expr.GetDisplayName()}': {ex.Message}", ex);
}
return IgnoreResult.Value;
}
if (value is Task<object> valueTask)
value = await valueTask;
}
catch (StopFilterExecutionException ex)
{
LastFilterError = ex.InnerException;
LastFilterStackTrace = stackTrace.ToArray();
if (RethrowExceptions)
throw ex.InnerException;
var skipExecutingFilters = SkipExecutingFiltersIfError.GetValueOrDefault(Context.SkipExecutingFiltersIfError);
if (skipExecutingFilters)
this.SkipFilterExecution = true;
var rethrow = ScriptConfig.FatalExceptions.Contains(ex.InnerException.GetType());
if (!rethrow)
{
string errorBinding = null;
if (ex.Options is Dictionary<string, object> filterParams)
{
if (filterParams.TryGetValue(ScriptConstants.AssignError, out object assignError))
{
errorBinding = assignError as string;
}
else if (filterParams.TryGetValue(ScriptConstants.CatchError, out object catchError))
{
errorBinding = catchError as string;
SkipFilterExecution = false;
LastFilterError = null;
LastFilterStackTrace = null;
}
if (filterParams.TryGetValue(ScriptConstants.IfErrorReturn, out object ifErrorReturn))
{
SkipFilterExecution = false;
LastFilterError = null;
LastFilterStackTrace = null;
return ifErrorReturn;
}
}
if (errorBinding == null)
errorBinding = AssignExceptionsTo ?? Context.AssignExceptionsTo;
if (!string.IsNullOrEmpty(errorBinding))
{
scope.ScopedParams[errorBinding] = ex.InnerException;
scope.ScopedParams[errorBinding + "StackTrace"] = stackTrace.Map(x => " at " + x).Join(Environment.NewLine);
return string.Empty;
}
}
if (SkipExecutingFiltersIfError.HasValue || Context.SkipExecutingFiltersIfError)
return string.Empty;
// rethrow exceptions which aren't handled
var exResult = Format.OnExpressionException(this, ex);
if (exResult != null)
await scope.OutputStream.WriteAsync(Format.EncodeValue(exResult).ToUtf8Bytes(), token);
else if (rethrow)
throw ex.InnerException;
var filterName = expr.GetDisplayName();
if (filterName.StartsWith("throw"))
throw ex.InnerException;
throw new TargetInvocationException($"Failed to invoke filter '{filterName}': {ex.InnerException.Message}", ex.InnerException);
}
}
if (value == null || value == JsNull.Value || value == StopExecution.Value)
return string.Empty; // treat as empty value if evaluated to null
return value;
}
internal string CreateMissingFilterErrorMessage(string filterName)
{
var registeredMethods = ScriptMethods.Union(Context.ScriptMethods).ToList();
var similarNonMatchingFilters = registeredMethods
.SelectMany(x => x.QueryFilters(filterName))
.Where(x => !(Context.ExcludeFiltersNamed.Contains(x.Name) || ExcludeFiltersNamed.Contains(x.Name)))
.ToList();
var sb = StringBuilderCache.Allocate()
.AppendLine($"Filter in '{VirtualPath}' named '{filterName}' was not found.");
if (similarNonMatchingFilters.Count > 0)
{
sb.Append("Check for correct usage in similar (but non-matching) filters:").AppendLine();
var normalFilters = similarNonMatchingFilters
.OrderBy(x => x.GetParameters().Length + (x.ReturnType == typeof(Task) ? 10 : 1))
.ToArray();
foreach (var mi in normalFilters)
{
var argsTypesWithoutContext = mi.GetParameters()
.Where(x => x.ParameterType != typeof(ScriptScopeContext))
.ToList();
sb.Append("{{ ");
if (argsTypesWithoutContext.Count == 0)
{
sb.Append($"{mi.Name} => {mi.ReturnType.Name}");
}
else
{
sb.Append($"{argsTypesWithoutContext[0].ParameterType.Name} | {mi.Name}(");
var piCount = 0;
foreach (var pi in argsTypesWithoutContext.Skip(1))
{
if (piCount++ > 0)
sb.Append(", ");
sb.Append(pi.ParameterType.Name);
}
var returnType = mi.ReturnType == typeof(Task)
? "(Stream)"
: mi.ReturnType.Name;
sb.Append($") => {returnType}");
}
sb.AppendLine(" }}");
}
}
else
{
var registeredFilterNames = registeredMethods.Map(x => $"'{x.GetType().Name}'").Join(", ");
sb.Append($"No similar filters named '{filterName}' were found in registered filter(s): {registeredFilterNames}.");
}
return StringBuilderCache.ReturnAndFree(sb);
}
// Filters with no args can be used in-place of bindings
private MethodInvoker GetFilterAsBinding(string name, out ScriptMethods filter) => GetFilterInvoker(name, 0, out filter);
private MethodInvoker GetContextFilterAsBinding(string name, out ScriptMethods filter) => GetContextFilterInvoker(name, 1, out filter);
internal object InvokeFilter(MethodInvoker invoker, ScriptMethods filter, object[] args, string binding)
{
if (invoker == null)
throw new NotSupportedException(CreateMissingFilterErrorMessage(binding.LeftPart('(')));
try
{
return invoker(filter, args);
}
catch (StopFilterExecutionException) { throw; }
catch (Exception ex)
{
var exResult = Format.OnExpressionException(this, ex);
if (exResult != null)
return exResult;
if (binding.StartsWith("throw"))
throw;
throw new TargetInvocationException($"Failed to invoke filter '{binding}': {ex.Message}", ex);
}
}
public ReadOnlySpan<char> ParseJsExpression(ScriptScopeContext scope, ReadOnlySpan<char> literal, out JsToken token)
{
try
{
return literal.ParseJsExpression(out token);
}
catch (ArgumentException e)
{
if (scope.ScopedParams.TryGetValue(nameof(PageVariableFragment), out var oVar)
&& oVar is PageVariableFragment var && !var.OriginalText.IsNullOrEmpty())
{
throw new Exception($"Invalid literal: {literal.ToString()} in '{var.OriginalText}'", e);
}
throw;
}
}
private readonly Dictionary<string, ScriptBlock> blocksMap = new Dictionary<string, ScriptBlock>();
public ScriptBlock TryGetBlock(string name) => blocksMap.TryGetValue(name, out var block) ? block : Context.GetBlock(name);
public ScriptBlock GetBlock(string name)
{
var block = TryGetBlock(name);
if (block == null)
throw new NotSupportedException($"Block in '{VirtualPath}' named '{name}' was not found.");
return block;
}
public ScriptScopeContext CreateScope(Stream outputStream=null) =>
new ScriptScopeContext(this, outputStream ?? MemoryStreamFactory.GetStream(), null);
internal MethodInvoker GetFilterInvoker(string name, int argsCount, out ScriptMethods filter) => GetInvoker(name, argsCount, InvokerType.Filter, out filter);
internal MethodInvoker GetContextFilterInvoker(string name, int argsCount, out ScriptMethods filter) => GetInvoker(name, argsCount, InvokerType.ContextFilter, out filter);
internal MethodInvoker GetContextBlockInvoker(string name, int argsCount, out ScriptMethods filter) => GetInvoker(name, argsCount, InvokerType.ContextBlock, out filter);
private MethodInvoker GetInvoker(string name, int argsCount, InvokerType invokerType, out ScriptMethods filter)
{
if (!Context.ExcludeFiltersNamed.Contains(name) && !ExcludeFiltersNamed.Contains(name))
{
foreach (var tplFilter in ScriptMethods)
{
var invoker = tplFilter?.GetInvoker(name, argsCount, invokerType);
if (invoker != null)
{
filter = tplFilter;
return invoker;
}
}
foreach (var tplFilter in Context.ScriptMethods)