HttpUpgradeProtocolAccess.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_SRC_HTTPUPGRADEPROTOCOLACCESS_H
10 #define SQUID_SRC_HTTPUPGRADEPROTOCOLACCESS_H
11 
12 #include "acl/forward.h"
13 #include "sbuf/SBuf.h"
14 
15 #include <deque>
16 #include <map>
17 
20 {
21 public:
22  ProtocolView(const char * const start, const size_t len);
23  explicit ProtocolView(const SBuf &proto);
24 
27 };
28 
29 std::ostream &operator <<(std::ostream &, const ProtocolView &);
30 
31 // HTTP is not explicit about case sensitivity of Upgrade protocol strings, but
32 // there are bug reports showing different case variants used for WebSocket. We
33 // conservatively preserve the received case and compare case-sensitively.
34 
37 inline bool
38 vAinB(const ProtocolView &a, const ProtocolView &b)
39 {
40  // Optimization: Do not assert(a.name == b.name).
41  return b.version.isEmpty() || (a.version == b.version);
42 }
43 
44 class ConfigParser;
45 
48 {
49 public:
50  HttpUpgradeProtocolAccess() = default;
52  HttpUpgradeProtocolAccess(HttpUpgradeProtocolAccess &&) = delete; // no copying of any kind
53 
55  const acl_access *findGuard(const SBuf &proto) const;
56 
59 
61  template <typename Visitor> inline void forEach(const Visitor &) const;
62 
65  template <typename Visitor> inline void forApplicable(const ProtocolView &, const Visitor &) const;
66 
67 private:
69  class NamedGuard
70  {
71  public:
72  NamedGuard(const char *rawProtocol, acl_access*);
73  NamedGuard(const NamedGuard &&) = delete; // no copying of any kind
74  ~NamedGuard();
75 
76  const SBuf protocol;
78  acl_access *guard = nullptr;
79  };
80 
82  typedef std::deque<NamedGuard> NamedGuards;
83 
85  inline static const SBuf &ProtoOther();
86 
89 
91  acl_access *other = nullptr;
92 };
93 
94 template <typename Visitor>
95 inline void
96 HttpUpgradeProtocolAccess::forEach(const Visitor &visitor) const
97 {
98  for (const auto &namedGuard: namedGuards)
99  visitor(namedGuard.protocol, namedGuard.guard);
100  if (other)
101  visitor(ProtoOther(), other);
102 }
103 
104 template <typename Visitor>
105 inline void
106 HttpUpgradeProtocolAccess::forApplicable(const ProtocolView &offer, const Visitor &visitor) const
107 {
108  auto seenApplicable = false;
109  for (const auto &namedGuard: namedGuards) {
110  if (offer.name != namedGuard.proto.name)
111  continue;
112  if (vAinB(offer, namedGuard.proto) && visitor(namedGuard.protocol, namedGuard.guard))
113  return;
114  seenApplicable = true; // may already be true
115  }
116  if (!seenApplicable && other) // OTHER is applicable if named rules were not
117  (void)visitor(ProtoOther(), other);
118 }
119 
120 inline const SBuf &
122 {
123  static const auto proto = new SBuf("OTHER");
124  return *proto;
125 }
126 
127 #endif /* SQUID_SRC_HTTPUPGRADEPROTOCOLACCESS_H */
128 
const acl_access * findGuard(const SBuf &proto) const
SBuf name
everything up to (but excluding) the first slash('/')
acl_access * guard
configured access rule; never nil
std::deque< NamedGuard > NamedGuards
maps HTTP Upgrade protocol name/version to the ACLs guarding its usage
bool isEmpty() const
Definition: SBuf.h:435
a reference to a protocol name[/version] string; no 0-termination is assumed
Definition: SBuf.h:93
acl_access * other
OTHER rules governing unnamed protocols.
const ProtocolView proto
optimization: compiled this->protocol
bool vAinB(const ProtocolView &a, const ProtocolView &b)
NamedGuards namedGuards
rules governing upgrades to explicitly named protocols
NamedGuard(const char *rawProtocol, acl_access *)
void configureGuard(ConfigParser &)
parses a single allow/deny rule
void forApplicable(const ProtocolView &, const Visitor &) const
const SBuf protocol
configured protocol name (and version)
std::ostream & operator<<(std::ostream &, const ProtocolView &)
a single configured access rule for an explicitly named protocol
ProtocolView(const char *const start, const size_t len)
HttpUpgradeProtocolAccess()=default
Allows or blocks HTTP Upgrade protocols (see http_upgrade_request_protocols)
void forEach(const Visitor &) const
iterates over all configured rules, calling the given visitor
static const SBuf & ProtoOther()
pseudonym to specify rules for "all other protocols"
SBuf version
everything after the name, including the slash('/')

 

Introduction

Documentation

Support

Miscellaneous