2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.100 2004-02-12 20:40:22 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;
900 er->diagnostics = diagnostics;
901 er->num_diagnostics = num_diagnostics;
902 return send_srw_response(res);
905 return send_http_response(404);
908 int Yaz_Proxy::send_PDU_convert(Z_APDU *apdu)
912 if (apdu->which == Z_APDU_initResponse)
914 Z_InitResponse *res = apdu->u.initResponse;
915 if (*res->result == 0)
917 send_to_srw_client_error(3, 0);
919 else if (!m_s2z_search_apdu)
921 send_srw_explain_response(0, 0);
925 handle_incoming_Z_PDU(m_s2z_search_apdu);
928 else if (m_s2z_search_apdu && apdu->which == Z_APDU_searchResponse)
930 m_s2z_search_apdu = 0;
931 Z_SearchResponse *res = apdu->u.searchResponse;
932 m_s2z_hit_count = *res->resultCount;
933 if (res->records && res->records->which == Z_Records_NSD)
935 send_to_srw_client_ok(0, res->records, 1);
937 else if (m_s2z_present_apdu && m_s2z_hit_count > 0)
940 Z_PresentRequest *pr = m_s2z_present_apdu->u.presentRequest;
942 if (*pr->resultSetStartPoint <= m_s2z_hit_count)
944 if (*pr->numberOfRecordsRequested+ *pr->resultSetStartPoint
946 *pr->numberOfRecordsRequested =
947 1 + m_s2z_hit_count - *pr->resultSetStartPoint;
949 handle_incoming_Z_PDU(m_s2z_present_apdu);
953 m_s2z_present_apdu = 0;
954 send_to_srw_client_ok(m_s2z_hit_count, res->records, 1);
957 else if (m_s2z_present_apdu && apdu->which == Z_APDU_presentResponse)
960 *m_s2z_present_apdu->u.presentRequest->resultSetStartPoint;
962 m_s2z_present_apdu = 0;
963 Z_PresentResponse *res = apdu->u.presentResponse;
964 send_to_srw_client_ok(m_s2z_hit_count, res->records, start);
970 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
971 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
973 int r = send_Z_PDU(apdu, &len);
975 m_bw_stat.add_bytes(len);
982 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
984 int kill_session = 0;
985 Z_ReferenceId **new_id = get_referenceIdP(apdu);
987 if (new_id && m_referenceId)
988 *new_id = m_referenceId;
990 if (apdu->which == Z_APDU_searchResponse)
992 Z_SearchResponse *sr = apdu->u.searchResponse;
993 Z_Records *p = sr->records;
994 if (p && p->which == Z_Records_NSD)
996 Z_DiagRec dr, *dr_p = &dr;
997 dr.which = Z_DiagRec_defaultFormat;
998 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1000 *sr->searchStatus = 0;
1001 display_diagrecs(&dr_p, 1);
1005 if (p && p->which == Z_Records_DBOSD)
1008 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1009 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1013 if (sr->resultCount)
1015 yaz_log(LOG_LOG, "%s%d hits", m_session_str,
1017 if (*sr->resultCount < 0)
1019 m_invalid_session = 1;
1022 *sr->searchStatus = 0;
1024 create_nonSurrogateDiagnostics(odr_encode(), 2, 0);
1025 *sr->resultCount = 0;
1030 else if (apdu->which == Z_APDU_presentResponse)
1032 Z_PresentResponse *sr = apdu->u.presentResponse;
1033 Z_Records *p = sr->records;
1034 if (p && p->which == Z_Records_NSD)
1036 Z_DiagRec dr, *dr_p = &dr;
1037 dr.which = Z_DiagRec_defaultFormat;
1038 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1039 if (*sr->presentStatus == Z_PresentStatus_success)
1040 *sr->presentStatus = Z_PresentStatus_failure;
1041 display_diagrecs(&dr_p, 1);
1043 if (p && p->which == Z_Records_DBOSD)
1046 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1047 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1051 else if (apdu->which == Z_APDU_initResponse)
1053 if (m_initRequest_options)
1056 (Odr_bitmask *)odr_malloc(odr_encode(),
1057 sizeof(Odr_bitmask));
1058 ODR_MASK_ZERO(nopt);
1061 for (i = 0; i<24; i++)
1062 if (ODR_MASK_GET(m_initRequest_options, i) &&
1063 ODR_MASK_GET(apdu->u.initResponse->options, i))
1064 ODR_MASK_SET(nopt, i);
1065 apdu->u.initResponse->options = nopt;
1067 if (m_initRequest_version)
1069 Z_ProtocolVersion *nopt =
1070 (Odr_bitmask *)odr_malloc(odr_encode(),
1071 sizeof(Odr_bitmask));
1072 ODR_MASK_ZERO(nopt);
1075 for (i = 0; i<8; i++)
1076 if (ODR_MASK_GET(m_initRequest_version, i) &&
1077 ODR_MASK_GET(apdu->u.initResponse->protocolVersion, i))
1078 ODR_MASK_SET(nopt, i);
1079 apdu->u.initResponse->protocolVersion = nopt;
1082 int r = send_PDU_convert(apdu);
1089 m_parent->pre_init();
1094 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
1097 const char *apdu_name_tmp = apdu_name(apdu);
1098 int r = send_Z_PDU(apdu, &len);
1099 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
1100 yaz_log (LOG_LOG, "%sSending %s to %s %d bytes",
1102 apdu_name_tmp, get_hostname(), len);
1103 m_bytes_sent += len;
1107 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
1109 if (apdu->which == Z_APDU_presentRequest)
1111 Z_PresentRequest *pr = apdu->u.presentRequest;
1112 int toget = *pr->numberOfRecordsRequested;
1113 int start = *pr->resultSetStartPoint;
1115 yaz_log(LOG_LOG, "%sPresent %s %d+%d", m_session_str,
1116 pr->resultSetId, start, toget);
1118 if (*m_parent->m_optimize == '0')
1121 if (!m_client->m_last_resultSetId)
1123 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1124 new_apdu->u.presentResponse->records =
1125 create_nonSurrogateDiagnostics(odr_encode(), 30,
1127 send_to_client(new_apdu);
1130 if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
1132 if (start+toget-1 > m_client->m_last_resultCount)
1134 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1135 new_apdu->u.presentResponse->records =
1136 create_nonSurrogateDiagnostics(odr_encode(), 13, 0);
1137 send_to_client(new_apdu);
1140 Z_NamePlusRecordList *npr;
1141 if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
1142 pr->preferredRecordSyntax,
1143 pr->recordComposition))
1145 yaz_log (LOG_LOG, "%sReturned cached records for present request",
1147 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1148 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1150 new_apdu->u.presentResponse->numberOfRecordsReturned
1151 = odr_intdup(odr_encode(), toget);
1153 new_apdu->u.presentResponse->records = (Z_Records*)
1154 odr_malloc(odr_encode(), sizeof(Z_Records));
1155 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
1156 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
1157 new_apdu->u.presentResponse->nextResultSetPosition =
1158 odr_intdup(odr_encode(), start+toget);
1160 send_to_client(new_apdu);
1166 if (apdu->which != Z_APDU_searchRequest)
1168 Z_SearchRequest *sr = apdu->u.searchRequest;
1169 Yaz_Z_Query *this_query = new Yaz_Z_Query;
1170 Yaz_Z_Databases this_databases;
1172 this_databases.set(sr->num_databaseNames, (const char **)
1175 this_query->set_Z_Query(sr->query);
1177 char query_str[120];
1178 this_query->print(query_str, sizeof(query_str)-1);
1179 yaz_log(LOG_LOG, "%sSearch %s", m_session_str, query_str);
1181 if (*m_parent->m_optimize != '0' &&
1182 m_client->m_last_ok && m_client->m_last_query &&
1183 m_client->m_last_query->match(this_query) &&
1184 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
1185 m_client->m_last_databases.match(this_databases))
1188 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
1189 m_client->m_last_resultCount < *sr->largeSetLowerBound)
1191 Z_NamePlusRecordList *npr;
1192 int toget = *sr->mediumSetPresentNumber;
1193 Z_RecordComposition *comp = 0;
1195 if (toget > m_client->m_last_resultCount)
1196 toget = m_client->m_last_resultCount;
1198 if (sr->mediumSetElementSetNames)
1200 comp = (Z_RecordComposition *)
1201 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1202 comp->which = Z_RecordComp_simple;
1203 comp->u.simple = sr->mediumSetElementSetNames;
1206 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1207 sr->preferredRecordSyntax, comp))
1209 yaz_log (LOG_LOG, "%sReturned cached records for medium set",
1211 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1212 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1213 new_apdu->u.searchResponse->resultCount =
1214 &m_client->m_last_resultCount;
1216 new_apdu->u.searchResponse->numberOfRecordsReturned
1217 = odr_intdup(odr_encode(), toget);
1219 new_apdu->u.searchResponse->presentStatus =
1220 odr_intdup(odr_encode(), Z_PresentStatus_success);
1221 new_apdu->u.searchResponse->records = (Z_Records*)
1222 odr_malloc(odr_encode(), sizeof(Z_Records));
1223 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1224 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1225 new_apdu->u.searchResponse->nextResultSetPosition =
1226 odr_intdup(odr_encode(), toget+1);
1227 send_to_client(new_apdu);
1233 // send present request (medium size)
1234 yaz_log (LOG_LOG, "%sOptimizing search for medium set",
1237 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1238 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1239 pr->referenceId = sr->referenceId;
1240 pr->resultSetId = sr->resultSetName;
1241 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1242 *pr->numberOfRecordsRequested = toget;
1243 pr->recordComposition = comp;
1244 m_client->m_sr_transform = 1;
1248 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
1249 m_client->m_last_resultCount <= 0)
1251 // large set. Return pseudo-search response immediately
1252 yaz_log (LOG_LOG, "%sOptimizing search for large set",
1254 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1255 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1256 new_apdu->u.searchResponse->resultCount =
1257 &m_client->m_last_resultCount;
1258 send_to_client(new_apdu);
1263 Z_NamePlusRecordList *npr;
1264 int toget = m_client->m_last_resultCount;
1265 Z_RecordComposition *comp = 0;
1267 // send a present request (small set)
1269 if (sr->smallSetElementSetNames)
1271 comp = (Z_RecordComposition *)
1272 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1273 comp->which = Z_RecordComp_simple;
1274 comp->u.simple = sr->smallSetElementSetNames;
1277 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1278 sr->preferredRecordSyntax, comp))
1280 yaz_log (LOG_LOG, "%sReturned cached records for small set",
1282 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1283 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1284 new_apdu->u.searchResponse->resultCount =
1285 &m_client->m_last_resultCount;
1287 new_apdu->u.searchResponse->numberOfRecordsReturned
1288 = odr_intdup(odr_encode(), toget);
1290 new_apdu->u.searchResponse->presentStatus =
1291 odr_intdup(odr_encode(), Z_PresentStatus_success);
1292 new_apdu->u.searchResponse->records = (Z_Records*)
1293 odr_malloc(odr_encode(), sizeof(Z_Records));
1294 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1295 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1296 new_apdu->u.searchResponse->nextResultSetPosition =
1297 odr_intdup(odr_encode(), toget+1);
1298 send_to_client(new_apdu);
1303 yaz_log (LOG_LOG, "%sOptimizing search for small set",
1305 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1306 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1307 pr->referenceId = sr->referenceId;
1308 pr->resultSetId = sr->resultSetName;
1309 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1310 *pr->numberOfRecordsRequested = toget;
1311 pr->recordComposition = comp;
1312 m_client->m_sr_transform = 1;
1317 else // query doesn't match
1319 delete m_client->m_last_query;
1320 m_client->m_last_query = this_query;
1321 m_client->m_last_ok = 0;
1322 m_client->m_cache.clear();
1323 m_client->m_resultSetStartPoint = 0;
1325 xfree (m_client->m_last_resultSetId);
1326 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
1328 m_client->m_last_databases.set(sr->num_databaseNames,
1329 (const char **) sr->databaseNames);
1335 void Yaz_Proxy::inc_request_no()
1337 char *cp = strchr(m_session_str, ' ');
1340 sprintf(cp+1, "%d ", m_request_no);
1343 void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
1347 m_bytes_recv += len;
1349 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
1350 yaz_log (LOG_DEBUG, "%sReceiving %s from client %d bytes",
1351 m_session_str, gdu_name(apdu), len);
1353 if (m_bw_hold_PDU) // double incoming PDU. shutdown now.
1356 m_bw_stat.add_bytes(len);
1357 m_pdu_stat.add_bytes(1);
1359 gettimeofday(&m_time_tv, 0);
1361 int bw_total = m_bw_stat.get_total();
1362 int pdu_total = m_pdu_stat.get_total();
1367 if (bw_total > m_bw_max)
1369 reduce = (bw_total/m_bw_max);
1374 if (pdu_total > m_pdu_max)
1376 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
1377 reduce = (reduce > nreduce) ? reduce : nreduce;
1382 yaz_log(LOG_LOG, "%sdelay=%d bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
1383 m_session_str, reduce, bw_total, pdu_total,
1384 m_bw_max, m_pdu_max);
1386 m_bw_hold_PDU = apdu; // save PDU and signal "on hold"
1387 timeout(reduce); // call us reduce seconds later
1389 else if (apdu->which == Z_GDU_Z3950)
1390 handle_incoming_Z_PDU(apdu->u.z3950);
1391 else if (apdu->which == Z_GDU_HTTP_Request)
1392 handle_incoming_HTTP(apdu->u.HTTP_Request);
1395 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
1397 if (m_max_record_retrieve)
1399 if (apdu->which == Z_APDU_presentRequest)
1401 Z_PresentRequest *pr = apdu->u.presentRequest;
1402 if (pr->numberOfRecordsRequested &&
1403 *pr->numberOfRecordsRequested > m_max_record_retrieve)
1404 *pr->numberOfRecordsRequested = m_max_record_retrieve;
1409 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
1411 const char *addinfo)
1413 Z_Records *rec = (Z_Records *)
1414 odr_malloc (odr, sizeof(*rec));
1416 odr_malloc (odr, sizeof(*err));
1417 Z_DiagRec *drec = (Z_DiagRec *)
1418 odr_malloc (odr, sizeof(*drec));
1419 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1420 odr_malloc (odr, sizeof(*dr));
1422 rec->which = Z_Records_NSD;
1423 rec->u.nonSurrogateDiagnostic = dr;
1424 dr->diagnosticSetId =
1425 yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
1426 dr->condition = err;
1427 dr->which = Z_DefaultDiagFormat_v2Addinfo;
1428 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
1432 Z_APDU *Yaz_Proxy::handle_query_transformation(Z_APDU *apdu)
1434 if (apdu->which == Z_APDU_searchRequest &&
1435 apdu->u.searchRequest->query &&
1436 apdu->u.searchRequest->query->which == Z_Query_type_104 &&
1437 apdu->u.searchRequest->query->u.type_104->which == Z_External_CQL)
1439 Z_RPNQuery *rpnquery = 0;
1440 Z_SearchRequest *sr = apdu->u.searchRequest;
1443 yaz_log(LOG_LOG, "%sCQL: %s", m_session_str,
1444 sr->query->u.type_104->u.cql);
1446 int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
1447 &rpnquery, odr_encode(),
1450 yaz_log(LOG_LOG, "%sNo CQL to RPN table", m_session_str);
1453 yaz_log(LOG_LOG, "%sCQL Conversion error %d", m_session_str, r);
1454 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1456 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1457 new_apdu->u.searchResponse->records =
1458 create_nonSurrogateDiagnostics(odr_encode(),
1459 yaz_diag_srw_to_bib1(r),
1461 *new_apdu->u.searchResponse->searchStatus = 0;
1463 send_to_client(new_apdu);
1469 sr->query->which = Z_Query_type_1;
1470 sr->query->u.type_1 = rpnquery;
1477 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
1479 if (apdu->which == Z_APDU_searchRequest)
1481 Z_SearchRequest *sr = apdu->u.searchRequest;
1485 Yaz_ProxyConfig *cfg = check_reconfigure();
1487 err = cfg->check_query(odr_encode(), m_default_target,
1488 sr->query, &addinfo);
1491 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1493 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1494 new_apdu->u.searchResponse->records =
1495 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1496 *new_apdu->u.searchResponse->searchStatus = 0;
1498 send_to_client(new_apdu);
1506 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
1509 if (apdu->which == Z_APDU_searchRequest)
1511 Z_SearchRequest *sr = apdu->u.searchRequest;
1514 Yaz_ProxyConfig *cfg = check_reconfigure();
1516 Z_RecordComposition rc_temp, *rc = 0;
1517 if (sr->smallSetElementSetNames)
1519 rc_temp.which = Z_RecordComp_simple;
1520 rc_temp.u.simple = sr->smallSetElementSetNames;
1524 char *stylesheet_name = 0;
1526 err = cfg->check_syntax(odr_encode(),
1528 sr->preferredRecordSyntax, rc,
1529 &addinfo, &stylesheet_name, &m_schema);
1530 if (stylesheet_name)
1532 if (m_stylesheet_xsp)
1533 xsltFreeStylesheet(m_stylesheet_xsp);
1534 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1536 m_stylesheet_offset = 0;
1537 xfree(stylesheet_name);
1541 sr->preferredRecordSyntax =
1542 yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN, VAL_USMARC);
1547 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1549 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1550 new_apdu->u.searchResponse->records =
1551 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1552 *new_apdu->u.searchResponse->searchStatus = 0;
1554 send_to_client(new_apdu);
1559 else if (apdu->which == Z_APDU_presentRequest)
1561 Z_PresentRequest *pr = apdu->u.presentRequest;
1564 Yaz_ProxyConfig *cfg = check_reconfigure();
1566 char *stylesheet_name = 0;
1568 err = cfg->check_syntax(odr_encode(), m_default_target,
1569 pr->preferredRecordSyntax,
1570 pr->recordComposition,
1571 &addinfo, &stylesheet_name, &m_schema);
1572 if (stylesheet_name)
1574 if (m_stylesheet_xsp)
1575 xsltFreeStylesheet(m_stylesheet_xsp);
1576 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1578 m_stylesheet_offset = 0;
1579 xfree(stylesheet_name);
1583 pr->preferredRecordSyntax =
1584 yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN, VAL_USMARC);
1589 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1591 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1592 new_apdu->u.presentResponse->records =
1593 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1594 *new_apdu->u.presentResponse->presentStatus =
1595 Z_PresentStatus_failure;
1597 send_to_client(new_apdu);
1605 Z_ElementSetNames *Yaz_Proxy::mk_esn_from_schema(ODR o, const char *schema)
1609 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1610 odr_malloc(o, sizeof(Z_ElementSetNames));
1611 esn->which = Z_ElementSetNames_generic;
1612 esn->u.generic = odr_strdup(o, schema);
1616 void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
1620 odr_destroy(m_s2z_odr_init);
1623 if (m_s2z_odr_search)
1625 odr_destroy(m_s2z_odr_search);
1626 m_s2z_odr_search = 0;
1629 m_http_keepalive = 0;
1631 if (!strcmp(hreq->version, "1.0"))
1633 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1634 if (v && !strcmp(v, "Keep-Alive"))
1635 m_http_keepalive = 1;
1637 m_http_keepalive = 0;
1638 m_http_version = "1.0";
1642 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1643 if (v && !strcmp(v, "close"))
1644 m_http_keepalive = 0;
1646 m_http_keepalive = 1;
1647 m_http_version = "1.1";
1650 Z_SRW_PDU *srw_pdu = 0;
1651 Z_SOAP *soap_package = 0;
1653 Z_SRW_diagnostic *diagnostic = 0;
1654 int num_diagnostic = 0;
1655 if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1657 || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1658 &charset, &diagnostic, &num_diagnostic) == 0)
1660 m_s2z_odr_init = odr_createmem(ODR_ENCODE);
1661 m_s2z_odr_search = odr_createmem(ODR_ENCODE);
1662 m_soap_ns = odr_strdup(m_s2z_odr_search, soap_package->ns);
1663 m_s2z_init_apdu = 0;
1664 m_s2z_search_apdu = 0;
1665 m_s2z_present_apdu = 0;
1667 m_s2z_stylesheet = 0;
1669 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
1671 Z_SRW_searchRetrieveRequest *srw_req = srw_pdu->u.request;
1673 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1674 // recordXPath unsupported.
1675 if (srw_req->recordXPath)
1677 yaz_add_srw_diagnostic(odr_decode(),
1678 &diagnostic, &num_diagnostic,
1681 // must have a query
1682 if (!srw_req->query.cql)
1684 yaz_add_srw_diagnostic(odr_decode(),
1685 &diagnostic, &num_diagnostic,
1689 if (srw_req->sort_type != Z_SRW_sort_type_none)
1691 yaz_add_srw_diagnostic(odr_decode(),
1692 &diagnostic, &num_diagnostic,
1696 if (srw_req->stylesheet)
1698 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1700 // set packing for response records ..
1701 if (srw_req->recordPacking &&
1702 !strcmp(srw_req->recordPacking, "xml"))
1703 m_s2z_packing = Z_SRW_recordPacking_XML;
1705 m_s2z_packing = Z_SRW_recordPacking_string;
1709 Z_SRW_PDU *srw_pdu =
1710 yaz_srw_get(odr_encode(),
1711 Z_SRW_searchRetrieve_response);
1712 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1714 srw_res->diagnostics = diagnostic;
1715 srw_res->num_diagnostics = num_diagnostic;
1716 send_srw_response(srw_pdu);
1720 // prepare search PDU
1721 m_s2z_search_apdu = zget_APDU(m_s2z_odr_search,
1722 Z_APDU_searchRequest);
1723 Z_SearchRequest *z_searchRequest =
1724 m_s2z_search_apdu->u.searchRequest;
1726 z_searchRequest->num_databaseNames = 1;
1727 z_searchRequest->databaseNames = (char**)
1728 odr_malloc(m_s2z_odr_search, sizeof(char *));
1729 z_searchRequest->databaseNames[0] = odr_strdup(m_s2z_odr_search,
1732 // query transformation
1733 Z_Query *query = (Z_Query *)
1734 odr_malloc(m_s2z_odr_search, sizeof(Z_Query));
1735 z_searchRequest->query = query;
1737 if (srw_req->query_type == Z_SRW_query_type_cql)
1739 Z_External *ext = (Z_External *)
1740 odr_malloc(m_s2z_odr_search, sizeof(*ext));
1741 ext->direct_reference =
1742 odr_getoidbystr(m_s2z_odr_search, "1.2.840.10003.16.2");
1743 ext->indirect_reference = 0;
1744 ext->descriptor = 0;
1745 ext->which = Z_External_CQL;
1746 ext->u.cql = srw_req->query.cql;
1748 query->which = Z_Query_type_104;
1749 query->u.type_104 = ext;
1751 else if (srw_req->query_type == Z_SRW_query_type_pqf)
1753 Z_RPNQuery *RPNquery;
1754 YAZ_PQF_Parser pqf_parser;
1756 pqf_parser = yaz_pqf_create ();
1758 RPNquery = yaz_pqf_parse (pqf_parser, m_s2z_odr_search,
1759 srw_req->query.pqf);
1762 const char *pqf_msg;
1764 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1765 yaz_log(LOG_LOG, "%*s^\n", off+4, "");
1766 yaz_log(LOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
1768 send_to_srw_client_error(10, 0);
1771 query->which = Z_Query_type_1;
1772 query->u.type_1 = RPNquery;
1774 yaz_pqf_destroy (pqf_parser);
1778 send_to_srw_client_error(7, "query");
1783 m_s2z_present_apdu = 0;
1785 if (srw_req->maximumRecords)
1786 max = *srw_req->maximumRecords;
1788 if (srw_req->startRecord)
1789 start = *srw_req->startRecord;
1792 // Some backend, such as Voyager doesn't honor piggyback
1793 // So we use present always (0 &&).
1794 if (0 && start <= 1) // Z39.50 piggyback
1796 *z_searchRequest->smallSetUpperBound = max;
1797 *z_searchRequest->mediumSetPresentNumber = max;
1798 *z_searchRequest->largeSetLowerBound = 2000000000; // 2e9
1800 z_searchRequest->preferredRecordSyntax =
1801 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1803 if (srw_req->recordSchema)
1805 z_searchRequest->smallSetElementSetNames =
1806 z_searchRequest->mediumSetElementSetNames =
1807 mk_esn_from_schema(m_s2z_odr_search,
1808 srw_req->recordSchema);
1811 else // Z39.50 present
1813 m_s2z_present_apdu = zget_APDU(m_s2z_odr_search,
1814 Z_APDU_presentRequest);
1815 Z_PresentRequest *z_presentRequest =
1816 m_s2z_present_apdu->u.presentRequest;
1817 *z_presentRequest->resultSetStartPoint = start;
1818 *z_presentRequest->numberOfRecordsRequested = max;
1819 z_presentRequest->preferredRecordSyntax =
1820 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1822 if (srw_req->recordSchema)
1824 z_presentRequest->recordComposition =
1825 (Z_RecordComposition *)
1826 odr_malloc(m_s2z_odr_search,
1827 sizeof(Z_RecordComposition));
1828 z_presentRequest->recordComposition->which =
1829 Z_RecordComp_simple;
1830 z_presentRequest->recordComposition->u.simple =
1831 mk_esn_from_schema(m_s2z_odr_search,
1832 srw_req->recordSchema);
1838 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1839 Z_APDU_initRequest);
1841 // prevent m_initRequest_apdu memory from being grabbed
1842 // in Yaz_Proxy::handle_incoming_Z_PDU
1843 m_initRequest_apdu = m_s2z_init_apdu;
1844 handle_incoming_Z_PDU(m_s2z_init_apdu);
1849 handle_incoming_Z_PDU(m_s2z_search_apdu);
1853 else if (srw_pdu->which == Z_SRW_explain_request)
1855 Z_SRW_explainRequest *srw_req = srw_pdu->u.explain_request;
1857 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1860 if (srw_req->stylesheet)
1862 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1864 if (srw_req->recordPacking &&
1865 !strcmp(srw_req->recordPacking, "xml"))
1866 m_s2z_packing = Z_SRW_recordPacking_XML;
1868 m_s2z_packing = Z_SRW_recordPacking_string;
1872 send_srw_explain_response(diagnostic, num_diagnostic);
1878 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1879 Z_APDU_initRequest);
1881 // prevent m_initRequest_apdu memory from being grabbed
1882 // in Yaz_Proxy::handle_incoming_Z_PDU
1883 m_initRequest_apdu = m_s2z_init_apdu;
1884 handle_incoming_Z_PDU(m_s2z_init_apdu);
1887 send_srw_explain_response(0, 0);
1890 else if (srw_pdu->which == Z_SRW_scan_request)
1892 m_s2z_database = odr_strdup(m_s2z_odr_init,
1893 srw_pdu->u.scan_request->database);
1895 yaz_add_srw_diagnostic(odr_decode(),
1896 &diagnostic, &num_diagnostic,
1898 Z_SRW_PDU *srw_pdu =
1899 yaz_srw_get(odr_encode(),
1900 Z_SRW_scan_response);
1901 Z_SRW_scanResponse *srw_res = srw_pdu->u.scan_response;
1903 srw_res->diagnostics = diagnostic;
1904 srw_res->num_diagnostics = num_diagnostic;
1905 send_srw_response(srw_pdu);
1912 send_to_srw_client_error(4, 0);
1916 Z_GDU *p = z_get_HTTP_Response(odr_encode(), 400);
1921 void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
1923 Z_ReferenceId **refid = get_referenceIdP(apdu);
1924 nmem_reset(m_referenceId_mem);
1925 if (refid && *refid)
1927 m_referenceId = (Z_ReferenceId *)
1928 nmem_malloc(m_referenceId_mem, sizeof(*m_referenceId));
1929 m_referenceId->len = m_referenceId->size = (*refid)->len;
1930 m_referenceId->buf = (unsigned char *)
1931 nmem_malloc(m_referenceId_mem, (*refid)->len);
1932 memcpy(m_referenceId->buf, (*refid)->buf, (*refid)->len);
1937 if (!m_client && m_invalid_session)
1939 m_apdu_invalid_session = apdu;
1940 m_mem_invalid_session = odr_extract_mem(odr_decode());
1941 apdu = m_initRequest_apdu;
1944 // Determine our client.
1945 Z_OtherInformation **oi;
1946 get_otherInfoAPDU(apdu, &oi);
1947 m_client = get_client(apdu, get_cookie(oi), get_proxy(oi));
1953 m_client->m_server = this;
1955 if (apdu->which == Z_APDU_initRequest)
1957 if (apdu->u.initRequest->implementationId)
1958 yaz_log(LOG_LOG, "%simplementationId: %s",
1959 m_session_str, apdu->u.initRequest->implementationId);
1960 if (apdu->u.initRequest->implementationName)
1961 yaz_log(LOG_LOG, "%simplementationName: %s",
1962 m_session_str, apdu->u.initRequest->implementationName);
1963 if (apdu->u.initRequest->implementationVersion)
1964 yaz_log(LOG_LOG, "%simplementationVersion: %s",
1965 m_session_str, apdu->u.initRequest->implementationVersion);
1966 if (m_initRequest_apdu == 0)
1968 if (m_initRequest_mem)
1969 nmem_destroy(m_initRequest_mem);
1970 m_initRequest_apdu = apdu;
1971 m_initRequest_mem = odr_extract_mem(odr_decode());
1973 // save init options for the response..
1974 m_initRequest_options = apdu->u.initRequest->options;
1976 apdu->u.initRequest->options =
1977 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
1978 sizeof(Odr_bitmask));
1979 ODR_MASK_ZERO(apdu->u.initRequest->options);
1981 for (i = 0; i<= 24; i++)
1982 ODR_MASK_SET(apdu->u.initRequest->options, i);
1983 ODR_MASK_CLEAR(apdu->u.initRequest->options,
1984 Z_Options_negotiationModel);
1987 m_initRequest_version = apdu->u.initRequest->protocolVersion;
1988 apdu->u.initRequest->protocolVersion =
1989 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
1990 sizeof(Odr_bitmask));
1991 ODR_MASK_ZERO(apdu->u.initRequest->protocolVersion);
1993 for (i = 0; i<= 8; i++)
1994 ODR_MASK_SET(apdu->u.initRequest->protocolVersion, i);
1996 if (m_client->m_init_flag)
1998 if (handle_init_response_for_invalid_session(apdu))
2000 if (m_client->m_initResponse)
2002 Z_APDU *apdu2 = m_client->m_initResponse;
2003 apdu2->u.initResponse->otherInfo = 0;
2004 if (m_client->m_cookie && *m_client->m_cookie)
2005 set_otherInformationString(apdu2, VAL_COOKIE, 1,
2006 m_client->m_cookie);
2007 apdu2->u.initResponse->referenceId =
2008 apdu->u.initRequest->referenceId;
2009 apdu2->u.initResponse->options = m_client->m_initResponse_options;
2010 apdu2->u.initResponse->protocolVersion =
2011 m_client->m_initResponse_version;
2013 send_to_client(apdu2);
2017 m_client->m_init_flag = 1;
2019 handle_max_record_retrieve(apdu);
2022 apdu = handle_syntax_validation(apdu);
2025 apdu = handle_query_transformation(apdu);
2028 apdu = handle_query_validation(apdu);
2031 apdu = result_set_optimize(apdu);
2034 m_client->timeout(m_target_idletime); // mark it active even
2035 // though we didn't use it
2039 // delete other info part from PDU before sending to target
2040 get_otherInfoAPDU(apdu, &oi);
2044 if (apdu->which == Z_APDU_presentRequest &&
2045 m_client->m_resultSetStartPoint == 0)
2047 Z_PresentRequest *pr = apdu->u.presentRequest;
2048 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
2049 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
2051 m_client->m_resultSetStartPoint = 0;
2053 if (m_client->send_to_target(apdu) < 0)
2060 m_client->m_waiting = 1;
2063 void Yaz_Proxy::connectNotify()
2067 void Yaz_Proxy::shutdown()
2069 m_invalid_session = 0;
2070 // only keep if keep_alive flag is set...
2072 m_client->m_pdu_recv < m_keepalive_limit_pdu &&
2073 m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
2074 m_client->m_waiting == 0)
2076 yaz_log(LOG_LOG, "%sShutdown (client to proxy) keepalive %s",
2078 m_client->get_hostname());
2079 yaz_log(LOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
2080 m_session_str, m_client->m_pdu_recv,
2081 m_client->m_bytes_sent + m_client->m_bytes_recv,
2082 m_keepalive_limit_bw, m_keepalive_limit_pdu);
2083 assert (m_client->m_waiting != 2);
2084 // Tell client (if any) that no server connection is there..
2085 m_client->m_server = 0;
2086 m_invalid_session = 0;
2090 yaz_log (LOG_LOG, "%sShutdown (client to proxy) close %s",
2092 m_client->get_hostname());
2093 assert (m_client->m_waiting != 2);
2098 yaz_log (LOG_LOG, "%sshutdown (client to proxy) bad state",
2104 yaz_log (LOG_LOG, "%sShutdown (client to proxy)",
2108 m_parent->pre_init();
2112 const char *Yaz_ProxyClient::get_session_str()
2116 return m_server->get_session_str();
2119 void Yaz_ProxyClient::shutdown()
2121 yaz_log (LOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
2127 void Yaz_Proxy::failNotify()
2130 yaz_log (LOG_LOG, "%sConnection closed by client",
2135 void Yaz_ProxyClient::failNotify()
2138 m_server->inc_request_no();
2139 yaz_log (LOG_LOG, "%sConnection closed by target %s",
2140 get_session_str(), get_hostname());
2144 void Yaz_ProxyClient::connectNotify()
2146 const char *s = get_session_str();
2147 const char *h = get_hostname();
2148 yaz_log (LOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
2150 timeout(m_target_idletime);
2155 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
2156 *the_PDU_Observable, int fd)
2158 return new Yaz_ProxyClient(the_PDU_Observable, 0);
2161 Yaz_ProxyClient::~Yaz_ProxyClient()
2166 m_next->m_prev = m_prev;
2167 m_waiting = 2; // for debugging purposes only.
2168 odr_destroy(m_init_odr);
2169 delete m_last_query;
2170 xfree (m_last_resultSetId);
2174 void Yaz_ProxyClient::pre_init_client()
2176 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
2177 Z_InitRequest *req = apdu->u.initRequest;
2180 for (i = 0; i<= 24; i++)
2181 ODR_MASK_SET(req->options, i);
2182 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2183 Z_Options_negotiationModel);
2184 for (i = 0; i<= 10; i++)
2185 ODR_MASK_SET(req->protocolVersion, i);
2187 if (send_to_target(apdu) < 0)
2198 void Yaz_Proxy::pre_init()
2201 const char *name = 0;
2202 const char *zurl_in_use[MAX_ZURL_PLEX];
2203 int limit_bw, limit_pdu, limit_req;
2204 int target_idletime, client_idletime;
2206 int keepalive_limit_bw, keepalive_limit_pdu;
2208 const char *cql2rpn = 0;
2210 Yaz_ProxyConfig *cfg = check_reconfigure();
2214 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
2219 for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
2220 &limit_bw, &limit_pdu, &limit_req,
2221 &target_idletime, &client_idletime,
2223 &keepalive_limit_bw,
2224 &keepalive_limit_pdu,
2231 for (j = 0; zurl_in_use[j]; j++)
2237 for (c = m_clientPool; c; c = c->m_next)
2239 if (!strcmp(zurl_in_use[j], c->get_hostname()))
2241 if (c->m_cookie == 0)
2243 if (c->m_server == 0)
2252 yaz_log(LOG_LOG, "%spre-init %s %s use=%d other=%d spare=%d "
2253 "preinit=%d",m_session_str,
2254 name, zurl_in_use[j], in_use, other, spare, pre_init);
2255 if (spare < pre_init)
2257 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
2258 c->m_next = m_clientPool;
2260 c->m_next->m_prev = &c->m_next;
2262 c->m_prev = &m_clientPool;
2264 if (m_log_mask & PROXY_LOG_APDU_SERVER)
2265 c->set_APDU_yazlog(1);
2267 c->set_APDU_yazlog(0);
2269 if (c->client(zurl_in_use[j]))
2277 c->m_target_idletime = target_idletime;
2278 c->m_seqno = m_seqno++;
2285 void Yaz_Proxy::timeoutNotify()
2291 timeout(m_client_idletime);
2292 Z_GDU *apdu = m_bw_hold_PDU;
2295 if (apdu->which == Z_GDU_Z3950)
2296 handle_incoming_Z_PDU(apdu->u.z3950);
2297 else if (apdu->which == Z_GDU_HTTP_Request)
2298 handle_incoming_HTTP(apdu->u.HTTP_Request);
2300 else if (m_stylesheet_nprl)
2301 convert_xsl_delay();
2306 yaz_log (LOG_LOG, "%sTimeout (client to proxy)", m_session_str);
2317 void Yaz_Proxy::markInvalid()
2320 m_invalid_session = 1;
2323 void Yaz_ProxyClient::timeoutNotify()
2326 m_server->inc_request_no();
2328 yaz_log (LOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
2332 if (m_server && m_init_flag)
2334 // target timed out in a session that was properly initialized
2335 // server object stay alive but we mark it as invalid so it
2336 // gets initialized again
2337 m_server->markInvalid();
2343 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
2344 Yaz_Proxy *parent) :
2345 Yaz_Z_Assoc (the_PDU_Observable)
2352 m_last_resultSetId = 0;
2353 m_last_resultCount = 0;
2357 m_init_odr = odr_createmem (ODR_DECODE);
2359 m_initResponse_options = 0;
2360 m_initResponse_version = 0;
2361 m_resultSetStartPoint = 0;
2362 m_bytes_sent = m_bytes_recv = 0;
2366 m_target_idletime = 600;
2370 const char *Yaz_Proxy::option(const char *name, const char *value)
2372 if (!strcmp (name, "optimize")) {
2375 m_optimize = xstrdup (value);
2382 void Yaz_ProxyClient::recv_HTTP_response(Z_HTTP_Response *apdu, int len)
2387 void Yaz_ProxyClient::recv_GDU(Z_GDU *apdu, int len)
2389 if (apdu->which == Z_GDU_Z3950)
2390 recv_Z_PDU(apdu->u.z3950, len);
2391 else if (apdu->which == Z_GDU_HTTP_Response)
2392 recv_HTTP_response(apdu->u.HTTP_Response, len);
2397 int Yaz_Proxy::handle_init_response_for_invalid_session(Z_APDU *apdu)
2399 if (!m_invalid_session)
2401 m_invalid_session = 0;
2402 handle_incoming_Z_PDU(m_apdu_invalid_session);
2403 assert (m_mem_invalid_session);
2404 nmem_destroy(m_mem_invalid_session);
2405 m_mem_invalid_session = 0;
2409 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
2411 m_bytes_recv += len;
2415 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
2416 yaz_log (LOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
2417 apdu_name(apdu), get_hostname(), len);
2418 if (apdu->which == Z_APDU_initResponse)
2420 if (!m_server) // if this is a pre init session , check for more
2422 NMEM nmem = odr_extract_mem (odr_decode());
2423 odr_reset (m_init_odr);
2424 nmem_transfer (m_init_odr->mem, nmem);
2425 m_initResponse = apdu;
2426 m_initResponse_options = apdu->u.initResponse->options;
2427 m_initResponse_version = apdu->u.initResponse->protocolVersion;
2429 Z_InitResponse *ir = apdu->u.initResponse;
2430 char *im0 = ir->implementationName;
2433 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
2440 strcat(im1, "(YAZ Proxy)");
2441 ir->implementationName = im1;
2443 nmem_destroy (nmem);
2445 if (m_server && m_server->handle_init_response_for_invalid_session(apdu))
2448 if (apdu->which == Z_APDU_searchResponse)
2450 Z_SearchResponse *sr = apdu->u.searchResponse;
2451 m_last_resultCount = *sr->resultCount;
2452 int status = *sr->searchStatus;
2453 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
2457 if (sr->records && sr->records->which == Z_Records_DBOSD)
2459 m_cache.add(odr_decode(),
2460 sr->records->u.databaseOrSurDiagnostics, 1,
2465 if (apdu->which == Z_APDU_presentResponse)
2467 Z_PresentResponse *pr = apdu->u.presentResponse;
2471 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2472 Z_SearchResponse *sr = new_apdu->u.searchResponse;
2473 sr->referenceId = pr->referenceId;
2474 *sr->resultCount = m_last_resultCount;
2475 sr->records = pr->records;
2476 sr->nextResultSetPosition = pr->nextResultSetPosition;
2477 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
2481 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
2483 m_cache.add(odr_decode(),
2484 pr->records->u.databaseOrSurDiagnostics,
2485 m_resultSetStartPoint, -1);
2486 m_resultSetStartPoint = 0;
2490 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
2493 m_server->send_to_client(apdu);
2495 if (apdu->which == Z_APDU_close)
2501 int Yaz_Proxy::server(const char *addr)
2503 int r = Yaz_Z_Assoc::server(addr);
2506 yaz_log(LOG_LOG, "%sStarted proxy " VERSION " on %s", m_session_str, addr);