2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy-config.cpp,v 1.12 2003-10-20 18:31:44 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,
286 if (q->which == Z_RPNStructure_complex)
288 int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
291 e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
294 else if (q->which == Z_RPNStructure_simple)
296 if (q->u.simple->which == Z_Operand_APT)
298 return check_type_1_attributes(
299 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
308 int Yaz_ProxyConfig::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
311 // possibly check for Bib-1
312 return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
316 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
322 ptr = find_target_node(name);
325 if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
326 return check_type_1(odr, ptr, query->u.type_1, addinfo);
332 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
333 Odr_oid *syntax, char **addinfo)
338 ptr = find_target_node(name);
341 for(ptr = ptr->children; ptr; ptr = ptr->next)
343 if (ptr->type == XML_ELEMENT_NODE &&
344 !strcmp((const char *) ptr->name, "syntax"))
346 int match = 0; // if we match record syntax
347 const char *match_type = 0;
348 const char *match_error = 0;
349 const char *match_marcxml = 0;
350 struct _xmlAttr *attr;
351 for (attr = ptr->properties; attr; attr = attr->next)
353 if (!strcmp((const char *) attr->name, "type") &&
354 attr->children && attr->children->type == XML_TEXT_NODE)
355 match_type = (const char *) attr->children->content;
356 if (!strcmp((const char *) attr->name, "error") &&
357 attr->children && attr->children->type == XML_TEXT_NODE)
358 match_error = (const char *) attr->children->content;
359 if (!strcmp((const char *) attr->name, "marcxml") &&
360 attr->children && attr->children->type == XML_TEXT_NODE)
361 match_marcxml = (const char *) attr->children->content;
365 if (!strcmp(match_type, "*"))
367 else if (!strcmp(match_type, "none"))
374 int match_oid[OID_SIZE];
375 oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
376 if (oid_oidcmp(match_oid, syntax) == 0)
390 char dotoid_str[100];
391 oid_to_dotstring(syntax, dotoid_str);
392 *addinfo = odr_strdup(odr, dotoid_str);
394 return atoi(match_error);
405 xmlNodePtr Yaz_ProxyConfig::find_target_node(const char *name)
410 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
412 if (ptr->type == XML_ELEMENT_NODE &&
413 !strcmp((const char *) ptr->name, "target"))
418 // <target default="1"> ?
419 struct _xmlAttr *attr;
420 for (attr = ptr->properties; attr; attr = attr->next)
421 if (!strcmp((const char *) attr->name, "default") &&
422 attr->children && attr->children->type == XML_TEXT_NODE)
424 xmlChar *t = attr->children->content;
431 // <target name="name"> ?
432 struct _xmlAttr *attr;
433 for (attr = ptr->properties; attr; attr = attr->next)
434 if (!strcmp((const char *) attr->name, "name"))
437 && attr->children->type==XML_TEXT_NODE
438 && attr->children->content
439 && (!strcmp((const char *) attr->children->content,
441 || !strcmp((const char *) attr->children->content,
454 int Yaz_ProxyConfig::get_target_no(int no,
460 int *target_idletime,
461 int *client_idletime,
463 int *keepalive_limit_bw,
464 int *keepalive_limit_pdu,
472 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
473 if (ptr->type == XML_ELEMENT_NODE &&
474 !strcmp((const char *) ptr->name, "target"))
478 struct _xmlAttr *attr;
479 for (attr = ptr->properties; attr; attr = attr->next)
480 if (!strcmp((const char *) attr->name, "name"))
483 && attr->children->type==XML_TEXT_NODE
484 && attr->children->content)
485 *name = (const char *) attr->children->content;
487 return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
488 target_idletime, client_idletime,
489 keepalive_limit_bw, keepalive_limit_pdu,
499 int Yaz_ProxyConfig::mycmp(const char *hay, const char *item, size_t len)
501 if (len == strlen(item) && memcmp(hay, item, len) == 0)
506 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
513 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
515 if (ptr->type == XML_ELEMENT_NODE
516 && !strcmp((const char *) ptr->name, "log"))
518 const char *v = get_text(ptr);
523 while (*cp && *cp != ',' && !isspace(*cp))
526 if (mycmp(v, "client-apdu", len))
527 *log_mask |= PROXY_LOG_APDU_CLIENT;
528 if (mycmp(v, "server-apdu", len))
529 *log_mask |= PROXY_LOG_APDU_SERVER;
530 if (mycmp(v, "client-requests", len))
531 *log_mask |= PROXY_LOG_REQ_CLIENT;
532 if (mycmp(v, "server-requests", len))
533 *log_mask |= PROXY_LOG_REQ_SERVER;
535 *log_mask |= atoi(v);
538 while (*cp && isspace(*cp))
543 if (ptr->type == XML_ELEMENT_NODE &&
544 !strcmp((const char *) ptr->name, "max-clients"))
546 const char *t = get_text(ptr);
549 *max_clients = atoi(t);
550 if (*max_clients < 1)
558 void Yaz_ProxyConfig::get_target_info(const char *name,
563 int *target_idletime,
564 int *client_idletime,
566 int *keepalive_limit_bw,
567 int *keepalive_limit_pdu,
579 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
581 if (ptr->type == XML_ELEMENT_NODE &&
582 !strcmp((const char *) ptr->name, "max-clients"))
584 const char *t = get_text(ptr);
587 *max_clients = atoi(t);
588 if (*max_clients < 1)
593 ptr = find_target_node(name);
601 return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
602 target_idletime, client_idletime,
603 keepalive_limit_bw, keepalive_limit_pdu,