2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.91 2004-01-15 23:44:58 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>
20 #include <libxslt/xsltutils.h>
21 #include <libxslt/transform.h>
24 static const char *apdu_name(Z_APDU *apdu)
28 case Z_APDU_initRequest:
30 case Z_APDU_initResponse:
31 return "initResponse";
32 case Z_APDU_searchRequest:
33 return "searchRequest";
34 case Z_APDU_searchResponse:
35 return "searchResponse";
36 case Z_APDU_presentRequest:
37 return "presentRequest";
38 case Z_APDU_presentResponse:
39 return "presentResponse";
40 case Z_APDU_deleteResultSetRequest:
41 return "deleteResultSetRequest";
42 case Z_APDU_deleteResultSetResponse:
43 return "deleteResultSetResponse";
44 case Z_APDU_scanRequest:
46 case Z_APDU_scanResponse:
47 return "scanResponse";
48 case Z_APDU_sortRequest:
50 case Z_APDU_sortResponse:
51 return "sortResponse";
52 case Z_APDU_extendedServicesRequest:
53 return "extendedServicesRequest";
54 case Z_APDU_extendedServicesResponse:
55 return "extendedServicesResponse";
62 static const char *gdu_name(Z_GDU *gdu)
67 return apdu_name(gdu->u.z3950);
68 case Z_GDU_HTTP_Request:
69 return "HTTP Request";
70 case Z_GDU_HTTP_Response:
71 return "HTTP Response";
73 return "Unknown request/response";
76 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
78 Yaz_Z_Assoc(the_PDU_Observable), m_bw_stat(60), m_pdu_stat(60)
80 m_PDU_Observable = the_PDU_Observable;
85 m_keepalive_limit_bw = 500000;
86 m_keepalive_limit_pdu = 1000;
89 m_proxy_authentication = 0;
93 m_client_idletime = 600;
94 m_target_idletime = 600;
95 m_optimize = xstrdup ("1");
96 strcpy(m_session_str, "0 ");
98 m_bytes_sent = m_bytes_recv = 0;
102 m_max_record_retrieve = 0;
106 m_invalid_session = 0;
109 m_stylesheet_schema = 0;
110 m_s2z_stylesheet = 0;
113 m_initRequest_apdu = 0;
114 m_initRequest_mem = 0;
115 m_initRequest_options = 0;
116 m_initRequest_version = 0;
117 m_apdu_invalid_session = 0;
118 m_mem_invalid_session = 0;
120 m_s2z_odr_search = 0;
122 m_s2z_search_apdu = 0;
123 m_s2z_present_apdu = 0;
124 m_http_keepalive = 0;
127 m_s2z_packing = Z_SRW_recordPacking_string;
128 m_time_tv.tv_sec = 0;
129 m_time_tv.tv_usec = 0;
132 Yaz_Proxy::~Yaz_Proxy()
134 yaz_log(LOG_LOG, "%sClosed %d/%d sent/recv bytes total", m_session_str,
135 m_bytes_sent, m_bytes_recv);
136 nmem_destroy(m_initRequest_mem);
137 nmem_destroy(m_mem_invalid_session);
138 xfree (m_proxyTarget);
139 xfree (m_default_target);
140 xfree (m_proxy_authentication);
142 xfree (m_stylesheet_schema);
145 odr_destroy(m_s2z_odr_init);
146 if (m_s2z_odr_search)
147 odr_destroy(m_s2z_odr_search);
151 int Yaz_Proxy::set_config(const char *config)
154 m_config = new Yaz_ProxyConfig();
155 xfree(m_config_fname);
156 m_config_fname = xstrdup(config);
157 int r = m_config->read_xml(config);
159 m_config->get_generic_info(&m_log_mask, &m_max_clients);
163 void Yaz_Proxy::set_default_target(const char *target)
165 xfree (m_default_target);
166 m_default_target = 0;
168 m_default_target = (char *) xstrdup (target);
171 void Yaz_Proxy::set_proxy_authentication (const char *auth)
173 xfree (m_proxy_authentication);
174 m_proxy_authentication = 0;
176 m_proxy_authentication = (char *) xstrdup (auth);
179 Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
182 return m_parent->check_reconfigure();
184 Yaz_ProxyConfig *cfg = m_config;
187 yaz_log(LOG_LOG, "reconfigure");
189 if (m_config_fname && cfg)
191 yaz_log(LOG_LOG, "reconfigure config %s", m_config_fname);
192 int r = cfg->read_xml(m_config_fname);
194 yaz_log(LOG_WARN, "reconfigure failed");
198 cfg->get_generic_info(&m_log_mask, &m_max_clients);
202 yaz_log(LOG_LOG, "reconfigure");
208 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
209 *the_PDU_Observable, int fd)
212 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable, this);
213 new_proxy->m_config = 0;
214 new_proxy->m_config_fname = 0;
215 new_proxy->timeout(m_client_idletime);
216 new_proxy->m_target_idletime = m_target_idletime;
217 new_proxy->set_default_target(m_default_target);
218 new_proxy->m_max_clients = m_max_clients;
219 new_proxy->m_log_mask = m_log_mask;
220 new_proxy->set_APDU_log(get_APDU_log());
221 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
222 new_proxy->set_APDU_yazlog(1);
224 new_proxy->set_APDU_yazlog(0);
225 new_proxy->set_proxy_authentication(m_proxy_authentication);
226 sprintf(new_proxy->m_session_str, "%ld:%d ", (long) time(0), m_session_no);
228 yaz_log (LOG_LOG, "%sNew session %s", new_proxy->m_session_str,
229 the_PDU_Observable->getpeername());
233 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
236 Z_OtherInformationUnit *oi;
238 ent.proto = PROTO_Z3950;
239 ent.oclass = CLASS_USERINFO;
240 ent.value = (oid_value) VAL_COOKIE;
241 assert (oid_ent_to_oid (&ent, oid));
243 if (oid_ent_to_oid (&ent, oid) &&
244 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
245 oi->which == Z_OtherInfo_characterInfo)
246 return oi->information.characterInfo;
250 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
253 Z_OtherInformationUnit *oi;
255 ent.proto = PROTO_Z3950;
256 ent.oclass = CLASS_USERINFO;
257 ent.value = (oid_value) VAL_PROXY;
258 if (oid_ent_to_oid (&ent, oid) &&
259 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
260 oi->which == Z_OtherInfo_characterInfo)
261 return oi->information.characterInfo;
265 const char *Yaz_Proxy::load_balance(const char **url)
267 int zurl_in_use[MAX_ZURL_PLEX];
271 for (i = 0; i<MAX_ZURL_PLEX; i++)
273 for (c = m_parent->m_clientPool; c; c = c->m_next)
275 for (i = 0; url[i]; i++)
276 if (!strcmp(url[i], c->get_hostname()))
281 for (i = 0; url[i]; i++)
283 yaz_log(LOG_DEBUG, "%szurl=%s use=%d",
284 m_session_str, url[i], zurl_in_use[i]);
285 if (min > zurl_in_use[i])
288 min = zurl_in_use[i];
294 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
295 const char *proxy_host)
298 Yaz_Proxy *parent = m_parent;
299 Yaz_ProxyClient *c = m_client;
303 const char *url[MAX_ZURL_PLEX];
304 Yaz_ProxyConfig *cfg = check_reconfigure();
308 /* only to be enabled for debugging... */
309 if (!strcmp(proxy_host, "stop"))
312 xfree(m_default_target);
313 m_default_target = xstrdup(proxy_host);
314 proxy_host = m_default_target;
316 int client_idletime = -1;
317 const char *cql2rpn_fname = 0;
318 url[0] = m_default_target;
323 cfg->get_target_info(proxy_host, url, &m_bw_max,
324 &m_pdu_max, &m_max_record_retrieve,
325 &m_target_idletime, &client_idletime,
326 &parent->m_max_clients,
327 &m_keepalive_limit_bw,
328 &m_keepalive_limit_pdu,
332 if (client_idletime != -1)
334 m_client_idletime = client_idletime;
335 timeout(m_client_idletime);
338 m_cql2rpn.set_pqf_file(cql2rpn_fname);
341 yaz_log(LOG_LOG, "%sNo default target", m_session_str);
344 // we don't handle multiplexing for cookie session, so we just
345 // pick the first one in this case (anonymous users will be able
346 // to use any backend)
347 if (cookie && *cookie)
348 m_proxyTarget = (char*) xstrdup(url[0]);
350 m_proxyTarget = (char*) xstrdup(load_balance(url));
352 if (cookie && *cookie)
353 { // search in sessions with a cookie
354 for (c = parent->m_clientPool; c; c = c->m_next)
357 assert (*c->m_prev == c);
358 if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
359 !strcmp(m_proxyTarget, c->get_hostname()))
362 // The following handles "cancel"
363 // If connection is busy (waiting for PDU) and
364 // we have an initRequest we can safely do re-open
365 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
367 yaz_log (LOG_LOG, "%s REOPEN target=%s", m_session_str,
374 c->m_last_resultCount = 0;
375 c->m_sr_transform = 0;
377 c->m_resultSetStartPoint = 0;
378 c->m_target_idletime = m_target_idletime;
379 if (c->client(m_proxyTarget))
386 c->m_seqno = parent->m_seqno;
387 if (c->m_server && c->m_server != this)
388 c->m_server->m_client = 0;
391 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
398 // don't have a client session yet. Search in session w/o cookie
399 for (c = parent->m_clientPool; c; c = c->m_next)
402 assert (*c->m_prev == c);
403 if (c->m_server == 0 && c->m_cookie == 0 &&
405 !strcmp(m_proxyTarget, c->get_hostname()))
408 yaz_log (LOG_LOG, "%sREUSE %s",
409 m_session_str, c->get_hostname());
411 c->m_seqno = parent->m_seqno;
412 assert(c->m_server == 0);
415 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
416 c->set_APDU_yazlog(1);
418 c->set_APDU_yazlog(0);
430 if (apdu->which != Z_APDU_initRequest)
432 yaz_log (LOG_LOG, "%sno init request as first PDU", m_session_str);
435 Z_InitRequest *initRequest = apdu->u.initRequest;
437 if (!initRequest->idAuthentication)
439 if (m_proxy_authentication)
441 initRequest->idAuthentication =
442 (Z_IdAuthentication *)
443 odr_malloc (odr_encode(),
444 sizeof(*initRequest->idAuthentication));
445 initRequest->idAuthentication->which =
446 Z_IdAuthentication_open;
447 initRequest->idAuthentication->u.open =
448 odr_strdup (odr_encode(), m_proxy_authentication);
451 // go through list of clients - and find the lowest/oldest one.
452 Yaz_ProxyClient *c_min = 0;
454 int no_of_clients = 0;
455 if (parent->m_clientPool)
456 yaz_log (LOG_DEBUG, "Existing sessions");
457 for (c = parent->m_clientPool; c; c = c->m_next)
459 yaz_log (LOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
460 c->m_waiting, c->get_hostname(),
461 c->m_cookie ? c->m_cookie : "");
463 if (min_seq < 0 || c->m_seqno < min_seq)
465 min_seq = c->m_seqno;
469 if (no_of_clients >= parent->m_max_clients)
472 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
474 yaz_log (LOG_LOG, "%sMAXCLIENTS Destroy %d",
475 m_session_str, c->m_seqno);
476 if (c->m_server && c->m_server != this)
482 yaz_log (LOG_LOG, "%sMAXCLIENTS Reuse %d %d %s",
484 c->m_seqno, parent->m_seqno, c->get_hostname());
488 c->m_cookie = xstrdup(cookie);
489 c->m_seqno = parent->m_seqno;
490 if (c->m_server && c->m_server != this)
492 c->m_server->m_client = 0;
496 c->m_target_idletime = m_target_idletime;
497 c->timeout(m_target_idletime);
499 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
500 c->set_APDU_yazlog(1);
502 c->set_APDU_yazlog(0);
509 yaz_log (LOG_LOG, "%sNEW %d %s",
510 m_session_str, parent->m_seqno, m_proxyTarget);
511 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), parent);
512 c->m_next = parent->m_clientPool;
514 c->m_next->m_prev = &c->m_next;
515 parent->m_clientPool = c;
516 c->m_prev = &parent->m_clientPool;
522 c->m_cookie = xstrdup(cookie);
524 c->m_seqno = parent->m_seqno;
526 c->m_last_resultCount = 0;
529 c->m_sr_transform = 0;
531 c->m_resultSetStartPoint = 0;
533 if (c->client(m_proxyTarget))
538 c->m_target_idletime = m_target_idletime;
541 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
542 c->set_APDU_yazlog(1);
544 c->set_APDU_yazlog(0);
546 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
550 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
553 for (i = 0; i<num; i++)
556 Z_DefaultDiagFormat *r;
557 Z_DiagRec *p = pp[i];
558 if (p->which != Z_DiagRec_defaultFormat)
560 yaz_log(LOG_LOG, "%sError no diagnostics", m_session_str);
564 r = p->u.defaultFormat;
565 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
566 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
567 yaz_log(LOG_LOG, "%sError unknown diagnostic set", m_session_str);
570 case Z_DefaultDiagFormat_v2Addinfo:
571 yaz_log(LOG_LOG, "%sError %d %s:%s",
573 *r->condition, diagbib1_str(*r->condition),
576 case Z_DefaultDiagFormat_v3Addinfo:
577 yaz_log(LOG_LOG, "%sError %d %s:%s",
579 *r->condition, diagbib1_str(*r->condition),
586 void Yaz_Proxy::convert_xsl(Z_NamePlusRecordList *p)
588 if (!m_stylesheet_schema)
590 xsltStylesheetPtr xsp;
592 xsp = xsltParseStylesheetFile((const xmlChar *) m_stylesheet_schema);
595 for (i = 0; i < p->num_records; i++)
597 Z_NamePlusRecord *npr = p->records[i];
598 if (npr->which == Z_NamePlusRecord_databaseRecord)
600 Z_External *r = npr->u.databaseRecord;
601 if (r->which == Z_External_octet)
603 xmlDocPtr res, doc = xmlParseMemory(
604 (char*) r->u.octet_aligned->buf,
605 r->u.octet_aligned->len);
607 res = xsltApplyStylesheet(xsp, doc, 0);
611 xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
613 p->records[i]->u.databaseRecord =
614 z_ext_record(odr_encode(), VAL_TEXT_XML,
615 (char*) out_buf, out_len);
622 xsltFreeStylesheet(xsp);
625 void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
629 yaz_marc_t mt = yaz_marc_create();
630 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
631 for (i = 0; i < p->num_records; i++)
633 Z_NamePlusRecord *npr = p->records[i];
634 if (npr->which == Z_NamePlusRecord_databaseRecord)
636 Z_External *r = npr->u.databaseRecord;
637 if (r->which == Z_External_octet)
641 if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
642 r->u.octet_aligned->len,
645 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
646 WRBUF wrbuf = wrbuf_alloc();
649 size_t inbytesleft = rlen;
650 const char *inp = result;
651 while (cd && inbytesleft)
653 size_t outbytesleft = sizeof(outbuf);
657 r = yaz_iconv (cd, (char**) &inp,
659 &outp, &outbytesleft);
660 if (r == (size_t) (-1))
662 int e = yaz_iconv_error(cd);
663 if (e != YAZ_ICONV_E2BIG)
665 yaz_log(LOG_WARN, "conversion failure");
669 wrbuf_write(wrbuf, outbuf, outp - outbuf);
674 npr->u.databaseRecord = z_ext_record(odr_encode(),
678 wrbuf_free(wrbuf, 1);
683 yaz_marc_destroy(mt);
686 void Yaz_Proxy::logtime()
688 if (m_time_tv.tv_sec)
691 gettimeofday(&tv, 0);
692 long diff = (tv.tv_sec - m_time_tv.tv_sec)*1000000 +
693 (tv.tv_usec - m_time_tv.tv_usec);
695 yaz_log(LOG_LOG, "%sElapsed %ld.%03ld", m_session_str,
696 diff/1000000, (diff/1000)%1000);
698 m_time_tv.tv_sec = 0;
699 m_time_tv.tv_usec = 0;
702 int Yaz_Proxy::send_http_response(int code)
704 ODR o = odr_encode();
705 Z_GDU *gdu = z_get_HTTP_Response(o, code);
706 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
708 hres->version = odr_strdup(o, m_http_version);
709 m_http_keepalive = 0;
710 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
712 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
716 int r = send_GDU(gdu, &len);
721 int Yaz_Proxy::send_srw_response(Z_SRW_PDU *srw_pdu)
723 ODR o = odr_encode();
724 const char *ctype = "text/xml";
725 Z_GDU *gdu = z_get_HTTP_Response(o, 200);
726 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
728 hres->version = odr_strdup(o, m_http_version);
729 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
730 if (m_http_keepalive)
731 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
733 static Z_SOAP_Handler soap_handlers[2] = {
735 {"http://www.loc.gov/zing/srw/", 0,
736 (Z_SOAP_fun) yaz_srw_codec},
741 Z_SOAP *soap_package = (Z_SOAP*) odr_malloc(o, sizeof(Z_SOAP));
742 soap_package->which = Z_SOAP_generic;
743 soap_package->u.generic =
744 (Z_SOAP_Generic *) odr_malloc(o, sizeof(*soap_package->u.generic));
745 soap_package->u.generic->no = 0;
746 soap_package->u.generic->ns = soap_handlers[0].ns;
747 soap_package->u.generic->p = (void *) srw_pdu;
748 soap_package->ns = m_soap_ns;
749 z_soap_codec_enc_xsl(o, &soap_package,
750 &hres->content_buf, &hres->content_len,
751 soap_handlers, 0, m_s2z_stylesheet);
752 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
754 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
758 int r = send_GDU(gdu, &len);
763 int Yaz_Proxy::send_to_srw_client_error(int srw_error, const char *add)
765 ODR o = odr_encode();
766 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
767 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
769 srw_res->num_diagnostics = 1;
770 srw_res->diagnostics = (Z_SRW_diagnostic *)
771 odr_malloc(o, sizeof(*srw_res->diagnostics));
772 srw_res->diagnostics[0].code = odr_intdup(o, srw_error);
773 srw_res->diagnostics[0].details = add ? odr_strdup(o, add) : 0;
774 return send_srw_response(srw_pdu);
777 int Yaz_Proxy::z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
778 Z_DefaultDiagFormat *ddf)
780 int bib1_code = *ddf->condition;
781 if (bib1_code == 109)
783 srw_res->num_diagnostics = 1;
784 srw_res->diagnostics = (Z_SRW_diagnostic *)
785 odr_malloc(o, sizeof(*srw_res->diagnostics));
786 srw_res->diagnostics[0].code =
787 odr_intdup(o, yaz_diag_bib1_to_srw(*ddf->condition));
788 srw_res->diagnostics[0].details = ddf->u.v2Addinfo;
792 int Yaz_Proxy::send_to_srw_client_ok(int hits, Z_Records *records, int start)
794 ODR o = odr_encode();
795 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
796 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
798 srw_res->numberOfRecords = odr_intdup (o, hits);
799 if (records && records->which == Z_Records_DBOSD)
801 srw_res->num_records =
802 records->u.databaseOrSurDiagnostics->num_records;
804 srw_res->records = (Z_SRW_record *)
805 odr_malloc(o, srw_res->num_records * sizeof(Z_SRW_record));
806 for (i = 0; i < srw_res->num_records; i++)
808 Z_NamePlusRecord *npr = records->u.databaseOrSurDiagnostics->records[i];
809 if (npr->which != Z_NamePlusRecord_databaseRecord)
811 srw_res->records[i].recordSchema = "diagnostic";
812 srw_res->records[i].recordPacking = m_s2z_packing;
813 srw_res->records[i].recordData_buf = "67";
814 srw_res->records[i].recordData_len = 2;
815 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
818 Z_External *r = npr->u.databaseRecord;
819 oident *ent = oid_getentbyoid(r->direct_reference);
820 if (r->which == Z_External_octet && ent->value == VAL_TEXT_XML)
822 srw_res->records[i].recordSchema = m_schema;
823 srw_res->records[i].recordPacking = m_s2z_packing;
824 srw_res->records[i].recordData_buf = (char*)
825 r->u.octet_aligned->buf;
826 srw_res->records[i].recordData_len = r->u.octet_aligned->len;
827 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
831 srw_res->records[i].recordSchema = "diagnostic";
832 srw_res->records[i].recordPacking = m_s2z_packing;
833 srw_res->records[i].recordData_buf = "67";
834 srw_res->records[i].recordData_len = 2;
835 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
839 if (records && records->which == Z_Records_NSD)
842 http_code = z_to_srw_diag(odr_encode(), srw_res,
843 records->u.nonSurrogateDiagnostic);
845 return send_http_response(http_code);
847 return send_srw_response(srw_pdu);
851 int Yaz_Proxy::send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
854 Yaz_ProxyConfig *cfg = check_reconfigure();
858 char *b = cfg->get_explain(odr_encode(), 0 /* target */,
859 m_s2z_database, &len);
862 Z_SRW_PDU *res = yaz_srw_get(odr_encode(), Z_SRW_explain_response);
863 Z_SRW_explainResponse *er = res->u.explain_response;
865 er->record.recordData_buf = b;
866 er->record.recordData_len = len;
867 er->record.recordPacking = m_s2z_packing;
869 er->diagnostics = diagnostics;
870 er->num_diagnostics = num_diagnostics;
871 return send_srw_response(res);
874 return send_http_response(404);
877 int Yaz_Proxy::send_PDU_convert(Z_APDU *apdu, int *len)
881 if (apdu->which == Z_APDU_initResponse)
883 Z_InitResponse *res = apdu->u.initResponse;
884 if (*res->result == 0)
886 send_to_srw_client_error(3, 0);
888 else if (!m_s2z_search_apdu)
890 send_srw_explain_response(0, 0);
894 handle_incoming_Z_PDU(m_s2z_search_apdu);
897 else if (m_s2z_search_apdu && apdu->which == Z_APDU_searchResponse)
899 m_s2z_search_apdu = 0;
900 Z_SearchResponse *res = apdu->u.searchResponse;
901 m_s2z_hit_count = *res->resultCount;
902 if (res->records && res->records->which == Z_Records_NSD)
904 send_to_srw_client_ok(0, res->records, 1);
906 else if (m_s2z_present_apdu && m_s2z_hit_count > 0)
909 Z_PresentRequest *pr = m_s2z_present_apdu->u.presentRequest;
911 if (*pr->resultSetStartPoint <= m_s2z_hit_count)
913 if (*pr->numberOfRecordsRequested+ *pr->resultSetStartPoint
915 *pr->numberOfRecordsRequested =
916 1 + m_s2z_hit_count - *pr->resultSetStartPoint;
918 handle_incoming_Z_PDU(m_s2z_present_apdu);
922 m_s2z_present_apdu = 0;
923 send_to_srw_client_ok(m_s2z_hit_count, res->records, 1);
926 else if (m_s2z_present_apdu && apdu->which == Z_APDU_presentResponse)
929 *m_s2z_present_apdu->u.presentRequest->resultSetStartPoint;
931 m_s2z_present_apdu = 0;
932 Z_PresentResponse *res = apdu->u.presentResponse;
933 send_to_srw_client_ok(m_s2z_hit_count, res->records, start);
938 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
939 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
941 int r = send_Z_PDU(apdu, len);
948 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
951 int kill_session = 0;
952 if (apdu->which == Z_APDU_searchResponse)
954 Z_SearchResponse *sr = apdu->u.searchResponse;
955 Z_Records *p = sr->records;
956 if (p && p->which == Z_Records_NSD)
958 Z_DiagRec dr, *dr_p = &dr;
959 dr.which = Z_DiagRec_defaultFormat;
960 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
962 *sr->searchStatus = 0;
963 display_diagrecs(&dr_p, 1);
967 if (p && p->which == Z_Records_DBOSD)
970 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
971 convert_xsl(p->u.databaseOrSurDiagnostics);
975 yaz_log(LOG_LOG, "%s%d hits", m_session_str,
977 if (*sr->resultCount < 0)
979 m_invalid_session = 1;
982 *sr->searchStatus = 0;
984 create_nonSurrogateDiagnostics(odr_encode(), 2, 0);
985 *sr->resultCount = 0;
990 else if (apdu->which == Z_APDU_presentResponse)
992 Z_PresentResponse *sr = apdu->u.presentResponse;
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;
999 if (*sr->presentStatus == Z_PresentStatus_success)
1000 *sr->presentStatus = Z_PresentStatus_failure;
1001 display_diagrecs(&dr_p, 1);
1003 if (p && p->which == Z_Records_DBOSD)
1006 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1007 convert_xsl(p->u.databaseOrSurDiagnostics);
1010 else if (apdu->which == Z_APDU_initResponse)
1012 if (m_initRequest_options)
1015 (Odr_bitmask *)odr_malloc(odr_encode(),
1016 sizeof(Odr_bitmask));
1017 ODR_MASK_ZERO(nopt);
1020 for (i = 0; i<24; i++)
1021 if (ODR_MASK_GET(m_initRequest_options, i) &&
1022 ODR_MASK_GET(apdu->u.initResponse->options, i))
1023 ODR_MASK_SET(nopt, i);
1024 apdu->u.initResponse->options = nopt;
1026 if (m_initRequest_version)
1028 Z_ProtocolVersion *nopt =
1029 (Odr_bitmask *)odr_malloc(odr_encode(),
1030 sizeof(Odr_bitmask));
1031 ODR_MASK_ZERO(nopt);
1034 for (i = 0; i<8; i++)
1035 if (ODR_MASK_GET(m_initRequest_version, i) &&
1036 ODR_MASK_GET(apdu->u.initResponse->protocolVersion, i))
1037 ODR_MASK_SET(nopt, i);
1038 apdu->u.initResponse->protocolVersion = nopt;
1041 int r = send_PDU_convert(apdu, &len);
1044 m_bytes_sent += len;
1045 m_bw_stat.add_bytes(len);
1050 m_parent->pre_init();
1054 if (!m_http_keepalive)
1067 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
1070 const char *apdu_name_tmp = apdu_name(apdu);
1071 int r = send_Z_PDU(apdu, &len);
1072 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
1073 yaz_log (LOG_LOG, "%sSending %s to %s %d bytes",
1075 apdu_name_tmp, get_hostname(), len);
1076 m_bytes_sent += len;
1080 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
1082 if (apdu->which == Z_APDU_presentRequest)
1084 Z_PresentRequest *pr = apdu->u.presentRequest;
1085 int toget = *pr->numberOfRecordsRequested;
1086 int start = *pr->resultSetStartPoint;
1088 yaz_log(LOG_LOG, "%sPresent %s %d+%d", m_session_str,
1089 pr->resultSetId, start, toget);
1091 if (*m_parent->m_optimize == '0')
1094 if (!m_client->m_last_resultSetId)
1096 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1097 new_apdu->u.presentResponse->records =
1098 create_nonSurrogateDiagnostics(odr_encode(), 30,
1100 send_to_client(new_apdu);
1103 if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
1105 if (start+toget-1 > m_client->m_last_resultCount)
1107 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1108 new_apdu->u.presentResponse->records =
1109 create_nonSurrogateDiagnostics(odr_encode(), 13, 0);
1110 send_to_client(new_apdu);
1113 Z_NamePlusRecordList *npr;
1114 if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
1115 pr->preferredRecordSyntax,
1116 pr->recordComposition))
1118 yaz_log (LOG_LOG, "%sReturned cached records for present request",
1120 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1121 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1123 new_apdu->u.presentResponse->numberOfRecordsReturned
1124 = odr_intdup(odr_encode(), toget);
1126 new_apdu->u.presentResponse->records = (Z_Records*)
1127 odr_malloc(odr_encode(), sizeof(Z_Records));
1128 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
1129 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
1130 new_apdu->u.presentResponse->nextResultSetPosition =
1131 odr_intdup(odr_encode(), start+toget);
1133 send_to_client(new_apdu);
1139 if (apdu->which != Z_APDU_searchRequest)
1141 Z_SearchRequest *sr = apdu->u.searchRequest;
1142 Yaz_Z_Query *this_query = new Yaz_Z_Query;
1143 Yaz_Z_Databases this_databases;
1145 this_databases.set(sr->num_databaseNames, (const char **)
1148 this_query->set_Z_Query(sr->query);
1150 char query_str[120];
1151 this_query->print(query_str, sizeof(query_str)-1);
1152 yaz_log(LOG_LOG, "%sSearch %s", m_session_str, query_str);
1154 if (*m_parent->m_optimize != '0' &&
1155 m_client->m_last_ok && m_client->m_last_query &&
1156 m_client->m_last_query->match(this_query) &&
1157 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
1158 m_client->m_last_databases.match(this_databases))
1161 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
1162 m_client->m_last_resultCount < *sr->largeSetLowerBound)
1164 Z_NamePlusRecordList *npr;
1165 int toget = *sr->mediumSetPresentNumber;
1166 Z_RecordComposition *comp = 0;
1168 if (toget > m_client->m_last_resultCount)
1169 toget = m_client->m_last_resultCount;
1171 if (sr->mediumSetElementSetNames)
1173 comp = (Z_RecordComposition *)
1174 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1175 comp->which = Z_RecordComp_simple;
1176 comp->u.simple = sr->mediumSetElementSetNames;
1179 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1180 sr->preferredRecordSyntax, comp))
1182 yaz_log (LOG_LOG, "%sReturned cached records for medium set",
1184 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1185 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1186 new_apdu->u.searchResponse->resultCount =
1187 &m_client->m_last_resultCount;
1189 new_apdu->u.searchResponse->numberOfRecordsReturned
1190 = odr_intdup(odr_encode(), toget);
1192 new_apdu->u.searchResponse->presentStatus =
1193 odr_intdup(odr_encode(), Z_PresentStatus_success);
1194 new_apdu->u.searchResponse->records = (Z_Records*)
1195 odr_malloc(odr_encode(), sizeof(Z_Records));
1196 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1197 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1198 new_apdu->u.searchResponse->nextResultSetPosition =
1199 odr_intdup(odr_encode(), toget+1);
1200 send_to_client(new_apdu);
1206 // send present request (medium size)
1207 yaz_log (LOG_LOG, "%sOptimizing search for medium set",
1210 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1211 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1212 pr->referenceId = sr->referenceId;
1213 pr->resultSetId = sr->resultSetName;
1214 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1215 *pr->numberOfRecordsRequested = toget;
1216 pr->recordComposition = comp;
1217 m_client->m_sr_transform = 1;
1221 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
1222 m_client->m_last_resultCount <= 0)
1224 // large set. Return pseudo-search response immediately
1225 yaz_log (LOG_LOG, "%sOptimizing search for large set",
1227 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1228 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1229 new_apdu->u.searchResponse->resultCount =
1230 &m_client->m_last_resultCount;
1231 send_to_client(new_apdu);
1236 Z_NamePlusRecordList *npr;
1237 int toget = m_client->m_last_resultCount;
1238 Z_RecordComposition *comp = 0;
1240 // send a present request (small set)
1242 if (sr->smallSetElementSetNames)
1244 comp = (Z_RecordComposition *)
1245 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1246 comp->which = Z_RecordComp_simple;
1247 comp->u.simple = sr->smallSetElementSetNames;
1250 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1251 sr->preferredRecordSyntax, comp))
1253 yaz_log (LOG_LOG, "%sReturned cached records for small set",
1255 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1256 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1257 new_apdu->u.searchResponse->resultCount =
1258 &m_client->m_last_resultCount;
1260 new_apdu->u.searchResponse->numberOfRecordsReturned
1261 = odr_intdup(odr_encode(), toget);
1263 new_apdu->u.searchResponse->presentStatus =
1264 odr_intdup(odr_encode(), Z_PresentStatus_success);
1265 new_apdu->u.searchResponse->records = (Z_Records*)
1266 odr_malloc(odr_encode(), sizeof(Z_Records));
1267 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1268 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1269 new_apdu->u.searchResponse->nextResultSetPosition =
1270 odr_intdup(odr_encode(), toget+1);
1271 send_to_client(new_apdu);
1276 yaz_log (LOG_LOG, "%sOptimizing search for small set",
1278 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1279 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1280 pr->referenceId = sr->referenceId;
1281 pr->resultSetId = sr->resultSetName;
1282 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1283 *pr->numberOfRecordsRequested = toget;
1284 pr->recordComposition = comp;
1285 m_client->m_sr_transform = 1;
1290 else // query doesn't match
1292 delete m_client->m_last_query;
1293 m_client->m_last_query = this_query;
1294 m_client->m_last_ok = 0;
1295 m_client->m_cache.clear();
1296 m_client->m_resultSetStartPoint = 0;
1298 xfree (m_client->m_last_resultSetId);
1299 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
1301 m_client->m_last_databases.set(sr->num_databaseNames,
1302 (const char **) sr->databaseNames);
1308 void Yaz_Proxy::inc_request_no()
1310 char *cp = strchr(m_session_str, ' ');
1313 sprintf(cp+1, "%d ", m_request_no);
1316 void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
1320 m_bytes_recv += len;
1322 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
1323 yaz_log (LOG_DEBUG, "%sReceiving %s from client %d bytes",
1324 m_session_str, gdu_name(apdu), len);
1326 if (m_bw_hold_PDU) // double incoming PDU. shutdown now.
1329 m_bw_stat.add_bytes(len);
1330 m_pdu_stat.add_bytes(1);
1332 gettimeofday(&m_time_tv, 0);
1334 int bw_total = m_bw_stat.get_total();
1335 int pdu_total = m_pdu_stat.get_total();
1340 if (bw_total > m_bw_max)
1342 reduce = (bw_total/m_bw_max);
1347 if (pdu_total > m_pdu_max)
1349 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
1350 reduce = (reduce > nreduce) ? reduce : nreduce;
1355 yaz_log(LOG_LOG, "%sdelay=%d bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
1356 m_session_str, reduce, bw_total, pdu_total,
1357 m_bw_max, m_pdu_max);
1359 m_bw_hold_PDU = apdu; // save PDU and signal "on hold"
1360 timeout(reduce); // call us reduce seconds later
1362 else if (apdu->which == Z_GDU_Z3950)
1363 handle_incoming_Z_PDU(apdu->u.z3950);
1364 else if (apdu->which == Z_GDU_HTTP_Request)
1365 handle_incoming_HTTP(apdu->u.HTTP_Request);
1368 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
1370 if (m_max_record_retrieve)
1372 if (apdu->which == Z_APDU_presentRequest)
1374 Z_PresentRequest *pr = apdu->u.presentRequest;
1375 if (pr->numberOfRecordsRequested &&
1376 *pr->numberOfRecordsRequested > m_max_record_retrieve)
1377 *pr->numberOfRecordsRequested = m_max_record_retrieve;
1382 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
1384 const char *addinfo)
1386 Z_Records *rec = (Z_Records *)
1387 odr_malloc (odr, sizeof(*rec));
1389 odr_malloc (odr, sizeof(*err));
1390 Z_DiagRec *drec = (Z_DiagRec *)
1391 odr_malloc (odr, sizeof(*drec));
1392 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1393 odr_malloc (odr, sizeof(*dr));
1395 rec->which = Z_Records_NSD;
1396 rec->u.nonSurrogateDiagnostic = dr;
1397 dr->diagnosticSetId =
1398 yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
1399 dr->condition = err;
1400 dr->which = Z_DefaultDiagFormat_v2Addinfo;
1401 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
1405 Z_APDU *Yaz_Proxy::handle_query_transformation(Z_APDU *apdu)
1407 if (apdu->which == Z_APDU_searchRequest &&
1408 apdu->u.searchRequest->query &&
1409 apdu->u.searchRequest->query->which == Z_Query_type_104 &&
1410 apdu->u.searchRequest->query->u.type_104->which == Z_External_CQL)
1412 Z_RPNQuery *rpnquery = 0;
1413 Z_SearchRequest *sr = apdu->u.searchRequest;
1416 yaz_log(LOG_LOG, "%sCQL: %s", m_session_str,
1417 sr->query->u.type_104->u.cql);
1419 int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
1420 &rpnquery, odr_encode(),
1423 yaz_log(LOG_LOG, "%sNo CQL to RPN table", m_session_str);
1426 yaz_log(LOG_LOG, "%sCQL Conversion error %d", m_session_str, r);
1427 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1429 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1430 new_apdu->u.searchResponse->records =
1431 create_nonSurrogateDiagnostics(odr_encode(),
1432 yaz_diag_srw_to_bib1(r),
1434 *new_apdu->u.searchResponse->searchStatus = 0;
1436 send_to_client(new_apdu);
1442 sr->query->which = Z_Query_type_1;
1443 sr->query->u.type_1 = rpnquery;
1450 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
1452 if (apdu->which == Z_APDU_searchRequest)
1454 Z_SearchRequest *sr = apdu->u.searchRequest;
1458 Yaz_ProxyConfig *cfg = check_reconfigure();
1460 err = cfg->check_query(odr_encode(), m_default_target,
1461 sr->query, &addinfo);
1464 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1466 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1467 new_apdu->u.searchResponse->records =
1468 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1469 *new_apdu->u.searchResponse->searchStatus = 0;
1471 send_to_client(new_apdu);
1479 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
1482 if (apdu->which == Z_APDU_searchRequest)
1484 Z_SearchRequest *sr = apdu->u.searchRequest;
1487 Yaz_ProxyConfig *cfg = check_reconfigure();
1489 Z_RecordComposition rc_temp, *rc = 0;
1490 if (sr->smallSetElementSetNames)
1492 rc_temp.which = Z_RecordComp_simple;
1493 rc_temp.u.simple = sr->smallSetElementSetNames;
1498 err = cfg->check_syntax(odr_encode(),
1500 sr->preferredRecordSyntax, rc,
1501 &addinfo, &m_stylesheet_schema, &m_schema);
1504 sr->preferredRecordSyntax =
1505 yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN, VAL_USMARC);
1510 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1512 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1513 new_apdu->u.searchResponse->records =
1514 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1515 *new_apdu->u.searchResponse->searchStatus = 0;
1517 send_to_client(new_apdu);
1522 else if (apdu->which == Z_APDU_presentRequest)
1524 Z_PresentRequest *pr = apdu->u.presentRequest;
1527 Yaz_ProxyConfig *cfg = check_reconfigure();
1530 err = cfg->check_syntax(odr_encode(), m_default_target,
1531 pr->preferredRecordSyntax,
1532 pr->recordComposition,
1533 &addinfo, &m_stylesheet_schema, &m_schema);
1536 pr->preferredRecordSyntax =
1537 yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN, VAL_USMARC);
1542 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1544 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1545 new_apdu->u.presentResponse->records =
1546 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1547 *new_apdu->u.presentResponse->presentStatus =
1548 Z_PresentStatus_failure;
1550 send_to_client(new_apdu);
1558 Z_ElementSetNames *Yaz_Proxy::mk_esn_from_schema(ODR o, const char *schema)
1562 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1563 odr_malloc(o, sizeof(Z_ElementSetNames));
1564 esn->which = Z_ElementSetNames_generic;
1565 esn->u.generic = odr_strdup(o, schema);
1569 void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
1573 odr_destroy(m_s2z_odr_init);
1576 if (m_s2z_odr_search)
1578 odr_destroy(m_s2z_odr_search);
1579 m_s2z_odr_search = 0;
1582 m_http_keepalive = 0;
1584 if (!strcmp(hreq->version, "1.0"))
1586 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1587 if (v && !strcmp(v, "Keep-Alive"))
1588 m_http_keepalive = 1;
1590 m_http_keepalive = 0;
1591 m_http_version = "1.0";
1595 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1596 if (v && !strcmp(v, "close"))
1597 m_http_keepalive = 0;
1599 m_http_keepalive = 1;
1600 m_http_version = "1.1";
1603 Z_SRW_PDU *srw_pdu = 0;
1604 Z_SOAP *soap_package = 0;
1606 Z_SRW_diagnostic *diagnostic = 0;
1607 int num_diagnostic = 0;
1608 if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1610 || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1611 &charset, &diagnostic, &num_diagnostic) == 0)
1613 m_s2z_odr_init = odr_createmem(ODR_ENCODE);
1614 m_s2z_odr_search = odr_createmem(ODR_ENCODE);
1615 m_soap_ns = odr_strdup(m_s2z_odr_search, soap_package->ns);
1616 m_s2z_init_apdu = 0;
1617 m_s2z_search_apdu = 0;
1618 m_s2z_present_apdu = 0;
1620 m_s2z_stylesheet = 0;
1622 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
1624 Z_SRW_searchRetrieveRequest *srw_req = srw_pdu->u.request;
1626 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1627 // recordXPath unsupported.
1628 if (srw_req->recordXPath)
1630 yaz_add_srw_diagnostic(odr_decode(),
1631 &diagnostic, &num_diagnostic,
1634 // must have a query
1635 if (!srw_req->query.cql)
1637 yaz_add_srw_diagnostic(odr_decode(),
1638 &diagnostic, &num_diagnostic,
1642 if (srw_req->sort_type != Z_SRW_sort_type_none)
1644 yaz_add_srw_diagnostic(odr_decode(),
1645 &diagnostic, &num_diagnostic,
1649 if (srw_req->stylesheet)
1651 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1653 // set packing for response records ..
1654 if (srw_req->recordPacking &&
1655 !strcmp(srw_req->recordPacking, "xml"))
1656 m_s2z_packing = Z_SRW_recordPacking_XML;
1658 m_s2z_packing = Z_SRW_recordPacking_string;
1662 Z_SRW_PDU *srw_pdu =
1663 yaz_srw_get(odr_encode(),
1664 Z_SRW_searchRetrieve_response);
1665 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1667 srw_res->diagnostics = diagnostic;
1668 srw_res->num_diagnostics = num_diagnostic;
1669 send_srw_response(srw_pdu);
1673 // prepare search PDU
1674 m_s2z_search_apdu = zget_APDU(m_s2z_odr_search,
1675 Z_APDU_searchRequest);
1676 Z_SearchRequest *z_searchRequest =
1677 m_s2z_search_apdu->u.searchRequest;
1679 z_searchRequest->num_databaseNames = 1;
1680 z_searchRequest->databaseNames = (char**)
1681 odr_malloc(m_s2z_odr_search, sizeof(char *));
1682 z_searchRequest->databaseNames[0] = odr_strdup(m_s2z_odr_search,
1685 // query transformation
1686 Z_Query *query = (Z_Query *)
1687 odr_malloc(m_s2z_odr_search, sizeof(Z_Query));
1688 z_searchRequest->query = query;
1690 if (srw_req->query_type == Z_SRW_query_type_cql)
1692 Z_External *ext = (Z_External *)
1693 odr_malloc(m_s2z_odr_search, sizeof(*ext));
1694 ext->direct_reference =
1695 odr_getoidbystr(m_s2z_odr_search, "1.2.840.10003.16.2");
1696 ext->indirect_reference = 0;
1697 ext->descriptor = 0;
1698 ext->which = Z_External_CQL;
1699 ext->u.cql = srw_req->query.cql;
1701 query->which = Z_Query_type_104;
1702 query->u.type_104 = ext;
1704 else if (srw_req->query_type == Z_SRW_query_type_pqf)
1706 Z_RPNQuery *RPNquery;
1707 YAZ_PQF_Parser pqf_parser;
1709 pqf_parser = yaz_pqf_create ();
1711 RPNquery = yaz_pqf_parse (pqf_parser, m_s2z_odr_search,
1712 srw_req->query.pqf);
1715 const char *pqf_msg;
1717 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1718 yaz_log(LOG_LOG, "%*s^\n", off+4, "");
1719 yaz_log(LOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
1721 send_to_srw_client_error(10, 0);
1724 query->which = Z_Query_type_1;
1725 query->u.type_1 = RPNquery;
1727 yaz_pqf_destroy (pqf_parser);
1731 send_to_srw_client_error(7, "query");
1736 m_s2z_present_apdu = 0;
1738 if (srw_req->maximumRecords)
1739 max = *srw_req->maximumRecords;
1741 if (srw_req->startRecord)
1742 start = *srw_req->startRecord;
1745 // Some backend, such as Voyager doesn't honor piggyback
1746 // So we use present always (0 &&).
1747 if (0 && start <= 1) // Z39.50 piggyback
1749 *z_searchRequest->smallSetUpperBound = max;
1750 *z_searchRequest->mediumSetPresentNumber = max;
1751 *z_searchRequest->largeSetLowerBound = 2000000000; // 2e9
1753 z_searchRequest->preferredRecordSyntax =
1754 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1756 if (srw_req->recordSchema)
1758 z_searchRequest->smallSetElementSetNames =
1759 z_searchRequest->mediumSetElementSetNames =
1760 mk_esn_from_schema(m_s2z_odr_search,
1761 srw_req->recordSchema);
1764 else // Z39.50 present
1766 m_s2z_present_apdu = zget_APDU(m_s2z_odr_search,
1767 Z_APDU_presentRequest);
1768 Z_PresentRequest *z_presentRequest =
1769 m_s2z_present_apdu->u.presentRequest;
1770 *z_presentRequest->resultSetStartPoint = start;
1771 *z_presentRequest->numberOfRecordsRequested = max;
1772 z_presentRequest->preferredRecordSyntax =
1773 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1775 z_presentRequest->recordComposition =
1776 (Z_RecordComposition *)
1777 odr_malloc(m_s2z_odr_search,
1778 sizeof(Z_RecordComposition));
1779 if (srw_req->recordSchema)
1781 z_presentRequest->recordComposition->which =
1782 Z_RecordComp_simple;
1783 z_presentRequest->recordComposition->u.simple =
1784 mk_esn_from_schema(m_s2z_odr_search,
1785 srw_req->recordSchema);
1791 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1792 Z_APDU_initRequest);
1794 // prevent m_initRequest_apdu memory from being grabbed
1795 // in Yaz_Proxy::handle_incoming_Z_PDU
1796 m_initRequest_apdu = m_s2z_init_apdu;
1797 handle_incoming_Z_PDU(m_s2z_init_apdu);
1802 handle_incoming_Z_PDU(m_s2z_search_apdu);
1806 else if (srw_pdu->which == Z_SRW_explain_request)
1808 Z_SRW_explainRequest *srw_req = srw_pdu->u.explain_request;
1810 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1813 if (srw_req->stylesheet)
1815 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1817 if (srw_req->recordPacking &&
1818 !strcmp(srw_req->recordPacking, "xml"))
1819 m_s2z_packing = Z_SRW_recordPacking_XML;
1821 m_s2z_packing = Z_SRW_recordPacking_string;
1825 send_srw_explain_response(diagnostic, num_diagnostic);
1831 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1832 Z_APDU_initRequest);
1834 // prevent m_initRequest_apdu memory from being grabbed
1835 // in Yaz_Proxy::handle_incoming_Z_PDU
1836 m_initRequest_apdu = m_s2z_init_apdu;
1837 handle_incoming_Z_PDU(m_s2z_init_apdu);
1840 send_srw_explain_response(0, 0);
1843 else if (srw_pdu->which == Z_SRW_scan_request)
1845 m_s2z_database = odr_strdup(m_s2z_odr_init,
1846 srw_pdu->u.scan_request->database);
1848 yaz_add_srw_diagnostic(odr_decode(),
1849 &diagnostic, &num_diagnostic,
1851 Z_SRW_PDU *srw_pdu =
1852 yaz_srw_get(odr_encode(),
1853 Z_SRW_scan_response);
1854 Z_SRW_scanResponse *srw_res = srw_pdu->u.scan_response;
1856 srw_res->diagnostics = diagnostic;
1857 srw_res->num_diagnostics = num_diagnostic;
1858 send_srw_response(srw_pdu);
1865 send_to_srw_client_error(4, 0);
1869 Z_GDU *p = z_get_HTTP_Response(odr_encode(), 400);
1874 void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
1876 if (!m_client && m_invalid_session)
1878 m_apdu_invalid_session = apdu;
1879 m_mem_invalid_session = odr_extract_mem(odr_decode());
1880 apdu = m_initRequest_apdu;
1883 // Determine our client.
1884 Z_OtherInformation **oi;
1885 get_otherInfoAPDU(apdu, &oi);
1886 m_client = get_client(apdu, get_cookie(oi), get_proxy(oi));
1892 m_client->m_server = this;
1894 if (apdu->which == Z_APDU_initRequest)
1896 if (apdu->u.initRequest->implementationId)
1897 yaz_log(LOG_LOG, "%simplementationId: %s",
1898 m_session_str, apdu->u.initRequest->implementationId);
1899 if (apdu->u.initRequest->implementationName)
1900 yaz_log(LOG_LOG, "%simplementationName: %s",
1901 m_session_str, apdu->u.initRequest->implementationName);
1902 if (apdu->u.initRequest->implementationVersion)
1903 yaz_log(LOG_LOG, "%simplementationVersion: %s",
1904 m_session_str, apdu->u.initRequest->implementationVersion);
1905 if (m_initRequest_apdu == 0)
1907 if (m_initRequest_mem)
1908 nmem_destroy(m_initRequest_mem);
1909 m_initRequest_apdu = apdu;
1910 m_initRequest_mem = odr_extract_mem(odr_decode());
1912 // save init options for the response..
1913 m_initRequest_options = apdu->u.initRequest->options;
1915 apdu->u.initRequest->options =
1916 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
1917 sizeof(Odr_bitmask));
1918 ODR_MASK_ZERO(apdu->u.initRequest->options);
1920 for (i = 0; i<= 24; i++)
1921 ODR_MASK_SET(apdu->u.initRequest->options, i);
1922 ODR_MASK_CLEAR(apdu->u.initRequest->options,
1923 Z_Options_negotiationModel);
1926 m_initRequest_version = apdu->u.initRequest->protocolVersion;
1927 apdu->u.initRequest->protocolVersion =
1928 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
1929 sizeof(Odr_bitmask));
1930 ODR_MASK_ZERO(apdu->u.initRequest->protocolVersion);
1932 for (i = 0; i<= 8; i++)
1933 ODR_MASK_SET(apdu->u.initRequest->protocolVersion, i);
1935 if (m_client->m_init_flag)
1937 if (handle_init_response_for_invalid_session(apdu))
1939 Z_APDU *apdu2 = m_client->m_initResponse;
1940 apdu2->u.initResponse->otherInfo = 0;
1941 if (m_client->m_cookie && *m_client->m_cookie)
1942 set_otherInformationString(apdu2, VAL_COOKIE, 1,
1943 m_client->m_cookie);
1944 apdu2->u.initResponse->referenceId =
1945 apdu->u.initRequest->referenceId;
1946 apdu2->u.initResponse->options = m_client->m_initResponse_options;
1947 apdu2->u.initResponse->protocolVersion =
1948 m_client->m_initResponse_version;
1950 send_to_client(apdu2);
1953 m_client->m_init_flag = 1;
1955 handle_max_record_retrieve(apdu);
1958 apdu = handle_syntax_validation(apdu);
1961 apdu = handle_query_transformation(apdu);
1964 apdu = handle_query_validation(apdu);
1967 apdu = result_set_optimize(apdu);
1970 m_client->timeout(m_target_idletime); // mark it active even
1971 // though we didn't use it
1975 // delete other info part from PDU before sending to target
1976 get_otherInfoAPDU(apdu, &oi);
1980 if (apdu->which == Z_APDU_presentRequest &&
1981 m_client->m_resultSetStartPoint == 0)
1983 Z_PresentRequest *pr = apdu->u.presentRequest;
1984 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
1985 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
1987 m_client->m_resultSetStartPoint = 0;
1989 if (m_client->send_to_target(apdu) < 0)
1996 m_client->m_waiting = 1;
1999 void Yaz_Proxy::connectNotify()
2003 void Yaz_Proxy::shutdown()
2005 m_invalid_session = 0;
2006 // only keep if keep_alive flag is set...
2008 m_client->m_pdu_recv < m_keepalive_limit_pdu &&
2009 m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
2010 m_client->m_waiting == 0)
2012 yaz_log(LOG_LOG, "%sShutdown (client to proxy) keepalive %s",
2014 m_client->get_hostname());
2015 yaz_log(LOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
2016 m_session_str, m_client->m_pdu_recv,
2017 m_client->m_bytes_sent + m_client->m_bytes_recv,
2018 m_keepalive_limit_bw, m_keepalive_limit_pdu);
2019 assert (m_client->m_waiting != 2);
2020 // Tell client (if any) that no server connection is there..
2021 m_client->m_server = 0;
2022 m_invalid_session = 0;
2026 yaz_log (LOG_LOG, "%sShutdown (client to proxy) close %s",
2028 m_client->get_hostname());
2029 assert (m_client->m_waiting != 2);
2034 yaz_log (LOG_LOG, "%sshutdown (client to proxy) bad state",
2040 yaz_log (LOG_LOG, "%sShutdown (client to proxy)",
2044 m_parent->pre_init();
2048 const char *Yaz_ProxyClient::get_session_str()
2052 return m_server->get_session_str();
2055 void Yaz_ProxyClient::shutdown()
2057 yaz_log (LOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
2063 void Yaz_Proxy::failNotify()
2066 yaz_log (LOG_LOG, "%sConnection closed by client",
2071 void Yaz_ProxyClient::failNotify()
2074 m_server->inc_request_no();
2075 yaz_log (LOG_LOG, "%sConnection closed by target %s",
2076 get_session_str(), get_hostname());
2080 void Yaz_ProxyClient::connectNotify()
2082 const char *s = get_session_str();
2083 const char *h = get_hostname();
2084 yaz_log (LOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
2086 timeout(m_target_idletime);
2091 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
2092 *the_PDU_Observable, int fd)
2094 return new Yaz_ProxyClient(the_PDU_Observable, 0);
2097 Yaz_ProxyClient::~Yaz_ProxyClient()
2102 m_next->m_prev = m_prev;
2103 m_waiting = 2; // for debugging purposes only.
2104 odr_destroy(m_init_odr);
2105 delete m_last_query;
2106 xfree (m_last_resultSetId);
2110 void Yaz_ProxyClient::pre_init_client()
2112 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
2113 Z_InitRequest *req = apdu->u.initRequest;
2116 for (i = 0; i<= 24; i++)
2117 ODR_MASK_SET(req->options, i);
2118 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2119 Z_Options_negotiationModel);
2120 for (i = 0; i<= 10; i++)
2121 ODR_MASK_SET(req->protocolVersion, i);
2123 if (send_to_target(apdu) < 0)
2134 void Yaz_Proxy::pre_init()
2137 const char *name = 0;
2138 const char *zurl_in_use[MAX_ZURL_PLEX];
2139 int limit_bw, limit_pdu, limit_req;
2140 int target_idletime, client_idletime;
2142 int keepalive_limit_bw, keepalive_limit_pdu;
2144 const char *cql2rpn = 0;
2146 Yaz_ProxyConfig *cfg = check_reconfigure();
2150 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
2155 for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
2156 &limit_bw, &limit_pdu, &limit_req,
2157 &target_idletime, &client_idletime,
2159 &keepalive_limit_bw,
2160 &keepalive_limit_pdu,
2167 for (j = 0; zurl_in_use[j]; j++)
2173 for (c = m_clientPool; c; c = c->m_next)
2175 if (!strcmp(zurl_in_use[j], c->get_hostname()))
2177 if (c->m_cookie == 0)
2179 if (c->m_server == 0)
2188 yaz_log(LOG_LOG, "%spre-init %s %s use=%d other=%d spare=%d "
2189 "preinit=%d",m_session_str,
2190 name, zurl_in_use[j], in_use, other, spare, pre_init);
2191 if (spare < pre_init)
2193 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
2194 c->m_next = m_clientPool;
2196 c->m_next->m_prev = &c->m_next;
2198 c->m_prev = &m_clientPool;
2200 if (m_log_mask & PROXY_LOG_APDU_SERVER)
2201 c->set_APDU_yazlog(1);
2203 c->set_APDU_yazlog(0);
2205 if (c->client(zurl_in_use[j]))
2213 c->m_target_idletime = target_idletime;
2214 c->m_seqno = m_seqno++;
2221 void Yaz_Proxy::timeoutNotify()
2227 timeout(m_client_idletime);
2228 Z_GDU *apdu = m_bw_hold_PDU;
2231 if (apdu->which == Z_GDU_Z3950)
2232 handle_incoming_Z_PDU(apdu->u.z3950);
2233 else if (apdu->which == Z_GDU_HTTP_Request)
2234 handle_incoming_HTTP(apdu->u.HTTP_Request);
2240 yaz_log (LOG_LOG, "%sTimeout (client to proxy)", m_session_str);
2251 void Yaz_Proxy::markInvalid()
2254 m_invalid_session = 1;
2257 void Yaz_ProxyClient::timeoutNotify()
2260 m_server->inc_request_no();
2262 yaz_log (LOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
2266 if (m_server && m_init_flag)
2268 // target timed out in a session that was properly initialized
2269 // server object stay alive but we mark it as invalid so it
2270 // gets initialized again
2271 m_server->markInvalid();
2277 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
2278 Yaz_Proxy *parent) :
2279 Yaz_Z_Assoc (the_PDU_Observable)
2286 m_last_resultSetId = 0;
2287 m_last_resultCount = 0;
2291 m_init_odr = odr_createmem (ODR_DECODE);
2293 m_initResponse_options = 0;
2294 m_initResponse_version = 0;
2295 m_resultSetStartPoint = 0;
2296 m_bytes_sent = m_bytes_recv = 0;
2300 m_target_idletime = 600;
2304 const char *Yaz_Proxy::option(const char *name, const char *value)
2306 if (!strcmp (name, "optimize")) {
2309 m_optimize = xstrdup (value);
2316 void Yaz_ProxyClient::recv_HTTP_response(Z_HTTP_Response *apdu, int len)
2321 void Yaz_ProxyClient::recv_GDU(Z_GDU *apdu, int len)
2323 if (apdu->which == Z_GDU_Z3950)
2324 recv_Z_PDU(apdu->u.z3950, len);
2325 else if (apdu->which == Z_GDU_HTTP_Response)
2326 recv_HTTP_response(apdu->u.HTTP_Response, len);
2331 int Yaz_Proxy::handle_init_response_for_invalid_session(Z_APDU *apdu)
2333 if (!m_invalid_session)
2335 m_invalid_session = 0;
2336 handle_incoming_Z_PDU(m_apdu_invalid_session);
2337 assert (m_mem_invalid_session);
2338 nmem_destroy(m_mem_invalid_session);
2339 m_mem_invalid_session = 0;
2343 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
2345 m_bytes_recv += len;
2348 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
2349 yaz_log (LOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
2350 apdu_name(apdu), get_hostname(), len);
2351 if (apdu->which == Z_APDU_initResponse)
2353 if (!m_server) // if this is a pre init session , check for more
2355 NMEM nmem = odr_extract_mem (odr_decode());
2356 odr_reset (m_init_odr);
2357 nmem_transfer (m_init_odr->mem, nmem);
2358 m_initResponse = apdu;
2359 m_initResponse_options = apdu->u.initResponse->options;
2360 m_initResponse_version = apdu->u.initResponse->protocolVersion;
2362 Z_InitResponse *ir = apdu->u.initResponse;
2363 char *im0 = ir->implementationName;
2366 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
2373 strcat(im1, "(YAZ Proxy)");
2374 ir->implementationName = im1;
2376 nmem_destroy (nmem);
2378 if (m_server && m_server->handle_init_response_for_invalid_session(apdu))
2381 if (apdu->which == Z_APDU_searchResponse)
2383 Z_SearchResponse *sr = apdu->u.searchResponse;
2384 m_last_resultCount = *sr->resultCount;
2385 int status = *sr->searchStatus;
2386 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
2390 if (sr->records && sr->records->which == Z_Records_DBOSD)
2392 m_cache.add(odr_decode(),
2393 sr->records->u.databaseOrSurDiagnostics, 1,
2398 if (apdu->which == Z_APDU_presentResponse)
2400 Z_PresentResponse *pr = apdu->u.presentResponse;
2404 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2405 Z_SearchResponse *sr = new_apdu->u.searchResponse;
2406 sr->referenceId = pr->referenceId;
2407 *sr->resultCount = m_last_resultCount;
2408 sr->records = pr->records;
2409 sr->nextResultSetPosition = pr->nextResultSetPosition;
2410 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
2414 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
2416 m_cache.add(odr_decode(),
2417 pr->records->u.databaseOrSurDiagnostics,
2418 m_resultSetStartPoint, -1);
2419 m_resultSetStartPoint = 0;
2423 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
2426 m_server->send_to_client(apdu);
2428 if (apdu->which == Z_APDU_close)
2434 int Yaz_Proxy::server(const char *addr)
2436 int r = Yaz_Z_Assoc::server(addr);
2439 yaz_log(LOG_LOG, "%sStarted proxy " VERSION " on %s", m_session_str, addr);