X-Git-Url: http://sru.miketaylor.org.uk/?a=blobdiff_plain;f=src%2Fyaz-proxy.cpp;h=96054a3afe121566f48efe61c10914c4445bbc6b;hb=79a9e68c4b373be7384552c58110f9ae28656b49;hp=1390f96322e56fcf35ff33b6e4130698a21461bb;hpb=02873e733a979cc98c13b24c934a8082adf27812;p=yazpp-moved-to-github.git diff --git a/src/yaz-proxy.cpp b/src/yaz-proxy.cpp index 1390f96..96054a3 100644 --- a/src/yaz-proxy.cpp +++ b/src/yaz-proxy.cpp @@ -2,7 +2,7 @@ * Copyright (c) 1998-2003, Index Data. * See the file LICENSE for details. * - * $Id: yaz-proxy.cpp,v 1.49 2003-10-03 13:01:42 adam Exp $ + * $Id: yaz-proxy.cpp,v 1.53 2003-10-08 09:49:05 adam Exp $ */ #include @@ -158,6 +158,35 @@ char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo) return 0; } +const char *Yaz_Proxy::load_balance(const char **url) +{ + int zurl_in_use[MAX_ZURL_PLEX]; + Yaz_ProxyClient *c; + int i; + + for (i = 0; im_clientPool; c; c = c->m_next) + { + for (i = 0; url[i]; i++) + if (!strcmp(url[i], c->get_hostname())) + zurl_in_use[i]++; + } + int min = 100000; + const char *ret = 0; + for (i = 0; url[i]; i++) + { + yaz_log(LOG_DEBUG, "%s zurl=%s use=%d", + m_session_str, url[i], zurl_in_use[i]); + if (min > zurl_in_use[i]) + { + ret = url[i]; + min = zurl_in_use[i]; + } + } + return ret; +} + Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu) { assert (m_parent); @@ -170,17 +199,17 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu) if (!m_proxyTarget) { + const char *url[MAX_ZURL_PLEX]; const char *proxy_host = get_proxy(oi); - if (!proxy_host) + if (proxy_host) { xfree(m_default_target); m_default_target = xstrdup(proxy_host); proxy_host = m_default_target; } - const char *url = 0; int client_idletime = -1; - m_config.get_target_info(proxy_host, &url, &m_keepalive, &m_bw_max, + m_config.get_target_info(proxy_host, url, &m_keepalive, &m_bw_max, &m_pdu_max, &m_max_record_retrieve, &m_target_idletime, &client_idletime, &parent->m_max_clients); @@ -189,12 +218,18 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu) m_client_idletime = client_idletime; timeout(m_client_idletime); } - if (!url) + if (!url[0]) { yaz_log(LOG_LOG, "%s No default target", m_session_str); return 0; } - m_proxyTarget = (char*) xstrdup(url); + // we don't handle multiplexing for cookie session, so we just + // pick the first one in this case (anonymous users will be able + // to use any backend) + if (cookie && *cookie) + m_proxyTarget = (char*) xstrdup(url[0]); + else + m_proxyTarget = (char*) xstrdup(load_balance(url)); } if (cookie && *cookie) { @@ -302,10 +337,10 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu) int min_seq = -1; int no_of_clients = 0; if (parent->m_clientPool) - yaz_log (LOG_LOG, "Existing sessions"); + yaz_log (LOG_DEBUG, "Existing sessions"); for (c = parent->m_clientPool; c; c = c->m_next) { - yaz_log (LOG_LOG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno, + yaz_log (LOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno, c->m_waiting, c->get_hostname(), c->m_cookie ? c->m_cookie : ""); no_of_clients++; @@ -658,7 +693,7 @@ Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu) } } } - else + else // query doesn't match { delete m_client->m_last_query; m_client->m_last_query = this_query; @@ -769,13 +804,11 @@ Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu) if (err) { Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse); - int *nulint = odr_intdup (odr_encode(), 0); new_apdu->u.searchResponse->referenceId = sr->referenceId; new_apdu->u.searchResponse->records = create_nonSurrogateDiagnostics(odr_encode(), err, addinfo); - new_apdu->u.searchResponse->searchStatus = nulint; - new_apdu->u.searchResponse->resultCount = nulint; + *new_apdu->u.searchResponse->searchStatus = 0; send_to_client(new_apdu); @@ -785,6 +818,59 @@ Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu) return apdu; } +Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu) +{ + if (apdu->which == Z_APDU_searchRequest) + { + Z_SearchRequest *sr = apdu->u.searchRequest; + if (*sr->smallSetUpperBound > 0 || *sr->largeSetLowerBound > 1) + { + int err; + char *addinfo = 0; + err = m_config.check_syntax(odr_encode(), m_default_target, + sr->preferredRecordSyntax, + &addinfo); + if (err) + { + Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse); + + new_apdu->u.searchResponse->referenceId = sr->referenceId; + new_apdu->u.searchResponse->records = + create_nonSurrogateDiagnostics(odr_encode(), err, addinfo); + *new_apdu->u.searchResponse->searchStatus = 0; + + send_to_client(new_apdu); + + return 0; + } + } + } + else if (apdu->which == Z_APDU_presentRequest) + { + Z_PresentRequest *pr = apdu->u.presentRequest; + int err; + char *addinfo = 0; + err = m_config.check_syntax(odr_encode(), m_default_target, + pr->preferredRecordSyntax, + &addinfo); + if (err) + { + Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse); + + new_apdu->u.presentResponse->referenceId = pr->referenceId; + new_apdu->u.presentResponse->records = + create_nonSurrogateDiagnostics(odr_encode(), err, addinfo); + *new_apdu->u.presentResponse->presentStatus = + Z_PresentStatus_failure; + + send_to_client(new_apdu); + + return 0; + } + } + return apdu; +} + void Yaz_Proxy::recv_Z_PDU_0(Z_APDU *apdu) { // Determine our client. @@ -798,6 +884,15 @@ void Yaz_Proxy::recv_Z_PDU_0(Z_APDU *apdu) if (apdu->which == Z_APDU_initRequest) { + if (apdu->u.initRequest->implementationId) + yaz_log(LOG_LOG, "%s implementationId: %s", + m_session_str, apdu->u.initRequest->implementationId); + if (apdu->u.initRequest->implementationName) + yaz_log(LOG_LOG, "%s implementationName: %s", + m_session_str, apdu->u.initRequest->implementationName); + if (apdu->u.initRequest->implementationVersion) + yaz_log(LOG_LOG, "%s implementationVersion: %s", + m_session_str, apdu->u.initRequest->implementationVersion); if (m_client->m_init_flag) { Z_APDU *apdu = m_client->m_initResponse; @@ -813,6 +908,9 @@ void Yaz_Proxy::recv_Z_PDU_0(Z_APDU *apdu) handle_max_record_retrieve(apdu); if (apdu) + apdu = handle_syntax_validation(apdu); + + if (apdu) apdu = handle_query_validation(apdu); if (apdu)