Use proper recordcharset for opacxml
[yazproxy-moved-to-github.git] / src / yaz-proxy.cpp
index 93aab1c..7f073cc 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: yaz-proxy.cpp,v 1.35 2005-08-15 12:53:08 adam Exp $
+/* $Id: yaz-proxy.cpp,v 1.42 2006-03-09 00:08:29 adam Exp $
    Copyright (c) 1998-2005, Index Data.
 
 This file is part of the yaz-proxy.
@@ -106,18 +106,18 @@ void Auth_Msg::result()
 {
     yaz_log(YLOG_LOG, "Auth_Msg:result proxy ok buf=%p len=%d",
             m_apdu_buf, m_apdu_len);
-    odr_setbuf(m_proxy->odr_decode(), m_apdu_buf, m_apdu_len, 0);
-    Z_APDU *apdu = 0;
-    int r = z_APDU(m_proxy->odr_decode(), &apdu, 0, 0);
-    if (r)
-        yaz_log(YLOG_LOG, "Auth_Msg::result z_APDU OK");
-    else
-        yaz_log(YLOG_LOG, "Auth_Msg::result z_APDU failed");
-    if (m_proxy->dec_ref())
+    if (m_proxy->dec_ref(false))
         yaz_log(YLOG_LOG, "Auth_Msg::proxy deleted meanwhile");
     else
     {
         yaz_log(YLOG_LOG, "Auth_Msg::proxy still alive");
+        odr_setbuf(m_proxy->odr_decode(), m_apdu_buf, m_apdu_len, 0);
+        Z_APDU *apdu = 0;
+        int r = z_APDU(m_proxy->odr_decode(), &apdu, 0, 0);
+        if (r)
+            yaz_log(YLOG_LOG, "Auth_Msg::result z_APDU OK");
+        else
+            yaz_log(YLOG_LOG, "Auth_Msg::result z_APDU failed");
         m_proxy->result_authentication(apdu, m_ret);
     }
     delete this;
@@ -132,7 +132,7 @@ void Yaz_Proxy::result_authentication(Z_APDU *apdu, int ret)
         Z_APDU *apdu_reject = zget_APDU(odr_encode(), Z_APDU_initResponse);
         *apdu_reject->u.initResponse->result = 0;
         send_to_client(apdu_reject);
-        dec_ref();
+        dec_ref(false);
     }
     else
         handle_incoming_Z_PDU_2(apdu);
@@ -219,9 +219,10 @@ Yaz_Proxy::Yaz_Proxy(IPDU_Observable *the_PDU_Observable,
     m_session_no = 0;
     m_bytes_sent = 0;
     m_bytes_recv = 0;
-    m_bw_hold_PDU = 0;
     m_bw_max = 0;
     m_pdu_max = 0;
+    m_search_max = 0;
+    m_connect_max = 0;
     m_timeout_mode = timeout_normal;
     m_timeout_gdu = 0;
     m_max_record_retrieve = 0;
@@ -279,6 +280,7 @@ Yaz_Proxy::Yaz_Proxy(IPDU_Observable *the_PDU_Observable,
         low_socket_open();
     m_my_thread = 0;
     m_ref_count = 1;
+    m_main_ptr_dec = false;
     m_peername = 0;
 }
 
@@ -421,7 +423,7 @@ IPDU_Observer *Yaz_Proxy::sessionNotify(IPDU_Observable
         m_proxy_negotiation_lang);
     // create thread object the first time we get an incoming connection
     if (!m_my_thread)
-        m_my_thread = new Msg_Thread(m_socket_observable);
+        m_my_thread = new Msg_Thread(m_socket_observable, 1);
     new_proxy->m_my_thread = m_my_thread;
     return new_proxy;
 }
@@ -537,6 +539,7 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
             int pre_init = 0;
             cfg->get_target_info(proxy_host, url, &m_bw_max,
                                  &m_pdu_max, &m_max_record_retrieve,
+                                 &m_search_max, &m_connect_max,
                                  &m_target_idletime, &client_idletime,
                                  &parent->m_max_clients,
                                  &m_keepalive_limit_bw,
@@ -1038,7 +1041,7 @@ void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p,
             {
                 WRBUF w = wrbuf_alloc();
 
-                yaz_display_OPAC(w, r->u.opac, 0);
+                yaz_opac_decode_wrbuf(mt, r->u.opac, w);
                 npr->u.databaseRecord = z_ext_record(
                     odr_encode(), VAL_TEXT_XML,
                     wrbuf_buf(w), wrbuf_len(w)
@@ -1105,6 +1108,9 @@ int Yaz_Proxy::send_http_response(int code)
     m_bytes_sent += len;
     m_bw_stat.add_bytes(len);
     logtime();
+
+    recv_GDU_more(true);
+
     return r;
 }
 
@@ -1151,20 +1157,20 @@ int Yaz_Proxy::send_srw_response(Z_SRW_PDU *srw_pdu)
     m_bytes_sent += len;
     m_bw_stat.add_bytes(len);
     logtime();
+
+    recv_GDU_more(true);
+
     return r;
 }
 
 int Yaz_Proxy::send_to_srw_client_error(int srw_error, const char *add)
 {
     ODR o = odr_encode();
-    Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
-    Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
-
-    srw_res->num_diagnostics = 1;
-    srw_res->diagnostics = (Z_SRW_diagnostic *)
-        odr_malloc(o, sizeof(*srw_res->diagnostics));
-    yaz_mk_std_diagnostic(o, srw_res->diagnostics, srw_error, add);
-    return send_srw_response(srw_pdu);
+    Z_SRW_diagnostic *diagnostic = (Z_SRW_diagnostic *)
+        odr_malloc(o, sizeof(*diagnostic));
+    int num_diagnostic = 1;
+    yaz_mk_std_diagnostic(o, diagnostic, srw_error, add);
+    return send_srw_search_response(diagnostic, num_diagnostic);
 }
 
 int Yaz_Proxy::z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
@@ -1241,6 +1247,18 @@ int Yaz_Proxy::send_to_srw_client_ok(int hits, Z_Records *records, int start)
     
 }
 
+int Yaz_Proxy::send_srw_search_response(Z_SRW_diagnostic *diagnostics,
+                                        int num_diagnostics)
+{
+    ODR o = odr_encode();
+    Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
+    Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
+
+    srw_res->num_diagnostics = num_diagnostics;
+    srw_res->diagnostics = diagnostics;
+    return send_srw_response(srw_pdu);
+}
+
 int Yaz_Proxy::send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
                                         int num_diagnostics)
 {
@@ -1797,7 +1815,6 @@ void Yaz_Proxy::recv_GDU_reduce(GDU *gdu)
     /* uncomment to force a big reduce */
     m_timeout_mode = timeout_reduce;
     m_timeout_gdu = gdu;
-    // m_bw_hold_PDU = apdu;  // save PDU and signal "on hold"
     timeout(3);       // call us reduce seconds later
     return;
 #endif    
@@ -1809,7 +1826,6 @@ void Yaz_Proxy::recv_GDU_reduce(GDU *gdu)
         
         m_timeout_mode = timeout_reduce;
         m_timeout_gdu = gdu;
-        // m_bw_hold_PDU = apdu;  // save PDU and signal "on hold"
         timeout(reduce);       // call us reduce seconds later
     }
     else
@@ -1830,8 +1846,8 @@ void Yaz_Proxy::recv_GDU_more(bool normal)
 
 void Yaz_Proxy::recv_GDU_normal(GDU *gdu)
 {
-    Z_GDU *apdu = gdu->get();
-    gdu->extract_odr_to(odr_decode());
+    Z_GDU *apdu = 0;
+    gdu->move_away_gdu(odr_decode(), &apdu);
     delete gdu;
 
     if (apdu->which == Z_GDU_Z3950)
@@ -2013,9 +2029,10 @@ void Yaz_Proxy::handle_charset_lang_negotiation(Z_APDU *apdu)
                 {
                     ODR_MASK_SET(initResponse->options,
                                  Z_Options_negotiationModel);
-                    ODR_MASK_SET(m_initRequest_options,
-                                 Z_Options_negotiationModel);
-
+                    if (m_initRequest_options)
+                        ODR_MASK_SET(m_initRequest_options,
+                                     Z_Options_negotiationModel);
+                    
                     oi->which = Z_OtherInfo_externallyDefinedInfo;    
                     oi->information.externallyDefinedInfo =
                         yaz_set_response_charneg(
@@ -2453,6 +2470,7 @@ int Yaz_Proxy::file_access(Z_HTTP_Request *hreq)
     }
     int len;
     send_GDU(gdu, &len);
+    recv_GDU_more(true);
     return 1;
 }
         
@@ -2606,7 +2624,8 @@ void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
                     const char *pqf_msg;
                     size_t off;
                     int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
-                    yaz_log(YLOG_LOG, "%*s^\n", off+4, "");
+                    int ioff = off;
+                    yaz_log(YLOG_LOG, "%*s^\n", ioff+4, "");
                     yaz_log(YLOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
                     
                     send_to_srw_client_error(10, 0);
@@ -3050,10 +3069,19 @@ void Yaz_Proxy::releaseClient()
         m_parent->pre_init();
 }
 
-bool Yaz_Proxy::dec_ref()
+bool Yaz_Proxy::dec_ref(bool main_ptr)
 {
     yaz_log(YLOG_LOG, "%sdec_ref count=%d", m_session_str, m_ref_count);
+
     assert(m_ref_count > 0);
+    if (main_ptr)
+    {
+        if (m_main_ptr_dec)
+            return false;
+        m_main_ptr_dec = true;
+    }
+
+    m_http_keepalive = 0;
 
     --m_ref_count;
     if (m_ref_count > 0)
@@ -3080,7 +3108,7 @@ void Yaz_ProxyClient::shutdown()
     if (m_server)
     {
         m_waiting = 1;      // ensure it's released from Proxy in releaseClient
-        m_server->dec_ref();
+        m_server->dec_ref(true);
     }
     else
         delete this;
@@ -3091,15 +3119,35 @@ void Yaz_Proxy::failNotify()
     inc_request_no();
     yaz_log (YLOG_LOG, "%sConnection closed by client",
              get_session_str());
-    dec_ref();
+    dec_ref(true);
 }
 
+void Yaz_Proxy::send_response_fail_client(const char *addr)
+{
+    yaz_log(YLOG_LOG, "%ssend_close_response", get_session_str());
+    if (m_http_version)
+    {
+        Z_SRW_diagnostic *diagnostic = 0;
+        int num_diagnostic = 0;
+        
+        yaz_add_srw_diagnostic(odr_encode(),
+                               &diagnostic, &num_diagnostic,
+                               YAZ_SRW_SYSTEM_TEMPORARILY_UNAVAILABLE, addr);
+        if (m_s2z_search_apdu)
+            send_srw_search_response(diagnostic, num_diagnostic);
+        else
+            send_srw_explain_response(diagnostic, num_diagnostic);
+    }            
+}
 void Yaz_ProxyClient::failNotify()
 {
     if (m_server)
         m_server->inc_request_no();
     yaz_log (YLOG_LOG, "%sConnection closed by target %s", 
              get_session_str(), get_hostname());
+
+    if (m_server)
+        m_server->send_response_fail_client(get_hostname());
     shutdown();
 }
 
@@ -3164,7 +3212,7 @@ void Yaz_Proxy::pre_init()
     int i;
     const char *name = 0;
     const char *zurl_in_use[MAX_ZURL_PLEX];
-    int limit_bw, limit_pdu, limit_req;
+    int limit_bw, limit_pdu, limit_req, limit_search, limit_connect;
     int target_idletime, client_idletime;
     int max_clients;
     int keepalive_limit_bw, keepalive_limit_pdu;
@@ -3185,6 +3233,7 @@ void Yaz_Proxy::pre_init()
 
     for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
                                           &limit_bw, &limit_pdu, &limit_req,
+                                          &limit_search, &limit_connect,
                                           &target_idletime, &client_idletime,
                                           &max_clients, 
                                           &keepalive_limit_bw,
@@ -3270,7 +3319,7 @@ void Yaz_Proxy::timeoutNotify()
             inc_request_no();
             m_in_queue.clear();
             yaz_log (YLOG_LOG, "%sTimeout (client to proxy)", m_session_str);
-            dec_ref();
+            dec_ref(true);
             break;
         case timeout_reduce:
             timeout(m_client_idletime);
@@ -3305,17 +3354,12 @@ void Yaz_ProxyClient::timeoutNotify()
 
     yaz_log (YLOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
              get_hostname());
-    m_waiting = 1;
-    m_root->pre_init();
-    if (m_server && m_init_flag)
-    {
-        // target timed out in a session that was properly initialized
-        // server object stay alive but we mark it as invalid so it
-        // gets initialized again
-        m_server->markInvalid();
-        m_server = 0;
-    }
+
+    if (m_server)
+        m_server->send_response_fail_client(get_hostname());
     shutdown();
+
+    m_root->pre_init();
 }
 
 Yaz_ProxyClient::Yaz_ProxyClient(IPDU_Observable *the_PDU_Observable,