CacheControlParser.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "CacheControlParser.h"
  6. namespace mozilla {
  7. namespace net {
  8. CacheControlParser::CacheControlParser(nsACString const &aHeader)
  9. : Tokenizer(aHeader, nullptr, "-_")
  10. , mMaxAgeSet(false)
  11. , mMaxAge(0)
  12. , mMaxStaleSet(false)
  13. , mMaxStale(0)
  14. , mMinFreshSet(false)
  15. , mMinFresh(0)
  16. , mNoCache(false)
  17. , mNoStore(false)
  18. {
  19. SkipWhites();
  20. if (!CheckEOF()) {
  21. Directive();
  22. }
  23. }
  24. void CacheControlParser::Directive()
  25. {
  26. if (CheckWord("no-cache")) {
  27. mNoCache = true;
  28. IgnoreDirective(); // ignore any optionally added values
  29. } else if (CheckWord("no-store")) {
  30. mNoStore = true;
  31. } else if (CheckWord("max-age")) {
  32. mMaxAgeSet = SecondsValue(&mMaxAge);
  33. } else if (CheckWord("max-stale")) {
  34. mMaxStaleSet = SecondsValue(&mMaxStale, PR_UINT32_MAX);
  35. } else if (CheckWord("min-fresh")) {
  36. mMinFreshSet = SecondsValue(&mMinFresh);
  37. } else {
  38. IgnoreDirective();
  39. }
  40. SkipWhites();
  41. if (CheckEOF()) {
  42. return;
  43. }
  44. if (CheckChar(',')) {
  45. SkipWhites();
  46. Directive();
  47. return;
  48. }
  49. NS_WARNING("Unexpected input in Cache-control header value");
  50. }
  51. bool CacheControlParser::SecondsValue(uint32_t *seconds, uint32_t defaultVal)
  52. {
  53. SkipWhites();
  54. if (!CheckChar('=')) {
  55. *seconds = defaultVal;
  56. return !!defaultVal;
  57. }
  58. SkipWhites();
  59. if (!ReadInteger(seconds)) {
  60. NS_WARNING("Unexpected value in Cache-control header value");
  61. return false;
  62. }
  63. return true;
  64. }
  65. void CacheControlParser::IgnoreDirective()
  66. {
  67. Token t;
  68. while (Next(t)) {
  69. if (t.Equals(Token::Char(',')) || t.Equals(Token::EndOfFile())) {
  70. Rollback();
  71. break;
  72. }
  73. if (t.Equals(Token::Char('"'))) {
  74. SkipUntil(Token::Char('"'));
  75. if (!CheckChar('"')) {
  76. NS_WARNING("Missing quoted string expansion in Cache-control header value");
  77. break;
  78. }
  79. }
  80. }
  81. }
  82. bool CacheControlParser::MaxAge(uint32_t *seconds)
  83. {
  84. *seconds = mMaxAge;
  85. return mMaxAgeSet;
  86. }
  87. bool CacheControlParser::MaxStale(uint32_t *seconds)
  88. {
  89. *seconds = mMaxStale;
  90. return mMaxStaleSet;
  91. }
  92. bool CacheControlParser::MinFresh(uint32_t *seconds)
  93. {
  94. *seconds = mMinFresh;
  95. return mMinFreshSet;
  96. }
  97. bool CacheControlParser::NoCache()
  98. {
  99. return mNoCache;
  100. }
  101. bool CacheControlParser::NoStore()
  102. {
  103. return mNoStore;
  104. }
  105. } // net
  106. } // mozilla