2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy-config.cpp,v 1.2 2003-10-03 13:01:42 adam Exp $
9 #include <yaz++/proxy.h>
11 Yaz_ProxyConfig::Yaz_ProxyConfig()
20 Yaz_ProxyConfig::~Yaz_ProxyConfig()
23 if (!m_copy && m_docPtr)
28 void Yaz_ProxyConfig::operator=(const Yaz_ProxyConfig &conf)
31 m_docPtr = conf.m_docPtr;
32 m_proxyPtr = conf.m_proxyPtr;
37 int Yaz_ProxyConfig::read_xml(const char *fname)
40 xmlDocPtr ndoc = xmlParseFile(fname);
44 yaz_log(LOG_WARN, "Config file %s not found or parse error", fname);
47 xmlNodePtr proxyPtr = xmlDocGetRootElement(ndoc);
48 if (!proxyPtr || proxyPtr->type != XML_ELEMENT_NODE ||
49 strcmp((const char *) proxyPtr->name, "proxy"))
51 yaz_log(LOG_WARN, "No proxy element in %s", fname);
55 m_proxyPtr = proxyPtr;
57 // OK: release previous and make it the current one.
68 const char *Yaz_ProxyConfig::get_text(xmlNodePtr ptr)
70 for(ptr = ptr->children; ptr; ptr = ptr->next)
71 if (ptr->type == XML_TEXT_NODE)
73 xmlChar *t = ptr->content;
78 return (const char *) t;
86 void Yaz_ProxyConfig::return_limit(xmlNodePtr ptr,
91 for (ptr = ptr->children; ptr; ptr = ptr->next)
93 if (ptr->type == XML_ELEMENT_NODE
94 && !strcmp((const char *) ptr->name, "bandwidth"))
96 const char *t = get_text(ptr);
100 if (ptr->type == XML_ELEMENT_NODE
101 && !strcmp((const char *) ptr->name, "retrieve"))
103 const char *t = get_text(ptr);
105 *limit_req = atoi(t);
107 if (ptr->type == XML_ELEMENT_NODE
108 && !strcmp((const char *) ptr->name, "pdu"))
110 const char *t = get_text(ptr);
112 *limit_pdu = atoi(t);
119 void Yaz_ProxyConfig::return_target_info(xmlNodePtr ptr,
125 int *target_idletime,
126 int *client_idletime)
129 for (; ptr; ptr = ptr->next)
131 if (ptr->type == XML_ELEMENT_NODE
132 && !strcmp((const char *) ptr->name, "url"))
134 const char *t = get_text(ptr);
138 if (ptr->type == XML_ELEMENT_NODE
139 && !strcmp((const char *) ptr->name, "keepalive"))
141 const char *t = get_text(ptr);
147 if (ptr->type == XML_ELEMENT_NODE
148 && !strcmp((const char *) ptr->name, "limit"))
149 return_limit(ptr, limit_bw, limit_pdu, limit_req);
150 if (ptr->type == XML_ELEMENT_NODE
151 && !strcmp((const char *) ptr->name, "target-timeout"))
153 const char *t = get_text(ptr);
156 *target_idletime = atoi(t);
157 if (*target_idletime < 0)
158 *target_idletime = 0;
161 if (ptr->type == XML_ELEMENT_NODE
162 && !strcmp((const char *) ptr->name, "client-timeout"))
164 const char *t = get_text(ptr);
167 *client_idletime = atoi(t);
168 if (*client_idletime < 0)
169 *client_idletime = 0;
176 int Yaz_ProxyConfig::check_type_1_attributes(ODR odr, xmlNodePtr ptr,
177 Z_AttributeList *attrs,
180 for(ptr = ptr->children; ptr; ptr = ptr->next)
182 if (ptr->type == XML_ELEMENT_NODE &&
183 !strcmp((const char *) ptr->name, "query"))
185 const char *match_type = 0;
186 const char *match_value = 0;
187 const char *match_error = 0;
188 struct _xmlAttr *attr;
189 for (attr = ptr->properties; attr; attr = attr->next)
191 if (!strcmp((const char *) attr->name, "type") &&
192 attr->children && attr->children->type == XML_TEXT_NODE)
193 match_type = (const char *) attr->children->content;
194 if (!strcmp((const char *) attr->name, "value") &&
195 attr->children && attr->children->type == XML_TEXT_NODE)
196 match_value = (const char *) attr->children->content;
197 if (!strcmp((const char *) attr->name, "error") &&
198 attr->children && attr->children->type == XML_TEXT_NODE)
199 match_error = (const char *) attr->children->content;
203 if (match_type && match_value)
205 for (i = 0; i<attrs->num_attributes; i++)
207 Z_AttributeElement *el = attrs->attributes[i];
211 if (!el->attributeType)
213 int type = *el->attributeType;
215 if (strcmp(match_type, "*")) {
216 if (type != atoi(match_type))
217 continue; // no match on type
219 if (el->which == Z_AttributeValue_numeric &&
222 int value = *el->value.numeric;
223 if (strcmp(match_value, "*")) {
224 if (value != atoi(match_value))
225 continue; // no match on value
227 sprintf(value_str, "%d", value);
234 *addinfo = odr_strdup(odr, value_str);
235 return atoi(match_error);
245 int Yaz_ProxyConfig::check_type_1_structure(ODR odr, xmlNodePtr ptr,
250 if (q->which == Z_RPNStructure_complex)
252 int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
255 e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
258 else if (q->which == Z_RPNStructure_simple)
260 if (q->u.simple->which == Z_Operand_APT)
262 return check_type_1_attributes(
263 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
270 int Yaz_ProxyConfig::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
273 // possibly check for Bib-1
274 return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
277 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
282 ptr = find_target_node(name);
285 if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
286 return check_type_1(odr, ptr, query->u.type_1, addinfo);
291 xmlNodePtr Yaz_ProxyConfig::find_target_node(const char *name)
294 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
296 if (ptr->type == XML_ELEMENT_NODE &&
297 !strcmp((const char *) ptr->name, "target"))
302 // <target default="1"> ?
303 struct _xmlAttr *attr;
304 for (attr = ptr->properties; attr; attr = attr->next)
305 if (!strcmp((const char *) attr->name, "default") &&
306 attr->children && attr->children->type == XML_TEXT_NODE)
308 xmlChar *t = attr->children->content;
315 // <target name="name"> ?
316 struct _xmlAttr *attr;
317 for (attr = ptr->properties; attr; attr = attr->next)
318 if (!strcmp((const char *) attr->name, "name"))
321 && attr->children->type==XML_TEXT_NODE
322 && attr->children->content
323 && (!strcmp((const char *) attr->children->content,
325 || !strcmp((const char *) attr->children->content,
338 void Yaz_ProxyConfig::get_target_info(const char *name,
344 int *target_idletime,
345 int *client_idletime,
355 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
357 if (ptr->type == XML_ELEMENT_NODE &&
358 !strcmp((const char *) ptr->name, "max-clients"))
360 const char *t = get_text(ptr);
363 *max_clients = atoi(t);
364 if (*max_clients < 1)
369 ptr = find_target_node(name);
374 return_target_info(ptr, url, keepalive, limit_bw, limit_pdu, limit_req,
375 target_idletime, client_idletime);