Https.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_RESOURCE__HTTPS
18 #define COM_BORA_SOFTWARE__BALAU_RESOURCE__HTTPS
19 
21 #include <Balau/Resource/Url.hpp>
24 
25 namespace Balau::Resource {
26 
30 class Https : public Url {
34  public: Https() = default;
35 
39  public: explicit Https(std::string uri_) : Url(std::move(uri_)) {}
40 
44  public: explicit Https(std::string_view uri_) : Url(uri_) {}
45 
49  public: explicit Https(const char * uri_) : Url(uri_) {}
50 
54  public: Https(const Https & copy) = default;
55 
59  public: Https(Https && rhs) noexcept : Url(std::move(rhs)) {}
60 
61  public: std::unique_ptr<Uri> clone() const override {
62  return std::unique_ptr<Uri>(new Https(*this));
63  }
64 
65  public: std::unique_ptr<Uri> append(const std::string & pathComponent) const override {
66  return std::unique_ptr<Uri>(new Https(appendPathComponent(pathComponent)));
67  }
68 
69  public: std::unique_ptr<Uri> resolve(std::string_view path) const override {
70  static const std::regex scheme { "[a-zA-Z][a-zA-Z0-9+-\\.]*:" };
71 
72  auto cleanPath = Util::Strings::trim(path);
73  auto str = std::string(cleanPath);
74 
75  if (Util::Strings::startsWithRegex(str, scheme)) {
76  std::unique_ptr<Uri> uri;
77  fromString(uri, str);
78  return uri;
79  }
80 
81  const UriComponents components(uri);
82 
83  if (Util::Strings::startsWith(str, "/")) {
84  // Absolute path.
85  std::string url = ::toString(components.scheme(), "://", components.host(), str);
86  return std::unique_ptr<Uri>(new Https(url));
87  } else {
88  // Relative path.
89  // TODO normalise path.
90  std::string url = ::toString(components.scheme(), "://", components.host(), "/", components.path(), "/", str);
91  return std::unique_ptr<Uri>(new Https(url));
92  }
93  }
94 
95  public: bool operator == (const Uri & rhs) const override {
96  const auto * o = dynamic_cast<const Https *>(&rhs);
97  return o != nullptr && uri == o->uri;
98  }
99 
100  public: bool canReadFrom() const override {
101  return true;
102  }
103 
104  public: bool canWriteTo() const override {
105  return false;
106  }
107 
108  public: template <typename AllocatorT> Balau::U8String<AllocatorT> toRawString() const {
109  return ::toString<AllocatorT>(uri);
110  }
111 
116  return HttpsByteReadResource(*this);
117  }
118 
123  return HttpsUtf8To32ReadResource(*this);
124  }
125 
126  public: std::unique_ptr<ByteReadResource> byteReadResource() const override {
127  return std::unique_ptr<ByteReadResource>(new HttpsByteReadResource(*this));
128  }
129 
130  public: std::unique_ptr<Utf8To32ReadResource> utf8To32ReadResource() const override {
131  return std::unique_ptr<Utf8To32ReadResource>(new HttpsUtf8To32ReadResource(*this));
132  }
133 
134  public: std::unique_ptr<ByteWriteResource> byteWriteResource() override {
135  ThrowBalauException(Exception::NotImplementedException, "HTTPS URIs do not have a byte write resource.");
136  }
137 
138  public: std::unique_ptr<Utf32To8WriteResource> utf32To8WriteResource() override {
139  ThrowBalauException(Exception::NotImplementedException, "HTTPS URIs do not have a Unicode write resource.");
140  }
141 
142  public: bool isRecursivelyIterable() const override {
143  return false;
144  }
145 
146  public: bool isIterable() const override {
147  return false;
148  }
149 
150  public: std::unique_ptr<RecursiveUriIterator> recursiveIterator() const override {
151  ThrowBalauException(Exception::NotImplementedException, "HTTPS URIs do not have recursive iterator.");
152  }
153 
154  public: std::unique_ptr<UriIterator> iterator() const override {
155  ThrowBalauException(Exception::NotImplementedException, "HTTPS URIs do not have iterators.");
156  }
157 
158  public: void dispatch(UriDispatcher & dispatcher) const override {
159  dispatcher.dispatch(*this);
160  }
161 };
162 
168 template <typename AllocatorT>
170  return https.toRawString<AllocatorT>();
171 }
172 
173 } // namespace Balau::Resource
174 
175 #endif // COM_BORA_SOFTWARE__BALAU_RESOURCE__HTTPS
Https(std::string uri_)
Create an HTTPS URI from the supplied URI string.
Definition: Https.hpp:39
Https(Https &&rhs) noexcept
Create an HTTPS URI by moving the contents of the supplied instance.
Definition: Https.hpp:59
bool operator==(const Uri &rhs) const override
Compare the supplied URI to the current URI.
Definition: Https.hpp:95
std::unique_ptr< UriIterator > iterator() const override
Get a (non-recursive) iterator.
Definition: Https.hpp:154
virtual void dispatch(const File &object)=0
Visit a File URI.
std::unique_ptr< ByteReadResource > byteReadResource() const override
Get a byte read resource for the URI.
Definition: Https.hpp:126
std::unique_ptr< Utf8To32ReadResource > utf8To32ReadResource() const override
Get a UTF-8 to UTF-32 read resource for the URI.
Definition: Https.hpp:130
void dispatch(UriDispatcher &dispatcher) const override
Visitor pattern dispatching.
Definition: Https.hpp:158
An HTTPS URL.
Definition: Https.hpp:30
std::unique_ptr< RecursiveUriIterator > recursiveIterator() const override
Get a recursive iterator.
Definition: Https.hpp:150
#define ThrowBalauException(ExceptionClass,...)
Throw a Balau style exception, with implicit file and line number, and optional stacktrace.
Definition: BalauException.hpp:45
void fromString(File &destination, std::string_view value)
Overwrite the supplied file URI by assignment by converting the supplied UTF-8 string to a file URI...
Definition: File.hpp:733
bool canWriteTo() const override
Can data be written to the URI via a write resource.
Definition: Https.hpp:104
An abstract universal resource identifier.
Definition: Uri.hpp:131
STL namespace.
A read-only HTTPS resource which is read as bytes.
std::unique_ptr< Utf32To8WriteResource > utf32To8WriteResource() override
Get a UTF-32 to UTF-8 write resource for the URI.
Definition: Https.hpp:138
std::unique_ptr< Uri > clone() const override
Clone the concrete Uri.
Definition: Https.hpp:61
A read-only HTTPS UTF-8 resource which is read as UTF-32 characters.
Https(const char *uri_)
Create an HTTPS URI from the supplied URI string.
Definition: Https.hpp:49
bool canReadFrom() const override
Can data be read from the URI via a read resource.
Definition: Https.hpp:100
The unified resource class hierarchy.
Definition: ByteReadResource.hpp:24
Thrown when a feature is not yet implemented.
Definition: BalauException.hpp:162
Balau::U8String< AllocatorT > toRawString() const
Get a string representing the raw URI.
Definition: Https.hpp:108
std::string_view host() const
Obtain a string view to the host.
Definition: UriComponents.hpp:205
HttpsUtf8To32ReadResource getUtf8To32ReadResource()
Get a UTF-8 to UTF-32 read resource for the HTTPS source.
Definition: Https.hpp:122
A read-only HTTP UTF-8 resource which is read as UTF-32 characters.
Definition: HttpsUtf8To32ReadResource.hpp:35
Https(std::string_view uri_)
Create an HTTPS URI from the supplied URI string.
Definition: Https.hpp:44
Https()=default
Create a null HTTPS URI.
bool isIterable() const override
Does the URI have a non-recursive iterator (examples: file and zip archive URIs). ...
Definition: Https.hpp:146
Balau::U8String< AllocatorT > toString(const File &file)
Print the file URI as a UTF-8 string.
Definition: File.hpp:724
An abstract Url (either http or https).
HttpsByteReadResource getByteReadResource()
Get a byte read resource for the HTTPS source.
Definition: Https.hpp:115
static bool startsWith(const StringT< CharT, T ... > &str, const PrefixT &prefix)
Does the string start with the specified prefix?
Definition: Strings.hpp:46
static std::string_view trim(const std::string_view &input)
Trim whitespace from the beginning and end of the supplied UTF-8 string.
Definition: Strings.hpp:706
std::unique_ptr< Uri > resolve(std::string_view path) const override
Resolve the relative or absolute path, in reference to the current URI.
Definition: Https.hpp:69
bool isRecursivelyIterable() const override
Does the URI have a recursive iterator (examples: file and zip archive URIs).
Definition: Https.hpp:142
Base Balau exception classes.
std::basic_string< char, std::char_traits< char >, AllocatorT > U8String
UTF-8 string type with selectable allocator.
Definition: ToStringA.hpp:41
std::unique_ptr< Uri > append(const std::string &pathComponent) const override
Appends the path component to the supplied URI, returning a new URI.
Definition: Https.hpp:65
std::string_view scheme() const
Obtain a string view to the scheme.
Definition: UriComponents.hpp:177
std::string_view path() const
Obtain a string view to the path.
Definition: UriComponents.hpp:234
static bool startsWithRegex(const std::string &str, const std::regex &prefix)
Does the string start with the specified regular expression?
Definition: Strings.hpp:94
A read-only HTTP resource which is read as bytes.
Definition: HttpsByteReadResource.hpp:33
std::unique_ptr< ByteWriteResource > byteWriteResource() override
Get a byte write resource for the URI.
Definition: Https.hpp:134
An abstract Url (either http or https).
Definition: Url.hpp:28
Visitor interface for URIs.
Definition: UriDispatcher.hpp:32
Parses a URI and provides views into the URI&#39;s components.
Definition: UriComponents.hpp:52