Fix virt_db creates result-set for failed search MP-627
[metaproxy-moved-to-github.git] / src / filter_virt_db.cpp
index 896ae47..11799d4 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Metaproxy.
-   Copyright (C) 2005-2012 Index Data
+   Copyright (C) Index Data
 
 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
@@ -32,6 +32,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <yaz/diagbib1.h>
 #include <yaz/match_glob.h>
 #include <yaz/log.h>
+#include <yaz/oid_db.h>
 
 #include <map>
 #include <iostream>
@@ -211,6 +212,13 @@ yf::VirtualDB::BackendPtr yf::VirtualDB::Frontend::init_backend(
     Z_GDU *org_gdu = m_init_gdu.get();
     Z_InitRequest *org_init = org_gdu->u.z3950->u.initRequest;
 
+
+    const char *peer_name = yaz_oi_get_string_oid(
+        &org_init->otherInfo, yaz_oid_userinfo_client_ip, 1, 0);
+    if (peer_name)
+        yaz_oi_set_string_oid(&init_apdu->u.initRequest->otherInfo, odr,
+                              yaz_oid_userinfo_client_ip, 1, peer_name);
+
     req->idAuthentication = org_init->idAuthentication;
     req->implementationId = org_init->implementationId;
     req->implementationName = org_init->implementationName;
@@ -369,10 +377,20 @@ void yf::VirtualDB::Frontend::search(mp::Package &package, Z_APDU *apdu_req)
         package.session().close();
         return;
     }
-    b->m_number_of_sets++;
 
-    m_sets[resultSetId] = VirtualDB::Set(b, backend_setname);
-    fixup_package(search_package, b);
+    Z_GDU *gdu = search_package.response().get();
+    if (gdu && gdu->which == Z_GDU_Z3950
+        && gdu->u.z3950->which == Z_APDU_searchResponse)
+    {
+        Z_SearchResponse *b_resp = gdu->u.z3950->u.searchResponse;
+        Z_Records *z_records = b_resp->records;
+        if (!z_records || (z_records && z_records->which == Z_Records_DBOSD))
+        {
+            b->m_number_of_sets++;
+            m_sets[resultSetId] = VirtualDB::Set(b, backend_setname);
+            fixup_package(search_package, b);
+        }
+    }
     package.response() = search_package.response();
 }
 
@@ -614,7 +632,6 @@ void yf::VirtualDB::Frontend::present(mp::Package &package, Z_APDU *apdu_req)
     {
         package.response() = present_package.response();
         package.session().close();
-        return;
     }
     else
     {