2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <folly/String.h>
26 * Class representing a URI.
28 * Consider http://www.facebook.com/foo/bar?key=foo#anchor
30 * The URI is broken down into its parts: scheme ("http"), authority
31 * (ie. host and port, in most cases: "www.facebook.com"), path
32 * ("/foo/bar"), query ("key=foo") and fragment ("anchor"). The scheme is
35 * If this Uri represents a URL, note that, to prevent ambiguity, the component
36 * parts are NOT percent-decoded; you should do this yourself with
37 * uriUnescape() (for the authority and path) and uriUnescape(...,
38 * UriEscapeMode::QUERY) (for the query, but probably only after splitting at
39 * '&' to identify the individual parameters).
44 * Parse a Uri from a string. Throws std::invalid_argument on parse error.
46 explicit Uri(StringPiece str);
48 const fbstring& scheme() const { return scheme_; }
49 const fbstring& username() const { return username_; }
50 const fbstring& password() const { return password_; }
52 * Get host part of URI. If host is an IPv6 address, square brackets will be
53 * returned, for example: "[::1]".
55 const fbstring& host() const { return host_; }
57 * Get host part of URI. If host is an IPv6 address, square brackets will not
58 * be returned, for exmaple "::1"; otherwise it returns the same thing as
61 * hostname() is what one needs to call if passing the host to any other tool
62 * or API that connects to that host/port; e.g. getaddrinfo() only understands
63 * IPv6 host without square brackets
65 fbstring hostname() const;
66 uint16_t port() const { return port_; }
67 const fbstring& path() const { return path_; }
68 const fbstring& query() const { return query_; }
69 const fbstring& fragment() const { return fragment_; }
71 fbstring authority() const;
73 template <class String>
74 String toString() const;
76 std::string str() const { return toString<std::string>(); }
77 fbstring fbstr() const { return toString<fbstring>(); }
79 void setPort(uint16_t port) {
85 * Get query parameters as key-value pairs.
86 * e.g. for URI containing query string: key1=foo&key2=&key3&=bar&=bar=
87 * In returned list, there are 3 entries:
91 * Parts "=bar" and "=bar=" are ignored, as they are not valid query
92 * parameters. "=bar" is missing parameter name, while "=bar=" has more than
93 * one equal signs, we don't know which one is the delimiter for key and
96 * Note, this method is not thread safe, it might update internal state, but
97 * only the first call to this method update the state. After the first call
98 * is finished, subsequent calls to this method are thread safe.
100 * @return query parameter key-value pairs in a vector, each element is a
101 * pair of which the first element is parameter name and the second
102 * one is parameter value
104 const std::vector<std::pair<fbstring, fbstring>>& getQueryParams();
116 std::vector<std::pair<fbstring, fbstring>> queryParams_;
121 #include <folly/Uri-inl.h>