Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: net/base/force_tls_state.cc

Issue 118049: Parsing routines for X-Force-TLS header.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/force_tls_state.h" 5 #include "net/base/force_tls_state.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/string_tokenizer.h"
9 #include "base/string_util.h"
8 #include "googleurl/src/gurl.h" 10 #include "googleurl/src/gurl.h"
9 #include "net/base/registry_controlled_domain.h" 11 #include "net/base/registry_controlled_domain.h"
10 12
11 namespace net { 13 namespace net {
12 14
13 ForceTLSState::ForceTLSState() { 15 ForceTLSState::ForceTLSState() {
14 } 16 }
15 17
16 void ForceTLSState::DidReceiveHeader(const GURL& url, 18 void ForceTLSState::DidReceiveHeader(const GURL& url,
17 const std::string& value) { 19 const std::string& value) {
18 // TODO(abarth): Actually parse |value| once the spec settles down. 20 // TODO(abarth): Actually parse |value| once the spec settles down.
19 EnableHost(url.host()); 21 EnableHost(url.host());
20 } 22 }
21 23
22 void ForceTLSState::EnableHost(const std::string& host) { 24 void ForceTLSState::EnableHost(const std::string& host) {
23 // TODO(abarth): Canonicalize host. 25 // TODO(abarth): Canonicalize host.
24 AutoLock lock(lock_); 26 AutoLock lock(lock_);
25 enabled_hosts_.insert(host); 27 enabled_hosts_.insert(host);
26 } 28 }
27 29
28 bool ForceTLSState::IsEnabledForHost(const std::string& host) { 30 bool ForceTLSState::IsEnabledForHost(const std::string& host) {
29 // TODO(abarth): Canonicalize host. 31 // TODO(abarth): Canonicalize host.
30 AutoLock lock(lock_); 32 AutoLock lock(lock_);
31 return enabled_hosts_.find(host) != enabled_hosts_.end(); 33 return enabled_hosts_.find(host) != enabled_hosts_.end();
32 } 34 }
33 35
36 // "X-Force-TLS" ":" "max-age" "=" delta-seconds *1INCLUDESUBDOMAINS
wtc 2009/06/01 17:59:19 OK, so you have the header's definition here. Wou
abarth-chromium 2009/06/02 02:24:05 Ok...... The only reason this method is public is
37 // INCLUDESUBDOMAINS = [ " includeSubDomains" ]
38 bool ForceTLSState::ParseHeader(const std::string& value,
39 int* max_age,
40 bool* include_subdomains) {
41 DCHECK(max_age);
42 DCHECK(include_subdomains);
43
44 int max_age_candidate;
45
46 enum ParserState {
47 START,
48 AFTER_MAX_AGE_LABEL,
49 AFTER_MAX_AGE_EQUALS,
50 AFTER_MAX_AGE,
51 AFTER_MAX_AGE_INCLUDE_SUB_DOMAINS_DELIMITER,
52 AFTER_INCLUDE_SUBDOMAINS,
53 } state = START;
54
55 StringTokenizer tokenizer(value, " =");
56 tokenizer.set_options(StringTokenizer::RETURN_DELIMS);
57 while (tokenizer.GetNext()) {
58 DCHECK(!tokenizer.token_is_delim() || tokenizer.token().length() == 1);
59 DCHECK(tokenizer.token_is_delim() || *tokenizer.token_begin() != ' ');
60 switch (state) {
61 case START:
62 if (*tokenizer.token_begin() == ' ')
63 continue;
64 if (!LowerCaseEqualsASCII(tokenizer.token(), "max-age"))
65 return false;
66 state = AFTER_MAX_AGE_LABEL;
67 break;
68
69 case AFTER_MAX_AGE_LABEL:
70 if (*tokenizer.token_begin() == ' ')
71 continue;
72 if (*tokenizer.token_begin() != '=')
73 return false;
74 DCHECK(tokenizer.token().length() == 1);
75 state = AFTER_MAX_AGE_EQUALS;
76 break;
77
78 case AFTER_MAX_AGE_EQUALS:
79 if (*tokenizer.token_begin() == ' ')
80 continue;
81 if (!StringToInt(tokenizer.token(), &max_age_candidate))
82 return false;
83 if (max_age_candidate < 0)
84 return false;
85 state = AFTER_MAX_AGE;
86 break;
87
88 case AFTER_MAX_AGE:
89 if (*tokenizer.token_begin() != ' ')
90 return false;
91 state = AFTER_MAX_AGE_INCLUDE_SUB_DOMAINS_DELIMITER;
92 break;
93
94 case AFTER_MAX_AGE_INCLUDE_SUB_DOMAINS_DELIMITER:
95 if (*tokenizer.token_begin() == ' ')
96 continue;
97 if (!LowerCaseEqualsASCII(tokenizer.token(), "includesubdomains"))
98 return false;
99 state = AFTER_INCLUDE_SUBDOMAINS;
100 break;
101
102 case AFTER_INCLUDE_SUBDOMAINS:
103 if (*tokenizer.token_begin() != ' ')
104 return false;
105 break;
106
107 default:
108 NOTREACHED();
109 }
110 }
111
112 // We've consumed all the input. Let's see what state we ended up in.
113 switch (state) {
114 case START:
115 case AFTER_MAX_AGE_LABEL:
116 case AFTER_MAX_AGE_EQUALS:
117 return false;
118 case AFTER_MAX_AGE:
119 case AFTER_MAX_AGE_INCLUDE_SUB_DOMAINS_DELIMITER:
120 *max_age = max_age_candidate;
121 *include_subdomains = false;
122 return true;
123 case AFTER_INCLUDE_SUBDOMAINS:
124 *max_age = max_age_candidate;
125 *include_subdomains = true;
126 return true;
127 default:
128 NOTREACHED();
129 return false;
130 }
131 }
132
34 } // namespace 133 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698