2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.104 2004-02-26 23:43:07 adam Exp $
12 #include <yaz/marcdisp.h>
13 #include <yaz/yaz-iconv.h>
15 #include <yaz/diagbib1.h>
16 #include <yaz++/proxy.h>
17 #include <yaz/pquery.h>
19 static const char *apdu_name(Z_APDU *apdu)
23 case Z_APDU_initRequest:
25 case Z_APDU_initResponse:
26 return "initResponse";
27 case Z_APDU_searchRequest:
28 return "searchRequest";
29 case Z_APDU_searchResponse:
30 return "searchResponse";
31 case Z_APDU_presentRequest:
32 return "presentRequest";
33 case Z_APDU_presentResponse:
34 return "presentResponse";
35 case Z_APDU_deleteResultSetRequest:
36 return "deleteResultSetRequest";
37 case Z_APDU_deleteResultSetResponse:
38 return "deleteResultSetResponse";
39 case Z_APDU_scanRequest:
41 case Z_APDU_scanResponse:
42 return "scanResponse";
43 case Z_APDU_sortRequest:
45 case Z_APDU_sortResponse:
46 return "sortResponse";
47 case Z_APDU_extendedServicesRequest:
48 return "extendedServicesRequest";
49 case Z_APDU_extendedServicesResponse:
50 return "extendedServicesResponse";
57 static const char *gdu_name(Z_GDU *gdu)
62 return apdu_name(gdu->u.z3950);
63 case Z_GDU_HTTP_Request:
64 return "HTTP Request";
65 case Z_GDU_HTTP_Response:
66 return "HTTP Response";
68 return "Unknown request/response";
71 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
73 Yaz_Z_Assoc(the_PDU_Observable), m_bw_stat(60), m_pdu_stat(60)
75 m_PDU_Observable = the_PDU_Observable;
80 m_keepalive_limit_bw = 500000;
81 m_keepalive_limit_pdu = 1000;
84 m_proxy_authentication = 0;
88 m_client_idletime = 600;
89 m_target_idletime = 600;
90 m_optimize = xstrdup ("1");
91 strcpy(m_session_str, "0 ");
98 m_max_record_retrieve = 0;
102 m_invalid_session = 0;
104 m_referenceId_mem = nmem_create();
107 m_stylesheet_xsp = 0;
108 m_stylesheet_nprl = 0;
109 m_s2z_stylesheet = 0;
112 m_initRequest_apdu = 0;
113 m_initRequest_mem = 0;
114 m_initRequest_preferredMessageSize = 0;
115 m_initRequest_maximumRecordSize = 0;
116 m_initRequest_options = 0;
117 m_initRequest_version = 0;
118 m_apdu_invalid_session = 0;
119 m_mem_invalid_session = 0;
121 m_s2z_odr_search = 0;
123 m_s2z_search_apdu = 0;
124 m_s2z_present_apdu = 0;
125 m_http_keepalive = 0;
128 m_s2z_packing = Z_SRW_recordPacking_string;
129 m_time_tv.tv_sec = 0;
130 m_time_tv.tv_usec = 0;
133 Yaz_Proxy::~Yaz_Proxy()
135 yaz_log(LOG_LOG, "%sClosed %d/%d sent/recv bytes total", m_session_str,
136 m_bytes_sent, m_bytes_recv);
137 nmem_destroy(m_initRequest_mem);
138 nmem_destroy(m_mem_invalid_session);
139 nmem_destroy(m_referenceId_mem);
141 xfree (m_proxyTarget);
142 xfree (m_default_target);
143 xfree (m_proxy_authentication);
146 if (m_stylesheet_xsp)
147 xsltFreeStylesheet(m_stylesheet_xsp);
151 odr_destroy(m_s2z_odr_init);
152 if (m_s2z_odr_search)
153 odr_destroy(m_s2z_odr_search);
157 int Yaz_Proxy::set_config(const char *config)
160 m_config = new Yaz_ProxyConfig();
161 xfree(m_config_fname);
162 m_config_fname = xstrdup(config);
163 int r = m_config->read_xml(config);
165 m_config->get_generic_info(&m_log_mask, &m_max_clients);
169 void Yaz_Proxy::set_default_target(const char *target)
171 xfree (m_default_target);
172 m_default_target = 0;
174 m_default_target = (char *) xstrdup (target);
177 void Yaz_Proxy::set_proxy_authentication (const char *auth)
179 xfree (m_proxy_authentication);
180 m_proxy_authentication = 0;
182 m_proxy_authentication = (char *) xstrdup (auth);
185 Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
188 return m_parent->check_reconfigure();
190 Yaz_ProxyConfig *cfg = m_config;
193 yaz_log(LOG_LOG, "reconfigure");
195 if (m_config_fname && cfg)
197 yaz_log(LOG_LOG, "reconfigure config %s", m_config_fname);
198 int r = cfg->read_xml(m_config_fname);
200 yaz_log(LOG_WARN, "reconfigure failed");
204 cfg->get_generic_info(&m_log_mask, &m_max_clients);
208 yaz_log(LOG_LOG, "reconfigure");
214 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
215 *the_PDU_Observable, int fd)
218 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable, this);
219 new_proxy->m_config = 0;
220 new_proxy->m_config_fname = 0;
221 new_proxy->timeout(m_client_idletime);
222 new_proxy->m_target_idletime = m_target_idletime;
223 new_proxy->set_default_target(m_default_target);
224 new_proxy->m_max_clients = m_max_clients;
225 new_proxy->m_log_mask = m_log_mask;
226 new_proxy->set_APDU_log(get_APDU_log());
227 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
228 new_proxy->set_APDU_yazlog(1);
230 new_proxy->set_APDU_yazlog(0);
231 new_proxy->set_proxy_authentication(m_proxy_authentication);
232 sprintf(new_proxy->m_session_str, "%ld:%d ", (long) time(0), m_session_no);
234 yaz_log (LOG_LOG, "%sNew session %s", new_proxy->m_session_str,
235 the_PDU_Observable->getpeername());
239 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
242 Z_OtherInformationUnit *oi;
244 ent.proto = PROTO_Z3950;
245 ent.oclass = CLASS_USERINFO;
246 ent.value = (oid_value) VAL_COOKIE;
247 assert (oid_ent_to_oid (&ent, oid));
249 if (oid_ent_to_oid (&ent, oid) &&
250 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
251 oi->which == Z_OtherInfo_characterInfo)
252 return oi->information.characterInfo;
256 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
259 Z_OtherInformationUnit *oi;
261 ent.proto = PROTO_Z3950;
262 ent.oclass = CLASS_USERINFO;
263 ent.value = (oid_value) VAL_PROXY;
264 if (oid_ent_to_oid (&ent, oid) &&
265 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
266 oi->which == Z_OtherInfo_characterInfo)
267 return oi->information.characterInfo;
271 const char *Yaz_Proxy::load_balance(const char **url)
273 int zurl_in_use[MAX_ZURL_PLEX];
274 int zurl_in_spare[MAX_ZURL_PLEX];
278 for (i = 0; i<MAX_ZURL_PLEX; i++)
281 zurl_in_spare[i] = 0;
283 for (c = m_parent->m_clientPool; c; c = c->m_next)
285 for (i = 0; url[i]; i++)
286 if (!strcmp(url[i], c->get_hostname()))
289 if (c->m_cookie == 0 && c->m_server == 0 && c->m_waiting == 0)
293 int min_use = 100000;
294 int spare_for_min = 0;
296 const char *ret_min = 0;
297 const char *ret_spare = 0;
298 for (i = 0; url[i]; i++)
300 yaz_log(LOG_DEBUG, "%szurl=%s use=%d spare=%d",
301 m_session_str, url[i], zurl_in_use[i], zurl_in_spare[i]);
302 if (min_use > zurl_in_use[i])
305 min_use = zurl_in_use[i];
306 spare_for_min = zurl_in_spare[i];
308 if (max_spare < zurl_in_spare[i])
311 max_spare = zurl_in_spare[i];
314 // use the one with minimum connections if spare is > 3
315 if (spare_for_min > 3)
317 // use one with most spares (if any)
323 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
324 const char *proxy_host)
327 Yaz_Proxy *parent = m_parent;
328 Yaz_ProxyClient *c = m_client;
332 const char *url[MAX_ZURL_PLEX];
333 Yaz_ProxyConfig *cfg = check_reconfigure();
337 /* only to be enabled for debugging... */
338 if (!strcmp(proxy_host, "stop"))
341 xfree(m_default_target);
342 m_default_target = xstrdup(proxy_host);
343 proxy_host = m_default_target;
345 int client_idletime = -1;
346 const char *cql2rpn_fname = 0;
347 url[0] = m_default_target;
352 cfg->get_target_info(proxy_host, url, &m_bw_max,
353 &m_pdu_max, &m_max_record_retrieve,
354 &m_target_idletime, &client_idletime,
355 &parent->m_max_clients,
356 &m_keepalive_limit_bw,
357 &m_keepalive_limit_pdu,
361 if (client_idletime != -1)
363 m_client_idletime = client_idletime;
364 timeout(m_client_idletime);
367 m_cql2rpn.set_pqf_file(cql2rpn_fname);
370 yaz_log(LOG_LOG, "%sNo default target", m_session_str);
373 // we don't handle multiplexing for cookie session, so we just
374 // pick the first one in this case (anonymous users will be able
375 // to use any backend)
376 if (cookie && *cookie)
377 m_proxyTarget = (char*) xstrdup(url[0]);
379 m_proxyTarget = (char*) xstrdup(load_balance(url));
381 if (cookie && *cookie)
382 { // search in sessions with a cookie
383 for (c = parent->m_clientPool; c; c = c->m_next)
386 assert (*c->m_prev == c);
387 if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
388 !strcmp(m_proxyTarget, c->get_hostname()))
391 // The following handles "cancel"
392 // If connection is busy (waiting for PDU) and
393 // we have an initRequest we can safely do re-open
394 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
396 yaz_log (LOG_LOG, "%s REOPEN target=%s", m_session_str,
403 c->m_last_resultCount = 0;
404 c->m_sr_transform = 0;
406 c->m_resultSetStartPoint = 0;
407 c->m_target_idletime = m_target_idletime;
408 if (c->client(m_proxyTarget))
415 c->m_seqno = parent->m_seqno;
416 if (c->m_server && c->m_server != this)
417 c->m_server->m_client = 0;
420 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
427 // don't have a client session yet. Search in session w/o cookie
428 for (c = parent->m_clientPool; c; c = c->m_next)
431 assert (*c->m_prev == c);
432 if (c->m_server == 0 && c->m_cookie == 0 &&
434 !strcmp(m_proxyTarget, c->get_hostname()))
437 yaz_log (LOG_LOG, "%sREUSE %d %s",
438 m_session_str, parent->m_seqno, c->get_hostname());
440 c->m_seqno = parent->m_seqno;
441 assert(c->m_server == 0);
444 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
445 c->set_APDU_yazlog(1);
447 c->set_APDU_yazlog(0);
459 if (apdu->which != Z_APDU_initRequest)
461 yaz_log (LOG_LOG, "%sno init request as first PDU", m_session_str);
464 Z_InitRequest *initRequest = apdu->u.initRequest;
466 if (!initRequest->idAuthentication)
468 if (m_proxy_authentication)
470 initRequest->idAuthentication =
471 (Z_IdAuthentication *)
472 odr_malloc (odr_encode(),
473 sizeof(*initRequest->idAuthentication));
474 initRequest->idAuthentication->which =
475 Z_IdAuthentication_open;
476 initRequest->idAuthentication->u.open =
477 odr_strdup (odr_encode(), m_proxy_authentication);
480 // go through list of clients - and find the lowest/oldest one.
481 Yaz_ProxyClient *c_min = 0;
483 int no_of_clients = 0;
484 if (parent->m_clientPool)
485 yaz_log (LOG_DEBUG, "Existing sessions");
486 for (c = parent->m_clientPool; c; c = c->m_next)
488 yaz_log (LOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
489 c->m_waiting, c->get_hostname(),
490 c->m_cookie ? c->m_cookie : "");
492 if (min_seq < 0 || c->m_seqno < min_seq)
494 min_seq = c->m_seqno;
498 if (no_of_clients >= parent->m_max_clients)
501 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
503 yaz_log (LOG_LOG, "%sMAXCLIENTS %d Destroy %d",
504 m_session_str, parent->m_max_clients, c->m_seqno);
505 if (c->m_server && c->m_server != this)
511 yaz_log (LOG_LOG, "%sMAXCLIENTS %d Reuse %d %d %s",
512 m_session_str, parent->m_max_clients,
513 c->m_seqno, parent->m_seqno, c->get_hostname());
517 c->m_cookie = xstrdup(cookie);
518 c->m_seqno = parent->m_seqno;
519 if (c->m_server && c->m_server != this)
521 c->m_server->m_client = 0;
525 c->m_target_idletime = m_target_idletime;
526 c->timeout(m_target_idletime);
528 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
529 c->set_APDU_yazlog(1);
531 c->set_APDU_yazlog(0);
538 yaz_log (LOG_LOG, "%sNEW %d %s",
539 m_session_str, parent->m_seqno, m_proxyTarget);
540 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), parent);
541 c->m_next = parent->m_clientPool;
543 c->m_next->m_prev = &c->m_next;
544 parent->m_clientPool = c;
545 c->m_prev = &parent->m_clientPool;
551 c->m_cookie = xstrdup(cookie);
553 c->m_seqno = parent->m_seqno;
555 c->m_last_resultCount = 0;
558 c->m_sr_transform = 0;
560 c->m_resultSetStartPoint = 0;
562 if (c->client(m_proxyTarget))
567 c->m_target_idletime = m_target_idletime;
570 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
571 c->set_APDU_yazlog(1);
573 c->set_APDU_yazlog(0);
575 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
579 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
582 for (i = 0; i<num; i++)
585 Z_DefaultDiagFormat *r;
586 Z_DiagRec *p = pp[i];
587 if (p->which != Z_DiagRec_defaultFormat)
589 yaz_log(LOG_LOG, "%sError no diagnostics", m_session_str);
593 r = p->u.defaultFormat;
594 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
595 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
596 yaz_log(LOG_LOG, "%sError unknown diagnostic set", m_session_str);
599 case Z_DefaultDiagFormat_v2Addinfo:
600 yaz_log(LOG_LOG, "%sError %d %s:%s",
602 *r->condition, diagbib1_str(*r->condition),
605 case Z_DefaultDiagFormat_v3Addinfo:
606 yaz_log(LOG_LOG, "%sError %d %s:%s",
608 *r->condition, diagbib1_str(*r->condition),
615 int Yaz_Proxy::convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu)
617 if (!m_stylesheet_xsp || p->num_records <= 0)
618 return 0; /* no XSLT to be done ... */
620 m_stylesheet_offset = 0;
621 m_stylesheet_nprl = p;
622 m_stylesheet_apdu = apdu;
627 void Yaz_Proxy::convert_xsl_delay()
629 Z_NamePlusRecord *npr = m_stylesheet_nprl->records[m_stylesheet_offset];
630 if (npr->which == Z_NamePlusRecord_databaseRecord)
632 Z_External *r = npr->u.databaseRecord;
633 if (r->which == Z_External_octet)
635 xmlDocPtr res, doc = xmlParseMemory(
636 (char*) r->u.octet_aligned->buf,
637 r->u.octet_aligned->len);
639 yaz_log(LOG_LOG, "%sXSLT convert %d",
640 m_session_str, m_stylesheet_offset);
641 res = xsltApplyStylesheet(m_stylesheet_xsp, doc, 0);
646 xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
648 m_stylesheet_nprl->records[m_stylesheet_offset]->
650 z_ext_record(odr_encode(), VAL_TEXT_XML,
651 (char*) out_buf, out_len);
658 m_stylesheet_offset++;
659 if (m_stylesheet_offset == m_stylesheet_nprl->num_records)
661 m_stylesheet_nprl = 0;
662 if (m_stylesheet_xsp)
663 xsltFreeStylesheet(m_stylesheet_xsp);
664 m_stylesheet_xsp = 0;
665 timeout(m_client_idletime);
666 send_PDU_convert(m_stylesheet_apdu);
672 void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
676 yaz_marc_t mt = yaz_marc_create();
677 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
678 for (i = 0; i < p->num_records; i++)
680 Z_NamePlusRecord *npr = p->records[i];
681 if (npr->which == Z_NamePlusRecord_databaseRecord)
683 Z_External *r = npr->u.databaseRecord;
684 if (r->which == Z_External_octet)
688 if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
689 r->u.octet_aligned->len,
692 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
693 WRBUF wrbuf = wrbuf_alloc();
696 size_t inbytesleft = rlen;
697 const char *inp = result;
698 while (cd && inbytesleft)
700 size_t outbytesleft = sizeof(outbuf);
704 r = yaz_iconv (cd, (char**) &inp,
706 &outp, &outbytesleft);
707 if (r == (size_t) (-1))
709 int e = yaz_iconv_error(cd);
710 if (e != YAZ_ICONV_E2BIG)
712 yaz_log(LOG_WARN, "conversion failure");
716 wrbuf_write(wrbuf, outbuf, outp - outbuf);
721 npr->u.databaseRecord = z_ext_record(odr_encode(),
725 wrbuf_free(wrbuf, 1);
730 yaz_marc_destroy(mt);
733 void Yaz_Proxy::logtime()
735 if (m_time_tv.tv_sec)
738 gettimeofday(&tv, 0);
739 long diff = (tv.tv_sec - m_time_tv.tv_sec)*1000000 +
740 (tv.tv_usec - m_time_tv.tv_usec);
742 yaz_log(LOG_LOG, "%sElapsed %ld.%03ld", m_session_str,
743 diff/1000000, (diff/1000)%1000);
745 m_time_tv.tv_sec = 0;
746 m_time_tv.tv_usec = 0;
749 int Yaz_Proxy::send_http_response(int code)
751 ODR o = odr_encode();
752 Z_GDU *gdu = z_get_HTTP_Response(o, code);
753 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
755 hres->version = odr_strdup(o, m_http_version);
756 if (m_http_keepalive)
757 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
761 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
763 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
767 int r = send_GDU(gdu, &len);
769 m_bw_stat.add_bytes(len);
774 int Yaz_Proxy::send_srw_response(Z_SRW_PDU *srw_pdu)
776 ODR o = odr_encode();
777 const char *ctype = "text/xml";
778 Z_GDU *gdu = z_get_HTTP_Response(o, 200);
779 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
781 hres->version = odr_strdup(o, m_http_version);
782 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
783 if (m_http_keepalive)
784 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
788 static Z_SOAP_Handler soap_handlers[2] = {
790 {"http://www.loc.gov/zing/srw/", 0,
791 (Z_SOAP_fun) yaz_srw_codec},
796 Z_SOAP *soap_package = (Z_SOAP*) odr_malloc(o, sizeof(Z_SOAP));
797 soap_package->which = Z_SOAP_generic;
798 soap_package->u.generic =
799 (Z_SOAP_Generic *) odr_malloc(o, sizeof(*soap_package->u.generic));
800 soap_package->u.generic->no = 0;
801 soap_package->u.generic->ns = soap_handlers[0].ns;
802 soap_package->u.generic->p = (void *) srw_pdu;
803 soap_package->ns = m_soap_ns;
804 z_soap_codec_enc_xsl(o, &soap_package,
805 &hres->content_buf, &hres->content_len,
806 soap_handlers, 0, m_s2z_stylesheet);
807 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
809 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
813 int r = send_GDU(gdu, &len);
815 m_bw_stat.add_bytes(len);
820 int Yaz_Proxy::send_to_srw_client_error(int srw_error, const char *add)
822 ODR o = odr_encode();
823 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
824 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
826 srw_res->num_diagnostics = 1;
827 srw_res->diagnostics = (Z_SRW_diagnostic *)
828 odr_malloc(o, sizeof(*srw_res->diagnostics));
829 yaz_mk_std_diagnostic(o, srw_res->diagnostics, srw_error, add);
830 return send_srw_response(srw_pdu);
833 int Yaz_Proxy::z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
834 Z_DefaultDiagFormat *ddf)
836 int bib1_code = *ddf->condition;
837 if (bib1_code == 109)
839 srw_res->num_diagnostics = 1;
840 srw_res->diagnostics = (Z_SRW_diagnostic *)
841 odr_malloc(o, sizeof(*srw_res->diagnostics));
842 yaz_mk_std_diagnostic(o, srw_res->diagnostics,
843 yaz_diag_bib1_to_srw(*ddf->condition),
848 int Yaz_Proxy::send_to_srw_client_ok(int hits, Z_Records *records, int start)
850 ODR o = odr_encode();
851 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
852 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
854 srw_res->numberOfRecords = odr_intdup (o, hits);
855 if (records && records->which == Z_Records_DBOSD)
857 srw_res->num_records =
858 records->u.databaseOrSurDiagnostics->num_records;
860 srw_res->records = (Z_SRW_record *)
861 odr_malloc(o, srw_res->num_records * sizeof(Z_SRW_record));
862 for (i = 0; i < srw_res->num_records; i++)
864 Z_NamePlusRecord *npr = records->u.databaseOrSurDiagnostics->records[i];
865 if (npr->which != Z_NamePlusRecord_databaseRecord)
867 srw_res->records[i].recordSchema = "diagnostic";
868 srw_res->records[i].recordPacking = m_s2z_packing;
869 srw_res->records[i].recordData_buf = "67";
870 srw_res->records[i].recordData_len = 2;
871 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
874 Z_External *r = npr->u.databaseRecord;
875 oident *ent = oid_getentbyoid(r->direct_reference);
876 if (r->which == Z_External_octet && ent->value == VAL_TEXT_XML)
878 srw_res->records[i].recordSchema = m_schema;
879 srw_res->records[i].recordPacking = m_s2z_packing;
880 srw_res->records[i].recordData_buf = (char*)
881 r->u.octet_aligned->buf;
882 srw_res->records[i].recordData_len = r->u.octet_aligned->len;
883 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
887 srw_res->records[i].recordSchema = "diagnostic";
888 srw_res->records[i].recordPacking = m_s2z_packing;
889 srw_res->records[i].recordData_buf = "67";
890 srw_res->records[i].recordData_len = 2;
891 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
895 if (records && records->which == Z_Records_NSD)
898 http_code = z_to_srw_diag(odr_encode(), srw_res,
899 records->u.nonSurrogateDiagnostic);
901 return send_http_response(http_code);
903 return send_srw_response(srw_pdu);
907 int Yaz_Proxy::send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
910 Yaz_ProxyConfig *cfg = check_reconfigure();
914 char *b = cfg->get_explain(odr_encode(), 0 /* target */,
915 m_s2z_database, &len);
918 Z_SRW_PDU *res = yaz_srw_get(odr_encode(), Z_SRW_explain_response);
919 Z_SRW_explainResponse *er = res->u.explain_response;
921 er->record.recordData_buf = b;
922 er->record.recordData_len = len;
923 er->record.recordPacking = m_s2z_packing;
924 er->record.recordSchema = "http://explain.z3950.org/dtd/2.0/";
926 er->diagnostics = diagnostics;
927 er->num_diagnostics = num_diagnostics;
928 return send_srw_response(res);
931 return send_http_response(404);
934 int Yaz_Proxy::send_PDU_convert(Z_APDU *apdu)
938 if (apdu->which == Z_APDU_initResponse)
940 Z_InitResponse *res = apdu->u.initResponse;
941 if (*res->result == 0)
943 send_to_srw_client_error(3, 0);
945 else if (!m_s2z_search_apdu)
947 send_srw_explain_response(0, 0);
951 handle_incoming_Z_PDU(m_s2z_search_apdu);
954 else if (m_s2z_search_apdu && apdu->which == Z_APDU_searchResponse)
956 m_s2z_search_apdu = 0;
957 Z_SearchResponse *res = apdu->u.searchResponse;
958 m_s2z_hit_count = *res->resultCount;
959 if (res->records && res->records->which == Z_Records_NSD)
961 send_to_srw_client_ok(0, res->records, 1);
963 else if (m_s2z_present_apdu && m_s2z_hit_count > 0)
966 Z_PresentRequest *pr = m_s2z_present_apdu->u.presentRequest;
968 if (*pr->resultSetStartPoint <= m_s2z_hit_count)
970 if (*pr->numberOfRecordsRequested+ *pr->resultSetStartPoint
972 *pr->numberOfRecordsRequested =
973 1 + m_s2z_hit_count - *pr->resultSetStartPoint;
975 handle_incoming_Z_PDU(m_s2z_present_apdu);
979 m_s2z_present_apdu = 0;
980 send_to_srw_client_ok(m_s2z_hit_count, res->records, 1);
983 else if (m_s2z_present_apdu && apdu->which == Z_APDU_presentResponse)
986 *m_s2z_present_apdu->u.presentRequest->resultSetStartPoint;
988 m_s2z_present_apdu = 0;
989 Z_PresentResponse *res = apdu->u.presentResponse;
990 send_to_srw_client_ok(m_s2z_hit_count, res->records, start);
996 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
997 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
999 int r = send_Z_PDU(apdu, &len);
1000 m_bytes_sent += len;
1001 m_bw_stat.add_bytes(len);
1008 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
1010 int kill_session = 0;
1011 Z_ReferenceId **new_id = get_referenceIdP(apdu);
1013 if (new_id && m_referenceId)
1014 *new_id = m_referenceId;
1016 if (apdu->which == Z_APDU_searchResponse)
1018 Z_SearchResponse *sr = apdu->u.searchResponse;
1019 Z_Records *p = sr->records;
1020 if (p && p->which == Z_Records_NSD)
1022 Z_DiagRec dr, *dr_p = &dr;
1023 dr.which = Z_DiagRec_defaultFormat;
1024 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1026 *sr->searchStatus = 0;
1027 display_diagrecs(&dr_p, 1);
1031 if (p && p->which == Z_Records_DBOSD)
1034 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1035 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1039 if (sr->resultCount)
1041 yaz_log(LOG_LOG, "%s%d hits", m_session_str,
1043 if (*sr->resultCount < 0)
1045 m_invalid_session = 1;
1048 *sr->searchStatus = 0;
1050 create_nonSurrogateDiagnostics(odr_encode(), 2, 0);
1051 *sr->resultCount = 0;
1056 else if (apdu->which == Z_APDU_presentResponse)
1058 Z_PresentResponse *sr = apdu->u.presentResponse;
1059 Z_Records *p = sr->records;
1060 if (p && p->which == Z_Records_NSD)
1062 Z_DiagRec dr, *dr_p = &dr;
1063 dr.which = Z_DiagRec_defaultFormat;
1064 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1065 if (*sr->presentStatus == Z_PresentStatus_success)
1066 *sr->presentStatus = Z_PresentStatus_failure;
1067 display_diagrecs(&dr_p, 1);
1069 if (p && p->which == Z_Records_DBOSD)
1072 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1073 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1077 else if (apdu->which == Z_APDU_initResponse)
1079 if (m_initRequest_options)
1082 (Odr_bitmask *)odr_malloc(odr_encode(),
1083 sizeof(Odr_bitmask));
1084 ODR_MASK_ZERO(nopt);
1087 for (i = 0; i<24; i++)
1088 if (ODR_MASK_GET(m_initRequest_options, i) &&
1089 ODR_MASK_GET(apdu->u.initResponse->options, i))
1090 ODR_MASK_SET(nopt, i);
1091 apdu->u.initResponse->options = nopt;
1093 if (m_initRequest_version)
1095 Z_ProtocolVersion *nopt =
1096 (Odr_bitmask *)odr_malloc(odr_encode(),
1097 sizeof(Odr_bitmask));
1098 ODR_MASK_ZERO(nopt);
1101 for (i = 0; i<8; i++)
1102 if (ODR_MASK_GET(m_initRequest_version, i) &&
1103 ODR_MASK_GET(apdu->u.initResponse->protocolVersion, i))
1104 ODR_MASK_SET(nopt, i);
1105 apdu->u.initResponse->protocolVersion = nopt;
1107 apdu->u.initResponse->preferredMessageSize =
1108 odr_intdup(odr_encode(),
1109 m_client->m_initResponse_preferredMessageSize >
1110 m_initRequest_preferredMessageSize ?
1111 m_initRequest_preferredMessageSize :
1112 m_client->m_initResponse_preferredMessageSize);
1113 apdu->u.initResponse->maximumRecordSize =
1114 odr_intdup(odr_encode(),
1115 m_client->m_initResponse_maximumRecordSize >
1116 m_initRequest_maximumRecordSize ?
1117 m_initRequest_maximumRecordSize :
1118 m_client->m_initResponse_maximumRecordSize);
1120 int r = send_PDU_convert(apdu);
1127 m_parent->pre_init();
1132 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
1135 const char *apdu_name_tmp = apdu_name(apdu);
1136 int r = send_Z_PDU(apdu, &len);
1137 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
1138 yaz_log (LOG_LOG, "%sSending %s to %s %d bytes",
1140 apdu_name_tmp, get_hostname(), len);
1141 m_bytes_sent += len;
1145 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
1147 if (apdu->which == Z_APDU_presentRequest)
1149 Z_PresentRequest *pr = apdu->u.presentRequest;
1150 int toget = *pr->numberOfRecordsRequested;
1151 int start = *pr->resultSetStartPoint;
1153 yaz_log(LOG_LOG, "%sPresent %s %d+%d", m_session_str,
1154 pr->resultSetId, start, toget);
1156 if (*m_parent->m_optimize == '0')
1159 if (!m_client->m_last_resultSetId)
1161 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1162 new_apdu->u.presentResponse->records =
1163 create_nonSurrogateDiagnostics(odr_encode(), 30,
1165 send_to_client(new_apdu);
1168 if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
1170 if (start+toget-1 > m_client->m_last_resultCount)
1172 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1173 new_apdu->u.presentResponse->records =
1174 create_nonSurrogateDiagnostics(odr_encode(), 13, 0);
1175 send_to_client(new_apdu);
1178 Z_NamePlusRecordList *npr;
1179 if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
1180 pr->preferredRecordSyntax,
1181 pr->recordComposition))
1183 yaz_log (LOG_LOG, "%sReturned cached records for present request",
1185 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1186 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1188 new_apdu->u.presentResponse->numberOfRecordsReturned
1189 = odr_intdup(odr_encode(), toget);
1191 new_apdu->u.presentResponse->records = (Z_Records*)
1192 odr_malloc(odr_encode(), sizeof(Z_Records));
1193 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
1194 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
1195 new_apdu->u.presentResponse->nextResultSetPosition =
1196 odr_intdup(odr_encode(), start+toget);
1198 send_to_client(new_apdu);
1204 if (apdu->which != Z_APDU_searchRequest)
1206 Z_SearchRequest *sr = apdu->u.searchRequest;
1207 Yaz_Z_Query *this_query = new Yaz_Z_Query;
1208 Yaz_Z_Databases this_databases;
1210 this_databases.set(sr->num_databaseNames, (const char **)
1213 this_query->set_Z_Query(sr->query);
1215 char query_str[120];
1216 this_query->print(query_str, sizeof(query_str)-1);
1217 yaz_log(LOG_LOG, "%sSearch %s", m_session_str, query_str);
1219 if (*m_parent->m_optimize != '0' &&
1220 m_client->m_last_ok && m_client->m_last_query &&
1221 m_client->m_last_query->match(this_query) &&
1222 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
1223 m_client->m_last_databases.match(this_databases))
1226 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
1227 m_client->m_last_resultCount < *sr->largeSetLowerBound)
1229 Z_NamePlusRecordList *npr;
1230 int toget = *sr->mediumSetPresentNumber;
1231 Z_RecordComposition *comp = 0;
1233 if (toget > m_client->m_last_resultCount)
1234 toget = m_client->m_last_resultCount;
1236 if (sr->mediumSetElementSetNames)
1238 comp = (Z_RecordComposition *)
1239 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1240 comp->which = Z_RecordComp_simple;
1241 comp->u.simple = sr->mediumSetElementSetNames;
1244 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1245 sr->preferredRecordSyntax, comp))
1247 yaz_log (LOG_LOG, "%sReturned cached records for medium set",
1249 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1250 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1251 new_apdu->u.searchResponse->resultCount =
1252 &m_client->m_last_resultCount;
1254 new_apdu->u.searchResponse->numberOfRecordsReturned
1255 = odr_intdup(odr_encode(), toget);
1257 new_apdu->u.searchResponse->presentStatus =
1258 odr_intdup(odr_encode(), Z_PresentStatus_success);
1259 new_apdu->u.searchResponse->records = (Z_Records*)
1260 odr_malloc(odr_encode(), sizeof(Z_Records));
1261 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1262 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1263 new_apdu->u.searchResponse->nextResultSetPosition =
1264 odr_intdup(odr_encode(), toget+1);
1265 send_to_client(new_apdu);
1271 // send present request (medium size)
1272 yaz_log (LOG_LOG, "%sOptimizing search for medium set",
1275 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1276 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1277 pr->referenceId = sr->referenceId;
1278 pr->resultSetId = sr->resultSetName;
1279 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1280 *pr->numberOfRecordsRequested = toget;
1281 pr->recordComposition = comp;
1282 m_client->m_sr_transform = 1;
1286 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
1287 m_client->m_last_resultCount <= 0)
1289 // large set. Return pseudo-search response immediately
1290 yaz_log (LOG_LOG, "%sOptimizing search for large set",
1292 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1293 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1294 new_apdu->u.searchResponse->resultCount =
1295 &m_client->m_last_resultCount;
1296 send_to_client(new_apdu);
1301 Z_NamePlusRecordList *npr;
1302 int toget = m_client->m_last_resultCount;
1303 Z_RecordComposition *comp = 0;
1305 // send a present request (small set)
1307 if (sr->smallSetElementSetNames)
1309 comp = (Z_RecordComposition *)
1310 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1311 comp->which = Z_RecordComp_simple;
1312 comp->u.simple = sr->smallSetElementSetNames;
1315 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1316 sr->preferredRecordSyntax, comp))
1318 yaz_log (LOG_LOG, "%sReturned cached records for small set",
1320 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1321 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1322 new_apdu->u.searchResponse->resultCount =
1323 &m_client->m_last_resultCount;
1325 new_apdu->u.searchResponse->numberOfRecordsReturned
1326 = odr_intdup(odr_encode(), toget);
1328 new_apdu->u.searchResponse->presentStatus =
1329 odr_intdup(odr_encode(), Z_PresentStatus_success);
1330 new_apdu->u.searchResponse->records = (Z_Records*)
1331 odr_malloc(odr_encode(), sizeof(Z_Records));
1332 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1333 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1334 new_apdu->u.searchResponse->nextResultSetPosition =
1335 odr_intdup(odr_encode(), toget+1);
1336 send_to_client(new_apdu);
1341 yaz_log (LOG_LOG, "%sOptimizing search for small set",
1343 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1344 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1345 pr->referenceId = sr->referenceId;
1346 pr->resultSetId = sr->resultSetName;
1347 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1348 *pr->numberOfRecordsRequested = toget;
1349 pr->recordComposition = comp;
1350 m_client->m_sr_transform = 1;
1355 else // query doesn't match
1357 delete m_client->m_last_query;
1358 m_client->m_last_query = this_query;
1359 m_client->m_last_ok = 0;
1360 m_client->m_cache.clear();
1361 m_client->m_resultSetStartPoint = 0;
1363 xfree (m_client->m_last_resultSetId);
1364 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
1366 m_client->m_last_databases.set(sr->num_databaseNames,
1367 (const char **) sr->databaseNames);
1373 void Yaz_Proxy::inc_request_no()
1375 char *cp = strchr(m_session_str, ' ');
1378 sprintf(cp+1, "%d ", m_request_no);
1381 void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
1385 m_bytes_recv += len;
1387 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
1388 yaz_log (LOG_DEBUG, "%sReceiving %s from client %d bytes",
1389 m_session_str, gdu_name(apdu), len);
1391 if (m_bw_hold_PDU) // double incoming PDU. shutdown now.
1394 m_bw_stat.add_bytes(len);
1395 m_pdu_stat.add_bytes(1);
1397 gettimeofday(&m_time_tv, 0);
1399 int bw_total = m_bw_stat.get_total();
1400 int pdu_total = m_pdu_stat.get_total();
1405 if (bw_total > m_bw_max)
1407 reduce = (bw_total/m_bw_max);
1412 if (pdu_total > m_pdu_max)
1414 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
1415 reduce = (reduce > nreduce) ? reduce : nreduce;
1420 yaz_log(LOG_LOG, "%sdelay=%d bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
1421 m_session_str, reduce, bw_total, pdu_total,
1422 m_bw_max, m_pdu_max);
1424 m_bw_hold_PDU = apdu; // save PDU and signal "on hold"
1425 timeout(reduce); // call us reduce seconds later
1427 else if (apdu->which == Z_GDU_Z3950)
1428 handle_incoming_Z_PDU(apdu->u.z3950);
1429 else if (apdu->which == Z_GDU_HTTP_Request)
1430 handle_incoming_HTTP(apdu->u.HTTP_Request);
1433 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
1435 if (m_max_record_retrieve)
1437 if (apdu->which == Z_APDU_presentRequest)
1439 Z_PresentRequest *pr = apdu->u.presentRequest;
1440 if (pr->numberOfRecordsRequested &&
1441 *pr->numberOfRecordsRequested > m_max_record_retrieve)
1442 *pr->numberOfRecordsRequested = m_max_record_retrieve;
1447 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
1449 const char *addinfo)
1451 Z_Records *rec = (Z_Records *)
1452 odr_malloc (odr, sizeof(*rec));
1454 odr_malloc (odr, sizeof(*err));
1455 Z_DiagRec *drec = (Z_DiagRec *)
1456 odr_malloc (odr, sizeof(*drec));
1457 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1458 odr_malloc (odr, sizeof(*dr));
1460 rec->which = Z_Records_NSD;
1461 rec->u.nonSurrogateDiagnostic = dr;
1462 dr->diagnosticSetId =
1463 yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
1464 dr->condition = err;
1465 dr->which = Z_DefaultDiagFormat_v2Addinfo;
1466 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
1470 Z_APDU *Yaz_Proxy::handle_query_transformation(Z_APDU *apdu)
1472 if (apdu->which == Z_APDU_searchRequest &&
1473 apdu->u.searchRequest->query &&
1474 apdu->u.searchRequest->query->which == Z_Query_type_104 &&
1475 apdu->u.searchRequest->query->u.type_104->which == Z_External_CQL)
1477 Z_RPNQuery *rpnquery = 0;
1478 Z_SearchRequest *sr = apdu->u.searchRequest;
1481 yaz_log(LOG_LOG, "%sCQL: %s", m_session_str,
1482 sr->query->u.type_104->u.cql);
1484 int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
1485 &rpnquery, odr_encode(),
1488 yaz_log(LOG_LOG, "%sNo CQL to RPN table", m_session_str);
1491 yaz_log(LOG_LOG, "%sCQL Conversion error %d", m_session_str, r);
1492 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1494 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1495 new_apdu->u.searchResponse->records =
1496 create_nonSurrogateDiagnostics(odr_encode(),
1497 yaz_diag_srw_to_bib1(r),
1499 *new_apdu->u.searchResponse->searchStatus = 0;
1501 send_to_client(new_apdu);
1507 sr->query->which = Z_Query_type_1;
1508 sr->query->u.type_1 = rpnquery;
1515 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
1517 if (apdu->which == Z_APDU_searchRequest)
1519 Z_SearchRequest *sr = apdu->u.searchRequest;
1523 Yaz_ProxyConfig *cfg = check_reconfigure();
1525 err = cfg->check_query(odr_encode(), m_default_target,
1526 sr->query, &addinfo);
1529 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1531 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1532 new_apdu->u.searchResponse->records =
1533 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1534 *new_apdu->u.searchResponse->searchStatus = 0;
1536 send_to_client(new_apdu);
1544 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
1547 if (apdu->which == Z_APDU_searchRequest)
1549 Z_SearchRequest *sr = apdu->u.searchRequest;
1552 Yaz_ProxyConfig *cfg = check_reconfigure();
1554 Z_RecordComposition rc_temp, *rc = 0;
1555 if (sr->smallSetElementSetNames)
1557 rc_temp.which = Z_RecordComp_simple;
1558 rc_temp.u.simple = sr->smallSetElementSetNames;
1562 char *stylesheet_name = 0;
1564 err = cfg->check_syntax(odr_encode(),
1566 sr->preferredRecordSyntax, rc,
1567 &addinfo, &stylesheet_name, &m_schema);
1568 if (stylesheet_name)
1570 if (m_stylesheet_xsp)
1571 xsltFreeStylesheet(m_stylesheet_xsp);
1572 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1574 m_stylesheet_offset = 0;
1575 xfree(stylesheet_name);
1579 sr->preferredRecordSyntax =
1580 yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN, VAL_USMARC);
1585 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1587 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1588 new_apdu->u.searchResponse->records =
1589 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1590 *new_apdu->u.searchResponse->searchStatus = 0;
1592 send_to_client(new_apdu);
1597 else if (apdu->which == Z_APDU_presentRequest)
1599 Z_PresentRequest *pr = apdu->u.presentRequest;
1602 Yaz_ProxyConfig *cfg = check_reconfigure();
1604 char *stylesheet_name = 0;
1606 err = cfg->check_syntax(odr_encode(), m_default_target,
1607 pr->preferredRecordSyntax,
1608 pr->recordComposition,
1609 &addinfo, &stylesheet_name, &m_schema);
1610 if (stylesheet_name)
1612 if (m_stylesheet_xsp)
1613 xsltFreeStylesheet(m_stylesheet_xsp);
1614 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1616 m_stylesheet_offset = 0;
1617 xfree(stylesheet_name);
1621 pr->preferredRecordSyntax =
1622 yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN, VAL_USMARC);
1627 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1629 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1630 new_apdu->u.presentResponse->records =
1631 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1632 *new_apdu->u.presentResponse->presentStatus =
1633 Z_PresentStatus_failure;
1635 send_to_client(new_apdu);
1643 Z_ElementSetNames *Yaz_Proxy::mk_esn_from_schema(ODR o, const char *schema)
1647 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1648 odr_malloc(o, sizeof(Z_ElementSetNames));
1649 esn->which = Z_ElementSetNames_generic;
1650 esn->u.generic = odr_strdup(o, schema);
1654 void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
1658 odr_destroy(m_s2z_odr_init);
1661 if (m_s2z_odr_search)
1663 odr_destroy(m_s2z_odr_search);
1664 m_s2z_odr_search = 0;
1667 m_http_keepalive = 0;
1669 if (!strcmp(hreq->version, "1.0"))
1671 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1672 if (v && !strcmp(v, "Keep-Alive"))
1673 m_http_keepalive = 1;
1675 m_http_keepalive = 0;
1676 m_http_version = "1.0";
1680 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1681 if (v && !strcmp(v, "close"))
1682 m_http_keepalive = 0;
1684 m_http_keepalive = 1;
1685 m_http_version = "1.1";
1688 Z_SRW_PDU *srw_pdu = 0;
1689 Z_SOAP *soap_package = 0;
1691 Z_SRW_diagnostic *diagnostic = 0;
1692 int num_diagnostic = 0;
1693 if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1695 || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1696 &charset, &diagnostic, &num_diagnostic) == 0)
1698 m_s2z_odr_init = odr_createmem(ODR_ENCODE);
1699 m_s2z_odr_search = odr_createmem(ODR_ENCODE);
1700 m_soap_ns = odr_strdup(m_s2z_odr_search, soap_package->ns);
1701 m_s2z_init_apdu = 0;
1702 m_s2z_search_apdu = 0;
1703 m_s2z_present_apdu = 0;
1705 m_s2z_stylesheet = 0;
1707 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
1709 Z_SRW_searchRetrieveRequest *srw_req = srw_pdu->u.request;
1711 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1712 // recordXPath unsupported.
1713 if (srw_req->recordXPath)
1715 yaz_add_srw_diagnostic(odr_decode(),
1716 &diagnostic, &num_diagnostic,
1719 // must have a query
1720 if (!srw_req->query.cql)
1722 yaz_add_srw_diagnostic(odr_decode(),
1723 &diagnostic, &num_diagnostic,
1727 if (srw_req->sort_type != Z_SRW_sort_type_none)
1729 yaz_add_srw_diagnostic(odr_decode(),
1730 &diagnostic, &num_diagnostic,
1734 if (srw_req->stylesheet)
1736 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1738 // set packing for response records ..
1739 if (srw_req->recordPacking &&
1740 !strcmp(srw_req->recordPacking, "xml"))
1741 m_s2z_packing = Z_SRW_recordPacking_XML;
1743 m_s2z_packing = Z_SRW_recordPacking_string;
1747 Z_SRW_PDU *srw_pdu =
1748 yaz_srw_get(odr_encode(),
1749 Z_SRW_searchRetrieve_response);
1750 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1752 srw_res->diagnostics = diagnostic;
1753 srw_res->num_diagnostics = num_diagnostic;
1754 send_srw_response(srw_pdu);
1758 // prepare search PDU
1759 m_s2z_search_apdu = zget_APDU(m_s2z_odr_search,
1760 Z_APDU_searchRequest);
1761 Z_SearchRequest *z_searchRequest =
1762 m_s2z_search_apdu->u.searchRequest;
1764 z_searchRequest->num_databaseNames = 1;
1765 z_searchRequest->databaseNames = (char**)
1766 odr_malloc(m_s2z_odr_search, sizeof(char *));
1767 z_searchRequest->databaseNames[0] = odr_strdup(m_s2z_odr_search,
1770 // query transformation
1771 Z_Query *query = (Z_Query *)
1772 odr_malloc(m_s2z_odr_search, sizeof(Z_Query));
1773 z_searchRequest->query = query;
1775 if (srw_req->query_type == Z_SRW_query_type_cql)
1777 Z_External *ext = (Z_External *)
1778 odr_malloc(m_s2z_odr_search, sizeof(*ext));
1779 ext->direct_reference =
1780 odr_getoidbystr(m_s2z_odr_search, "1.2.840.10003.16.2");
1781 ext->indirect_reference = 0;
1782 ext->descriptor = 0;
1783 ext->which = Z_External_CQL;
1784 ext->u.cql = srw_req->query.cql;
1786 query->which = Z_Query_type_104;
1787 query->u.type_104 = ext;
1789 else if (srw_req->query_type == Z_SRW_query_type_pqf)
1791 Z_RPNQuery *RPNquery;
1792 YAZ_PQF_Parser pqf_parser;
1794 pqf_parser = yaz_pqf_create ();
1796 RPNquery = yaz_pqf_parse (pqf_parser, m_s2z_odr_search,
1797 srw_req->query.pqf);
1800 const char *pqf_msg;
1802 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1803 yaz_log(LOG_LOG, "%*s^\n", off+4, "");
1804 yaz_log(LOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
1806 send_to_srw_client_error(10, 0);
1809 query->which = Z_Query_type_1;
1810 query->u.type_1 = RPNquery;
1812 yaz_pqf_destroy (pqf_parser);
1816 send_to_srw_client_error(7, "query");
1821 m_s2z_present_apdu = 0;
1823 if (srw_req->maximumRecords)
1824 max = *srw_req->maximumRecords;
1826 if (srw_req->startRecord)
1827 start = *srw_req->startRecord;
1830 // Some backend, such as Voyager doesn't honor piggyback
1831 // So we use present always (0 &&).
1832 if (0 && start <= 1) // Z39.50 piggyback
1834 *z_searchRequest->smallSetUpperBound = max;
1835 *z_searchRequest->mediumSetPresentNumber = max;
1836 *z_searchRequest->largeSetLowerBound = 2000000000; // 2e9
1838 z_searchRequest->preferredRecordSyntax =
1839 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1841 if (srw_req->recordSchema)
1843 z_searchRequest->smallSetElementSetNames =
1844 z_searchRequest->mediumSetElementSetNames =
1845 mk_esn_from_schema(m_s2z_odr_search,
1846 srw_req->recordSchema);
1849 else // Z39.50 present
1851 m_s2z_present_apdu = zget_APDU(m_s2z_odr_search,
1852 Z_APDU_presentRequest);
1853 Z_PresentRequest *z_presentRequest =
1854 m_s2z_present_apdu->u.presentRequest;
1855 *z_presentRequest->resultSetStartPoint = start;
1856 *z_presentRequest->numberOfRecordsRequested = max;
1857 z_presentRequest->preferredRecordSyntax =
1858 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1860 if (srw_req->recordSchema)
1862 z_presentRequest->recordComposition =
1863 (Z_RecordComposition *)
1864 odr_malloc(m_s2z_odr_search,
1865 sizeof(Z_RecordComposition));
1866 z_presentRequest->recordComposition->which =
1867 Z_RecordComp_simple;
1868 z_presentRequest->recordComposition->u.simple =
1869 mk_esn_from_schema(m_s2z_odr_search,
1870 srw_req->recordSchema);
1876 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1877 Z_APDU_initRequest);
1879 // prevent m_initRequest_apdu memory from being grabbed
1880 // in Yaz_Proxy::handle_incoming_Z_PDU
1881 m_initRequest_apdu = m_s2z_init_apdu;
1882 handle_incoming_Z_PDU(m_s2z_init_apdu);
1887 handle_incoming_Z_PDU(m_s2z_search_apdu);
1891 else if (srw_pdu->which == Z_SRW_explain_request)
1893 Z_SRW_explainRequest *srw_req = srw_pdu->u.explain_request;
1895 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1898 if (srw_req->stylesheet)
1900 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1902 if (srw_req->recordPacking &&
1903 !strcmp(srw_req->recordPacking, "xml"))
1904 m_s2z_packing = Z_SRW_recordPacking_XML;
1906 m_s2z_packing = Z_SRW_recordPacking_string;
1910 send_srw_explain_response(diagnostic, num_diagnostic);
1916 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1917 Z_APDU_initRequest);
1919 // prevent m_initRequest_apdu memory from being grabbed
1920 // in Yaz_Proxy::handle_incoming_Z_PDU
1921 m_initRequest_apdu = m_s2z_init_apdu;
1922 handle_incoming_Z_PDU(m_s2z_init_apdu);
1925 send_srw_explain_response(0, 0);
1928 else if (srw_pdu->which == Z_SRW_scan_request)
1930 m_s2z_database = odr_strdup(m_s2z_odr_init,
1931 srw_pdu->u.scan_request->database);
1933 yaz_add_srw_diagnostic(odr_decode(),
1934 &diagnostic, &num_diagnostic,
1936 Z_SRW_PDU *srw_pdu =
1937 yaz_srw_get(odr_encode(),
1938 Z_SRW_scan_response);
1939 Z_SRW_scanResponse *srw_res = srw_pdu->u.scan_response;
1941 srw_res->diagnostics = diagnostic;
1942 srw_res->num_diagnostics = num_diagnostic;
1943 send_srw_response(srw_pdu);
1950 send_to_srw_client_error(4, 0);
1954 Z_GDU *p = z_get_HTTP_Response(odr_encode(), 400);
1959 void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
1961 Z_ReferenceId **refid = get_referenceIdP(apdu);
1962 nmem_reset(m_referenceId_mem);
1963 if (refid && *refid)
1965 m_referenceId = (Z_ReferenceId *)
1966 nmem_malloc(m_referenceId_mem, sizeof(*m_referenceId));
1967 m_referenceId->len = m_referenceId->size = (*refid)->len;
1968 m_referenceId->buf = (unsigned char *)
1969 nmem_malloc(m_referenceId_mem, (*refid)->len);
1970 memcpy(m_referenceId->buf, (*refid)->buf, (*refid)->len);
1975 if (!m_client && m_invalid_session)
1977 m_apdu_invalid_session = apdu;
1978 m_mem_invalid_session = odr_extract_mem(odr_decode());
1979 apdu = m_initRequest_apdu;
1982 // Determine our client.
1983 Z_OtherInformation **oi;
1984 get_otherInfoAPDU(apdu, &oi);
1985 m_client = get_client(apdu, get_cookie(oi), get_proxy(oi));
1991 m_client->m_server = this;
1993 if (apdu->which == Z_APDU_initRequest)
1995 if (apdu->u.initRequest->implementationId)
1996 yaz_log(LOG_LOG, "%simplementationId: %s",
1997 m_session_str, apdu->u.initRequest->implementationId);
1998 if (apdu->u.initRequest->implementationName)
1999 yaz_log(LOG_LOG, "%simplementationName: %s",
2000 m_session_str, apdu->u.initRequest->implementationName);
2001 if (apdu->u.initRequest->implementationVersion)
2002 yaz_log(LOG_LOG, "%simplementationVersion: %s",
2003 m_session_str, apdu->u.initRequest->implementationVersion);
2004 if (m_initRequest_apdu == 0)
2006 if (m_initRequest_mem)
2007 nmem_destroy(m_initRequest_mem);
2008 m_initRequest_apdu = apdu;
2009 m_initRequest_mem = odr_extract_mem(odr_decode());
2011 m_initRequest_preferredMessageSize = *apdu->u.initRequest->
2012 preferredMessageSize;
2013 *apdu->u.initRequest->preferredMessageSize = 1024*1024;
2014 m_initRequest_maximumRecordSize = *apdu->u.initRequest->
2016 *apdu->u.initRequest->maximumRecordSize = 1024*1024;
2018 // save init options for the response..
2019 m_initRequest_options = apdu->u.initRequest->options;
2021 apdu->u.initRequest->options =
2022 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2023 sizeof(Odr_bitmask));
2024 ODR_MASK_ZERO(apdu->u.initRequest->options);
2026 for (i = 0; i<= 24; i++)
2027 ODR_MASK_SET(apdu->u.initRequest->options, i);
2028 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2029 Z_Options_negotiationModel);
2030 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2031 Z_Options_concurrentOperations);
2034 m_initRequest_version = apdu->u.initRequest->protocolVersion;
2035 apdu->u.initRequest->protocolVersion =
2036 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2037 sizeof(Odr_bitmask));
2038 ODR_MASK_ZERO(apdu->u.initRequest->protocolVersion);
2040 for (i = 0; i<= 8; i++)
2041 ODR_MASK_SET(apdu->u.initRequest->protocolVersion, i);
2043 if (m_client->m_init_flag)
2045 if (handle_init_response_for_invalid_session(apdu))
2047 if (m_client->m_initResponse)
2049 Z_APDU *apdu2 = m_client->m_initResponse;
2050 apdu2->u.initResponse->otherInfo = 0;
2051 if (m_client->m_cookie && *m_client->m_cookie)
2052 set_otherInformationString(apdu2, VAL_COOKIE, 1,
2053 m_client->m_cookie);
2054 apdu2->u.initResponse->referenceId =
2055 apdu->u.initRequest->referenceId;
2056 apdu2->u.initResponse->options = m_client->m_initResponse_options;
2057 apdu2->u.initResponse->protocolVersion =
2058 m_client->m_initResponse_version;
2060 send_to_client(apdu2);
2064 m_client->m_init_flag = 1;
2066 handle_max_record_retrieve(apdu);
2069 apdu = handle_syntax_validation(apdu);
2072 apdu = handle_query_transformation(apdu);
2075 apdu = handle_query_validation(apdu);
2078 apdu = result_set_optimize(apdu);
2081 m_client->timeout(m_target_idletime); // mark it active even
2082 // though we didn't use it
2086 // delete other info part from PDU before sending to target
2087 get_otherInfoAPDU(apdu, &oi);
2091 if (apdu->which == Z_APDU_presentRequest &&
2092 m_client->m_resultSetStartPoint == 0)
2094 Z_PresentRequest *pr = apdu->u.presentRequest;
2095 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
2096 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
2098 m_client->m_resultSetStartPoint = 0;
2100 if (m_client->send_to_target(apdu) < 0)
2107 m_client->m_waiting = 1;
2110 void Yaz_Proxy::connectNotify()
2114 void Yaz_Proxy::shutdown()
2116 m_invalid_session = 0;
2117 // only keep if keep_alive flag is set...
2119 m_client->m_pdu_recv < m_keepalive_limit_pdu &&
2120 m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
2121 m_client->m_waiting == 0)
2123 yaz_log(LOG_LOG, "%sShutdown (client to proxy) keepalive %s",
2125 m_client->get_hostname());
2126 yaz_log(LOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
2127 m_session_str, m_client->m_pdu_recv,
2128 m_client->m_bytes_sent + m_client->m_bytes_recv,
2129 m_keepalive_limit_bw, m_keepalive_limit_pdu);
2130 assert (m_client->m_waiting != 2);
2131 // Tell client (if any) that no server connection is there..
2132 m_client->m_server = 0;
2133 m_invalid_session = 0;
2137 yaz_log (LOG_LOG, "%sShutdown (client to proxy) close %s",
2139 m_client->get_hostname());
2140 assert (m_client->m_waiting != 2);
2145 yaz_log (LOG_LOG, "%sshutdown (client to proxy) bad state",
2151 yaz_log (LOG_LOG, "%sShutdown (client to proxy)",
2155 m_parent->pre_init();
2159 const char *Yaz_ProxyClient::get_session_str()
2163 return m_server->get_session_str();
2166 void Yaz_ProxyClient::shutdown()
2168 yaz_log (LOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
2174 void Yaz_Proxy::failNotify()
2177 yaz_log (LOG_LOG, "%sConnection closed by client",
2182 void Yaz_ProxyClient::failNotify()
2185 m_server->inc_request_no();
2186 yaz_log (LOG_LOG, "%sConnection closed by target %s",
2187 get_session_str(), get_hostname());
2191 void Yaz_ProxyClient::connectNotify()
2193 const char *s = get_session_str();
2194 const char *h = get_hostname();
2195 yaz_log (LOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
2197 timeout(m_target_idletime);
2202 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
2203 *the_PDU_Observable, int fd)
2205 return new Yaz_ProxyClient(the_PDU_Observable, 0);
2208 Yaz_ProxyClient::~Yaz_ProxyClient()
2213 m_next->m_prev = m_prev;
2214 m_waiting = 2; // for debugging purposes only.
2215 odr_destroy(m_init_odr);
2216 delete m_last_query;
2217 xfree (m_last_resultSetId);
2221 void Yaz_ProxyClient::pre_init_client()
2223 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
2224 Z_InitRequest *req = apdu->u.initRequest;
2227 for (i = 0; i<= 24; i++)
2228 ODR_MASK_SET(req->options, i);
2229 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2230 Z_Options_negotiationModel);
2231 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2232 Z_Options_concurrentOperations);
2233 for (i = 0; i<= 10; i++)
2234 ODR_MASK_SET(req->protocolVersion, i);
2236 if (send_to_target(apdu) < 0)
2247 void Yaz_Proxy::pre_init()
2250 const char *name = 0;
2251 const char *zurl_in_use[MAX_ZURL_PLEX];
2252 int limit_bw, limit_pdu, limit_req;
2253 int target_idletime, client_idletime;
2255 int keepalive_limit_bw, keepalive_limit_pdu;
2257 const char *cql2rpn = 0;
2259 Yaz_ProxyConfig *cfg = check_reconfigure();
2263 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
2268 for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
2269 &limit_bw, &limit_pdu, &limit_req,
2270 &target_idletime, &client_idletime,
2272 &keepalive_limit_bw,
2273 &keepalive_limit_pdu,
2280 for (j = 0; zurl_in_use[j]; j++)
2284 int spare_waiting = 0;
2287 for (c = m_clientPool; c; c = c->m_next)
2289 if (!strcmp(zurl_in_use[j], c->get_hostname()))
2291 if (c->m_cookie == 0)
2293 if (c->m_server == 0)
2305 yaz_log(LOG_LOG, "%spre-init %s %s use=%d other=%d spare=%d "
2306 "sparew=%d preinit=%d",m_session_str,
2307 name, zurl_in_use[j], in_use, other,
2308 spare, spare_waiting, pre_init);
2309 if (spare < pre_init)
2311 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
2312 c->m_next = m_clientPool;
2314 c->m_next->m_prev = &c->m_next;
2316 c->m_prev = &m_clientPool;
2318 if (m_log_mask & PROXY_LOG_APDU_SERVER)
2319 c->set_APDU_yazlog(1);
2321 c->set_APDU_yazlog(0);
2323 if (c->client(zurl_in_use[j]))
2331 c->m_target_idletime = target_idletime;
2332 c->m_seqno = m_seqno++;
2339 void Yaz_Proxy::timeoutNotify()
2345 timeout(m_client_idletime);
2346 Z_GDU *apdu = m_bw_hold_PDU;
2349 if (apdu->which == Z_GDU_Z3950)
2350 handle_incoming_Z_PDU(apdu->u.z3950);
2351 else if (apdu->which == Z_GDU_HTTP_Request)
2352 handle_incoming_HTTP(apdu->u.HTTP_Request);
2354 else if (m_stylesheet_nprl)
2355 convert_xsl_delay();
2360 yaz_log (LOG_LOG, "%sTimeout (client to proxy)", m_session_str);
2371 void Yaz_Proxy::markInvalid()
2374 m_invalid_session = 1;
2377 void Yaz_ProxyClient::timeoutNotify()
2380 m_server->inc_request_no();
2382 yaz_log (LOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
2386 if (m_server && m_init_flag)
2388 // target timed out in a session that was properly initialized
2389 // server object stay alive but we mark it as invalid so it
2390 // gets initialized again
2391 m_server->markInvalid();
2397 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
2398 Yaz_Proxy *parent) :
2399 Yaz_Z_Assoc (the_PDU_Observable)
2406 m_last_resultSetId = 0;
2407 m_last_resultCount = 0;
2411 m_init_odr = odr_createmem (ODR_DECODE);
2413 m_initResponse_options = 0;
2414 m_initResponse_version = 0;
2415 m_initResponse_preferredMessageSize = 0;
2416 m_initResponse_maximumRecordSize = 0;
2417 m_resultSetStartPoint = 0;
2418 m_bytes_sent = m_bytes_recv = 0;
2422 m_target_idletime = 600;
2426 const char *Yaz_Proxy::option(const char *name, const char *value)
2428 if (!strcmp (name, "optimize")) {
2431 m_optimize = xstrdup (value);
2438 void Yaz_ProxyClient::recv_HTTP_response(Z_HTTP_Response *apdu, int len)
2443 void Yaz_ProxyClient::recv_GDU(Z_GDU *apdu, int len)
2445 if (apdu->which == Z_GDU_Z3950)
2446 recv_Z_PDU(apdu->u.z3950, len);
2447 else if (apdu->which == Z_GDU_HTTP_Response)
2448 recv_HTTP_response(apdu->u.HTTP_Response, len);
2453 int Yaz_Proxy::handle_init_response_for_invalid_session(Z_APDU *apdu)
2455 if (!m_invalid_session)
2457 m_invalid_session = 0;
2458 handle_incoming_Z_PDU(m_apdu_invalid_session);
2459 assert (m_mem_invalid_session);
2460 nmem_destroy(m_mem_invalid_session);
2461 m_mem_invalid_session = 0;
2465 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
2467 m_bytes_recv += len;
2471 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
2472 yaz_log (LOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
2473 apdu_name(apdu), get_hostname(), len);
2474 if (apdu->which == Z_APDU_initResponse)
2476 if (!m_server) // if this is a pre init session , check for more
2478 NMEM nmem = odr_extract_mem (odr_decode());
2479 odr_reset (m_init_odr);
2480 nmem_transfer (m_init_odr->mem, nmem);
2481 m_initResponse = apdu;
2482 m_initResponse_options = apdu->u.initResponse->options;
2483 m_initResponse_version = apdu->u.initResponse->protocolVersion;
2484 m_initResponse_preferredMessageSize =
2485 *apdu->u.initResponse->preferredMessageSize;
2486 m_initResponse_maximumRecordSize =
2487 *apdu->u.initResponse->maximumRecordSize;
2489 Z_InitResponse *ir = apdu->u.initResponse;
2490 char *im0 = ir->implementationName;
2493 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
2500 strcat(im1, "(YAZ Proxy)");
2501 ir->implementationName = im1;
2503 nmem_destroy (nmem);
2505 if (m_server && m_server->handle_init_response_for_invalid_session(apdu))
2508 if (apdu->which == Z_APDU_searchResponse)
2510 Z_SearchResponse *sr = apdu->u.searchResponse;
2511 m_last_resultCount = *sr->resultCount;
2512 int status = *sr->searchStatus;
2513 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
2517 if (sr->records && sr->records->which == Z_Records_DBOSD)
2519 m_cache.add(odr_decode(),
2520 sr->records->u.databaseOrSurDiagnostics, 1,
2525 if (apdu->which == Z_APDU_presentResponse)
2527 Z_PresentResponse *pr = apdu->u.presentResponse;
2531 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2532 Z_SearchResponse *sr = new_apdu->u.searchResponse;
2533 sr->referenceId = pr->referenceId;
2534 *sr->resultCount = m_last_resultCount;
2535 sr->records = pr->records;
2536 sr->nextResultSetPosition = pr->nextResultSetPosition;
2537 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
2541 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
2543 m_cache.add(odr_decode(),
2544 pr->records->u.databaseOrSurDiagnostics,
2545 m_resultSetStartPoint, -1);
2546 m_resultSetStartPoint = 0;
2550 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
2553 m_server->send_to_client(apdu);
2555 if (apdu->which == Z_APDU_close)
2561 int Yaz_Proxy::server(const char *addr)
2563 int r = Yaz_Z_Assoc::server(addr);
2566 yaz_log(LOG_LOG, "%sStarted proxy " VERSION " on %s", m_session_str, addr);