2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy-config.cpp,v 1.3 2003-10-04 06:44:16 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;
177 int Yaz_ProxyConfig::check_type_1_attributes(ODR odr, xmlNodePtr ptr,
178 Z_AttributeList *attrs,
181 for(ptr = ptr->children; ptr; ptr = ptr->next)
183 if (ptr->type == XML_ELEMENT_NODE &&
184 !strcmp((const char *) ptr->name, "query"))
186 const char *match_type = 0;
187 const char *match_value = 0;
188 const char *match_error = 0;
189 struct _xmlAttr *attr;
190 for (attr = ptr->properties; attr; attr = attr->next)
192 if (!strcmp((const char *) attr->name, "type") &&
193 attr->children && attr->children->type == XML_TEXT_NODE)
194 match_type = (const char *) attr->children->content;
195 if (!strcmp((const char *) attr->name, "value") &&
196 attr->children && attr->children->type == XML_TEXT_NODE)
197 match_value = (const char *) attr->children->content;
198 if (!strcmp((const char *) attr->name, "error") &&
199 attr->children && attr->children->type == XML_TEXT_NODE)
200 match_error = (const char *) attr->children->content;
204 if (match_type && match_value)
206 for (i = 0; i<attrs->num_attributes; i++)
208 Z_AttributeElement *el = attrs->attributes[i];
212 if (!el->attributeType)
214 int type = *el->attributeType;
216 if (strcmp(match_type, "*")) {
217 if (type != atoi(match_type))
218 continue; // no match on type
220 if (el->which == Z_AttributeValue_numeric &&
223 int value = *el->value.numeric;
224 if (strcmp(match_value, "*")) {
225 if (value != atoi(match_value))
226 continue; // no match on value
228 sprintf(value_str, "%d", value);
235 *addinfo = odr_strdup(odr, value_str);
236 return atoi(match_error);
248 int Yaz_ProxyConfig::check_type_1_structure(ODR odr, xmlNodePtr ptr,
253 if (q->which == Z_RPNStructure_complex)
255 int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
258 e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
261 else if (q->which == Z_RPNStructure_simple)
263 if (q->u.simple->which == Z_Operand_APT)
265 return check_type_1_attributes(
266 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
275 int Yaz_ProxyConfig::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
278 // possibly check for Bib-1
279 return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
283 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
289 ptr = find_target_node(name);
292 if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
293 return check_type_1(odr, ptr, query->u.type_1, addinfo);
300 xmlNodePtr Yaz_ProxyConfig::find_target_node(const char *name)
303 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
305 if (ptr->type == XML_ELEMENT_NODE &&
306 !strcmp((const char *) ptr->name, "target"))
311 // <target default="1"> ?
312 struct _xmlAttr *attr;
313 for (attr = ptr->properties; attr; attr = attr->next)
314 if (!strcmp((const char *) attr->name, "default") &&
315 attr->children && attr->children->type == XML_TEXT_NODE)
317 xmlChar *t = attr->children->content;
324 // <target name="name"> ?
325 struct _xmlAttr *attr;
326 for (attr = ptr->properties; attr; attr = attr->next)
327 if (!strcmp((const char *) attr->name, "name"))
330 && attr->children->type==XML_TEXT_NODE
331 && attr->children->content
332 && (!strcmp((const char *) attr->children->content,
334 || !strcmp((const char *) attr->children->content,
347 void Yaz_ProxyConfig::get_target_info(const char *name,
353 int *target_idletime,
354 int *client_idletime,
364 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
366 if (ptr->type == XML_ELEMENT_NODE &&
367 !strcmp((const char *) ptr->name, "max-clients"))
369 const char *t = get_text(ptr);
372 *max_clients = atoi(t);
373 if (*max_clients < 1)
378 ptr = find_target_node(name);
383 return_target_info(ptr, url, keepalive, limit_bw, limit_pdu, limit_req,
384 target_idletime, client_idletime);