2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.95 2004-01-30 12:02:21 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;
105 m_stylesheet_xsp = 0;
106 m_s2z_stylesheet = 0;
109 m_initRequest_apdu = 0;
110 m_initRequest_mem = 0;
111 m_initRequest_options = 0;
112 m_initRequest_version = 0;
113 m_apdu_invalid_session = 0;
114 m_mem_invalid_session = 0;
116 m_s2z_odr_search = 0;
118 m_s2z_search_apdu = 0;
119 m_s2z_present_apdu = 0;
120 m_http_keepalive = 0;
123 m_s2z_packing = Z_SRW_recordPacking_string;
124 m_time_tv.tv_sec = 0;
125 m_time_tv.tv_usec = 0;
128 Yaz_Proxy::~Yaz_Proxy()
130 yaz_log(LOG_LOG, "%sClosed %d/%d sent/recv bytes total", m_session_str,
131 m_bytes_sent, m_bytes_recv);
132 nmem_destroy(m_initRequest_mem);
133 nmem_destroy(m_mem_invalid_session);
134 xfree (m_proxyTarget);
135 xfree (m_default_target);
136 xfree (m_proxy_authentication);
139 if (m_stylesheet_xsp)
140 xsltFreeStylesheet(m_stylesheet_xsp);
144 odr_destroy(m_s2z_odr_init);
145 if (m_s2z_odr_search)
146 odr_destroy(m_s2z_odr_search);
150 int Yaz_Proxy::set_config(const char *config)
153 m_config = new Yaz_ProxyConfig();
154 xfree(m_config_fname);
155 m_config_fname = xstrdup(config);
156 int r = m_config->read_xml(config);
158 m_config->get_generic_info(&m_log_mask, &m_max_clients);
162 void Yaz_Proxy::set_default_target(const char *target)
164 xfree (m_default_target);
165 m_default_target = 0;
167 m_default_target = (char *) xstrdup (target);
170 void Yaz_Proxy::set_proxy_authentication (const char *auth)
172 xfree (m_proxy_authentication);
173 m_proxy_authentication = 0;
175 m_proxy_authentication = (char *) xstrdup (auth);
178 Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
181 return m_parent->check_reconfigure();
183 Yaz_ProxyConfig *cfg = m_config;
186 yaz_log(LOG_LOG, "reconfigure");
188 if (m_config_fname && cfg)
190 yaz_log(LOG_LOG, "reconfigure config %s", m_config_fname);
191 int r = cfg->read_xml(m_config_fname);
193 yaz_log(LOG_WARN, "reconfigure failed");
197 cfg->get_generic_info(&m_log_mask, &m_max_clients);
201 yaz_log(LOG_LOG, "reconfigure");
207 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
208 *the_PDU_Observable, int fd)
211 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable, this);
212 new_proxy->m_config = 0;
213 new_proxy->m_config_fname = 0;
214 new_proxy->timeout(m_client_idletime);
215 new_proxy->m_target_idletime = m_target_idletime;
216 new_proxy->set_default_target(m_default_target);
217 new_proxy->m_max_clients = m_max_clients;
218 new_proxy->m_log_mask = m_log_mask;
219 new_proxy->set_APDU_log(get_APDU_log());
220 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
221 new_proxy->set_APDU_yazlog(1);
223 new_proxy->set_APDU_yazlog(0);
224 new_proxy->set_proxy_authentication(m_proxy_authentication);
225 sprintf(new_proxy->m_session_str, "%ld:%d ", (long) time(0), m_session_no);
227 yaz_log (LOG_LOG, "%sNew session %s", new_proxy->m_session_str,
228 the_PDU_Observable->getpeername());
232 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
235 Z_OtherInformationUnit *oi;
237 ent.proto = PROTO_Z3950;
238 ent.oclass = CLASS_USERINFO;
239 ent.value = (oid_value) VAL_COOKIE;
240 assert (oid_ent_to_oid (&ent, oid));
242 if (oid_ent_to_oid (&ent, oid) &&
243 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
244 oi->which == Z_OtherInfo_characterInfo)
245 return oi->information.characterInfo;
249 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
252 Z_OtherInformationUnit *oi;
254 ent.proto = PROTO_Z3950;
255 ent.oclass = CLASS_USERINFO;
256 ent.value = (oid_value) VAL_PROXY;
257 if (oid_ent_to_oid (&ent, oid) &&
258 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
259 oi->which == Z_OtherInfo_characterInfo)
260 return oi->information.characterInfo;
264 const char *Yaz_Proxy::load_balance(const char **url)
266 int zurl_in_use[MAX_ZURL_PLEX];
270 for (i = 0; i<MAX_ZURL_PLEX; i++)
272 for (c = m_parent->m_clientPool; c; c = c->m_next)
274 for (i = 0; url[i]; i++)
275 if (!strcmp(url[i], c->get_hostname()))
280 for (i = 0; url[i]; i++)
282 yaz_log(LOG_DEBUG, "%szurl=%s use=%d",
283 m_session_str, url[i], zurl_in_use[i]);
284 if (min > zurl_in_use[i])
287 min = zurl_in_use[i];
293 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
294 const char *proxy_host)
297 Yaz_Proxy *parent = m_parent;
298 Yaz_ProxyClient *c = m_client;
302 const char *url[MAX_ZURL_PLEX];
303 Yaz_ProxyConfig *cfg = check_reconfigure();
307 /* only to be enabled for debugging... */
308 if (!strcmp(proxy_host, "stop"))
311 xfree(m_default_target);
312 m_default_target = xstrdup(proxy_host);
313 proxy_host = m_default_target;
315 int client_idletime = -1;
316 const char *cql2rpn_fname = 0;
317 url[0] = m_default_target;
322 cfg->get_target_info(proxy_host, url, &m_bw_max,
323 &m_pdu_max, &m_max_record_retrieve,
324 &m_target_idletime, &client_idletime,
325 &parent->m_max_clients,
326 &m_keepalive_limit_bw,
327 &m_keepalive_limit_pdu,
331 if (client_idletime != -1)
333 m_client_idletime = client_idletime;
334 timeout(m_client_idletime);
337 m_cql2rpn.set_pqf_file(cql2rpn_fname);
340 yaz_log(LOG_LOG, "%sNo default target", m_session_str);
343 // we don't handle multiplexing for cookie session, so we just
344 // pick the first one in this case (anonymous users will be able
345 // to use any backend)
346 if (cookie && *cookie)
347 m_proxyTarget = (char*) xstrdup(url[0]);
349 m_proxyTarget = (char*) xstrdup(load_balance(url));
351 if (cookie && *cookie)
352 { // search in sessions with a cookie
353 for (c = parent->m_clientPool; c; c = c->m_next)
356 assert (*c->m_prev == c);
357 if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
358 !strcmp(m_proxyTarget, c->get_hostname()))
361 // The following handles "cancel"
362 // If connection is busy (waiting for PDU) and
363 // we have an initRequest we can safely do re-open
364 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
366 yaz_log (LOG_LOG, "%s REOPEN target=%s", m_session_str,
373 c->m_last_resultCount = 0;
374 c->m_sr_transform = 0;
376 c->m_resultSetStartPoint = 0;
377 c->m_target_idletime = m_target_idletime;
378 if (c->client(m_proxyTarget))
385 c->m_seqno = parent->m_seqno;
386 if (c->m_server && c->m_server != this)
387 c->m_server->m_client = 0;
390 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
397 // don't have a client session yet. Search in session w/o cookie
398 for (c = parent->m_clientPool; c; c = c->m_next)
401 assert (*c->m_prev == c);
402 if (c->m_server == 0 && c->m_cookie == 0 &&
404 !strcmp(m_proxyTarget, c->get_hostname()))
407 yaz_log (LOG_LOG, "%sREUSE %s",
408 m_session_str, c->get_hostname());
410 c->m_seqno = parent->m_seqno;
411 assert(c->m_server == 0);
414 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
415 c->set_APDU_yazlog(1);
417 c->set_APDU_yazlog(0);
429 if (apdu->which != Z_APDU_initRequest)
431 yaz_log (LOG_LOG, "%sno init request as first PDU", m_session_str);
434 Z_InitRequest *initRequest = apdu->u.initRequest;
436 if (!initRequest->idAuthentication)
438 if (m_proxy_authentication)
440 initRequest->idAuthentication =
441 (Z_IdAuthentication *)
442 odr_malloc (odr_encode(),
443 sizeof(*initRequest->idAuthentication));
444 initRequest->idAuthentication->which =
445 Z_IdAuthentication_open;
446 initRequest->idAuthentication->u.open =
447 odr_strdup (odr_encode(), m_proxy_authentication);
450 // go through list of clients - and find the lowest/oldest one.
451 Yaz_ProxyClient *c_min = 0;
453 int no_of_clients = 0;
454 if (parent->m_clientPool)
455 yaz_log (LOG_DEBUG, "Existing sessions");
456 for (c = parent->m_clientPool; c; c = c->m_next)
458 yaz_log (LOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
459 c->m_waiting, c->get_hostname(),
460 c->m_cookie ? c->m_cookie : "");
462 if (min_seq < 0 || c->m_seqno < min_seq)
464 min_seq = c->m_seqno;
468 if (no_of_clients >= parent->m_max_clients)
471 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
473 yaz_log (LOG_LOG, "%sMAXCLIENTS Destroy %d",
474 m_session_str, c->m_seqno);
475 if (c->m_server && c->m_server != this)
481 yaz_log (LOG_LOG, "%sMAXCLIENTS Reuse %d %d %s",
483 c->m_seqno, parent->m_seqno, c->get_hostname());
487 c->m_cookie = xstrdup(cookie);
488 c->m_seqno = parent->m_seqno;
489 if (c->m_server && c->m_server != this)
491 c->m_server->m_client = 0;
495 c->m_target_idletime = m_target_idletime;
496 c->timeout(m_target_idletime);
498 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
499 c->set_APDU_yazlog(1);
501 c->set_APDU_yazlog(0);
508 yaz_log (LOG_LOG, "%sNEW %d %s",
509 m_session_str, parent->m_seqno, m_proxyTarget);
510 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), parent);
511 c->m_next = parent->m_clientPool;
513 c->m_next->m_prev = &c->m_next;
514 parent->m_clientPool = c;
515 c->m_prev = &parent->m_clientPool;
521 c->m_cookie = xstrdup(cookie);
523 c->m_seqno = parent->m_seqno;
525 c->m_last_resultCount = 0;
528 c->m_sr_transform = 0;
530 c->m_resultSetStartPoint = 0;
532 if (c->client(m_proxyTarget))
537 c->m_target_idletime = m_target_idletime;
540 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
541 c->set_APDU_yazlog(1);
543 c->set_APDU_yazlog(0);
545 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
549 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
552 for (i = 0; i<num; i++)
555 Z_DefaultDiagFormat *r;
556 Z_DiagRec *p = pp[i];
557 if (p->which != Z_DiagRec_defaultFormat)
559 yaz_log(LOG_LOG, "%sError no diagnostics", m_session_str);
563 r = p->u.defaultFormat;
564 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
565 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
566 yaz_log(LOG_LOG, "%sError unknown diagnostic set", m_session_str);
569 case Z_DefaultDiagFormat_v2Addinfo:
570 yaz_log(LOG_LOG, "%sError %d %s:%s",
572 *r->condition, diagbib1_str(*r->condition),
575 case Z_DefaultDiagFormat_v3Addinfo:
576 yaz_log(LOG_LOG, "%sError %d %s:%s",
578 *r->condition, diagbib1_str(*r->condition),
585 int Yaz_Proxy::convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu)
587 if (!m_stylesheet_xsp || p->num_records <= 0)
588 return 0; /* no XSLT to be done ... */
590 m_stylesheet_offset = 0;
591 m_stylesheet_nprl = p;
592 m_stylesheet_apdu = apdu;
597 void Yaz_Proxy::convert_xsl_delay()
599 Z_NamePlusRecord *npr = m_stylesheet_nprl->records[m_stylesheet_offset];
600 if (npr->which == Z_NamePlusRecord_databaseRecord)
602 Z_External *r = npr->u.databaseRecord;
603 if (r->which == Z_External_octet)
605 xmlDocPtr res, doc = xmlParseMemory(
606 (char*) r->u.octet_aligned->buf,
607 r->u.octet_aligned->len);
609 yaz_log(LOG_LOG, "%sXSLT convert %d",
610 m_session_str, m_stylesheet_offset);
611 res = xsltApplyStylesheet(m_stylesheet_xsp, doc, 0);
616 xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
618 m_stylesheet_nprl->records[m_stylesheet_offset]->
620 z_ext_record(odr_encode(), VAL_TEXT_XML,
621 (char*) out_buf, out_len);
628 m_stylesheet_offset++;
629 if (m_stylesheet_offset == m_stylesheet_nprl->num_records)
631 m_stylesheet_nprl = 0;
632 if (m_stylesheet_xsp)
633 xsltFreeStylesheet(m_stylesheet_xsp);
634 m_stylesheet_xsp = 0;
635 timeout(m_client_idletime);
636 int r = send_PDU_convert(m_stylesheet_apdu);
642 void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
646 yaz_marc_t mt = yaz_marc_create();
647 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
648 for (i = 0; i < p->num_records; i++)
650 Z_NamePlusRecord *npr = p->records[i];
651 if (npr->which == Z_NamePlusRecord_databaseRecord)
653 Z_External *r = npr->u.databaseRecord;
654 if (r->which == Z_External_octet)
658 if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
659 r->u.octet_aligned->len,
662 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
663 WRBUF wrbuf = wrbuf_alloc();
666 size_t inbytesleft = rlen;
667 const char *inp = result;
668 while (cd && inbytesleft)
670 size_t outbytesleft = sizeof(outbuf);
674 r = yaz_iconv (cd, (char**) &inp,
676 &outp, &outbytesleft);
677 if (r == (size_t) (-1))
679 int e = yaz_iconv_error(cd);
680 if (e != YAZ_ICONV_E2BIG)
682 yaz_log(LOG_WARN, "conversion failure");
686 wrbuf_write(wrbuf, outbuf, outp - outbuf);
691 npr->u.databaseRecord = z_ext_record(odr_encode(),
695 wrbuf_free(wrbuf, 1);
700 yaz_marc_destroy(mt);
703 void Yaz_Proxy::logtime()
705 if (m_time_tv.tv_sec)
708 gettimeofday(&tv, 0);
709 long diff = (tv.tv_sec - m_time_tv.tv_sec)*1000000 +
710 (tv.tv_usec - m_time_tv.tv_usec);
712 yaz_log(LOG_LOG, "%sElapsed %ld.%03ld", m_session_str,
713 diff/1000000, (diff/1000)%1000);
715 m_time_tv.tv_sec = 0;
716 m_time_tv.tv_usec = 0;
719 int Yaz_Proxy::send_http_response(int code)
721 ODR o = odr_encode();
722 Z_GDU *gdu = z_get_HTTP_Response(o, code);
723 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
725 hres->version = odr_strdup(o, m_http_version);
726 if (m_http_keepalive)
727 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
731 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
733 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
737 int r = send_GDU(gdu, &len);
739 m_bw_stat.add_bytes(len);
744 int Yaz_Proxy::send_srw_response(Z_SRW_PDU *srw_pdu)
746 ODR o = odr_encode();
747 const char *ctype = "text/xml";
748 Z_GDU *gdu = z_get_HTTP_Response(o, 200);
749 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
751 hres->version = odr_strdup(o, m_http_version);
752 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
753 if (m_http_keepalive)
754 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
758 static Z_SOAP_Handler soap_handlers[2] = {
760 {"http://www.loc.gov/zing/srw/", 0,
761 (Z_SOAP_fun) yaz_srw_codec},
766 Z_SOAP *soap_package = (Z_SOAP*) odr_malloc(o, sizeof(Z_SOAP));
767 soap_package->which = Z_SOAP_generic;
768 soap_package->u.generic =
769 (Z_SOAP_Generic *) odr_malloc(o, sizeof(*soap_package->u.generic));
770 soap_package->u.generic->no = 0;
771 soap_package->u.generic->ns = soap_handlers[0].ns;
772 soap_package->u.generic->p = (void *) srw_pdu;
773 soap_package->ns = m_soap_ns;
774 z_soap_codec_enc_xsl(o, &soap_package,
775 &hres->content_buf, &hres->content_len,
776 soap_handlers, 0, m_s2z_stylesheet);
777 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
779 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
783 int r = send_GDU(gdu, &len);
785 m_bw_stat.add_bytes(len);
790 int Yaz_Proxy::send_to_srw_client_error(int srw_error, const char *add)
792 ODR o = odr_encode();
793 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
794 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
796 srw_res->num_diagnostics = 1;
797 srw_res->diagnostics = (Z_SRW_diagnostic *)
798 odr_malloc(o, sizeof(*srw_res->diagnostics));
799 yaz_mk_std_diagnostic(o, srw_res->diagnostics, srw_error, add);
800 return send_srw_response(srw_pdu);
803 int Yaz_Proxy::z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
804 Z_DefaultDiagFormat *ddf)
806 int bib1_code = *ddf->condition;
807 if (bib1_code == 109)
809 srw_res->num_diagnostics = 1;
810 srw_res->diagnostics = (Z_SRW_diagnostic *)
811 odr_malloc(o, sizeof(*srw_res->diagnostics));
812 yaz_mk_std_diagnostic(o, srw_res->diagnostics,
813 yaz_diag_bib1_to_srw(*ddf->condition),
818 int Yaz_Proxy::send_to_srw_client_ok(int hits, Z_Records *records, int start)
820 ODR o = odr_encode();
821 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
822 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
824 srw_res->numberOfRecords = odr_intdup (o, hits);
825 if (records && records->which == Z_Records_DBOSD)
827 srw_res->num_records =
828 records->u.databaseOrSurDiagnostics->num_records;
830 srw_res->records = (Z_SRW_record *)
831 odr_malloc(o, srw_res->num_records * sizeof(Z_SRW_record));
832 for (i = 0; i < srw_res->num_records; i++)
834 Z_NamePlusRecord *npr = records->u.databaseOrSurDiagnostics->records[i];
835 if (npr->which != Z_NamePlusRecord_databaseRecord)
837 srw_res->records[i].recordSchema = "diagnostic";
838 srw_res->records[i].recordPacking = m_s2z_packing;
839 srw_res->records[i].recordData_buf = "67";
840 srw_res->records[i].recordData_len = 2;
841 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
844 Z_External *r = npr->u.databaseRecord;
845 oident *ent = oid_getentbyoid(r->direct_reference);
846 if (r->which == Z_External_octet && ent->value == VAL_TEXT_XML)
848 srw_res->records[i].recordSchema = m_schema;
849 srw_res->records[i].recordPacking = m_s2z_packing;
850 srw_res->records[i].recordData_buf = (char*)
851 r->u.octet_aligned->buf;
852 srw_res->records[i].recordData_len = r->u.octet_aligned->len;
853 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
857 srw_res->records[i].recordSchema = "diagnostic";
858 srw_res->records[i].recordPacking = m_s2z_packing;
859 srw_res->records[i].recordData_buf = "67";
860 srw_res->records[i].recordData_len = 2;
861 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
865 if (records && records->which == Z_Records_NSD)
868 http_code = z_to_srw_diag(odr_encode(), srw_res,
869 records->u.nonSurrogateDiagnostic);
871 return send_http_response(http_code);
873 return send_srw_response(srw_pdu);
877 int Yaz_Proxy::send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
880 Yaz_ProxyConfig *cfg = check_reconfigure();
884 char *b = cfg->get_explain(odr_encode(), 0 /* target */,
885 m_s2z_database, &len);
888 Z_SRW_PDU *res = yaz_srw_get(odr_encode(), Z_SRW_explain_response);
889 Z_SRW_explainResponse *er = res->u.explain_response;
891 er->record.recordData_buf = b;
892 er->record.recordData_len = len;
893 er->record.recordPacking = m_s2z_packing;
895 er->diagnostics = diagnostics;
896 er->num_diagnostics = num_diagnostics;
897 return send_srw_response(res);
900 return send_http_response(404);
903 int Yaz_Proxy::send_PDU_convert(Z_APDU *apdu)
907 if (apdu->which == Z_APDU_initResponse)
909 Z_InitResponse *res = apdu->u.initResponse;
910 if (*res->result == 0)
912 send_to_srw_client_error(3, 0);
914 else if (!m_s2z_search_apdu)
916 send_srw_explain_response(0, 0);
920 handle_incoming_Z_PDU(m_s2z_search_apdu);
923 else if (m_s2z_search_apdu && apdu->which == Z_APDU_searchResponse)
925 m_s2z_search_apdu = 0;
926 Z_SearchResponse *res = apdu->u.searchResponse;
927 m_s2z_hit_count = *res->resultCount;
928 if (res->records && res->records->which == Z_Records_NSD)
930 send_to_srw_client_ok(0, res->records, 1);
932 else if (m_s2z_present_apdu && m_s2z_hit_count > 0)
935 Z_PresentRequest *pr = m_s2z_present_apdu->u.presentRequest;
937 if (*pr->resultSetStartPoint <= m_s2z_hit_count)
939 if (*pr->numberOfRecordsRequested+ *pr->resultSetStartPoint
941 *pr->numberOfRecordsRequested =
942 1 + m_s2z_hit_count - *pr->resultSetStartPoint;
944 handle_incoming_Z_PDU(m_s2z_present_apdu);
948 m_s2z_present_apdu = 0;
949 send_to_srw_client_ok(m_s2z_hit_count, res->records, 1);
952 else if (m_s2z_present_apdu && apdu->which == Z_APDU_presentResponse)
955 *m_s2z_present_apdu->u.presentRequest->resultSetStartPoint;
957 m_s2z_present_apdu = 0;
958 Z_PresentResponse *res = apdu->u.presentResponse;
959 send_to_srw_client_ok(m_s2z_hit_count, res->records, start);
965 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
966 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
968 int r = send_Z_PDU(apdu, &len);
970 m_bw_stat.add_bytes(len);
977 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
979 int kill_session = 0;
980 if (apdu->which == Z_APDU_searchResponse)
982 Z_SearchResponse *sr = apdu->u.searchResponse;
983 Z_Records *p = sr->records;
984 if (p && p->which == Z_Records_NSD)
986 Z_DiagRec dr, *dr_p = &dr;
987 dr.which = Z_DiagRec_defaultFormat;
988 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
990 *sr->searchStatus = 0;
991 display_diagrecs(&dr_p, 1);
995 if (p && p->which == Z_Records_DBOSD)
998 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
999 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1003 if (sr->resultCount)
1005 yaz_log(LOG_LOG, "%s%d hits", m_session_str,
1007 if (*sr->resultCount < 0)
1009 m_invalid_session = 1;
1012 *sr->searchStatus = 0;
1014 create_nonSurrogateDiagnostics(odr_encode(), 2, 0);
1015 *sr->resultCount = 0;
1020 else if (apdu->which == Z_APDU_presentResponse)
1022 Z_PresentResponse *sr = apdu->u.presentResponse;
1023 Z_Records *p = sr->records;
1024 if (p && p->which == Z_Records_NSD)
1026 Z_DiagRec dr, *dr_p = &dr;
1027 dr.which = Z_DiagRec_defaultFormat;
1028 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1029 if (*sr->presentStatus == Z_PresentStatus_success)
1030 *sr->presentStatus = Z_PresentStatus_failure;
1031 display_diagrecs(&dr_p, 1);
1033 if (p && p->which == Z_Records_DBOSD)
1036 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1037 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1041 else if (apdu->which == Z_APDU_initResponse)
1043 if (m_initRequest_options)
1046 (Odr_bitmask *)odr_malloc(odr_encode(),
1047 sizeof(Odr_bitmask));
1048 ODR_MASK_ZERO(nopt);
1051 for (i = 0; i<24; i++)
1052 if (ODR_MASK_GET(m_initRequest_options, i) &&
1053 ODR_MASK_GET(apdu->u.initResponse->options, i))
1054 ODR_MASK_SET(nopt, i);
1055 apdu->u.initResponse->options = nopt;
1057 if (m_initRequest_version)
1059 Z_ProtocolVersion *nopt =
1060 (Odr_bitmask *)odr_malloc(odr_encode(),
1061 sizeof(Odr_bitmask));
1062 ODR_MASK_ZERO(nopt);
1065 for (i = 0; i<8; i++)
1066 if (ODR_MASK_GET(m_initRequest_version, i) &&
1067 ODR_MASK_GET(apdu->u.initResponse->protocolVersion, i))
1068 ODR_MASK_SET(nopt, i);
1069 apdu->u.initResponse->protocolVersion = nopt;
1072 int r = send_PDU_convert(apdu);
1079 m_parent->pre_init();
1084 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
1087 const char *apdu_name_tmp = apdu_name(apdu);
1088 int r = send_Z_PDU(apdu, &len);
1089 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
1090 yaz_log (LOG_LOG, "%sSending %s to %s %d bytes",
1092 apdu_name_tmp, get_hostname(), len);
1093 m_bytes_sent += len;
1097 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
1099 if (apdu->which == Z_APDU_presentRequest)
1101 Z_PresentRequest *pr = apdu->u.presentRequest;
1102 int toget = *pr->numberOfRecordsRequested;
1103 int start = *pr->resultSetStartPoint;
1105 yaz_log(LOG_LOG, "%sPresent %s %d+%d", m_session_str,
1106 pr->resultSetId, start, toget);
1108 if (*m_parent->m_optimize == '0')
1111 if (!m_client->m_last_resultSetId)
1113 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1114 new_apdu->u.presentResponse->records =
1115 create_nonSurrogateDiagnostics(odr_encode(), 30,
1117 send_to_client(new_apdu);
1120 if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
1122 if (start+toget-1 > m_client->m_last_resultCount)
1124 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1125 new_apdu->u.presentResponse->records =
1126 create_nonSurrogateDiagnostics(odr_encode(), 13, 0);
1127 send_to_client(new_apdu);
1130 Z_NamePlusRecordList *npr;
1131 if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
1132 pr->preferredRecordSyntax,
1133 pr->recordComposition))
1135 yaz_log (LOG_LOG, "%sReturned cached records for present request",
1137 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1138 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1140 new_apdu->u.presentResponse->numberOfRecordsReturned
1141 = odr_intdup(odr_encode(), toget);
1143 new_apdu->u.presentResponse->records = (Z_Records*)
1144 odr_malloc(odr_encode(), sizeof(Z_Records));
1145 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
1146 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
1147 new_apdu->u.presentResponse->nextResultSetPosition =
1148 odr_intdup(odr_encode(), start+toget);
1150 send_to_client(new_apdu);
1156 if (apdu->which != Z_APDU_searchRequest)
1158 Z_SearchRequest *sr = apdu->u.searchRequest;
1159 Yaz_Z_Query *this_query = new Yaz_Z_Query;
1160 Yaz_Z_Databases this_databases;
1162 this_databases.set(sr->num_databaseNames, (const char **)
1165 this_query->set_Z_Query(sr->query);
1167 char query_str[120];
1168 this_query->print(query_str, sizeof(query_str)-1);
1169 yaz_log(LOG_LOG, "%sSearch %s", m_session_str, query_str);
1171 if (*m_parent->m_optimize != '0' &&
1172 m_client->m_last_ok && m_client->m_last_query &&
1173 m_client->m_last_query->match(this_query) &&
1174 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
1175 m_client->m_last_databases.match(this_databases))
1178 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
1179 m_client->m_last_resultCount < *sr->largeSetLowerBound)
1181 Z_NamePlusRecordList *npr;
1182 int toget = *sr->mediumSetPresentNumber;
1183 Z_RecordComposition *comp = 0;
1185 if (toget > m_client->m_last_resultCount)
1186 toget = m_client->m_last_resultCount;
1188 if (sr->mediumSetElementSetNames)
1190 comp = (Z_RecordComposition *)
1191 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1192 comp->which = Z_RecordComp_simple;
1193 comp->u.simple = sr->mediumSetElementSetNames;
1196 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1197 sr->preferredRecordSyntax, comp))
1199 yaz_log (LOG_LOG, "%sReturned cached records for medium set",
1201 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1202 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1203 new_apdu->u.searchResponse->resultCount =
1204 &m_client->m_last_resultCount;
1206 new_apdu->u.searchResponse->numberOfRecordsReturned
1207 = odr_intdup(odr_encode(), toget);
1209 new_apdu->u.searchResponse->presentStatus =
1210 odr_intdup(odr_encode(), Z_PresentStatus_success);
1211 new_apdu->u.searchResponse->records = (Z_Records*)
1212 odr_malloc(odr_encode(), sizeof(Z_Records));
1213 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1214 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1215 new_apdu->u.searchResponse->nextResultSetPosition =
1216 odr_intdup(odr_encode(), toget+1);
1217 send_to_client(new_apdu);
1223 // send present request (medium size)
1224 yaz_log (LOG_LOG, "%sOptimizing search for medium set",
1227 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1228 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1229 pr->referenceId = sr->referenceId;
1230 pr->resultSetId = sr->resultSetName;
1231 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1232 *pr->numberOfRecordsRequested = toget;
1233 pr->recordComposition = comp;
1234 m_client->m_sr_transform = 1;
1238 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
1239 m_client->m_last_resultCount <= 0)
1241 // large set. Return pseudo-search response immediately
1242 yaz_log (LOG_LOG, "%sOptimizing search for large set",
1244 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1245 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1246 new_apdu->u.searchResponse->resultCount =
1247 &m_client->m_last_resultCount;
1248 send_to_client(new_apdu);
1253 Z_NamePlusRecordList *npr;
1254 int toget = m_client->m_last_resultCount;
1255 Z_RecordComposition *comp = 0;
1257 // send a present request (small set)
1259 if (sr->smallSetElementSetNames)
1261 comp = (Z_RecordComposition *)
1262 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1263 comp->which = Z_RecordComp_simple;
1264 comp->u.simple = sr->smallSetElementSetNames;
1267 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1268 sr->preferredRecordSyntax, comp))
1270 yaz_log (LOG_LOG, "%sReturned cached records for small set",
1272 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1273 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1274 new_apdu->u.searchResponse->resultCount =
1275 &m_client->m_last_resultCount;
1277 new_apdu->u.searchResponse->numberOfRecordsReturned
1278 = odr_intdup(odr_encode(), toget);
1280 new_apdu->u.searchResponse->presentStatus =
1281 odr_intdup(odr_encode(), Z_PresentStatus_success);
1282 new_apdu->u.searchResponse->records = (Z_Records*)
1283 odr_malloc(odr_encode(), sizeof(Z_Records));
1284 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1285 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1286 new_apdu->u.searchResponse->nextResultSetPosition =
1287 odr_intdup(odr_encode(), toget+1);
1288 send_to_client(new_apdu);
1293 yaz_log (LOG_LOG, "%sOptimizing search for small set",
1295 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1296 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1297 pr->referenceId = sr->referenceId;
1298 pr->resultSetId = sr->resultSetName;
1299 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1300 *pr->numberOfRecordsRequested = toget;
1301 pr->recordComposition = comp;
1302 m_client->m_sr_transform = 1;
1307 else // query doesn't match
1309 delete m_client->m_last_query;
1310 m_client->m_last_query = this_query;
1311 m_client->m_last_ok = 0;
1312 m_client->m_cache.clear();
1313 m_client->m_resultSetStartPoint = 0;
1315 xfree (m_client->m_last_resultSetId);
1316 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
1318 m_client->m_last_databases.set(sr->num_databaseNames,
1319 (const char **) sr->databaseNames);
1325 void Yaz_Proxy::inc_request_no()
1327 char *cp = strchr(m_session_str, ' ');
1330 sprintf(cp+1, "%d ", m_request_no);
1333 void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
1337 m_bytes_recv += len;
1339 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
1340 yaz_log (LOG_DEBUG, "%sReceiving %s from client %d bytes",
1341 m_session_str, gdu_name(apdu), len);
1343 if (m_bw_hold_PDU) // double incoming PDU. shutdown now.
1346 m_bw_stat.add_bytes(len);
1347 m_pdu_stat.add_bytes(1);
1349 gettimeofday(&m_time_tv, 0);
1351 int bw_total = m_bw_stat.get_total();
1352 int pdu_total = m_pdu_stat.get_total();
1357 if (bw_total > m_bw_max)
1359 reduce = (bw_total/m_bw_max);
1364 if (pdu_total > m_pdu_max)
1366 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
1367 reduce = (reduce > nreduce) ? reduce : nreduce;
1372 yaz_log(LOG_LOG, "%sdelay=%d bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
1373 m_session_str, reduce, bw_total, pdu_total,
1374 m_bw_max, m_pdu_max);
1376 m_bw_hold_PDU = apdu; // save PDU and signal "on hold"
1377 timeout(reduce); // call us reduce seconds later
1379 else if (apdu->which == Z_GDU_Z3950)
1380 handle_incoming_Z_PDU(apdu->u.z3950);
1381 else if (apdu->which == Z_GDU_HTTP_Request)
1382 handle_incoming_HTTP(apdu->u.HTTP_Request);
1385 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
1387 if (m_max_record_retrieve)
1389 if (apdu->which == Z_APDU_presentRequest)
1391 Z_PresentRequest *pr = apdu->u.presentRequest;
1392 if (pr->numberOfRecordsRequested &&
1393 *pr->numberOfRecordsRequested > m_max_record_retrieve)
1394 *pr->numberOfRecordsRequested = m_max_record_retrieve;
1399 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
1401 const char *addinfo)
1403 Z_Records *rec = (Z_Records *)
1404 odr_malloc (odr, sizeof(*rec));
1406 odr_malloc (odr, sizeof(*err));
1407 Z_DiagRec *drec = (Z_DiagRec *)
1408 odr_malloc (odr, sizeof(*drec));
1409 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1410 odr_malloc (odr, sizeof(*dr));
1412 rec->which = Z_Records_NSD;
1413 rec->u.nonSurrogateDiagnostic = dr;
1414 dr->diagnosticSetId =
1415 yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
1416 dr->condition = err;
1417 dr->which = Z_DefaultDiagFormat_v2Addinfo;
1418 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
1422 Z_APDU *Yaz_Proxy::handle_query_transformation(Z_APDU *apdu)
1424 if (apdu->which == Z_APDU_searchRequest &&
1425 apdu->u.searchRequest->query &&
1426 apdu->u.searchRequest->query->which == Z_Query_type_104 &&
1427 apdu->u.searchRequest->query->u.type_104->which == Z_External_CQL)
1429 Z_RPNQuery *rpnquery = 0;
1430 Z_SearchRequest *sr = apdu->u.searchRequest;
1433 yaz_log(LOG_LOG, "%sCQL: %s", m_session_str,
1434 sr->query->u.type_104->u.cql);
1436 int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
1437 &rpnquery, odr_encode(),
1440 yaz_log(LOG_LOG, "%sNo CQL to RPN table", m_session_str);
1443 yaz_log(LOG_LOG, "%sCQL Conversion error %d", m_session_str, r);
1444 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1446 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1447 new_apdu->u.searchResponse->records =
1448 create_nonSurrogateDiagnostics(odr_encode(),
1449 yaz_diag_srw_to_bib1(r),
1451 *new_apdu->u.searchResponse->searchStatus = 0;
1453 send_to_client(new_apdu);
1459 sr->query->which = Z_Query_type_1;
1460 sr->query->u.type_1 = rpnquery;
1467 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
1469 if (apdu->which == Z_APDU_searchRequest)
1471 Z_SearchRequest *sr = apdu->u.searchRequest;
1475 Yaz_ProxyConfig *cfg = check_reconfigure();
1477 err = cfg->check_query(odr_encode(), m_default_target,
1478 sr->query, &addinfo);
1481 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1483 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1484 new_apdu->u.searchResponse->records =
1485 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1486 *new_apdu->u.searchResponse->searchStatus = 0;
1488 send_to_client(new_apdu);
1496 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
1499 if (apdu->which == Z_APDU_searchRequest)
1501 Z_SearchRequest *sr = apdu->u.searchRequest;
1504 Yaz_ProxyConfig *cfg = check_reconfigure();
1506 Z_RecordComposition rc_temp, *rc = 0;
1507 if (sr->smallSetElementSetNames)
1509 rc_temp.which = Z_RecordComp_simple;
1510 rc_temp.u.simple = sr->smallSetElementSetNames;
1514 char *stylesheet_name = 0;
1516 err = cfg->check_syntax(odr_encode(),
1518 sr->preferredRecordSyntax, rc,
1519 &addinfo, &stylesheet_name, &m_schema);
1520 if (stylesheet_name)
1522 if (m_stylesheet_xsp)
1523 xsltFreeStylesheet(m_stylesheet_xsp);
1524 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1526 m_stylesheet_offset = 0;
1527 xfree(stylesheet_name);
1531 sr->preferredRecordSyntax =
1532 yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN, VAL_USMARC);
1537 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1539 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1540 new_apdu->u.searchResponse->records =
1541 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1542 *new_apdu->u.searchResponse->searchStatus = 0;
1544 send_to_client(new_apdu);
1549 else if (apdu->which == Z_APDU_presentRequest)
1551 Z_PresentRequest *pr = apdu->u.presentRequest;
1554 Yaz_ProxyConfig *cfg = check_reconfigure();
1556 char *stylesheet_name = 0;
1558 err = cfg->check_syntax(odr_encode(), m_default_target,
1559 pr->preferredRecordSyntax,
1560 pr->recordComposition,
1561 &addinfo, &stylesheet_name, &m_schema);
1562 if (stylesheet_name)
1564 if (m_stylesheet_xsp)
1565 xsltFreeStylesheet(m_stylesheet_xsp);
1566 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1568 m_stylesheet_offset = 0;
1569 xfree(stylesheet_name);
1573 pr->preferredRecordSyntax =
1574 yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN, VAL_USMARC);
1579 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1581 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1582 new_apdu->u.presentResponse->records =
1583 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1584 *new_apdu->u.presentResponse->presentStatus =
1585 Z_PresentStatus_failure;
1587 send_to_client(new_apdu);
1595 Z_ElementSetNames *Yaz_Proxy::mk_esn_from_schema(ODR o, const char *schema)
1599 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1600 odr_malloc(o, sizeof(Z_ElementSetNames));
1601 esn->which = Z_ElementSetNames_generic;
1602 esn->u.generic = odr_strdup(o, schema);
1606 void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
1610 odr_destroy(m_s2z_odr_init);
1613 if (m_s2z_odr_search)
1615 odr_destroy(m_s2z_odr_search);
1616 m_s2z_odr_search = 0;
1619 m_http_keepalive = 0;
1621 if (!strcmp(hreq->version, "1.0"))
1623 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1624 if (v && !strcmp(v, "Keep-Alive"))
1625 m_http_keepalive = 1;
1627 m_http_keepalive = 0;
1628 m_http_version = "1.0";
1632 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1633 if (v && !strcmp(v, "close"))
1634 m_http_keepalive = 0;
1636 m_http_keepalive = 1;
1637 m_http_version = "1.1";
1640 Z_SRW_PDU *srw_pdu = 0;
1641 Z_SOAP *soap_package = 0;
1643 Z_SRW_diagnostic *diagnostic = 0;
1644 int num_diagnostic = 0;
1645 if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1647 || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1648 &charset, &diagnostic, &num_diagnostic) == 0)
1650 m_s2z_odr_init = odr_createmem(ODR_ENCODE);
1651 m_s2z_odr_search = odr_createmem(ODR_ENCODE);
1652 m_soap_ns = odr_strdup(m_s2z_odr_search, soap_package->ns);
1653 m_s2z_init_apdu = 0;
1654 m_s2z_search_apdu = 0;
1655 m_s2z_present_apdu = 0;
1657 m_s2z_stylesheet = 0;
1659 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
1661 Z_SRW_searchRetrieveRequest *srw_req = srw_pdu->u.request;
1663 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1664 // recordXPath unsupported.
1665 if (srw_req->recordXPath)
1667 yaz_add_srw_diagnostic(odr_decode(),
1668 &diagnostic, &num_diagnostic,
1671 // must have a query
1672 if (!srw_req->query.cql)
1674 yaz_add_srw_diagnostic(odr_decode(),
1675 &diagnostic, &num_diagnostic,
1679 if (srw_req->sort_type != Z_SRW_sort_type_none)
1681 yaz_add_srw_diagnostic(odr_decode(),
1682 &diagnostic, &num_diagnostic,
1686 if (srw_req->stylesheet)
1688 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1690 // set packing for response records ..
1691 if (srw_req->recordPacking &&
1692 !strcmp(srw_req->recordPacking, "xml"))
1693 m_s2z_packing = Z_SRW_recordPacking_XML;
1695 m_s2z_packing = Z_SRW_recordPacking_string;
1699 Z_SRW_PDU *srw_pdu =
1700 yaz_srw_get(odr_encode(),
1701 Z_SRW_searchRetrieve_response);
1702 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1704 srw_res->diagnostics = diagnostic;
1705 srw_res->num_diagnostics = num_diagnostic;
1706 send_srw_response(srw_pdu);
1710 // prepare search PDU
1711 m_s2z_search_apdu = zget_APDU(m_s2z_odr_search,
1712 Z_APDU_searchRequest);
1713 Z_SearchRequest *z_searchRequest =
1714 m_s2z_search_apdu->u.searchRequest;
1716 z_searchRequest->num_databaseNames = 1;
1717 z_searchRequest->databaseNames = (char**)
1718 odr_malloc(m_s2z_odr_search, sizeof(char *));
1719 z_searchRequest->databaseNames[0] = odr_strdup(m_s2z_odr_search,
1722 // query transformation
1723 Z_Query *query = (Z_Query *)
1724 odr_malloc(m_s2z_odr_search, sizeof(Z_Query));
1725 z_searchRequest->query = query;
1727 if (srw_req->query_type == Z_SRW_query_type_cql)
1729 Z_External *ext = (Z_External *)
1730 odr_malloc(m_s2z_odr_search, sizeof(*ext));
1731 ext->direct_reference =
1732 odr_getoidbystr(m_s2z_odr_search, "1.2.840.10003.16.2");
1733 ext->indirect_reference = 0;
1734 ext->descriptor = 0;
1735 ext->which = Z_External_CQL;
1736 ext->u.cql = srw_req->query.cql;
1738 query->which = Z_Query_type_104;
1739 query->u.type_104 = ext;
1741 else if (srw_req->query_type == Z_SRW_query_type_pqf)
1743 Z_RPNQuery *RPNquery;
1744 YAZ_PQF_Parser pqf_parser;
1746 pqf_parser = yaz_pqf_create ();
1748 RPNquery = yaz_pqf_parse (pqf_parser, m_s2z_odr_search,
1749 srw_req->query.pqf);
1752 const char *pqf_msg;
1754 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1755 yaz_log(LOG_LOG, "%*s^\n", off+4, "");
1756 yaz_log(LOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
1758 send_to_srw_client_error(10, 0);
1761 query->which = Z_Query_type_1;
1762 query->u.type_1 = RPNquery;
1764 yaz_pqf_destroy (pqf_parser);
1768 send_to_srw_client_error(7, "query");
1773 m_s2z_present_apdu = 0;
1775 if (srw_req->maximumRecords)
1776 max = *srw_req->maximumRecords;
1778 if (srw_req->startRecord)
1779 start = *srw_req->startRecord;
1782 // Some backend, such as Voyager doesn't honor piggyback
1783 // So we use present always (0 &&).
1784 if (0 && start <= 1) // Z39.50 piggyback
1786 *z_searchRequest->smallSetUpperBound = max;
1787 *z_searchRequest->mediumSetPresentNumber = max;
1788 *z_searchRequest->largeSetLowerBound = 2000000000; // 2e9
1790 z_searchRequest->preferredRecordSyntax =
1791 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1793 if (srw_req->recordSchema)
1795 z_searchRequest->smallSetElementSetNames =
1796 z_searchRequest->mediumSetElementSetNames =
1797 mk_esn_from_schema(m_s2z_odr_search,
1798 srw_req->recordSchema);
1801 else // Z39.50 present
1803 m_s2z_present_apdu = zget_APDU(m_s2z_odr_search,
1804 Z_APDU_presentRequest);
1805 Z_PresentRequest *z_presentRequest =
1806 m_s2z_present_apdu->u.presentRequest;
1807 *z_presentRequest->resultSetStartPoint = start;
1808 *z_presentRequest->numberOfRecordsRequested = max;
1809 z_presentRequest->preferredRecordSyntax =
1810 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1812 if (srw_req->recordSchema)
1814 z_presentRequest->recordComposition =
1815 (Z_RecordComposition *)
1816 odr_malloc(m_s2z_odr_search,
1817 sizeof(Z_RecordComposition));
1818 z_presentRequest->recordComposition->which =
1819 Z_RecordComp_simple;
1820 z_presentRequest->recordComposition->u.simple =
1821 mk_esn_from_schema(m_s2z_odr_search,
1822 srw_req->recordSchema);
1828 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1829 Z_APDU_initRequest);
1831 // prevent m_initRequest_apdu memory from being grabbed
1832 // in Yaz_Proxy::handle_incoming_Z_PDU
1833 m_initRequest_apdu = m_s2z_init_apdu;
1834 handle_incoming_Z_PDU(m_s2z_init_apdu);
1839 handle_incoming_Z_PDU(m_s2z_search_apdu);
1843 else if (srw_pdu->which == Z_SRW_explain_request)
1845 Z_SRW_explainRequest *srw_req = srw_pdu->u.explain_request;
1847 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1850 if (srw_req->stylesheet)
1852 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1854 if (srw_req->recordPacking &&
1855 !strcmp(srw_req->recordPacking, "xml"))
1856 m_s2z_packing = Z_SRW_recordPacking_XML;
1858 m_s2z_packing = Z_SRW_recordPacking_string;
1862 send_srw_explain_response(diagnostic, num_diagnostic);
1868 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1869 Z_APDU_initRequest);
1871 // prevent m_initRequest_apdu memory from being grabbed
1872 // in Yaz_Proxy::handle_incoming_Z_PDU
1873 m_initRequest_apdu = m_s2z_init_apdu;
1874 handle_incoming_Z_PDU(m_s2z_init_apdu);
1877 send_srw_explain_response(0, 0);
1880 else if (srw_pdu->which == Z_SRW_scan_request)
1882 m_s2z_database = odr_strdup(m_s2z_odr_init,
1883 srw_pdu->u.scan_request->database);
1885 yaz_add_srw_diagnostic(odr_decode(),
1886 &diagnostic, &num_diagnostic,
1888 Z_SRW_PDU *srw_pdu =
1889 yaz_srw_get(odr_encode(),
1890 Z_SRW_scan_response);
1891 Z_SRW_scanResponse *srw_res = srw_pdu->u.scan_response;
1893 srw_res->diagnostics = diagnostic;
1894 srw_res->num_diagnostics = num_diagnostic;
1895 send_srw_response(srw_pdu);
1902 send_to_srw_client_error(4, 0);
1906 Z_GDU *p = z_get_HTTP_Response(odr_encode(), 400);
1911 void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
1913 if (!m_client && m_invalid_session)
1915 m_apdu_invalid_session = apdu;
1916 m_mem_invalid_session = odr_extract_mem(odr_decode());
1917 apdu = m_initRequest_apdu;
1920 // Determine our client.
1921 Z_OtherInformation **oi;
1922 get_otherInfoAPDU(apdu, &oi);
1923 m_client = get_client(apdu, get_cookie(oi), get_proxy(oi));
1929 m_client->m_server = this;
1931 if (apdu->which == Z_APDU_initRequest)
1933 if (apdu->u.initRequest->implementationId)
1934 yaz_log(LOG_LOG, "%simplementationId: %s",
1935 m_session_str, apdu->u.initRequest->implementationId);
1936 if (apdu->u.initRequest->implementationName)
1937 yaz_log(LOG_LOG, "%simplementationName: %s",
1938 m_session_str, apdu->u.initRequest->implementationName);
1939 if (apdu->u.initRequest->implementationVersion)
1940 yaz_log(LOG_LOG, "%simplementationVersion: %s",
1941 m_session_str, apdu->u.initRequest->implementationVersion);
1942 if (m_initRequest_apdu == 0)
1944 if (m_initRequest_mem)
1945 nmem_destroy(m_initRequest_mem);
1946 m_initRequest_apdu = apdu;
1947 m_initRequest_mem = odr_extract_mem(odr_decode());
1949 // save init options for the response..
1950 m_initRequest_options = apdu->u.initRequest->options;
1952 apdu->u.initRequest->options =
1953 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
1954 sizeof(Odr_bitmask));
1955 ODR_MASK_ZERO(apdu->u.initRequest->options);
1957 for (i = 0; i<= 24; i++)
1958 ODR_MASK_SET(apdu->u.initRequest->options, i);
1959 ODR_MASK_CLEAR(apdu->u.initRequest->options,
1960 Z_Options_negotiationModel);
1963 m_initRequest_version = apdu->u.initRequest->protocolVersion;
1964 apdu->u.initRequest->protocolVersion =
1965 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
1966 sizeof(Odr_bitmask));
1967 ODR_MASK_ZERO(apdu->u.initRequest->protocolVersion);
1969 for (i = 0; i<= 8; i++)
1970 ODR_MASK_SET(apdu->u.initRequest->protocolVersion, i);
1972 if (m_client->m_init_flag)
1974 if (handle_init_response_for_invalid_session(apdu))
1976 Z_APDU *apdu2 = m_client->m_initResponse;
1977 apdu2->u.initResponse->otherInfo = 0;
1978 if (m_client->m_cookie && *m_client->m_cookie)
1979 set_otherInformationString(apdu2, VAL_COOKIE, 1,
1980 m_client->m_cookie);
1981 apdu2->u.initResponse->referenceId =
1982 apdu->u.initRequest->referenceId;
1983 apdu2->u.initResponse->options = m_client->m_initResponse_options;
1984 apdu2->u.initResponse->protocolVersion =
1985 m_client->m_initResponse_version;
1987 send_to_client(apdu2);
1990 m_client->m_init_flag = 1;
1992 handle_max_record_retrieve(apdu);
1995 apdu = handle_syntax_validation(apdu);
1998 apdu = handle_query_transformation(apdu);
2001 apdu = handle_query_validation(apdu);
2004 apdu = result_set_optimize(apdu);
2007 m_client->timeout(m_target_idletime); // mark it active even
2008 // though we didn't use it
2012 // delete other info part from PDU before sending to target
2013 get_otherInfoAPDU(apdu, &oi);
2017 if (apdu->which == Z_APDU_presentRequest &&
2018 m_client->m_resultSetStartPoint == 0)
2020 Z_PresentRequest *pr = apdu->u.presentRequest;
2021 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
2022 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
2024 m_client->m_resultSetStartPoint = 0;
2026 if (m_client->send_to_target(apdu) < 0)
2033 m_client->m_waiting = 1;
2036 void Yaz_Proxy::connectNotify()
2040 void Yaz_Proxy::shutdown()
2042 m_invalid_session = 0;
2043 // only keep if keep_alive flag is set...
2045 m_client->m_pdu_recv < m_keepalive_limit_pdu &&
2046 m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
2047 m_client->m_waiting == 0)
2049 yaz_log(LOG_LOG, "%sShutdown (client to proxy) keepalive %s",
2051 m_client->get_hostname());
2052 yaz_log(LOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
2053 m_session_str, m_client->m_pdu_recv,
2054 m_client->m_bytes_sent + m_client->m_bytes_recv,
2055 m_keepalive_limit_bw, m_keepalive_limit_pdu);
2056 assert (m_client->m_waiting != 2);
2057 // Tell client (if any) that no server connection is there..
2058 m_client->m_server = 0;
2059 m_invalid_session = 0;
2063 yaz_log (LOG_LOG, "%sShutdown (client to proxy) close %s",
2065 m_client->get_hostname());
2066 assert (m_client->m_waiting != 2);
2071 yaz_log (LOG_LOG, "%sshutdown (client to proxy) bad state",
2077 yaz_log (LOG_LOG, "%sShutdown (client to proxy)",
2081 m_parent->pre_init();
2085 const char *Yaz_ProxyClient::get_session_str()
2089 return m_server->get_session_str();
2092 void Yaz_ProxyClient::shutdown()
2094 yaz_log (LOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
2100 void Yaz_Proxy::failNotify()
2103 yaz_log (LOG_LOG, "%sConnection closed by client",
2108 void Yaz_ProxyClient::failNotify()
2111 m_server->inc_request_no();
2112 yaz_log (LOG_LOG, "%sConnection closed by target %s",
2113 get_session_str(), get_hostname());
2117 void Yaz_ProxyClient::connectNotify()
2119 const char *s = get_session_str();
2120 const char *h = get_hostname();
2121 yaz_log (LOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
2123 timeout(m_target_idletime);
2128 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
2129 *the_PDU_Observable, int fd)
2131 return new Yaz_ProxyClient(the_PDU_Observable, 0);
2134 Yaz_ProxyClient::~Yaz_ProxyClient()
2139 m_next->m_prev = m_prev;
2140 m_waiting = 2; // for debugging purposes only.
2141 odr_destroy(m_init_odr);
2142 delete m_last_query;
2143 xfree (m_last_resultSetId);
2147 void Yaz_ProxyClient::pre_init_client()
2149 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
2150 Z_InitRequest *req = apdu->u.initRequest;
2153 for (i = 0; i<= 24; i++)
2154 ODR_MASK_SET(req->options, i);
2155 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2156 Z_Options_negotiationModel);
2157 for (i = 0; i<= 10; i++)
2158 ODR_MASK_SET(req->protocolVersion, i);
2160 if (send_to_target(apdu) < 0)
2171 void Yaz_Proxy::pre_init()
2174 const char *name = 0;
2175 const char *zurl_in_use[MAX_ZURL_PLEX];
2176 int limit_bw, limit_pdu, limit_req;
2177 int target_idletime, client_idletime;
2179 int keepalive_limit_bw, keepalive_limit_pdu;
2181 const char *cql2rpn = 0;
2183 Yaz_ProxyConfig *cfg = check_reconfigure();
2187 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
2192 for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
2193 &limit_bw, &limit_pdu, &limit_req,
2194 &target_idletime, &client_idletime,
2196 &keepalive_limit_bw,
2197 &keepalive_limit_pdu,
2204 for (j = 0; zurl_in_use[j]; j++)
2210 for (c = m_clientPool; c; c = c->m_next)
2212 if (!strcmp(zurl_in_use[j], c->get_hostname()))
2214 if (c->m_cookie == 0)
2216 if (c->m_server == 0)
2225 yaz_log(LOG_LOG, "%spre-init %s %s use=%d other=%d spare=%d "
2226 "preinit=%d",m_session_str,
2227 name, zurl_in_use[j], in_use, other, spare, pre_init);
2228 if (spare < pre_init)
2230 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
2231 c->m_next = m_clientPool;
2233 c->m_next->m_prev = &c->m_next;
2235 c->m_prev = &m_clientPool;
2237 if (m_log_mask & PROXY_LOG_APDU_SERVER)
2238 c->set_APDU_yazlog(1);
2240 c->set_APDU_yazlog(0);
2242 if (c->client(zurl_in_use[j]))
2250 c->m_target_idletime = target_idletime;
2251 c->m_seqno = m_seqno++;
2258 void Yaz_Proxy::timeoutNotify()
2264 timeout(m_client_idletime);
2265 Z_GDU *apdu = m_bw_hold_PDU;
2268 if (apdu->which == Z_GDU_Z3950)
2269 handle_incoming_Z_PDU(apdu->u.z3950);
2270 else if (apdu->which == Z_GDU_HTTP_Request)
2271 handle_incoming_HTTP(apdu->u.HTTP_Request);
2273 else if (m_stylesheet_nprl)
2274 convert_xsl_delay();
2279 yaz_log (LOG_LOG, "%sTimeout (client to proxy)", m_session_str);
2290 void Yaz_Proxy::markInvalid()
2293 m_invalid_session = 1;
2296 void Yaz_ProxyClient::timeoutNotify()
2299 m_server->inc_request_no();
2301 yaz_log (LOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
2305 if (m_server && m_init_flag)
2307 // target timed out in a session that was properly initialized
2308 // server object stay alive but we mark it as invalid so it
2309 // gets initialized again
2310 m_server->markInvalid();
2316 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
2317 Yaz_Proxy *parent) :
2318 Yaz_Z_Assoc (the_PDU_Observable)
2325 m_last_resultSetId = 0;
2326 m_last_resultCount = 0;
2330 m_init_odr = odr_createmem (ODR_DECODE);
2332 m_initResponse_options = 0;
2333 m_initResponse_version = 0;
2334 m_resultSetStartPoint = 0;
2335 m_bytes_sent = m_bytes_recv = 0;
2339 m_target_idletime = 600;
2343 const char *Yaz_Proxy::option(const char *name, const char *value)
2345 if (!strcmp (name, "optimize")) {
2348 m_optimize = xstrdup (value);
2355 void Yaz_ProxyClient::recv_HTTP_response(Z_HTTP_Response *apdu, int len)
2360 void Yaz_ProxyClient::recv_GDU(Z_GDU *apdu, int len)
2362 if (apdu->which == Z_GDU_Z3950)
2363 recv_Z_PDU(apdu->u.z3950, len);
2364 else if (apdu->which == Z_GDU_HTTP_Response)
2365 recv_HTTP_response(apdu->u.HTTP_Response, len);
2370 int Yaz_Proxy::handle_init_response_for_invalid_session(Z_APDU *apdu)
2372 if (!m_invalid_session)
2374 m_invalid_session = 0;
2375 handle_incoming_Z_PDU(m_apdu_invalid_session);
2376 assert (m_mem_invalid_session);
2377 nmem_destroy(m_mem_invalid_session);
2378 m_mem_invalid_session = 0;
2382 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
2384 m_bytes_recv += len;
2388 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
2389 yaz_log (LOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
2390 apdu_name(apdu), get_hostname(), len);
2391 if (apdu->which == Z_APDU_initResponse)
2393 if (!m_server) // if this is a pre init session , check for more
2395 NMEM nmem = odr_extract_mem (odr_decode());
2396 odr_reset (m_init_odr);
2397 nmem_transfer (m_init_odr->mem, nmem);
2398 m_initResponse = apdu;
2399 m_initResponse_options = apdu->u.initResponse->options;
2400 m_initResponse_version = apdu->u.initResponse->protocolVersion;
2402 Z_InitResponse *ir = apdu->u.initResponse;
2403 char *im0 = ir->implementationName;
2406 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
2413 strcat(im1, "(YAZ Proxy)");
2414 ir->implementationName = im1;
2416 nmem_destroy (nmem);
2418 if (m_server && m_server->handle_init_response_for_invalid_session(apdu))
2421 if (apdu->which == Z_APDU_searchResponse)
2423 Z_SearchResponse *sr = apdu->u.searchResponse;
2424 m_last_resultCount = *sr->resultCount;
2425 int status = *sr->searchStatus;
2426 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
2430 if (sr->records && sr->records->which == Z_Records_DBOSD)
2432 m_cache.add(odr_decode(),
2433 sr->records->u.databaseOrSurDiagnostics, 1,
2438 if (apdu->which == Z_APDU_presentResponse)
2440 Z_PresentResponse *pr = apdu->u.presentResponse;
2444 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2445 Z_SearchResponse *sr = new_apdu->u.searchResponse;
2446 sr->referenceId = pr->referenceId;
2447 *sr->resultCount = m_last_resultCount;
2448 sr->records = pr->records;
2449 sr->nextResultSetPosition = pr->nextResultSetPosition;
2450 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
2454 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
2456 m_cache.add(odr_decode(),
2457 pr->records->u.databaseOrSurDiagnostics,
2458 m_resultSetStartPoint, -1);
2459 m_resultSetStartPoint = 0;
2463 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
2466 m_server->send_to_client(apdu);
2468 if (apdu->which == Z_APDU_close)
2474 int Yaz_Proxy::server(const char *addr)
2476 int r = Yaz_Z_Assoc::server(addr);
2479 yaz_log(LOG_LOG, "%sStarted proxy " VERSION " on %s", m_session_str, addr);