2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy-config.cpp,v 1.8 2003-10-10 17:58:29 adam Exp $
10 #include <yaz++/proxy.h>
12 Yaz_ProxyConfig::Yaz_ProxyConfig()
21 Yaz_ProxyConfig::~Yaz_ProxyConfig()
24 if (!m_copy && m_docPtr)
29 int Yaz_ProxyConfig::read_xml(const char *fname)
32 xmlDocPtr ndoc = xmlParseFile(fname);
36 yaz_log(LOG_WARN, "Config file %s not found or parse error", fname);
39 xmlNodePtr proxyPtr = xmlDocGetRootElement(ndoc);
40 if (!proxyPtr || proxyPtr->type != XML_ELEMENT_NODE ||
41 strcmp((const char *) proxyPtr->name, "proxy"))
43 yaz_log(LOG_WARN, "No proxy element in %s", fname);
47 m_proxyPtr = proxyPtr;
49 // OK: release previous and make it the current one.
60 const char *Yaz_ProxyConfig::get_text(xmlNodePtr ptr)
62 for(ptr = ptr->children; ptr; ptr = ptr->next)
63 if (ptr->type == XML_TEXT_NODE)
65 xmlChar *t = ptr->content;
70 return (const char *) t;
78 void Yaz_ProxyConfig::return_limit(xmlNodePtr ptr,
83 for (ptr = ptr->children; ptr; ptr = ptr->next)
85 if (ptr->type == XML_ELEMENT_NODE
86 && !strcmp((const char *) ptr->name, "bandwidth"))
88 const char *t = get_text(ptr);
92 if (ptr->type == XML_ELEMENT_NODE
93 && !strcmp((const char *) ptr->name, "retrieve"))
95 const char *t = get_text(ptr);
99 if (ptr->type == XML_ELEMENT_NODE
100 && !strcmp((const char *) ptr->name, "pdu"))
102 const char *t = get_text(ptr);
104 *limit_pdu = atoi(t);
111 void Yaz_ProxyConfig::return_target_info(xmlNodePtr ptr,
116 int *target_idletime,
117 int *client_idletime,
118 int *keepalive_limit_bw,
119 int *keepalive_limit_pdu)
123 for (; ptr; ptr = ptr->next)
125 if (ptr->type == XML_ELEMENT_NODE
126 && !strcmp((const char *) ptr->name, "url"))
128 const char *t = get_text(ptr);
129 if (t && no_url < MAX_ZURL_PLEX)
135 if (ptr->type == XML_ELEMENT_NODE
136 && !strcmp((const char *) ptr->name, "keepalive"))
139 *keepalive_limit_bw = 500000;
140 *keepalive_limit_pdu = 1000;
141 return_limit(ptr, keepalive_limit_bw, keepalive_limit_pdu,
144 if (ptr->type == XML_ELEMENT_NODE
145 && !strcmp((const char *) ptr->name, "limit"))
146 return_limit(ptr, limit_bw, limit_pdu, limit_req);
147 if (ptr->type == XML_ELEMENT_NODE
148 && !strcmp((const char *) ptr->name, "target-timeout"))
150 const char *t = get_text(ptr);
153 *target_idletime = atoi(t);
154 if (*target_idletime < 0)
155 *target_idletime = 0;
158 if (ptr->type == XML_ELEMENT_NODE
159 && !strcmp((const char *) ptr->name, "client-timeout"))
161 const char *t = get_text(ptr);
164 *client_idletime = atoi(t);
165 if (*client_idletime < 0)
166 *client_idletime = 0;
173 int Yaz_ProxyConfig::atoi_l(const char **cp)
176 while (**cp && isdigit(**cp))
178 v = v*10 + (**cp - '0');
184 int Yaz_ProxyConfig::match_list(int v, const char *m)
188 while(*m && isspace(*m))
199 if (v >= l && v <= h)
208 int Yaz_ProxyConfig::check_type_1_attributes(ODR odr, xmlNodePtr ptr,
209 Z_AttributeList *attrs,
212 for(ptr = ptr->children; ptr; ptr = ptr->next)
214 if (ptr->type == XML_ELEMENT_NODE &&
215 !strcmp((const char *) ptr->name, "attribute"))
217 const char *match_type = 0;
218 const char *match_value = 0;
219 const char *match_error = 0;
220 struct _xmlAttr *attr;
221 for (attr = ptr->properties; attr; attr = attr->next)
223 if (!strcmp((const char *) attr->name, "type") &&
224 attr->children && attr->children->type == XML_TEXT_NODE)
225 match_type = (const char *) attr->children->content;
226 if (!strcmp((const char *) attr->name, "value") &&
227 attr->children && attr->children->type == XML_TEXT_NODE)
228 match_value = (const char *) attr->children->content;
229 if (!strcmp((const char *) attr->name, "error") &&
230 attr->children && attr->children->type == XML_TEXT_NODE)
231 match_error = (const char *) attr->children->content;
235 if (match_type && match_value)
237 for (i = 0; i<attrs->num_attributes; i++)
239 Z_AttributeElement *el = attrs->attributes[i];
243 if (!el->attributeType)
245 int type = *el->attributeType;
247 if (!match_list(type, match_type))
249 if (el->which == Z_AttributeValue_numeric &&
252 if (!match_list(*el->value.numeric, match_value))
254 sprintf (value_str, "%d", *el->value.numeric);
261 *addinfo = odr_strdup(odr, value_str);
262 return atoi(match_error);
274 int Yaz_ProxyConfig::check_type_1_structure(ODR odr, xmlNodePtr ptr,
279 if (q->which == Z_RPNStructure_complex)
281 int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
284 e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
287 else if (q->which == Z_RPNStructure_simple)
289 if (q->u.simple->which == Z_Operand_APT)
291 return check_type_1_attributes(
292 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
301 int Yaz_ProxyConfig::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
304 // possibly check for Bib-1
305 return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
309 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
315 ptr = find_target_node(name);
318 if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
319 return check_type_1(odr, ptr, query->u.type_1, addinfo);
325 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
326 Odr_oid *syntax, char **addinfo)
331 ptr = find_target_node(name);
334 for(ptr = ptr->children; ptr; ptr = ptr->next)
336 if (ptr->type == XML_ELEMENT_NODE &&
337 !strcmp((const char *) ptr->name, "syntax"))
339 int match = 0; // if we match record syntax
340 const char *match_type = 0;
341 const char *match_error = 0;
342 const char *match_marcxml = 0;
343 struct _xmlAttr *attr;
344 for (attr = ptr->properties; attr; attr = attr->next)
346 if (!strcmp((const char *) attr->name, "type") &&
347 attr->children && attr->children->type == XML_TEXT_NODE)
348 match_type = (const char *) attr->children->content;
349 if (!strcmp((const char *) attr->name, "error") &&
350 attr->children && attr->children->type == XML_TEXT_NODE)
351 match_error = (const char *) attr->children->content;
352 if (!strcmp((const char *) attr->name, "marcxml") &&
353 attr->children && attr->children->type == XML_TEXT_NODE)
354 match_marcxml = (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)
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)
403 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
405 if (ptr->type == XML_ELEMENT_NODE &&
406 !strcmp((const char *) ptr->name, "target"))
411 // <target default="1"> ?
412 struct _xmlAttr *attr;
413 for (attr = ptr->properties; attr; attr = attr->next)
414 if (!strcmp((const char *) attr->name, "default") &&
415 attr->children && attr->children->type == XML_TEXT_NODE)
417 xmlChar *t = attr->children->content;
424 // <target name="name"> ?
425 struct _xmlAttr *attr;
426 for (attr = ptr->properties; attr; attr = attr->next)
427 if (!strcmp((const char *) attr->name, "name"))
430 && attr->children->type==XML_TEXT_NODE
431 && attr->children->content
432 && (!strcmp((const char *) attr->children->content,
434 || !strcmp((const char *) attr->children->content,
447 void Yaz_ProxyConfig::get_target_info(const char *name,
452 int *target_idletime,
453 int *client_idletime,
455 int *keepalive_limit_bw,
456 int *keepalive_limit_pdu)
467 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
469 if (ptr->type == XML_ELEMENT_NODE &&
470 !strcmp((const char *) ptr->name, "max-clients"))
472 const char *t = get_text(ptr);
475 *max_clients = atoi(t);
476 if (*max_clients < 1)
481 ptr = find_target_node(name);
489 return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
490 target_idletime, client_idletime,
491 keepalive_limit_bw, keepalive_limit_pdu);