proxygen
ParseURL.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 <folly/Conv.h>
13 #include <folly/String.h>
15 #include <string>
16 
17 namespace proxygen {
18 
19 // ParseURL can handle non-fully-formed URLs. This class must not persist beyond
20 // the lifetime of the buffer underlying the input StringPiece
21 
22 class ParseURL {
23  public:
24  ParseURL() {}
26  init(urlVal);
27  }
28 
29  void init(folly::StringPiece urlVal) {
30  CHECK(!initialized_);
31  url_ = urlVal;
32  parse();
33  initialized_ = true;
34  }
35 
37  return url_;
38  }
39 
41  return scheme_;
42  }
43 
45  return authority_;
46  }
47 
48  bool hasHost() const {
49  return valid() && !host_.empty();
50  }
51 
53  return host_;
54  }
55 
56  uint16_t port() const {
57  return port_;
58  }
59 
61  std::string rc = host_.str();
62  if (port_ != 0) {
63  folly::toAppend(":", port_, &rc);
64  }
65  return rc;
66  }
67 
69  return path_;
70  }
71 
73  return query_;
74  }
75 
77  return fragment_;
78  }
79 
80  bool valid() const {
81  return valid_;
82  }
83 
85  stripBrackets();
86  return hostNoBrackets_;
87  }
88 
89  bool hostIsIPAddress();
90 
92 
93  private:
94  FB_EXPORT void parse() noexcept;
95 
96  void parseNonFully() noexcept;
97 
98  bool parseAuthority() noexcept;
99 
109  bool valid_{false};
110  bool initialized_{false};
111 };
112 
113 }
#define FB_EXPORT
Definition: Export.h:26
FB_EXPORT void parse() noexcept
Definition: ParseURL.cpp:60
std::string authority() const
Definition: ParseURL.h:44
folly::StringPiece query() const
Definition: ParseURL.h:72
std::string str() const
Definition: Range.h:591
folly::StringPiece url() const
Definition: ParseURL.h:36
folly::StringPiece hostNoBrackets()
Definition: ParseURL.h:84
void init(folly::StringPiece urlVal)
Definition: ParseURL.h:29
ParseURL(folly::StringPiece urlVal) noexcept
Definition: ParseURL.h:25
bool hasHost() const
Definition: ParseURL.h:48
folly::StringPiece host() const
Definition: ParseURL.h:52
uint16_t port_
Definition: ParseURL.h:108
requires E e noexcept(noexcept(s.error(std::move(e))))
std::string hostAndPort() const
Definition: ParseURL.h:60
folly::StringPiece query_
Definition: ParseURL.h:106
FB_EXPORT void stripBrackets() noexcept
Definition: ParseURL.cpp:192
constexpr bool empty() const
Definition: Range.h:443
folly::StringPiece host_
Definition: ParseURL.h:103
folly::StringPiece path_
Definition: ParseURL.h:105
folly::StringPiece path() const
Definition: ParseURL.h:68
folly::StringPiece url_
Definition: ParseURL.h:100
folly::StringPiece fragment_
Definition: ParseURL.h:107
void toAppend(char value, Tgt *result)
Definition: Conv.h:406
bool valid() const
Definition: ParseURL.h:80
uint16_t port() const
Definition: ParseURL.h:56
bool parseAuthority() noexcept
Definition: ParseURL.cpp:150
const char * string
Definition: Conv.cpp:212
folly::StringPiece hostNoBrackets_
Definition: ParseURL.h:104
bool hostIsIPAddress()
Definition: ParseURL.cpp:177
folly::StringPiece scheme() const
Definition: ParseURL.h:40
std::string authority_
Definition: ParseURL.h:102
void parseNonFully() noexcept
Definition: ParseURL.cpp:102
folly::StringPiece scheme_
Definition: ParseURL.h:101
folly::StringPiece fragment() const
Definition: ParseURL.h:76