2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy-config.cpp,v 1.5 2003-10-08 08:15:01 adam Exp $
10 #include <yaz++/proxy.h>
12 Yaz_ProxyConfig::Yaz_ProxyConfig()
21 Yaz_ProxyConfig::~Yaz_ProxyConfig()
24 if (!m_copy && m_docPtr)
29 void Yaz_ProxyConfig::operator=(const Yaz_ProxyConfig &conf)
32 m_docPtr = conf.m_docPtr;
33 m_proxyPtr = conf.m_proxyPtr;
38 int Yaz_ProxyConfig::read_xml(const char *fname)
41 xmlDocPtr ndoc = xmlParseFile(fname);
45 yaz_log(LOG_WARN, "Config file %s not found or parse error", fname);
48 xmlNodePtr proxyPtr = xmlDocGetRootElement(ndoc);
49 if (!proxyPtr || proxyPtr->type != XML_ELEMENT_NODE ||
50 strcmp((const char *) proxyPtr->name, "proxy"))
52 yaz_log(LOG_WARN, "No proxy element in %s", fname);
56 m_proxyPtr = proxyPtr;
58 // OK: release previous and make it the current one.
69 const char *Yaz_ProxyConfig::get_text(xmlNodePtr ptr)
71 for(ptr = ptr->children; ptr; ptr = ptr->next)
72 if (ptr->type == XML_TEXT_NODE)
74 xmlChar *t = ptr->content;
79 return (const char *) t;
87 void Yaz_ProxyConfig::return_limit(xmlNodePtr ptr,
92 for (ptr = ptr->children; ptr; ptr = ptr->next)
94 if (ptr->type == XML_ELEMENT_NODE
95 && !strcmp((const char *) ptr->name, "bandwidth"))
97 const char *t = get_text(ptr);
101 if (ptr->type == XML_ELEMENT_NODE
102 && !strcmp((const char *) ptr->name, "retrieve"))
104 const char *t = get_text(ptr);
106 *limit_req = atoi(t);
108 if (ptr->type == XML_ELEMENT_NODE
109 && !strcmp((const char *) ptr->name, "pdu"))
111 const char *t = get_text(ptr);
113 *limit_pdu = atoi(t);
120 void Yaz_ProxyConfig::return_target_info(xmlNodePtr ptr,
126 int *target_idletime,
127 int *client_idletime)
130 for (; ptr; ptr = ptr->next)
132 if (ptr->type == XML_ELEMENT_NODE
133 && !strcmp((const char *) ptr->name, "url"))
135 const char *t = get_text(ptr);
139 if (ptr->type == XML_ELEMENT_NODE
140 && !strcmp((const char *) ptr->name, "keepalive"))
142 const char *t = get_text(ptr);
148 if (ptr->type == XML_ELEMENT_NODE
149 && !strcmp((const char *) ptr->name, "limit"))
150 return_limit(ptr, limit_bw, limit_pdu, limit_req);
151 if (ptr->type == XML_ELEMENT_NODE
152 && !strcmp((const char *) ptr->name, "target-timeout"))
154 const char *t = get_text(ptr);
157 *target_idletime = atoi(t);
158 if (*target_idletime < 0)
159 *target_idletime = 0;
162 if (ptr->type == XML_ELEMENT_NODE
163 && !strcmp((const char *) ptr->name, "client-timeout"))
165 const char *t = get_text(ptr);
168 *client_idletime = atoi(t);
169 if (*client_idletime < 0)
170 *client_idletime = 0;
177 int Yaz_ProxyConfig::atoi_l(const char **cp)
180 while (**cp && isdigit(**cp))
182 v = v*10 + (**cp - '0');
188 int Yaz_ProxyConfig::match_list(int v, const char *m)
192 while(*m && isspace(*m))
203 if (v >= l && v <= h)
212 int Yaz_ProxyConfig::check_type_1_attributes(ODR odr, xmlNodePtr ptr,
213 Z_AttributeList *attrs,
216 for(ptr = ptr->children; ptr; ptr = ptr->next)
218 if (ptr->type == XML_ELEMENT_NODE &&
219 !strcmp((const char *) ptr->name, "query"))
221 const char *match_type = 0;
222 const char *match_value = 0;
223 const char *match_error = 0;
224 struct _xmlAttr *attr;
225 for (attr = ptr->properties; attr; attr = attr->next)
227 if (!strcmp((const char *) attr->name, "type") &&
228 attr->children && attr->children->type == XML_TEXT_NODE)
229 match_type = (const char *) attr->children->content;
230 if (!strcmp((const char *) attr->name, "value") &&
231 attr->children && attr->children->type == XML_TEXT_NODE)
232 match_value = (const char *) attr->children->content;
233 if (!strcmp((const char *) attr->name, "error") &&
234 attr->children && attr->children->type == XML_TEXT_NODE)
235 match_error = (const char *) attr->children->content;
239 if (match_type && match_value)
241 for (i = 0; i<attrs->num_attributes; i++)
243 Z_AttributeElement *el = attrs->attributes[i];
247 if (!el->attributeType)
249 int type = *el->attributeType;
251 if (!match_list(type, match_type))
253 if (el->which == Z_AttributeValue_numeric &&
256 if (!match_list(*el->value.numeric, match_value))
258 sprintf (value_str, "%d", *el->value.numeric);
265 *addinfo = odr_strdup(odr, value_str);
266 return atoi(match_error);
278 int Yaz_ProxyConfig::check_type_1_structure(ODR odr, xmlNodePtr ptr,
283 if (q->which == Z_RPNStructure_complex)
285 int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
288 e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
291 else if (q->which == Z_RPNStructure_simple)
293 if (q->u.simple->which == Z_Operand_APT)
295 return check_type_1_attributes(
296 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
305 int Yaz_ProxyConfig::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
308 // possibly check for Bib-1
309 return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
313 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
319 ptr = find_target_node(name);
322 if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
323 return check_type_1(odr, ptr, query->u.type_1, addinfo);
329 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
330 Odr_oid *syntax, char **addinfo)
335 ptr = find_target_node(name);
338 for(ptr = ptr->children; ptr; ptr = ptr->next)
340 if (ptr->type == XML_ELEMENT_NODE &&
341 !strcmp((const char *) ptr->name, "syntax"))
343 int match = 0; // if we match record syntax
344 const char *match_type = 0;
345 const char *match_error = 0;
346 struct _xmlAttr *attr;
347 for (attr = ptr->properties; attr; attr = attr->next)
349 if (!strcmp((const char *) attr->name, "type") &&
350 attr->children && attr->children->type == XML_TEXT_NODE)
351 match_type = (const char *) attr->children->content;
352 if (!strcmp((const char *) attr->name, "error") &&
353 attr->children && attr->children->type == XML_TEXT_NODE)
354 match_error = (const char *) attr->children->content;
358 if (!strcmp(match_type, "*"))
360 else if (!strcmp(match_type, "none"))
367 int match_oid[OID_SIZE];
368 oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
369 if (oid_oidcmp(match_oid, syntax) == 0)
379 char dotoid_str[100];
380 oid_to_dotstring(syntax, dotoid_str);
381 *addinfo = odr_strdup(odr, dotoid_str);
383 return atoi(match_error);
394 xmlNodePtr Yaz_ProxyConfig::find_target_node(const char *name)
397 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
399 if (ptr->type == XML_ELEMENT_NODE &&
400 !strcmp((const char *) ptr->name, "target"))
405 // <target default="1"> ?
406 struct _xmlAttr *attr;
407 for (attr = ptr->properties; attr; attr = attr->next)
408 if (!strcmp((const char *) attr->name, "default") &&
409 attr->children && attr->children->type == XML_TEXT_NODE)
411 xmlChar *t = attr->children->content;
418 // <target name="name"> ?
419 struct _xmlAttr *attr;
420 for (attr = ptr->properties; attr; attr = attr->next)
421 if (!strcmp((const char *) attr->name, "name"))
424 && attr->children->type==XML_TEXT_NODE
425 && attr->children->content
426 && (!strcmp((const char *) attr->children->content,
428 || !strcmp((const char *) attr->children->content,
441 void Yaz_ProxyConfig::get_target_info(const char *name,
447 int *target_idletime,
448 int *client_idletime,
458 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
460 if (ptr->type == XML_ELEMENT_NODE &&
461 !strcmp((const char *) ptr->name, "max-clients"))
463 const char *t = get_text(ptr);
466 *max_clients = atoi(t);
467 if (*max_clients < 1)
472 ptr = find_target_node(name);
477 return_target_info(ptr, url, keepalive, limit_bw, limit_pdu, limit_req,
478 target_idletime, client_idletime);