2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.101 2004-02-15 16:41:14 adam Exp $
12 #include <yaz/marcdisp.h>
13 #include <yaz/yaz-iconv.h>
15 #include <yaz/diagbib1.h>
16 #include <yaz++/proxy.h>
17 #include <yaz/pquery.h>
19 static const char *apdu_name(Z_APDU *apdu)
23 case Z_APDU_initRequest:
25 case Z_APDU_initResponse:
26 return "initResponse";
27 case Z_APDU_searchRequest:
28 return "searchRequest";
29 case Z_APDU_searchResponse:
30 return "searchResponse";
31 case Z_APDU_presentRequest:
32 return "presentRequest";
33 case Z_APDU_presentResponse:
34 return "presentResponse";
35 case Z_APDU_deleteResultSetRequest:
36 return "deleteResultSetRequest";
37 case Z_APDU_deleteResultSetResponse:
38 return "deleteResultSetResponse";
39 case Z_APDU_scanRequest:
41 case Z_APDU_scanResponse:
42 return "scanResponse";
43 case Z_APDU_sortRequest:
45 case Z_APDU_sortResponse:
46 return "sortResponse";
47 case Z_APDU_extendedServicesRequest:
48 return "extendedServicesRequest";
49 case Z_APDU_extendedServicesResponse:
50 return "extendedServicesResponse";
57 static const char *gdu_name(Z_GDU *gdu)
62 return apdu_name(gdu->u.z3950);
63 case Z_GDU_HTTP_Request:
64 return "HTTP Request";
65 case Z_GDU_HTTP_Response:
66 return "HTTP Response";
68 return "Unknown request/response";
71 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
73 Yaz_Z_Assoc(the_PDU_Observable), m_bw_stat(60), m_pdu_stat(60)
75 m_PDU_Observable = the_PDU_Observable;
80 m_keepalive_limit_bw = 500000;
81 m_keepalive_limit_pdu = 1000;
84 m_proxy_authentication = 0;
88 m_client_idletime = 600;
89 m_target_idletime = 600;
90 m_optimize = xstrdup ("1");
91 strcpy(m_session_str, "0 ");
98 m_max_record_retrieve = 0;
102 m_invalid_session = 0;
104 m_referenceId_mem = nmem_create();
107 m_stylesheet_xsp = 0;
108 m_stylesheet_nprl = 0;
109 m_s2z_stylesheet = 0;
112 m_initRequest_apdu = 0;
113 m_initRequest_mem = 0;
114 m_initRequest_options = 0;
115 m_initRequest_version = 0;
116 m_apdu_invalid_session = 0;
117 m_mem_invalid_session = 0;
119 m_s2z_odr_search = 0;
121 m_s2z_search_apdu = 0;
122 m_s2z_present_apdu = 0;
123 m_http_keepalive = 0;
126 m_s2z_packing = Z_SRW_recordPacking_string;
127 m_time_tv.tv_sec = 0;
128 m_time_tv.tv_usec = 0;
131 Yaz_Proxy::~Yaz_Proxy()
133 yaz_log(LOG_LOG, "%sClosed %d/%d sent/recv bytes total", m_session_str,
134 m_bytes_sent, m_bytes_recv);
135 nmem_destroy(m_initRequest_mem);
136 nmem_destroy(m_mem_invalid_session);
137 nmem_destroy(m_referenceId_mem);
139 xfree (m_proxyTarget);
140 xfree (m_default_target);
141 xfree (m_proxy_authentication);
144 if (m_stylesheet_xsp)
145 xsltFreeStylesheet(m_stylesheet_xsp);
149 odr_destroy(m_s2z_odr_init);
150 if (m_s2z_odr_search)
151 odr_destroy(m_s2z_odr_search);
155 int Yaz_Proxy::set_config(const char *config)
158 m_config = new Yaz_ProxyConfig();
159 xfree(m_config_fname);
160 m_config_fname = xstrdup(config);
161 int r = m_config->read_xml(config);
163 m_config->get_generic_info(&m_log_mask, &m_max_clients);
167 void Yaz_Proxy::set_default_target(const char *target)
169 xfree (m_default_target);
170 m_default_target = 0;
172 m_default_target = (char *) xstrdup (target);
175 void Yaz_Proxy::set_proxy_authentication (const char *auth)
177 xfree (m_proxy_authentication);
178 m_proxy_authentication = 0;
180 m_proxy_authentication = (char *) xstrdup (auth);
183 Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
186 return m_parent->check_reconfigure();
188 Yaz_ProxyConfig *cfg = m_config;
191 yaz_log(LOG_LOG, "reconfigure");
193 if (m_config_fname && cfg)
195 yaz_log(LOG_LOG, "reconfigure config %s", m_config_fname);
196 int r = cfg->read_xml(m_config_fname);
198 yaz_log(LOG_WARN, "reconfigure failed");
202 cfg->get_generic_info(&m_log_mask, &m_max_clients);
206 yaz_log(LOG_LOG, "reconfigure");
212 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
213 *the_PDU_Observable, int fd)
216 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable, this);
217 new_proxy->m_config = 0;
218 new_proxy->m_config_fname = 0;
219 new_proxy->timeout(m_client_idletime);
220 new_proxy->m_target_idletime = m_target_idletime;
221 new_proxy->set_default_target(m_default_target);
222 new_proxy->m_max_clients = m_max_clients;
223 new_proxy->m_log_mask = m_log_mask;
224 new_proxy->set_APDU_log(get_APDU_log());
225 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
226 new_proxy->set_APDU_yazlog(1);
228 new_proxy->set_APDU_yazlog(0);
229 new_proxy->set_proxy_authentication(m_proxy_authentication);
230 sprintf(new_proxy->m_session_str, "%ld:%d ", (long) time(0), m_session_no);
232 yaz_log (LOG_LOG, "%sNew session %s", new_proxy->m_session_str,
233 the_PDU_Observable->getpeername());
237 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
240 Z_OtherInformationUnit *oi;
242 ent.proto = PROTO_Z3950;
243 ent.oclass = CLASS_USERINFO;
244 ent.value = (oid_value) VAL_COOKIE;
245 assert (oid_ent_to_oid (&ent, oid));
247 if (oid_ent_to_oid (&ent, oid) &&
248 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
249 oi->which == Z_OtherInfo_characterInfo)
250 return oi->information.characterInfo;
254 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
257 Z_OtherInformationUnit *oi;
259 ent.proto = PROTO_Z3950;
260 ent.oclass = CLASS_USERINFO;
261 ent.value = (oid_value) VAL_PROXY;
262 if (oid_ent_to_oid (&ent, oid) &&
263 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
264 oi->which == Z_OtherInfo_characterInfo)
265 return oi->information.characterInfo;
269 const char *Yaz_Proxy::load_balance(const char **url)
271 int zurl_in_use[MAX_ZURL_PLEX];
275 for (i = 0; i<MAX_ZURL_PLEX; i++)
277 for (c = m_parent->m_clientPool; c; c = c->m_next)
279 for (i = 0; url[i]; i++)
280 if (!strcmp(url[i], c->get_hostname()))
285 for (i = 0; url[i]; i++)
287 yaz_log(LOG_DEBUG, "%szurl=%s use=%d",
288 m_session_str, url[i], zurl_in_use[i]);
289 if (min > zurl_in_use[i])
292 min = zurl_in_use[i];
298 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
299 const char *proxy_host)
302 Yaz_Proxy *parent = m_parent;
303 Yaz_ProxyClient *c = m_client;
307 const char *url[MAX_ZURL_PLEX];
308 Yaz_ProxyConfig *cfg = check_reconfigure();
312 /* only to be enabled for debugging... */
313 if (!strcmp(proxy_host, "stop"))
316 xfree(m_default_target);
317 m_default_target = xstrdup(proxy_host);
318 proxy_host = m_default_target;
320 int client_idletime = -1;
321 const char *cql2rpn_fname = 0;
322 url[0] = m_default_target;
327 cfg->get_target_info(proxy_host, url, &m_bw_max,
328 &m_pdu_max, &m_max_record_retrieve,
329 &m_target_idletime, &client_idletime,
330 &parent->m_max_clients,
331 &m_keepalive_limit_bw,
332 &m_keepalive_limit_pdu,
336 if (client_idletime != -1)
338 m_client_idletime = client_idletime;
339 timeout(m_client_idletime);
342 m_cql2rpn.set_pqf_file(cql2rpn_fname);
345 yaz_log(LOG_LOG, "%sNo default target", m_session_str);
348 // we don't handle multiplexing for cookie session, so we just
349 // pick the first one in this case (anonymous users will be able
350 // to use any backend)
351 if (cookie && *cookie)
352 m_proxyTarget = (char*) xstrdup(url[0]);
354 m_proxyTarget = (char*) xstrdup(load_balance(url));
356 if (cookie && *cookie)
357 { // search in sessions with a cookie
358 for (c = parent->m_clientPool; c; c = c->m_next)
361 assert (*c->m_prev == c);
362 if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
363 !strcmp(m_proxyTarget, c->get_hostname()))
366 // The following handles "cancel"
367 // If connection is busy (waiting for PDU) and
368 // we have an initRequest we can safely do re-open
369 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
371 yaz_log (LOG_LOG, "%s REOPEN target=%s", m_session_str,
378 c->m_last_resultCount = 0;
379 c->m_sr_transform = 0;
381 c->m_resultSetStartPoint = 0;
382 c->m_target_idletime = m_target_idletime;
383 if (c->client(m_proxyTarget))
390 c->m_seqno = parent->m_seqno;
391 if (c->m_server && c->m_server != this)
392 c->m_server->m_client = 0;
395 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
402 // don't have a client session yet. Search in session w/o cookie
403 for (c = parent->m_clientPool; c; c = c->m_next)
406 assert (*c->m_prev == c);
407 if (c->m_server == 0 && c->m_cookie == 0 &&
409 !strcmp(m_proxyTarget, c->get_hostname()))
412 yaz_log (LOG_LOG, "%sREUSE %s",
413 m_session_str, c->get_hostname());
415 c->m_seqno = parent->m_seqno;
416 assert(c->m_server == 0);
419 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
420 c->set_APDU_yazlog(1);
422 c->set_APDU_yazlog(0);
434 if (apdu->which != Z_APDU_initRequest)
436 yaz_log (LOG_LOG, "%sno init request as first PDU", m_session_str);
439 Z_InitRequest *initRequest = apdu->u.initRequest;
441 if (!initRequest->idAuthentication)
443 if (m_proxy_authentication)
445 initRequest->idAuthentication =
446 (Z_IdAuthentication *)
447 odr_malloc (odr_encode(),
448 sizeof(*initRequest->idAuthentication));
449 initRequest->idAuthentication->which =
450 Z_IdAuthentication_open;
451 initRequest->idAuthentication->u.open =
452 odr_strdup (odr_encode(), m_proxy_authentication);
455 // go through list of clients - and find the lowest/oldest one.
456 Yaz_ProxyClient *c_min = 0;
458 int no_of_clients = 0;
459 if (parent->m_clientPool)
460 yaz_log (LOG_DEBUG, "Existing sessions");
461 for (c = parent->m_clientPool; c; c = c->m_next)
463 yaz_log (LOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
464 c->m_waiting, c->get_hostname(),
465 c->m_cookie ? c->m_cookie : "");
467 if (min_seq < 0 || c->m_seqno < min_seq)
469 min_seq = c->m_seqno;
473 if (no_of_clients >= parent->m_max_clients)
476 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
478 yaz_log (LOG_LOG, "%sMAXCLIENTS Destroy %d",
479 m_session_str, c->m_seqno);
480 if (c->m_server && c->m_server != this)
486 yaz_log (LOG_LOG, "%sMAXCLIENTS Reuse %d %d %s",
488 c->m_seqno, parent->m_seqno, c->get_hostname());
492 c->m_cookie = xstrdup(cookie);
493 c->m_seqno = parent->m_seqno;
494 if (c->m_server && c->m_server != this)
496 c->m_server->m_client = 0;
500 c->m_target_idletime = m_target_idletime;
501 c->timeout(m_target_idletime);
503 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
504 c->set_APDU_yazlog(1);
506 c->set_APDU_yazlog(0);
513 yaz_log (LOG_LOG, "%sNEW %d %s",
514 m_session_str, parent->m_seqno, m_proxyTarget);
515 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), parent);
516 c->m_next = parent->m_clientPool;
518 c->m_next->m_prev = &c->m_next;
519 parent->m_clientPool = c;
520 c->m_prev = &parent->m_clientPool;
526 c->m_cookie = xstrdup(cookie);
528 c->m_seqno = parent->m_seqno;
530 c->m_last_resultCount = 0;
533 c->m_sr_transform = 0;
535 c->m_resultSetStartPoint = 0;
537 if (c->client(m_proxyTarget))
542 c->m_target_idletime = m_target_idletime;
545 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
546 c->set_APDU_yazlog(1);
548 c->set_APDU_yazlog(0);
550 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
554 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
557 for (i = 0; i<num; i++)
560 Z_DefaultDiagFormat *r;
561 Z_DiagRec *p = pp[i];
562 if (p->which != Z_DiagRec_defaultFormat)
564 yaz_log(LOG_LOG, "%sError no diagnostics", m_session_str);
568 r = p->u.defaultFormat;
569 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
570 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
571 yaz_log(LOG_LOG, "%sError unknown diagnostic set", m_session_str);
574 case Z_DefaultDiagFormat_v2Addinfo:
575 yaz_log(LOG_LOG, "%sError %d %s:%s",
577 *r->condition, diagbib1_str(*r->condition),
580 case Z_DefaultDiagFormat_v3Addinfo:
581 yaz_log(LOG_LOG, "%sError %d %s:%s",
583 *r->condition, diagbib1_str(*r->condition),
590 int Yaz_Proxy::convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu)
592 if (!m_stylesheet_xsp || p->num_records <= 0)
593 return 0; /* no XSLT to be done ... */
595 m_stylesheet_offset = 0;
596 m_stylesheet_nprl = p;
597 m_stylesheet_apdu = apdu;
602 void Yaz_Proxy::convert_xsl_delay()
604 Z_NamePlusRecord *npr = m_stylesheet_nprl->records[m_stylesheet_offset];
605 if (npr->which == Z_NamePlusRecord_databaseRecord)
607 Z_External *r = npr->u.databaseRecord;
608 if (r->which == Z_External_octet)
610 xmlDocPtr res, doc = xmlParseMemory(
611 (char*) r->u.octet_aligned->buf,
612 r->u.octet_aligned->len);
614 yaz_log(LOG_LOG, "%sXSLT convert %d",
615 m_session_str, m_stylesheet_offset);
616 res = xsltApplyStylesheet(m_stylesheet_xsp, doc, 0);
621 xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
623 m_stylesheet_nprl->records[m_stylesheet_offset]->
625 z_ext_record(odr_encode(), VAL_TEXT_XML,
626 (char*) out_buf, out_len);
633 m_stylesheet_offset++;
634 if (m_stylesheet_offset == m_stylesheet_nprl->num_records)
636 m_stylesheet_nprl = 0;
637 if (m_stylesheet_xsp)
638 xsltFreeStylesheet(m_stylesheet_xsp);
639 m_stylesheet_xsp = 0;
640 timeout(m_client_idletime);
641 send_PDU_convert(m_stylesheet_apdu);
647 void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
651 yaz_marc_t mt = yaz_marc_create();
652 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
653 for (i = 0; i < p->num_records; i++)
655 Z_NamePlusRecord *npr = p->records[i];
656 if (npr->which == Z_NamePlusRecord_databaseRecord)
658 Z_External *r = npr->u.databaseRecord;
659 if (r->which == Z_External_octet)
663 if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
664 r->u.octet_aligned->len,
667 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
668 WRBUF wrbuf = wrbuf_alloc();
671 size_t inbytesleft = rlen;
672 const char *inp = result;
673 while (cd && inbytesleft)
675 size_t outbytesleft = sizeof(outbuf);
679 r = yaz_iconv (cd, (char**) &inp,
681 &outp, &outbytesleft);
682 if (r == (size_t) (-1))
684 int e = yaz_iconv_error(cd);
685 if (e != YAZ_ICONV_E2BIG)
687 yaz_log(LOG_WARN, "conversion failure");
691 wrbuf_write(wrbuf, outbuf, outp - outbuf);
696 npr->u.databaseRecord = z_ext_record(odr_encode(),
700 wrbuf_free(wrbuf, 1);
705 yaz_marc_destroy(mt);
708 void Yaz_Proxy::logtime()
710 if (m_time_tv.tv_sec)
713 gettimeofday(&tv, 0);
714 long diff = (tv.tv_sec - m_time_tv.tv_sec)*1000000 +
715 (tv.tv_usec - m_time_tv.tv_usec);
717 yaz_log(LOG_LOG, "%sElapsed %ld.%03ld", m_session_str,
718 diff/1000000, (diff/1000)%1000);
720 m_time_tv.tv_sec = 0;
721 m_time_tv.tv_usec = 0;
724 int Yaz_Proxy::send_http_response(int code)
726 ODR o = odr_encode();
727 Z_GDU *gdu = z_get_HTTP_Response(o, code);
728 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
730 hres->version = odr_strdup(o, m_http_version);
731 if (m_http_keepalive)
732 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
736 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
738 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
742 int r = send_GDU(gdu, &len);
744 m_bw_stat.add_bytes(len);
749 int Yaz_Proxy::send_srw_response(Z_SRW_PDU *srw_pdu)
751 ODR o = odr_encode();
752 const char *ctype = "text/xml";
753 Z_GDU *gdu = z_get_HTTP_Response(o, 200);
754 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
756 hres->version = odr_strdup(o, m_http_version);
757 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
758 if (m_http_keepalive)
759 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
763 static Z_SOAP_Handler soap_handlers[2] = {
765 {"http://www.loc.gov/zing/srw/", 0,
766 (Z_SOAP_fun) yaz_srw_codec},
771 Z_SOAP *soap_package = (Z_SOAP*) odr_malloc(o, sizeof(Z_SOAP));
772 soap_package->which = Z_SOAP_generic;
773 soap_package->u.generic =
774 (Z_SOAP_Generic *) odr_malloc(o, sizeof(*soap_package->u.generic));
775 soap_package->u.generic->no = 0;
776 soap_package->u.generic->ns = soap_handlers[0].ns;
777 soap_package->u.generic->p = (void *) srw_pdu;
778 soap_package->ns = m_soap_ns;
779 z_soap_codec_enc_xsl(o, &soap_package,
780 &hres->content_buf, &hres->content_len,
781 soap_handlers, 0, m_s2z_stylesheet);
782 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
784 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
788 int r = send_GDU(gdu, &len);
790 m_bw_stat.add_bytes(len);
795 int Yaz_Proxy::send_to_srw_client_error(int srw_error, const char *add)
797 ODR o = odr_encode();
798 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
799 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
801 srw_res->num_diagnostics = 1;
802 srw_res->diagnostics = (Z_SRW_diagnostic *)
803 odr_malloc(o, sizeof(*srw_res->diagnostics));
804 yaz_mk_std_diagnostic(o, srw_res->diagnostics, srw_error, add);
805 return send_srw_response(srw_pdu);
808 int Yaz_Proxy::z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
809 Z_DefaultDiagFormat *ddf)
811 int bib1_code = *ddf->condition;
812 if (bib1_code == 109)
814 srw_res->num_diagnostics = 1;
815 srw_res->diagnostics = (Z_SRW_diagnostic *)
816 odr_malloc(o, sizeof(*srw_res->diagnostics));
817 yaz_mk_std_diagnostic(o, srw_res->diagnostics,
818 yaz_diag_bib1_to_srw(*ddf->condition),
823 int Yaz_Proxy::send_to_srw_client_ok(int hits, Z_Records *records, int start)
825 ODR o = odr_encode();
826 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
827 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
829 srw_res->numberOfRecords = odr_intdup (o, hits);
830 if (records && records->which == Z_Records_DBOSD)
832 srw_res->num_records =
833 records->u.databaseOrSurDiagnostics->num_records;
835 srw_res->records = (Z_SRW_record *)
836 odr_malloc(o, srw_res->num_records * sizeof(Z_SRW_record));
837 for (i = 0; i < srw_res->num_records; i++)
839 Z_NamePlusRecord *npr = records->u.databaseOrSurDiagnostics->records[i];
840 if (npr->which != Z_NamePlusRecord_databaseRecord)
842 srw_res->records[i].recordSchema = "diagnostic";
843 srw_res->records[i].recordPacking = m_s2z_packing;
844 srw_res->records[i].recordData_buf = "67";
845 srw_res->records[i].recordData_len = 2;
846 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
849 Z_External *r = npr->u.databaseRecord;
850 oident *ent = oid_getentbyoid(r->direct_reference);
851 if (r->which == Z_External_octet && ent->value == VAL_TEXT_XML)
853 srw_res->records[i].recordSchema = m_schema;
854 srw_res->records[i].recordPacking = m_s2z_packing;
855 srw_res->records[i].recordData_buf = (char*)
856 r->u.octet_aligned->buf;
857 srw_res->records[i].recordData_len = r->u.octet_aligned->len;
858 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
862 srw_res->records[i].recordSchema = "diagnostic";
863 srw_res->records[i].recordPacking = m_s2z_packing;
864 srw_res->records[i].recordData_buf = "67";
865 srw_res->records[i].recordData_len = 2;
866 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
870 if (records && records->which == Z_Records_NSD)
873 http_code = z_to_srw_diag(odr_encode(), srw_res,
874 records->u.nonSurrogateDiagnostic);
876 return send_http_response(http_code);
878 return send_srw_response(srw_pdu);
882 int Yaz_Proxy::send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
885 Yaz_ProxyConfig *cfg = check_reconfigure();
889 char *b = cfg->get_explain(odr_encode(), 0 /* target */,
890 m_s2z_database, &len);
893 Z_SRW_PDU *res = yaz_srw_get(odr_encode(), Z_SRW_explain_response);
894 Z_SRW_explainResponse *er = res->u.explain_response;
896 er->record.recordData_buf = b;
897 er->record.recordData_len = len;
898 er->record.recordPacking = m_s2z_packing;
899 er->record.recordSchema = "http://explain.z3950.org/dtd/2.0/";
901 er->diagnostics = diagnostics;
902 er->num_diagnostics = num_diagnostics;
903 return send_srw_response(res);
906 return send_http_response(404);
909 int Yaz_Proxy::send_PDU_convert(Z_APDU *apdu)
913 if (apdu->which == Z_APDU_initResponse)
915 Z_InitResponse *res = apdu->u.initResponse;
916 if (*res->result == 0)
918 send_to_srw_client_error(3, 0);
920 else if (!m_s2z_search_apdu)
922 send_srw_explain_response(0, 0);
926 handle_incoming_Z_PDU(m_s2z_search_apdu);
929 else if (m_s2z_search_apdu && apdu->which == Z_APDU_searchResponse)
931 m_s2z_search_apdu = 0;
932 Z_SearchResponse *res = apdu->u.searchResponse;
933 m_s2z_hit_count = *res->resultCount;
934 if (res->records && res->records->which == Z_Records_NSD)
936 send_to_srw_client_ok(0, res->records, 1);
938 else if (m_s2z_present_apdu && m_s2z_hit_count > 0)
941 Z_PresentRequest *pr = m_s2z_present_apdu->u.presentRequest;
943 if (*pr->resultSetStartPoint <= m_s2z_hit_count)
945 if (*pr->numberOfRecordsRequested+ *pr->resultSetStartPoint
947 *pr->numberOfRecordsRequested =
948 1 + m_s2z_hit_count - *pr->resultSetStartPoint;
950 handle_incoming_Z_PDU(m_s2z_present_apdu);
954 m_s2z_present_apdu = 0;
955 send_to_srw_client_ok(m_s2z_hit_count, res->records, 1);
958 else if (m_s2z_present_apdu && apdu->which == Z_APDU_presentResponse)
961 *m_s2z_present_apdu->u.presentRequest->resultSetStartPoint;
963 m_s2z_present_apdu = 0;
964 Z_PresentResponse *res = apdu->u.presentResponse;
965 send_to_srw_client_ok(m_s2z_hit_count, res->records, start);
971 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
972 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
974 int r = send_Z_PDU(apdu, &len);
976 m_bw_stat.add_bytes(len);
983 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
985 int kill_session = 0;
986 Z_ReferenceId **new_id = get_referenceIdP(apdu);
988 if (new_id && m_referenceId)
989 *new_id = m_referenceId;
991 if (apdu->which == Z_APDU_searchResponse)
993 Z_SearchResponse *sr = apdu->u.searchResponse;
994 Z_Records *p = sr->records;
995 if (p && p->which == Z_Records_NSD)
997 Z_DiagRec dr, *dr_p = &dr;
998 dr.which = Z_DiagRec_defaultFormat;
999 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1001 *sr->searchStatus = 0;
1002 display_diagrecs(&dr_p, 1);
1006 if (p && p->which == Z_Records_DBOSD)
1009 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1010 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1014 if (sr->resultCount)
1016 yaz_log(LOG_LOG, "%s%d hits", m_session_str,
1018 if (*sr->resultCount < 0)
1020 m_invalid_session = 1;
1023 *sr->searchStatus = 0;
1025 create_nonSurrogateDiagnostics(odr_encode(), 2, 0);
1026 *sr->resultCount = 0;
1031 else if (apdu->which == Z_APDU_presentResponse)
1033 Z_PresentResponse *sr = apdu->u.presentResponse;
1034 Z_Records *p = sr->records;
1035 if (p && p->which == Z_Records_NSD)
1037 Z_DiagRec dr, *dr_p = &dr;
1038 dr.which = Z_DiagRec_defaultFormat;
1039 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1040 if (*sr->presentStatus == Z_PresentStatus_success)
1041 *sr->presentStatus = Z_PresentStatus_failure;
1042 display_diagrecs(&dr_p, 1);
1044 if (p && p->which == Z_Records_DBOSD)
1047 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1048 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1052 else if (apdu->which == Z_APDU_initResponse)
1054 if (m_initRequest_options)
1057 (Odr_bitmask *)odr_malloc(odr_encode(),
1058 sizeof(Odr_bitmask));
1059 ODR_MASK_ZERO(nopt);
1062 for (i = 0; i<24; i++)
1063 if (ODR_MASK_GET(m_initRequest_options, i) &&
1064 ODR_MASK_GET(apdu->u.initResponse->options, i))
1065 ODR_MASK_SET(nopt, i);
1066 apdu->u.initResponse->options = nopt;
1068 if (m_initRequest_version)
1070 Z_ProtocolVersion *nopt =
1071 (Odr_bitmask *)odr_malloc(odr_encode(),
1072 sizeof(Odr_bitmask));
1073 ODR_MASK_ZERO(nopt);
1076 for (i = 0; i<8; i++)
1077 if (ODR_MASK_GET(m_initRequest_version, i) &&
1078 ODR_MASK_GET(apdu->u.initResponse->protocolVersion, i))
1079 ODR_MASK_SET(nopt, i);
1080 apdu->u.initResponse->protocolVersion = nopt;
1083 int r = send_PDU_convert(apdu);
1090 m_parent->pre_init();
1095 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
1098 const char *apdu_name_tmp = apdu_name(apdu);
1099 int r = send_Z_PDU(apdu, &len);
1100 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
1101 yaz_log (LOG_LOG, "%sSending %s to %s %d bytes",
1103 apdu_name_tmp, get_hostname(), len);
1104 m_bytes_sent += len;
1108 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
1110 if (apdu->which == Z_APDU_presentRequest)
1112 Z_PresentRequest *pr = apdu->u.presentRequest;
1113 int toget = *pr->numberOfRecordsRequested;
1114 int start = *pr->resultSetStartPoint;
1116 yaz_log(LOG_LOG, "%sPresent %s %d+%d", m_session_str,
1117 pr->resultSetId, start, toget);
1119 if (*m_parent->m_optimize == '0')
1122 if (!m_client->m_last_resultSetId)
1124 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1125 new_apdu->u.presentResponse->records =
1126 create_nonSurrogateDiagnostics(odr_encode(), 30,
1128 send_to_client(new_apdu);
1131 if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
1133 if (start+toget-1 > m_client->m_last_resultCount)
1135 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1136 new_apdu->u.presentResponse->records =
1137 create_nonSurrogateDiagnostics(odr_encode(), 13, 0);
1138 send_to_client(new_apdu);
1141 Z_NamePlusRecordList *npr;
1142 if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
1143 pr->preferredRecordSyntax,
1144 pr->recordComposition))
1146 yaz_log (LOG_LOG, "%sReturned cached records for present request",
1148 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1149 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1151 new_apdu->u.presentResponse->numberOfRecordsReturned
1152 = odr_intdup(odr_encode(), toget);
1154 new_apdu->u.presentResponse->records = (Z_Records*)
1155 odr_malloc(odr_encode(), sizeof(Z_Records));
1156 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
1157 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
1158 new_apdu->u.presentResponse->nextResultSetPosition =
1159 odr_intdup(odr_encode(), start+toget);
1161 send_to_client(new_apdu);
1167 if (apdu->which != Z_APDU_searchRequest)
1169 Z_SearchRequest *sr = apdu->u.searchRequest;
1170 Yaz_Z_Query *this_query = new Yaz_Z_Query;
1171 Yaz_Z_Databases this_databases;
1173 this_databases.set(sr->num_databaseNames, (const char **)
1176 this_query->set_Z_Query(sr->query);
1178 char query_str[120];
1179 this_query->print(query_str, sizeof(query_str)-1);
1180 yaz_log(LOG_LOG, "%sSearch %s", m_session_str, query_str);
1182 if (*m_parent->m_optimize != '0' &&
1183 m_client->m_last_ok && m_client->m_last_query &&
1184 m_client->m_last_query->match(this_query) &&
1185 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
1186 m_client->m_last_databases.match(this_databases))
1189 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
1190 m_client->m_last_resultCount < *sr->largeSetLowerBound)
1192 Z_NamePlusRecordList *npr;
1193 int toget = *sr->mediumSetPresentNumber;
1194 Z_RecordComposition *comp = 0;
1196 if (toget > m_client->m_last_resultCount)
1197 toget = m_client->m_last_resultCount;
1199 if (sr->mediumSetElementSetNames)
1201 comp = (Z_RecordComposition *)
1202 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1203 comp->which = Z_RecordComp_simple;
1204 comp->u.simple = sr->mediumSetElementSetNames;
1207 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1208 sr->preferredRecordSyntax, comp))
1210 yaz_log (LOG_LOG, "%sReturned cached records for medium set",
1212 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1213 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1214 new_apdu->u.searchResponse->resultCount =
1215 &m_client->m_last_resultCount;
1217 new_apdu->u.searchResponse->numberOfRecordsReturned
1218 = odr_intdup(odr_encode(), toget);
1220 new_apdu->u.searchResponse->presentStatus =
1221 odr_intdup(odr_encode(), Z_PresentStatus_success);
1222 new_apdu->u.searchResponse->records = (Z_Records*)
1223 odr_malloc(odr_encode(), sizeof(Z_Records));
1224 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1225 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1226 new_apdu->u.searchResponse->nextResultSetPosition =
1227 odr_intdup(odr_encode(), toget+1);
1228 send_to_client(new_apdu);
1234 // send present request (medium size)
1235 yaz_log (LOG_LOG, "%sOptimizing search for medium set",
1238 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1239 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1240 pr->referenceId = sr->referenceId;
1241 pr->resultSetId = sr->resultSetName;
1242 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1243 *pr->numberOfRecordsRequested = toget;
1244 pr->recordComposition = comp;
1245 m_client->m_sr_transform = 1;
1249 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
1250 m_client->m_last_resultCount <= 0)
1252 // large set. Return pseudo-search response immediately
1253 yaz_log (LOG_LOG, "%sOptimizing search for large set",
1255 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1256 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1257 new_apdu->u.searchResponse->resultCount =
1258 &m_client->m_last_resultCount;
1259 send_to_client(new_apdu);
1264 Z_NamePlusRecordList *npr;
1265 int toget = m_client->m_last_resultCount;
1266 Z_RecordComposition *comp = 0;
1268 // send a present request (small set)
1270 if (sr->smallSetElementSetNames)
1272 comp = (Z_RecordComposition *)
1273 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1274 comp->which = Z_RecordComp_simple;
1275 comp->u.simple = sr->smallSetElementSetNames;
1278 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1279 sr->preferredRecordSyntax, comp))
1281 yaz_log (LOG_LOG, "%sReturned cached records for small set",
1283 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1284 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1285 new_apdu->u.searchResponse->resultCount =
1286 &m_client->m_last_resultCount;
1288 new_apdu->u.searchResponse->numberOfRecordsReturned
1289 = odr_intdup(odr_encode(), toget);
1291 new_apdu->u.searchResponse->presentStatus =
1292 odr_intdup(odr_encode(), Z_PresentStatus_success);
1293 new_apdu->u.searchResponse->records = (Z_Records*)
1294 odr_malloc(odr_encode(), sizeof(Z_Records));
1295 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1296 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1297 new_apdu->u.searchResponse->nextResultSetPosition =
1298 odr_intdup(odr_encode(), toget+1);
1299 send_to_client(new_apdu);
1304 yaz_log (LOG_LOG, "%sOptimizing search for small set",
1306 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1307 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1308 pr->referenceId = sr->referenceId;
1309 pr->resultSetId = sr->resultSetName;
1310 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1311 *pr->numberOfRecordsRequested = toget;
1312 pr->recordComposition = comp;
1313 m_client->m_sr_transform = 1;
1318 else // query doesn't match
1320 delete m_client->m_last_query;
1321 m_client->m_last_query = this_query;
1322 m_client->m_last_ok = 0;
1323 m_client->m_cache.clear();
1324 m_client->m_resultSetStartPoint = 0;
1326 xfree (m_client->m_last_resultSetId);
1327 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
1329 m_client->m_last_databases.set(sr->num_databaseNames,
1330 (const char **) sr->databaseNames);
1336 void Yaz_Proxy::inc_request_no()
1338 char *cp = strchr(m_session_str, ' ');
1341 sprintf(cp+1, "%d ", m_request_no);
1344 void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
1348 m_bytes_recv += len;
1350 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
1351 yaz_log (LOG_DEBUG, "%sReceiving %s from client %d bytes",
1352 m_session_str, gdu_name(apdu), len);
1354 if (m_bw_hold_PDU) // double incoming PDU. shutdown now.
1357 m_bw_stat.add_bytes(len);
1358 m_pdu_stat.add_bytes(1);
1360 gettimeofday(&m_time_tv, 0);
1362 int bw_total = m_bw_stat.get_total();
1363 int pdu_total = m_pdu_stat.get_total();
1368 if (bw_total > m_bw_max)
1370 reduce = (bw_total/m_bw_max);
1375 if (pdu_total > m_pdu_max)
1377 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
1378 reduce = (reduce > nreduce) ? reduce : nreduce;
1383 yaz_log(LOG_LOG, "%sdelay=%d bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
1384 m_session_str, reduce, bw_total, pdu_total,
1385 m_bw_max, m_pdu_max);
1387 m_bw_hold_PDU = apdu; // save PDU and signal "on hold"
1388 timeout(reduce); // call us reduce seconds later
1390 else if (apdu->which == Z_GDU_Z3950)
1391 handle_incoming_Z_PDU(apdu->u.z3950);
1392 else if (apdu->which == Z_GDU_HTTP_Request)
1393 handle_incoming_HTTP(apdu->u.HTTP_Request);
1396 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
1398 if (m_max_record_retrieve)
1400 if (apdu->which == Z_APDU_presentRequest)
1402 Z_PresentRequest *pr = apdu->u.presentRequest;
1403 if (pr->numberOfRecordsRequested &&
1404 *pr->numberOfRecordsRequested > m_max_record_retrieve)
1405 *pr->numberOfRecordsRequested = m_max_record_retrieve;
1410 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
1412 const char *addinfo)
1414 Z_Records *rec = (Z_Records *)
1415 odr_malloc (odr, sizeof(*rec));
1417 odr_malloc (odr, sizeof(*err));
1418 Z_DiagRec *drec = (Z_DiagRec *)
1419 odr_malloc (odr, sizeof(*drec));
1420 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1421 odr_malloc (odr, sizeof(*dr));
1423 rec->which = Z_Records_NSD;
1424 rec->u.nonSurrogateDiagnostic = dr;
1425 dr->diagnosticSetId =
1426 yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
1427 dr->condition = err;
1428 dr->which = Z_DefaultDiagFormat_v2Addinfo;
1429 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
1433 Z_APDU *Yaz_Proxy::handle_query_transformation(Z_APDU *apdu)
1435 if (apdu->which == Z_APDU_searchRequest &&
1436 apdu->u.searchRequest->query &&
1437 apdu->u.searchRequest->query->which == Z_Query_type_104 &&
1438 apdu->u.searchRequest->query->u.type_104->which == Z_External_CQL)
1440 Z_RPNQuery *rpnquery = 0;
1441 Z_SearchRequest *sr = apdu->u.searchRequest;
1444 yaz_log(LOG_LOG, "%sCQL: %s", m_session_str,
1445 sr->query->u.type_104->u.cql);
1447 int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
1448 &rpnquery, odr_encode(),
1451 yaz_log(LOG_LOG, "%sNo CQL to RPN table", m_session_str);
1454 yaz_log(LOG_LOG, "%sCQL Conversion error %d", m_session_str, r);
1455 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1457 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1458 new_apdu->u.searchResponse->records =
1459 create_nonSurrogateDiagnostics(odr_encode(),
1460 yaz_diag_srw_to_bib1(r),
1462 *new_apdu->u.searchResponse->searchStatus = 0;
1464 send_to_client(new_apdu);
1470 sr->query->which = Z_Query_type_1;
1471 sr->query->u.type_1 = rpnquery;
1478 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
1480 if (apdu->which == Z_APDU_searchRequest)
1482 Z_SearchRequest *sr = apdu->u.searchRequest;
1486 Yaz_ProxyConfig *cfg = check_reconfigure();
1488 err = cfg->check_query(odr_encode(), m_default_target,
1489 sr->query, &addinfo);
1492 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1494 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1495 new_apdu->u.searchResponse->records =
1496 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1497 *new_apdu->u.searchResponse->searchStatus = 0;
1499 send_to_client(new_apdu);
1507 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
1510 if (apdu->which == Z_APDU_searchRequest)
1512 Z_SearchRequest *sr = apdu->u.searchRequest;
1515 Yaz_ProxyConfig *cfg = check_reconfigure();
1517 Z_RecordComposition rc_temp, *rc = 0;
1518 if (sr->smallSetElementSetNames)
1520 rc_temp.which = Z_RecordComp_simple;
1521 rc_temp.u.simple = sr->smallSetElementSetNames;
1525 char *stylesheet_name = 0;
1527 err = cfg->check_syntax(odr_encode(),
1529 sr->preferredRecordSyntax, rc,
1530 &addinfo, &stylesheet_name, &m_schema);
1531 if (stylesheet_name)
1533 if (m_stylesheet_xsp)
1534 xsltFreeStylesheet(m_stylesheet_xsp);
1535 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1537 m_stylesheet_offset = 0;
1538 xfree(stylesheet_name);
1542 sr->preferredRecordSyntax =
1543 yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN, VAL_USMARC);
1548 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1550 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1551 new_apdu->u.searchResponse->records =
1552 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1553 *new_apdu->u.searchResponse->searchStatus = 0;
1555 send_to_client(new_apdu);
1560 else if (apdu->which == Z_APDU_presentRequest)
1562 Z_PresentRequest *pr = apdu->u.presentRequest;
1565 Yaz_ProxyConfig *cfg = check_reconfigure();
1567 char *stylesheet_name = 0;
1569 err = cfg->check_syntax(odr_encode(), m_default_target,
1570 pr->preferredRecordSyntax,
1571 pr->recordComposition,
1572 &addinfo, &stylesheet_name, &m_schema);
1573 if (stylesheet_name)
1575 if (m_stylesheet_xsp)
1576 xsltFreeStylesheet(m_stylesheet_xsp);
1577 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1579 m_stylesheet_offset = 0;
1580 xfree(stylesheet_name);
1584 pr->preferredRecordSyntax =
1585 yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN, VAL_USMARC);
1590 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1592 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1593 new_apdu->u.presentResponse->records =
1594 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1595 *new_apdu->u.presentResponse->presentStatus =
1596 Z_PresentStatus_failure;
1598 send_to_client(new_apdu);
1606 Z_ElementSetNames *Yaz_Proxy::mk_esn_from_schema(ODR o, const char *schema)
1610 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1611 odr_malloc(o, sizeof(Z_ElementSetNames));
1612 esn->which = Z_ElementSetNames_generic;
1613 esn->u.generic = odr_strdup(o, schema);
1617 void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
1621 odr_destroy(m_s2z_odr_init);
1624 if (m_s2z_odr_search)
1626 odr_destroy(m_s2z_odr_search);
1627 m_s2z_odr_search = 0;
1630 m_http_keepalive = 0;
1632 if (!strcmp(hreq->version, "1.0"))
1634 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1635 if (v && !strcmp(v, "Keep-Alive"))
1636 m_http_keepalive = 1;
1638 m_http_keepalive = 0;
1639 m_http_version = "1.0";
1643 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1644 if (v && !strcmp(v, "close"))
1645 m_http_keepalive = 0;
1647 m_http_keepalive = 1;
1648 m_http_version = "1.1";
1651 Z_SRW_PDU *srw_pdu = 0;
1652 Z_SOAP *soap_package = 0;
1654 Z_SRW_diagnostic *diagnostic = 0;
1655 int num_diagnostic = 0;
1656 if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1658 || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1659 &charset, &diagnostic, &num_diagnostic) == 0)
1661 m_s2z_odr_init = odr_createmem(ODR_ENCODE);
1662 m_s2z_odr_search = odr_createmem(ODR_ENCODE);
1663 m_soap_ns = odr_strdup(m_s2z_odr_search, soap_package->ns);
1664 m_s2z_init_apdu = 0;
1665 m_s2z_search_apdu = 0;
1666 m_s2z_present_apdu = 0;
1668 m_s2z_stylesheet = 0;
1670 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
1672 Z_SRW_searchRetrieveRequest *srw_req = srw_pdu->u.request;
1674 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1675 // recordXPath unsupported.
1676 if (srw_req->recordXPath)
1678 yaz_add_srw_diagnostic(odr_decode(),
1679 &diagnostic, &num_diagnostic,
1682 // must have a query
1683 if (!srw_req->query.cql)
1685 yaz_add_srw_diagnostic(odr_decode(),
1686 &diagnostic, &num_diagnostic,
1690 if (srw_req->sort_type != Z_SRW_sort_type_none)
1692 yaz_add_srw_diagnostic(odr_decode(),
1693 &diagnostic, &num_diagnostic,
1697 if (srw_req->stylesheet)
1699 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1701 // set packing for response records ..
1702 if (srw_req->recordPacking &&
1703 !strcmp(srw_req->recordPacking, "xml"))
1704 m_s2z_packing = Z_SRW_recordPacking_XML;
1706 m_s2z_packing = Z_SRW_recordPacking_string;
1710 Z_SRW_PDU *srw_pdu =
1711 yaz_srw_get(odr_encode(),
1712 Z_SRW_searchRetrieve_response);
1713 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1715 srw_res->diagnostics = diagnostic;
1716 srw_res->num_diagnostics = num_diagnostic;
1717 send_srw_response(srw_pdu);
1721 // prepare search PDU
1722 m_s2z_search_apdu = zget_APDU(m_s2z_odr_search,
1723 Z_APDU_searchRequest);
1724 Z_SearchRequest *z_searchRequest =
1725 m_s2z_search_apdu->u.searchRequest;
1727 z_searchRequest->num_databaseNames = 1;
1728 z_searchRequest->databaseNames = (char**)
1729 odr_malloc(m_s2z_odr_search, sizeof(char *));
1730 z_searchRequest->databaseNames[0] = odr_strdup(m_s2z_odr_search,
1733 // query transformation
1734 Z_Query *query = (Z_Query *)
1735 odr_malloc(m_s2z_odr_search, sizeof(Z_Query));
1736 z_searchRequest->query = query;
1738 if (srw_req->query_type == Z_SRW_query_type_cql)
1740 Z_External *ext = (Z_External *)
1741 odr_malloc(m_s2z_odr_search, sizeof(*ext));
1742 ext->direct_reference =
1743 odr_getoidbystr(m_s2z_odr_search, "1.2.840.10003.16.2");
1744 ext->indirect_reference = 0;
1745 ext->descriptor = 0;
1746 ext->which = Z_External_CQL;
1747 ext->u.cql = srw_req->query.cql;
1749 query->which = Z_Query_type_104;
1750 query->u.type_104 = ext;
1752 else if (srw_req->query_type == Z_SRW_query_type_pqf)
1754 Z_RPNQuery *RPNquery;
1755 YAZ_PQF_Parser pqf_parser;
1757 pqf_parser = yaz_pqf_create ();
1759 RPNquery = yaz_pqf_parse (pqf_parser, m_s2z_odr_search,
1760 srw_req->query.pqf);
1763 const char *pqf_msg;
1765 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1766 yaz_log(LOG_LOG, "%*s^\n", off+4, "");
1767 yaz_log(LOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
1769 send_to_srw_client_error(10, 0);
1772 query->which = Z_Query_type_1;
1773 query->u.type_1 = RPNquery;
1775 yaz_pqf_destroy (pqf_parser);
1779 send_to_srw_client_error(7, "query");
1784 m_s2z_present_apdu = 0;
1786 if (srw_req->maximumRecords)
1787 max = *srw_req->maximumRecords;
1789 if (srw_req->startRecord)
1790 start = *srw_req->startRecord;
1793 // Some backend, such as Voyager doesn't honor piggyback
1794 // So we use present always (0 &&).
1795 if (0 && start <= 1) // Z39.50 piggyback
1797 *z_searchRequest->smallSetUpperBound = max;
1798 *z_searchRequest->mediumSetPresentNumber = max;
1799 *z_searchRequest->largeSetLowerBound = 2000000000; // 2e9
1801 z_searchRequest->preferredRecordSyntax =
1802 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1804 if (srw_req->recordSchema)
1806 z_searchRequest->smallSetElementSetNames =
1807 z_searchRequest->mediumSetElementSetNames =
1808 mk_esn_from_schema(m_s2z_odr_search,
1809 srw_req->recordSchema);
1812 else // Z39.50 present
1814 m_s2z_present_apdu = zget_APDU(m_s2z_odr_search,
1815 Z_APDU_presentRequest);
1816 Z_PresentRequest *z_presentRequest =
1817 m_s2z_present_apdu->u.presentRequest;
1818 *z_presentRequest->resultSetStartPoint = start;
1819 *z_presentRequest->numberOfRecordsRequested = max;
1820 z_presentRequest->preferredRecordSyntax =
1821 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1823 if (srw_req->recordSchema)
1825 z_presentRequest->recordComposition =
1826 (Z_RecordComposition *)
1827 odr_malloc(m_s2z_odr_search,
1828 sizeof(Z_RecordComposition));
1829 z_presentRequest->recordComposition->which =
1830 Z_RecordComp_simple;
1831 z_presentRequest->recordComposition->u.simple =
1832 mk_esn_from_schema(m_s2z_odr_search,
1833 srw_req->recordSchema);
1839 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1840 Z_APDU_initRequest);
1842 // prevent m_initRequest_apdu memory from being grabbed
1843 // in Yaz_Proxy::handle_incoming_Z_PDU
1844 m_initRequest_apdu = m_s2z_init_apdu;
1845 handle_incoming_Z_PDU(m_s2z_init_apdu);
1850 handle_incoming_Z_PDU(m_s2z_search_apdu);
1854 else if (srw_pdu->which == Z_SRW_explain_request)
1856 Z_SRW_explainRequest *srw_req = srw_pdu->u.explain_request;
1858 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1861 if (srw_req->stylesheet)
1863 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1865 if (srw_req->recordPacking &&
1866 !strcmp(srw_req->recordPacking, "xml"))
1867 m_s2z_packing = Z_SRW_recordPacking_XML;
1869 m_s2z_packing = Z_SRW_recordPacking_string;
1873 send_srw_explain_response(diagnostic, num_diagnostic);
1879 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1880 Z_APDU_initRequest);
1882 // prevent m_initRequest_apdu memory from being grabbed
1883 // in Yaz_Proxy::handle_incoming_Z_PDU
1884 m_initRequest_apdu = m_s2z_init_apdu;
1885 handle_incoming_Z_PDU(m_s2z_init_apdu);
1888 send_srw_explain_response(0, 0);
1891 else if (srw_pdu->which == Z_SRW_scan_request)
1893 m_s2z_database = odr_strdup(m_s2z_odr_init,
1894 srw_pdu->u.scan_request->database);
1896 yaz_add_srw_diagnostic(odr_decode(),
1897 &diagnostic, &num_diagnostic,
1899 Z_SRW_PDU *srw_pdu =
1900 yaz_srw_get(odr_encode(),
1901 Z_SRW_scan_response);
1902 Z_SRW_scanResponse *srw_res = srw_pdu->u.scan_response;
1904 srw_res->diagnostics = diagnostic;
1905 srw_res->num_diagnostics = num_diagnostic;
1906 send_srw_response(srw_pdu);
1913 send_to_srw_client_error(4, 0);
1917 Z_GDU *p = z_get_HTTP_Response(odr_encode(), 400);
1922 void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
1924 Z_ReferenceId **refid = get_referenceIdP(apdu);
1925 nmem_reset(m_referenceId_mem);
1926 if (refid && *refid)
1928 m_referenceId = (Z_ReferenceId *)
1929 nmem_malloc(m_referenceId_mem, sizeof(*m_referenceId));
1930 m_referenceId->len = m_referenceId->size = (*refid)->len;
1931 m_referenceId->buf = (unsigned char *)
1932 nmem_malloc(m_referenceId_mem, (*refid)->len);
1933 memcpy(m_referenceId->buf, (*refid)->buf, (*refid)->len);
1938 if (!m_client && m_invalid_session)
1940 m_apdu_invalid_session = apdu;
1941 m_mem_invalid_session = odr_extract_mem(odr_decode());
1942 apdu = m_initRequest_apdu;
1945 // Determine our client.
1946 Z_OtherInformation **oi;
1947 get_otherInfoAPDU(apdu, &oi);
1948 m_client = get_client(apdu, get_cookie(oi), get_proxy(oi));
1954 m_client->m_server = this;
1956 if (apdu->which == Z_APDU_initRequest)
1958 if (apdu->u.initRequest->implementationId)
1959 yaz_log(LOG_LOG, "%simplementationId: %s",
1960 m_session_str, apdu->u.initRequest->implementationId);
1961 if (apdu->u.initRequest->implementationName)
1962 yaz_log(LOG_LOG, "%simplementationName: %s",
1963 m_session_str, apdu->u.initRequest->implementationName);
1964 if (apdu->u.initRequest->implementationVersion)
1965 yaz_log(LOG_LOG, "%simplementationVersion: %s",
1966 m_session_str, apdu->u.initRequest->implementationVersion);
1967 if (m_initRequest_apdu == 0)
1969 if (m_initRequest_mem)
1970 nmem_destroy(m_initRequest_mem);
1971 m_initRequest_apdu = apdu;
1972 m_initRequest_mem = odr_extract_mem(odr_decode());
1974 // save init options for the response..
1975 m_initRequest_options = apdu->u.initRequest->options;
1977 apdu->u.initRequest->options =
1978 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
1979 sizeof(Odr_bitmask));
1980 ODR_MASK_ZERO(apdu->u.initRequest->options);
1982 for (i = 0; i<= 24; i++)
1983 ODR_MASK_SET(apdu->u.initRequest->options, i);
1984 ODR_MASK_CLEAR(apdu->u.initRequest->options,
1985 Z_Options_negotiationModel);
1988 m_initRequest_version = apdu->u.initRequest->protocolVersion;
1989 apdu->u.initRequest->protocolVersion =
1990 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
1991 sizeof(Odr_bitmask));
1992 ODR_MASK_ZERO(apdu->u.initRequest->protocolVersion);
1994 for (i = 0; i<= 8; i++)
1995 ODR_MASK_SET(apdu->u.initRequest->protocolVersion, i);
1997 if (m_client->m_init_flag)
1999 if (handle_init_response_for_invalid_session(apdu))
2001 if (m_client->m_initResponse)
2003 Z_APDU *apdu2 = m_client->m_initResponse;
2004 apdu2->u.initResponse->otherInfo = 0;
2005 if (m_client->m_cookie && *m_client->m_cookie)
2006 set_otherInformationString(apdu2, VAL_COOKIE, 1,
2007 m_client->m_cookie);
2008 apdu2->u.initResponse->referenceId =
2009 apdu->u.initRequest->referenceId;
2010 apdu2->u.initResponse->options = m_client->m_initResponse_options;
2011 apdu2->u.initResponse->protocolVersion =
2012 m_client->m_initResponse_version;
2014 send_to_client(apdu2);
2018 m_client->m_init_flag = 1;
2020 handle_max_record_retrieve(apdu);
2023 apdu = handle_syntax_validation(apdu);
2026 apdu = handle_query_transformation(apdu);
2029 apdu = handle_query_validation(apdu);
2032 apdu = result_set_optimize(apdu);
2035 m_client->timeout(m_target_idletime); // mark it active even
2036 // though we didn't use it
2040 // delete other info part from PDU before sending to target
2041 get_otherInfoAPDU(apdu, &oi);
2045 if (apdu->which == Z_APDU_presentRequest &&
2046 m_client->m_resultSetStartPoint == 0)
2048 Z_PresentRequest *pr = apdu->u.presentRequest;
2049 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
2050 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
2052 m_client->m_resultSetStartPoint = 0;
2054 if (m_client->send_to_target(apdu) < 0)
2061 m_client->m_waiting = 1;
2064 void Yaz_Proxy::connectNotify()
2068 void Yaz_Proxy::shutdown()
2070 m_invalid_session = 0;
2071 // only keep if keep_alive flag is set...
2073 m_client->m_pdu_recv < m_keepalive_limit_pdu &&
2074 m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
2075 m_client->m_waiting == 0)
2077 yaz_log(LOG_LOG, "%sShutdown (client to proxy) keepalive %s",
2079 m_client->get_hostname());
2080 yaz_log(LOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
2081 m_session_str, m_client->m_pdu_recv,
2082 m_client->m_bytes_sent + m_client->m_bytes_recv,
2083 m_keepalive_limit_bw, m_keepalive_limit_pdu);
2084 assert (m_client->m_waiting != 2);
2085 // Tell client (if any) that no server connection is there..
2086 m_client->m_server = 0;
2087 m_invalid_session = 0;
2091 yaz_log (LOG_LOG, "%sShutdown (client to proxy) close %s",
2093 m_client->get_hostname());
2094 assert (m_client->m_waiting != 2);
2099 yaz_log (LOG_LOG, "%sshutdown (client to proxy) bad state",
2105 yaz_log (LOG_LOG, "%sShutdown (client to proxy)",
2109 m_parent->pre_init();
2113 const char *Yaz_ProxyClient::get_session_str()
2117 return m_server->get_session_str();
2120 void Yaz_ProxyClient::shutdown()
2122 yaz_log (LOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
2128 void Yaz_Proxy::failNotify()
2131 yaz_log (LOG_LOG, "%sConnection closed by client",
2136 void Yaz_ProxyClient::failNotify()
2139 m_server->inc_request_no();
2140 yaz_log (LOG_LOG, "%sConnection closed by target %s",
2141 get_session_str(), get_hostname());
2145 void Yaz_ProxyClient::connectNotify()
2147 const char *s = get_session_str();
2148 const char *h = get_hostname();
2149 yaz_log (LOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
2151 timeout(m_target_idletime);
2156 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
2157 *the_PDU_Observable, int fd)
2159 return new Yaz_ProxyClient(the_PDU_Observable, 0);
2162 Yaz_ProxyClient::~Yaz_ProxyClient()
2167 m_next->m_prev = m_prev;
2168 m_waiting = 2; // for debugging purposes only.
2169 odr_destroy(m_init_odr);
2170 delete m_last_query;
2171 xfree (m_last_resultSetId);
2175 void Yaz_ProxyClient::pre_init_client()
2177 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
2178 Z_InitRequest *req = apdu->u.initRequest;
2181 for (i = 0; i<= 24; i++)
2182 ODR_MASK_SET(req->options, i);
2183 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2184 Z_Options_negotiationModel);
2185 for (i = 0; i<= 10; i++)
2186 ODR_MASK_SET(req->protocolVersion, i);
2188 if (send_to_target(apdu) < 0)
2199 void Yaz_Proxy::pre_init()
2202 const char *name = 0;
2203 const char *zurl_in_use[MAX_ZURL_PLEX];
2204 int limit_bw, limit_pdu, limit_req;
2205 int target_idletime, client_idletime;
2207 int keepalive_limit_bw, keepalive_limit_pdu;
2209 const char *cql2rpn = 0;
2211 Yaz_ProxyConfig *cfg = check_reconfigure();
2215 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
2220 for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
2221 &limit_bw, &limit_pdu, &limit_req,
2222 &target_idletime, &client_idletime,
2224 &keepalive_limit_bw,
2225 &keepalive_limit_pdu,
2232 for (j = 0; zurl_in_use[j]; j++)
2238 for (c = m_clientPool; c; c = c->m_next)
2240 if (!strcmp(zurl_in_use[j], c->get_hostname()))
2242 if (c->m_cookie == 0)
2244 if (c->m_server == 0)
2253 yaz_log(LOG_LOG, "%spre-init %s %s use=%d other=%d spare=%d "
2254 "preinit=%d",m_session_str,
2255 name, zurl_in_use[j], in_use, other, spare, pre_init);
2256 if (spare < pre_init)
2258 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
2259 c->m_next = m_clientPool;
2261 c->m_next->m_prev = &c->m_next;
2263 c->m_prev = &m_clientPool;
2265 if (m_log_mask & PROXY_LOG_APDU_SERVER)
2266 c->set_APDU_yazlog(1);
2268 c->set_APDU_yazlog(0);
2270 if (c->client(zurl_in_use[j]))
2278 c->m_target_idletime = target_idletime;
2279 c->m_seqno = m_seqno++;
2286 void Yaz_Proxy::timeoutNotify()
2292 timeout(m_client_idletime);
2293 Z_GDU *apdu = m_bw_hold_PDU;
2296 if (apdu->which == Z_GDU_Z3950)
2297 handle_incoming_Z_PDU(apdu->u.z3950);
2298 else if (apdu->which == Z_GDU_HTTP_Request)
2299 handle_incoming_HTTP(apdu->u.HTTP_Request);
2301 else if (m_stylesheet_nprl)
2302 convert_xsl_delay();
2307 yaz_log (LOG_LOG, "%sTimeout (client to proxy)", m_session_str);
2318 void Yaz_Proxy::markInvalid()
2321 m_invalid_session = 1;
2324 void Yaz_ProxyClient::timeoutNotify()
2327 m_server->inc_request_no();
2329 yaz_log (LOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
2333 if (m_server && m_init_flag)
2335 // target timed out in a session that was properly initialized
2336 // server object stay alive but we mark it as invalid so it
2337 // gets initialized again
2338 m_server->markInvalid();
2344 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
2345 Yaz_Proxy *parent) :
2346 Yaz_Z_Assoc (the_PDU_Observable)
2353 m_last_resultSetId = 0;
2354 m_last_resultCount = 0;
2358 m_init_odr = odr_createmem (ODR_DECODE);
2360 m_initResponse_options = 0;
2361 m_initResponse_version = 0;
2362 m_resultSetStartPoint = 0;
2363 m_bytes_sent = m_bytes_recv = 0;
2367 m_target_idletime = 600;
2371 const char *Yaz_Proxy::option(const char *name, const char *value)
2373 if (!strcmp (name, "optimize")) {
2376 m_optimize = xstrdup (value);
2383 void Yaz_ProxyClient::recv_HTTP_response(Z_HTTP_Response *apdu, int len)
2388 void Yaz_ProxyClient::recv_GDU(Z_GDU *apdu, int len)
2390 if (apdu->which == Z_GDU_Z3950)
2391 recv_Z_PDU(apdu->u.z3950, len);
2392 else if (apdu->which == Z_GDU_HTTP_Response)
2393 recv_HTTP_response(apdu->u.HTTP_Response, len);
2398 int Yaz_Proxy::handle_init_response_for_invalid_session(Z_APDU *apdu)
2400 if (!m_invalid_session)
2402 m_invalid_session = 0;
2403 handle_incoming_Z_PDU(m_apdu_invalid_session);
2404 assert (m_mem_invalid_session);
2405 nmem_destroy(m_mem_invalid_session);
2406 m_mem_invalid_session = 0;
2410 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
2412 m_bytes_recv += len;
2416 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
2417 yaz_log (LOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
2418 apdu_name(apdu), get_hostname(), len);
2419 if (apdu->which == Z_APDU_initResponse)
2421 if (!m_server) // if this is a pre init session , check for more
2423 NMEM nmem = odr_extract_mem (odr_decode());
2424 odr_reset (m_init_odr);
2425 nmem_transfer (m_init_odr->mem, nmem);
2426 m_initResponse = apdu;
2427 m_initResponse_options = apdu->u.initResponse->options;
2428 m_initResponse_version = apdu->u.initResponse->protocolVersion;
2430 Z_InitResponse *ir = apdu->u.initResponse;
2431 char *im0 = ir->implementationName;
2434 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
2441 strcat(im1, "(YAZ Proxy)");
2442 ir->implementationName = im1;
2444 nmem_destroy (nmem);
2446 if (m_server && m_server->handle_init_response_for_invalid_session(apdu))
2449 if (apdu->which == Z_APDU_searchResponse)
2451 Z_SearchResponse *sr = apdu->u.searchResponse;
2452 m_last_resultCount = *sr->resultCount;
2453 int status = *sr->searchStatus;
2454 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
2458 if (sr->records && sr->records->which == Z_Records_DBOSD)
2460 m_cache.add(odr_decode(),
2461 sr->records->u.databaseOrSurDiagnostics, 1,
2466 if (apdu->which == Z_APDU_presentResponse)
2468 Z_PresentResponse *pr = apdu->u.presentResponse;
2472 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2473 Z_SearchResponse *sr = new_apdu->u.searchResponse;
2474 sr->referenceId = pr->referenceId;
2475 *sr->resultCount = m_last_resultCount;
2476 sr->records = pr->records;
2477 sr->nextResultSetPosition = pr->nextResultSetPosition;
2478 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
2482 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
2484 m_cache.add(odr_decode(),
2485 pr->records->u.databaseOrSurDiagnostics,
2486 m_resultSetStartPoint, -1);
2487 m_resultSetStartPoint = 0;
2491 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
2494 m_server->send_to_client(apdu);
2496 if (apdu->which == Z_APDU_close)
2502 int Yaz_Proxy::server(const char *addr)
2504 int r = Yaz_Z_Assoc::server(addr);
2507 yaz_log(LOG_LOG, "%sStarted proxy " VERSION " on %s", m_session_str, addr);