Fixed Bug #1044 -- connections should no lomnger be re-used if authent tokens
[pazpar2-moved-to-github.git] / src / logic.c
index 2a39b42..ea6e66f 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: logic.c,v 1.30 2007-05-17 22:56:41 jakub Exp $
+/* $Id: logic.c,v 1.35 2007-06-02 04:32:28 quinn Exp $
    Copyright (c) 2006-2007, Index Data.
 
 This file is part of Pazpar2.
@@ -82,7 +82,8 @@ struct parameters global_parameters =
     "",
     "",
     0,
-    0,
+    0, /* dump_records */
+    0, /* debug_mode */
     30,
     "81",
     "Index Data PazPar2",
@@ -153,48 +154,73 @@ xmlDoc *normalize_record(struct session_database *sdb, Z_External *rec)
 {
     struct database_retrievalmap *m;
     struct database *db = sdb->database;
-    xmlNode *res;
-    xmlDoc *rdoc;
+    xmlDoc *rdoc = 0;
+    const Odr_oid *oid = rec->direct_reference;
 
-    // First normalize to XML
-    if (sdb->yaz_marc)
+    /* convert response record to XML somehow */
+    if (rec->which == Z_External_octet && oid
+        && !oid_oidcmp(oid, yaz_oid_recsyn_xml))
     {
-        char *buf;
-        int len;
-        if (rec->which != Z_External_octet)
+        /* xml already */
+        rdoc = xmlParseMemory((char*) rec->u.octet_aligned->buf,
+                              rec->u.octet_aligned->len);
+        if (!rdoc)
         {
-            yaz_log(YLOG_WARN, "Unexpected external branch, probably BER %s",
+            yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
                     db->url);
             return 0;
         }
-        buf = (char*) rec->u.octet_aligned->buf;
-        len = rec->u.octet_aligned->len;
-        if (yaz_marc_read_iso2709(sdb->yaz_marc, buf, len) < 0)
+    }
+    else if (oid && yaz_oid_is_iso2709(oid))
+    {
+        /* ISO2709 gets converted to MARCXML */
+        if (!sdb->yaz_marc)
         {
-            yaz_log(YLOG_WARN, "Failed to decode MARC %s", db->url);
+            yaz_log(YLOG_FATAL, "Unable to handle ISO2709 record");
             return 0;
         }
-
-        yaz_marc_write_using_libxml2(sdb->yaz_marc, 1);
-        if (yaz_marc_write_xml(sdb->yaz_marc, &res,
-                    "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
+        else
         {
-            yaz_log(YLOG_WARN, "Failed to encode as XML %s",
-                    db->url);
-            return 0;
+            xmlNode *res;
+            char *buf;
+            int len;
+            
+            if (rec->which != Z_External_octet)
+            {
+                yaz_log(YLOG_WARN, "Unexpected external branch, probably BER %s",
+                        db->url);
+                return 0;
+            }
+            buf = (char*) rec->u.octet_aligned->buf;
+            len = rec->u.octet_aligned->len;
+            if (yaz_marc_read_iso2709(sdb->yaz_marc, buf, len) < 0)
+            {
+                yaz_log(YLOG_WARN, "Failed to decode MARC %s", db->url);
+                return 0;
+            }
+            
+            yaz_marc_write_using_libxml2(sdb->yaz_marc, 1);
+            if (yaz_marc_write_xml(sdb->yaz_marc, &res,
+                                   "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
+            {
+                yaz_log(YLOG_WARN, "Failed to encode as XML %s",
+                        db->url);
+                return 0;
+            }
+            rdoc = xmlNewDoc((xmlChar *) "1.0");
+            xmlDocSetRootElement(rdoc, res);
         }
-        rdoc = xmlNewDoc((xmlChar *) "1.0");
-        xmlDocSetRootElement(rdoc, res);
-
     }
     else
     {
+        char oid_name_buf[OID_STR_MAX];
+        const char *oid_name = yaz_oid_to_string_buf(oid, 0, oid_name_buf);
         yaz_log(YLOG_FATAL, 
-                "Unknown native_syntax in normalize_record from %s",
-                db->url);
+                "Unable to handle record of type %s from %s", 
+                oid_name, db->url);
         return 0;
     }
-
+    
     if (global_parameters.dump_records){
         fprintf(stderr, 
                 "Input Record (normalized) from %s\n----------------\n",
@@ -476,9 +502,7 @@ char *search(struct session *se, char *query, char *filter)
     }
 
     for (cl = se->clients; cl; cl = client_next_in_session(cl))
-    {
         client_prep_connection(cl);
-    }
 
     return 0;
 }
@@ -493,7 +517,16 @@ static void session_init_databases_fun(void *context, struct database *db)
 
     new->database = db;
     new->yaz_marc = 0;
-    new->pct = pp2_charset_create();
+    
+#ifdef HAVE_ICU
+    if (global_parameters.server && global_parameters.server->icu_chn)
+        new->pct = pp2_charset_create(global_parameters.server->icu_chn);
+    else
+        new->pct = pp2_charset_create(0);
+#else // HAVE_ICU
+    new->pct = pp2_charset_create(0);
+#endif // HAVE_ICU
+
     new->map = 0;
     new->settings 
         = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);