Use backendtype none instead of finmarc
[yazproxy-moved-to-github.git] / src / yaz-proxy-config.cpp
1 /* $Id: yaz-proxy-config.cpp,v 1.11 2004-12-03 14:28:18 adam Exp $
2    Copyright (c) 1998-2004, Index Data.
3
4 This file is part of the yaz-proxy.
5
6 YAZ proxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with YAZ proxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include <ctype.h>
23 #include <yaz/ylog.h>
24 #include "proxyp.h"
25
26 class Yaz_ProxyConfigP {
27     friend class Yaz_ProxyConfig;
28
29     int m_copy;
30
31     int mycmp(const char *hay, const char *item, size_t len);
32     int match_list(int v, const char *m);
33     int atoi_l(const char **cp);
34 #if HAVE_XSLT
35     int check_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
36                      const char *schema_identifier);
37     xmlDocPtr m_docPtr;
38     xmlNodePtr m_proxyPtr;
39     void return_target_info(xmlNodePtr ptr, const char **url,
40                             int *limit_bw, int *limit_pdu, int *limit_req,
41                             int *target_idletime, int *client_idletime,
42                             int *keepalive_limit_bw, int *keepalive_limit_pdu,
43                             int *pre_init, const char **cql2rpn,
44                             const char **authentication);
45     void return_limit(xmlNodePtr ptr,
46                       int *limit_bw, int *limit_pdu, int *limit_req);
47     int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
48                      char **addinfo);
49     xmlNodePtr find_target_node(const char *name, const char *db);
50     xmlNodePtr find_target_db(xmlNodePtr ptr, const char *db);
51     const char *get_text(xmlNodePtr ptr);
52     int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
53                                 Z_AttributeList *attrs,
54                                 char **addinfo);
55     int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
56                                char **addinfo);
57     int get_explain_ptr(const char *host, const char *db,
58                         xmlNodePtr *ptr_target, xmlNodePtr *ptr_explain);
59 #endif
60 };
61
62 Yaz_ProxyConfig::Yaz_ProxyConfig()
63 {
64     m_cp = new Yaz_ProxyConfigP;
65     m_cp->m_copy = 0;
66 #if HAVE_XSLT
67     m_cp->m_docPtr = 0;
68     m_cp->m_proxyPtr = 0;
69 #endif
70 }
71
72 Yaz_ProxyConfig::~Yaz_ProxyConfig()
73 {
74 #if HAVE_XSLT
75     if (!m_cp->m_copy && m_cp->m_docPtr)
76         xmlFreeDoc(m_cp->m_docPtr);
77 #endif
78     delete m_cp;
79 }
80
81 int Yaz_ProxyConfig::read_xml(const char *fname)
82 {
83 #if HAVE_XSLT
84     xmlDocPtr ndoc = xmlParseFile(fname);
85
86     if (!ndoc)
87     {
88         yaz_log(YLOG_WARN, "Config file %s not found or parse error", fname);
89         return -1;  // no good
90     }
91     int noSubstitutions = xmlXIncludeProcess(ndoc);
92     if (noSubstitutions == -1)
93         yaz_log(YLOG_WARN, "XInclude processing failed on config %s", fname);
94
95     xmlNodePtr proxyPtr = xmlDocGetRootElement(ndoc);
96     if (!proxyPtr || proxyPtr->type != XML_ELEMENT_NODE ||
97         strcmp((const char *) proxyPtr->name, "proxy"))
98     {
99         yaz_log(YLOG_WARN, "No proxy element in %s", fname);
100         xmlFreeDoc(ndoc);
101         return -1;
102     }
103     m_cp->m_proxyPtr = proxyPtr;
104
105     // OK: release previous and make it the current one.
106     if (m_cp->m_docPtr)
107         xmlFreeDoc(m_cp->m_docPtr);
108     m_cp->m_docPtr = ndoc;
109     return 0;
110 #else
111     return -2;
112 #endif
113 }
114
115 #if HAVE_XSLT
116 const char *Yaz_ProxyConfigP::get_text(xmlNodePtr ptr)
117 {
118     for(ptr = ptr->children; ptr; ptr = ptr->next)
119         if (ptr->type == XML_TEXT_NODE)
120         {
121             xmlChar *t = ptr->content;
122             if (t)
123             {
124                 while (*t == ' ')
125                     t++;
126                 return (const char *) t;
127             }
128         }
129     return 0;
130 }
131 #endif
132
133 #if HAVE_XSLT
134 void Yaz_ProxyConfigP::return_limit(xmlNodePtr ptr,
135                                    int *limit_bw,
136                                    int *limit_pdu,
137                                    int *limit_req)
138 {
139     for (ptr = ptr->children; ptr; ptr = ptr->next)
140     {
141         if (ptr->type == XML_ELEMENT_NODE 
142             && !strcmp((const char *) ptr->name, "bandwidth"))
143         {
144             const char *t = get_text(ptr);
145             if (t)
146                 *limit_bw = atoi(t);
147         }
148         if (ptr->type == XML_ELEMENT_NODE 
149             && !strcmp((const char *) ptr->name, "retrieve"))
150         {
151             const char *t = get_text(ptr);
152             if (t)
153                 *limit_req = atoi(t);
154         }
155         if (ptr->type == XML_ELEMENT_NODE 
156             && !strcmp((const char *) ptr->name, "pdu"))
157         {
158             const char *t = get_text(ptr);
159             if (t)
160                 *limit_pdu = atoi(t);
161         }
162     }
163 }
164 #endif
165
166 #if HAVE_XSLT
167 void Yaz_ProxyConfigP::return_target_info(xmlNodePtr ptr,
168                                           const char **url,
169                                           int *limit_bw,
170                                           int *limit_pdu,
171                                           int *limit_req,
172                                           int *target_idletime,
173                                           int *client_idletime,
174                                           int *keepalive_limit_bw,
175                                           int *keepalive_limit_pdu,
176                                           int *pre_init,
177                                           const char **cql2rpn,
178                                           const char **authentication)
179 {
180     *pre_init = 0;
181     int no_url = 0;
182     ptr = ptr->children;
183     for (; ptr; ptr = ptr->next)
184     {
185         if (ptr->type == XML_ELEMENT_NODE 
186             && !strcmp((const char *) ptr->name, "preinit"))
187         {
188             const char *v = get_text(ptr);
189             *pre_init = v ? atoi(v) : 1;
190         }
191         if (ptr->type == XML_ELEMENT_NODE 
192             && !strcmp((const char *) ptr->name, "url"))
193         {
194             const char *t = get_text(ptr);
195             if (t && no_url < MAX_ZURL_PLEX)
196             {
197                 url[no_url++] = t;
198                 url[no_url] = 0;
199             }
200         }
201         if (ptr->type == XML_ELEMENT_NODE 
202             && !strcmp((const char *) ptr->name, "keepalive"))
203         {
204             int dummy;
205             *keepalive_limit_bw = 500000;
206             *keepalive_limit_pdu = 1000;
207             return_limit(ptr, keepalive_limit_bw, keepalive_limit_pdu,
208                          &dummy);
209         }
210         if (ptr->type == XML_ELEMENT_NODE 
211             && !strcmp((const char *) ptr->name, "limit"))
212             return_limit(ptr, limit_bw, limit_pdu, limit_req);
213         if (ptr->type == XML_ELEMENT_NODE 
214             && !strcmp((const char *) ptr->name, "target-timeout"))
215         {
216             const char *t = get_text(ptr);
217             if (t)
218             {
219                 *target_idletime = atoi(t);
220                 if (*target_idletime < 0)
221                     *target_idletime = 0;
222             }
223         }
224         if (ptr->type == XML_ELEMENT_NODE 
225             && !strcmp((const char *) ptr->name, "client-timeout"))
226         {
227             const char *t = get_text(ptr);
228             if (t)
229             {
230                 *client_idletime = atoi(t);
231                 if (*client_idletime < 0)
232                     *client_idletime = 0;
233             }
234         }
235         if (ptr->type == XML_ELEMENT_NODE 
236             && !strcmp((const char *) ptr->name, "cql2rpn"))
237         {
238             const char *t = get_text(ptr);
239             if (t)
240                 *cql2rpn = t;
241         }
242         if (ptr->type == XML_ELEMENT_NODE 
243             && !strcmp((const char *) ptr->name, "authentication"))
244         {
245             const char *t = get_text(ptr);
246             if (t)
247                 *authentication = t;
248         }
249     }
250 }
251 #endif
252
253 int Yaz_ProxyConfigP::atoi_l(const char **cp)
254 {
255     int v = 0;
256     while (**cp && isdigit(**cp))
257     {
258         v = v*10 + (**cp - '0');
259         (*cp)++;
260     }
261     return v;
262 }
263
264 int Yaz_ProxyConfigP::match_list(int v, const char *m)
265 {
266     while(m && *m)
267     {
268         while(*m && isspace(*m))
269             m++;
270         if (*m == '*')
271             return 1;
272         int l = atoi_l(&m);
273         int h = l;
274         if (*m == '-')
275         {
276             ++m;
277             h = atoi_l(&m);
278         }
279         if (v >= l && v <= h)
280           return 1;
281         if (*m == ',')
282             m++;
283     }
284     return 0;
285 }
286
287 #if HAVE_XSLT
288 int Yaz_ProxyConfigP::check_type_1_attributes(ODR odr, xmlNodePtr ptrl,
289                                               Z_AttributeList *attrs,
290                                               char **addinfo)
291 {
292     int i;
293     for (i = 0; i<attrs->num_attributes; i++)
294     {
295         Z_AttributeElement *el = attrs->attributes[i];
296         
297         if (!el->attributeType)
298             continue;
299         int type = *el->attributeType;
300         int *value = 0;
301         
302         if (el->which == Z_AttributeValue_numeric && el->value.numeric)
303             value = el->value.numeric;
304         
305         xmlNodePtr ptr;
306         for(ptr = ptrl->children; ptr; ptr = ptr->next)
307         {
308             if (ptr->type == XML_ELEMENT_NODE &&
309                 !strcmp((const char *) ptr->name, "attribute"))
310             {
311                 const char *match_type = 0;
312                 const char *match_value = 0;
313                 const char *match_error = 0;
314                 struct _xmlAttr *attr;
315                 for (attr = ptr->properties; attr; attr = attr->next)
316                 {
317                     if (!strcmp((const char *) attr->name, "type") &&
318                         attr->children && attr->children->type == XML_TEXT_NODE)
319                         match_type = (const char *) attr->children->content;
320                     if (!strcmp((const char *) attr->name, "value") &&
321                         attr->children && attr->children->type == XML_TEXT_NODE)
322                         match_value = (const char *) attr->children->content;
323                     if (!strcmp((const char *) attr->name, "error") &&
324                         attr->children && attr->children->type == XML_TEXT_NODE)
325                         match_error = (const char *) attr->children->content;
326                 }
327                 if (match_type && match_value)
328                 {
329                     char addinfo_str[20];
330                     if (!match_list(type, match_type))
331                         continue;
332                     
333                     *addinfo_str = '\0';
334                     if (!strcmp(match_type, "*"))
335                         sprintf (addinfo_str, "%d", type);
336                     else if (value)
337                     {
338                         if (!match_list(*value, match_value))
339                             continue;
340                         sprintf (addinfo_str, "%d", *value);
341                     }
342                     else
343                         continue;
344                     
345                     if (match_error)
346                     {
347                         if (*addinfo_str)
348                             *addinfo = odr_strdup(odr, addinfo_str);
349                         return atoi(match_error);
350                     }
351                     break;
352                 }
353             }
354         }
355     }
356     return 0;
357 }
358 #endif
359
360 #if HAVE_XSLT
361 int Yaz_ProxyConfigP::check_type_1_structure(ODR odr, xmlNodePtr ptr,
362                                             Z_RPNStructure *q,
363                                             char **addinfo)
364 {
365     if (q->which == Z_RPNStructure_complex)
366     {
367         int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
368         if (e)
369             return e;
370         e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
371         return e;
372     }
373     else if (q->which == Z_RPNStructure_simple)
374     {
375         if (q->u.simple->which == Z_Operand_APT)
376         {
377             return check_type_1_attributes(
378                 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
379                 addinfo);
380         }
381     }
382     return 0;
383 }
384 #endif
385
386 #if HAVE_XSLT
387 int Yaz_ProxyConfigP::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
388                                    char **addinfo)
389 {
390     // possibly check for Bib-1
391     return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
392 }
393 #endif
394
395 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
396                                  char **addinfo)
397 {
398 #if HAVE_XSLT
399     xmlNodePtr ptr;
400     
401     ptr = m_cp->find_target_node(name, 0);
402     if (ptr)
403     {
404         if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
405             return m_cp->check_type_1(odr, ptr, query->u.type_1, addinfo);
406     }
407 #endif
408     return 0;
409 }
410
411 #if HAVE_XSLT
412 int Yaz_ProxyConfigP::check_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
413                                    const char *schema_identifier)
414 {
415     char *esn = 0;
416     int default_match = 1;
417     if (comp && comp->which == Z_RecordComp_simple &&
418         comp->u.simple && comp->u.simple->which == Z_ElementSetNames_generic)
419     {
420         esn = comp->u.simple->u.generic;
421     }
422     // if no ESN/schema was given accept..
423     if (!esn)
424         return 1;
425     // check if schema identifier match
426     if (schema_identifier && !strcmp(esn, schema_identifier))
427         return 1;
428     // Check each name element
429     for (; ptr; ptr = ptr->next)
430     {
431         if (ptr->type == XML_ELEMENT_NODE 
432             && !strcmp((const char *) ptr->name, "name"))
433         {
434             xmlNodePtr tptr = ptr->children;
435             default_match = 0;
436             for (; tptr; tptr = tptr->next)
437                 if (tptr->type == XML_TEXT_NODE && tptr->content)
438                 {
439                     xmlChar *t = tptr->content;
440                     while (*t && isspace(*t))
441                         t++;
442                     int i = 0;
443                     while (esn[i] && esn[i] == t[i])
444                         i++;
445                     if (!esn[i] && (!t[i] || isspace(t[i])))
446                         return 1;
447                 }
448         }
449     }
450     return default_match;
451 }
452 #endif
453
454 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
455                                   Odr_oid *syntax, Z_RecordComposition *comp,
456                                   char **addinfo,
457                                   char **stylesheet, char **schema,
458                                   char **backend_type,
459                                   char **backend_charset,
460                                   char **usemarcon_ini_stage1,
461                                   char **usemarcon_ini_stage2
462                                   )
463 {
464     if (stylesheet)
465     {
466         xfree (*stylesheet);
467         *stylesheet = 0;
468     }
469     if (schema)
470     {
471         xfree (*schema);
472         *schema = 0;
473     }
474     if (backend_type)
475     {
476         xfree (*backend_type);
477         *backend_type = 0;
478     }
479     if (backend_charset)
480     {
481         xfree (*backend_charset);
482         *backend_charset = 0;
483     }
484     if (usemarcon_ini_stage1)
485     {
486         xfree (*usemarcon_ini_stage1);
487         *usemarcon_ini_stage1 = 0;
488     }
489     if (usemarcon_ini_stage2)
490     {
491         xfree (*usemarcon_ini_stage2);
492         *usemarcon_ini_stage2 = 0;
493     }
494 #if HAVE_XSLT
495     int syntax_has_matched = 0;
496     xmlNodePtr ptr;
497     
498     ptr = m_cp->find_target_node(name, 0);
499     if (!ptr)
500         return 0;
501     for(ptr = ptr->children; ptr; ptr = ptr->next)
502     {
503         if (ptr->type == XML_ELEMENT_NODE &&
504             !strcmp((const char *) ptr->name, "syntax"))
505         {
506             int match = 0;  // if we match record syntax
507             const char *match_type = 0;
508             const char *match_error = 0;
509             const char *match_marcxml = 0;
510             const char *match_stylesheet = 0;
511             const char *match_identifier = 0;
512             const char *match_backend_type = 0;
513             const char *match_backend_charset = 0;
514             const char *match_usemarcon_ini_stage1 = 0;
515             const char *match_usemarcon_ini_stage2 = 0;
516             struct _xmlAttr *attr;
517             for (attr = ptr->properties; attr; attr = attr->next)
518             {
519                 if (!strcmp((const char *) attr->name, "type") &&
520                     attr->children && attr->children->type == XML_TEXT_NODE)
521                     match_type = (const char *) attr->children->content;
522                 if (!strcmp((const char *) attr->name, "error") &&
523                     attr->children && attr->children->type == XML_TEXT_NODE)
524                     match_error = (const char *) attr->children->content;
525                 if (!strcmp((const char *) attr->name, "marcxml") &&
526                     attr->children && attr->children->type == XML_TEXT_NODE)
527                     match_marcxml = (const char *) attr->children->content;
528                 if (!strcmp((const char *) attr->name, "stylesheet") &&
529                     attr->children && attr->children->type == XML_TEXT_NODE)
530                     match_stylesheet = (const char *) attr->children->content;
531                 if (!strcmp((const char *) attr->name, "identifier") &&
532                     attr->children && attr->children->type == XML_TEXT_NODE)
533                     match_identifier = (const char *) attr->children->content;
534                 if (!strcmp((const char *) attr->name, "backendtype") &&
535                     attr->children && attr->children->type == XML_TEXT_NODE)
536                     match_backend_type = (const char *)
537                         attr->children->content;
538                 if (!strcmp((const char *) attr->name, "backendcharset") &&
539                     attr->children && attr->children->type == XML_TEXT_NODE)
540                     match_backend_charset = (const char *)
541                         attr->children->content;
542                 if (!strcmp((const char *) attr->name, "usemarconstage1") &&
543                     attr->children && attr->children->type == XML_TEXT_NODE)
544                     match_usemarcon_ini_stage1 = (const char *)
545                         attr->children->content;
546                 if (!strcmp((const char *) attr->name, "usemarconstage2") &&
547                     attr->children && attr->children->type == XML_TEXT_NODE)
548                     match_usemarcon_ini_stage2 = (const char *)
549                         attr->children->content;
550             }
551             if (match_type)
552             {
553                 if (!strcmp(match_type, "*"))
554                     match = 1;
555                 else if (!strcmp(match_type, "none"))
556                 {
557                     if (syntax == 0)
558                         match = 1;
559                 }
560                 else if (syntax)
561                 {
562                     int match_oid[OID_SIZE];
563                     oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
564                     if (oid_oidcmp(match_oid, syntax) == 0)
565                         match = 1;
566                 }
567             }
568             if (match)
569             {
570                 if (!match_error)
571                     syntax_has_matched = 1;
572                 match = m_cp->check_schema(ptr->children, comp,
573                                            match_identifier);
574             }
575             if (match)
576             {
577                 if (stylesheet && match_stylesheet)
578                 {
579                     xfree(*stylesheet);
580                     *stylesheet = xstrdup(match_stylesheet);
581                 }
582                 if (schema && match_identifier)
583                 {
584                     xfree(*schema);
585                     *schema = xstrdup(match_identifier);
586                 }
587                 if (backend_type && match_backend_type)
588                 {
589                     xfree(*backend_type);
590                     *backend_type = xstrdup(match_backend_type);
591                 }
592                 if (backend_charset && match_backend_charset)
593                 {
594                     xfree(*backend_charset);
595                     *backend_charset = xstrdup(match_backend_charset);
596                 }
597                 if (usemarcon_ini_stage1 && match_usemarcon_ini_stage1)
598                 {
599                     xfree(*usemarcon_ini_stage1);
600                     *usemarcon_ini_stage1 = xstrdup(match_usemarcon_ini_stage1);
601                 }
602                 if (usemarcon_ini_stage1 && match_usemarcon_ini_stage2)
603                 {
604                     xfree(*usemarcon_ini_stage2);
605                     *usemarcon_ini_stage2 = xstrdup(match_usemarcon_ini_stage2);
606                 }
607                 if (match_marcxml)
608                 {
609                     return -1;
610                 }
611                 if (match_error)
612                 {
613                     if (syntax_has_matched)  // if syntax OK, bad schema/ESN
614                         return 25;
615                     if (syntax)
616                     {
617                         char dotoid_str[100];
618                         oid_to_dotstring(syntax, dotoid_str);
619                         *addinfo = odr_strdup(odr, dotoid_str);
620                     }
621                     return atoi(match_error);
622                 }
623                 return 0;
624             }
625         }
626     }
627 #endif
628     return 0;
629 }
630
631 #if HAVE_XSLT
632 xmlNodePtr Yaz_ProxyConfigP::find_target_db(xmlNodePtr ptr, const char *db)
633 {
634     xmlNodePtr dptr;
635     if (!db)
636         return ptr;
637     if (!ptr)
638         return 0;
639     for (dptr = ptr->children; dptr; dptr = dptr->next)
640         if (dptr->type == XML_ELEMENT_NODE &&
641             !strcmp((const char *) dptr->name, "database"))
642         {
643             struct _xmlAttr *attr;
644             for (attr = dptr->properties; attr; attr = attr->next)
645                 if (!strcmp((const char *) attr->name, "name"))
646                 {
647                     if (attr->children
648                         && attr->children->type==XML_TEXT_NODE
649                         && attr->children->content 
650                         && (!strcmp((const char *) attr->children->content, db)
651                             || !strcmp((const char *) attr->children->content,
652                                        "*")))
653                         return dptr;
654                 }
655         }
656     return ptr;
657 }
658     
659 xmlNodePtr Yaz_ProxyConfigP::find_target_node(const char *name, const char *db)
660 {
661     xmlNodePtr ptr;
662     if (!m_proxyPtr)
663         return 0;
664     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
665     {
666         if (ptr->type == XML_ELEMENT_NODE &&
667             !strcmp((const char *) ptr->name, "target"))
668         {
669             // default one ? 
670             if (!name)
671             {
672                 // <target default="1"> ?
673                 struct _xmlAttr *attr;
674                 for (attr = ptr->properties; attr; attr = attr->next)
675                     if (!strcmp((const char *) attr->name, "default") &&
676                         attr->children && attr->children->type == XML_TEXT_NODE)
677                     {
678                         xmlChar *t = attr->children->content;
679                         if (!t || *t == '1')
680                         {
681                             return find_target_db(ptr, db);
682                         }
683                     }
684             }
685             else
686             {
687                 // <target name="name"> ?
688                 struct _xmlAttr *attr;
689                 for (attr = ptr->properties; attr; attr = attr->next)
690                     if (!strcmp((const char *) attr->name, "name"))
691                     {
692                         if (attr->children
693                             && attr->children->type==XML_TEXT_NODE
694                             && attr->children->content 
695                             && (!strcmp((const char *) attr->children->content,
696                                         name)
697                                 || !strcmp((const char *) attr->children->content,
698                                            "*")))
699                         {
700                             return find_target_db(ptr, db);
701                         }
702                     }
703             }
704         }
705     }
706     return 0;
707 }
708 #endif
709
710 int Yaz_ProxyConfig::get_target_no(int no,
711                                    const char **name,
712                                    const char **url,
713                                    int *limit_bw,
714                                    int *limit_pdu,
715                                    int *limit_req,
716                                    int *target_idletime,
717                                    int *client_idletime,
718                                    int *max_clients,
719                                    int *keepalive_limit_bw,
720                                    int *keepalive_limit_pdu,
721                                    int *pre_init,
722                                    const char **cql2rpn,
723                                    const char **authentication)
724 {
725 #if HAVE_XSLT
726     xmlNodePtr ptr;
727     if (!m_cp->m_proxyPtr)
728         return 0;
729     int i = 0;
730     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
731         if (ptr->type == XML_ELEMENT_NODE &&
732             !strcmp((const char *) ptr->name, "target"))
733         {
734             if (i == no)
735             {
736                 struct _xmlAttr *attr;
737                 for (attr = ptr->properties; attr; attr = attr->next)
738                     if (!strcmp((const char *) attr->name, "name"))
739                     {
740                         if (attr->children
741                             && attr->children->type==XML_TEXT_NODE
742                             && attr->children->content)
743                             *name = (const char *) attr->children->content;
744                     }
745                 m_cp->return_target_info(
746                     ptr, url,
747                     limit_bw, limit_pdu, limit_req,
748                     target_idletime, client_idletime,
749                     keepalive_limit_bw, keepalive_limit_pdu,
750                     pre_init, cql2rpn, authentication);
751                 return 1;
752             }
753             i++;
754         }
755 #endif
756     return 0;
757 }
758
759 int Yaz_ProxyConfigP::mycmp(const char *hay, const char *item, size_t len)
760 {
761     if (len == strlen(item) && memcmp(hay, item, len) == 0)
762         return 1;
763     return 0;
764 }
765
766 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
767                                        int *max_clients)
768 {
769 #if HAVE_XSLT
770     xmlNodePtr ptr;
771     if (!m_cp->m_proxyPtr)
772         return;
773     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
774     {
775         if (ptr->type == XML_ELEMENT_NODE 
776             && !strcmp((const char *) ptr->name, "log"))
777         {
778             const char *v = m_cp->get_text(ptr);
779             *log_mask = 0;
780             while (v && *v)
781             {
782                 const char *cp = v;
783                 while (*cp && *cp != ',' && !isspace(*cp))
784                     cp++;
785                 size_t len = cp - v;
786                 if (m_cp->mycmp(v, "client-apdu", len))
787                     *log_mask |= PROXY_LOG_APDU_CLIENT;
788                 if (m_cp->mycmp(v, "server-apdu", len))
789                     *log_mask |= PROXY_LOG_APDU_SERVER;
790                 if (m_cp->mycmp(v, "client-requests", len))
791                     *log_mask |= PROXY_LOG_REQ_CLIENT;
792                 if (m_cp->mycmp(v, "server-requests", len))
793                     *log_mask |= PROXY_LOG_REQ_SERVER;
794                 if (isdigit(*v))
795                     *log_mask |= atoi(v);
796                 if (*cp == ',')
797                     cp++;
798                 while (*cp && isspace(*cp))
799                     cp++;
800                 v = cp;
801             }
802         }
803         if (ptr->type == XML_ELEMENT_NODE &&
804             !strcmp((const char *) ptr->name, "max-clients"))
805         {
806             const char *t = m_cp->get_text(ptr);
807             if (t)
808             {
809                 *max_clients = atoi(t);
810                 if (*max_clients  < 1)
811                     *max_clients = 1;
812             }
813         }
814     }
815 #endif
816 }
817
818 #if HAVE_XSLT
819 int Yaz_ProxyConfigP::get_explain_ptr(const char *host, const char *db,
820                                       xmlNodePtr *ptr_target,
821                                       xmlNodePtr *ptr_explain)
822 {
823     xmlNodePtr ptr;
824     if (!m_proxyPtr)
825         return 0;
826     if (!db)
827         return 0;
828     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
829     {
830         if (ptr->type == XML_ELEMENT_NODE &&
831             !strcmp((const char *) ptr->name, "target"))
832         {
833             *ptr_target = ptr;
834             xmlNodePtr ptr = (*ptr_target)->children;
835             for (; ptr; ptr = ptr->next)
836             {
837                 if (ptr->type == XML_ELEMENT_NODE &&
838                     !strcmp((const char *) ptr->name, "explain"))
839                 {
840                     *ptr_explain = ptr;
841                     xmlNodePtr ptr = (*ptr_explain)->children;
842
843                     for (; ptr; ptr = ptr->next)
844                         if (ptr->type == XML_ELEMENT_NODE &&
845                             !strcmp((const char *) ptr->name, "serverInfo"))
846                             break;
847                     if (!ptr)
848                         continue;
849                     for (ptr = ptr->children; ptr; ptr = ptr->next)
850                         if (ptr->type == XML_ELEMENT_NODE &&
851                             !strcmp((const char *) ptr->name, "database"))
852                             break;
853                     
854                     if (!ptr)
855                         continue;
856                     for (ptr = ptr->children; ptr; ptr = ptr->next)
857                         if (ptr->type == XML_TEXT_NODE &&
858                             ptr->content &&
859                             !strcmp((const char *) ptr->content, db))
860                             break;
861                     if (!ptr)
862                         continue;
863                     return 1;
864                 }
865             }
866         }
867     }
868     return 0;
869 }
870 #endif
871
872 const char *Yaz_ProxyConfig::get_explain_name(const char *db,
873                                               const char **backend_db)
874 {
875 #if HAVE_XSLT
876     xmlNodePtr ptr_target, ptr_explain;
877     if (m_cp->get_explain_ptr(0, db, &ptr_target, &ptr_explain)
878         && ptr_target)
879     {
880         struct _xmlAttr *attr;
881         const char *name = 0;
882         
883         for (attr = ptr_target->properties; attr; attr = attr->next)
884             if (!strcmp((const char *) attr->name, "name")
885                 && attr->children
886                 && attr->children->type==XML_TEXT_NODE
887                 && attr->children->content 
888                 && attr->children->content[0])
889             {
890                 name = (const char *)attr->children->content;
891                 break;
892             }
893         if (name)
894         {
895             for (attr = ptr_target->properties; attr; attr = attr->next)
896                 if (!strcmp((const char *) attr->name, "database"))
897                 {
898                     if (attr->children
899                         && attr->children->type==XML_TEXT_NODE
900                         && attr->children->content)
901                         *backend_db = (const char *) attr->children->content;
902                 }
903             return name;
904         }
905     }
906 #endif
907     return 0;
908 }
909
910 char *Yaz_ProxyConfig::get_explain_doc(ODR odr, const char *name,
911                                        const char *db, int *len)
912 {
913 #if HAVE_XSLT
914     xmlNodePtr ptr_target, ptr_explain;
915     if (m_cp->get_explain_ptr(0 /* host */, db, &ptr_target, &ptr_explain))
916     {
917         xmlNodePtr ptr2 = xmlCopyNode(ptr_explain, 1);
918         
919         xmlDocPtr doc = xmlNewDoc((const xmlChar *) "1.0");
920         
921         xmlDocSetRootElement(doc, ptr2);
922         
923         xmlChar *buf_out;
924         xmlDocDumpMemory(doc, &buf_out, len);
925         char *content = (char*) odr_malloc(odr, *len);
926         memcpy(content, buf_out, *len);
927         
928         xmlFree(buf_out);
929         xmlFreeDoc(doc);
930         return content;
931     }
932 #endif
933     return 0;
934 }
935
936 void Yaz_ProxyConfig::get_target_info(const char *name,
937                                       const char **url,
938                                       int *limit_bw,
939                                       int *limit_pdu,
940                                       int *limit_req,
941                                       int *target_idletime,
942                                       int *client_idletime,
943                                       int *max_clients,
944                                       int *keepalive_limit_bw,
945                                       int *keepalive_limit_pdu,
946                                       int *pre_init,
947                                       const char **cql2rpn,
948                                       const char **authentication)
949 {
950 #if HAVE_XSLT
951     xmlNodePtr ptr;
952     if (!m_cp->m_proxyPtr)
953     {
954         url[0] = name;
955         url[1] = 0;
956         return;
957     }
958     url[0] = 0;
959     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
960     {
961         if (ptr->type == XML_ELEMENT_NODE &&
962             !strcmp((const char *) ptr->name, "max-clients"))
963         {
964             const char *t = m_cp->get_text(ptr);
965             if (t)
966             {
967                 *max_clients = atoi(t);
968                 if (*max_clients  < 1)
969                     *max_clients = 1;
970             }
971         }
972     }
973     ptr = m_cp->find_target_node(name, 0);
974     if (ptr)
975     {
976         if (name)
977         {
978             url[0] = name;
979             url[1] = 0;
980         }
981         m_cp->return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
982                                  target_idletime, client_idletime,
983                                  keepalive_limit_bw, keepalive_limit_pdu,
984                                  pre_init, cql2rpn, authentication);
985     }
986 #else
987     *url = name;
988     return;
989 #endif
990 }
991
992