session_shared: Simplify ttl conditional
[metaproxy-moved-to-github.git] / src / filter_zoom.cpp
index 45ead65..0d5349a 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Metaproxy.
-   Copyright (C) 2005-2011 Index Data
+   Copyright (C) 2005-2012 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
@@ -92,6 +92,7 @@ namespace metaproxy_1 {
             SearchablePtr sptr;
             xsltStylesheetPtr xsp;
             std::string content_session_id;
+            bool enable_cproxy;
         public:
             Backend(SearchablePtr sptr);
             ~Backend();
@@ -129,7 +130,8 @@ namespace metaproxy_1 {
                                         char **addinfo,
                                         ODR odr,
                                         std::string authentication,
-                                        std::string proxy);
+                                        std::string proxy,
+                                        std::string realm);
             
             void prepare_elements(BackendPtr b,
                                   Odr_oid *preferredRecordSyntax,
@@ -220,6 +222,7 @@ yf::Zoom::Backend::Backend(SearchablePtr ptr) : sptr(ptr)
     ZOOM_connection_save_apdu_wrbuf(m_connection, m_apdu_wrbuf);
     m_resultset = 0;
     xsp = 0;
+    enable_cproxy = true;
 }
 
 yf::Zoom::Backend::~Backend()
@@ -228,6 +231,7 @@ yf::Zoom::Backend::~Backend()
         xsltFreeStylesheet(xsp);
     ZOOM_connection_destroy(m_connection);
     ZOOM_resultset_destroy(m_resultset);
+    wrbuf_destroy(m_apdu_wrbuf);
 }
 
 
@@ -275,6 +279,7 @@ void yf::Zoom::Backend::connect(std::string zurl,
 void yf::Zoom::Backend::search(ZOOM_query q, Odr_int *hits,
                                int *error, char **addinfo, ODR odr)
 {
+    ZOOM_resultset_destroy(m_resultset);
     m_resultset = ZOOM_connection_search(m_connection, q);
     get_zoom_error(error, addinfo, odr);
     if (*error == 0)
@@ -665,7 +670,8 @@ bool yf::Zoom::Frontend::create_content_session(mp::Package &package,
                                                 int *error, char **addinfo,
                                                 ODR odr,
                                                 std::string authentication,
-                                                std::string proxy)
+                                                std::string proxy,
+                                                std::string realm)
 {
     if (b->sptr->contentConnector.length())
     {
@@ -699,11 +705,14 @@ bool yf::Zoom::Frontend::create_content_session(mp::Package &package,
             wrbuf_printf(w, "auth: %s\n", authentication.c_str());
         if (proxy.length())
             wrbuf_printf(w, "proxy: %s\n", proxy.c_str());
+        if (realm.length())
+            wrbuf_printf(w, "realm: %s\n", realm.c_str());
 
         fwrite(wrbuf_buf(w), 1, wrbuf_len(w), file);
         fclose(file);
         package.log("zoom", YLOG_LOG, "content file: %s", fname);
         xfree(fname);
+        wrbuf_destroy(w);
     }
     return true;
 }
@@ -738,6 +747,7 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
     const char *param_password = 0;
     const char *param_content_user = 0;
     const char *param_content_password = 0;
+    const char *param_nocproxy = 0;
     int no_parms = 0;
 
     char **names;
@@ -771,6 +781,8 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
             param_content_password = value;
         else if (!strcmp(name, "content-proxy"))
             content_proxy = value;
+        else if (!strcmp(name, "nocproxy"))
+            param_nocproxy = value;
         else if (!strcmp(name, "proxy"))
         {
             char **dstr;
@@ -947,6 +959,7 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
 
     b->xsp = xsp;
     b->m_frontend_database = database;
+    b->enable_cproxy = param_nocproxy ? false : true;
 
     if (sptr->query_encoding.length())
         b->set_option("rpnCharset", sptr->query_encoding);
@@ -1001,6 +1014,11 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
             out_names[no_out_args] = "subdatabase";
             out_values[no_out_args++] = odr_strdup(odr, sptr->cfSubDB.c_str());
         }
+        if (param_nocproxy)
+        {
+            out_names[no_out_args] = "nocproxy";
+            out_values[no_out_args++] = odr_strdup(odr, param_nocproxy);
+        }
     }
     else
     {
@@ -1044,11 +1062,12 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
     }
     package.log("zoom", YLOG_LOG, "url: %s", url.c_str());
     b->connect(url, error, addinfo, odr);
-    if (*error == 0)
+    if (*error == 0 && b->enable_cproxy)
         create_content_session(package, b, error, addinfo, odr,
                                content_authentication.length() ?
                                content_authentication : authentication,
-                               content_proxy.length() ? content_proxy : proxy);
+                               content_proxy.length() ? content_proxy : proxy,
+                               realm);
     if (*error == 0)
         m_backend = b;
     return b;
@@ -1174,8 +1193,6 @@ Z_Records *yf::Zoom::Frontend::get_records(Package &package,
             Z_NamePlusRecord *npr = 0;
             const char *addinfo;
 
-            package.log("zoom", YLOG_LOG, "Inspecting record at position %d",
-                        start + i);
             int sur_error = ZOOM_record_error(recs[i], 0 /* msg */,
                                               &addinfo, 0 /* diagset */);
                 
@@ -1266,7 +1283,7 @@ Z_Records *yf::Zoom::Frontend::get_records(Package &package,
                     }
                 }
 
-                if (rec_buf)
+                if (rec_buf && b->enable_cproxy)
                 {
                     xmlDoc *doc = xmlParseMemory(rec_buf, rec_len);
                     std::string res = 
@@ -1497,59 +1514,11 @@ next_proxy:
             log_diagnostic(package, error, addinfo);
             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
             package.response() = apdu_res;
+            cql_parser_destroy(cp);
             return;
         }
         char ccl_buf[1024];
-
         r = cql_to_ccl_buf(cn, ccl_buf, sizeof(ccl_buf));
-        if (r == 0)
-        {
-            ccl_wrbuf = wrbuf_alloc();
-            wrbuf_puts(ccl_wrbuf, ccl_buf);
-            
-            WRBUF sru_sortkeys_wrbuf = wrbuf_alloc();
-
-            cql_sortby_to_sortkeys(cn, wrbuf_vp_puts, sru_sortkeys_wrbuf);
-            WRBUF sort_spec_wrbuf = wrbuf_alloc();
-            yaz_srw_sortkeys_to_sort_spec(wrbuf_cstr(sru_sortkeys_wrbuf),
-                                          sort_spec_wrbuf);
-            wrbuf_destroy(sru_sortkeys_wrbuf);
-
-            yaz_tok_cfg_t tc = yaz_tok_cfg_create();
-            yaz_tok_parse_t tp =
-                yaz_tok_parse_buf(tc, wrbuf_cstr(sort_spec_wrbuf));
-            yaz_tok_cfg_destroy(tc);
-
-            /* go through sortspec and map fields */
-            int token = yaz_tok_move(tp);
-            while (token != YAZ_TOK_EOF)
-            {
-                if (token == YAZ_TOK_STRING)
-                {
-                    const char *field = yaz_tok_parse_string(tp);
-                    std::map<std::string,std::string>::iterator it;
-                    it = b->sptr->sortmap.find(field);
-                    if (it != b->sptr->sortmap.end())
-                        sortkeys += it->second;
-                    else
-                        sortkeys += field;
-                }
-                sortkeys += " ";
-                token = yaz_tok_move(tp);
-                if (token == YAZ_TOK_STRING)
-                {
-                    sortkeys += yaz_tok_parse_string(tp);
-                }
-                if (token != YAZ_TOK_EOF)
-                {
-                    sortkeys += " ";
-                    token = yaz_tok_move(tp);
-                }
-            }
-            yaz_tok_parse_destroy(tp);
-            wrbuf_destroy(sort_spec_wrbuf);
-        }
-        cql_parser_destroy(cp);
         if (r)
         {
             error = YAZ_BIB1_MALFORMED_QUERY;
@@ -1558,8 +1527,66 @@ next_proxy:
             log_diagnostic(package, error, addinfo);
             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
             package.response() = apdu_res;
+            cql_parser_destroy(cp);
             return;
         }
+
+        WRBUF sru_sortkeys_wrbuf = wrbuf_alloc();
+        if (cql_sortby_to_sortkeys(cn, wrbuf_vp_puts, sru_sortkeys_wrbuf))
+        {
+            error = YAZ_BIB1_ILLEGAL_SORT_RELATION;
+            const char *addinfo = "CQL to CCL sortby conversion";
+
+            log_diagnostic(package, error, addinfo);
+            apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
+            package.response() = apdu_res;
+            wrbuf_destroy(sru_sortkeys_wrbuf);
+            cql_parser_destroy(cp);
+            return;
+        }
+        WRBUF sort_spec_wrbuf = wrbuf_alloc();
+        yaz_srw_sortkeys_to_sort_spec(wrbuf_cstr(sru_sortkeys_wrbuf),
+                                      sort_spec_wrbuf);
+        wrbuf_destroy(sru_sortkeys_wrbuf);
+
+        ccl_wrbuf = wrbuf_alloc();
+        wrbuf_puts(ccl_wrbuf, ccl_buf);
+        
+        yaz_tok_cfg_t tc = yaz_tok_cfg_create();
+        yaz_tok_parse_t tp =
+            yaz_tok_parse_buf(tc, wrbuf_cstr(sort_spec_wrbuf));
+        yaz_tok_cfg_destroy(tc);
+        
+        /* go through sortspec and map fields */
+        int token = yaz_tok_move(tp);
+        while (token != YAZ_TOK_EOF)
+        {
+            if (token == YAZ_TOK_STRING)
+            {
+                const char *field = yaz_tok_parse_string(tp);
+                std::map<std::string,std::string>::iterator it;
+                it = b->sptr->sortmap.find(field);
+                if (it != b->sptr->sortmap.end())
+                    sortkeys += it->second;
+                else
+                    sortkeys += field;
+            }
+            sortkeys += " ";
+            token = yaz_tok_move(tp);
+            if (token == YAZ_TOK_STRING)
+            {
+                sortkeys += yaz_tok_parse_string(tp);
+            }
+            if (token != YAZ_TOK_EOF)
+            {
+                sortkeys += " ";
+                token = yaz_tok_move(tp);
+            }
+        }
+        yaz_tok_parse_destroy(tp);
+        wrbuf_destroy(sort_spec_wrbuf);
+
+        cql_parser_destroy(cp);
     }
     else
     {