2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.61 2003-10-16 13:40:41 adam Exp $
11 #include <yaz/marcdisp.h>
12 #include <yaz/yaz-iconv.h>
14 #include <yaz/diagbib1.h>
15 #include <yaz++/proxy.h>
17 static const char *apdu_name(Z_APDU *apdu)
21 case Z_APDU_initRequest:
23 case Z_APDU_initResponse:
24 return "initResponse";
25 case Z_APDU_searchRequest:
26 return "searchRequest";
27 case Z_APDU_searchResponse:
28 return "searchResponse";
29 case Z_APDU_presentRequest:
30 return "presentRequest";
31 case Z_APDU_presentResponse:
32 return "presentResponse";
33 case Z_APDU_deleteResultSetRequest:
34 return "deleteResultSetRequest";
35 case Z_APDU_deleteResultSetResponse:
36 return "deleteResultSetResponse";
37 case Z_APDU_scanRequest:
39 case Z_APDU_scanResponse:
40 return "scanResponse";
41 case Z_APDU_sortRequest:
43 case Z_APDU_sortResponse:
44 return "sortResponse";
45 case Z_APDU_extendedServicesRequest:
46 return "extendedServicesRequest";
47 case Z_APDU_extendedServicesResponse:
48 return "extendedServicesResponse";
55 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
57 Yaz_Z_Assoc(the_PDU_Observable), m_bw_stat(60), m_pdu_stat(60)
59 m_PDU_Observable = the_PDU_Observable;
64 m_keepalive_limit_bw = 500000;
65 m_keepalive_limit_pdu = 1000;
68 m_proxy_authentication = 0;
71 m_client_idletime = 600;
72 m_target_idletime = 600;
73 m_optimize = xstrdup ("1");
74 strcpy(m_session_str, "x");
76 m_bytes_sent = m_bytes_recv = 0;
80 m_max_record_retrieve = 0;
84 m_invalid_session = 0;
89 Yaz_Proxy::~Yaz_Proxy()
91 yaz_log(LOG_LOG, "%sClosed %d/%d sent/recv bytes total", m_session_str,
92 m_bytes_sent, m_bytes_recv);
93 xfree (m_proxyTarget);
94 xfree (m_default_target);
95 xfree (m_proxy_authentication);
100 int Yaz_Proxy::set_config(const char *config)
103 m_config = new Yaz_ProxyConfig();
104 xfree(m_config_fname);
105 m_config_fname = xstrdup(config);
106 int r = m_config->read_xml(config);
110 void Yaz_Proxy::set_default_target(const char *target)
112 xfree (m_default_target);
113 m_default_target = 0;
115 m_default_target = (char *) xstrdup (target);
118 void Yaz_Proxy::set_proxy_authentication (const char *auth)
120 xfree (m_proxy_authentication);
121 m_proxy_authentication = 0;
123 m_proxy_authentication = (char *) xstrdup (auth);
126 Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
129 return m_parent->check_reconfigure();
131 Yaz_ProxyConfig *cfg = m_config;
134 yaz_log(LOG_LOG, "reconfigure");
136 if (m_config_fname && cfg)
138 yaz_log(LOG_LOG, "reconfigure config %s", m_config_fname);
139 int r = cfg->read_xml(m_config_fname);
141 yaz_log(LOG_WARN, "reconfigure failed");
144 yaz_log(LOG_LOG, "reconfigure");
150 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
151 *the_PDU_Observable, int fd)
154 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable, this);
155 new_proxy->m_config = 0;
156 new_proxy->m_config_fname = 0;
157 new_proxy->timeout(m_client_idletime);
158 new_proxy->m_target_idletime = m_target_idletime;
159 new_proxy->set_default_target(m_default_target);
160 new_proxy->set_APDU_log(get_APDU_log());
161 new_proxy->set_proxy_authentication(m_proxy_authentication);
162 sprintf(new_proxy->m_session_str, "%ld:%d ", (long) time(0), m_session_no);
164 yaz_log (LOG_LOG, "%sNew session %s", new_proxy->m_session_str,
165 the_PDU_Observable->getpeername());
169 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
172 Z_OtherInformationUnit *oi;
174 ent.proto = PROTO_Z3950;
175 ent.oclass = CLASS_USERINFO;
176 ent.value = (oid_value) VAL_COOKIE;
177 assert (oid_ent_to_oid (&ent, oid));
179 if (oid_ent_to_oid (&ent, oid) &&
180 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
181 oi->which == Z_OtherInfo_characterInfo)
182 return oi->information.characterInfo;
186 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
189 Z_OtherInformationUnit *oi;
191 ent.proto = PROTO_Z3950;
192 ent.oclass = CLASS_USERINFO;
193 ent.value = (oid_value) VAL_PROXY;
194 if (oid_ent_to_oid (&ent, oid) &&
195 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
196 oi->which == Z_OtherInfo_characterInfo)
197 return oi->information.characterInfo;
201 const char *Yaz_Proxy::load_balance(const char **url)
203 int zurl_in_use[MAX_ZURL_PLEX];
207 for (i = 0; i<MAX_ZURL_PLEX; i++)
209 for (c = m_parent->m_clientPool; c; c = c->m_next)
211 for (i = 0; url[i]; i++)
212 if (!strcmp(url[i], c->get_hostname()))
217 for (i = 0; url[i]; i++)
219 yaz_log(LOG_DEBUG, "%szurl=%s use=%d",
220 m_session_str, url[i], zurl_in_use[i]);
221 if (min > zurl_in_use[i])
224 min = zurl_in_use[i];
230 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
233 Yaz_Proxy *parent = m_parent;
234 Z_OtherInformation **oi;
235 Yaz_ProxyClient *c = m_client;
237 get_otherInfoAPDU(apdu, &oi);
238 char *cookie = get_cookie(oi);
242 const char *url[MAX_ZURL_PLEX];
243 const char *proxy_host = get_proxy(oi);
244 Yaz_ProxyConfig *cfg = check_reconfigure();
247 xfree(m_default_target);
248 m_default_target = xstrdup(proxy_host);
249 proxy_host = m_default_target;
251 int client_idletime = -1;
252 url[0] = m_default_target;
258 cfg->get_target_info(proxy_host, url, &m_bw_max,
259 &m_pdu_max, &m_max_record_retrieve,
260 &m_target_idletime, &client_idletime,
261 &parent->m_max_clients,
262 &m_keepalive_limit_bw,
263 &m_keepalive_limit_pdu,
264 &pre_init, &log_mask);
266 if (client_idletime != -1)
268 m_client_idletime = client_idletime;
269 timeout(m_client_idletime);
273 yaz_log(LOG_LOG, "%sNo default target", m_session_str);
276 // we don't handle multiplexing for cookie session, so we just
277 // pick the first one in this case (anonymous users will be able
278 // to use any backend)
279 if (cookie && *cookie)
280 m_proxyTarget = (char*) xstrdup(url[0]);
282 m_proxyTarget = (char*) xstrdup(load_balance(url));
284 if (cookie && *cookie)
286 Yaz_ProxyClient *cc = 0;
288 for (c = parent->m_clientPool; c; c = c->m_next)
291 assert (*c->m_prev == c);
292 if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
293 !strcmp(m_proxyTarget, c->get_hostname()))
302 // The following handles "cancel"
303 // If connection is busy (waiting for PDU) and
304 // we have an initRequest we can safely do re-open
305 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
307 yaz_log (LOG_LOG, "%s REOPEN target=%s", m_session_str,
314 c->m_last_resultCount = 0;
315 c->m_sr_transform = 0;
317 c->m_resultSetStartPoint = 0;
318 c->m_target_idletime = m_target_idletime;
319 if (c->client(m_proxyTarget))
326 c->m_seqno = parent->m_seqno;
327 if (c->m_server && c->m_server != this)
328 c->m_server->m_client = 0;
331 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
337 Yaz_ProxyClient *cc = 0;
339 for (c = parent->m_clientPool; c; c = c->m_next)
342 assert (*c->m_prev == c);
343 if (c->m_server == 0 && c->m_cookie == 0 &&
345 !strcmp(m_proxyTarget, c->get_hostname()))
355 yaz_log (LOG_LOG, "%sREUSE %d %d %s",
357 c->m_seqno, parent->m_seqno, c->get_hostname());
359 c->m_seqno = parent->m_seqno;
360 assert(c->m_server == 0);
372 if (apdu->which != Z_APDU_initRequest)
374 yaz_log (LOG_LOG, "no first INIT!");
377 Z_InitRequest *initRequest = apdu->u.initRequest;
379 if (!initRequest->idAuthentication)
381 if (m_proxy_authentication)
383 initRequest->idAuthentication =
384 (Z_IdAuthentication *)
385 odr_malloc (odr_encode(),
386 sizeof(*initRequest->idAuthentication));
387 initRequest->idAuthentication->which =
388 Z_IdAuthentication_open;
389 initRequest->idAuthentication->u.open =
390 odr_strdup (odr_encode(), m_proxy_authentication);
394 // go through list of clients - and find the lowest/oldest one.
395 Yaz_ProxyClient *c_min = 0;
397 int no_of_clients = 0;
398 if (parent->m_clientPool)
399 yaz_log (LOG_DEBUG, "Existing sessions");
400 for (c = parent->m_clientPool; c; c = c->m_next)
402 yaz_log (LOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
403 c->m_waiting, c->get_hostname(),
404 c->m_cookie ? c->m_cookie : "");
406 if (min_seq < 0 || c->m_seqno < min_seq)
408 min_seq = c->m_seqno;
412 if (no_of_clients >= parent->m_max_clients)
415 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
417 yaz_log (LOG_LOG, "%sMAXCLIENTS Destroy %d",
418 m_session_str, c->m_seqno);
419 if (c->m_server && c->m_server != this)
425 yaz_log (LOG_LOG, "%sMAXCLIENTS Reuse %d %d %s",
427 c->m_seqno, parent->m_seqno, c->get_hostname());
431 c->m_cookie = xstrdup(cookie);
432 c->m_seqno = parent->m_seqno;
433 if (c->m_server && c->m_server != this)
435 c->m_server->m_client = 0;
439 c->m_target_idletime = m_target_idletime;
440 c->timeout(m_target_idletime);
446 yaz_log (LOG_LOG, "%sNEW %d %s",
447 m_session_str, parent->m_seqno, m_proxyTarget);
448 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), parent);
449 c->m_next = parent->m_clientPool;
451 c->m_next->m_prev = &c->m_next;
452 parent->m_clientPool = c;
453 c->m_prev = &parent->m_clientPool;
459 c->m_cookie = xstrdup(cookie);
461 c->m_seqno = parent->m_seqno;
463 c->m_last_resultCount = 0;
466 c->m_sr_transform = 0;
468 c->m_resultSetStartPoint = 0;
470 if (c->client(m_proxyTarget))
475 c->m_target_idletime = m_target_idletime;
479 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
483 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
486 for (i = 0; i<num; i++)
489 Z_DefaultDiagFormat *r;
490 Z_DiagRec *p = pp[i];
491 if (p->which != Z_DiagRec_defaultFormat)
493 yaz_log(LOG_LOG, "%sError no diagnostics", m_session_str);
497 r = p->u.defaultFormat;
498 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
499 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
500 yaz_log(LOG_LOG, "%sError unknown diagnostic set", m_session_str);
503 case Z_DefaultDiagFormat_v2Addinfo:
504 yaz_log(LOG_LOG, "%sError %d %s:%s",
506 *r->condition, diagbib1_str(*r->condition),
509 case Z_DefaultDiagFormat_v3Addinfo:
510 yaz_log(LOG_LOG, "%sError %d %s:%s",
512 *r->condition, diagbib1_str(*r->condition),
519 void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
523 yaz_marc_t mt = yaz_marc_create();
524 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
525 for (i = 0; i < p->num_records; i++)
527 Z_NamePlusRecord *npr = p->records[i];
528 if (npr->which == Z_NamePlusRecord_databaseRecord)
530 Z_External *r = npr->u.databaseRecord;
531 if (r->which == Z_External_octet)
535 if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
536 r->u.octet_aligned->len,
539 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
540 WRBUF wrbuf = wrbuf_alloc();
543 size_t inbytesleft = rlen;
544 const char *inp = result;
545 while (cd && inbytesleft)
547 size_t outbytesleft = sizeof(outbuf);
551 r = yaz_iconv (cd, (char**) &inp,
553 &outp, &outbytesleft);
554 if (r == (size_t) (-1))
556 int e = yaz_iconv_error(cd);
557 if (e != YAZ_ICONV_E2BIG)
559 yaz_log(LOG_WARN, "conversion failure");
563 wrbuf_write(wrbuf, outbuf, outp - outbuf);
568 npr->u.databaseRecord = z_ext_record(odr_encode(),
572 wrbuf_free(wrbuf, 1);
577 yaz_marc_destroy(mt);
580 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
583 if (apdu->which == Z_APDU_searchResponse)
585 Z_SearchResponse *sr = apdu->u.searchResponse;
586 Z_Records *p = sr->records;
587 if (p && p->which == Z_Records_NSD)
589 Z_DiagRec dr, *dr_p = &dr;
590 dr.which = Z_DiagRec_defaultFormat;
591 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
593 display_diagrecs(&dr_p, 1);
597 if (m_marcxml_flag && p && p->which == Z_Records_DBOSD)
598 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
601 yaz_log(LOG_LOG, "%s%d hits", m_session_str,
603 if (*sr->resultCount < 0)
604 m_invalid_session = 1;
608 else if (apdu->which == Z_APDU_presentResponse)
610 Z_PresentResponse *sr = apdu->u.presentResponse;
611 Z_Records *p = sr->records;
612 if (p && p->which == Z_Records_NSD)
614 Z_DiagRec dr, *dr_p = &dr;
615 dr.which = Z_DiagRec_defaultFormat;
616 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
618 display_diagrecs(&dr_p, 1);
620 if (m_marcxml_flag && p && p->which == Z_Records_DBOSD)
621 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
623 int r = send_Z_PDU(apdu, &len);
624 yaz_log (LOG_DEBUG, "%sSending %s to client %d bytes", m_session_str,
625 apdu_name(apdu), len);
627 m_bw_stat.add_bytes(len);
631 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
634 int r = send_Z_PDU(apdu, &len);
635 yaz_log (LOG_LOG, "%sSending %s to %s %d bytes",
637 apdu_name(apdu), get_hostname(), len);
642 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
644 if (apdu->which == Z_APDU_presentRequest)
646 Z_PresentRequest *pr = apdu->u.presentRequest;
647 Z_NamePlusRecordList *npr;
648 int toget = *pr->numberOfRecordsRequested;
649 int start = *pr->resultSetStartPoint;
651 yaz_log(LOG_LOG, "%sPresent %s %d+%d", m_session_str,
652 pr->resultSetId, start, toget);
654 if (*m_parent->m_optimize == '0')
657 if (!m_client->m_last_resultSetId)
659 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
660 new_apdu->u.presentResponse->records =
661 create_nonSurrogateDiagnostics(odr_encode(), 30,
663 send_to_client(new_apdu);
666 if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
668 if (start+toget-1 > m_client->m_last_resultCount)
670 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
671 new_apdu->u.presentResponse->records =
672 create_nonSurrogateDiagnostics(odr_encode(), 13, 0);
673 send_to_client(new_apdu);
676 if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
677 pr->preferredRecordSyntax,
678 pr->recordComposition))
680 yaz_log (LOG_LOG, "%sReturned cached records for present request",
682 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
683 new_apdu->u.presentResponse->referenceId = pr->referenceId;
685 new_apdu->u.presentResponse->numberOfRecordsReturned
686 = odr_intdup(odr_encode(), toget);
688 new_apdu->u.presentResponse->records = (Z_Records*)
689 odr_malloc(odr_encode(), sizeof(Z_Records));
690 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
691 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
692 new_apdu->u.presentResponse->nextResultSetPosition =
693 odr_intdup(odr_encode(), start+toget);
695 send_to_client(new_apdu);
701 if (apdu->which != Z_APDU_searchRequest)
703 Z_SearchRequest *sr = apdu->u.searchRequest;
704 Yaz_Z_Query *this_query = new Yaz_Z_Query;
705 Yaz_Z_Databases this_databases;
707 this_databases.set(sr->num_databaseNames, (const char **)
710 this_query->set_Z_Query(sr->query);
713 this_query->print(query_str, sizeof(query_str)-1);
714 yaz_log(LOG_LOG, "%sSearch %s", m_session_str, query_str);
716 if (*m_parent->m_optimize != '0' &&
717 m_client->m_last_ok && m_client->m_last_query &&
718 m_client->m_last_query->match(this_query) &&
719 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
720 m_client->m_last_databases.match(this_databases))
723 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
724 m_client->m_last_resultCount < *sr->largeSetLowerBound)
726 Z_NamePlusRecordList *npr;
727 int toget = *sr->mediumSetPresentNumber;
728 Z_RecordComposition *comp = 0;
730 if (toget > m_client->m_last_resultCount)
731 toget = m_client->m_last_resultCount;
733 if (sr->mediumSetElementSetNames)
735 comp = (Z_RecordComposition *)
736 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
737 comp->which = Z_RecordComp_simple;
738 comp->u.simple = sr->mediumSetElementSetNames;
741 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
742 sr->preferredRecordSyntax, comp))
744 yaz_log (LOG_LOG, "%sReturned cached records for medium set",
746 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
747 new_apdu->u.searchResponse->referenceId = sr->referenceId;
748 new_apdu->u.searchResponse->resultCount =
749 &m_client->m_last_resultCount;
751 new_apdu->u.searchResponse->numberOfRecordsReturned
752 = odr_intdup(odr_encode(), toget);
754 new_apdu->u.searchResponse->presentStatus =
755 odr_intdup(odr_encode(), Z_PresentStatus_success);
756 new_apdu->u.searchResponse->records = (Z_Records*)
757 odr_malloc(odr_encode(), sizeof(Z_Records));
758 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
759 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
760 new_apdu->u.searchResponse->nextResultSetPosition =
761 odr_intdup(odr_encode(), toget+1);
762 send_to_client(new_apdu);
768 // send present request (medium size)
769 yaz_log (LOG_LOG, "%sOptimizing search for medium set",
772 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
773 Z_PresentRequest *pr = new_apdu->u.presentRequest;
774 pr->referenceId = sr->referenceId;
775 pr->resultSetId = sr->resultSetName;
776 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
777 *pr->numberOfRecordsRequested = toget;
778 pr->recordComposition = comp;
779 m_client->m_sr_transform = 1;
783 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
784 m_client->m_last_resultCount <= 0)
786 // large set. Return pseudo-search response immediately
787 yaz_log (LOG_LOG, "%sOptimizing search for large set",
789 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
790 new_apdu->u.searchResponse->referenceId = sr->referenceId;
791 new_apdu->u.searchResponse->resultCount =
792 &m_client->m_last_resultCount;
793 send_to_client(new_apdu);
798 Z_NamePlusRecordList *npr;
799 int toget = m_client->m_last_resultCount;
800 Z_RecordComposition *comp = 0;
802 // send a present request (small set)
804 if (sr->smallSetElementSetNames)
806 comp = (Z_RecordComposition *)
807 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
808 comp->which = Z_RecordComp_simple;
809 comp->u.simple = sr->smallSetElementSetNames;
812 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
813 sr->preferredRecordSyntax, comp))
815 yaz_log (LOG_LOG, "%sReturned cached records for small set",
817 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
818 new_apdu->u.searchResponse->referenceId = sr->referenceId;
819 new_apdu->u.searchResponse->resultCount =
820 &m_client->m_last_resultCount;
822 new_apdu->u.searchResponse->numberOfRecordsReturned
823 = odr_intdup(odr_encode(), toget);
825 new_apdu->u.searchResponse->presentStatus =
826 odr_intdup(odr_encode(), Z_PresentStatus_success);
827 new_apdu->u.searchResponse->records = (Z_Records*)
828 odr_malloc(odr_encode(), sizeof(Z_Records));
829 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
830 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
831 new_apdu->u.searchResponse->nextResultSetPosition =
832 odr_intdup(odr_encode(), toget+1);
833 send_to_client(new_apdu);
838 yaz_log (LOG_LOG, "%sOptimizing search for small set",
840 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
841 Z_PresentRequest *pr = new_apdu->u.presentRequest;
842 pr->referenceId = sr->referenceId;
843 pr->resultSetId = sr->resultSetName;
844 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
845 *pr->numberOfRecordsRequested = toget;
846 pr->recordComposition = comp;
847 m_client->m_sr_transform = 1;
852 else // query doesn't match
854 delete m_client->m_last_query;
855 m_client->m_last_query = this_query;
856 m_client->m_last_ok = 0;
857 m_client->m_cache.clear();
858 m_client->m_resultSetStartPoint = 0;
860 xfree (m_client->m_last_resultSetId);
861 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
863 m_client->m_last_databases.set(sr->num_databaseNames,
864 (const char **) sr->databaseNames);
870 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu, int len)
872 char *cp = strchr(m_session_str, ' ');
875 sprintf(cp+1, "%d ", m_request_no);
880 yaz_log (LOG_DEBUG, "%sReceiving %s from client %d bytes", m_session_str,
881 apdu_name(apdu), len);
883 if (m_bw_hold_PDU) // double incoming PDU. shutdown now.
886 m_bw_stat.add_bytes(len);
887 m_pdu_stat.add_bytes(1);
889 int bw_total = m_bw_stat.get_total();
890 int pdu_total = m_pdu_stat.get_total();
892 yaz_log(LOG_LOG, "%sstat bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
893 m_session_str, bw_total, pdu_total, m_bw_max, m_pdu_max);
896 if (bw_total > m_bw_max)
898 reduce = (bw_total/m_bw_max);
903 if (pdu_total > m_pdu_max)
905 int nreduce = (60/m_pdu_max);
906 reduce = (reduce > nreduce) ? reduce : nreduce;
911 yaz_log(LOG_LOG, "%sLimit delay=%d", m_session_str, reduce);
912 m_bw_hold_PDU = apdu; // save PDU and signal "on hold"
913 timeout(reduce); // call us reduce seconds later
916 recv_Z_PDU_0(apdu); // all fine. Proceed receive PDU as usual
919 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
921 if (m_max_record_retrieve)
923 if (apdu->which == Z_APDU_presentRequest)
925 Z_PresentRequest *pr = apdu->u.presentRequest;
926 if (pr->numberOfRecordsRequested &&
927 *pr->numberOfRecordsRequested > m_max_record_retrieve)
928 *pr->numberOfRecordsRequested = m_max_record_retrieve;
933 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
937 Z_Records *rec = (Z_Records *)
938 odr_malloc (odr, sizeof(*rec));
940 odr_malloc (odr, sizeof(*err));
941 Z_DiagRec *drec = (Z_DiagRec *)
942 odr_malloc (odr, sizeof(*drec));
943 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
944 odr_malloc (odr, sizeof(*dr));
946 rec->which = Z_Records_NSD;
947 rec->u.nonSurrogateDiagnostic = dr;
948 dr->diagnosticSetId =
949 yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
951 dr->which = Z_DefaultDiagFormat_v2Addinfo;
952 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
956 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
958 if (apdu->which == Z_APDU_searchRequest)
960 Z_SearchRequest *sr = apdu->u.searchRequest;
964 Yaz_ProxyConfig *cfg = check_reconfigure();
966 err = cfg->check_query(odr_encode(), m_default_target,
967 sr->query, &addinfo);
970 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
972 new_apdu->u.searchResponse->referenceId = sr->referenceId;
973 new_apdu->u.searchResponse->records =
974 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
975 *new_apdu->u.searchResponse->searchStatus = 0;
977 send_to_client(new_apdu);
985 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
988 if (apdu->which == Z_APDU_searchRequest)
990 Z_SearchRequest *sr = apdu->u.searchRequest;
991 if (*sr->smallSetUpperBound > 0 || *sr->mediumSetPresentNumber > 0)
995 Yaz_ProxyConfig *cfg = check_reconfigure();
998 err = cfg->check_syntax(odr_encode(),
1000 sr->preferredRecordSyntax,
1004 sr->preferredRecordSyntax =
1005 yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN,
1011 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1013 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1014 new_apdu->u.searchResponse->records =
1015 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1016 *new_apdu->u.searchResponse->searchStatus = 0;
1018 send_to_client(new_apdu);
1024 else if (apdu->which == Z_APDU_presentRequest)
1026 Z_PresentRequest *pr = apdu->u.presentRequest;
1029 Yaz_ProxyConfig *cfg = check_reconfigure();
1032 err = cfg->check_syntax(odr_encode(), m_default_target,
1033 pr->preferredRecordSyntax,
1037 pr->preferredRecordSyntax =
1038 yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN,
1044 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1046 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1047 new_apdu->u.presentResponse->records =
1048 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1049 *new_apdu->u.presentResponse->presentStatus =
1050 Z_PresentStatus_failure;
1052 send_to_client(new_apdu);
1060 void Yaz_Proxy::recv_Z_PDU_0(Z_APDU *apdu)
1062 // Determine our client.
1063 m_client = get_client(apdu);
1069 m_client->m_server = this;
1071 if (apdu->which == Z_APDU_initRequest)
1073 if (apdu->u.initRequest->implementationId)
1074 yaz_log(LOG_LOG, "%simplementationId: %s",
1075 m_session_str, apdu->u.initRequest->implementationId);
1076 if (apdu->u.initRequest->implementationName)
1077 yaz_log(LOG_LOG, "%simplementationName: %s",
1078 m_session_str, apdu->u.initRequest->implementationName);
1079 if (apdu->u.initRequest->implementationVersion)
1080 yaz_log(LOG_LOG, "%simplementationVersion: %s",
1081 m_session_str, apdu->u.initRequest->implementationVersion);
1082 if (m_client->m_init_flag)
1084 Z_APDU *apdu = m_client->m_initResponse;
1085 apdu->u.initResponse->otherInfo = 0;
1086 if (m_client->m_cookie && *m_client->m_cookie)
1087 set_otherInformationString(apdu, VAL_COOKIE, 1,
1088 m_client->m_cookie);
1089 send_to_client(apdu);
1092 m_client->m_init_flag = 1;
1094 handle_max_record_retrieve(apdu);
1097 apdu = handle_syntax_validation(apdu);
1100 apdu = handle_query_validation(apdu);
1103 apdu = result_set_optimize(apdu);
1106 m_client->timeout(m_target_idletime); // mark it active even
1107 // though we didn't use it
1111 // delete other info part from PDU before sending to target
1112 Z_OtherInformation **oi;
1113 get_otherInfoAPDU(apdu, &oi);
1117 if (apdu->which == Z_APDU_presentRequest &&
1118 m_client->m_resultSetStartPoint == 0)
1120 Z_PresentRequest *pr = apdu->u.presentRequest;
1121 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
1122 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
1124 m_client->m_resultSetStartPoint = 0;
1126 if (m_client->send_to_target(apdu) < 0)
1133 m_client->m_waiting = 1;
1136 void Yaz_Proxy::connectNotify()
1140 void Yaz_Proxy::shutdown()
1142 // only keep if keep_alive flag is set...
1144 !m_invalid_session &&
1145 m_client->m_pdu_recv < m_keepalive_limit_pdu &&
1146 m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
1147 m_client->m_waiting == 0)
1149 yaz_log(LOG_LOG, "%sShutdown (client to proxy) keepalive %s",
1151 m_client->get_hostname());
1152 yaz_log(LOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
1153 m_session_str, m_client->m_pdu_recv,
1154 m_client->m_bytes_sent + m_client->m_bytes_recv,
1155 m_keepalive_limit_bw, m_keepalive_limit_pdu);
1156 assert (m_client->m_waiting != 2);
1157 // Tell client (if any) that no server connection is there..
1158 m_client->m_server = 0;
1162 yaz_log (LOG_LOG, "%sShutdown (client to proxy) close %s",
1164 m_client->get_hostname());
1165 assert (m_client->m_waiting != 2);
1170 yaz_log (LOG_LOG, "%sshutdown (client to proxy) bad state",
1176 yaz_log (LOG_LOG, "%sShutdown (client to proxy)",
1180 m_parent->pre_init();
1184 const char *Yaz_ProxyClient::get_session_str()
1188 return m_server->get_session_str();
1191 void Yaz_ProxyClient::shutdown()
1193 yaz_log (LOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
1199 void Yaz_Proxy::failNotify()
1201 yaz_log (LOG_LOG, "%sConnection closed by client",
1206 void Yaz_ProxyClient::failNotify()
1208 yaz_log (LOG_LOG, "%sConnection closed by target %s",
1209 get_session_str(), get_hostname());
1213 void Yaz_ProxyClient::connectNotify()
1215 const char *s = get_session_str();
1216 const char *h = get_hostname();
1217 yaz_log (LOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
1219 timeout(m_target_idletime);
1224 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
1225 *the_PDU_Observable, int fd)
1227 return new Yaz_ProxyClient(the_PDU_Observable, 0);
1230 Yaz_ProxyClient::~Yaz_ProxyClient()
1235 m_next->m_prev = m_prev;
1236 m_waiting = 2; // for debugging purposes only.
1237 odr_destroy(m_init_odr);
1238 delete m_last_query;
1239 xfree (m_last_resultSetId);
1243 void Yaz_ProxyClient::pre_init_client()
1245 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
1246 Z_InitRequest *req = apdu->u.initRequest;
1248 ODR_MASK_SET(req->options, Z_Options_search);
1249 ODR_MASK_SET(req->options, Z_Options_present);
1250 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
1251 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
1252 ODR_MASK_SET(req->options, Z_Options_scan);
1253 ODR_MASK_SET(req->options, Z_Options_sort);
1254 ODR_MASK_SET(req->options, Z_Options_extendedServices);
1255 ODR_MASK_SET(req->options, Z_Options_delSet);
1257 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
1258 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
1259 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
1261 if (send_to_target(apdu) < 0)
1272 void Yaz_Proxy::pre_init()
1275 const char *name = 0;
1276 const char *zurl_in_use[MAX_ZURL_PLEX];
1277 int limit_bw, limit_pdu, limit_req;
1278 int target_idletime, client_idletime;
1280 int keepalive_limit_bw, keepalive_limit_pdu;
1284 Yaz_ProxyConfig *cfg = check_reconfigure();
1286 yaz_log(LOG_LOG, "pre_init");
1288 for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
1289 &limit_bw, &limit_pdu, &limit_req,
1290 &target_idletime, &client_idletime,
1292 &keepalive_limit_bw,
1293 &keepalive_limit_pdu,
1294 &pre_init, &log_mask) ; i++)
1299 for (j = 0; zurl_in_use[j]; j++)
1303 for (c = m_clientPool; c; c = c->m_next)
1305 if (!strcmp(zurl_in_use[j], c->get_hostname())
1306 && c->m_server == 0 && c->m_cookie == 0)
1309 yaz_log(LOG_LOG, "pre_init %s %s spare=%d pre_init=%d",
1310 name, zurl_in_use[j], spare, pre_init);
1311 if (spare < pre_init)
1313 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
1314 c->m_next = m_clientPool;
1316 c->m_next->m_prev = &c->m_next;
1318 c->m_prev = &m_clientPool;
1320 if (c->client(zurl_in_use[j]))
1328 c->m_target_idletime = target_idletime;
1329 yaz_log(LOG_LOG, "pre_init name=%s zurl=%s timeout=%d", name,
1330 zurl_in_use[j], target_idletime);
1331 c->m_seqno = m_seqno++;
1338 void Yaz_Proxy::timeoutNotify()
1344 timeout(m_client_idletime);
1345 Z_APDU *apdu = m_bw_hold_PDU;
1351 yaz_log (LOG_LOG, "%sTimeout (client to proxy)", m_session_str);
1362 void Yaz_ProxyClient::timeoutNotify()
1364 yaz_log (LOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
1371 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
1372 Yaz_Proxy *parent) :
1373 Yaz_Z_Assoc (the_PDU_Observable)
1380 m_last_resultSetId = 0;
1381 m_last_resultCount = 0;
1385 m_init_odr = odr_createmem (ODR_DECODE);
1387 m_resultSetStartPoint = 0;
1388 m_bytes_sent = m_bytes_recv = 0;
1392 m_target_idletime = 600;
1396 const char *Yaz_Proxy::option(const char *name, const char *value)
1398 if (!strcmp (name, "optimize")) {
1401 m_optimize = xstrdup (value);
1408 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
1410 m_bytes_recv += len;
1413 yaz_log (LOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
1414 apdu_name(apdu), get_hostname(), len);
1415 if (apdu->which == Z_APDU_initResponse)
1417 if (!m_server) // if this is a pre init session , check for more
1419 NMEM nmem = odr_extract_mem (odr_decode());
1420 odr_reset (m_init_odr);
1421 nmem_transfer (m_init_odr->mem, nmem);
1422 m_initResponse = apdu;
1424 Z_InitResponse *ir = apdu->u.initResponse;
1425 char *im0 = ir->implementationName;
1428 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
1435 strcat(im1, "(YAZ Proxy)");
1436 ir->implementationName = im1;
1438 nmem_destroy (nmem);
1440 if (apdu->which == Z_APDU_searchResponse)
1442 Z_SearchResponse *sr = apdu->u.searchResponse;
1443 m_last_resultCount = *sr->resultCount;
1444 int status = *sr->searchStatus;
1445 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
1449 if (sr->records && sr->records->which == Z_Records_DBOSD)
1451 m_cache.add(odr_decode(),
1452 sr->records->u.databaseOrSurDiagnostics, 1,
1457 if (apdu->which == Z_APDU_presentResponse)
1459 Z_PresentResponse *pr = apdu->u.presentResponse;
1463 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1464 Z_SearchResponse *sr = new_apdu->u.searchResponse;
1465 sr->referenceId = pr->referenceId;
1466 *sr->resultCount = m_last_resultCount;
1467 sr->records = pr->records;
1468 sr->nextResultSetPosition = pr->nextResultSetPosition;
1469 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
1473 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
1475 m_cache.add(odr_decode(),
1476 pr->records->u.databaseOrSurDiagnostics,
1477 m_resultSetStartPoint, -1);
1478 m_resultSetStartPoint = 0;
1482 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
1485 m_server->send_to_client(apdu);
1487 if (apdu->which == Z_APDU_close)
1493 void Yaz_Proxy::server(const char *addr)
1495 Yaz_Z_Assoc::server(addr);