proxygen
URL.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11 
12 #include <sstream>
13 
15 
16 namespace proxygen {
17 
22 class URL {
23  public:
24  explicit URL(const std::string& url = "") noexcept {
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  }
50 
52  const std::string& scheme,
53  const std::string& hostAndPort,
54  const std::string& path,
55  const std::string& query,
56  const std::string& fragment) noexcept {
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  }
67 
68  URL(const std::string scheme,
69  const std::string host,
70  uint16_t port = 0,
71  const std::string path = "",
72  const std::string query = "",
73  const std::string fragment = "") noexcept :
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  }
93 
95  return valid_;
96  }
97 
99  return url_;
100  }
101 
103  return port_;
104  }
105 
107  return scheme_;
108  }
109 
111  return scheme_ == "https";
112  }
113 
115  return valid_ && !host_.empty();
116  }
117 
119  return host_;
120  }
121 
123  return port_ ? folly::to<std::string>(host_, ":", port_) : host_;
124  }
125 
127  return path_;
128  }
129 
131  return query_;
132  }
133 
135  return fragment_;
136  }
137 
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  }
145 
146  friend bool operator==(const URL& lhs, const URL& rhs) {
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  }
155 
156  friend bool operator!=(const URL& lhs, const URL& rhs) {
157  return !(lhs == rhs);
158  }
159 
160  private:
167 
169 
170  /* Does this represent a valid URL */
171  bool valid_{false};
172 };
173 
174 } // namespace proxygen
URL(const std::string &url="") noexcept
Definition: URL.h:24
std::string url_
Definition: URL.h:168
const std::string & getHost() const noexcept
Definition: URL.h:118
folly::StringPiece query() const
Definition: ParseURL.h:72
std::string str() const
Definition: Range.h:591
std::string makeRelativeURL() const noexcept
Definition: URL.h:138
folly::StringPiece url() const
Definition: ParseURL.h:36
bool isValid() const noexcept
Definition: URL.h:94
std::string host_
Definition: URL.h:162
folly::StringPiece hostNoBrackets()
Definition: ParseURL.h:84
const std::string & getUrl() const noexcept
Definition: URL.h:98
requires E e noexcept(noexcept(s.error(std::move(e))))
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
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649
const std::string & getPath() const noexcept
Definition: URL.h:126
folly::StringPiece path() const
Definition: ParseURL.h:68
bool hasHost() const noexcept
Definition: URL.h:114
friend bool operator!=(const URL &lhs, const URL &rhs)
Definition: URL.h:156
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
Definition: URL.h:68
const std::string & getQuery() const noexcept
Definition: URL.h:130
std::string path_
Definition: URL.h:164
uint16_t port() const
Definition: ParseURL.h:56
bool isSecure() const noexcept
Definition: URL.h:110
std::string fragment_
Definition: URL.h:166
std::string scheme_
Definition: URL.h:161
const char * string
Definition: Conv.cpp:212
std::string query_
Definition: URL.h:165
friend bool operator==(const URL &lhs, const URL &rhs)
Definition: URL.h:146
const
Definition: upload.py:398
const std::string & getScheme() const noexcept
Definition: URL.h:106
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
uint16_t getPort() const noexcept
Definition: URL.h:102
folly::StringPiece scheme() const
Definition: ParseURL.h:40
const std::string & getFragment() const noexcept
Definition: URL.h:134
folly::StringPiece fragment() const
Definition: ParseURL.h:76