Filter virt_db: close backend for Init rejected
[metaproxy-moved-to-github.git] / src / filter_virt_db.cpp
index cf4a088..a7d0ae8 100644 (file)
@@ -1,8 +1,20 @@
-/* $Id: filter_virt_db.cpp,v 1.50 2007-01-25 14:05:54 adam Exp $
-   Copyright (c) 2005-2007, Index Data.
+/* This file is part of Metaproxy.
+   Copyright (C) 2005-2009 Index Data
 
-   See the LICENSE file for details
- */
+Metaproxy is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
 
 #include "config.hpp"
 
@@ -19,6 +31,7 @@
 #include <yaz/zgdu.h>
 #include <yaz/otherinfo.h>
 #include <yaz/diagbib1.h>
+#include <yaz/match_glob.h>
 
 #include <map>
 #include <iostream>
@@ -38,8 +51,10 @@ namespace metaproxy_1 {
             std::string m_setname;
         };
         struct VirtualDB::Map {
-            Map(std::list<std::string> targets, std::string route);
+            Map(std::string database, std::list<std::string> targets, std::string route);
             Map();
+            bool match(const std::string db) const;
+            std::string m_dbpattern;
             std::list<std::string> m_targets;
             std::string m_route;
         };
@@ -93,11 +108,12 @@ namespace metaproxy_1 {
             FrontendPtr get_frontend(Package &package);
             void release_frontend(Package &package);
         private:
-            std::map<std::string, VirtualDB::Map>m_maps;
+            std::list<VirtualDB::Map>m_maps;
             typedef std::map<std::string,VirtualDB::Set>::iterator Sets_it;
             boost::mutex m_mutex;
             boost::condition m_cond_session_ready;
             std::map<mp::Session, FrontendPtr> m_clients;
+            bool pass_vhosts;
         };
     }
 }
@@ -129,8 +145,15 @@ yf::VirtualDB::BackendPtr yf::VirtualDB::Frontend::create_backend_from_databases
     std::map<std::string,bool> targets_dedup;
     for (; db_it != databases.end(); db_it++)
     {
-        std::map<std::string, VirtualDB::Map>::iterator map_it;
-        map_it = m_p->m_maps.find(mp::util::database_name_normalize(*db_it));
+        std::list<VirtualDB::Map>::const_iterator map_it;
+        map_it = m_p->m_maps.begin();
+        while (map_it != m_p->m_maps.end())
+        {
+            if (map_it->match(*db_it))
+                break;
+            map_it++;
+        }
+
         if (map_it == m_p->m_maps.end())  // database not found
         {
             error_code = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
@@ -139,25 +162,26 @@ yf::VirtualDB::BackendPtr yf::VirtualDB::Frontend::create_backend_from_databases
             return ptr;
         }
         std::list<std::string>::const_iterator t_it =
-            map_it->second.m_targets.begin();
-        for (; t_it != map_it->second.m_targets.end(); t_it++)
-            targets_dedup[*t_it] = true;
+            map_it->m_targets.begin();
+        for (; t_it != map_it->m_targets.end(); t_it++) {
+            if (!targets_dedup[*t_it])
+            {
+                targets_dedup[*t_it] = true;
+                b->m_targets.push_back(*t_it);
+            }
+        }
 
         // see if we have a route conflict.
-        if (!first_route && b->m_route != map_it->second.m_route)
+        if (!first_route && b->m_route != map_it->m_route)
         {
             // we have a conflict.. 
             error_code =  YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
             BackendPtr ptr;
             return ptr;
         }
-        b->m_route = map_it->second.m_route;
+        b->m_route = map_it->m_route;
         first_route = false;
     }
-    std::map<std::string,bool>::const_iterator tm_it = targets_dedup.begin();
-    for (; tm_it != targets_dedup.end(); tm_it++)
-        b->m_targets.push_back(tm_it->first);
-
     return b;
 }
 
@@ -212,31 +236,24 @@ yf::VirtualDB::BackendPtr yf::VirtualDB::Frontend::init_backend(
         {
             b->m_named_result_sets = true;
         }
-        if (!*res->result)
+        if (*res->result)
         {
-            error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
-            mp::util::get_init_diagnostics(res, error_code, addinfo);
-            BackendPtr null;
-            return null; 
+            m_backend_list.push_back(b);
+            return b;
+
         }
-    }
-    else
-    {
         error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
-        // addinfo = database;
-        BackendPtr null;
-        return null;
-    }        
-    if (init_package.session().is_closed())
+        mp::util::get_init_diagnostics(res, error_code, addinfo);
+    }
+    if (!init_package.session().is_closed())
     {
-        error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
-        // addinfo = database;
-        BackendPtr null;
-        return null;
+        Package close_package(b->m_backend_session, package.origin());
+        close_package.copy_filter(package);
+        close_package.session().close();
+        close_package.move(b->m_route);  // closing it
     }
-
-    m_backend_list.push_back(b);
-    return b;
+    BackendPtr null;
+    return null; 
 }
 
 void yf::VirtualDB::Frontend::search(mp::Package &package, Z_APDU *apdu_req)
@@ -448,8 +465,9 @@ yf::VirtualDB::Set::~Set()
 {
 }
 
-yf::VirtualDB::Map::Map(std::list<std::string> targets, std::string route)
-    : m_targets(targets), m_route(route) 
+yf::VirtualDB::Map::Map(std::string database, 
+                        std::list<std::string> targets, std::string route)
+    : m_dbpattern(database), m_targets(targets), m_route(route) 
 {
 }
 
@@ -457,8 +475,17 @@ yf::VirtualDB::Map::Map()
 {
 }
 
+bool yf::VirtualDB::Map::match(const std::string db) const
+{
+    std::string norm_db = mp::util::database_name_normalize(db);
+    if (yaz_match_glob(m_dbpattern.c_str(), norm_db.c_str()))
+        return true;
+    return false;
+}
+
 yf::VirtualDB::VirtualDB() : m_p(new VirtualDB::Rep)
 {
+    m_p->pass_vhosts = false;
 }
 
 yf::VirtualDB::~VirtualDB() {
@@ -477,20 +504,26 @@ void yf::VirtualDB::Frontend::fixup_npr_record(ODR odr, Z_NamePlusRecord *npr,
              db_it != b->m_frontend_databases.end(); db_it++)
         {
             // see which target it corresponds to.. (if any)
-            std::map<std::string,VirtualDB::Map>::const_iterator map_it;
-            map_it = m_p->m_maps.find(*db_it);
+            std::list<VirtualDB::Map>::const_iterator map_it =
+                m_p->m_maps.begin();
+            while (map_it != m_p->m_maps.end())
+            {
+                if (map_it->match(*db_it))
+                    break;
+                map_it++;
+            }
             if (map_it != m_p->m_maps.end())
             { 
-                VirtualDB::Map m = map_it->second;
-                
-                std::list<std::string>::const_iterator t;
-                for (t = m.m_targets.begin(); t != m.m_targets.end(); t++)
+                std::list<std::string>::const_iterator t
+                    = map_it->m_targets.begin();
+                while (t != map_it->m_targets.end())
                 {
                     if (*t == b_database)
                     {
                         npr->databaseName = odr_strdup(odr, (*db_it).c_str());
                         return;
                     }
+                    t++;
                 }
             }
             
@@ -654,8 +687,8 @@ void yf::VirtualDB::add_map_db2targets(std::string db,
                                      std::list<std::string> targets,
                                      std::string route)
 {
-    m_p->m_maps[mp::util::database_name_normalize(db)] 
-        = VirtualDB::Map(targets, route);
+    m_p->m_maps.push_back(
+        VirtualDB::Map(mp::util::database_name_normalize(db), targets, route));
 }
 
 
@@ -666,8 +699,7 @@ void yf::VirtualDB::add_map_db2target(std::string db,
     std::list<std::string> targets;
     targets.push_back(target);
 
-    m_p->m_maps[mp::util::database_name_normalize(db)]
-        = VirtualDB::Map(targets, route);
+    add_map_db2targets(db, targets, route);
 }
 
 void yf::VirtualDB::process(mp::Package &package) const
@@ -683,7 +715,12 @@ void yf::VirtualDB::process(mp::Package &package) const
         
         std::list<std::string> vhosts;
         mp::util::get_vhost_otherinfo(req->otherInfo, vhosts);
-        if (vhosts.size() == 0)
+
+        if (vhosts.size() > 0 && m_p->pass_vhosts)
+        {
+            package.move();
+        }
+        else
         {
             f->m_init_gdu = gdu;
             
@@ -718,8 +755,6 @@ void yf::VirtualDB::process(mp::Package &package) const
             package.response() = apdu;
             f->m_is_virtual = true;
         }
-        else
-            package.move();
     }
     else if (!f->m_is_virtual)
         package.move();
@@ -768,13 +803,17 @@ void yf::VirtualDB::process(mp::Package &package) const
 }
 
 
-void mp::filter::VirtualDB::configure(const xmlNode * ptr)
+void mp::filter::VirtualDB::configure(const xmlNode * ptr, bool test_only)
 {
     for (ptr = ptr->children; ptr; ptr = ptr->next)
     {
         if (ptr->type != XML_ELEMENT_NODE)
             continue;
-        if (!strcmp((const char *) ptr->name, "virtual"))
+        if (!strcmp((const char *) ptr->name, "pass-vhosts"))
+        {
+            m_p->pass_vhosts = mp::xml::get_bool(ptr->children, false);
+        }
+        else if (!strcmp((const char *) ptr->name, "virtual"))
         {
             std::string database;
             std::list<std::string> targets;
@@ -825,8 +864,9 @@ extern "C" {
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
- * c-file-style: "stroustrup"
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab
  */
+