Line data Source code
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 10 : 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 : , mNoCache(false)
21 10 : , mNoStore(false)
22 : {
23 10 : SkipWhites();
24 10 : if (!CheckEOF()) {
25 0 : Directive();
26 : }
27 10 : }
28 :
29 0 : void CacheControlParser::Directive()
30 : {
31 0 : if (CheckWord("no-cache")) {
32 0 : mNoCache = true;
33 0 : IgnoreDirective(); // ignore any optionally added values
34 0 : } else if (CheckWord("no-store")) {
35 0 : mNoStore = true;
36 0 : } else if (CheckWord("max-age")) {
37 0 : mMaxAgeSet = SecondsValue(&mMaxAge);
38 0 : } else if (CheckWord("max-stale")) {
39 0 : mMaxStaleSet = SecondsValue(&mMaxStale, PR_UINT32_MAX);
40 0 : } else if (CheckWord("min-fresh")) {
41 0 : mMinFreshSet = SecondsValue(&mMinFresh);
42 : } else {
43 0 : IgnoreDirective();
44 : }
45 :
46 0 : SkipWhites();
47 0 : if (CheckEOF()) {
48 0 : return;
49 : }
50 0 : if (CheckChar(',')) {
51 0 : SkipWhites();
52 0 : Directive();
53 0 : return;
54 : }
55 :
56 0 : NS_WARNING("Unexpected input in Cache-control header value");
57 : }
58 :
59 0 : bool CacheControlParser::SecondsValue(uint32_t *seconds, uint32_t defaultVal)
60 : {
61 0 : SkipWhites();
62 0 : if (!CheckChar('=')) {
63 0 : *seconds = defaultVal;
64 0 : return !!defaultVal;
65 : }
66 :
67 0 : SkipWhites();
68 0 : if (!ReadInteger(seconds)) {
69 0 : NS_WARNING("Unexpected value in Cache-control header value");
70 0 : return false;
71 : }
72 :
73 0 : return true;
74 : }
75 :
76 0 : void CacheControlParser::IgnoreDirective()
77 : {
78 0 : Token t;
79 0 : while (Next(t)) {
80 0 : if (t.Equals(Token::Char(',')) || t.Equals(Token::EndOfFile())) {
81 0 : Rollback();
82 0 : break;
83 : }
84 0 : if (t.Equals(Token::Char('"'))) {
85 0 : SkipUntil(Token::Char('"'));
86 0 : if (!CheckChar('"')) {
87 0 : NS_WARNING("Missing quoted string expansion in Cache-control header value");
88 0 : break;
89 : }
90 : }
91 : }
92 0 : }
93 :
94 2 : bool CacheControlParser::MaxAge(uint32_t *seconds)
95 : {
96 2 : *seconds = mMaxAge;
97 2 : return mMaxAgeSet;
98 : }
99 :
100 2 : bool CacheControlParser::MaxStale(uint32_t *seconds)
101 : {
102 2 : *seconds = mMaxStale;
103 2 : return mMaxStaleSet;
104 : }
105 :
106 2 : bool CacheControlParser::MinFresh(uint32_t *seconds)
107 : {
108 2 : *seconds = mMinFresh;
109 2 : return mMinFreshSet;
110 : }
111 :
112 2 : bool CacheControlParser::NoCache()
113 : {
114 2 : return mNoCache;
115 : }
116 :
117 10 : bool CacheControlParser::NoStore()
118 : {
119 10 : return mNoStore;
120 : }
121 :
122 : } // net
123 : } // mozilla
|