blob: b737d5eab6fb9fc18577d3bdc2451fc8a8e77426 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/websockets/websocket_extension_parser.h"
#include "base/strings/string_util.h"
namespace net {
WebSocketExtensionParser::WebSocketExtensionParser() {}
WebSocketExtensionParser::~WebSocketExtensionParser() {}
bool WebSocketExtensionParser::Parse(const char* data, size_t size) {
current_ = data;
end_ = data + size;
extensions_.clear();
bool failed = false;
while (true) {
WebSocketExtension extension;
if (!ConsumeExtension(&extension)) {
failed = true;
break;
}
extensions_.push_back(extension);
ConsumeSpaces();
if (!ConsumeIfMatch(',')) {
break;
}
}
if (!failed && current_ == end_)
return true;
extensions_.clear();
return false;
}
bool WebSocketExtensionParser::Consume(char c) {
ConsumeSpaces();
if (current_ == end_ || c != current_[0]) {
return false;
}
++current_;
return true;
}
bool WebSocketExtensionParser::ConsumeExtension(WebSocketExtension* extension) {
base::StringPiece name;
if (!ConsumeToken(&name))
return false;
*extension = WebSocketExtension(name.as_string());
while (ConsumeIfMatch(';')) {
WebSocketExtension::Parameter parameter((std::string()));
if (!ConsumeExtensionParameter(¶meter))
return false;
extension->Add(parameter);
}
return true;
}
bool WebSocketExtensionParser::ConsumeExtensionParameter(
WebSocketExtension::Parameter* parameter) {
base::StringPiece name, value;
std::string value_string;
if (!ConsumeToken(&name))
return false;
if (!ConsumeIfMatch('=')) {
*parameter = WebSocketExtension::Parameter(name.as_string());
return true;
}
if (Lookahead('\"')) {
if (!ConsumeQuotedToken(&value_string))
return false;
} else {
if (!ConsumeToken(&value))
return false;
value_string = value.as_string();
}
*parameter = WebSocketExtension::Parameter(name.as_string(), value_string);
return true;
}
bool WebSocketExtensionParser::ConsumeToken(base::StringPiece* token) {
ConsumeSpaces();
const char* head = current_;
while (current_ < end_ &&
!IsControl(current_[0]) && !IsSeparator(current_[0]))
++current_;
if (current_ == head) {
return false;
}
*token = base::StringPiece(head, current_ - head);
return true;
}
bool WebSocketExtensionParser::ConsumeQuotedToken(std::string* token) {
if (!Consume('"'))
return false;
*token = "";
while (current_ < end_ && !IsControl(current_[0])) {
if (UnconsumedBytes() >= 2 && current_[0] == '\\') {
char next = current_[1];
if (IsControl(next) || IsSeparator(next)) break;
*token += next;
current_ += 2;
} else if (IsSeparator(current_[0])) {
break;
} else {
*token += current_[0];
++current_;
}
}
// We can't use Consume here because we don't want to consume spaces.
if (current_ >= end_ || current_[0] != '"')
return false;
++current_;
return !token->empty();
}
void WebSocketExtensionParser::ConsumeSpaces() {
while (current_ < end_ && (current_[0] == ' ' || current_[0] == '\t'))
++current_;
return;
}
bool WebSocketExtensionParser::Lookahead(char c) {
const char* head = current_;
bool result = Consume(c);
current_ = head;
return result;
}
bool WebSocketExtensionParser::ConsumeIfMatch(char c) {
const char* head = current_;
if (!Consume(c)) {
current_ = head;
return false;
}
return true;
}
// static
bool WebSocketExtensionParser::IsControl(char c) {
return (0 <= c && c <= 31) || c == 127;
}
// static
bool WebSocketExtensionParser::IsSeparator(char c) {
const char separators[] = "()<>@,;:\\\"/[]?={} \t";
return strchr(separators, c) != NULL;
}
} // namespace net
|