2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy-config.cpp,v 1.7 2003-10-09 12:11:10 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,
125 int *target_idletime,
126 int *client_idletime,
127 int *keepalive_limit_bw,
128 int *keepalive_limit_pdu)
132 for (; ptr; ptr = ptr->next)
134 if (ptr->type == XML_ELEMENT_NODE
135 && !strcmp((const char *) ptr->name, "url"))
137 const char *t = get_text(ptr);
138 if (t && no_url < MAX_ZURL_PLEX)
144 if (ptr->type == XML_ELEMENT_NODE
145 && !strcmp((const char *) ptr->name, "keepalive"))
148 *keepalive_limit_bw = 500000;
149 *keepalive_limit_pdu = 1000;
150 return_limit(ptr, keepalive_limit_bw, keepalive_limit_pdu,
153 if (ptr->type == XML_ELEMENT_NODE
154 && !strcmp((const char *) ptr->name, "limit"))
155 return_limit(ptr, limit_bw, limit_pdu, limit_req);
156 if (ptr->type == XML_ELEMENT_NODE
157 && !strcmp((const char *) ptr->name, "target-timeout"))
159 const char *t = get_text(ptr);
162 *target_idletime = atoi(t);
163 if (*target_idletime < 0)
164 *target_idletime = 0;
167 if (ptr->type == XML_ELEMENT_NODE
168 && !strcmp((const char *) ptr->name, "client-timeout"))
170 const char *t = get_text(ptr);
173 *client_idletime = atoi(t);
174 if (*client_idletime < 0)
175 *client_idletime = 0;
182 int Yaz_ProxyConfig::atoi_l(const char **cp)
185 while (**cp && isdigit(**cp))
187 v = v*10 + (**cp - '0');
193 int Yaz_ProxyConfig::match_list(int v, const char *m)
197 while(*m && isspace(*m))
208 if (v >= l && v <= h)
217 int Yaz_ProxyConfig::check_type_1_attributes(ODR odr, xmlNodePtr ptr,
218 Z_AttributeList *attrs,
221 for(ptr = ptr->children; ptr; ptr = ptr->next)
223 if (ptr->type == XML_ELEMENT_NODE &&
224 !strcmp((const char *) ptr->name, "attribute"))
226 const char *match_type = 0;
227 const char *match_value = 0;
228 const char *match_error = 0;
229 struct _xmlAttr *attr;
230 for (attr = ptr->properties; attr; attr = attr->next)
232 if (!strcmp((const char *) attr->name, "type") &&
233 attr->children && attr->children->type == XML_TEXT_NODE)
234 match_type = (const char *) attr->children->content;
235 if (!strcmp((const char *) attr->name, "value") &&
236 attr->children && attr->children->type == XML_TEXT_NODE)
237 match_value = (const char *) attr->children->content;
238 if (!strcmp((const char *) attr->name, "error") &&
239 attr->children && attr->children->type == XML_TEXT_NODE)
240 match_error = (const char *) attr->children->content;
244 if (match_type && match_value)
246 for (i = 0; i<attrs->num_attributes; i++)
248 Z_AttributeElement *el = attrs->attributes[i];
252 if (!el->attributeType)
254 int type = *el->attributeType;
256 if (!match_list(type, match_type))
258 if (el->which == Z_AttributeValue_numeric &&
261 if (!match_list(*el->value.numeric, match_value))
263 sprintf (value_str, "%d", *el->value.numeric);
270 *addinfo = odr_strdup(odr, value_str);
271 return atoi(match_error);
283 int Yaz_ProxyConfig::check_type_1_structure(ODR odr, xmlNodePtr ptr,
288 if (q->which == Z_RPNStructure_complex)
290 int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
293 e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
296 else if (q->which == Z_RPNStructure_simple)
298 if (q->u.simple->which == Z_Operand_APT)
300 return check_type_1_attributes(
301 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
310 int Yaz_ProxyConfig::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
313 // possibly check for Bib-1
314 return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
318 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
324 ptr = find_target_node(name);
327 if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
328 return check_type_1(odr, ptr, query->u.type_1, addinfo);
334 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
335 Odr_oid *syntax, char **addinfo)
340 ptr = find_target_node(name);
343 for(ptr = ptr->children; ptr; ptr = ptr->next)
345 if (ptr->type == XML_ELEMENT_NODE &&
346 !strcmp((const char *) ptr->name, "syntax"))
348 int match = 0; // if we match record syntax
349 const char *match_type = 0;
350 const char *match_error = 0;
351 struct _xmlAttr *attr;
352 for (attr = ptr->properties; attr; attr = attr->next)
354 if (!strcmp((const char *) attr->name, "type") &&
355 attr->children && attr->children->type == XML_TEXT_NODE)
356 match_type = (const char *) attr->children->content;
357 if (!strcmp((const char *) attr->name, "error") &&
358 attr->children && attr->children->type == XML_TEXT_NODE)
359 match_error = (const char *) attr->children->content;
363 if (!strcmp(match_type, "*"))
365 else if (!strcmp(match_type, "none"))
372 int match_oid[OID_SIZE];
373 oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
374 if (oid_oidcmp(match_oid, syntax) == 0)
384 char dotoid_str[100];
385 oid_to_dotstring(syntax, dotoid_str);
386 *addinfo = odr_strdup(odr, dotoid_str);
388 return atoi(match_error);
399 xmlNodePtr Yaz_ProxyConfig::find_target_node(const char *name)
404 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
406 if (ptr->type == XML_ELEMENT_NODE &&
407 !strcmp((const char *) ptr->name, "target"))
412 // <target default="1"> ?
413 struct _xmlAttr *attr;
414 for (attr = ptr->properties; attr; attr = attr->next)
415 if (!strcmp((const char *) attr->name, "default") &&
416 attr->children && attr->children->type == XML_TEXT_NODE)
418 xmlChar *t = attr->children->content;
425 // <target name="name"> ?
426 struct _xmlAttr *attr;
427 for (attr = ptr->properties; attr; attr = attr->next)
428 if (!strcmp((const char *) attr->name, "name"))
431 && attr->children->type==XML_TEXT_NODE
432 && attr->children->content
433 && (!strcmp((const char *) attr->children->content,
435 || !strcmp((const char *) attr->children->content,
448 void Yaz_ProxyConfig::get_target_info(const char *name,
453 int *target_idletime,
454 int *client_idletime,
456 int *keepalive_limit_bw,
457 int *keepalive_limit_pdu)
468 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
470 if (ptr->type == XML_ELEMENT_NODE &&
471 !strcmp((const char *) ptr->name, "max-clients"))
473 const char *t = get_text(ptr);
476 *max_clients = atoi(t);
477 if (*max_clients < 1)
482 ptr = find_target_node(name);
490 return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
491 target_idletime, client_idletime,
492 keepalive_limit_bw, keepalive_limit_pdu);