New attribute "backendcharset" for syntax section which specifies
[yazproxy-moved-to-github.git] / src / yaz-proxy-config.cpp
1 /* $Id: yaz-proxy-config.cpp,v 1.6 2004-08-29 13:01:43 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/log.h>
24 #include <yazproxy/proxy.h>
25
26 #if HAVE_XSLT
27 #include <libxml/parser.h>
28 #include <libxml/tree.h>
29 #include <libxslt/xsltutils.h>
30 #include <libxslt/transform.h>
31 #endif
32
33 class Yaz_ProxyConfigP {
34     friend class Yaz_ProxyConfig;
35
36     int m_copy;
37
38     int mycmp(const char *hay, const char *item, size_t len);
39     int match_list(int v, const char *m);
40     int atoi_l(const char **cp);
41 #if HAVE_XSLT
42     int check_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
43                      const char *schema_identifier);
44     xmlDocPtr m_docPtr;
45     xmlNodePtr m_proxyPtr;
46     void return_target_info(xmlNodePtr ptr, const char **url,
47                             int *limit_bw, int *limit_pdu, int *limit_req,
48                             int *target_idletime, int *client_idletime,
49                             int *keepalive_limit_bw, int *keepalive_limit_pdu,
50                             int *pre_init, const char **cql2rpn);
51     void return_limit(xmlNodePtr ptr,
52                       int *limit_bw, int *limit_pdu, int *limit_req);
53     int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
54                      char **addinfo);
55     xmlNodePtr find_target_node(const char *name, const char *db);
56     xmlNodePtr find_target_db(xmlNodePtr ptr, const char *db);
57     const char *get_text(xmlNodePtr ptr);
58     int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
59                                 Z_AttributeList *attrs,
60                                 char **addinfo);
61     int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
62                                char **addinfo);
63 #endif
64 };
65
66 Yaz_ProxyConfig::Yaz_ProxyConfig()
67 {
68     m_cp = new Yaz_ProxyConfigP;
69     m_cp->m_copy = 0;
70 #if HAVE_XSLT
71     m_cp->m_docPtr = 0;
72     m_cp->m_proxyPtr = 0;
73 #endif
74 }
75
76 Yaz_ProxyConfig::~Yaz_ProxyConfig()
77 {
78 #if HAVE_XSLT
79     if (!m_cp->m_copy && m_cp->m_docPtr)
80         xmlFreeDoc(m_cp->m_docPtr);
81 #endif
82     delete m_cp;
83 }
84
85 int Yaz_ProxyConfig::read_xml(const char *fname)
86 {
87 #if HAVE_XSLT
88     xmlDocPtr ndoc = xmlParseFile(fname);
89
90     if (!ndoc)
91     {
92         yaz_log(LOG_WARN, "Config file %s not found or parse error", fname);
93         return -1;  // no good
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(LOG_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 {
179     *pre_init = 0;
180     int no_url = 0;
181     ptr = ptr->children;
182     for (; ptr; ptr = ptr->next)
183     {
184         if (ptr->type == XML_ELEMENT_NODE 
185             && !strcmp((const char *) ptr->name, "preinit"))
186         {
187             const char *v = get_text(ptr);
188             *pre_init = v ? atoi(v) : 1;
189         }
190         if (ptr->type == XML_ELEMENT_NODE 
191             && !strcmp((const char *) ptr->name, "url"))
192         {
193             const char *t = get_text(ptr);
194             if (t && no_url < MAX_ZURL_PLEX)
195             {
196                 url[no_url++] = t;
197                 url[no_url] = 0;
198             }
199         }
200         if (ptr->type == XML_ELEMENT_NODE 
201             && !strcmp((const char *) ptr->name, "keepalive"))
202         {
203             int dummy;
204             *keepalive_limit_bw = 500000;
205             *keepalive_limit_pdu = 1000;
206             return_limit(ptr, keepalive_limit_bw, keepalive_limit_pdu,
207                          &dummy);
208         }
209         if (ptr->type == XML_ELEMENT_NODE 
210             && !strcmp((const char *) ptr->name, "limit"))
211             return_limit(ptr, limit_bw, limit_pdu, limit_req);
212         if (ptr->type == XML_ELEMENT_NODE 
213             && !strcmp((const char *) ptr->name, "target-timeout"))
214         {
215             const char *t = get_text(ptr);
216             if (t)
217             {
218                 *target_idletime = atoi(t);
219                 if (*target_idletime < 0)
220                     *target_idletime = 0;
221             }
222         }
223         if (ptr->type == XML_ELEMENT_NODE 
224             && !strcmp((const char *) ptr->name, "client-timeout"))
225         {
226             const char *t = get_text(ptr);
227             if (t)
228             {
229                 *client_idletime = atoi(t);
230                 if (*client_idletime < 0)
231                     *client_idletime = 0;
232             }
233         }
234         if (ptr->type == XML_ELEMENT_NODE 
235             && !strcmp((const char *) ptr->name, "cql2rpn"))
236         {
237             const char *t = get_text(ptr);
238             if (t)
239                 *cql2rpn = t;
240         }
241     }
242 }
243 #endif
244
245 int Yaz_ProxyConfigP::atoi_l(const char **cp)
246 {
247     int v = 0;
248     while (**cp && isdigit(**cp))
249     {
250         v = v*10 + (**cp - '0');
251         (*cp)++;
252     }
253     return v;
254 }
255
256 int Yaz_ProxyConfigP::match_list(int v, const char *m)
257 {
258     while(m && *m)
259     {
260         while(*m && isspace(*m))
261             m++;
262         if (*m == '*')
263             return 1;
264         int l = atoi_l(&m);
265         int h = l;
266         if (*m == '-')
267         {
268             ++m;
269             h = atoi_l(&m);
270         }
271         if (v >= l && v <= h)
272           return 1;
273         if (*m == ',')
274             m++;
275     }
276     return 0;
277 }
278
279 #if HAVE_XSLT
280 int Yaz_ProxyConfigP::check_type_1_attributes(ODR odr, xmlNodePtr ptrl,
281                                               Z_AttributeList *attrs,
282                                               char **addinfo)
283 {
284     int i;
285     for (i = 0; i<attrs->num_attributes; i++)
286     {
287         Z_AttributeElement *el = attrs->attributes[i];
288         
289         if (!el->attributeType)
290             continue;
291         int type = *el->attributeType;
292         int *value = 0;
293         
294         if (el->which == Z_AttributeValue_numeric && el->value.numeric)
295             value = el->value.numeric;
296         
297         xmlNodePtr ptr;
298         for(ptr = ptrl->children; ptr; ptr = ptr->next)
299         {
300             if (ptr->type == XML_ELEMENT_NODE &&
301                 !strcmp((const char *) ptr->name, "attribute"))
302             {
303                 const char *match_type = 0;
304                 const char *match_value = 0;
305                 const char *match_error = 0;
306                 struct _xmlAttr *attr;
307                 for (attr = ptr->properties; attr; attr = attr->next)
308                 {
309                     if (!strcmp((const char *) attr->name, "type") &&
310                         attr->children && attr->children->type == XML_TEXT_NODE)
311                         match_type = (const char *) attr->children->content;
312                     if (!strcmp((const char *) attr->name, "value") &&
313                         attr->children && attr->children->type == XML_TEXT_NODE)
314                         match_value = (const char *) attr->children->content;
315                     if (!strcmp((const char *) attr->name, "error") &&
316                         attr->children && attr->children->type == XML_TEXT_NODE)
317                         match_error = (const char *) attr->children->content;
318                 }
319                 if (match_type && match_value)
320                 {
321                     char addinfo_str[20];
322                     if (!match_list(type, match_type))
323                         continue;
324                     
325                     *addinfo_str = '\0';
326                     if (!strcmp(match_type, "*"))
327                         sprintf (addinfo_str, "%d", type);
328                     else if (value)
329                     {
330                         if (!match_list(*value, match_value))
331                             continue;
332                         sprintf (addinfo_str, "%d", *value);
333                     }
334                     else
335                         continue;
336                     
337                     if (match_error)
338                     {
339                         if (*addinfo_str)
340                             *addinfo = odr_strdup(odr, addinfo_str);
341                         return atoi(match_error);
342                     }
343                     break;
344                 }
345             }
346         }
347     }
348     return 0;
349 }
350 #endif
351
352 #if HAVE_XSLT
353 int Yaz_ProxyConfigP::check_type_1_structure(ODR odr, xmlNodePtr ptr,
354                                             Z_RPNStructure *q,
355                                             char **addinfo)
356 {
357     if (q->which == Z_RPNStructure_complex)
358     {
359         int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
360         if (e)
361             return e;
362         e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
363         return e;
364     }
365     else if (q->which == Z_RPNStructure_simple)
366     {
367         if (q->u.simple->which == Z_Operand_APT)
368         {
369             return check_type_1_attributes(
370                 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
371                 addinfo);
372         }
373     }
374     return 0;
375 }
376 #endif
377
378 #if HAVE_XSLT
379 int Yaz_ProxyConfigP::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
380                                    char **addinfo)
381 {
382     // possibly check for Bib-1
383     return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
384 }
385 #endif
386
387 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
388                                  char **addinfo)
389 {
390 #if HAVE_XSLT
391     xmlNodePtr ptr;
392     
393     ptr = m_cp->find_target_node(name, 0);
394     if (ptr)
395     {
396         if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
397             return m_cp->check_type_1(odr, ptr, query->u.type_1, addinfo);
398     }
399 #endif
400     return 0;
401 }
402
403 #if HAVE_XSLT
404 int Yaz_ProxyConfigP::check_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
405                                    const char *schema_identifier)
406 {
407     char *esn = 0;
408     int default_match = 1;
409     if (comp && comp->which == Z_RecordComp_simple &&
410         comp->u.simple && comp->u.simple->which == Z_ElementSetNames_generic)
411     {
412         esn = comp->u.simple->u.generic;
413     }
414     // if no ESN/schema was given accept..
415     if (!esn)
416         return 1;
417     // check if schema identifier match
418     if (schema_identifier && !strcmp(esn, schema_identifier))
419         return 1;
420     // Check each name element
421     for (; ptr; ptr = ptr->next)
422     {
423         if (ptr->type == XML_ELEMENT_NODE 
424             && !strcmp((const char *) ptr->name, "name"))
425         {
426             xmlNodePtr tptr = ptr->children;
427             default_match = 0;
428             for (; tptr; tptr = tptr->next)
429                 if (tptr->type == XML_TEXT_NODE && tptr->content)
430                 {
431                     xmlChar *t = tptr->content;
432                     while (*t && isspace(*t))
433                         t++;
434                     int i = 0;
435                     while (esn[i] && esn[i] == t[i])
436                         i++;
437                     if (!esn[i] && (!t[i] || isspace(t[i])))
438                         return 1;
439                 }
440         }
441     }
442     return default_match;
443 }
444 #endif
445
446 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
447                                   Odr_oid *syntax, Z_RecordComposition *comp,
448                                   char **addinfo,
449                                   char **stylesheet, char **schema,
450                                   char **backend_type,
451                                   char **backend_charset)
452 {
453     if (stylesheet)
454     {
455         xfree (*stylesheet);
456         *stylesheet = 0;
457     }
458     if (schema)
459     {
460         xfree (*schema);
461         *schema = 0;
462     }
463     if (backend_type)
464     {
465         xfree (*backend_type);
466         *backend_type = 0;
467     }
468     if (backend_charset)
469     {
470         xfree (*backend_charset);
471         *backend_charset = 0;
472     }
473 #if HAVE_XSLT
474     int syntax_has_matched = 0;
475     xmlNodePtr ptr;
476     
477     ptr = m_cp->find_target_node(name, 0);
478     if (!ptr)
479         return 0;
480     for(ptr = ptr->children; ptr; ptr = ptr->next)
481     {
482         if (ptr->type == XML_ELEMENT_NODE &&
483             !strcmp((const char *) ptr->name, "syntax"))
484         {
485             int match = 0;  // if we match record syntax
486             const char *match_type = 0;
487             const char *match_error = 0;
488             const char *match_marcxml = 0;
489             const char *match_stylesheet = 0;
490             const char *match_identifier = 0;
491             const char *match_backend_type = 0;
492             const char *match_backend_charset = 0;
493             struct _xmlAttr *attr;
494             for (attr = ptr->properties; attr; attr = attr->next)
495             {
496                 if (!strcmp((const char *) attr->name, "type") &&
497                     attr->children && attr->children->type == XML_TEXT_NODE)
498                     match_type = (const char *) attr->children->content;
499                 if (!strcmp((const char *) attr->name, "error") &&
500                     attr->children && attr->children->type == XML_TEXT_NODE)
501                     match_error = (const char *) attr->children->content;
502                 if (!strcmp((const char *) attr->name, "marcxml") &&
503                     attr->children && attr->children->type == XML_TEXT_NODE)
504                     match_marcxml = (const char *) attr->children->content;
505                 if (!strcmp((const char *) attr->name, "stylesheet") &&
506                     attr->children && attr->children->type == XML_TEXT_NODE)
507                     match_stylesheet = (const char *) attr->children->content;
508                 if (!strcmp((const char *) attr->name, "identifier") &&
509                     attr->children && attr->children->type == XML_TEXT_NODE)
510                     match_identifier = (const char *) attr->children->content;
511                 if (!strcmp((const char *) attr->name, "backendtype") &&
512                     attr->children && attr->children->type == XML_TEXT_NODE)
513                     match_backend_type = (const char *)
514                         attr->children->content;
515                 if (!strcmp((const char *) attr->name, "backendcharset") &&
516                     attr->children && attr->children->type == XML_TEXT_NODE)
517                     match_backend_charset = (const char *)
518                         attr->children->content;
519             }
520             if (match_type)
521             {
522                 if (!strcmp(match_type, "*"))
523                     match = 1;
524                 else if (!strcmp(match_type, "none"))
525                 {
526                     if (syntax == 0)
527                         match = 1;
528                 }
529                 else if (syntax)
530                 {
531                     int match_oid[OID_SIZE];
532                     oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
533                     if (oid_oidcmp(match_oid, syntax) == 0)
534                         match = 1;
535                 }
536             }
537             if (match)
538             {
539                 if (!match_error)
540                     syntax_has_matched = 1;
541                 match = m_cp->check_schema(ptr->children, comp,
542                                            match_identifier);
543             }
544             if (match)
545             {
546                 if (stylesheet && match_stylesheet)
547                 {
548                     xfree(*stylesheet);
549                     *stylesheet = xstrdup(match_stylesheet);
550                 }
551                 if (schema && match_identifier)
552                 {
553                     xfree(*schema);
554                     *schema = xstrdup(match_identifier);
555                 }
556                 if (backend_type && match_backend_type)
557                 {
558                     xfree(*backend_type);
559                     *backend_type = xstrdup(match_backend_type);
560                 }
561                 if (backend_charset && match_backend_charset)
562                 {
563                     xfree(*backend_charset);
564                     *backend_charset = xstrdup(match_backend_charset);
565                 }
566                 if (match_marcxml)
567                 {
568                     return -1;
569                 }
570                 if (match_error)
571                 {
572                     if (syntax_has_matched)  // if syntax OK, bad schema/ESN
573                         return 25;
574                     if (syntax)
575                     {
576                         char dotoid_str[100];
577                         oid_to_dotstring(syntax, dotoid_str);
578                         *addinfo = odr_strdup(odr, dotoid_str);
579                     }
580                     return atoi(match_error);
581                 }
582                 return 0;
583             }
584         }
585     }
586 #endif
587     return 0;
588 }
589
590 #if HAVE_XSLT
591 xmlNodePtr Yaz_ProxyConfigP::find_target_db(xmlNodePtr ptr, const char *db)
592 {
593     xmlNodePtr dptr;
594     if (!db)
595         return ptr;
596     if (!ptr)
597         return 0;
598     for (dptr = ptr->children; dptr; dptr = dptr->next)
599         if (dptr->type == XML_ELEMENT_NODE &&
600             !strcmp((const char *) dptr->name, "database"))
601         {
602             struct _xmlAttr *attr;
603             for (attr = dptr->properties; attr; attr = attr->next)
604                 if (!strcmp((const char *) attr->name, "name"))
605                 {
606                     if (attr->children
607                         && attr->children->type==XML_TEXT_NODE
608                         && attr->children->content 
609                         && (!strcmp((const char *) attr->children->content, db)
610                             || !strcmp((const char *) attr->children->content,
611                                        "*")))
612                         return dptr;
613                 }
614         }
615     return ptr;
616 }
617     
618 xmlNodePtr Yaz_ProxyConfigP::find_target_node(const char *name, const char *db)
619 {
620     xmlNodePtr ptr;
621     if (!m_proxyPtr)
622         return 0;
623     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
624     {
625         if (ptr->type == XML_ELEMENT_NODE &&
626             !strcmp((const char *) ptr->name, "target"))
627         {
628             // default one ? 
629             if (!name)
630             {
631                 // <target default="1"> ?
632                 struct _xmlAttr *attr;
633                 for (attr = ptr->properties; attr; attr = attr->next)
634                     if (!strcmp((const char *) attr->name, "default") &&
635                         attr->children && attr->children->type == XML_TEXT_NODE)
636                     {
637                         xmlChar *t = attr->children->content;
638                         if (!t || *t == '1')
639                         {
640                             return find_target_db(ptr, db);
641                         }
642                     }
643             }
644             else
645             {
646                 // <target name="name"> ?
647                 struct _xmlAttr *attr;
648                 for (attr = ptr->properties; attr; attr = attr->next)
649                     if (!strcmp((const char *) attr->name, "name"))
650                     {
651                         if (attr->children
652                             && attr->children->type==XML_TEXT_NODE
653                             && attr->children->content 
654                             && (!strcmp((const char *) attr->children->content,
655                                         name)
656                                 || !strcmp((const char *) attr->children->content,
657                                            "*")))
658                         {
659                             return find_target_db(ptr, db);
660                         }
661                     }
662             }
663         }
664     }
665     return 0;
666 }
667 #endif
668
669 int Yaz_ProxyConfig::get_target_no(int no,
670                                    const char **name,
671                                    const char **url,
672                                    int *limit_bw,
673                                    int *limit_pdu,
674                                    int *limit_req,
675                                    int *target_idletime,
676                                    int *client_idletime,
677                                    int *max_clients,
678                                    int *keepalive_limit_bw,
679                                    int *keepalive_limit_pdu,
680                                    int *pre_init,
681                                    const char **cql2rpn)
682 {
683 #if HAVE_XSLT
684     xmlNodePtr ptr;
685     if (!m_cp->m_proxyPtr)
686         return 0;
687     int i = 0;
688     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
689         if (ptr->type == XML_ELEMENT_NODE &&
690             !strcmp((const char *) ptr->name, "target"))
691         {
692             if (i == no)
693             {
694                 struct _xmlAttr *attr;
695                 for (attr = ptr->properties; attr; attr = attr->next)
696                     if (!strcmp((const char *) attr->name, "name"))
697                     {
698                         if (attr->children
699                             && attr->children->type==XML_TEXT_NODE
700                             && attr->children->content)
701                             *name = (const char *) attr->children->content;
702                     }
703                 m_cp->return_target_info(
704                     ptr, url,
705                     limit_bw, limit_pdu, limit_req,
706                     target_idletime, client_idletime,
707                     keepalive_limit_bw, keepalive_limit_pdu,
708                     pre_init, cql2rpn);
709                 return 1;
710             }
711             i++;
712         }
713 #endif
714     return 0;
715 }
716
717 int Yaz_ProxyConfigP::mycmp(const char *hay, const char *item, size_t len)
718 {
719     if (len == strlen(item) && memcmp(hay, item, len) == 0)
720         return 1;
721     return 0;
722 }
723
724 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
725                                        int *max_clients)
726 {
727 #if HAVE_XSLT
728     xmlNodePtr ptr;
729     if (!m_cp->m_proxyPtr)
730         return;
731     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
732     {
733         if (ptr->type == XML_ELEMENT_NODE 
734             && !strcmp((const char *) ptr->name, "log"))
735         {
736             const char *v = m_cp->get_text(ptr);
737             *log_mask = 0;
738             while (v && *v)
739             {
740                 const char *cp = v;
741                 while (*cp && *cp != ',' && !isspace(*cp))
742                     cp++;
743                 size_t len = cp - v;
744                 if (m_cp->mycmp(v, "client-apdu", len))
745                     *log_mask |= PROXY_LOG_APDU_CLIENT;
746                 if (m_cp->mycmp(v, "server-apdu", len))
747                     *log_mask |= PROXY_LOG_APDU_SERVER;
748                 if (m_cp->mycmp(v, "client-requests", len))
749                     *log_mask |= PROXY_LOG_REQ_CLIENT;
750                 if (m_cp->mycmp(v, "server-requests", len))
751                     *log_mask |= PROXY_LOG_REQ_SERVER;
752                 if (isdigit(*v))
753                     *log_mask |= atoi(v);
754                 if (*cp == ',')
755                     cp++;
756                 while (*cp && isspace(*cp))
757                     cp++;
758                 v = cp;
759             }
760         }
761         if (ptr->type == XML_ELEMENT_NODE &&
762             !strcmp((const char *) ptr->name, "max-clients"))
763         {
764             const char *t = m_cp->get_text(ptr);
765             if (t)
766             {
767                 *max_clients = atoi(t);
768                 if (*max_clients  < 1)
769                     *max_clients = 1;
770             }
771         }
772     }
773 #endif
774 }
775
776 char *Yaz_ProxyConfig::get_explain(ODR odr, const char *name, const char *db,
777                                    int *len)
778 {
779 #if HAVE_XSLT
780     xmlNodePtr ptr = m_cp->find_target_node(name, db);
781     if (ptr)
782     {
783         ptr = ptr->children;
784         for (; ptr; ptr = ptr->next)
785             if (ptr->type == XML_ELEMENT_NODE &&
786                 !strcmp((const char *) ptr->name, "explain"))
787             {
788                 xmlNodePtr ptr1 = ptr->children;
789                 if (db)
790                 {
791                     for (; ptr1; ptr1 = ptr1->next)
792                         if (ptr1->type == XML_ELEMENT_NODE &&
793                             !strcmp((const char *) ptr1->name, "serverInfo"))
794                             break;
795                     if (!ptr1)
796                         continue;
797                     for (ptr1 = ptr1->children; ptr1; ptr1 = ptr1->next)
798                         if (ptr1->type == XML_ELEMENT_NODE &&
799                             !strcmp((const char *) ptr1->name, "database"))
800                             break;
801                     
802                     if (!ptr1)
803                         continue;
804                     for (ptr1 = ptr1->children; ptr1; ptr1 = ptr1->next)
805                         if (ptr1->type == XML_TEXT_NODE &&
806                             ptr1->content &&
807                             !strcmp((const char *) ptr1->content, db))
808                             break;
809                     if (!ptr1)
810                         continue;
811                 }
812                 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
813
814                 xmlDocPtr doc = xmlNewDoc((const xmlChar *) "1.0");
815                 
816                 xmlDocSetRootElement(doc, ptr2);
817                 
818                 xmlChar *buf_out;
819                 xmlDocDumpMemory(doc, &buf_out, len);
820                 char *content = (char*) odr_malloc(odr, *len);
821                 memcpy(content, buf_out, *len);
822                 
823                 xmlFree(buf_out);
824                 xmlFreeDoc(doc);
825                 return content;
826             }
827     }
828 #endif
829     return 0;
830 }
831
832 void Yaz_ProxyConfig::get_target_info(const char *name,
833                                       const char **url,
834                                       int *limit_bw,
835                                       int *limit_pdu,
836                                       int *limit_req,
837                                       int *target_idletime,
838                                       int *client_idletime,
839                                       int *max_clients,
840                                       int *keepalive_limit_bw,
841                                       int *keepalive_limit_pdu,
842                                       int *pre_init,
843                                       const char **cql2rpn)
844 {
845 #if HAVE_XSLT
846     xmlNodePtr ptr;
847     if (!m_cp->m_proxyPtr)
848     {
849         url[0] = name;
850         url[1] = 0;
851         return;
852     }
853     url[0] = 0;
854     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
855     {
856         if (ptr->type == XML_ELEMENT_NODE &&
857             !strcmp((const char *) ptr->name, "max-clients"))
858         {
859             const char *t = m_cp->get_text(ptr);
860             if (t)
861             {
862                 *max_clients = atoi(t);
863                 if (*max_clients  < 1)
864                     *max_clients = 1;
865             }
866         }
867     }
868     ptr = m_cp->find_target_node(name, 0);
869     if (ptr)
870     {
871         if (name)
872         {
873             url[0] = name;
874             url[1] = 0;
875         }
876         m_cp->return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
877                                  target_idletime, client_idletime,
878                                  keepalive_limit_bw, keepalive_limit_pdu,
879                                  pre_init, cql2rpn);
880     }
881 #else
882     *url = name;
883     return;
884 #endif
885 }
886
887