Request.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_web_protocol_http_incoming_Request_hpp
  25. #define oatpp_web_protocol_http_incoming_Request_hpp
  26. #include "oatpp/web/protocol/http/Http.hpp"
  27. #include "oatpp/web/protocol/http/incoming/BodyDecoder.hpp"
  28. #include "oatpp/web/url/mapping/Pattern.hpp"
  29. #include "oatpp/network/Url.hpp"
  30. #include "oatpp/core/data/Bundle.hpp"
  31. namespace oatpp { namespace web { namespace protocol { namespace http { namespace incoming {
  32. /**
  33. * Class http::incoming::Request AKA IncomingRequest represents client's incoming request.
  34. */
  35. class Request : public oatpp::base::Countable {
  36. private:
  37. std::shared_ptr<oatpp::data::stream::IOStream> m_connection;
  38. http::RequestStartingLine m_startingLine;
  39. url::mapping::Pattern::MatchMap m_pathVariables;
  40. http::Headers m_headers;
  41. std::shared_ptr<oatpp::data::stream::InputStream> m_bodyStream;
  42. /*
  43. * Request should be preconfigured with default BodyDecoder.
  44. * Custom BodyDecoder can be set on demand
  45. */
  46. std::shared_ptr<const http::incoming::BodyDecoder> m_bodyDecoder;
  47. mutable bool m_queryParamsParsed; // used for lazy parsing of QueryParams
  48. mutable http::QueryParams m_queryParams;
  49. data::Bundle m_bundle;
  50. public:
  51. Request(const std::shared_ptr<oatpp::data::stream::IOStream>& connection,
  52. const http::RequestStartingLine& startingLine,
  53. const http::Headers& headers,
  54. const std::shared_ptr<oatpp::data::stream::InputStream>& bodyStream,
  55. const std::shared_ptr<const http::incoming::BodyDecoder>& bodyDecoder);
  56. public:
  57. static std::shared_ptr<Request> createShared(const std::shared_ptr<oatpp::data::stream::IOStream>& connection,
  58. const http::RequestStartingLine& startingLine,
  59. const http::Headers& headers,
  60. const std::shared_ptr<oatpp::data::stream::InputStream>& bodyStream,
  61. const std::shared_ptr<const http::incoming::BodyDecoder>& bodyDecoder);
  62. /**
  63. * Get raw connection stream.
  64. * @return - &id:std::shared_ptr<oatpp::data::stream::IOStream> m_connection;.
  65. */
  66. std::shared_ptr<oatpp::data::stream::IOStream> getConnection();
  67. /**
  68. * Get map of url query parameters.
  69. * Query parameters will be lazy parsed from url "tail"
  70. * Please note: lazy parsing of query parameters is not thread-safe!
  71. * @return map<key, value> for "&key=value"
  72. */
  73. const http::QueryParams& getQueryParameters() const;
  74. /**
  75. * Get query parameter value by name
  76. * @param name
  77. * @return query parameter value
  78. */
  79. oatpp::String getQueryParameter(const oatpp::data::share::StringKeyLabel& name) const;
  80. /**
  81. * Get query parameter value by name with defaultValue
  82. * @param name
  83. * @param defaultValue
  84. * @return query parameter value or defaultValue if no such parameter found
  85. */
  86. oatpp::String getQueryParameter(const oatpp::data::share::StringKeyLabel& name, const oatpp::String& defaultValue) const;
  87. /**
  88. * Get request starting line. (method, path, protocol)
  89. * @return starting line structure
  90. */
  91. const http::RequestStartingLine& getStartingLine() const;
  92. /**
  93. * Set request path variables.
  94. * @param pathVariables - &id:oatpp::web::url::mapping::Pattern::MatchMap;.
  95. */
  96. void setPathVariables(const url::mapping::Pattern::MatchMap& pathVariables);
  97. /**
  98. * Get path variables according to path-pattern. <br>
  99. * Ex. given request path="/sum/19/1" for path-pattern="/sum/{a}/{b}" <br>
  100. * getPathVariables().getVariable("a") == 19, getPathVariables().getVariable("b") == 1.
  101. *
  102. * @return url MatchMap
  103. */
  104. const url::mapping::Pattern::MatchMap& getPathVariables() const;
  105. /**
  106. * Get request's headers map
  107. * @return Headers map
  108. */
  109. const http::Headers& getHeaders() const;
  110. /**
  111. * Get request's body stream
  112. * @return body stream
  113. */
  114. std::shared_ptr<oatpp::data::stream::InputStream> getBodyStream() const;
  115. /**
  116. * Get body decoder.
  117. * @return Body decoder
  118. */
  119. std::shared_ptr<const http::incoming::BodyDecoder> getBodyDecoder() const;
  120. /**
  121. * Add http header.
  122. * @param key - &id:oatpp::String;.
  123. * @param value - &id:oatpp::String;.
  124. */
  125. void putHeader(const oatpp::String& key, const oatpp::String& value);
  126. /**
  127. * Add http header if not already exists.
  128. * @param key - &id:oatpp::String;.
  129. * @param value - &id:oatpp::String;.
  130. * @return - `true` if header was added.
  131. */
  132. bool putHeaderIfNotExists(const oatpp::String& key, const oatpp::String& value);
  133. /**
  134. * Replaces or adds header.
  135. * @param key - &id:oatpp::String;.
  136. * @param value - &id:oatpp::String;.
  137. * @return - `true` if header was replaces, `false` if header was added.
  138. */
  139. bool putOrReplaceHeader(const oatpp::String& key, const oatpp::String& value);
  140. /**
  141. * Replaces or adds header.
  142. * @param key - &id:oatpp::data::share::StringKeyLabelCI;.
  143. * @param value - &id:oatpp::data::share::StringKeyLabel;.
  144. * @return - `true` if header was replaces, `false` if header was added.
  145. */
  146. bool putOrReplaceHeader_Unsafe(const oatpp::data::share::StringKeyLabelCI& key, const oatpp::data::share::StringKeyLabel& value);
  147. /**
  148. * Add http header.
  149. * @param key - &id:oatpp::data::share::StringKeyLabelCI;.
  150. * @param value - &id:oatpp::data::share::StringKeyLabel;.
  151. */
  152. void putHeader_Unsafe(const oatpp::data::share::StringKeyLabelCI& key, const oatpp::data::share::StringKeyLabel& value);
  153. /**
  154. * Add http header if not already exists.
  155. * @param key - &id:oatpp::data::share::StringKeyLabelCI;.
  156. * @param value - &id:oatpp::data::share::StringKeyLabel;.
  157. * @return - `true` if header was added.
  158. */
  159. bool putHeaderIfNotExists_Unsafe(const oatpp::data::share::StringKeyLabelCI& key, const oatpp::data::share::StringKeyLabel& value);
  160. /**
  161. * Get header value
  162. * @param headerName - &id:oatpp::data::share::StringKeyLabelCI;.
  163. * @return - &id:oatpp::String;.
  164. */
  165. oatpp::String getHeader(const oatpp::data::share::StringKeyLabelCI& headerName) const;
  166. /**
  167. * Get path variable according to path-pattern
  168. * @param name
  169. * @return matched value for path-pattern
  170. */
  171. oatpp::String getPathVariable(const oatpp::data::share::StringKeyLabel& name) const;
  172. /**
  173. * Get path tail according to path-pattern
  174. * Ex. given request path="/hello/path/tail" for path-pattern="/hello/\*"
  175. * tail == "path/tail"
  176. * note '/' symbol is required before '*'
  177. * @return matched tail-value for path-pattern
  178. */
  179. oatpp::String getPathTail() const;
  180. /**
  181. * Put data to bundle.
  182. * @param key
  183. * @param polymorph
  184. */
  185. void putBundleData(const oatpp::String& key, const oatpp::Void& polymorph);
  186. /**
  187. * Get data from bundle by key.
  188. * @tparam WrapperType
  189. * @param key
  190. * @return
  191. */
  192. template<typename WrapperType>
  193. WrapperType getBundleData(const oatpp::String& key) const {
  194. return m_bundle.template get<WrapperType>(key);
  195. }
  196. /**
  197. * Get bundle object.
  198. * @return
  199. */
  200. const data::Bundle& getBundle() const;
  201. /**
  202. * Transfer body. <br>
  203. * Read body chunk by chunk and pass chunks to the `writeCallback`.
  204. * @param writeCallback - &id:oatpp::data::stream::WriteCallback;.
  205. */
  206. void transferBody(const base::ObjectHandle<data::stream::WriteCallback>& writeCallback) const;
  207. /**
  208. * Stream content of the body-stream to toStream
  209. * @param toStream
  210. */
  211. void transferBodyToStream(const base::ObjectHandle<data::stream::OutputStream>& toStream) const;
  212. /**
  213. * Transfer body stream to string
  214. * @return body as string
  215. */
  216. oatpp::String readBodyToString() const;
  217. /**
  218. * Transfer body to String and parse it as DTO
  219. * @tparam Wrapper - ObjectWrapper type.
  220. * @param objectMapper
  221. * @return DTO
  222. */
  223. template<class Wrapper>
  224. Wrapper readBodyToDto(const base::ObjectHandle<data::mapping::ObjectMapper>& objectMapper) const {
  225. return objectMapper->readFromString<Wrapper>(m_bodyDecoder->decodeToString(m_headers, m_bodyStream.get(), m_connection.get()));
  226. }
  227. // Async
  228. /**
  229. * Transfer body in Asynchronous manner. <br>
  230. * Read body chunk by chunk and pass chunks to the `writeCallback`.
  231. * @param writeCallback - `std::shared_ptr` to &id:oatpp::data::stream::WriteCallback;.
  232. * @return - &id:oatpp::async::CoroutineStarter;.
  233. */
  234. async::CoroutineStarter transferBodyAsync(const std::shared_ptr<data::stream::WriteCallback>& writeCallback) const;
  235. /**
  236. * Transfer body stream to toStream Async
  237. * @param toStream
  238. * @return - &id:oatpp::async::CoroutineStarter;.
  239. */
  240. oatpp::async::CoroutineStarter transferBodyToStreamAsync(const std::shared_ptr<oatpp::data::stream::OutputStream>& toStream) const;
  241. /**
  242. * Transfer body stream to string Async.
  243. * @return - &id:oatpp::async::CoroutineStarterForResult;.
  244. */
  245. async::CoroutineStarterForResult<const oatpp::String&> readBodyToStringAsync() const;
  246. /**
  247. * Transfer body to String and parse it as DTO
  248. * @tparam Wrapper - DTO `ObjectWrapper`.
  249. * @param objectMapper
  250. * @return - &id:oatpp::async::CoroutineStarterForResult;.
  251. */
  252. template<class Wrapper>
  253. oatpp::async::CoroutineStarterForResult<const Wrapper&>
  254. readBodyToDtoAsync(const std::shared_ptr<oatpp::data::mapping::ObjectMapper>& objectMapper) const {
  255. return m_bodyDecoder->decodeToDtoAsync<Wrapper>(m_headers, m_bodyStream, m_connection, objectMapper);
  256. }
  257. };
  258. }}}}}
  259. #endif /* oatpp_web_protocol_http_incoming_Request_hpp */