2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy-config.cpp,v 1.6 2003-10-08 09:32:49 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)
131 for (; ptr; ptr = ptr->next)
133 if (ptr->type == XML_ELEMENT_NODE
134 && !strcmp((const char *) ptr->name, "url"))
136 const char *t = get_text(ptr);
137 if (t && no_url < MAX_ZURL_PLEX)
143 if (ptr->type == XML_ELEMENT_NODE
144 && !strcmp((const char *) ptr->name, "keepalive"))
146 const char *t = get_text(ptr);
152 if (ptr->type == XML_ELEMENT_NODE
153 && !strcmp((const char *) ptr->name, "limit"))
154 return_limit(ptr, limit_bw, limit_pdu, limit_req);
155 if (ptr->type == XML_ELEMENT_NODE
156 && !strcmp((const char *) ptr->name, "target-timeout"))
158 const char *t = get_text(ptr);
161 *target_idletime = atoi(t);
162 if (*target_idletime < 0)
163 *target_idletime = 0;
166 if (ptr->type == XML_ELEMENT_NODE
167 && !strcmp((const char *) ptr->name, "client-timeout"))
169 const char *t = get_text(ptr);
172 *client_idletime = atoi(t);
173 if (*client_idletime < 0)
174 *client_idletime = 0;
181 int Yaz_ProxyConfig::atoi_l(const char **cp)
184 while (**cp && isdigit(**cp))
186 v = v*10 + (**cp - '0');
192 int Yaz_ProxyConfig::match_list(int v, const char *m)
196 while(*m && isspace(*m))
207 if (v >= l && v <= h)
216 int Yaz_ProxyConfig::check_type_1_attributes(ODR odr, xmlNodePtr ptr,
217 Z_AttributeList *attrs,
220 for(ptr = ptr->children; ptr; ptr = ptr->next)
222 if (ptr->type == XML_ELEMENT_NODE &&
223 !strcmp((const char *) ptr->name, "query"))
225 const char *match_type = 0;
226 const char *match_value = 0;
227 const char *match_error = 0;
228 struct _xmlAttr *attr;
229 for (attr = ptr->properties; attr; attr = attr->next)
231 if (!strcmp((const char *) attr->name, "type") &&
232 attr->children && attr->children->type == XML_TEXT_NODE)
233 match_type = (const char *) attr->children->content;
234 if (!strcmp((const char *) attr->name, "value") &&
235 attr->children && attr->children->type == XML_TEXT_NODE)
236 match_value = (const char *) attr->children->content;
237 if (!strcmp((const char *) attr->name, "error") &&
238 attr->children && attr->children->type == XML_TEXT_NODE)
239 match_error = (const char *) attr->children->content;
243 if (match_type && match_value)
245 for (i = 0; i<attrs->num_attributes; i++)
247 Z_AttributeElement *el = attrs->attributes[i];
251 if (!el->attributeType)
253 int type = *el->attributeType;
255 if (!match_list(type, match_type))
257 if (el->which == Z_AttributeValue_numeric &&
260 if (!match_list(*el->value.numeric, match_value))
262 sprintf (value_str, "%d", *el->value.numeric);
269 *addinfo = odr_strdup(odr, value_str);
270 return atoi(match_error);
282 int Yaz_ProxyConfig::check_type_1_structure(ODR odr, xmlNodePtr ptr,
287 if (q->which == Z_RPNStructure_complex)
289 int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
292 e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
295 else if (q->which == Z_RPNStructure_simple)
297 if (q->u.simple->which == Z_Operand_APT)
299 return check_type_1_attributes(
300 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
309 int Yaz_ProxyConfig::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
312 // possibly check for Bib-1
313 return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
317 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
323 ptr = find_target_node(name);
326 if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
327 return check_type_1(odr, ptr, query->u.type_1, addinfo);
333 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
334 Odr_oid *syntax, char **addinfo)
339 ptr = find_target_node(name);
342 for(ptr = ptr->children; ptr; ptr = ptr->next)
344 if (ptr->type == XML_ELEMENT_NODE &&
345 !strcmp((const char *) ptr->name, "syntax"))
347 int match = 0; // if we match record syntax
348 const char *match_type = 0;
349 const char *match_error = 0;
350 struct _xmlAttr *attr;
351 for (attr = ptr->properties; attr; attr = attr->next)
353 if (!strcmp((const char *) attr->name, "type") &&
354 attr->children && attr->children->type == XML_TEXT_NODE)
355 match_type = (const char *) attr->children->content;
356 if (!strcmp((const char *) attr->name, "error") &&
357 attr->children && attr->children->type == XML_TEXT_NODE)
358 match_error = (const char *) attr->children->content;
362 if (!strcmp(match_type, "*"))
364 else if (!strcmp(match_type, "none"))
371 int match_oid[OID_SIZE];
372 oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
373 if (oid_oidcmp(match_oid, syntax) == 0)
383 char dotoid_str[100];
384 oid_to_dotstring(syntax, dotoid_str);
385 *addinfo = odr_strdup(odr, dotoid_str);
387 return atoi(match_error);
398 xmlNodePtr Yaz_ProxyConfig::find_target_node(const char *name)
401 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
403 if (ptr->type == XML_ELEMENT_NODE &&
404 !strcmp((const char *) ptr->name, "target"))
409 // <target default="1"> ?
410 struct _xmlAttr *attr;
411 for (attr = ptr->properties; attr; attr = attr->next)
412 if (!strcmp((const char *) attr->name, "default") &&
413 attr->children && attr->children->type == XML_TEXT_NODE)
415 xmlChar *t = attr->children->content;
422 // <target name="name"> ?
423 struct _xmlAttr *attr;
424 for (attr = ptr->properties; attr; attr = attr->next)
425 if (!strcmp((const char *) attr->name, "name"))
428 && attr->children->type==XML_TEXT_NODE
429 && attr->children->content
430 && (!strcmp((const char *) attr->children->content,
432 || !strcmp((const char *) attr->children->content,
445 void Yaz_ProxyConfig::get_target_info(const char *name,
451 int *target_idletime,
452 int *client_idletime,
464 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
466 if (ptr->type == XML_ELEMENT_NODE &&
467 !strcmp((const char *) ptr->name, "max-clients"))
469 const char *t = get_text(ptr);
472 *max_clients = atoi(t);
473 if (*max_clients < 1)
478 ptr = find_target_node(name);
486 return_target_info(ptr, url, keepalive, limit_bw, limit_pdu, limit_req,
487 target_idletime, client_idletime);