]> git.ipfire.org Git - thirdparty/squid.git/blob - src/anyp/UriScheme.h
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / anyp / UriScheme.h
1 /*
2 * Copyright (C) 1996-2021 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_ANYP_URISCHEME_H
10 #define SQUID_ANYP_URISCHEME_H
11
12 #include "anyp/ProtocolType.h"
13 #include "sbuf/SBuf.h"
14
15 #include <iosfwd>
16 #include <vector>
17
18 namespace AnyP
19 {
20
21 /** This class represents a URI Scheme such as http:// https://, wais://, urn: etc.
22 * It does not represent the PROTOCOL that such schemes refer to.
23 */
24 class UriScheme
25 {
26 public:
27 typedef std::vector<SBuf> LowercaseSchemeNames;
28
29 UriScheme() : theScheme_(AnyP::PROTO_NONE) {}
30 /// \param img Explicit scheme representation for unknown/none schemes.
31 UriScheme(AnyP::ProtocolType const aScheme, const char *img = nullptr);
32 UriScheme(const AnyP::UriScheme &o) : theScheme_(o.theScheme_), image_(o.image_) {}
33 UriScheme(AnyP::UriScheme &&) = default;
34 ~UriScheme() {}
35
36 AnyP::UriScheme& operator=(const AnyP::UriScheme &o) {
37 theScheme_ = o.theScheme_;
38 image_ = o.image_;
39 return *this;
40 }
41 AnyP::UriScheme& operator=(AnyP::UriScheme &&) = default;
42
43 operator AnyP::ProtocolType() const { return theScheme_; }
44 // XXX: does not account for comparison of unknown schemes (by image)
45 bool operator != (AnyP::ProtocolType const & aProtocol) const { return theScheme_ != aProtocol; }
46
47 /** Get a char string representation of the scheme.
48 * Does not include the ':' or "://" terminators.
49 */
50 SBuf image() const {return image_;}
51
52 unsigned short defaultPort() const;
53
54 /// initializes down-cased protocol scheme names array
55 static void Init();
56
57 /// \returns ProtocolType for the given scheme name or PROTO_UNKNOWN
58 static const AnyP::ProtocolType FindProtocolType(const SBuf &);
59
60 private:
61 /// optimization: stores down-cased protocol scheme names, copied from
62 /// AnyP::ProtocolType_str
63 static LowercaseSchemeNames LowercaseSchemeNames_;
64
65 /// This is a typecode pointer into the enum/registry of protocols handled.
66 AnyP::ProtocolType theScheme_;
67
68 /// the string representation
69 SBuf image_;
70 };
71
72 } // namespace AnyP
73
74 inline std::ostream &
75 operator << (std::ostream &os, AnyP::UriScheme const &scheme)
76 {
77 os << scheme.image();
78 return os;
79 }
80
81 #endif /* SQUID_ANYP_URISCHEME_H */
82