xref: /aosp_15_r20/external/cronet/net/websockets/websocket_extension.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #ifndef NET_WEBSOCKETS_WEBSOCKET_EXTENSION_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_EXTENSION_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "net/base/net_export.h"
12 
13 namespace net {
14 
15 // A WebSocketExtension instance represents a WebSocket extension specified
16 // in RFC6455.
17 class NET_EXPORT_PRIVATE WebSocketExtension {
18  public:
19   // Note that RFC6455 does not allow a parameter with an empty value.
20   class NET_EXPORT_PRIVATE Parameter final {
21    public:
22     // Construct a parameter which does not have a value.
23     explicit Parameter(const std::string& name);
24     // Construct a parameter with a non-empty value.
25     Parameter(const std::string& name, const std::string& value);
26 
27     bool operator==(const Parameter&) const;
28 
HasValue()29     bool HasValue() const { return !value_.empty(); }
name()30     const std::string& name() const { return name_; }
value()31     const std::string& value() const { return value_; }
32 
33     // The default copy constructor and the assignment operator are defined:
34     // we need them.
35    private:
36     std::string name_;
37     std::string value_;
38   };
39 
40   WebSocketExtension();
41   explicit WebSocketExtension(const std::string& name);
42   WebSocketExtension(const WebSocketExtension& other);
43   ~WebSocketExtension();
44 
Add(const Parameter & parameter)45   void Add(const Parameter& parameter) { parameters_.push_back(parameter); }
name()46   const std::string& name() const { return name_; }
parameters()47   const std::vector<Parameter>& parameters() const { return parameters_; }
48 
49   // Equivalent() returns true if `name_` and `parameters_` are the same, even
50   // if the order of keys in `parameters_` is different.
51   bool Equivalent(const WebSocketExtension& other) const;
52 
53   std::string ToString() const;
54 
55   // The default copy constructor and the assignment operator are defined:
56   // we need them.
57  private:
58   std::string name_;
59   std::vector<Parameter> parameters_;
60 };
61 
62 }  // namespace net
63 
64 #endif  // NET_WEBSOCKETS_WEBSOCKET_EXTENSION_H_
65