1 /* $Id: yaz-proxy.cpp,v 1.1 2004-03-29 22:46:51 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
25 #include <sys/types.h>
29 #include <yaz/marcdisp.h>
30 #include <yaz/yaz-iconv.h>
32 #include <yaz/diagbib1.h>
33 #include <yaz++/proxy/proxy.h>
34 #include <yaz/pquery.h>
36 static const char *apdu_name(Z_APDU *apdu)
40 case Z_APDU_initRequest:
42 case Z_APDU_initResponse:
43 return "initResponse";
44 case Z_APDU_searchRequest:
45 return "searchRequest";
46 case Z_APDU_searchResponse:
47 return "searchResponse";
48 case Z_APDU_presentRequest:
49 return "presentRequest";
50 case Z_APDU_presentResponse:
51 return "presentResponse";
52 case Z_APDU_deleteResultSetRequest:
53 return "deleteResultSetRequest";
54 case Z_APDU_deleteResultSetResponse:
55 return "deleteResultSetResponse";
56 case Z_APDU_scanRequest:
58 case Z_APDU_scanResponse:
59 return "scanResponse";
60 case Z_APDU_sortRequest:
62 case Z_APDU_sortResponse:
63 return "sortResponse";
64 case Z_APDU_extendedServicesRequest:
65 return "extendedServicesRequest";
66 case Z_APDU_extendedServicesResponse:
67 return "extendedServicesResponse";
74 static const char *gdu_name(Z_GDU *gdu)
79 return apdu_name(gdu->u.z3950);
80 case Z_GDU_HTTP_Request:
81 return "HTTP Request";
82 case Z_GDU_HTTP_Response:
83 return "HTTP Response";
85 return "Unknown request/response";
88 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
90 Yaz_Z_Assoc(the_PDU_Observable), m_bw_stat(60), m_pdu_stat(60)
92 m_PDU_Observable = the_PDU_Observable;
97 m_keepalive_limit_bw = 500000;
98 m_keepalive_limit_pdu = 1000;
100 m_default_target = 0;
101 m_proxy_authentication = 0;
105 m_client_idletime = 600;
106 m_target_idletime = 600;
107 m_optimize = xstrdup ("1");
108 strcpy(m_session_str, "0 ");
115 m_max_record_retrieve = 0;
119 m_invalid_session = 0;
121 m_referenceId_mem = nmem_create();
124 m_stylesheet_xsp = 0;
125 m_stylesheet_nprl = 0;
126 m_s2z_stylesheet = 0;
129 m_initRequest_apdu = 0;
130 m_initRequest_mem = 0;
131 m_initRequest_preferredMessageSize = 0;
132 m_initRequest_maximumRecordSize = 0;
133 m_initRequest_options = 0;
134 m_initRequest_version = 0;
135 m_apdu_invalid_session = 0;
136 m_mem_invalid_session = 0;
138 m_s2z_odr_search = 0;
140 m_s2z_search_apdu = 0;
141 m_s2z_present_apdu = 0;
142 m_http_keepalive = 0;
145 m_s2z_packing = Z_SRW_recordPacking_string;
146 m_time_tv.tv_sec = 0;
147 m_time_tv.tv_usec = 0;
152 Yaz_Proxy::~Yaz_Proxy()
154 yaz_log(LOG_LOG, "%sClosed %d/%d sent/recv bytes total", m_session_str,
155 m_bytes_sent, m_bytes_recv);
156 nmem_destroy(m_initRequest_mem);
157 nmem_destroy(m_mem_invalid_session);
158 nmem_destroy(m_referenceId_mem);
160 xfree (m_proxyTarget);
161 xfree (m_default_target);
162 xfree (m_proxy_authentication);
165 if (m_stylesheet_xsp)
166 xsltFreeStylesheet(m_stylesheet_xsp);
170 odr_destroy(m_s2z_odr_init);
171 if (m_s2z_odr_search)
172 odr_destroy(m_s2z_odr_search);
178 int Yaz_Proxy::set_config(const char *config)
181 m_config = new Yaz_ProxyConfig();
182 xfree(m_config_fname);
183 m_config_fname = xstrdup(config);
184 int r = m_config->read_xml(config);
186 m_config->get_generic_info(&m_log_mask, &m_max_clients);
190 void Yaz_Proxy::set_default_target(const char *target)
192 xfree (m_default_target);
193 m_default_target = 0;
195 m_default_target = (char *) xstrdup (target);
198 void Yaz_Proxy::set_proxy_authentication (const char *auth)
200 xfree (m_proxy_authentication);
201 m_proxy_authentication = 0;
203 m_proxy_authentication = (char *) xstrdup (auth);
206 Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
209 return m_parent->check_reconfigure();
211 Yaz_ProxyConfig *cfg = m_config;
214 yaz_log(LOG_LOG, "reconfigure");
216 if (m_config_fname && cfg)
218 yaz_log(LOG_LOG, "reconfigure config %s", m_config_fname);
219 int r = cfg->read_xml(m_config_fname);
221 yaz_log(LOG_WARN, "reconfigure failed");
225 cfg->get_generic_info(&m_log_mask, &m_max_clients);
229 yaz_log(LOG_LOG, "reconfigure");
235 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
236 *the_PDU_Observable, int fd)
239 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable, this);
240 new_proxy->m_config = 0;
241 new_proxy->m_config_fname = 0;
242 new_proxy->timeout(m_client_idletime);
243 new_proxy->m_target_idletime = m_target_idletime;
244 new_proxy->set_default_target(m_default_target);
245 new_proxy->m_max_clients = m_max_clients;
246 new_proxy->m_log_mask = m_log_mask;
247 new_proxy->set_APDU_log(get_APDU_log());
248 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
249 new_proxy->set_APDU_yazlog(1);
251 new_proxy->set_APDU_yazlog(0);
252 new_proxy->set_proxy_authentication(m_proxy_authentication);
253 sprintf(new_proxy->m_session_str, "%ld:%d ", (long) time(0), m_session_no);
255 yaz_log (LOG_LOG, "%sNew session %s", new_proxy->m_session_str,
256 the_PDU_Observable->getpeername());
260 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
263 Z_OtherInformationUnit *oi;
265 ent.proto = PROTO_Z3950;
266 ent.oclass = CLASS_USERINFO;
267 ent.value = (oid_value) VAL_COOKIE;
268 assert (oid_ent_to_oid (&ent, oid));
270 if (oid_ent_to_oid (&ent, oid) &&
271 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
272 oi->which == Z_OtherInfo_characterInfo)
273 return oi->information.characterInfo;
277 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
280 Z_OtherInformationUnit *oi;
282 ent.proto = PROTO_Z3950;
283 ent.oclass = CLASS_USERINFO;
284 ent.value = (oid_value) VAL_PROXY;
285 if (oid_ent_to_oid (&ent, oid) &&
286 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
287 oi->which == Z_OtherInfo_characterInfo)
288 return oi->information.characterInfo;
292 const char *Yaz_Proxy::load_balance(const char **url)
294 int zurl_in_use[MAX_ZURL_PLEX];
295 int zurl_in_spare[MAX_ZURL_PLEX];
299 for (i = 0; i<MAX_ZURL_PLEX; i++)
302 zurl_in_spare[i] = 0;
304 for (c = m_parent->m_clientPool; c; c = c->m_next)
306 for (i = 0; url[i]; i++)
307 if (!strcmp(url[i], c->get_hostname()))
310 if (c->m_cookie == 0 && c->m_server == 0 && c->m_waiting == 0)
314 int min_use = 100000;
315 int spare_for_min = 0;
317 const char *ret_min = 0;
318 const char *ret_spare = 0;
319 for (i = 0; url[i]; i++)
321 yaz_log(LOG_DEBUG, "%szurl=%s use=%d spare=%d",
322 m_session_str, url[i], zurl_in_use[i], zurl_in_spare[i]);
323 if (min_use > zurl_in_use[i])
326 min_use = zurl_in_use[i];
327 spare_for_min = zurl_in_spare[i];
329 if (max_spare < zurl_in_spare[i])
332 max_spare = zurl_in_spare[i];
335 // use the one with minimum connections if spare is > 3
336 if (spare_for_min > 3)
338 // use one with most spares (if any)
344 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
345 const char *proxy_host)
348 Yaz_Proxy *parent = m_parent;
349 Yaz_ProxyClient *c = m_client;
353 const char *url[MAX_ZURL_PLEX];
354 Yaz_ProxyConfig *cfg = check_reconfigure();
358 /* only to be enabled for debugging... */
359 if (!strcmp(proxy_host, "stop"))
362 xfree(m_default_target);
363 m_default_target = xstrdup(proxy_host);
364 proxy_host = m_default_target;
366 int client_idletime = -1;
367 const char *cql2rpn_fname = 0;
368 url[0] = m_default_target;
373 cfg->get_target_info(proxy_host, url, &m_bw_max,
374 &m_pdu_max, &m_max_record_retrieve,
375 &m_target_idletime, &client_idletime,
376 &parent->m_max_clients,
377 &m_keepalive_limit_bw,
378 &m_keepalive_limit_pdu,
382 if (client_idletime != -1)
384 m_client_idletime = client_idletime;
385 timeout(m_client_idletime);
388 m_cql2rpn.set_pqf_file(cql2rpn_fname);
391 yaz_log(LOG_LOG, "%sNo default target", m_session_str);
394 // we don't handle multiplexing for cookie session, so we just
395 // pick the first one in this case (anonymous users will be able
396 // to use any backend)
397 if (cookie && *cookie)
398 m_proxyTarget = (char*) xstrdup(url[0]);
400 m_proxyTarget = (char*) xstrdup(load_balance(url));
402 if (cookie && *cookie)
403 { // search in sessions with a cookie
404 for (c = parent->m_clientPool; c; c = c->m_next)
407 assert (*c->m_prev == c);
408 if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
409 !strcmp(m_proxyTarget, c->get_hostname()))
412 // The following handles "cancel"
413 // If connection is busy (waiting for PDU) and
414 // we have an initRequest we can safely do re-open
415 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
417 yaz_log (LOG_LOG, "%s REOPEN target=%s", m_session_str,
424 c->m_last_resultCount = 0;
425 c->m_sr_transform = 0;
427 c->m_resultSetStartPoint = 0;
428 c->m_target_idletime = m_target_idletime;
429 if (c->client(m_proxyTarget))
436 c->m_seqno = parent->m_seqno;
437 if (c->m_server && c->m_server != this)
438 c->m_server->m_client = 0;
441 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
448 // don't have a client session yet. Search in session w/o cookie
449 for (c = parent->m_clientPool; c; c = c->m_next)
452 assert (*c->m_prev == c);
453 if (c->m_server == 0 && c->m_cookie == 0 &&
455 !strcmp(m_proxyTarget, c->get_hostname()))
458 yaz_log (LOG_LOG, "%sREUSE %d %s",
459 m_session_str, parent->m_seqno, c->get_hostname());
461 c->m_seqno = parent->m_seqno;
462 assert(c->m_server == 0);
465 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
466 c->set_APDU_yazlog(1);
468 c->set_APDU_yazlog(0);
480 if (apdu->which != Z_APDU_initRequest)
482 yaz_log (LOG_LOG, "%sno init request as first PDU", m_session_str);
485 Z_InitRequest *initRequest = apdu->u.initRequest;
487 if (!initRequest->idAuthentication)
489 if (m_proxy_authentication)
491 initRequest->idAuthentication =
492 (Z_IdAuthentication *)
493 odr_malloc (odr_encode(),
494 sizeof(*initRequest->idAuthentication));
495 initRequest->idAuthentication->which =
496 Z_IdAuthentication_open;
497 initRequest->idAuthentication->u.open =
498 odr_strdup (odr_encode(), m_proxy_authentication);
501 // go through list of clients - and find the lowest/oldest one.
502 Yaz_ProxyClient *c_min = 0;
504 int no_of_clients = 0;
505 if (parent->m_clientPool)
506 yaz_log (LOG_DEBUG, "Existing sessions");
507 for (c = parent->m_clientPool; c; c = c->m_next)
509 yaz_log (LOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
510 c->m_waiting, c->get_hostname(),
511 c->m_cookie ? c->m_cookie : "");
513 if (min_seq < 0 || c->m_seqno < min_seq)
515 min_seq = c->m_seqno;
519 if (no_of_clients >= parent->m_max_clients)
522 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
524 yaz_log (LOG_LOG, "%sMAXCLIENTS %d Destroy %d",
525 m_session_str, parent->m_max_clients, c->m_seqno);
526 if (c->m_server && c->m_server != this)
532 yaz_log (LOG_LOG, "%sMAXCLIENTS %d Reuse %d %d %s",
533 m_session_str, parent->m_max_clients,
534 c->m_seqno, parent->m_seqno, c->get_hostname());
538 c->m_cookie = xstrdup(cookie);
539 c->m_seqno = parent->m_seqno;
540 if (c->m_server && c->m_server != this)
542 c->m_server->m_client = 0;
546 c->m_target_idletime = m_target_idletime;
547 c->timeout(m_target_idletime);
549 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
550 c->set_APDU_yazlog(1);
552 c->set_APDU_yazlog(0);
559 yaz_log (LOG_LOG, "%sNEW %d %s",
560 m_session_str, parent->m_seqno, m_proxyTarget);
561 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), parent);
562 c->m_next = parent->m_clientPool;
564 c->m_next->m_prev = &c->m_next;
565 parent->m_clientPool = c;
566 c->m_prev = &parent->m_clientPool;
572 c->m_cookie = xstrdup(cookie);
574 c->m_seqno = parent->m_seqno;
576 c->m_last_resultCount = 0;
579 c->m_sr_transform = 0;
581 c->m_resultSetStartPoint = 0;
583 if (c->client(m_proxyTarget))
588 c->m_target_idletime = m_target_idletime;
591 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
592 c->set_APDU_yazlog(1);
594 c->set_APDU_yazlog(0);
596 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
600 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
603 for (i = 0; i<num; i++)
606 Z_DefaultDiagFormat *r;
607 Z_DiagRec *p = pp[i];
608 if (p->which != Z_DiagRec_defaultFormat)
610 yaz_log(LOG_LOG, "%sError no diagnostics", m_session_str);
614 r = p->u.defaultFormat;
615 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
616 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
617 yaz_log(LOG_LOG, "%sError unknown diagnostic set", m_session_str);
620 case Z_DefaultDiagFormat_v2Addinfo:
621 yaz_log(LOG_LOG, "%sError %d %s:%s",
623 *r->condition, diagbib1_str(*r->condition),
626 case Z_DefaultDiagFormat_v3Addinfo:
627 yaz_log(LOG_LOG, "%sError %d %s:%s",
629 *r->condition, diagbib1_str(*r->condition),
636 int Yaz_Proxy::convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu)
638 if (!m_stylesheet_xsp || p->num_records <= 0)
639 return 0; /* no XSLT to be done ... */
641 m_stylesheet_offset = 0;
642 m_stylesheet_nprl = p;
643 m_stylesheet_apdu = apdu;
648 void Yaz_Proxy::convert_xsl_delay()
650 Z_NamePlusRecord *npr = m_stylesheet_nprl->records[m_stylesheet_offset];
651 if (npr->which == Z_NamePlusRecord_databaseRecord)
653 Z_External *r = npr->u.databaseRecord;
654 if (r->which == Z_External_octet)
657 fwrite((char*) r->u.octet_aligned->buf, 1, r->u.octet_aligned->len, stdout);
659 xmlDocPtr res, doc = xmlParseMemory(
660 (char*) r->u.octet_aligned->buf,
661 r->u.octet_aligned->len);
664 yaz_log(LOG_LOG, "%sXSLT convert %d",
665 m_session_str, m_stylesheet_offset);
666 res = xsltApplyStylesheet(m_stylesheet_xsp, doc, 0);
672 xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
674 m_stylesheet_nprl->records[m_stylesheet_offset]->
676 z_ext_record(odr_encode(), VAL_TEXT_XML,
677 (char*) out_buf, out_len);
685 m_stylesheet_offset++;
686 if (m_stylesheet_offset == m_stylesheet_nprl->num_records)
688 m_stylesheet_nprl = 0;
689 if (m_stylesheet_xsp)
690 xsltFreeStylesheet(m_stylesheet_xsp);
691 m_stylesheet_xsp = 0;
692 timeout(m_client_idletime);
693 send_PDU_convert(m_stylesheet_apdu);
699 void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
703 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
704 yaz_marc_t mt = yaz_marc_create();
705 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
706 yaz_marc_iconv(mt, cd);
707 for (i = 0; i < p->num_records; i++)
709 Z_NamePlusRecord *npr = p->records[i];
710 if (npr->which == Z_NamePlusRecord_databaseRecord)
712 Z_External *r = npr->u.databaseRecord;
713 if (r->which == Z_External_octet)
717 if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
718 r->u.octet_aligned->len,
721 npr->u.databaseRecord = z_ext_record(odr_encode(),
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);
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_REQ_CLIENT)
1388 yaz_log (LOG_LOG, "%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 m_parent->low_socket_close();
1572 if (m_stylesheet_xsp)
1573 xsltFreeStylesheet(m_stylesheet_xsp);
1575 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1577 m_stylesheet_offset = 0;
1578 xfree(stylesheet_name);
1580 m_parent->low_socket_open();
1584 sr->preferredRecordSyntax =
1585 yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN, VAL_USMARC);
1590 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1592 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1593 new_apdu->u.searchResponse->records =
1594 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1595 *new_apdu->u.searchResponse->searchStatus = 0;
1597 send_to_client(new_apdu);
1602 else if (apdu->which == Z_APDU_presentRequest)
1604 Z_PresentRequest *pr = apdu->u.presentRequest;
1607 Yaz_ProxyConfig *cfg = check_reconfigure();
1609 char *stylesheet_name = 0;
1611 err = cfg->check_syntax(odr_encode(), m_default_target,
1612 pr->preferredRecordSyntax,
1613 pr->recordComposition,
1614 &addinfo, &stylesheet_name, &m_schema);
1615 if (stylesheet_name)
1617 m_parent->low_socket_close();
1619 if (m_stylesheet_xsp)
1620 xsltFreeStylesheet(m_stylesheet_xsp);
1622 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1624 m_stylesheet_offset = 0;
1625 xfree(stylesheet_name);
1627 m_parent->low_socket_open();
1631 pr->preferredRecordSyntax =
1632 yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN, VAL_USMARC);
1637 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1639 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1640 new_apdu->u.presentResponse->records =
1641 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1642 *new_apdu->u.presentResponse->presentStatus =
1643 Z_PresentStatus_failure;
1645 send_to_client(new_apdu);
1653 Z_ElementSetNames *Yaz_Proxy::mk_esn_from_schema(ODR o, const char *schema)
1657 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1658 odr_malloc(o, sizeof(Z_ElementSetNames));
1659 esn->which = Z_ElementSetNames_generic;
1660 esn->u.generic = odr_strdup(o, schema);
1664 void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
1668 odr_destroy(m_s2z_odr_init);
1671 if (m_s2z_odr_search)
1673 odr_destroy(m_s2z_odr_search);
1674 m_s2z_odr_search = 0;
1677 m_http_keepalive = 0;
1679 if (!strcmp(hreq->version, "1.0"))
1681 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1682 if (v && !strcmp(v, "Keep-Alive"))
1683 m_http_keepalive = 1;
1685 m_http_keepalive = 0;
1686 m_http_version = "1.0";
1690 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1691 if (v && !strcmp(v, "close"))
1692 m_http_keepalive = 0;
1694 m_http_keepalive = 1;
1695 m_http_version = "1.1";
1698 Z_SRW_PDU *srw_pdu = 0;
1699 Z_SOAP *soap_package = 0;
1701 Z_SRW_diagnostic *diagnostic = 0;
1702 int num_diagnostic = 0;
1703 if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1705 || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1706 &charset, &diagnostic, &num_diagnostic) == 0)
1708 m_s2z_odr_init = odr_createmem(ODR_ENCODE);
1709 m_s2z_odr_search = odr_createmem(ODR_ENCODE);
1710 m_soap_ns = odr_strdup(m_s2z_odr_search, soap_package->ns);
1711 m_s2z_init_apdu = 0;
1712 m_s2z_search_apdu = 0;
1713 m_s2z_present_apdu = 0;
1715 m_s2z_stylesheet = 0;
1717 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
1719 Z_SRW_searchRetrieveRequest *srw_req = srw_pdu->u.request;
1721 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1722 // recordXPath unsupported.
1723 if (srw_req->recordXPath)
1725 yaz_add_srw_diagnostic(odr_decode(),
1726 &diagnostic, &num_diagnostic,
1729 // must have a query
1730 if (!srw_req->query.cql)
1732 yaz_add_srw_diagnostic(odr_decode(),
1733 &diagnostic, &num_diagnostic,
1737 if (srw_req->sort_type != Z_SRW_sort_type_none)
1739 yaz_add_srw_diagnostic(odr_decode(),
1740 &diagnostic, &num_diagnostic,
1744 if (srw_req->stylesheet)
1746 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1748 // set packing for response records ..
1749 if (srw_req->recordPacking &&
1750 !strcmp(srw_req->recordPacking, "xml"))
1751 m_s2z_packing = Z_SRW_recordPacking_XML;
1753 m_s2z_packing = Z_SRW_recordPacking_string;
1757 Z_SRW_PDU *srw_pdu =
1758 yaz_srw_get(odr_encode(),
1759 Z_SRW_searchRetrieve_response);
1760 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1762 srw_res->diagnostics = diagnostic;
1763 srw_res->num_diagnostics = num_diagnostic;
1764 send_srw_response(srw_pdu);
1768 // prepare search PDU
1769 m_s2z_search_apdu = zget_APDU(m_s2z_odr_search,
1770 Z_APDU_searchRequest);
1771 Z_SearchRequest *z_searchRequest =
1772 m_s2z_search_apdu->u.searchRequest;
1774 z_searchRequest->num_databaseNames = 1;
1775 z_searchRequest->databaseNames = (char**)
1776 odr_malloc(m_s2z_odr_search, sizeof(char *));
1777 z_searchRequest->databaseNames[0] = odr_strdup(m_s2z_odr_search,
1780 // query transformation
1781 Z_Query *query = (Z_Query *)
1782 odr_malloc(m_s2z_odr_search, sizeof(Z_Query));
1783 z_searchRequest->query = query;
1785 if (srw_req->query_type == Z_SRW_query_type_cql)
1787 Z_External *ext = (Z_External *)
1788 odr_malloc(m_s2z_odr_search, sizeof(*ext));
1789 ext->direct_reference =
1790 odr_getoidbystr(m_s2z_odr_search, "1.2.840.10003.16.2");
1791 ext->indirect_reference = 0;
1792 ext->descriptor = 0;
1793 ext->which = Z_External_CQL;
1794 ext->u.cql = srw_req->query.cql;
1796 query->which = Z_Query_type_104;
1797 query->u.type_104 = ext;
1799 else if (srw_req->query_type == Z_SRW_query_type_pqf)
1801 Z_RPNQuery *RPNquery;
1802 YAZ_PQF_Parser pqf_parser;
1804 pqf_parser = yaz_pqf_create ();
1806 RPNquery = yaz_pqf_parse (pqf_parser, m_s2z_odr_search,
1807 srw_req->query.pqf);
1810 const char *pqf_msg;
1812 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1813 yaz_log(LOG_LOG, "%*s^\n", off+4, "");
1814 yaz_log(LOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
1816 send_to_srw_client_error(10, 0);
1819 query->which = Z_Query_type_1;
1820 query->u.type_1 = RPNquery;
1822 yaz_pqf_destroy (pqf_parser);
1826 send_to_srw_client_error(7, "query");
1831 m_s2z_present_apdu = 0;
1833 if (srw_req->maximumRecords)
1834 max = *srw_req->maximumRecords;
1836 if (srw_req->startRecord)
1837 start = *srw_req->startRecord;
1840 // Some backend, such as Voyager doesn't honor piggyback
1841 // So we use present always (0 &&).
1842 if (0 && start <= 1) // Z39.50 piggyback
1844 *z_searchRequest->smallSetUpperBound = max;
1845 *z_searchRequest->mediumSetPresentNumber = max;
1846 *z_searchRequest->largeSetLowerBound = 2000000000; // 2e9
1848 z_searchRequest->preferredRecordSyntax =
1849 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1851 if (srw_req->recordSchema)
1853 z_searchRequest->smallSetElementSetNames =
1854 z_searchRequest->mediumSetElementSetNames =
1855 mk_esn_from_schema(m_s2z_odr_search,
1856 srw_req->recordSchema);
1859 else // Z39.50 present
1861 m_s2z_present_apdu = zget_APDU(m_s2z_odr_search,
1862 Z_APDU_presentRequest);
1863 Z_PresentRequest *z_presentRequest =
1864 m_s2z_present_apdu->u.presentRequest;
1865 *z_presentRequest->resultSetStartPoint = start;
1866 *z_presentRequest->numberOfRecordsRequested = max;
1867 z_presentRequest->preferredRecordSyntax =
1868 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1870 if (srw_req->recordSchema)
1872 z_presentRequest->recordComposition =
1873 (Z_RecordComposition *)
1874 odr_malloc(m_s2z_odr_search,
1875 sizeof(Z_RecordComposition));
1876 z_presentRequest->recordComposition->which =
1877 Z_RecordComp_simple;
1878 z_presentRequest->recordComposition->u.simple =
1879 mk_esn_from_schema(m_s2z_odr_search,
1880 srw_req->recordSchema);
1886 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1887 Z_APDU_initRequest);
1889 // prevent m_initRequest_apdu memory from being grabbed
1890 // in Yaz_Proxy::handle_incoming_Z_PDU
1891 m_initRequest_apdu = m_s2z_init_apdu;
1892 handle_incoming_Z_PDU(m_s2z_init_apdu);
1897 handle_incoming_Z_PDU(m_s2z_search_apdu);
1901 else if (srw_pdu->which == Z_SRW_explain_request)
1903 Z_SRW_explainRequest *srw_req = srw_pdu->u.explain_request;
1905 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1908 if (srw_req->stylesheet)
1910 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1912 if (srw_req->recordPacking &&
1913 !strcmp(srw_req->recordPacking, "xml"))
1914 m_s2z_packing = Z_SRW_recordPacking_XML;
1916 m_s2z_packing = Z_SRW_recordPacking_string;
1920 send_srw_explain_response(diagnostic, num_diagnostic);
1926 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1927 Z_APDU_initRequest);
1929 // prevent m_initRequest_apdu memory from being grabbed
1930 // in Yaz_Proxy::handle_incoming_Z_PDU
1931 m_initRequest_apdu = m_s2z_init_apdu;
1932 handle_incoming_Z_PDU(m_s2z_init_apdu);
1935 send_srw_explain_response(0, 0);
1938 else if (srw_pdu->which == Z_SRW_scan_request)
1940 m_s2z_database = odr_strdup(m_s2z_odr_init,
1941 srw_pdu->u.scan_request->database);
1943 yaz_add_srw_diagnostic(odr_decode(),
1944 &diagnostic, &num_diagnostic,
1946 Z_SRW_PDU *srw_pdu =
1947 yaz_srw_get(odr_encode(),
1948 Z_SRW_scan_response);
1949 Z_SRW_scanResponse *srw_res = srw_pdu->u.scan_response;
1951 srw_res->diagnostics = diagnostic;
1952 srw_res->num_diagnostics = num_diagnostic;
1953 send_srw_response(srw_pdu);
1960 send_to_srw_client_error(4, 0);
1964 Z_GDU *p = z_get_HTTP_Response(odr_encode(), 400);
1969 void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
1971 Z_ReferenceId **refid = get_referenceIdP(apdu);
1972 nmem_reset(m_referenceId_mem);
1973 if (refid && *refid)
1975 m_referenceId = (Z_ReferenceId *)
1976 nmem_malloc(m_referenceId_mem, sizeof(*m_referenceId));
1977 m_referenceId->len = m_referenceId->size = (*refid)->len;
1978 m_referenceId->buf = (unsigned char *)
1979 nmem_malloc(m_referenceId_mem, (*refid)->len);
1980 memcpy(m_referenceId->buf, (*refid)->buf, (*refid)->len);
1985 if (!m_client && m_invalid_session)
1987 m_apdu_invalid_session = apdu;
1988 m_mem_invalid_session = odr_extract_mem(odr_decode());
1989 apdu = m_initRequest_apdu;
1992 // Determine our client.
1993 Z_OtherInformation **oi;
1994 get_otherInfoAPDU(apdu, &oi);
1995 m_client = get_client(apdu, get_cookie(oi), get_proxy(oi));
2001 m_client->m_server = this;
2003 if (apdu->which == Z_APDU_initRequest)
2005 if (apdu->u.initRequest->implementationId)
2006 yaz_log(LOG_LOG, "%simplementationId: %s",
2007 m_session_str, apdu->u.initRequest->implementationId);
2008 if (apdu->u.initRequest->implementationName)
2009 yaz_log(LOG_LOG, "%simplementationName: %s",
2010 m_session_str, apdu->u.initRequest->implementationName);
2011 if (apdu->u.initRequest->implementationVersion)
2012 yaz_log(LOG_LOG, "%simplementationVersion: %s",
2013 m_session_str, apdu->u.initRequest->implementationVersion);
2014 if (m_initRequest_apdu == 0)
2016 if (m_initRequest_mem)
2017 nmem_destroy(m_initRequest_mem);
2018 m_initRequest_apdu = apdu;
2019 m_initRequest_mem = odr_extract_mem(odr_decode());
2021 m_initRequest_preferredMessageSize = *apdu->u.initRequest->
2022 preferredMessageSize;
2023 *apdu->u.initRequest->preferredMessageSize = 1024*1024;
2024 m_initRequest_maximumRecordSize = *apdu->u.initRequest->
2026 *apdu->u.initRequest->maximumRecordSize = 1024*1024;
2028 // save init options for the response..
2029 m_initRequest_options = apdu->u.initRequest->options;
2031 apdu->u.initRequest->options =
2032 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2033 sizeof(Odr_bitmask));
2034 ODR_MASK_ZERO(apdu->u.initRequest->options);
2036 for (i = 0; i<= 24; i++)
2037 ODR_MASK_SET(apdu->u.initRequest->options, i);
2038 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2039 Z_Options_negotiationModel);
2040 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2041 Z_Options_concurrentOperations);
2044 m_initRequest_version = apdu->u.initRequest->protocolVersion;
2045 apdu->u.initRequest->protocolVersion =
2046 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2047 sizeof(Odr_bitmask));
2048 ODR_MASK_ZERO(apdu->u.initRequest->protocolVersion);
2050 for (i = 0; i<= 8; i++)
2051 ODR_MASK_SET(apdu->u.initRequest->protocolVersion, i);
2053 if (m_client->m_init_flag)
2055 if (handle_init_response_for_invalid_session(apdu))
2057 if (m_client->m_initResponse)
2059 Z_APDU *apdu2 = m_client->m_initResponse;
2060 apdu2->u.initResponse->otherInfo = 0;
2061 if (m_client->m_cookie && *m_client->m_cookie)
2062 set_otherInformationString(apdu2, VAL_COOKIE, 1,
2063 m_client->m_cookie);
2064 apdu2->u.initResponse->referenceId =
2065 apdu->u.initRequest->referenceId;
2066 apdu2->u.initResponse->options = m_client->m_initResponse_options;
2067 apdu2->u.initResponse->protocolVersion =
2068 m_client->m_initResponse_version;
2070 send_to_client(apdu2);
2074 m_client->m_init_flag = 1;
2076 handle_max_record_retrieve(apdu);
2079 apdu = handle_syntax_validation(apdu);
2082 apdu = handle_query_transformation(apdu);
2085 apdu = handle_query_validation(apdu);
2088 apdu = result_set_optimize(apdu);
2091 m_client->timeout(m_target_idletime); // mark it active even
2092 // though we didn't use it
2096 // delete other info part from PDU before sending to target
2097 get_otherInfoAPDU(apdu, &oi);
2101 if (apdu->which == Z_APDU_presentRequest &&
2102 m_client->m_resultSetStartPoint == 0)
2104 Z_PresentRequest *pr = apdu->u.presentRequest;
2105 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
2106 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
2108 m_client->m_resultSetStartPoint = 0;
2110 if (m_client->send_to_target(apdu) < 0)
2117 m_client->m_waiting = 1;
2120 void Yaz_Proxy::connectNotify()
2124 void Yaz_Proxy::shutdown()
2126 m_invalid_session = 0;
2127 // only keep if keep_alive flag is set...
2129 m_client->m_pdu_recv < m_keepalive_limit_pdu &&
2130 m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
2131 m_client->m_waiting == 0)
2133 yaz_log(LOG_LOG, "%sShutdown (client to proxy) keepalive %s",
2135 m_client->get_hostname());
2136 yaz_log(LOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
2137 m_session_str, m_client->m_pdu_recv,
2138 m_client->m_bytes_sent + m_client->m_bytes_recv,
2139 m_keepalive_limit_bw, m_keepalive_limit_pdu);
2140 assert (m_client->m_waiting != 2);
2141 // Tell client (if any) that no server connection is there..
2142 m_client->m_server = 0;
2143 m_invalid_session = 0;
2147 yaz_log (LOG_LOG, "%sShutdown (client to proxy) close %s",
2149 m_client->get_hostname());
2150 assert (m_client->m_waiting != 2);
2155 yaz_log (LOG_LOG, "%sshutdown (client to proxy) bad state",
2161 yaz_log (LOG_LOG, "%sShutdown (client to proxy)",
2165 m_parent->pre_init();
2169 const char *Yaz_ProxyClient::get_session_str()
2173 return m_server->get_session_str();
2176 void Yaz_ProxyClient::shutdown()
2178 yaz_log (LOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
2184 void Yaz_Proxy::failNotify()
2187 yaz_log (LOG_LOG, "%sConnection closed by client",
2192 void Yaz_ProxyClient::failNotify()
2195 m_server->inc_request_no();
2196 yaz_log (LOG_LOG, "%sConnection closed by target %s",
2197 get_session_str(), get_hostname());
2201 void Yaz_ProxyClient::connectNotify()
2203 const char *s = get_session_str();
2204 const char *h = get_hostname();
2205 yaz_log (LOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
2207 timeout(m_target_idletime);
2212 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
2213 *the_PDU_Observable, int fd)
2215 return new Yaz_ProxyClient(the_PDU_Observable, 0);
2218 Yaz_ProxyClient::~Yaz_ProxyClient()
2223 m_next->m_prev = m_prev;
2224 m_waiting = 2; // for debugging purposes only.
2225 odr_destroy(m_init_odr);
2226 delete m_last_query;
2227 xfree (m_last_resultSetId);
2231 void Yaz_ProxyClient::pre_init_client()
2233 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
2234 Z_InitRequest *req = apdu->u.initRequest;
2237 for (i = 0; i<= 24; i++)
2238 ODR_MASK_SET(req->options, i);
2239 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2240 Z_Options_negotiationModel);
2241 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2242 Z_Options_concurrentOperations);
2243 for (i = 0; i<= 10; i++)
2244 ODR_MASK_SET(req->protocolVersion, i);
2246 if (send_to_target(apdu) < 0)
2257 void Yaz_Proxy::pre_init()
2260 const char *name = 0;
2261 const char *zurl_in_use[MAX_ZURL_PLEX];
2262 int limit_bw, limit_pdu, limit_req;
2263 int target_idletime, client_idletime;
2265 int keepalive_limit_bw, keepalive_limit_pdu;
2267 const char *cql2rpn = 0;
2269 Yaz_ProxyConfig *cfg = check_reconfigure();
2273 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
2278 for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
2279 &limit_bw, &limit_pdu, &limit_req,
2280 &target_idletime, &client_idletime,
2282 &keepalive_limit_bw,
2283 &keepalive_limit_pdu,
2290 for (j = 0; zurl_in_use[j]; j++)
2294 int spare_waiting = 0;
2297 for (c = m_clientPool; c; c = c->m_next)
2299 if (!strcmp(zurl_in_use[j], c->get_hostname()))
2301 if (c->m_cookie == 0)
2303 if (c->m_server == 0)
2315 yaz_log(LOG_LOG, "%spre-init %s %s use=%d other=%d spare=%d "
2316 "sparew=%d preinit=%d",m_session_str,
2317 name, zurl_in_use[j], in_use, other,
2318 spare, spare_waiting, pre_init);
2319 if (spare + spare_waiting < pre_init)
2321 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
2322 c->m_next = m_clientPool;
2324 c->m_next->m_prev = &c->m_next;
2326 c->m_prev = &m_clientPool;
2328 if (m_log_mask & PROXY_LOG_APDU_SERVER)
2329 c->set_APDU_yazlog(1);
2331 c->set_APDU_yazlog(0);
2333 if (c->client(zurl_in_use[j]))
2341 c->m_target_idletime = target_idletime;
2342 c->m_seqno = m_seqno++;
2349 void Yaz_Proxy::timeoutNotify()
2355 timeout(m_client_idletime);
2356 Z_GDU *apdu = m_bw_hold_PDU;
2359 if (apdu->which == Z_GDU_Z3950)
2360 handle_incoming_Z_PDU(apdu->u.z3950);
2361 else if (apdu->which == Z_GDU_HTTP_Request)
2362 handle_incoming_HTTP(apdu->u.HTTP_Request);
2364 else if (m_stylesheet_nprl)
2365 convert_xsl_delay();
2370 yaz_log (LOG_LOG, "%sTimeout (client to proxy)", m_session_str);
2381 void Yaz_Proxy::markInvalid()
2384 m_invalid_session = 1;
2387 void Yaz_ProxyClient::timeoutNotify()
2390 m_server->inc_request_no();
2392 yaz_log (LOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
2396 if (m_server && m_init_flag)
2398 // target timed out in a session that was properly initialized
2399 // server object stay alive but we mark it as invalid so it
2400 // gets initialized again
2401 m_server->markInvalid();
2407 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
2408 Yaz_Proxy *parent) :
2409 Yaz_Z_Assoc (the_PDU_Observable)
2416 m_last_resultSetId = 0;
2417 m_last_resultCount = 0;
2421 m_init_odr = odr_createmem (ODR_DECODE);
2423 m_initResponse_options = 0;
2424 m_initResponse_version = 0;
2425 m_initResponse_preferredMessageSize = 0;
2426 m_initResponse_maximumRecordSize = 0;
2427 m_resultSetStartPoint = 0;
2428 m_bytes_sent = m_bytes_recv = 0;
2432 m_target_idletime = 600;
2436 const char *Yaz_Proxy::option(const char *name, const char *value)
2438 if (!strcmp (name, "optimize")) {
2441 m_optimize = xstrdup (value);
2448 void Yaz_ProxyClient::recv_HTTP_response(Z_HTTP_Response *apdu, int len)
2453 void Yaz_ProxyClient::recv_GDU(Z_GDU *apdu, int len)
2455 if (apdu->which == Z_GDU_Z3950)
2456 recv_Z_PDU(apdu->u.z3950, len);
2457 else if (apdu->which == Z_GDU_HTTP_Response)
2458 recv_HTTP_response(apdu->u.HTTP_Response, len);
2463 int Yaz_Proxy::handle_init_response_for_invalid_session(Z_APDU *apdu)
2465 if (!m_invalid_session)
2467 m_invalid_session = 0;
2468 handle_incoming_Z_PDU(m_apdu_invalid_session);
2469 assert (m_mem_invalid_session);
2470 nmem_destroy(m_mem_invalid_session);
2471 m_mem_invalid_session = 0;
2475 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
2477 m_bytes_recv += len;
2481 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
2482 yaz_log (LOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
2483 apdu_name(apdu), get_hostname(), len);
2484 if (apdu->which == Z_APDU_initResponse)
2486 if (!m_server) // if this is a pre init session , check for more
2488 NMEM nmem = odr_extract_mem (odr_decode());
2489 odr_reset (m_init_odr);
2490 nmem_transfer (m_init_odr->mem, nmem);
2491 m_initResponse = apdu;
2492 m_initResponse_options = apdu->u.initResponse->options;
2493 m_initResponse_version = apdu->u.initResponse->protocolVersion;
2494 m_initResponse_preferredMessageSize =
2495 *apdu->u.initResponse->preferredMessageSize;
2496 m_initResponse_maximumRecordSize =
2497 *apdu->u.initResponse->maximumRecordSize;
2499 Z_InitResponse *ir = apdu->u.initResponse;
2500 char *im0 = ir->implementationName;
2503 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
2510 strcat(im1, "(YAZ Proxy)");
2511 ir->implementationName = im1;
2513 nmem_destroy (nmem);
2515 if (m_server && m_server->handle_init_response_for_invalid_session(apdu))
2518 if (apdu->which == Z_APDU_searchResponse)
2520 Z_SearchResponse *sr = apdu->u.searchResponse;
2521 m_last_resultCount = *sr->resultCount;
2522 int status = *sr->searchStatus;
2523 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
2527 if (sr->records && sr->records->which == Z_Records_DBOSD)
2529 m_cache.add(odr_decode(),
2530 sr->records->u.databaseOrSurDiagnostics, 1,
2535 if (apdu->which == Z_APDU_presentResponse)
2537 Z_PresentResponse *pr = apdu->u.presentResponse;
2541 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2542 Z_SearchResponse *sr = new_apdu->u.searchResponse;
2543 sr->referenceId = pr->referenceId;
2544 *sr->resultCount = m_last_resultCount;
2545 sr->records = pr->records;
2546 sr->nextResultSetPosition = pr->nextResultSetPosition;
2547 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
2551 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
2553 m_cache.add(odr_decode(),
2554 pr->records->u.databaseOrSurDiagnostics,
2555 m_resultSetStartPoint, -1);
2556 m_resultSetStartPoint = 0;
2560 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
2563 m_server->send_to_client(apdu);
2565 if (apdu->which == Z_APDU_close)
2571 void Yaz_Proxy::low_socket_close()
2574 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
2575 if (m_lo_fd[i] >= 0)
2576 ::close(m_lo_fd[i]);
2579 void Yaz_Proxy::low_socket_open()
2582 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
2583 m_lo_fd[i] = open("/dev/null", O_RDONLY);
2586 int Yaz_Proxy::server(const char *addr)
2588 int r = Yaz_Z_Assoc::server(addr);
2591 yaz_log(LOG_LOG, "%sStarted proxy " VERSION " on %s", m_session_str, addr);