DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
UriBuilder.cpp
Go to the documentation of this file.
1#include "pch.h"
2#include "System/UriBuilder.h"
3#include <string>
4
5namespace DotNetDupe {
6 namespace System {
7 UriBuilder::UriBuilder() : _port(-1) {}
8 UriBuilder::UriBuilder(const String& uri) : _uri(uri), _port(-1) {
9 // Simple parsing to initialize fields if needed
10 }
12 : _scheme(uri.GetScheme()), _host(uri.GetHost()), _port(uri.GetPort()),
13 _path(uri.GetAbsolutePath()), _query(uri.GetQuery()), _fragment(uri.GetFragment()) {
14 _uri = uri.ToString();
15 }
16 UriBuilder::UriBuilder(const String& scheme, const String& host)
17 : _scheme(scheme), _host(host), _port(-1) {}
18 UriBuilder::UriBuilder(const String& scheme, const String& host, int port)
19 : _scheme(scheme), _host(host), _port(port) {}
20
21 String UriBuilder::GetScheme() const { return _scheme; }
22 void UriBuilder::SetScheme(const String& value) { _scheme = value; }
23
24 String UriBuilder::GetHost() const { return _host; }
25 void UriBuilder::SetHost(const String& value) { _host = value; }
26
27 int UriBuilder::GetPort() const { return _port; }
28 void UriBuilder::SetPort(int value) { _port = value; }
29
30 String UriBuilder::GetPath() const { return _path; }
31 void UriBuilder::SetPath(const String& value) { _path = value; }
32
33 String UriBuilder::GetQuery() const { return _query; }
34 void UriBuilder::SetQuery(const String& value) { _query = value; }
35
36 String UriBuilder::GetFragment() const { return _fragment; }
37 void UriBuilder::SetFragment(const String& value) { _fragment = value; }
38
39 String UriBuilder::GetUserName() const { return _userName; }
40 void UriBuilder::SetUserName(const String& value) { _userName = value; }
41
42 String UriBuilder::GetPassword() const { return _password; }
43 void UriBuilder::SetPassword(const String& value) { _password = value; }
44
47 return Uri(ToString());
48 }
49
50 static void AppendAuthority(std::string& result, const String& host, const String& user, const String& pass, int port) {
52 if (host.IsEmpty()) return;
53 result += "//";
54
56 if (!user.IsEmpty()) {
57 result += (const char*)user;
58 if (!pass.IsEmpty()) {
59 result += ":"; result += (const char*)pass;
60 }
61 result += "@";
62 }
63
65 result += (const char*)host;
66 if (port != -1) {
67 result += ":"; result += std::to_string(port);
68 }
69 }
70
71 static void AppendPathAndQuery(std::string& result, const String& path, const String& query, const String& fragment) {
73 if (!path.IsEmpty()) {
74 if (path[0] != '/') result += "/";
75 result += (const char*)path;
76 }
77
79 if (!query.IsEmpty()) {
80 if (query[0] != '?') result += "?";
81 result += (const char*)query;
82 }
83
85 if (!fragment.IsEmpty()) {
86 if (fragment[0] != '#') result += "#";
87 result += (const char*)fragment;
88 }
89 }
90
93 std::string result;
94 if (!_scheme.IsEmpty()) {
95 result += (const char*)_scheme; result += ":";
96 }
97
99 AppendAuthority(result, _host, _userName, _password, _port);
100 AppendPathAndQuery(result, _path, _query, _fragment);
101 return String(result.c_str());
102 }
103 }
104}
Provides a custom constructor for uniform resource identifiers (URIs) and modifies URIs for the Uri c...
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
String()
Initializes a new instance of the String class to an empty string.
Definition String.cpp:64
void SetScheme(const String &value)
Sets the scheme name of the URI.
String GetHost() const
Gets the Domain Name System (DNS) host name or IP address of a server.
String GetFragment() const
Gets the fragment portion of the URI.
String ToString()
Formats the configured components into a canonical URI string.
int GetPort() const
Gets the port number of the URI.
void SetHost(const String &value)
Sets the Domain Name System (DNS) host name or IP address of a server.
void SetFragment(const String &value)
Sets the fragment portion of the URI.
void SetPath(const String &value)
Sets the path to the resource referenced by the URI.
String GetPath() const
Gets the path to the resource referenced by the URI.
void SetPort(int value)
Sets the port number of the URI.
String GetScheme() const
Gets the scheme name of the URI.
String GetUserName() const
Gets the user name associated with the user that accesses the URI.
void SetUserName(const String &value)
Sets the user name associated with the user that accesses the URI.
String GetQuery() const
Gets any query information included in the URI.
String GetPassword() const
Gets the password associated with the user that accesses the URI.
void SetPassword(const String &value)
Sets the password associated with the user that accesses the URI.
void SetQuery(const String &value)
Sets any query information included in the URI.
Uri GetUri()
Gets the Uri instance constructed by the specified UriBuilder instance.
UriBuilder()
Initializes a new instance of the UriBuilder class.
Definition UriBuilder.cpp:7
Represents an object representation of a Uniform Resource Identifier (URI) and provides easy access t...
Definition Uri.h:21
Uri(const String &uriString)
Initializes a new instance of the Uri class with the specified URI string.
Definition Uri.cpp:9
String ToString() const
Gets the canonical string representation for the specified Uri instance.
Definition Uri.cpp:113
static void AppendPathAndQuery(std::string &result, const String &path, const String &query, const String &fragment)
static void AppendAuthority(std::string &result, const String &host, const String &user, const String &pass, int port)