]> git.ipfire.org Git - thirdparty/squid.git/blame - src/anyp/UriScheme.h
Maintenance: automate header guards 2/3 (#1655)
[thirdparty/squid.git] / src / anyp / UriScheme.h
CommitLineData
bbc27441 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
bbc27441
AJ
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
ff9d9458
FC
9#ifndef SQUID_SRC_ANYP_URISCHEME_H
10#define SQUID_SRC_ANYP_URISCHEME_H
1ca54a54
AJ
11
12#include "anyp/ProtocolType.h"
d31d59d8 13#include "sbuf/SBuf.h"
074d6a40 14
1ca54a54 15#include <iosfwd>
380b09ae 16#include <optional>
45c1d1e6 17#include <vector>
1ca54a54
AJ
18
19namespace AnyP
20{
21
380b09ae
AR
22/// validated/supported port number; these values are never zero
23using KnownPort = uint16_t;
24
25/// validated/supported port number (if any)
26using Port = std::optional<KnownPort>;
27
1ca54a54
AJ
28/** This class represents a URI Scheme such as http:// https://, wais://, urn: etc.
29 * It does not represent the PROTOCOL that such schemes refer to.
30 */
31class UriScheme
32{
33public:
50a43a49
AR
34 typedef std::vector<SBuf> LowercaseSchemeNames;
35
1ca54a54 36 UriScheme() : theScheme_(AnyP::PROTO_NONE) {}
50a43a49 37 /// \param img Explicit scheme representation for unknown/none schemes.
d31d59d8
AJ
38 UriScheme(AnyP::ProtocolType const aScheme, const char *img = nullptr);
39 UriScheme(const AnyP::UriScheme &o) : theScheme_(o.theScheme_), image_(o.image_) {}
40 UriScheme(AnyP::UriScheme &&) = default;
1ca54a54
AJ
41 ~UriScheme() {}
42
d31d59d8
AJ
43 AnyP::UriScheme& operator=(const AnyP::UriScheme &o) {
44 theScheme_ = o.theScheme_;
45 image_ = o.image_;
46 return *this;
47 }
48 AnyP::UriScheme& operator=(AnyP::UriScheme &&) = default;
1ca54a54 49
d31d59d8
AJ
50 operator AnyP::ProtocolType() const { return theScheme_; }
51 // XXX: does not account for comparison of unknown schemes (by image)
1ca54a54
AJ
52 bool operator != (AnyP::ProtocolType const & aProtocol) const { return theScheme_ != aProtocol; }
53
54 /** Get a char string representation of the scheme.
d31d59d8 55 * Does not include the ':' or "://" terminators.
1ca54a54 56 */
d31d59d8 57 SBuf image() const {return image_;}
1ca54a54 58
380b09ae 59 Port defaultPort() const;
5c51bffb 60
50a43a49
AR
61 /// initializes down-cased protocol scheme names array
62 static void Init();
63
6c880a16 64 /// \returns ProtocolType for the given scheme name or PROTO_UNKNOWN
8b082ed9 65 static AnyP::ProtocolType FindProtocolType(const SBuf &);
6c880a16 66
1ca54a54 67private:
50a43a49
AR
68 /// optimization: stores down-cased protocol scheme names, copied from
69 /// AnyP::ProtocolType_str
70 static LowercaseSchemeNames LowercaseSchemeNames_;
71
1ca54a54
AJ
72 /// This is a typecode pointer into the enum/registry of protocols handled.
73 AnyP::ProtocolType theScheme_;
d31d59d8
AJ
74
75 /// the string representation
76 SBuf image_;
1ca54a54
AJ
77};
78
1ca54a54 79inline std::ostream &
25ecffe5 80operator <<(std::ostream &os, const UriScheme &scheme)
1ca54a54 81{
d31d59d8 82 os << scheme.image();
1ca54a54
AJ
83 return os;
84}
85
25ecffe5
AR
86} // namespace AnyP
87
ff9d9458 88#endif /* SQUID_SRC_ANYP_URISCHEME_H */
f53969cc 89