2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.54 2003-10-09 12:11:10 adam Exp $
12 #include <yaz/diagbib1.h>
13 #include <yaz++/proxy.h>
15 static const char *apdu_name(Z_APDU *apdu)
19 case Z_APDU_initRequest:
21 case Z_APDU_initResponse:
22 return "initResponse";
23 case Z_APDU_searchRequest:
24 return "searchRequest";
25 case Z_APDU_searchResponse:
26 return "searchResponse";
27 case Z_APDU_presentRequest:
28 return "presentRequest";
29 case Z_APDU_presentResponse:
30 return "presentResponse";
31 case Z_APDU_deleteResultSetRequest:
32 return "deleteResultSetRequest";
33 case Z_APDU_deleteResultSetResponse:
34 return "deleteResultSetResponse";
35 case Z_APDU_scanRequest:
37 case Z_APDU_scanResponse:
38 return "scanResponse";
39 case Z_APDU_sortRequest:
41 case Z_APDU_sortResponse:
42 return "sortResponse";
43 case Z_APDU_extendedServicesRequest:
44 return "extendedServicesRequest";
45 case Z_APDU_extendedServicesResponse:
46 return "extendedServicesResponse";
53 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
54 Yaz_Z_Assoc(the_PDU_Observable), m_bw_stat(60), m_pdu_stat(60)
56 m_PDU_Observable = the_PDU_Observable;
61 m_keepalive_limit_bw = 500000;
62 m_keepalive_limit_pdu = 1000;
65 m_proxy_authentication = 0;
68 m_client_idletime = 600;
69 m_target_idletime = 600;
70 m_optimize = xstrdup ("1");
71 strcpy(m_session_str, "x");
73 m_bytes_sent = m_bytes_recv = 0;
77 m_max_record_retrieve = 0;
81 m_invalid_session = 0;
84 Yaz_Proxy::~Yaz_Proxy()
86 yaz_log(LOG_LOG, "%sClosed %d/%d sent/recv bytes total", m_session_str,
87 m_bytes_sent, m_bytes_recv);
88 xfree (m_proxyTarget);
89 xfree (m_default_target);
90 xfree (m_proxy_authentication);
93 m_parent->check_reconfigure();
96 int Yaz_Proxy::set_config(const char *config)
98 xfree(m_config_fname);
99 m_config_fname = xstrdup(config);
100 int r = m_config.read_xml(config);
104 void Yaz_Proxy::set_default_target(const char *target)
106 xfree (m_default_target);
107 m_default_target = 0;
109 m_default_target = (char *) xstrdup (target);
112 void Yaz_Proxy::set_proxy_authentication (const char *auth)
114 xfree (m_proxy_authentication);
115 m_proxy_authentication = 0;
117 m_proxy_authentication = (char *) xstrdup (auth);
120 void Yaz_Proxy::check_reconfigure()
124 yaz_log(LOG_LOG, "reconfigure");
128 yaz_log(LOG_LOG, "reconfigure config %s", m_config_fname);
129 int r = m_config.read_xml(m_config_fname);
131 yaz_log(LOG_WARN, "reconfigure failed");
134 yaz_log(LOG_LOG, "reconfigure");
139 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
140 *the_PDU_Observable, int fd)
143 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
144 new_proxy->m_parent = this;
145 new_proxy->m_config = m_config;
146 new_proxy->m_config_fname = 0;
147 new_proxy->timeout(m_client_idletime);
148 new_proxy->m_target_idletime = m_target_idletime;
149 new_proxy->set_default_target(m_default_target);
150 new_proxy->set_APDU_log(get_APDU_log());
151 new_proxy->set_proxy_authentication(m_proxy_authentication);
152 sprintf(new_proxy->m_session_str, "%ld:%d ", (long) time(0), m_session_no);
154 yaz_log (LOG_LOG, "%sNew session %s", new_proxy->m_session_str,
155 the_PDU_Observable->getpeername());
159 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
162 Z_OtherInformationUnit *oi;
164 ent.proto = PROTO_Z3950;
165 ent.oclass = CLASS_USERINFO;
166 ent.value = (oid_value) VAL_COOKIE;
167 assert (oid_ent_to_oid (&ent, oid));
169 if (oid_ent_to_oid (&ent, oid) &&
170 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
171 oi->which == Z_OtherInfo_characterInfo)
172 return oi->information.characterInfo;
176 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
179 Z_OtherInformationUnit *oi;
181 ent.proto = PROTO_Z3950;
182 ent.oclass = CLASS_USERINFO;
183 ent.value = (oid_value) VAL_PROXY;
184 if (oid_ent_to_oid (&ent, oid) &&
185 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
186 oi->which == Z_OtherInfo_characterInfo)
187 return oi->information.characterInfo;
191 const char *Yaz_Proxy::load_balance(const char **url)
193 int zurl_in_use[MAX_ZURL_PLEX];
197 for (i = 0; i<MAX_ZURL_PLEX; i++)
199 for (c = m_parent->m_clientPool; c; c = c->m_next)
201 for (i = 0; url[i]; i++)
202 if (!strcmp(url[i], c->get_hostname()))
207 for (i = 0; url[i]; i++)
209 yaz_log(LOG_DEBUG, "%szurl=%s use=%d",
210 m_session_str, url[i], zurl_in_use[i]);
211 if (min > zurl_in_use[i])
214 min = zurl_in_use[i];
220 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
223 Yaz_Proxy *parent = m_parent;
224 Z_OtherInformation **oi;
225 Yaz_ProxyClient *c = m_client;
227 get_otherInfoAPDU(apdu, &oi);
228 char *cookie = get_cookie(oi);
232 const char *url[MAX_ZURL_PLEX];
233 const char *proxy_host = get_proxy(oi);
236 xfree(m_default_target);
237 m_default_target = xstrdup(proxy_host);
238 proxy_host = m_default_target;
241 int client_idletime = -1;
242 m_config.get_target_info(proxy_host, url, &m_bw_max,
243 &m_pdu_max, &m_max_record_retrieve,
244 &m_target_idletime, &client_idletime,
245 &parent->m_max_clients,
246 &m_keepalive_limit_bw, &m_keepalive_limit_pdu);
247 if (client_idletime != -1)
249 m_client_idletime = client_idletime;
250 timeout(m_client_idletime);
254 yaz_log(LOG_LOG, "%sNo default target", m_session_str);
257 // we don't handle multiplexing for cookie session, so we just
258 // pick the first one in this case (anonymous users will be able
259 // to use any backend)
260 if (cookie && *cookie)
261 m_proxyTarget = (char*) xstrdup(url[0]);
263 m_proxyTarget = (char*) xstrdup(load_balance(url));
265 if (cookie && *cookie)
267 Yaz_ProxyClient *cc = 0;
269 for (c = parent->m_clientPool; c; c = c->m_next)
272 assert (*c->m_prev == c);
273 if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
274 !strcmp(m_proxyTarget, c->get_hostname()))
283 // The following handles "cancel"
284 // If connection is busy (waiting for PDU) and
285 // we have an initRequest we can safely do re-open
286 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
288 yaz_log (LOG_LOG, "%s REOPEN target=%s", m_session_str,
291 c->client(m_proxyTarget);
296 c->m_last_resultCount = 0;
297 c->m_sr_transform = 0;
299 c->m_resultSetStartPoint = 0;
300 c->timeout(m_target_idletime);
302 c->m_seqno = parent->m_seqno;
303 if (c->m_server && c->m_server != this)
304 c->m_server->m_client = 0;
307 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
313 Yaz_ProxyClient *cc = 0;
315 for (c = parent->m_clientPool; c; c = c->m_next)
318 assert (*c->m_prev == c);
319 if (c->m_server == 0 && c->m_cookie == 0 &&
320 !strcmp(m_proxyTarget, c->get_hostname()))
330 yaz_log (LOG_LOG, "%sREUSE %d %d %s",
332 c->m_seqno, parent->m_seqno, c->get_hostname());
334 c->m_seqno = parent->m_seqno;
335 assert(c->m_server == 0);
344 if (apdu->which != Z_APDU_initRequest)
346 yaz_log (LOG_LOG, "no first INIT!");
349 Z_InitRequest *initRequest = apdu->u.initRequest;
351 if (!initRequest->idAuthentication)
353 if (m_proxy_authentication)
355 initRequest->idAuthentication =
356 (Z_IdAuthentication *)
357 odr_malloc (odr_encode(),
358 sizeof(*initRequest->idAuthentication));
359 initRequest->idAuthentication->which =
360 Z_IdAuthentication_open;
361 initRequest->idAuthentication->u.open =
362 odr_strdup (odr_encode(), m_proxy_authentication);
366 // go through list of clients - and find the lowest/oldest one.
367 Yaz_ProxyClient *c_min = 0;
369 int no_of_clients = 0;
370 if (parent->m_clientPool)
371 yaz_log (LOG_DEBUG, "Existing sessions");
372 for (c = parent->m_clientPool; c; c = c->m_next)
374 yaz_log (LOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
375 c->m_waiting, c->get_hostname(),
376 c->m_cookie ? c->m_cookie : "");
378 if (min_seq < 0 || c->m_seqno < min_seq)
380 min_seq = c->m_seqno;
384 if (no_of_clients >= parent->m_max_clients)
387 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
389 yaz_log (LOG_LOG, "%sMAXCLIENTS Destroy %d",
390 m_session_str, c->m_seqno);
391 if (c->m_server && c->m_server != this)
397 yaz_log (LOG_LOG, "%sMAXCLIENTS Reuse %d %d %s",
399 c->m_seqno, parent->m_seqno, c->get_hostname());
403 c->m_cookie = xstrdup(cookie);
404 c->m_seqno = parent->m_seqno;
405 if (c->m_server && c->m_server != this)
407 c->m_server->m_client = 0;
416 yaz_log (LOG_LOG, "%sNEW %d %s",
417 m_session_str, parent->m_seqno, m_proxyTarget);
418 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
419 c->m_next = parent->m_clientPool;
421 c->m_next->m_prev = &c->m_next;
422 parent->m_clientPool = c;
423 c->m_prev = &parent->m_clientPool;
429 c->m_cookie = xstrdup(cookie);
431 c->m_seqno = parent->m_seqno;
432 c->client(m_proxyTarget);
434 c->m_last_resultCount = 0;
437 c->m_sr_transform = 0;
439 c->m_resultSetStartPoint = 0;
444 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
448 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
451 for (i = 0; i<num; i++)
454 Z_DefaultDiagFormat *r;
455 Z_DiagRec *p = pp[i];
456 if (p->which != Z_DiagRec_defaultFormat)
458 yaz_log(LOG_LOG, "%sError no diagnostics", m_session_str);
462 r = p->u.defaultFormat;
463 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
464 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
465 yaz_log(LOG_LOG, "%sError unknown diagnostic set", m_session_str);
468 case Z_DefaultDiagFormat_v2Addinfo:
469 yaz_log(LOG_LOG, "%sError %d %s:%s",
471 *r->condition, diagbib1_str(*r->condition),
474 case Z_DefaultDiagFormat_v3Addinfo:
475 yaz_log(LOG_LOG, "%sError %d %s:%s",
477 *r->condition, diagbib1_str(*r->condition),
484 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
487 if (apdu->which == Z_APDU_searchResponse)
489 Z_SearchResponse *sr = apdu->u.searchResponse;
490 Z_Records *p = sr->records;
491 if (p && p->which == Z_Records_NSD)
493 Z_DiagRec dr, *dr_p = &dr;
494 dr.which = Z_DiagRec_defaultFormat;
495 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
497 display_diagrecs(&dr_p, 1);
503 yaz_log(LOG_LOG, "%s%d hits", m_session_str,
505 if (*sr->resultCount < 0)
506 m_invalid_session = 1;
510 else if (apdu->which == Z_APDU_presentResponse)
512 Z_PresentResponse *sr = apdu->u.presentResponse;
513 Z_Records *p = sr->records;
514 if (p && p->which == Z_Records_NSD)
516 Z_DiagRec dr, *dr_p = &dr;
517 dr.which = Z_DiagRec_defaultFormat;
518 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
520 display_diagrecs(&dr_p, 1);
523 int r = send_Z_PDU(apdu, &len);
524 yaz_log (LOG_DEBUG, "%sSending %s to client %d bytes", m_session_str,
525 apdu_name(apdu), len);
527 m_bw_stat.add_bytes(len);
531 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
534 int r = send_Z_PDU(apdu, &len);
535 yaz_log (LOG_DEBUG, "%sSending %s to %s %d bytes",
537 apdu_name(apdu), get_hostname(), len);
542 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
544 if (*m_parent->m_optimize == '0')
545 return apdu; // don't optimize result sets..
546 if (apdu->which == Z_APDU_presentRequest)
548 Z_PresentRequest *pr = apdu->u.presentRequest;
549 Z_NamePlusRecordList *npr;
550 int toget = *pr->numberOfRecordsRequested;
551 int start = *pr->resultSetStartPoint;
553 if (m_client->m_last_resultSetId &&
554 !strcmp(m_client->m_last_resultSetId, pr->resultSetId))
556 if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
557 pr->preferredRecordSyntax,
558 pr->recordComposition))
560 yaz_log (LOG_LOG, "%sReturned cached records for present request",
562 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
563 new_apdu->u.presentResponse->referenceId = pr->referenceId;
565 new_apdu->u.presentResponse->numberOfRecordsReturned
566 = odr_intdup(odr_encode(), toget);
568 new_apdu->u.presentResponse->records = (Z_Records*)
569 odr_malloc(odr_encode(), sizeof(Z_Records));
570 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
571 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
572 new_apdu->u.presentResponse->nextResultSetPosition =
573 odr_intdup(odr_encode(), start+toget);
575 send_to_client(new_apdu);
581 if (apdu->which != Z_APDU_searchRequest)
583 Z_SearchRequest *sr = apdu->u.searchRequest;
584 Yaz_Z_Query *this_query = new Yaz_Z_Query;
585 Yaz_Z_Databases this_databases;
587 this_databases.set(sr->num_databaseNames, (const char **)
590 this_query->set_Z_Query(sr->query);
593 this_query->print(query_str, sizeof(query_str)-1);
594 yaz_log(LOG_LOG, "%sQuery %s", m_session_str, query_str);
596 if (m_client->m_last_ok && m_client->m_last_query &&
597 m_client->m_last_query->match(this_query) &&
598 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
599 m_client->m_last_databases.match(this_databases))
602 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
603 m_client->m_last_resultCount < *sr->largeSetLowerBound)
605 Z_NamePlusRecordList *npr;
606 int toget = *sr->mediumSetPresentNumber;
607 Z_RecordComposition *comp = 0;
609 if (toget > m_client->m_last_resultCount)
610 toget = m_client->m_last_resultCount;
612 if (sr->mediumSetElementSetNames)
614 comp = (Z_RecordComposition *)
615 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
616 comp->which = Z_RecordComp_simple;
617 comp->u.simple = sr->mediumSetElementSetNames;
620 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
621 sr->preferredRecordSyntax, comp))
623 yaz_log (LOG_LOG, "%sReturned cached records for medium set",
625 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
626 new_apdu->u.searchResponse->referenceId = sr->referenceId;
627 new_apdu->u.searchResponse->resultCount =
628 &m_client->m_last_resultCount;
630 new_apdu->u.searchResponse->numberOfRecordsReturned
631 = odr_intdup(odr_encode(), toget);
633 new_apdu->u.searchResponse->presentStatus =
634 odr_intdup(odr_encode(), Z_PresentStatus_success);
635 new_apdu->u.searchResponse->records = (Z_Records*)
636 odr_malloc(odr_encode(), sizeof(Z_Records));
637 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
638 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
639 new_apdu->u.searchResponse->nextResultSetPosition =
640 odr_intdup(odr_encode(), toget+1);
641 send_to_client(new_apdu);
647 // send present request (medium size)
648 yaz_log (LOG_LOG, "%sOptimizing search for medium set",
651 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
652 Z_PresentRequest *pr = new_apdu->u.presentRequest;
653 pr->referenceId = sr->referenceId;
654 pr->resultSetId = sr->resultSetName;
655 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
656 *pr->numberOfRecordsRequested = toget;
657 pr->recordComposition = comp;
658 m_client->m_sr_transform = 1;
662 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
663 m_client->m_last_resultCount <= 0)
665 // large set. Return pseudo-search response immediately
666 yaz_log (LOG_LOG, "%sOptimizing search for large set",
668 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
669 new_apdu->u.searchResponse->referenceId = sr->referenceId;
670 new_apdu->u.searchResponse->resultCount =
671 &m_client->m_last_resultCount;
672 send_to_client(new_apdu);
677 Z_NamePlusRecordList *npr;
678 int toget = m_client->m_last_resultCount;
679 Z_RecordComposition *comp = 0;
681 // send a present request (small set)
683 if (sr->smallSetElementSetNames)
685 comp = (Z_RecordComposition *)
686 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
687 comp->which = Z_RecordComp_simple;
688 comp->u.simple = sr->smallSetElementSetNames;
691 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
692 sr->preferredRecordSyntax, comp))
694 yaz_log (LOG_LOG, "%sReturned cached records for small set",
696 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
697 new_apdu->u.searchResponse->referenceId = sr->referenceId;
698 new_apdu->u.searchResponse->resultCount =
699 &m_client->m_last_resultCount;
701 new_apdu->u.searchResponse->numberOfRecordsReturned
702 = odr_intdup(odr_encode(), toget);
704 new_apdu->u.searchResponse->presentStatus =
705 odr_intdup(odr_encode(), Z_PresentStatus_success);
706 new_apdu->u.searchResponse->records = (Z_Records*)
707 odr_malloc(odr_encode(), sizeof(Z_Records));
708 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
709 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
710 new_apdu->u.searchResponse->nextResultSetPosition =
711 odr_intdup(odr_encode(), toget+1);
712 send_to_client(new_apdu);
717 yaz_log (LOG_LOG, "%sOptimizing search for small set",
719 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
720 Z_PresentRequest *pr = new_apdu->u.presentRequest;
721 pr->referenceId = sr->referenceId;
722 pr->resultSetId = sr->resultSetName;
723 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
724 *pr->numberOfRecordsRequested = toget;
725 pr->recordComposition = comp;
726 m_client->m_sr_transform = 1;
731 else // query doesn't match
733 delete m_client->m_last_query;
734 m_client->m_last_query = this_query;
735 m_client->m_last_ok = 0;
736 m_client->m_cache.clear();
737 m_client->m_resultSetStartPoint = 0;
739 xfree (m_client->m_last_resultSetId);
740 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
742 m_client->m_last_databases.set(sr->num_databaseNames,
743 (const char **) sr->databaseNames);
749 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu, int len)
751 char *cp = strchr(m_session_str, ' ');
754 sprintf(cp+1, "%d ", m_request_no);
759 yaz_log (LOG_DEBUG, "%sReceiving %s from client %d bytes", m_session_str,
760 apdu_name(apdu), len);
762 if (m_bw_hold_PDU) // double incoming PDU. shutdown now.
765 m_bw_stat.add_bytes(len);
766 m_pdu_stat.add_bytes(1);
768 int bw_total = m_bw_stat.get_total();
769 int pdu_total = m_pdu_stat.get_total();
771 yaz_log(LOG_LOG, "%sstat bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
772 m_session_str, bw_total, pdu_total, m_bw_max, m_pdu_max);
775 if (bw_total > m_bw_max)
777 reduce = (bw_total/m_bw_max);
782 if (pdu_total > m_pdu_max)
784 int nreduce = (60/m_pdu_max);
785 reduce = (reduce > nreduce) ? reduce : nreduce;
790 yaz_log(LOG_LOG, "%sLimit delay=%d", m_session_str, reduce);
791 m_bw_hold_PDU = apdu; // save PDU and signal "on hold"
792 timeout(reduce); // call us reduce seconds later
795 recv_Z_PDU_0(apdu); // all fine. Proceed receive PDU as usual
798 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
800 if (m_max_record_retrieve)
802 if (apdu->which == Z_APDU_presentRequest)
804 Z_PresentRequest *pr = apdu->u.presentRequest;
805 if (pr->numberOfRecordsRequested &&
806 *pr->numberOfRecordsRequested > m_max_record_retrieve)
807 *pr->numberOfRecordsRequested = m_max_record_retrieve;
812 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
816 Z_Records *rec = (Z_Records *)
817 odr_malloc (odr, sizeof(*rec));
819 odr_malloc (odr, sizeof(*err));
820 Z_DiagRec *drec = (Z_DiagRec *)
821 odr_malloc (odr, sizeof(*drec));
822 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
823 odr_malloc (odr, sizeof(*dr));
825 rec->which = Z_Records_NSD;
826 rec->u.nonSurrogateDiagnostic = dr;
827 dr->diagnosticSetId =
828 yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
830 dr->which = Z_DefaultDiagFormat_v2Addinfo;
831 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
835 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
837 if (apdu->which == Z_APDU_searchRequest)
839 Z_SearchRequest *sr = apdu->u.searchRequest;
842 err = m_config.check_query(odr_encode(), m_default_target, sr->query,
846 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
848 new_apdu->u.searchResponse->referenceId = sr->referenceId;
849 new_apdu->u.searchResponse->records =
850 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
851 *new_apdu->u.searchResponse->searchStatus = 0;
853 send_to_client(new_apdu);
861 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
863 if (apdu->which == Z_APDU_searchRequest)
865 Z_SearchRequest *sr = apdu->u.searchRequest;
866 if (*sr->smallSetUpperBound > 0 || *sr->largeSetLowerBound > 1)
870 err = m_config.check_syntax(odr_encode(), m_default_target,
871 sr->preferredRecordSyntax,
875 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
877 new_apdu->u.searchResponse->referenceId = sr->referenceId;
878 new_apdu->u.searchResponse->records =
879 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
880 *new_apdu->u.searchResponse->searchStatus = 0;
882 send_to_client(new_apdu);
888 else if (apdu->which == Z_APDU_presentRequest)
890 Z_PresentRequest *pr = apdu->u.presentRequest;
893 err = m_config.check_syntax(odr_encode(), m_default_target,
894 pr->preferredRecordSyntax,
898 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
900 new_apdu->u.presentResponse->referenceId = pr->referenceId;
901 new_apdu->u.presentResponse->records =
902 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
903 *new_apdu->u.presentResponse->presentStatus =
904 Z_PresentStatus_failure;
906 send_to_client(new_apdu);
914 void Yaz_Proxy::recv_Z_PDU_0(Z_APDU *apdu)
916 // Determine our client.
917 m_client = get_client(apdu);
923 m_client->m_server = this;
925 if (apdu->which == Z_APDU_initRequest)
927 if (apdu->u.initRequest->implementationId)
928 yaz_log(LOG_LOG, "%simplementationId: %s",
929 m_session_str, apdu->u.initRequest->implementationId);
930 if (apdu->u.initRequest->implementationName)
931 yaz_log(LOG_LOG, "%simplementationName: %s",
932 m_session_str, apdu->u.initRequest->implementationName);
933 if (apdu->u.initRequest->implementationVersion)
934 yaz_log(LOG_LOG, "%simplementationVersion: %s",
935 m_session_str, apdu->u.initRequest->implementationVersion);
936 if (m_client->m_init_flag)
938 Z_APDU *apdu = m_client->m_initResponse;
939 apdu->u.initResponse->otherInfo = 0;
940 if (m_client->m_cookie && *m_client->m_cookie)
941 set_otherInformationString(apdu, VAL_COOKIE, 1,
943 send_to_client(apdu);
946 m_client->m_init_flag = 1;
948 handle_max_record_retrieve(apdu);
951 apdu = handle_syntax_validation(apdu);
954 apdu = handle_query_validation(apdu);
957 apdu = result_set_optimize(apdu);
960 m_client->timeout(m_target_idletime); // mark it active even
961 // though we didn't use it
965 // delete other info part from PDU before sending to target
966 Z_OtherInformation **oi;
967 get_otherInfoAPDU(apdu, &oi);
971 if (apdu->which == Z_APDU_presentRequest &&
972 m_client->m_resultSetStartPoint == 0)
974 Z_PresentRequest *pr = apdu->u.presentRequest;
975 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
976 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
978 m_client->m_resultSetStartPoint = 0;
980 if (m_client->send_to_target(apdu) < 0)
987 m_client->m_waiting = 1;
990 void Yaz_Proxy::connectNotify()
994 void Yaz_Proxy::shutdown()
996 // only keep if keep_alive flag is set...
998 !m_invalid_session &&
999 m_client->m_pdu_recv < m_keepalive_limit_pdu &&
1000 m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
1001 m_client->m_waiting == 0)
1003 yaz_log(LOG_LOG, "%sShutdown (client to proxy) keepalive %s",
1005 m_client->get_hostname());
1006 yaz_log(LOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
1007 m_session_str, m_client->m_pdu_recv,
1008 m_client->m_bytes_sent + m_client->m_bytes_recv,
1009 m_keepalive_limit_bw, m_keepalive_limit_pdu);
1010 assert (m_client->m_waiting != 2);
1011 // Tell client (if any) that no server connection is there..
1012 m_client->m_server = 0;
1016 yaz_log (LOG_LOG, "%sShutdown (client to proxy) close %s",
1018 m_client->get_hostname());
1019 assert (m_client->m_waiting != 2);
1024 yaz_log (LOG_LOG, "%sshutdown (client to proxy) bad state",
1030 yaz_log (LOG_LOG, "%sShutdown (client to proxy)",
1036 const char *Yaz_ProxyClient::get_session_str()
1040 return m_server->get_session_str();
1043 void Yaz_ProxyClient::shutdown()
1045 yaz_log (LOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
1051 void Yaz_Proxy::failNotify()
1053 yaz_log (LOG_LOG, "%sConnection closed by client",
1058 void Yaz_ProxyClient::failNotify()
1060 yaz_log (LOG_LOG, "%sConnection closed by target %s",
1061 get_session_str(), get_hostname());
1065 void Yaz_ProxyClient::connectNotify()
1067 yaz_log (LOG_LOG, "%sConnection accepted by %s", get_session_str(),
1071 to = m_server->get_target_idletime();
1077 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
1078 *the_PDU_Observable, int fd)
1080 return new Yaz_ProxyClient(the_PDU_Observable);
1083 Yaz_ProxyClient::~Yaz_ProxyClient()
1088 m_next->m_prev = m_prev;
1089 m_waiting = 2; // for debugging purposes only.
1090 odr_destroy(m_init_odr);
1091 delete m_last_query;
1092 xfree (m_last_resultSetId);
1096 void Yaz_Proxy::timeoutNotify()
1100 timeout(m_client_idletime);
1101 Z_APDU *apdu = m_bw_hold_PDU;
1107 yaz_log (LOG_LOG, "%sTimeout (client to proxy)", m_session_str);
1112 void Yaz_ProxyClient::timeoutNotify()
1114 yaz_log (LOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
1119 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
1120 Yaz_Z_Assoc (the_PDU_Observable)
1127 m_last_resultSetId = 0;
1128 m_last_resultCount = 0;
1132 m_init_odr = odr_createmem (ODR_DECODE);
1134 m_resultSetStartPoint = 0;
1135 m_bytes_sent = m_bytes_recv = 0;
1139 const char *Yaz_Proxy::option(const char *name, const char *value)
1141 if (!strcmp (name, "optimize")) {
1144 m_optimize = xstrdup (value);
1151 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
1153 m_bytes_recv += len;
1156 yaz_log (LOG_DEBUG, "%sReceiving %s from %s %d bytes", get_session_str(),
1157 apdu_name(apdu), get_hostname(), len);
1158 if (apdu->which == Z_APDU_initResponse)
1160 NMEM nmem = odr_extract_mem (odr_decode());
1161 odr_reset (m_init_odr);
1162 nmem_transfer (m_init_odr->mem, nmem);
1163 m_initResponse = apdu;
1165 Z_InitResponse *ir = apdu->u.initResponse;
1166 char *im0 = ir->implementationName;
1169 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
1176 strcat(im1, "(YAZ Proxy)");
1177 ir->implementationName = im1;
1179 nmem_destroy (nmem);
1181 if (apdu->which == Z_APDU_searchResponse)
1183 Z_SearchResponse *sr = apdu->u.searchResponse;
1184 m_last_resultCount = *sr->resultCount;
1185 int status = *sr->searchStatus;
1186 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
1190 if (sr->records && sr->records->which == Z_Records_DBOSD)
1192 m_cache.add(odr_decode(),
1193 sr->records->u.databaseOrSurDiagnostics, 1,
1198 if (apdu->which == Z_APDU_presentResponse)
1200 Z_PresentResponse *pr = apdu->u.presentResponse;
1204 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1205 Z_SearchResponse *sr = new_apdu->u.searchResponse;
1206 sr->referenceId = pr->referenceId;
1207 *sr->resultCount = m_last_resultCount;
1208 sr->records = pr->records;
1209 sr->nextResultSetPosition = pr->nextResultSetPosition;
1210 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
1214 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
1216 m_cache.add(odr_decode(),
1217 pr->records->u.databaseOrSurDiagnostics,
1218 m_resultSetStartPoint, -1);
1219 m_resultSetStartPoint = 0;
1223 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
1226 m_server->send_to_client(apdu);
1228 if (apdu->which == Z_APDU_close)