Url.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /***************************************************************************
  2. *
  3. * Project _____ __ ____ _ _
  4. * ( _ ) /__\ (_ _)_| |_ _| |_
  5. * )(_)( /(__)\ )( (_ _)(_ _)
  6. * (_____)(__)(__)(__) |_| |_|
  7. *
  8. *
  9. * Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ***************************************************************************/
  24. #ifndef oatpp_network_Url_hpp
  25. #define oatpp_network_Url_hpp
  26. #include "oatpp/core/data/share/LazyStringMap.hpp"
  27. #include "oatpp/core/parser/Caret.hpp"
  28. #include "oatpp/core/Types.hpp"
  29. namespace oatpp { namespace network {
  30. /**
  31. * Class holding URL information.
  32. */
  33. class Url {
  34. public:
  35. /**
  36. * Convenience typedef for &id:oatpp::data::share::StringKeyLabel;.
  37. */
  38. typedef oatpp::data::share::StringKeyLabel StringKeyLabel;
  39. public:
  40. /**
  41. * Parameters - map string to string. &id:oatpp::data::share::LazyStringMultimap;.
  42. */
  43. typedef oatpp::data::share::LazyStringMultimap<oatpp::data::share::StringKeyLabel> Parameters;
  44. public:
  45. /**
  46. * Structure representing URL Authority information.
  47. */
  48. struct Authority {
  49. /**
  50. * User info.
  51. */
  52. oatpp::String userInfo;
  53. /**
  54. * Host.
  55. */
  56. oatpp::String host;
  57. /**
  58. * Port. Treat -1 as undefined or as default.
  59. */
  60. v_int32 port = -1;
  61. };
  62. public:
  63. /**
  64. * Url parser.
  65. */
  66. class Parser {
  67. public:
  68. /**
  69. * parse `<scheme>`:
  70. * example "http", "https", "ftp"
  71. * returns lowercase string before ':' char
  72. * caret should be at the first char of the scheme
  73. */
  74. static oatpp::String parseScheme(oatpp::parser::Caret& caret);
  75. /**
  76. * parse url authority components.
  77. * userinfo is not parsed into login and password separately as
  78. * inclusion of password in userinfo is deprecated and ignored here
  79. * caret should be at the first char of the authority (not at "//")
  80. */
  81. static Url::Authority parseAuthority(oatpp::parser::Caret& caret);
  82. /**
  83. * parse path of the url
  84. * caret should be at the first char of the path
  85. */
  86. static oatpp::String parsePath(oatpp::parser::Caret& caret);
  87. /**
  88. * parse query params in form of `"?<paramName>=<paramValue>&<paramName>=<paramValue>..."` referred by ParsingCaret
  89. * and put that params to Parameters map
  90. */
  91. static void parseQueryParams(Url::Parameters& params, oatpp::parser::Caret& caret);
  92. /**
  93. * parse query params in form of `"?<paramName>=<paramValue>&<paramName>=<paramValue>..."` referred by str
  94. * and put that params to Parameters map
  95. */
  96. static void parseQueryParams(Url::Parameters& params, const oatpp::String& str);
  97. /**
  98. * parse query params in form of `"?<paramName>=<paramValue>&<paramName>=<paramValue>..."` referred by ParsingCaret
  99. */
  100. static Url::Parameters parseQueryParams(oatpp::parser::Caret& caret);
  101. /**
  102. * parse query params in form of `"?<paramName>=<paramValue>&<paramName>=<paramValue>..."` referred by str
  103. */
  104. static Url::Parameters parseQueryParams(const oatpp::String& str);
  105. /**
  106. * Parse Url
  107. * @param caret
  108. * @return parsed URL structure
  109. */
  110. static Url parseUrl(oatpp::parser::Caret& caret);
  111. /**
  112. * Parse Url
  113. * @param str
  114. * @return parsed URL structure
  115. */
  116. static Url parseUrl(const oatpp::String& str);
  117. };
  118. public:
  119. /**
  120. * Url scheme. Ex.: [http, https, ftp, etc.]
  121. */
  122. oatpp::String scheme;
  123. /**
  124. * Utl authority.
  125. */
  126. Authority authority;
  127. /**
  128. * Path to resource.
  129. */
  130. oatpp::String path;
  131. /**
  132. * Query params.
  133. */
  134. Parameters queryParams;
  135. };
  136. }}
  137. #endif /* oatpp_network_url_Url_hpp */