2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.107 2004-03-01 17:18:39 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)
642 xmlDocPtr res, doc = xmlParseMemory(
643 (char*) r->u.octet_aligned->buf,
644 r->u.octet_aligned->len);
646 yaz_log(LOG_LOG, "%sXSLT convert %d",
647 m_session_str, m_stylesheet_offset);
648 res = xsltApplyStylesheet(m_stylesheet_xsp, doc, 0);
654 xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
656 m_stylesheet_nprl->records[m_stylesheet_offset]->
658 z_ext_record(odr_encode(), VAL_TEXT_XML,
659 (char*) out_buf, out_len);
667 m_stylesheet_offset++;
668 if (m_stylesheet_offset == m_stylesheet_nprl->num_records)
670 m_stylesheet_nprl = 0;
671 if (m_stylesheet_xsp)
672 xsltFreeStylesheet(m_stylesheet_xsp);
673 m_stylesheet_xsp = 0;
674 timeout(m_client_idletime);
675 send_PDU_convert(m_stylesheet_apdu);
681 void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
685 yaz_marc_t mt = yaz_marc_create();
686 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
687 for (i = 0; i < p->num_records; i++)
689 Z_NamePlusRecord *npr = p->records[i];
690 if (npr->which == Z_NamePlusRecord_databaseRecord)
692 Z_External *r = npr->u.databaseRecord;
693 if (r->which == Z_External_octet)
697 if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
698 r->u.octet_aligned->len,
701 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
702 WRBUF wrbuf = wrbuf_alloc();
705 size_t inbytesleft = rlen;
706 const char *inp = result;
707 while (cd && inbytesleft)
709 size_t outbytesleft = sizeof(outbuf);
713 r = yaz_iconv (cd, (char**) &inp,
715 &outp, &outbytesleft);
716 if (r == (size_t) (-1))
718 int e = yaz_iconv_error(cd);
719 if (e != YAZ_ICONV_E2BIG)
721 yaz_log(LOG_WARN, "conversion failure");
725 wrbuf_write(wrbuf, outbuf, outp - outbuf);
730 npr->u.databaseRecord = z_ext_record(odr_encode(),
734 wrbuf_free(wrbuf, 1);
739 yaz_marc_destroy(mt);
742 void Yaz_Proxy::logtime()
744 if (m_time_tv.tv_sec)
747 gettimeofday(&tv, 0);
748 long diff = (tv.tv_sec - m_time_tv.tv_sec)*1000000 +
749 (tv.tv_usec - m_time_tv.tv_usec);
751 yaz_log(LOG_LOG, "%sElapsed %ld.%03ld", m_session_str,
752 diff/1000000, (diff/1000)%1000);
754 m_time_tv.tv_sec = 0;
755 m_time_tv.tv_usec = 0;
758 int Yaz_Proxy::send_http_response(int code)
760 ODR o = odr_encode();
761 Z_GDU *gdu = z_get_HTTP_Response(o, code);
762 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
764 hres->version = odr_strdup(o, m_http_version);
765 if (m_http_keepalive)
766 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
770 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
772 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
776 int r = send_GDU(gdu, &len);
778 m_bw_stat.add_bytes(len);
783 int Yaz_Proxy::send_srw_response(Z_SRW_PDU *srw_pdu)
785 ODR o = odr_encode();
786 const char *ctype = "text/xml";
787 Z_GDU *gdu = z_get_HTTP_Response(o, 200);
788 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
790 hres->version = odr_strdup(o, m_http_version);
791 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
792 if (m_http_keepalive)
793 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
797 static Z_SOAP_Handler soap_handlers[2] = {
799 {"http://www.loc.gov/zing/srw/", 0,
800 (Z_SOAP_fun) yaz_srw_codec},
805 Z_SOAP *soap_package = (Z_SOAP*) odr_malloc(o, sizeof(Z_SOAP));
806 soap_package->which = Z_SOAP_generic;
807 soap_package->u.generic =
808 (Z_SOAP_Generic *) odr_malloc(o, sizeof(*soap_package->u.generic));
809 soap_package->u.generic->no = 0;
810 soap_package->u.generic->ns = soap_handlers[0].ns;
811 soap_package->u.generic->p = (void *) srw_pdu;
812 soap_package->ns = m_soap_ns;
813 z_soap_codec_enc_xsl(o, &soap_package,
814 &hres->content_buf, &hres->content_len,
815 soap_handlers, 0, m_s2z_stylesheet);
816 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
818 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
822 int r = send_GDU(gdu, &len);
824 m_bw_stat.add_bytes(len);
829 int Yaz_Proxy::send_to_srw_client_error(int srw_error, const char *add)
831 ODR o = odr_encode();
832 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
833 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
835 srw_res->num_diagnostics = 1;
836 srw_res->diagnostics = (Z_SRW_diagnostic *)
837 odr_malloc(o, sizeof(*srw_res->diagnostics));
838 yaz_mk_std_diagnostic(o, srw_res->diagnostics, srw_error, add);
839 return send_srw_response(srw_pdu);
842 int Yaz_Proxy::z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
843 Z_DefaultDiagFormat *ddf)
845 int bib1_code = *ddf->condition;
846 if (bib1_code == 109)
848 srw_res->num_diagnostics = 1;
849 srw_res->diagnostics = (Z_SRW_diagnostic *)
850 odr_malloc(o, sizeof(*srw_res->diagnostics));
851 yaz_mk_std_diagnostic(o, srw_res->diagnostics,
852 yaz_diag_bib1_to_srw(*ddf->condition),
857 int Yaz_Proxy::send_to_srw_client_ok(int hits, Z_Records *records, int start)
859 ODR o = odr_encode();
860 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
861 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
863 srw_res->numberOfRecords = odr_intdup (o, hits);
864 if (records && records->which == Z_Records_DBOSD)
866 srw_res->num_records =
867 records->u.databaseOrSurDiagnostics->num_records;
869 srw_res->records = (Z_SRW_record *)
870 odr_malloc(o, srw_res->num_records * sizeof(Z_SRW_record));
871 for (i = 0; i < srw_res->num_records; i++)
873 Z_NamePlusRecord *npr = records->u.databaseOrSurDiagnostics->records[i];
874 if (npr->which != Z_NamePlusRecord_databaseRecord)
876 srw_res->records[i].recordSchema = "diagnostic";
877 srw_res->records[i].recordPacking = m_s2z_packing;
878 srw_res->records[i].recordData_buf = "67";
879 srw_res->records[i].recordData_len = 2;
880 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
883 Z_External *r = npr->u.databaseRecord;
884 oident *ent = oid_getentbyoid(r->direct_reference);
885 if (r->which == Z_External_octet && ent->value == VAL_TEXT_XML)
887 srw_res->records[i].recordSchema = m_schema;
888 srw_res->records[i].recordPacking = m_s2z_packing;
889 srw_res->records[i].recordData_buf = (char*)
890 r->u.octet_aligned->buf;
891 srw_res->records[i].recordData_len = r->u.octet_aligned->len;
892 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
896 srw_res->records[i].recordSchema = "diagnostic";
897 srw_res->records[i].recordPacking = m_s2z_packing;
898 srw_res->records[i].recordData_buf = "67";
899 srw_res->records[i].recordData_len = 2;
900 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
904 if (records && records->which == Z_Records_NSD)
907 http_code = z_to_srw_diag(odr_encode(), srw_res,
908 records->u.nonSurrogateDiagnostic);
910 return send_http_response(http_code);
912 return send_srw_response(srw_pdu);
916 int Yaz_Proxy::send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
919 Yaz_ProxyConfig *cfg = check_reconfigure();
923 char *b = cfg->get_explain(odr_encode(), 0 /* target */,
924 m_s2z_database, &len);
927 Z_SRW_PDU *res = yaz_srw_get(odr_encode(), Z_SRW_explain_response);
928 Z_SRW_explainResponse *er = res->u.explain_response;
930 er->record.recordData_buf = b;
931 er->record.recordData_len = len;
932 er->record.recordPacking = m_s2z_packing;
933 er->record.recordSchema = "http://explain.z3950.org/dtd/2.0/";
935 er->diagnostics = diagnostics;
936 er->num_diagnostics = num_diagnostics;
937 return send_srw_response(res);
940 return send_http_response(404);
943 int Yaz_Proxy::send_PDU_convert(Z_APDU *apdu)
947 if (apdu->which == Z_APDU_initResponse)
949 Z_InitResponse *res = apdu->u.initResponse;
950 if (*res->result == 0)
952 send_to_srw_client_error(3, 0);
954 else if (!m_s2z_search_apdu)
956 send_srw_explain_response(0, 0);
960 handle_incoming_Z_PDU(m_s2z_search_apdu);
963 else if (m_s2z_search_apdu && apdu->which == Z_APDU_searchResponse)
965 m_s2z_search_apdu = 0;
966 Z_SearchResponse *res = apdu->u.searchResponse;
967 m_s2z_hit_count = *res->resultCount;
968 if (res->records && res->records->which == Z_Records_NSD)
970 send_to_srw_client_ok(0, res->records, 1);
972 else if (m_s2z_present_apdu && m_s2z_hit_count > 0)
975 Z_PresentRequest *pr = m_s2z_present_apdu->u.presentRequest;
977 if (*pr->resultSetStartPoint <= m_s2z_hit_count)
979 if (*pr->numberOfRecordsRequested+ *pr->resultSetStartPoint
981 *pr->numberOfRecordsRequested =
982 1 + m_s2z_hit_count - *pr->resultSetStartPoint;
984 handle_incoming_Z_PDU(m_s2z_present_apdu);
988 m_s2z_present_apdu = 0;
989 send_to_srw_client_ok(m_s2z_hit_count, res->records, 1);
992 else if (m_s2z_present_apdu && apdu->which == Z_APDU_presentResponse)
995 *m_s2z_present_apdu->u.presentRequest->resultSetStartPoint;
997 m_s2z_present_apdu = 0;
998 Z_PresentResponse *res = apdu->u.presentResponse;
999 send_to_srw_client_ok(m_s2z_hit_count, res->records, start);
1005 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
1006 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
1008 int r = send_Z_PDU(apdu, &len);
1009 m_bytes_sent += len;
1010 m_bw_stat.add_bytes(len);
1017 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
1019 int kill_session = 0;
1020 Z_ReferenceId **new_id = get_referenceIdP(apdu);
1022 if (new_id && m_referenceId)
1023 *new_id = m_referenceId;
1025 if (apdu->which == Z_APDU_searchResponse)
1027 Z_SearchResponse *sr = apdu->u.searchResponse;
1028 Z_Records *p = sr->records;
1029 if (p && p->which == Z_Records_NSD)
1031 Z_DiagRec dr, *dr_p = &dr;
1032 dr.which = Z_DiagRec_defaultFormat;
1033 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1035 *sr->searchStatus = 0;
1036 display_diagrecs(&dr_p, 1);
1040 if (p && p->which == Z_Records_DBOSD)
1043 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1044 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1048 if (sr->resultCount)
1050 yaz_log(LOG_LOG, "%s%d hits", m_session_str,
1052 if (*sr->resultCount < 0)
1054 m_invalid_session = 1;
1057 *sr->searchStatus = 0;
1059 create_nonSurrogateDiagnostics(odr_encode(), 2, 0);
1060 *sr->resultCount = 0;
1065 else if (apdu->which == Z_APDU_presentResponse)
1067 Z_PresentResponse *sr = apdu->u.presentResponse;
1068 Z_Records *p = sr->records;
1069 if (p && p->which == Z_Records_NSD)
1071 Z_DiagRec dr, *dr_p = &dr;
1072 dr.which = Z_DiagRec_defaultFormat;
1073 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1074 if (*sr->presentStatus == Z_PresentStatus_success)
1075 *sr->presentStatus = Z_PresentStatus_failure;
1076 display_diagrecs(&dr_p, 1);
1078 if (p && p->which == Z_Records_DBOSD)
1081 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1082 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1086 else if (apdu->which == Z_APDU_initResponse)
1088 if (m_initRequest_options)
1091 (Odr_bitmask *)odr_malloc(odr_encode(),
1092 sizeof(Odr_bitmask));
1093 ODR_MASK_ZERO(nopt);
1096 for (i = 0; i<24; i++)
1097 if (ODR_MASK_GET(m_initRequest_options, i) &&
1098 ODR_MASK_GET(apdu->u.initResponse->options, i))
1099 ODR_MASK_SET(nopt, i);
1100 apdu->u.initResponse->options = nopt;
1102 if (m_initRequest_version)
1104 Z_ProtocolVersion *nopt =
1105 (Odr_bitmask *)odr_malloc(odr_encode(),
1106 sizeof(Odr_bitmask));
1107 ODR_MASK_ZERO(nopt);
1110 for (i = 0; i<8; i++)
1111 if (ODR_MASK_GET(m_initRequest_version, i) &&
1112 ODR_MASK_GET(apdu->u.initResponse->protocolVersion, i))
1113 ODR_MASK_SET(nopt, i);
1114 apdu->u.initResponse->protocolVersion = nopt;
1116 apdu->u.initResponse->preferredMessageSize =
1117 odr_intdup(odr_encode(),
1118 m_client->m_initResponse_preferredMessageSize >
1119 m_initRequest_preferredMessageSize ?
1120 m_initRequest_preferredMessageSize :
1121 m_client->m_initResponse_preferredMessageSize);
1122 apdu->u.initResponse->maximumRecordSize =
1123 odr_intdup(odr_encode(),
1124 m_client->m_initResponse_maximumRecordSize >
1125 m_initRequest_maximumRecordSize ?
1126 m_initRequest_maximumRecordSize :
1127 m_client->m_initResponse_maximumRecordSize);
1129 int r = send_PDU_convert(apdu);
1136 m_parent->pre_init();
1141 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
1144 const char *apdu_name_tmp = apdu_name(apdu);
1145 int r = send_Z_PDU(apdu, &len);
1146 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
1147 yaz_log (LOG_LOG, "%sSending %s to %s %d bytes",
1149 apdu_name_tmp, get_hostname(), len);
1150 m_bytes_sent += len;
1154 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
1156 if (apdu->which == Z_APDU_presentRequest)
1158 Z_PresentRequest *pr = apdu->u.presentRequest;
1159 int toget = *pr->numberOfRecordsRequested;
1160 int start = *pr->resultSetStartPoint;
1162 yaz_log(LOG_LOG, "%sPresent %s %d+%d", m_session_str,
1163 pr->resultSetId, start, toget);
1165 if (*m_parent->m_optimize == '0')
1168 if (!m_client->m_last_resultSetId)
1170 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1171 new_apdu->u.presentResponse->records =
1172 create_nonSurrogateDiagnostics(odr_encode(), 30,
1174 send_to_client(new_apdu);
1177 if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
1179 if (start+toget-1 > m_client->m_last_resultCount)
1181 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1182 new_apdu->u.presentResponse->records =
1183 create_nonSurrogateDiagnostics(odr_encode(), 13, 0);
1184 send_to_client(new_apdu);
1187 Z_NamePlusRecordList *npr;
1188 if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
1189 pr->preferredRecordSyntax,
1190 pr->recordComposition))
1192 yaz_log (LOG_LOG, "%sReturned cached records for present request",
1194 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1195 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1197 new_apdu->u.presentResponse->numberOfRecordsReturned
1198 = odr_intdup(odr_encode(), toget);
1200 new_apdu->u.presentResponse->records = (Z_Records*)
1201 odr_malloc(odr_encode(), sizeof(Z_Records));
1202 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
1203 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
1204 new_apdu->u.presentResponse->nextResultSetPosition =
1205 odr_intdup(odr_encode(), start+toget);
1207 send_to_client(new_apdu);
1213 if (apdu->which != Z_APDU_searchRequest)
1215 Z_SearchRequest *sr = apdu->u.searchRequest;
1216 Yaz_Z_Query *this_query = new Yaz_Z_Query;
1217 Yaz_Z_Databases this_databases;
1219 this_databases.set(sr->num_databaseNames, (const char **)
1222 this_query->set_Z_Query(sr->query);
1224 char query_str[120];
1225 this_query->print(query_str, sizeof(query_str)-1);
1226 yaz_log(LOG_LOG, "%sSearch %s", m_session_str, query_str);
1228 if (*m_parent->m_optimize != '0' &&
1229 m_client->m_last_ok && m_client->m_last_query &&
1230 m_client->m_last_query->match(this_query) &&
1231 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
1232 m_client->m_last_databases.match(this_databases))
1235 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
1236 m_client->m_last_resultCount < *sr->largeSetLowerBound)
1238 Z_NamePlusRecordList *npr;
1239 int toget = *sr->mediumSetPresentNumber;
1240 Z_RecordComposition *comp = 0;
1242 if (toget > m_client->m_last_resultCount)
1243 toget = m_client->m_last_resultCount;
1245 if (sr->mediumSetElementSetNames)
1247 comp = (Z_RecordComposition *)
1248 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1249 comp->which = Z_RecordComp_simple;
1250 comp->u.simple = sr->mediumSetElementSetNames;
1253 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1254 sr->preferredRecordSyntax, comp))
1256 yaz_log (LOG_LOG, "%sReturned cached records for medium set",
1258 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1259 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1260 new_apdu->u.searchResponse->resultCount =
1261 &m_client->m_last_resultCount;
1263 new_apdu->u.searchResponse->numberOfRecordsReturned
1264 = odr_intdup(odr_encode(), toget);
1266 new_apdu->u.searchResponse->presentStatus =
1267 odr_intdup(odr_encode(), Z_PresentStatus_success);
1268 new_apdu->u.searchResponse->records = (Z_Records*)
1269 odr_malloc(odr_encode(), sizeof(Z_Records));
1270 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1271 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1272 new_apdu->u.searchResponse->nextResultSetPosition =
1273 odr_intdup(odr_encode(), toget+1);
1274 send_to_client(new_apdu);
1280 // send present request (medium size)
1281 yaz_log (LOG_LOG, "%sOptimizing search for medium set",
1284 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1285 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1286 pr->referenceId = sr->referenceId;
1287 pr->resultSetId = sr->resultSetName;
1288 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1289 *pr->numberOfRecordsRequested = toget;
1290 pr->recordComposition = comp;
1291 m_client->m_sr_transform = 1;
1295 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
1296 m_client->m_last_resultCount <= 0)
1298 // large set. Return pseudo-search response immediately
1299 yaz_log (LOG_LOG, "%sOptimizing search for large set",
1301 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1302 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1303 new_apdu->u.searchResponse->resultCount =
1304 &m_client->m_last_resultCount;
1305 send_to_client(new_apdu);
1310 Z_NamePlusRecordList *npr;
1311 int toget = m_client->m_last_resultCount;
1312 Z_RecordComposition *comp = 0;
1314 // send a present request (small set)
1316 if (sr->smallSetElementSetNames)
1318 comp = (Z_RecordComposition *)
1319 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1320 comp->which = Z_RecordComp_simple;
1321 comp->u.simple = sr->smallSetElementSetNames;
1324 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1325 sr->preferredRecordSyntax, comp))
1327 yaz_log (LOG_LOG, "%sReturned cached records for small set",
1329 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1330 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1331 new_apdu->u.searchResponse->resultCount =
1332 &m_client->m_last_resultCount;
1334 new_apdu->u.searchResponse->numberOfRecordsReturned
1335 = odr_intdup(odr_encode(), toget);
1337 new_apdu->u.searchResponse->presentStatus =
1338 odr_intdup(odr_encode(), Z_PresentStatus_success);
1339 new_apdu->u.searchResponse->records = (Z_Records*)
1340 odr_malloc(odr_encode(), sizeof(Z_Records));
1341 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1342 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1343 new_apdu->u.searchResponse->nextResultSetPosition =
1344 odr_intdup(odr_encode(), toget+1);
1345 send_to_client(new_apdu);
1350 yaz_log (LOG_LOG, "%sOptimizing search for small set",
1352 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1353 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1354 pr->referenceId = sr->referenceId;
1355 pr->resultSetId = sr->resultSetName;
1356 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1357 *pr->numberOfRecordsRequested = toget;
1358 pr->recordComposition = comp;
1359 m_client->m_sr_transform = 1;
1364 else // query doesn't match
1366 delete m_client->m_last_query;
1367 m_client->m_last_query = this_query;
1368 m_client->m_last_ok = 0;
1369 m_client->m_cache.clear();
1370 m_client->m_resultSetStartPoint = 0;
1372 xfree (m_client->m_last_resultSetId);
1373 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
1375 m_client->m_last_databases.set(sr->num_databaseNames,
1376 (const char **) sr->databaseNames);
1382 void Yaz_Proxy::inc_request_no()
1384 char *cp = strchr(m_session_str, ' ');
1387 sprintf(cp+1, "%d ", m_request_no);
1390 void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
1394 m_bytes_recv += len;
1396 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
1397 yaz_log (LOG_DEBUG, "%sReceiving %s from client %d bytes",
1398 m_session_str, gdu_name(apdu), len);
1400 if (m_bw_hold_PDU) // double incoming PDU. shutdown now.
1403 m_bw_stat.add_bytes(len);
1404 m_pdu_stat.add_bytes(1);
1406 gettimeofday(&m_time_tv, 0);
1408 int bw_total = m_bw_stat.get_total();
1409 int pdu_total = m_pdu_stat.get_total();
1414 if (bw_total > m_bw_max)
1416 reduce = (bw_total/m_bw_max);
1421 if (pdu_total > m_pdu_max)
1423 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
1424 reduce = (reduce > nreduce) ? reduce : nreduce;
1429 yaz_log(LOG_LOG, "%sdelay=%d bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
1430 m_session_str, reduce, bw_total, pdu_total,
1431 m_bw_max, m_pdu_max);
1433 m_bw_hold_PDU = apdu; // save PDU and signal "on hold"
1434 timeout(reduce); // call us reduce seconds later
1436 else if (apdu->which == Z_GDU_Z3950)
1437 handle_incoming_Z_PDU(apdu->u.z3950);
1438 else if (apdu->which == Z_GDU_HTTP_Request)
1439 handle_incoming_HTTP(apdu->u.HTTP_Request);
1442 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
1444 if (m_max_record_retrieve)
1446 if (apdu->which == Z_APDU_presentRequest)
1448 Z_PresentRequest *pr = apdu->u.presentRequest;
1449 if (pr->numberOfRecordsRequested &&
1450 *pr->numberOfRecordsRequested > m_max_record_retrieve)
1451 *pr->numberOfRecordsRequested = m_max_record_retrieve;
1456 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
1458 const char *addinfo)
1460 Z_Records *rec = (Z_Records *)
1461 odr_malloc (odr, sizeof(*rec));
1463 odr_malloc (odr, sizeof(*err));
1464 Z_DiagRec *drec = (Z_DiagRec *)
1465 odr_malloc (odr, sizeof(*drec));
1466 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1467 odr_malloc (odr, sizeof(*dr));
1469 rec->which = Z_Records_NSD;
1470 rec->u.nonSurrogateDiagnostic = dr;
1471 dr->diagnosticSetId =
1472 yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
1473 dr->condition = err;
1474 dr->which = Z_DefaultDiagFormat_v2Addinfo;
1475 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
1479 Z_APDU *Yaz_Proxy::handle_query_transformation(Z_APDU *apdu)
1481 if (apdu->which == Z_APDU_searchRequest &&
1482 apdu->u.searchRequest->query &&
1483 apdu->u.searchRequest->query->which == Z_Query_type_104 &&
1484 apdu->u.searchRequest->query->u.type_104->which == Z_External_CQL)
1486 Z_RPNQuery *rpnquery = 0;
1487 Z_SearchRequest *sr = apdu->u.searchRequest;
1490 yaz_log(LOG_LOG, "%sCQL: %s", m_session_str,
1491 sr->query->u.type_104->u.cql);
1493 int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
1494 &rpnquery, odr_encode(),
1497 yaz_log(LOG_LOG, "%sNo CQL to RPN table", m_session_str);
1500 yaz_log(LOG_LOG, "%sCQL Conversion error %d", m_session_str, r);
1501 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1503 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1504 new_apdu->u.searchResponse->records =
1505 create_nonSurrogateDiagnostics(odr_encode(),
1506 yaz_diag_srw_to_bib1(r),
1508 *new_apdu->u.searchResponse->searchStatus = 0;
1510 send_to_client(new_apdu);
1516 sr->query->which = Z_Query_type_1;
1517 sr->query->u.type_1 = rpnquery;
1524 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
1526 if (apdu->which == Z_APDU_searchRequest)
1528 Z_SearchRequest *sr = apdu->u.searchRequest;
1532 Yaz_ProxyConfig *cfg = check_reconfigure();
1534 err = cfg->check_query(odr_encode(), m_default_target,
1535 sr->query, &addinfo);
1538 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1540 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1541 new_apdu->u.searchResponse->records =
1542 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1543 *new_apdu->u.searchResponse->searchStatus = 0;
1545 send_to_client(new_apdu);
1553 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
1556 if (apdu->which == Z_APDU_searchRequest)
1558 Z_SearchRequest *sr = apdu->u.searchRequest;
1561 Yaz_ProxyConfig *cfg = check_reconfigure();
1563 Z_RecordComposition rc_temp, *rc = 0;
1564 if (sr->smallSetElementSetNames)
1566 rc_temp.which = Z_RecordComp_simple;
1567 rc_temp.u.simple = sr->smallSetElementSetNames;
1571 char *stylesheet_name = 0;
1573 err = cfg->check_syntax(odr_encode(),
1575 sr->preferredRecordSyntax, rc,
1576 &addinfo, &stylesheet_name, &m_schema);
1577 if (stylesheet_name)
1579 m_parent->low_socket_close();
1581 if (m_stylesheet_xsp)
1582 xsltFreeStylesheet(m_stylesheet_xsp);
1584 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1586 m_stylesheet_offset = 0;
1587 xfree(stylesheet_name);
1589 m_parent->low_socket_open();
1593 sr->preferredRecordSyntax =
1594 yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN, VAL_USMARC);
1599 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1601 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1602 new_apdu->u.searchResponse->records =
1603 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1604 *new_apdu->u.searchResponse->searchStatus = 0;
1606 send_to_client(new_apdu);
1611 else if (apdu->which == Z_APDU_presentRequest)
1613 Z_PresentRequest *pr = apdu->u.presentRequest;
1616 Yaz_ProxyConfig *cfg = check_reconfigure();
1618 char *stylesheet_name = 0;
1620 err = cfg->check_syntax(odr_encode(), m_default_target,
1621 pr->preferredRecordSyntax,
1622 pr->recordComposition,
1623 &addinfo, &stylesheet_name, &m_schema);
1624 if (stylesheet_name)
1626 m_parent->low_socket_close();
1628 if (m_stylesheet_xsp)
1629 xsltFreeStylesheet(m_stylesheet_xsp);
1631 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1633 m_stylesheet_offset = 0;
1634 xfree(stylesheet_name);
1636 m_parent->low_socket_open();
1640 pr->preferredRecordSyntax =
1641 yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN, VAL_USMARC);
1646 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1648 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1649 new_apdu->u.presentResponse->records =
1650 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1651 *new_apdu->u.presentResponse->presentStatus =
1652 Z_PresentStatus_failure;
1654 send_to_client(new_apdu);
1662 Z_ElementSetNames *Yaz_Proxy::mk_esn_from_schema(ODR o, const char *schema)
1666 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1667 odr_malloc(o, sizeof(Z_ElementSetNames));
1668 esn->which = Z_ElementSetNames_generic;
1669 esn->u.generic = odr_strdup(o, schema);
1673 void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
1677 odr_destroy(m_s2z_odr_init);
1680 if (m_s2z_odr_search)
1682 odr_destroy(m_s2z_odr_search);
1683 m_s2z_odr_search = 0;
1686 m_http_keepalive = 0;
1688 if (!strcmp(hreq->version, "1.0"))
1690 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1691 if (v && !strcmp(v, "Keep-Alive"))
1692 m_http_keepalive = 1;
1694 m_http_keepalive = 0;
1695 m_http_version = "1.0";
1699 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1700 if (v && !strcmp(v, "close"))
1701 m_http_keepalive = 0;
1703 m_http_keepalive = 1;
1704 m_http_version = "1.1";
1707 Z_SRW_PDU *srw_pdu = 0;
1708 Z_SOAP *soap_package = 0;
1710 Z_SRW_diagnostic *diagnostic = 0;
1711 int num_diagnostic = 0;
1712 if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1714 || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1715 &charset, &diagnostic, &num_diagnostic) == 0)
1717 m_s2z_odr_init = odr_createmem(ODR_ENCODE);
1718 m_s2z_odr_search = odr_createmem(ODR_ENCODE);
1719 m_soap_ns = odr_strdup(m_s2z_odr_search, soap_package->ns);
1720 m_s2z_init_apdu = 0;
1721 m_s2z_search_apdu = 0;
1722 m_s2z_present_apdu = 0;
1724 m_s2z_stylesheet = 0;
1726 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
1728 Z_SRW_searchRetrieveRequest *srw_req = srw_pdu->u.request;
1730 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1731 // recordXPath unsupported.
1732 if (srw_req->recordXPath)
1734 yaz_add_srw_diagnostic(odr_decode(),
1735 &diagnostic, &num_diagnostic,
1738 // must have a query
1739 if (!srw_req->query.cql)
1741 yaz_add_srw_diagnostic(odr_decode(),
1742 &diagnostic, &num_diagnostic,
1746 if (srw_req->sort_type != Z_SRW_sort_type_none)
1748 yaz_add_srw_diagnostic(odr_decode(),
1749 &diagnostic, &num_diagnostic,
1753 if (srw_req->stylesheet)
1755 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1757 // set packing for response records ..
1758 if (srw_req->recordPacking &&
1759 !strcmp(srw_req->recordPacking, "xml"))
1760 m_s2z_packing = Z_SRW_recordPacking_XML;
1762 m_s2z_packing = Z_SRW_recordPacking_string;
1766 Z_SRW_PDU *srw_pdu =
1767 yaz_srw_get(odr_encode(),
1768 Z_SRW_searchRetrieve_response);
1769 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1771 srw_res->diagnostics = diagnostic;
1772 srw_res->num_diagnostics = num_diagnostic;
1773 send_srw_response(srw_pdu);
1777 // prepare search PDU
1778 m_s2z_search_apdu = zget_APDU(m_s2z_odr_search,
1779 Z_APDU_searchRequest);
1780 Z_SearchRequest *z_searchRequest =
1781 m_s2z_search_apdu->u.searchRequest;
1783 z_searchRequest->num_databaseNames = 1;
1784 z_searchRequest->databaseNames = (char**)
1785 odr_malloc(m_s2z_odr_search, sizeof(char *));
1786 z_searchRequest->databaseNames[0] = odr_strdup(m_s2z_odr_search,
1789 // query transformation
1790 Z_Query *query = (Z_Query *)
1791 odr_malloc(m_s2z_odr_search, sizeof(Z_Query));
1792 z_searchRequest->query = query;
1794 if (srw_req->query_type == Z_SRW_query_type_cql)
1796 Z_External *ext = (Z_External *)
1797 odr_malloc(m_s2z_odr_search, sizeof(*ext));
1798 ext->direct_reference =
1799 odr_getoidbystr(m_s2z_odr_search, "1.2.840.10003.16.2");
1800 ext->indirect_reference = 0;
1801 ext->descriptor = 0;
1802 ext->which = Z_External_CQL;
1803 ext->u.cql = srw_req->query.cql;
1805 query->which = Z_Query_type_104;
1806 query->u.type_104 = ext;
1808 else if (srw_req->query_type == Z_SRW_query_type_pqf)
1810 Z_RPNQuery *RPNquery;
1811 YAZ_PQF_Parser pqf_parser;
1813 pqf_parser = yaz_pqf_create ();
1815 RPNquery = yaz_pqf_parse (pqf_parser, m_s2z_odr_search,
1816 srw_req->query.pqf);
1819 const char *pqf_msg;
1821 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1822 yaz_log(LOG_LOG, "%*s^\n", off+4, "");
1823 yaz_log(LOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
1825 send_to_srw_client_error(10, 0);
1828 query->which = Z_Query_type_1;
1829 query->u.type_1 = RPNquery;
1831 yaz_pqf_destroy (pqf_parser);
1835 send_to_srw_client_error(7, "query");
1840 m_s2z_present_apdu = 0;
1842 if (srw_req->maximumRecords)
1843 max = *srw_req->maximumRecords;
1845 if (srw_req->startRecord)
1846 start = *srw_req->startRecord;
1849 // Some backend, such as Voyager doesn't honor piggyback
1850 // So we use present always (0 &&).
1851 if (0 && start <= 1) // Z39.50 piggyback
1853 *z_searchRequest->smallSetUpperBound = max;
1854 *z_searchRequest->mediumSetPresentNumber = max;
1855 *z_searchRequest->largeSetLowerBound = 2000000000; // 2e9
1857 z_searchRequest->preferredRecordSyntax =
1858 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1860 if (srw_req->recordSchema)
1862 z_searchRequest->smallSetElementSetNames =
1863 z_searchRequest->mediumSetElementSetNames =
1864 mk_esn_from_schema(m_s2z_odr_search,
1865 srw_req->recordSchema);
1868 else // Z39.50 present
1870 m_s2z_present_apdu = zget_APDU(m_s2z_odr_search,
1871 Z_APDU_presentRequest);
1872 Z_PresentRequest *z_presentRequest =
1873 m_s2z_present_apdu->u.presentRequest;
1874 *z_presentRequest->resultSetStartPoint = start;
1875 *z_presentRequest->numberOfRecordsRequested = max;
1876 z_presentRequest->preferredRecordSyntax =
1877 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1879 if (srw_req->recordSchema)
1881 z_presentRequest->recordComposition =
1882 (Z_RecordComposition *)
1883 odr_malloc(m_s2z_odr_search,
1884 sizeof(Z_RecordComposition));
1885 z_presentRequest->recordComposition->which =
1886 Z_RecordComp_simple;
1887 z_presentRequest->recordComposition->u.simple =
1888 mk_esn_from_schema(m_s2z_odr_search,
1889 srw_req->recordSchema);
1895 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1896 Z_APDU_initRequest);
1898 // prevent m_initRequest_apdu memory from being grabbed
1899 // in Yaz_Proxy::handle_incoming_Z_PDU
1900 m_initRequest_apdu = m_s2z_init_apdu;
1901 handle_incoming_Z_PDU(m_s2z_init_apdu);
1906 handle_incoming_Z_PDU(m_s2z_search_apdu);
1910 else if (srw_pdu->which == Z_SRW_explain_request)
1912 Z_SRW_explainRequest *srw_req = srw_pdu->u.explain_request;
1914 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1917 if (srw_req->stylesheet)
1919 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1921 if (srw_req->recordPacking &&
1922 !strcmp(srw_req->recordPacking, "xml"))
1923 m_s2z_packing = Z_SRW_recordPacking_XML;
1925 m_s2z_packing = Z_SRW_recordPacking_string;
1929 send_srw_explain_response(diagnostic, num_diagnostic);
1935 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1936 Z_APDU_initRequest);
1938 // prevent m_initRequest_apdu memory from being grabbed
1939 // in Yaz_Proxy::handle_incoming_Z_PDU
1940 m_initRequest_apdu = m_s2z_init_apdu;
1941 handle_incoming_Z_PDU(m_s2z_init_apdu);
1944 send_srw_explain_response(0, 0);
1947 else if (srw_pdu->which == Z_SRW_scan_request)
1949 m_s2z_database = odr_strdup(m_s2z_odr_init,
1950 srw_pdu->u.scan_request->database);
1952 yaz_add_srw_diagnostic(odr_decode(),
1953 &diagnostic, &num_diagnostic,
1955 Z_SRW_PDU *srw_pdu =
1956 yaz_srw_get(odr_encode(),
1957 Z_SRW_scan_response);
1958 Z_SRW_scanResponse *srw_res = srw_pdu->u.scan_response;
1960 srw_res->diagnostics = diagnostic;
1961 srw_res->num_diagnostics = num_diagnostic;
1962 send_srw_response(srw_pdu);
1969 send_to_srw_client_error(4, 0);
1973 Z_GDU *p = z_get_HTTP_Response(odr_encode(), 400);
1978 void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
1980 Z_ReferenceId **refid = get_referenceIdP(apdu);
1981 nmem_reset(m_referenceId_mem);
1982 if (refid && *refid)
1984 m_referenceId = (Z_ReferenceId *)
1985 nmem_malloc(m_referenceId_mem, sizeof(*m_referenceId));
1986 m_referenceId->len = m_referenceId->size = (*refid)->len;
1987 m_referenceId->buf = (unsigned char *)
1988 nmem_malloc(m_referenceId_mem, (*refid)->len);
1989 memcpy(m_referenceId->buf, (*refid)->buf, (*refid)->len);
1994 if (!m_client && m_invalid_session)
1996 m_apdu_invalid_session = apdu;
1997 m_mem_invalid_session = odr_extract_mem(odr_decode());
1998 apdu = m_initRequest_apdu;
2001 // Determine our client.
2002 Z_OtherInformation **oi;
2003 get_otherInfoAPDU(apdu, &oi);
2004 m_client = get_client(apdu, get_cookie(oi), get_proxy(oi));
2010 m_client->m_server = this;
2012 if (apdu->which == Z_APDU_initRequest)
2014 if (apdu->u.initRequest->implementationId)
2015 yaz_log(LOG_LOG, "%simplementationId: %s",
2016 m_session_str, apdu->u.initRequest->implementationId);
2017 if (apdu->u.initRequest->implementationName)
2018 yaz_log(LOG_LOG, "%simplementationName: %s",
2019 m_session_str, apdu->u.initRequest->implementationName);
2020 if (apdu->u.initRequest->implementationVersion)
2021 yaz_log(LOG_LOG, "%simplementationVersion: %s",
2022 m_session_str, apdu->u.initRequest->implementationVersion);
2023 if (m_initRequest_apdu == 0)
2025 if (m_initRequest_mem)
2026 nmem_destroy(m_initRequest_mem);
2027 m_initRequest_apdu = apdu;
2028 m_initRequest_mem = odr_extract_mem(odr_decode());
2030 m_initRequest_preferredMessageSize = *apdu->u.initRequest->
2031 preferredMessageSize;
2032 *apdu->u.initRequest->preferredMessageSize = 1024*1024;
2033 m_initRequest_maximumRecordSize = *apdu->u.initRequest->
2035 *apdu->u.initRequest->maximumRecordSize = 1024*1024;
2037 // save init options for the response..
2038 m_initRequest_options = apdu->u.initRequest->options;
2040 apdu->u.initRequest->options =
2041 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2042 sizeof(Odr_bitmask));
2043 ODR_MASK_ZERO(apdu->u.initRequest->options);
2045 for (i = 0; i<= 24; i++)
2046 ODR_MASK_SET(apdu->u.initRequest->options, i);
2047 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2048 Z_Options_negotiationModel);
2049 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2050 Z_Options_concurrentOperations);
2053 m_initRequest_version = apdu->u.initRequest->protocolVersion;
2054 apdu->u.initRequest->protocolVersion =
2055 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2056 sizeof(Odr_bitmask));
2057 ODR_MASK_ZERO(apdu->u.initRequest->protocolVersion);
2059 for (i = 0; i<= 8; i++)
2060 ODR_MASK_SET(apdu->u.initRequest->protocolVersion, i);
2062 if (m_client->m_init_flag)
2064 if (handle_init_response_for_invalid_session(apdu))
2066 if (m_client->m_initResponse)
2068 Z_APDU *apdu2 = m_client->m_initResponse;
2069 apdu2->u.initResponse->otherInfo = 0;
2070 if (m_client->m_cookie && *m_client->m_cookie)
2071 set_otherInformationString(apdu2, VAL_COOKIE, 1,
2072 m_client->m_cookie);
2073 apdu2->u.initResponse->referenceId =
2074 apdu->u.initRequest->referenceId;
2075 apdu2->u.initResponse->options = m_client->m_initResponse_options;
2076 apdu2->u.initResponse->protocolVersion =
2077 m_client->m_initResponse_version;
2079 send_to_client(apdu2);
2083 m_client->m_init_flag = 1;
2085 handle_max_record_retrieve(apdu);
2088 apdu = handle_syntax_validation(apdu);
2091 apdu = handle_query_transformation(apdu);
2094 apdu = handle_query_validation(apdu);
2097 apdu = result_set_optimize(apdu);
2100 m_client->timeout(m_target_idletime); // mark it active even
2101 // though we didn't use it
2105 // delete other info part from PDU before sending to target
2106 get_otherInfoAPDU(apdu, &oi);
2110 if (apdu->which == Z_APDU_presentRequest &&
2111 m_client->m_resultSetStartPoint == 0)
2113 Z_PresentRequest *pr = apdu->u.presentRequest;
2114 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
2115 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
2117 m_client->m_resultSetStartPoint = 0;
2119 if (m_client->send_to_target(apdu) < 0)
2126 m_client->m_waiting = 1;
2129 void Yaz_Proxy::connectNotify()
2133 void Yaz_Proxy::shutdown()
2135 m_invalid_session = 0;
2136 // only keep if keep_alive flag is set...
2138 m_client->m_pdu_recv < m_keepalive_limit_pdu &&
2139 m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
2140 m_client->m_waiting == 0)
2142 yaz_log(LOG_LOG, "%sShutdown (client to proxy) keepalive %s",
2144 m_client->get_hostname());
2145 yaz_log(LOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
2146 m_session_str, m_client->m_pdu_recv,
2147 m_client->m_bytes_sent + m_client->m_bytes_recv,
2148 m_keepalive_limit_bw, m_keepalive_limit_pdu);
2149 assert (m_client->m_waiting != 2);
2150 // Tell client (if any) that no server connection is there..
2151 m_client->m_server = 0;
2152 m_invalid_session = 0;
2156 yaz_log (LOG_LOG, "%sShutdown (client to proxy) close %s",
2158 m_client->get_hostname());
2159 assert (m_client->m_waiting != 2);
2164 yaz_log (LOG_LOG, "%sshutdown (client to proxy) bad state",
2170 yaz_log (LOG_LOG, "%sShutdown (client to proxy)",
2174 m_parent->pre_init();
2178 const char *Yaz_ProxyClient::get_session_str()
2182 return m_server->get_session_str();
2185 void Yaz_ProxyClient::shutdown()
2187 yaz_log (LOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
2193 void Yaz_Proxy::failNotify()
2196 yaz_log (LOG_LOG, "%sConnection closed by client",
2201 void Yaz_ProxyClient::failNotify()
2204 m_server->inc_request_no();
2205 yaz_log (LOG_LOG, "%sConnection closed by target %s",
2206 get_session_str(), get_hostname());
2210 void Yaz_ProxyClient::connectNotify()
2212 const char *s = get_session_str();
2213 const char *h = get_hostname();
2214 yaz_log (LOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
2216 timeout(m_target_idletime);
2221 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
2222 *the_PDU_Observable, int fd)
2224 return new Yaz_ProxyClient(the_PDU_Observable, 0);
2227 Yaz_ProxyClient::~Yaz_ProxyClient()
2232 m_next->m_prev = m_prev;
2233 m_waiting = 2; // for debugging purposes only.
2234 odr_destroy(m_init_odr);
2235 delete m_last_query;
2236 xfree (m_last_resultSetId);
2240 void Yaz_ProxyClient::pre_init_client()
2242 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
2243 Z_InitRequest *req = apdu->u.initRequest;
2246 for (i = 0; i<= 24; i++)
2247 ODR_MASK_SET(req->options, i);
2248 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2249 Z_Options_negotiationModel);
2250 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2251 Z_Options_concurrentOperations);
2252 for (i = 0; i<= 10; i++)
2253 ODR_MASK_SET(req->protocolVersion, i);
2255 if (send_to_target(apdu) < 0)
2266 void Yaz_Proxy::pre_init()
2269 const char *name = 0;
2270 const char *zurl_in_use[MAX_ZURL_PLEX];
2271 int limit_bw, limit_pdu, limit_req;
2272 int target_idletime, client_idletime;
2274 int keepalive_limit_bw, keepalive_limit_pdu;
2276 const char *cql2rpn = 0;
2278 Yaz_ProxyConfig *cfg = check_reconfigure();
2282 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
2287 for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
2288 &limit_bw, &limit_pdu, &limit_req,
2289 &target_idletime, &client_idletime,
2291 &keepalive_limit_bw,
2292 &keepalive_limit_pdu,
2299 for (j = 0; zurl_in_use[j]; j++)
2303 int spare_waiting = 0;
2306 for (c = m_clientPool; c; c = c->m_next)
2308 if (!strcmp(zurl_in_use[j], c->get_hostname()))
2310 if (c->m_cookie == 0)
2312 if (c->m_server == 0)
2324 yaz_log(LOG_LOG, "%spre-init %s %s use=%d other=%d spare=%d "
2325 "sparew=%d preinit=%d",m_session_str,
2326 name, zurl_in_use[j], in_use, other,
2327 spare, spare_waiting, pre_init);
2328 if (spare + spare_waiting < pre_init)
2330 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
2331 c->m_next = m_clientPool;
2333 c->m_next->m_prev = &c->m_next;
2335 c->m_prev = &m_clientPool;
2337 if (m_log_mask & PROXY_LOG_APDU_SERVER)
2338 c->set_APDU_yazlog(1);
2340 c->set_APDU_yazlog(0);
2342 if (c->client(zurl_in_use[j]))
2350 c->m_target_idletime = target_idletime;
2351 c->m_seqno = m_seqno++;
2358 void Yaz_Proxy::timeoutNotify()
2364 timeout(m_client_idletime);
2365 Z_GDU *apdu = m_bw_hold_PDU;
2368 if (apdu->which == Z_GDU_Z3950)
2369 handle_incoming_Z_PDU(apdu->u.z3950);
2370 else if (apdu->which == Z_GDU_HTTP_Request)
2371 handle_incoming_HTTP(apdu->u.HTTP_Request);
2373 else if (m_stylesheet_nprl)
2374 convert_xsl_delay();
2379 yaz_log (LOG_LOG, "%sTimeout (client to proxy)", m_session_str);
2390 void Yaz_Proxy::markInvalid()
2393 m_invalid_session = 1;
2396 void Yaz_ProxyClient::timeoutNotify()
2399 m_server->inc_request_no();
2401 yaz_log (LOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
2405 if (m_server && m_init_flag)
2407 // target timed out in a session that was properly initialized
2408 // server object stay alive but we mark it as invalid so it
2409 // gets initialized again
2410 m_server->markInvalid();
2416 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
2417 Yaz_Proxy *parent) :
2418 Yaz_Z_Assoc (the_PDU_Observable)
2425 m_last_resultSetId = 0;
2426 m_last_resultCount = 0;
2430 m_init_odr = odr_createmem (ODR_DECODE);
2432 m_initResponse_options = 0;
2433 m_initResponse_version = 0;
2434 m_initResponse_preferredMessageSize = 0;
2435 m_initResponse_maximumRecordSize = 0;
2436 m_resultSetStartPoint = 0;
2437 m_bytes_sent = m_bytes_recv = 0;
2441 m_target_idletime = 600;
2445 const char *Yaz_Proxy::option(const char *name, const char *value)
2447 if (!strcmp (name, "optimize")) {
2450 m_optimize = xstrdup (value);
2457 void Yaz_ProxyClient::recv_HTTP_response(Z_HTTP_Response *apdu, int len)
2462 void Yaz_ProxyClient::recv_GDU(Z_GDU *apdu, int len)
2464 if (apdu->which == Z_GDU_Z3950)
2465 recv_Z_PDU(apdu->u.z3950, len);
2466 else if (apdu->which == Z_GDU_HTTP_Response)
2467 recv_HTTP_response(apdu->u.HTTP_Response, len);
2472 int Yaz_Proxy::handle_init_response_for_invalid_session(Z_APDU *apdu)
2474 if (!m_invalid_session)
2476 m_invalid_session = 0;
2477 handle_incoming_Z_PDU(m_apdu_invalid_session);
2478 assert (m_mem_invalid_session);
2479 nmem_destroy(m_mem_invalid_session);
2480 m_mem_invalid_session = 0;
2484 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
2486 m_bytes_recv += len;
2490 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
2491 yaz_log (LOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
2492 apdu_name(apdu), get_hostname(), len);
2493 if (apdu->which == Z_APDU_initResponse)
2495 if (!m_server) // if this is a pre init session , check for more
2497 NMEM nmem = odr_extract_mem (odr_decode());
2498 odr_reset (m_init_odr);
2499 nmem_transfer (m_init_odr->mem, nmem);
2500 m_initResponse = apdu;
2501 m_initResponse_options = apdu->u.initResponse->options;
2502 m_initResponse_version = apdu->u.initResponse->protocolVersion;
2503 m_initResponse_preferredMessageSize =
2504 *apdu->u.initResponse->preferredMessageSize;
2505 m_initResponse_maximumRecordSize =
2506 *apdu->u.initResponse->maximumRecordSize;
2508 Z_InitResponse *ir = apdu->u.initResponse;
2509 char *im0 = ir->implementationName;
2512 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
2519 strcat(im1, "(YAZ Proxy)");
2520 ir->implementationName = im1;
2522 nmem_destroy (nmem);
2524 if (m_server && m_server->handle_init_response_for_invalid_session(apdu))
2527 if (apdu->which == Z_APDU_searchResponse)
2529 Z_SearchResponse *sr = apdu->u.searchResponse;
2530 m_last_resultCount = *sr->resultCount;
2531 int status = *sr->searchStatus;
2532 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
2536 if (sr->records && sr->records->which == Z_Records_DBOSD)
2538 m_cache.add(odr_decode(),
2539 sr->records->u.databaseOrSurDiagnostics, 1,
2544 if (apdu->which == Z_APDU_presentResponse)
2546 Z_PresentResponse *pr = apdu->u.presentResponse;
2550 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2551 Z_SearchResponse *sr = new_apdu->u.searchResponse;
2552 sr->referenceId = pr->referenceId;
2553 *sr->resultCount = m_last_resultCount;
2554 sr->records = pr->records;
2555 sr->nextResultSetPosition = pr->nextResultSetPosition;
2556 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
2560 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
2562 m_cache.add(odr_decode(),
2563 pr->records->u.databaseOrSurDiagnostics,
2564 m_resultSetStartPoint, -1);
2565 m_resultSetStartPoint = 0;
2569 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
2572 m_server->send_to_client(apdu);
2574 if (apdu->which == Z_APDU_close)
2580 void Yaz_Proxy::low_socket_close()
2583 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
2584 if (m_lo_fd[i] >= 0)
2585 ::close(m_lo_fd[i]);
2588 void Yaz_Proxy::low_socket_open()
2591 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
2592 m_lo_fd[i] = open("/dev/null", O_RDONLY);
2595 int Yaz_Proxy::server(const char *addr)
2597 int r = Yaz_Z_Assoc::server(addr);
2600 yaz_log(LOG_LOG, "%sStarted proxy " VERSION " on %s", m_session_str, addr);