X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fyaz-proxy.cpp;h=3a6bb9ee544ca30a3c80da4741136e18d3e28934;hb=f45771ec3032b3ef67ec4c275da242e3c647b53e;hp=d351988109c6d20d21cb25511f7edcb30fcb751c;hpb=b416df50f7dec326e96cd809220ea66a66044e88;p=yazpp-moved-to-github.git diff --git a/src/yaz-proxy.cpp b/src/yaz-proxy.cpp index d351988..3a6bb9e 100644 --- a/src/yaz-proxy.cpp +++ b/src/yaz-proxy.cpp @@ -2,97 +2,7 @@ * Copyright (c) 1998-2001, Index Data. * See the file LICENSE for details. * - * $Log: yaz-proxy.cpp,v $ - * Revision 1.27 2001-11-02 13:06:29 adam - * Fixed bug in result_set_optimize where LSLB == resultCount. - * - * Revision 1.26 2001/08/13 16:39:12 adam - * PDU_Assoc keeps track of children. Using yaz_log instead of logf. - * - * Revision 1.25 2001/04/25 18:59:30 adam - * Added referenceId handling for server. - * - * Revision 1.24 2001/04/10 10:48:08 adam - * Fixed problem where proxy could cash bad result sets. - * - * Revision 1.23 2001/03/26 14:43:49 adam - * New threaded PDU association. - * - * Revision 1.22 2000/11/20 11:27:33 adam - * Fixes for connect operation (timeout and notify fix). - * - * Revision 1.21 2000/11/01 14:22:59 adam - * Added fd parameter for method IYaz_PDU_Observer::clone. - * - * Revision 1.20 2000/10/24 12:29:57 adam - * Fixed bug in proxy where a Yaz_ProxyClient could be owned by - * two Yaz_Proxy's (fatal). - * - * Revision 1.19 2000/10/11 11:58:16 adam - * Moved header files to include/yaz++. Switched to libtool and automake. - * Configure script creates yaz++-config script. - * - * Revision 1.18 2000/09/08 10:23:42 adam - * Added skeleton of yaz-z-server. - * - * Revision 1.17 2000/09/05 13:57:28 adam - * Fixed get_otherInfoAPDU to return otherInfo for extended services. - * - * Revision 1.16 2000/09/04 08:29:22 adam - * Fixed memory leak(s). Added re-use of associations, rather than - * re-init, when maximum number of targets are in use. - * - * Revision 1.15 2000/08/31 14:41:55 adam - * Proxy no longer generates cookies (it's up to the client). Proxy - * re-opens if target new op is started before previous operation finishes. - * - * Revision 1.14 2000/08/10 08:42:42 adam - * Fixes for {set,get}_APDU_log. - * - * Revision 1.13 2000/08/07 14:19:59 adam - * Fixed serious bug regarding timeouts. Improved logging for proxy. - * - * Revision 1.12 2000/07/04 13:48:49 adam - * Implemented upper-limit on proxy-to-target sessions. - * - * Revision 1.11 1999/12/06 13:52:45 adam - * Modified for new location of YAZ header files. Experimental threaded - * operation. - * - * Revision 1.10 1999/11/10 10:02:34 adam - * Work on proxy. - * - * Revision 1.9 1999/09/13 12:53:44 adam - * Proxy removes OtherInfo Proxy Address and Session ID. Other - * Otherinfo remains untouched. - * - * Revision 1.8 1999/05/04 10:53:00 adam - * Changed the way the PROXY behaves when lost cookie is received. - * - * Revision 1.7 1999/04/28 13:31:17 adam - * Better result set optimisation for proxy. - * - * Revision 1.6 1999/04/27 07:52:13 adam - * Improved proxy; added query match for result set re-use. - * - * Revision 1.5 1999/04/21 12:09:01 adam - * Many improvements. Modified to proxy server to work with "sessions" - * based on cookies. - * - * Revision 1.4 1999/04/20 10:30:05 adam - * Implemented various stuff for client and proxy. Updated calls - * to ODR to reflect new name parameter. - * - * Revision 1.3 1999/04/09 11:46:57 adam - * Added object Yaz_Z_Assoc. Much more functional client. - * - * Revision 1.2 1999/01/28 13:08:46 adam - * Yaz_PDU_Assoc better encapsulated. Memory leak fix in - * yaz-socket-manager.cc. - * - * Revision 1.1.1.1 1999/01/28 09:41:07 adam - * First implementation of YAZ++. - * + * $Id: yaz-proxy.cpp,v 1.31 2002-01-14 12:01:28 adam Exp $ */ #include @@ -111,16 +21,20 @@ Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) : m_seqno = 1; m_keepalive = 1; m_proxyTarget = 0; + m_proxy_authentication = 0; m_max_clients = 50; m_seed = time(0); + m_optimize = xstrdup ("1"); } Yaz_Proxy::~Yaz_Proxy() { xfree (m_proxyTarget); + xfree (m_proxy_authentication); + xfree (m_optimize); } -void Yaz_Proxy::set_proxyTarget(const char *target) +void Yaz_Proxy::set_proxy_target(const char *target) { xfree (m_proxyTarget); m_proxyTarget = 0; @@ -128,14 +42,23 @@ void Yaz_Proxy::set_proxyTarget(const char *target) m_proxyTarget = (char *) xstrdup (target); } +void Yaz_Proxy::set_proxy_authentication (const char *auth) +{ + xfree (m_proxy_authentication); + m_proxy_authentication = 0; + if (auth) + m_proxy_authentication = (char *) xstrdup (auth); +} + IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable *the_PDU_Observable, int fd) { Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable); new_proxy->m_parent = this; new_proxy->timeout(500); - new_proxy->set_proxyTarget(m_proxyTarget); + new_proxy->set_proxy_target(m_proxyTarget); new_proxy->set_APDU_log(get_APDU_log()); + new_proxy->set_proxy_authentication(m_proxy_authentication); return new_proxy; } @@ -180,34 +103,28 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu) get_otherInfoAPDU(apdu, &oi); char *cookie = get_cookie(oi); - yaz_log (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie : - "null"); const char *proxy_host = get_proxy(oi); if (proxy_host) - set_proxyTarget(proxy_host); - yaz_log (LOG_LOG, "proxy_host = %s", m_proxyTarget ? m_proxyTarget:"none"); + set_proxy_target(proxy_host); // no target specified at all? if (!m_proxyTarget) return 0; + if (!strcmp(m_proxyTarget, "stop")) + exit (0); if (cookie && *cookie) { - yaz_log (LOG_LOG, "lookup of clients cookie=%s target=%s", - cookie, m_proxyTarget); Yaz_ProxyClient *cc = 0; for (c = parent->m_clientPool; c; c = c->m_next) { - yaz_log (LOG_LOG, " found client cookie = %s target=%s seqno=%d", - c->m_cookie, c->get_hostname(), c->m_seqno); assert (c->m_prev); assert (*c->m_prev == c); if (!strcmp(cookie,c->m_cookie) && !strcmp(m_proxyTarget, c->get_hostname())) { - yaz_log (LOG_LOG, "found!"); cc = c; } } @@ -224,9 +141,8 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu) c->close(); c->client(m_proxyTarget); c->m_init_flag = 0; - - delete c->m_last_query; - c->m_last_query = 0; + + c->m_last_ok = 0; c->m_last_resultCount = 0; c->m_sr_transform = 0; c->m_waiting = 0; @@ -238,7 +154,7 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu) c->m_server = this; c->m_seqno = parent->m_seqno; (parent->m_seqno)++; - yaz_log (LOG_LOG, "get_client 1 %p %p", this, c); + yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c); return c; } } @@ -250,7 +166,23 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu) return 0; } yaz_log (LOG_LOG, "got InitRequest"); - + Z_InitRequest *initRequest = apdu->u.initRequest; + + if (!initRequest->idAuthentication) + { + if (m_proxy_authentication) + { + initRequest->idAuthentication = + (Z_IdAuthentication *) + odr_malloc (odr_encode(), + sizeof(*initRequest->idAuthentication)); + initRequest->idAuthentication->which = + Z_IdAuthentication_open; + initRequest->idAuthentication->u.open = + odr_strdup (odr_encode(), m_proxy_authentication); + } + } + // go through list of clients - and find the lowest/oldest one. Yaz_ProxyClient *c_min = 0; int min_seq = -1; @@ -291,7 +223,7 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu) delete c->m_server; } (parent->m_seqno)++; - yaz_log (LOG_LOG, "get_client 2 %p %p", this, c); + yaz_log (LOG_DEBUG, "get_client 2 %p %p", this, c); return c; } } @@ -315,17 +247,15 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu) c->m_seqno = parent->m_seqno; c->client(m_proxyTarget); c->m_init_flag = 0; - - delete c->m_last_query; - c->m_last_query = 0; c->m_last_resultCount = 0; + c->m_last_ok = 0; c->m_sr_transform = 0; c->m_waiting = 0; c->timeout(10); (parent->m_seqno)++; } - yaz_log (LOG_LOG, "get_client 3 %p %p", this, c); + yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c); return c; } @@ -333,19 +263,27 @@ Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu) { if (apdu->which != Z_APDU_searchRequest) return apdu; + if (*m_parent->m_optimize != '1') + return apdu; Z_SearchRequest *sr = apdu->u.searchRequest; Yaz_Z_Query *this_query = new Yaz_Z_Query; + Yaz_Z_Databases this_databases; + + this_databases.set(sr->num_databaseNames, (const char **) + sr->databaseNames); this_query->set_Z_Query(sr->query); - if (m_client->m_last_query && - m_client->m_last_query->match(this_query)) + if (m_client->m_last_ok && m_client->m_last_query && + m_client->m_last_query->match(this_query) && + m_client->m_last_databases.match(this_databases)) { delete this_query; if (m_client->m_last_resultCount > *sr->smallSetUpperBound && m_client->m_last_resultCount < *sr->largeSetLowerBound) { // medium Set + // send present request (medium size) yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize medium set"); Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest); Z_PresentRequest *pr = new_apdu->u.presentRequest; @@ -366,7 +304,7 @@ Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu) else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound || m_client->m_last_resultCount == 0) { - // large set + // large set. Return pseudo-search response immediately yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize large set"); Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse); new_apdu->u.searchResponse->referenceId = sr->referenceId; @@ -378,6 +316,7 @@ Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu) else { // small set + // send a present request (small set) yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize small set"); Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest); Z_PresentRequest *pr = new_apdu->u.presentRequest; @@ -401,6 +340,9 @@ Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu) yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize new set"); delete m_client->m_last_query; m_client->m_last_query = this_query; + m_client->m_last_ok = 0; + m_client->m_last_databases.set(sr->num_databaseNames, + (const char **) sr->databaseNames); } return apdu; } @@ -459,29 +401,31 @@ void Yaz_Proxy::connectNotify() void Yaz_Proxy::shutdown() { - yaz_log (LOG_LOG, "shutdown (client to proxy)"); // only keep if keep_alive flag and cookie is set... if (m_keepalive && m_client && m_client->m_cookie[0]) { - yaz_log (LOG_LOG, "shutdown - keepalive this=%p, m_server=%p", - this, m_client->m_server); if (m_client->m_waiting == 2) abort(); // Tell client (if any) that no server connection is there.. m_client->m_server = 0; + yaz_log (LOG_LOG, "shutdown (client to proxy) keepalive %s", m_client->get_hostname()); } else if (m_client) { - yaz_log (LOG_LOG, "deleting %p %p", this, m_client); if (m_client->m_waiting == 2) abort(); + yaz_log (LOG_LOG, "shutdown (client to proxy) close %s", m_client->get_hostname()); delete m_client; } else if (!m_parent) { - yaz_log (LOG_LOG, "abort %p", this); + yaz_log (LOG_LOG, "shutdown (client to proxy) bad state"); abort(); } + else + { + yaz_log (LOG_LOG, "shutdown (client to proxy)"); + } delete this; } @@ -494,7 +438,7 @@ void Yaz_ProxyClient::shutdown() void Yaz_Proxy::failNotify() { - yaz_log (LOG_LOG, "connection closed by client"); + yaz_log (LOG_LOG, "Yaz_Proxy connection closed by client"); shutdown(); } @@ -548,10 +492,23 @@ Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) : m_init_flag = 0; m_last_query = 0; m_last_resultCount = 0; + m_last_ok = 0; m_sr_transform = 0; m_waiting = 0; } +const char *Yaz_Proxy::option(const char *name, const char *value) +{ + if (!strcmp (name, "optimize")) { + if (value) { + xfree (m_optimize); + m_optimize = xstrdup (value); + } + return m_optimize; + } + return 0; +} + void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu) { m_waiting = 0; @@ -560,13 +517,10 @@ void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu) { m_last_resultCount = *apdu->u.searchResponse->resultCount; int status = *apdu->u.searchResponse->searchStatus; - if (! status || ( - apdu->u.searchResponse->records && - apdu->u.searchResponse->records->which != Z_Records_DBOSD)) - { - delete m_last_query; - m_last_query = 0; - } + if (status && + (!apdu->u.searchResponse->records || + apdu->u.searchResponse->records->which == Z_Records_DBOSD)) + m_last_ok = 1; } if (apdu->which == Z_APDU_presentResponse && m_sr_transform) { @@ -581,7 +535,7 @@ void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu) sr->numberOfRecordsReturned = pr->numberOfRecordsReturned; apdu = new_apdu; } - if (m_cookie) + if (m_cookie && *m_cookie) set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie); if (m_server) {