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> |
| 9 | #include <boost/beast/core/flat_static_buffer.hpp> |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 10 | #include <boost/beast/http.hpp> |
| 11 | #include <boost/beast/websocket.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 12 | #include <chrono> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
| 15 | #include "crow/http_response.h" |
| 16 | #include "crow/logging.h" |
| 17 | #include "crow/middleware_context.h" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 18 | #include "crow/timer_queue.h" |
Ed Tanous | a29c997 | 2018-11-29 15:54:32 -0800 | [diff] [blame] | 19 | #include "crow/utility.h" |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 20 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 21 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 22 | #include <boost/asio/ssl.hpp> |
| 23 | #include <boost/beast/experimental/core/ssl_stream.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 24 | #endif |
| 25 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 26 | namespace crow |
| 27 | { |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 28 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 29 | inline void prettyPrintJson(crow::Response& res) |
| 30 | { |
Jason M. Bills | 193ad2f | 2018-09-26 15:08:52 -0700 | [diff] [blame] | 31 | std::string value = res.jsonValue.dump(4, ' ', true); |
Ed Tanous | a29c997 | 2018-11-29 15:54:32 -0800 | [diff] [blame] | 32 | utility::escapeHtml(value); |
| 33 | utility::convertToLinks(value); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | res.body() = "<html>\n" |
| 35 | "<head>\n" |
| 36 | "<title>Redfish API</title>\n" |
| 37 | "<link rel=\"stylesheet\" type=\"text/css\" " |
| 38 | "href=\"/styles/default.css\">\n" |
| 39 | "<script src=\"/highlight.pack.js\"></script>" |
| 40 | "<script>hljs.initHighlightingOnLoad();</script>" |
| 41 | "</head>\n" |
| 42 | "<body>\n" |
| 43 | "<div style=\"max-width: 576px;margin:0 auto;\">\n" |
| 44 | "<img src=\"/DMTF_Redfish_logo_2017.svg\" alt=\"redfish\" " |
| 45 | "height=\"406px\" " |
| 46 | "width=\"576px\">\n" |
| 47 | "<br>\n" |
| 48 | "<pre>\n" |
| 49 | "<code class=\"json\">" + |
| 50 | value + |
| 51 | "</code>\n" |
| 52 | "</pre>\n" |
| 53 | "</div>\n" |
| 54 | "</body>\n" |
| 55 | "</html>\n"; |
Ed Tanous | 93ef580 | 2019-01-03 10:15:41 -0800 | [diff] [blame] | 56 | res.addHeader("Content-Type", "text/html;charset=UTF-8"); |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 59 | using namespace boost; |
| 60 | using tcp = asio::ip::tcp; |
| 61 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 62 | namespace detail |
| 63 | { |
| 64 | template <typename MW> struct CheckBeforeHandleArity3Const |
| 65 | { |
| 66 | template <typename T, |
| 67 | void (T::*)(Request&, Response&, typename MW::Context&) const = |
| 68 | &T::beforeHandle> |
| 69 | struct Get |
| 70 | { |
| 71 | }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 74 | template <typename MW> struct CheckBeforeHandleArity3 |
| 75 | { |
| 76 | template <typename T, void (T::*)(Request&, Response&, |
| 77 | typename MW::Context&) = &T::beforeHandle> |
| 78 | struct Get |
| 79 | { |
| 80 | }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 83 | template <typename MW> struct CheckAfterHandleArity3Const |
| 84 | { |
| 85 | template <typename T, |
| 86 | void (T::*)(Request&, Response&, typename MW::Context&) const = |
| 87 | &T::afterHandle> |
| 88 | struct Get |
| 89 | { |
| 90 | }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 93 | template <typename MW> struct CheckAfterHandleArity3 |
| 94 | { |
| 95 | template <typename T, void (T::*)(Request&, Response&, |
| 96 | typename MW::Context&) = &T::afterHandle> |
| 97 | struct Get |
| 98 | { |
| 99 | }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 102 | template <typename T> struct IsBeforeHandleArity3Impl |
| 103 | { |
| 104 | template <typename C> |
| 105 | static std::true_type |
| 106 | f(typename CheckBeforeHandleArity3Const<T>::template Get<C>*); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 107 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 108 | template <typename C> |
| 109 | static std::true_type |
| 110 | f(typename CheckBeforeHandleArity3<T>::template Get<C>*); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 111 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 112 | template <typename C> static std::false_type f(...); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 113 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 114 | public: |
| 115 | static const bool value = decltype(f<T>(nullptr))::value; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 118 | template <typename T> struct IsAfterHandleArity3Impl |
| 119 | { |
| 120 | template <typename C> |
| 121 | static std::true_type |
| 122 | f(typename CheckAfterHandleArity3Const<T>::template Get<C>*); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 123 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 124 | template <typename C> |
| 125 | static std::true_type |
| 126 | f(typename CheckAfterHandleArity3<T>::template Get<C>*); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 127 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 128 | template <typename C> static std::false_type f(...); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 129 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 130 | public: |
| 131 | static const bool value = decltype(f<T>(nullptr))::value; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | template <typename MW, typename Context, typename ParentContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 135 | typename std::enable_if<!IsBeforeHandleArity3Impl<MW>::value>::type |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 136 | beforeHandlerCall(MW& mw, Request& req, Response& res, Context& ctx, |
| 137 | ParentContext& /*parent_ctx*/) |
| 138 | { |
| 139 | mw.beforeHandle(req, res, ctx.template get<MW>(), ctx); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | template <typename MW, typename Context, typename ParentContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 143 | typename std::enable_if<IsBeforeHandleArity3Impl<MW>::value>::type |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 144 | beforeHandlerCall(MW& mw, Request& req, Response& res, Context& ctx, |
| 145 | ParentContext& /*parent_ctx*/) |
| 146 | { |
| 147 | mw.beforeHandle(req, res, ctx.template get<MW>()); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | template <typename MW, typename Context, typename ParentContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 151 | typename std::enable_if<!IsAfterHandleArity3Impl<MW>::value>::type |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 152 | afterHandlerCall(MW& mw, Request& req, Response& res, Context& ctx, |
| 153 | ParentContext& /*parent_ctx*/) |
| 154 | { |
| 155 | mw.afterHandle(req, res, ctx.template get<MW>(), ctx); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | template <typename MW, typename Context, typename ParentContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 159 | typename std::enable_if<IsAfterHandleArity3Impl<MW>::value>::type |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 160 | afterHandlerCall(MW& mw, Request& req, Response& res, Context& ctx, |
| 161 | ParentContext& /*parent_ctx*/) |
| 162 | { |
| 163 | mw.afterHandle(req, res, ctx.template get<MW>()); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | template <int N, typename Context, typename Container, typename CurrentMW, |
| 167 | typename... Middlewares> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 168 | bool middlewareCallHelper(Container& middlewares, Request& req, Response& res, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 169 | Context& ctx) |
| 170 | { |
| 171 | using parent_context_t = typename Context::template partial<N - 1>; |
| 172 | beforeHandlerCall<CurrentMW, Context, parent_context_t>( |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 173 | std::get<N>(middlewares), req, res, ctx, |
| 174 | static_cast<parent_context_t&>(ctx)); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 175 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 176 | if (res.isCompleted()) |
| 177 | { |
| 178 | afterHandlerCall<CurrentMW, Context, parent_context_t>( |
| 179 | std::get<N>(middlewares), req, res, ctx, |
| 180 | static_cast<parent_context_t&>(ctx)); |
| 181 | return true; |
| 182 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 183 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 184 | if (middlewareCallHelper<N + 1, Context, Container, Middlewares...>( |
| 185 | middlewares, req, res, ctx)) |
| 186 | { |
| 187 | afterHandlerCall<CurrentMW, Context, parent_context_t>( |
| 188 | std::get<N>(middlewares), req, res, ctx, |
| 189 | static_cast<parent_context_t&>(ctx)); |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | return false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | template <int N, typename Context, typename Container> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 197 | bool middlewareCallHelper(Container& /*middlewares*/, Request& /*req*/, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 198 | Response& /*res*/, Context& /*ctx*/) |
| 199 | { |
| 200 | return false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | template <int N, typename Context, typename Container> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 204 | typename std::enable_if<(N < 0)>::type |
| 205 | afterHandlersCallHelper(Container& /*middlewares*/, Context& /*Context*/, |
| 206 | Request& /*req*/, Response& /*res*/) |
| 207 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | template <int N, typename Context, typename Container> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 211 | typename std::enable_if<(N == 0)>::type |
| 212 | afterHandlersCallHelper(Container& middlewares, Context& ctx, Request& req, |
| 213 | Response& res) |
| 214 | { |
| 215 | using parent_context_t = typename Context::template partial<N - 1>; |
| 216 | using CurrentMW = typename std::tuple_element< |
| 217 | N, typename std::remove_reference<Container>::type>::type; |
| 218 | afterHandlerCall<CurrentMW, Context, parent_context_t>( |
| 219 | std::get<N>(middlewares), req, res, ctx, |
| 220 | static_cast<parent_context_t&>(ctx)); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 221 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 222 | |
| 223 | template <int N, typename Context, typename Container> |
| 224 | typename std::enable_if<(N > 0)>::type |
| 225 | afterHandlersCallHelper(Container& middlewares, Context& ctx, Request& req, |
| 226 | Response& res) |
| 227 | { |
| 228 | using parent_context_t = typename Context::template partial<N - 1>; |
| 229 | using CurrentMW = typename std::tuple_element< |
| 230 | N, typename std::remove_reference<Container>::type>::type; |
| 231 | afterHandlerCall<CurrentMW, Context, parent_context_t>( |
| 232 | std::get<N>(middlewares), req, res, ctx, |
| 233 | static_cast<parent_context_t&>(ctx)); |
| 234 | afterHandlersCallHelper<N - 1, Context, Container>(middlewares, ctx, req, |
| 235 | res); |
| 236 | } |
| 237 | } // namespace detail |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 238 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 239 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 240 | static std::atomic<int> connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 241 | #endif |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 242 | |
| 243 | // request body limit size: 30M |
| 244 | constexpr unsigned int httpReqBodyLimit = 1024 * 1024 * 30; |
| 245 | |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 246 | template <typename Adaptor, typename Handler, typename... Middlewares> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 247 | class Connection |
| 248 | { |
| 249 | public: |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 250 | Connection(boost::asio::io_context& ioService, Handler* handler, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 251 | const std::string& server_name, |
| 252 | std::tuple<Middlewares...>* middlewares, |
| 253 | std::function<std::string()>& get_cached_date_str_f, |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 254 | detail::TimerQueue& timerQueue, Adaptor adaptorIn) : |
| 255 | adaptor(std::move(adaptorIn)), |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 256 | handler(handler), serverName(server_name), middlewares(middlewares), |
| 257 | getCachedDateStr(get_cached_date_str_f), timerQueue(timerQueue) |
| 258 | { |
| 259 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
| 260 | // Temporarily changed to 30MB; Need to modify uploading/authentication |
| 261 | // mechanism |
| 262 | parser->body_limit(httpReqBodyLimit); |
| 263 | req.emplace(parser->get()); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 264 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 265 | connectionCount++; |
| 266 | BMCWEB_LOG_DEBUG << this << " Connection open, total " |
| 267 | << connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 268 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 269 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 270 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 271 | ~Connection() |
| 272 | { |
| 273 | res.completeRequestHandler = nullptr; |
| 274 | cancelDeadlineTimer(); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 275 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 276 | connectionCount--; |
| 277 | BMCWEB_LOG_DEBUG << this << " Connection closed, total " |
| 278 | << connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 279 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 282 | Adaptor& socket() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 283 | { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 284 | return adaptor; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 287 | void start() |
| 288 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 289 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 290 | startDeadline(); |
| 291 | // TODO(ed) Abstract this to a more clever class with the idea of an |
| 292 | // asynchronous "start" |
| 293 | if constexpr (std::is_same_v<Adaptor, |
| 294 | boost::beast::ssl_stream< |
| 295 | boost::asio::ip::tcp::socket>>) |
| 296 | { |
| 297 | adaptor.async_handshake( |
| 298 | boost::asio::ssl::stream_base::server, |
| 299 | [this](const boost::system::error_code& ec) { |
| 300 | if (ec) |
| 301 | { |
| 302 | checkDestroy(); |
| 303 | return; |
| 304 | } |
| 305 | doReadHeaders(); |
| 306 | }); |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | doReadHeaders(); |
| 311 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 312 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 313 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 314 | void handle() |
| 315 | { |
| 316 | cancelDeadlineTimer(); |
| 317 | bool isInvalidRequest = false; |
| 318 | const boost::string_view connection = |
| 319 | req->getHeaderValue(boost::beast::http::field::connection); |
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; |
| 327 | res = Response(boost::beast::http::status::bad_request); |
| 328 | } |
| 329 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 330 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 331 | std::string epName; |
| 332 | boost::system::error_code ec; |
| 333 | tcp::endpoint ep = adaptor.lowest_layer().remote_endpoint(ec); |
| 334 | if (!ec) |
| 335 | { |
| 336 | epName = boost::lexical_cast<std::string>(ep); |
| 337 | } |
| 338 | |
| 339 | BMCWEB_LOG_INFO << "Request: " << epName << " " << this << " HTTP/" |
| 340 | << req->version() / 10 << "." << req->version() % 10 |
| 341 | << ' ' << req->methodString() << " " << req->target(); |
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 | needToCallAfterHandlers = false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 344 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 345 | if (!isInvalidRequest) |
| 346 | { |
| 347 | res.completeRequestHandler = [] {}; |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 348 | res.isAliveHelper = [this]() -> bool { |
| 349 | return adaptor.lowest_layer().is_open(); |
| 350 | }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 351 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 352 | ctx = detail::Context<Middlewares...>(); |
| 353 | req->middlewareContext = (void*)&ctx; |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 354 | req->ioService = &adaptor.get_executor().context(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 355 | detail::middlewareCallHelper< |
| 356 | 0, decltype(ctx), decltype(*middlewares), Middlewares...>( |
| 357 | *middlewares, *req, res, ctx); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 358 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 359 | if (!res.completed) |
| 360 | { |
| 361 | if (req->isUpgrade() && |
| 362 | boost::iequals( |
| 363 | req->getHeaderValue(boost::beast::http::field::upgrade), |
| 364 | "websocket")) |
| 365 | { |
| 366 | handler->handleUpgrade(*req, res, std::move(adaptor)); |
| 367 | return; |
| 368 | } |
| 369 | res.completeRequestHandler = [this] { |
| 370 | this->completeRequest(); |
| 371 | }; |
| 372 | needToCallAfterHandlers = true; |
| 373 | handler->handle(*req, res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 374 | } |
| 375 | else |
| 376 | { |
| 377 | completeRequest(); |
| 378 | } |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | completeRequest(); |
| 383 | } |
| 384 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 385 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 386 | void completeRequest() |
| 387 | { |
| 388 | BMCWEB_LOG_INFO << "Response: " << this << ' ' << req->url << ' ' |
| 389 | << res.resultInt() << " keepalive=" << req->keepAlive(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 390 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 391 | if (needToCallAfterHandlers) |
| 392 | { |
| 393 | needToCallAfterHandlers = false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 394 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 395 | // call all afterHandler of middlewares |
| 396 | detail::afterHandlersCallHelper<((int)sizeof...(Middlewares) - 1), |
| 397 | decltype(ctx), |
| 398 | decltype(*middlewares)>( |
| 399 | *middlewares, ctx, *req, res); |
| 400 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 401 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 402 | // auto self = this->shared_from_this(); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 403 | res.completeRequestHandler = res.completeRequestHandler = [] {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 404 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 405 | if (!adaptor.lowest_layer().is_open()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 406 | { |
| 407 | // BMCWEB_LOG_DEBUG << this << " delete (socket is closed) " << |
| 408 | // isReading |
| 409 | // << ' ' << isWriting; |
| 410 | // delete this; |
| 411 | return; |
| 412 | } |
| 413 | if (res.body().empty() && !res.jsonValue.empty()) |
| 414 | { |
| 415 | if (http_helpers::requestPrefersHtml(*req)) |
| 416 | { |
| 417 | prettyPrintJson(res); |
| 418 | } |
| 419 | else |
| 420 | { |
| 421 | res.jsonMode(); |
Jason M. Bills | 193ad2f | 2018-09-26 15:08:52 -0700 | [diff] [blame] | 422 | res.body() = res.jsonValue.dump(2, ' ', true); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 425 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 426 | if (res.resultInt() >= 400 && res.body().empty()) |
| 427 | { |
| 428 | res.body() = std::string(res.reason()); |
| 429 | } |
| 430 | res.addHeader(boost::beast::http::field::server, serverName); |
| 431 | res.addHeader(boost::beast::http::field::date, getCachedDateStr()); |
| 432 | |
| 433 | res.keepAlive(req->keepAlive()); |
| 434 | |
| 435 | doWrite(); |
| 436 | } |
| 437 | |
| 438 | private: |
| 439 | void doReadHeaders() |
| 440 | { |
| 441 | // auto self = this->shared_from_this(); |
| 442 | isReading = true; |
| 443 | BMCWEB_LOG_DEBUG << this << " doReadHeaders"; |
| 444 | |
| 445 | // Clean up any previous Connection. |
| 446 | boost::beast::http::async_read_header( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 447 | adaptor, buffer, *parser, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 448 | [this](const boost::system::error_code& ec, |
| 449 | std::size_t bytes_transferred) { |
| 450 | isReading = false; |
| 451 | BMCWEB_LOG_ERROR << this << " async_read_header " |
| 452 | << bytes_transferred << " Bytes"; |
| 453 | bool errorWhileReading = false; |
| 454 | if (ec) |
| 455 | { |
| 456 | errorWhileReading = true; |
| 457 | BMCWEB_LOG_ERROR |
| 458 | << this << " Error while reading: " << ec.message(); |
| 459 | } |
| 460 | else |
| 461 | { |
| 462 | // if the adaptor isn't open anymore, and wasn't handed to a |
| 463 | // websocket, treat as an error |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 464 | if (!adaptor.lowest_layer().is_open() && !req->isUpgrade()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 465 | { |
| 466 | errorWhileReading = true; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | if (errorWhileReading) |
| 471 | { |
| 472 | cancelDeadlineTimer(); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 473 | adaptor.lowest_layer().close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 474 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
| 475 | checkDestroy(); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | // Compute the url parameters for the request |
| 480 | req->url = req->target(); |
| 481 | std::size_t index = req->url.find("?"); |
| 482 | if (index != boost::string_view::npos) |
| 483 | { |
Jason M. Bills | 43fcbe5 | 2018-10-16 15:19:20 -0700 | [diff] [blame] | 484 | req->url = req->url.substr(0, index); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 485 | } |
| 486 | req->urlParams = QueryString(std::string(req->target())); |
| 487 | doRead(); |
| 488 | }); |
| 489 | } |
| 490 | |
| 491 | void doRead() |
| 492 | { |
| 493 | // auto self = this->shared_from_this(); |
| 494 | isReading = true; |
| 495 | BMCWEB_LOG_DEBUG << this << " doRead"; |
| 496 | |
| 497 | boost::beast::http::async_read( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 498 | adaptor, buffer, *parser, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 499 | [this](const boost::system::error_code& ec, |
| 500 | std::size_t bytes_transferred) { |
| 501 | BMCWEB_LOG_ERROR << this << " async_read " << bytes_transferred |
| 502 | << " Bytes"; |
| 503 | isReading = false; |
| 504 | |
| 505 | bool errorWhileReading = false; |
| 506 | if (ec) |
| 507 | { |
| 508 | BMCWEB_LOG_ERROR << "Error while reading: " << ec.message(); |
| 509 | errorWhileReading = true; |
| 510 | } |
| 511 | else |
| 512 | { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 513 | if (!adaptor.lowest_layer().is_open()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 514 | { |
| 515 | errorWhileReading = true; |
| 516 | } |
| 517 | } |
| 518 | if (errorWhileReading) |
| 519 | { |
| 520 | cancelDeadlineTimer(); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 521 | adaptor.lowest_layer().close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 522 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
| 523 | checkDestroy(); |
| 524 | return; |
| 525 | } |
| 526 | handle(); |
| 527 | }); |
| 528 | } |
| 529 | |
| 530 | void doWrite() |
| 531 | { |
| 532 | // auto self = this->shared_from_this(); |
| 533 | isWriting = true; |
| 534 | BMCWEB_LOG_DEBUG << "Doing Write"; |
| 535 | res.preparePayload(); |
| 536 | serializer.emplace(*res.stringResponse); |
| 537 | boost::beast::http::async_write( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 538 | adaptor, *serializer, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 539 | [&](const boost::system::error_code& ec, |
| 540 | std::size_t bytes_transferred) { |
| 541 | isWriting = false; |
| 542 | BMCWEB_LOG_DEBUG << this << " Wrote " << bytes_transferred |
| 543 | << " bytes"; |
| 544 | |
| 545 | if (ec) |
| 546 | { |
| 547 | BMCWEB_LOG_DEBUG << this << " from write(2)"; |
| 548 | checkDestroy(); |
| 549 | return; |
| 550 | } |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 551 | if (!res.keepAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 552 | { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 553 | adaptor.lowest_layer().close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 554 | BMCWEB_LOG_DEBUG << this << " from write(1)"; |
| 555 | checkDestroy(); |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | serializer.reset(); |
| 560 | BMCWEB_LOG_DEBUG << this << " Clearing response"; |
| 561 | res.clear(); |
| 562 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
| 563 | parser->body_limit(httpReqBodyLimit); // reset body limit for |
| 564 | // newly created parser |
| 565 | buffer.consume(buffer.size()); |
| 566 | |
| 567 | req.emplace(parser->get()); |
| 568 | doReadHeaders(); |
| 569 | }); |
| 570 | } |
| 571 | |
| 572 | void checkDestroy() |
| 573 | { |
| 574 | BMCWEB_LOG_DEBUG << this << " isReading " << isReading << " isWriting " |
| 575 | << isWriting; |
| 576 | if (!isReading && !isWriting) |
| 577 | { |
| 578 | BMCWEB_LOG_DEBUG << this << " delete (idle) "; |
| 579 | delete this; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | void cancelDeadlineTimer() |
| 584 | { |
| 585 | BMCWEB_LOG_DEBUG << this << " timer cancelled: " << &timerQueue << ' ' |
| 586 | << timerCancelKey; |
| 587 | timerQueue.cancel(timerCancelKey); |
| 588 | } |
| 589 | |
| 590 | void startDeadline() |
| 591 | { |
| 592 | cancelDeadlineTimer(); |
| 593 | |
| 594 | timerCancelKey = timerQueue.add([this] { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 595 | if (!adaptor.lowest_layer().is_open()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 596 | { |
| 597 | return; |
| 598 | } |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 599 | adaptor.lowest_layer().close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 600 | }); |
| 601 | BMCWEB_LOG_DEBUG << this << " timer added: " << &timerQueue << ' ' |
| 602 | << timerCancelKey; |
| 603 | } |
| 604 | |
| 605 | private: |
| 606 | Adaptor adaptor; |
| 607 | Handler* handler; |
| 608 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 609 | // Making this a std::optional allows it to be efficiently destroyed and |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 610 | // re-created on Connection reset |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 611 | std::optional< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 612 | boost::beast::http::request_parser<boost::beast::http::string_body>> |
| 613 | parser; |
| 614 | |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 615 | boost::beast::flat_static_buffer<8192> buffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 616 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 617 | std::optional<boost::beast::http::response_serializer< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 618 | boost::beast::http::string_body>> |
| 619 | serializer; |
| 620 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 621 | std::optional<crow::Request> req; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 622 | crow::Response res; |
| 623 | |
| 624 | const std::string& serverName; |
| 625 | |
| 626 | int timerCancelKey{-1}; |
| 627 | |
| 628 | bool isReading{}; |
| 629 | bool isWriting{}; |
| 630 | bool needToCallAfterHandlers{}; |
| 631 | bool needToStartReadAfterComplete{}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 632 | |
| 633 | std::tuple<Middlewares...>* middlewares; |
| 634 | detail::Context<Middlewares...> ctx; |
| 635 | |
| 636 | std::function<std::string()>& getCachedDateStr; |
| 637 | detail::TimerQueue& timerQueue; |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 638 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 639 | } // namespace crow |