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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
|
//------------------------------------------------------------------------------
// <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);
}
}
else {
if (_isMaxAgeSet || _isProxyMaxAgeSet) {
/* update max-age, s-maxage components of Cache-Control header */
if (headerCacheControl != null) {
sb = new StringBuilder(headerCacheControl.Value);
}
else {
sb = new StringBuilder();
}
age = _utcTimestampRequest - _utcTimestampCreated;
if (_isMaxAgeSet) {
maxAge = _maxAge - age;
if (maxAge < TimeSpan.Zero) {
maxAge = TimeSpan.Zero;
}
if (!_noMaxAgeInCacheControl)
AppendValueToHeader(sb, "max-age=" + ((long)maxAge.TotalSeconds).ToString(CultureInfo.InvariantCulture));
}
if (_isProxyMaxAgeSet) {
proxyMaxAge = _proxyMaxAge - age;
if (proxyMaxAge < TimeSpan.Zero) {
proxyMaxAge = TimeSpan.Zero;
}
if (!_noMaxAgeInCacheControl)
AppendValueToHeader(sb, "s-maxage=" + ((long)(proxyMaxAge).TotalSeconds).ToString(CultureInfo.InvariantCulture));
}
headerCacheControl = new HttpResponseHeader(HttpWorkerRequest.HeaderCacheControl, sb.ToString());
}
}
}
if (headerCacheControl != null) {
headers.Add(headerCacheControl);
}
if (_headerPragma != null) {
headers.Add(_headerPragma);
}
if (headerExpires != null) {
headers.Add(headerExpires);
}
if (_headerLastModified != null) {
headers.Add(_headerLastModified);
}
if (_headerEtag != null) {
headers.Add(_headerEtag);
}
if (_headerVaryBy != null) {
headers.Add(_headerVaryBy);
}
}
/*
* Public methods
*/
internal HttpCachePolicySettings GetCurrentSettings(HttpResponse response) {
String[] varyByContentEncodings;
String[] varyByHeaders;
String[] varyByParams;
String[] privateFields;
String[] noCacheFields;
ValidationCallbackInfo[] validationCallbackInfo;
UpdateCachedHeaders(response);
varyByContentEncodings = _varyByContentEncodings.GetContentEncodings();
varyByHeaders = _varyByHeaders.GetHeaders();
varyByParams = _varyByParams.GetParams();
if (_privateFields != null) {
privateFields = _privateFields.GetAllKeys();
}
else {
privateFields = null;
}
if (_noCacheFields != null) {
noCacheFields = _noCacheFields.GetAllKeys();
}
else {
noCacheFields = null;
}
if (_validationCallbackInfo != null) {
validationCallbackInfo = new ValidationCallbackInfo[_validationCallbackInfo.Count];
_validationCallbackInfo.CopyTo(0, validationCallbackInfo, 0, _validationCallbackInfo.Count);
}
else {
validationCallbackInfo = null;
}
return new HttpCachePolicySettings(
_isModified,
validationCallbackInfo,
_hasSetCookieHeader,
_noServerCaching,
_cacheExtension,
_noTransforms,
_ignoreRangeRequests,
varyByContentEncodings,
varyByHeaders,
varyByParams,
_varyByCustom,
_cacheability,
_noStore,
privateFields,
noCacheFields,
_utcExpires,
_isExpiresSet,
_maxAge,
_isMaxAgeSet,
_proxyMaxAge,
_isProxyMaxAgeSet,
_slidingExpiration,
_slidingDelta,
_utcTimestampCreated,
_validUntilExpires,
_allowInHistory,
_revalidation,
_utcLastModified,
_isLastModifiedSet,
_etag,
_generateLastModifiedFromFiles,
_generateEtagFromFiles,
_omitVaryStar,
_headerCacheControl,
_headerPragma,
_headerExpires,
_headerLastModified,
_headerEtag,
_headerVaryBy,
_hasUserProvidedDependencies);
}
internal bool HasValidationPolicy() {
return _generateLastModifiedFromFiles
|| _generateEtagFromFiles
|| _validationCallbackInfo != null
|| (_validUntilExpires == 1 && _slidingExpiration != 1);
}
internal bool HasExpirationPolicy() {
return _slidingExpiration != 1 && (_isExpiresSet || _isMaxAgeSet);
}
internal bool IsKernelCacheable(HttpRequest request, bool enableKernelCacheForVaryByStar) {
return _cacheability == HttpCacheability.Public
&& !_hasUserProvidedDependencies // Consider (Microsoft): rework dependency model to support user-provided dependencies
&& !_hasSetCookieHeader
&& !_noServerCaching
&& HasExpirationPolicy()
&& _cacheExtension == null
&& !_varyByContentEncodings.IsModified()
&& !_varyByHeaders.IsModified()
&& (!_varyByParams.IsModified() || _varyByParams.IgnoreParams || (_varyByParams.IsVaryByStar && enableKernelCacheForVaryByStar))
&& !_noStore
&& _varyByCustom == null
&& _privateFields == null
&& _noCacheFields == null
&& _validationCallbackInfo == null
&& (request != null && request.HttpVerb == HttpVerb.GET);
}
// VSUQFE 4225: expose some cache policy info
// because ISAPIWorkerRequestInProcForIIS6.CheckKernelModeCacheability needs to know about it
internal bool IsVaryByStar {get {return _varyByParams.IsVaryByStar; }}
internal DateTime UtcGetAbsoluteExpiration() {
DateTime absoluteExpiration = Cache.NoAbsoluteExpiration;
Debug.Assert(_utcTimestampCreated != DateTime.MinValue, "_utcTimestampCreated != DateTime.MinValue");
if (_slidingExpiration != 1) {
if (_isMaxAgeSet) {
absoluteExpiration = _utcTimestampCreated + _maxAge;
}
else if (_isExpiresSet) {
absoluteExpiration = _utcExpires;
}
}
return absoluteExpiration;
}
// Expose this property to OutputCacheUtility class
// In order to enable Out of Band output cache module to access the Validation Callback Info
internal IEnumerable GetValidationCallbacks() {
if (_validationCallbackInfo == null) {
return new ArrayList();
}
return _validationCallbackInfo;
}
/*
* Cache at server?
*/
/// <devdoc>
/// <para>A call to this method stops all server caching for the current response. </para>
/// </devdoc>
public void SetNoServerCaching() {
Dirtied();
_noServerCaching = true;
}
/// <summary>
/// Return True if we should stops all server caching for current response
/// </summary>
/// <returns></returns>
public bool GetNoServerCaching() {
return _noServerCaching;
}
internal void SetHasSetCookieHeader() {
Dirtied();
_hasSetCookieHeader = true;
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void SetVaryByCustom(string custom) {
if (custom == null) {
throw new ArgumentNullException("custom");
}
if (_varyByCustom != null) {
throw new InvalidOperationException(SR.GetString(SR.VaryByCustom_already_set));
}
Dirtied();
_varyByCustom = custom;
}
/// <summary>
/// Get the Vary by Custom Value
/// </summary>
/// <returns></returns>
public string GetVaryByCustom() {
return _varyByCustom;
}
/*
* Cache-Control: extension
*/
/// <devdoc>
/// <para>Appends a cache control extension directive to the Cache-Control: header.</para>
/// </devdoc>
public void AppendCacheExtension(String extension) {
if (extension == null) {
throw new ArgumentNullException("extension");
}
Dirtied();
if (_cacheExtension == null) {
_cacheExtension = extension;
}
else {
_cacheExtension = _cacheExtension + ", " + extension;
}
}
/// <summary>
/// Get Cache Extensions Value
/// </summary>
/// <returns></returns>
public string GetCacheExtensions() {
return _cacheExtension;
}
/*
* Cache-Control: no-transform
*/
/// <devdoc>
/// <para>Enables the sending of the CacheControl:
/// no-transform directive.</para>
/// </devdoc>
public void SetNoTransforms() {
Dirtied();
_noTransforms = true;
}
/// <summary>
/// Return true if No-transform directive, enables the sending of the CacheControl
/// </summary>
/// <returns></returns>
public bool GetNoTransforms() {
return _noTransforms;
}
internal void SetIgnoreRangeRequests() {
Dirtied();
_ignoreRangeRequests = true;
}
/// <summary>
/// Return true if ignore range request
/// </summary>
/// <returns></returns>
public bool GetIgnoreRangeRequests() {
return _ignoreRangeRequests;
}
/// <devdoc>
/// <para>Contains policy for the Vary: header.</para>
/// </devdoc>
public HttpCacheVaryByContentEncodings VaryByContentEncodings {
get {
return _varyByContentEncodings;
}
}
/// <devdoc>
/// <para>Contains policy for the Vary: header.</para>
/// </devdoc>
public HttpCacheVaryByHeaders VaryByHeaders {
get {
return _varyByHeaders;
}
}
/// <devdoc>
/// <para>Contains params to vary GETs and POSTs by.</para>
/// </devdoc>
public HttpCacheVaryByParams VaryByParams {
get {
return _varyByParams;
}
}
/*
* Cacheability policy
*
* Cache-Control: public | private[=1#field] | no-cache[=1#field] | no-store
*/
/// <devdoc>
/// <para>Sets the Cache-Control header to one of the values of
/// HttpCacheability. This is used to enable the Cache-Control: public, private, and no-cache directives.</para>
/// </devdoc>
public void SetCacheability(HttpCacheability cacheability) {
if ((int) cacheability < (int) HttpCacheabilityLimits.MinValue ||
(int) HttpCacheabilityLimits.MaxValue < (int) cacheability) {
throw new ArgumentOutOfRangeException("cacheability");
}
if (s_cacheabilityValues[(int)cacheability] < s_cacheabilityValues[(int)_cacheability]) {
Dirtied();
_cacheability = cacheability;
}
}
/// <summary>
/// Get the Cache-control (public, private and no-cache) directive
/// </summary>
/// <returns></returns>
public HttpCacheability GetCacheability() {
return _cacheability;
}
/// <devdoc>
/// <para>Sets the Cache-Control header to one of the values of HttpCacheability in
/// conjunction with a field-level exclusion directive.</para>
/// </devdoc>
public void SetCacheability(HttpCacheability cacheability, String field) {
if (field == null) {
throw new ArgumentNullException("field");
}
switch (cacheability) {
case HttpCacheability.Private:
if (_privateFields == null) {
_privateFields = new HttpDictionary();
}
_privateFields.SetValue(field, field);
break;
case HttpCacheability.NoCache:
if (_noCacheFields == null) {
_noCacheFields = new HttpDictionary();
}
_noCacheFields.SetValue(field, field);
break;
default:
throw new ArgumentException(
SR.GetString(SR.Cacheability_for_field_must_be_private_or_nocache),
"cacheability");
}
Dirtied();
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void SetNoStore() {
Dirtied();
_noStore = true;
}
internal void SetDependencies(bool hasUserProvidedDependencies) {
Dirtied();
_hasUserProvidedDependencies = hasUserProvidedDependencies;
}
/// <summary>
/// return true if no store is set
/// </summary>
/// <returns></returns>
public bool GetNoStore() {
return _noStore;
}
/*
* Expiration policy.
*/
/*
* Expires: RFC date
*/
/// <devdoc>
/// <para>Sets the Expires: header to the given absolute date.</para>
/// </devdoc>
public void SetExpires(DateTime date) {
DateTime utcDate, utcNow;
utcDate = DateTimeUtil.ConvertToUniversalTime(date);
utcNow = DateTime.UtcNow;
if (utcDate - utcNow > s_oneYear) {
utcDate = utcNow + s_oneYear;
}
if (!_isExpiresSet || utcDate < _utcExpires) {
Dirtied();
_utcExpires = utcDate;
_isExpiresSet = true;
}
}
/// <summary>
/// Return the expire header as absolute expire datetime
/// </summary>
/// <returns></returns>
public DateTime GetExpires() {
return _utcExpires;
}
/*
* Cache-Control: max-age=delta-seconds
*/
/// <devdoc>
/// <para>Sets Cache-Control: s-maxage based on the specified time span</para>
/// </devdoc>
public void SetMaxAge(TimeSpan delta) {
if (delta < TimeSpan.Zero) {
throw new ArgumentOutOfRangeException("delta");
}
if (s_oneYear < delta) {
delta = s_oneYear;
}
if (!_isMaxAgeSet || delta < _maxAge) {
Dirtied();
_maxAge = delta;
_isMaxAgeSet = true;
}
}
/// <summary>
/// Get the Cache-Control Max Age
/// </summary>
/// <returns></returns>
public TimeSpan GetMaxAge() {
return _maxAge;
}
// Suppress max-age and s-maxage in cache-control header (required for IIS6 kernel mode cache)
internal void SetNoMaxAgeInCacheControl() {
_noMaxAgeInCacheControl = true;
}
/*
* Cache-Control: s-maxage=delta-seconds
*/
/// <devdoc>
/// <para>Sets the Cache-Control: s-maxage header based on the specified time span.</para>
/// </devdoc>
public void SetProxyMaxAge(TimeSpan delta) {
if (delta < TimeSpan.Zero) {
throw new ArgumentOutOfRangeException("delta");
}
if (!_isProxyMaxAgeSet || delta < _proxyMaxAge) {
Dirtied();
_proxyMaxAge = delta;
_isProxyMaxAgeSet = true;
}
}
/// <summary>
/// Get the Cache-Control: Proxy Max Age Value
/// </summary>
/// <returns></returns>
public TimeSpan GetProxyMaxAge() {
return _proxyMaxAge;
}
/*
* Sliding Expiration
*/
/// <devdoc>
/// <para>Make expiration sliding: that is, if cached, it should be renewed with each
/// response. This feature is identical in spirit to the IIS
/// configuration option to add an expiration header relative to the current response
/// time. This feature is identical in spirit to the IIS configuration option to add
/// an expiration header relative to the current response time.</para>
/// </devdoc>
public void SetSlidingExpiration(bool slide) {
if (_slidingExpiration == -1 || _slidingExpiration == 1) {
Dirtied();
_slidingExpiration = (slide) ? 1 : 0;
}
}
/// <summary>
/// Return true if to make expiration sliding. that is, if cached, it should be renewed with each
/// response. This feature is identical in spirit to the IIS
/// configuration option to add an expiration header relative to the current response
/// time. This feature is identical in spirit to the IIS configuration option to add
/// an expiration header relative to the current response time.
/// </summary>
/// <returns></returns>
public bool HasSlidingExpiration() {
return _slidingExpiration == 1;
}
public void SetValidUntilExpires(bool validUntilExpires) {
if (_validUntilExpires == -1 || _validUntilExpires == 1) {
Dirtied();
_validUntilExpires = (validUntilExpires) ? 1 : 0;
}
}
/// <summary>
/// Return true if valid until expires
/// </summary>
/// <returns></returns>
public bool IsValidUntilExpires() {
return _validUntilExpires == 1;
}
public void SetAllowResponseInBrowserHistory(bool allow) {
if (_allowInHistory == -1 || _allowInHistory == 1) {
Dirtied();
_allowInHistory = (allow) ? 1 : 0;
}
}
/*
* Validation policy.
*/
/*
* Cache-control: must-revalidate | proxy-revalidate
*/
/// <devdoc>
/// <para>Set the Cache-Control: header to reflect either the must-revalidate or
/// proxy-revalidate directives based on the supplied value. The default is to
/// not send either of these directives unless explicitly enabled using this
/// method.</para>
/// </devdoc>
public void SetRevalidation(HttpCacheRevalidation revalidation) {
if ((int) revalidation < (int) HttpCacheRevalidationLimits.MinValue ||
(int) HttpCacheRevalidationLimits.MaxValue < (int) revalidation) {
throw new ArgumentOutOfRangeException("revalidation");
}
if ((int) revalidation < (int) _revalidation) {
Dirtied();
_revalidation = revalidation;
}
}
/// <summary>
/// Get the Cache-Control: header to reflect either the must-revalidate or
/// proxy-revalidate directives.
/// The default is to not send either of these directives unless explicitly enabled using this method.
/// </summary>
/// <returns></returns>
public HttpCacheRevalidation GetRevalidation() {
return _revalidation;
}
/*
* Etag
*/
/// <devdoc>
/// <para>Set the ETag header to the supplied string. Once an ETag is set,
/// subsequent attempts to set it will fail and an exception will be thrown.</para>
/// </devdoc>
public void SetETag(String etag) {
if (etag == null) {
throw new ArgumentNullException("etag");
}
if (_etag != null) {
throw new InvalidOperationException(SR.GetString(SR.Etag_already_set));
}
if (_generateEtagFromFiles) {
throw new InvalidOperationException(SR.GetString(SR.Cant_both_set_and_generate_Etag));
}
Dirtied();
_etag = etag;
}
/// <summary>
/// Get the ETag header. Once an ETag is set,
/// subsequent attempts to set it will fail and an exception will be thrown.
/// </summary>
/// <returns></returns>
public string GetETag() {
return _etag;
}
/*
* Last-Modified: RFC Date
*/
/// <devdoc>
/// <para>Set the Last-Modified: header to the DateTime value supplied. If this
/// violates the restrictiveness hierarchy, this method will fail.</para>
/// </devdoc>
public void SetLastModified(DateTime date) {
DateTime utcDate = DateTimeUtil.ConvertToUniversalTime(date);
UtcSetLastModified(utcDate);
}
void UtcSetLastModified(DateTime utcDate) {
/*
* DevDiv# 545481
* Time may differ if the system time changes in the middle of the request.
* Adjust the timestamp to Now if necessary.
*/
DateTime utcNow = DateTime.UtcNow;
if (utcDate > utcNow) {
utcDate = utcNow;
}
/*
* Because HTTP dates have a resolution of 1 second, we
* need to store dates with 1 second resolution or comparisons
* will be off.
*/
utcDate = new DateTime(utcDate.Ticks - (utcDate.Ticks % TimeSpan.TicksPerSecond));
if (!_isLastModifiedSet || utcDate > _utcLastModified) {
Dirtied();
_utcLastModified = utcDate;
_isLastModifiedSet = true;
}
}
/// <summary>
/// Get the Last-Modified header.
/// </summary>
/// <returns></returns>
public DateTime GetUtcLastModified() {
return _utcLastModified;
}
/// <devdoc>
/// <para>Sets the Last-Modified: header based on the timestamps of the
/// file dependencies of the handler.</para>
/// </devdoc>
public void SetLastModifiedFromFileDependencies() {
Dirtied();
_generateLastModifiedFromFiles = true;
}
/// <summary>
/// Return true if the Last-Modified header is set to base on the timestamps of the
/// file dependencies of the handler.
/// </summary>
/// <returns></returns>
public bool GetLastModifiedFromFileDependencies() {
return _generateLastModifiedFromFiles;
}
/// <devdoc>
/// <para>Sets the Etag header based on the timestamps of the file
/// dependencies of the handler.</para>
/// </devdoc>
public void SetETagFromFileDependencies() {
if (_etag != null) {
throw new InvalidOperationException(SR.GetString(SR.Cant_both_set_and_generate_Etag));
}
Dirtied();
_generateEtagFromFiles = true;
}
/// <summary>
/// Return true if the Etag header has been set to base on the timestamps of the file
/// dependencies of the handler
/// </summary>
/// <returns></returns>
public bool GetETagFromFileDependencies() {
return _generateEtagFromFiles;
}
public void SetOmitVaryStar(bool omit) {
Dirtied();
if (_omitVaryStar == -1 || _omitVaryStar == 1) {
Dirtied();
_omitVaryStar = (omit) ? 1 : 0;
}
}
/// <summary>
/// Return true if to omit Vary Star
/// </summary>
/// <returns></returns>
public int GetOmitVaryStar() {
return _omitVaryStar;
}
/// <devdoc>
/// <para>Registers a validation callback for the current response.</para>
/// </devdoc>
public void AddValidationCallback(
HttpCacheValidateHandler handler, Object data) {
if (handler == null) {
throw new ArgumentNullException("handler");
}
Dirtied();
if (_validationCallbackInfo == null) {
_validationCallbackInfo = new ArrayList();
}
_validationCallbackInfo.Add(new ValidationCallbackInfo(handler, data));
}
/// <summary>
/// Utc Timestamp Created
/// </summary>
public DateTime UtcTimestampCreated {
get {
return _utcTimestampCreated;
}
set {
_utcTimestampCreated = value;
}
}
}
}
|