Http.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__HTTP
18 #define COM_BORA_SOFTWARE__BALAU_RESOURCE__HTTP
19 
23 #include <Balau/Resource/Url.hpp>
25 
26 namespace Balau::Resource {
27 
31 class Http : public Url {
35  public: Http() = default;
36 
40  public: explicit Http(std::string && uri_) : Url(std::move(uri_)) {}
41 
45  public: explicit Http(std::string_view uri_) : Url(uri_) {}
46 
50  public: explicit Http(const char * uri_) : Url(uri_) {}
51 
55  public: Http(const Http & copy) = default;
56 
60  public: Http(Http && rhs) noexcept : Url(std::move(rhs)) {}
61 
62  public: std::unique_ptr<Uri> clone() const override {
63  return std::unique_ptr<Uri>(new Http(*this));
64  }
65 
66  public: std::unique_ptr<Uri> append(const std::string & pathComponent) const override {
67  return std::unique_ptr<Uri>(new Http(appendPathComponent(pathComponent)));
68  }
69 
70  public: std::unique_ptr<Uri> resolve(std::string_view path) const override {
71  static const std::regex scheme { "[a-zA-Z][a-zA-Z0-9+-\\.]*:" };
72 
73  auto cleanPath = Util::Strings::trim(path);
74  auto str = std::string(cleanPath);
75 
76  if (Util::Strings::startsWithRegex(str, scheme)) {
77  std::unique_ptr<Uri> uri;
78  fromString(uri, str);
79  return uri;
80  }
81 
82  const UriComponents components(uri);
83 
84  if (Util::Strings::startsWith(str, "/")) {
85  // Absolute path.
86  std::string url = ::toString(components.scheme(), "://", components.host(), str);
87  return std::unique_ptr<Uri>(new Http(url));
88  } else {
89  // Relative path.
90  // TODO normalise path.
91  std::string url = ::toString(components.scheme(), "://", components.host(), "/", components.path(), "/", str);
92  return std::unique_ptr<Uri>(new Http(url));
93  }
94  }
95 
96  public: bool operator == (const Uri & rhs) const override {
97  const auto * o = dynamic_cast<const Http *>(&rhs);
98  return o != nullptr && uri == o->uri;
99  }
100 
101  public: bool canReadFrom() const override {
102  return true;
103  }
104 
105  public: bool canWriteTo() const override {
106  return false;
107  }
108 
109  public: template <typename AllocatorT> Balau::U8String<AllocatorT> toRawString() const {
110  return ::toString<AllocatorT>(uri);
111  }
112 
117  return HttpByteReadResource(*this);
118  }
119 
124  return HttpUtf8To32ReadResource(*this);
125  }
126 
127  public: std::unique_ptr<ByteReadResource> byteReadResource() const override {
128  return std::unique_ptr<ByteReadResource>(new HttpByteReadResource(*this));
129  }
130 
131  public: std::unique_ptr<Utf8To32ReadResource> utf8To32ReadResource() const override {
132  return std::unique_ptr<Utf8To32ReadResource>(new HttpUtf8To32ReadResource(*this));
133  }
134 
135  public: std::unique_ptr<ByteWriteResource> byteWriteResource() override {
136  ThrowBalauException(Exception::NotImplementedException, "HTTP URIs do not have a byte write resource.");
137  }
138 
139  public: std::unique_ptr<Utf32To8WriteResource> utf32To8WriteResource() override {
140  ThrowBalauException(Exception::NotImplementedException, "HTTP URIs do not have a Unicode write resource.");
141  }
142 
143  public: bool isRecursivelyIterable() const override {
144  return false;
145  }
146 
147  public: bool isIterable() const override {
148  return false;
149  }
150 
151  public: std::unique_ptr<RecursiveUriIterator> recursiveIterator() const override {
152  ThrowBalauException(Exception::NotImplementedException, "HTTP URIs do not have recursive iterators.");
153  }
154 
155  public: std::unique_ptr<UriIterator> iterator() const override {
156  ThrowBalauException(Exception::NotImplementedException, "HTTP URIs do not have iterators.");
157  }
158 
159  public: void dispatch(UriDispatcher & dispatcher) const override {
160  dispatcher.dispatch(*this);
161  }
162 };
163 
169 template <typename AllocatorT>
171  return http.toRawString<AllocatorT>();
172 }
173 
174 } // namespace Balau::Resource
175 
176 #endif // COM_BORA_SOFTWARE__BALAU_RESOURCE__HTTP
virtual void dispatch(const File &object)=0
Visit a File URI.
HttpByteReadResource getByteReadResource() const
Get a byte read resource for the HTTP source.
Definition: Http.hpp:116
Http()=default
Create a null HTTP URI.
std::unique_ptr< Uri > append(const std::string &pathComponent) const override
Appends the path component to the supplied URI, returning a new URI.
Definition: Http.hpp:66
std::unique_ptr< Uri > clone() const override
Clone the concrete Uri.
Definition: Http.hpp:62
A read-only HTTP UTF-8 resource which is read as UTF-32 characters.
Definition: HttpUtf8To32ReadResource.hpp:35
#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
A read-only HTTP resource which is read as bytes.
Definition: HttpByteReadResource.hpp:33
An abstract universal resource identifier.
Definition: Uri.hpp:131
STL namespace.
Http(std::string &&uri_)
Create an HTTP URI from the supplied URI string.
Definition: Http.hpp:40
std::unique_ptr< Utf8To32ReadResource > utf8To32ReadResource() const override
Get a UTF-8 to UTF-32 read resource for the URI.
Definition: Http.hpp:131
std::unique_ptr< UriIterator > iterator() const override
Get a (non-recursive) iterator.
Definition: Http.hpp:155
bool operator==(const Uri &rhs) const override
Compare the supplied URI to the current URI.
Definition: Http.hpp:96
The unified resource class hierarchy.
Definition: ByteReadResource.hpp:24
An HTTP URL.
Definition: Http.hpp:31
Thrown when a feature is not yet implemented.
Definition: BalauException.hpp:162
bool isRecursivelyIterable() const override
Does the URI have a recursive iterator (examples: file and zip archive URIs).
Definition: Http.hpp:143
std::string_view host() const
Obtain a string view to the host.
Definition: UriComponents.hpp:205
std::unique_ptr< ByteWriteResource > byteWriteResource() override
Get a byte write resource for the URI.
Definition: Http.hpp:135
Http(std::string_view uri_)
Create an HTTP URI from the supplied URI string.
Definition: Http.hpp:45
std::unique_ptr< ByteReadResource > byteReadResource() const override
Get a byte read resource for the URI.
Definition: Http.hpp:127
Balau::U8String< AllocatorT > toString(const File &file)
Print the file URI as a UTF-8 string.
Definition: File.hpp:724
std::unique_ptr< Uri > resolve(std::string_view path) const override
Resolve the relative or absolute path, in reference to the current URI.
Definition: Http.hpp:70
An abstract Url (either http or https).
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
A read-only HTTP UTF-8 resource which is read as UTF-32 characters.
bool isIterable() const override
Does the URI have a non-recursive iterator (examples: file and zip archive URIs). ...
Definition: Http.hpp:147
Http(const char *uri_)
Create an HTTP URI from the supplied URI string.
Definition: Http.hpp:50
Http(Http &&rhs) noexcept
Create an HTTP URI by moving the contents of the supplied instance.
Definition: Http.hpp:60
A read-only HTTP resource which is read as bytes.
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
void dispatch(UriDispatcher &dispatcher) const override
Visitor pattern dispatching.
Definition: Http.hpp:159
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
HttpUtf8To32ReadResource getUtf8To32ReadResource() const
Get a UTF-8 to UTF-32 read resource for the HTTP source.
Definition: Http.hpp:123
std::unique_ptr< Utf32To8WriteResource > utf32To8WriteResource() override
Get a UTF-32 to UTF-8 write resource for the URI.
Definition: Http.hpp:139
bool canWriteTo() const override
Can data be written to the URI via a write resource.
Definition: Http.hpp:105
A class representing the parsed components of a URI.
std::unique_ptr< RecursiveUriIterator > recursiveIterator() const override
Get a recursive iterator.
Definition: Http.hpp:151
bool canReadFrom() const override
Can data be read from the URI via a read resource.
Definition: Http.hpp:101
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
Balau::U8String< AllocatorT > toRawString() const
Get a string representing the raw URI.
Definition: Http.hpp:109