proxygen
proxygen::ParseURL Class Reference

#include <ParseURL.h>

Public Member Functions

 ParseURL ()
 
 ParseURL (folly::StringPiece urlVal) noexcept
 
void init (folly::StringPiece urlVal)
 
folly::StringPiece url () const
 
folly::StringPiece scheme () const
 
std::string authority () const
 
bool hasHost () const
 
folly::StringPiece host () const
 
uint16_t port () const
 
std::string hostAndPort () const
 
folly::StringPiece path () const
 
folly::StringPiece query () const
 
folly::StringPiece fragment () const
 
bool valid () const
 
folly::StringPiece hostNoBrackets ()
 
bool hostIsIPAddress ()
 
FB_EXPORT void stripBrackets () noexcept
 

Private Member Functions

FB_EXPORT void parse () noexcept
 
void parseNonFully () noexcept
 
bool parseAuthority () noexcept
 

Private Attributes

folly::StringPiece url_
 
folly::StringPiece scheme_
 
std::string authority_
 
folly::StringPiece host_
 
folly::StringPiece hostNoBrackets_
 
folly::StringPiece path_
 
folly::StringPiece query_
 
folly::StringPiece fragment_
 
uint16_t port_ {0}
 
bool valid_ {false}
 
bool initialized_ {false}
 

Detailed Description

Definition at line 22 of file ParseURL.h.

Constructor & Destructor Documentation

proxygen::ParseURL::ParseURL ( )
inline

Definition at line 24 of file ParseURL.h.

24 {}
proxygen::ParseURL::ParseURL ( folly::StringPiece  urlVal)
inlineexplicitnoexcept

Definition at line 25 of file ParseURL.h.

References init().

25  {
26  init(urlVal);
27  }
void init(folly::StringPiece urlVal)
Definition: ParseURL.h:29

Member Function Documentation

std::string proxygen::ParseURL::authority ( ) const
inline

Definition at line 44 of file ParseURL.h.

References authority_.

Referenced by proxygen::HTTPMessage::setQueryString(), and testParseURL().

44  {
45  return authority_;
46  }
std::string authority_
Definition: ParseURL.h:102
folly::StringPiece proxygen::ParseURL::fragment ( ) const
inline

Definition at line 76 of file ParseURL.h.

References fragment_.

Referenced by proxygen::HTTPMessage::setQueryString(), and proxygen::URL::URL().

76  {
77  return fragment_;
78  }
folly::StringPiece fragment_
Definition: ParseURL.h:107
bool proxygen::ParseURL::hasHost ( ) const
inline

Definition at line 48 of file ParseURL.h.

References folly::Range< Iter >::empty(), host_, and valid().

Referenced by proxygen::HTTP1xCodec::onHeadersComplete().

48  {
49  return valid() && !host_.empty();
50  }
constexpr bool empty() const
Definition: Range.h:443
folly::StringPiece host_
Definition: ParseURL.h:103
bool valid() const
Definition: ParseURL.h:80
folly::StringPiece proxygen::ParseURL::host ( ) const
inline

Definition at line 52 of file ParseURL.h.

References host_.

Referenced by TEST(), and testParseURL().

52  {
53  return host_;
54  }
folly::StringPiece host_
Definition: ParseURL.h:103
std::string proxygen::ParseURL::hostAndPort ( ) const
inline

Definition at line 60 of file ParseURL.h.

References host_, port_, folly::Range< Iter >::str(), string, and folly::toAppend().

Referenced by proxygen::HTTP1xCodec::onHeadersComplete().

60  {
61  std::string rc = host_.str();
62  if (port_ != 0) {
63  folly::toAppend(":", port_, &rc);
64  }
65  return rc;
66  }
std::string str() const
Definition: Range.h:591
uint16_t port_
Definition: ParseURL.h:108
folly::StringPiece host_
Definition: ParseURL.h:103
void toAppend(char value, Tgt *result)
Definition: Conv.h:406
const char * string
Definition: Conv.cpp:212
bool proxygen::ParseURL::hostIsIPAddress ( )

Definition at line 177 of file ParseURL.cpp.

References folly::Range< Iter >::find(), hostNoBrackets_, folly::Range< Iter >::str(), stripBrackets(), and valid_.

Referenced by hostNoBrackets(), and testHostIsIpAddress().

177  {
178  if (!valid_) {
179  return false;
180  }
181 
182  stripBrackets();
183  int af = hostNoBrackets_.find(':') == std::string::npos ? AF_INET : AF_INET6;
184  char buf4[sizeof(in_addr)];
185  char buf6[sizeof(in6_addr)];
186  // we have to make a copy of hostNoBrackets_ since the string piece is not
187  // null-terminated
188  return inet_pton(af, hostNoBrackets_.str().c_str(),
189  af == AF_INET ? buf4 : buf6) == 1;
190 }
std::string str() const
Definition: Range.h:591
size_type find(const_range_type str) const
Definition: Range.h:721
FB_EXPORT void stripBrackets() noexcept
Definition: ParseURL.cpp:192
folly::StringPiece hostNoBrackets_
Definition: ParseURL.h:104
folly::StringPiece proxygen::ParseURL::hostNoBrackets ( )
inline

Definition at line 84 of file ParseURL.h.

References FB_EXPORT, hostIsIPAddress(), hostNoBrackets_, folly::pushmi::__adl::noexcept(), parse(), parseAuthority(), parseNonFully(), and stripBrackets().

Referenced by TEST(), and proxygen::URL::URL().

84  {
85  stripBrackets();
86  return hostNoBrackets_;
87  }
FB_EXPORT void stripBrackets() noexcept
Definition: ParseURL.cpp:192
folly::StringPiece hostNoBrackets_
Definition: ParseURL.h:104
void proxygen::ParseURL::init ( folly::StringPiece  urlVal)
inline

Definition at line 29 of file ParseURL.h.

References initialized_, parse(), and url_.

Referenced by ParseURL().

29  {
30  CHECK(!initialized_);
31  url_ = urlVal;
32  parse();
33  initialized_ = true;
34  }
FB_EXPORT void parse() noexcept
Definition: ParseURL.cpp:60
folly::StringPiece url_
Definition: ParseURL.h:100
void proxygen::ParseURL::parse ( )
privatenoexcept

Definition at line 60 of file ParseURL.cpp.

References authority_, folly::Range< Iter >::data(), http_parser_url::field_data, fragment_, host_, http_parser_parse_url(), http_parser_url::len, http_parser_url::off, parseNonFully(), path_, http_parser_url::port, port_, query_, scheme_, folly::Range< Iter >::size(), folly::Range< Iter >::str(), folly::Range< Iter >::subpiece(), UF_FRAGMENT, UF_HOST, UF_PATH, UF_QUERY, UF_SCHEMA, url_, valid_, and proxygen::validateScheme().

Referenced by hostNoBrackets(), and init().

60  {
61  if (validateScheme(url_)) {
62  struct http_parser_url u;
63  memset(&u, 0, sizeof(struct http_parser_url)); // init before used
64  valid_ = !(http_parser_parse_url(url_.data(), url_.size(), 0, &u));
65 
66  if(valid_) {
67  // Since we init the http_parser_url with all fields to 0, if the field
68  // not present in url, it would be [0, 0], means that this field starts at
69  // 0 and len = 0, we will get "" from this. So no need to check field_set
70  // before get field.
71 
72  scheme_ = url_.subpiece(u.field_data[UF_SCHEMA].off,
73  u.field_data[UF_SCHEMA].len);
74 
75  if(u.field_data[UF_HOST].off != 0 &&
76  url_[u.field_data[UF_HOST].off - 1] == '[') {
77  // special case: host: [::1]
78  host_ = url_.subpiece(u.field_data[UF_HOST].off - 1,
79  u.field_data[UF_HOST].len + 2);
80  } else {
81  host_ = url_.subpiece(u.field_data[UF_HOST].off,
82  u.field_data[UF_HOST].len);
83  }
84 
85  port_ = u.port;
86 
87  path_ = url_.subpiece(u.field_data[UF_PATH].off,
88  u.field_data[UF_PATH].len);
89  query_ = url_.subpiece(u.field_data[UF_QUERY].off,
90  u.field_data[UF_QUERY].len);
91  fragment_ = url_.subpiece(u.field_data[UF_FRAGMENT].off,
92  u.field_data[UF_FRAGMENT].len);
93 
94  authority_ = (port_) ? folly::to<std::string>(host_, ":", port_)
95  : host_.str();
96  }
97  } else {
98  parseNonFully();
99  }
100 }
static bool validateScheme(folly::StringPiece url)
Definition: ParseURL.cpp:48
std::string str() const
Definition: Range.h:591
constexpr size_type size() const
Definition: Range.h:431
uint16_t port_
Definition: ParseURL.h:108
folly::StringPiece query_
Definition: ParseURL.h:106
folly::StringPiece host_
Definition: ParseURL.h:103
int http_parser_parse_url(const char *buf, size_t buflen, int is_connect, struct http_parser_url *u)
Definition: http_parser.c:2332
folly::StringPiece path_
Definition: ParseURL.h:105
constexpr Iter data() const
Definition: Range.h:446
folly::StringPiece url_
Definition: ParseURL.h:100
Range subpiece(size_type first, size_type length=npos) const
Definition: Range.h:686
folly::StringPiece fragment_
Definition: ParseURL.h:107
std::string authority_
Definition: ParseURL.h:102
void parseNonFully() noexcept
Definition: ParseURL.cpp:102
folly::StringPiece scheme_
Definition: ParseURL.h:101
bool proxygen::ParseURL::parseAuthority ( )
privatenoexcept

Definition at line 150 of file ParseURL.cpp.

References authority_, host_, and port_.

Referenced by hostNoBrackets(), and parseNonFully().

150  {
151  auto left = authority_.find("[");
152  auto right = authority_.find("]");
153 
154  auto pos = authority_.find(":", right != std::string::npos ? right : 0);
155  if (pos != std::string::npos) {
156  try {
157  port_ = folly::to<uint16_t>(
158  folly::StringPiece(authority_, pos+1, std::string::npos));
159  } catch (...) {
160  return false;
161  }
162  }
163 
164  if (left == std::string::npos && right == std::string::npos) {
165  // not a ipv6 literal
167  return true;
168  } else if (left < right && right != std::string::npos) {
169  // a ipv6 literal
170  host_ = folly::StringPiece(authority_, left, right - left + 1);
171  return true;
172  } else {
173  return false;
174  }
175 }
uint16_t port_
Definition: ParseURL.h:108
folly::StringPiece host_
Definition: ParseURL.h:103
Range< const char * > StringPiece
std::string authority_
Definition: ParseURL.h:102
void proxygen::ParseURL::parseNonFully ( )
privatenoexcept

Definition at line 102 of file ParseURL.cpp.

References authority_, folly::Range< Iter >::empty(), folly::Range< Iter >::find(), fragment_, min, parseAuthority(), path_, query_, folly::Range< Iter >::str(), folly::Range< Iter >::subpiece(), url_, valid_, and proxygen::validateURL().

Referenced by hostNoBrackets(), and parse().

102  {
103  if (url_.empty()) {
104  valid_ = false;
105  return;
106  }
107 
108  // Check if the URL has only printable characters and no control character.
109  if (!validateURL(url_)) {
110  valid_ = false;
111  return;
112  }
113 
114  auto pathStart = url_.find('/');
115  auto queryStart = url_.find('?');
116  auto hashStart = url_.find('#');
117 
118  auto queryEnd = std::min(hashStart, std::string::npos);
119  auto pathEnd = std::min(queryStart, hashStart);
120  auto authorityEnd = std::min(pathStart, pathEnd);
121 
122  authority_ = url_.subpiece(0, authorityEnd).str();
123 
124  if (pathStart < pathEnd) {
125  path_ = url_.subpiece(pathStart, pathEnd - pathStart);
126  } else {
127  // missing the '/', e.g. '?query=3'
128  path_ = "";
129  }
130 
131  if (queryStart < queryEnd) {
132  query_ = url_.subpiece(queryStart + 1, queryEnd - queryStart - 1);
133  } else if (queryStart != std::string::npos && hashStart < queryStart) {
134  valid_ = false;
135  return;
136  }
137 
138  if (hashStart != std::string::npos) {
139  fragment_ = url_.subpiece(hashStart + 1, std::string::npos);
140  }
141 
142  if (!parseAuthority()) {
143  valid_ = false;
144  return;
145  }
146 
147  valid_ = true;
148 }
std::string str() const
Definition: Range.h:591
size_type find(const_range_type str) const
Definition: Range.h:721
bool validateURL(folly::ByteRange url)
Definition: UtilInl.h:25
folly::StringPiece query_
Definition: ParseURL.h:106
constexpr bool empty() const
Definition: Range.h:443
LogLevel min
Definition: LogLevel.cpp:30
folly::StringPiece path_
Definition: ParseURL.h:105
folly::StringPiece url_
Definition: ParseURL.h:100
Range subpiece(size_type first, size_type length=npos) const
Definition: Range.h:686
folly::StringPiece fragment_
Definition: ParseURL.h:107
bool parseAuthority() noexcept
Definition: ParseURL.cpp:150
std::string authority_
Definition: ParseURL.h:102
folly::StringPiece proxygen::ParseURL::path ( ) const
inline

Definition at line 68 of file ParseURL.h.

References path_.

Referenced by proxygen::HTTPMessage::setQueryString(), proxygen::HTTPMessage::setURL(), testParseURL(), and proxygen::URL::URL().

68  {
69  return path_;
70  }
folly::StringPiece path_
Definition: ParseURL.h:105
uint16_t proxygen::ParseURL::port ( ) const
inline

Definition at line 56 of file ParseURL.h.

References port_.

Referenced by testParseURL(), and proxygen::URL::URL().

56  {
57  return port_;
58  }
uint16_t port_
Definition: ParseURL.h:108
folly::StringPiece proxygen::ParseURL::query ( ) const
inline

Definition at line 72 of file ParseURL.h.

References query_.

Referenced by proxygen::HTTPMessage::setURL(), testParseURL(), and proxygen::URL::URL().

72  {
73  return query_;
74  }
folly::StringPiece query_
Definition: ParseURL.h:106
folly::StringPiece proxygen::ParseURL::scheme ( ) const
inline

Definition at line 40 of file ParseURL.h.

References scheme_.

Referenced by proxygen::HTTPMessage::setQueryString(), testParseURL(), and proxygen::URL::URL().

40  {
41  return scheme_;
42  }
folly::StringPiece scheme_
Definition: ParseURL.h:101
void proxygen::ParseURL::stripBrackets ( )
noexcept

Definition at line 192 of file ParseURL.cpp.

References folly::Range< Iter >::back(), folly::Range< Iter >::empty(), folly::Range< Iter >::front(), host_, hostNoBrackets_, folly::Range< Iter >::size(), and folly::Range< Iter >::subpiece().

Referenced by hostIsIPAddress(), and hostNoBrackets().

192  {
193  if (hostNoBrackets_.empty()) {
194  if (!host_.empty() && host_.front() == '[' && host_.back() == ']') {
196  } else {
198  }
199  }
200 }
constexpr size_type size() const
Definition: Range.h:431
constexpr bool empty() const
Definition: Range.h:443
folly::StringPiece host_
Definition: ParseURL.h:103
Range subpiece(size_type first, size_type length=npos) const
Definition: Range.h:686
value_type & front()
Definition: Range.h:464
folly::StringPiece hostNoBrackets_
Definition: ParseURL.h:104
value_type & back()
Definition: Range.h:468
folly::StringPiece proxygen::ParseURL::url ( ) const
inline

Definition at line 36 of file ParseURL.h.

References url_.

Referenced by testParseURL(), and proxygen::URL::URL().

36  {
37  return url_;
38  }
folly::StringPiece url_
Definition: ParseURL.h:100
bool proxygen::ParseURL::valid ( ) const
inline

Definition at line 80 of file ParseURL.h.

References valid_.

Referenced by hasHost(), proxygen::HTTPMessage::setQueryString(), proxygen::HTTPMessage::setURL(), and testParseURL().

80  {
81  return valid_;
82  }

Member Data Documentation

std::string proxygen::ParseURL::authority_
private

Definition at line 102 of file ParseURL.h.

Referenced by authority(), parse(), parseAuthority(), and parseNonFully().

folly::StringPiece proxygen::ParseURL::fragment_
private

Definition at line 107 of file ParseURL.h.

Referenced by fragment(), parse(), and parseNonFully().

folly::StringPiece proxygen::ParseURL::host_
private

Definition at line 103 of file ParseURL.h.

Referenced by hasHost(), host(), hostAndPort(), parse(), parseAuthority(), and stripBrackets().

folly::StringPiece proxygen::ParseURL::hostNoBrackets_
private

Definition at line 104 of file ParseURL.h.

Referenced by hostIsIPAddress(), hostNoBrackets(), and stripBrackets().

bool proxygen::ParseURL::initialized_ {false}
private

Definition at line 110 of file ParseURL.h.

Referenced by init().

folly::StringPiece proxygen::ParseURL::path_
private

Definition at line 105 of file ParseURL.h.

Referenced by parse(), parseNonFully(), and path().

uint16_t proxygen::ParseURL::port_ {0}
private

Definition at line 108 of file ParseURL.h.

Referenced by hostAndPort(), parse(), parseAuthority(), and port().

folly::StringPiece proxygen::ParseURL::query_
private

Definition at line 106 of file ParseURL.h.

Referenced by parse(), parseNonFully(), and query().

folly::StringPiece proxygen::ParseURL::scheme_
private

Definition at line 101 of file ParseURL.h.

Referenced by parse(), and scheme().

folly::StringPiece proxygen::ParseURL::url_
private

Definition at line 100 of file ParseURL.h.

Referenced by init(), parse(), parseNonFully(), and url().

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

Definition at line 109 of file ParseURL.h.

Referenced by hostIsIPAddress(), parse(), parseNonFully(), and valid().


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