Added yp2::PlainFile class which reads Unix-style plain text files.
[metaproxy-moved-to-github.git] / src / util.cpp
index 38fd9e2..4570c31 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: util.cpp,v 1.8 2006-01-17 16:56:48 adam Exp $
+/* $Id: util.cpp,v 1.13 2006-01-20 22:38:12 marc Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -9,8 +9,38 @@
 #include <yaz/odr.h>
 #include <yaz/pquery.h>
 #include <yaz/otherinfo.h>
+#include <yaz/querytowrbuf.h> // for yaz_query_to_wrbuf()
 #include "util.hpp"
 
+//#include <iostream>
+
+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();
@@ -30,6 +60,49 @@ bool yp2::util::pqf(ODR odr, Z_APDU *apdu, const std::string &q) {
     return true;
 }
 
+
+std::string yp2::util::zQueryToString(Z_Query *query)
+{
+    std::string query_str = "";
+
+    if (query && query->which == Z_Query_type_1){
+        Z_RPNQuery *rpn = query->u.type_1;
+        
+        if (rpn){
+            
+            // allocate wrbuf (strings in YAZ!)
+            WRBUF w = wrbuf_alloc();
+            
+            // put query in w
+            yaz_rpnquery_to_wrbuf(w, rpn);
+            
+            // from w to std::string
+            query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
+            
+            // destroy wrbuf
+            wrbuf_free(w, 1);
+        }
+    }
+
+#if 0
+    if (query && query->which == Z_Query_type_1){
+        
+        // allocate wrbuf (strings in YAZ!)
+        WRBUF w = wrbuf_alloc();
+        
+        // put query in w
+        yaz_query_to_wrbuf(w, query);
+        
+        // from w to std::string
+        query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
+        
+        // destroy wrbuf
+        wrbuf_free(w, 1);
+    }    
+#endif
+    return query_str;
+}
+
 void yp2::util::get_default_diag(Z_DefaultDiagFormat *r,
                                  int &error_code, std::string &addinfo)
 {
@@ -98,6 +171,19 @@ int yp2::util::get_vhost_otherinfo(Z_OtherInformation **otherInformation,
     return cat;
 }
 
+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++)
+    {
+        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)
 {
@@ -243,6 +329,7 @@ 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);
@@ -255,23 +342,62 @@ 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;
 }
 
+Z_GDU *yp2::odr::create_HTTP_Response(yp2::Session &session,
+                                      Z_HTTP_Request *hreq, int code)
+{
+    const char *response_version = "1.0";
+    bool keepalive = false;
+    if (!strcmp(hreq->version, "1.0")) 
+    {
+        const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
+        if (v && !strcmp(v, "Keep-Alive"))
+            keepalive = true;
+        else
+            session.close();
+        response_version = "1.0";
+    }
+    else
+    {
+        const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
+        if (v && !strcmp(v, "close"))
+            session.close();
+        else
+            keepalive = true;
+        response_version = "1.1";
+    }
+
+    Z_GDU *gdu = z_get_HTTP_Response(m_odr, code);
+    Z_HTTP_Response *hres = gdu->u.HTTP_Response;
+    hres->version = odr_strdup(m_odr, response_version);
+    if (keepalive)
+        z_HTTP_header_add(m_odr, &hres->headers, "Connection", "Keep-Alive");
+    
+    return gdu;
+}
+
 Z_ReferenceId **yp2::util::get_referenceId(Z_APDU *apdu)
 {
     switch (apdu->which)