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

Side by Side Diff: net/websockets/websocket_extension_parser.cc

Issue 1050013002: [WebSocketExtensionParser] Have Consume.* methods return bool instead of using has_error() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed unnecessary variable Created 5 years, 8 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/websockets/websocket_extension_parser.h" 5 #include "net/websockets/websocket_extension_parser.h"
6 6
7 #include "base/strings/string_util.h" 7 #include "base/strings/string_util.h"
8 8
9 namespace net { 9 namespace net {
10 10
11 WebSocketExtensionParser::WebSocketExtensionParser() {} 11 WebSocketExtensionParser::WebSocketExtensionParser() {}
12 12
13 WebSocketExtensionParser::~WebSocketExtensionParser() {} 13 WebSocketExtensionParser::~WebSocketExtensionParser() {}
14 14
15 void WebSocketExtensionParser::Parse(const char* data, size_t size) { 15 bool WebSocketExtensionParser::Parse(const char* data, size_t size) {
16 current_ = data; 16 current_ = data;
17 end_ = data + size; 17 end_ = data + size;
18 has_error_ = false;
19 extensions_.clear(); 18 extensions_.clear();
20 19
20 bool failed = false;
21
21 while (true) { 22 while (true) {
22 WebSocketExtension extension; 23 WebSocketExtension extension;
23 ConsumeExtension(&extension); 24 if (!ConsumeExtension(&extension)) {
24 if (has_error_) 25 failed = true;
25 break; 26 break;
27 }
26 extensions_.push_back(extension); 28 extensions_.push_back(extension);
27 29
28 ConsumeSpaces(); 30 ConsumeSpaces();
29 DCHECK(!has_error_);
30 31
31 if (!ConsumeIfMatch(',')) { 32 if (!ConsumeIfMatch(',')) {
32 break; 33 break;
33 } 34 }
34 } 35 }
35 36
36 has_error_ = has_error_ || current_ != end_; 37 if (!failed && current_ == end_)
37 if (has_error_) 38 return true;
38 extensions_.clear(); 39
40 extensions_.clear();
41 return false;
39 } 42 }
40 43
41 void WebSocketExtensionParser::Consume(char c) { 44 bool WebSocketExtensionParser::Consume(char c) {
42 DCHECK(!has_error_);
43 ConsumeSpaces(); 45 ConsumeSpaces();
44 DCHECK(!has_error_);
45 if (current_ == end_ || c != current_[0]) { 46 if (current_ == end_ || c != current_[0]) {
46 has_error_ = true; 47 return false;
47 return;
48 } 48 }
49 ++current_; 49 ++current_;
50 return true;
50 } 51 }
51 52
52 void WebSocketExtensionParser::ConsumeExtension(WebSocketExtension* extension) { 53 bool WebSocketExtensionParser::ConsumeExtension(WebSocketExtension* extension) {
53 DCHECK(!has_error_);
54 base::StringPiece name; 54 base::StringPiece name;
55 ConsumeToken(&name); 55 if (!ConsumeToken(&name))
56 if (has_error_) return; 56 return false;
57 *extension = WebSocketExtension(name.as_string()); 57 *extension = WebSocketExtension(name.as_string());
58 58
59 while (ConsumeIfMatch(';')) { 59 while (ConsumeIfMatch(';')) {
60 WebSocketExtension::Parameter parameter((std::string())); 60 WebSocketExtension::Parameter parameter((std::string()));
61 ConsumeExtensionParameter(&parameter); 61 if (!ConsumeExtensionParameter(&parameter))
62 if (has_error_) return; 62 return false;
63 extension->Add(parameter); 63 extension->Add(parameter);
64 } 64 }
65
66 return true;
65 } 67 }
66 68
67 void WebSocketExtensionParser::ConsumeExtensionParameter( 69 bool WebSocketExtensionParser::ConsumeExtensionParameter(
68 WebSocketExtension::Parameter* parameter) { 70 WebSocketExtension::Parameter* parameter) {
69 DCHECK(!has_error_);
70 base::StringPiece name, value; 71 base::StringPiece name, value;
71 std::string value_string; 72 std::string value_string;
72 73
73 ConsumeToken(&name); 74 if (!ConsumeToken(&name))
74 if (has_error_) return; 75 return false;
76
75 if (!ConsumeIfMatch('=')) { 77 if (!ConsumeIfMatch('=')) {
76 *parameter = WebSocketExtension::Parameter(name.as_string()); 78 *parameter = WebSocketExtension::Parameter(name.as_string());
77 return; 79 return true;
78 } 80 }
79 81
80 if (Lookahead('\"')) { 82 if (Lookahead('\"')) {
81 ConsumeQuotedToken(&value_string); 83 if (!ConsumeQuotedToken(&value_string))
84 return false;
82 } else { 85 } else {
83 ConsumeToken(&value); 86 if (!ConsumeToken(&value))
87 return false;
84 value_string = value.as_string(); 88 value_string = value.as_string();
eroman 2015/04/06 19:07:52 FYI: Before value.as_string() was called unconditi
tyoshino (SeeGerritForStatus) 2015/04/13 14:11:59 Thanks. value_string is not used when has_error_ i
85 } 89 }
86 if (has_error_) return;
87 *parameter = WebSocketExtension::Parameter(name.as_string(), value_string); 90 *parameter = WebSocketExtension::Parameter(name.as_string(), value_string);
91 return true;
88 } 92 }
89 93
90 void WebSocketExtensionParser::ConsumeToken(base::StringPiece* token) { 94 bool WebSocketExtensionParser::ConsumeToken(base::StringPiece* token) {
91 DCHECK(!has_error_);
92 ConsumeSpaces(); 95 ConsumeSpaces();
93 DCHECK(!has_error_);
94 const char* head = current_; 96 const char* head = current_;
95 while (current_ < end_ && 97 while (current_ < end_ &&
96 !IsControl(current_[0]) && !IsSeparator(current_[0])) 98 !IsControl(current_[0]) && !IsSeparator(current_[0]))
97 ++current_; 99 ++current_;
98 if (current_ == head) { 100 if (current_ == head) {
99 has_error_ = true; 101 return false;
100 return;
101 } 102 }
102 *token = base::StringPiece(head, current_ - head); 103 *token = base::StringPiece(head, current_ - head);
104 return true;
103 } 105 }
104 106
105 void WebSocketExtensionParser::ConsumeQuotedToken(std::string* token) { 107 bool WebSocketExtensionParser::ConsumeQuotedToken(std::string* token) {
106 DCHECK(!has_error_); 108 if (!Consume('"'))
107 Consume('"'); 109 return false;
108 if (has_error_) return; 110
109 *token = ""; 111 *token = "";
110 while (current_ < end_ && !IsControl(current_[0])) { 112 while (current_ < end_ && !IsControl(current_[0])) {
111 if (UnconsumedBytes() >= 2 && current_[0] == '\\') { 113 if (UnconsumedBytes() >= 2 && current_[0] == '\\') {
112 char next = current_[1]; 114 char next = current_[1];
113 if (IsControl(next) || IsSeparator(next)) break; 115 if (IsControl(next) || IsSeparator(next)) break;
114 *token += next; 116 *token += next;
115 current_ += 2; 117 current_ += 2;
116 } else if (IsSeparator(current_[0])) { 118 } else if (IsSeparator(current_[0])) {
117 break; 119 break;
118 } else { 120 } else {
119 *token += current_[0]; 121 *token += current_[0];
120 ++current_; 122 ++current_;
121 } 123 }
122 } 124 }
123 // We can't use Consume here because we don't want to consume spaces. 125 // We can't use Consume here because we don't want to consume spaces.
124 if (current_ < end_ && current_[0] == '"') 126 if (current_ < end_ && current_[0] == '"')
125 ++current_; 127 ++current_;
126 else 128 else
127 has_error_ = true; 129 return false;
eroman 2015/04/06 19:07:52 optional: You might inverse the test above to do:
tyoshino (SeeGerritForStatus) 2015/04/13 14:11:59 Done.
128 has_error_ = has_error_ || token->empty(); 130
131 if (token->empty())
eroman 2015/04/06 19:07:52 optinal: return !token->empty();
tyoshino (SeeGerritForStatus) 2015/04/13 14:11:59 Done.
132 return false;
133
134 return true;
129 } 135 }
130 136
131 void WebSocketExtensionParser::ConsumeSpaces() { 137 void WebSocketExtensionParser::ConsumeSpaces() {
132 DCHECK(!has_error_);
133 while (current_ < end_ && (current_[0] == ' ' || current_[0] == '\t')) 138 while (current_ < end_ && (current_[0] == ' ' || current_[0] == '\t'))
134 ++current_; 139 ++current_;
135 return; 140 return;
136 } 141 }
137 142
138 bool WebSocketExtensionParser::Lookahead(char c) { 143 bool WebSocketExtensionParser::Lookahead(char c) {
139 DCHECK(!has_error_);
140 const char* head = current_; 144 const char* head = current_;
141 145 bool result = Consume(c);
142 Consume(c);
143 bool result = !has_error_;
144 current_ = head; 146 current_ = head;
145 has_error_ = false;
146 return result; 147 return result;
147 } 148 }
148 149
149 bool WebSocketExtensionParser::ConsumeIfMatch(char c) { 150 bool WebSocketExtensionParser::ConsumeIfMatch(char c) {
150 DCHECK(!has_error_);
151 const char* head = current_; 151 const char* head = current_;
152 if (Consume(c))
eroman 2015/04/06 19:07:52 optional: I find the opposite more readable: if (
tyoshino (SeeGerritForStatus) 2015/04/13 14:11:59 Done.
153 return true;
152 154
153 Consume(c); 155 current_ = head;
154 if (has_error_) { 156 return false;
155 current_ = head;
156 has_error_ = false;
157 return false;
158 }
159 return true;
160 } 157 }
161 158
162 // static 159 // static
163 bool WebSocketExtensionParser::IsControl(char c) { 160 bool WebSocketExtensionParser::IsControl(char c) {
164 return (0 <= c && c <= 31) || c == 127; 161 return (0 <= c && c <= 31) || c == 127;
165 } 162 }
166 163
167 // static 164 // static
168 bool WebSocketExtensionParser::IsSeparator(char c) { 165 bool WebSocketExtensionParser::IsSeparator(char c) {
169 const char separators[] = "()<>@,;:\\\"/[]?={} \t"; 166 const char separators[] = "()<>@,;:\\\"/[]?={} \t";
170 return strchr(separators, c) != NULL; 167 return strchr(separators, c) != NULL;
171 } 168 }
172 169
173 } // namespace net 170 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698