nlib
HttpStyleUri.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_HTTPSTYLEURI_H_
4 #define INCLUDE_NN_NLIB_HTTPSTYLEURI_H_
5 
6 #include <utility>
7 
8 #include "nn/nlib/UniquePtr.h"
9 #include "nn/nlib/Uri.h"
10 
11 NLIB_NAMESPACE_BEGIN
12 
14  public:
15  typedef std::pair<const char*, const char*> KeyValue;
16  // cppcheck-suppress uninitMemberVar
17  HttpStyleUri() NLIB_NOEXCEPT { this->Reset(); }
19  NLIB_VIS_PUBLIC bool FromUri(const Uri& uri) NLIB_NOEXCEPT;
20  NLIB_VIS_PUBLIC bool ToUri(Uri* uri) const NLIB_NOEXCEPT;
21  NLIB_VIS_PUBLIC bool Parse(const char* str) NLIB_NOEXCEPT;
22  NLIB_VIS_PUBLIC bool ComposeString(char* buf, size_t size) const NLIB_NOEXCEPT;
23  template <size_t N>
24  bool ComposeString(char (&buf)[N]) const NLIB_NOEXCEPT {
25  return this->ComposeString(buf, N);
26  }
27  NLIB_VIS_PUBLIC bool SetScheme(const char* scheme) NLIB_NOEXCEPT;
28  NLIB_VIS_PUBLIC bool SetUserInfo(const char* userinfo) NLIB_NOEXCEPT;
29  NLIB_VIS_PUBLIC bool SetHost(const char* host) NLIB_NOEXCEPT;
30  NLIB_VIS_PUBLIC bool SetPort(int port) NLIB_NOEXCEPT;
31  NLIB_VIS_PUBLIC bool SetPath(const char* path) NLIB_NOEXCEPT;
32  bool SetQuery(const char* query) NLIB_NOEXCEPT { return m_Query.Set(query); }
33  NLIB_VIS_PUBLIC bool SetFragment(const char* fragment) NLIB_NOEXCEPT;
34  bool AddQuery(const char* key, const char* value) NLIB_NOEXCEPT {
35  return m_Query.Add(key, value);
36  }
37  const char* GetScheme() const NLIB_NOEXCEPT {
38  return m_IsSchemeHttps ? "https" : "http";
39  }
40  const char* GetUserInfo() const NLIB_NOEXCEPT { return m_UserInfo.get(); }
41  const char* GetHost() const NLIB_NOEXCEPT { return m_Host.get(); }
42  int GetPort() const NLIB_NOEXCEPT { return m_Port; }
43  size_t GetNumSegment() const NLIB_NOEXCEPT {
44  return m_Segments.GetNumSegment();
45  }
46  const char* GetSegment(size_t i) const NLIB_NOEXCEPT {
47  return m_Segments.GetSegment(i);
48  }
49  bool IsPathDirectory() const NLIB_NOEXCEPT {
50  return m_Segments.IsDirectory();
51  }
52  const char* GetQueryValue(const char* key) const NLIB_NOEXCEPT {
53  return m_Query.GetValue(key);
54  }
55  size_t GetNumQuery() const NLIB_NOEXCEPT { return m_Query.GetNumQuery(); }
56  const KeyValue* GetQueryKeyValue(size_t i) const NLIB_NOEXCEPT {
57  return m_Query.GetKeyValue(i);
58  }
59  const char* GetFragment() const NLIB_NOEXCEPT { return m_Fragment.get(); }
60  NLIB_VIS_PUBLIC bool AddBaseUri(const Uri& relative, const HttpStyleUri& base) NLIB_NOEXCEPT;
61  NLIB_VIS_PUBLIC void Reset() NLIB_NOEXCEPT;
62 
63  private:
64  bool ComposeQuery(char* first, char* last) const NLIB_NOEXCEPT {
65  return m_Query.Compose(&first, last);
66  }
67 
68  private:
69  bool m_IsSchemeHttps;
70  int m_Port;
71  UniquePtr<char[]> m_UserInfo;
72  UniquePtr<char[]> m_Host;
73  UniquePtr<char[]> m_Fragment;
74  detail::Segments m_Segments;
75  detail::Query m_Query;
76 };
77 
78 NLIB_NAMESPACE_END
79 #endif // INCLUDE_NN_NLIB_HTTPSTYLEURI_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
const KeyValue * GetQueryKeyValue(size_t i) const noexcept
Specifies an index and gets a key/value pair. If not found, returns NULL.
Definition: HttpStyleUri.h:56
bool AddQuery(const char *key, const char *value) noexcept
Adds a key and value to a query.
Definition: HttpStyleUri.h:34
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
bool SetQuery(const char *query) noexcept
Sets a query based on a query string.
Definition: HttpStyleUri.h:32
Defines that class that is corresponding to std::unique_ptr.
The class for parsing URIs that use the http and https schemes, and for constructing URI strings...
Definition: HttpStyleUri.h:13
size_t GetNumQuery() const noexcept
Gets the value corresponding to the key. If not found, returns NULL.
Definition: HttpStyleUri.h:55
const char * GetScheme() const noexcept
Gets the name of the URI scheme. The name is either http or https.
Definition: HttpStyleUri.h:37
std::pair< const char *, const char * > KeyValue
The type that stores query keys and values.
Definition: HttpStyleUri.h:15
HttpStyleUri() noexcept
Instantiates the object with default parameters (default constructor). Initializes an object...
Definition: HttpStyleUri.h:17
int GetPort() const noexcept
Gets the port number.
Definition: HttpStyleUri.h:42
const char * GetSegment(size_t i) const noexcept
Specifies an index and gets a segment.
Definition: HttpStyleUri.h:46
const char * GetUserInfo() const noexcept
Gets the user information.
Definition: HttpStyleUri.h:40
const char * GetHost() const noexcept
Gets the hostname.
Definition: HttpStyleUri.h:41
bool IsPathDirectory() const noexcept
Gets whether the path is in the form of a directory path.
Definition: HttpStyleUri.h:49
The class for parsing and constructing regular URIs.
Definition: Uri.h:18
size_t GetNumSegment() const noexcept
Gets the number of stored segments.
Definition: HttpStyleUri.h:43
Defines the class for handling URIs.
const char * GetFragment() const noexcept
Gets a fragment string.
Definition: HttpStyleUri.h:59
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
const char * GetQueryValue(const char *key) const noexcept
Specifies a key and gets a value. If not found, returns NULL.
Definition: HttpStyleUri.h:52