Copyright 2013 -> 2014
[folly.git] / folly / Uri.h
1 /*
2  * Copyright 2014 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 #ifndef FOLLY_URI_H_
18 #define FOLLY_URI_H_
19
20 #include "folly/String.h"
21
22 namespace folly {
23
24 /**
25  * Class representing a URI.
26  *
27  * Consider http://www.facebook.com/foo/bar?key=foo#anchor
28  *
29  * The URI is broken down into its parts: scheme ("http"), authority
30  * (ie. host and port, in most cases: "www.facebook.com"), path
31  * ("/foo/bar"), query ("key=foo") and fragment ("anchor").  The scheme is
32  * lower-cased.
33  *
34  * If this Uri represents a URL, note that, to prevent ambiguity, the component
35  * parts are NOT percent-decoded; you should do this yourself with
36  * uriUnescape() (for the authority and path) and uriUnescape(...,
37  * UriEscapeMode::QUERY) (for the query, but probably only after splitting at
38  * '&' to identify the individual parameters).
39  */
40 class Uri {
41  public:
42   /**
43    * Parse a Uri from a string.  Throws std::invalid_argument on parse error.
44    */
45   explicit Uri(StringPiece str);
46
47   const fbstring& scheme() const { return scheme_; }
48   const fbstring& username() const { return username_; }
49   const fbstring& password() const { return password_; }
50   const fbstring& host() const { return host_; }
51   uint16_t port() const { return port_; }
52   const fbstring& path() const { return path_; }
53   const fbstring& query() const { return query_; }
54   const fbstring& fragment() const { return fragment_; }
55
56   fbstring authority() const;
57
58   template <class String>
59   String toString() const;
60
61   std::string str() const { return toString<std::string>(); }
62   fbstring fbstr() const { return toString<fbstring>(); }
63
64  private:
65   fbstring scheme_;
66   fbstring username_;
67   fbstring password_;
68   fbstring host_;
69   uint16_t port_;
70   fbstring path_;
71   fbstring query_;
72   fbstring fragment_;
73 };
74
75 }  // namespace folly
76
77 #include "folly/Uri-inl.h"
78
79 #endif /* FOLLY_URI_H_ */