1 /* $Id: yaz-proxy.cpp,v 1.8 2004-10-19 18:34:15 adam Exp $
2 Copyright (c) 1998-2004, Index Data.
4 This file is part of the yaz-proxy.
6 YAZ proxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with YAZ proxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
33 #include <sys/types.h>
37 #include <yaz/marcdisp.h>
38 #include <yaz/yaz-iconv.h>
40 #include <yaz/diagbib1.h>
41 #include <yazproxy/proxy.h>
42 #include <yaz/pquery.h>
43 #include <yaz/otherinfo.h>
46 #include <libxml/parser.h>
47 #include <libxml/tree.h>
48 #include <libxslt/xsltutils.h>
49 #include <libxslt/transform.h>
52 static const char *apdu_name(Z_APDU *apdu)
56 case Z_APDU_initRequest:
58 case Z_APDU_initResponse:
59 return "initResponse";
60 case Z_APDU_searchRequest:
61 return "searchRequest";
62 case Z_APDU_searchResponse:
63 return "searchResponse";
64 case Z_APDU_presentRequest:
65 return "presentRequest";
66 case Z_APDU_presentResponse:
67 return "presentResponse";
68 case Z_APDU_deleteResultSetRequest:
69 return "deleteResultSetRequest";
70 case Z_APDU_deleteResultSetResponse:
71 return "deleteResultSetResponse";
72 case Z_APDU_scanRequest:
74 case Z_APDU_scanResponse:
75 return "scanResponse";
76 case Z_APDU_sortRequest:
78 case Z_APDU_sortResponse:
79 return "sortResponse";
80 case Z_APDU_extendedServicesRequest:
81 return "extendedServicesRequest";
82 case Z_APDU_extendedServicesResponse:
83 return "extendedServicesResponse";
90 static const char *gdu_name(Z_GDU *gdu)
95 return apdu_name(gdu->u.z3950);
96 case Z_GDU_HTTP_Request:
97 return "HTTP Request";
98 case Z_GDU_HTTP_Response:
99 return "HTTP Response";
101 return "Unknown request/response";
104 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
106 Yaz_Z_Assoc(the_PDU_Observable), m_bw_stat(60), m_pdu_stat(60)
108 m_PDU_Observable = the_PDU_Observable;
113 m_keepalive_limit_bw = 500000;
114 m_keepalive_limit_pdu = 1000;
116 m_default_target = 0;
117 m_proxy_authentication = 0;
121 m_client_idletime = 600;
122 m_target_idletime = 600;
123 m_optimize = xstrdup ("1");
124 strcpy(m_session_str, "0 ");
131 m_max_record_retrieve = 0;
135 m_invalid_session = 0;
137 m_referenceId_mem = nmem_create();
140 m_stylesheet_xsp = 0;
141 m_stylesheet_nprl = 0;
142 m_s2z_stylesheet = 0;
146 m_backend_charset = 0;
148 m_initRequest_apdu = 0;
149 m_initRequest_mem = 0;
150 m_initRequest_preferredMessageSize = 0;
151 m_initRequest_maximumRecordSize = 0;
152 m_initRequest_options = 0;
153 m_initRequest_version = 0;
154 m_apdu_invalid_session = 0;
155 m_mem_invalid_session = 0;
157 m_s2z_odr_search = 0;
159 m_s2z_search_apdu = 0;
160 m_s2z_present_apdu = 0;
161 m_http_keepalive = 0;
164 m_s2z_packing = Z_SRW_recordPacking_string;
165 #if HAVE_GETTIMEOFDAY
166 m_time_tv = xmalloc(sizeof(struct timeval));
167 struct timeval *tv = (struct timeval *) m_time_tv;
177 Yaz_Proxy::~Yaz_Proxy()
179 yaz_log(LOG_LOG, "%sClosed %d/%d sent/recv bytes total", m_session_str,
180 m_bytes_sent, m_bytes_recv);
181 nmem_destroy(m_initRequest_mem);
182 nmem_destroy(m_mem_invalid_session);
183 nmem_destroy(m_referenceId_mem);
185 xfree(m_proxyTarget);
186 xfree(m_default_target);
187 xfree(m_proxy_authentication);
191 if (m_stylesheet_xsp)
192 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
197 xfree (m_backend_type);
198 xfree (m_backend_charset);
200 odr_destroy(m_s2z_odr_init);
201 if (m_s2z_odr_search)
202 odr_destroy(m_s2z_odr_search);
208 int Yaz_Proxy::set_config(const char *config)
211 m_config = new Yaz_ProxyConfig();
212 xfree(m_config_fname);
213 m_config_fname = xstrdup(config);
214 int r = m_config->read_xml(config);
216 m_config->get_generic_info(&m_log_mask, &m_max_clients);
220 void Yaz_Proxy::set_default_target(const char *target)
222 xfree (m_default_target);
223 m_default_target = 0;
225 m_default_target = (char *) xstrdup (target);
228 void Yaz_Proxy::set_proxy_authentication (const char *auth)
230 xfree (m_proxy_authentication);
231 m_proxy_authentication = 0;
233 m_proxy_authentication = (char *) xstrdup (auth);
236 Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
239 return m_parent->check_reconfigure();
241 Yaz_ProxyConfig *cfg = m_config;
244 yaz_log(LOG_LOG, "reconfigure");
246 if (m_config_fname && cfg)
248 yaz_log(LOG_LOG, "reconfigure config %s", m_config_fname);
249 int r = cfg->read_xml(m_config_fname);
251 yaz_log(LOG_WARN, "reconfigure failed");
255 cfg->get_generic_info(&m_log_mask, &m_max_clients);
259 yaz_log(LOG_LOG, "reconfigure");
265 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
266 *the_PDU_Observable, int fd)
269 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable, this);
270 new_proxy->m_config = 0;
271 new_proxy->m_config_fname = 0;
272 new_proxy->timeout(m_client_idletime);
273 new_proxy->m_target_idletime = m_target_idletime;
274 new_proxy->set_default_target(m_default_target);
275 new_proxy->m_max_clients = m_max_clients;
276 new_proxy->m_log_mask = m_log_mask;
277 new_proxy->set_APDU_log(get_APDU_log());
278 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
279 new_proxy->set_APDU_yazlog(1);
281 new_proxy->set_APDU_yazlog(0);
282 new_proxy->set_proxy_authentication(m_proxy_authentication);
283 sprintf(new_proxy->m_session_str, "%ld:%d ", (long) time(0), m_session_no);
285 yaz_log (LOG_LOG, "%sNew session %s", new_proxy->m_session_str,
286 the_PDU_Observable->getpeername());
290 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
293 Z_OtherInformationUnit *oi;
295 ent.proto = PROTO_Z3950;
296 ent.oclass = CLASS_USERINFO;
297 ent.value = (oid_value) VAL_COOKIE;
298 assert (oid_ent_to_oid (&ent, oid));
300 if (oid_ent_to_oid (&ent, oid) &&
301 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
302 oi->which == Z_OtherInfo_characterInfo)
303 return oi->information.characterInfo;
307 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
310 Z_OtherInformationUnit *oi;
312 ent.proto = PROTO_Z3950;
313 ent.oclass = CLASS_USERINFO;
314 ent.value = (oid_value) VAL_PROXY;
315 if (oid_ent_to_oid (&ent, oid) &&
316 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
317 oi->which == Z_OtherInfo_characterInfo)
318 return oi->information.characterInfo;
322 const char *Yaz_Proxy::load_balance(const char **url)
324 int zurl_in_use[MAX_ZURL_PLEX];
325 int zurl_in_spare[MAX_ZURL_PLEX];
329 for (i = 0; i<MAX_ZURL_PLEX; i++)
332 zurl_in_spare[i] = 0;
334 for (c = m_parent->m_clientPool; c; c = c->m_next)
336 for (i = 0; url[i]; i++)
337 if (!strcmp(url[i], c->get_hostname()))
340 if (c->m_cookie == 0 && c->m_server == 0 && c->m_waiting == 0)
344 int min_use = 100000;
345 int spare_for_min = 0;
347 const char *ret_min = 0;
348 const char *ret_spare = 0;
349 for (i = 0; url[i]; i++)
351 yaz_log(LOG_DEBUG, "%szurl=%s use=%d spare=%d",
352 m_session_str, url[i], zurl_in_use[i], zurl_in_spare[i]);
353 if (min_use > zurl_in_use[i])
356 min_use = zurl_in_use[i];
357 spare_for_min = zurl_in_spare[i];
359 if (max_spare < zurl_in_spare[i])
362 max_spare = zurl_in_spare[i];
365 // use the one with minimum connections if spare is > 3
366 if (spare_for_min > 3)
368 // use one with most spares (if any)
374 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
375 const char *proxy_host)
378 Yaz_Proxy *parent = m_parent;
379 Yaz_ProxyClient *c = m_client;
383 const char *url[MAX_ZURL_PLEX];
384 Yaz_ProxyConfig *cfg = check_reconfigure();
388 /* only to be enabled for debugging... */
389 if (!strcmp(proxy_host, "stop"))
392 xfree(m_default_target);
393 m_default_target = xstrdup(proxy_host);
395 proxy_host = m_default_target;
396 int client_idletime = -1;
397 const char *cql2rpn_fname = 0;
398 url[0] = m_default_target;
403 cfg->get_target_info(proxy_host, url, &m_bw_max,
404 &m_pdu_max, &m_max_record_retrieve,
405 &m_target_idletime, &client_idletime,
406 &parent->m_max_clients,
407 &m_keepalive_limit_bw,
408 &m_keepalive_limit_pdu,
412 if (client_idletime != -1)
414 m_client_idletime = client_idletime;
415 timeout(m_client_idletime);
418 m_cql2rpn.set_pqf_file(cql2rpn_fname);
421 yaz_log(LOG_LOG, "%sNo default target", m_session_str);
424 // we don't handle multiplexing for cookie session, so we just
425 // pick the first one in this case (anonymous users will be able
426 // to use any backend)
427 if (cookie && *cookie)
428 m_proxyTarget = (char*) xstrdup(url[0]);
430 m_proxyTarget = (char*) xstrdup(load_balance(url));
432 if (cookie && *cookie)
433 { // search in sessions with a cookie
434 for (c = parent->m_clientPool; c; c = c->m_next)
437 assert (*c->m_prev == c);
438 if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
439 !strcmp(m_proxyTarget, c->get_hostname()))
442 // The following handles "cancel"
443 // If connection is busy (waiting for PDU) and
444 // we have an initRequest we can safely do re-open
445 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
447 yaz_log (LOG_LOG, "%s REOPEN target=%s", m_session_str,
454 c->m_last_resultCount = 0;
455 c->m_sr_transform = 0;
457 c->m_resultSetStartPoint = 0;
458 c->m_target_idletime = m_target_idletime;
459 if (c->client(m_proxyTarget))
466 c->m_seqno = parent->m_seqno;
467 if (c->m_server && c->m_server != this)
468 c->m_server->m_client = 0;
471 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
478 // don't have a client session yet. Search in session w/o cookie
479 for (c = parent->m_clientPool; c; c = c->m_next)
482 assert (*c->m_prev == c);
483 if (c->m_server == 0 && c->m_cookie == 0 &&
485 !strcmp(m_proxyTarget, c->get_hostname()))
488 yaz_log (LOG_LOG, "%sREUSE %d %s",
489 m_session_str, parent->m_seqno, c->get_hostname());
491 c->m_seqno = parent->m_seqno;
492 assert(c->m_server == 0);
495 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
496 c->set_APDU_yazlog(1);
498 c->set_APDU_yazlog(0);
510 if (apdu->which != Z_APDU_initRequest)
512 yaz_log (LOG_LOG, "%sno init request as first PDU", m_session_str);
515 Z_InitRequest *initRequest = apdu->u.initRequest;
517 if (!initRequest->idAuthentication)
519 if (m_proxy_authentication)
521 initRequest->idAuthentication =
522 (Z_IdAuthentication *)
523 odr_malloc (odr_encode(),
524 sizeof(*initRequest->idAuthentication));
525 initRequest->idAuthentication->which =
526 Z_IdAuthentication_open;
527 initRequest->idAuthentication->u.open =
528 odr_strdup (odr_encode(), m_proxy_authentication);
531 // go through list of clients - and find the lowest/oldest one.
532 Yaz_ProxyClient *c_min = 0;
534 int no_of_clients = 0;
535 if (parent->m_clientPool)
536 yaz_log (LOG_DEBUG, "Existing sessions");
537 for (c = parent->m_clientPool; c; c = c->m_next)
539 yaz_log (LOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
540 c->m_waiting, c->get_hostname(),
541 c->m_cookie ? c->m_cookie : "");
543 if (min_seq < 0 || c->m_seqno < min_seq)
545 min_seq = c->m_seqno;
549 if (no_of_clients >= parent->m_max_clients)
552 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
554 yaz_log (LOG_LOG, "%sMAXCLIENTS %d Destroy %d",
555 m_session_str, parent->m_max_clients, c->m_seqno);
556 if (c->m_server && c->m_server != this)
562 yaz_log (LOG_LOG, "%sMAXCLIENTS %d Reuse %d %d %s",
563 m_session_str, parent->m_max_clients,
564 c->m_seqno, parent->m_seqno, c->get_hostname());
568 c->m_cookie = xstrdup(cookie);
569 c->m_seqno = parent->m_seqno;
570 if (c->m_server && c->m_server != this)
572 c->m_server->m_client = 0;
576 c->m_target_idletime = m_target_idletime;
577 c->timeout(m_target_idletime);
579 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
580 c->set_APDU_yazlog(1);
582 c->set_APDU_yazlog(0);
589 yaz_log (LOG_LOG, "%sNEW %d %s",
590 m_session_str, parent->m_seqno, m_proxyTarget);
591 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), parent);
592 c->m_next = parent->m_clientPool;
594 c->m_next->m_prev = &c->m_next;
595 parent->m_clientPool = c;
596 c->m_prev = &parent->m_clientPool;
602 c->m_cookie = xstrdup(cookie);
604 c->m_seqno = parent->m_seqno;
606 c->m_last_resultCount = 0;
609 c->m_sr_transform = 0;
611 c->m_resultSetStartPoint = 0;
613 if (c->client(m_proxyTarget))
618 c->m_target_idletime = m_target_idletime;
621 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
622 c->set_APDU_yazlog(1);
624 c->set_APDU_yazlog(0);
626 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
630 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
633 for (i = 0; i<num; i++)
636 Z_DefaultDiagFormat *r;
637 Z_DiagRec *p = pp[i];
638 if (p->which != Z_DiagRec_defaultFormat)
640 yaz_log(LOG_LOG, "%sError no diagnostics", m_session_str);
644 r = p->u.defaultFormat;
645 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
646 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
647 yaz_log(LOG_LOG, "%sError unknown diagnostic set", m_session_str);
650 case Z_DefaultDiagFormat_v2Addinfo:
651 yaz_log(LOG_LOG, "%sError %d %s:%s",
653 *r->condition, diagbib1_str(*r->condition),
656 case Z_DefaultDiagFormat_v3Addinfo:
657 yaz_log(LOG_LOG, "%sError %d %s:%s",
659 *r->condition, diagbib1_str(*r->condition),
666 int Yaz_Proxy::convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu)
668 if (!m_stylesheet_xsp || p->num_records <= 0)
669 return 0; /* no XSLT to be done ... */
671 m_stylesheet_offset = 0;
672 m_stylesheet_nprl = p;
673 m_stylesheet_apdu = apdu;
678 void Yaz_Proxy::convert_xsl_delay()
680 Z_NamePlusRecord *npr = m_stylesheet_nprl->records[m_stylesheet_offset];
682 if (npr->which == Z_NamePlusRecord_databaseRecord)
684 Z_External *r = npr->u.databaseRecord;
685 if (r->which == Z_External_octet)
688 fwrite((char*) r->u.octet_aligned->buf, 1, r->u.octet_aligned->len, stdout);
690 xmlDocPtr res, doc = xmlParseMemory(
691 (char*) r->u.octet_aligned->buf,
692 r->u.octet_aligned->len);
695 yaz_log(LOG_LOG, "%sXSLT convert %d",
696 m_session_str, m_stylesheet_offset);
697 res = xsltApplyStylesheet((xsltStylesheetPtr) m_stylesheet_xsp,
704 xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
706 m_stylesheet_nprl->records[m_stylesheet_offset]->
708 z_ext_record(odr_encode(), VAL_TEXT_XML,
709 (char*) out_buf, out_len);
718 m_stylesheet_offset++;
719 if (m_stylesheet_offset == m_stylesheet_nprl->num_records)
721 m_stylesheet_nprl = 0;
723 if (m_stylesheet_xsp)
724 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
726 m_stylesheet_xsp = 0;
727 timeout(m_client_idletime);
728 send_PDU_convert(m_stylesheet_apdu);
734 void Yaz_Proxy::convert_to_frontend_type(Z_NamePlusRecordList *p)
736 if (m_frontend_type != VAL_NONE)
739 for (i = 0; i < p->num_records; i++)
741 Z_NamePlusRecord *npr = p->records[i];
742 if (npr->which == Z_NamePlusRecord_databaseRecord)
744 Z_External *r = npr->u.databaseRecord;
745 if (r->which == Z_External_octet)
747 npr->u.databaseRecord =
748 z_ext_record(odr_encode(),
750 (char*) r->u.octet_aligned->buf,
751 r->u.octet_aligned->len);
758 void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p,
759 const char *backend_charset)
763 if (!backend_charset)
764 backend_charset = "MARC-8";
765 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
766 yaz_marc_t mt = yaz_marc_create();
767 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
768 yaz_marc_iconv(mt, cd);
769 for (i = 0; i < p->num_records; i++)
771 Z_NamePlusRecord *npr = p->records[i];
772 if (npr->which == Z_NamePlusRecord_databaseRecord)
774 Z_External *r = npr->u.databaseRecord;
775 if (r->which == Z_External_octet)
779 if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
780 r->u.octet_aligned->len,
783 npr->u.databaseRecord = z_ext_record(odr_encode(),
792 yaz_marc_destroy(mt);
795 void Yaz_Proxy::logtime()
797 #if HAVE_GETTIMEOFDAY
798 struct timeval *tv = (struct timeval*) m_time_tv;
802 gettimeofday(&tv1, 0);
803 long diff = (tv1.tv_sec - tv->tv_sec)*1000000 +
804 (tv1.tv_usec - tv->tv_usec);
806 yaz_log(LOG_LOG, "%sElapsed %ld.%03ld", m_session_str,
807 diff/1000000, (diff/1000)%1000);
814 int Yaz_Proxy::send_http_response(int code)
816 ODR o = odr_encode();
817 Z_GDU *gdu = z_get_HTTP_Response(o, code);
818 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
820 hres->version = odr_strdup(o, m_http_version);
821 if (m_http_keepalive)
822 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
826 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
828 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
832 int r = send_GDU(gdu, &len);
834 m_bw_stat.add_bytes(len);
839 int Yaz_Proxy::send_srw_response(Z_SRW_PDU *srw_pdu)
841 ODR o = odr_encode();
842 const char *ctype = "text/xml";
843 Z_GDU *gdu = z_get_HTTP_Response(o, 200);
844 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
846 hres->version = odr_strdup(o, m_http_version);
847 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
848 if (m_http_keepalive)
849 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
853 static Z_SOAP_Handler soap_handlers[2] = {
855 {"http://www.loc.gov/zing/srw/", 0,
856 (Z_SOAP_fun) yaz_srw_codec},
861 Z_SOAP *soap_package = (Z_SOAP*) odr_malloc(o, sizeof(Z_SOAP));
862 soap_package->which = Z_SOAP_generic;
863 soap_package->u.generic =
864 (Z_SOAP_Generic *) odr_malloc(o, sizeof(*soap_package->u.generic));
865 soap_package->u.generic->no = 0;
866 soap_package->u.generic->ns = soap_handlers[0].ns;
867 soap_package->u.generic->p = (void *) srw_pdu;
868 soap_package->ns = m_soap_ns;
869 z_soap_codec_enc_xsl(o, &soap_package,
870 &hres->content_buf, &hres->content_len,
871 soap_handlers, 0, m_s2z_stylesheet);
872 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
874 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
878 int r = send_GDU(gdu, &len);
880 m_bw_stat.add_bytes(len);
885 int Yaz_Proxy::send_to_srw_client_error(int srw_error, const char *add)
887 ODR o = odr_encode();
888 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
889 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
891 srw_res->num_diagnostics = 1;
892 srw_res->diagnostics = (Z_SRW_diagnostic *)
893 odr_malloc(o, sizeof(*srw_res->diagnostics));
894 yaz_mk_std_diagnostic(o, srw_res->diagnostics, srw_error, add);
895 return send_srw_response(srw_pdu);
898 int Yaz_Proxy::z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
899 Z_DefaultDiagFormat *ddf)
901 int bib1_code = *ddf->condition;
902 if (bib1_code == 109)
904 srw_res->num_diagnostics = 1;
905 srw_res->diagnostics = (Z_SRW_diagnostic *)
906 odr_malloc(o, sizeof(*srw_res->diagnostics));
907 yaz_mk_std_diagnostic(o, srw_res->diagnostics,
908 yaz_diag_bib1_to_srw(*ddf->condition),
913 int Yaz_Proxy::send_to_srw_client_ok(int hits, Z_Records *records, int start)
915 ODR o = odr_encode();
916 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
917 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
919 srw_res->numberOfRecords = odr_intdup (o, hits);
920 if (records && records->which == Z_Records_DBOSD)
922 srw_res->num_records =
923 records->u.databaseOrSurDiagnostics->num_records;
925 srw_res->records = (Z_SRW_record *)
926 odr_malloc(o, srw_res->num_records * sizeof(Z_SRW_record));
927 for (i = 0; i < srw_res->num_records; i++)
929 Z_NamePlusRecord *npr = records->u.databaseOrSurDiagnostics->records[i];
930 if (npr->which != Z_NamePlusRecord_databaseRecord)
932 srw_res->records[i].recordSchema = "diagnostic";
933 srw_res->records[i].recordPacking = m_s2z_packing;
934 srw_res->records[i].recordData_buf = "67";
935 srw_res->records[i].recordData_len = 2;
936 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
939 Z_External *r = npr->u.databaseRecord;
940 oident *ent = oid_getentbyoid(r->direct_reference);
941 if (r->which == Z_External_octet && ent->value == VAL_TEXT_XML)
943 srw_res->records[i].recordSchema = m_schema;
944 srw_res->records[i].recordPacking = m_s2z_packing;
945 srw_res->records[i].recordData_buf = (char*)
946 r->u.octet_aligned->buf;
947 srw_res->records[i].recordData_len = r->u.octet_aligned->len;
948 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
952 srw_res->records[i].recordSchema = "diagnostic";
953 srw_res->records[i].recordPacking = m_s2z_packing;
954 srw_res->records[i].recordData_buf = "67";
955 srw_res->records[i].recordData_len = 2;
956 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
960 if (records && records->which == Z_Records_NSD)
963 http_code = z_to_srw_diag(odr_encode(), srw_res,
964 records->u.nonSurrogateDiagnostic);
966 return send_http_response(http_code);
968 return send_srw_response(srw_pdu);
972 int Yaz_Proxy::send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
975 Yaz_ProxyConfig *cfg = check_reconfigure();
979 char *b = cfg->get_explain_doc(odr_encode(), 0 /* target */,
980 m_s2z_database, &len);
983 Z_SRW_PDU *res = yaz_srw_get(odr_encode(), Z_SRW_explain_response);
984 Z_SRW_explainResponse *er = res->u.explain_response;
986 er->record.recordData_buf = b;
987 er->record.recordData_len = len;
988 er->record.recordPacking = m_s2z_packing;
989 er->record.recordSchema = "http://explain.z3950.org/dtd/2.0/";
991 er->diagnostics = diagnostics;
992 er->num_diagnostics = num_diagnostics;
993 return send_srw_response(res);
996 return send_http_response(404);
999 int Yaz_Proxy::send_PDU_convert(Z_APDU *apdu)
1003 if (apdu->which == Z_APDU_initResponse)
1005 Z_InitResponse *res = apdu->u.initResponse;
1006 if (*res->result == 0)
1008 send_to_srw_client_error(3, 0);
1010 else if (!m_s2z_search_apdu)
1012 send_srw_explain_response(0, 0);
1016 handle_incoming_Z_PDU(m_s2z_search_apdu);
1019 else if (m_s2z_search_apdu && apdu->which == Z_APDU_searchResponse)
1021 m_s2z_search_apdu = 0;
1022 Z_SearchResponse *res = apdu->u.searchResponse;
1023 m_s2z_hit_count = *res->resultCount;
1024 if (res->records && res->records->which == Z_Records_NSD)
1026 send_to_srw_client_ok(0, res->records, 1);
1028 else if (m_s2z_present_apdu && m_s2z_hit_count > 0)
1031 Z_PresentRequest *pr = m_s2z_present_apdu->u.presentRequest;
1033 if (*pr->resultSetStartPoint <= m_s2z_hit_count)
1035 if (*pr->numberOfRecordsRequested+ *pr->resultSetStartPoint
1037 *pr->numberOfRecordsRequested =
1038 1 + m_s2z_hit_count - *pr->resultSetStartPoint;
1040 handle_incoming_Z_PDU(m_s2z_present_apdu);
1044 m_s2z_present_apdu = 0;
1045 send_to_srw_client_ok(m_s2z_hit_count, res->records, 1);
1048 else if (m_s2z_present_apdu && apdu->which == Z_APDU_presentResponse)
1051 *m_s2z_present_apdu->u.presentRequest->resultSetStartPoint;
1053 m_s2z_present_apdu = 0;
1054 Z_PresentResponse *res = apdu->u.presentResponse;
1055 send_to_srw_client_ok(m_s2z_hit_count, res->records, start);
1061 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
1062 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
1064 int r = send_Z_PDU(apdu, &len);
1065 m_bytes_sent += len;
1066 m_bw_stat.add_bytes(len);
1073 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
1075 int kill_session = 0;
1076 Z_ReferenceId **new_id = get_referenceIdP(apdu);
1079 *new_id = m_referenceId;
1081 if (apdu->which == Z_APDU_searchResponse)
1083 Z_SearchResponse *sr = apdu->u.searchResponse;
1084 Z_Records *p = sr->records;
1085 if (p && p->which == Z_Records_NSD)
1087 Z_DiagRec dr, *dr_p = &dr;
1088 dr.which = Z_DiagRec_defaultFormat;
1089 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1091 *sr->searchStatus = 0;
1092 display_diagrecs(&dr_p, 1);
1096 if (p && p->which == Z_Records_DBOSD)
1099 convert_to_frontend_type(p->u.databaseOrSurDiagnostics);
1101 convert_to_marcxml(p->u.databaseOrSurDiagnostics,
1103 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1107 if (sr->resultCount)
1109 yaz_log(LOG_LOG, "%s%d hits", m_session_str,
1111 if (*sr->resultCount < 0)
1113 m_invalid_session = 1;
1116 *sr->searchStatus = 0;
1118 create_nonSurrogateDiagnostics(odr_encode(), 2, 0);
1119 *sr->resultCount = 0;
1124 else if (apdu->which == Z_APDU_presentResponse)
1126 Z_PresentResponse *sr = apdu->u.presentResponse;
1127 Z_Records *p = sr->records;
1128 if (p && p->which == Z_Records_NSD)
1130 Z_DiagRec dr, *dr_p = &dr;
1131 dr.which = Z_DiagRec_defaultFormat;
1132 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1133 if (*sr->presentStatus == Z_PresentStatus_success)
1134 *sr->presentStatus = Z_PresentStatus_failure;
1135 display_diagrecs(&dr_p, 1);
1137 if (p && p->which == Z_Records_DBOSD)
1140 convert_to_frontend_type(p->u.databaseOrSurDiagnostics);
1142 convert_to_marcxml(p->u.databaseOrSurDiagnostics,
1144 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1148 else if (apdu->which == Z_APDU_initResponse)
1150 if (m_initRequest_options)
1153 (Odr_bitmask *)odr_malloc(odr_encode(),
1154 sizeof(Odr_bitmask));
1155 ODR_MASK_ZERO(nopt);
1158 for (i = 0; i<24; i++)
1159 if (ODR_MASK_GET(m_initRequest_options, i) &&
1160 ODR_MASK_GET(apdu->u.initResponse->options, i))
1161 ODR_MASK_SET(nopt, i);
1162 apdu->u.initResponse->options = nopt;
1164 if (m_initRequest_version)
1166 Z_ProtocolVersion *nopt =
1167 (Odr_bitmask *)odr_malloc(odr_encode(),
1168 sizeof(Odr_bitmask));
1169 ODR_MASK_ZERO(nopt);
1172 for (i = 0; i<8; i++)
1173 if (ODR_MASK_GET(m_initRequest_version, i) &&
1174 ODR_MASK_GET(apdu->u.initResponse->protocolVersion, i))
1175 ODR_MASK_SET(nopt, i);
1176 apdu->u.initResponse->protocolVersion = nopt;
1178 apdu->u.initResponse->preferredMessageSize =
1179 odr_intdup(odr_encode(),
1180 m_client->m_initResponse_preferredMessageSize >
1181 m_initRequest_preferredMessageSize ?
1182 m_initRequest_preferredMessageSize :
1183 m_client->m_initResponse_preferredMessageSize);
1184 apdu->u.initResponse->maximumRecordSize =
1185 odr_intdup(odr_encode(),
1186 m_client->m_initResponse_maximumRecordSize >
1187 m_initRequest_maximumRecordSize ?
1188 m_initRequest_maximumRecordSize :
1189 m_client->m_initResponse_maximumRecordSize);
1191 int r = send_PDU_convert(apdu);
1198 m_parent->pre_init();
1203 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
1206 const char *apdu_name_tmp = apdu_name(apdu);
1207 int r = send_Z_PDU(apdu, &len);
1208 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
1209 yaz_log (LOG_LOG, "%sSending %s to %s %d bytes",
1211 apdu_name_tmp, get_hostname(), len);
1212 m_bytes_sent += len;
1216 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
1218 if (apdu->which == Z_APDU_presentRequest)
1220 Z_PresentRequest *pr = apdu->u.presentRequest;
1221 int toget = *pr->numberOfRecordsRequested;
1222 int start = *pr->resultSetStartPoint;
1224 yaz_log(LOG_LOG, "%sPresent %s %d+%d", m_session_str,
1225 pr->resultSetId, start, toget);
1227 if (*m_parent->m_optimize == '0')
1230 if (!m_client->m_last_resultSetId)
1232 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1233 new_apdu->u.presentResponse->records =
1234 create_nonSurrogateDiagnostics(odr_encode(), 30,
1236 send_to_client(new_apdu);
1239 if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
1241 if (start+toget-1 > m_client->m_last_resultCount)
1243 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1244 new_apdu->u.presentResponse->records =
1245 create_nonSurrogateDiagnostics(odr_encode(), 13, 0);
1246 send_to_client(new_apdu);
1249 Z_NamePlusRecordList *npr;
1251 yaz_log(LOG_LOG, "%sCache lookup %d+%d syntax=%s",
1252 m_session_str, start, toget, yaz_z3950oid_to_str(
1253 pr->preferredRecordSyntax, &oclass));
1255 if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
1256 pr->preferredRecordSyntax,
1257 pr->recordComposition))
1259 yaz_log (LOG_LOG, "%sReturned cached records for present request",
1261 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1262 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1264 new_apdu->u.presentResponse->numberOfRecordsReturned
1265 = odr_intdup(odr_encode(), toget);
1267 new_apdu->u.presentResponse->records = (Z_Records*)
1268 odr_malloc(odr_encode(), sizeof(Z_Records));
1269 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
1270 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
1271 new_apdu->u.presentResponse->nextResultSetPosition =
1272 odr_intdup(odr_encode(), start+toget);
1274 send_to_client(new_apdu);
1280 if (apdu->which != Z_APDU_searchRequest)
1282 Z_SearchRequest *sr = apdu->u.searchRequest;
1283 Yaz_Z_Query *this_query = new Yaz_Z_Query;
1284 Yaz_Z_Databases this_databases;
1286 this_databases.set(sr->num_databaseNames, (const char **)
1289 this_query->set_Z_Query(sr->query);
1291 char query_str[120];
1292 this_query->print(query_str, sizeof(query_str)-1);
1293 yaz_log(LOG_LOG, "%sSearch %s", m_session_str, query_str);
1295 if (*m_parent->m_optimize != '0' &&
1296 m_client->m_last_ok && m_client->m_last_query &&
1297 m_client->m_last_query->match(this_query) &&
1298 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
1299 m_client->m_last_databases.match(this_databases))
1302 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
1303 m_client->m_last_resultCount < *sr->largeSetLowerBound)
1305 Z_NamePlusRecordList *npr;
1306 int toget = *sr->mediumSetPresentNumber;
1307 Z_RecordComposition *comp = 0;
1309 if (toget > m_client->m_last_resultCount)
1310 toget = m_client->m_last_resultCount;
1312 if (sr->mediumSetElementSetNames)
1314 comp = (Z_RecordComposition *)
1315 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1316 comp->which = Z_RecordComp_simple;
1317 comp->u.simple = sr->mediumSetElementSetNames;
1320 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1321 sr->preferredRecordSyntax, comp))
1323 yaz_log (LOG_LOG, "%sReturned cached records for medium set",
1325 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1326 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1327 new_apdu->u.searchResponse->resultCount =
1328 &m_client->m_last_resultCount;
1330 new_apdu->u.searchResponse->numberOfRecordsReturned
1331 = odr_intdup(odr_encode(), toget);
1333 new_apdu->u.searchResponse->presentStatus =
1334 odr_intdup(odr_encode(), Z_PresentStatus_success);
1335 new_apdu->u.searchResponse->records = (Z_Records*)
1336 odr_malloc(odr_encode(), sizeof(Z_Records));
1337 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1338 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1339 new_apdu->u.searchResponse->nextResultSetPosition =
1340 odr_intdup(odr_encode(), toget+1);
1341 send_to_client(new_apdu);
1347 // send present request (medium size)
1348 yaz_log (LOG_LOG, "%sOptimizing search for medium set",
1351 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1352 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1353 pr->referenceId = sr->referenceId;
1354 pr->resultSetId = sr->resultSetName;
1355 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1356 *pr->numberOfRecordsRequested = toget;
1357 pr->recordComposition = comp;
1358 m_client->m_sr_transform = 1;
1362 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
1363 m_client->m_last_resultCount <= 0)
1365 // large set. Return pseudo-search response immediately
1366 yaz_log (LOG_LOG, "%sOptimizing search for large set",
1368 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1369 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1370 new_apdu->u.searchResponse->resultCount =
1371 &m_client->m_last_resultCount;
1372 send_to_client(new_apdu);
1377 Z_NamePlusRecordList *npr;
1378 int toget = m_client->m_last_resultCount;
1379 Z_RecordComposition *comp = 0;
1381 // send a present request (small set)
1383 if (sr->smallSetElementSetNames)
1385 comp = (Z_RecordComposition *)
1386 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1387 comp->which = Z_RecordComp_simple;
1388 comp->u.simple = sr->smallSetElementSetNames;
1391 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1392 sr->preferredRecordSyntax, comp))
1394 yaz_log (LOG_LOG, "%sReturned cached records for small set",
1396 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1397 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1398 new_apdu->u.searchResponse->resultCount =
1399 &m_client->m_last_resultCount;
1401 new_apdu->u.searchResponse->numberOfRecordsReturned
1402 = odr_intdup(odr_encode(), toget);
1404 new_apdu->u.searchResponse->presentStatus =
1405 odr_intdup(odr_encode(), Z_PresentStatus_success);
1406 new_apdu->u.searchResponse->records = (Z_Records*)
1407 odr_malloc(odr_encode(), sizeof(Z_Records));
1408 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1409 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1410 new_apdu->u.searchResponse->nextResultSetPosition =
1411 odr_intdup(odr_encode(), toget+1);
1412 send_to_client(new_apdu);
1417 yaz_log (LOG_LOG, "%sOptimizing search for small set",
1419 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1420 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1421 pr->referenceId = sr->referenceId;
1422 pr->resultSetId = sr->resultSetName;
1423 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1424 *pr->numberOfRecordsRequested = toget;
1425 pr->recordComposition = comp;
1426 m_client->m_sr_transform = 1;
1431 else // query doesn't match
1433 delete m_client->m_last_query;
1434 m_client->m_last_query = this_query;
1435 m_client->m_last_ok = 0;
1436 m_client->m_cache.clear();
1437 m_client->m_resultSetStartPoint = 0;
1439 xfree (m_client->m_last_resultSetId);
1440 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
1442 m_client->m_last_databases.set(sr->num_databaseNames,
1443 (const char **) sr->databaseNames);
1449 void Yaz_Proxy::inc_request_no()
1451 char *cp = strchr(m_session_str, ' ');
1454 sprintf(cp+1, "%d ", m_request_no);
1457 void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
1461 m_bytes_recv += len;
1463 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
1464 yaz_log (LOG_LOG, "%sReceiving %s from client %d bytes",
1465 m_session_str, gdu_name(apdu), len);
1467 if (m_bw_hold_PDU) // double incoming PDU. shutdown now.
1470 m_bw_stat.add_bytes(len);
1471 m_pdu_stat.add_bytes(1);
1473 #if HAVE_GETTIMEOFDAY
1474 gettimeofday((struct timeval *) m_time_tv, 0);
1477 int bw_total = m_bw_stat.get_total();
1478 int pdu_total = m_pdu_stat.get_total();
1483 if (bw_total > m_bw_max)
1485 reduce = (bw_total/m_bw_max);
1490 if (pdu_total > m_pdu_max)
1492 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
1493 reduce = (reduce > nreduce) ? reduce : nreduce;
1498 yaz_log(LOG_LOG, "%sdelay=%d bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
1499 m_session_str, reduce, bw_total, pdu_total,
1500 m_bw_max, m_pdu_max);
1502 m_bw_hold_PDU = apdu; // save PDU and signal "on hold"
1503 timeout(reduce); // call us reduce seconds later
1505 else if (apdu->which == Z_GDU_Z3950)
1506 handle_incoming_Z_PDU(apdu->u.z3950);
1507 else if (apdu->which == Z_GDU_HTTP_Request)
1508 handle_incoming_HTTP(apdu->u.HTTP_Request);
1511 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
1513 if (m_max_record_retrieve)
1515 if (apdu->which == Z_APDU_presentRequest)
1517 Z_PresentRequest *pr = apdu->u.presentRequest;
1518 if (pr->numberOfRecordsRequested &&
1519 *pr->numberOfRecordsRequested > m_max_record_retrieve)
1520 *pr->numberOfRecordsRequested = m_max_record_retrieve;
1525 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
1527 const char *addinfo)
1529 Z_Records *rec = (Z_Records *)
1530 odr_malloc (odr, sizeof(*rec));
1532 odr_malloc (odr, sizeof(*err));
1533 Z_DiagRec *drec = (Z_DiagRec *)
1534 odr_malloc (odr, sizeof(*drec));
1535 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1536 odr_malloc (odr, sizeof(*dr));
1538 rec->which = Z_Records_NSD;
1539 rec->u.nonSurrogateDiagnostic = dr;
1540 dr->diagnosticSetId =
1541 yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
1542 dr->condition = err;
1543 dr->which = Z_DefaultDiagFormat_v2Addinfo;
1544 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
1548 Z_APDU *Yaz_Proxy::handle_query_transformation(Z_APDU *apdu)
1550 if (apdu->which == Z_APDU_searchRequest &&
1551 apdu->u.searchRequest->query &&
1552 apdu->u.searchRequest->query->which == Z_Query_type_104 &&
1553 apdu->u.searchRequest->query->u.type_104->which == Z_External_CQL)
1555 Z_RPNQuery *rpnquery = 0;
1556 Z_SearchRequest *sr = apdu->u.searchRequest;
1559 yaz_log(LOG_LOG, "%sCQL: %s", m_session_str,
1560 sr->query->u.type_104->u.cql);
1562 int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
1563 &rpnquery, odr_encode(),
1566 yaz_log(LOG_LOG, "%sNo CQL to RPN table", m_session_str);
1569 yaz_log(LOG_LOG, "%sCQL Conversion error %d", m_session_str, r);
1570 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1572 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1573 new_apdu->u.searchResponse->records =
1574 create_nonSurrogateDiagnostics(odr_encode(),
1575 yaz_diag_srw_to_bib1(r),
1577 *new_apdu->u.searchResponse->searchStatus = 0;
1579 send_to_client(new_apdu);
1585 sr->query->which = Z_Query_type_1;
1586 sr->query->u.type_1 = rpnquery;
1593 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
1595 if (apdu->which == Z_APDU_searchRequest)
1597 Z_SearchRequest *sr = apdu->u.searchRequest;
1601 Yaz_ProxyConfig *cfg = check_reconfigure();
1603 err = cfg->check_query(odr_encode(), m_default_target,
1604 sr->query, &addinfo);
1607 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1609 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1610 new_apdu->u.searchResponse->records =
1611 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1612 *new_apdu->u.searchResponse->searchStatus = 0;
1614 send_to_client(new_apdu);
1622 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
1625 if (apdu->which == Z_APDU_searchRequest)
1627 Z_SearchRequest *sr = apdu->u.searchRequest;
1630 Yaz_ProxyConfig *cfg = check_reconfigure();
1632 Z_RecordComposition rc_temp, *rc = 0;
1633 if (sr->smallSetElementSetNames)
1635 rc_temp.which = Z_RecordComp_simple;
1636 rc_temp.u.simple = sr->smallSetElementSetNames;
1640 if (sr->preferredRecordSyntax)
1643 ent = oid_getentbyoid(sr->preferredRecordSyntax);
1644 m_frontend_type = ent->value;
1647 m_frontend_type = VAL_NONE;
1649 char *stylesheet_name = 0;
1651 err = cfg->check_syntax(odr_encode(),
1653 sr->preferredRecordSyntax, rc,
1654 &addinfo, &stylesheet_name, &m_schema,
1655 &m_backend_type, &m_backend_charset);
1656 if (stylesheet_name)
1658 m_parent->low_socket_close();
1661 if (m_stylesheet_xsp)
1662 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
1663 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1666 m_stylesheet_offset = 0;
1667 xfree(stylesheet_name);
1669 m_parent->low_socket_open();
1673 sr->smallSetElementSetNames = 0;
1674 sr->mediumSetElementSetNames = 0;
1678 sr->preferredRecordSyntax =
1679 yaz_str_to_z3950oid(odr_encode(), CLASS_RECSYN,
1683 sr->preferredRecordSyntax =
1684 yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN,
1690 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1692 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1693 new_apdu->u.searchResponse->records =
1694 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1695 *new_apdu->u.searchResponse->searchStatus = 0;
1697 send_to_client(new_apdu);
1701 else if (m_backend_type)
1703 sr->preferredRecordSyntax =
1704 yaz_str_to_z3950oid(odr_encode(), CLASS_RECSYN, m_backend_type);
1707 else if (apdu->which == Z_APDU_presentRequest)
1709 Z_PresentRequest *pr = apdu->u.presentRequest;
1712 Yaz_ProxyConfig *cfg = check_reconfigure();
1714 if (pr->preferredRecordSyntax)
1717 ent = oid_getentbyoid(pr->preferredRecordSyntax);
1718 m_frontend_type = ent->value;
1721 m_frontend_type = VAL_NONE;
1723 char *stylesheet_name = 0;
1725 err = cfg->check_syntax(odr_encode(), m_default_target,
1726 pr->preferredRecordSyntax,
1727 pr->recordComposition,
1728 &addinfo, &stylesheet_name, &m_schema,
1729 &m_backend_type, &m_backend_charset);
1730 if (stylesheet_name)
1732 m_parent->low_socket_close();
1735 if (m_stylesheet_xsp)
1736 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
1737 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1740 m_stylesheet_offset = 0;
1741 xfree(stylesheet_name);
1743 m_parent->low_socket_open();
1747 pr->recordComposition = 0;
1751 pr->preferredRecordSyntax =
1752 yaz_str_to_z3950oid(odr_encode(), CLASS_RECSYN,
1756 pr->preferredRecordSyntax =
1757 yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN,
1763 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1765 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1766 new_apdu->u.presentResponse->records =
1767 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1768 *new_apdu->u.presentResponse->presentStatus =
1769 Z_PresentStatus_failure;
1771 send_to_client(new_apdu);
1775 else if (m_backend_type)
1777 pr->preferredRecordSyntax =
1778 yaz_str_to_z3950oid(odr_encode(), CLASS_RECSYN, m_backend_type);
1784 Z_ElementSetNames *Yaz_Proxy::mk_esn_from_schema(ODR o, const char *schema)
1788 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1789 odr_malloc(o, sizeof(Z_ElementSetNames));
1790 esn->which = Z_ElementSetNames_generic;
1791 esn->u.generic = odr_strdup(o, schema);
1795 void Yaz_Proxy::srw_get_client(const char *db, const char **backend_db)
1798 Yaz_ProxyConfig *cfg = check_reconfigure();
1800 t = cfg->get_explain_name(db, backend_db);
1802 if (m_client && m_default_target && t && strcmp(m_default_target, t))
1809 xfree(m_default_target);
1810 m_default_target = xstrdup(t);
1814 void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
1818 odr_destroy(m_s2z_odr_init);
1821 if (m_s2z_odr_search)
1823 odr_destroy(m_s2z_odr_search);
1824 m_s2z_odr_search = 0;
1827 m_http_keepalive = 0;
1829 if (!strcmp(hreq->version, "1.0"))
1831 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1832 if (v && !strcmp(v, "Keep-Alive"))
1833 m_http_keepalive = 1;
1835 m_http_keepalive = 0;
1836 m_http_version = "1.0";
1840 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1841 if (v && !strcmp(v, "close"))
1842 m_http_keepalive = 0;
1844 m_http_keepalive = 1;
1845 m_http_version = "1.1";
1848 Z_SRW_PDU *srw_pdu = 0;
1849 Z_SOAP *soap_package = 0;
1851 Z_SRW_diagnostic *diagnostic = 0;
1852 int num_diagnostic = 0;
1853 if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1855 || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1856 &charset, &diagnostic, &num_diagnostic) == 0)
1858 m_s2z_odr_init = odr_createmem(ODR_ENCODE);
1859 m_s2z_odr_search = odr_createmem(ODR_ENCODE);
1860 m_soap_ns = odr_strdup(m_s2z_odr_search, soap_package->ns);
1861 m_s2z_init_apdu = 0;
1862 m_s2z_search_apdu = 0;
1863 m_s2z_present_apdu = 0;
1865 m_s2z_stylesheet = 0;
1867 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
1869 Z_SRW_searchRetrieveRequest *srw_req = srw_pdu->u.request;
1871 const char *backend_db = srw_req->database;
1872 srw_get_client(srw_req->database, &backend_db);
1874 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1875 // recordXPath unsupported.
1876 if (srw_req->recordXPath)
1878 yaz_add_srw_diagnostic(odr_decode(),
1879 &diagnostic, &num_diagnostic,
1883 if (srw_req->sort_type != Z_SRW_sort_type_none)
1885 yaz_add_srw_diagnostic(odr_decode(),
1886 &diagnostic, &num_diagnostic,
1890 if (srw_req->stylesheet)
1892 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1894 // set packing for response records ..
1895 if (srw_req->recordPacking &&
1896 !strcmp(srw_req->recordPacking, "xml"))
1897 m_s2z_packing = Z_SRW_recordPacking_XML;
1899 m_s2z_packing = Z_SRW_recordPacking_string;
1903 Z_SRW_PDU *srw_pdu =
1904 yaz_srw_get(odr_encode(),
1905 Z_SRW_searchRetrieve_response);
1906 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1908 srw_res->diagnostics = diagnostic;
1909 srw_res->num_diagnostics = num_diagnostic;
1910 send_srw_response(srw_pdu);
1914 // prepare search PDU
1915 m_s2z_search_apdu = zget_APDU(m_s2z_odr_search,
1916 Z_APDU_searchRequest);
1917 Z_SearchRequest *z_searchRequest =
1918 m_s2z_search_apdu->u.searchRequest;
1920 z_searchRequest->num_databaseNames = 1;
1921 z_searchRequest->databaseNames = (char**)
1922 odr_malloc(m_s2z_odr_search, sizeof(char *));
1923 z_searchRequest->databaseNames[0] = odr_strdup(m_s2z_odr_search,
1926 // query transformation
1927 Z_Query *query = (Z_Query *)
1928 odr_malloc(m_s2z_odr_search, sizeof(Z_Query));
1929 z_searchRequest->query = query;
1931 if (srw_req->query_type == Z_SRW_query_type_cql)
1933 Z_External *ext = (Z_External *)
1934 odr_malloc(m_s2z_odr_search, sizeof(*ext));
1935 ext->direct_reference =
1936 odr_getoidbystr(m_s2z_odr_search, "1.2.840.10003.16.2");
1937 ext->indirect_reference = 0;
1938 ext->descriptor = 0;
1939 ext->which = Z_External_CQL;
1940 ext->u.cql = srw_req->query.cql;
1942 query->which = Z_Query_type_104;
1943 query->u.type_104 = ext;
1945 else if (srw_req->query_type == Z_SRW_query_type_pqf)
1947 Z_RPNQuery *RPNquery;
1948 YAZ_PQF_Parser pqf_parser;
1950 pqf_parser = yaz_pqf_create ();
1952 RPNquery = yaz_pqf_parse (pqf_parser, m_s2z_odr_search,
1953 srw_req->query.pqf);
1956 const char *pqf_msg;
1958 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1959 yaz_log(LOG_LOG, "%*s^\n", off+4, "");
1960 yaz_log(LOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
1962 send_to_srw_client_error(10, 0);
1965 query->which = Z_Query_type_1;
1966 query->u.type_1 = RPNquery;
1968 yaz_pqf_destroy (pqf_parser);
1972 send_to_srw_client_error(7, "query");
1977 m_s2z_present_apdu = 0;
1979 if (srw_req->maximumRecords)
1980 max = *srw_req->maximumRecords;
1982 if (srw_req->startRecord)
1983 start = *srw_req->startRecord;
1986 // Some backend, such as Voyager doesn't honor piggyback
1987 // So we use present always (0 &&).
1988 if (0 && start <= 1) // Z39.50 piggyback
1990 *z_searchRequest->smallSetUpperBound = max;
1991 *z_searchRequest->mediumSetPresentNumber = max;
1992 *z_searchRequest->largeSetLowerBound = 2000000000; // 2e9
1994 z_searchRequest->preferredRecordSyntax =
1995 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1997 if (srw_req->recordSchema)
1999 z_searchRequest->smallSetElementSetNames =
2000 z_searchRequest->mediumSetElementSetNames =
2001 mk_esn_from_schema(m_s2z_odr_search,
2002 srw_req->recordSchema);
2005 else // Z39.50 present
2007 m_s2z_present_apdu = zget_APDU(m_s2z_odr_search,
2008 Z_APDU_presentRequest);
2009 Z_PresentRequest *z_presentRequest =
2010 m_s2z_present_apdu->u.presentRequest;
2011 *z_presentRequest->resultSetStartPoint = start;
2012 *z_presentRequest->numberOfRecordsRequested = max;
2013 z_presentRequest->preferredRecordSyntax =
2014 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
2016 if (srw_req->recordSchema)
2018 z_presentRequest->recordComposition =
2019 (Z_RecordComposition *)
2020 odr_malloc(m_s2z_odr_search,
2021 sizeof(Z_RecordComposition));
2022 z_presentRequest->recordComposition->which =
2023 Z_RecordComp_simple;
2024 z_presentRequest->recordComposition->u.simple =
2025 mk_esn_from_schema(m_s2z_odr_search,
2026 srw_req->recordSchema);
2032 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
2033 Z_APDU_initRequest);
2035 // prevent m_initRequest_apdu memory from being grabbed
2036 // in Yaz_Proxy::handle_incoming_Z_PDU
2037 m_initRequest_apdu = m_s2z_init_apdu;
2038 handle_incoming_Z_PDU(m_s2z_init_apdu);
2043 handle_incoming_Z_PDU(m_s2z_search_apdu);
2047 else if (srw_pdu->which == Z_SRW_explain_request)
2049 Z_SRW_explainRequest *srw_req = srw_pdu->u.explain_request;
2051 const char *backend_db = srw_req->database;
2052 srw_get_client(srw_req->database, &backend_db);
2054 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
2057 if (srw_req->stylesheet)
2059 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
2061 if (srw_req->recordPacking &&
2062 !strcmp(srw_req->recordPacking, "xml"))
2063 m_s2z_packing = Z_SRW_recordPacking_XML;
2065 m_s2z_packing = Z_SRW_recordPacking_string;
2069 send_srw_explain_response(diagnostic, num_diagnostic);
2075 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
2076 Z_APDU_initRequest);
2078 // prevent m_initRequest_apdu memory from being grabbed
2079 // in Yaz_Proxy::handle_incoming_Z_PDU
2080 m_initRequest_apdu = m_s2z_init_apdu;
2081 handle_incoming_Z_PDU(m_s2z_init_apdu);
2084 send_srw_explain_response(0, 0);
2087 else if (srw_pdu->which == Z_SRW_scan_request)
2089 m_s2z_database = odr_strdup(m_s2z_odr_init,
2090 srw_pdu->u.scan_request->database);
2092 yaz_add_srw_diagnostic(odr_decode(),
2093 &diagnostic, &num_diagnostic,
2095 Z_SRW_PDU *srw_pdu =
2096 yaz_srw_get(odr_encode(),
2097 Z_SRW_scan_response);
2098 Z_SRW_scanResponse *srw_res = srw_pdu->u.scan_response;
2100 srw_res->diagnostics = diagnostic;
2101 srw_res->num_diagnostics = num_diagnostic;
2102 send_srw_response(srw_pdu);
2109 send_to_srw_client_error(4, 0);
2113 Z_GDU *p = z_get_HTTP_Response(odr_encode(), 400);
2118 void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
2120 Z_ReferenceId **refid = get_referenceIdP(apdu);
2121 nmem_reset(m_referenceId_mem);
2122 if (refid && *refid)
2124 m_referenceId = (Z_ReferenceId *)
2125 nmem_malloc(m_referenceId_mem, sizeof(*m_referenceId));
2126 m_referenceId->len = m_referenceId->size = (*refid)->len;
2127 m_referenceId->buf = (unsigned char *)
2128 nmem_malloc(m_referenceId_mem, (*refid)->len);
2129 memcpy(m_referenceId->buf, (*refid)->buf, (*refid)->len);
2134 if (!m_client && m_invalid_session)
2136 m_apdu_invalid_session = apdu;
2137 m_mem_invalid_session = odr_extract_mem(odr_decode());
2138 apdu = m_initRequest_apdu;
2141 // Determine our client.
2142 Z_OtherInformation **oi;
2143 get_otherInfoAPDU(apdu, &oi);
2144 m_client = get_client(apdu, get_cookie(oi), get_proxy(oi));
2150 m_client->m_server = this;
2152 if (apdu->which == Z_APDU_initRequest)
2154 if (apdu->u.initRequest->implementationId)
2155 yaz_log(LOG_LOG, "%simplementationId: %s",
2156 m_session_str, apdu->u.initRequest->implementationId);
2157 if (apdu->u.initRequest->implementationName)
2158 yaz_log(LOG_LOG, "%simplementationName: %s",
2159 m_session_str, apdu->u.initRequest->implementationName);
2160 if (apdu->u.initRequest->implementationVersion)
2161 yaz_log(LOG_LOG, "%simplementationVersion: %s",
2162 m_session_str, apdu->u.initRequest->implementationVersion);
2163 if (m_initRequest_apdu == 0)
2165 if (m_initRequest_mem)
2166 nmem_destroy(m_initRequest_mem);
2167 m_initRequest_apdu = apdu;
2168 m_initRequest_mem = odr_extract_mem(odr_decode());
2170 m_initRequest_preferredMessageSize = *apdu->u.initRequest->
2171 preferredMessageSize;
2172 *apdu->u.initRequest->preferredMessageSize = 1024*1024;
2173 m_initRequest_maximumRecordSize = *apdu->u.initRequest->
2175 *apdu->u.initRequest->maximumRecordSize = 1024*1024;
2177 // save init options for the response..
2178 m_initRequest_options = apdu->u.initRequest->options;
2180 apdu->u.initRequest->options =
2181 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2182 sizeof(Odr_bitmask));
2183 ODR_MASK_ZERO(apdu->u.initRequest->options);
2185 for (i = 0; i<= 24; i++)
2186 ODR_MASK_SET(apdu->u.initRequest->options, i);
2187 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2188 Z_Options_negotiationModel);
2189 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2190 Z_Options_concurrentOperations);
2193 m_initRequest_version = apdu->u.initRequest->protocolVersion;
2194 apdu->u.initRequest->protocolVersion =
2195 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2196 sizeof(Odr_bitmask));
2197 ODR_MASK_ZERO(apdu->u.initRequest->protocolVersion);
2199 for (i = 0; i<= 8; i++)
2200 ODR_MASK_SET(apdu->u.initRequest->protocolVersion, i);
2202 if (m_client->m_init_flag)
2204 if (handle_init_response_for_invalid_session(apdu))
2206 if (m_client->m_initResponse)
2208 Z_APDU *apdu2 = m_client->m_initResponse;
2209 apdu2->u.initResponse->otherInfo = 0;
2210 if (m_client->m_cookie && *m_client->m_cookie)
2211 set_otherInformationString(apdu2, VAL_COOKIE, 1,
2212 m_client->m_cookie);
2213 apdu2->u.initResponse->referenceId =
2214 apdu->u.initRequest->referenceId;
2215 apdu2->u.initResponse->options = m_client->m_initResponse_options;
2216 apdu2->u.initResponse->protocolVersion =
2217 m_client->m_initResponse_version;
2219 send_to_client(apdu2);
2223 m_client->m_init_flag = 1;
2225 handle_max_record_retrieve(apdu);
2228 apdu = handle_syntax_validation(apdu);
2231 apdu = handle_query_transformation(apdu);
2234 apdu = handle_query_validation(apdu);
2237 apdu = result_set_optimize(apdu);
2240 m_client->timeout(m_target_idletime); // mark it active even
2241 // though we didn't use it
2245 // delete other info part from PDU before sending to target
2246 get_otherInfoAPDU(apdu, &oi);
2250 if (apdu->which == Z_APDU_presentRequest &&
2251 m_client->m_resultSetStartPoint == 0)
2253 Z_PresentRequest *pr = apdu->u.presentRequest;
2254 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
2255 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
2257 m_client->m_resultSetStartPoint = 0;
2259 if (m_client->send_to_target(apdu) < 0)
2266 m_client->m_waiting = 1;
2269 void Yaz_Proxy::connectNotify()
2273 void Yaz_Proxy::releaseClient()
2275 xfree(m_proxyTarget);
2277 m_invalid_session = 0;
2278 // only keep if keep_alive flag is set...
2280 m_client->m_pdu_recv < m_keepalive_limit_pdu &&
2281 m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
2282 m_client->m_waiting == 0)
2284 yaz_log(LOG_LOG, "%sShutdown (client to proxy) keepalive %s",
2286 m_client->get_hostname());
2287 yaz_log(LOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
2288 m_session_str, m_client->m_pdu_recv,
2289 m_client->m_bytes_sent + m_client->m_bytes_recv,
2290 m_keepalive_limit_bw, m_keepalive_limit_pdu);
2291 assert (m_client->m_waiting != 2);
2292 // Tell client (if any) that no server connection is there..
2293 m_client->m_server = 0;
2294 m_invalid_session = 0;
2299 yaz_log (LOG_LOG, "%sShutdown (client to proxy) close %s",
2301 m_client->get_hostname());
2302 assert (m_client->m_waiting != 2);
2308 yaz_log (LOG_LOG, "%sshutdown (client to proxy) bad state",
2314 yaz_log (LOG_LOG, "%sShutdown (client to proxy)",
2318 m_parent->pre_init();
2321 void Yaz_Proxy::shutdown()
2327 const char *Yaz_ProxyClient::get_session_str()
2331 return m_server->get_session_str();
2334 void Yaz_ProxyClient::shutdown()
2336 yaz_log (LOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
2342 void Yaz_Proxy::failNotify()
2345 yaz_log (LOG_LOG, "%sConnection closed by client",
2350 void Yaz_ProxyClient::failNotify()
2353 m_server->inc_request_no();
2354 yaz_log (LOG_LOG, "%sConnection closed by target %s",
2355 get_session_str(), get_hostname());
2359 void Yaz_ProxyClient::connectNotify()
2361 const char *s = get_session_str();
2362 const char *h = get_hostname();
2363 yaz_log (LOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
2365 timeout(m_target_idletime);
2370 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
2371 *the_PDU_Observable, int fd)
2373 return new Yaz_ProxyClient(the_PDU_Observable, 0);
2376 Yaz_ProxyClient::~Yaz_ProxyClient()
2381 m_next->m_prev = m_prev;
2382 m_waiting = 2; // for debugging purposes only.
2383 odr_destroy(m_init_odr);
2384 delete m_last_query;
2385 xfree (m_last_resultSetId);
2389 void Yaz_ProxyClient::pre_init_client()
2391 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
2392 Z_InitRequest *req = apdu->u.initRequest;
2395 for (i = 0; i<= 24; i++)
2396 ODR_MASK_SET(req->options, i);
2397 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2398 Z_Options_negotiationModel);
2399 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2400 Z_Options_concurrentOperations);
2401 for (i = 0; i<= 10; i++)
2402 ODR_MASK_SET(req->protocolVersion, i);
2404 if (send_to_target(apdu) < 0)
2415 void Yaz_Proxy::pre_init()
2418 const char *name = 0;
2419 const char *zurl_in_use[MAX_ZURL_PLEX];
2420 int limit_bw, limit_pdu, limit_req;
2421 int target_idletime, client_idletime;
2423 int keepalive_limit_bw, keepalive_limit_pdu;
2425 const char *cql2rpn = 0;
2427 Yaz_ProxyConfig *cfg = check_reconfigure();
2431 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
2436 for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
2437 &limit_bw, &limit_pdu, &limit_req,
2438 &target_idletime, &client_idletime,
2440 &keepalive_limit_bw,
2441 &keepalive_limit_pdu,
2448 for (j = 0; zurl_in_use[j]; j++)
2452 int spare_waiting = 0;
2455 for (c = m_clientPool; c; c = c->m_next)
2457 if (!strcmp(zurl_in_use[j], c->get_hostname()))
2459 if (c->m_cookie == 0)
2461 if (c->m_server == 0)
2473 yaz_log(LOG_LOG, "%spre-init %s %s use=%d other=%d spare=%d "
2474 "sparew=%d preinit=%d",m_session_str,
2475 name, zurl_in_use[j], in_use, other,
2476 spare, spare_waiting, pre_init);
2477 if (spare + spare_waiting < pre_init)
2479 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
2480 c->m_next = m_clientPool;
2482 c->m_next->m_prev = &c->m_next;
2484 c->m_prev = &m_clientPool;
2486 if (m_log_mask & PROXY_LOG_APDU_SERVER)
2487 c->set_APDU_yazlog(1);
2489 c->set_APDU_yazlog(0);
2491 if (c->client(zurl_in_use[j]))
2499 c->m_target_idletime = target_idletime;
2500 c->m_seqno = m_seqno++;
2507 void Yaz_Proxy::timeoutNotify()
2513 timeout(m_client_idletime);
2514 Z_GDU *apdu = m_bw_hold_PDU;
2517 if (apdu->which == Z_GDU_Z3950)
2518 handle_incoming_Z_PDU(apdu->u.z3950);
2519 else if (apdu->which == Z_GDU_HTTP_Request)
2520 handle_incoming_HTTP(apdu->u.HTTP_Request);
2522 else if (m_stylesheet_nprl)
2523 convert_xsl_delay();
2528 yaz_log (LOG_LOG, "%sTimeout (client to proxy)", m_session_str);
2539 void Yaz_Proxy::markInvalid()
2542 m_invalid_session = 1;
2545 void Yaz_ProxyClient::timeoutNotify()
2548 m_server->inc_request_no();
2550 yaz_log (LOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
2554 if (m_server && m_init_flag)
2556 // target timed out in a session that was properly initialized
2557 // server object stay alive but we mark it as invalid so it
2558 // gets initialized again
2559 m_server->markInvalid();
2565 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
2566 Yaz_Proxy *parent) :
2567 Yaz_Z_Assoc (the_PDU_Observable)
2574 m_last_resultSetId = 0;
2575 m_last_resultCount = 0;
2579 m_init_odr = odr_createmem (ODR_DECODE);
2581 m_initResponse_options = 0;
2582 m_initResponse_version = 0;
2583 m_initResponse_preferredMessageSize = 0;
2584 m_initResponse_maximumRecordSize = 0;
2585 m_resultSetStartPoint = 0;
2586 m_bytes_sent = m_bytes_recv = 0;
2590 m_target_idletime = 600;
2594 const char *Yaz_Proxy::option(const char *name, const char *value)
2596 if (!strcmp (name, "optimize")) {
2599 m_optimize = xstrdup (value);
2606 void Yaz_ProxyClient::recv_HTTP_response(Z_HTTP_Response *apdu, int len)
2611 void Yaz_ProxyClient::recv_GDU(Z_GDU *apdu, int len)
2613 if (apdu->which == Z_GDU_Z3950)
2614 recv_Z_PDU(apdu->u.z3950, len);
2615 else if (apdu->which == Z_GDU_HTTP_Response)
2616 recv_HTTP_response(apdu->u.HTTP_Response, len);
2621 int Yaz_Proxy::handle_init_response_for_invalid_session(Z_APDU *apdu)
2623 if (!m_invalid_session)
2625 m_invalid_session = 0;
2626 handle_incoming_Z_PDU(m_apdu_invalid_session);
2627 assert (m_mem_invalid_session);
2628 nmem_destroy(m_mem_invalid_session);
2629 m_mem_invalid_session = 0;
2633 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
2635 m_bytes_recv += len;
2639 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
2640 yaz_log (LOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
2641 apdu_name(apdu), get_hostname(), len);
2642 if (apdu->which == Z_APDU_initResponse)
2644 if (!m_server) // if this is a pre init session , check for more
2646 NMEM nmem = odr_extract_mem (odr_decode());
2647 odr_reset (m_init_odr);
2648 nmem_transfer (m_init_odr->mem, nmem);
2649 m_initResponse = apdu;
2650 m_initResponse_options = apdu->u.initResponse->options;
2651 m_initResponse_version = apdu->u.initResponse->protocolVersion;
2652 m_initResponse_preferredMessageSize =
2653 *apdu->u.initResponse->preferredMessageSize;
2654 m_initResponse_maximumRecordSize =
2655 *apdu->u.initResponse->maximumRecordSize;
2657 Z_InitResponse *ir = apdu->u.initResponse;
2658 char *im0 = ir->implementationName;
2661 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
2668 strcat(im1, "(YAZ Proxy)");
2669 ir->implementationName = im1;
2671 nmem_destroy (nmem);
2673 if (m_server && m_server->handle_init_response_for_invalid_session(apdu))
2676 if (apdu->which == Z_APDU_searchResponse)
2678 Z_SearchResponse *sr = apdu->u.searchResponse;
2679 m_last_resultCount = *sr->resultCount;
2680 int status = *sr->searchStatus;
2681 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
2685 if (sr->records && sr->records->which == Z_Records_DBOSD)
2687 m_cache.add(odr_decode(),
2688 sr->records->u.databaseOrSurDiagnostics, 1,
2693 if (apdu->which == Z_APDU_presentResponse)
2695 Z_PresentResponse *pr = apdu->u.presentResponse;
2699 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2700 Z_SearchResponse *sr = new_apdu->u.searchResponse;
2701 sr->referenceId = pr->referenceId;
2702 *sr->resultCount = m_last_resultCount;
2703 sr->records = pr->records;
2704 sr->nextResultSetPosition = pr->nextResultSetPosition;
2705 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
2709 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
2711 m_cache.add(odr_decode(),
2712 pr->records->u.databaseOrSurDiagnostics,
2713 m_resultSetStartPoint, -1);
2714 m_resultSetStartPoint = 0;
2718 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
2721 m_server->send_to_client(apdu);
2723 if (apdu->which == Z_APDU_close)
2729 void Yaz_Proxy::low_socket_close()
2734 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
2735 if (m_lo_fd[i] >= 0)
2736 ::close(m_lo_fd[i]);
2740 void Yaz_Proxy::low_socket_open()
2745 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
2746 m_lo_fd[i] = open("/dev/null", O_RDONLY);
2750 int Yaz_Proxy::server(const char *addr)
2752 int r = Yaz_Z_Assoc::server(addr);
2755 yaz_log(LOG_LOG, "%sStarted proxy "
2759 " on %s", m_session_str, addr);