1 /* $Id: yaz-proxy.cpp,v 1.4 2004-04-22 07:46:21 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>
45 #include <libxml/parser.h>
46 #include <libxml/tree.h>
47 #include <libxslt/xsltutils.h>
48 #include <libxslt/transform.h>
51 static const char *apdu_name(Z_APDU *apdu)
55 case Z_APDU_initRequest:
57 case Z_APDU_initResponse:
58 return "initResponse";
59 case Z_APDU_searchRequest:
60 return "searchRequest";
61 case Z_APDU_searchResponse:
62 return "searchResponse";
63 case Z_APDU_presentRequest:
64 return "presentRequest";
65 case Z_APDU_presentResponse:
66 return "presentResponse";
67 case Z_APDU_deleteResultSetRequest:
68 return "deleteResultSetRequest";
69 case Z_APDU_deleteResultSetResponse:
70 return "deleteResultSetResponse";
71 case Z_APDU_scanRequest:
73 case Z_APDU_scanResponse:
74 return "scanResponse";
75 case Z_APDU_sortRequest:
77 case Z_APDU_sortResponse:
78 return "sortResponse";
79 case Z_APDU_extendedServicesRequest:
80 return "extendedServicesRequest";
81 case Z_APDU_extendedServicesResponse:
82 return "extendedServicesResponse";
89 static const char *gdu_name(Z_GDU *gdu)
94 return apdu_name(gdu->u.z3950);
95 case Z_GDU_HTTP_Request:
96 return "HTTP Request";
97 case Z_GDU_HTTP_Response:
98 return "HTTP Response";
100 return "Unknown request/response";
103 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
105 Yaz_Z_Assoc(the_PDU_Observable), m_bw_stat(60), m_pdu_stat(60)
107 m_PDU_Observable = the_PDU_Observable;
112 m_keepalive_limit_bw = 500000;
113 m_keepalive_limit_pdu = 1000;
115 m_default_target = 0;
116 m_proxy_authentication = 0;
120 m_client_idletime = 600;
121 m_target_idletime = 600;
122 m_optimize = xstrdup ("1");
123 strcpy(m_session_str, "0 ");
130 m_max_record_retrieve = 0;
134 m_invalid_session = 0;
136 m_referenceId_mem = nmem_create();
139 m_stylesheet_xsp = 0;
140 m_stylesheet_nprl = 0;
141 m_s2z_stylesheet = 0;
144 m_initRequest_apdu = 0;
145 m_initRequest_mem = 0;
146 m_initRequest_preferredMessageSize = 0;
147 m_initRequest_maximumRecordSize = 0;
148 m_initRequest_options = 0;
149 m_initRequest_version = 0;
150 m_apdu_invalid_session = 0;
151 m_mem_invalid_session = 0;
153 m_s2z_odr_search = 0;
155 m_s2z_search_apdu = 0;
156 m_s2z_present_apdu = 0;
157 m_http_keepalive = 0;
160 m_s2z_packing = Z_SRW_recordPacking_string;
161 #if HAVE_GETTIMEOFDAY
162 m_time_tv = xmalloc(sizeof(struct timeval));
163 struct timeval *tv = (struct timeval *) m_time_tv;
173 Yaz_Proxy::~Yaz_Proxy()
175 yaz_log(LOG_LOG, "%sClosed %d/%d sent/recv bytes total", m_session_str,
176 m_bytes_sent, m_bytes_recv);
177 nmem_destroy(m_initRequest_mem);
178 nmem_destroy(m_mem_invalid_session);
179 nmem_destroy(m_referenceId_mem);
181 xfree (m_proxyTarget);
182 xfree (m_default_target);
183 xfree (m_proxy_authentication);
187 if (m_stylesheet_xsp)
188 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
194 odr_destroy(m_s2z_odr_init);
195 if (m_s2z_odr_search)
196 odr_destroy(m_s2z_odr_search);
202 int Yaz_Proxy::set_config(const char *config)
205 m_config = new Yaz_ProxyConfig();
206 xfree(m_config_fname);
207 m_config_fname = xstrdup(config);
208 int r = m_config->read_xml(config);
210 m_config->get_generic_info(&m_log_mask, &m_max_clients);
214 void Yaz_Proxy::set_default_target(const char *target)
216 xfree (m_default_target);
217 m_default_target = 0;
219 m_default_target = (char *) xstrdup (target);
222 void Yaz_Proxy::set_proxy_authentication (const char *auth)
224 xfree (m_proxy_authentication);
225 m_proxy_authentication = 0;
227 m_proxy_authentication = (char *) xstrdup (auth);
230 Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
233 return m_parent->check_reconfigure();
235 Yaz_ProxyConfig *cfg = m_config;
238 yaz_log(LOG_LOG, "reconfigure");
240 if (m_config_fname && cfg)
242 yaz_log(LOG_LOG, "reconfigure config %s", m_config_fname);
243 int r = cfg->read_xml(m_config_fname);
245 yaz_log(LOG_WARN, "reconfigure failed");
249 cfg->get_generic_info(&m_log_mask, &m_max_clients);
253 yaz_log(LOG_LOG, "reconfigure");
259 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
260 *the_PDU_Observable, int fd)
263 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable, this);
264 new_proxy->m_config = 0;
265 new_proxy->m_config_fname = 0;
266 new_proxy->timeout(m_client_idletime);
267 new_proxy->m_target_idletime = m_target_idletime;
268 new_proxy->set_default_target(m_default_target);
269 new_proxy->m_max_clients = m_max_clients;
270 new_proxy->m_log_mask = m_log_mask;
271 new_proxy->set_APDU_log(get_APDU_log());
272 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
273 new_proxy->set_APDU_yazlog(1);
275 new_proxy->set_APDU_yazlog(0);
276 new_proxy->set_proxy_authentication(m_proxy_authentication);
277 sprintf(new_proxy->m_session_str, "%ld:%d ", (long) time(0), m_session_no);
279 yaz_log (LOG_LOG, "%sNew session %s", new_proxy->m_session_str,
280 the_PDU_Observable->getpeername());
284 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
287 Z_OtherInformationUnit *oi;
289 ent.proto = PROTO_Z3950;
290 ent.oclass = CLASS_USERINFO;
291 ent.value = (oid_value) VAL_COOKIE;
292 assert (oid_ent_to_oid (&ent, oid));
294 if (oid_ent_to_oid (&ent, oid) &&
295 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
296 oi->which == Z_OtherInfo_characterInfo)
297 return oi->information.characterInfo;
301 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
304 Z_OtherInformationUnit *oi;
306 ent.proto = PROTO_Z3950;
307 ent.oclass = CLASS_USERINFO;
308 ent.value = (oid_value) VAL_PROXY;
309 if (oid_ent_to_oid (&ent, oid) &&
310 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
311 oi->which == Z_OtherInfo_characterInfo)
312 return oi->information.characterInfo;
316 const char *Yaz_Proxy::load_balance(const char **url)
318 int zurl_in_use[MAX_ZURL_PLEX];
319 int zurl_in_spare[MAX_ZURL_PLEX];
323 for (i = 0; i<MAX_ZURL_PLEX; i++)
326 zurl_in_spare[i] = 0;
328 for (c = m_parent->m_clientPool; c; c = c->m_next)
330 for (i = 0; url[i]; i++)
331 if (!strcmp(url[i], c->get_hostname()))
334 if (c->m_cookie == 0 && c->m_server == 0 && c->m_waiting == 0)
338 int min_use = 100000;
339 int spare_for_min = 0;
341 const char *ret_min = 0;
342 const char *ret_spare = 0;
343 for (i = 0; url[i]; i++)
345 yaz_log(LOG_DEBUG, "%szurl=%s use=%d spare=%d",
346 m_session_str, url[i], zurl_in_use[i], zurl_in_spare[i]);
347 if (min_use > zurl_in_use[i])
350 min_use = zurl_in_use[i];
351 spare_for_min = zurl_in_spare[i];
353 if (max_spare < zurl_in_spare[i])
356 max_spare = zurl_in_spare[i];
359 // use the one with minimum connections if spare is > 3
360 if (spare_for_min > 3)
362 // use one with most spares (if any)
368 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
369 const char *proxy_host)
372 Yaz_Proxy *parent = m_parent;
373 Yaz_ProxyClient *c = m_client;
377 const char *url[MAX_ZURL_PLEX];
378 Yaz_ProxyConfig *cfg = check_reconfigure();
382 /* only to be enabled for debugging... */
383 if (!strcmp(proxy_host, "stop"))
386 xfree(m_default_target);
387 m_default_target = xstrdup(proxy_host);
388 proxy_host = m_default_target;
390 int client_idletime = -1;
391 const char *cql2rpn_fname = 0;
392 url[0] = m_default_target;
397 cfg->get_target_info(proxy_host, url, &m_bw_max,
398 &m_pdu_max, &m_max_record_retrieve,
399 &m_target_idletime, &client_idletime,
400 &parent->m_max_clients,
401 &m_keepalive_limit_bw,
402 &m_keepalive_limit_pdu,
406 if (client_idletime != -1)
408 m_client_idletime = client_idletime;
409 timeout(m_client_idletime);
412 m_cql2rpn.set_pqf_file(cql2rpn_fname);
415 yaz_log(LOG_LOG, "%sNo default target", m_session_str);
418 // we don't handle multiplexing for cookie session, so we just
419 // pick the first one in this case (anonymous users will be able
420 // to use any backend)
421 if (cookie && *cookie)
422 m_proxyTarget = (char*) xstrdup(url[0]);
424 m_proxyTarget = (char*) xstrdup(load_balance(url));
426 if (cookie && *cookie)
427 { // search in sessions with a cookie
428 for (c = parent->m_clientPool; c; c = c->m_next)
431 assert (*c->m_prev == c);
432 if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
433 !strcmp(m_proxyTarget, c->get_hostname()))
436 // The following handles "cancel"
437 // If connection is busy (waiting for PDU) and
438 // we have an initRequest we can safely do re-open
439 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
441 yaz_log (LOG_LOG, "%s REOPEN target=%s", m_session_str,
448 c->m_last_resultCount = 0;
449 c->m_sr_transform = 0;
451 c->m_resultSetStartPoint = 0;
452 c->m_target_idletime = m_target_idletime;
453 if (c->client(m_proxyTarget))
460 c->m_seqno = parent->m_seqno;
461 if (c->m_server && c->m_server != this)
462 c->m_server->m_client = 0;
465 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
472 // don't have a client session yet. Search in session w/o cookie
473 for (c = parent->m_clientPool; c; c = c->m_next)
476 assert (*c->m_prev == c);
477 if (c->m_server == 0 && c->m_cookie == 0 &&
479 !strcmp(m_proxyTarget, c->get_hostname()))
482 yaz_log (LOG_LOG, "%sREUSE %d %s",
483 m_session_str, parent->m_seqno, c->get_hostname());
485 c->m_seqno = parent->m_seqno;
486 assert(c->m_server == 0);
489 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
490 c->set_APDU_yazlog(1);
492 c->set_APDU_yazlog(0);
504 if (apdu->which != Z_APDU_initRequest)
506 yaz_log (LOG_LOG, "%sno init request as first PDU", m_session_str);
509 Z_InitRequest *initRequest = apdu->u.initRequest;
511 if (!initRequest->idAuthentication)
513 if (m_proxy_authentication)
515 initRequest->idAuthentication =
516 (Z_IdAuthentication *)
517 odr_malloc (odr_encode(),
518 sizeof(*initRequest->idAuthentication));
519 initRequest->idAuthentication->which =
520 Z_IdAuthentication_open;
521 initRequest->idAuthentication->u.open =
522 odr_strdup (odr_encode(), m_proxy_authentication);
525 // go through list of clients - and find the lowest/oldest one.
526 Yaz_ProxyClient *c_min = 0;
528 int no_of_clients = 0;
529 if (parent->m_clientPool)
530 yaz_log (LOG_DEBUG, "Existing sessions");
531 for (c = parent->m_clientPool; c; c = c->m_next)
533 yaz_log (LOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
534 c->m_waiting, c->get_hostname(),
535 c->m_cookie ? c->m_cookie : "");
537 if (min_seq < 0 || c->m_seqno < min_seq)
539 min_seq = c->m_seqno;
543 if (no_of_clients >= parent->m_max_clients)
546 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
548 yaz_log (LOG_LOG, "%sMAXCLIENTS %d Destroy %d",
549 m_session_str, parent->m_max_clients, c->m_seqno);
550 if (c->m_server && c->m_server != this)
556 yaz_log (LOG_LOG, "%sMAXCLIENTS %d Reuse %d %d %s",
557 m_session_str, parent->m_max_clients,
558 c->m_seqno, parent->m_seqno, c->get_hostname());
562 c->m_cookie = xstrdup(cookie);
563 c->m_seqno = parent->m_seqno;
564 if (c->m_server && c->m_server != this)
566 c->m_server->m_client = 0;
570 c->m_target_idletime = m_target_idletime;
571 c->timeout(m_target_idletime);
573 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
574 c->set_APDU_yazlog(1);
576 c->set_APDU_yazlog(0);
583 yaz_log (LOG_LOG, "%sNEW %d %s",
584 m_session_str, parent->m_seqno, m_proxyTarget);
585 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), parent);
586 c->m_next = parent->m_clientPool;
588 c->m_next->m_prev = &c->m_next;
589 parent->m_clientPool = c;
590 c->m_prev = &parent->m_clientPool;
596 c->m_cookie = xstrdup(cookie);
598 c->m_seqno = parent->m_seqno;
600 c->m_last_resultCount = 0;
603 c->m_sr_transform = 0;
605 c->m_resultSetStartPoint = 0;
607 if (c->client(m_proxyTarget))
612 c->m_target_idletime = m_target_idletime;
615 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
616 c->set_APDU_yazlog(1);
618 c->set_APDU_yazlog(0);
620 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
624 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
627 for (i = 0; i<num; i++)
630 Z_DefaultDiagFormat *r;
631 Z_DiagRec *p = pp[i];
632 if (p->which != Z_DiagRec_defaultFormat)
634 yaz_log(LOG_LOG, "%sError no diagnostics", m_session_str);
638 r = p->u.defaultFormat;
639 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
640 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
641 yaz_log(LOG_LOG, "%sError unknown diagnostic set", m_session_str);
644 case Z_DefaultDiagFormat_v2Addinfo:
645 yaz_log(LOG_LOG, "%sError %d %s:%s",
647 *r->condition, diagbib1_str(*r->condition),
650 case Z_DefaultDiagFormat_v3Addinfo:
651 yaz_log(LOG_LOG, "%sError %d %s:%s",
653 *r->condition, diagbib1_str(*r->condition),
660 int Yaz_Proxy::convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu)
662 if (!m_stylesheet_xsp || p->num_records <= 0)
663 return 0; /* no XSLT to be done ... */
665 m_stylesheet_offset = 0;
666 m_stylesheet_nprl = p;
667 m_stylesheet_apdu = apdu;
672 void Yaz_Proxy::convert_xsl_delay()
674 Z_NamePlusRecord *npr = m_stylesheet_nprl->records[m_stylesheet_offset];
676 if (npr->which == Z_NamePlusRecord_databaseRecord)
678 Z_External *r = npr->u.databaseRecord;
679 if (r->which == Z_External_octet)
682 fwrite((char*) r->u.octet_aligned->buf, 1, r->u.octet_aligned->len, stdout);
684 xmlDocPtr res, doc = xmlParseMemory(
685 (char*) r->u.octet_aligned->buf,
686 r->u.octet_aligned->len);
689 yaz_log(LOG_LOG, "%sXSLT convert %d",
690 m_session_str, m_stylesheet_offset);
691 res = xsltApplyStylesheet((xsltStylesheetPtr) m_stylesheet_xsp,
698 xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
700 m_stylesheet_nprl->records[m_stylesheet_offset]->
702 z_ext_record(odr_encode(), VAL_TEXT_XML,
703 (char*) out_buf, out_len);
712 m_stylesheet_offset++;
713 if (m_stylesheet_offset == m_stylesheet_nprl->num_records)
715 m_stylesheet_nprl = 0;
717 if (m_stylesheet_xsp)
718 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
720 m_stylesheet_xsp = 0;
721 timeout(m_client_idletime);
722 send_PDU_convert(m_stylesheet_apdu);
728 void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
732 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
733 yaz_marc_t mt = yaz_marc_create();
734 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
735 yaz_marc_iconv(mt, cd);
736 for (i = 0; i < p->num_records; i++)
738 Z_NamePlusRecord *npr = p->records[i];
739 if (npr->which == Z_NamePlusRecord_databaseRecord)
741 Z_External *r = npr->u.databaseRecord;
742 if (r->which == Z_External_octet)
746 if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
747 r->u.octet_aligned->len,
750 npr->u.databaseRecord = z_ext_record(odr_encode(),
759 yaz_marc_destroy(mt);
762 void Yaz_Proxy::logtime()
764 #if HAVE_GETTIMEOFDAY
765 struct timeval *tv = (struct timeval*) m_time_tv;
769 gettimeofday(&tv1, 0);
770 long diff = (tv1.tv_sec - tv->tv_sec)*1000000 +
771 (tv1.tv_usec - tv->tv_usec);
773 yaz_log(LOG_LOG, "%sElapsed %ld.%03ld", m_session_str,
774 diff/1000000, (diff/1000)%1000);
781 int Yaz_Proxy::send_http_response(int code)
783 ODR o = odr_encode();
784 Z_GDU *gdu = z_get_HTTP_Response(o, code);
785 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
787 hres->version = odr_strdup(o, m_http_version);
788 if (m_http_keepalive)
789 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
793 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
795 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
799 int r = send_GDU(gdu, &len);
801 m_bw_stat.add_bytes(len);
806 int Yaz_Proxy::send_srw_response(Z_SRW_PDU *srw_pdu)
808 ODR o = odr_encode();
809 const char *ctype = "text/xml";
810 Z_GDU *gdu = z_get_HTTP_Response(o, 200);
811 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
813 hres->version = odr_strdup(o, m_http_version);
814 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
815 if (m_http_keepalive)
816 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
820 static Z_SOAP_Handler soap_handlers[2] = {
822 {"http://www.loc.gov/zing/srw/", 0,
823 (Z_SOAP_fun) yaz_srw_codec},
828 Z_SOAP *soap_package = (Z_SOAP*) odr_malloc(o, sizeof(Z_SOAP));
829 soap_package->which = Z_SOAP_generic;
830 soap_package->u.generic =
831 (Z_SOAP_Generic *) odr_malloc(o, sizeof(*soap_package->u.generic));
832 soap_package->u.generic->no = 0;
833 soap_package->u.generic->ns = soap_handlers[0].ns;
834 soap_package->u.generic->p = (void *) srw_pdu;
835 soap_package->ns = m_soap_ns;
836 z_soap_codec_enc_xsl(o, &soap_package,
837 &hres->content_buf, &hres->content_len,
838 soap_handlers, 0, m_s2z_stylesheet);
839 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
841 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
845 int r = send_GDU(gdu, &len);
847 m_bw_stat.add_bytes(len);
852 int Yaz_Proxy::send_to_srw_client_error(int srw_error, const char *add)
854 ODR o = odr_encode();
855 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
856 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
858 srw_res->num_diagnostics = 1;
859 srw_res->diagnostics = (Z_SRW_diagnostic *)
860 odr_malloc(o, sizeof(*srw_res->diagnostics));
861 yaz_mk_std_diagnostic(o, srw_res->diagnostics, srw_error, add);
862 return send_srw_response(srw_pdu);
865 int Yaz_Proxy::z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
866 Z_DefaultDiagFormat *ddf)
868 int bib1_code = *ddf->condition;
869 if (bib1_code == 109)
871 srw_res->num_diagnostics = 1;
872 srw_res->diagnostics = (Z_SRW_diagnostic *)
873 odr_malloc(o, sizeof(*srw_res->diagnostics));
874 yaz_mk_std_diagnostic(o, srw_res->diagnostics,
875 yaz_diag_bib1_to_srw(*ddf->condition),
880 int Yaz_Proxy::send_to_srw_client_ok(int hits, Z_Records *records, int start)
882 ODR o = odr_encode();
883 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
884 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
886 srw_res->numberOfRecords = odr_intdup (o, hits);
887 if (records && records->which == Z_Records_DBOSD)
889 srw_res->num_records =
890 records->u.databaseOrSurDiagnostics->num_records;
892 srw_res->records = (Z_SRW_record *)
893 odr_malloc(o, srw_res->num_records * sizeof(Z_SRW_record));
894 for (i = 0; i < srw_res->num_records; i++)
896 Z_NamePlusRecord *npr = records->u.databaseOrSurDiagnostics->records[i];
897 if (npr->which != Z_NamePlusRecord_databaseRecord)
899 srw_res->records[i].recordSchema = "diagnostic";
900 srw_res->records[i].recordPacking = m_s2z_packing;
901 srw_res->records[i].recordData_buf = "67";
902 srw_res->records[i].recordData_len = 2;
903 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
906 Z_External *r = npr->u.databaseRecord;
907 oident *ent = oid_getentbyoid(r->direct_reference);
908 if (r->which == Z_External_octet && ent->value == VAL_TEXT_XML)
910 srw_res->records[i].recordSchema = m_schema;
911 srw_res->records[i].recordPacking = m_s2z_packing;
912 srw_res->records[i].recordData_buf = (char*)
913 r->u.octet_aligned->buf;
914 srw_res->records[i].recordData_len = r->u.octet_aligned->len;
915 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
919 srw_res->records[i].recordSchema = "diagnostic";
920 srw_res->records[i].recordPacking = m_s2z_packing;
921 srw_res->records[i].recordData_buf = "67";
922 srw_res->records[i].recordData_len = 2;
923 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
927 if (records && records->which == Z_Records_NSD)
930 http_code = z_to_srw_diag(odr_encode(), srw_res,
931 records->u.nonSurrogateDiagnostic);
933 return send_http_response(http_code);
935 return send_srw_response(srw_pdu);
939 int Yaz_Proxy::send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
942 Yaz_ProxyConfig *cfg = check_reconfigure();
946 char *b = cfg->get_explain(odr_encode(), 0 /* target */,
947 m_s2z_database, &len);
950 Z_SRW_PDU *res = yaz_srw_get(odr_encode(), Z_SRW_explain_response);
951 Z_SRW_explainResponse *er = res->u.explain_response;
953 er->record.recordData_buf = b;
954 er->record.recordData_len = len;
955 er->record.recordPacking = m_s2z_packing;
956 er->record.recordSchema = "http://explain.z3950.org/dtd/2.0/";
958 er->diagnostics = diagnostics;
959 er->num_diagnostics = num_diagnostics;
960 return send_srw_response(res);
963 return send_http_response(404);
966 int Yaz_Proxy::send_PDU_convert(Z_APDU *apdu)
970 if (apdu->which == Z_APDU_initResponse)
972 Z_InitResponse *res = apdu->u.initResponse;
973 if (*res->result == 0)
975 send_to_srw_client_error(3, 0);
977 else if (!m_s2z_search_apdu)
979 send_srw_explain_response(0, 0);
983 handle_incoming_Z_PDU(m_s2z_search_apdu);
986 else if (m_s2z_search_apdu && apdu->which == Z_APDU_searchResponse)
988 m_s2z_search_apdu = 0;
989 Z_SearchResponse *res = apdu->u.searchResponse;
990 m_s2z_hit_count = *res->resultCount;
991 if (res->records && res->records->which == Z_Records_NSD)
993 send_to_srw_client_ok(0, res->records, 1);
995 else if (m_s2z_present_apdu && m_s2z_hit_count > 0)
998 Z_PresentRequest *pr = m_s2z_present_apdu->u.presentRequest;
1000 if (*pr->resultSetStartPoint <= m_s2z_hit_count)
1002 if (*pr->numberOfRecordsRequested+ *pr->resultSetStartPoint
1004 *pr->numberOfRecordsRequested =
1005 1 + m_s2z_hit_count - *pr->resultSetStartPoint;
1007 handle_incoming_Z_PDU(m_s2z_present_apdu);
1011 m_s2z_present_apdu = 0;
1012 send_to_srw_client_ok(m_s2z_hit_count, res->records, 1);
1015 else if (m_s2z_present_apdu && apdu->which == Z_APDU_presentResponse)
1018 *m_s2z_present_apdu->u.presentRequest->resultSetStartPoint;
1020 m_s2z_present_apdu = 0;
1021 Z_PresentResponse *res = apdu->u.presentResponse;
1022 send_to_srw_client_ok(m_s2z_hit_count, res->records, start);
1028 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
1029 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
1031 int r = send_Z_PDU(apdu, &len);
1032 m_bytes_sent += len;
1033 m_bw_stat.add_bytes(len);
1040 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
1042 int kill_session = 0;
1043 Z_ReferenceId **new_id = get_referenceIdP(apdu);
1046 *new_id = m_referenceId;
1048 if (apdu->which == Z_APDU_searchResponse)
1050 Z_SearchResponse *sr = apdu->u.searchResponse;
1051 Z_Records *p = sr->records;
1052 if (p && p->which == Z_Records_NSD)
1054 Z_DiagRec dr, *dr_p = &dr;
1055 dr.which = Z_DiagRec_defaultFormat;
1056 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1058 *sr->searchStatus = 0;
1059 display_diagrecs(&dr_p, 1);
1063 if (p && p->which == Z_Records_DBOSD)
1066 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1067 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1071 if (sr->resultCount)
1073 yaz_log(LOG_LOG, "%s%d hits", m_session_str,
1075 if (*sr->resultCount < 0)
1077 m_invalid_session = 1;
1080 *sr->searchStatus = 0;
1082 create_nonSurrogateDiagnostics(odr_encode(), 2, 0);
1083 *sr->resultCount = 0;
1088 else if (apdu->which == Z_APDU_presentResponse)
1090 Z_PresentResponse *sr = apdu->u.presentResponse;
1091 Z_Records *p = sr->records;
1092 if (p && p->which == Z_Records_NSD)
1094 Z_DiagRec dr, *dr_p = &dr;
1095 dr.which = Z_DiagRec_defaultFormat;
1096 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1097 if (*sr->presentStatus == Z_PresentStatus_success)
1098 *sr->presentStatus = Z_PresentStatus_failure;
1099 display_diagrecs(&dr_p, 1);
1101 if (p && p->which == Z_Records_DBOSD)
1104 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1105 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1109 else if (apdu->which == Z_APDU_initResponse)
1111 if (m_initRequest_options)
1114 (Odr_bitmask *)odr_malloc(odr_encode(),
1115 sizeof(Odr_bitmask));
1116 ODR_MASK_ZERO(nopt);
1119 for (i = 0; i<24; i++)
1120 if (ODR_MASK_GET(m_initRequest_options, i) &&
1121 ODR_MASK_GET(apdu->u.initResponse->options, i))
1122 ODR_MASK_SET(nopt, i);
1123 apdu->u.initResponse->options = nopt;
1125 if (m_initRequest_version)
1127 Z_ProtocolVersion *nopt =
1128 (Odr_bitmask *)odr_malloc(odr_encode(),
1129 sizeof(Odr_bitmask));
1130 ODR_MASK_ZERO(nopt);
1133 for (i = 0; i<8; i++)
1134 if (ODR_MASK_GET(m_initRequest_version, i) &&
1135 ODR_MASK_GET(apdu->u.initResponse->protocolVersion, i))
1136 ODR_MASK_SET(nopt, i);
1137 apdu->u.initResponse->protocolVersion = nopt;
1139 apdu->u.initResponse->preferredMessageSize =
1140 odr_intdup(odr_encode(),
1141 m_client->m_initResponse_preferredMessageSize >
1142 m_initRequest_preferredMessageSize ?
1143 m_initRequest_preferredMessageSize :
1144 m_client->m_initResponse_preferredMessageSize);
1145 apdu->u.initResponse->maximumRecordSize =
1146 odr_intdup(odr_encode(),
1147 m_client->m_initResponse_maximumRecordSize >
1148 m_initRequest_maximumRecordSize ?
1149 m_initRequest_maximumRecordSize :
1150 m_client->m_initResponse_maximumRecordSize);
1152 int r = send_PDU_convert(apdu);
1159 m_parent->pre_init();
1164 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
1167 const char *apdu_name_tmp = apdu_name(apdu);
1168 int r = send_Z_PDU(apdu, &len);
1169 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
1170 yaz_log (LOG_LOG, "%sSending %s to %s %d bytes",
1172 apdu_name_tmp, get_hostname(), len);
1173 m_bytes_sent += len;
1177 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
1179 if (apdu->which == Z_APDU_presentRequest)
1181 Z_PresentRequest *pr = apdu->u.presentRequest;
1182 int toget = *pr->numberOfRecordsRequested;
1183 int start = *pr->resultSetStartPoint;
1185 yaz_log(LOG_LOG, "%sPresent %s %d+%d", m_session_str,
1186 pr->resultSetId, start, toget);
1188 if (*m_parent->m_optimize == '0')
1191 if (!m_client->m_last_resultSetId)
1193 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1194 new_apdu->u.presentResponse->records =
1195 create_nonSurrogateDiagnostics(odr_encode(), 30,
1197 send_to_client(new_apdu);
1200 if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
1202 if (start+toget-1 > m_client->m_last_resultCount)
1204 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1205 new_apdu->u.presentResponse->records =
1206 create_nonSurrogateDiagnostics(odr_encode(), 13, 0);
1207 send_to_client(new_apdu);
1210 Z_NamePlusRecordList *npr;
1211 if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
1212 pr->preferredRecordSyntax,
1213 pr->recordComposition))
1215 yaz_log (LOG_LOG, "%sReturned cached records for present request",
1217 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1218 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1220 new_apdu->u.presentResponse->numberOfRecordsReturned
1221 = odr_intdup(odr_encode(), toget);
1223 new_apdu->u.presentResponse->records = (Z_Records*)
1224 odr_malloc(odr_encode(), sizeof(Z_Records));
1225 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
1226 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
1227 new_apdu->u.presentResponse->nextResultSetPosition =
1228 odr_intdup(odr_encode(), start+toget);
1230 send_to_client(new_apdu);
1236 if (apdu->which != Z_APDU_searchRequest)
1238 Z_SearchRequest *sr = apdu->u.searchRequest;
1239 Yaz_Z_Query *this_query = new Yaz_Z_Query;
1240 Yaz_Z_Databases this_databases;
1242 this_databases.set(sr->num_databaseNames, (const char **)
1245 this_query->set_Z_Query(sr->query);
1247 char query_str[120];
1248 this_query->print(query_str, sizeof(query_str)-1);
1249 yaz_log(LOG_LOG, "%sSearch %s", m_session_str, query_str);
1251 if (*m_parent->m_optimize != '0' &&
1252 m_client->m_last_ok && m_client->m_last_query &&
1253 m_client->m_last_query->match(this_query) &&
1254 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
1255 m_client->m_last_databases.match(this_databases))
1258 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
1259 m_client->m_last_resultCount < *sr->largeSetLowerBound)
1261 Z_NamePlusRecordList *npr;
1262 int toget = *sr->mediumSetPresentNumber;
1263 Z_RecordComposition *comp = 0;
1265 if (toget > m_client->m_last_resultCount)
1266 toget = m_client->m_last_resultCount;
1268 if (sr->mediumSetElementSetNames)
1270 comp = (Z_RecordComposition *)
1271 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1272 comp->which = Z_RecordComp_simple;
1273 comp->u.simple = sr->mediumSetElementSetNames;
1276 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1277 sr->preferredRecordSyntax, comp))
1279 yaz_log (LOG_LOG, "%sReturned cached records for medium set",
1281 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1282 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1283 new_apdu->u.searchResponse->resultCount =
1284 &m_client->m_last_resultCount;
1286 new_apdu->u.searchResponse->numberOfRecordsReturned
1287 = odr_intdup(odr_encode(), toget);
1289 new_apdu->u.searchResponse->presentStatus =
1290 odr_intdup(odr_encode(), Z_PresentStatus_success);
1291 new_apdu->u.searchResponse->records = (Z_Records*)
1292 odr_malloc(odr_encode(), sizeof(Z_Records));
1293 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1294 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1295 new_apdu->u.searchResponse->nextResultSetPosition =
1296 odr_intdup(odr_encode(), toget+1);
1297 send_to_client(new_apdu);
1303 // send present request (medium size)
1304 yaz_log (LOG_LOG, "%sOptimizing search for medium set",
1307 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1308 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1309 pr->referenceId = sr->referenceId;
1310 pr->resultSetId = sr->resultSetName;
1311 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1312 *pr->numberOfRecordsRequested = toget;
1313 pr->recordComposition = comp;
1314 m_client->m_sr_transform = 1;
1318 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
1319 m_client->m_last_resultCount <= 0)
1321 // large set. Return pseudo-search response immediately
1322 yaz_log (LOG_LOG, "%sOptimizing search for large set",
1324 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1325 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1326 new_apdu->u.searchResponse->resultCount =
1327 &m_client->m_last_resultCount;
1328 send_to_client(new_apdu);
1333 Z_NamePlusRecordList *npr;
1334 int toget = m_client->m_last_resultCount;
1335 Z_RecordComposition *comp = 0;
1337 // send a present request (small set)
1339 if (sr->smallSetElementSetNames)
1341 comp = (Z_RecordComposition *)
1342 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1343 comp->which = Z_RecordComp_simple;
1344 comp->u.simple = sr->smallSetElementSetNames;
1347 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1348 sr->preferredRecordSyntax, comp))
1350 yaz_log (LOG_LOG, "%sReturned cached records for small set",
1352 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1353 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1354 new_apdu->u.searchResponse->resultCount =
1355 &m_client->m_last_resultCount;
1357 new_apdu->u.searchResponse->numberOfRecordsReturned
1358 = odr_intdup(odr_encode(), toget);
1360 new_apdu->u.searchResponse->presentStatus =
1361 odr_intdup(odr_encode(), Z_PresentStatus_success);
1362 new_apdu->u.searchResponse->records = (Z_Records*)
1363 odr_malloc(odr_encode(), sizeof(Z_Records));
1364 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1365 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1366 new_apdu->u.searchResponse->nextResultSetPosition =
1367 odr_intdup(odr_encode(), toget+1);
1368 send_to_client(new_apdu);
1373 yaz_log (LOG_LOG, "%sOptimizing search for small set",
1375 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1376 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1377 pr->referenceId = sr->referenceId;
1378 pr->resultSetId = sr->resultSetName;
1379 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1380 *pr->numberOfRecordsRequested = toget;
1381 pr->recordComposition = comp;
1382 m_client->m_sr_transform = 1;
1387 else // query doesn't match
1389 delete m_client->m_last_query;
1390 m_client->m_last_query = this_query;
1391 m_client->m_last_ok = 0;
1392 m_client->m_cache.clear();
1393 m_client->m_resultSetStartPoint = 0;
1395 xfree (m_client->m_last_resultSetId);
1396 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
1398 m_client->m_last_databases.set(sr->num_databaseNames,
1399 (const char **) sr->databaseNames);
1405 void Yaz_Proxy::inc_request_no()
1407 char *cp = strchr(m_session_str, ' ');
1410 sprintf(cp+1, "%d ", m_request_no);
1413 void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
1417 m_bytes_recv += len;
1419 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
1420 yaz_log (LOG_LOG, "%sReceiving %s from client %d bytes",
1421 m_session_str, gdu_name(apdu), len);
1423 if (m_bw_hold_PDU) // double incoming PDU. shutdown now.
1426 m_bw_stat.add_bytes(len);
1427 m_pdu_stat.add_bytes(1);
1429 #if HAVE_GETTIMEOFDAY
1430 gettimeofday((struct timeval *) m_time_tv, 0);
1433 int bw_total = m_bw_stat.get_total();
1434 int pdu_total = m_pdu_stat.get_total();
1439 if (bw_total > m_bw_max)
1441 reduce = (bw_total/m_bw_max);
1446 if (pdu_total > m_pdu_max)
1448 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
1449 reduce = (reduce > nreduce) ? reduce : nreduce;
1454 yaz_log(LOG_LOG, "%sdelay=%d bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
1455 m_session_str, reduce, bw_total, pdu_total,
1456 m_bw_max, m_pdu_max);
1458 m_bw_hold_PDU = apdu; // save PDU and signal "on hold"
1459 timeout(reduce); // call us reduce seconds later
1461 else if (apdu->which == Z_GDU_Z3950)
1462 handle_incoming_Z_PDU(apdu->u.z3950);
1463 else if (apdu->which == Z_GDU_HTTP_Request)
1464 handle_incoming_HTTP(apdu->u.HTTP_Request);
1467 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
1469 if (m_max_record_retrieve)
1471 if (apdu->which == Z_APDU_presentRequest)
1473 Z_PresentRequest *pr = apdu->u.presentRequest;
1474 if (pr->numberOfRecordsRequested &&
1475 *pr->numberOfRecordsRequested > m_max_record_retrieve)
1476 *pr->numberOfRecordsRequested = m_max_record_retrieve;
1481 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
1483 const char *addinfo)
1485 Z_Records *rec = (Z_Records *)
1486 odr_malloc (odr, sizeof(*rec));
1488 odr_malloc (odr, sizeof(*err));
1489 Z_DiagRec *drec = (Z_DiagRec *)
1490 odr_malloc (odr, sizeof(*drec));
1491 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1492 odr_malloc (odr, sizeof(*dr));
1494 rec->which = Z_Records_NSD;
1495 rec->u.nonSurrogateDiagnostic = dr;
1496 dr->diagnosticSetId =
1497 yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
1498 dr->condition = err;
1499 dr->which = Z_DefaultDiagFormat_v2Addinfo;
1500 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
1504 Z_APDU *Yaz_Proxy::handle_query_transformation(Z_APDU *apdu)
1506 if (apdu->which == Z_APDU_searchRequest &&
1507 apdu->u.searchRequest->query &&
1508 apdu->u.searchRequest->query->which == Z_Query_type_104 &&
1509 apdu->u.searchRequest->query->u.type_104->which == Z_External_CQL)
1511 Z_RPNQuery *rpnquery = 0;
1512 Z_SearchRequest *sr = apdu->u.searchRequest;
1515 yaz_log(LOG_LOG, "%sCQL: %s", m_session_str,
1516 sr->query->u.type_104->u.cql);
1518 int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
1519 &rpnquery, odr_encode(),
1522 yaz_log(LOG_LOG, "%sNo CQL to RPN table", m_session_str);
1525 yaz_log(LOG_LOG, "%sCQL Conversion error %d", m_session_str, r);
1526 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1528 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1529 new_apdu->u.searchResponse->records =
1530 create_nonSurrogateDiagnostics(odr_encode(),
1531 yaz_diag_srw_to_bib1(r),
1533 *new_apdu->u.searchResponse->searchStatus = 0;
1535 send_to_client(new_apdu);
1541 sr->query->which = Z_Query_type_1;
1542 sr->query->u.type_1 = rpnquery;
1549 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
1551 if (apdu->which == Z_APDU_searchRequest)
1553 Z_SearchRequest *sr = apdu->u.searchRequest;
1557 Yaz_ProxyConfig *cfg = check_reconfigure();
1559 err = cfg->check_query(odr_encode(), m_default_target,
1560 sr->query, &addinfo);
1563 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1565 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1566 new_apdu->u.searchResponse->records =
1567 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1568 *new_apdu->u.searchResponse->searchStatus = 0;
1570 send_to_client(new_apdu);
1578 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
1581 if (apdu->which == Z_APDU_searchRequest)
1583 Z_SearchRequest *sr = apdu->u.searchRequest;
1586 Yaz_ProxyConfig *cfg = check_reconfigure();
1588 Z_RecordComposition rc_temp, *rc = 0;
1589 if (sr->smallSetElementSetNames)
1591 rc_temp.which = Z_RecordComp_simple;
1592 rc_temp.u.simple = sr->smallSetElementSetNames;
1596 char *stylesheet_name = 0;
1598 err = cfg->check_syntax(odr_encode(),
1600 sr->preferredRecordSyntax, rc,
1601 &addinfo, &stylesheet_name, &m_schema);
1602 if (stylesheet_name)
1604 m_parent->low_socket_close();
1607 if (m_stylesheet_xsp)
1608 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
1609 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1612 m_stylesheet_offset = 0;
1613 xfree(stylesheet_name);
1615 m_parent->low_socket_open();
1619 sr->preferredRecordSyntax =
1620 yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN, VAL_USMARC);
1625 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1627 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1628 new_apdu->u.searchResponse->records =
1629 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1630 *new_apdu->u.searchResponse->searchStatus = 0;
1632 send_to_client(new_apdu);
1637 else if (apdu->which == Z_APDU_presentRequest)
1639 Z_PresentRequest *pr = apdu->u.presentRequest;
1642 Yaz_ProxyConfig *cfg = check_reconfigure();
1644 char *stylesheet_name = 0;
1646 err = cfg->check_syntax(odr_encode(), m_default_target,
1647 pr->preferredRecordSyntax,
1648 pr->recordComposition,
1649 &addinfo, &stylesheet_name, &m_schema);
1650 if (stylesheet_name)
1652 m_parent->low_socket_close();
1655 if (m_stylesheet_xsp)
1656 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
1657 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1660 m_stylesheet_offset = 0;
1661 xfree(stylesheet_name);
1663 m_parent->low_socket_open();
1667 pr->preferredRecordSyntax =
1668 yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN, VAL_USMARC);
1673 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1675 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1676 new_apdu->u.presentResponse->records =
1677 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1678 *new_apdu->u.presentResponse->presentStatus =
1679 Z_PresentStatus_failure;
1681 send_to_client(new_apdu);
1689 Z_ElementSetNames *Yaz_Proxy::mk_esn_from_schema(ODR o, const char *schema)
1693 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1694 odr_malloc(o, sizeof(Z_ElementSetNames));
1695 esn->which = Z_ElementSetNames_generic;
1696 esn->u.generic = odr_strdup(o, schema);
1700 void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
1704 odr_destroy(m_s2z_odr_init);
1707 if (m_s2z_odr_search)
1709 odr_destroy(m_s2z_odr_search);
1710 m_s2z_odr_search = 0;
1713 m_http_keepalive = 0;
1715 if (!strcmp(hreq->version, "1.0"))
1717 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1718 if (v && !strcmp(v, "Keep-Alive"))
1719 m_http_keepalive = 1;
1721 m_http_keepalive = 0;
1722 m_http_version = "1.0";
1726 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1727 if (v && !strcmp(v, "close"))
1728 m_http_keepalive = 0;
1730 m_http_keepalive = 1;
1731 m_http_version = "1.1";
1734 Z_SRW_PDU *srw_pdu = 0;
1735 Z_SOAP *soap_package = 0;
1737 Z_SRW_diagnostic *diagnostic = 0;
1738 int num_diagnostic = 0;
1739 if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1741 || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1742 &charset, &diagnostic, &num_diagnostic) == 0)
1744 m_s2z_odr_init = odr_createmem(ODR_ENCODE);
1745 m_s2z_odr_search = odr_createmem(ODR_ENCODE);
1746 m_soap_ns = odr_strdup(m_s2z_odr_search, soap_package->ns);
1747 m_s2z_init_apdu = 0;
1748 m_s2z_search_apdu = 0;
1749 m_s2z_present_apdu = 0;
1751 m_s2z_stylesheet = 0;
1753 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
1755 Z_SRW_searchRetrieveRequest *srw_req = srw_pdu->u.request;
1757 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1758 // recordXPath unsupported.
1759 if (srw_req->recordXPath)
1761 yaz_add_srw_diagnostic(odr_decode(),
1762 &diagnostic, &num_diagnostic,
1766 if (srw_req->sort_type != Z_SRW_sort_type_none)
1768 yaz_add_srw_diagnostic(odr_decode(),
1769 &diagnostic, &num_diagnostic,
1773 if (srw_req->stylesheet)
1775 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1777 // set packing for response records ..
1778 if (srw_req->recordPacking &&
1779 !strcmp(srw_req->recordPacking, "xml"))
1780 m_s2z_packing = Z_SRW_recordPacking_XML;
1782 m_s2z_packing = Z_SRW_recordPacking_string;
1786 Z_SRW_PDU *srw_pdu =
1787 yaz_srw_get(odr_encode(),
1788 Z_SRW_searchRetrieve_response);
1789 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1791 srw_res->diagnostics = diagnostic;
1792 srw_res->num_diagnostics = num_diagnostic;
1793 send_srw_response(srw_pdu);
1797 // prepare search PDU
1798 m_s2z_search_apdu = zget_APDU(m_s2z_odr_search,
1799 Z_APDU_searchRequest);
1800 Z_SearchRequest *z_searchRequest =
1801 m_s2z_search_apdu->u.searchRequest;
1803 z_searchRequest->num_databaseNames = 1;
1804 z_searchRequest->databaseNames = (char**)
1805 odr_malloc(m_s2z_odr_search, sizeof(char *));
1806 z_searchRequest->databaseNames[0] = odr_strdup(m_s2z_odr_search,
1809 // query transformation
1810 Z_Query *query = (Z_Query *)
1811 odr_malloc(m_s2z_odr_search, sizeof(Z_Query));
1812 z_searchRequest->query = query;
1814 if (srw_req->query_type == Z_SRW_query_type_cql)
1816 Z_External *ext = (Z_External *)
1817 odr_malloc(m_s2z_odr_search, sizeof(*ext));
1818 ext->direct_reference =
1819 odr_getoidbystr(m_s2z_odr_search, "1.2.840.10003.16.2");
1820 ext->indirect_reference = 0;
1821 ext->descriptor = 0;
1822 ext->which = Z_External_CQL;
1823 ext->u.cql = srw_req->query.cql;
1825 query->which = Z_Query_type_104;
1826 query->u.type_104 = ext;
1828 else if (srw_req->query_type == Z_SRW_query_type_pqf)
1830 Z_RPNQuery *RPNquery;
1831 YAZ_PQF_Parser pqf_parser;
1833 pqf_parser = yaz_pqf_create ();
1835 RPNquery = yaz_pqf_parse (pqf_parser, m_s2z_odr_search,
1836 srw_req->query.pqf);
1839 const char *pqf_msg;
1841 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1842 yaz_log(LOG_LOG, "%*s^\n", off+4, "");
1843 yaz_log(LOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
1845 send_to_srw_client_error(10, 0);
1848 query->which = Z_Query_type_1;
1849 query->u.type_1 = RPNquery;
1851 yaz_pqf_destroy (pqf_parser);
1855 send_to_srw_client_error(7, "query");
1860 m_s2z_present_apdu = 0;
1862 if (srw_req->maximumRecords)
1863 max = *srw_req->maximumRecords;
1865 if (srw_req->startRecord)
1866 start = *srw_req->startRecord;
1869 // Some backend, such as Voyager doesn't honor piggyback
1870 // So we use present always (0 &&).
1871 if (0 && start <= 1) // Z39.50 piggyback
1873 *z_searchRequest->smallSetUpperBound = max;
1874 *z_searchRequest->mediumSetPresentNumber = max;
1875 *z_searchRequest->largeSetLowerBound = 2000000000; // 2e9
1877 z_searchRequest->preferredRecordSyntax =
1878 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1880 if (srw_req->recordSchema)
1882 z_searchRequest->smallSetElementSetNames =
1883 z_searchRequest->mediumSetElementSetNames =
1884 mk_esn_from_schema(m_s2z_odr_search,
1885 srw_req->recordSchema);
1888 else // Z39.50 present
1890 m_s2z_present_apdu = zget_APDU(m_s2z_odr_search,
1891 Z_APDU_presentRequest);
1892 Z_PresentRequest *z_presentRequest =
1893 m_s2z_present_apdu->u.presentRequest;
1894 *z_presentRequest->resultSetStartPoint = start;
1895 *z_presentRequest->numberOfRecordsRequested = max;
1896 z_presentRequest->preferredRecordSyntax =
1897 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1899 if (srw_req->recordSchema)
1901 z_presentRequest->recordComposition =
1902 (Z_RecordComposition *)
1903 odr_malloc(m_s2z_odr_search,
1904 sizeof(Z_RecordComposition));
1905 z_presentRequest->recordComposition->which =
1906 Z_RecordComp_simple;
1907 z_presentRequest->recordComposition->u.simple =
1908 mk_esn_from_schema(m_s2z_odr_search,
1909 srw_req->recordSchema);
1915 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1916 Z_APDU_initRequest);
1918 // prevent m_initRequest_apdu memory from being grabbed
1919 // in Yaz_Proxy::handle_incoming_Z_PDU
1920 m_initRequest_apdu = m_s2z_init_apdu;
1921 handle_incoming_Z_PDU(m_s2z_init_apdu);
1926 handle_incoming_Z_PDU(m_s2z_search_apdu);
1930 else if (srw_pdu->which == Z_SRW_explain_request)
1932 Z_SRW_explainRequest *srw_req = srw_pdu->u.explain_request;
1934 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1937 if (srw_req->stylesheet)
1939 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1941 if (srw_req->recordPacking &&
1942 !strcmp(srw_req->recordPacking, "xml"))
1943 m_s2z_packing = Z_SRW_recordPacking_XML;
1945 m_s2z_packing = Z_SRW_recordPacking_string;
1949 send_srw_explain_response(diagnostic, num_diagnostic);
1955 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1956 Z_APDU_initRequest);
1958 // prevent m_initRequest_apdu memory from being grabbed
1959 // in Yaz_Proxy::handle_incoming_Z_PDU
1960 m_initRequest_apdu = m_s2z_init_apdu;
1961 handle_incoming_Z_PDU(m_s2z_init_apdu);
1964 send_srw_explain_response(0, 0);
1967 else if (srw_pdu->which == Z_SRW_scan_request)
1969 m_s2z_database = odr_strdup(m_s2z_odr_init,
1970 srw_pdu->u.scan_request->database);
1972 yaz_add_srw_diagnostic(odr_decode(),
1973 &diagnostic, &num_diagnostic,
1975 Z_SRW_PDU *srw_pdu =
1976 yaz_srw_get(odr_encode(),
1977 Z_SRW_scan_response);
1978 Z_SRW_scanResponse *srw_res = srw_pdu->u.scan_response;
1980 srw_res->diagnostics = diagnostic;
1981 srw_res->num_diagnostics = num_diagnostic;
1982 send_srw_response(srw_pdu);
1989 send_to_srw_client_error(4, 0);
1993 Z_GDU *p = z_get_HTTP_Response(odr_encode(), 400);
1998 void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
2000 Z_ReferenceId **refid = get_referenceIdP(apdu);
2001 nmem_reset(m_referenceId_mem);
2002 if (refid && *refid)
2004 m_referenceId = (Z_ReferenceId *)
2005 nmem_malloc(m_referenceId_mem, sizeof(*m_referenceId));
2006 m_referenceId->len = m_referenceId->size = (*refid)->len;
2007 m_referenceId->buf = (unsigned char *)
2008 nmem_malloc(m_referenceId_mem, (*refid)->len);
2009 memcpy(m_referenceId->buf, (*refid)->buf, (*refid)->len);
2014 if (!m_client && m_invalid_session)
2016 m_apdu_invalid_session = apdu;
2017 m_mem_invalid_session = odr_extract_mem(odr_decode());
2018 apdu = m_initRequest_apdu;
2021 // Determine our client.
2022 Z_OtherInformation **oi;
2023 get_otherInfoAPDU(apdu, &oi);
2024 m_client = get_client(apdu, get_cookie(oi), get_proxy(oi));
2030 m_client->m_server = this;
2032 if (apdu->which == Z_APDU_initRequest)
2034 if (apdu->u.initRequest->implementationId)
2035 yaz_log(LOG_LOG, "%simplementationId: %s",
2036 m_session_str, apdu->u.initRequest->implementationId);
2037 if (apdu->u.initRequest->implementationName)
2038 yaz_log(LOG_LOG, "%simplementationName: %s",
2039 m_session_str, apdu->u.initRequest->implementationName);
2040 if (apdu->u.initRequest->implementationVersion)
2041 yaz_log(LOG_LOG, "%simplementationVersion: %s",
2042 m_session_str, apdu->u.initRequest->implementationVersion);
2043 if (m_initRequest_apdu == 0)
2045 if (m_initRequest_mem)
2046 nmem_destroy(m_initRequest_mem);
2047 m_initRequest_apdu = apdu;
2048 m_initRequest_mem = odr_extract_mem(odr_decode());
2050 m_initRequest_preferredMessageSize = *apdu->u.initRequest->
2051 preferredMessageSize;
2052 *apdu->u.initRequest->preferredMessageSize = 1024*1024;
2053 m_initRequest_maximumRecordSize = *apdu->u.initRequest->
2055 *apdu->u.initRequest->maximumRecordSize = 1024*1024;
2057 // save init options for the response..
2058 m_initRequest_options = apdu->u.initRequest->options;
2060 apdu->u.initRequest->options =
2061 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2062 sizeof(Odr_bitmask));
2063 ODR_MASK_ZERO(apdu->u.initRequest->options);
2065 for (i = 0; i<= 24; i++)
2066 ODR_MASK_SET(apdu->u.initRequest->options, i);
2067 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2068 Z_Options_negotiationModel);
2069 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2070 Z_Options_concurrentOperations);
2073 m_initRequest_version = apdu->u.initRequest->protocolVersion;
2074 apdu->u.initRequest->protocolVersion =
2075 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2076 sizeof(Odr_bitmask));
2077 ODR_MASK_ZERO(apdu->u.initRequest->protocolVersion);
2079 for (i = 0; i<= 8; i++)
2080 ODR_MASK_SET(apdu->u.initRequest->protocolVersion, i);
2082 if (m_client->m_init_flag)
2084 if (handle_init_response_for_invalid_session(apdu))
2086 if (m_client->m_initResponse)
2088 Z_APDU *apdu2 = m_client->m_initResponse;
2089 apdu2->u.initResponse->otherInfo = 0;
2090 if (m_client->m_cookie && *m_client->m_cookie)
2091 set_otherInformationString(apdu2, VAL_COOKIE, 1,
2092 m_client->m_cookie);
2093 apdu2->u.initResponse->referenceId =
2094 apdu->u.initRequest->referenceId;
2095 apdu2->u.initResponse->options = m_client->m_initResponse_options;
2096 apdu2->u.initResponse->protocolVersion =
2097 m_client->m_initResponse_version;
2099 send_to_client(apdu2);
2103 m_client->m_init_flag = 1;
2105 handle_max_record_retrieve(apdu);
2108 apdu = handle_syntax_validation(apdu);
2111 apdu = handle_query_transformation(apdu);
2114 apdu = handle_query_validation(apdu);
2117 apdu = result_set_optimize(apdu);
2120 m_client->timeout(m_target_idletime); // mark it active even
2121 // though we didn't use it
2125 // delete other info part from PDU before sending to target
2126 get_otherInfoAPDU(apdu, &oi);
2130 if (apdu->which == Z_APDU_presentRequest &&
2131 m_client->m_resultSetStartPoint == 0)
2133 Z_PresentRequest *pr = apdu->u.presentRequest;
2134 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
2135 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
2137 m_client->m_resultSetStartPoint = 0;
2139 if (m_client->send_to_target(apdu) < 0)
2146 m_client->m_waiting = 1;
2149 void Yaz_Proxy::connectNotify()
2153 void Yaz_Proxy::shutdown()
2155 m_invalid_session = 0;
2156 // only keep if keep_alive flag is set...
2158 m_client->m_pdu_recv < m_keepalive_limit_pdu &&
2159 m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
2160 m_client->m_waiting == 0)
2162 yaz_log(LOG_LOG, "%sShutdown (client to proxy) keepalive %s",
2164 m_client->get_hostname());
2165 yaz_log(LOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
2166 m_session_str, m_client->m_pdu_recv,
2167 m_client->m_bytes_sent + m_client->m_bytes_recv,
2168 m_keepalive_limit_bw, m_keepalive_limit_pdu);
2169 assert (m_client->m_waiting != 2);
2170 // Tell client (if any) that no server connection is there..
2171 m_client->m_server = 0;
2172 m_invalid_session = 0;
2176 yaz_log (LOG_LOG, "%sShutdown (client to proxy) close %s",
2178 m_client->get_hostname());
2179 assert (m_client->m_waiting != 2);
2184 yaz_log (LOG_LOG, "%sshutdown (client to proxy) bad state",
2190 yaz_log (LOG_LOG, "%sShutdown (client to proxy)",
2194 m_parent->pre_init();
2198 const char *Yaz_ProxyClient::get_session_str()
2202 return m_server->get_session_str();
2205 void Yaz_ProxyClient::shutdown()
2207 yaz_log (LOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
2213 void Yaz_Proxy::failNotify()
2216 yaz_log (LOG_LOG, "%sConnection closed by client",
2221 void Yaz_ProxyClient::failNotify()
2224 m_server->inc_request_no();
2225 yaz_log (LOG_LOG, "%sConnection closed by target %s",
2226 get_session_str(), get_hostname());
2230 void Yaz_ProxyClient::connectNotify()
2232 const char *s = get_session_str();
2233 const char *h = get_hostname();
2234 yaz_log (LOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
2236 timeout(m_target_idletime);
2241 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
2242 *the_PDU_Observable, int fd)
2244 return new Yaz_ProxyClient(the_PDU_Observable, 0);
2247 Yaz_ProxyClient::~Yaz_ProxyClient()
2252 m_next->m_prev = m_prev;
2253 m_waiting = 2; // for debugging purposes only.
2254 odr_destroy(m_init_odr);
2255 delete m_last_query;
2256 xfree (m_last_resultSetId);
2260 void Yaz_ProxyClient::pre_init_client()
2262 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
2263 Z_InitRequest *req = apdu->u.initRequest;
2266 for (i = 0; i<= 24; i++)
2267 ODR_MASK_SET(req->options, i);
2268 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2269 Z_Options_negotiationModel);
2270 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2271 Z_Options_concurrentOperations);
2272 for (i = 0; i<= 10; i++)
2273 ODR_MASK_SET(req->protocolVersion, i);
2275 if (send_to_target(apdu) < 0)
2286 void Yaz_Proxy::pre_init()
2289 const char *name = 0;
2290 const char *zurl_in_use[MAX_ZURL_PLEX];
2291 int limit_bw, limit_pdu, limit_req;
2292 int target_idletime, client_idletime;
2294 int keepalive_limit_bw, keepalive_limit_pdu;
2296 const char *cql2rpn = 0;
2298 Yaz_ProxyConfig *cfg = check_reconfigure();
2302 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
2307 for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
2308 &limit_bw, &limit_pdu, &limit_req,
2309 &target_idletime, &client_idletime,
2311 &keepalive_limit_bw,
2312 &keepalive_limit_pdu,
2319 for (j = 0; zurl_in_use[j]; j++)
2323 int spare_waiting = 0;
2326 for (c = m_clientPool; c; c = c->m_next)
2328 if (!strcmp(zurl_in_use[j], c->get_hostname()))
2330 if (c->m_cookie == 0)
2332 if (c->m_server == 0)
2344 yaz_log(LOG_LOG, "%spre-init %s %s use=%d other=%d spare=%d "
2345 "sparew=%d preinit=%d",m_session_str,
2346 name, zurl_in_use[j], in_use, other,
2347 spare, spare_waiting, pre_init);
2348 if (spare + spare_waiting < pre_init)
2350 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
2351 c->m_next = m_clientPool;
2353 c->m_next->m_prev = &c->m_next;
2355 c->m_prev = &m_clientPool;
2357 if (m_log_mask & PROXY_LOG_APDU_SERVER)
2358 c->set_APDU_yazlog(1);
2360 c->set_APDU_yazlog(0);
2362 if (c->client(zurl_in_use[j]))
2370 c->m_target_idletime = target_idletime;
2371 c->m_seqno = m_seqno++;
2378 void Yaz_Proxy::timeoutNotify()
2384 timeout(m_client_idletime);
2385 Z_GDU *apdu = m_bw_hold_PDU;
2388 if (apdu->which == Z_GDU_Z3950)
2389 handle_incoming_Z_PDU(apdu->u.z3950);
2390 else if (apdu->which == Z_GDU_HTTP_Request)
2391 handle_incoming_HTTP(apdu->u.HTTP_Request);
2393 else if (m_stylesheet_nprl)
2394 convert_xsl_delay();
2399 yaz_log (LOG_LOG, "%sTimeout (client to proxy)", m_session_str);
2410 void Yaz_Proxy::markInvalid()
2413 m_invalid_session = 1;
2416 void Yaz_ProxyClient::timeoutNotify()
2419 m_server->inc_request_no();
2421 yaz_log (LOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
2425 if (m_server && m_init_flag)
2427 // target timed out in a session that was properly initialized
2428 // server object stay alive but we mark it as invalid so it
2429 // gets initialized again
2430 m_server->markInvalid();
2436 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
2437 Yaz_Proxy *parent) :
2438 Yaz_Z_Assoc (the_PDU_Observable)
2445 m_last_resultSetId = 0;
2446 m_last_resultCount = 0;
2450 m_init_odr = odr_createmem (ODR_DECODE);
2452 m_initResponse_options = 0;
2453 m_initResponse_version = 0;
2454 m_initResponse_preferredMessageSize = 0;
2455 m_initResponse_maximumRecordSize = 0;
2456 m_resultSetStartPoint = 0;
2457 m_bytes_sent = m_bytes_recv = 0;
2461 m_target_idletime = 600;
2465 const char *Yaz_Proxy::option(const char *name, const char *value)
2467 if (!strcmp (name, "optimize")) {
2470 m_optimize = xstrdup (value);
2477 void Yaz_ProxyClient::recv_HTTP_response(Z_HTTP_Response *apdu, int len)
2482 void Yaz_ProxyClient::recv_GDU(Z_GDU *apdu, int len)
2484 if (apdu->which == Z_GDU_Z3950)
2485 recv_Z_PDU(apdu->u.z3950, len);
2486 else if (apdu->which == Z_GDU_HTTP_Response)
2487 recv_HTTP_response(apdu->u.HTTP_Response, len);
2492 int Yaz_Proxy::handle_init_response_for_invalid_session(Z_APDU *apdu)
2494 if (!m_invalid_session)
2496 m_invalid_session = 0;
2497 handle_incoming_Z_PDU(m_apdu_invalid_session);
2498 assert (m_mem_invalid_session);
2499 nmem_destroy(m_mem_invalid_session);
2500 m_mem_invalid_session = 0;
2504 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
2506 m_bytes_recv += len;
2510 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
2511 yaz_log (LOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
2512 apdu_name(apdu), get_hostname(), len);
2513 if (apdu->which == Z_APDU_initResponse)
2515 if (!m_server) // if this is a pre init session , check for more
2517 NMEM nmem = odr_extract_mem (odr_decode());
2518 odr_reset (m_init_odr);
2519 nmem_transfer (m_init_odr->mem, nmem);
2520 m_initResponse = apdu;
2521 m_initResponse_options = apdu->u.initResponse->options;
2522 m_initResponse_version = apdu->u.initResponse->protocolVersion;
2523 m_initResponse_preferredMessageSize =
2524 *apdu->u.initResponse->preferredMessageSize;
2525 m_initResponse_maximumRecordSize =
2526 *apdu->u.initResponse->maximumRecordSize;
2528 Z_InitResponse *ir = apdu->u.initResponse;
2529 char *im0 = ir->implementationName;
2532 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
2539 strcat(im1, "(YAZ Proxy)");
2540 ir->implementationName = im1;
2542 nmem_destroy (nmem);
2544 if (m_server && m_server->handle_init_response_for_invalid_session(apdu))
2547 if (apdu->which == Z_APDU_searchResponse)
2549 Z_SearchResponse *sr = apdu->u.searchResponse;
2550 m_last_resultCount = *sr->resultCount;
2551 int status = *sr->searchStatus;
2552 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
2556 if (sr->records && sr->records->which == Z_Records_DBOSD)
2558 m_cache.add(odr_decode(),
2559 sr->records->u.databaseOrSurDiagnostics, 1,
2564 if (apdu->which == Z_APDU_presentResponse)
2566 Z_PresentResponse *pr = apdu->u.presentResponse;
2570 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2571 Z_SearchResponse *sr = new_apdu->u.searchResponse;
2572 sr->referenceId = pr->referenceId;
2573 *sr->resultCount = m_last_resultCount;
2574 sr->records = pr->records;
2575 sr->nextResultSetPosition = pr->nextResultSetPosition;
2576 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
2580 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
2582 m_cache.add(odr_decode(),
2583 pr->records->u.databaseOrSurDiagnostics,
2584 m_resultSetStartPoint, -1);
2585 m_resultSetStartPoint = 0;
2589 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
2592 m_server->send_to_client(apdu);
2594 if (apdu->which == Z_APDU_close)
2600 void Yaz_Proxy::low_socket_close()
2605 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
2606 if (m_lo_fd[i] >= 0)
2607 ::close(m_lo_fd[i]);
2611 void Yaz_Proxy::low_socket_open()
2616 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
2617 m_lo_fd[i] = open("/dev/null", O_RDONLY);
2621 int Yaz_Proxy::server(const char *addr)
2623 int r = Yaz_Z_Assoc::server(addr);
2626 yaz_log(LOG_LOG, "%sStarted proxy "
2630 " on %s", m_session_str, addr);