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> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 248 | class Connection |
| 249 | { |
| 250 | public: |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 251 | Connection(boost::asio::io_context& ioService, Handler* handlerIn, |
| 252 | const std::string& ServerNameIn, |
| 253 | std::tuple<Middlewares...>* middlewaresIn, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 254 | std::function<std::string()>& get_cached_date_str_f, |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 255 | detail::TimerQueue& timerQueueIn, Adaptor adaptorIn) : |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 256 | adaptor(std::move(adaptorIn)), |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 257 | handler(handlerIn), serverName(ServerNameIn), |
| 258 | middlewares(middlewaresIn), getCachedDateStr(get_cached_date_str_f), |
| 259 | timerQueue(timerQueueIn) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 260 | { |
| 261 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
| 262 | // Temporarily changed to 30MB; Need to modify uploading/authentication |
| 263 | // mechanism |
| 264 | parser->body_limit(httpReqBodyLimit); |
| 265 | req.emplace(parser->get()); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 266 | |
| 267 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame^] | 268 | auto ca_available = !std::filesystem::is_empty( |
| 269 | std::filesystem::path(ensuressl::trustStorePath)); |
| 270 | if (ca_available && crow::persistent_data::SessionStore::getInstance() |
| 271 | .getAuthMethodsConfig() |
| 272 | .tls) |
| 273 | { |
| 274 | adaptor.set_verify_mode(boost::asio::ssl::verify_peer); |
| 275 | SSL_set_session_id_context( |
| 276 | adaptor.native_handle(), |
| 277 | reinterpret_cast<const unsigned char*>(serverName.c_str()), |
| 278 | serverName.length()); |
| 279 | BMCWEB_LOG_DEBUG << this << " TLS is enabled on this connection."; |
| 280 | } |
| 281 | |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 282 | adaptor.set_verify_callback([this]( |
| 283 | bool preverified, |
| 284 | boost::asio::ssl::verify_context& ctx) { |
| 285 | // do nothing if TLS is disabled |
| 286 | if (!crow::persistent_data::SessionStore::getInstance() |
| 287 | .getAuthMethodsConfig() |
| 288 | .tls) |
| 289 | { |
| 290 | BMCWEB_LOG_DEBUG << this << " TLS auth_config is disabled"; |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 291 | return true; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // We always return true to allow full auth flow |
| 295 | if (!preverified) |
| 296 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame^] | 297 | BMCWEB_LOG_DEBUG << this << " TLS preverification failed."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 298 | return true; |
| 299 | } |
| 300 | |
| 301 | X509_STORE_CTX* cts = ctx.native_handle(); |
| 302 | if (cts == nullptr) |
| 303 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame^] | 304 | BMCWEB_LOG_DEBUG << this << " Cannot get native TLS handle."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 305 | return true; |
| 306 | } |
| 307 | |
| 308 | // Get certificate |
| 309 | X509* peerCert = |
| 310 | X509_STORE_CTX_get_current_cert(ctx.native_handle()); |
| 311 | if (peerCert == nullptr) |
| 312 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame^] | 313 | BMCWEB_LOG_DEBUG << this |
| 314 | << " Cannot get current TLS certificate."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 315 | return true; |
| 316 | } |
| 317 | |
| 318 | // Check if certificate is OK |
| 319 | int error = X509_STORE_CTX_get_error(cts); |
| 320 | if (error != X509_V_OK) |
| 321 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame^] | 322 | BMCWEB_LOG_INFO << this << " Last TLS error is: " << error; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 323 | return true; |
| 324 | } |
| 325 | // Check that we have reached final certificate in chain |
| 326 | int32_t depth = X509_STORE_CTX_get_error_depth(cts); |
| 327 | if (depth != 0) |
| 328 | |
| 329 | { |
| 330 | BMCWEB_LOG_DEBUG |
| 331 | << this << " Certificate verification in progress (depth " |
| 332 | << depth << "), waiting to reach final depth"; |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | BMCWEB_LOG_DEBUG << this |
| 337 | << " Certificate verification of final depth"; |
| 338 | |
| 339 | // Verify KeyUsage |
| 340 | bool isKeyUsageDigitalSignature = false; |
| 341 | bool isKeyUsageKeyAgreement = false; |
| 342 | |
| 343 | ASN1_BIT_STRING* usage = static_cast<ASN1_BIT_STRING*>( |
| 344 | X509_get_ext_d2i(peerCert, NID_key_usage, NULL, NULL)); |
| 345 | |
| 346 | if (usage == nullptr) |
| 347 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame^] | 348 | BMCWEB_LOG_DEBUG << this << " TLS usage is null"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 349 | return true; |
| 350 | } |
| 351 | |
| 352 | for (int i = 0; i < usage->length; i++) |
| 353 | { |
| 354 | if (KU_DIGITAL_SIGNATURE & usage->data[i]) |
| 355 | { |
| 356 | isKeyUsageDigitalSignature = true; |
| 357 | } |
| 358 | if (KU_KEY_AGREEMENT & usage->data[i]) |
| 359 | { |
| 360 | isKeyUsageKeyAgreement = true; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if (!isKeyUsageDigitalSignature || !isKeyUsageKeyAgreement) |
| 365 | { |
| 366 | BMCWEB_LOG_DEBUG << this |
| 367 | << " Certificate ExtendedKeyUsage does " |
| 368 | "not allow provided certificate to " |
| 369 | "be used for user authentication"; |
| 370 | return true; |
| 371 | } |
| 372 | |
| 373 | // Determine that ExtendedKeyUsage includes Client Auth |
| 374 | |
| 375 | stack_st_ASN1_OBJECT* extUsage = static_cast<stack_st_ASN1_OBJECT*>( |
| 376 | X509_get_ext_d2i(peerCert, NID_ext_key_usage, NULL, NULL)); |
| 377 | |
| 378 | if (extUsage == nullptr) |
| 379 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame^] | 380 | BMCWEB_LOG_DEBUG << this << " TLS extUsage is null"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 381 | return true; |
| 382 | } |
| 383 | |
| 384 | bool isExKeyUsageClientAuth = false; |
| 385 | for (int i = 0; i < sk_ASN1_OBJECT_num(extUsage); i++) |
| 386 | { |
| 387 | if (NID_client_auth == |
| 388 | OBJ_obj2nid(sk_ASN1_OBJECT_value(extUsage, i))) |
| 389 | { |
| 390 | isExKeyUsageClientAuth = true; |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | // Certificate has to have proper key usages set |
| 396 | if (!isExKeyUsageClientAuth) |
| 397 | { |
| 398 | BMCWEB_LOG_DEBUG << this |
| 399 | << " Certificate ExtendedKeyUsage does " |
| 400 | "not allow provided certificate to " |
| 401 | "be used for user authentication"; |
| 402 | return true; |
| 403 | } |
| 404 | std::string sslUser; |
| 405 | // Extract username contained in CommonName |
| 406 | sslUser.resize(256, '\0'); |
| 407 | |
| 408 | int status = X509_NAME_get_text_by_NID( |
| 409 | X509_get_subject_name(peerCert), NID_commonName, sslUser.data(), |
| 410 | static_cast<int>(sslUser.size())); |
| 411 | |
| 412 | if (status == -1) |
| 413 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame^] | 414 | BMCWEB_LOG_DEBUG |
| 415 | << this << " TLS cannot get username to create session"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 416 | return true; |
| 417 | } |
| 418 | |
| 419 | size_t lastChar = sslUser.find('\0'); |
| 420 | if (lastChar == std::string::npos || lastChar == 0) |
| 421 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame^] | 422 | BMCWEB_LOG_DEBUG << this << " Invalid TLS user name"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 423 | return true; |
| 424 | } |
| 425 | sslUser.resize(lastChar); |
| 426 | |
| 427 | session = persistent_data::SessionStore::getInstance() |
| 428 | .generateUserSession( |
| 429 | sslUser, |
| 430 | crow::persistent_data::PersistenceType::TIMEOUT); |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame^] | 431 | if (auto sp = session.lock()) |
| 432 | { |
| 433 | BMCWEB_LOG_DEBUG << this |
| 434 | << " Generating TLS session: " << sp->uniqueId; |
| 435 | } |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 436 | return true; |
| 437 | }); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 438 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 439 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 440 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 441 | connectionCount++; |
| 442 | BMCWEB_LOG_DEBUG << this << " Connection open, total " |
| 443 | << connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 444 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 445 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 446 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 447 | ~Connection() |
| 448 | { |
| 449 | res.completeRequestHandler = nullptr; |
| 450 | cancelDeadlineTimer(); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 451 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 452 | connectionCount--; |
| 453 | BMCWEB_LOG_DEBUG << this << " Connection closed, total " |
| 454 | << connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 455 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 458 | Adaptor& socket() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 459 | { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 460 | return adaptor; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 463 | void start() |
| 464 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 465 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 466 | startDeadline(); |
| 467 | // TODO(ed) Abstract this to a more clever class with the idea of an |
| 468 | // asynchronous "start" |
| 469 | if constexpr (std::is_same_v<Adaptor, |
| 470 | boost::beast::ssl_stream< |
| 471 | boost::asio::ip::tcp::socket>>) |
| 472 | { |
| 473 | adaptor.async_handshake( |
| 474 | boost::asio::ssl::stream_base::server, |
| 475 | [this](const boost::system::error_code& ec) { |
| 476 | if (ec) |
| 477 | { |
| 478 | checkDestroy(); |
| 479 | return; |
| 480 | } |
| 481 | doReadHeaders(); |
| 482 | }); |
| 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 + |
| 543 | "; Secure; HttpOnly"); |
| 544 | BMCWEB_LOG_DEBUG |
| 545 | << this << " TLS session: " << sp->uniqueId |
| 546 | << " with cookie will be used for this request."; |
| 547 | req->session = sp; |
| 548 | } |
| 549 | } |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 550 | } |
| 551 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 552 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 553 | detail::middlewareCallHelper< |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 554 | 0U, decltype(ctx), decltype(*middlewares), Middlewares...>( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 555 | *middlewares, *req, res, ctx); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 556 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 557 | if (!res.completed) |
| 558 | { |
| 559 | if (req->isUpgrade() && |
| 560 | boost::iequals( |
| 561 | req->getHeaderValue(boost::beast::http::field::upgrade), |
| 562 | "websocket")) |
| 563 | { |
| 564 | handler->handleUpgrade(*req, res, std::move(adaptor)); |
| 565 | return; |
| 566 | } |
| 567 | res.completeRequestHandler = [this] { |
| 568 | this->completeRequest(); |
| 569 | }; |
| 570 | needToCallAfterHandlers = true; |
| 571 | handler->handle(*req, res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 572 | } |
| 573 | else |
| 574 | { |
| 575 | completeRequest(); |
| 576 | } |
| 577 | } |
| 578 | else |
| 579 | { |
| 580 | completeRequest(); |
| 581 | } |
| 582 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 583 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 584 | bool isAlive() |
| 585 | { |
| 586 | |
| 587 | if constexpr (std::is_same_v<Adaptor, |
| 588 | boost::beast::ssl_stream< |
| 589 | boost::asio::ip::tcp::socket>>) |
| 590 | { |
| 591 | return adaptor.next_layer().is_open(); |
| 592 | } |
| 593 | else |
| 594 | { |
| 595 | return adaptor.is_open(); |
| 596 | } |
| 597 | } |
| 598 | void close() |
| 599 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 600 | if constexpr (std::is_same_v<Adaptor, |
| 601 | boost::beast::ssl_stream< |
| 602 | boost::asio::ip::tcp::socket>>) |
| 603 | { |
| 604 | adaptor.next_layer().close(); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 605 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 606 | if (auto sp = session.lock()) |
| 607 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame^] | 608 | BMCWEB_LOG_DEBUG << this |
| 609 | << " Removing TLS session: " << sp->uniqueId; |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 610 | persistent_data::SessionStore::getInstance().removeSession(sp); |
| 611 | } |
| 612 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 613 | } |
| 614 | else |
| 615 | { |
| 616 | adaptor.close(); |
| 617 | } |
| 618 | } |
| 619 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 620 | void completeRequest() |
| 621 | { |
| 622 | BMCWEB_LOG_INFO << "Response: " << this << ' ' << req->url << ' ' |
| 623 | << res.resultInt() << " keepalive=" << req->keepAlive(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 624 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 625 | if (needToCallAfterHandlers) |
| 626 | { |
| 627 | needToCallAfterHandlers = false; |
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 | // call all afterHandler of middlewares |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 630 | detail::afterHandlersCallHelper<sizeof...(Middlewares) - 1, |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 631 | decltype(ctx), |
| 632 | decltype(*middlewares)>( |
| 633 | *middlewares, ctx, *req, res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 634 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 635 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 636 | // auto self = this->shared_from_this(); |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 637 | res.completeRequestHandler = res.completeRequestHandler = [] {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 638 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 639 | if (!isAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 640 | { |
| 641 | // BMCWEB_LOG_DEBUG << this << " delete (socket is closed) " << |
| 642 | // isReading |
| 643 | // << ' ' << isWriting; |
| 644 | // delete this; |
| 645 | return; |
| 646 | } |
| 647 | if (res.body().empty() && !res.jsonValue.empty()) |
| 648 | { |
| 649 | if (http_helpers::requestPrefersHtml(*req)) |
| 650 | { |
| 651 | prettyPrintJson(res); |
| 652 | } |
| 653 | else |
| 654 | { |
| 655 | res.jsonMode(); |
Jason M. Bills | 193ad2f | 2018-09-26 15:08:52 -0700 | [diff] [blame] | 656 | res.body() = res.jsonValue.dump(2, ' ', true); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 657 | } |
| 658 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 659 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 660 | if (res.resultInt() >= 400 && res.body().empty()) |
| 661 | { |
| 662 | res.body() = std::string(res.reason()); |
| 663 | } |
Ed Tanous | 6295bec | 2019-09-03 10:11:01 -0700 | [diff] [blame] | 664 | |
| 665 | if (res.result() == boost::beast::http::status::no_content) |
| 666 | { |
| 667 | // Boost beast throws if content is provided on a no-content |
| 668 | // response. Ideally, this would never happen, but in the case that |
| 669 | // it does, we don't want to throw. |
| 670 | BMCWEB_LOG_CRITICAL |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 671 | << this << " Response content provided but code was no-content"; |
Ed Tanous | 6295bec | 2019-09-03 10:11:01 -0700 | [diff] [blame] | 672 | res.body().clear(); |
| 673 | } |
| 674 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 675 | res.addHeader(boost::beast::http::field::server, serverName); |
| 676 | res.addHeader(boost::beast::http::field::date, getCachedDateStr()); |
| 677 | |
| 678 | res.keepAlive(req->keepAlive()); |
| 679 | |
| 680 | doWrite(); |
| 681 | } |
| 682 | |
| 683 | private: |
| 684 | void doReadHeaders() |
| 685 | { |
| 686 | // auto self = this->shared_from_this(); |
| 687 | isReading = true; |
| 688 | BMCWEB_LOG_DEBUG << this << " doReadHeaders"; |
| 689 | |
| 690 | // Clean up any previous Connection. |
| 691 | boost::beast::http::async_read_header( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 692 | adaptor, buffer, *parser, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 693 | [this](const boost::system::error_code& ec, |
| 694 | std::size_t bytes_transferred) { |
| 695 | isReading = false; |
| 696 | BMCWEB_LOG_ERROR << this << " async_read_header " |
| 697 | << bytes_transferred << " Bytes"; |
| 698 | bool errorWhileReading = false; |
| 699 | if (ec) |
| 700 | { |
| 701 | errorWhileReading = true; |
| 702 | BMCWEB_LOG_ERROR |
| 703 | << this << " Error while reading: " << ec.message(); |
| 704 | } |
| 705 | else |
| 706 | { |
| 707 | // if the adaptor isn't open anymore, and wasn't handed to a |
| 708 | // websocket, treat as an error |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 709 | if (!isAlive() && !req->isUpgrade()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 710 | { |
| 711 | errorWhileReading = true; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | if (errorWhileReading) |
| 716 | { |
| 717 | cancelDeadlineTimer(); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 718 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 719 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
| 720 | checkDestroy(); |
| 721 | return; |
| 722 | } |
| 723 | |
| 724 | // Compute the url parameters for the request |
| 725 | req->url = req->target(); |
| 726 | std::size_t index = req->url.find("?"); |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 727 | if (index != std::string_view::npos) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 728 | { |
Jason M. Bills | 43fcbe5 | 2018-10-16 15:19:20 -0700 | [diff] [blame] | 729 | req->url = req->url.substr(0, index); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 730 | } |
| 731 | req->urlParams = QueryString(std::string(req->target())); |
| 732 | doRead(); |
| 733 | }); |
| 734 | } |
| 735 | |
| 736 | void doRead() |
| 737 | { |
| 738 | // auto self = this->shared_from_this(); |
| 739 | isReading = true; |
| 740 | BMCWEB_LOG_DEBUG << this << " doRead"; |
| 741 | |
| 742 | boost::beast::http::async_read( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 743 | adaptor, buffer, *parser, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 744 | [this](const boost::system::error_code& ec, |
| 745 | std::size_t bytes_transferred) { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 746 | BMCWEB_LOG_DEBUG << this << " async_read " << bytes_transferred |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 747 | << " Bytes"; |
| 748 | isReading = false; |
| 749 | |
| 750 | bool errorWhileReading = false; |
| 751 | if (ec) |
| 752 | { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 753 | BMCWEB_LOG_ERROR |
| 754 | << this << " Error while reading: " << ec.message(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 755 | errorWhileReading = true; |
| 756 | } |
| 757 | else |
| 758 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 759 | if (!isAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 760 | { |
| 761 | errorWhileReading = true; |
| 762 | } |
| 763 | } |
| 764 | if (errorWhileReading) |
| 765 | { |
| 766 | cancelDeadlineTimer(); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 767 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 768 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
| 769 | checkDestroy(); |
| 770 | return; |
| 771 | } |
| 772 | handle(); |
| 773 | }); |
| 774 | } |
| 775 | |
| 776 | void doWrite() |
| 777 | { |
| 778 | // auto self = this->shared_from_this(); |
| 779 | isWriting = true; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 780 | BMCWEB_LOG_DEBUG << this << " doWrite"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 781 | res.preparePayload(); |
| 782 | serializer.emplace(*res.stringResponse); |
| 783 | boost::beast::http::async_write( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 784 | adaptor, *serializer, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 785 | [&](const boost::system::error_code& ec, |
| 786 | std::size_t bytes_transferred) { |
| 787 | isWriting = false; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 788 | BMCWEB_LOG_DEBUG << this << " async_write " << bytes_transferred |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 789 | << " bytes"; |
| 790 | |
| 791 | if (ec) |
| 792 | { |
| 793 | BMCWEB_LOG_DEBUG << this << " from write(2)"; |
| 794 | checkDestroy(); |
| 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)"; |
| 801 | checkDestroy(); |
| 802 | return; |
| 803 | } |
| 804 | |
| 805 | serializer.reset(); |
| 806 | BMCWEB_LOG_DEBUG << this << " Clearing response"; |
| 807 | res.clear(); |
| 808 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
| 809 | parser->body_limit(httpReqBodyLimit); // reset body limit for |
| 810 | // newly created parser |
| 811 | buffer.consume(buffer.size()); |
| 812 | |
| 813 | req.emplace(parser->get()); |
| 814 | doReadHeaders(); |
| 815 | }); |
| 816 | } |
| 817 | |
| 818 | void checkDestroy() |
| 819 | { |
| 820 | BMCWEB_LOG_DEBUG << this << " isReading " << isReading << " isWriting " |
| 821 | << isWriting; |
| 822 | if (!isReading && !isWriting) |
| 823 | { |
| 824 | BMCWEB_LOG_DEBUG << this << " delete (idle) "; |
| 825 | delete this; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | void cancelDeadlineTimer() |
| 830 | { |
| 831 | BMCWEB_LOG_DEBUG << this << " timer cancelled: " << &timerQueue << ' ' |
| 832 | << timerCancelKey; |
| 833 | timerQueue.cancel(timerCancelKey); |
| 834 | } |
| 835 | |
| 836 | void startDeadline() |
| 837 | { |
| 838 | cancelDeadlineTimer(); |
| 839 | |
| 840 | timerCancelKey = timerQueue.add([this] { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 841 | if (!isAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 842 | { |
| 843 | return; |
| 844 | } |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 845 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 846 | }); |
| 847 | BMCWEB_LOG_DEBUG << this << " timer added: " << &timerQueue << ' ' |
| 848 | << timerCancelKey; |
| 849 | } |
| 850 | |
| 851 | private: |
| 852 | Adaptor adaptor; |
| 853 | Handler* handler; |
| 854 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 855 | // Making this a std::optional allows it to be efficiently destroyed and |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 856 | // re-created on Connection reset |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 857 | std::optional< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 858 | boost::beast::http::request_parser<boost::beast::http::string_body>> |
| 859 | parser; |
| 860 | |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 861 | boost::beast::flat_static_buffer<8192> buffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 862 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 863 | std::optional<boost::beast::http::response_serializer< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 864 | boost::beast::http::string_body>> |
| 865 | serializer; |
| 866 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 867 | std::optional<crow::Request> req; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 868 | crow::Response res; |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 869 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 870 | std::weak_ptr<crow::persistent_data::UserSession> session; |
| 871 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 872 | |
| 873 | const std::string& serverName; |
| 874 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 875 | size_t timerCancelKey = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 876 | |
| 877 | bool isReading{}; |
| 878 | bool isWriting{}; |
| 879 | bool needToCallAfterHandlers{}; |
| 880 | bool needToStartReadAfterComplete{}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 881 | |
| 882 | std::tuple<Middlewares...>* middlewares; |
| 883 | detail::Context<Middlewares...> ctx; |
| 884 | |
| 885 | std::function<std::string()>& getCachedDateStr; |
| 886 | detail::TimerQueue& timerQueue; |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 887 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 888 | } // namespace crow |