2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy-config.cpp,v 1.19 2004-01-06 21:17:42 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,
121 const char **cql2rpn)
126 for (; ptr; ptr = ptr->next)
128 if (ptr->type == XML_ELEMENT_NODE
129 && !strcmp((const char *) ptr->name, "preinit"))
131 const char *v = get_text(ptr);
132 *pre_init = v ? atoi(v) : 1;
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;
178 if (ptr->type == XML_ELEMENT_NODE
179 && !strcmp((const char *) ptr->name, "cql2rpn"))
181 const char *t = get_text(ptr);
189 int Yaz_ProxyConfig::atoi_l(const char **cp)
192 while (**cp && isdigit(**cp))
194 v = v*10 + (**cp - '0');
200 int Yaz_ProxyConfig::match_list(int v, const char *m)
204 while(*m && isspace(*m))
215 if (v >= l && v <= h)
224 int Yaz_ProxyConfig::check_type_1_attributes(ODR odr, xmlNodePtr ptrl,
225 Z_AttributeList *attrs,
229 for (i = 0; i<attrs->num_attributes; i++)
231 Z_AttributeElement *el = attrs->attributes[i];
233 if (!el->attributeType)
235 int type = *el->attributeType;
238 if (el->which == Z_AttributeValue_numeric && el->value.numeric)
239 value = el->value.numeric;
242 for(ptr = ptrl->children; ptr; ptr = ptr->next)
244 if (ptr->type == XML_ELEMENT_NODE &&
245 !strcmp((const char *) ptr->name, "attribute"))
247 const char *match_type = 0;
248 const char *match_value = 0;
249 const char *match_error = 0;
250 struct _xmlAttr *attr;
251 for (attr = ptr->properties; attr; attr = attr->next)
253 if (!strcmp((const char *) attr->name, "type") &&
254 attr->children && attr->children->type == XML_TEXT_NODE)
255 match_type = (const char *) attr->children->content;
256 if (!strcmp((const char *) attr->name, "value") &&
257 attr->children && attr->children->type == XML_TEXT_NODE)
258 match_value = (const char *) attr->children->content;
259 if (!strcmp((const char *) attr->name, "error") &&
260 attr->children && attr->children->type == XML_TEXT_NODE)
261 match_error = (const char *) attr->children->content;
263 if (match_type && match_value)
265 char addinfo_str[20];
266 if (!match_list(type, match_type))
270 if (!strcmp(match_type, "*"))
271 sprintf (addinfo_str, "%d", type);
274 if (!match_list(*value, match_value))
276 sprintf (addinfo_str, "%d", *value);
284 *addinfo = odr_strdup(odr, addinfo_str);
285 return atoi(match_error);
297 int Yaz_ProxyConfig::check_type_1_structure(ODR odr, xmlNodePtr ptr,
301 if (q->which == Z_RPNStructure_complex)
303 int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
306 e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
309 else if (q->which == Z_RPNStructure_simple)
311 if (q->u.simple->which == Z_Operand_APT)
313 return check_type_1_attributes(
314 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
323 int Yaz_ProxyConfig::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
326 // possibly check for Bib-1
327 return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
331 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
337 ptr = find_target_node(name, 0);
340 if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
341 return check_type_1(odr, ptr, query->u.type_1, addinfo);
348 int Yaz_ProxyConfig::check_esn(xmlNodePtr ptr, Z_RecordComposition *comp)
351 int default_match = 1;
352 if (comp && comp->which == Z_RecordComp_simple &&
353 comp->u.simple && comp->u.simple->which == Z_ElementSetNames_generic)
355 esn = comp->u.simple->u.generic;
359 for (; ptr; ptr = ptr->next)
361 if (ptr->type == XML_TEXT_NODE)
364 xmlChar *t = ptr->content;
367 while (*t && isspace(*t))
371 while (esn[i] && esn[i] == *s)
376 if (!esn[i] && (!*s || isspace(*s)))
378 while (*s && !isspace(*s))
384 return default_match;
388 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
389 Odr_oid *syntax, Z_RecordComposition *comp,
399 int syntax_has_matched = 0;
402 ptr = find_target_node(name, 0);
405 for(ptr = ptr->children; ptr; ptr = ptr->next)
407 if (ptr->type == XML_ELEMENT_NODE &&
408 !strcmp((const char *) ptr->name, "syntax"))
410 int match = 0; // if we match record syntax
411 const char *match_type = 0;
412 const char *match_error = 0;
413 const char *match_marcxml = 0;
414 const char *match_stylesheet = 0;
415 struct _xmlAttr *attr;
416 for (attr = ptr->properties; attr; attr = attr->next)
418 if (!strcmp((const char *) attr->name, "type") &&
419 attr->children && attr->children->type == XML_TEXT_NODE)
420 match_type = (const char *) attr->children->content;
421 if (!strcmp((const char *) attr->name, "error") &&
422 attr->children && attr->children->type == XML_TEXT_NODE)
423 match_error = (const char *) attr->children->content;
424 if (!strcmp((const char *) attr->name, "marcxml") &&
425 attr->children && attr->children->type == XML_TEXT_NODE)
426 match_marcxml = (const char *) attr->children->content;
427 if (!strcmp((const char *) attr->name, "stylesheet") &&
428 attr->children && attr->children->type == XML_TEXT_NODE)
429 match_stylesheet = (const char *) attr->children->content;
433 if (!strcmp(match_type, "*"))
435 else if (!strcmp(match_type, "none"))
442 int match_oid[OID_SIZE];
443 oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
444 if (oid_oidcmp(match_oid, syntax) == 0)
450 syntax_has_matched = 1;
451 match = check_esn(ptr->children, comp);
455 if (stylesheet && match_stylesheet)
458 *stylesheet = xstrdup(match_stylesheet);
466 if (syntax_has_matched) // if syntax did match, schema/ESN was bad
470 char dotoid_str[100];
471 oid_to_dotstring(syntax, dotoid_str);
472 *addinfo = odr_strdup(odr, dotoid_str);
474 return atoi(match_error);
485 xmlNodePtr Yaz_ProxyConfig::find_target_db(xmlNodePtr ptr, const char *db)
492 for (dptr = ptr->children; dptr; dptr = dptr->next)
493 if (dptr->type == XML_ELEMENT_NODE &&
494 !strcmp((const char *) dptr->name, "database"))
496 struct _xmlAttr *attr;
497 for (attr = dptr->properties; attr; attr = attr->next)
498 if (!strcmp((const char *) attr->name, "name"))
501 && attr->children->type==XML_TEXT_NODE
502 && attr->children->content
503 && (!strcmp((const char *) attr->children->content, db)
504 || !strcmp((const char *) attr->children->content,
512 xmlNodePtr Yaz_ProxyConfig::find_target_node(const char *name, const char *db)
517 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
519 if (ptr->type == XML_ELEMENT_NODE &&
520 !strcmp((const char *) ptr->name, "target"))
525 // <target default="1"> ?
526 struct _xmlAttr *attr;
527 for (attr = ptr->properties; attr; attr = attr->next)
528 if (!strcmp((const char *) attr->name, "default") &&
529 attr->children && attr->children->type == XML_TEXT_NODE)
531 xmlChar *t = attr->children->content;
534 return find_target_db(ptr, db);
540 // <target name="name"> ?
541 struct _xmlAttr *attr;
542 for (attr = ptr->properties; attr; attr = attr->next)
543 if (!strcmp((const char *) attr->name, "name"))
546 && attr->children->type==XML_TEXT_NODE
547 && attr->children->content
548 && (!strcmp((const char *) attr->children->content,
550 || !strcmp((const char *) attr->children->content,
553 return find_target_db(ptr, db);
563 int Yaz_ProxyConfig::get_target_no(int no,
569 int *target_idletime,
570 int *client_idletime,
572 int *keepalive_limit_bw,
573 int *keepalive_limit_pdu,
575 const char **cql2rpn)
582 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
583 if (ptr->type == XML_ELEMENT_NODE &&
584 !strcmp((const char *) ptr->name, "target"))
588 struct _xmlAttr *attr;
589 for (attr = ptr->properties; attr; attr = attr->next)
590 if (!strcmp((const char *) attr->name, "name"))
593 && attr->children->type==XML_TEXT_NODE
594 && attr->children->content)
595 *name = (const char *) attr->children->content;
597 return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
598 target_idletime, client_idletime,
599 keepalive_limit_bw, keepalive_limit_pdu,
609 int Yaz_ProxyConfig::mycmp(const char *hay, const char *item, size_t len)
611 if (len == strlen(item) && memcmp(hay, item, len) == 0)
616 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
623 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
625 if (ptr->type == XML_ELEMENT_NODE
626 && !strcmp((const char *) ptr->name, "log"))
628 const char *v = get_text(ptr);
633 while (*cp && *cp != ',' && !isspace(*cp))
636 if (mycmp(v, "client-apdu", len))
637 *log_mask |= PROXY_LOG_APDU_CLIENT;
638 if (mycmp(v, "server-apdu", len))
639 *log_mask |= PROXY_LOG_APDU_SERVER;
640 if (mycmp(v, "client-requests", len))
641 *log_mask |= PROXY_LOG_REQ_CLIENT;
642 if (mycmp(v, "server-requests", len))
643 *log_mask |= PROXY_LOG_REQ_SERVER;
645 *log_mask |= atoi(v);
648 while (*cp && isspace(*cp))
653 if (ptr->type == XML_ELEMENT_NODE &&
654 !strcmp((const char *) ptr->name, "max-clients"))
656 const char *t = get_text(ptr);
659 *max_clients = atoi(t);
660 if (*max_clients < 1)
668 char *Yaz_ProxyConfig::get_explain(ODR odr, const char *name, const char *db,
672 xmlNodePtr ptr = find_target_node(name, db);
676 for (; ptr; ptr = ptr->next)
677 if (ptr->type == XML_ELEMENT_NODE &&
678 !strcmp((const char *) ptr->name, "explain"))
680 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
682 xmlDocPtr doc = xmlNewDoc((const xmlChar *) "1.0");
684 xmlDocSetRootElement(doc, ptr2);
688 xmlDocDumpMemory(doc, &buf_out, len);
689 char *content = (char*) odr_malloc(odr, *len);
690 memcpy(content, buf_out, *len);
698 yaz_log(LOG_WARN, "No explain node 1");
701 yaz_log(LOG_WARN, "No explain node");
705 void Yaz_ProxyConfig::get_target_info(const char *name,
710 int *target_idletime,
711 int *client_idletime,
713 int *keepalive_limit_bw,
714 int *keepalive_limit_pdu,
716 const char **cql2rpn)
727 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
729 if (ptr->type == XML_ELEMENT_NODE &&
730 !strcmp((const char *) ptr->name, "max-clients"))
732 const char *t = get_text(ptr);
735 *max_clients = atoi(t);
736 if (*max_clients < 1)
741 ptr = find_target_node(name, 0);
749 return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
750 target_idletime, client_idletime,
751 keepalive_limit_bw, keepalive_limit_pdu,