• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors
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 
7 #include <map>
8 #include <string>
9 #include <utility>
10 
11 #include "base/check.h"
12 #include "net/http/http_util.h"
13 
14 namespace net {
15 
Parameter(const std::string & name)16 WebSocketExtension::Parameter::Parameter(const std::string& name)
17     : name_(name) {}
18 
Parameter(const std::string & name,const std::string & value)19 WebSocketExtension::Parameter::Parameter(const std::string& name,
20                                          const std::string& value)
21     : name_(name), value_(value) {
22   DCHECK(!value.empty());
23   // |extension-param| must be a token.
24   DCHECK(HttpUtil::IsToken(value));
25 }
26 
Equals(const Parameter & other) const27 bool WebSocketExtension::Parameter::Equals(const Parameter& other) const {
28   return name_ == other.name_ && value_ == other.value_;
29 }
30 
31 WebSocketExtension::WebSocketExtension() = default;
32 
WebSocketExtension(const std::string & name)33 WebSocketExtension::WebSocketExtension(const std::string& name)
34     : name_(name) {}
35 
36 WebSocketExtension::WebSocketExtension(const WebSocketExtension& other) =
37     default;
38 
39 WebSocketExtension::~WebSocketExtension() = default;
40 
Equals(const WebSocketExtension & other) const41 bool WebSocketExtension::Equals(const WebSocketExtension& other) const {
42   if (name_ != other.name_) return false;
43   if (parameters_.size() != other.parameters_.size()) return false;
44 
45   std::multimap<std::string, std::string> this_parameters, other_parameters;
46   for (const auto& p : parameters_) {
47     this_parameters.insert(std::make_pair(p.name(), p.value()));
48   }
49   for (const auto& p : other.parameters_) {
50     other_parameters.insert(std::make_pair(p.name(), p.value()));
51   }
52   return this_parameters == other_parameters;
53 }
54 
ToString() const55 std::string WebSocketExtension::ToString() const {
56   if (name_.empty())
57     return std::string();
58 
59   std::string result = name_;
60 
61   for (const auto& param : parameters_) {
62     result += "; " + param.name();
63     if (!param.HasValue())
64       continue;
65 
66     // |extension-param| must be a token and we don't need to quote it.
67     DCHECK(HttpUtil::IsToken(param.value()));
68     result += "=" + param.value();
69   }
70   return result;
71 }
72 
73 }  // namespace net
74