tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CacheControlParser.cpp (3598B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set sw=2 ts=8 et tw=80 : */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "CacheControlParser.h"
      8 
      9 namespace mozilla {
     10 namespace net {
     11 
     12 CacheControlParser::CacheControlParser(nsACString const& aHeader)
     13    : Tokenizer(aHeader, nullptr, "-_"),
     14      mMaxAgeSet(false),
     15      mMaxAge(0),
     16      mMaxStaleSet(false),
     17      mMaxStale(0),
     18      mMinFreshSet(false),
     19      mMinFresh(0),
     20      mStaleWhileRevalidateSet(false),
     21      mStaleWhileRevalidate(0),
     22      mNoCache(false),
     23      mNoStore(false),
     24      mPublic(false),
     25      mPrivate(false),
     26      mImmutable(false) {
     27  SkipWhites();
     28  if (!CheckEOF()) {
     29    Directive();
     30  }
     31 }
     32 
     33 void CacheControlParser::Directive() {
     34  nsAutoCString word;
     35  do {
     36    SkipWhites();
     37    if (!ReadWord(word)) {
     38      return;
     39    }
     40 
     41    ToLowerCase(word);
     42    if (word == "no-cache") {
     43      mNoCache = true;
     44      IgnoreDirective();  // ignore any optionally added values
     45    } else if (word == "no-store") {
     46      mNoStore = true;
     47    } else if (word == "max-age") {
     48      mMaxAgeSet = SecondsValue(&mMaxAge);
     49    } else if (word == "max-stale") {
     50      mMaxStaleSet = SecondsValue(&mMaxStale, PR_UINT32_MAX);
     51    } else if (word == "min-fresh") {
     52      mMinFreshSet = SecondsValue(&mMinFresh);
     53    } else if (word == "stale-while-revalidate") {
     54      mStaleWhileRevalidateSet = SecondsValue(&mStaleWhileRevalidate);
     55    } else if (word == "public") {
     56      mPublic = true;
     57    } else if (word == "private") {
     58      mPrivate = true;
     59    } else if (word == "immutable") {
     60      mImmutable = true;
     61    } else {
     62      IgnoreDirective();
     63    }
     64 
     65    SkipWhites();
     66    if (CheckEOF()) {
     67      return;
     68    }
     69 
     70  } while (CheckChar(','));
     71 
     72  NS_WARNING("Unexpected input in Cache-control header value");
     73 }
     74 
     75 bool CacheControlParser::SecondsValue(uint32_t* seconds, uint32_t defaultVal) {
     76  SkipWhites();
     77  if (!CheckChar('=')) {
     78    IgnoreDirective();
     79    *seconds = defaultVal;
     80    return !!defaultVal;
     81  }
     82 
     83  SkipWhites();
     84  if (!ReadInteger(seconds)) {
     85    NS_WARNING("Unexpected value in Cache-control header value");
     86    IgnoreDirective();
     87    *seconds = defaultVal;
     88    return !!defaultVal;
     89  }
     90 
     91  return true;
     92 }
     93 
     94 void CacheControlParser::IgnoreDirective() {
     95  Token t;
     96  while (Next(t)) {
     97    if (t.Equals(Token::Char(',')) || t.Equals(Token::EndOfFile())) {
     98      Rollback();
     99      break;
    100    }
    101    if (t.Equals(Token::Char('"'))) {
    102      SkipUntil(Token::Char('"'));
    103      if (!CheckChar('"')) {
    104        NS_WARNING(
    105            "Missing quoted string expansion in Cache-control header value");
    106        break;
    107      }
    108    }
    109  }
    110 }
    111 
    112 bool CacheControlParser::MaxAge(uint32_t* seconds) {
    113  *seconds = mMaxAge;
    114  return mMaxAgeSet;
    115 }
    116 
    117 bool CacheControlParser::MaxStale(uint32_t* seconds) {
    118  *seconds = mMaxStale;
    119  return mMaxStaleSet;
    120 }
    121 
    122 bool CacheControlParser::MinFresh(uint32_t* seconds) {
    123  *seconds = mMinFresh;
    124  return mMinFreshSet;
    125 }
    126 
    127 bool CacheControlParser::StaleWhileRevalidate(uint32_t* seconds) {
    128  *seconds = mStaleWhileRevalidate;
    129  return mStaleWhileRevalidateSet;
    130 }
    131 
    132 bool CacheControlParser::NoCache() { return mNoCache; }
    133 
    134 bool CacheControlParser::NoStore() { return mNoStore; }
    135 
    136 bool CacheControlParser::Public() { return mPublic; }
    137 
    138 bool CacheControlParser::Private() { return mPrivate; }
    139 
    140 bool CacheControlParser::Immutable() { return mImmutable; }
    141 
    142 }  // namespace net
    143 }  // namespace mozilla