Uri.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2013-present 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. #pragma once
  17. #define FOLLY_URI_H_
  18. #include <string>
  19. #include <vector>
  20. #include <folly/String.h>
  21. namespace folly {
  22. /**
  23. * Class representing a URI.
  24. *
  25. * Consider http://www.facebook.com/foo/bar?key=foo#anchor
  26. *
  27. * The URI is broken down into its parts: scheme ("http"), authority
  28. * (ie. host and port, in most cases: "www.facebook.com"), path
  29. * ("/foo/bar"), query ("key=foo") and fragment ("anchor"). The scheme is
  30. * lower-cased.
  31. *
  32. * If this Uri represents a URL, note that, to prevent ambiguity, the component
  33. * parts are NOT percent-decoded; you should do this yourself with
  34. * uriUnescape() (for the authority and path) and uriUnescape(...,
  35. * UriEscapeMode::QUERY) (for the query, but probably only after splitting at
  36. * '&' to identify the individual parameters).
  37. */
  38. class Uri {
  39. public:
  40. /**
  41. * Parse a Uri from a string. Throws std::invalid_argument on parse error.
  42. */
  43. explicit Uri(StringPiece str);
  44. const std::string& scheme() const {
  45. return scheme_;
  46. }
  47. const std::string& username() const {
  48. return username_;
  49. }
  50. const std::string& password() const {
  51. return password_;
  52. }
  53. /**
  54. * Get host part of URI. If host is an IPv6 address, square brackets will be
  55. * returned, for example: "[::1]".
  56. */
  57. const std::string& host() const {
  58. return host_;
  59. }
  60. /**
  61. * Get host part of URI. If host is an IPv6 address, square brackets will not
  62. * be returned, for exmaple "::1"; otherwise it returns the same thing as
  63. * host().
  64. *
  65. * hostname() is what one needs to call if passing the host to any other tool
  66. * or API that connects to that host/port; e.g. getaddrinfo() only understands
  67. * IPv6 host without square brackets
  68. */
  69. std::string hostname() const;
  70. uint16_t port() const {
  71. return port_;
  72. }
  73. const std::string& path() const {
  74. return path_;
  75. }
  76. const std::string& query() const {
  77. return query_;
  78. }
  79. const std::string& fragment() const {
  80. return fragment_;
  81. }
  82. std::string authority() const;
  83. template <class String>
  84. String toString() const;
  85. std::string str() const {
  86. return toString<std::string>();
  87. }
  88. fbstring fbstr() const {
  89. return toString<fbstring>();
  90. }
  91. void setPort(uint16_t port) {
  92. hasAuthority_ = true;
  93. port_ = port;
  94. }
  95. /**
  96. * Get query parameters as key-value pairs.
  97. * e.g. for URI containing query string: key1=foo&key2=&key3&=bar&=bar=
  98. * In returned list, there are 3 entries:
  99. * "key1" => "foo"
  100. * "key2" => ""
  101. * "key3" => ""
  102. * Parts "=bar" and "=bar=" are ignored, as they are not valid query
  103. * parameters. "=bar" is missing parameter name, while "=bar=" has more than
  104. * one equal signs, we don't know which one is the delimiter for key and
  105. * value.
  106. *
  107. * Note, this method is not thread safe, it might update internal state, but
  108. * only the first call to this method update the state. After the first call
  109. * is finished, subsequent calls to this method are thread safe.
  110. *
  111. * @return query parameter key-value pairs in a vector, each element is a
  112. * pair of which the first element is parameter name and the second
  113. * one is parameter value
  114. */
  115. const std::vector<std::pair<std::string, std::string>>& getQueryParams();
  116. private:
  117. std::string scheme_;
  118. std::string username_;
  119. std::string password_;
  120. std::string host_;
  121. bool hasAuthority_;
  122. uint16_t port_;
  123. std::string path_;
  124. std::string query_;
  125. std::string fragment_;
  126. std::vector<std::pair<std::string, std::string>> queryParams_;
  127. };
  128. } // namespace folly
  129. #include <folly/Uri-inl.h>