metaproxy  1.21.0
origin.cpp
Go to the documentation of this file.
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 <assert.h>
21 #include <metaproxy/origin.hpp>
22 #include <yaz/log.h>
23 #include <iostream>
24 
25 namespace mp = metaproxy_1;
26 
27 mp::Origin::Origin() : m_address(""), m_origin_id(0), m_max_sockets(0)
28 {
29 }
30 
31 void mp::Origin::set_max_sockets(int max_sockets)
32 {
33  m_max_sockets = max_sockets;
34 }
35 
36 int mp::Origin::get_max_sockets()
37 {
38  return m_max_sockets;
39 }
40 
41 void mp::Origin::set_tcpip_address(std::string addr, unsigned long s)
42 {
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;
46  m_address = addr;
47  if (tmp.length())
48  {
49  m_address.append(" ");
50  m_address.append(tmp);
51  }
52  else
53  {
54  size_t pos = addr.find(' ');
55  assert (pos != std::string::npos);
56  }
57  m_origin_id = s;
58 }
59 
60 void mp::Origin::set_custom_session(const std::string &s)
61 {
62  m_custom_session = s;
63 }
64 
65 std::string mp::Origin::get_forward_address() const
66 {
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);
72  else
73  return m_address;
74 }
75 
76 std::string mp::Origin::get_address()
77 {
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)
81  {
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);
85  else
86  return m_address.substr(0, pos2);
87  }
88  else
89  return m_address;
90 }
91 
92 std::string mp::Origin::get_bind_address()
93 {
94  // return last component of address
95  size_t pos2 = m_address.rfind(' ');
96  if (pos2 != std::string::npos && pos2 > 0)
97  {
98  return m_address.substr(pos2 + 1);
99  }
100  else
101  return m_address;
102 }
103 
104 
105 std::ostream& std::operator<<(std::ostream& os, const mp::Origin& o)
106 {
107  // print first component of address
108  std::string a = o.get_forward_address();
109  if (a.length())
110  os << a;
111  else
112  os << "0";
113  os << ":" << o.m_origin_id;
114  if (o.m_custom_session.length())
115  os << ":" << o.m_custom_session;
116  return os;
117 }
118 
119 /*
120  * Local variables:
121  * c-basic-offset: 4
122  * c-file-style: "Stroustrup"
123  * indent-tabs-mode: nil
124  * End:
125  * vim: shiftwidth=4 tabstop=8 expandtab
126  */
127 
std::ostream & operator<<(std::ostream &os, Z_GDU &zgdu)
Definition: gduutil.cpp:33