Fix attribute type checking
[yazpp-moved-to-github.git] / src / yaz-proxy-config.cpp
index 2d5d22e..1742cf8 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 1998-2003, Index Data.
  * See the file LICENSE for details.
  * 
- * $Id: yaz-proxy-config.cpp,v 1.10 2003-10-16 13:40:41 adam Exp $
+ * $Id: yaz-proxy-config.cpp,v 1.13 2003-10-24 10:21:24 adam Exp $
  */
 
 #include <ctype.h>
@@ -117,8 +117,7 @@ void Yaz_ProxyConfig::return_target_info(xmlNodePtr ptr,
                                         int *client_idletime,
                                         int *keepalive_limit_bw,
                                         int *keepalive_limit_pdu,
-                                        int *pre_init,
-                                        int *log_mask)
+                                        int *pre_init)
 {
     *pre_init = 0;
     int no_url = 0;
@@ -126,30 +125,6 @@ void Yaz_ProxyConfig::return_target_info(xmlNodePtr ptr,
     for (; ptr; ptr = ptr->next)
     {
        if (ptr->type == XML_ELEMENT_NODE 
-           && !strcmp((const char *) ptr->name, "log"))
-       {
-           const char *v = get_text(ptr);
-           *log_mask = 0;
-           while (v && *v)
-           {
-               const char *cp = v;
-               while (*cp && *cp != ',' && !isspace(*cp))
-                   cp++;
-               size_t len = cp - v;
-               if (len == 4 && !memcmp(v, "apdu", 4))
-                   *log_mask |= PROXY_LOG_APDU;
-               if (len == 3 && !memcmp(v, "req", 3))
-                   *log_mask |= PROXY_LOG_REQ;
-               if (isdigit(*v))
-                   *log_mask |= atoi(v);
-               if (*cp == ',')
-                   cp++;
-               while (*cp && isspace(*cp))
-                   cp++;
-               v = cp;
-           }
-       }
-       if (ptr->type == XML_ELEMENT_NODE 
            && !strcmp((const char *) ptr->name, "preinit"))
        {
            const char *v = get_text(ptr);
@@ -238,60 +213,67 @@ int Yaz_ProxyConfig::match_list(int v, const char *m)
 }
 
 #if HAVE_XML2
-int Yaz_ProxyConfig::check_type_1_attributes(ODR odr, xmlNodePtr ptr,
+int Yaz_ProxyConfig::check_type_1_attributes(ODR odr, xmlNodePtr ptrl,
                                             Z_AttributeList *attrs,
                                             char **addinfo)
 {
-    for(ptr = ptr->children; ptr; ptr = ptr->next)
+    int i;
+    for (i = 0; i<attrs->num_attributes; i++)
     {
-       if (ptr->type == XML_ELEMENT_NODE &&
-           !strcmp((const char *) ptr->name, "attribute"))
+       Z_AttributeElement *el = attrs->attributes[i];
+       
+       if (!el->attributeType)
+           continue;
+       int type = *el->attributeType;
+       int *value = 0;
+       
+       if (el->which == Z_AttributeValue_numeric && el->value.numeric)
+           value = el->value.numeric;
+       
+       xmlNodePtr ptr;
+       for(ptr = ptrl->children; ptr; ptr = ptr->next)
        {
-           const char *match_type = 0;
-           const char *match_value = 0;
-           const char *match_error = 0;
-           struct _xmlAttr *attr;
-           for (attr = ptr->properties; attr; attr = attr->next)
-           {
-               if (!strcmp((const char *) attr->name, "type") &&
-                   attr->children && attr->children->type == XML_TEXT_NODE)
-                   match_type = (const char *) attr->children->content;
-               if (!strcmp((const char *) attr->name, "value") &&
-                   attr->children && attr->children->type == XML_TEXT_NODE)
-                   match_value = (const char *) attr->children->content;
-               if (!strcmp((const char *) attr->name, "error") &&
-                   attr->children && attr->children->type == XML_TEXT_NODE)
-                   match_error = (const char *) attr->children->content;
-           }
-           int i;
-
-           if (match_type && match_value)
+           if (ptr->type == XML_ELEMENT_NODE &&
+               !strcmp((const char *) ptr->name, "attribute"))
            {
-               for (i = 0; i<attrs->num_attributes; i++)
+               const char *match_type = 0;
+               const char *match_value = 0;
+               const char *match_error = 0;
+               struct _xmlAttr *attr;
+               for (attr = ptr->properties; attr; attr = attr->next)
                {
-                   Z_AttributeElement *el = attrs->attributes[i];
-                   char value_str[20];
-                   
-                   value_str[0] = '\0';
-                   if (!el->attributeType)
-                       continue;
-                   int type = *el->attributeType;
-
+                   if (!strcmp((const char *) attr->name, "type") &&
+                       attr->children && attr->children->type == XML_TEXT_NODE)
+                       match_type = (const char *) attr->children->content;
+                   if (!strcmp((const char *) attr->name, "value") &&
+                       attr->children && attr->children->type == XML_TEXT_NODE)
+                       match_value = (const char *) attr->children->content;
+                   if (!strcmp((const char *) attr->name, "error") &&
+                       attr->children && attr->children->type == XML_TEXT_NODE)
+                       match_error = (const char *) attr->children->content;
+               }
+               if (match_type && match_value)
+               {
+                   char addinfo_str[20];
                    if (!match_list(type, match_type))
                        continue;
-                   if (el->which == Z_AttributeValue_numeric && 
-                       el->value.numeric)
+                   
+                   *addinfo_str = '\0';
+                   if (!strcmp(match_type, "*"))
+                       sprintf (addinfo_str, "%d", type);
+                   else if (value)
                    {
-                       if (!match_list(*el->value.numeric, match_value))
+                       if (!match_list(*value, match_value))
                            continue;
-                       sprintf (value_str, "%d", *el->value.numeric);
+                       sprintf (addinfo_str, "%d", *value);
                    }
                    else
                        continue;
+                   
                    if (match_error)
                    {
-                       if (*value_str)
-                           *addinfo = odr_strdup(odr, value_str);
+                       if (*addinfo_str)
+                           *addinfo = odr_strdup(odr, addinfo_str);
                        return atoi(match_error);
                    }
                    return 0;
@@ -308,7 +290,6 @@ int Yaz_ProxyConfig::check_type_1_structure(ODR odr, xmlNodePtr ptr,
                                            Z_RPNStructure *q,
                                            char **addinfo)
 {
-    int c;
     if (q->which == Z_RPNStructure_complex)
     {
        int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
@@ -488,8 +469,7 @@ int Yaz_ProxyConfig::get_target_no(int no,
                                   int *max_clients,
                                   int *keepalive_limit_bw,
                                   int *keepalive_limit_pdu,
-                                  int *pre_init,
-                                  int *log_mask)
+                                  int *pre_init)
 {
 #if HAVE_XML2
     xmlNodePtr ptr;
@@ -514,7 +494,7 @@ int Yaz_ProxyConfig::get_target_no(int no,
                return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
                                   target_idletime, client_idletime,
                                   keepalive_limit_bw, keepalive_limit_pdu,
-                                  pre_init, log_mask);
+                                  pre_init);
                return 1;
            }
            i++;
@@ -523,6 +503,65 @@ int Yaz_ProxyConfig::get_target_no(int no,
     return 0;
 }
 
+int Yaz_ProxyConfig::mycmp(const char *hay, const char *item, size_t len)
+{
+    if (len == strlen(item) && memcmp(hay, item, len) == 0)
+       return 1;
+    return 0;
+}
+
+void Yaz_ProxyConfig::get_generic_info(int *log_mask,
+                                      int *max_clients)
+{
+#if HAVE_XML2
+    xmlNodePtr ptr;
+    if (!m_proxyPtr)
+       return;
+    for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
+    {
+       if (ptr->type == XML_ELEMENT_NODE 
+           && !strcmp((const char *) ptr->name, "log"))
+       {
+           const char *v = get_text(ptr);
+           *log_mask = 0;
+           while (v && *v)
+           {
+               const char *cp = v;
+               while (*cp && *cp != ',' && !isspace(*cp))
+                   cp++;
+               size_t len = cp - v;
+               if (mycmp(v, "client-apdu", len))
+                   *log_mask |= PROXY_LOG_APDU_CLIENT;
+               if (mycmp(v, "server-apdu", len))
+                   *log_mask |= PROXY_LOG_APDU_SERVER;
+               if (mycmp(v, "client-requests", len))
+                   *log_mask |= PROXY_LOG_REQ_CLIENT;
+               if (mycmp(v, "server-requests", len))
+                   *log_mask |= PROXY_LOG_REQ_SERVER;
+               if (isdigit(*v))
+                   *log_mask |= atoi(v);
+               if (*cp == ',')
+                   cp++;
+               while (*cp && isspace(*cp))
+                   cp++;
+               v = cp;
+           }
+       }
+       if (ptr->type == XML_ELEMENT_NODE &&
+           !strcmp((const char *) ptr->name, "max-clients"))
+       {
+           const char *t = get_text(ptr);
+           if (t)
+           {
+               *max_clients = atoi(t);
+               if (*max_clients  < 1)
+                   *max_clients = 1;
+           }
+       }
+    }
+#endif
+}
+
 void Yaz_ProxyConfig::get_target_info(const char *name,
                                      const char **url,
                                      int *limit_bw,
@@ -533,8 +572,7 @@ void Yaz_ProxyConfig::get_target_info(const char *name,
                                      int *max_clients,
                                      int *keepalive_limit_bw,
                                      int *keepalive_limit_pdu,
-                                     int *pre_init,
-                                     int *log_mask)
+                                     int *pre_init)
 {
 #if HAVE_XML2
     xmlNodePtr ptr;
@@ -570,7 +608,7 @@ void Yaz_ProxyConfig::get_target_info(const char *name,
        return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
                           target_idletime, client_idletime,
                           keepalive_limit_bw, keepalive_limit_pdu,
-                          pre_init, log_mask);
+                          pre_init);
     }
 #else
     *url = name;