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