Update documentation for Synchronized
[folly.git] / folly / Uri.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #pragma once
18 #define FOLLY_URI_H_
19
20 #include <folly/String.h>
21 #include <vector>
22
23 namespace folly {
24
25 /**
26  * Class representing a URI.
27  *
28  * Consider http://www.facebook.com/foo/bar?key=foo#anchor
29  *
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
33  * lower-cased.
34  *
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).
40  */
41 class Uri {
42  public:
43   /**
44    * Parse a Uri from a string.  Throws std::invalid_argument on parse error.
45    */
46   explicit Uri(StringPiece str);
47
48   const fbstring& scheme() const { return scheme_; }
49   const fbstring& username() const { return username_; }
50   const fbstring& password() const { return password_; }
51   /**
52    * Get host part of URI. If host is an IPv6 address, square brackets will be
53    * returned, for example: "[::1]".
54    */
55   const fbstring& host() const { return host_; }
56   /**
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
59    * host().
60    *
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
64    */
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_; }
70
71   fbstring authority() const;
72
73   template <class String>
74   String toString() const;
75
76   std::string str() const { return toString<std::string>(); }
77   fbstring fbstr() const { return toString<fbstring>(); }
78
79   void setPort(uint16_t port) {
80     hasAuthority_ = true;
81     port_ = port;
82   }
83
84   /**
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:
88    *     "key1" => "foo"
89    *     "key2" => ""
90    *     "key3" => ""
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
94    * value.
95    *
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.
99    *
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
103    */
104   const std::vector<std::pair<fbstring, fbstring>>& getQueryParams();
105
106  private:
107   fbstring scheme_;
108   fbstring username_;
109   fbstring password_;
110   fbstring host_;
111   bool hasAuthority_;
112   uint16_t port_;
113   fbstring path_;
114   fbstring query_;
115   fbstring fragment_;
116   std::vector<std::pair<fbstring, fbstring>> queryParams_;
117 };
118
119 }  // namespace folly
120
121 #include <folly/Uri-inl.h>