1 /* This file is part of Metaproxy.
2 Copyright (C) Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <metaproxy/origin.hpp>
25 namespace mp = metaproxy_1;
27 mp::Origin::Origin() : m_address(""), m_origin_id(0), m_max_sockets(0)
31 void mp::Origin::set_max_sockets(int max_sockets)
33 m_max_sockets = max_sockets;
36 int mp::Origin::get_max_sockets()
41 void mp::Origin::set_tcpip_address(std::string addr, unsigned long s)
43 // assume first call is immediate reverse IP: + bind IP
44 // 2nd call might be X-Forwarded .. we use that for logging
45 std::string tmp = m_address;
49 m_address.append(" ");
50 m_address.append(tmp);
54 size_t pos = addr.find(' ');
55 assert (pos != std::string::npos);
60 void mp::Origin::set_custom_session(const std::string &s)
65 std::string mp::Origin::get_forward_address() const
67 // return first component of address
68 // That's either first part of X-Forwarded component
69 size_t pos = m_address.find(' ');
70 if (pos != std::string::npos)
71 return m_address.substr(0, pos);
76 std::string mp::Origin::get_address()
78 // return 2nd last component of address (last is listening IP)
79 size_t pos2 = m_address.rfind(' ');
80 if (pos2 != std::string::npos && pos2 > 0)
82 size_t pos1 = m_address.rfind(' ', pos2 - 1);
83 if (pos1 != std::string::npos)
84 return m_address.substr(pos1 + 1, pos2 - pos1 - 1);
86 return m_address.substr(0, pos2);
92 std::string mp::Origin::get_bind_address()
94 // return last component of address
95 size_t pos2 = m_address.rfind(' ');
96 if (pos2 != std::string::npos && pos2 > 0)
98 return m_address.substr(pos2 + 1);
105 std::ostream& std::operator<<(std::ostream& os, const mp::Origin& o)
107 // print first component of address
108 std::string a = o.get_forward_address();
113 os << ":" << o.m_origin_id;
114 if (o.m_custom_session.length())
115 os << ":" << o.m_custom_session;
122 * c-file-style: "Stroustrup"
123 * indent-tabs-mode: nil
125 * vim: shiftwidth=4 tabstop=8 expandtab