e4f9415cf4494aa1b6edfb6a63d34957b6a5c685
[metaproxy-moved-to-github.git] / src / origin.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17 */
18
19 #include "config.hpp"
20 #include <metaproxy/origin.hpp>
21 #include <yaz/log.h>
22 #include <iostream>
23
24 namespace mp = metaproxy_1;
25
26 mp::Origin::Origin() : m_address(""), m_origin_id(0), m_max_sockets(0)
27 {
28 }
29
30 void mp::Origin::set_max_sockets(int max_sockets)
31 {
32     m_max_sockets = max_sockets;
33 }
34
35 int mp::Origin::get_max_sockets()
36 {
37     return m_max_sockets;
38 }
39
40 void mp::Origin::set_tcpip_address(std::string addr, unsigned long s)
41 {
42     // assume first call is immediate reverse IP: cs_addrstr(COMSTACK)
43     // 2nd call might be X-Forwarded .. we use that for logging
44     std::string tmp = m_address;
45     m_address = addr;
46     if (tmp.length())
47     {
48         m_address.append(" ");
49         m_address.append(tmp);
50     }
51     m_origin_id = s;
52 }
53
54 void mp::Origin::set_custom_session(const std::string &s)
55 {
56     m_custom_session = s;
57 }
58
59 std::string mp::Origin::get_forward_address() const
60 {
61     // return first component of address
62     // That's either first part of X-Forwarded component
63     size_t pos = m_address.find(' ');
64     if (pos != std::string::npos)
65         return m_address.substr(0, pos);
66     else
67         return m_address;
68 }
69
70 std::string mp::Origin::get_address()
71 {
72     // return last component of address
73     size_t pos = m_address.rfind(' ');
74     if (pos != std::string::npos)
75         return m_address.substr(pos + 1);
76     else
77         return m_address;
78 }
79
80 std::ostream& std::operator<<(std::ostream& os, const mp::Origin& o)
81 {
82     // print first component of address
83     std::string a = o.get_forward_address();
84     if (a.length())
85         os << a;
86     else
87         os << "0";
88     os << ":" << o.m_origin_id;
89     if (o.m_custom_session.length())
90         os << ":" << o.m_custom_session;
91     return os;
92 }
93
94 /*
95  * Local variables:
96  * c-basic-offset: 4
97  * c-file-style: "Stroustrup"
98  * indent-tabs-mode: nil
99  * End:
100  * vim: shiftwidth=4 tabstop=8 expandtab
101  */
102