added first version of query-rewrite filter, only empty shell. functionality missing
[metaproxy-moved-to-github.git] / src / util.cpp
index da9eb53..d745065 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: util.cpp,v 1.5 2006-01-16 15:51:56 adam Exp $
+/* $Id: util.cpp,v 1.11 2006-01-18 14:10:47 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -8,8 +8,35 @@
 
 #include <yaz/odr.h>
 #include <yaz/pquery.h>
+#include <yaz/otherinfo.h>
 #include "util.hpp"
 
+void yp2::util::piggyback(int smallSetUpperBound,
+                          int largeSetLowerBound,
+                          int mediumSetPresentNumber,
+                          int result_set_size,
+                          int &number_to_present)
+{
+    // deal with piggyback
+
+    if (result_set_size < smallSetUpperBound)
+    {
+        // small set . Return all records in set
+        number_to_present = result_set_size;
+    }
+    else if (result_set_size > largeSetLowerBound)
+    {
+        // large set . Return no records
+        number_to_present = 0;
+    }
+    else
+    {
+        // medium set . Return mediumSetPresentNumber records
+        number_to_present = mediumSetPresentNumber;
+        if (number_to_present > result_set_size)
+            number_to_present = result_set_size;
+    }
+}
 
 bool yp2::util::pqf(ODR odr, Z_APDU *apdu, const std::string &q) {
     YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
@@ -29,42 +56,133 @@ bool yp2::util::pqf(ODR odr, Z_APDU *apdu, const std::string &q) {
     return true;
 }
 
+void yp2::util::get_default_diag(Z_DefaultDiagFormat *r,
+                                 int &error_code, std::string &addinfo)
+{
+    error_code = *r->condition;
+    switch (r->which)
+    {
+    case Z_DefaultDiagFormat_v2Addinfo:
+        addinfo = std::string(r->u.v2Addinfo);
+        break;
+    case Z_DefaultDiagFormat_v3Addinfo:
+        addinfo = r->u.v3Addinfo;
+        break;
+    }
+}
 
-bool yp2::util::set_databases_from_zurl(ODR odr, std::string zurl,
-                                        int *db_num, char ***db_strings)
+void yp2::util::get_init_diagnostics(Z_InitResponse *initrs,
+                                     int &error_code, std::string &addinfo)
 {
-    const char *sep = strchr(zurl.c_str(), '/');
-    if (!sep)
-        return false;
+    Z_External *uif = initrs->userInformationField;
+    
+    if (uif && uif->which == Z_External_userInfo1)
+    {
+        Z_OtherInformation *ui = uif->u.userInfo1;
+        int i;
+        for (i = 0; i < ui->num_elements; i++)
+        {
+            Z_OtherInformationUnit *unit = ui->list[i];
+            if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
+                unit->information.externallyDefinedInfo &&
+                unit->information.externallyDefinedInfo->which ==
+                Z_External_diag1) 
+            {
+                Z_DiagnosticFormat *diag = 
+                    unit->information.externallyDefinedInfo->u.diag1;
 
-    int num = 0;
-    const char *cp1 = sep+1;
-    while(1)
+                if (diag->num > 0)
+                {
+                    Z_DiagnosticFormat_s *ds = diag->elements[0];
+                    if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
+                        yp2::util::get_default_diag(ds->u.defaultDiagRec,
+                                                    error_code, addinfo);
+                }
+            } 
+        }
+    }
+}
+
+int yp2::util::get_vhost_otherinfo(Z_OtherInformation **otherInformation,
+                                   bool remove_flag,
+                                   std::list<std::string> &vhosts)
+{
+    int cat;
+    for (cat = 1; ; cat++)
     {
-        const char *cp2 = strchr(cp1, '+');
-        if (!cp2)
+        // check virtual host
+        const char *vhost =
+            yaz_oi_get_string_oidval(otherInformation,
+                                     VAL_PROXY, 
+                                     cat /* categoryValue */,
+                                     remove_flag /* delete flag */);
+        if (!vhost)
             break;
-        cp1 = cp2+1;
-        num++;
+        vhosts.push_back(std::string(vhost));
     }
-    *db_num = num+1;
-    *db_strings = (char **) odr_malloc(odr, sizeof(char*) * (*db_num));
+    --cat;
+    return cat;
+}
 
-    num = 0;
-    cp1 = sep+1;
-    while(1)
+void yp2::util::set_vhost_otherinfo(Z_OtherInformation **otherInformation,
+                                    ODR odr,
+                                    const std::list<std::string> &vhosts)
+{
+    int cat;
+    std::list<std::string>::const_iterator it = vhosts.begin();
+    for (cat = 1; it != vhosts.end() ; cat++, it++)
     {
-        const char *cp2 = strchr(cp1, '+');
-        if (cp2)
-            (*db_strings)[num] = odr_strdupn(odr, cp1, cp2-cp1-1);
-        else
+        yaz_oi_set_string_oidval(otherInformation, odr,
+                                 VAL_PROXY, cat, it->c_str());
+    }
+}
+
+void yp2::util::split_zurl(std::string zurl, std::string &host,
+                           std::list<std::string> &db)
+{
+    const char *zurl_cstr = zurl.c_str();
+    const char *sep = strchr(zurl_cstr, '/');
+    
+    if (sep)
+    {
+        host = std::string(zurl_cstr, sep - zurl_cstr);
+
+        const char *cp1 = sep+1;
+        while(1)
         {
-            (*db_strings)[num] = odr_strdup(odr, cp1);
-            break;
+            const char *cp2 = strchr(cp1, '+');
+            if (cp2)
+                db.push_back(std::string(cp1, cp2 - cp1));
+            else
+            {
+                db.push_back(std::string(cp1));
+                break;
+            }
+            cp1 = cp2+1;
         }
-        cp1 = cp2+1;
-        num++;
     }
+    else
+    {
+        host = zurl;
+    }
+}
+
+bool yp2::util::set_databases_from_zurl(ODR odr, std::string zurl,
+                                        int *db_num, char ***db_strings)
+{
+    std::string host;
+    std::list<std::string> dblist;
+
+    split_zurl(zurl, host, dblist);
+   
+    if (dblist.size() == 0)
+        return false;
+    *db_num = dblist.size();
+    *db_strings = (char **) odr_malloc(odr, sizeof(char*) * (*db_num));
+
+    std::list<std::string>::const_iterator it = dblist.begin();
+    for (int i = 0; it != dblist.end(); it++, i++)
+        (*db_strings)[i] = odr_strdup(odr, it->c_str());
     return true;
 }
 
@@ -164,9 +282,11 @@ Z_APDU *yp2::odr::create_presentResponse(Z_APDU *in_apdu,
     {
         Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
         apdu->u.presentResponse->records = rec;
+        
         rec->which = Z_Records_NSD;
         rec->u.nonSurrogateDiagnostic =
             zget_DefaultDiagFormat(m_odr, error, addinfo);
+        *apdu->u.presentResponse->presentStatus = Z_PresentStatus_failure;
     }
     return apdu;
 }
@@ -175,20 +295,26 @@ Z_APDU *yp2::odr::create_scanResponse(Z_APDU *in_apdu,
                                       int error, const char *addinfo)
 {
     Z_APDU *apdu = create_APDU(Z_APDU_scanResponse, in_apdu);
+    Z_ScanResponse *res = apdu->u.scanResponse;
+    res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
+    res->entries->num_entries = 0;
+    res->entries->entries = 0;
+
     if (error)
     {
-        Z_ScanResponse *res = apdu->u.scanResponse;
-        res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
         *res->scanStatus = Z_Scan_failure;
 
-        res->entries->num_entries = 0;
-        res->entries->entries = 0;
         res->entries->num_nonsurrogateDiagnostics = 1;
         res->entries->nonsurrogateDiagnostics = (Z_DiagRec **)
             odr_malloc(m_odr, sizeof(Z_DiagRec *));
         res->entries->nonsurrogateDiagnostics[0] = 
             zget_DiagRec(m_odr, error, addinfo);
     }
+    else
+    {
+        res->entries->num_nonsurrogateDiagnostics = 0;
+        res->entries->nonsurrogateDiagnostics = 0;
+    }
     return apdu;
 }