2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.110 2004-03-17 10:49:22 adam Exp $
11 #include <sys/types.h>
15 #include <yaz/marcdisp.h>
16 #include <yaz/yaz-iconv.h>
18 #include <yaz/diagbib1.h>
19 #include <yaz++/proxy.h>
20 #include <yaz/pquery.h>
22 static const char *apdu_name(Z_APDU *apdu)
26 case Z_APDU_initRequest:
28 case Z_APDU_initResponse:
29 return "initResponse";
30 case Z_APDU_searchRequest:
31 return "searchRequest";
32 case Z_APDU_searchResponse:
33 return "searchResponse";
34 case Z_APDU_presentRequest:
35 return "presentRequest";
36 case Z_APDU_presentResponse:
37 return "presentResponse";
38 case Z_APDU_deleteResultSetRequest:
39 return "deleteResultSetRequest";
40 case Z_APDU_deleteResultSetResponse:
41 return "deleteResultSetResponse";
42 case Z_APDU_scanRequest:
44 case Z_APDU_scanResponse:
45 return "scanResponse";
46 case Z_APDU_sortRequest:
48 case Z_APDU_sortResponse:
49 return "sortResponse";
50 case Z_APDU_extendedServicesRequest:
51 return "extendedServicesRequest";
52 case Z_APDU_extendedServicesResponse:
53 return "extendedServicesResponse";
60 static const char *gdu_name(Z_GDU *gdu)
65 return apdu_name(gdu->u.z3950);
66 case Z_GDU_HTTP_Request:
67 return "HTTP Request";
68 case Z_GDU_HTTP_Response:
69 return "HTTP Response";
71 return "Unknown request/response";
74 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
76 Yaz_Z_Assoc(the_PDU_Observable), m_bw_stat(60), m_pdu_stat(60)
78 m_PDU_Observable = the_PDU_Observable;
83 m_keepalive_limit_bw = 500000;
84 m_keepalive_limit_pdu = 1000;
87 m_proxy_authentication = 0;
91 m_client_idletime = 600;
92 m_target_idletime = 600;
93 m_optimize = xstrdup ("1");
94 strcpy(m_session_str, "0 ");
101 m_max_record_retrieve = 0;
105 m_invalid_session = 0;
107 m_referenceId_mem = nmem_create();
110 m_stylesheet_xsp = 0;
111 m_stylesheet_nprl = 0;
112 m_s2z_stylesheet = 0;
115 m_initRequest_apdu = 0;
116 m_initRequest_mem = 0;
117 m_initRequest_preferredMessageSize = 0;
118 m_initRequest_maximumRecordSize = 0;
119 m_initRequest_options = 0;
120 m_initRequest_version = 0;
121 m_apdu_invalid_session = 0;
122 m_mem_invalid_session = 0;
124 m_s2z_odr_search = 0;
126 m_s2z_search_apdu = 0;
127 m_s2z_present_apdu = 0;
128 m_http_keepalive = 0;
131 m_s2z_packing = Z_SRW_recordPacking_string;
132 m_time_tv.tv_sec = 0;
133 m_time_tv.tv_usec = 0;
138 Yaz_Proxy::~Yaz_Proxy()
140 yaz_log(LOG_LOG, "%sClosed %d/%d sent/recv bytes total", m_session_str,
141 m_bytes_sent, m_bytes_recv);
142 nmem_destroy(m_initRequest_mem);
143 nmem_destroy(m_mem_invalid_session);
144 nmem_destroy(m_referenceId_mem);
146 xfree (m_proxyTarget);
147 xfree (m_default_target);
148 xfree (m_proxy_authentication);
151 if (m_stylesheet_xsp)
152 xsltFreeStylesheet(m_stylesheet_xsp);
156 odr_destroy(m_s2z_odr_init);
157 if (m_s2z_odr_search)
158 odr_destroy(m_s2z_odr_search);
164 int Yaz_Proxy::set_config(const char *config)
167 m_config = new Yaz_ProxyConfig();
168 xfree(m_config_fname);
169 m_config_fname = xstrdup(config);
170 int r = m_config->read_xml(config);
172 m_config->get_generic_info(&m_log_mask, &m_max_clients);
176 void Yaz_Proxy::set_default_target(const char *target)
178 xfree (m_default_target);
179 m_default_target = 0;
181 m_default_target = (char *) xstrdup (target);
184 void Yaz_Proxy::set_proxy_authentication (const char *auth)
186 xfree (m_proxy_authentication);
187 m_proxy_authentication = 0;
189 m_proxy_authentication = (char *) xstrdup (auth);
192 Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
195 return m_parent->check_reconfigure();
197 Yaz_ProxyConfig *cfg = m_config;
200 yaz_log(LOG_LOG, "reconfigure");
202 if (m_config_fname && cfg)
204 yaz_log(LOG_LOG, "reconfigure config %s", m_config_fname);
205 int r = cfg->read_xml(m_config_fname);
207 yaz_log(LOG_WARN, "reconfigure failed");
211 cfg->get_generic_info(&m_log_mask, &m_max_clients);
215 yaz_log(LOG_LOG, "reconfigure");
221 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
222 *the_PDU_Observable, int fd)
225 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable, this);
226 new_proxy->m_config = 0;
227 new_proxy->m_config_fname = 0;
228 new_proxy->timeout(m_client_idletime);
229 new_proxy->m_target_idletime = m_target_idletime;
230 new_proxy->set_default_target(m_default_target);
231 new_proxy->m_max_clients = m_max_clients;
232 new_proxy->m_log_mask = m_log_mask;
233 new_proxy->set_APDU_log(get_APDU_log());
234 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
235 new_proxy->set_APDU_yazlog(1);
237 new_proxy->set_APDU_yazlog(0);
238 new_proxy->set_proxy_authentication(m_proxy_authentication);
239 sprintf(new_proxy->m_session_str, "%ld:%d ", (long) time(0), m_session_no);
241 yaz_log (LOG_LOG, "%sNew session %s", new_proxy->m_session_str,
242 the_PDU_Observable->getpeername());
246 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
249 Z_OtherInformationUnit *oi;
251 ent.proto = PROTO_Z3950;
252 ent.oclass = CLASS_USERINFO;
253 ent.value = (oid_value) VAL_COOKIE;
254 assert (oid_ent_to_oid (&ent, oid));
256 if (oid_ent_to_oid (&ent, oid) &&
257 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
258 oi->which == Z_OtherInfo_characterInfo)
259 return oi->information.characterInfo;
263 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
266 Z_OtherInformationUnit *oi;
268 ent.proto = PROTO_Z3950;
269 ent.oclass = CLASS_USERINFO;
270 ent.value = (oid_value) VAL_PROXY;
271 if (oid_ent_to_oid (&ent, oid) &&
272 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
273 oi->which == Z_OtherInfo_characterInfo)
274 return oi->information.characterInfo;
278 const char *Yaz_Proxy::load_balance(const char **url)
280 int zurl_in_use[MAX_ZURL_PLEX];
281 int zurl_in_spare[MAX_ZURL_PLEX];
285 for (i = 0; i<MAX_ZURL_PLEX; i++)
288 zurl_in_spare[i] = 0;
290 for (c = m_parent->m_clientPool; c; c = c->m_next)
292 for (i = 0; url[i]; i++)
293 if (!strcmp(url[i], c->get_hostname()))
296 if (c->m_cookie == 0 && c->m_server == 0 && c->m_waiting == 0)
300 int min_use = 100000;
301 int spare_for_min = 0;
303 const char *ret_min = 0;
304 const char *ret_spare = 0;
305 for (i = 0; url[i]; i++)
307 yaz_log(LOG_DEBUG, "%szurl=%s use=%d spare=%d",
308 m_session_str, url[i], zurl_in_use[i], zurl_in_spare[i]);
309 if (min_use > zurl_in_use[i])
312 min_use = zurl_in_use[i];
313 spare_for_min = zurl_in_spare[i];
315 if (max_spare < zurl_in_spare[i])
318 max_spare = zurl_in_spare[i];
321 // use the one with minimum connections if spare is > 3
322 if (spare_for_min > 3)
324 // use one with most spares (if any)
330 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
331 const char *proxy_host)
334 Yaz_Proxy *parent = m_parent;
335 Yaz_ProxyClient *c = m_client;
339 const char *url[MAX_ZURL_PLEX];
340 Yaz_ProxyConfig *cfg = check_reconfigure();
344 /* only to be enabled for debugging... */
345 if (!strcmp(proxy_host, "stop"))
348 xfree(m_default_target);
349 m_default_target = xstrdup(proxy_host);
350 proxy_host = m_default_target;
352 int client_idletime = -1;
353 const char *cql2rpn_fname = 0;
354 url[0] = m_default_target;
359 cfg->get_target_info(proxy_host, url, &m_bw_max,
360 &m_pdu_max, &m_max_record_retrieve,
361 &m_target_idletime, &client_idletime,
362 &parent->m_max_clients,
363 &m_keepalive_limit_bw,
364 &m_keepalive_limit_pdu,
368 if (client_idletime != -1)
370 m_client_idletime = client_idletime;
371 timeout(m_client_idletime);
374 m_cql2rpn.set_pqf_file(cql2rpn_fname);
377 yaz_log(LOG_LOG, "%sNo default target", m_session_str);
380 // we don't handle multiplexing for cookie session, so we just
381 // pick the first one in this case (anonymous users will be able
382 // to use any backend)
383 if (cookie && *cookie)
384 m_proxyTarget = (char*) xstrdup(url[0]);
386 m_proxyTarget = (char*) xstrdup(load_balance(url));
388 if (cookie && *cookie)
389 { // search in sessions with a cookie
390 for (c = parent->m_clientPool; c; c = c->m_next)
393 assert (*c->m_prev == c);
394 if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
395 !strcmp(m_proxyTarget, c->get_hostname()))
398 // The following handles "cancel"
399 // If connection is busy (waiting for PDU) and
400 // we have an initRequest we can safely do re-open
401 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
403 yaz_log (LOG_LOG, "%s REOPEN target=%s", m_session_str,
410 c->m_last_resultCount = 0;
411 c->m_sr_transform = 0;
413 c->m_resultSetStartPoint = 0;
414 c->m_target_idletime = m_target_idletime;
415 if (c->client(m_proxyTarget))
422 c->m_seqno = parent->m_seqno;
423 if (c->m_server && c->m_server != this)
424 c->m_server->m_client = 0;
427 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
434 // don't have a client session yet. Search in session w/o cookie
435 for (c = parent->m_clientPool; c; c = c->m_next)
438 assert (*c->m_prev == c);
439 if (c->m_server == 0 && c->m_cookie == 0 &&
441 !strcmp(m_proxyTarget, c->get_hostname()))
444 yaz_log (LOG_LOG, "%sREUSE %d %s",
445 m_session_str, parent->m_seqno, c->get_hostname());
447 c->m_seqno = parent->m_seqno;
448 assert(c->m_server == 0);
451 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
452 c->set_APDU_yazlog(1);
454 c->set_APDU_yazlog(0);
466 if (apdu->which != Z_APDU_initRequest)
468 yaz_log (LOG_LOG, "%sno init request as first PDU", m_session_str);
471 Z_InitRequest *initRequest = apdu->u.initRequest;
473 if (!initRequest->idAuthentication)
475 if (m_proxy_authentication)
477 initRequest->idAuthentication =
478 (Z_IdAuthentication *)
479 odr_malloc (odr_encode(),
480 sizeof(*initRequest->idAuthentication));
481 initRequest->idAuthentication->which =
482 Z_IdAuthentication_open;
483 initRequest->idAuthentication->u.open =
484 odr_strdup (odr_encode(), m_proxy_authentication);
487 // go through list of clients - and find the lowest/oldest one.
488 Yaz_ProxyClient *c_min = 0;
490 int no_of_clients = 0;
491 if (parent->m_clientPool)
492 yaz_log (LOG_DEBUG, "Existing sessions");
493 for (c = parent->m_clientPool; c; c = c->m_next)
495 yaz_log (LOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
496 c->m_waiting, c->get_hostname(),
497 c->m_cookie ? c->m_cookie : "");
499 if (min_seq < 0 || c->m_seqno < min_seq)
501 min_seq = c->m_seqno;
505 if (no_of_clients >= parent->m_max_clients)
508 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
510 yaz_log (LOG_LOG, "%sMAXCLIENTS %d Destroy %d",
511 m_session_str, parent->m_max_clients, c->m_seqno);
512 if (c->m_server && c->m_server != this)
518 yaz_log (LOG_LOG, "%sMAXCLIENTS %d Reuse %d %d %s",
519 m_session_str, parent->m_max_clients,
520 c->m_seqno, parent->m_seqno, c->get_hostname());
524 c->m_cookie = xstrdup(cookie);
525 c->m_seqno = parent->m_seqno;
526 if (c->m_server && c->m_server != this)
528 c->m_server->m_client = 0;
532 c->m_target_idletime = m_target_idletime;
533 c->timeout(m_target_idletime);
535 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
536 c->set_APDU_yazlog(1);
538 c->set_APDU_yazlog(0);
545 yaz_log (LOG_LOG, "%sNEW %d %s",
546 m_session_str, parent->m_seqno, m_proxyTarget);
547 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), parent);
548 c->m_next = parent->m_clientPool;
550 c->m_next->m_prev = &c->m_next;
551 parent->m_clientPool = c;
552 c->m_prev = &parent->m_clientPool;
558 c->m_cookie = xstrdup(cookie);
560 c->m_seqno = parent->m_seqno;
562 c->m_last_resultCount = 0;
565 c->m_sr_transform = 0;
567 c->m_resultSetStartPoint = 0;
569 if (c->client(m_proxyTarget))
574 c->m_target_idletime = m_target_idletime;
577 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
578 c->set_APDU_yazlog(1);
580 c->set_APDU_yazlog(0);
582 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
586 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
589 for (i = 0; i<num; i++)
592 Z_DefaultDiagFormat *r;
593 Z_DiagRec *p = pp[i];
594 if (p->which != Z_DiagRec_defaultFormat)
596 yaz_log(LOG_LOG, "%sError no diagnostics", m_session_str);
600 r = p->u.defaultFormat;
601 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
602 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
603 yaz_log(LOG_LOG, "%sError unknown diagnostic set", m_session_str);
606 case Z_DefaultDiagFormat_v2Addinfo:
607 yaz_log(LOG_LOG, "%sError %d %s:%s",
609 *r->condition, diagbib1_str(*r->condition),
612 case Z_DefaultDiagFormat_v3Addinfo:
613 yaz_log(LOG_LOG, "%sError %d %s:%s",
615 *r->condition, diagbib1_str(*r->condition),
622 int Yaz_Proxy::convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu)
624 if (!m_stylesheet_xsp || p->num_records <= 0)
625 return 0; /* no XSLT to be done ... */
627 m_stylesheet_offset = 0;
628 m_stylesheet_nprl = p;
629 m_stylesheet_apdu = apdu;
634 void Yaz_Proxy::convert_xsl_delay()
636 Z_NamePlusRecord *npr = m_stylesheet_nprl->records[m_stylesheet_offset];
637 if (npr->which == Z_NamePlusRecord_databaseRecord)
639 Z_External *r = npr->u.databaseRecord;
640 if (r->which == Z_External_octet)
643 fwrite((char*) r->u.octet_aligned->buf, 1, r->u.octet_aligned->len, stdout);
645 xmlDocPtr res, doc = xmlParseMemory(
646 (char*) r->u.octet_aligned->buf,
647 r->u.octet_aligned->len);
650 yaz_log(LOG_LOG, "%sXSLT convert %d",
651 m_session_str, m_stylesheet_offset);
652 res = xsltApplyStylesheet(m_stylesheet_xsp, doc, 0);
658 xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
660 m_stylesheet_nprl->records[m_stylesheet_offset]->
662 z_ext_record(odr_encode(), VAL_TEXT_XML,
663 (char*) out_buf, out_len);
671 m_stylesheet_offset++;
672 if (m_stylesheet_offset == m_stylesheet_nprl->num_records)
674 m_stylesheet_nprl = 0;
675 if (m_stylesheet_xsp)
676 xsltFreeStylesheet(m_stylesheet_xsp);
677 m_stylesheet_xsp = 0;
678 timeout(m_client_idletime);
679 send_PDU_convert(m_stylesheet_apdu);
685 void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
689 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
690 yaz_marc_t mt = yaz_marc_create();
691 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
692 yaz_marc_iconv(mt, cd);
693 for (i = 0; i < p->num_records; i++)
695 Z_NamePlusRecord *npr = p->records[i];
696 if (npr->which == Z_NamePlusRecord_databaseRecord)
698 Z_External *r = npr->u.databaseRecord;
699 if (r->which == Z_External_octet)
703 if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
704 r->u.octet_aligned->len,
707 npr->u.databaseRecord = z_ext_record(odr_encode(),
716 yaz_marc_destroy(mt);
719 void Yaz_Proxy::logtime()
721 if (m_time_tv.tv_sec)
724 gettimeofday(&tv, 0);
725 long diff = (tv.tv_sec - m_time_tv.tv_sec)*1000000 +
726 (tv.tv_usec - m_time_tv.tv_usec);
728 yaz_log(LOG_LOG, "%sElapsed %ld.%03ld", m_session_str,
729 diff/1000000, (diff/1000)%1000);
731 m_time_tv.tv_sec = 0;
732 m_time_tv.tv_usec = 0;
735 int Yaz_Proxy::send_http_response(int code)
737 ODR o = odr_encode();
738 Z_GDU *gdu = z_get_HTTP_Response(o, code);
739 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
741 hres->version = odr_strdup(o, m_http_version);
742 if (m_http_keepalive)
743 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
747 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
749 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
753 int r = send_GDU(gdu, &len);
755 m_bw_stat.add_bytes(len);
760 int Yaz_Proxy::send_srw_response(Z_SRW_PDU *srw_pdu)
762 ODR o = odr_encode();
763 const char *ctype = "text/xml";
764 Z_GDU *gdu = z_get_HTTP_Response(o, 200);
765 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
767 hres->version = odr_strdup(o, m_http_version);
768 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
769 if (m_http_keepalive)
770 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
774 static Z_SOAP_Handler soap_handlers[2] = {
776 {"http://www.loc.gov/zing/srw/", 0,
777 (Z_SOAP_fun) yaz_srw_codec},
782 Z_SOAP *soap_package = (Z_SOAP*) odr_malloc(o, sizeof(Z_SOAP));
783 soap_package->which = Z_SOAP_generic;
784 soap_package->u.generic =
785 (Z_SOAP_Generic *) odr_malloc(o, sizeof(*soap_package->u.generic));
786 soap_package->u.generic->no = 0;
787 soap_package->u.generic->ns = soap_handlers[0].ns;
788 soap_package->u.generic->p = (void *) srw_pdu;
789 soap_package->ns = m_soap_ns;
790 z_soap_codec_enc_xsl(o, &soap_package,
791 &hres->content_buf, &hres->content_len,
792 soap_handlers, 0, m_s2z_stylesheet);
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_to_srw_client_error(int srw_error, const char *add)
808 ODR o = odr_encode();
809 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
810 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
812 srw_res->num_diagnostics = 1;
813 srw_res->diagnostics = (Z_SRW_diagnostic *)
814 odr_malloc(o, sizeof(*srw_res->diagnostics));
815 yaz_mk_std_diagnostic(o, srw_res->diagnostics, srw_error, add);
816 return send_srw_response(srw_pdu);
819 int Yaz_Proxy::z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
820 Z_DefaultDiagFormat *ddf)
822 int bib1_code = *ddf->condition;
823 if (bib1_code == 109)
825 srw_res->num_diagnostics = 1;
826 srw_res->diagnostics = (Z_SRW_diagnostic *)
827 odr_malloc(o, sizeof(*srw_res->diagnostics));
828 yaz_mk_std_diagnostic(o, srw_res->diagnostics,
829 yaz_diag_bib1_to_srw(*ddf->condition),
834 int Yaz_Proxy::send_to_srw_client_ok(int hits, Z_Records *records, int start)
836 ODR o = odr_encode();
837 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
838 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
840 srw_res->numberOfRecords = odr_intdup (o, hits);
841 if (records && records->which == Z_Records_DBOSD)
843 srw_res->num_records =
844 records->u.databaseOrSurDiagnostics->num_records;
846 srw_res->records = (Z_SRW_record *)
847 odr_malloc(o, srw_res->num_records * sizeof(Z_SRW_record));
848 for (i = 0; i < srw_res->num_records; i++)
850 Z_NamePlusRecord *npr = records->u.databaseOrSurDiagnostics->records[i];
851 if (npr->which != Z_NamePlusRecord_databaseRecord)
853 srw_res->records[i].recordSchema = "diagnostic";
854 srw_res->records[i].recordPacking = m_s2z_packing;
855 srw_res->records[i].recordData_buf = "67";
856 srw_res->records[i].recordData_len = 2;
857 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
860 Z_External *r = npr->u.databaseRecord;
861 oident *ent = oid_getentbyoid(r->direct_reference);
862 if (r->which == Z_External_octet && ent->value == VAL_TEXT_XML)
864 srw_res->records[i].recordSchema = m_schema;
865 srw_res->records[i].recordPacking = m_s2z_packing;
866 srw_res->records[i].recordData_buf = (char*)
867 r->u.octet_aligned->buf;
868 srw_res->records[i].recordData_len = r->u.octet_aligned->len;
869 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
873 srw_res->records[i].recordSchema = "diagnostic";
874 srw_res->records[i].recordPacking = m_s2z_packing;
875 srw_res->records[i].recordData_buf = "67";
876 srw_res->records[i].recordData_len = 2;
877 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
881 if (records && records->which == Z_Records_NSD)
884 http_code = z_to_srw_diag(odr_encode(), srw_res,
885 records->u.nonSurrogateDiagnostic);
887 return send_http_response(http_code);
889 return send_srw_response(srw_pdu);
893 int Yaz_Proxy::send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
896 Yaz_ProxyConfig *cfg = check_reconfigure();
900 char *b = cfg->get_explain(odr_encode(), 0 /* target */,
901 m_s2z_database, &len);
904 Z_SRW_PDU *res = yaz_srw_get(odr_encode(), Z_SRW_explain_response);
905 Z_SRW_explainResponse *er = res->u.explain_response;
907 er->record.recordData_buf = b;
908 er->record.recordData_len = len;
909 er->record.recordPacking = m_s2z_packing;
910 er->record.recordSchema = "http://explain.z3950.org/dtd/2.0/";
912 er->diagnostics = diagnostics;
913 er->num_diagnostics = num_diagnostics;
914 return send_srw_response(res);
917 return send_http_response(404);
920 int Yaz_Proxy::send_PDU_convert(Z_APDU *apdu)
924 if (apdu->which == Z_APDU_initResponse)
926 Z_InitResponse *res = apdu->u.initResponse;
927 if (*res->result == 0)
929 send_to_srw_client_error(3, 0);
931 else if (!m_s2z_search_apdu)
933 send_srw_explain_response(0, 0);
937 handle_incoming_Z_PDU(m_s2z_search_apdu);
940 else if (m_s2z_search_apdu && apdu->which == Z_APDU_searchResponse)
942 m_s2z_search_apdu = 0;
943 Z_SearchResponse *res = apdu->u.searchResponse;
944 m_s2z_hit_count = *res->resultCount;
945 if (res->records && res->records->which == Z_Records_NSD)
947 send_to_srw_client_ok(0, res->records, 1);
949 else if (m_s2z_present_apdu && m_s2z_hit_count > 0)
952 Z_PresentRequest *pr = m_s2z_present_apdu->u.presentRequest;
954 if (*pr->resultSetStartPoint <= m_s2z_hit_count)
956 if (*pr->numberOfRecordsRequested+ *pr->resultSetStartPoint
958 *pr->numberOfRecordsRequested =
959 1 + m_s2z_hit_count - *pr->resultSetStartPoint;
961 handle_incoming_Z_PDU(m_s2z_present_apdu);
965 m_s2z_present_apdu = 0;
966 send_to_srw_client_ok(m_s2z_hit_count, res->records, 1);
969 else if (m_s2z_present_apdu && apdu->which == Z_APDU_presentResponse)
972 *m_s2z_present_apdu->u.presentRequest->resultSetStartPoint;
974 m_s2z_present_apdu = 0;
975 Z_PresentResponse *res = apdu->u.presentResponse;
976 send_to_srw_client_ok(m_s2z_hit_count, res->records, start);
982 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
983 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
985 int r = send_Z_PDU(apdu, &len);
987 m_bw_stat.add_bytes(len);
994 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
996 int kill_session = 0;
997 Z_ReferenceId **new_id = get_referenceIdP(apdu);
1000 *new_id = m_referenceId;
1002 if (apdu->which == Z_APDU_searchResponse)
1004 Z_SearchResponse *sr = apdu->u.searchResponse;
1005 Z_Records *p = sr->records;
1006 if (p && p->which == Z_Records_NSD)
1008 Z_DiagRec dr, *dr_p = &dr;
1009 dr.which = Z_DiagRec_defaultFormat;
1010 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1012 *sr->searchStatus = 0;
1013 display_diagrecs(&dr_p, 1);
1017 if (p && p->which == Z_Records_DBOSD)
1020 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1021 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1025 if (sr->resultCount)
1027 yaz_log(LOG_LOG, "%s%d hits", m_session_str,
1029 if (*sr->resultCount < 0)
1031 m_invalid_session = 1;
1034 *sr->searchStatus = 0;
1036 create_nonSurrogateDiagnostics(odr_encode(), 2, 0);
1037 *sr->resultCount = 0;
1042 else if (apdu->which == Z_APDU_presentResponse)
1044 Z_PresentResponse *sr = apdu->u.presentResponse;
1045 Z_Records *p = sr->records;
1046 if (p && p->which == Z_Records_NSD)
1048 Z_DiagRec dr, *dr_p = &dr;
1049 dr.which = Z_DiagRec_defaultFormat;
1050 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1051 if (*sr->presentStatus == Z_PresentStatus_success)
1052 *sr->presentStatus = Z_PresentStatus_failure;
1053 display_diagrecs(&dr_p, 1);
1055 if (p && p->which == Z_Records_DBOSD)
1058 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1059 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1063 else if (apdu->which == Z_APDU_initResponse)
1065 if (m_initRequest_options)
1068 (Odr_bitmask *)odr_malloc(odr_encode(),
1069 sizeof(Odr_bitmask));
1070 ODR_MASK_ZERO(nopt);
1073 for (i = 0; i<24; i++)
1074 if (ODR_MASK_GET(m_initRequest_options, i) &&
1075 ODR_MASK_GET(apdu->u.initResponse->options, i))
1076 ODR_MASK_SET(nopt, i);
1077 apdu->u.initResponse->options = nopt;
1079 if (m_initRequest_version)
1081 Z_ProtocolVersion *nopt =
1082 (Odr_bitmask *)odr_malloc(odr_encode(),
1083 sizeof(Odr_bitmask));
1084 ODR_MASK_ZERO(nopt);
1087 for (i = 0; i<8; i++)
1088 if (ODR_MASK_GET(m_initRequest_version, i) &&
1089 ODR_MASK_GET(apdu->u.initResponse->protocolVersion, i))
1090 ODR_MASK_SET(nopt, i);
1091 apdu->u.initResponse->protocolVersion = nopt;
1093 apdu->u.initResponse->preferredMessageSize =
1094 odr_intdup(odr_encode(),
1095 m_client->m_initResponse_preferredMessageSize >
1096 m_initRequest_preferredMessageSize ?
1097 m_initRequest_preferredMessageSize :
1098 m_client->m_initResponse_preferredMessageSize);
1099 apdu->u.initResponse->maximumRecordSize =
1100 odr_intdup(odr_encode(),
1101 m_client->m_initResponse_maximumRecordSize >
1102 m_initRequest_maximumRecordSize ?
1103 m_initRequest_maximumRecordSize :
1104 m_client->m_initResponse_maximumRecordSize);
1106 int r = send_PDU_convert(apdu);
1113 m_parent->pre_init();
1118 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
1121 const char *apdu_name_tmp = apdu_name(apdu);
1122 int r = send_Z_PDU(apdu, &len);
1123 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
1124 yaz_log (LOG_LOG, "%sSending %s to %s %d bytes",
1126 apdu_name_tmp, get_hostname(), len);
1127 m_bytes_sent += len;
1131 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
1133 if (apdu->which == Z_APDU_presentRequest)
1135 Z_PresentRequest *pr = apdu->u.presentRequest;
1136 int toget = *pr->numberOfRecordsRequested;
1137 int start = *pr->resultSetStartPoint;
1139 yaz_log(LOG_LOG, "%sPresent %s %d+%d", m_session_str,
1140 pr->resultSetId, start, toget);
1142 if (*m_parent->m_optimize == '0')
1145 if (!m_client->m_last_resultSetId)
1147 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1148 new_apdu->u.presentResponse->records =
1149 create_nonSurrogateDiagnostics(odr_encode(), 30,
1151 send_to_client(new_apdu);
1154 if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
1156 if (start+toget-1 > m_client->m_last_resultCount)
1158 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1159 new_apdu->u.presentResponse->records =
1160 create_nonSurrogateDiagnostics(odr_encode(), 13, 0);
1161 send_to_client(new_apdu);
1164 Z_NamePlusRecordList *npr;
1165 if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
1166 pr->preferredRecordSyntax,
1167 pr->recordComposition))
1169 yaz_log (LOG_LOG, "%sReturned cached records for present request",
1171 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1172 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1174 new_apdu->u.presentResponse->numberOfRecordsReturned
1175 = odr_intdup(odr_encode(), toget);
1177 new_apdu->u.presentResponse->records = (Z_Records*)
1178 odr_malloc(odr_encode(), sizeof(Z_Records));
1179 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
1180 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
1181 new_apdu->u.presentResponse->nextResultSetPosition =
1182 odr_intdup(odr_encode(), start+toget);
1184 send_to_client(new_apdu);
1190 if (apdu->which != Z_APDU_searchRequest)
1192 Z_SearchRequest *sr = apdu->u.searchRequest;
1193 Yaz_Z_Query *this_query = new Yaz_Z_Query;
1194 Yaz_Z_Databases this_databases;
1196 this_databases.set(sr->num_databaseNames, (const char **)
1199 this_query->set_Z_Query(sr->query);
1201 char query_str[120];
1202 this_query->print(query_str, sizeof(query_str)-1);
1203 yaz_log(LOG_LOG, "%sSearch %s", m_session_str, query_str);
1205 if (*m_parent->m_optimize != '0' &&
1206 m_client->m_last_ok && m_client->m_last_query &&
1207 m_client->m_last_query->match(this_query) &&
1208 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
1209 m_client->m_last_databases.match(this_databases))
1212 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
1213 m_client->m_last_resultCount < *sr->largeSetLowerBound)
1215 Z_NamePlusRecordList *npr;
1216 int toget = *sr->mediumSetPresentNumber;
1217 Z_RecordComposition *comp = 0;
1219 if (toget > m_client->m_last_resultCount)
1220 toget = m_client->m_last_resultCount;
1222 if (sr->mediumSetElementSetNames)
1224 comp = (Z_RecordComposition *)
1225 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1226 comp->which = Z_RecordComp_simple;
1227 comp->u.simple = sr->mediumSetElementSetNames;
1230 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1231 sr->preferredRecordSyntax, comp))
1233 yaz_log (LOG_LOG, "%sReturned cached records for medium set",
1235 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1236 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1237 new_apdu->u.searchResponse->resultCount =
1238 &m_client->m_last_resultCount;
1240 new_apdu->u.searchResponse->numberOfRecordsReturned
1241 = odr_intdup(odr_encode(), toget);
1243 new_apdu->u.searchResponse->presentStatus =
1244 odr_intdup(odr_encode(), Z_PresentStatus_success);
1245 new_apdu->u.searchResponse->records = (Z_Records*)
1246 odr_malloc(odr_encode(), sizeof(Z_Records));
1247 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1248 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1249 new_apdu->u.searchResponse->nextResultSetPosition =
1250 odr_intdup(odr_encode(), toget+1);
1251 send_to_client(new_apdu);
1257 // send present request (medium size)
1258 yaz_log (LOG_LOG, "%sOptimizing search for medium set",
1261 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1262 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1263 pr->referenceId = sr->referenceId;
1264 pr->resultSetId = sr->resultSetName;
1265 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1266 *pr->numberOfRecordsRequested = toget;
1267 pr->recordComposition = comp;
1268 m_client->m_sr_transform = 1;
1272 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
1273 m_client->m_last_resultCount <= 0)
1275 // large set. Return pseudo-search response immediately
1276 yaz_log (LOG_LOG, "%sOptimizing search for large set",
1278 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1279 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1280 new_apdu->u.searchResponse->resultCount =
1281 &m_client->m_last_resultCount;
1282 send_to_client(new_apdu);
1287 Z_NamePlusRecordList *npr;
1288 int toget = m_client->m_last_resultCount;
1289 Z_RecordComposition *comp = 0;
1291 // send a present request (small set)
1293 if (sr->smallSetElementSetNames)
1295 comp = (Z_RecordComposition *)
1296 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1297 comp->which = Z_RecordComp_simple;
1298 comp->u.simple = sr->smallSetElementSetNames;
1301 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1302 sr->preferredRecordSyntax, comp))
1304 yaz_log (LOG_LOG, "%sReturned cached records for small set",
1306 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1307 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1308 new_apdu->u.searchResponse->resultCount =
1309 &m_client->m_last_resultCount;
1311 new_apdu->u.searchResponse->numberOfRecordsReturned
1312 = odr_intdup(odr_encode(), toget);
1314 new_apdu->u.searchResponse->presentStatus =
1315 odr_intdup(odr_encode(), Z_PresentStatus_success);
1316 new_apdu->u.searchResponse->records = (Z_Records*)
1317 odr_malloc(odr_encode(), sizeof(Z_Records));
1318 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1319 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1320 new_apdu->u.searchResponse->nextResultSetPosition =
1321 odr_intdup(odr_encode(), toget+1);
1322 send_to_client(new_apdu);
1327 yaz_log (LOG_LOG, "%sOptimizing search for small set",
1329 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1330 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1331 pr->referenceId = sr->referenceId;
1332 pr->resultSetId = sr->resultSetName;
1333 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1334 *pr->numberOfRecordsRequested = toget;
1335 pr->recordComposition = comp;
1336 m_client->m_sr_transform = 1;
1341 else // query doesn't match
1343 delete m_client->m_last_query;
1344 m_client->m_last_query = this_query;
1345 m_client->m_last_ok = 0;
1346 m_client->m_cache.clear();
1347 m_client->m_resultSetStartPoint = 0;
1349 xfree (m_client->m_last_resultSetId);
1350 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
1352 m_client->m_last_databases.set(sr->num_databaseNames,
1353 (const char **) sr->databaseNames);
1359 void Yaz_Proxy::inc_request_no()
1361 char *cp = strchr(m_session_str, ' ');
1364 sprintf(cp+1, "%d ", m_request_no);
1367 void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
1371 m_bytes_recv += len;
1373 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
1374 yaz_log (LOG_LOG, "%sReceiving %s from client %d bytes",
1375 m_session_str, gdu_name(apdu), len);
1377 if (m_bw_hold_PDU) // double incoming PDU. shutdown now.
1380 m_bw_stat.add_bytes(len);
1381 m_pdu_stat.add_bytes(1);
1383 gettimeofday(&m_time_tv, 0);
1385 int bw_total = m_bw_stat.get_total();
1386 int pdu_total = m_pdu_stat.get_total();
1391 if (bw_total > m_bw_max)
1393 reduce = (bw_total/m_bw_max);
1398 if (pdu_total > m_pdu_max)
1400 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
1401 reduce = (reduce > nreduce) ? reduce : nreduce;
1406 yaz_log(LOG_LOG, "%sdelay=%d bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
1407 m_session_str, reduce, bw_total, pdu_total,
1408 m_bw_max, m_pdu_max);
1410 m_bw_hold_PDU = apdu; // save PDU and signal "on hold"
1411 timeout(reduce); // call us reduce seconds later
1413 else if (apdu->which == Z_GDU_Z3950)
1414 handle_incoming_Z_PDU(apdu->u.z3950);
1415 else if (apdu->which == Z_GDU_HTTP_Request)
1416 handle_incoming_HTTP(apdu->u.HTTP_Request);
1419 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
1421 if (m_max_record_retrieve)
1423 if (apdu->which == Z_APDU_presentRequest)
1425 Z_PresentRequest *pr = apdu->u.presentRequest;
1426 if (pr->numberOfRecordsRequested &&
1427 *pr->numberOfRecordsRequested > m_max_record_retrieve)
1428 *pr->numberOfRecordsRequested = m_max_record_retrieve;
1433 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
1435 const char *addinfo)
1437 Z_Records *rec = (Z_Records *)
1438 odr_malloc (odr, sizeof(*rec));
1440 odr_malloc (odr, sizeof(*err));
1441 Z_DiagRec *drec = (Z_DiagRec *)
1442 odr_malloc (odr, sizeof(*drec));
1443 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1444 odr_malloc (odr, sizeof(*dr));
1446 rec->which = Z_Records_NSD;
1447 rec->u.nonSurrogateDiagnostic = dr;
1448 dr->diagnosticSetId =
1449 yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
1450 dr->condition = err;
1451 dr->which = Z_DefaultDiagFormat_v2Addinfo;
1452 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
1456 Z_APDU *Yaz_Proxy::handle_query_transformation(Z_APDU *apdu)
1458 if (apdu->which == Z_APDU_searchRequest &&
1459 apdu->u.searchRequest->query &&
1460 apdu->u.searchRequest->query->which == Z_Query_type_104 &&
1461 apdu->u.searchRequest->query->u.type_104->which == Z_External_CQL)
1463 Z_RPNQuery *rpnquery = 0;
1464 Z_SearchRequest *sr = apdu->u.searchRequest;
1467 yaz_log(LOG_LOG, "%sCQL: %s", m_session_str,
1468 sr->query->u.type_104->u.cql);
1470 int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
1471 &rpnquery, odr_encode(),
1474 yaz_log(LOG_LOG, "%sNo CQL to RPN table", m_session_str);
1477 yaz_log(LOG_LOG, "%sCQL Conversion error %d", m_session_str, r);
1478 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1480 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1481 new_apdu->u.searchResponse->records =
1482 create_nonSurrogateDiagnostics(odr_encode(),
1483 yaz_diag_srw_to_bib1(r),
1485 *new_apdu->u.searchResponse->searchStatus = 0;
1487 send_to_client(new_apdu);
1493 sr->query->which = Z_Query_type_1;
1494 sr->query->u.type_1 = rpnquery;
1501 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
1503 if (apdu->which == Z_APDU_searchRequest)
1505 Z_SearchRequest *sr = apdu->u.searchRequest;
1509 Yaz_ProxyConfig *cfg = check_reconfigure();
1511 err = cfg->check_query(odr_encode(), m_default_target,
1512 sr->query, &addinfo);
1515 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1517 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1518 new_apdu->u.searchResponse->records =
1519 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1520 *new_apdu->u.searchResponse->searchStatus = 0;
1522 send_to_client(new_apdu);
1530 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
1533 if (apdu->which == Z_APDU_searchRequest)
1535 Z_SearchRequest *sr = apdu->u.searchRequest;
1538 Yaz_ProxyConfig *cfg = check_reconfigure();
1540 Z_RecordComposition rc_temp, *rc = 0;
1541 if (sr->smallSetElementSetNames)
1543 rc_temp.which = Z_RecordComp_simple;
1544 rc_temp.u.simple = sr->smallSetElementSetNames;
1548 char *stylesheet_name = 0;
1550 err = cfg->check_syntax(odr_encode(),
1552 sr->preferredRecordSyntax, rc,
1553 &addinfo, &stylesheet_name, &m_schema);
1554 if (stylesheet_name)
1556 m_parent->low_socket_close();
1558 if (m_stylesheet_xsp)
1559 xsltFreeStylesheet(m_stylesheet_xsp);
1561 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1563 m_stylesheet_offset = 0;
1564 xfree(stylesheet_name);
1566 m_parent->low_socket_open();
1570 sr->preferredRecordSyntax =
1571 yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN, VAL_USMARC);
1576 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1578 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1579 new_apdu->u.searchResponse->records =
1580 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1581 *new_apdu->u.searchResponse->searchStatus = 0;
1583 send_to_client(new_apdu);
1588 else if (apdu->which == Z_APDU_presentRequest)
1590 Z_PresentRequest *pr = apdu->u.presentRequest;
1593 Yaz_ProxyConfig *cfg = check_reconfigure();
1595 char *stylesheet_name = 0;
1597 err = cfg->check_syntax(odr_encode(), m_default_target,
1598 pr->preferredRecordSyntax,
1599 pr->recordComposition,
1600 &addinfo, &stylesheet_name, &m_schema);
1601 if (stylesheet_name)
1603 m_parent->low_socket_close();
1605 if (m_stylesheet_xsp)
1606 xsltFreeStylesheet(m_stylesheet_xsp);
1608 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1610 m_stylesheet_offset = 0;
1611 xfree(stylesheet_name);
1613 m_parent->low_socket_open();
1617 pr->preferredRecordSyntax =
1618 yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN, VAL_USMARC);
1623 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1625 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1626 new_apdu->u.presentResponse->records =
1627 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1628 *new_apdu->u.presentResponse->presentStatus =
1629 Z_PresentStatus_failure;
1631 send_to_client(new_apdu);
1639 Z_ElementSetNames *Yaz_Proxy::mk_esn_from_schema(ODR o, const char *schema)
1643 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1644 odr_malloc(o, sizeof(Z_ElementSetNames));
1645 esn->which = Z_ElementSetNames_generic;
1646 esn->u.generic = odr_strdup(o, schema);
1650 void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
1654 odr_destroy(m_s2z_odr_init);
1657 if (m_s2z_odr_search)
1659 odr_destroy(m_s2z_odr_search);
1660 m_s2z_odr_search = 0;
1663 m_http_keepalive = 0;
1665 if (!strcmp(hreq->version, "1.0"))
1667 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1668 if (v && !strcmp(v, "Keep-Alive"))
1669 m_http_keepalive = 1;
1671 m_http_keepalive = 0;
1672 m_http_version = "1.0";
1676 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1677 if (v && !strcmp(v, "close"))
1678 m_http_keepalive = 0;
1680 m_http_keepalive = 1;
1681 m_http_version = "1.1";
1684 Z_SRW_PDU *srw_pdu = 0;
1685 Z_SOAP *soap_package = 0;
1687 Z_SRW_diagnostic *diagnostic = 0;
1688 int num_diagnostic = 0;
1689 if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1691 || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1692 &charset, &diagnostic, &num_diagnostic) == 0)
1694 m_s2z_odr_init = odr_createmem(ODR_ENCODE);
1695 m_s2z_odr_search = odr_createmem(ODR_ENCODE);
1696 m_soap_ns = odr_strdup(m_s2z_odr_search, soap_package->ns);
1697 m_s2z_init_apdu = 0;
1698 m_s2z_search_apdu = 0;
1699 m_s2z_present_apdu = 0;
1701 m_s2z_stylesheet = 0;
1703 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
1705 Z_SRW_searchRetrieveRequest *srw_req = srw_pdu->u.request;
1707 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1708 // recordXPath unsupported.
1709 if (srw_req->recordXPath)
1711 yaz_add_srw_diagnostic(odr_decode(),
1712 &diagnostic, &num_diagnostic,
1715 // must have a query
1716 if (!srw_req->query.cql)
1718 yaz_add_srw_diagnostic(odr_decode(),
1719 &diagnostic, &num_diagnostic,
1723 if (srw_req->sort_type != Z_SRW_sort_type_none)
1725 yaz_add_srw_diagnostic(odr_decode(),
1726 &diagnostic, &num_diagnostic,
1730 if (srw_req->stylesheet)
1732 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1734 // set packing for response records ..
1735 if (srw_req->recordPacking &&
1736 !strcmp(srw_req->recordPacking, "xml"))
1737 m_s2z_packing = Z_SRW_recordPacking_XML;
1739 m_s2z_packing = Z_SRW_recordPacking_string;
1743 Z_SRW_PDU *srw_pdu =
1744 yaz_srw_get(odr_encode(),
1745 Z_SRW_searchRetrieve_response);
1746 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1748 srw_res->diagnostics = diagnostic;
1749 srw_res->num_diagnostics = num_diagnostic;
1750 send_srw_response(srw_pdu);
1754 // prepare search PDU
1755 m_s2z_search_apdu = zget_APDU(m_s2z_odr_search,
1756 Z_APDU_searchRequest);
1757 Z_SearchRequest *z_searchRequest =
1758 m_s2z_search_apdu->u.searchRequest;
1760 z_searchRequest->num_databaseNames = 1;
1761 z_searchRequest->databaseNames = (char**)
1762 odr_malloc(m_s2z_odr_search, sizeof(char *));
1763 z_searchRequest->databaseNames[0] = odr_strdup(m_s2z_odr_search,
1766 // query transformation
1767 Z_Query *query = (Z_Query *)
1768 odr_malloc(m_s2z_odr_search, sizeof(Z_Query));
1769 z_searchRequest->query = query;
1771 if (srw_req->query_type == Z_SRW_query_type_cql)
1773 Z_External *ext = (Z_External *)
1774 odr_malloc(m_s2z_odr_search, sizeof(*ext));
1775 ext->direct_reference =
1776 odr_getoidbystr(m_s2z_odr_search, "1.2.840.10003.16.2");
1777 ext->indirect_reference = 0;
1778 ext->descriptor = 0;
1779 ext->which = Z_External_CQL;
1780 ext->u.cql = srw_req->query.cql;
1782 query->which = Z_Query_type_104;
1783 query->u.type_104 = ext;
1785 else if (srw_req->query_type == Z_SRW_query_type_pqf)
1787 Z_RPNQuery *RPNquery;
1788 YAZ_PQF_Parser pqf_parser;
1790 pqf_parser = yaz_pqf_create ();
1792 RPNquery = yaz_pqf_parse (pqf_parser, m_s2z_odr_search,
1793 srw_req->query.pqf);
1796 const char *pqf_msg;
1798 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1799 yaz_log(LOG_LOG, "%*s^\n", off+4, "");
1800 yaz_log(LOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
1802 send_to_srw_client_error(10, 0);
1805 query->which = Z_Query_type_1;
1806 query->u.type_1 = RPNquery;
1808 yaz_pqf_destroy (pqf_parser);
1812 send_to_srw_client_error(7, "query");
1817 m_s2z_present_apdu = 0;
1819 if (srw_req->maximumRecords)
1820 max = *srw_req->maximumRecords;
1822 if (srw_req->startRecord)
1823 start = *srw_req->startRecord;
1826 // Some backend, such as Voyager doesn't honor piggyback
1827 // So we use present always (0 &&).
1828 if (0 && start <= 1) // Z39.50 piggyback
1830 *z_searchRequest->smallSetUpperBound = max;
1831 *z_searchRequest->mediumSetPresentNumber = max;
1832 *z_searchRequest->largeSetLowerBound = 2000000000; // 2e9
1834 z_searchRequest->preferredRecordSyntax =
1835 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1837 if (srw_req->recordSchema)
1839 z_searchRequest->smallSetElementSetNames =
1840 z_searchRequest->mediumSetElementSetNames =
1841 mk_esn_from_schema(m_s2z_odr_search,
1842 srw_req->recordSchema);
1845 else // Z39.50 present
1847 m_s2z_present_apdu = zget_APDU(m_s2z_odr_search,
1848 Z_APDU_presentRequest);
1849 Z_PresentRequest *z_presentRequest =
1850 m_s2z_present_apdu->u.presentRequest;
1851 *z_presentRequest->resultSetStartPoint = start;
1852 *z_presentRequest->numberOfRecordsRequested = max;
1853 z_presentRequest->preferredRecordSyntax =
1854 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1856 if (srw_req->recordSchema)
1858 z_presentRequest->recordComposition =
1859 (Z_RecordComposition *)
1860 odr_malloc(m_s2z_odr_search,
1861 sizeof(Z_RecordComposition));
1862 z_presentRequest->recordComposition->which =
1863 Z_RecordComp_simple;
1864 z_presentRequest->recordComposition->u.simple =
1865 mk_esn_from_schema(m_s2z_odr_search,
1866 srw_req->recordSchema);
1872 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1873 Z_APDU_initRequest);
1875 // prevent m_initRequest_apdu memory from being grabbed
1876 // in Yaz_Proxy::handle_incoming_Z_PDU
1877 m_initRequest_apdu = m_s2z_init_apdu;
1878 handle_incoming_Z_PDU(m_s2z_init_apdu);
1883 handle_incoming_Z_PDU(m_s2z_search_apdu);
1887 else if (srw_pdu->which == Z_SRW_explain_request)
1889 Z_SRW_explainRequest *srw_req = srw_pdu->u.explain_request;
1891 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1894 if (srw_req->stylesheet)
1896 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1898 if (srw_req->recordPacking &&
1899 !strcmp(srw_req->recordPacking, "xml"))
1900 m_s2z_packing = Z_SRW_recordPacking_XML;
1902 m_s2z_packing = Z_SRW_recordPacking_string;
1906 send_srw_explain_response(diagnostic, num_diagnostic);
1912 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1913 Z_APDU_initRequest);
1915 // prevent m_initRequest_apdu memory from being grabbed
1916 // in Yaz_Proxy::handle_incoming_Z_PDU
1917 m_initRequest_apdu = m_s2z_init_apdu;
1918 handle_incoming_Z_PDU(m_s2z_init_apdu);
1921 send_srw_explain_response(0, 0);
1924 else if (srw_pdu->which == Z_SRW_scan_request)
1926 m_s2z_database = odr_strdup(m_s2z_odr_init,
1927 srw_pdu->u.scan_request->database);
1929 yaz_add_srw_diagnostic(odr_decode(),
1930 &diagnostic, &num_diagnostic,
1932 Z_SRW_PDU *srw_pdu =
1933 yaz_srw_get(odr_encode(),
1934 Z_SRW_scan_response);
1935 Z_SRW_scanResponse *srw_res = srw_pdu->u.scan_response;
1937 srw_res->diagnostics = diagnostic;
1938 srw_res->num_diagnostics = num_diagnostic;
1939 send_srw_response(srw_pdu);
1946 send_to_srw_client_error(4, 0);
1950 Z_GDU *p = z_get_HTTP_Response(odr_encode(), 400);
1955 void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
1957 Z_ReferenceId **refid = get_referenceIdP(apdu);
1958 nmem_reset(m_referenceId_mem);
1959 if (refid && *refid)
1961 m_referenceId = (Z_ReferenceId *)
1962 nmem_malloc(m_referenceId_mem, sizeof(*m_referenceId));
1963 m_referenceId->len = m_referenceId->size = (*refid)->len;
1964 m_referenceId->buf = (unsigned char *)
1965 nmem_malloc(m_referenceId_mem, (*refid)->len);
1966 memcpy(m_referenceId->buf, (*refid)->buf, (*refid)->len);
1971 if (!m_client && m_invalid_session)
1973 m_apdu_invalid_session = apdu;
1974 m_mem_invalid_session = odr_extract_mem(odr_decode());
1975 apdu = m_initRequest_apdu;
1978 // Determine our client.
1979 Z_OtherInformation **oi;
1980 get_otherInfoAPDU(apdu, &oi);
1981 m_client = get_client(apdu, get_cookie(oi), get_proxy(oi));
1987 m_client->m_server = this;
1989 if (apdu->which == Z_APDU_initRequest)
1991 if (apdu->u.initRequest->implementationId)
1992 yaz_log(LOG_LOG, "%simplementationId: %s",
1993 m_session_str, apdu->u.initRequest->implementationId);
1994 if (apdu->u.initRequest->implementationName)
1995 yaz_log(LOG_LOG, "%simplementationName: %s",
1996 m_session_str, apdu->u.initRequest->implementationName);
1997 if (apdu->u.initRequest->implementationVersion)
1998 yaz_log(LOG_LOG, "%simplementationVersion: %s",
1999 m_session_str, apdu->u.initRequest->implementationVersion);
2000 if (m_initRequest_apdu == 0)
2002 if (m_initRequest_mem)
2003 nmem_destroy(m_initRequest_mem);
2004 m_initRequest_apdu = apdu;
2005 m_initRequest_mem = odr_extract_mem(odr_decode());
2007 m_initRequest_preferredMessageSize = *apdu->u.initRequest->
2008 preferredMessageSize;
2009 *apdu->u.initRequest->preferredMessageSize = 1024*1024;
2010 m_initRequest_maximumRecordSize = *apdu->u.initRequest->
2012 *apdu->u.initRequest->maximumRecordSize = 1024*1024;
2014 // save init options for the response..
2015 m_initRequest_options = apdu->u.initRequest->options;
2017 apdu->u.initRequest->options =
2018 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2019 sizeof(Odr_bitmask));
2020 ODR_MASK_ZERO(apdu->u.initRequest->options);
2022 for (i = 0; i<= 24; i++)
2023 ODR_MASK_SET(apdu->u.initRequest->options, i);
2024 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2025 Z_Options_negotiationModel);
2026 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2027 Z_Options_concurrentOperations);
2030 m_initRequest_version = apdu->u.initRequest->protocolVersion;
2031 apdu->u.initRequest->protocolVersion =
2032 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2033 sizeof(Odr_bitmask));
2034 ODR_MASK_ZERO(apdu->u.initRequest->protocolVersion);
2036 for (i = 0; i<= 8; i++)
2037 ODR_MASK_SET(apdu->u.initRequest->protocolVersion, i);
2039 if (m_client->m_init_flag)
2041 if (handle_init_response_for_invalid_session(apdu))
2043 if (m_client->m_initResponse)
2045 Z_APDU *apdu2 = m_client->m_initResponse;
2046 apdu2->u.initResponse->otherInfo = 0;
2047 if (m_client->m_cookie && *m_client->m_cookie)
2048 set_otherInformationString(apdu2, VAL_COOKIE, 1,
2049 m_client->m_cookie);
2050 apdu2->u.initResponse->referenceId =
2051 apdu->u.initRequest->referenceId;
2052 apdu2->u.initResponse->options = m_client->m_initResponse_options;
2053 apdu2->u.initResponse->protocolVersion =
2054 m_client->m_initResponse_version;
2056 send_to_client(apdu2);
2060 m_client->m_init_flag = 1;
2062 handle_max_record_retrieve(apdu);
2065 apdu = handle_syntax_validation(apdu);
2068 apdu = handle_query_transformation(apdu);
2071 apdu = handle_query_validation(apdu);
2074 apdu = result_set_optimize(apdu);
2077 m_client->timeout(m_target_idletime); // mark it active even
2078 // though we didn't use it
2082 // delete other info part from PDU before sending to target
2083 get_otherInfoAPDU(apdu, &oi);
2087 if (apdu->which == Z_APDU_presentRequest &&
2088 m_client->m_resultSetStartPoint == 0)
2090 Z_PresentRequest *pr = apdu->u.presentRequest;
2091 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
2092 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
2094 m_client->m_resultSetStartPoint = 0;
2096 if (m_client->send_to_target(apdu) < 0)
2103 m_client->m_waiting = 1;
2106 void Yaz_Proxy::connectNotify()
2110 void Yaz_Proxy::shutdown()
2112 m_invalid_session = 0;
2113 // only keep if keep_alive flag is set...
2115 m_client->m_pdu_recv < m_keepalive_limit_pdu &&
2116 m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
2117 m_client->m_waiting == 0)
2119 yaz_log(LOG_LOG, "%sShutdown (client to proxy) keepalive %s",
2121 m_client->get_hostname());
2122 yaz_log(LOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
2123 m_session_str, m_client->m_pdu_recv,
2124 m_client->m_bytes_sent + m_client->m_bytes_recv,
2125 m_keepalive_limit_bw, m_keepalive_limit_pdu);
2126 assert (m_client->m_waiting != 2);
2127 // Tell client (if any) that no server connection is there..
2128 m_client->m_server = 0;
2129 m_invalid_session = 0;
2133 yaz_log (LOG_LOG, "%sShutdown (client to proxy) close %s",
2135 m_client->get_hostname());
2136 assert (m_client->m_waiting != 2);
2141 yaz_log (LOG_LOG, "%sshutdown (client to proxy) bad state",
2147 yaz_log (LOG_LOG, "%sShutdown (client to proxy)",
2151 m_parent->pre_init();
2155 const char *Yaz_ProxyClient::get_session_str()
2159 return m_server->get_session_str();
2162 void Yaz_ProxyClient::shutdown()
2164 yaz_log (LOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
2170 void Yaz_Proxy::failNotify()
2173 yaz_log (LOG_LOG, "%sConnection closed by client",
2178 void Yaz_ProxyClient::failNotify()
2181 m_server->inc_request_no();
2182 yaz_log (LOG_LOG, "%sConnection closed by target %s",
2183 get_session_str(), get_hostname());
2187 void Yaz_ProxyClient::connectNotify()
2189 const char *s = get_session_str();
2190 const char *h = get_hostname();
2191 yaz_log (LOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
2193 timeout(m_target_idletime);
2198 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
2199 *the_PDU_Observable, int fd)
2201 return new Yaz_ProxyClient(the_PDU_Observable, 0);
2204 Yaz_ProxyClient::~Yaz_ProxyClient()
2209 m_next->m_prev = m_prev;
2210 m_waiting = 2; // for debugging purposes only.
2211 odr_destroy(m_init_odr);
2212 delete m_last_query;
2213 xfree (m_last_resultSetId);
2217 void Yaz_ProxyClient::pre_init_client()
2219 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
2220 Z_InitRequest *req = apdu->u.initRequest;
2223 for (i = 0; i<= 24; i++)
2224 ODR_MASK_SET(req->options, i);
2225 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2226 Z_Options_negotiationModel);
2227 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2228 Z_Options_concurrentOperations);
2229 for (i = 0; i<= 10; i++)
2230 ODR_MASK_SET(req->protocolVersion, i);
2232 if (send_to_target(apdu) < 0)
2243 void Yaz_Proxy::pre_init()
2246 const char *name = 0;
2247 const char *zurl_in_use[MAX_ZURL_PLEX];
2248 int limit_bw, limit_pdu, limit_req;
2249 int target_idletime, client_idletime;
2251 int keepalive_limit_bw, keepalive_limit_pdu;
2253 const char *cql2rpn = 0;
2255 Yaz_ProxyConfig *cfg = check_reconfigure();
2259 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
2264 for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
2265 &limit_bw, &limit_pdu, &limit_req,
2266 &target_idletime, &client_idletime,
2268 &keepalive_limit_bw,
2269 &keepalive_limit_pdu,
2276 for (j = 0; zurl_in_use[j]; j++)
2280 int spare_waiting = 0;
2283 for (c = m_clientPool; c; c = c->m_next)
2285 if (!strcmp(zurl_in_use[j], c->get_hostname()))
2287 if (c->m_cookie == 0)
2289 if (c->m_server == 0)
2301 yaz_log(LOG_LOG, "%spre-init %s %s use=%d other=%d spare=%d "
2302 "sparew=%d preinit=%d",m_session_str,
2303 name, zurl_in_use[j], in_use, other,
2304 spare, spare_waiting, pre_init);
2305 if (spare + spare_waiting < pre_init)
2307 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
2308 c->m_next = m_clientPool;
2310 c->m_next->m_prev = &c->m_next;
2312 c->m_prev = &m_clientPool;
2314 if (m_log_mask & PROXY_LOG_APDU_SERVER)
2315 c->set_APDU_yazlog(1);
2317 c->set_APDU_yazlog(0);
2319 if (c->client(zurl_in_use[j]))
2327 c->m_target_idletime = target_idletime;
2328 c->m_seqno = m_seqno++;
2335 void Yaz_Proxy::timeoutNotify()
2341 timeout(m_client_idletime);
2342 Z_GDU *apdu = m_bw_hold_PDU;
2345 if (apdu->which == Z_GDU_Z3950)
2346 handle_incoming_Z_PDU(apdu->u.z3950);
2347 else if (apdu->which == Z_GDU_HTTP_Request)
2348 handle_incoming_HTTP(apdu->u.HTTP_Request);
2350 else if (m_stylesheet_nprl)
2351 convert_xsl_delay();
2356 yaz_log (LOG_LOG, "%sTimeout (client to proxy)", m_session_str);
2367 void Yaz_Proxy::markInvalid()
2370 m_invalid_session = 1;
2373 void Yaz_ProxyClient::timeoutNotify()
2376 m_server->inc_request_no();
2378 yaz_log (LOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
2382 if (m_server && m_init_flag)
2384 // target timed out in a session that was properly initialized
2385 // server object stay alive but we mark it as invalid so it
2386 // gets initialized again
2387 m_server->markInvalid();
2393 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
2394 Yaz_Proxy *parent) :
2395 Yaz_Z_Assoc (the_PDU_Observable)
2402 m_last_resultSetId = 0;
2403 m_last_resultCount = 0;
2407 m_init_odr = odr_createmem (ODR_DECODE);
2409 m_initResponse_options = 0;
2410 m_initResponse_version = 0;
2411 m_initResponse_preferredMessageSize = 0;
2412 m_initResponse_maximumRecordSize = 0;
2413 m_resultSetStartPoint = 0;
2414 m_bytes_sent = m_bytes_recv = 0;
2418 m_target_idletime = 600;
2422 const char *Yaz_Proxy::option(const char *name, const char *value)
2424 if (!strcmp (name, "optimize")) {
2427 m_optimize = xstrdup (value);
2434 void Yaz_ProxyClient::recv_HTTP_response(Z_HTTP_Response *apdu, int len)
2439 void Yaz_ProxyClient::recv_GDU(Z_GDU *apdu, int len)
2441 if (apdu->which == Z_GDU_Z3950)
2442 recv_Z_PDU(apdu->u.z3950, len);
2443 else if (apdu->which == Z_GDU_HTTP_Response)
2444 recv_HTTP_response(apdu->u.HTTP_Response, len);
2449 int Yaz_Proxy::handle_init_response_for_invalid_session(Z_APDU *apdu)
2451 if (!m_invalid_session)
2453 m_invalid_session = 0;
2454 handle_incoming_Z_PDU(m_apdu_invalid_session);
2455 assert (m_mem_invalid_session);
2456 nmem_destroy(m_mem_invalid_session);
2457 m_mem_invalid_session = 0;
2461 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
2463 m_bytes_recv += len;
2467 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
2468 yaz_log (LOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
2469 apdu_name(apdu), get_hostname(), len);
2470 if (apdu->which == Z_APDU_initResponse)
2472 if (!m_server) // if this is a pre init session , check for more
2474 NMEM nmem = odr_extract_mem (odr_decode());
2475 odr_reset (m_init_odr);
2476 nmem_transfer (m_init_odr->mem, nmem);
2477 m_initResponse = apdu;
2478 m_initResponse_options = apdu->u.initResponse->options;
2479 m_initResponse_version = apdu->u.initResponse->protocolVersion;
2480 m_initResponse_preferredMessageSize =
2481 *apdu->u.initResponse->preferredMessageSize;
2482 m_initResponse_maximumRecordSize =
2483 *apdu->u.initResponse->maximumRecordSize;
2485 Z_InitResponse *ir = apdu->u.initResponse;
2486 char *im0 = ir->implementationName;
2489 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
2496 strcat(im1, "(YAZ Proxy)");
2497 ir->implementationName = im1;
2499 nmem_destroy (nmem);
2501 if (m_server && m_server->handle_init_response_for_invalid_session(apdu))
2504 if (apdu->which == Z_APDU_searchResponse)
2506 Z_SearchResponse *sr = apdu->u.searchResponse;
2507 m_last_resultCount = *sr->resultCount;
2508 int status = *sr->searchStatus;
2509 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
2513 if (sr->records && sr->records->which == Z_Records_DBOSD)
2515 m_cache.add(odr_decode(),
2516 sr->records->u.databaseOrSurDiagnostics, 1,
2521 if (apdu->which == Z_APDU_presentResponse)
2523 Z_PresentResponse *pr = apdu->u.presentResponse;
2527 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2528 Z_SearchResponse *sr = new_apdu->u.searchResponse;
2529 sr->referenceId = pr->referenceId;
2530 *sr->resultCount = m_last_resultCount;
2531 sr->records = pr->records;
2532 sr->nextResultSetPosition = pr->nextResultSetPosition;
2533 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
2537 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
2539 m_cache.add(odr_decode(),
2540 pr->records->u.databaseOrSurDiagnostics,
2541 m_resultSetStartPoint, -1);
2542 m_resultSetStartPoint = 0;
2546 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
2549 m_server->send_to_client(apdu);
2551 if (apdu->which == Z_APDU_close)
2557 void Yaz_Proxy::low_socket_close()
2560 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
2561 if (m_lo_fd[i] >= 0)
2562 ::close(m_lo_fd[i]);
2565 void Yaz_Proxy::low_socket_open()
2568 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
2569 m_lo_fd[i] = open("/dev/null", O_RDONLY);
2572 int Yaz_Proxy::server(const char *addr)
2574 int r = Yaz_Z_Assoc::server(addr);
2577 yaz_log(LOG_LOG, "%sStarted proxy " VERSION " on %s", m_session_str, addr);