• 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 "base/ranges/algorithm.h"
13 #include "net/http/http_util.h"
14 
15 namespace net {
16 
Parameter(const std::string & name)17 WebSocketExtension::Parameter::Parameter(const std::string& name)
18     : name_(name) {}
19 
Parameter(const std::string & name,const std::string & value)20 WebSocketExtension::Parameter::Parameter(const std::string& name,
21                                          const std::string& value)
22     : name_(name), value_(value) {
23   DCHECK(!value.empty());
24   // |extension-param| must be a token.
25   DCHECK(HttpUtil::IsToken(value));
26 }
27 
28 bool WebSocketExtension::Parameter::operator==(const Parameter& other) const =
29     default;
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 
Equivalent(const WebSocketExtension & other) const41 bool WebSocketExtension::Equivalent(const WebSocketExtension& other) const {
42   if (name_ != other.name_) return false;
43   if (parameters_.size() != other.parameters_.size()) return false;
44 
45   // Take copies in order to sort.
46   std::vector<Parameter> mine_sorted = parameters_;
47   std::vector<Parameter> other_sorted = other.parameters_;
48 
49   auto comparator = std::less<std::string>();
50   auto extract_name = [](const Parameter& param) -> const std::string& {
51     return param.name();
52   };
53 
54   // Sort by key, preserving order of values.
55   base::ranges::stable_sort(mine_sorted, comparator, extract_name);
56   base::ranges::stable_sort(other_sorted, comparator, extract_name);
57 
58   return mine_sorted == other_sorted;
59 }
60 
ToString() const61 std::string WebSocketExtension::ToString() const {
62   if (name_.empty())
63     return std::string();
64 
65   std::string result = name_;
66 
67   for (const auto& param : parameters_) {
68     result += "; " + param.name();
69     if (!param.HasValue())
70       continue;
71 
72     // |extension-param| must be a token and we don't need to quote it.
73     DCHECK(HttpUtil::IsToken(param.value()));
74     result += "=" + param.value();
75   }
76   return result;
77 }
78 
79 }  // namespace net
80