]> git.ipfire.org Git - thirdparty/squid.git/blob - src/proxyp/Header.h
Maintenance: automate header guards 2/3 (#1655)
[thirdparty/squid.git] / src / proxyp / Header.h
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_PROXYP_HEADER_H
10 #define SQUID_SRC_PROXYP_HEADER_H
11
12 #include "base/RefCount.h"
13 #include "ip/Address.h"
14 #include "proxyp/Elements.h"
15 #include "sbuf/SBuf.h"
16
17 #include <vector>
18
19 namespace ProxyProtocol {
20
21 /// PROXY protocol v1 or v2 header
22 class Header: public RefCountable
23 {
24 public:
25 typedef RefCount<Header> Pointer;
26 typedef std::vector<Two::Tlv> Tlvs;
27
28 Header(const SBuf &ver, const Two::Command cmd);
29
30 /// HTTP header-like string representation of the header.
31 /// The returned string has one line per pseudo header and
32 /// one line per TLV (if any).
33 SBuf toMime() const;
34
35 /// \returns a delimiter-separated list of values of TLVs of the given type
36 SBuf getValues(const uint32_t headerType, const char delimiter = ',') const;
37
38 /// Searches for the first key=value pair occurrence within the
39 /// value for the provided TLV type. Assumes that the TLV value
40 /// is a delimiter-separated list.
41 /// \returns the value of the found pair or the empty string.
42 SBuf getElem(const uint32_t headerType, const char *member, const char delimiter) const;
43
44 /// PROXY protocol version
45 const SBuf &version() const { return version_; }
46
47 /// whether source and destination addresses are valid addresses of the original "client" connection
48 bool hasForwardedAddresses() const { return !localConnection() && hasAddresses(); }
49
50 /// marks the header as lacking address information
51 void ignoreAddresses() { ignoreAddresses_ = true; }
52
53 /// whether the header relays address information (including LOCAL connections)
54 bool hasAddresses() const { return !ignoreAddresses_; }
55
56 /// \returns "4" or "6" if both source and destination addresses are IPv4 or IPv6
57 /// \returns "mix" otherwise
58 const SBuf &addressFamily() const;
59
60 /// source address of the client connection
61 Ip::Address sourceAddress;
62 /// intended destination address of the client connection
63 Ip::Address destinationAddress;
64 /// empty in v1 headers and when ignored in v2 headers
65 Tlvs tlvs;
66
67 private:
68 /// Whether the connection over PROXY protocol is 'cmdLocal'.
69 /// Such connections are established without being relayed.
70 /// Received addresses and TLVs are discarded in this mode.
71 bool localConnection() const { return command_ == Two::cmdLocal; }
72
73 /// PROXY protocol version
74 SBuf version_;
75
76 /// for v2 headers: the command field
77 /// for v1 headers: Two::cmdProxy
78 Two::Command command_;
79
80 /// true if the header relays no address information
81 bool ignoreAddresses_;
82 };
83
84 } // namespace ProxyProtocol
85
86 #endif /* SQUID_SRC_PROXYP_HEADER_H */
87