/** * * @file HttpServer.h * @author An Tao * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license * that can be found in the License file. * * Drogon * */ #pragma once #include #include #include #include #include #include "impl_forwards.h" struct CallbackParamPack; namespace drogon { struct ControllerBinderBase; class HttpServer : trantor::NonCopyable { public: HttpServer(trantor::EventLoop *loop, const trantor::InetAddress &listenAddr, std::string name); ~HttpServer(); void setIoLoops(const std::vector &ioLoops) { server_.setIoLoops(ioLoops); } void start(); void stop(); void enableSSL(trantor::TLSPolicyPtr policy) { server_.enableSSL(std::move(policy)); } const trantor::InetAddress &address() const { return server_.address(); } void setBeforeListenSockOptCallback(std::function cb) { beforeListenSetSockOptCallback_ = std::move(cb); } void setAfterAcceptSockOptCallback(std::function cb) { afterAcceptSetSockOptCallback_ = std::move(cb); } private: friend class HttpInternalForwardHelper; static void onConnection(const trantor::TcpConnectionPtr &conn); static void onMessage(const trantor::TcpConnectionPtr &, trantor::MsgBuffer *); static void onRequests(const trantor::TcpConnectionPtr &, const std::vector &, const std::shared_ptr &); struct HttpRequestParamPack { std::shared_ptr binderPtr; std::function callback; }; struct WsRequestParamPack { std::shared_ptr binderPtr; std::function callback; WebSocketConnectionImplPtr wsConnPtr; }; // Http request handling steps static void onHttpRequest(const HttpRequestImplPtr &, std::function &&); static void httpRequestRouting( const HttpRequestImplPtr &req, std::function &&callback); static void httpRequestHandling( const HttpRequestImplPtr &req, std::shared_ptr &&binderPtr, std::function &&callback); // Websocket request handling steps static void onWebsocketRequest( const HttpRequestImplPtr &, std::function &&, WebSocketConnectionImplPtr &&); static void websocketRequestRouting( const HttpRequestImplPtr &req, std::function &&callback, WebSocketConnectionImplPtr &&wsConnPtr); static void websocketRequestHandling( const HttpRequestImplPtr &req, std::shared_ptr &&binderPtr, std::function &&callback, WebSocketConnectionImplPtr &&wsConnPtr); // Http/Websocket shared handling steps template static void requestPostRouting(const HttpRequestImplPtr &req, Pack &&pack); template static void requestPassMiddlewares(const HttpRequestImplPtr &req, Pack &&pack); template static void requestPreHandling(const HttpRequestImplPtr &req, Pack &&pack); // Response buffering and sending static void handleResponse( const HttpResponsePtr &response, const std::shared_ptr ¶mPack, bool *respReadyPtr); static void sendResponse(const trantor::TcpConnectionPtr &, const HttpResponsePtr &, bool isHeadMethod); static void sendResponses( const trantor::TcpConnectionPtr &conn, const std::vector> &responses, trantor::MsgBuffer &buffer); trantor::TcpServer server_; std::function beforeListenSetSockOptCallback_; std::function afterAcceptSetSockOptCallback_; }; class HttpInternalForwardHelper { public: static void forward(const HttpRequestImplPtr &req, std::function &&callback) { return HttpServer::onHttpRequest(req, std::move(callback)); } }; } // namespace drogon