Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
Adriana Kobylak | 0e1cf26 | 2019-12-05 13:57:57 -0600 | [diff] [blame] | 2 | #include "config.h" |
| 3 | |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 4 | #include "http_response.h" |
| 5 | #include "logging.h" |
| 6 | #include "middleware_context.h" |
| 7 | #include "timer_queue.h" |
| 8 | #include "utility.h" |
| 9 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 10 | #include "authorization.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 11 | #include "http_utility.hpp" |
| 12 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 13 | #include <boost/algorithm/string.hpp> |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 14 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 15 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 16 | #include <boost/asio/ip/tcp.hpp> |
Ed Tanous | 2f1ebcd | 2019-02-13 19:39:07 -0800 | [diff] [blame] | 17 | #include <boost/asio/ssl.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 18 | #include <boost/beast/core/flat_static_buffer.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 19 | #include <boost/beast/http.hpp> |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 20 | #include <boost/beast/ssl/ssl_stream.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 21 | #include <boost/beast/websocket.hpp> |
| 22 | #include <ssl_key_handler.hpp> |
| 23 | |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 24 | #include <atomic> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 25 | #include <chrono> |
| 26 | #include <vector> |
| 27 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 28 | namespace crow |
| 29 | { |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 30 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 31 | inline void prettyPrintJson(crow::Response& res) |
| 32 | { |
Jason M. Bills | 193ad2f | 2018-09-26 15:08:52 -0700 | [diff] [blame] | 33 | std::string value = res.jsonValue.dump(4, ' ', true); |
Ed Tanous | a29c997 | 2018-11-29 15:54:32 -0800 | [diff] [blame] | 34 | utility::escapeHtml(value); |
| 35 | utility::convertToLinks(value); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 36 | res.body() = "<html>\n" |
| 37 | "<head>\n" |
| 38 | "<title>Redfish API</title>\n" |
| 39 | "<link rel=\"stylesheet\" type=\"text/css\" " |
| 40 | "href=\"/styles/default.css\">\n" |
| 41 | "<script src=\"/highlight.pack.js\"></script>" |
| 42 | "<script>hljs.initHighlightingOnLoad();</script>" |
| 43 | "</head>\n" |
| 44 | "<body>\n" |
| 45 | "<div style=\"max-width: 576px;margin:0 auto;\">\n" |
| 46 | "<img src=\"/DMTF_Redfish_logo_2017.svg\" alt=\"redfish\" " |
| 47 | "height=\"406px\" " |
| 48 | "width=\"576px\">\n" |
| 49 | "<br>\n" |
| 50 | "<pre>\n" |
| 51 | "<code class=\"json\">" + |
| 52 | value + |
| 53 | "</code>\n" |
| 54 | "</pre>\n" |
| 55 | "</div>\n" |
| 56 | "</body>\n" |
| 57 | "</html>\n"; |
Ed Tanous | 93ef580 | 2019-01-03 10:15:41 -0800 | [diff] [blame] | 58 | res.addHeader("Content-Type", "text/html;charset=UTF-8"); |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 61 | using namespace boost; |
| 62 | using tcp = asio::ip::tcp; |
| 63 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | namespace detail |
| 65 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 66 | template <typename MW> |
| 67 | struct CheckBeforeHandleArity3Const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 68 | { |
| 69 | template <typename T, |
| 70 | void (T::*)(Request&, Response&, typename MW::Context&) const = |
| 71 | &T::beforeHandle> |
| 72 | struct Get |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 73 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 76 | template <typename MW> |
| 77 | struct CheckBeforeHandleArity3 |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 78 | { |
| 79 | template <typename T, void (T::*)(Request&, Response&, |
| 80 | typename MW::Context&) = &T::beforeHandle> |
| 81 | struct Get |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 82 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 85 | template <typename MW> |
| 86 | struct CheckAfterHandleArity3Const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 87 | { |
| 88 | template <typename T, |
| 89 | void (T::*)(Request&, Response&, typename MW::Context&) const = |
| 90 | &T::afterHandle> |
| 91 | struct Get |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 92 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 95 | template <typename MW> |
| 96 | struct CheckAfterHandleArity3 |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 97 | { |
| 98 | template <typename T, void (T::*)(Request&, Response&, |
| 99 | typename MW::Context&) = &T::afterHandle> |
| 100 | struct Get |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 101 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 104 | template <typename T> |
| 105 | struct IsBeforeHandleArity3Impl |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 106 | { |
| 107 | template <typename C> |
| 108 | static std::true_type |
| 109 | f(typename CheckBeforeHandleArity3Const<T>::template Get<C>*); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 110 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 111 | template <typename C> |
| 112 | static std::true_type |
| 113 | f(typename CheckBeforeHandleArity3<T>::template Get<C>*); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 114 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 115 | template <typename C> |
| 116 | static std::false_type f(...); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 117 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 118 | public: |
Ed Tanous | 0c838cf | 2019-10-24 10:01:46 -0700 | [diff] [blame] | 119 | static constexpr bool value = decltype(f<T>(nullptr))::value; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 122 | template <typename T> |
| 123 | struct IsAfterHandleArity3Impl |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 124 | { |
| 125 | template <typename C> |
| 126 | static std::true_type |
| 127 | f(typename CheckAfterHandleArity3Const<T>::template Get<C>*); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 128 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 129 | template <typename C> |
| 130 | static std::true_type |
| 131 | f(typename CheckAfterHandleArity3<T>::template Get<C>*); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 132 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 133 | template <typename C> |
| 134 | static std::false_type f(...); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 135 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 136 | public: |
Ed Tanous | 0c838cf | 2019-10-24 10:01:46 -0700 | [diff] [blame] | 137 | static constexpr bool value = decltype(f<T>(nullptr))::value; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | template <typename MW, typename Context, typename ParentContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 141 | typename std::enable_if<!IsBeforeHandleArity3Impl<MW>::value>::type |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 142 | beforeHandlerCall(MW& mw, Request& req, Response& res, Context& ctx, |
| 143 | ParentContext& /*parent_ctx*/) |
| 144 | { |
| 145 | mw.beforeHandle(req, res, ctx.template get<MW>(), ctx); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | template <typename MW, typename Context, typename ParentContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 149 | typename std::enable_if<IsBeforeHandleArity3Impl<MW>::value>::type |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 150 | beforeHandlerCall(MW& mw, Request& req, Response& res, Context& ctx, |
| 151 | ParentContext& /*parent_ctx*/) |
| 152 | { |
| 153 | mw.beforeHandle(req, res, ctx.template get<MW>()); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | template <typename MW, typename Context, typename ParentContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 157 | typename std::enable_if<!IsAfterHandleArity3Impl<MW>::value>::type |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 158 | afterHandlerCall(MW& mw, Request& req, Response& res, Context& ctx, |
| 159 | ParentContext& /*parent_ctx*/) |
| 160 | { |
| 161 | mw.afterHandle(req, res, ctx.template get<MW>(), ctx); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | template <typename MW, typename Context, typename ParentContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 165 | typename std::enable_if<IsAfterHandleArity3Impl<MW>::value>::type |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 166 | afterHandlerCall(MW& mw, Request& req, Response& res, Context& ctx, |
| 167 | ParentContext& /*parent_ctx*/) |
| 168 | { |
| 169 | mw.afterHandle(req, res, ctx.template get<MW>()); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 172 | template <size_t N, typename Context, typename Container, typename CurrentMW, |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 173 | typename... Middlewares> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 174 | bool middlewareCallHelper(Container& middlewares, Request& req, Response& res, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 175 | Context& ctx) |
| 176 | { |
| 177 | using parent_context_t = typename Context::template partial<N - 1>; |
| 178 | beforeHandlerCall<CurrentMW, Context, parent_context_t>( |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 179 | std::get<N>(middlewares), req, res, ctx, |
| 180 | static_cast<parent_context_t&>(ctx)); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 181 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 182 | if (res.isCompleted()) |
| 183 | { |
| 184 | afterHandlerCall<CurrentMW, Context, parent_context_t>( |
| 185 | std::get<N>(middlewares), req, res, ctx, |
| 186 | static_cast<parent_context_t&>(ctx)); |
| 187 | return true; |
| 188 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 189 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 190 | if (middlewareCallHelper<N + 1, Context, Container, Middlewares...>( |
| 191 | middlewares, req, res, ctx)) |
| 192 | { |
| 193 | afterHandlerCall<CurrentMW, Context, parent_context_t>( |
| 194 | std::get<N>(middlewares), req, res, ctx, |
| 195 | static_cast<parent_context_t&>(ctx)); |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | return false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 202 | template <size_t N, typename Context, typename Container> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 203 | bool middlewareCallHelper(Container& /*middlewares*/, Request& /*req*/, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 204 | Response& /*res*/, Context& /*ctx*/) |
| 205 | { |
| 206 | return false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 209 | template <size_t N, typename Context, typename Container> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 210 | typename std::enable_if<(N < 0)>::type |
| 211 | afterHandlersCallHelper(Container& /*middlewares*/, Context& /*Context*/, |
| 212 | Request& /*req*/, Response& /*res*/) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 213 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 214 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 215 | template <size_t N, typename Context, typename Container> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 216 | typename std::enable_if<(N == 0)>::type |
| 217 | afterHandlersCallHelper(Container& middlewares, Context& ctx, Request& req, |
| 218 | Response& res) |
| 219 | { |
| 220 | using parent_context_t = typename Context::template partial<N - 1>; |
| 221 | using CurrentMW = typename std::tuple_element< |
| 222 | N, typename std::remove_reference<Container>::type>::type; |
| 223 | afterHandlerCall<CurrentMW, Context, parent_context_t>( |
| 224 | std::get<N>(middlewares), req, res, ctx, |
| 225 | static_cast<parent_context_t&>(ctx)); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 226 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 227 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 228 | template <size_t N, typename Context, typename Container> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 229 | typename std::enable_if<(N > 0)>::type |
| 230 | afterHandlersCallHelper(Container& middlewares, Context& ctx, Request& req, |
| 231 | Response& res) |
| 232 | { |
| 233 | using parent_context_t = typename Context::template partial<N - 1>; |
| 234 | using CurrentMW = typename std::tuple_element< |
| 235 | N, typename std::remove_reference<Container>::type>::type; |
| 236 | afterHandlerCall<CurrentMW, Context, parent_context_t>( |
| 237 | std::get<N>(middlewares), req, res, ctx, |
| 238 | static_cast<parent_context_t&>(ctx)); |
| 239 | afterHandlersCallHelper<N - 1, Context, Container>(middlewares, ctx, req, |
| 240 | res); |
| 241 | } |
| 242 | } // namespace detail |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 243 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 244 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 245 | static std::atomic<int> connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 246 | #endif |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 247 | |
Adriana Kobylak | 0e1cf26 | 2019-12-05 13:57:57 -0600 | [diff] [blame] | 248 | // request body limit size set by the BMCWEB_HTTP_REQ_BODY_LIMIT_MB option |
| 249 | constexpr unsigned int httpReqBodyLimit = |
| 250 | 1024 * 1024 * BMCWEB_HTTP_REQ_BODY_LIMIT_MB; |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 251 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 252 | constexpr uint64_t loggedOutPostBodyLimit = 4096; |
| 253 | |
| 254 | constexpr uint32_t httpHeaderLimit = 8192; |
| 255 | |
| 256 | // drop all connections after 1 minute, this time limit was chosen |
| 257 | // arbitrarily and can be adjusted later if needed |
| 258 | static constexpr const size_t loggedInAttempts = |
| 259 | (60 / timerQueueTimeoutSeconds); |
| 260 | |
| 261 | static constexpr const size_t loggedOutAttempts = |
| 262 | (15 / timerQueueTimeoutSeconds); |
| 263 | |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 264 | template <typename Adaptor, typename Handler, typename... Middlewares> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 265 | class Connection : |
| 266 | public std::enable_shared_from_this< |
| 267 | Connection<Adaptor, Handler, Middlewares...>> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 268 | { |
| 269 | public: |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 270 | Connection(boost::asio::io_context& ioService, Handler* handlerIn, |
| 271 | const std::string& ServerNameIn, |
| 272 | std::tuple<Middlewares...>* middlewaresIn, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 273 | std::function<std::string()>& get_cached_date_str_f, |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 274 | detail::TimerQueue& timerQueueIn, Adaptor adaptorIn) : |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 275 | adaptor(std::move(adaptorIn)), |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 276 | handler(handlerIn), serverName(ServerNameIn), |
| 277 | middlewares(middlewaresIn), getCachedDateStr(get_cached_date_str_f), |
| 278 | timerQueue(timerQueueIn) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 279 | { |
| 280 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 281 | parser->body_limit(httpReqBodyLimit); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 282 | parser->header_limit(httpHeaderLimit); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 283 | req.emplace(parser->get()); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 284 | |
| 285 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 286 | auto ca_available = !std::filesystem::is_empty( |
| 287 | std::filesystem::path(ensuressl::trustStorePath)); |
| 288 | if (ca_available && crow::persistent_data::SessionStore::getInstance() |
| 289 | .getAuthMethodsConfig() |
| 290 | .tls) |
| 291 | { |
| 292 | adaptor.set_verify_mode(boost::asio::ssl::verify_peer); |
| 293 | SSL_set_session_id_context( |
| 294 | adaptor.native_handle(), |
| 295 | reinterpret_cast<const unsigned char*>(serverName.c_str()), |
Zbigniew Kurzynski | cac94c5 | 2019-11-07 12:55:04 +0100 | [diff] [blame] | 296 | static_cast<unsigned int>(serverName.length())); |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 297 | BMCWEB_LOG_DEBUG << this << " TLS is enabled on this connection."; |
| 298 | } |
| 299 | |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 300 | adaptor.set_verify_callback([this]( |
| 301 | bool preverified, |
| 302 | boost::asio::ssl::verify_context& ctx) { |
| 303 | // do nothing if TLS is disabled |
| 304 | if (!crow::persistent_data::SessionStore::getInstance() |
| 305 | .getAuthMethodsConfig() |
| 306 | .tls) |
| 307 | { |
| 308 | BMCWEB_LOG_DEBUG << this << " TLS auth_config is disabled"; |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 309 | return true; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // We always return true to allow full auth flow |
| 313 | if (!preverified) |
| 314 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 315 | BMCWEB_LOG_DEBUG << this << " TLS preverification failed."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 316 | return true; |
| 317 | } |
| 318 | |
| 319 | X509_STORE_CTX* cts = ctx.native_handle(); |
| 320 | if (cts == nullptr) |
| 321 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 322 | BMCWEB_LOG_DEBUG << this << " Cannot get native TLS handle."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 323 | return true; |
| 324 | } |
| 325 | |
| 326 | // Get certificate |
| 327 | X509* peerCert = |
| 328 | X509_STORE_CTX_get_current_cert(ctx.native_handle()); |
| 329 | if (peerCert == nullptr) |
| 330 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 331 | BMCWEB_LOG_DEBUG << this |
| 332 | << " Cannot get current TLS certificate."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 333 | return true; |
| 334 | } |
| 335 | |
| 336 | // Check if certificate is OK |
| 337 | int error = X509_STORE_CTX_get_error(cts); |
| 338 | if (error != X509_V_OK) |
| 339 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 340 | BMCWEB_LOG_INFO << this << " Last TLS error is: " << error; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 341 | return true; |
| 342 | } |
| 343 | // Check that we have reached final certificate in chain |
| 344 | int32_t depth = X509_STORE_CTX_get_error_depth(cts); |
| 345 | if (depth != 0) |
| 346 | |
| 347 | { |
| 348 | BMCWEB_LOG_DEBUG |
| 349 | << this << " Certificate verification in progress (depth " |
| 350 | << depth << "), waiting to reach final depth"; |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | BMCWEB_LOG_DEBUG << this |
| 355 | << " Certificate verification of final depth"; |
| 356 | |
| 357 | // Verify KeyUsage |
| 358 | bool isKeyUsageDigitalSignature = false; |
| 359 | bool isKeyUsageKeyAgreement = false; |
| 360 | |
| 361 | ASN1_BIT_STRING* usage = static_cast<ASN1_BIT_STRING*>( |
| 362 | X509_get_ext_d2i(peerCert, NID_key_usage, NULL, NULL)); |
| 363 | |
| 364 | if (usage == nullptr) |
| 365 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 366 | BMCWEB_LOG_DEBUG << this << " TLS usage is null"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 367 | return true; |
| 368 | } |
| 369 | |
| 370 | for (int i = 0; i < usage->length; i++) |
| 371 | { |
| 372 | if (KU_DIGITAL_SIGNATURE & usage->data[i]) |
| 373 | { |
| 374 | isKeyUsageDigitalSignature = true; |
| 375 | } |
| 376 | if (KU_KEY_AGREEMENT & usage->data[i]) |
| 377 | { |
| 378 | isKeyUsageKeyAgreement = true; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (!isKeyUsageDigitalSignature || !isKeyUsageKeyAgreement) |
| 383 | { |
| 384 | BMCWEB_LOG_DEBUG << this |
| 385 | << " Certificate ExtendedKeyUsage does " |
| 386 | "not allow provided certificate to " |
| 387 | "be used for user authentication"; |
| 388 | return true; |
| 389 | } |
Zbigniew Kurzynski | 09d02f8 | 2020-03-30 13:41:42 +0200 | [diff] [blame] | 390 | ASN1_BIT_STRING_free(usage); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 391 | |
| 392 | // Determine that ExtendedKeyUsage includes Client Auth |
| 393 | |
| 394 | stack_st_ASN1_OBJECT* extUsage = static_cast<stack_st_ASN1_OBJECT*>( |
| 395 | X509_get_ext_d2i(peerCert, NID_ext_key_usage, NULL, NULL)); |
| 396 | |
| 397 | if (extUsage == nullptr) |
| 398 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 399 | BMCWEB_LOG_DEBUG << this << " TLS extUsage is null"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 400 | return true; |
| 401 | } |
| 402 | |
| 403 | bool isExKeyUsageClientAuth = false; |
| 404 | for (int i = 0; i < sk_ASN1_OBJECT_num(extUsage); i++) |
| 405 | { |
| 406 | if (NID_client_auth == |
| 407 | OBJ_obj2nid(sk_ASN1_OBJECT_value(extUsage, i))) |
| 408 | { |
| 409 | isExKeyUsageClientAuth = true; |
| 410 | break; |
| 411 | } |
| 412 | } |
Zbigniew Kurzynski | 09d02f8 | 2020-03-30 13:41:42 +0200 | [diff] [blame] | 413 | sk_ASN1_OBJECT_free(extUsage); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 414 | |
| 415 | // Certificate has to have proper key usages set |
| 416 | if (!isExKeyUsageClientAuth) |
| 417 | { |
| 418 | BMCWEB_LOG_DEBUG << this |
| 419 | << " Certificate ExtendedKeyUsage does " |
| 420 | "not allow provided certificate to " |
| 421 | "be used for user authentication"; |
| 422 | return true; |
| 423 | } |
| 424 | std::string sslUser; |
| 425 | // Extract username contained in CommonName |
| 426 | sslUser.resize(256, '\0'); |
| 427 | |
| 428 | int status = X509_NAME_get_text_by_NID( |
| 429 | X509_get_subject_name(peerCert), NID_commonName, sslUser.data(), |
| 430 | static_cast<int>(sslUser.size())); |
| 431 | |
| 432 | if (status == -1) |
| 433 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 434 | BMCWEB_LOG_DEBUG |
| 435 | << this << " TLS cannot get username to create session"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 436 | return true; |
| 437 | } |
| 438 | |
| 439 | size_t lastChar = sslUser.find('\0'); |
| 440 | if (lastChar == std::string::npos || lastChar == 0) |
| 441 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 442 | BMCWEB_LOG_DEBUG << this << " Invalid TLS user name"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 443 | return true; |
| 444 | } |
| 445 | sslUser.resize(lastChar); |
| 446 | |
| 447 | session = persistent_data::SessionStore::getInstance() |
| 448 | .generateUserSession( |
| 449 | sslUser, |
| 450 | crow::persistent_data::PersistenceType::TIMEOUT); |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 451 | if (auto sp = session.lock()) |
| 452 | { |
| 453 | BMCWEB_LOG_DEBUG << this |
| 454 | << " Generating TLS session: " << sp->uniqueId; |
| 455 | } |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 456 | return true; |
| 457 | }); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 458 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 459 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 460 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 461 | connectionCount++; |
| 462 | BMCWEB_LOG_DEBUG << this << " Connection open, total " |
| 463 | << connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 464 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 465 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 466 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 467 | ~Connection() |
| 468 | { |
| 469 | res.completeRequestHandler = nullptr; |
| 470 | cancelDeadlineTimer(); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 471 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 472 | connectionCount--; |
| 473 | BMCWEB_LOG_DEBUG << this << " Connection closed, total " |
| 474 | << connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 475 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 478 | Adaptor& socket() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 479 | { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 480 | return adaptor; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 483 | void start() |
| 484 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 485 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 486 | startDeadline(0); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 487 | // TODO(ed) Abstract this to a more clever class with the idea of an |
| 488 | // asynchronous "start" |
| 489 | if constexpr (std::is_same_v<Adaptor, |
| 490 | boost::beast::ssl_stream< |
| 491 | boost::asio::ip::tcp::socket>>) |
| 492 | { |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 493 | adaptor.async_handshake(boost::asio::ssl::stream_base::server, |
| 494 | [this, self(shared_from_this())]( |
| 495 | const boost::system::error_code& ec) { |
| 496 | if (ec) |
| 497 | { |
| 498 | return; |
| 499 | } |
| 500 | doReadHeaders(); |
| 501 | }); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 502 | } |
| 503 | else |
| 504 | { |
| 505 | doReadHeaders(); |
| 506 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 507 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 508 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 509 | void handle() |
| 510 | { |
| 511 | cancelDeadlineTimer(); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 512 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 513 | bool isInvalidRequest = false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 514 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 515 | // Check for HTTP version 1.1. |
| 516 | if (req->version() == 11) |
| 517 | { |
| 518 | if (req->getHeaderValue(boost::beast::http::field::host).empty()) |
| 519 | { |
| 520 | isInvalidRequest = true; |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 521 | res.result(boost::beast::http::status::bad_request); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 524 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 525 | BMCWEB_LOG_INFO << "Request: " |
| 526 | << " " << this << " HTTP/" << req->version() / 10 << "." |
| 527 | << req->version() % 10 << ' ' << req->methodString() |
| 528 | << " " << req->target(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 529 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 530 | needToCallAfterHandlers = false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 531 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 532 | if (!isInvalidRequest) |
| 533 | { |
raviteja-b | 4722efe | 2020-02-03 12:23:18 -0600 | [diff] [blame] | 534 | req->socket = [this, self = shared_from_this()]() -> Adaptor& { |
| 535 | return self->socket(); |
| 536 | }; |
| 537 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 538 | res.completeRequestHandler = [] {}; |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 539 | res.isAliveHelper = [this]() -> bool { return isAlive(); }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 540 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 541 | ctx = detail::Context<Middlewares...>(); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 542 | req->middlewareContext = static_cast<void*>(&ctx); |
| 543 | req->ioService = static_cast<decltype(req->ioService)>( |
| 544 | &adaptor.get_executor().context()); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 545 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 546 | detail::middlewareCallHelper< |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 547 | 0U, decltype(ctx), decltype(*middlewares), Middlewares...>( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 548 | *middlewares, *req, res, ctx); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 549 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 550 | if (!res.completed) |
| 551 | { |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 552 | needToCallAfterHandlers = true; |
| 553 | res.completeRequestHandler = [self(shared_from_this())] { |
| 554 | self->completeRequest(); |
| 555 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 556 | if (req->isUpgrade() && |
| 557 | boost::iequals( |
| 558 | req->getHeaderValue(boost::beast::http::field::upgrade), |
| 559 | "websocket")) |
| 560 | { |
| 561 | handler->handleUpgrade(*req, res, std::move(adaptor)); |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 562 | // delete lambda with self shared_ptr |
| 563 | // to enable connection destruction |
| 564 | res.completeRequestHandler = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 565 | return; |
| 566 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 567 | handler->handle(*req, res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 568 | } |
| 569 | else |
| 570 | { |
| 571 | completeRequest(); |
| 572 | } |
| 573 | } |
| 574 | else |
| 575 | { |
| 576 | completeRequest(); |
| 577 | } |
| 578 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 579 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 580 | bool isAlive() |
| 581 | { |
| 582 | |
| 583 | if constexpr (std::is_same_v<Adaptor, |
| 584 | boost::beast::ssl_stream< |
| 585 | boost::asio::ip::tcp::socket>>) |
| 586 | { |
| 587 | return adaptor.next_layer().is_open(); |
| 588 | } |
| 589 | else |
| 590 | { |
| 591 | return adaptor.is_open(); |
| 592 | } |
| 593 | } |
| 594 | void close() |
| 595 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 596 | if constexpr (std::is_same_v<Adaptor, |
| 597 | boost::beast::ssl_stream< |
| 598 | boost::asio::ip::tcp::socket>>) |
| 599 | { |
| 600 | adaptor.next_layer().close(); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 601 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 602 | if (auto sp = session.lock()) |
| 603 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 604 | BMCWEB_LOG_DEBUG << this |
| 605 | << " Removing TLS session: " << sp->uniqueId; |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 606 | persistent_data::SessionStore::getInstance().removeSession(sp); |
| 607 | } |
| 608 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 609 | } |
| 610 | else |
| 611 | { |
| 612 | adaptor.close(); |
| 613 | } |
| 614 | } |
| 615 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 616 | void completeRequest() |
| 617 | { |
| 618 | BMCWEB_LOG_INFO << "Response: " << this << ' ' << req->url << ' ' |
| 619 | << res.resultInt() << " keepalive=" << req->keepAlive(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 620 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 621 | if (needToCallAfterHandlers) |
| 622 | { |
| 623 | needToCallAfterHandlers = false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 624 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 625 | // call all afterHandler of middlewares |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 626 | detail::afterHandlersCallHelper<sizeof...(Middlewares) - 1, |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 627 | decltype(ctx), |
| 628 | decltype(*middlewares)>( |
| 629 | *middlewares, ctx, *req, res); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 630 | |
| 631 | crow::authorization::cleanupTempSession(*req); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 632 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 633 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 634 | if (!isAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 635 | { |
| 636 | // BMCWEB_LOG_DEBUG << this << " delete (socket is closed) " << |
| 637 | // isReading |
| 638 | // << ' ' << isWriting; |
| 639 | // delete this; |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 640 | |
| 641 | // delete lambda with self shared_ptr |
| 642 | // to enable connection destruction |
| 643 | res.completeRequestHandler = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 644 | return; |
| 645 | } |
| 646 | if (res.body().empty() && !res.jsonValue.empty()) |
| 647 | { |
| 648 | if (http_helpers::requestPrefersHtml(*req)) |
| 649 | { |
| 650 | prettyPrintJson(res); |
| 651 | } |
| 652 | else |
| 653 | { |
| 654 | res.jsonMode(); |
Jason M. Bills | 193ad2f | 2018-09-26 15:08:52 -0700 | [diff] [blame] | 655 | res.body() = res.jsonValue.dump(2, ' ', true); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 656 | } |
| 657 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 658 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 659 | if (res.resultInt() >= 400 && res.body().empty()) |
| 660 | { |
| 661 | res.body() = std::string(res.reason()); |
| 662 | } |
Ed Tanous | 6295bec | 2019-09-03 10:11:01 -0700 | [diff] [blame] | 663 | |
| 664 | if (res.result() == boost::beast::http::status::no_content) |
| 665 | { |
| 666 | // Boost beast throws if content is provided on a no-content |
| 667 | // response. Ideally, this would never happen, but in the case that |
| 668 | // it does, we don't want to throw. |
| 669 | BMCWEB_LOG_CRITICAL |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 670 | << this << " Response content provided but code was no-content"; |
Ed Tanous | 6295bec | 2019-09-03 10:11:01 -0700 | [diff] [blame] | 671 | res.body().clear(); |
| 672 | } |
| 673 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 674 | res.addHeader(boost::beast::http::field::server, serverName); |
| 675 | res.addHeader(boost::beast::http::field::date, getCachedDateStr()); |
| 676 | |
| 677 | res.keepAlive(req->keepAlive()); |
| 678 | |
| 679 | doWrite(); |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 680 | |
| 681 | // delete lambda with self shared_ptr |
| 682 | // to enable connection destruction |
| 683 | res.completeRequestHandler = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | private: |
| 687 | void doReadHeaders() |
| 688 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 689 | BMCWEB_LOG_DEBUG << this << " doReadHeaders"; |
| 690 | |
| 691 | // Clean up any previous Connection. |
| 692 | boost::beast::http::async_read_header( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 693 | adaptor, buffer, *parser, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 694 | [this, |
| 695 | self(shared_from_this())](const boost::system::error_code& ec, |
| 696 | std::size_t bytes_transferred) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 697 | BMCWEB_LOG_ERROR << this << " async_read_header " |
| 698 | << bytes_transferred << " Bytes"; |
| 699 | bool errorWhileReading = false; |
| 700 | if (ec) |
| 701 | { |
| 702 | errorWhileReading = true; |
| 703 | BMCWEB_LOG_ERROR |
| 704 | << this << " Error while reading: " << ec.message(); |
| 705 | } |
| 706 | else |
| 707 | { |
| 708 | // if the adaptor isn't open anymore, and wasn't handed to a |
| 709 | // websocket, treat as an error |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 710 | if (!isAlive() && !req->isUpgrade()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 711 | { |
| 712 | errorWhileReading = true; |
| 713 | } |
| 714 | } |
| 715 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 716 | cancelDeadlineTimer(); |
| 717 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 718 | if (errorWhileReading) |
| 719 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 720 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 721 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 722 | return; |
| 723 | } |
| 724 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 725 | if (!req) |
| 726 | { |
| 727 | close(); |
| 728 | return; |
| 729 | } |
| 730 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 731 | // Compute the url parameters for the request |
| 732 | req->url = req->target(); |
| 733 | std::size_t index = req->url.find("?"); |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 734 | if (index != std::string_view::npos) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 735 | { |
Jason M. Bills | 43fcbe5 | 2018-10-16 15:19:20 -0700 | [diff] [blame] | 736 | req->url = req->url.substr(0, index); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 737 | } |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame^] | 738 | crow::authorization::authenticate(*req, res, session); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 739 | |
| 740 | bool loggedIn = req && req->session; |
| 741 | if (loggedIn) |
| 742 | { |
| 743 | startDeadline(loggedInAttempts); |
| 744 | BMCWEB_LOG_DEBUG << "Starting slow deadline"; |
| 745 | |
| 746 | req->urlParams = QueryString(std::string(req->target())); |
| 747 | } |
| 748 | else |
| 749 | { |
| 750 | const boost::optional<uint64_t> contentLength = |
| 751 | parser->content_length(); |
| 752 | if (contentLength && |
| 753 | *contentLength > loggedOutPostBodyLimit) |
| 754 | { |
| 755 | BMCWEB_LOG_DEBUG << "Content length greater than limit " |
| 756 | << *contentLength; |
| 757 | close(); |
| 758 | return; |
| 759 | } |
| 760 | |
| 761 | startDeadline(loggedOutAttempts); |
| 762 | BMCWEB_LOG_DEBUG << "Starting quick deadline"; |
| 763 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 764 | doRead(); |
| 765 | }); |
| 766 | } |
| 767 | |
| 768 | void doRead() |
| 769 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 770 | BMCWEB_LOG_DEBUG << this << " doRead"; |
| 771 | |
| 772 | boost::beast::http::async_read( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 773 | adaptor, buffer, *parser, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 774 | [this, |
| 775 | self(shared_from_this())](const boost::system::error_code& ec, |
| 776 | std::size_t bytes_transferred) { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 777 | BMCWEB_LOG_DEBUG << this << " async_read " << bytes_transferred |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 778 | << " Bytes"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 779 | |
| 780 | bool errorWhileReading = false; |
| 781 | if (ec) |
| 782 | { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 783 | BMCWEB_LOG_ERROR |
| 784 | << this << " Error while reading: " << ec.message(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 785 | errorWhileReading = true; |
| 786 | } |
| 787 | else |
| 788 | { |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 789 | if (isAlive()) |
| 790 | { |
| 791 | cancelDeadlineTimer(); |
| 792 | bool loggedIn = req && req->session; |
| 793 | if (loggedIn) |
| 794 | { |
| 795 | startDeadline(loggedInAttempts); |
| 796 | } |
| 797 | else |
| 798 | { |
| 799 | startDeadline(loggedOutAttempts); |
| 800 | } |
| 801 | } |
| 802 | else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 803 | { |
| 804 | errorWhileReading = true; |
| 805 | } |
| 806 | } |
| 807 | if (errorWhileReading) |
| 808 | { |
| 809 | cancelDeadlineTimer(); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 810 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 811 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 812 | return; |
| 813 | } |
| 814 | handle(); |
| 815 | }); |
| 816 | } |
| 817 | |
| 818 | void doWrite() |
| 819 | { |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 820 | bool loggedIn = req && req->session; |
| 821 | if (loggedIn) |
| 822 | { |
| 823 | startDeadline(loggedInAttempts); |
| 824 | } |
| 825 | else |
| 826 | { |
| 827 | startDeadline(loggedOutAttempts); |
| 828 | } |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 829 | BMCWEB_LOG_DEBUG << this << " doWrite"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 830 | res.preparePayload(); |
| 831 | serializer.emplace(*res.stringResponse); |
| 832 | boost::beast::http::async_write( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 833 | adaptor, *serializer, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 834 | [this, |
| 835 | self(shared_from_this())](const boost::system::error_code& ec, |
| 836 | std::size_t bytes_transferred) { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 837 | BMCWEB_LOG_DEBUG << this << " async_write " << bytes_transferred |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 838 | << " bytes"; |
| 839 | |
James Feist | 54d8bb1 | 2020-07-20 13:28:59 -0700 | [diff] [blame] | 840 | cancelDeadlineTimer(); |
| 841 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 842 | if (ec) |
| 843 | { |
| 844 | BMCWEB_LOG_DEBUG << this << " from write(2)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 845 | return; |
| 846 | } |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 847 | if (!res.keepAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 848 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 849 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 850 | BMCWEB_LOG_DEBUG << this << " from write(1)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 851 | return; |
| 852 | } |
| 853 | |
| 854 | serializer.reset(); |
| 855 | BMCWEB_LOG_DEBUG << this << " Clearing response"; |
| 856 | res.clear(); |
| 857 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
Gunnar Mills | ded2a1e | 2020-07-24 09:46:33 -0500 | [diff] [blame] | 858 | parser->body_limit(httpReqBodyLimit); // reset body limit for |
| 859 | // newly created parser |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 860 | buffer.consume(buffer.size()); |
| 861 | |
| 862 | req.emplace(parser->get()); |
| 863 | doReadHeaders(); |
| 864 | }); |
| 865 | } |
| 866 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 867 | void cancelDeadlineTimer() |
| 868 | { |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 869 | if (timerCancelKey) |
| 870 | { |
| 871 | BMCWEB_LOG_DEBUG << this << " timer cancelled: " << &timerQueue |
| 872 | << ' ' << *timerCancelKey; |
| 873 | timerQueue.cancel(*timerCancelKey); |
| 874 | timerCancelKey.reset(); |
| 875 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 876 | } |
| 877 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 878 | void startDeadline(size_t timerIterations) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 879 | { |
| 880 | cancelDeadlineTimer(); |
| 881 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 882 | if (timerIterations) |
| 883 | { |
| 884 | timerIterations--; |
| 885 | } |
Jan Sowinski | 2b5e08e | 2020-01-09 17:16:02 +0100 | [diff] [blame] | 886 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 887 | timerCancelKey = |
James Feist | be5dfca | 2020-07-22 08:54:59 -0700 | [diff] [blame] | 888 | timerQueue.add([self(shared_from_this()), timerIterations, |
| 889 | readCount{parser->get().body().size()}] { |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 890 | // Mark timer as not active to avoid canceling it during |
| 891 | // Connection destructor which leads to double free issue |
| 892 | self->timerCancelKey.reset(); |
| 893 | if (!self->isAlive()) |
| 894 | { |
| 895 | return; |
| 896 | } |
Jan Sowinski | 2b5e08e | 2020-01-09 17:16:02 +0100 | [diff] [blame] | 897 | |
James Feist | be5dfca | 2020-07-22 08:54:59 -0700 | [diff] [blame] | 898 | bool loggedIn = self->req && self->req->session; |
| 899 | // allow slow uploads for logged in users |
| 900 | if (loggedIn && self->parser->get().body().size() > readCount) |
| 901 | { |
| 902 | BMCWEB_LOG_DEBUG << self.get() |
| 903 | << " restart timer - read in progress"; |
| 904 | self->startDeadline(timerIterations); |
| 905 | return; |
| 906 | } |
| 907 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 908 | // Threshold can be used to drop slow connections |
| 909 | // to protect against slow-rate DoS attack |
| 910 | if (timerIterations) |
| 911 | { |
James Feist | be5dfca | 2020-07-22 08:54:59 -0700 | [diff] [blame] | 912 | BMCWEB_LOG_DEBUG << self.get() << " restart timer"; |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 913 | self->startDeadline(timerIterations); |
| 914 | return; |
| 915 | } |
| 916 | |
| 917 | self->close(); |
| 918 | }); |
James Feist | cb6cb49 | 2020-04-03 13:36:17 -0700 | [diff] [blame] | 919 | |
| 920 | if (!timerCancelKey) |
| 921 | { |
| 922 | close(); |
| 923 | return; |
| 924 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 925 | BMCWEB_LOG_DEBUG << this << " timer added: " << &timerQueue << ' ' |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 926 | << *timerCancelKey; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | private: |
| 930 | Adaptor adaptor; |
| 931 | Handler* handler; |
| 932 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 933 | // Making this a std::optional allows it to be efficiently destroyed and |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 934 | // re-created on Connection reset |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 935 | std::optional< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 936 | boost::beast::http::request_parser<boost::beast::http::string_body>> |
| 937 | parser; |
| 938 | |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 939 | boost::beast::flat_static_buffer<8192> buffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 940 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 941 | std::optional<boost::beast::http::response_serializer< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 942 | boost::beast::http::string_body>> |
| 943 | serializer; |
| 944 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 945 | std::optional<crow::Request> req; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 946 | crow::Response res; |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 947 | std::weak_ptr<crow::persistent_data::UserSession> session; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 948 | |
| 949 | const std::string& serverName; |
| 950 | |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 951 | std::optional<size_t> timerCancelKey; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 952 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 953 | bool needToCallAfterHandlers{}; |
| 954 | bool needToStartReadAfterComplete{}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 955 | |
| 956 | std::tuple<Middlewares...>* middlewares; |
| 957 | detail::Context<Middlewares...> ctx; |
| 958 | |
| 959 | std::function<std::string()>& getCachedDateStr; |
| 960 | detail::TimerQueue& timerQueue; |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 961 | |
| 962 | using std::enable_shared_from_this< |
| 963 | Connection<Adaptor, Handler, Middlewares...>>::shared_from_this; |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 964 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 965 | } // namespace crow |