z3950_client: fixup addinfo for init response
authorAdam Dickmeiss <adam@indexdata.dk>
Mon, 30 Apr 2012 09:38:35 +0000 (11:38 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Mon, 30 Apr 2012 09:38:35 +0000 (11:38 +0200)
src/filter_z3950_client.cpp

index fe3049e..60b9390 100644 (file)
@@ -56,6 +56,7 @@ namespace metaproxy_1 {
             void timeoutNotify();
             void recv_GDU(Z_GDU *gdu, int len);
             void fixup_nsd(ODR odr, Z_Records *records);
+            void fixup_init(ODR odr, Z_InitResponse *initrs);
             yazpp_1::IPDU_Observer* sessionNotify(
                 yazpp_1::IPDU_Observable *the_PDU_Observable,
                 int fd);
@@ -178,10 +179,10 @@ void yf::Z3950Client::Assoc::fixup_nsd(ODR odr, Z_Records *records)
         
         // should really check for nsd->which.. But union has two members
         // containing almost same data
-        //  char *v2Addinfo;
+        const char *v2Addinfo = nsd->u.v2Addinfo;
        //  Z_InternationalString *v3Addinfo;
 
-        if (nsd->u.v2Addinfo)
+        if (v2Addinfo && *v2Addinfo)
         {
             addinfo.assign(nsd->u.v2Addinfo);
             addinfo += " ";
@@ -191,6 +192,53 @@ void yf::Z3950Client::Assoc::fixup_nsd(ODR odr, Z_Records *records)
     }
 }
 
+void yf::Z3950Client::Assoc::fixup_init(ODR odr, Z_InitResponse *initrs)
+{
+    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 j;
+                for (j = 0; j < diag->num; j++)
+                {
+                    Z_DiagnosticFormat_s *ds = diag->elements[j];
+                    if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
+                    {
+                        Z_DefaultDiagFormat *r = ds->u.defaultDiagRec;
+                        char *oaddinfo = r->u.v2Addinfo;
+                        char *naddinfo = (char *) odr_malloc(
+                            odr,
+                            (oaddinfo ? strlen(oaddinfo) : 0) + 20 +
+                            m_host.length());
+                        if (oaddinfo && *oaddinfo)
+                        {
+                            strcpy(naddinfo, oaddinfo);
+                            strcat(naddinfo, " ");
+                        }
+                        strcat(naddinfo, "(backend=");
+                        strcat(naddinfo, m_host.c_str());
+                        strcat(naddinfo, ")");
+
+                        r->u.v2Addinfo = naddinfo;
+                    }
+                }
+            } 
+        }
+    }
+}
+
 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
 {
     m_waiting = false;
@@ -209,6 +257,9 @@ void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
             case Z_APDU_presentResponse:
                 fixup_nsd(odr, apdu->u.presentResponse->records);
                 break;
+            case Z_APDU_initResponse:
+                fixup_init(odr, apdu->u.initResponse);
+                break;
             }
         }
         m_package->response() = gdu;