1 /* $Id: yaz-proxy.cpp,v 1.4 2004-03-30 18:14:13 adam Exp $
2 Copyright (c) 1998-2004, Index Data.
4 This file is part of the yaz-proxy.
6 Zebra is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Zebra; see the file LICENSE.proxy. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
29 #include <sys/types.h>
33 #include <yaz/marcdisp.h>
34 #include <yaz/yaz-iconv.h>
36 #include <yaz/diagbib1.h>
37 #include <yaz++/proxy/proxy.h>
38 #include <yaz/pquery.h>
40 static const char *apdu_name(Z_APDU *apdu)
44 case Z_APDU_initRequest:
46 case Z_APDU_initResponse:
47 return "initResponse";
48 case Z_APDU_searchRequest:
49 return "searchRequest";
50 case Z_APDU_searchResponse:
51 return "searchResponse";
52 case Z_APDU_presentRequest:
53 return "presentRequest";
54 case Z_APDU_presentResponse:
55 return "presentResponse";
56 case Z_APDU_deleteResultSetRequest:
57 return "deleteResultSetRequest";
58 case Z_APDU_deleteResultSetResponse:
59 return "deleteResultSetResponse";
60 case Z_APDU_scanRequest:
62 case Z_APDU_scanResponse:
63 return "scanResponse";
64 case Z_APDU_sortRequest:
66 case Z_APDU_sortResponse:
67 return "sortResponse";
68 case Z_APDU_extendedServicesRequest:
69 return "extendedServicesRequest";
70 case Z_APDU_extendedServicesResponse:
71 return "extendedServicesResponse";
78 static const char *gdu_name(Z_GDU *gdu)
83 return apdu_name(gdu->u.z3950);
84 case Z_GDU_HTTP_Request:
85 return "HTTP Request";
86 case Z_GDU_HTTP_Response:
87 return "HTTP Response";
89 return "Unknown request/response";
92 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
94 Yaz_Z_Assoc(the_PDU_Observable), m_bw_stat(60), m_pdu_stat(60)
96 m_PDU_Observable = the_PDU_Observable;
101 m_keepalive_limit_bw = 500000;
102 m_keepalive_limit_pdu = 1000;
104 m_default_target = 0;
105 m_proxy_authentication = 0;
109 m_client_idletime = 600;
110 m_target_idletime = 600;
111 m_optimize = xstrdup ("1");
112 strcpy(m_session_str, "0 ");
119 m_max_record_retrieve = 0;
123 m_invalid_session = 0;
125 m_referenceId_mem = nmem_create();
128 m_stylesheet_xsp = 0;
129 m_stylesheet_nprl = 0;
130 m_s2z_stylesheet = 0;
133 m_initRequest_apdu = 0;
134 m_initRequest_mem = 0;
135 m_initRequest_preferredMessageSize = 0;
136 m_initRequest_maximumRecordSize = 0;
137 m_initRequest_options = 0;
138 m_initRequest_version = 0;
139 m_apdu_invalid_session = 0;
140 m_mem_invalid_session = 0;
142 m_s2z_odr_search = 0;
144 m_s2z_search_apdu = 0;
145 m_s2z_present_apdu = 0;
146 m_http_keepalive = 0;
149 m_s2z_packing = Z_SRW_recordPacking_string;
150 #if HAVE_GETTIMEOFDAY
151 m_time_tv.tv_sec = 0;
152 m_time_tv.tv_usec = 0;
158 Yaz_Proxy::~Yaz_Proxy()
160 yaz_log(LOG_LOG, "%sClosed %d/%d sent/recv bytes total", m_session_str,
161 m_bytes_sent, m_bytes_recv);
162 nmem_destroy(m_initRequest_mem);
163 nmem_destroy(m_mem_invalid_session);
164 nmem_destroy(m_referenceId_mem);
166 xfree (m_proxyTarget);
167 xfree (m_default_target);
168 xfree (m_proxy_authentication);
172 if (m_stylesheet_xsp)
173 xsltFreeStylesheet(m_stylesheet_xsp);
177 odr_destroy(m_s2z_odr_init);
178 if (m_s2z_odr_search)
179 odr_destroy(m_s2z_odr_search);
185 int Yaz_Proxy::set_config(const char *config)
188 m_config = new Yaz_ProxyConfig();
189 xfree(m_config_fname);
190 m_config_fname = xstrdup(config);
191 int r = m_config->read_xml(config);
193 m_config->get_generic_info(&m_log_mask, &m_max_clients);
197 void Yaz_Proxy::set_default_target(const char *target)
199 xfree (m_default_target);
200 m_default_target = 0;
202 m_default_target = (char *) xstrdup (target);
205 void Yaz_Proxy::set_proxy_authentication (const char *auth)
207 xfree (m_proxy_authentication);
208 m_proxy_authentication = 0;
210 m_proxy_authentication = (char *) xstrdup (auth);
213 Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
216 return m_parent->check_reconfigure();
218 Yaz_ProxyConfig *cfg = m_config;
221 yaz_log(LOG_LOG, "reconfigure");
223 if (m_config_fname && cfg)
225 yaz_log(LOG_LOG, "reconfigure config %s", m_config_fname);
226 int r = cfg->read_xml(m_config_fname);
228 yaz_log(LOG_WARN, "reconfigure failed");
232 cfg->get_generic_info(&m_log_mask, &m_max_clients);
236 yaz_log(LOG_LOG, "reconfigure");
242 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
243 *the_PDU_Observable, int fd)
246 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable, this);
247 new_proxy->m_config = 0;
248 new_proxy->m_config_fname = 0;
249 new_proxy->timeout(m_client_idletime);
250 new_proxy->m_target_idletime = m_target_idletime;
251 new_proxy->set_default_target(m_default_target);
252 new_proxy->m_max_clients = m_max_clients;
253 new_proxy->m_log_mask = m_log_mask;
254 new_proxy->set_APDU_log(get_APDU_log());
255 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
256 new_proxy->set_APDU_yazlog(1);
258 new_proxy->set_APDU_yazlog(0);
259 new_proxy->set_proxy_authentication(m_proxy_authentication);
260 sprintf(new_proxy->m_session_str, "%ld:%d ", (long) time(0), m_session_no);
262 yaz_log (LOG_LOG, "%sNew session %s", new_proxy->m_session_str,
263 the_PDU_Observable->getpeername());
267 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
270 Z_OtherInformationUnit *oi;
272 ent.proto = PROTO_Z3950;
273 ent.oclass = CLASS_USERINFO;
274 ent.value = (oid_value) VAL_COOKIE;
275 assert (oid_ent_to_oid (&ent, oid));
277 if (oid_ent_to_oid (&ent, oid) &&
278 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
279 oi->which == Z_OtherInfo_characterInfo)
280 return oi->information.characterInfo;
284 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
287 Z_OtherInformationUnit *oi;
289 ent.proto = PROTO_Z3950;
290 ent.oclass = CLASS_USERINFO;
291 ent.value = (oid_value) VAL_PROXY;
292 if (oid_ent_to_oid (&ent, oid) &&
293 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
294 oi->which == Z_OtherInfo_characterInfo)
295 return oi->information.characterInfo;
299 const char *Yaz_Proxy::load_balance(const char **url)
301 int zurl_in_use[MAX_ZURL_PLEX];
302 int zurl_in_spare[MAX_ZURL_PLEX];
306 for (i = 0; i<MAX_ZURL_PLEX; i++)
309 zurl_in_spare[i] = 0;
311 for (c = m_parent->m_clientPool; c; c = c->m_next)
313 for (i = 0; url[i]; i++)
314 if (!strcmp(url[i], c->get_hostname()))
317 if (c->m_cookie == 0 && c->m_server == 0 && c->m_waiting == 0)
321 int min_use = 100000;
322 int spare_for_min = 0;
324 const char *ret_min = 0;
325 const char *ret_spare = 0;
326 for (i = 0; url[i]; i++)
328 yaz_log(LOG_DEBUG, "%szurl=%s use=%d spare=%d",
329 m_session_str, url[i], zurl_in_use[i], zurl_in_spare[i]);
330 if (min_use > zurl_in_use[i])
333 min_use = zurl_in_use[i];
334 spare_for_min = zurl_in_spare[i];
336 if (max_spare < zurl_in_spare[i])
339 max_spare = zurl_in_spare[i];
342 // use the one with minimum connections if spare is > 3
343 if (spare_for_min > 3)
345 // use one with most spares (if any)
351 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
352 const char *proxy_host)
355 Yaz_Proxy *parent = m_parent;
356 Yaz_ProxyClient *c = m_client;
360 const char *url[MAX_ZURL_PLEX];
361 Yaz_ProxyConfig *cfg = check_reconfigure();
365 /* only to be enabled for debugging... */
366 if (!strcmp(proxy_host, "stop"))
369 xfree(m_default_target);
370 m_default_target = xstrdup(proxy_host);
371 proxy_host = m_default_target;
373 int client_idletime = -1;
374 const char *cql2rpn_fname = 0;
375 url[0] = m_default_target;
380 cfg->get_target_info(proxy_host, url, &m_bw_max,
381 &m_pdu_max, &m_max_record_retrieve,
382 &m_target_idletime, &client_idletime,
383 &parent->m_max_clients,
384 &m_keepalive_limit_bw,
385 &m_keepalive_limit_pdu,
389 if (client_idletime != -1)
391 m_client_idletime = client_idletime;
392 timeout(m_client_idletime);
395 m_cql2rpn.set_pqf_file(cql2rpn_fname);
398 yaz_log(LOG_LOG, "%sNo default target", m_session_str);
401 // we don't handle multiplexing for cookie session, so we just
402 // pick the first one in this case (anonymous users will be able
403 // to use any backend)
404 if (cookie && *cookie)
405 m_proxyTarget = (char*) xstrdup(url[0]);
407 m_proxyTarget = (char*) xstrdup(load_balance(url));
409 if (cookie && *cookie)
410 { // search in sessions with a cookie
411 for (c = parent->m_clientPool; c; c = c->m_next)
414 assert (*c->m_prev == c);
415 if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
416 !strcmp(m_proxyTarget, c->get_hostname()))
419 // The following handles "cancel"
420 // If connection is busy (waiting for PDU) and
421 // we have an initRequest we can safely do re-open
422 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
424 yaz_log (LOG_LOG, "%s REOPEN target=%s", m_session_str,
431 c->m_last_resultCount = 0;
432 c->m_sr_transform = 0;
434 c->m_resultSetStartPoint = 0;
435 c->m_target_idletime = m_target_idletime;
436 if (c->client(m_proxyTarget))
443 c->m_seqno = parent->m_seqno;
444 if (c->m_server && c->m_server != this)
445 c->m_server->m_client = 0;
448 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
455 // don't have a client session yet. Search in session w/o cookie
456 for (c = parent->m_clientPool; c; c = c->m_next)
459 assert (*c->m_prev == c);
460 if (c->m_server == 0 && c->m_cookie == 0 &&
462 !strcmp(m_proxyTarget, c->get_hostname()))
465 yaz_log (LOG_LOG, "%sREUSE %d %s",
466 m_session_str, parent->m_seqno, c->get_hostname());
468 c->m_seqno = parent->m_seqno;
469 assert(c->m_server == 0);
472 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
473 c->set_APDU_yazlog(1);
475 c->set_APDU_yazlog(0);
487 if (apdu->which != Z_APDU_initRequest)
489 yaz_log (LOG_LOG, "%sno init request as first PDU", m_session_str);
492 Z_InitRequest *initRequest = apdu->u.initRequest;
494 if (!initRequest->idAuthentication)
496 if (m_proxy_authentication)
498 initRequest->idAuthentication =
499 (Z_IdAuthentication *)
500 odr_malloc (odr_encode(),
501 sizeof(*initRequest->idAuthentication));
502 initRequest->idAuthentication->which =
503 Z_IdAuthentication_open;
504 initRequest->idAuthentication->u.open =
505 odr_strdup (odr_encode(), m_proxy_authentication);
508 // go through list of clients - and find the lowest/oldest one.
509 Yaz_ProxyClient *c_min = 0;
511 int no_of_clients = 0;
512 if (parent->m_clientPool)
513 yaz_log (LOG_DEBUG, "Existing sessions");
514 for (c = parent->m_clientPool; c; c = c->m_next)
516 yaz_log (LOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
517 c->m_waiting, c->get_hostname(),
518 c->m_cookie ? c->m_cookie : "");
520 if (min_seq < 0 || c->m_seqno < min_seq)
522 min_seq = c->m_seqno;
526 if (no_of_clients >= parent->m_max_clients)
529 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
531 yaz_log (LOG_LOG, "%sMAXCLIENTS %d Destroy %d",
532 m_session_str, parent->m_max_clients, c->m_seqno);
533 if (c->m_server && c->m_server != this)
539 yaz_log (LOG_LOG, "%sMAXCLIENTS %d Reuse %d %d %s",
540 m_session_str, parent->m_max_clients,
541 c->m_seqno, parent->m_seqno, c->get_hostname());
545 c->m_cookie = xstrdup(cookie);
546 c->m_seqno = parent->m_seqno;
547 if (c->m_server && c->m_server != this)
549 c->m_server->m_client = 0;
553 c->m_target_idletime = m_target_idletime;
554 c->timeout(m_target_idletime);
556 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
557 c->set_APDU_yazlog(1);
559 c->set_APDU_yazlog(0);
566 yaz_log (LOG_LOG, "%sNEW %d %s",
567 m_session_str, parent->m_seqno, m_proxyTarget);
568 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), parent);
569 c->m_next = parent->m_clientPool;
571 c->m_next->m_prev = &c->m_next;
572 parent->m_clientPool = c;
573 c->m_prev = &parent->m_clientPool;
579 c->m_cookie = xstrdup(cookie);
581 c->m_seqno = parent->m_seqno;
583 c->m_last_resultCount = 0;
586 c->m_sr_transform = 0;
588 c->m_resultSetStartPoint = 0;
590 if (c->client(m_proxyTarget))
595 c->m_target_idletime = m_target_idletime;
598 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
599 c->set_APDU_yazlog(1);
601 c->set_APDU_yazlog(0);
603 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
607 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
610 for (i = 0; i<num; i++)
613 Z_DefaultDiagFormat *r;
614 Z_DiagRec *p = pp[i];
615 if (p->which != Z_DiagRec_defaultFormat)
617 yaz_log(LOG_LOG, "%sError no diagnostics", m_session_str);
621 r = p->u.defaultFormat;
622 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
623 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
624 yaz_log(LOG_LOG, "%sError unknown diagnostic set", m_session_str);
627 case Z_DefaultDiagFormat_v2Addinfo:
628 yaz_log(LOG_LOG, "%sError %d %s:%s",
630 *r->condition, diagbib1_str(*r->condition),
633 case Z_DefaultDiagFormat_v3Addinfo:
634 yaz_log(LOG_LOG, "%sError %d %s:%s",
636 *r->condition, diagbib1_str(*r->condition),
643 int Yaz_Proxy::convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu)
645 if (!m_stylesheet_xsp || p->num_records <= 0)
646 return 0; /* no XSLT to be done ... */
648 m_stylesheet_offset = 0;
649 m_stylesheet_nprl = p;
650 m_stylesheet_apdu = apdu;
655 void Yaz_Proxy::convert_xsl_delay()
657 Z_NamePlusRecord *npr = m_stylesheet_nprl->records[m_stylesheet_offset];
659 if (npr->which == Z_NamePlusRecord_databaseRecord)
661 Z_External *r = npr->u.databaseRecord;
662 if (r->which == Z_External_octet)
665 fwrite((char*) r->u.octet_aligned->buf, 1, r->u.octet_aligned->len, stdout);
667 xmlDocPtr res, doc = xmlParseMemory(
668 (char*) r->u.octet_aligned->buf,
669 r->u.octet_aligned->len);
672 yaz_log(LOG_LOG, "%sXSLT convert %d",
673 m_session_str, m_stylesheet_offset);
674 res = xsltApplyStylesheet(m_stylesheet_xsp, doc, 0);
680 xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
682 m_stylesheet_nprl->records[m_stylesheet_offset]->
684 z_ext_record(odr_encode(), VAL_TEXT_XML,
685 (char*) out_buf, out_len);
694 m_stylesheet_offset++;
695 if (m_stylesheet_offset == m_stylesheet_nprl->num_records)
697 m_stylesheet_nprl = 0;
699 if (m_stylesheet_xsp)
700 xsltFreeStylesheet(m_stylesheet_xsp);
702 m_stylesheet_xsp = 0;
703 timeout(m_client_idletime);
704 send_PDU_convert(m_stylesheet_apdu);
710 void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
714 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
715 yaz_marc_t mt = yaz_marc_create();
716 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
717 yaz_marc_iconv(mt, cd);
718 for (i = 0; i < p->num_records; i++)
720 Z_NamePlusRecord *npr = p->records[i];
721 if (npr->which == Z_NamePlusRecord_databaseRecord)
723 Z_External *r = npr->u.databaseRecord;
724 if (r->which == Z_External_octet)
728 if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
729 r->u.octet_aligned->len,
732 npr->u.databaseRecord = z_ext_record(odr_encode(),
741 yaz_marc_destroy(mt);
744 void Yaz_Proxy::logtime()
746 #if HAVE_GETTIMEOFDAY
747 if (m_time_tv.tv_sec)
750 gettimeofday(&tv, 0);
751 long diff = (tv.tv_sec - m_time_tv.tv_sec)*1000000 +
752 (tv.tv_usec - m_time_tv.tv_usec);
754 yaz_log(LOG_LOG, "%sElapsed %ld.%03ld", m_session_str,
755 diff/1000000, (diff/1000)%1000);
757 m_time_tv.tv_sec = 0;
758 m_time_tv.tv_usec = 0;
762 int Yaz_Proxy::send_http_response(int code)
764 ODR o = odr_encode();
765 Z_GDU *gdu = z_get_HTTP_Response(o, code);
766 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
768 hres->version = odr_strdup(o, m_http_version);
769 if (m_http_keepalive)
770 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
774 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
776 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
780 int r = send_GDU(gdu, &len);
782 m_bw_stat.add_bytes(len);
787 int Yaz_Proxy::send_srw_response(Z_SRW_PDU *srw_pdu)
789 ODR o = odr_encode();
790 const char *ctype = "text/xml";
791 Z_GDU *gdu = z_get_HTTP_Response(o, 200);
792 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
794 hres->version = odr_strdup(o, m_http_version);
795 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
796 if (m_http_keepalive)
797 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
801 static Z_SOAP_Handler soap_handlers[2] = {
803 {"http://www.loc.gov/zing/srw/", 0,
804 (Z_SOAP_fun) yaz_srw_codec},
809 Z_SOAP *soap_package = (Z_SOAP*) odr_malloc(o, sizeof(Z_SOAP));
810 soap_package->which = Z_SOAP_generic;
811 soap_package->u.generic =
812 (Z_SOAP_Generic *) odr_malloc(o, sizeof(*soap_package->u.generic));
813 soap_package->u.generic->no = 0;
814 soap_package->u.generic->ns = soap_handlers[0].ns;
815 soap_package->u.generic->p = (void *) srw_pdu;
816 soap_package->ns = m_soap_ns;
817 z_soap_codec_enc_xsl(o, &soap_package,
818 &hres->content_buf, &hres->content_len,
819 soap_handlers, 0, m_s2z_stylesheet);
820 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
822 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
826 int r = send_GDU(gdu, &len);
828 m_bw_stat.add_bytes(len);
833 int Yaz_Proxy::send_to_srw_client_error(int srw_error, const char *add)
835 ODR o = odr_encode();
836 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
837 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
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, srw_error, add);
843 return send_srw_response(srw_pdu);
846 int Yaz_Proxy::z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
847 Z_DefaultDiagFormat *ddf)
849 int bib1_code = *ddf->condition;
850 if (bib1_code == 109)
852 srw_res->num_diagnostics = 1;
853 srw_res->diagnostics = (Z_SRW_diagnostic *)
854 odr_malloc(o, sizeof(*srw_res->diagnostics));
855 yaz_mk_std_diagnostic(o, srw_res->diagnostics,
856 yaz_diag_bib1_to_srw(*ddf->condition),
861 int Yaz_Proxy::send_to_srw_client_ok(int hits, Z_Records *records, int start)
863 ODR o = odr_encode();
864 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
865 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
867 srw_res->numberOfRecords = odr_intdup (o, hits);
868 if (records && records->which == Z_Records_DBOSD)
870 srw_res->num_records =
871 records->u.databaseOrSurDiagnostics->num_records;
873 srw_res->records = (Z_SRW_record *)
874 odr_malloc(o, srw_res->num_records * sizeof(Z_SRW_record));
875 for (i = 0; i < srw_res->num_records; i++)
877 Z_NamePlusRecord *npr = records->u.databaseOrSurDiagnostics->records[i];
878 if (npr->which != Z_NamePlusRecord_databaseRecord)
880 srw_res->records[i].recordSchema = "diagnostic";
881 srw_res->records[i].recordPacking = m_s2z_packing;
882 srw_res->records[i].recordData_buf = "67";
883 srw_res->records[i].recordData_len = 2;
884 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
887 Z_External *r = npr->u.databaseRecord;
888 oident *ent = oid_getentbyoid(r->direct_reference);
889 if (r->which == Z_External_octet && ent->value == VAL_TEXT_XML)
891 srw_res->records[i].recordSchema = m_schema;
892 srw_res->records[i].recordPacking = m_s2z_packing;
893 srw_res->records[i].recordData_buf = (char*)
894 r->u.octet_aligned->buf;
895 srw_res->records[i].recordData_len = r->u.octet_aligned->len;
896 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
900 srw_res->records[i].recordSchema = "diagnostic";
901 srw_res->records[i].recordPacking = m_s2z_packing;
902 srw_res->records[i].recordData_buf = "67";
903 srw_res->records[i].recordData_len = 2;
904 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
908 if (records && records->which == Z_Records_NSD)
911 http_code = z_to_srw_diag(odr_encode(), srw_res,
912 records->u.nonSurrogateDiagnostic);
914 return send_http_response(http_code);
916 return send_srw_response(srw_pdu);
920 int Yaz_Proxy::send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
923 Yaz_ProxyConfig *cfg = check_reconfigure();
927 char *b = cfg->get_explain(odr_encode(), 0 /* target */,
928 m_s2z_database, &len);
931 Z_SRW_PDU *res = yaz_srw_get(odr_encode(), Z_SRW_explain_response);
932 Z_SRW_explainResponse *er = res->u.explain_response;
934 er->record.recordData_buf = b;
935 er->record.recordData_len = len;
936 er->record.recordPacking = m_s2z_packing;
937 er->record.recordSchema = "http://explain.z3950.org/dtd/2.0/";
939 er->diagnostics = diagnostics;
940 er->num_diagnostics = num_diagnostics;
941 return send_srw_response(res);
944 return send_http_response(404);
947 int Yaz_Proxy::send_PDU_convert(Z_APDU *apdu)
951 if (apdu->which == Z_APDU_initResponse)
953 Z_InitResponse *res = apdu->u.initResponse;
954 if (*res->result == 0)
956 send_to_srw_client_error(3, 0);
958 else if (!m_s2z_search_apdu)
960 send_srw_explain_response(0, 0);
964 handle_incoming_Z_PDU(m_s2z_search_apdu);
967 else if (m_s2z_search_apdu && apdu->which == Z_APDU_searchResponse)
969 m_s2z_search_apdu = 0;
970 Z_SearchResponse *res = apdu->u.searchResponse;
971 m_s2z_hit_count = *res->resultCount;
972 if (res->records && res->records->which == Z_Records_NSD)
974 send_to_srw_client_ok(0, res->records, 1);
976 else if (m_s2z_present_apdu && m_s2z_hit_count > 0)
979 Z_PresentRequest *pr = m_s2z_present_apdu->u.presentRequest;
981 if (*pr->resultSetStartPoint <= m_s2z_hit_count)
983 if (*pr->numberOfRecordsRequested+ *pr->resultSetStartPoint
985 *pr->numberOfRecordsRequested =
986 1 + m_s2z_hit_count - *pr->resultSetStartPoint;
988 handle_incoming_Z_PDU(m_s2z_present_apdu);
992 m_s2z_present_apdu = 0;
993 send_to_srw_client_ok(m_s2z_hit_count, res->records, 1);
996 else if (m_s2z_present_apdu && apdu->which == Z_APDU_presentResponse)
999 *m_s2z_present_apdu->u.presentRequest->resultSetStartPoint;
1001 m_s2z_present_apdu = 0;
1002 Z_PresentResponse *res = apdu->u.presentResponse;
1003 send_to_srw_client_ok(m_s2z_hit_count, res->records, start);
1009 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
1010 yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
1012 int r = send_Z_PDU(apdu, &len);
1013 m_bytes_sent += len;
1014 m_bw_stat.add_bytes(len);
1021 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
1023 int kill_session = 0;
1024 Z_ReferenceId **new_id = get_referenceIdP(apdu);
1027 *new_id = m_referenceId;
1029 if (apdu->which == Z_APDU_searchResponse)
1031 Z_SearchResponse *sr = apdu->u.searchResponse;
1032 Z_Records *p = sr->records;
1033 if (p && p->which == Z_Records_NSD)
1035 Z_DiagRec dr, *dr_p = &dr;
1036 dr.which = Z_DiagRec_defaultFormat;
1037 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1039 *sr->searchStatus = 0;
1040 display_diagrecs(&dr_p, 1);
1044 if (p && p->which == Z_Records_DBOSD)
1047 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1048 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1052 if (sr->resultCount)
1054 yaz_log(LOG_LOG, "%s%d hits", m_session_str,
1056 if (*sr->resultCount < 0)
1058 m_invalid_session = 1;
1061 *sr->searchStatus = 0;
1063 create_nonSurrogateDiagnostics(odr_encode(), 2, 0);
1064 *sr->resultCount = 0;
1069 else if (apdu->which == Z_APDU_presentResponse)
1071 Z_PresentResponse *sr = apdu->u.presentResponse;
1072 Z_Records *p = sr->records;
1073 if (p && p->which == Z_Records_NSD)
1075 Z_DiagRec dr, *dr_p = &dr;
1076 dr.which = Z_DiagRec_defaultFormat;
1077 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1078 if (*sr->presentStatus == Z_PresentStatus_success)
1079 *sr->presentStatus = Z_PresentStatus_failure;
1080 display_diagrecs(&dr_p, 1);
1082 if (p && p->which == Z_Records_DBOSD)
1085 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1086 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1090 else if (apdu->which == Z_APDU_initResponse)
1092 if (m_initRequest_options)
1095 (Odr_bitmask *)odr_malloc(odr_encode(),
1096 sizeof(Odr_bitmask));
1097 ODR_MASK_ZERO(nopt);
1100 for (i = 0; i<24; i++)
1101 if (ODR_MASK_GET(m_initRequest_options, i) &&
1102 ODR_MASK_GET(apdu->u.initResponse->options, i))
1103 ODR_MASK_SET(nopt, i);
1104 apdu->u.initResponse->options = nopt;
1106 if (m_initRequest_version)
1108 Z_ProtocolVersion *nopt =
1109 (Odr_bitmask *)odr_malloc(odr_encode(),
1110 sizeof(Odr_bitmask));
1111 ODR_MASK_ZERO(nopt);
1114 for (i = 0; i<8; i++)
1115 if (ODR_MASK_GET(m_initRequest_version, i) &&
1116 ODR_MASK_GET(apdu->u.initResponse->protocolVersion, i))
1117 ODR_MASK_SET(nopt, i);
1118 apdu->u.initResponse->protocolVersion = nopt;
1120 apdu->u.initResponse->preferredMessageSize =
1121 odr_intdup(odr_encode(),
1122 m_client->m_initResponse_preferredMessageSize >
1123 m_initRequest_preferredMessageSize ?
1124 m_initRequest_preferredMessageSize :
1125 m_client->m_initResponse_preferredMessageSize);
1126 apdu->u.initResponse->maximumRecordSize =
1127 odr_intdup(odr_encode(),
1128 m_client->m_initResponse_maximumRecordSize >
1129 m_initRequest_maximumRecordSize ?
1130 m_initRequest_maximumRecordSize :
1131 m_client->m_initResponse_maximumRecordSize);
1133 int r = send_PDU_convert(apdu);
1140 m_parent->pre_init();
1145 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
1148 const char *apdu_name_tmp = apdu_name(apdu);
1149 int r = send_Z_PDU(apdu, &len);
1150 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
1151 yaz_log (LOG_LOG, "%sSending %s to %s %d bytes",
1153 apdu_name_tmp, get_hostname(), len);
1154 m_bytes_sent += len;
1158 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
1160 if (apdu->which == Z_APDU_presentRequest)
1162 Z_PresentRequest *pr = apdu->u.presentRequest;
1163 int toget = *pr->numberOfRecordsRequested;
1164 int start = *pr->resultSetStartPoint;
1166 yaz_log(LOG_LOG, "%sPresent %s %d+%d", m_session_str,
1167 pr->resultSetId, start, toget);
1169 if (*m_parent->m_optimize == '0')
1172 if (!m_client->m_last_resultSetId)
1174 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1175 new_apdu->u.presentResponse->records =
1176 create_nonSurrogateDiagnostics(odr_encode(), 30,
1178 send_to_client(new_apdu);
1181 if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
1183 if (start+toget-1 > m_client->m_last_resultCount)
1185 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1186 new_apdu->u.presentResponse->records =
1187 create_nonSurrogateDiagnostics(odr_encode(), 13, 0);
1188 send_to_client(new_apdu);
1191 Z_NamePlusRecordList *npr;
1192 if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
1193 pr->preferredRecordSyntax,
1194 pr->recordComposition))
1196 yaz_log (LOG_LOG, "%sReturned cached records for present request",
1198 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1199 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1201 new_apdu->u.presentResponse->numberOfRecordsReturned
1202 = odr_intdup(odr_encode(), toget);
1204 new_apdu->u.presentResponse->records = (Z_Records*)
1205 odr_malloc(odr_encode(), sizeof(Z_Records));
1206 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
1207 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
1208 new_apdu->u.presentResponse->nextResultSetPosition =
1209 odr_intdup(odr_encode(), start+toget);
1211 send_to_client(new_apdu);
1217 if (apdu->which != Z_APDU_searchRequest)
1219 Z_SearchRequest *sr = apdu->u.searchRequest;
1220 Yaz_Z_Query *this_query = new Yaz_Z_Query;
1221 Yaz_Z_Databases this_databases;
1223 this_databases.set(sr->num_databaseNames, (const char **)
1226 this_query->set_Z_Query(sr->query);
1228 char query_str[120];
1229 this_query->print(query_str, sizeof(query_str)-1);
1230 yaz_log(LOG_LOG, "%sSearch %s", m_session_str, query_str);
1232 if (*m_parent->m_optimize != '0' &&
1233 m_client->m_last_ok && m_client->m_last_query &&
1234 m_client->m_last_query->match(this_query) &&
1235 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
1236 m_client->m_last_databases.match(this_databases))
1239 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
1240 m_client->m_last_resultCount < *sr->largeSetLowerBound)
1242 Z_NamePlusRecordList *npr;
1243 int toget = *sr->mediumSetPresentNumber;
1244 Z_RecordComposition *comp = 0;
1246 if (toget > m_client->m_last_resultCount)
1247 toget = m_client->m_last_resultCount;
1249 if (sr->mediumSetElementSetNames)
1251 comp = (Z_RecordComposition *)
1252 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1253 comp->which = Z_RecordComp_simple;
1254 comp->u.simple = sr->mediumSetElementSetNames;
1257 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1258 sr->preferredRecordSyntax, comp))
1260 yaz_log (LOG_LOG, "%sReturned cached records for medium set",
1262 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1263 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1264 new_apdu->u.searchResponse->resultCount =
1265 &m_client->m_last_resultCount;
1267 new_apdu->u.searchResponse->numberOfRecordsReturned
1268 = odr_intdup(odr_encode(), toget);
1270 new_apdu->u.searchResponse->presentStatus =
1271 odr_intdup(odr_encode(), Z_PresentStatus_success);
1272 new_apdu->u.searchResponse->records = (Z_Records*)
1273 odr_malloc(odr_encode(), sizeof(Z_Records));
1274 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1275 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1276 new_apdu->u.searchResponse->nextResultSetPosition =
1277 odr_intdup(odr_encode(), toget+1);
1278 send_to_client(new_apdu);
1284 // send present request (medium size)
1285 yaz_log (LOG_LOG, "%sOptimizing search for medium set",
1288 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1289 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1290 pr->referenceId = sr->referenceId;
1291 pr->resultSetId = sr->resultSetName;
1292 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1293 *pr->numberOfRecordsRequested = toget;
1294 pr->recordComposition = comp;
1295 m_client->m_sr_transform = 1;
1299 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
1300 m_client->m_last_resultCount <= 0)
1302 // large set. Return pseudo-search response immediately
1303 yaz_log (LOG_LOG, "%sOptimizing search for large set",
1305 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1306 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1307 new_apdu->u.searchResponse->resultCount =
1308 &m_client->m_last_resultCount;
1309 send_to_client(new_apdu);
1314 Z_NamePlusRecordList *npr;
1315 int toget = m_client->m_last_resultCount;
1316 Z_RecordComposition *comp = 0;
1318 // send a present request (small set)
1320 if (sr->smallSetElementSetNames)
1322 comp = (Z_RecordComposition *)
1323 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1324 comp->which = Z_RecordComp_simple;
1325 comp->u.simple = sr->smallSetElementSetNames;
1328 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1329 sr->preferredRecordSyntax, comp))
1331 yaz_log (LOG_LOG, "%sReturned cached records for small set",
1333 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1334 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1335 new_apdu->u.searchResponse->resultCount =
1336 &m_client->m_last_resultCount;
1338 new_apdu->u.searchResponse->numberOfRecordsReturned
1339 = odr_intdup(odr_encode(), toget);
1341 new_apdu->u.searchResponse->presentStatus =
1342 odr_intdup(odr_encode(), Z_PresentStatus_success);
1343 new_apdu->u.searchResponse->records = (Z_Records*)
1344 odr_malloc(odr_encode(), sizeof(Z_Records));
1345 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1346 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1347 new_apdu->u.searchResponse->nextResultSetPosition =
1348 odr_intdup(odr_encode(), toget+1);
1349 send_to_client(new_apdu);
1354 yaz_log (LOG_LOG, "%sOptimizing search for small set",
1356 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1357 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1358 pr->referenceId = sr->referenceId;
1359 pr->resultSetId = sr->resultSetName;
1360 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1361 *pr->numberOfRecordsRequested = toget;
1362 pr->recordComposition = comp;
1363 m_client->m_sr_transform = 1;
1368 else // query doesn't match
1370 delete m_client->m_last_query;
1371 m_client->m_last_query = this_query;
1372 m_client->m_last_ok = 0;
1373 m_client->m_cache.clear();
1374 m_client->m_resultSetStartPoint = 0;
1376 xfree (m_client->m_last_resultSetId);
1377 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
1379 m_client->m_last_databases.set(sr->num_databaseNames,
1380 (const char **) sr->databaseNames);
1386 void Yaz_Proxy::inc_request_no()
1388 char *cp = strchr(m_session_str, ' ');
1391 sprintf(cp+1, "%d ", m_request_no);
1394 void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
1398 m_bytes_recv += len;
1400 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
1401 yaz_log (LOG_LOG, "%sReceiving %s from client %d bytes",
1402 m_session_str, gdu_name(apdu), len);
1404 if (m_bw_hold_PDU) // double incoming PDU. shutdown now.
1407 m_bw_stat.add_bytes(len);
1408 m_pdu_stat.add_bytes(1);
1410 #if HAVE_GETTIMEOFDAY
1411 gettimeofday(&m_time_tv, 0);
1414 int bw_total = m_bw_stat.get_total();
1415 int pdu_total = m_pdu_stat.get_total();
1420 if (bw_total > m_bw_max)
1422 reduce = (bw_total/m_bw_max);
1427 if (pdu_total > m_pdu_max)
1429 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
1430 reduce = (reduce > nreduce) ? reduce : nreduce;
1435 yaz_log(LOG_LOG, "%sdelay=%d bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
1436 m_session_str, reduce, bw_total, pdu_total,
1437 m_bw_max, m_pdu_max);
1439 m_bw_hold_PDU = apdu; // save PDU and signal "on hold"
1440 timeout(reduce); // call us reduce seconds later
1442 else if (apdu->which == Z_GDU_Z3950)
1443 handle_incoming_Z_PDU(apdu->u.z3950);
1444 else if (apdu->which == Z_GDU_HTTP_Request)
1445 handle_incoming_HTTP(apdu->u.HTTP_Request);
1448 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
1450 if (m_max_record_retrieve)
1452 if (apdu->which == Z_APDU_presentRequest)
1454 Z_PresentRequest *pr = apdu->u.presentRequest;
1455 if (pr->numberOfRecordsRequested &&
1456 *pr->numberOfRecordsRequested > m_max_record_retrieve)
1457 *pr->numberOfRecordsRequested = m_max_record_retrieve;
1462 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
1464 const char *addinfo)
1466 Z_Records *rec = (Z_Records *)
1467 odr_malloc (odr, sizeof(*rec));
1469 odr_malloc (odr, sizeof(*err));
1470 Z_DiagRec *drec = (Z_DiagRec *)
1471 odr_malloc (odr, sizeof(*drec));
1472 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1473 odr_malloc (odr, sizeof(*dr));
1475 rec->which = Z_Records_NSD;
1476 rec->u.nonSurrogateDiagnostic = dr;
1477 dr->diagnosticSetId =
1478 yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
1479 dr->condition = err;
1480 dr->which = Z_DefaultDiagFormat_v2Addinfo;
1481 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
1485 Z_APDU *Yaz_Proxy::handle_query_transformation(Z_APDU *apdu)
1487 if (apdu->which == Z_APDU_searchRequest &&
1488 apdu->u.searchRequest->query &&
1489 apdu->u.searchRequest->query->which == Z_Query_type_104 &&
1490 apdu->u.searchRequest->query->u.type_104->which == Z_External_CQL)
1492 Z_RPNQuery *rpnquery = 0;
1493 Z_SearchRequest *sr = apdu->u.searchRequest;
1496 yaz_log(LOG_LOG, "%sCQL: %s", m_session_str,
1497 sr->query->u.type_104->u.cql);
1499 int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
1500 &rpnquery, odr_encode(),
1503 yaz_log(LOG_LOG, "%sNo CQL to RPN table", m_session_str);
1506 yaz_log(LOG_LOG, "%sCQL Conversion error %d", m_session_str, r);
1507 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1509 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1510 new_apdu->u.searchResponse->records =
1511 create_nonSurrogateDiagnostics(odr_encode(),
1512 yaz_diag_srw_to_bib1(r),
1514 *new_apdu->u.searchResponse->searchStatus = 0;
1516 send_to_client(new_apdu);
1522 sr->query->which = Z_Query_type_1;
1523 sr->query->u.type_1 = rpnquery;
1530 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
1532 if (apdu->which == Z_APDU_searchRequest)
1534 Z_SearchRequest *sr = apdu->u.searchRequest;
1538 Yaz_ProxyConfig *cfg = check_reconfigure();
1540 err = cfg->check_query(odr_encode(), m_default_target,
1541 sr->query, &addinfo);
1544 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1546 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1547 new_apdu->u.searchResponse->records =
1548 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1549 *new_apdu->u.searchResponse->searchStatus = 0;
1551 send_to_client(new_apdu);
1559 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
1562 if (apdu->which == Z_APDU_searchRequest)
1564 Z_SearchRequest *sr = apdu->u.searchRequest;
1567 Yaz_ProxyConfig *cfg = check_reconfigure();
1569 Z_RecordComposition rc_temp, *rc = 0;
1570 if (sr->smallSetElementSetNames)
1572 rc_temp.which = Z_RecordComp_simple;
1573 rc_temp.u.simple = sr->smallSetElementSetNames;
1577 char *stylesheet_name = 0;
1579 err = cfg->check_syntax(odr_encode(),
1581 sr->preferredRecordSyntax, rc,
1582 &addinfo, &stylesheet_name, &m_schema);
1583 if (stylesheet_name)
1585 m_parent->low_socket_close();
1588 if (m_stylesheet_xsp)
1589 xsltFreeStylesheet(m_stylesheet_xsp);
1590 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1593 m_stylesheet_offset = 0;
1594 xfree(stylesheet_name);
1596 m_parent->low_socket_open();
1600 sr->preferredRecordSyntax =
1601 yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN, VAL_USMARC);
1606 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1608 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1609 new_apdu->u.searchResponse->records =
1610 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1611 *new_apdu->u.searchResponse->searchStatus = 0;
1613 send_to_client(new_apdu);
1618 else if (apdu->which == Z_APDU_presentRequest)
1620 Z_PresentRequest *pr = apdu->u.presentRequest;
1623 Yaz_ProxyConfig *cfg = check_reconfigure();
1625 char *stylesheet_name = 0;
1627 err = cfg->check_syntax(odr_encode(), m_default_target,
1628 pr->preferredRecordSyntax,
1629 pr->recordComposition,
1630 &addinfo, &stylesheet_name, &m_schema);
1631 if (stylesheet_name)
1633 m_parent->low_socket_close();
1636 if (m_stylesheet_xsp)
1637 xsltFreeStylesheet(m_stylesheet_xsp);
1639 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1642 m_stylesheet_offset = 0;
1643 xfree(stylesheet_name);
1645 m_parent->low_socket_open();
1649 pr->preferredRecordSyntax =
1650 yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN, VAL_USMARC);
1655 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1657 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1658 new_apdu->u.presentResponse->records =
1659 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1660 *new_apdu->u.presentResponse->presentStatus =
1661 Z_PresentStatus_failure;
1663 send_to_client(new_apdu);
1671 Z_ElementSetNames *Yaz_Proxy::mk_esn_from_schema(ODR o, const char *schema)
1675 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1676 odr_malloc(o, sizeof(Z_ElementSetNames));
1677 esn->which = Z_ElementSetNames_generic;
1678 esn->u.generic = odr_strdup(o, schema);
1682 void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
1686 odr_destroy(m_s2z_odr_init);
1689 if (m_s2z_odr_search)
1691 odr_destroy(m_s2z_odr_search);
1692 m_s2z_odr_search = 0;
1695 m_http_keepalive = 0;
1697 if (!strcmp(hreq->version, "1.0"))
1699 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1700 if (v && !strcmp(v, "Keep-Alive"))
1701 m_http_keepalive = 1;
1703 m_http_keepalive = 0;
1704 m_http_version = "1.0";
1708 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1709 if (v && !strcmp(v, "close"))
1710 m_http_keepalive = 0;
1712 m_http_keepalive = 1;
1713 m_http_version = "1.1";
1716 Z_SRW_PDU *srw_pdu = 0;
1717 Z_SOAP *soap_package = 0;
1719 Z_SRW_diagnostic *diagnostic = 0;
1720 int num_diagnostic = 0;
1721 if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1723 || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1724 &charset, &diagnostic, &num_diagnostic) == 0)
1726 m_s2z_odr_init = odr_createmem(ODR_ENCODE);
1727 m_s2z_odr_search = odr_createmem(ODR_ENCODE);
1728 m_soap_ns = odr_strdup(m_s2z_odr_search, soap_package->ns);
1729 m_s2z_init_apdu = 0;
1730 m_s2z_search_apdu = 0;
1731 m_s2z_present_apdu = 0;
1733 m_s2z_stylesheet = 0;
1735 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
1737 Z_SRW_searchRetrieveRequest *srw_req = srw_pdu->u.request;
1739 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1740 // recordXPath unsupported.
1741 if (srw_req->recordXPath)
1743 yaz_add_srw_diagnostic(odr_decode(),
1744 &diagnostic, &num_diagnostic,
1748 if (srw_req->sort_type != Z_SRW_sort_type_none)
1750 yaz_add_srw_diagnostic(odr_decode(),
1751 &diagnostic, &num_diagnostic,
1755 if (srw_req->stylesheet)
1757 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1759 // set packing for response records ..
1760 if (srw_req->recordPacking &&
1761 !strcmp(srw_req->recordPacking, "xml"))
1762 m_s2z_packing = Z_SRW_recordPacking_XML;
1764 m_s2z_packing = Z_SRW_recordPacking_string;
1768 Z_SRW_PDU *srw_pdu =
1769 yaz_srw_get(odr_encode(),
1770 Z_SRW_searchRetrieve_response);
1771 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1773 srw_res->diagnostics = diagnostic;
1774 srw_res->num_diagnostics = num_diagnostic;
1775 send_srw_response(srw_pdu);
1779 // prepare search PDU
1780 m_s2z_search_apdu = zget_APDU(m_s2z_odr_search,
1781 Z_APDU_searchRequest);
1782 Z_SearchRequest *z_searchRequest =
1783 m_s2z_search_apdu->u.searchRequest;
1785 z_searchRequest->num_databaseNames = 1;
1786 z_searchRequest->databaseNames = (char**)
1787 odr_malloc(m_s2z_odr_search, sizeof(char *));
1788 z_searchRequest->databaseNames[0] = odr_strdup(m_s2z_odr_search,
1791 // query transformation
1792 Z_Query *query = (Z_Query *)
1793 odr_malloc(m_s2z_odr_search, sizeof(Z_Query));
1794 z_searchRequest->query = query;
1796 if (srw_req->query_type == Z_SRW_query_type_cql)
1798 Z_External *ext = (Z_External *)
1799 odr_malloc(m_s2z_odr_search, sizeof(*ext));
1800 ext->direct_reference =
1801 odr_getoidbystr(m_s2z_odr_search, "1.2.840.10003.16.2");
1802 ext->indirect_reference = 0;
1803 ext->descriptor = 0;
1804 ext->which = Z_External_CQL;
1805 ext->u.cql = srw_req->query.cql;
1807 query->which = Z_Query_type_104;
1808 query->u.type_104 = ext;
1810 else if (srw_req->query_type == Z_SRW_query_type_pqf)
1812 Z_RPNQuery *RPNquery;
1813 YAZ_PQF_Parser pqf_parser;
1815 pqf_parser = yaz_pqf_create ();
1817 RPNquery = yaz_pqf_parse (pqf_parser, m_s2z_odr_search,
1818 srw_req->query.pqf);
1821 const char *pqf_msg;
1823 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1824 yaz_log(LOG_LOG, "%*s^\n", off+4, "");
1825 yaz_log(LOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
1827 send_to_srw_client_error(10, 0);
1830 query->which = Z_Query_type_1;
1831 query->u.type_1 = RPNquery;
1833 yaz_pqf_destroy (pqf_parser);
1837 send_to_srw_client_error(7, "query");
1842 m_s2z_present_apdu = 0;
1844 if (srw_req->maximumRecords)
1845 max = *srw_req->maximumRecords;
1847 if (srw_req->startRecord)
1848 start = *srw_req->startRecord;
1851 // Some backend, such as Voyager doesn't honor piggyback
1852 // So we use present always (0 &&).
1853 if (0 && start <= 1) // Z39.50 piggyback
1855 *z_searchRequest->smallSetUpperBound = max;
1856 *z_searchRequest->mediumSetPresentNumber = max;
1857 *z_searchRequest->largeSetLowerBound = 2000000000; // 2e9
1859 z_searchRequest->preferredRecordSyntax =
1860 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1862 if (srw_req->recordSchema)
1864 z_searchRequest->smallSetElementSetNames =
1865 z_searchRequest->mediumSetElementSetNames =
1866 mk_esn_from_schema(m_s2z_odr_search,
1867 srw_req->recordSchema);
1870 else // Z39.50 present
1872 m_s2z_present_apdu = zget_APDU(m_s2z_odr_search,
1873 Z_APDU_presentRequest);
1874 Z_PresentRequest *z_presentRequest =
1875 m_s2z_present_apdu->u.presentRequest;
1876 *z_presentRequest->resultSetStartPoint = start;
1877 *z_presentRequest->numberOfRecordsRequested = max;
1878 z_presentRequest->preferredRecordSyntax =
1879 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1881 if (srw_req->recordSchema)
1883 z_presentRequest->recordComposition =
1884 (Z_RecordComposition *)
1885 odr_malloc(m_s2z_odr_search,
1886 sizeof(Z_RecordComposition));
1887 z_presentRequest->recordComposition->which =
1888 Z_RecordComp_simple;
1889 z_presentRequest->recordComposition->u.simple =
1890 mk_esn_from_schema(m_s2z_odr_search,
1891 srw_req->recordSchema);
1897 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1898 Z_APDU_initRequest);
1900 // prevent m_initRequest_apdu memory from being grabbed
1901 // in Yaz_Proxy::handle_incoming_Z_PDU
1902 m_initRequest_apdu = m_s2z_init_apdu;
1903 handle_incoming_Z_PDU(m_s2z_init_apdu);
1908 handle_incoming_Z_PDU(m_s2z_search_apdu);
1912 else if (srw_pdu->which == Z_SRW_explain_request)
1914 Z_SRW_explainRequest *srw_req = srw_pdu->u.explain_request;
1916 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1919 if (srw_req->stylesheet)
1921 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1923 if (srw_req->recordPacking &&
1924 !strcmp(srw_req->recordPacking, "xml"))
1925 m_s2z_packing = Z_SRW_recordPacking_XML;
1927 m_s2z_packing = Z_SRW_recordPacking_string;
1931 send_srw_explain_response(diagnostic, num_diagnostic);
1937 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1938 Z_APDU_initRequest);
1940 // prevent m_initRequest_apdu memory from being grabbed
1941 // in Yaz_Proxy::handle_incoming_Z_PDU
1942 m_initRequest_apdu = m_s2z_init_apdu;
1943 handle_incoming_Z_PDU(m_s2z_init_apdu);
1946 send_srw_explain_response(0, 0);
1949 else if (srw_pdu->which == Z_SRW_scan_request)
1951 m_s2z_database = odr_strdup(m_s2z_odr_init,
1952 srw_pdu->u.scan_request->database);
1954 yaz_add_srw_diagnostic(odr_decode(),
1955 &diagnostic, &num_diagnostic,
1957 Z_SRW_PDU *srw_pdu =
1958 yaz_srw_get(odr_encode(),
1959 Z_SRW_scan_response);
1960 Z_SRW_scanResponse *srw_res = srw_pdu->u.scan_response;
1962 srw_res->diagnostics = diagnostic;
1963 srw_res->num_diagnostics = num_diagnostic;
1964 send_srw_response(srw_pdu);
1971 send_to_srw_client_error(4, 0);
1975 Z_GDU *p = z_get_HTTP_Response(odr_encode(), 400);
1980 void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
1982 Z_ReferenceId **refid = get_referenceIdP(apdu);
1983 nmem_reset(m_referenceId_mem);
1984 if (refid && *refid)
1986 m_referenceId = (Z_ReferenceId *)
1987 nmem_malloc(m_referenceId_mem, sizeof(*m_referenceId));
1988 m_referenceId->len = m_referenceId->size = (*refid)->len;
1989 m_referenceId->buf = (unsigned char *)
1990 nmem_malloc(m_referenceId_mem, (*refid)->len);
1991 memcpy(m_referenceId->buf, (*refid)->buf, (*refid)->len);
1996 if (!m_client && m_invalid_session)
1998 m_apdu_invalid_session = apdu;
1999 m_mem_invalid_session = odr_extract_mem(odr_decode());
2000 apdu = m_initRequest_apdu;
2003 // Determine our client.
2004 Z_OtherInformation **oi;
2005 get_otherInfoAPDU(apdu, &oi);
2006 m_client = get_client(apdu, get_cookie(oi), get_proxy(oi));
2012 m_client->m_server = this;
2014 if (apdu->which == Z_APDU_initRequest)
2016 if (apdu->u.initRequest->implementationId)
2017 yaz_log(LOG_LOG, "%simplementationId: %s",
2018 m_session_str, apdu->u.initRequest->implementationId);
2019 if (apdu->u.initRequest->implementationName)
2020 yaz_log(LOG_LOG, "%simplementationName: %s",
2021 m_session_str, apdu->u.initRequest->implementationName);
2022 if (apdu->u.initRequest->implementationVersion)
2023 yaz_log(LOG_LOG, "%simplementationVersion: %s",
2024 m_session_str, apdu->u.initRequest->implementationVersion);
2025 if (m_initRequest_apdu == 0)
2027 if (m_initRequest_mem)
2028 nmem_destroy(m_initRequest_mem);
2029 m_initRequest_apdu = apdu;
2030 m_initRequest_mem = odr_extract_mem(odr_decode());
2032 m_initRequest_preferredMessageSize = *apdu->u.initRequest->
2033 preferredMessageSize;
2034 *apdu->u.initRequest->preferredMessageSize = 1024*1024;
2035 m_initRequest_maximumRecordSize = *apdu->u.initRequest->
2037 *apdu->u.initRequest->maximumRecordSize = 1024*1024;
2039 // save init options for the response..
2040 m_initRequest_options = apdu->u.initRequest->options;
2042 apdu->u.initRequest->options =
2043 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2044 sizeof(Odr_bitmask));
2045 ODR_MASK_ZERO(apdu->u.initRequest->options);
2047 for (i = 0; i<= 24; i++)
2048 ODR_MASK_SET(apdu->u.initRequest->options, i);
2049 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2050 Z_Options_negotiationModel);
2051 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2052 Z_Options_concurrentOperations);
2055 m_initRequest_version = apdu->u.initRequest->protocolVersion;
2056 apdu->u.initRequest->protocolVersion =
2057 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2058 sizeof(Odr_bitmask));
2059 ODR_MASK_ZERO(apdu->u.initRequest->protocolVersion);
2061 for (i = 0; i<= 8; i++)
2062 ODR_MASK_SET(apdu->u.initRequest->protocolVersion, i);
2064 if (m_client->m_init_flag)
2066 if (handle_init_response_for_invalid_session(apdu))
2068 if (m_client->m_initResponse)
2070 Z_APDU *apdu2 = m_client->m_initResponse;
2071 apdu2->u.initResponse->otherInfo = 0;
2072 if (m_client->m_cookie && *m_client->m_cookie)
2073 set_otherInformationString(apdu2, VAL_COOKIE, 1,
2074 m_client->m_cookie);
2075 apdu2->u.initResponse->referenceId =
2076 apdu->u.initRequest->referenceId;
2077 apdu2->u.initResponse->options = m_client->m_initResponse_options;
2078 apdu2->u.initResponse->protocolVersion =
2079 m_client->m_initResponse_version;
2081 send_to_client(apdu2);
2085 m_client->m_init_flag = 1;
2087 handle_max_record_retrieve(apdu);
2090 apdu = handle_syntax_validation(apdu);
2093 apdu = handle_query_transformation(apdu);
2096 apdu = handle_query_validation(apdu);
2099 apdu = result_set_optimize(apdu);
2102 m_client->timeout(m_target_idletime); // mark it active even
2103 // though we didn't use it
2107 // delete other info part from PDU before sending to target
2108 get_otherInfoAPDU(apdu, &oi);
2112 if (apdu->which == Z_APDU_presentRequest &&
2113 m_client->m_resultSetStartPoint == 0)
2115 Z_PresentRequest *pr = apdu->u.presentRequest;
2116 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
2117 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
2119 m_client->m_resultSetStartPoint = 0;
2121 if (m_client->send_to_target(apdu) < 0)
2128 m_client->m_waiting = 1;
2131 void Yaz_Proxy::connectNotify()
2135 void Yaz_Proxy::shutdown()
2137 m_invalid_session = 0;
2138 // only keep if keep_alive flag is set...
2140 m_client->m_pdu_recv < m_keepalive_limit_pdu &&
2141 m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
2142 m_client->m_waiting == 0)
2144 yaz_log(LOG_LOG, "%sShutdown (client to proxy) keepalive %s",
2146 m_client->get_hostname());
2147 yaz_log(LOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
2148 m_session_str, m_client->m_pdu_recv,
2149 m_client->m_bytes_sent + m_client->m_bytes_recv,
2150 m_keepalive_limit_bw, m_keepalive_limit_pdu);
2151 assert (m_client->m_waiting != 2);
2152 // Tell client (if any) that no server connection is there..
2153 m_client->m_server = 0;
2154 m_invalid_session = 0;
2158 yaz_log (LOG_LOG, "%sShutdown (client to proxy) close %s",
2160 m_client->get_hostname());
2161 assert (m_client->m_waiting != 2);
2166 yaz_log (LOG_LOG, "%sshutdown (client to proxy) bad state",
2172 yaz_log (LOG_LOG, "%sShutdown (client to proxy)",
2176 m_parent->pre_init();
2180 const char *Yaz_ProxyClient::get_session_str()
2184 return m_server->get_session_str();
2187 void Yaz_ProxyClient::shutdown()
2189 yaz_log (LOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
2195 void Yaz_Proxy::failNotify()
2198 yaz_log (LOG_LOG, "%sConnection closed by client",
2203 void Yaz_ProxyClient::failNotify()
2206 m_server->inc_request_no();
2207 yaz_log (LOG_LOG, "%sConnection closed by target %s",
2208 get_session_str(), get_hostname());
2212 void Yaz_ProxyClient::connectNotify()
2214 const char *s = get_session_str();
2215 const char *h = get_hostname();
2216 yaz_log (LOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
2218 timeout(m_target_idletime);
2223 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
2224 *the_PDU_Observable, int fd)
2226 return new Yaz_ProxyClient(the_PDU_Observable, 0);
2229 Yaz_ProxyClient::~Yaz_ProxyClient()
2234 m_next->m_prev = m_prev;
2235 m_waiting = 2; // for debugging purposes only.
2236 odr_destroy(m_init_odr);
2237 delete m_last_query;
2238 xfree (m_last_resultSetId);
2242 void Yaz_ProxyClient::pre_init_client()
2244 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
2245 Z_InitRequest *req = apdu->u.initRequest;
2248 for (i = 0; i<= 24; i++)
2249 ODR_MASK_SET(req->options, i);
2250 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2251 Z_Options_negotiationModel);
2252 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2253 Z_Options_concurrentOperations);
2254 for (i = 0; i<= 10; i++)
2255 ODR_MASK_SET(req->protocolVersion, i);
2257 if (send_to_target(apdu) < 0)
2268 void Yaz_Proxy::pre_init()
2271 const char *name = 0;
2272 const char *zurl_in_use[MAX_ZURL_PLEX];
2273 int limit_bw, limit_pdu, limit_req;
2274 int target_idletime, client_idletime;
2276 int keepalive_limit_bw, keepalive_limit_pdu;
2278 const char *cql2rpn = 0;
2280 Yaz_ProxyConfig *cfg = check_reconfigure();
2284 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
2289 for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
2290 &limit_bw, &limit_pdu, &limit_req,
2291 &target_idletime, &client_idletime,
2293 &keepalive_limit_bw,
2294 &keepalive_limit_pdu,
2301 for (j = 0; zurl_in_use[j]; j++)
2305 int spare_waiting = 0;
2308 for (c = m_clientPool; c; c = c->m_next)
2310 if (!strcmp(zurl_in_use[j], c->get_hostname()))
2312 if (c->m_cookie == 0)
2314 if (c->m_server == 0)
2326 yaz_log(LOG_LOG, "%spre-init %s %s use=%d other=%d spare=%d "
2327 "sparew=%d preinit=%d",m_session_str,
2328 name, zurl_in_use[j], in_use, other,
2329 spare, spare_waiting, pre_init);
2330 if (spare + spare_waiting < pre_init)
2332 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
2333 c->m_next = m_clientPool;
2335 c->m_next->m_prev = &c->m_next;
2337 c->m_prev = &m_clientPool;
2339 if (m_log_mask & PROXY_LOG_APDU_SERVER)
2340 c->set_APDU_yazlog(1);
2342 c->set_APDU_yazlog(0);
2344 if (c->client(zurl_in_use[j]))
2352 c->m_target_idletime = target_idletime;
2353 c->m_seqno = m_seqno++;
2360 void Yaz_Proxy::timeoutNotify()
2366 timeout(m_client_idletime);
2367 Z_GDU *apdu = m_bw_hold_PDU;
2370 if (apdu->which == Z_GDU_Z3950)
2371 handle_incoming_Z_PDU(apdu->u.z3950);
2372 else if (apdu->which == Z_GDU_HTTP_Request)
2373 handle_incoming_HTTP(apdu->u.HTTP_Request);
2375 else if (m_stylesheet_nprl)
2376 convert_xsl_delay();
2381 yaz_log (LOG_LOG, "%sTimeout (client to proxy)", m_session_str);
2392 void Yaz_Proxy::markInvalid()
2395 m_invalid_session = 1;
2398 void Yaz_ProxyClient::timeoutNotify()
2401 m_server->inc_request_no();
2403 yaz_log (LOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
2407 if (m_server && m_init_flag)
2409 // target timed out in a session that was properly initialized
2410 // server object stay alive but we mark it as invalid so it
2411 // gets initialized again
2412 m_server->markInvalid();
2418 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
2419 Yaz_Proxy *parent) :
2420 Yaz_Z_Assoc (the_PDU_Observable)
2427 m_last_resultSetId = 0;
2428 m_last_resultCount = 0;
2432 m_init_odr = odr_createmem (ODR_DECODE);
2434 m_initResponse_options = 0;
2435 m_initResponse_version = 0;
2436 m_initResponse_preferredMessageSize = 0;
2437 m_initResponse_maximumRecordSize = 0;
2438 m_resultSetStartPoint = 0;
2439 m_bytes_sent = m_bytes_recv = 0;
2443 m_target_idletime = 600;
2447 const char *Yaz_Proxy::option(const char *name, const char *value)
2449 if (!strcmp (name, "optimize")) {
2452 m_optimize = xstrdup (value);
2459 void Yaz_ProxyClient::recv_HTTP_response(Z_HTTP_Response *apdu, int len)
2464 void Yaz_ProxyClient::recv_GDU(Z_GDU *apdu, int len)
2466 if (apdu->which == Z_GDU_Z3950)
2467 recv_Z_PDU(apdu->u.z3950, len);
2468 else if (apdu->which == Z_GDU_HTTP_Response)
2469 recv_HTTP_response(apdu->u.HTTP_Response, len);
2474 int Yaz_Proxy::handle_init_response_for_invalid_session(Z_APDU *apdu)
2476 if (!m_invalid_session)
2478 m_invalid_session = 0;
2479 handle_incoming_Z_PDU(m_apdu_invalid_session);
2480 assert (m_mem_invalid_session);
2481 nmem_destroy(m_mem_invalid_session);
2482 m_mem_invalid_session = 0;
2486 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
2488 m_bytes_recv += len;
2492 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
2493 yaz_log (LOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
2494 apdu_name(apdu), get_hostname(), len);
2495 if (apdu->which == Z_APDU_initResponse)
2497 if (!m_server) // if this is a pre init session , check for more
2499 NMEM nmem = odr_extract_mem (odr_decode());
2500 odr_reset (m_init_odr);
2501 nmem_transfer (m_init_odr->mem, nmem);
2502 m_initResponse = apdu;
2503 m_initResponse_options = apdu->u.initResponse->options;
2504 m_initResponse_version = apdu->u.initResponse->protocolVersion;
2505 m_initResponse_preferredMessageSize =
2506 *apdu->u.initResponse->preferredMessageSize;
2507 m_initResponse_maximumRecordSize =
2508 *apdu->u.initResponse->maximumRecordSize;
2510 Z_InitResponse *ir = apdu->u.initResponse;
2511 char *im0 = ir->implementationName;
2514 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
2521 strcat(im1, "(YAZ Proxy)");
2522 ir->implementationName = im1;
2524 nmem_destroy (nmem);
2526 if (m_server && m_server->handle_init_response_for_invalid_session(apdu))
2529 if (apdu->which == Z_APDU_searchResponse)
2531 Z_SearchResponse *sr = apdu->u.searchResponse;
2532 m_last_resultCount = *sr->resultCount;
2533 int status = *sr->searchStatus;
2534 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
2538 if (sr->records && sr->records->which == Z_Records_DBOSD)
2540 m_cache.add(odr_decode(),
2541 sr->records->u.databaseOrSurDiagnostics, 1,
2546 if (apdu->which == Z_APDU_presentResponse)
2548 Z_PresentResponse *pr = apdu->u.presentResponse;
2552 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2553 Z_SearchResponse *sr = new_apdu->u.searchResponse;
2554 sr->referenceId = pr->referenceId;
2555 *sr->resultCount = m_last_resultCount;
2556 sr->records = pr->records;
2557 sr->nextResultSetPosition = pr->nextResultSetPosition;
2558 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
2562 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
2564 m_cache.add(odr_decode(),
2565 pr->records->u.databaseOrSurDiagnostics,
2566 m_resultSetStartPoint, -1);
2567 m_resultSetStartPoint = 0;
2571 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
2574 m_server->send_to_client(apdu);
2576 if (apdu->which == Z_APDU_close)
2582 void Yaz_Proxy::low_socket_close()
2587 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
2588 if (m_lo_fd[i] >= 0)
2589 ::close(m_lo_fd[i]);
2593 void Yaz_Proxy::low_socket_open()
2598 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
2599 m_lo_fd[i] = open("/dev/null", O_RDONLY);
2603 int Yaz_Proxy::server(const char *addr)
2605 int r = Yaz_Z_Assoc::server(addr);
2608 yaz_log(LOG_LOG, "%sStarted proxy "
2612 " on %s", m_session_str, addr);