uri.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #ifndef GUL_URI_H
  2. #define GUL_URI_H
  3. #include<regex>
  4. #include<string>
  5. #include<algorithm>
  6. #include<stdexcept>
  7. #include<algorithm>
  8. #include<locale>
  9. #include<cctype>
  10. namespace gul
  11. {
  12. struct uri
  13. {
  14. std::string scheme;
  15. std::string user;
  16. std::string password;
  17. std::string host;
  18. std::string port;
  19. std::string path;
  20. std::string query;
  21. std::string fragment;
  22. uri()
  23. {}
  24. uri(const std::string &str)
  25. {
  26. parse(str);
  27. }
  28. uri(const char *str)
  29. {
  30. parse(str);
  31. }
  32. uri& operator=(const std::string & str)
  33. {
  34. parse(str);
  35. return *this;
  36. }
  37. uri& operator=(const char * str)
  38. {
  39. parse(str);
  40. return *this;
  41. }
  42. operator std::string() const
  43. {
  44. return toString();
  45. }
  46. std::string toString() const
  47. {
  48. std::string out;
  49. if( scheme.size()!=0 ) out += scheme + ":";
  50. auto auth = getAuthority();
  51. if(auth.size())
  52. {
  53. out += "//" + auth;
  54. }
  55. out += path;
  56. if(query.size())
  57. {
  58. out += "?" + query;
  59. }
  60. if( fragment.size())
  61. {
  62. out += "#" + fragment;
  63. }
  64. return out;
  65. }
  66. std::string getAuthority() const
  67. {
  68. std::string out;
  69. if( user.size() )
  70. {
  71. out += user;
  72. if(password.size())
  73. out += ":" + password;
  74. out += "@";
  75. }
  76. out += host;
  77. if( port.size() )
  78. out += ":" + port;
  79. return out;
  80. }
  81. void parse(const std::string & str)
  82. {
  83. // This parsing code was taken from Facebook's Folly library
  84. // the Boost regex was replaced with std::regex
  85. /*
  86. * Copyright (c) Facebook, Inc. and its affiliates.
  87. *
  88. * Licensed under the Apache License, Version 2.0 (the "License");
  89. * you may not use this file except in compliance with the License.
  90. * You may obtain a copy of the License at
  91. *
  92. * http://www.apache.org/licenses/LICENSE-2.0
  93. *
  94. * Unless required by applicable law or agreed to in writing, software
  95. * distributed under the License is distributed on an "AS IS" BASIS,
  96. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  97. * See the License for the specific language governing permissions and
  98. * limitations under the License.
  99. */
  100. static const std::regex uriRegex(
  101. "([a-zA-Z][a-zA-Z0-9+.-]*):" // scheme:
  102. "([^?#]*)" // authority and path
  103. "(?:\\?([^#]*))?" // ?query
  104. "(?:#(.*))?"); // #fragment
  105. static const std::regex authorityAndPathRegex("//([^/]*)(/.*)?");
  106. std::smatch match;
  107. if ( !std::regex_match(str, match, uriRegex))
  108. {
  109. throw std::invalid_argument( "invalid URI: " + str);
  110. }
  111. scheme = match[1];
  112. std::transform(scheme.begin(), scheme.end(), scheme.begin(),
  113. [](auto const & c){ return static_cast<std::string::value_type>(std::tolower(c)); });
  114. std::string authorityAndPath(match[2].first, match[2].second);
  115. std::smatch authorityAndPathMatch;
  116. if (!std::regex_match(
  117. authorityAndPath,
  118. authorityAndPathMatch,
  119. authorityAndPathRegex)) {
  120. // Does not start with //, doesn't have authority
  121. path = authorityAndPath;
  122. }
  123. else
  124. {
  125. static const std::regex authorityRegex(
  126. "(?:([^@:]*)(?::([^@]*))?@)?" // username, password
  127. "(\\[[^\\]]*\\]|[^\\[:]*)" // host (IP-literal (e.g. '['+IPv6+']',
  128. // dotted-IPv4, or named host)
  129. "(?::(\\d*))?"); // port
  130. std::string authority = authorityAndPathMatch[1];
  131. std::smatch authorityMatch;
  132. if (!std::regex_match(
  133. authority,
  134. authorityMatch,
  135. authorityRegex)) {
  136. throw std::invalid_argument( "invalid URI authority");
  137. }
  138. port = std::string(authorityMatch[4].first, authorityMatch[4].second);
  139. user = authorityMatch[1];
  140. password = authorityMatch[2];
  141. host = authorityMatch[3];
  142. path = authorityAndPathMatch[2];
  143. }
  144. query = match[3];//, 3);
  145. fragment = match[4];//, 4);
  146. }
  147. std::string_view getMediaEncoding() const
  148. {
  149. auto i = std::find( path.begin(), path.end(), ',');
  150. if( i == path.end() )
  151. return {};
  152. return std::string_view(&path[0], static_cast<size_t>(std::distance(path.begin(),i)) );
  153. }
  154. std::string_view getMediaType() const
  155. {
  156. auto i = std::find( path.begin(), path.end(), ';');
  157. if( i == path.end() )
  158. return {};
  159. auto j = std::find( i, path.end(), ',');
  160. if( j == path.end() )
  161. return {};
  162. ++i;
  163. return std::string_view(&(*i), static_cast<size_t>(std::distance(i,j)));
  164. }
  165. std::string_view getMediaData() const
  166. {
  167. auto i = getMediaEncoding();
  168. auto & f = path[ i.size()+1];
  169. auto & b = path.back();
  170. return std::string_view( &f, 1+static_cast<size_t>( std::distance(&f,&b)));
  171. }
  172. };
  173. }
  174. #endif