| OLD | NEW |
| 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 |
| 13 WebSocketExtensionParser::~WebSocketExtensionParser() {} | 14 WebSocketExtensionParser::~WebSocketExtensionParser() { |
| 15 } |
| 14 | 16 |
| 15 void WebSocketExtensionParser::Parse(const char* data, size_t size) { | 17 void WebSocketExtensionParser::Parse(const char* data, size_t size) { |
| 16 current_ = data; | 18 current_ = data; |
| 17 end_ = data + size; | 19 end_ = data + size; |
| 18 has_error_ = false; | 20 has_error_ = false; |
| 19 | 21 |
| 20 ConsumeExtension(&extension_); | 22 ConsumeExtension(&extension_); |
| 21 if (has_error_) return; | 23 if (has_error_) |
| 24 return; |
| 22 ConsumeSpaces(); | 25 ConsumeSpaces(); |
| 23 has_error_ = has_error_ || (current_ != end_); | 26 has_error_ = has_error_ || (current_ != end_); |
| 24 } | 27 } |
| 25 | 28 |
| 26 void WebSocketExtensionParser::Consume(char c) { | 29 void WebSocketExtensionParser::Consume(char c) { |
| 27 DCHECK(!has_error_); | 30 DCHECK(!has_error_); |
| 28 ConsumeSpaces(); | 31 ConsumeSpaces(); |
| 29 DCHECK(!has_error_); | 32 DCHECK(!has_error_); |
| 30 if (current_ == end_ || c != current_[0]) { | 33 if (current_ == end_ || c != current_[0]) { |
| 31 has_error_ = true; | 34 has_error_ = true; |
| 32 return; | 35 return; |
| 33 } | 36 } |
| 34 ++current_; | 37 ++current_; |
| 35 } | 38 } |
| 36 | 39 |
| 37 void WebSocketExtensionParser::ConsumeExtension(WebSocketExtension* extension) { | 40 void WebSocketExtensionParser::ConsumeExtension(WebSocketExtension* extension) { |
| 38 DCHECK(!has_error_); | 41 DCHECK(!has_error_); |
| 39 base::StringPiece name; | 42 base::StringPiece name; |
| 40 ConsumeToken(&name); | 43 ConsumeToken(&name); |
| 41 if (has_error_) return; | 44 if (has_error_) |
| 45 return; |
| 42 *extension = WebSocketExtension(name.as_string()); | 46 *extension = WebSocketExtension(name.as_string()); |
| 43 | 47 |
| 44 while (ConsumeIfMatch(';')) { | 48 while (ConsumeIfMatch(';')) { |
| 45 WebSocketExtension::Parameter parameter((std::string())); | 49 WebSocketExtension::Parameter parameter((std::string())); |
| 46 ConsumeExtensionParameter(¶meter); | 50 ConsumeExtensionParameter(¶meter); |
| 47 if (has_error_) return; | 51 if (has_error_) |
| 52 return; |
| 48 extension->Add(parameter); | 53 extension->Add(parameter); |
| 49 } | 54 } |
| 50 } | 55 } |
| 51 | 56 |
| 52 void WebSocketExtensionParser::ConsumeExtensionParameter( | 57 void WebSocketExtensionParser::ConsumeExtensionParameter( |
| 53 WebSocketExtension::Parameter* parameter) { | 58 WebSocketExtension::Parameter* parameter) { |
| 54 DCHECK(!has_error_); | 59 DCHECK(!has_error_); |
| 55 base::StringPiece name, value; | 60 base::StringPiece name, value; |
| 56 std::string value_string; | 61 std::string value_string; |
| 57 | 62 |
| 58 ConsumeToken(&name); | 63 ConsumeToken(&name); |
| 59 if (has_error_) return; | 64 if (has_error_) |
| 65 return; |
| 60 if (!ConsumeIfMatch('=')) { | 66 if (!ConsumeIfMatch('=')) { |
| 61 *parameter = WebSocketExtension::Parameter(name.as_string()); | 67 *parameter = WebSocketExtension::Parameter(name.as_string()); |
| 62 return; | 68 return; |
| 63 } | 69 } |
| 64 | 70 |
| 65 if (Lookahead('\"')) { | 71 if (Lookahead('\"')) { |
| 66 ConsumeQuotedToken(&value_string); | 72 ConsumeQuotedToken(&value_string); |
| 67 } else { | 73 } else { |
| 68 ConsumeToken(&value); | 74 ConsumeToken(&value); |
| 69 value_string = value.as_string(); | 75 value_string = value.as_string(); |
| 70 } | 76 } |
| 71 if (has_error_) return; | 77 if (has_error_) |
| 78 return; |
| 72 *parameter = WebSocketExtension::Parameter(name.as_string(), value_string); | 79 *parameter = WebSocketExtension::Parameter(name.as_string(), value_string); |
| 73 } | 80 } |
| 74 | 81 |
| 75 void WebSocketExtensionParser::ConsumeToken(base::StringPiece* token) { | 82 void WebSocketExtensionParser::ConsumeToken(base::StringPiece* token) { |
| 76 DCHECK(!has_error_); | 83 DCHECK(!has_error_); |
| 77 ConsumeSpaces(); | 84 ConsumeSpaces(); |
| 78 DCHECK(!has_error_); | 85 DCHECK(!has_error_); |
| 79 const char* head = current_; | 86 const char* head = current_; |
| 80 while (current_ < end_ && | 87 while (current_ < end_ && !IsControl(current_[0]) && |
| 81 !IsControl(current_[0]) && !IsSeparator(current_[0])) | 88 !IsSeparator(current_[0])) |
| 82 ++current_; | 89 ++current_; |
| 83 if (current_ == head) { | 90 if (current_ == head) { |
| 84 has_error_ = true; | 91 has_error_ = true; |
| 85 return; | 92 return; |
| 86 } | 93 } |
| 87 *token = base::StringPiece(head, current_ - head); | 94 *token = base::StringPiece(head, current_ - head); |
| 88 } | 95 } |
| 89 | 96 |
| 90 void WebSocketExtensionParser::ConsumeQuotedToken(std::string* token) { | 97 void WebSocketExtensionParser::ConsumeQuotedToken(std::string* token) { |
| 91 DCHECK(!has_error_); | 98 DCHECK(!has_error_); |
| 92 Consume('"'); | 99 Consume('"'); |
| 93 if (has_error_) return; | 100 if (has_error_) |
| 101 return; |
| 94 *token = ""; | 102 *token = ""; |
| 95 while (current_ < end_ && !IsControl(current_[0])) { | 103 while (current_ < end_ && !IsControl(current_[0])) { |
| 96 if (UnconsumedBytes() >= 2 && current_[0] == '\\') { | 104 if (UnconsumedBytes() >= 2 && current_[0] == '\\') { |
| 97 char next = current_[1]; | 105 char next = current_[1]; |
| 98 if (IsControl(next) || IsSeparator(next)) break; | 106 if (IsControl(next) || IsSeparator(next)) |
| 107 break; |
| 99 *token += next; | 108 *token += next; |
| 100 current_ += 2; | 109 current_ += 2; |
| 101 } else if (IsSeparator(current_[0])) { | 110 } else if (IsSeparator(current_[0])) { |
| 102 break; | 111 break; |
| 103 } else { | 112 } else { |
| 104 *token += current_[0]; | 113 *token += current_[0]; |
| 105 ++current_; | 114 ++current_; |
| 106 } | 115 } |
| 107 } | 116 } |
| 108 // We can't use Consume here because we don't want to consume spaces. | 117 // We can't use Consume here because we don't want to consume spaces. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 return (0 <= c && c <= 31) || c == 127; | 158 return (0 <= c && c <= 31) || c == 127; |
| 150 } | 159 } |
| 151 | 160 |
| 152 // static | 161 // static |
| 153 bool WebSocketExtensionParser::IsSeparator(char c) { | 162 bool WebSocketExtensionParser::IsSeparator(char c) { |
| 154 const char separators[] = "()<>@,;:\\\"/[]?={} \t"; | 163 const char separators[] = "()<>@,;:\\\"/[]?={} \t"; |
| 155 return strchr(separators, c) != NULL; | 164 return strchr(separators, c) != NULL; |
| 156 } | 165 } |
| 157 | 166 |
| 158 } // namespace net | 167 } // namespace net |
| OLD | NEW |