Bug fix: Handle the case where an APDU can be decoded but not encoded.
[yazproxy-moved-to-github.git] / src / yaz-proxy.cpp
index 14c2f5e..0a68075 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: yaz-proxy.cpp,v 1.57 2006-04-13 00:43:56 adam Exp $
+/* $Id: yaz-proxy.cpp,v 1.60 2006-04-17 07:38:21 adam Exp $
    Copyright (c) 1998-2006, Index Data.
 
 This file is part of the yazproxy.
@@ -438,6 +438,7 @@ IPDU_Observer *Yaz_Proxy::sessionNotify(IPDU_Observable
     else
         sprintf(session_str, "%ld:%d %d ",
                 (long) time(0), m_session_no, 0);
+    m_session_no++;
 
     yaz_log (YLOG_LOG, "%sNew session %s", session_str, peername);
 
@@ -1847,6 +1848,25 @@ void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
         yaz_log (YLOG_LOG, "%sReceiving %s from client %d bytes",
                  m_session_str, gdu_name(apdu), len);
 
+#if 0
+    // try to make a _bad_ attribute set ID .. Don't enable this in prod.
+    if (apdu->which == Z_GDU_Z3950 
+        && apdu->u.z3950->which == Z_APDU_searchRequest)
+    {
+        Z_SearchRequest *req = apdu->u.z3950->u.searchRequest;
+        if (req->query && req->query->which == Z_Query_type_1)
+        {
+            Z_RPNQuery *rpnquery = req->query->u.type_1;
+            if (rpnquery->attributeSetId)
+            {
+                rpnquery->attributeSetId[0] = -2;
+                rpnquery->attributeSetId[1] = -1;
+                yaz_log(YLOG_WARN, "%sBAD FIXUP TEST", m_session_str);
+            }
+        }
+    }
+#endif
+
 #if HAVE_GETTIMEOFDAY
     gettimeofday((struct timeval *) m_time_tv, 0);
 #endif
@@ -1854,8 +1874,16 @@ void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
     m_pdu_stat.add_bytes(1);
 
     GDU *gdu = new GDU(apdu);
-    m_in_queue.enqueue(gdu);
 
+    if (gdu->get() == 0)
+    {
+        delete gdu;
+        yaz_log(YLOG_LOG, "%sUnable to encode package", m_session_str);
+        m_in_queue.clear();
+        dec_ref(true);
+        return;
+    }
+    m_in_queue.enqueue(gdu);
     recv_GDU_more(false);
 }
 
@@ -1981,7 +2009,10 @@ void Yaz_Proxy::recv_GDU_more(bool normal)
     while (m_timeout_mode == timeout_normal && (g = m_in_queue.dequeue()))
     {
         m_timeout_mode = timeout_busy;
+        inc_ref();
         recv_GDU_reduce(g);
+        if (dec_ref(false))
+            break;
     }
 }
 
@@ -3241,6 +3272,7 @@ void Yaz_Proxy::releaseClient()
 
 bool Yaz_Proxy::dec_ref(bool main_ptr)
 {
+    main_ptr = false;
     assert(m_ref_count > 0);
     if (main_ptr)
     {
@@ -3275,7 +3307,7 @@ void Yaz_ProxyClient::shutdown()
 
     if (m_server)
     {
-        m_waiting = 1;      // ensure it's released from Proxy in releaseClient
+        m_waiting = 1;   // ensure it's released from Yaz_Proxy::releaseClient
         m_server->dec_ref(true);
     }
     else
@@ -3285,8 +3317,7 @@ void Yaz_ProxyClient::shutdown()
 void Yaz_Proxy::failNotify()
 {
     inc_request_no();
-    yaz_log (YLOG_LOG, "%sConnection closed by client",
-             get_session_str());
+    yaz_log (YLOG_LOG, "%sConnection closed by client", get_session_str());
     dec_ref(true);
 }