Using yaz/log.h again. Added config file HTTP access.
[yazproxy-moved-to-github.git] / src / yaz-proxy-config.cpp
1 /* $Id: yaz-proxy-config.cpp,v 1.12 2004-12-13 20:52:33 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 "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 const char *Yaz_ProxyConfig::check_mime_type(const char *path)
455 {
456     struct {
457         const char *mask;
458         const char *type;
459     } types[] = {
460         {".xml", "text/xml"},
461         {".xsl", "text/xml"},
462         {".tkl", "text/xml"},
463         {".xsd", "text/xml"},
464         {0, "text/plain"},
465         {0, 0},
466     };
467     int i;
468     size_t plen = strlen (path);
469     for (i = 0; types[i].type; i++)
470         if (types[i].mask == 0)
471             return types[i].type;
472         else
473         {
474             size_t mlen = strlen(types[i].mask);
475             if (plen > mlen && !memcmp(path+plen-mlen, types[i].mask, mlen))
476                 return types[i].type;
477         }
478     return "application/octet-stream";
479 }
480
481
482 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
483                                   Odr_oid *syntax, Z_RecordComposition *comp,
484                                   char **addinfo,
485                                   char **stylesheet, char **schema,
486                                   char **backend_type,
487                                   char **backend_charset,
488                                   char **usemarcon_ini_stage1,
489                                   char **usemarcon_ini_stage2
490                                   )
491 {
492     if (stylesheet)
493     {
494         xfree (*stylesheet);
495         *stylesheet = 0;
496     }
497     if (schema)
498     {
499         xfree (*schema);
500         *schema = 0;
501     }
502     if (backend_type)
503     {
504         xfree (*backend_type);
505         *backend_type = 0;
506     }
507     if (backend_charset)
508     {
509         xfree (*backend_charset);
510         *backend_charset = 0;
511     }
512     if (usemarcon_ini_stage1)
513     {
514         xfree (*usemarcon_ini_stage1);
515         *usemarcon_ini_stage1 = 0;
516     }
517     if (usemarcon_ini_stage2)
518     {
519         xfree (*usemarcon_ini_stage2);
520         *usemarcon_ini_stage2 = 0;
521     }
522 #if HAVE_XSLT
523     int syntax_has_matched = 0;
524     xmlNodePtr ptr;
525     
526     ptr = m_cp->find_target_node(name, 0);
527     if (!ptr)
528         return 0;
529     for(ptr = ptr->children; ptr; ptr = ptr->next)
530     {
531         if (ptr->type == XML_ELEMENT_NODE &&
532             !strcmp((const char *) ptr->name, "syntax"))
533         {
534             int match = 0;  // if we match record syntax
535             const char *match_type = 0;
536             const char *match_error = 0;
537             const char *match_marcxml = 0;
538             const char *match_stylesheet = 0;
539             const char *match_identifier = 0;
540             const char *match_backend_type = 0;
541             const char *match_backend_charset = 0;
542             const char *match_usemarcon_ini_stage1 = 0;
543             const char *match_usemarcon_ini_stage2 = 0;
544             struct _xmlAttr *attr;
545             for (attr = ptr->properties; attr; attr = attr->next)
546             {
547                 if (!strcmp((const char *) attr->name, "type") &&
548                     attr->children && attr->children->type == XML_TEXT_NODE)
549                     match_type = (const char *) attr->children->content;
550                 if (!strcmp((const char *) attr->name, "error") &&
551                     attr->children && attr->children->type == XML_TEXT_NODE)
552                     match_error = (const char *) attr->children->content;
553                 if (!strcmp((const char *) attr->name, "marcxml") &&
554                     attr->children && attr->children->type == XML_TEXT_NODE)
555                     match_marcxml = (const char *) attr->children->content;
556                 if (!strcmp((const char *) attr->name, "stylesheet") &&
557                     attr->children && attr->children->type == XML_TEXT_NODE)
558                     match_stylesheet = (const char *) attr->children->content;
559                 if (!strcmp((const char *) attr->name, "identifier") &&
560                     attr->children && attr->children->type == XML_TEXT_NODE)
561                     match_identifier = (const char *) attr->children->content;
562                 if (!strcmp((const char *) attr->name, "backendtype") &&
563                     attr->children && attr->children->type == XML_TEXT_NODE)
564                     match_backend_type = (const char *)
565                         attr->children->content;
566                 if (!strcmp((const char *) attr->name, "backendcharset") &&
567                     attr->children && attr->children->type == XML_TEXT_NODE)
568                     match_backend_charset = (const char *)
569                         attr->children->content;
570                 if (!strcmp((const char *) attr->name, "usemarconstage1") &&
571                     attr->children && attr->children->type == XML_TEXT_NODE)
572                     match_usemarcon_ini_stage1 = (const char *)
573                         attr->children->content;
574                 if (!strcmp((const char *) attr->name, "usemarconstage2") &&
575                     attr->children && attr->children->type == XML_TEXT_NODE)
576                     match_usemarcon_ini_stage2 = (const char *)
577                         attr->children->content;
578             }
579             if (match_type)
580             {
581                 if (!strcmp(match_type, "*"))
582                     match = 1;
583                 else if (!strcmp(match_type, "none"))
584                 {
585                     if (syntax == 0)
586                         match = 1;
587                 }
588                 else if (syntax)
589                 {
590                     int match_oid[OID_SIZE];
591                     oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
592                     if (oid_oidcmp(match_oid, syntax) == 0)
593                         match = 1;
594                 }
595             }
596             if (match)
597             {
598                 if (!match_error)
599                     syntax_has_matched = 1;
600                 match = m_cp->check_schema(ptr->children, comp,
601                                            match_identifier);
602             }
603             if (match)
604             {
605                 if (stylesheet && match_stylesheet)
606                 {
607                     xfree(*stylesheet);
608                     *stylesheet = xstrdup(match_stylesheet);
609                 }
610                 if (schema && match_identifier)
611                 {
612                     xfree(*schema);
613                     *schema = xstrdup(match_identifier);
614                 }
615                 if (backend_type && match_backend_type)
616                 {
617                     xfree(*backend_type);
618                     *backend_type = xstrdup(match_backend_type);
619                 }
620                 if (backend_charset && match_backend_charset)
621                 {
622                     xfree(*backend_charset);
623                     *backend_charset = xstrdup(match_backend_charset);
624                 }
625                 if (usemarcon_ini_stage1 && match_usemarcon_ini_stage1)
626                 {
627                     xfree(*usemarcon_ini_stage1);
628                     *usemarcon_ini_stage1 = xstrdup(match_usemarcon_ini_stage1);
629                 }
630                 if (usemarcon_ini_stage1 && match_usemarcon_ini_stage2)
631                 {
632                     xfree(*usemarcon_ini_stage2);
633                     *usemarcon_ini_stage2 = xstrdup(match_usemarcon_ini_stage2);
634                 }
635                 if (match_marcxml)
636                 {
637                     return -1;
638                 }
639                 if (match_error)
640                 {
641                     if (syntax_has_matched)  // if syntax OK, bad schema/ESN
642                         return 25;
643                     if (syntax)
644                     {
645                         char dotoid_str[100];
646                         oid_to_dotstring(syntax, dotoid_str);
647                         *addinfo = odr_strdup(odr, dotoid_str);
648                     }
649                     return atoi(match_error);
650                 }
651                 return 0;
652             }
653         }
654     }
655 #endif
656     return 0;
657 }
658
659 #if HAVE_XSLT
660 xmlNodePtr Yaz_ProxyConfigP::find_target_db(xmlNodePtr ptr, const char *db)
661 {
662     xmlNodePtr dptr;
663     if (!db)
664         return ptr;
665     if (!ptr)
666         return 0;
667     for (dptr = ptr->children; dptr; dptr = dptr->next)
668         if (dptr->type == XML_ELEMENT_NODE &&
669             !strcmp((const char *) dptr->name, "database"))
670         {
671             struct _xmlAttr *attr;
672             for (attr = dptr->properties; attr; attr = attr->next)
673                 if (!strcmp((const char *) attr->name, "name"))
674                 {
675                     if (attr->children
676                         && attr->children->type==XML_TEXT_NODE
677                         && attr->children->content 
678                         && (!strcmp((const char *) attr->children->content, db)
679                             || !strcmp((const char *) attr->children->content,
680                                        "*")))
681                         return dptr;
682                 }
683         }
684     return ptr;
685 }
686     
687 xmlNodePtr Yaz_ProxyConfigP::find_target_node(const char *name, const char *db)
688 {
689     xmlNodePtr ptr;
690     if (!m_proxyPtr)
691         return 0;
692     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
693     {
694         if (ptr->type == XML_ELEMENT_NODE &&
695             !strcmp((const char *) ptr->name, "target"))
696         {
697             // default one ? 
698             if (!name)
699             {
700                 // <target default="1"> ?
701                 struct _xmlAttr *attr;
702                 for (attr = ptr->properties; attr; attr = attr->next)
703                     if (!strcmp((const char *) attr->name, "default") &&
704                         attr->children && attr->children->type == XML_TEXT_NODE)
705                     {
706                         xmlChar *t = attr->children->content;
707                         if (!t || *t == '1')
708                         {
709                             return find_target_db(ptr, db);
710                         }
711                     }
712             }
713             else
714             {
715                 // <target name="name"> ?
716                 struct _xmlAttr *attr;
717                 for (attr = ptr->properties; attr; attr = attr->next)
718                     if (!strcmp((const char *) attr->name, "name"))
719                     {
720                         if (attr->children
721                             && attr->children->type==XML_TEXT_NODE
722                             && attr->children->content 
723                             && (!strcmp((const char *) attr->children->content,
724                                         name)
725                                 || !strcmp((const char *) attr->children->content,
726                                            "*")))
727                         {
728                             return find_target_db(ptr, db);
729                         }
730                     }
731             }
732         }
733     }
734     return 0;
735 }
736 #endif
737
738 int Yaz_ProxyConfig::get_target_no(int no,
739                                    const char **name,
740                                    const char **url,
741                                    int *limit_bw,
742                                    int *limit_pdu,
743                                    int *limit_req,
744                                    int *target_idletime,
745                                    int *client_idletime,
746                                    int *max_clients,
747                                    int *keepalive_limit_bw,
748                                    int *keepalive_limit_pdu,
749                                    int *pre_init,
750                                    const char **cql2rpn,
751                                    const char **authentication)
752 {
753 #if HAVE_XSLT
754     xmlNodePtr ptr;
755     if (!m_cp->m_proxyPtr)
756         return 0;
757     int i = 0;
758     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
759         if (ptr->type == XML_ELEMENT_NODE &&
760             !strcmp((const char *) ptr->name, "target"))
761         {
762             if (i == no)
763             {
764                 struct _xmlAttr *attr;
765                 for (attr = ptr->properties; attr; attr = attr->next)
766                     if (!strcmp((const char *) attr->name, "name"))
767                     {
768                         if (attr->children
769                             && attr->children->type==XML_TEXT_NODE
770                             && attr->children->content)
771                             *name = (const char *) attr->children->content;
772                     }
773                 m_cp->return_target_info(
774                     ptr, url,
775                     limit_bw, limit_pdu, limit_req,
776                     target_idletime, client_idletime,
777                     keepalive_limit_bw, keepalive_limit_pdu,
778                     pre_init, cql2rpn, authentication);
779                 return 1;
780             }
781             i++;
782         }
783 #endif
784     return 0;
785 }
786
787 int Yaz_ProxyConfigP::mycmp(const char *hay, const char *item, size_t len)
788 {
789     if (len == strlen(item) && memcmp(hay, item, len) == 0)
790         return 1;
791     return 0;
792 }
793
794 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
795                                        int *max_clients)
796 {
797 #if HAVE_XSLT
798     xmlNodePtr ptr;
799     if (!m_cp->m_proxyPtr)
800         return;
801     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
802     {
803         if (ptr->type == XML_ELEMENT_NODE 
804             && !strcmp((const char *) ptr->name, "log"))
805         {
806             const char *v = m_cp->get_text(ptr);
807             *log_mask = 0;
808             while (v && *v)
809             {
810                 const char *cp = v;
811                 while (*cp && *cp != ',' && !isspace(*cp))
812                     cp++;
813                 size_t len = cp - v;
814                 if (m_cp->mycmp(v, "client-apdu", len))
815                     *log_mask |= PROXY_LOG_APDU_CLIENT;
816                 if (m_cp->mycmp(v, "server-apdu", len))
817                     *log_mask |= PROXY_LOG_APDU_SERVER;
818                 if (m_cp->mycmp(v, "client-requests", len))
819                     *log_mask |= PROXY_LOG_REQ_CLIENT;
820                 if (m_cp->mycmp(v, "server-requests", len))
821                     *log_mask |= PROXY_LOG_REQ_SERVER;
822                 if (isdigit(*v))
823                     *log_mask |= atoi(v);
824                 if (*cp == ',')
825                     cp++;
826                 while (*cp && isspace(*cp))
827                     cp++;
828                 v = cp;
829             }
830         }
831         if (ptr->type == XML_ELEMENT_NODE &&
832             !strcmp((const char *) ptr->name, "max-clients"))
833         {
834             const char *t = m_cp->get_text(ptr);
835             if (t)
836             {
837                 *max_clients = atoi(t);
838                 if (*max_clients  < 1)
839                     *max_clients = 1;
840             }
841         }
842     }
843 #endif
844 }
845
846 #if HAVE_XSLT
847 int Yaz_ProxyConfigP::get_explain_ptr(const char *host, const char *db,
848                                       xmlNodePtr *ptr_target,
849                                       xmlNodePtr *ptr_explain)
850 {
851     xmlNodePtr ptr;
852     if (!m_proxyPtr)
853         return 0;
854     if (!db)
855         return 0;
856     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
857     {
858         if (ptr->type == XML_ELEMENT_NODE &&
859             !strcmp((const char *) ptr->name, "target"))
860         {
861             *ptr_target = ptr;
862             xmlNodePtr ptr = (*ptr_target)->children;
863             for (; ptr; ptr = ptr->next)
864             {
865                 if (ptr->type == XML_ELEMENT_NODE &&
866                     !strcmp((const char *) ptr->name, "explain"))
867                 {
868                     *ptr_explain = ptr;
869                     xmlNodePtr ptr = (*ptr_explain)->children;
870
871                     for (; ptr; ptr = ptr->next)
872                         if (ptr->type == XML_ELEMENT_NODE &&
873                             !strcmp((const char *) ptr->name, "serverInfo"))
874                             break;
875                     if (!ptr)
876                         continue;
877                     for (ptr = ptr->children; ptr; ptr = ptr->next)
878                         if (ptr->type == XML_ELEMENT_NODE &&
879                             !strcmp((const char *) ptr->name, "database"))
880                             break;
881                     
882                     if (!ptr)
883                         continue;
884                     for (ptr = ptr->children; ptr; ptr = ptr->next)
885                         if (ptr->type == XML_TEXT_NODE &&
886                             ptr->content &&
887                             !strcmp((const char *) ptr->content, db))
888                             break;
889                     if (!ptr)
890                         continue;
891                     return 1;
892                 }
893             }
894         }
895     }
896     return 0;
897 }
898 #endif
899
900 const char *Yaz_ProxyConfig::get_explain_name(const char *db,
901                                               const char **backend_db)
902 {
903 #if HAVE_XSLT
904     xmlNodePtr ptr_target, ptr_explain;
905     if (m_cp->get_explain_ptr(0, db, &ptr_target, &ptr_explain)
906         && ptr_target)
907     {
908         struct _xmlAttr *attr;
909         const char *name = 0;
910         
911         for (attr = ptr_target->properties; attr; attr = attr->next)
912             if (!strcmp((const char *) attr->name, "name")
913                 && attr->children
914                 && attr->children->type==XML_TEXT_NODE
915                 && attr->children->content 
916                 && attr->children->content[0])
917             {
918                 name = (const char *)attr->children->content;
919                 break;
920             }
921         if (name)
922         {
923             for (attr = ptr_target->properties; attr; attr = attr->next)
924                 if (!strcmp((const char *) attr->name, "database"))
925                 {
926                     if (attr->children
927                         && attr->children->type==XML_TEXT_NODE
928                         && attr->children->content)
929                         *backend_db = (const char *) attr->children->content;
930                 }
931             return name;
932         }
933     }
934 #endif
935     return 0;
936 }
937
938 char *Yaz_ProxyConfig::get_explain_doc(ODR odr, const char *name,
939                                        const char *db, int *len)
940 {
941 #if HAVE_XSLT
942     xmlNodePtr ptr_target, ptr_explain;
943     if (m_cp->get_explain_ptr(0 /* host */, db, &ptr_target, &ptr_explain))
944     {
945         xmlNodePtr ptr2 = xmlCopyNode(ptr_explain, 1);
946         
947         xmlDocPtr doc = xmlNewDoc((const xmlChar *) "1.0");
948         
949         xmlDocSetRootElement(doc, ptr2);
950         
951         xmlChar *buf_out;
952         xmlDocDumpMemory(doc, &buf_out, len);
953         char *content = (char*) odr_malloc(odr, *len);
954         memcpy(content, buf_out, *len);
955         
956         xmlFree(buf_out);
957         xmlFreeDoc(doc);
958         return content;
959     }
960 #endif
961     return 0;
962 }
963
964 void Yaz_ProxyConfig::get_target_info(const char *name,
965                                       const char **url,
966                                       int *limit_bw,
967                                       int *limit_pdu,
968                                       int *limit_req,
969                                       int *target_idletime,
970                                       int *client_idletime,
971                                       int *max_clients,
972                                       int *keepalive_limit_bw,
973                                       int *keepalive_limit_pdu,
974                                       int *pre_init,
975                                       const char **cql2rpn,
976                                       const char **authentication)
977 {
978 #if HAVE_XSLT
979     xmlNodePtr ptr;
980     if (!m_cp->m_proxyPtr)
981     {
982         url[0] = name;
983         url[1] = 0;
984         return;
985     }
986     url[0] = 0;
987     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
988     {
989         if (ptr->type == XML_ELEMENT_NODE &&
990             !strcmp((const char *) ptr->name, "max-clients"))
991         {
992             const char *t = m_cp->get_text(ptr);
993             if (t)
994             {
995                 *max_clients = atoi(t);
996                 if (*max_clients  < 1)
997                     *max_clients = 1;
998             }
999         }
1000     }
1001     ptr = m_cp->find_target_node(name, 0);
1002     if (ptr)
1003     {
1004         if (name)
1005         {
1006             url[0] = name;
1007             url[1] = 0;
1008         }
1009         m_cp->return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
1010                                  target_idletime, client_idletime,
1011                                  keepalive_limit_bw, keepalive_limit_pdu,
1012                                  pre_init, cql2rpn, authentication);
1013     }
1014 #else
1015     *url = name;
1016     return;
1017 #endif
1018 }
1019
1020