2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy-config.cpp,v 1.9 2003-10-13 19:16: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,
125 for (; ptr; ptr = ptr->next)
127 if (ptr->type == XML_ELEMENT_NODE
128 && !strcmp((const char *) ptr->name, "preinit"))
130 const char *v = get_text(ptr);
131 *pre_init = v ? atoi(v) : 1;
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"))
147 *keepalive_limit_bw = 500000;
148 *keepalive_limit_pdu = 1000;
149 return_limit(ptr, keepalive_limit_bw, keepalive_limit_pdu,
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, "attribute"))
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 const char *match_marcxml = 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;
360 if (!strcmp((const char *) attr->name, "marcxml") &&
361 attr->children && attr->children->type == XML_TEXT_NODE)
362 match_marcxml = (const char *) attr->children->content;
366 if (!strcmp(match_type, "*"))
368 else if (!strcmp(match_type, "none"))
375 int match_oid[OID_SIZE];
376 oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
377 if (oid_oidcmp(match_oid, syntax) == 0)
391 char dotoid_str[100];
392 oid_to_dotstring(syntax, dotoid_str);
393 *addinfo = odr_strdup(odr, dotoid_str);
395 return atoi(match_error);
406 xmlNodePtr Yaz_ProxyConfig::find_target_node(const char *name)
411 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
413 if (ptr->type == XML_ELEMENT_NODE &&
414 !strcmp((const char *) ptr->name, "target"))
419 // <target default="1"> ?
420 struct _xmlAttr *attr;
421 for (attr = ptr->properties; attr; attr = attr->next)
422 if (!strcmp((const char *) attr->name, "default") &&
423 attr->children && attr->children->type == XML_TEXT_NODE)
425 xmlChar *t = attr->children->content;
432 // <target name="name"> ?
433 struct _xmlAttr *attr;
434 for (attr = ptr->properties; attr; attr = attr->next)
435 if (!strcmp((const char *) attr->name, "name"))
438 && attr->children->type==XML_TEXT_NODE
439 && attr->children->content
440 && (!strcmp((const char *) attr->children->content,
442 || !strcmp((const char *) attr->children->content,
455 int Yaz_ProxyConfig::get_target_no(int no,
461 int *target_idletime,
462 int *client_idletime,
464 int *keepalive_limit_bw,
465 int *keepalive_limit_pdu,
473 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
474 if (ptr->type == XML_ELEMENT_NODE &&
475 !strcmp((const char *) ptr->name, "target"))
479 struct _xmlAttr *attr;
480 for (attr = ptr->properties; attr; attr = attr->next)
481 if (!strcmp((const char *) attr->name, "name"))
484 && attr->children->type==XML_TEXT_NODE
485 && attr->children->content)
486 *name = (const char *) attr->children->content;
488 return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
489 target_idletime, client_idletime,
490 keepalive_limit_bw, keepalive_limit_pdu,
500 void Yaz_ProxyConfig::get_target_info(const char *name,
505 int *target_idletime,
506 int *client_idletime,
508 int *keepalive_limit_bw,
509 int *keepalive_limit_pdu,
521 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
523 if (ptr->type == XML_ELEMENT_NODE &&
524 !strcmp((const char *) ptr->name, "max-clients"))
526 const char *t = get_text(ptr);
529 *max_clients = atoi(t);
530 if (*max_clients < 1)
535 ptr = find_target_node(name);
543 return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
544 target_idletime, client_idletime,
545 keepalive_limit_bw, keepalive_limit_pdu,