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