/src/mozilla-central/netwerk/protocol/http/CacheControlParser.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | , mNoCache(false) |
21 | | , mNoStore(false) |
22 | 0 | { |
23 | 0 | SkipWhites(); |
24 | 0 | if (!CheckEOF()) { |
25 | 0 | Directive(); |
26 | 0 | } |
27 | 0 | } |
28 | | |
29 | | void CacheControlParser::Directive() |
30 | 0 | { |
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 | 0 | } else { |
43 | 0 | IgnoreDirective(); |
44 | 0 | } |
45 | 0 |
|
46 | 0 | SkipWhites(); |
47 | 0 | if (CheckEOF()) { |
48 | 0 | return; |
49 | 0 | } |
50 | 0 | if (CheckChar(',')) { |
51 | 0 | SkipWhites(); |
52 | 0 | Directive(); |
53 | 0 | return; |
54 | 0 | } |
55 | 0 | |
56 | 0 | NS_WARNING("Unexpected input in Cache-control header value"); |
57 | 0 | } |
58 | | |
59 | | bool CacheControlParser::SecondsValue(uint32_t *seconds, uint32_t defaultVal) |
60 | 0 | { |
61 | 0 | SkipWhites(); |
62 | 0 | if (!CheckChar('=')) { |
63 | 0 | *seconds = defaultVal; |
64 | 0 | return !!defaultVal; |
65 | 0 | } |
66 | 0 | |
67 | 0 | SkipWhites(); |
68 | 0 | if (!ReadInteger(seconds)) { |
69 | 0 | NS_WARNING("Unexpected value in Cache-control header value"); |
70 | 0 | return false; |
71 | 0 | } |
72 | 0 |
|
73 | 0 | return true; |
74 | 0 | } |
75 | | |
76 | | void CacheControlParser::IgnoreDirective() |
77 | 0 | { |
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 | 0 | } |
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 | 0 | } |
90 | 0 | } |
91 | 0 | } |
92 | 0 | } |
93 | | |
94 | | bool CacheControlParser::MaxAge(uint32_t *seconds) |
95 | 0 | { |
96 | 0 | *seconds = mMaxAge; |
97 | 0 | return mMaxAgeSet; |
98 | 0 | } |
99 | | |
100 | | bool CacheControlParser::MaxStale(uint32_t *seconds) |
101 | 0 | { |
102 | 0 | *seconds = mMaxStale; |
103 | 0 | return mMaxStaleSet; |
104 | 0 | } |
105 | | |
106 | | bool CacheControlParser::MinFresh(uint32_t *seconds) |
107 | 0 | { |
108 | 0 | *seconds = mMinFresh; |
109 | 0 | return mMinFreshSet; |
110 | 0 | } |
111 | | |
112 | | bool CacheControlParser::NoCache() |
113 | 0 | { |
114 | 0 | return mNoCache; |
115 | 0 | } |
116 | | |
117 | | bool CacheControlParser::NoStore() |
118 | 0 | { |
119 | 0 | return mNoStore; |
120 | 0 | } |
121 | | |
122 | | } // net |
123 | | } // mozilla |