proxygen
proxygen::URL Class Reference

#include <URL.h>

Public Member Functions

 URL (const std::string &url="") noexcept
 
 URL (const std::string scheme, const std::string host, uint16_t port=0, const std::string path="", const std::string query="", const std::string fragment="") noexcept
 
bool isValid () const noexcept
 
const std::stringgetUrl () const noexcept
 
uint16_t getPort () const noexcept
 
const std::stringgetScheme () const noexcept
 
bool isSecure () const noexcept
 
bool hasHost () const noexcept
 
const std::stringgetHost () const noexcept
 
std::string getHostAndPort () const noexcept
 
const std::stringgetPath () const noexcept
 
const std::stringgetQuery () const noexcept
 
const std::stringgetFragment () const noexcept
 
std::string makeRelativeURL () const noexcept
 

Static Public Member Functions

static std::string createUrl (const std::string &scheme, const std::string &hostAndPort, const std::string &path, const std::string &query, const std::string &fragment) noexcept
 

Private Attributes

std::string scheme_
 
std::string host_
 
uint16_t port_
 
std::string path_
 
std::string query_
 
std::string fragment_
 
std::string url_
 
bool valid_ {false}
 

Friends

bool operator== (const URL &lhs, const URL &rhs)
 
bool operator!= (const URL &lhs, const URL &rhs)
 

Detailed Description

Struct representing a URL.

Definition at line 22 of file URL.h.

Constructor & Destructor Documentation

proxygen::URL::URL ( const std::string url = "")
inlineexplicitnoexcept

Definition at line 24 of file URL.h.

References proxygen::ParseURL::fragment(), fragment_, host_, proxygen::ParseURL::hostNoBrackets(), isSecure(), proxygen::ParseURL::path(), path_, proxygen::ParseURL::port(), port_, proxygen::ParseURL::query(), query_, proxygen::ParseURL::scheme(), scheme_, folly::Range< Iter >::str(), folly::pushmi::operators::transform, proxygen::ParseURL::url(), url_, and valid_.

24  {
25  valid_ = false;
26 
27  ParseURL parseUrl(url);
28 
29  scheme_ = parseUrl.scheme().str();
30  host_ = parseUrl.hostNoBrackets().str();
31  path_ = parseUrl.path().str();
32  query_ = parseUrl.query().str();
33  fragment_ = parseUrl.fragment().str();
34  url_ = parseUrl.url().str();
35 
36  scheme_ = parseUrl.scheme().str();
37  std::transform(scheme_.begin(),
38  scheme_.end(),
39  scheme_.begin(),
40  ::tolower);
41  valid_ = (scheme_ == "http" || scheme_ == "https");
42 
43  if (parseUrl.port()) {
44  port_ = parseUrl.port();
45  } else {
46  port_ = isSecure() ? 443 : 80;
47  }
48 
49  }
std::string url_
Definition: URL.h:168
std::string host_
Definition: URL.h:162
PUSHMI_INLINE_VAR constexpr detail::transform_fn transform
Definition: transform.h:158
uint16_t port_
Definition: URL.h:163
std::string path_
Definition: URL.h:164
bool isSecure() const noexcept
Definition: URL.h:110
std::string fragment_
Definition: URL.h:166
std::string scheme_
Definition: URL.h:161
std::string query_
Definition: URL.h:165
bool valid_
Definition: URL.h:171
proxygen::URL::URL ( const std::string  scheme,
const std::string  host,
uint16_t  port = 0,
const std::string  path = "",
const std::string  query = "",
const std::string  fragment = "" 
)
inlinenoexcept

Definition at line 68 of file URL.h.

References isSecure(), port_, folly::pushmi::operators::transform, and valid_.

73  :
74  scheme_(scheme),
75  host_(host),
76  port_(port),
77  path_(path),
78  query_(query),
79  fragment_(fragment),
81 
82  std::transform(scheme_.begin(),
83  scheme_.end(),
84  scheme_.begin(),
85  ::tolower);
86 
87  valid_ = (scheme == "http" || scheme == "https");
88 
89  if (port_ == 0) {
90  port_ = isSecure() ? 443 : 80;
91  }
92  }
std::string url_
Definition: URL.h:168
std::string host_
Definition: URL.h:162
PUSHMI_INLINE_VAR constexpr detail::transform_fn transform
Definition: transform.h:158
uint16_t port_
Definition: URL.h:163
std::string getHostAndPort() const noexcept
Definition: URL.h:122
std::string path_
Definition: URL.h:164
bool isSecure() const noexcept
Definition: URL.h:110
std::string fragment_
Definition: URL.h:166
std::string scheme_
Definition: URL.h:161
std::string query_
Definition: URL.h:165
bool valid_
Definition: URL.h:171
static std::string createUrl(const std::string &scheme, const std::string &hostAndPort, const std::string &path, const std::string &query, const std::string &fragment) noexcept
Definition: URL.h:51

Member Function Documentation

static std::string proxygen::URL::createUrl ( const std::string scheme,
const std::string hostAndPort,
const std::string path,
const std::string query,
const std::string fragment 
)
inlinestaticnoexcept

Definition at line 51 of file URL.h.

56  {
57  std::ostringstream out;
58  out << scheme << "://" << hostAndPort << '/' << path;
59  if (!query.empty()) {
60  out << '?' << query;
61  }
62  if (!fragment.empty()) {
63  out << '#' << fragment;
64  }
65  return out.str();
66  }
const std::string& proxygen::URL::getFragment ( ) const
inlinenoexcept

Definition at line 134 of file URL.h.

References fragment_.

134  {
135  return fragment_;
136  }
std::string fragment_
Definition: URL.h:166
const std::string& proxygen::URL::getHost ( ) const
inlinenoexcept

Definition at line 118 of file URL.h.

References host_.

Referenced by CurlService::CurlClient::getServerName(), and main().

118  {
119  return host_;
120  }
std::string host_
Definition: URL.h:162
std::string proxygen::URL::getHostAndPort ( ) const
inlinenoexcept

Definition at line 122 of file URL.h.

References host_, and port_.

Referenced by CurlService::CurlClient::connectError(), and CurlService::CurlClient::sendRequest().

122  {
123  return port_ ? folly::to<std::string>(host_, ":", port_) : host_;
124  }
std::string host_
Definition: URL.h:162
uint16_t port_
Definition: URL.h:163
const std::string& proxygen::URL::getPath ( ) const
inlinenoexcept

Definition at line 126 of file URL.h.

References path_.

126  {
127  return path_;
128  }
std::string path_
Definition: URL.h:164
uint16_t proxygen::URL::getPort ( ) const
inlinenoexcept

Definition at line 102 of file URL.h.

References port_.

Referenced by main().

102  {
103  return port_;
104  }
uint16_t port_
Definition: URL.h:163
const std::string& proxygen::URL::getQuery ( ) const
inlinenoexcept

Definition at line 130 of file URL.h.

References query_.

130  {
131  return query_;
132  }
std::string query_
Definition: URL.h:165
const std::string& proxygen::URL::getScheme ( ) const
inlinenoexcept

Definition at line 106 of file URL.h.

References scheme_.

106  {
107  return scheme_;
108  }
std::string scheme_
Definition: URL.h:161
const std::string& proxygen::URL::getUrl ( ) const
inlinenoexcept

Definition at line 98 of file URL.h.

References url_.

Referenced by CurlService::CurlClient::CurlClient(), and CurlService::CurlClient::sendRequest().

98  {
99  return url_;
100  }
std::string url_
Definition: URL.h:168
bool proxygen::URL::hasHost ( ) const
inlinenoexcept

Definition at line 114 of file URL.h.

References host_, and valid_.

114  {
115  return valid_ && !host_.empty();
116  }
std::string host_
Definition: URL.h:162
bool valid_
Definition: URL.h:171
bool proxygen::URL::isSecure ( ) const
inlinenoexcept

Definition at line 110 of file URL.h.

References scheme_.

Referenced by CurlService::CurlClient::connectSuccess(), main(), CurlService::CurlClient::sendRequest(), and URL().

110  {
111  return scheme_ == "https";
112  }
std::string scheme_
Definition: URL.h:161
bool proxygen::URL::isValid ( ) const
inlinenoexcept

Definition at line 94 of file URL.h.

References valid_.

94  {
95  return valid_;
96  }
bool valid_
Definition: URL.h:171
std::string proxygen::URL::makeRelativeURL ( ) const
inlinenoexcept

Definition at line 138 of file URL.h.

References fragment_, path_, and query_.

Referenced by CurlService::CurlClient::sendRequest().

138  {
139  return folly::to<std::string>(
140  path_.empty() ? "/" : path_,
141  query_.empty() ? "" : folly::to<std::string>('?', query_),
142  fragment_.empty() ? "" : folly::to<std::string>('#', fragment_));
143 
144  }
std::string path_
Definition: URL.h:164
std::string fragment_
Definition: URL.h:166
std::string query_
Definition: URL.h:165

Friends And Related Function Documentation

bool operator!= ( const URL lhs,
const URL rhs 
)
friend

Definition at line 156 of file URL.h.

156  {
157  return !(lhs == rhs);
158  }
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649
bool operator== ( const URL lhs,
const URL rhs 
)
friend

Definition at line 146 of file URL.h.

146  {
147  return lhs.getScheme() == rhs.getScheme() &&
148  lhs.getHost() == rhs.getHost() &&
149  lhs.getPort() == rhs.getPort() &&
150  lhs.getPath() == rhs.getPath() &&
151  lhs.getQuery() == rhs.getQuery() &&
152  lhs.getFragment() == rhs.getFragment() &&
153  lhs.getUrl() == rhs.getUrl();
154  }
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649

Member Data Documentation

std::string proxygen::URL::fragment_
private

Definition at line 166 of file URL.h.

Referenced by getFragment(), makeRelativeURL(), and URL().

std::string proxygen::URL::host_
private

Definition at line 162 of file URL.h.

Referenced by getHost(), getHostAndPort(), hasHost(), and URL().

std::string proxygen::URL::path_
private

Definition at line 164 of file URL.h.

Referenced by getPath(), makeRelativeURL(), and URL().

uint16_t proxygen::URL::port_
private

Definition at line 163 of file URL.h.

Referenced by getHostAndPort(), getPort(), and URL().

std::string proxygen::URL::query_
private

Definition at line 165 of file URL.h.

Referenced by getQuery(), makeRelativeURL(), and URL().

std::string proxygen::URL::scheme_
private

Definition at line 161 of file URL.h.

Referenced by getScheme(), isSecure(), and URL().

std::string proxygen::URL::url_
private

Definition at line 168 of file URL.h.

Referenced by getUrl(), and URL().

bool proxygen::URL::valid_ {false}
private

Definition at line 171 of file URL.h.

Referenced by hasHost(), isValid(), and URL().


The documentation for this class was generated from the following file: