blob: c5e8e1720bf237e695df3c79a7efd8252e8f5ffc [file] [log] [blame]
[email protected]d28e67a2013-09-19 10:31:431// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/websockets/websocket_extension.h"
6
yhirano8387aee2015-09-14 05:46:497#include <map>
[email protected]d28e67a2013-09-19 10:31:438#include <string>
9
10#include "base/logging.h"
yhiranoa10dd4e2015-09-28 09:06:3411#include "net/http/http_util.h"
[email protected]d28e67a2013-09-19 10:31:4312
13namespace net {
14
15WebSocketExtension::Parameter::Parameter(const std::string& name)
16 : name_(name) {}
17
18WebSocketExtension::Parameter::Parameter(const std::string& name,
19 const std::string& value)
20 : name_(name), value_(value) {
21 DCHECK(!value.empty());
yhiranoa10dd4e2015-09-28 09:06:3422 // |extension-param| must be a token.
23 DCHECK(HttpUtil::IsToken(value));
[email protected]d28e67a2013-09-19 10:31:4324}
25
26bool WebSocketExtension::Parameter::Equals(const Parameter& other) const {
27 return name_ == other.name_ && value_ == other.value_;
28}
29
30WebSocketExtension::WebSocketExtension() {}
31
32WebSocketExtension::WebSocketExtension(const std::string& name)
33 : name_(name) {}
34
35WebSocketExtension::~WebSocketExtension() {}
36
37bool WebSocketExtension::Equals(const WebSocketExtension& other) const {
38 if (name_ != other.name_) return false;
39 if (parameters_.size() != other.parameters_.size()) return false;
yhirano8387aee2015-09-14 05:46:4940
41 std::multimap<std::string, std::string> this_parameters, other_parameters;
42 for (const auto& p : parameters_) {
43 this_parameters.insert(std::make_pair(p.name(), p.value()));
[email protected]d28e67a2013-09-19 10:31:4344 }
yhirano8387aee2015-09-14 05:46:4945 for (const auto& p : other.parameters_) {
46 other_parameters.insert(std::make_pair(p.name(), p.value()));
47 }
48 return this_parameters == other_parameters;
[email protected]d28e67a2013-09-19 10:31:4349}
50
yhiranoa10dd4e2015-09-28 09:06:3451std::string WebSocketExtension::ToString() const {
52 if (name_.empty())
53 return std::string();
54
55 std::string result = name_;
56
57 for (const auto& param : parameters_) {
58 result += "; " + param.name();
59 if (!param.HasValue())
60 continue;
61
62 // |extension-param| must be a token and we don't need to quote it.
63 DCHECK(HttpUtil::IsToken(param.value()));
64 result += "=" + param.value();
65 }
66 return result;
67}
68
[email protected]d28e67a2013-09-19 10:31:4369} // namespace net