-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathHttpCachePolicy.cs
1796 lines (1517 loc) · 72 KB
/
HttpCachePolicy.cs
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
//------------------------------------------------------------------------------
// <copyright file="HttpCachePolicy.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
/*
* Cache Policy class
*
* Copyright (c) 1998 Microsoft Corporation
*/
namespace System.Web {
using System;
using System.Collections;
using System.Globalization;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using System.Web.Caching;
using System.Web.Compilation;
using System.Web.Configuration;
using System.Web.Management;
using System.Web.Security.Cryptography;
using System.Web.Util;
using Debug = System.Web.Util.Debug;
//
// Public constants for cache-control
//
/// <devdoc>
/// <para>
/// Provides enumeration values for all cache-control header settings.
/// </para>
/// </devdoc>
public enum HttpCacheability {
/// <devdoc>
/// <para>
/// Indicates that
/// without a field name, a cache must force successful revalidation with the
/// origin server before satisfying the request. With a field name, the cache may
/// use the response to satisfy a subsequent request.
/// </para>
/// </devdoc>
NoCache = 1,
/// <devdoc>
/// <para>
/// Default value. Specifies that the response is cachable only on the client,
/// not by shared caches.
/// </para>
/// </devdoc>
Private,
/// <devdoc>
/// <para>
/// Specifies that the response should only be cached at the server.
/// Clients receive headers equivalent to a NoCache directive.
/// </para>
/// </devdoc>
Server,
ServerAndNoCache = Server,
/// <devdoc>
/// <para>
/// Specifies that the response is cachable by clients and shared caches.
/// </para>
/// </devdoc>
Public,
ServerAndPrivate,
}
enum HttpCacheabilityLimits {
MinValue = HttpCacheability.NoCache,
MaxValue = HttpCacheability.ServerAndPrivate,
None = MaxValue + 1,
}
/// <devdoc>
/// <para>
/// This class is a light abstraction over the Cache-Control: revalidation
/// directives.
/// </para>
/// </devdoc>
public enum HttpCacheRevalidation {
/// <devdoc>
/// <para>
/// Indicates that Cache-Control: must-revalidate should be sent.
/// </para>
/// </devdoc>
AllCaches = 1,
/// <devdoc>
/// <para>
/// Indicates that Cache-Control: proxy-revalidate should be sent.
/// </para>
/// </devdoc>
ProxyCaches = 2,
/// <devdoc>
/// <para>
/// Default value. Indicates that no property has been set. If this is set, no
/// cache revalitation directive is sent.
/// </para>
/// </devdoc>
None = 3,
}
enum HttpCacheRevalidationLimits {
MinValue = HttpCacheRevalidation.AllCaches,
MaxValue = HttpCacheRevalidation.None
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public enum HttpValidationStatus {
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
Invalid = 1,
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
IgnoreThisRequest = 2,
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
Valid = 3
}
/// <devdoc>
/// <para>Called back when the handler wants validation on a cache
/// item before it's served from the cache. If any handler invalidates
/// the item, the item is evicted from the cache and the request is handled as
/// if a cache miss were generated.</para>
/// </devdoc>
public delegate void HttpCacheValidateHandler(
HttpContext context, Object data, ref HttpValidationStatus validationStatus);
sealed class ValidationCallbackInfo {
internal readonly HttpCacheValidateHandler handler;
internal readonly Object data;
internal ValidationCallbackInfo(HttpCacheValidateHandler handler, Object data) {
this.handler = handler;
this.data = data;
}
}
[Serializable]
sealed class HttpCachePolicySettings {
/* internal access */
internal readonly bool _isModified;
[NonSerialized]
internal ValidationCallbackInfo[] _validationCallbackInfo;
private string[] _validationCallbackInfoForSerialization;
internal readonly HttpResponseHeader _headerCacheControl;
internal readonly HttpResponseHeader _headerPragma;
internal readonly HttpResponseHeader _headerExpires;
internal readonly HttpResponseHeader _headerLastModified;
internal readonly HttpResponseHeader _headerEtag;
internal readonly HttpResponseHeader _headerVaryBy;
/* internal access */
internal readonly bool _hasSetCookieHeader;
internal readonly bool _noServerCaching;
internal readonly String _cacheExtension;
internal readonly bool _noTransforms;
internal readonly bool _ignoreRangeRequests;
internal readonly String[] _varyByContentEncodings;
internal readonly String[] _varyByHeaderValues;
internal readonly String[] _varyByParamValues;
internal readonly string _varyByCustom;
internal readonly HttpCacheability _cacheability;
internal readonly bool _noStore;
internal readonly String[] _privateFields;
internal readonly String[] _noCacheFields;
internal readonly DateTime _utcExpires;
internal readonly bool _isExpiresSet;
internal readonly TimeSpan _maxAge;
internal readonly bool _isMaxAgeSet;
internal readonly TimeSpan _proxyMaxAge;
internal readonly bool _isProxyMaxAgeSet;
internal readonly int _slidingExpiration;
internal readonly TimeSpan _slidingDelta;
internal readonly DateTime _utcTimestampCreated;
internal readonly int _validUntilExpires;
internal readonly int _allowInHistory;
internal readonly HttpCacheRevalidation _revalidation;
internal readonly DateTime _utcLastModified;
internal readonly bool _isLastModifiedSet;
internal readonly String _etag;
internal readonly bool _generateLastModifiedFromFiles;
internal readonly bool _generateEtagFromFiles;
internal readonly int _omitVaryStar;
internal readonly bool _hasUserProvidedDependencies;
internal HttpCachePolicySettings(
bool isModified,
ValidationCallbackInfo[] validationCallbackInfo,
bool hasSetCookieHeader,
bool noServerCaching,
String cacheExtension,
bool noTransforms,
bool ignoreRangeRequests,
String[] varyByContentEncodings,
String[] varyByHeaderValues,
String[] varyByParamValues,
string varyByCustom,
HttpCacheability cacheability,
bool noStore,
String[] privateFields,
String[] noCacheFields,
DateTime utcExpires,
bool isExpiresSet,
TimeSpan maxAge,
bool isMaxAgeSet,
TimeSpan proxyMaxAge,
bool isProxyMaxAgeSet,
int slidingExpiration,
TimeSpan slidingDelta,
DateTime utcTimestampCreated,
int validUntilExpires,
int allowInHistory,
HttpCacheRevalidation revalidation,
DateTime utcLastModified,
bool isLastModifiedSet,
String etag,
bool generateLastModifiedFromFiles,
bool generateEtagFromFiles,
int omitVaryStar,
HttpResponseHeader headerCacheControl,
HttpResponseHeader headerPragma,
HttpResponseHeader headerExpires,
HttpResponseHeader headerLastModified,
HttpResponseHeader headerEtag,
HttpResponseHeader headerVaryBy,
bool hasUserProvidedDependencies) {
_isModified = isModified ;
_validationCallbackInfo = validationCallbackInfo ;
_hasSetCookieHeader = hasSetCookieHeader ;
_noServerCaching = noServerCaching ;
_cacheExtension = cacheExtension ;
_noTransforms = noTransforms ;
_ignoreRangeRequests = ignoreRangeRequests ;
_varyByContentEncodings = varyByContentEncodings ;
_varyByHeaderValues = varyByHeaderValues ;
_varyByParamValues = varyByParamValues ;
_varyByCustom = varyByCustom ;
_cacheability = cacheability ;
_noStore = noStore ;
_privateFields = privateFields ;
_noCacheFields = noCacheFields ;
_utcExpires = utcExpires ;
_isExpiresSet = isExpiresSet ;
_maxAge = maxAge ;
_isMaxAgeSet = isMaxAgeSet ;
_proxyMaxAge = proxyMaxAge ;
_isProxyMaxAgeSet = isProxyMaxAgeSet ;
_slidingExpiration = slidingExpiration ;
_slidingDelta = slidingDelta ;
_utcTimestampCreated = utcTimestampCreated ;
_validUntilExpires = validUntilExpires ;
_allowInHistory = allowInHistory ;
_revalidation = revalidation ;
_utcLastModified = utcLastModified ;
_isLastModifiedSet = isLastModifiedSet ;
_etag = etag ;
_generateLastModifiedFromFiles = generateLastModifiedFromFiles ;
_generateEtagFromFiles = generateEtagFromFiles ;
_omitVaryStar = omitVaryStar ;
_headerCacheControl = headerCacheControl ;
_headerPragma = headerPragma ;
_headerExpires = headerExpires ;
_headerLastModified = headerLastModified ;
_headerEtag = headerEtag ;
_headerVaryBy = headerVaryBy ;
_hasUserProvidedDependencies = hasUserProvidedDependencies ;
}
[OnSerializing()]
private void OnSerializingMethod(StreamingContext context) {
if (_validationCallbackInfo == null)
return;
// create a string representation of each callback
// note that ValidationCallbackInfo.data is assumed to be null
String[] callbackInfos = new String[_validationCallbackInfo.Length * 2];
for (int i = 0; i < _validationCallbackInfo.Length; i++) {
Debug.Assert(_validationCallbackInfo[i].data == null, "_validationCallbackInfo[i].data == null");
HttpCacheValidateHandler handler = _validationCallbackInfo[i].handler;
string targetTypeName = System.Web.UI.Util.GetAssemblyQualifiedTypeName(handler.Method.ReflectedType);
string methodName = handler.Method.Name;
callbackInfos[2 * i] = targetTypeName;
callbackInfos[2 * i + 1] = methodName;
}
_validationCallbackInfoForSerialization = callbackInfos;
}
[OnDeserialized()]
private void OnDeserializedMethod(StreamingContext context) {
if (_validationCallbackInfoForSerialization == null)
return;
// re-create each ValidationCallbackInfo from its string representation
ValidationCallbackInfo[] callbackInfos = new ValidationCallbackInfo[_validationCallbackInfoForSerialization.Length / 2];
for (int i = 0; i < _validationCallbackInfoForSerialization.Length; i += 2) {
string targetTypeName = _validationCallbackInfoForSerialization[i];
string methodName = _validationCallbackInfoForSerialization[i+1];
Type target = null;
if (!String.IsNullOrEmpty(targetTypeName)) {
target = BuildManager.GetType(targetTypeName, true /*throwOnFail*/, false /*ignoreCase*/);
}
if (target == null) {
throw new SerializationException(SR.GetString(SR.Type_cannot_be_resolved, targetTypeName));
}
HttpCacheValidateHandler handler = (HttpCacheValidateHandler) Delegate.CreateDelegate(typeof(HttpCacheValidateHandler), target, methodName);
callbackInfos[i / 2] = new ValidationCallbackInfo(handler, null);
}
_validationCallbackInfo = callbackInfos;
}
internal bool IsModified {get {return _isModified ;}}
internal ValidationCallbackInfo[] ValidationCallbackInfo {get {return _validationCallbackInfo ;}}
internal HttpResponseHeader HeaderCacheControl {get {return _headerCacheControl ;}}
internal HttpResponseHeader HeaderPragma {get {return _headerPragma ;}}
internal HttpResponseHeader HeaderExpires {get {return _headerExpires ;}}
internal HttpResponseHeader HeaderLastModified {get {return _headerLastModified ;}}
internal HttpResponseHeader HeaderEtag {get {return _headerEtag ;}}
internal HttpResponseHeader HeaderVaryBy {get {return _headerVaryBy ;}}
internal bool hasSetCookieHeader {get {return _hasSetCookieHeader ;}}
internal bool NoServerCaching {get {return _noServerCaching ;}}
internal String CacheExtension {get {return _cacheExtension ;}}
internal bool NoTransforms {get {return _noTransforms ;}}
internal bool IgnoreRangeRequests {get {return _ignoreRangeRequests ;}}
internal String[] VaryByContentEncodings {get {
return (_varyByContentEncodings == null) ? null : (string[]) _varyByContentEncodings.Clone() ;}}
internal String[] VaryByHeaders {get {
return (_varyByHeaderValues == null) ? null : (string[]) _varyByHeaderValues.Clone() ;}}
internal String[] VaryByParams {get {
return (_varyByParamValues == null) ? null : (string[]) _varyByParamValues.Clone() ;}}
internal bool IgnoreParams {get {
return _varyByParamValues != null && _varyByParamValues[0].Length == 0;}}
internal HttpCacheability CacheabilityInternal {get { return _cacheability;}}
internal bool NoStore {get {return _noStore ;}}
internal String[] PrivateFields {get {
return (_privateFields == null) ? null : (string[]) _privateFields.Clone() ;}}
internal String[] NoCacheFields {get {
return (_noCacheFields == null) ? null : (string[]) _noCacheFields.Clone() ;}}
internal DateTime UtcExpires {get {return _utcExpires ;}}
internal bool IsExpiresSet {get {return _isExpiresSet ;}}
internal TimeSpan MaxAge {get {return _maxAge ;}}
internal bool IsMaxAgeSet {get {return _isMaxAgeSet ;}}
internal TimeSpan ProxyMaxAge {get {return _proxyMaxAge ;}}
internal bool IsProxyMaxAgeSet {get {return _isProxyMaxAgeSet ;}}
internal int SlidingExpirationInternal {get {return _slidingExpiration ;}}
internal bool SlidingExpiration {get {return _slidingExpiration == 1 ;}}
internal TimeSpan SlidingDelta {get {return _slidingDelta ;}}
internal DateTime UtcTimestampCreated {get {return _utcTimestampCreated ;}}
internal int ValidUntilExpiresInternal {get {return _validUntilExpires ;}}
internal bool ValidUntilExpires {get {
return _validUntilExpires == 1
&& !SlidingExpiration
&& !GenerateLastModifiedFromFiles
&& !GenerateEtagFromFiles
&& ValidationCallbackInfo == null;}}
internal int AllowInHistoryInternal {get {return _allowInHistory ;}}
internal HttpCacheRevalidation Revalidation {get {return _revalidation ;}}
internal DateTime UtcLastModified {get {return _utcLastModified ;}}
internal bool IsLastModifiedSet {get {return _isLastModifiedSet ;}}
internal String ETag {get {return _etag ;}}
internal bool GenerateLastModifiedFromFiles {get {return _generateLastModifiedFromFiles;}}
internal bool GenerateEtagFromFiles {get {return _generateEtagFromFiles ;}}
internal string VaryByCustom {get {return _varyByCustom ;}}
internal bool HasUserProvidedDependencies {get {return _hasUserProvidedDependencies; }}
internal bool IsValidationCallbackSerializable() {
if (_validationCallbackInfo != null) {
foreach(ValidationCallbackInfo info in _validationCallbackInfo) {
if (info.data != null
|| !info.handler.Method.IsStatic) {
return false;
}
}
}
return true;
}
internal bool HasValidationPolicy() {
return ValidUntilExpires
|| GenerateLastModifiedFromFiles
|| GenerateEtagFromFiles
|| ValidationCallbackInfo != null;
}
internal int OmitVaryStarInternal {get {return _omitVaryStar;}}
}
/// <devdoc>
/// <para>Contains methods for controlling the ASP.NET output cache.</para>
/// </devdoc>
public sealed class HttpCachePolicy {
static TimeSpan s_oneYear = new TimeSpan(TimeSpan.TicksPerDay * 365);
static HttpResponseHeader s_headerPragmaNoCache;
static HttpResponseHeader s_headerExpiresMinus1;
bool _isModified;
bool _hasSetCookieHeader;
bool _noServerCaching;
String _cacheExtension;
bool _noTransforms;
bool _ignoreRangeRequests;
HttpCacheVaryByContentEncodings _varyByContentEncodings;
HttpCacheVaryByHeaders _varyByHeaders;
HttpCacheVaryByParams _varyByParams;
string _varyByCustom;
HttpCacheability _cacheability;
bool _noStore;
HttpDictionary _privateFields;
HttpDictionary _noCacheFields;
DateTime _utcExpires;
bool _isExpiresSet;
TimeSpan _maxAge;
bool _isMaxAgeSet;
TimeSpan _proxyMaxAge;
bool _isProxyMaxAgeSet;
int _slidingExpiration;
DateTime _utcTimestampCreated;
TimeSpan _slidingDelta;
DateTime _utcTimestampRequest;
int _validUntilExpires;
int _allowInHistory;
HttpCacheRevalidation _revalidation;
DateTime _utcLastModified;
bool _isLastModifiedSet;
String _etag;
bool _generateLastModifiedFromFiles;
bool _generateEtagFromFiles;
int _omitVaryStar;
ArrayList _validationCallbackInfo;
bool _useCachedHeaders;
HttpResponseHeader _headerCacheControl;
HttpResponseHeader _headerPragma;
HttpResponseHeader _headerExpires;
HttpResponseHeader _headerLastModified;
HttpResponseHeader _headerEtag;
HttpResponseHeader _headerVaryBy;
bool _noMaxAgeInCacheControl;
bool _hasUserProvidedDependencies;
internal HttpCachePolicy() {
_varyByContentEncodings = new HttpCacheVaryByContentEncodings();
_varyByHeaders = new HttpCacheVaryByHeaders();
_varyByParams = new HttpCacheVaryByParams();
Reset();
}
/*
* Restore original values
*/
internal void Reset() {
_varyByContentEncodings.Reset();
_varyByHeaders.Reset();
_varyByParams.Reset();
_isModified = false;
_hasSetCookieHeader = false;
_noServerCaching = false;
_cacheExtension = null;
_noTransforms = false;
_ignoreRangeRequests = false;
_varyByCustom = null;
_cacheability = (HttpCacheability) (int) HttpCacheabilityLimits.None;
_noStore = false;
_privateFields = null;
_noCacheFields = null;
_utcExpires = DateTime.MinValue;
_isExpiresSet = false;
_maxAge = TimeSpan.Zero;
_isMaxAgeSet = false;
_proxyMaxAge = TimeSpan.Zero;
_isProxyMaxAgeSet = false;
_slidingExpiration = -1;
_slidingDelta = TimeSpan.Zero;
_utcTimestampCreated = DateTime.MinValue;
_utcTimestampRequest = DateTime.MinValue;
_validUntilExpires = -1;
_allowInHistory = -1;
_revalidation = HttpCacheRevalidation.None;
_utcLastModified = DateTime.MinValue;
_isLastModifiedSet = false;
_etag = null;
_generateLastModifiedFromFiles = false;
_generateEtagFromFiles = false;
_validationCallbackInfo = null;
_useCachedHeaders = false;
_headerCacheControl = null;
_headerPragma = null;
_headerExpires = null;
_headerLastModified = null;
_headerEtag = null;
_headerVaryBy = null;
_noMaxAgeInCacheControl = false;
_hasUserProvidedDependencies = false;
_omitVaryStar = -1;
}
/*
* Reset based on a cached response. Includes data needed to generate
* header for a cached response.
*/
internal void ResetFromHttpCachePolicySettings(
HttpCachePolicySettings settings,
DateTime utcTimestampRequest) {
int i, n;
string[] fields;
_utcTimestampRequest = utcTimestampRequest;
_varyByContentEncodings.SetContentEncodings(settings.VaryByContentEncodings);
_varyByHeaders.SetHeaders(settings.VaryByHeaders);
_varyByParams.SetParams(settings.VaryByParams);
_isModified = settings.IsModified;
_hasSetCookieHeader = settings.hasSetCookieHeader;
_noServerCaching = settings.NoServerCaching;
_cacheExtension = settings.CacheExtension;
_noTransforms = settings.NoTransforms;
_ignoreRangeRequests = settings.IgnoreRangeRequests;
_varyByCustom = settings.VaryByCustom;
_cacheability = settings.CacheabilityInternal;
_noStore = settings.NoStore;
_utcExpires = settings.UtcExpires;
_isExpiresSet = settings.IsExpiresSet;
_maxAge = settings.MaxAge;
_isMaxAgeSet = settings.IsMaxAgeSet;
_proxyMaxAge = settings.ProxyMaxAge;
_isProxyMaxAgeSet = settings.IsProxyMaxAgeSet;
_slidingExpiration = settings.SlidingExpirationInternal;
_slidingDelta = settings.SlidingDelta;
_utcTimestampCreated = settings.UtcTimestampCreated;
_validUntilExpires = settings.ValidUntilExpiresInternal;
_allowInHistory = settings.AllowInHistoryInternal;
_revalidation = settings.Revalidation;
_utcLastModified = settings.UtcLastModified;
_isLastModifiedSet = settings.IsLastModifiedSet;
_etag = settings.ETag;
_generateLastModifiedFromFiles = settings.GenerateLastModifiedFromFiles;
_generateEtagFromFiles = settings.GenerateEtagFromFiles;
_omitVaryStar = settings.OmitVaryStarInternal;
_hasUserProvidedDependencies = settings.HasUserProvidedDependencies;
_useCachedHeaders = true;
_headerCacheControl = settings.HeaderCacheControl;
_headerPragma = settings.HeaderPragma;
_headerExpires = settings.HeaderExpires;
_headerLastModified = settings.HeaderLastModified;
_headerEtag = settings.HeaderEtag;
_headerVaryBy = settings.HeaderVaryBy;
_noMaxAgeInCacheControl = false;
fields = settings.PrivateFields;
if (fields != null) {
_privateFields = new HttpDictionary();
for (i = 0, n = fields.Length; i < n; i++) {
_privateFields.SetValue(fields[i], fields[i]);
}
}
fields = settings.NoCacheFields;
if (fields != null) {
_noCacheFields = new HttpDictionary();
for (i = 0, n = fields.Length; i < n; i++) {
_noCacheFields.SetValue(fields[i], fields[i]);
}
}
if (settings.ValidationCallbackInfo != null) {
_validationCallbackInfo = new ArrayList();
for (i = 0, n = settings.ValidationCallbackInfo.Length; i < n; i++) {
_validationCallbackInfo.Add(new ValidationCallbackInfo(
settings.ValidationCallbackInfo[i].handler,
settings.ValidationCallbackInfo[i].data));
}
}
}
/// <summary>
/// Return true if the CachePolicy has been modified
/// </summary>
/// <returns></returns>
public bool IsModified() {
return _isModified || _varyByContentEncodings.IsModified() || _varyByHeaders.IsModified() || _varyByParams.IsModified();
}
void Dirtied() {
_isModified = true;
_useCachedHeaders = false;
}
static internal void AppendValueToHeader(StringBuilder s, String value) {
if (!String.IsNullOrEmpty(value)) {
if (s.Length > 0) {
s.Append(", ");
}
s.Append(value);
}
}
static readonly string[] s_cacheabilityTokens = new String[]
{
null, // no enum
"no-cache", // HttpCacheability.NoCache
"private", // HttpCacheability.Private
"no-cache", // HttpCacheability.ServerAndNoCache
"public", // HttpCacheability.Public
"private", // HttpCacheability.ServerAndPrivate
null // None - not specified
};
static readonly string[] s_revalidationTokens = new String[]
{
null, // no enum
"must-revalidate", // HttpCacheRevalidation.AllCaches
"proxy-revalidate", // HttpCacheRevalidation.ProxyCaches
null // HttpCacheRevalidation.None
};
static readonly int[] s_cacheabilityValues = new int[]
{
-1, // no enum
0, // HttpCacheability.NoCache
2, // HttpCacheability.Private
1, // HttpCacheability.ServerAndNoCache
4, // HttpCacheability.Public
3, // HttpCacheability.ServerAndPrivate
100, // None - though private by default, an explicit set will override
};
DateTime UpdateLastModifiedTimeFromDependency(CacheDependency dep) {
DateTime utcFileLastModifiedMax = dep.UtcLastModified;
if (utcFileLastModifiedMax < _utcLastModified) {
utcFileLastModifiedMax = _utcLastModified;
}
// account for difference between file system time
// and DateTime.Now. On some machines it appears that
// the last modified time is further in the future
// that DateTime.Now
DateTime utcNow = DateTime.UtcNow;
if (utcFileLastModifiedMax > utcNow) {
utcFileLastModifiedMax = utcNow;
}
return utcFileLastModifiedMax;
}
/*
* Calculate LastModified and ETag
*
* The LastModified date is the latest last-modified date of
* every file that is added as a dependency.
*
* The ETag is generated by concatentating the appdomain id,
* filenames and last modified dates of all files into a single string,
* then hashing it and Base 64 encoding the hash.
*/
void UpdateFromDependencies(HttpResponse response) {
CacheDependency dep = null;
// if _etag != null && _generateEtagFromFiles == true, then this HttpCachePolicy
// was created from HttpCachePolicySettings and we don't need to update _etag.
if (_etag == null && _generateEtagFromFiles) {
dep = response.CreateCacheDependencyForResponse();
if (dep == null) {
return;
}
string id = dep.GetUniqueID();
if (id == null) {
throw new HttpException(SR.GetString(SR.No_UniqueId_Cache_Dependency));
}
DateTime utcFileLastModifiedMax = UpdateLastModifiedTimeFromDependency(dep);
StringBuilder sb = new StringBuilder(256);
sb.Append(HttpRuntime.AppDomainIdInternal);
sb.Append(id);
sb.Append("+LM");
sb.Append(utcFileLastModifiedMax.Ticks.ToString(CultureInfo.InvariantCulture));
_etag = Convert.ToBase64String(CryptoUtil.ComputeSHA256Hash(Encoding.UTF8.GetBytes(sb.ToString())));
//WOS 1540412: if we generate the etag based on file dependencies, encapsulate it within quotes.
_etag = "\"" + _etag + "\"";
}
if (_generateLastModifiedFromFiles) {
if (dep == null) {
dep = response.CreateCacheDependencyForResponse();
if (dep == null) {
return;
}
}
DateTime utcFileLastModifiedMax = UpdateLastModifiedTimeFromDependency(dep);
UtcSetLastModified(utcFileLastModifiedMax);
}
}
void UpdateCachedHeaders(HttpResponse response) {
StringBuilder sb;
HttpCacheability cacheability;
int i, n;
String expirationDate;
String lastModifiedDate;
String varyByHeaders;
bool omitVaryStar;
if (_useCachedHeaders) {
return;
}
//To enable Out of Band OutputCache Module support, we will always refresh the UtcTimestampRequest.
if (_utcTimestampCreated == DateTime.MinValue) {
_utcTimestampCreated = response.Context.UtcTimestamp;
}
_utcTimestampRequest = response.Context.UtcTimestamp;
if (_slidingExpiration != 1) {
_slidingDelta = TimeSpan.Zero;
}
else if (_isMaxAgeSet) {
_slidingDelta = _maxAge;
}
else if (_isExpiresSet) {
_slidingDelta = _utcExpires - _utcTimestampCreated;
}
else {
_slidingDelta = TimeSpan.Zero;
}
_headerCacheControl = null;
_headerPragma = null;
_headerExpires = null;
_headerLastModified = null;
_headerEtag = null;
_headerVaryBy = null;
UpdateFromDependencies(response);
/*
* Cache control header
*/
sb = new StringBuilder();
if (_cacheability == (HttpCacheability) (int) HttpCacheabilityLimits.None) {
cacheability = HttpCacheability.Private;
}
else {
cacheability = _cacheability;
}
AppendValueToHeader(sb, s_cacheabilityTokens[(int) cacheability]);
if (cacheability == HttpCacheability.Public && _privateFields != null) {
Debug.Assert(_privateFields.Size > 0);
AppendValueToHeader(sb, "private=\"");
sb.Append(_privateFields.GetKey(0));
for (i = 1, n = _privateFields.Size; i < n; i++) {
AppendValueToHeader(sb, _privateFields.GetKey(i));
}
sb.Append('\"');
}
if ( cacheability != HttpCacheability.NoCache &&
cacheability != HttpCacheability.ServerAndNoCache &&
_noCacheFields != null) {
Debug.Assert(_noCacheFields.Size > 0);
AppendValueToHeader(sb, "no-cache=\"");
sb.Append(_noCacheFields.GetKey(0));
for (i = 1, n = _noCacheFields.Size; i < n; i++) {
AppendValueToHeader(sb, _noCacheFields.GetKey(i));
}
sb.Append('\"');
}
if (_noStore) {
AppendValueToHeader(sb, "no-store");
}
AppendValueToHeader(sb, s_revalidationTokens[(int)_revalidation]);
if (_noTransforms) {
AppendValueToHeader(sb, "no-transform");
}
if (_cacheExtension != null) {
AppendValueToHeader(sb, _cacheExtension);
}
/*
* don't send expiration information when item shouldn't be cached
* for cached header, only add max-age when it doesn't change
* based on the time requested
*/
if ( _slidingExpiration == 1
&& cacheability != HttpCacheability.NoCache
&& cacheability != HttpCacheability.ServerAndNoCache) {
if (_isMaxAgeSet && !_noMaxAgeInCacheControl) {
AppendValueToHeader(sb, "max-age=" + ((long)_maxAge.TotalSeconds).ToString(CultureInfo.InvariantCulture));
}
if (_isProxyMaxAgeSet && !_noMaxAgeInCacheControl) {
AppendValueToHeader(sb, "s-maxage=" + ((long)(_proxyMaxAge).TotalSeconds).ToString(CultureInfo.InvariantCulture));
}
}
if (sb.Length > 0) {
_headerCacheControl = new HttpResponseHeader(HttpWorkerRequest.HeaderCacheControl, sb.ToString());
}
/*
* Pragma: no-cache and Expires: -1
*/
if (cacheability == HttpCacheability.NoCache || cacheability == HttpCacheability.ServerAndNoCache) {
if (s_headerPragmaNoCache == null) {
s_headerPragmaNoCache = new HttpResponseHeader(HttpWorkerRequest.HeaderPragma, "no-cache");
}
_headerPragma = s_headerPragmaNoCache;
if (_allowInHistory != 1) {
if (s_headerExpiresMinus1 == null) {
s_headerExpiresMinus1 = new HttpResponseHeader(HttpWorkerRequest.HeaderExpires, "-1");
}
_headerExpires = s_headerExpiresMinus1;
}
}
else {
/*
* Expires header.
*/
if (_isExpiresSet && _slidingExpiration != 1) {
expirationDate = HttpUtility.FormatHttpDateTimeUtc(_utcExpires);
_headerExpires = new HttpResponseHeader(HttpWorkerRequest.HeaderExpires, expirationDate);
}
/*
* Last Modified header.
*/
if (_isLastModifiedSet) {
lastModifiedDate = HttpUtility.FormatHttpDateTimeUtc(_utcLastModified);
_headerLastModified = new HttpResponseHeader(HttpWorkerRequest.HeaderLastModified, lastModifiedDate);
}
if (cacheability != HttpCacheability.Private) {
/*
* Etag.
*/
if (_etag != null) {
_headerEtag = new HttpResponseHeader(HttpWorkerRequest.HeaderEtag, _etag);
}
/*
* Vary
*/
varyByHeaders = null;
// automatic VaryStar processing
// See if anyone has explicitly set this value
if (_omitVaryStar != -1) {
omitVaryStar = _omitVaryStar == 1 ? true : false;
}
else {
// If no one has set this value, go with the default from config
RuntimeConfig config = RuntimeConfig.GetLKGConfig(response.Context);
OutputCacheSection outputCacheConfig = config.OutputCache;
if (outputCacheConfig != null) {
omitVaryStar = outputCacheConfig.OmitVaryStar;
}
else {
omitVaryStar = OutputCacheSection.DefaultOmitVaryStar;
}
}
if (!omitVaryStar) {
// Dev10 Bug 425047 - OutputCache Location="ServerAndClient" (HttpCacheability.ServerAndPrivate) should
// not use "Vary: *" so the response can be cached on the client
if (_varyByCustom != null || (_varyByParams.IsModified() && !_varyByParams.IgnoreParams)) {
varyByHeaders = "*";
}
}
if (varyByHeaders == null) {
varyByHeaders = _varyByHeaders.ToHeaderString();
}
if (varyByHeaders != null) {
_headerVaryBy = new HttpResponseHeader(HttpWorkerRequest.HeaderVary, varyByHeaders);
}
}
}
_useCachedHeaders = true;
}
/*
* Generate headers and append them to the list
*/
internal void GetHeaders(ArrayList headers, HttpResponse response) {
StringBuilder sb;
String expirationDate;
TimeSpan age, maxAge, proxyMaxAge;
DateTime utcExpires;
HttpResponseHeader headerExpires;
HttpResponseHeader headerCacheControl;
UpdateCachedHeaders(response);
headerExpires = _headerExpires;
headerCacheControl = _headerCacheControl;
/*
* reconstruct headers that vary with time
* don't send expiration information when item shouldn't be cached
*/
if (_cacheability != HttpCacheability.NoCache && _cacheability != HttpCacheability.ServerAndNoCache) {
if (_slidingExpiration == 1) {
/* update Expires header */
if (_isExpiresSet) {
utcExpires = _utcTimestampRequest + _slidingDelta;
expirationDate = HttpUtility.FormatHttpDateTimeUtc(utcExpires);
headerExpires = new HttpResponseHeader(HttpWorkerRequest.HeaderExpires, expirationDate);
}
}