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: |
| 116 | static const 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: |
| 132 | static const 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()); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 266 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 267 | connectionCount++; |
| 268 | BMCWEB_LOG_DEBUG << this << " Connection open, total " |
| 269 | << connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 270 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 271 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 272 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 273 | ~Connection() |
| 274 | { |
| 275 | res.completeRequestHandler = nullptr; |
| 276 | cancelDeadlineTimer(); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 277 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 278 | connectionCount--; |
| 279 | BMCWEB_LOG_DEBUG << this << " Connection closed, total " |
| 280 | << connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 281 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 284 | Adaptor& socket() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 285 | { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 286 | return adaptor; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 289 | void start() |
| 290 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 291 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 292 | startDeadline(); |
| 293 | // TODO(ed) Abstract this to a more clever class with the idea of an |
| 294 | // asynchronous "start" |
| 295 | if constexpr (std::is_same_v<Adaptor, |
| 296 | boost::beast::ssl_stream< |
| 297 | boost::asio::ip::tcp::socket>>) |
| 298 | { |
| 299 | adaptor.async_handshake( |
| 300 | boost::asio::ssl::stream_base::server, |
| 301 | [this](const boost::system::error_code& ec) { |
| 302 | if (ec) |
| 303 | { |
| 304 | checkDestroy(); |
| 305 | return; |
| 306 | } |
| 307 | doReadHeaders(); |
| 308 | }); |
| 309 | } |
| 310 | else |
| 311 | { |
| 312 | doReadHeaders(); |
| 313 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 314 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 315 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 316 | void handle() |
| 317 | { |
| 318 | cancelDeadlineTimer(); |
| 319 | bool isInvalidRequest = false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 320 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 321 | // Check for HTTP version 1.1. |
| 322 | if (req->version() == 11) |
| 323 | { |
| 324 | if (req->getHeaderValue(boost::beast::http::field::host).empty()) |
| 325 | { |
| 326 | isInvalidRequest = true; |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 327 | res.result(boost::beast::http::status::bad_request); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 330 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 331 | BMCWEB_LOG_INFO << "Request: " |
| 332 | << " " << this << " HTTP/" << req->version() / 10 << "." |
| 333 | << req->version() % 10 << ' ' << req->methodString() |
| 334 | << " " << req->target(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 335 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 336 | needToCallAfterHandlers = false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 337 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 338 | if (!isInvalidRequest) |
| 339 | { |
| 340 | res.completeRequestHandler = [] {}; |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 341 | res.isAliveHelper = [this]() -> bool { return isAlive(); }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 342 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 343 | ctx = detail::Context<Middlewares...>(); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 344 | req->middlewareContext = static_cast<void*>(&ctx); |
| 345 | req->ioService = static_cast<decltype(req->ioService)>( |
| 346 | &adaptor.get_executor().context()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 347 | detail::middlewareCallHelper< |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 348 | 0U, decltype(ctx), decltype(*middlewares), Middlewares...>( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 349 | *middlewares, *req, res, ctx); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 350 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 351 | if (!res.completed) |
| 352 | { |
| 353 | if (req->isUpgrade() && |
| 354 | boost::iequals( |
| 355 | req->getHeaderValue(boost::beast::http::field::upgrade), |
| 356 | "websocket")) |
| 357 | { |
| 358 | handler->handleUpgrade(*req, res, std::move(adaptor)); |
| 359 | return; |
| 360 | } |
| 361 | res.completeRequestHandler = [this] { |
| 362 | this->completeRequest(); |
| 363 | }; |
| 364 | needToCallAfterHandlers = true; |
| 365 | handler->handle(*req, res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 366 | } |
| 367 | else |
| 368 | { |
| 369 | completeRequest(); |
| 370 | } |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | completeRequest(); |
| 375 | } |
| 376 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 377 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 378 | bool isAlive() |
| 379 | { |
| 380 | |
| 381 | if constexpr (std::is_same_v<Adaptor, |
| 382 | boost::beast::ssl_stream< |
| 383 | boost::asio::ip::tcp::socket>>) |
| 384 | { |
| 385 | return adaptor.next_layer().is_open(); |
| 386 | } |
| 387 | else |
| 388 | { |
| 389 | return adaptor.is_open(); |
| 390 | } |
| 391 | } |
| 392 | void close() |
| 393 | { |
| 394 | |
| 395 | if constexpr (std::is_same_v<Adaptor, |
| 396 | boost::beast::ssl_stream< |
| 397 | boost::asio::ip::tcp::socket>>) |
| 398 | { |
| 399 | adaptor.next_layer().close(); |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | adaptor.close(); |
| 404 | } |
| 405 | } |
| 406 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 407 | void completeRequest() |
| 408 | { |
| 409 | BMCWEB_LOG_INFO << "Response: " << this << ' ' << req->url << ' ' |
| 410 | << res.resultInt() << " keepalive=" << req->keepAlive(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 411 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 412 | if (needToCallAfterHandlers) |
| 413 | { |
| 414 | needToCallAfterHandlers = false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 415 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 416 | // call all afterHandler of middlewares |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 417 | detail::afterHandlersCallHelper<sizeof...(Middlewares) - 1, |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 418 | decltype(ctx), |
| 419 | decltype(*middlewares)>( |
| 420 | *middlewares, ctx, *req, res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 421 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 422 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 423 | // auto self = this->shared_from_this(); |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 424 | res.completeRequestHandler = res.completeRequestHandler = [] {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 425 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 426 | if (!isAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 427 | { |
| 428 | // BMCWEB_LOG_DEBUG << this << " delete (socket is closed) " << |
| 429 | // isReading |
| 430 | // << ' ' << isWriting; |
| 431 | // delete this; |
| 432 | return; |
| 433 | } |
| 434 | if (res.body().empty() && !res.jsonValue.empty()) |
| 435 | { |
| 436 | if (http_helpers::requestPrefersHtml(*req)) |
| 437 | { |
| 438 | prettyPrintJson(res); |
| 439 | } |
| 440 | else |
| 441 | { |
| 442 | res.jsonMode(); |
Jason M. Bills | 193ad2f | 2018-09-26 15:08:52 -0700 | [diff] [blame] | 443 | res.body() = res.jsonValue.dump(2, ' ', true); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 444 | } |
| 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 | if (res.resultInt() >= 400 && res.body().empty()) |
| 448 | { |
| 449 | res.body() = std::string(res.reason()); |
| 450 | } |
Ed Tanous | 6295bec | 2019-09-03 10:11:01 -0700 | [diff] [blame] | 451 | |
| 452 | if (res.result() == boost::beast::http::status::no_content) |
| 453 | { |
| 454 | // Boost beast throws if content is provided on a no-content |
| 455 | // response. Ideally, this would never happen, but in the case that |
| 456 | // it does, we don't want to throw. |
| 457 | BMCWEB_LOG_CRITICAL |
| 458 | << "Response content provided but code was no-content"; |
| 459 | res.body().clear(); |
| 460 | } |
| 461 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 462 | res.addHeader(boost::beast::http::field::server, serverName); |
| 463 | res.addHeader(boost::beast::http::field::date, getCachedDateStr()); |
| 464 | |
| 465 | res.keepAlive(req->keepAlive()); |
| 466 | |
| 467 | doWrite(); |
| 468 | } |
| 469 | |
| 470 | private: |
| 471 | void doReadHeaders() |
| 472 | { |
| 473 | // auto self = this->shared_from_this(); |
| 474 | isReading = true; |
| 475 | BMCWEB_LOG_DEBUG << this << " doReadHeaders"; |
| 476 | |
| 477 | // Clean up any previous Connection. |
| 478 | boost::beast::http::async_read_header( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 479 | adaptor, buffer, *parser, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 480 | [this](const boost::system::error_code& ec, |
| 481 | std::size_t bytes_transferred) { |
| 482 | isReading = false; |
| 483 | BMCWEB_LOG_ERROR << this << " async_read_header " |
| 484 | << bytes_transferred << " Bytes"; |
| 485 | bool errorWhileReading = false; |
| 486 | if (ec) |
| 487 | { |
| 488 | errorWhileReading = true; |
| 489 | BMCWEB_LOG_ERROR |
| 490 | << this << " Error while reading: " << ec.message(); |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | // if the adaptor isn't open anymore, and wasn't handed to a |
| 495 | // websocket, treat as an error |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 496 | if (!isAlive() && !req->isUpgrade()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 497 | { |
| 498 | errorWhileReading = true; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | if (errorWhileReading) |
| 503 | { |
| 504 | cancelDeadlineTimer(); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 505 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 506 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
| 507 | checkDestroy(); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | // Compute the url parameters for the request |
| 512 | req->url = req->target(); |
| 513 | std::size_t index = req->url.find("?"); |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 514 | if (index != std::string_view::npos) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 515 | { |
Jason M. Bills | 43fcbe5 | 2018-10-16 15:19:20 -0700 | [diff] [blame] | 516 | req->url = req->url.substr(0, index); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 517 | } |
| 518 | req->urlParams = QueryString(std::string(req->target())); |
| 519 | doRead(); |
| 520 | }); |
| 521 | } |
| 522 | |
| 523 | void doRead() |
| 524 | { |
| 525 | // auto self = this->shared_from_this(); |
| 526 | isReading = true; |
| 527 | BMCWEB_LOG_DEBUG << this << " doRead"; |
| 528 | |
| 529 | boost::beast::http::async_read( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 530 | adaptor, buffer, *parser, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 531 | [this](const boost::system::error_code& ec, |
| 532 | std::size_t bytes_transferred) { |
| 533 | BMCWEB_LOG_ERROR << this << " async_read " << bytes_transferred |
| 534 | << " Bytes"; |
| 535 | isReading = false; |
| 536 | |
| 537 | bool errorWhileReading = false; |
| 538 | if (ec) |
| 539 | { |
| 540 | BMCWEB_LOG_ERROR << "Error while reading: " << ec.message(); |
| 541 | errorWhileReading = true; |
| 542 | } |
| 543 | else |
| 544 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 545 | if (!isAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 546 | { |
| 547 | errorWhileReading = true; |
| 548 | } |
| 549 | } |
| 550 | if (errorWhileReading) |
| 551 | { |
| 552 | cancelDeadlineTimer(); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 553 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 554 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
| 555 | checkDestroy(); |
| 556 | return; |
| 557 | } |
| 558 | handle(); |
| 559 | }); |
| 560 | } |
| 561 | |
| 562 | void doWrite() |
| 563 | { |
| 564 | // auto self = this->shared_from_this(); |
| 565 | isWriting = true; |
| 566 | BMCWEB_LOG_DEBUG << "Doing Write"; |
| 567 | res.preparePayload(); |
| 568 | serializer.emplace(*res.stringResponse); |
| 569 | boost::beast::http::async_write( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 570 | adaptor, *serializer, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 571 | [&](const boost::system::error_code& ec, |
| 572 | std::size_t bytes_transferred) { |
| 573 | isWriting = false; |
| 574 | BMCWEB_LOG_DEBUG << this << " Wrote " << bytes_transferred |
| 575 | << " bytes"; |
| 576 | |
| 577 | if (ec) |
| 578 | { |
| 579 | BMCWEB_LOG_DEBUG << this << " from write(2)"; |
| 580 | checkDestroy(); |
| 581 | return; |
| 582 | } |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 583 | if (!res.keepAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 584 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 585 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 586 | BMCWEB_LOG_DEBUG << this << " from write(1)"; |
| 587 | checkDestroy(); |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | serializer.reset(); |
| 592 | BMCWEB_LOG_DEBUG << this << " Clearing response"; |
| 593 | res.clear(); |
| 594 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
| 595 | parser->body_limit(httpReqBodyLimit); // reset body limit for |
| 596 | // newly created parser |
| 597 | buffer.consume(buffer.size()); |
| 598 | |
| 599 | req.emplace(parser->get()); |
| 600 | doReadHeaders(); |
| 601 | }); |
| 602 | } |
| 603 | |
| 604 | void checkDestroy() |
| 605 | { |
| 606 | BMCWEB_LOG_DEBUG << this << " isReading " << isReading << " isWriting " |
| 607 | << isWriting; |
| 608 | if (!isReading && !isWriting) |
| 609 | { |
| 610 | BMCWEB_LOG_DEBUG << this << " delete (idle) "; |
| 611 | delete this; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | void cancelDeadlineTimer() |
| 616 | { |
| 617 | BMCWEB_LOG_DEBUG << this << " timer cancelled: " << &timerQueue << ' ' |
| 618 | << timerCancelKey; |
| 619 | timerQueue.cancel(timerCancelKey); |
| 620 | } |
| 621 | |
| 622 | void startDeadline() |
| 623 | { |
| 624 | cancelDeadlineTimer(); |
| 625 | |
| 626 | timerCancelKey = timerQueue.add([this] { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 627 | if (!isAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 628 | { |
| 629 | return; |
| 630 | } |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 631 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 632 | }); |
| 633 | BMCWEB_LOG_DEBUG << this << " timer added: " << &timerQueue << ' ' |
| 634 | << timerCancelKey; |
| 635 | } |
| 636 | |
| 637 | private: |
| 638 | Adaptor adaptor; |
| 639 | Handler* handler; |
| 640 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 641 | // Making this a std::optional allows it to be efficiently destroyed and |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 642 | // re-created on Connection reset |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 643 | std::optional< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 644 | boost::beast::http::request_parser<boost::beast::http::string_body>> |
| 645 | parser; |
| 646 | |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 647 | boost::beast::flat_static_buffer<8192> buffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 648 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 649 | std::optional<boost::beast::http::response_serializer< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 650 | boost::beast::http::string_body>> |
| 651 | serializer; |
| 652 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 653 | std::optional<crow::Request> req; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 654 | crow::Response res; |
| 655 | |
| 656 | const std::string& serverName; |
| 657 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 658 | size_t timerCancelKey = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 659 | |
| 660 | bool isReading{}; |
| 661 | bool isWriting{}; |
| 662 | bool needToCallAfterHandlers{}; |
| 663 | bool needToStartReadAfterComplete{}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 664 | |
| 665 | std::tuple<Middlewares...>* middlewares; |
| 666 | detail::Context<Middlewares...> ctx; |
| 667 | |
| 668 | std::function<std::string()>& getCachedDateStr; |
| 669 | detail::TimerQueue& timerQueue; |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 670 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 671 | } // namespace crow |