2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.51 2003-10-08 08:15:01 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;
64 m_proxy_authentication = 0;
67 m_client_idletime = 600;
68 m_target_idletime = 600;
69 m_optimize = xstrdup ("1");
70 strcpy(m_session_str, "x");
72 m_bytes_sent = m_bytes_recv = 0;
76 m_max_record_retrieve = 0;
79 Yaz_Proxy::~Yaz_Proxy()
81 yaz_log(LOG_LOG, "%s Closed %d/%d sent/recv bytes total", m_session_str,
82 m_bytes_sent, m_bytes_recv);
83 xfree (m_proxyTarget);
84 xfree (m_default_target);
85 xfree (m_proxy_authentication);
89 int Yaz_Proxy::set_config(const char *config)
91 int r = m_config.read_xml(config);
95 void Yaz_Proxy::set_default_target(const char *target)
97 xfree (m_default_target);
100 m_default_target = (char *) xstrdup (target);
103 void Yaz_Proxy::set_proxy_authentication (const char *auth)
105 xfree (m_proxy_authentication);
106 m_proxy_authentication = 0;
108 m_proxy_authentication = (char *) xstrdup (auth);
111 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
112 *the_PDU_Observable, int fd)
114 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
115 new_proxy->m_parent = this;
116 new_proxy->m_config = m_config;
117 new_proxy->timeout(m_client_idletime);
118 new_proxy->m_target_idletime = m_target_idletime;
119 new_proxy->set_default_target(m_default_target);
120 new_proxy->set_APDU_log(get_APDU_log());
121 new_proxy->set_proxy_authentication(m_proxy_authentication);
122 sprintf(new_proxy->m_session_str, "%ld:%d", (long) time(0), m_session_no);
124 yaz_log (LOG_LOG, "%s New session %s", new_proxy->m_session_str,
125 the_PDU_Observable->getpeername());
129 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
132 Z_OtherInformationUnit *oi;
134 ent.proto = PROTO_Z3950;
135 ent.oclass = CLASS_USERINFO;
136 ent.value = (oid_value) VAL_COOKIE;
137 assert (oid_ent_to_oid (&ent, oid));
139 if (oid_ent_to_oid (&ent, oid) &&
140 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
141 oi->which == Z_OtherInfo_characterInfo)
142 return oi->information.characterInfo;
146 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
149 Z_OtherInformationUnit *oi;
151 ent.proto = PROTO_Z3950;
152 ent.oclass = CLASS_USERINFO;
153 ent.value = (oid_value) VAL_PROXY;
154 if (oid_ent_to_oid (&ent, oid) &&
155 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
156 oi->which == Z_OtherInfo_characterInfo)
157 return oi->information.characterInfo;
161 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
164 Yaz_Proxy *parent = m_parent;
165 Z_OtherInformation **oi;
166 Yaz_ProxyClient *c = m_client;
168 get_otherInfoAPDU(apdu, &oi);
169 char *cookie = get_cookie(oi);
173 const char *proxy_host = get_proxy(oi);
176 xfree(m_default_target);
177 m_default_target = xstrdup(proxy_host);
178 proxy_host = m_default_target;
182 int client_idletime = -1;
183 m_config.get_target_info(proxy_host, &url, &m_keepalive, &m_bw_max,
184 &m_pdu_max, &m_max_record_retrieve,
185 &m_target_idletime, &client_idletime,
186 &parent->m_max_clients);
187 if (client_idletime != -1)
189 m_client_idletime = client_idletime;
190 timeout(m_client_idletime);
194 yaz_log(LOG_LOG, "%s No default target", m_session_str);
197 m_proxyTarget = (char*) xstrdup(url);
199 if (cookie && *cookie)
201 Yaz_ProxyClient *cc = 0;
203 for (c = parent->m_clientPool; c; c = c->m_next)
206 assert (*c->m_prev == c);
207 if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
208 !strcmp(m_proxyTarget, c->get_hostname()))
217 // The following handles "cancel"
218 // If connection is busy (waiting for PDU) and
219 // we have an initRequest we can safely do re-open
220 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
222 yaz_log (LOG_LOG, "%s REOPEN target=%s", m_session_str,
225 c->client(m_proxyTarget);
230 c->m_last_resultCount = 0;
231 c->m_sr_transform = 0;
233 c->m_resultSetStartPoint = 0;
234 c->timeout(m_target_idletime);
236 c->m_seqno = parent->m_seqno;
237 if (c->m_server && c->m_server != this)
238 c->m_server->m_client = 0;
241 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
247 Yaz_ProxyClient *cc = 0;
249 for (c = parent->m_clientPool; c; c = c->m_next)
252 assert (*c->m_prev == c);
253 if (c->m_server == 0 && c->m_cookie == 0 &&
254 !strcmp(m_proxyTarget, c->get_hostname()))
264 yaz_log (LOG_LOG, "%s REUSE %d %d %s",
266 c->m_seqno, parent->m_seqno, c->get_hostname());
268 c->m_seqno = parent->m_seqno;
269 assert(c->m_server == 0);
278 if (apdu->which != Z_APDU_initRequest)
280 yaz_log (LOG_LOG, "no first INIT!");
283 Z_InitRequest *initRequest = apdu->u.initRequest;
285 if (!initRequest->idAuthentication)
287 if (m_proxy_authentication)
289 initRequest->idAuthentication =
290 (Z_IdAuthentication *)
291 odr_malloc (odr_encode(),
292 sizeof(*initRequest->idAuthentication));
293 initRequest->idAuthentication->which =
294 Z_IdAuthentication_open;
295 initRequest->idAuthentication->u.open =
296 odr_strdup (odr_encode(), m_proxy_authentication);
300 // go through list of clients - and find the lowest/oldest one.
301 Yaz_ProxyClient *c_min = 0;
303 int no_of_clients = 0;
304 if (parent->m_clientPool)
305 yaz_log (LOG_LOG, "Existing sessions");
306 for (c = parent->m_clientPool; c; c = c->m_next)
308 yaz_log (LOG_LOG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
309 c->m_waiting, c->get_hostname(),
310 c->m_cookie ? c->m_cookie : "");
312 if (min_seq < 0 || c->m_seqno < min_seq)
314 min_seq = c->m_seqno;
318 if (no_of_clients >= parent->m_max_clients)
321 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
323 yaz_log (LOG_LOG, "%s MAXCLIENTS Destroy %d",
324 m_session_str, c->m_seqno);
325 if (c->m_server && c->m_server != this)
331 yaz_log (LOG_LOG, "%s MAXCLIENTS Reuse %d %d %s",
333 c->m_seqno, parent->m_seqno, c->get_hostname());
337 c->m_cookie = xstrdup(cookie);
338 c->m_seqno = parent->m_seqno;
339 if (c->m_server && c->m_server != this)
341 c->m_server->m_client = 0;
350 yaz_log (LOG_LOG, "%s NEW %d %s",
351 m_session_str, parent->m_seqno, m_proxyTarget);
352 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
353 c->m_next = parent->m_clientPool;
355 c->m_next->m_prev = &c->m_next;
356 parent->m_clientPool = c;
357 c->m_prev = &parent->m_clientPool;
363 c->m_cookie = xstrdup(cookie);
365 c->m_seqno = parent->m_seqno;
366 c->client(m_proxyTarget);
368 c->m_last_resultCount = 0;
371 c->m_sr_transform = 0;
373 c->m_resultSetStartPoint = 0;
378 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
382 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
385 for (i = 0; i<num; i++)
388 Z_DefaultDiagFormat *r;
389 Z_DiagRec *p = pp[i];
390 if (p->which != Z_DiagRec_defaultFormat)
392 yaz_log(LOG_LOG, "%s Error no diagnostics", m_session_str);
396 r = p->u.defaultFormat;
397 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
398 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
399 yaz_log(LOG_LOG, "%s Error unknown diagnostic set", m_session_str);
402 case Z_DefaultDiagFormat_v2Addinfo:
403 yaz_log(LOG_LOG, "%s Error %d %s:%s",
405 *r->condition, diagbib1_str(*r->condition),
408 case Z_DefaultDiagFormat_v3Addinfo:
409 yaz_log(LOG_LOG, "%s Error %d %s:%s",
411 *r->condition, diagbib1_str(*r->condition),
418 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
421 if (apdu->which == Z_APDU_searchResponse)
423 Z_SearchResponse *sr = apdu->u.searchResponse;
424 Z_Records *p = sr->records;
425 if (p && p->which == Z_Records_NSD)
427 Z_DiagRec dr, *dr_p = &dr;
428 dr.which = Z_DiagRec_defaultFormat;
429 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
431 display_diagrecs(&dr_p, 1);
436 yaz_log(LOG_LOG, "%s %d hits", m_session_str,
440 else if (apdu->which == Z_APDU_presentResponse)
442 Z_PresentResponse *sr = apdu->u.presentResponse;
443 Z_Records *p = sr->records;
444 if (p && p->which == Z_Records_NSD)
446 Z_DiagRec dr, *dr_p = &dr;
447 dr.which = Z_DiagRec_defaultFormat;
448 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
450 display_diagrecs(&dr_p, 1);
453 int r = send_Z_PDU(apdu, &len);
454 yaz_log (LOG_LOG, "%s Sending %s to client %d bytes", m_session_str,
455 apdu_name(apdu), len);
457 m_bw_stat.add_bytes(len);
461 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
464 int r = send_Z_PDU(apdu, &len);
465 yaz_log (LOG_LOG, "%s Sending %s to %s %d bytes",
467 apdu_name(apdu), get_hostname(), len);
472 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
474 if (*m_parent->m_optimize == '0')
475 return apdu; // don't optimize result sets..
476 if (apdu->which == Z_APDU_presentRequest)
478 Z_PresentRequest *pr = apdu->u.presentRequest;
479 Z_NamePlusRecordList *npr;
480 int toget = *pr->numberOfRecordsRequested;
481 int start = *pr->resultSetStartPoint;
483 if (m_client->m_last_resultSetId &&
484 !strcmp(m_client->m_last_resultSetId, pr->resultSetId))
486 if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
487 pr->preferredRecordSyntax,
488 pr->recordComposition))
490 yaz_log (LOG_LOG, "%s Returned cache records for present request",
492 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
493 new_apdu->u.presentResponse->referenceId = pr->referenceId;
495 new_apdu->u.presentResponse->numberOfRecordsReturned
496 = odr_intdup(odr_encode(), toget);
498 new_apdu->u.presentResponse->records = (Z_Records*)
499 odr_malloc(odr_encode(), sizeof(Z_Records));
500 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
501 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
502 new_apdu->u.presentResponse->nextResultSetPosition =
503 odr_intdup(odr_encode(), start+toget);
505 send_to_client(new_apdu);
511 if (apdu->which != Z_APDU_searchRequest)
513 Z_SearchRequest *sr = apdu->u.searchRequest;
514 Yaz_Z_Query *this_query = new Yaz_Z_Query;
515 Yaz_Z_Databases this_databases;
517 this_databases.set(sr->num_databaseNames, (const char **)
520 this_query->set_Z_Query(sr->query);
523 this_query->print(query_str, sizeof(query_str)-1);
524 yaz_log(LOG_LOG, "%s Query %s", m_session_str, query_str);
526 if (m_client->m_last_ok && m_client->m_last_query &&
527 m_client->m_last_query->match(this_query) &&
528 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
529 m_client->m_last_databases.match(this_databases))
532 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
533 m_client->m_last_resultCount < *sr->largeSetLowerBound)
535 Z_NamePlusRecordList *npr;
536 int toget = *sr->mediumSetPresentNumber;
537 Z_RecordComposition *comp = 0;
539 if (toget > m_client->m_last_resultCount)
540 toget = m_client->m_last_resultCount;
542 if (sr->mediumSetElementSetNames)
544 comp = (Z_RecordComposition *)
545 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
546 comp->which = Z_RecordComp_simple;
547 comp->u.simple = sr->mediumSetElementSetNames;
550 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
551 sr->preferredRecordSyntax, comp))
553 yaz_log (LOG_LOG, "%s Returned cache records for medium set",
555 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
556 new_apdu->u.searchResponse->referenceId = sr->referenceId;
557 new_apdu->u.searchResponse->resultCount =
558 &m_client->m_last_resultCount;
560 new_apdu->u.searchResponse->numberOfRecordsReturned
561 = odr_intdup(odr_encode(), toget);
563 new_apdu->u.searchResponse->presentStatus =
564 odr_intdup(odr_encode(), Z_PresentStatus_success);
565 new_apdu->u.searchResponse->records = (Z_Records*)
566 odr_malloc(odr_encode(), sizeof(Z_Records));
567 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
568 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
569 new_apdu->u.searchResponse->nextResultSetPosition =
570 odr_intdup(odr_encode(), toget+1);
571 send_to_client(new_apdu);
577 // send present request (medium size)
578 yaz_log (LOG_LOG, "%s Optimizing search for medium set",
581 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
582 Z_PresentRequest *pr = new_apdu->u.presentRequest;
583 pr->referenceId = sr->referenceId;
584 pr->resultSetId = sr->resultSetName;
585 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
586 *pr->numberOfRecordsRequested = toget;
587 pr->recordComposition = comp;
588 m_client->m_sr_transform = 1;
592 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
593 m_client->m_last_resultCount <= 0)
595 // large set. Return pseudo-search response immediately
596 yaz_log (LOG_LOG, "%s Optimizing search for large set",
598 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
599 new_apdu->u.searchResponse->referenceId = sr->referenceId;
600 new_apdu->u.searchResponse->resultCount =
601 &m_client->m_last_resultCount;
602 send_to_client(new_apdu);
607 Z_NamePlusRecordList *npr;
608 int toget = m_client->m_last_resultCount;
609 Z_RecordComposition *comp = 0;
611 // send a present request (small set)
613 if (sr->smallSetElementSetNames)
615 comp = (Z_RecordComposition *)
616 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
617 comp->which = Z_RecordComp_simple;
618 comp->u.simple = sr->smallSetElementSetNames;
621 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
622 sr->preferredRecordSyntax, comp))
624 yaz_log (LOG_LOG, "%s Returned cache records for small set",
626 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
627 new_apdu->u.searchResponse->referenceId = sr->referenceId;
628 new_apdu->u.searchResponse->resultCount =
629 &m_client->m_last_resultCount;
631 new_apdu->u.searchResponse->numberOfRecordsReturned
632 = odr_intdup(odr_encode(), toget);
634 new_apdu->u.searchResponse->presentStatus =
635 odr_intdup(odr_encode(), Z_PresentStatus_success);
636 new_apdu->u.searchResponse->records = (Z_Records*)
637 odr_malloc(odr_encode(), sizeof(Z_Records));
638 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
639 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
640 new_apdu->u.searchResponse->nextResultSetPosition =
641 odr_intdup(odr_encode(), toget+1);
642 send_to_client(new_apdu);
647 yaz_log (LOG_LOG, "%s Optimizing search for small set",
649 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
650 Z_PresentRequest *pr = new_apdu->u.presentRequest;
651 pr->referenceId = sr->referenceId;
652 pr->resultSetId = sr->resultSetName;
653 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
654 *pr->numberOfRecordsRequested = toget;
655 pr->recordComposition = comp;
656 m_client->m_sr_transform = 1;
661 else // query doesn't match
663 delete m_client->m_last_query;
664 m_client->m_last_query = this_query;
665 m_client->m_last_ok = 0;
666 m_client->m_cache.clear();
667 m_client->m_resultSetStartPoint = 0;
669 xfree (m_client->m_last_resultSetId);
670 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
672 m_client->m_last_databases.set(sr->num_databaseNames,
673 (const char **) sr->databaseNames);
679 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu, int len)
684 yaz_log (LOG_LOG, "%s Receiving %s from client %d bytes", m_session_str,
685 apdu_name(apdu), len);
687 if (m_bw_hold_PDU) // double incoming PDU. shutdown now.
690 m_bw_stat.add_bytes(len);
691 m_pdu_stat.add_bytes(1);
693 int bw_total = m_bw_stat.get_total();
694 int pdu_total = m_pdu_stat.get_total();
696 yaz_log(LOG_LOG, "%s stat bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
697 m_session_str, bw_total, pdu_total, m_bw_max, m_pdu_max);
700 if (bw_total > m_bw_max)
702 reduce = (bw_total/m_bw_max);
707 if (pdu_total > m_pdu_max)
709 int nreduce = (60/m_pdu_max);
710 reduce = (reduce > nreduce) ? reduce : nreduce;
715 yaz_log(LOG_LOG, "%s Limit delay=%d", m_session_str, reduce);
716 m_bw_hold_PDU = apdu; // save PDU and signal "on hold"
717 timeout(reduce); // call us reduce seconds later
720 recv_Z_PDU_0(apdu); // all fine. Proceed receive PDU as usual
723 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
725 if (m_max_record_retrieve)
727 if (apdu->which == Z_APDU_presentRequest)
729 Z_PresentRequest *pr = apdu->u.presentRequest;
730 if (pr->numberOfRecordsRequested &&
731 *pr->numberOfRecordsRequested > m_max_record_retrieve)
732 *pr->numberOfRecordsRequested = m_max_record_retrieve;
737 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
741 Z_Records *rec = (Z_Records *)
742 odr_malloc (odr, sizeof(*rec));
744 odr_malloc (odr, sizeof(*err));
745 Z_DiagRec *drec = (Z_DiagRec *)
746 odr_malloc (odr, sizeof(*drec));
747 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
748 odr_malloc (odr, sizeof(*dr));
750 rec->which = Z_Records_NSD;
751 rec->u.nonSurrogateDiagnostic = dr;
752 dr->diagnosticSetId =
753 yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
755 dr->which = Z_DefaultDiagFormat_v2Addinfo;
756 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
760 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
762 if (apdu->which == Z_APDU_searchRequest)
764 Z_SearchRequest *sr = apdu->u.searchRequest;
767 err = m_config.check_query(odr_encode(), m_default_target, sr->query,
771 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
773 new_apdu->u.searchResponse->referenceId = sr->referenceId;
774 new_apdu->u.searchResponse->records =
775 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
776 *new_apdu->u.searchResponse->searchStatus = 0;
778 send_to_client(new_apdu);
786 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
788 if (apdu->which == Z_APDU_searchRequest)
790 Z_SearchRequest *sr = apdu->u.searchRequest;
791 if (*sr->smallSetUpperBound > 0 || *sr->largeSetLowerBound > 1)
795 err = m_config.check_syntax(odr_encode(), m_default_target,
796 sr->preferredRecordSyntax,
800 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
802 new_apdu->u.searchResponse->referenceId = sr->referenceId;
803 new_apdu->u.searchResponse->records =
804 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
805 *new_apdu->u.searchResponse->searchStatus = 0;
807 send_to_client(new_apdu);
813 else if (apdu->which == Z_APDU_presentRequest)
815 Z_PresentRequest *pr = apdu->u.presentRequest;
818 err = m_config.check_syntax(odr_encode(), m_default_target,
819 pr->preferredRecordSyntax,
823 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
825 new_apdu->u.presentResponse->referenceId = pr->referenceId;
826 new_apdu->u.presentResponse->records =
827 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
828 *new_apdu->u.presentResponse->presentStatus =
829 Z_PresentStatus_failure;
831 send_to_client(new_apdu);
839 void Yaz_Proxy::recv_Z_PDU_0(Z_APDU *apdu)
841 // Determine our client.
842 m_client = get_client(apdu);
848 m_client->m_server = this;
850 if (apdu->which == Z_APDU_initRequest)
852 if (apdu->u.initRequest->implementationId)
853 yaz_log(LOG_LOG, "%s implementationId: %s",
854 m_session_str, apdu->u.initRequest->implementationId);
855 if (apdu->u.initRequest->implementationName)
856 yaz_log(LOG_LOG, "%s implementationName: %s",
857 m_session_str, apdu->u.initRequest->implementationName);
858 if (apdu->u.initRequest->implementationVersion)
859 yaz_log(LOG_LOG, "%s implementationVersion: %s",
860 m_session_str, apdu->u.initRequest->implementationVersion);
861 if (m_client->m_init_flag)
863 Z_APDU *apdu = m_client->m_initResponse;
864 apdu->u.initResponse->otherInfo = 0;
865 if (m_client->m_cookie && *m_client->m_cookie)
866 set_otherInformationString(apdu, VAL_COOKIE, 1,
868 send_to_client(apdu);
871 m_client->m_init_flag = 1;
873 handle_max_record_retrieve(apdu);
876 apdu = handle_syntax_validation(apdu);
879 apdu = handle_query_validation(apdu);
882 apdu = result_set_optimize(apdu);
885 m_client->timeout(m_target_idletime); // mark it active even
886 // though we didn't use it
890 // delete other info part from PDU before sending to target
891 Z_OtherInformation **oi;
892 get_otherInfoAPDU(apdu, &oi);
896 if (apdu->which == Z_APDU_presentRequest &&
897 m_client->m_resultSetStartPoint == 0)
899 Z_PresentRequest *pr = apdu->u.presentRequest;
900 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
901 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
903 m_client->m_resultSetStartPoint = 0;
905 if (m_client->send_to_target(apdu) < 0)
912 m_client->m_waiting = 1;
915 void Yaz_Proxy::connectNotify()
919 void Yaz_Proxy::shutdown()
921 // only keep if keep_alive flag is set...
922 if (m_keepalive && m_client && m_client->m_waiting == 0)
924 yaz_log (LOG_LOG, "%s Shutdown (client to proxy) keepalive %s",
926 m_client->get_hostname());
927 assert (m_client->m_waiting != 2);
928 // Tell client (if any) that no server connection is there..
929 m_client->m_server = 0;
933 yaz_log (LOG_LOG, "%s Shutdown (client to proxy) close %s",
935 m_client->get_hostname());
936 assert (m_client->m_waiting != 2);
941 yaz_log (LOG_LOG, "%s shutdown (client to proxy) bad state",
947 yaz_log (LOG_LOG, "%s Shutdown (client to proxy)",
953 const char *Yaz_ProxyClient::get_session_str()
957 return m_server->get_session_str();
960 void Yaz_ProxyClient::shutdown()
962 yaz_log (LOG_LOG, "%s Shutdown (proxy to target) %s", get_session_str(),
968 void Yaz_Proxy::failNotify()
970 yaz_log (LOG_LOG, "%s Connection closed by client",
975 void Yaz_ProxyClient::failNotify()
977 yaz_log (LOG_LOG, "%s Connection closed by target %s",
978 get_session_str(), get_hostname());
982 void Yaz_ProxyClient::connectNotify()
984 yaz_log (LOG_LOG, "%s Connection accepted by %s", get_session_str(),
988 to = m_server->get_target_idletime();
994 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
995 *the_PDU_Observable, int fd)
997 return new Yaz_ProxyClient(the_PDU_Observable);
1000 Yaz_ProxyClient::~Yaz_ProxyClient()
1005 m_next->m_prev = m_prev;
1006 m_waiting = 2; // for debugging purposes only.
1007 odr_destroy(m_init_odr);
1008 delete m_last_query;
1009 xfree (m_last_resultSetId);
1013 void Yaz_Proxy::timeoutNotify()
1017 timeout(m_client_idletime);
1018 Z_APDU *apdu = m_bw_hold_PDU;
1024 yaz_log (LOG_LOG, "%s Timeout (client to proxy)", m_session_str);
1029 void Yaz_ProxyClient::timeoutNotify()
1031 yaz_log (LOG_LOG, "%s Timeout (proxy to target) %s", get_session_str(),
1036 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
1037 Yaz_Z_Assoc (the_PDU_Observable)
1044 m_last_resultSetId = 0;
1045 m_last_resultCount = 0;
1049 m_init_odr = odr_createmem (ODR_DECODE);
1051 m_resultSetStartPoint = 0;
1052 m_bytes_sent = m_bytes_recv = 0;
1055 const char *Yaz_Proxy::option(const char *name, const char *value)
1057 if (!strcmp (name, "optimize")) {
1060 m_optimize = xstrdup (value);
1067 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
1069 m_bytes_recv += len;
1071 yaz_log (LOG_LOG, "%s Receiving %s from %s %d bytes", get_session_str(),
1072 apdu_name(apdu), get_hostname(), len);
1073 if (apdu->which == Z_APDU_initResponse)
1075 NMEM nmem = odr_extract_mem (odr_decode());
1076 odr_reset (m_init_odr);
1077 nmem_transfer (m_init_odr->mem, nmem);
1078 m_initResponse = apdu;
1080 Z_InitResponse *ir = apdu->u.initResponse;
1081 char *im0 = ir->implementationName;
1084 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
1091 strcat(im1, "(YAZ Proxy)");
1092 ir->implementationName = im1;
1094 nmem_destroy (nmem);
1096 if (apdu->which == Z_APDU_searchResponse)
1098 Z_SearchResponse *sr = apdu->u.searchResponse;
1099 m_last_resultCount = *sr->resultCount;
1100 int status = *sr->searchStatus;
1101 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
1105 if (sr->records && sr->records->which == Z_Records_DBOSD)
1107 m_cache.add(odr_decode(),
1108 sr->records->u.databaseOrSurDiagnostics, 1,
1113 if (apdu->which == Z_APDU_presentResponse)
1115 Z_PresentResponse *pr = apdu->u.presentResponse;
1119 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1120 Z_SearchResponse *sr = new_apdu->u.searchResponse;
1121 sr->referenceId = pr->referenceId;
1122 *sr->resultCount = m_last_resultCount;
1123 sr->records = pr->records;
1124 sr->nextResultSetPosition = pr->nextResultSetPosition;
1125 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
1129 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
1131 m_cache.add(odr_decode(),
1132 pr->records->u.databaseOrSurDiagnostics,
1133 m_resultSetStartPoint, -1);
1134 m_resultSetStartPoint = 0;
1138 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
1141 m_server->send_to_client(apdu);
1143 if (apdu->which == Z_APDU_close)