2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy-config.cpp,v 1.20 2004-01-07 11:10:55 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_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
349 const char **found_schema,
350 const char *schema_identifier)
353 int default_match = 1;
354 *found_schema = schema_identifier; // may be NULL
355 if (comp && comp->which == Z_RecordComp_simple &&
356 comp->u.simple && comp->u.simple->which == Z_ElementSetNames_generic)
358 esn = comp->u.simple->u.generic;
360 // if no ESN/schema was given accept..
363 // check if schema identifier match
364 if (schema_identifier && !strcmp(esn, schema_identifier))
367 // Check each name element
368 for (; ptr; ptr = ptr->next)
370 if (ptr->type == XML_ELEMENT_NODE
371 && !strcmp((const char *) ptr->name, "name"))
373 xmlNodePtr tptr = ptr->children;
375 for (; tptr; tptr = tptr->next)
376 if (tptr->type == XML_TEXT_NODE && tptr->content)
378 xmlChar *t = tptr->content;
379 while (*t && isspace(*t))
382 while (esn[i] && esn[i] == t[i])
384 if (!esn[i] && (!t[i] || isspace(t[i])))
389 return default_match;
393 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
394 Odr_oid *syntax, Z_RecordComposition *comp,
396 char **stylesheet, char **schema)
409 int syntax_has_matched = 0;
412 ptr = find_target_node(name, 0);
415 for(ptr = ptr->children; ptr; ptr = ptr->next)
417 if (ptr->type == XML_ELEMENT_NODE &&
418 !strcmp((const char *) ptr->name, "syntax"))
420 int match = 0; // if we match record syntax
421 const char *match_type = 0;
422 const char *match_error = 0;
423 const char *match_marcxml = 0;
424 const char *match_stylesheet = 0;
425 const char *match_identifier = 0;
426 struct _xmlAttr *attr;
427 for (attr = ptr->properties; attr; attr = attr->next)
429 if (!strcmp((const char *) attr->name, "type") &&
430 attr->children && attr->children->type == XML_TEXT_NODE)
431 match_type = (const char *) attr->children->content;
432 if (!strcmp((const char *) attr->name, "error") &&
433 attr->children && attr->children->type == XML_TEXT_NODE)
434 match_error = (const char *) attr->children->content;
435 if (!strcmp((const char *) attr->name, "marcxml") &&
436 attr->children && attr->children->type == XML_TEXT_NODE)
437 match_marcxml = (const char *) attr->children->content;
438 if (!strcmp((const char *) attr->name, "stylesheet") &&
439 attr->children && attr->children->type == XML_TEXT_NODE)
440 match_stylesheet = (const char *) attr->children->content;
441 if (!strcmp((const char *) attr->name, "identifier") &&
442 attr->children && attr->children->type == XML_TEXT_NODE)
443 match_identifier = (const char *) attr->children->content;
447 if (!strcmp(match_type, "*"))
449 else if (!strcmp(match_type, "none"))
456 int match_oid[OID_SIZE];
457 oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
458 if (oid_oidcmp(match_oid, syntax) == 0)
462 const char *match_schema = 0;
465 syntax_has_matched = 1;
466 match = check_schema(ptr->children, comp, &match_schema,
471 if (stylesheet && match_stylesheet)
474 *stylesheet = xstrdup(match_stylesheet);
476 if (schema && match_schema)
478 yaz_log(LOG_LOG, "Match_schema=%s", match_schema);
480 *schema = xstrdup(match_schema);
483 yaz_log(LOG_LOG, "NO SCHEMA");
490 if (syntax_has_matched) // if syntax did match, schema/ESN was bad
494 char dotoid_str[100];
495 oid_to_dotstring(syntax, dotoid_str);
496 *addinfo = odr_strdup(odr, dotoid_str);
498 return atoi(match_error);
509 xmlNodePtr Yaz_ProxyConfig::find_target_db(xmlNodePtr ptr, const char *db)
516 for (dptr = ptr->children; dptr; dptr = dptr->next)
517 if (dptr->type == XML_ELEMENT_NODE &&
518 !strcmp((const char *) dptr->name, "database"))
520 struct _xmlAttr *attr;
521 for (attr = dptr->properties; attr; attr = attr->next)
522 if (!strcmp((const char *) attr->name, "name"))
525 && attr->children->type==XML_TEXT_NODE
526 && attr->children->content
527 && (!strcmp((const char *) attr->children->content, db)
528 || !strcmp((const char *) attr->children->content,
536 xmlNodePtr Yaz_ProxyConfig::find_target_node(const char *name, const char *db)
541 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
543 if (ptr->type == XML_ELEMENT_NODE &&
544 !strcmp((const char *) ptr->name, "target"))
549 // <target default="1"> ?
550 struct _xmlAttr *attr;
551 for (attr = ptr->properties; attr; attr = attr->next)
552 if (!strcmp((const char *) attr->name, "default") &&
553 attr->children && attr->children->type == XML_TEXT_NODE)
555 xmlChar *t = attr->children->content;
558 return find_target_db(ptr, db);
564 // <target name="name"> ?
565 struct _xmlAttr *attr;
566 for (attr = ptr->properties; attr; attr = attr->next)
567 if (!strcmp((const char *) attr->name, "name"))
570 && attr->children->type==XML_TEXT_NODE
571 && attr->children->content
572 && (!strcmp((const char *) attr->children->content,
574 || !strcmp((const char *) attr->children->content,
577 return find_target_db(ptr, db);
587 int Yaz_ProxyConfig::get_target_no(int no,
593 int *target_idletime,
594 int *client_idletime,
596 int *keepalive_limit_bw,
597 int *keepalive_limit_pdu,
599 const char **cql2rpn)
606 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
607 if (ptr->type == XML_ELEMENT_NODE &&
608 !strcmp((const char *) ptr->name, "target"))
612 struct _xmlAttr *attr;
613 for (attr = ptr->properties; attr; attr = attr->next)
614 if (!strcmp((const char *) attr->name, "name"))
617 && attr->children->type==XML_TEXT_NODE
618 && attr->children->content)
619 *name = (const char *) attr->children->content;
621 return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
622 target_idletime, client_idletime,
623 keepalive_limit_bw, keepalive_limit_pdu,
633 int Yaz_ProxyConfig::mycmp(const char *hay, const char *item, size_t len)
635 if (len == strlen(item) && memcmp(hay, item, len) == 0)
640 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
647 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
649 if (ptr->type == XML_ELEMENT_NODE
650 && !strcmp((const char *) ptr->name, "log"))
652 const char *v = get_text(ptr);
657 while (*cp && *cp != ',' && !isspace(*cp))
660 if (mycmp(v, "client-apdu", len))
661 *log_mask |= PROXY_LOG_APDU_CLIENT;
662 if (mycmp(v, "server-apdu", len))
663 *log_mask |= PROXY_LOG_APDU_SERVER;
664 if (mycmp(v, "client-requests", len))
665 *log_mask |= PROXY_LOG_REQ_CLIENT;
666 if (mycmp(v, "server-requests", len))
667 *log_mask |= PROXY_LOG_REQ_SERVER;
669 *log_mask |= atoi(v);
672 while (*cp && isspace(*cp))
677 if (ptr->type == XML_ELEMENT_NODE &&
678 !strcmp((const char *) ptr->name, "max-clients"))
680 const char *t = get_text(ptr);
683 *max_clients = atoi(t);
684 if (*max_clients < 1)
692 char *Yaz_ProxyConfig::get_explain(ODR odr, const char *name, const char *db,
696 xmlNodePtr ptr = find_target_node(name, db);
700 for (; ptr; ptr = ptr->next)
701 if (ptr->type == XML_ELEMENT_NODE &&
702 !strcmp((const char *) ptr->name, "explain"))
704 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
706 xmlDocPtr doc = xmlNewDoc((const xmlChar *) "1.0");
708 xmlDocSetRootElement(doc, ptr2);
712 xmlDocDumpMemory(doc, &buf_out, len);
713 char *content = (char*) odr_malloc(odr, *len);
714 memcpy(content, buf_out, *len);
722 yaz_log(LOG_WARN, "No explain node 1");
725 yaz_log(LOG_WARN, "No explain node");
729 void Yaz_ProxyConfig::get_target_info(const char *name,
734 int *target_idletime,
735 int *client_idletime,
737 int *keepalive_limit_bw,
738 int *keepalive_limit_pdu,
740 const char **cql2rpn)
751 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
753 if (ptr->type == XML_ELEMENT_NODE &&
754 !strcmp((const char *) ptr->name, "max-clients"))
756 const char *t = get_text(ptr);
759 *max_clients = atoi(t);
760 if (*max_clients < 1)
765 ptr = find_target_node(name, 0);
773 return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
774 target_idletime, client_idletime,
775 keepalive_limit_bw, keepalive_limit_pdu,