diff --git a/examples/helloworld/main.cc b/examples/helloworld/main.cc index e92d27ca..fd25d524 100644 --- a/examples/helloworld/main.cc +++ b/examples/helloworld/main.cc @@ -1,3 +1,4 @@ +#include #ifdef _WIN32 #include #else @@ -15,8 +16,10 @@ int main() // sent to Drogon app().registerHandler( "/", - [](const HttpRequestPtr &, + [](const HttpRequestPtr &request, std::function &&callback) { + LOG_INFO << "connected:" + << (request->connected() ? "true" : "false"); auto resp = HttpResponse::newHttpResponse(); resp->setBody("Hello, World!"); callback(resp); diff --git a/lib/inc/drogon/HttpRequest.h b/lib/inc/drogon/HttpRequest.h index 68c4271b..1bc9eabb 100644 --- a/lib/inc/drogon/HttpRequest.h +++ b/lib/inc/drogon/HttpRequest.h @@ -504,6 +504,8 @@ class DROGON_EXPORT HttpRequest virtual void setContentTypeString(const char *typeString, size_t typeStringLength) = 0; + virtual bool connected() const noexcept = 0; + virtual ~HttpRequest() { } diff --git a/lib/src/HttpRequestImpl.cc b/lib/src/HttpRequestImpl.cc index 83fc771c..dc6f68f7 100644 --- a/lib/src/HttpRequestImpl.cc +++ b/lib/src/HttpRequestImpl.cc @@ -600,6 +600,7 @@ void HttpRequestImpl::swap(HttpRequestImpl &that) noexcept swap(streamFinishCb_, that.streamFinishCb_); swap(streamExceptionPtr_, that.streamExceptionPtr_); swap(startProcessing_, that.startProcessing_); + swap(connPtr_, that.connPtr_); } const char *HttpRequestImpl::versionString() const diff --git a/lib/src/HttpRequestImpl.h b/lib/src/HttpRequestImpl.h index 412aecf9..bd0a4689 100644 --- a/lib/src/HttpRequestImpl.h +++ b/lib/src/HttpRequestImpl.h @@ -26,9 +26,12 @@ #include #include #include +#include #include +#include +#include #include -#include +#include #include #include #include @@ -97,6 +100,7 @@ class HttpRequestImpl : public HttpRequest streamFinishCb_ = nullptr; streamExceptionPtr_ = nullptr; startProcessing_ = false; + connPtr_.reset(); } trantor::EventLoop *getLoop() @@ -326,6 +330,11 @@ class HttpRequestImpl : public HttpRequest peerCertificate_ = cert; } + void setConnectionPtr(const std::shared_ptr &ptr) + { + connPtr_ = ptr; + } + void addHeader(const char *start, const char *colon, const char *end); void removeHeader(std::string key) override @@ -554,6 +563,15 @@ class HttpRequestImpl : public HttpRequest return keepAlive_; } + bool connected() const noexcept override + { + if (auto conn = connPtr_.lock()) + { + return conn->connected(); + } + return false; + } + bool isOnSecureConnection() const noexcept override { return isOnSecureConnection_; @@ -705,6 +723,7 @@ class HttpRequestImpl : public HttpRequest RequestStreamReaderPtr streamReaderPtr_; std::exception_ptr streamExceptionPtr_; bool startProcessing_{false}; + std::weak_ptr connPtr_; protected: std::string content_; diff --git a/lib/src/HttpServer.cc b/lib/src/HttpServer.cc index 1cf60ad3..257976b1 100644 --- a/lib/src/HttpServer.cc +++ b/lib/src/HttpServer.cc @@ -220,6 +220,7 @@ void HttpServer::onMessage(const TcpConnectionPtr &conn, MsgBuffer *buf) req->setCreationDate(trantor::Date::date()); req->setSecure(conn->isSSLConnection()); req->setPeerCertificate(conn->peerCertificate()); + req->setConnectionPtr(conn); // TODO: maybe call onRequests() directly in stream mode requests.push_back(req); }