]> git.ipfire.org Git - thirdparty/squid.git/blame - src/anyp/UriScheme.cc
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / anyp / UriScheme.cc
CommitLineData
1ca54a54 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.
1ca54a54 7 */
bbc27441
AJ
8
9/* DEBUG: section 23 URL Scheme parsing */
10
1ca54a54
AJ
11#include "squid.h"
12#include "anyp/UriScheme.h"
13
50a43a49
AR
14AnyP::UriScheme::LowercaseSchemeNames AnyP::UriScheme::LowercaseSchemeNames_;
15
d31d59d8
AJ
16AnyP::UriScheme::UriScheme(AnyP::ProtocolType const aScheme, const char *img) :
17 theScheme_(aScheme)
1ca54a54 18{
50a43a49
AR
19 // RFC 3986 section 3.1: schemes are case-insensitive.
20
21 // To improve diagnostic, remember exactly how an unsupported scheme looks like.
22 // XXX: Object users may rely on toLower() canonicalization that we refuse to provide.
23 if (img && theScheme_ == AnyP::PROTO_UNKNOWN)
e71eae24 24 image_ = img;
50a43a49
AR
25
26 // XXX: A broken caller supplies an image of an absent scheme?
27 // XXX: We assume that the caller is using a lower-case image.
28 else if (img && theScheme_ == AnyP::PROTO_NONE)
e71eae24 29 image_ = img;
50a43a49
AR
30
31 else if (theScheme_ > AnyP::PROTO_NONE && theScheme_ < AnyP::PROTO_MAX)
32 image_ = LowercaseSchemeNames_.at(theScheme_);
33 // else, the image remains empty (e.g., "://example.com/")
34 // hopefully, theScheme_ is PROTO_NONE here
35}
36
37void
38AnyP::UriScheme::Init()
39{
40 if (LowercaseSchemeNames_.empty()) {
41 LowercaseSchemeNames_.reserve(sizeof(SBuf) * AnyP::PROTO_MAX);
42 // TODO: use base/EnumIterator.h if possible
43 for (int i = AnyP::PROTO_NONE; i < AnyP::PROTO_MAX; ++i) {
44 SBuf image(ProtocolType_str[i]);
45 image.toLower();
46 LowercaseSchemeNames_.emplace_back(image);
47 }
1ca54a54 48 }
1ca54a54 49}
f53969cc 50
8b082ed9 51AnyP::ProtocolType
6c880a16
AJ
52AnyP::UriScheme::FindProtocolType(const SBuf &scheme)
53{
54 if (scheme.isEmpty())
55 return AnyP::PROTO_NONE;
56
57 Init();
58
59 auto img = scheme;
60 img.toLower();
61 // TODO: use base/EnumIterator.h if possible
62 for (int i = AnyP::PROTO_NONE + 1; i < AnyP::PROTO_UNKNOWN; ++i) {
63 if (LowercaseSchemeNames_.at(i) == img)
64 return AnyP::ProtocolType(i);
65 }
66
67 return AnyP::PROTO_UNKNOWN;
68}
69
5c51bffb
AJ
70unsigned short
71AnyP::UriScheme::defaultPort() const
72{
73 switch (theScheme_) {
74
75 case AnyP::PROTO_HTTP:
76 return 80;
77
78 case AnyP::PROTO_HTTPS:
79 return 443;
80
81 case AnyP::PROTO_FTP:
82 return 21;
83
84 case AnyP::PROTO_COAP:
85 case AnyP::PROTO_COAPS:
86 // coaps:// default is TBA as of draft-ietf-core-coap-08.
87 // Assuming IANA policy of allocating same port for base and TLS protocol versions will occur.
88 return 5683;
89
5c51bffb
AJ
90 case AnyP::PROTO_WAIS:
91 return 210;
92
93 case AnyP::PROTO_CACHE_OBJECT:
94 return CACHE_HTTP_PORT;
95
96 case AnyP::PROTO_WHOIS:
97 return 43;
98
99 default:
100 return 0;
101 }
102}
103