Limit record cache size
[yazpp-moved-to-github.git] / src / yaz-proxy-config.cpp
index 329a037..1abd49f 100644 (file)
@@ -2,9 +2,10 @@
  * Copyright (c) 1998-2003, Index Data.
  * See the file LICENSE for details.
  * 
- * $Id: yaz-proxy-config.cpp,v 1.2 2003-10-03 13:01:42 adam Exp $
+ * $Id: yaz-proxy-config.cpp,v 1.5 2003-10-08 08:15:01 adam Exp $
  */
 
+#include <ctype.h>
 #include <yaz/log.h>
 #include <yaz++/proxy.h>
 
@@ -173,6 +174,41 @@ void Yaz_ProxyConfig::return_target_info(xmlNodePtr ptr,
 }
 #endif
 
+int Yaz_ProxyConfig::atoi_l(const char **cp)
+{
+    int v = 0;
+    while (**cp && isdigit(**cp))
+    {
+       v = v*10 + (**cp - '0');
+       (*cp)++;
+    }
+    return v;
+}
+
+int Yaz_ProxyConfig::match_list(int v, const char *m)
+{
+  while(m && *m)
+  {
+      while(*m && isspace(*m))
+         m++;
+      if (*m == '*')
+         return 1;
+      int l = atoi_l(&m);
+      int h = l;
+      if (*m == '-')
+      {
+         ++m;
+         h = atoi_l(&m);
+      }
+      if (v >= l && v <= h)
+         return 1;
+      if (*m == ',')
+         m++;
+  }
+  return 0;
+}
+
+#if HAVE_XML2
 int Yaz_ProxyConfig::check_type_1_attributes(ODR odr, xmlNodePtr ptr,
                                             Z_AttributeList *attrs,
                                             char **addinfo)
@@ -212,19 +248,14 @@ int Yaz_ProxyConfig::check_type_1_attributes(ODR odr, xmlNodePtr ptr,
                        continue;
                    int type = *el->attributeType;
 
-                   if (strcmp(match_type, "*")) {
-                       if (type != atoi(match_type))
-                           continue;  // no match on type
-                   }
+                   if (!match_list(type, match_type))
+                       continue;
                    if (el->which == Z_AttributeValue_numeric && 
                        el->value.numeric)
                    {
-                       int value = *el->value.numeric;
-                       if (strcmp(match_value, "*")) {
-                           if (value != atoi(match_value))
-                               continue;  // no match on value
-                       }
-                       sprintf(value_str, "%d", value);
+                       if (!match_list(*el->value.numeric, match_value))
+                           continue;
+                       sprintf (value_str, "%d", *el->value.numeric);
                    }
                    else
                        continue;
@@ -241,7 +272,9 @@ int Yaz_ProxyConfig::check_type_1_attributes(ODR odr, xmlNodePtr ptr,
     }
     return 0;
 }
+#endif
 
+#if HAVE_XML2
 int Yaz_ProxyConfig::check_type_1_structure(ODR odr, xmlNodePtr ptr,
                                            Z_RPNStructure *q,
                                            char **addinfo)
@@ -266,17 +299,21 @@ int Yaz_ProxyConfig::check_type_1_structure(ODR odr, xmlNodePtr ptr,
     }
     return 0;
 }
+#endif
 
+#if HAVE_XML2
 int Yaz_ProxyConfig::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
                                  char **addinfo)
 {
     // possibly check for Bib-1
     return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
 }
+#endif
 
 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
                                 char **addinfo)
 {
+#if HAVE_XML2
     xmlNodePtr ptr;
     
     ptr = find_target_node(name);
@@ -285,9 +322,75 @@ int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
        if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
            return check_type_1(odr, ptr, query->u.type_1, addinfo);
     }
+#endif
     return 0;
 }
 
+int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
+                                 Odr_oid *syntax, char **addinfo)
+{
+#if HAVE_XML2
+    xmlNodePtr ptr;
+    
+    ptr = find_target_node(name);
+    if (!ptr)
+       return 0;
+    for(ptr = ptr->children; ptr; ptr = ptr->next)
+    {
+       if (ptr->type == XML_ELEMENT_NODE &&
+           !strcmp((const char *) ptr->name, "syntax"))
+       {
+           int match = 0;  // if we match record syntax
+           const char *match_type = 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, "error") &&
+                   attr->children && attr->children->type == XML_TEXT_NODE)
+                   match_error = (const char *) attr->children->content;
+           }
+           if (match_type)
+           {
+               if (!strcmp(match_type, "*"))
+                   match = 1;
+               else if (!strcmp(match_type, "none"))
+               {
+                   if (syntax == 0)
+                       match = 1;
+               }
+               else if (syntax)
+               {
+                   int match_oid[OID_SIZE];
+                   oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
+                   if (oid_oidcmp(match_oid, syntax) == 0)
+                       match = 1;
+               }
+           }
+           if (match)
+           {
+               if (match_error)
+               {
+                   if (syntax)
+                   {
+                       char dotoid_str[100];
+                       oid_to_dotstring(syntax, dotoid_str);
+                       *addinfo = odr_strdup(odr, dotoid_str);
+                   }
+                   return atoi(match_error);
+               }
+               return 0;
+           }
+       }
+    }
+#endif
+    return 0;
+}
+
+#if HAVE_XML2
 xmlNodePtr Yaz_ProxyConfig::find_target_node(const char *name)
 {
     xmlNodePtr ptr;
@@ -333,7 +436,7 @@ xmlNodePtr Yaz_ProxyConfig::find_target_node(const char *name)
     }
     return 0;
 }
-
+#endif
 
 void Yaz_ProxyConfig::get_target_info(const char *name,
                                      const char **url,