Url.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__URL
18 #define COM_BORA_SOFTWARE__BALAU_RESOURCE__URL
19 
20 #include <Balau/Resource/Uri.hpp>
22 
23 namespace Balau::Resource {
24 
28 class Url : public Uri {
29  public: bool isRegularDirectory() const override {
30  return false;
31  }
32 
33  public: bool isRegularFile() const override {
34  return false;
35  }
36 
37  public: std::string toUriString() const override {
38  return uri;
39  }
40 
41  public: std::string toRawString() const override {
42  return uri;
43  }
44 
45  public: size_t hashcode() const noexcept override {
46  return std::hash<std::string>()(uri);
47  }
48 
50 
51  protected: Url() = default;
52 
53  protected: explicit Url(std::string && uri_) : uri(std::move(uri_)) {}
54 
55  protected: explicit Url(std::string_view uri_) : uri(std::string(uri_)) {}
56 
57  protected: explicit Url(const char * uri_) : uri(std::string(uri_)) {}
58 
59  protected: Url(const Url & copy) : uri(copy.uri) {}
60 
61  protected: Url(Url && rhs) noexcept : uri(std::move(rhs.uri)) {}
62 
63  protected: std::string appendPathComponent(const std::string & pathComponent) const {
64  const UriComponents components(uri);
65  // TODO normalise path.
66 
67  std::string url = ::toString(
68  components.scheme(), "://", components.host(), components.path(), "/", pathComponent
69  );
70 
71  if (components.hasQuery()) {
72  url += "?";
73  url += components.query();
74  }
75 
76  if (components.hasFragment()) {
77  url += "#";
78  url += components.fragment();
79  }
80 
81  return url;
82  }
83 
84 
85  protected: std::string uri;
86 };
87 
88 } // namespace Balau::Resource
89 
90 #endif // COM_BORA_SOFTWARE__BALAU_RESOURCE__URL
std::string_view fragment() const
Obtain a string view to the fragment.
Definition: UriComponents.hpp:272
size_t hashcode() const noexcept override
Get the URI&#39;s hash code.
Definition: Url.hpp:45
An abstract universal resource identifier.
Definition: Uri.hpp:131
The unified resource class hierarchy.
Definition: ByteReadResource.hpp:24
std::string_view query() const
Obtain a string view to the query.
Definition: UriComponents.hpp:252
bool hasFragment() const
Returns true if the URI has a fragment.
Definition: UriComponents.hpp:168
std::string_view host() const
Obtain a string view to the host.
Definition: UriComponents.hpp:205
std::string toUriString() const override
Get a string representing the URI, complete with scheme.
Definition: Url.hpp:37
bool isRegularDirectory() const override
Returns true if the URI points to a file directory.
Definition: Url.hpp:29
Balau::U8String< AllocatorT > toString(const File &file)
Print the file URI as a UTF-8 string.
Definition: File.hpp:724
std::string_view scheme() const
Obtain a string view to the scheme.
Definition: UriComponents.hpp:177
std::string toRawString() const override
Get a string representing the raw URI.
Definition: Url.hpp:41
bool hasQuery() const
Returns true if the URI has a query.
Definition: UriComponents.hpp:159
std::string_view path() const
Obtain a string view to the path.
Definition: UriComponents.hpp:234
bool isRegularFile() const override
Returns true if the URI is a regular file.
Definition: Url.hpp:33
A class representing the parsed components of a URI.
The abstract URI base class.
An abstract Url (either http or https).
Definition: Url.hpp:28
Parses a URI and provides views into the URI&#39;s components.
Definition: UriComponents.hpp:52