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