From 3423dede5da03833a376e6e389d9698b05a96eaf Mon Sep 17 00:00:00 2001 From: Zhe Ma Date: Thu, 24 Oct 2019 16:07:44 +0800 Subject: [PATCH] Add IP address to Request. --- webcc/connection.cc | 7 +++++++ webcc/request.h | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/webcc/connection.cc b/webcc/connection.cc index f432bde..48cc41e 100644 --- a/webcc/connection.cc +++ b/webcc/connection.cc @@ -19,6 +19,13 @@ Connection::Connection(tcp::socket socket, ConnectionPool* pool, void Connection::Start() { request_.reset(new Request{}); + + boost::system::error_code ec; + auto endpoint = socket_.remote_endpoint(ec); + if (!ec) { + request_->set_ip(endpoint.address().to_string()); + } + request_parser_.Init(request_.get(), view_matcher_); DoRead(); } diff --git a/webcc/request.h b/webcc/request.h index d19e97a..dedaacf 100644 --- a/webcc/request.h +++ b/webcc/request.h @@ -55,6 +55,14 @@ public: args_ = args; } + const std::string& ip() const { + return ip_; + } + + void set_ip(const std::string& ip) { + ip_ = ip; + } + // Check if the body is a multi-part form data. bool IsForm() const; @@ -73,6 +81,9 @@ private: // The URL regex matched arguments (usually resource ID's). // Used by server only. UrlArgs args_; + + // Client IP address. + std::string ip_; }; using RequestPtr = std::shared_ptr;