X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Forigin.cpp;h=b6acea6372f0e583aa9f1a71d1bc6131e0d1e147;hb=586d78659d671683f33ec55f4a7d32b28e345ccd;hp=758134d0314cf29bfea7684cfc2ba14f5dc78e49;hpb=b70b9ec78f0ab1c3ed3b432de986159129a0e4ed;p=metaproxy-moved-to-github.git diff --git a/src/origin.cpp b/src/origin.cpp index 758134d..b6acea6 100644 --- a/src/origin.cpp +++ b/src/origin.cpp @@ -1,68 +1,127 @@ -/* $Id: origin.cpp,v 1.5 2007-01-25 14:05:54 adam Exp $ - Copyright (c) 2005-2007, Index Data. +/* This file is part of Metaproxy. + Copyright (C) Index Data - See the LICENSE file for details - */ +Metaproxy is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 2, or (at your option) any later +version. +Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. -//#include "config.hpp" -#include "origin.hpp" +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "config.hpp" +#include +#include +#include #include namespace mp = metaproxy_1; -mp::Origin::Origin(std::string listen_host, - unsigned int listen_port) - : m_type(API), m_address(""), m_origin_id(0), - m_listen_host(listen_host), m_listen_port(listen_port) +mp::Origin::Origin() : m_address(""), m_origin_id(0), m_max_sockets(0) { } -std::string mp::Origin::listen_host() const +void mp::Origin::set_max_sockets(int max_sockets) { - return m_listen_host; -}; + m_max_sockets = max_sockets; +} -std::string & mp::Origin::listen_host() +int mp::Origin::get_max_sockets() { - return m_listen_host; -}; + return m_max_sockets; +} -unsigned int mp::Origin::listen_port() const +void mp::Origin::set_tcpip_address(std::string addr, unsigned long s) { - return m_listen_port; -}; + // assume first call is immediate reverse IP: + bind IP + // 2nd call might be X-Forwarded .. we use that for logging + std::string tmp = m_address; + m_address = addr; + if (tmp.length()) + { + m_address.append(" "); + m_address.append(tmp); + } + else + { + size_t pos = addr.find(' '); + assert (pos != std::string::npos); + } + m_origin_id = s; +} -unsigned int & mp::Origin::listen_port() +void mp::Origin::set_custom_session(const std::string &s) { - return m_listen_port; -}; + m_custom_session = s; +} +std::string mp::Origin::get_forward_address() const +{ + // return first component of address + // That's either first part of X-Forwarded component + size_t pos = m_address.find(' '); + if (pos != std::string::npos) + return m_address.substr(0, pos); + else + return m_address; +} +std::string mp::Origin::get_address() +{ + // return 2nd last component of address (last is listening IP) + size_t pos2 = m_address.rfind(' '); + if (pos2 != std::string::npos && pos2 > 0) + { + size_t pos1 = m_address.rfind(' ', pos2 - 1); + if (pos1 != std::string::npos) + return m_address.substr(pos1 + 1, pos2 - pos1 - 1); + else + return m_address.substr(0, pos2); + } + else + return m_address; +} -void mp::Origin::set_tcpip_address(std::string addr, unsigned long s) +std::string mp::Origin::get_bind_address() { - m_type = TCPIP; - m_address = addr; - m_origin_id = s; + // return last component of address + size_t pos2 = m_address.rfind(' '); + if (pos2 != std::string::npos && pos2 > 0) + { + return m_address.substr(pos2 + 1); + } + else + return m_address; } -std::ostream& std::operator<<(std::ostream& os, mp::Origin& o) + +std::ostream& std::operator<<(std::ostream& os, const mp::Origin& o) { - if (o.m_address != "") - os << o.m_address; + // print first component of address + std::string a = o.get_forward_address(); + if (a.length()) + os << a; else os << "0"; os << ":" << o.m_origin_id; + if (o.m_custom_session.length()) + os << ":" << o.m_custom_session; return os; } - + /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil - * c-file-style: "stroustrup" * End: * vim: shiftwidth=4 tabstop=8 expandtab */ +