proxygen
folly::Uri Class Reference

#include <Uri.h>

Public Member Functions

 Uri (StringPiece str)
 
const std::stringscheme () const
 
const std::stringusername () const
 
const std::stringpassword () const
 
const std::stringhost () const
 
std::string hostname () const
 
uint16_t port () const
 
const std::stringpath () const
 
const std::stringquery () const
 
const std::stringfragment () const
 
std::string authority () const
 
template<class String >
String toString () const
 
std::string str () const
 
fbstring fbstr () const
 
void setPort (uint16_t port)
 
const std::vector< std::pair< std::string, std::string > > & getQueryParams ()
 

Private Attributes

std::string scheme_
 
std::string username_
 
std::string password_
 
std::string host_
 
bool hasAuthority_
 
uint16_t port_
 
std::string path_
 
std::string query_
 
std::string fragment_
 
std::vector< std::pair< std::string, std::string > > queryParams_
 

Detailed Description

Class representing a URI.

Consider http://www.facebook.com/foo/bar?key=foo#anchor

The URI is broken down into its parts: scheme ("http"), authority (ie. host and port, in most cases: "www.facebook.com"), path ("/foo/bar"), query ("key=foo") and fragment ("anchor"). The scheme is lower-cased.

If this Uri represents a URL, note that, to prevent ambiguity, the component parts are NOT percent-decoded; you should do this yourself with uriUnescape() (for the authority and path) and uriUnescape(..., UriEscapeMode::QUERY) (for the query, but probably only after splitting at '&' to identify the individual parameters).

Definition at line 43 of file Uri.h.

Constructor & Destructor Documentation

folly::Uri::Uri ( StringPiece  str)
explicit

Parse a Uri from a string. Throws std::invalid_argument on parse error.

Definition at line 35 of file Uri.cpp.

References authority(), folly::Range< Iter >::begin(), folly::Range< Iter >::empty(), folly::Range< Iter >::end(), folly::gen::first, fragment_, hasAuthority_, host_, password_, path_, port(), port_, query_, regex, scheme_, folly::pushmi::operators::transform, UNLIKELY, and username_.

35  : hasAuthority_(false), port_(0) {
36  static const boost::regex uriRegex(
37  "([a-zA-Z][a-zA-Z0-9+.-]*):" // scheme:
38  "([^?#]*)" // authority and path
39  "(?:\\?([^#]*))?" // ?query
40  "(?:#(.*))?"); // #fragment
41  static const boost::regex authorityAndPathRegex("//([^/]*)(/.*)?");
42 
43  boost::cmatch match;
44  if (UNLIKELY(!boost::regex_match(str.begin(), str.end(), match, uriRegex))) {
45  throw std::invalid_argument(to<std::string>("invalid URI ", str));
46  }
47 
48  scheme_ = submatch(match, 1);
49  std::transform(scheme_.begin(), scheme_.end(), scheme_.begin(), ::tolower);
50 
51  StringPiece authorityAndPath(match[2].first, match[2].second);
52  boost::cmatch authorityAndPathMatch;
53  if (!boost::regex_match(
54  authorityAndPath.begin(),
55  authorityAndPath.end(),
56  authorityAndPathMatch,
57  authorityAndPathRegex)) {
58  // Does not start with //, doesn't have authority
59  hasAuthority_ = false;
60  path_ = authorityAndPath.str();
61  } else {
62  static const boost::regex authorityRegex(
63  "(?:([^@:]*)(?::([^@]*))?@)?" // username, password
64  "(\\[[^\\]]*\\]|[^\\[:]*)" // host (IP-literal (e.g. '['+IPv6+']',
65  // dotted-IPv4, or named host)
66  "(?::(\\d*))?"); // port
67 
68  const auto authority = authorityAndPathMatch[1];
69  boost::cmatch authorityMatch;
70  if (!boost::regex_match(
71  authority.first,
72  authority.second,
73  authorityMatch,
74  authorityRegex)) {
75  throw std::invalid_argument(to<std::string>(
76  "invalid URI authority ",
77  StringPiece(authority.first, authority.second)));
78  }
79 
80  StringPiece port(authorityMatch[4].first, authorityMatch[4].second);
81  if (!port.empty()) {
82  port_ = to<uint16_t>(port);
83  }
84 
85  hasAuthority_ = true;
86  username_ = submatch(authorityMatch, 1);
87  password_ = submatch(authorityMatch, 2);
88  host_ = submatch(authorityMatch, 3);
89  path_ = submatch(authorityAndPathMatch, 2);
90  }
91 
92  query_ = submatch(match, 3);
93  fragment_ = submatch(match, 4);
94 }
std::string path_
Definition: Uri.h:135
std::string username_
Definition: Uri.h:130
uint16_t port_
Definition: Uri.h:134
regex
Definition: CMakeCache.txt:563
std::string query_
Definition: Uri.h:136
PUSHMI_INLINE_VAR constexpr detail::transform_fn transform
Definition: transform.h:158
uint16_t port() const
Definition: Uri.h:76
std::string str() const
Definition: Uri.h:94
bool hasAuthority_
Definition: Uri.h:133
std::string host_
Definition: Uri.h:132
std::string authority() const
Definition: Uri.cpp:96
std::string fragment_
Definition: Uri.h:137
Range< const char * > StringPiece
#define UNLIKELY(x)
Definition: Likely.h:48
std::string password_
Definition: Uri.h:131
std::string scheme_
Definition: Uri.h:129
constexpr detail::First first
Definition: Base-inl.h:2553

Member Function Documentation

std::string folly::Uri::authority ( ) const

Definition at line 96 of file Uri.cpp.

References folly::empty(), host(), password(), port(), folly::size(), string, folly::toAppend(), and username().

Referenced by fragment(), TEST(), and Uri().

96  {
97  std::string result;
98 
99  // Port is 5 characters max and we have up to 3 delimiters.
100  result.reserve(host().size() + username().size() + password().size() + 8);
101 
102  if (!username().empty() || !password().empty()) {
103  result.append(username());
104 
105  if (!password().empty()) {
106  result.push_back(':');
107  result.append(password());
108  }
109 
110  result.push_back('@');
111  }
112 
113  result.append(host());
114 
115  if (port() != 0) {
116  result.push_back(':');
117  toAppend(port(), &result);
118  }
119 
120  return result;
121 }
const std::string & password() const
Definition: Uri.h:56
const std::string & username() const
Definition: Uri.h:53
uint16_t port() const
Definition: Uri.h:76
const std::string & host() const
Definition: Uri.h:63
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
void toAppend(char value, Tgt *result)
Definition: Conv.h:406
const char * string
Definition: Conv.cpp:212
fbstring folly::Uri::fbstr ( ) const
inline

Definition at line 97 of file Uri.h.

Referenced by TEST().

97  {
98  return toString<fbstring>();
99  }
const std::string& folly::Uri::fragment ( ) const
inline

Definition at line 85 of file Uri.h.

References authority(), fragment_, string, and toString().

Referenced by folly::uri_detail::as_tuple(), and TEST().

85  {
86  return fragment_;
87  }
std::string fragment_
Definition: Uri.h:137
const std::vector< std::pair< std::string, std::string > > & folly::Uri::getQueryParams ( )

Get query parameters as key-value pairs. e.g. for URI containing query string: key1=foo&key2=&key3&=bar&=bar= In returned list, there are 3 entries: "key1" => "foo" "key2" => "" "key3" => "" Parts "=bar" and "=bar=" are ignored, as they are not valid query parameters. "=bar" is missing parameter name, while "=bar=" has more than one equal signs, we don't know which one is the delimiter for key and value.

Note, this method is not thread safe, it might update internal state, but only the first call to this method update the state. After the first call is finished, subsequent calls to this method are thread safe.

Returns
query parameter key-value pairs in a vector, each element is a pair of which the first element is parameter name and the second one is parameter value

Definition at line 132 of file Uri.cpp.

References folly::gen::first, query_, queryParams_, regex, and string.

Referenced by BENCHMARK(), setPort(), and TEST().

132  {
133  if (!query_.empty() && queryParams_.empty()) {
134  // Parse query string
135  static const boost::regex queryParamRegex(
136  "(^|&)" /*start of query or start of parameter "&"*/
137  "([^=&]*)=?" /*parameter name and "=" if value is expected*/
138  "([^=&]*)" /*parameter value*/
139  "(?=(&|$))" /*forward reference, next should be end of query or
140  start of next parameter*/);
141  const boost::cregex_iterator paramBeginItr(
142  query_.data(), query_.data() + query_.size(), queryParamRegex);
143  boost::cregex_iterator paramEndItr;
144  for (auto itr = paramBeginItr; itr != paramEndItr; ++itr) {
145  if (itr->length(2) == 0) {
146  // key is empty, ignore it
147  continue;
148  }
149  queryParams_.emplace_back(
150  std::string((*itr)[2].first, (*itr)[2].second), // parameter name
151  std::string((*itr)[3].first, (*itr)[3].second) // parameter value
152  );
153  }
154  }
155  return queryParams_;
156 }
regex
Definition: CMakeCache.txt:563
std::string query_
Definition: Uri.h:136
const char * string
Definition: Conv.cpp:212
std::vector< std::pair< std::string, std::string > > queryParams_
Definition: Uri.h:138
constexpr detail::First first
Definition: Base-inl.h:2553
const std::string& folly::Uri::host ( ) const
inline

Get host part of URI. If host is an IPv6 address, square brackets will be returned, for example: "[::1]".

Definition at line 63 of file Uri.h.

References host_, hostname(), and string.

Referenced by folly::uri_detail::as_tuple(), authority(), and TEST().

63  {
64  return host_;
65  }
std::string host_
Definition: Uri.h:132
std::string folly::Uri::hostname ( ) const

Get host part of URI. If host is an IPv6 address, square brackets will not be returned, for exmaple "::1"; otherwise it returns the same thing as host().

hostname() is what one needs to call if passing the host to any other tool or API that connects to that host/port; e.g. getaddrinfo() only understands IPv6 host without square brackets

Definition at line 123 of file Uri.cpp.

References host_.

Referenced by host(), and TEST().

123  {
124  if (host_.size() > 0 && host_[0] == '[') {
125  // If it starts with '[', then it should end with ']', this is ensured by
126  // regex
127  return host_.substr(1, host_.size() - 2);
128  }
129  return host_;
130 }
std::string host_
Definition: Uri.h:132
const std::string& folly::Uri::password ( ) const
inline

Definition at line 56 of file Uri.h.

References password_.

Referenced by folly::uri_detail::as_tuple(), authority(), and TEST().

56  {
57  return password_;
58  }
std::string password_
Definition: Uri.h:131
const std::string& folly::Uri::path ( ) const
inline

Definition at line 79 of file Uri.h.

References path_.

Referenced by folly::uri_detail::as_tuple(), and TEST().

79  {
80  return path_;
81  }
std::string path_
Definition: Uri.h:135
uint16_t folly::Uri::port ( ) const
inline

Definition at line 76 of file Uri.h.

References port_.

Referenced by folly::uri_detail::as_tuple(), authority(), setPort(), TEST(), and Uri().

76  {
77  return port_;
78  }
uint16_t port_
Definition: Uri.h:134
const std::string& folly::Uri::query ( ) const
inline

Definition at line 82 of file Uri.h.

References query_.

Referenced by folly::uri_detail::as_tuple(), and TEST().

82  {
83  return query_;
84  }
std::string query_
Definition: Uri.h:136
const std::string& folly::Uri::scheme ( ) const
inline

Definition at line 50 of file Uri.h.

References scheme_.

Referenced by folly::uri_detail::as_tuple(), and TEST().

50  {
51  return scheme_;
52  }
std::string scheme_
Definition: Uri.h:129
void folly::Uri::setPort ( uint16_t  port)
inline

Definition at line 101 of file Uri.h.

References getQueryParams(), hasAuthority_, port(), and port_.

101  {
102  hasAuthority_ = true;
103  port_ = port;
104  }
uint16_t port_
Definition: Uri.h:134
uint16_t port() const
Definition: Uri.h:76
bool hasAuthority_
Definition: Uri.h:133
std::string folly::Uri::str ( ) const
inline

Definition at line 94 of file Uri.h.

94  {
95  return toString<std::string>();
96  }
template<class String >
String folly::Uri::toString ( ) const

Definition at line 56 of file Uri-inl.h.

References folly::toAppend().

Referenced by fragment().

56  {
57  String str;
58  if (hasAuthority_) {
59  toAppend(scheme_, "://", &str);
60  if (!password_.empty()) {
61  toAppend(username_, ":", password_, "@", &str);
62  } else if (!username_.empty()) {
63  toAppend(username_, "@", &str);
64  }
65  toAppend(host_, &str);
66  if (port_ != 0) {
67  toAppend(":", port_, &str);
68  }
69  } else {
70  toAppend(scheme_, ":", &str);
71  }
72  toAppend(path_, &str);
73  if (!query_.empty()) {
74  toAppend("?", query_, &str);
75  }
76  if (!fragment_.empty()) {
77  toAppend("#", fragment_, &str);
78  }
79  return str;
80 }
std::string path_
Definition: Uri.h:135
std::string username_
Definition: Uri.h:130
uint16_t port_
Definition: Uri.h:134
std::string query_
Definition: Uri.h:136
std::string str() const
Definition: Uri.h:94
bool hasAuthority_
Definition: Uri.h:133
std::string host_
Definition: Uri.h:132
void toAppend(char value, Tgt *result)
Definition: Conv.h:406
std::string fragment_
Definition: Uri.h:137
std::string password_
Definition: Uri.h:131
std::string scheme_
Definition: Uri.h:129
const std::string& folly::Uri::username ( ) const
inline

Definition at line 53 of file Uri.h.

References username_.

Referenced by folly::uri_detail::as_tuple(), authority(), and TEST().

53  {
54  return username_;
55  }
std::string username_
Definition: Uri.h:130

Member Data Documentation

std::string folly::Uri::fragment_
private

Definition at line 137 of file Uri.h.

Referenced by fragment(), and Uri().

bool folly::Uri::hasAuthority_
private

Definition at line 133 of file Uri.h.

Referenced by setPort(), and Uri().

std::string folly::Uri::host_
private

Definition at line 132 of file Uri.h.

Referenced by host(), hostname(), and Uri().

std::string folly::Uri::password_
private

Definition at line 131 of file Uri.h.

Referenced by password(), and Uri().

std::string folly::Uri::path_
private

Definition at line 135 of file Uri.h.

Referenced by path(), and Uri().

uint16_t folly::Uri::port_
private

Definition at line 134 of file Uri.h.

Referenced by port(), setPort(), and Uri().

std::string folly::Uri::query_
private

Definition at line 136 of file Uri.h.

Referenced by getQueryParams(), query(), and Uri().

std::vector<std::pair<std::string, std::string> > folly::Uri::queryParams_
private

Definition at line 138 of file Uri.h.

Referenced by getQueryParams().

std::string folly::Uri::scheme_
private

Definition at line 129 of file Uri.h.

Referenced by scheme(), and Uri().

std::string folly::Uri::username_
private

Definition at line 130 of file Uri.h.

Referenced by Uri(), and username().


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