Repository: thrift Updated Branches: refs/heads/master 9cc7e8696 -> 9be413fca
Add getOrigin() function to TTransport getOrigin returns the origin of a request, the value depends on the transport used Project: http://git-wip-us.apache.org/repos/asf/thrift/repo Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/9be413fc Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/9be413fc Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/9be413fc Branch: refs/heads/master Commit: 9be413fca40d75559c2776618c904a5e140d3418 Parents: 9cc7e86 Author: Pascal Bach <[email protected]> Authored: Thu Aug 21 13:37:11 2014 +0200 Committer: Pascal Bach <[email protected]> Committed: Thu Aug 21 14:36:25 2014 +0200 ---------------------------------------------------------------------- lib/cpp/src/thrift/transport/TBufferTransports.h | 14 +++++++++++++- lib/cpp/src/thrift/transport/THttpServer.cpp | 2 ++ lib/cpp/src/thrift/transport/THttpTransport.cpp | 12 ++++++++++++ lib/cpp/src/thrift/transport/THttpTransport.h | 3 +++ lib/cpp/src/thrift/transport/TSocket.cpp | 6 ++++++ lib/cpp/src/thrift/transport/TSocket.h | 7 +++++++ lib/cpp/src/thrift/transport/TTransport.h | 12 ++++++++++++ 7 files changed, 55 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/thrift/blob/9be413fc/lib/cpp/src/thrift/transport/TBufferTransports.h ---------------------------------------------------------------------- diff --git a/lib/cpp/src/thrift/transport/TBufferTransports.h b/lib/cpp/src/thrift/transport/TBufferTransports.h index b669a59..af9412c 100644 --- a/lib/cpp/src/thrift/transport/TBufferTransports.h +++ b/lib/cpp/src/thrift/transport/TBufferTransports.h @@ -126,7 +126,6 @@ class TBufferBase : public TVirtualTransport<TBufferBase> { } } - protected: /// Slow path read. @@ -253,6 +252,12 @@ class TBufferedTransport void flush(); + /** + * Returns the origin of the underlying transport + */ + virtual const std::string getOrigin() { + return transport_->getOrigin(); + } /** * The following behavior is currently implemented by TBufferedTransport, @@ -393,6 +398,13 @@ class TFramedTransport return TBufferBase::readAll(buf,len); } + /** + * Returns the origin of the underlying transport + */ + virtual const std::string getOrigin() { + return transport_->getOrigin(); + } + protected: /** * Reads a frame of input from the underlying stream. http://git-wip-us.apache.org/repos/asf/thrift/blob/9be413fc/lib/cpp/src/thrift/transport/THttpServer.cpp ---------------------------------------------------------------------- diff --git a/lib/cpp/src/thrift/transport/THttpServer.cpp b/lib/cpp/src/thrift/transport/THttpServer.cpp index e5b3327..620bbd2 100644 --- a/lib/cpp/src/thrift/transport/THttpServer.cpp +++ b/lib/cpp/src/thrift/transport/THttpServer.cpp @@ -49,6 +49,8 @@ void THttpServer::parseHeader(char* header) { } else if (strncmp(header, "Content-Length", sz) == 0) { chunked_ = false; contentLength_ = atoi(value); + } else if (strncmp(header, "X-Forwarded-For", sz) == 0) { + origin_ = value; } } http://git-wip-us.apache.org/repos/asf/thrift/blob/9be413fc/lib/cpp/src/thrift/transport/THttpTransport.cpp ---------------------------------------------------------------------- diff --git a/lib/cpp/src/thrift/transport/THttpTransport.cpp b/lib/cpp/src/thrift/transport/THttpTransport.cpp index c415ddb..79ee7d5 100644 --- a/lib/cpp/src/thrift/transport/THttpTransport.cpp +++ b/lib/cpp/src/thrift/transport/THttpTransport.cpp @@ -17,6 +17,8 @@ * under the License. */ +#include <sstream> + #include <thrift/transport/THttpTransport.h> namespace apache { namespace thrift { namespace transport { @@ -29,6 +31,7 @@ const int THttpTransport::CRLF_LEN = 2; THttpTransport::THttpTransport(boost::shared_ptr<TTransport> transport) : transport_(transport), + origin_(""), readHeaders_(true), chunked_(false), chunkedDone_(false), @@ -249,4 +252,13 @@ void THttpTransport::write(const uint8_t* buf, uint32_t len) { writeBuffer_.write(buf, len); } +const std::string THttpTransport::getOrigin() { + std::ostringstream oss; + if ( !origin_.empty()) { + oss << origin_ << ", "; + } + oss << transport_->getOrigin(); + return oss.str(); +} + }}} http://git-wip-us.apache.org/repos/asf/thrift/blob/9be413fc/lib/cpp/src/thrift/transport/THttpTransport.h ---------------------------------------------------------------------- diff --git a/lib/cpp/src/thrift/transport/THttpTransport.h b/lib/cpp/src/thrift/transport/THttpTransport.h index a2e8834..8967c74 100644 --- a/lib/cpp/src/thrift/transport/THttpTransport.h +++ b/lib/cpp/src/thrift/transport/THttpTransport.h @@ -62,9 +62,12 @@ class THttpTransport : public TVirtualTransport<THttpTransport> { virtual void flush() = 0; + virtual const std::string getOrigin(); + protected: boost::shared_ptr<TTransport> transport_; + std::string origin_; TMemoryBuffer writeBuffer_; TMemoryBuffer readBuffer_; http://git-wip-us.apache.org/repos/asf/thrift/blob/9be413fc/lib/cpp/src/thrift/transport/TSocket.cpp ---------------------------------------------------------------------- diff --git a/lib/cpp/src/thrift/transport/TSocket.cpp b/lib/cpp/src/thrift/transport/TSocket.cpp old mode 100755 new mode 100644 index e80f712..82678ba --- a/lib/cpp/src/thrift/transport/TSocket.cpp +++ b/lib/cpp/src/thrift/transport/TSocket.cpp @@ -841,4 +841,10 @@ bool TSocket::getUseLowMinRto() { return useLowMinRto_; } +const std::string TSocket::getOrigin() { + std::ostringstream oss; + oss << getPeerHost() << ":" << getPeerPort(); + return oss.str(); +} + }}} // apache::thrift::transport http://git-wip-us.apache.org/repos/asf/thrift/blob/9be413fc/lib/cpp/src/thrift/transport/TSocket.h ---------------------------------------------------------------------- diff --git a/lib/cpp/src/thrift/transport/TSocket.h b/lib/cpp/src/thrift/transport/TSocket.h index 062a5b9..c873218 100644 --- a/lib/cpp/src/thrift/transport/TSocket.h +++ b/lib/cpp/src/thrift/transport/TSocket.h @@ -235,6 +235,13 @@ class TSocket : public TVirtualTransport<TSocket> { static bool getUseLowMinRto(); /** + * Get the origin the socket is connected to + * + * @return string peer host identifier and port + */ + virtual const std::string getOrigin(); + + /** * Constructor to create socket from raw UNIX handle. */ TSocket(THRIFT_SOCKET socket); http://git-wip-us.apache.org/repos/asf/thrift/blob/9be413fc/lib/cpp/src/thrift/transport/TTransport.h ---------------------------------------------------------------------- diff --git a/lib/cpp/src/thrift/transport/TTransport.h b/lib/cpp/src/thrift/transport/TTransport.h index 3b552c4..6e9a698 100644 --- a/lib/cpp/src/thrift/transport/TTransport.h +++ b/lib/cpp/src/thrift/transport/TTransport.h @@ -237,6 +237,18 @@ class TTransport { "Base TTransport cannot consume."); } + /** + * Returns the origin of the transports call. The value depends on the + * transport used. An IP based transport for example will return the + * IP address of the client making the request. + * If the transport doesn't know the origin Unknown is returned. + * + * The returned value can be used in a log message for example + */ + virtual const std::string getOrigin() { + return "Unknown"; + } + protected: /** * Simple constructor.
