1 /* $Id: yaz-proxy.cpp,v 1.27 2005-05-27 18:07:49 adam Exp $
2 Copyright (c) 1998-2005, Index Data.
4 This file is part of the yaz-proxy.
6 YAZ proxy 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 YAZ proxy 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 YAZ proxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #define HAVE_SYS_STAT_H 1
24 #define HAVE_SYS_TYPES_H 1
34 #include <sys/types.h>
46 #include <yaz/marcdisp.h>
47 #include <yaz/yaz-iconv.h>
49 #include <yaz/diagbib1.h>
51 #include <yaz/pquery.h>
52 #include <yaz/otherinfo.h>
53 #include <yaz/charneg.h>
56 static const char *apdu_name(Z_APDU *apdu)
60 case Z_APDU_initRequest:
62 case Z_APDU_initResponse:
63 return "initResponse";
64 case Z_APDU_searchRequest:
65 return "searchRequest";
66 case Z_APDU_searchResponse:
67 return "searchResponse";
68 case Z_APDU_presentRequest:
69 return "presentRequest";
70 case Z_APDU_presentResponse:
71 return "presentResponse";
72 case Z_APDU_deleteResultSetRequest:
73 return "deleteResultSetRequest";
74 case Z_APDU_deleteResultSetResponse:
75 return "deleteResultSetResponse";
76 case Z_APDU_scanRequest:
78 case Z_APDU_scanResponse:
79 return "scanResponse";
80 case Z_APDU_sortRequest:
82 case Z_APDU_sortResponse:
83 return "sortResponse";
84 case Z_APDU_extendedServicesRequest:
85 return "extendedServicesRequest";
86 case Z_APDU_extendedServicesResponse:
87 return "extendedServicesResponse";
94 static const char *gdu_name(Z_GDU *gdu)
99 return apdu_name(gdu->u.z3950);
100 case Z_GDU_HTTP_Request:
101 return "HTTP Request";
102 case Z_GDU_HTTP_Response:
103 return "HTTP Response";
105 return "Unknown request/response";
107 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
109 Yaz_Z_Assoc(the_PDU_Observable), m_bw_stat(60), m_pdu_stat(60)
111 m_PDU_Observable = the_PDU_Observable;
116 m_keepalive_limit_bw = 500000;
117 m_keepalive_limit_pdu = 1000;
119 m_default_target = 0;
120 m_proxy_negotiation_charset = 0;
121 m_proxy_negotiation_lang = 0;
122 m_charset_converter = new Yaz_CharsetConverter;
126 m_client_idletime = 600;
127 m_target_idletime = 600;
128 m_optimize = xstrdup ("1");
129 strcpy(m_session_str, "0 ");
136 m_max_record_retrieve = 0;
140 m_invalid_session = 0;
142 m_referenceId_mem = nmem_create();
144 m_marcxml_mode = none;
145 m_stylesheet_xsp = 0;
146 m_stylesheet_nprl = 0;
147 m_s2z_stylesheet = 0;
151 m_backend_charset = 0;
153 m_initRequest_apdu = 0;
154 m_initRequest_mem = 0;
155 m_initRequest_preferredMessageSize = 0;
156 m_initRequest_maximumRecordSize = 0;
157 m_initRequest_options = 0;
158 m_initRequest_version = 0;
159 m_initRequest_oi_negotiation_charsets = 0;
160 m_initRequest_oi_negotiation_num_charsets = 0;
161 m_initRequest_oi_negotiation_langs = 0;
162 m_initRequest_oi_negotiation_num_langs = 0;
163 m_initRequest_oi_negotiation_selected = 0;
164 m_apdu_invalid_session = 0;
165 m_mem_invalid_session = 0;
167 m_s2z_odr_search = 0;
169 m_s2z_search_apdu = 0;
170 m_s2z_present_apdu = 0;
171 m_http_keepalive = 0;
174 m_s2z_packing = Z_SRW_recordPacking_string;
175 #if HAVE_GETTIMEOFDAY
176 m_time_tv = xmalloc(sizeof(struct timeval));
177 struct timeval *tv = (struct timeval *) m_time_tv;
183 m_usemarcon_ini_stage1 = 0;
184 m_usemarcon_ini_stage2 = 0;
185 m_usemarcon = new Yaz_usemarcon();
190 Yaz_Proxy::~Yaz_Proxy()
192 yaz_log(YLOG_LOG, "%sClosed %d/%d sent/recv bytes total", m_session_str,
193 m_bytes_sent, m_bytes_recv);
194 nmem_destroy(m_initRequest_mem);
195 nmem_destroy(m_mem_invalid_session);
196 nmem_destroy(m_referenceId_mem);
198 xfree(m_proxyTarget);
199 xfree(m_default_target);
200 xfree(m_proxy_negotiation_charset);
201 xfree(m_proxy_negotiation_lang);
202 delete m_charset_converter;
206 if (m_stylesheet_xsp)
207 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
212 xfree (m_backend_type);
213 xfree (m_backend_charset);
214 xfree (m_usemarcon_ini_stage1);
215 xfree (m_usemarcon_ini_stage2);
218 odr_destroy(m_s2z_odr_init);
219 if (m_s2z_odr_search)
220 odr_destroy(m_s2z_odr_search);
226 void Yaz_Proxy::set_debug_mode(int mode)
231 int Yaz_Proxy::set_config(const char *config)
234 m_config = new Yaz_ProxyConfig();
235 xfree(m_config_fname);
236 m_config_fname = xstrdup(config);
237 int r = m_config->read_xml(config);
239 m_config->get_generic_info(&m_log_mask, &m_max_clients);
243 void Yaz_Proxy::set_default_target(const char *target)
245 xfree (m_default_target);
246 m_default_target = 0;
248 m_default_target = (char *) xstrdup (target);
251 void Yaz_Proxy::set_proxy_negotiation (const char *charset, const char *lang)
253 yaz_log(YLOG_LOG, "%sSet the proxy negotiation: charset to '%s', "
254 "language to '%s'", m_session_str, charset?charset:"none",
256 xfree (m_proxy_negotiation_charset);
257 xfree (m_proxy_negotiation_lang);
258 m_proxy_negotiation_charset = m_proxy_negotiation_lang = 0;
260 m_proxy_negotiation_charset = (char *) xstrdup (charset);
262 m_proxy_negotiation_lang = (char *) xstrdup (lang);
265 Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
268 return m_parent->check_reconfigure();
270 Yaz_ProxyConfig *cfg = m_config;
273 yaz_log(YLOG_LOG, "reconfigure");
275 if (m_config_fname && cfg)
277 yaz_log(YLOG_LOG, "reconfigure config %s", m_config_fname);
278 int r = cfg->read_xml(m_config_fname);
280 yaz_log(YLOG_WARN, "reconfigure failed");
284 cfg->get_generic_info(&m_log_mask, &m_max_clients);
288 yaz_log(YLOG_LOG, "reconfigure");
294 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
295 *the_PDU_Observable, int fd)
298 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable, this);
299 new_proxy->m_config = 0;
300 new_proxy->m_config_fname = 0;
301 new_proxy->timeout(m_client_idletime);
302 new_proxy->m_target_idletime = m_target_idletime;
303 new_proxy->set_default_target(m_default_target);
304 new_proxy->m_max_clients = m_max_clients;
305 new_proxy->m_log_mask = m_log_mask;
306 new_proxy->set_APDU_log(get_APDU_log());
307 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
308 new_proxy->set_APDU_yazlog(1);
310 new_proxy->set_APDU_yazlog(0);
311 sprintf(new_proxy->m_session_str, "%ld:%d ", (long) time(0), m_session_no);
313 yaz_log (YLOG_LOG, "%sNew session %s", new_proxy->m_session_str,
314 the_PDU_Observable->getpeername());
315 new_proxy->set_proxy_negotiation(m_proxy_negotiation_charset,
316 m_proxy_negotiation_lang);
320 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
323 Z_OtherInformationUnit *oi;
325 ent.proto = PROTO_Z3950;
326 ent.oclass = CLASS_USERINFO;
327 ent.value = (oid_value) VAL_COOKIE;
328 assert (oid_ent_to_oid (&ent, oid));
330 if (oid_ent_to_oid (&ent, oid) &&
331 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
332 oi->which == Z_OtherInfo_characterInfo)
333 return oi->information.characterInfo;
336 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
339 Z_OtherInformationUnit *oi;
341 ent.proto = PROTO_Z3950;
342 ent.oclass = CLASS_USERINFO;
343 ent.value = (oid_value) VAL_PROXY;
344 if (oid_ent_to_oid (&ent, oid) &&
345 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
346 oi->which == Z_OtherInfo_characterInfo)
347 return oi->information.characterInfo;
350 const char *Yaz_Proxy::load_balance(const char **url)
352 int zurl_in_use[MAX_ZURL_PLEX];
353 int zurl_in_spare[MAX_ZURL_PLEX];
357 for (i = 0; i<MAX_ZURL_PLEX; i++)
360 zurl_in_spare[i] = 0;
362 for (c = m_parent->m_clientPool; c; c = c->m_next)
364 for (i = 0; url[i]; i++)
365 if (!strcmp(url[i], c->get_hostname()))
368 if (c->m_cookie == 0 && c->m_server == 0 && c->m_waiting == 0)
372 int min_use = 100000;
373 int spare_for_min = 0;
375 const char *ret_min = 0;
376 const char *ret_spare = 0;
377 for (i = 0; url[i]; i++)
379 yaz_log(YLOG_DEBUG, "%szurl=%s use=%d spare=%d",
380 m_session_str, url[i], zurl_in_use[i], zurl_in_spare[i]);
381 if (min_use > zurl_in_use[i])
384 min_use = zurl_in_use[i];
385 spare_for_min = zurl_in_spare[i];
387 if (max_spare < zurl_in_spare[i])
390 max_spare = zurl_in_spare[i];
396 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
397 const char *proxy_host)
400 Yaz_Proxy *parent = m_parent;
401 Yaz_ProxyClient *c = m_client;
405 const char *url[MAX_ZURL_PLEX];
406 Yaz_ProxyConfig *cfg = check_reconfigure();
409 if (parent && parent->m_debug_mode)
411 // only to be enabled for debugging...
412 if (!strcmp(proxy_host, "stop"))
415 xfree(m_default_target);
416 m_default_target = xstrdup(proxy_host);
418 proxy_host = m_default_target;
419 int client_idletime = -1;
420 const char *cql2rpn_fname = 0;
421 const char *negotiation_charset = 0;
422 const char *negotiation_lang = 0;
423 const char *query_charset = 0;
424 url[0] = m_default_target;
429 cfg->get_target_info(proxy_host, url, &m_bw_max,
430 &m_pdu_max, &m_max_record_retrieve,
431 &m_target_idletime, &client_idletime,
432 &parent->m_max_clients,
433 &m_keepalive_limit_bw,
434 &m_keepalive_limit_pdu,
437 &negotiation_charset,
441 if (client_idletime != -1)
443 m_client_idletime = client_idletime;
444 timeout(m_client_idletime);
447 m_cql2rpn.set_pqf_file(cql2rpn_fname);
448 if (negotiation_charset || negotiation_lang)
450 set_proxy_negotiation(negotiation_charset,
453 m_charset_converter->set_target_query_charset(query_charset);
456 yaz_log(YLOG_LOG, "%sNo default target", m_session_str);
459 // we don't handle multiplexing for cookie session, so we just
460 // pick the first one in this case (anonymous users will be able
461 // to use any backend)
462 if (cookie && *cookie)
463 m_proxyTarget = (char*) xstrdup(url[0]);
465 m_proxyTarget = (char*) xstrdup(load_balance(url));
467 if (cookie && *cookie)
468 { // search in sessions with a cookie
469 for (c = parent->m_clientPool; c; c = c->m_next)
472 assert (*c->m_prev == c);
473 if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
474 !strcmp(m_proxyTarget, c->get_hostname()))
477 // The following handles "cancel"
478 // If connection is busy (waiting for PDU) and
479 // we have an initRequest we can safely do re-open
480 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
482 yaz_log (YLOG_LOG, "%s REOPEN target=%s", m_session_str,
489 c->m_last_resultCount = 0;
490 c->m_sr_transform = 0;
492 c->m_resultSetStartPoint = 0;
493 c->m_target_idletime = m_target_idletime;
494 if (c->client(m_proxyTarget))
501 c->m_seqno = parent->m_seqno;
502 if (c->m_server && c->m_server != this)
503 c->m_server->m_client = 0;
506 yaz_log (YLOG_DEBUG, "get_client 1 %p %p", this, c);
512 apdu->which == Z_APDU_initRequest &&
513 apdu->u.initRequest->idAuthentication == 0 &&
514 !ODR_MASK_GET(apdu->u.initRequest->options, Z_Options_negotiationModel))
516 // anonymous sessions without cookie.
517 // if authentication is set it is NOT anonymous se we can't share them.
518 // If charset and lang negotiation is use it is NOT anonymous session too.
519 for (c = parent->m_clientPool; c; c = c->m_next)
522 assert(*c->m_prev == c);
523 if (c->m_server == 0 && c->m_cookie == 0 &&
525 !strcmp(m_proxyTarget, c->get_hostname()))
528 yaz_log (YLOG_LOG, "%sREUSE %d %s",
529 m_session_str, parent->m_seqno, c->get_hostname());
531 c->m_seqno = parent->m_seqno;
532 assert(c->m_server == 0);
535 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
536 c->set_APDU_yazlog(1);
538 c->set_APDU_yazlog(0);
550 if (apdu->which != Z_APDU_initRequest)
552 yaz_log (YLOG_LOG, "%sno init request as first PDU", m_session_str);
555 Z_InitRequest *initRequest = apdu->u.initRequest;
557 if (initRequest->idAuthentication)
559 // the client uses authentication. We set the keepalive PDU
560 // to 0 so we don't cache it in releaseClient
561 m_keepalive_limit_pdu = 0;
563 // go through list of clients - and find the lowest/oldest one.
564 Yaz_ProxyClient *c_min = 0;
566 int no_of_clients = 0;
567 if (parent->m_clientPool)
568 yaz_log (YLOG_DEBUG, "Existing sessions");
569 for (c = parent->m_clientPool; c; c = c->m_next)
571 yaz_log (YLOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
572 c->m_waiting, c->get_hostname(),
573 c->m_cookie ? c->m_cookie : "");
575 if (min_seq < 0 || c->m_seqno < min_seq)
577 min_seq = c->m_seqno;
581 if (no_of_clients >= parent->m_max_clients)
584 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
586 yaz_log (YLOG_LOG, "%sMAXCLIENTS %d Destroy %d",
587 m_session_str, parent->m_max_clients, c->m_seqno);
588 if (c->m_server && c->m_server != this)
594 yaz_log (YLOG_LOG, "%sMAXCLIENTS %d Reuse %d %d %s",
595 m_session_str, parent->m_max_clients,
596 c->m_seqno, parent->m_seqno, c->get_hostname());
600 c->m_cookie = xstrdup(cookie);
601 c->m_seqno = parent->m_seqno;
602 if (c->m_server && c->m_server != this)
604 c->m_server->m_client = 0;
608 c->m_target_idletime = m_target_idletime;
609 c->timeout(m_target_idletime);
611 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
612 c->set_APDU_yazlog(1);
614 c->set_APDU_yazlog(0);
621 yaz_log (YLOG_LOG, "%sNEW %d %s",
622 m_session_str, parent->m_seqno, m_proxyTarget);
623 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), parent);
624 c->m_next = parent->m_clientPool;
626 c->m_next->m_prev = &c->m_next;
627 parent->m_clientPool = c;
628 c->m_prev = &parent->m_clientPool;
634 c->m_cookie = xstrdup(cookie);
636 c->m_seqno = parent->m_seqno;
638 c->m_last_resultCount = 0;
641 c->m_sr_transform = 0;
643 c->m_resultSetStartPoint = 0;
645 if (c->client(m_proxyTarget))
650 c->m_target_idletime = m_target_idletime;
653 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
654 c->set_APDU_yazlog(1);
656 c->set_APDU_yazlog(0);
658 yaz_log (YLOG_DEBUG, "get_client 3 %p %p", this, c);
661 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
664 for (i = 0; i<num; i++)
667 Z_DefaultDiagFormat *r;
668 Z_DiagRec *p = pp[i];
669 if (p->which != Z_DiagRec_defaultFormat)
671 yaz_log(YLOG_LOG, "%sError no diagnostics", m_session_str);
675 r = p->u.defaultFormat;
676 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
677 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
678 yaz_log(YLOG_LOG, "%sError unknown diagnostic set", m_session_str);
681 case Z_DefaultDiagFormat_v2Addinfo:
682 yaz_log(YLOG_LOG, "%sError %d %s:%s",
684 *r->condition, diagbib1_str(*r->condition),
687 case Z_DefaultDiagFormat_v3Addinfo:
688 yaz_log(YLOG_LOG, "%sError %d %s:%s",
690 *r->condition, diagbib1_str(*r->condition),
697 int Yaz_Proxy::convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu)
699 if (!m_stylesheet_xsp || p->num_records <= 0)
700 return 0; /* no XSLT to be done ... */
702 m_stylesheet_offset = 0;
703 m_stylesheet_nprl = p;
704 m_stylesheet_apdu = apdu;
709 void Yaz_Proxy::convert_xsl_delay()
712 Z_NamePlusRecord *npr = m_stylesheet_nprl->records[m_stylesheet_offset];
713 if (npr->which == Z_NamePlusRecord_databaseRecord)
715 Z_External *r = npr->u.databaseRecord;
716 if (r->which == Z_External_octet)
719 fwrite((char*) r->u.octet_aligned->buf, 1, r->u.octet_aligned->len, stdout);
721 xmlDocPtr res, doc = xmlParseMemory(
722 (char*) r->u.octet_aligned->buf,
723 r->u.octet_aligned->len);
726 yaz_log(YLOG_LOG, "%sXSLT convert %d",
727 m_session_str, m_stylesheet_offset);
728 res = xsltApplyStylesheet((xsltStylesheetPtr) m_stylesheet_xsp,
735 xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
737 m_stylesheet_nprl->records[m_stylesheet_offset]->
739 z_ext_record(odr_encode(), VAL_TEXT_XML,
740 (char*) out_buf, out_len);
749 m_stylesheet_offset++;
750 if (m_stylesheet_offset == m_stylesheet_nprl->num_records)
752 m_stylesheet_nprl = 0;
754 if (m_stylesheet_xsp)
755 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
757 m_stylesheet_xsp = 0;
758 timeout(m_client_idletime);
759 send_PDU_convert(m_stylesheet_apdu);
765 void Yaz_Proxy::convert_to_frontend_type(Z_NamePlusRecordList *p)
767 if (m_frontend_type != VAL_NONE)
770 for (i = 0; i < p->num_records; i++)
772 Z_NamePlusRecord *npr = p->records[i];
773 if (npr->which == Z_NamePlusRecord_databaseRecord)
775 Z_External *r = npr->u.databaseRecord;
776 if (r->which == Z_External_octet)
779 if (m_usemarcon_ini_stage1 && *m_usemarcon_ini_stage1)
781 if (!m_usemarcon->m_stage1)
783 m_usemarcon->m_stage1 = new CDetails();
785 m_usemarcon->m_stage1->SetIniFileName(m_usemarcon_ini_stage1);
786 m_usemarcon->m_stage1->SetMarcRecord((char*) r->u.octet_aligned->buf, r->u.octet_aligned->len);
787 int res = m_usemarcon->m_stage1->Start();
792 m_usemarcon->m_stage1->GetMarcRecord(converted, convlen);
793 if (m_usemarcon_ini_stage2 && *m_usemarcon_ini_stage2)
795 if (!m_usemarcon->m_stage2)
797 m_usemarcon->m_stage2 = new CDetails();
799 m_usemarcon->m_stage2->SetIniFileName(m_usemarcon_ini_stage2);
800 m_usemarcon->m_stage2->SetMarcRecord(converted, convlen);
801 res = m_usemarcon->m_stage2->Start();
805 m_usemarcon->m_stage2->GetMarcRecord(converted, convlen);
809 yaz_log(YLOG_LOG, "%sUSEMARCON stage 2 error %d", m_session_str, res);
812 npr->u.databaseRecord =
813 z_ext_record(odr_encode(),
821 yaz_log(YLOG_LOG, "%sUSEMARCON stage 1 error %d", m_session_str, res);
827 npr->u.databaseRecord =
828 z_ext_record(odr_encode(),
830 (char*) r->u.octet_aligned->buf,
831 r->u.octet_aligned->len);
838 void Yaz_Proxy::convert_records_charset(Z_NamePlusRecordList *p,
839 const char *backend_charset)
841 yaz_log(YLOG_LOG, "%sconvert_to_marc", m_session_str);
842 int sel = m_charset_converter->get_client_charset_selected();
843 const char *client_record_charset =
844 m_charset_converter->get_client_query_charset();
845 if (sel && backend_charset && client_record_charset &&
846 strcmp(backend_charset, client_record_charset))
849 yaz_iconv_t cd = yaz_iconv_open(client_record_charset,
851 yaz_marc_t mt = yaz_marc_create();
852 yaz_marc_xml(mt, YAZ_MARC_ISO2709);
853 yaz_marc_iconv(mt, cd);
854 for (i = 0; i < p->num_records; i++)
856 Z_NamePlusRecord *npr = p->records[i];
857 if (npr->which == Z_NamePlusRecord_databaseRecord)
859 Z_External *r = npr->u.databaseRecord;
860 oident *ent = oid_getentbyoid(r->direct_reference);
861 if (!ent || ent->value == VAL_NONE)
864 if (ent->value == VAL_SUTRS)
866 WRBUF w = wrbuf_alloc();
868 wrbuf_iconv_write(w, cd, (char*) r->u.octet_aligned->buf,
869 r->u.octet_aligned->len);
870 npr->u.databaseRecord =
871 z_ext_record(odr_encode(), ent->value, wrbuf_buf(w),
875 else if (ent->value == VAL_TEXT_XML)
879 else if (r->which == Z_External_octet)
883 if (yaz_marc_decode_buf(mt,
884 (char*) r->u.octet_aligned->buf,
885 r->u.octet_aligned->len,
888 npr->u.databaseRecord =
889 z_ext_record(odr_encode(), ent->value, result, rlen);
890 yaz_log(YLOG_LOG, "%sRecoding MARC record",
898 yaz_marc_destroy(mt);
902 yaz_log(YLOG_LOG, "%sSkipping marc convert", m_session_str);
906 void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p,
907 const char *backend_charset)
910 if (!backend_charset)
911 backend_charset = "MARC-8";
912 yaz_iconv_t cd = yaz_iconv_open("UTF-8", backend_charset);
913 yaz_marc_t mt = yaz_marc_create();
914 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
915 yaz_marc_iconv(mt, cd);
916 for (i = 0; i < p->num_records; i++)
918 Z_NamePlusRecord *npr = p->records[i];
919 if (npr->which == Z_NamePlusRecord_databaseRecord)
921 Z_External *r = npr->u.databaseRecord;
922 if (r->which == Z_External_OPAC)
924 WRBUF w = wrbuf_alloc();
926 yaz_display_OPAC(w, r->u.opac, 0);
927 npr->u.databaseRecord = z_ext_record(
928 odr_encode(), VAL_TEXT_XML,
929 wrbuf_buf(w), wrbuf_len(w)
933 else if (r->which == Z_External_octet)
937 if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
938 r->u.octet_aligned->len,
941 npr->u.databaseRecord =
942 z_ext_record(odr_encode(), VAL_TEXT_XML, result, rlen);
949 yaz_marc_destroy(mt);
952 void Yaz_Proxy::logtime()
954 #if HAVE_GETTIMEOFDAY
955 struct timeval *tv = (struct timeval*) m_time_tv;
959 gettimeofday(&tv1, 0);
960 long diff = (tv1.tv_sec - tv->tv_sec)*1000000 +
961 (tv1.tv_usec - tv->tv_usec);
963 yaz_log(YLOG_LOG, "%sElapsed %ld.%03ld", m_session_str,
964 diff/1000000, (diff/1000)%1000);
971 int Yaz_Proxy::send_http_response(int code)
973 ODR o = odr_encode();
974 Z_GDU *gdu = z_get_HTTP_Response(o, code);
975 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
977 hres->version = odr_strdup(o, m_http_version);
978 if (m_http_keepalive)
979 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
983 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
985 yaz_log (YLOG_LOG, "%sSending %s to client", m_session_str,
989 int r = send_GDU(gdu, &len);
991 m_bw_stat.add_bytes(len);
996 int Yaz_Proxy::send_srw_response(Z_SRW_PDU *srw_pdu)
998 ODR o = odr_encode();
999 const char *ctype = "text/xml";
1000 Z_GDU *gdu = z_get_HTTP_Response(o, 200);
1001 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
1003 hres->version = odr_strdup(o, m_http_version);
1004 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
1005 if (m_http_keepalive)
1006 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
1010 static Z_SOAP_Handler soap_handlers[2] = {
1012 {"http://www.loc.gov/zing/srw/", 0,
1013 (Z_SOAP_fun) yaz_srw_codec},
1018 Z_SOAP *soap_package = (Z_SOAP*) odr_malloc(o, sizeof(Z_SOAP));
1019 soap_package->which = Z_SOAP_generic;
1020 soap_package->u.generic =
1021 (Z_SOAP_Generic *) odr_malloc(o, sizeof(*soap_package->u.generic));
1022 soap_package->u.generic->no = 0;
1023 soap_package->u.generic->ns = soap_handlers[0].ns;
1024 soap_package->u.generic->p = (void *) srw_pdu;
1025 soap_package->ns = m_soap_ns;
1026 z_soap_codec_enc_xsl(o, &soap_package,
1027 &hres->content_buf, &hres->content_len,
1028 soap_handlers, 0, m_s2z_stylesheet);
1029 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
1031 yaz_log (YLOG_LOG, "%sSending %s to client", m_session_str,
1035 int r = send_GDU(gdu, &len);
1036 m_bytes_sent += len;
1037 m_bw_stat.add_bytes(len);
1042 int Yaz_Proxy::send_to_srw_client_error(int srw_error, const char *add)
1044 ODR o = odr_encode();
1045 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
1046 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1048 srw_res->num_diagnostics = 1;
1049 srw_res->diagnostics = (Z_SRW_diagnostic *)
1050 odr_malloc(o, sizeof(*srw_res->diagnostics));
1051 yaz_mk_std_diagnostic(o, srw_res->diagnostics, srw_error, add);
1052 return send_srw_response(srw_pdu);
1055 int Yaz_Proxy::z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
1056 Z_DefaultDiagFormat *ddf)
1058 int bib1_code = *ddf->condition;
1059 if (bib1_code == 109)
1061 srw_res->num_diagnostics = 1;
1062 srw_res->diagnostics = (Z_SRW_diagnostic *)
1063 odr_malloc(o, sizeof(*srw_res->diagnostics));
1064 yaz_mk_std_diagnostic(o, srw_res->diagnostics,
1065 yaz_diag_bib1_to_srw(*ddf->condition),
1070 int Yaz_Proxy::send_to_srw_client_ok(int hits, Z_Records *records, int start)
1072 ODR o = odr_encode();
1073 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
1074 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1076 srw_res->numberOfRecords = odr_intdup (o, hits);
1077 if (records && records->which == Z_Records_DBOSD)
1079 srw_res->num_records =
1080 records->u.databaseOrSurDiagnostics->num_records;
1082 srw_res->records = (Z_SRW_record *)
1083 odr_malloc(o, srw_res->num_records * sizeof(Z_SRW_record));
1084 for (i = 0; i < srw_res->num_records; i++)
1086 Z_NamePlusRecord *npr = records->u.databaseOrSurDiagnostics->records[i];
1087 if (npr->which != Z_NamePlusRecord_databaseRecord)
1089 srw_res->records[i].recordSchema = "diagnostic";
1090 srw_res->records[i].recordPacking = m_s2z_packing;
1091 srw_res->records[i].recordData_buf = "67";
1092 srw_res->records[i].recordData_len = 2;
1093 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
1096 Z_External *r = npr->u.databaseRecord;
1097 oident *ent = oid_getentbyoid(r->direct_reference);
1098 if (r->which == Z_External_octet && ent->value == VAL_TEXT_XML)
1100 srw_res->records[i].recordSchema = m_schema;
1101 srw_res->records[i].recordPacking = m_s2z_packing;
1102 srw_res->records[i].recordData_buf = (char*)
1103 r->u.octet_aligned->buf;
1104 srw_res->records[i].recordData_len = r->u.octet_aligned->len;
1105 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
1109 srw_res->records[i].recordSchema = "diagnostic";
1110 srw_res->records[i].recordPacking = m_s2z_packing;
1111 srw_res->records[i].recordData_buf = "67";
1112 srw_res->records[i].recordData_len = 2;
1113 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
1117 if (records && records->which == Z_Records_NSD)
1120 http_code = z_to_srw_diag(odr_encode(), srw_res,
1121 records->u.nonSurrogateDiagnostic);
1123 return send_http_response(http_code);
1125 return send_srw_response(srw_pdu);
1129 int Yaz_Proxy::send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
1130 int num_diagnostics)
1132 Yaz_ProxyConfig *cfg = check_reconfigure();
1136 char *b = cfg->get_explain_doc(odr_encode(), 0 /* target */,
1137 m_s2z_database, &len);
1140 Z_SRW_PDU *res = yaz_srw_get(odr_encode(), Z_SRW_explain_response);
1141 Z_SRW_explainResponse *er = res->u.explain_response;
1143 er->record.recordData_buf = b;
1144 er->record.recordData_len = len;
1145 er->record.recordPacking = m_s2z_packing;
1146 er->record.recordSchema = "http://explain.z3950.org/dtd/2.0/";
1148 er->diagnostics = diagnostics;
1149 er->num_diagnostics = num_diagnostics;
1150 return send_srw_response(res);
1153 return send_http_response(404);
1156 int Yaz_Proxy::send_PDU_convert(Z_APDU *apdu)
1160 if (apdu->which == Z_APDU_initResponse)
1162 Z_InitResponse *res = apdu->u.initResponse;
1163 if (*res->result == 0)
1165 send_to_srw_client_error(3, 0);
1167 else if (!m_s2z_search_apdu)
1169 send_srw_explain_response(0, 0);
1173 handle_incoming_Z_PDU(m_s2z_search_apdu);
1176 else if (m_s2z_search_apdu && apdu->which == Z_APDU_searchResponse)
1178 m_s2z_search_apdu = 0;
1179 Z_SearchResponse *res = apdu->u.searchResponse;
1180 m_s2z_hit_count = *res->resultCount;
1181 if (res->records && res->records->which == Z_Records_NSD)
1183 send_to_srw_client_ok(0, res->records, 1);
1185 else if (m_s2z_present_apdu && m_s2z_hit_count > 0)
1188 Z_PresentRequest *pr = m_s2z_present_apdu->u.presentRequest;
1190 if (*pr->resultSetStartPoint <= m_s2z_hit_count)
1192 if (*pr->numberOfRecordsRequested+ *pr->resultSetStartPoint
1194 *pr->numberOfRecordsRequested =
1195 1 + m_s2z_hit_count - *pr->resultSetStartPoint;
1197 handle_incoming_Z_PDU(m_s2z_present_apdu);
1201 m_s2z_present_apdu = 0;
1202 send_to_srw_client_ok(m_s2z_hit_count, res->records, 1);
1205 else if (m_s2z_present_apdu && apdu->which == Z_APDU_presentResponse)
1208 *m_s2z_present_apdu->u.presentRequest->resultSetStartPoint;
1210 m_s2z_present_apdu = 0;
1211 Z_PresentResponse *res = apdu->u.presentResponse;
1212 send_to_srw_client_ok(m_s2z_hit_count, res->records, start);
1218 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
1219 yaz_log (YLOG_LOG, "%sSending %s to client", m_session_str,
1221 int r = send_Z_PDU(apdu, &len);
1222 m_bytes_sent += len;
1223 m_bw_stat.add_bytes(len);
1230 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
1232 int kill_session = 0;
1233 Z_ReferenceId **new_id = get_referenceIdP(apdu);
1236 *new_id = m_referenceId;
1238 if (apdu->which == Z_APDU_searchResponse)
1240 Z_SearchResponse *sr = apdu->u.searchResponse;
1241 Z_Records *p = sr->records;
1242 if (p && p->which == Z_Records_NSD)
1244 Z_DiagRec dr, *dr_p = &dr;
1245 dr.which = Z_DiagRec_defaultFormat;
1246 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1248 *sr->searchStatus = 0;
1249 display_diagrecs(&dr_p, 1);
1253 if (p && p->which == Z_Records_DBOSD)
1257 || m_usemarcon_ini_stage1 || m_usemarcon_ini_stage2
1260 convert_to_frontend_type(p->u.databaseOrSurDiagnostics);
1261 if (m_marcxml_mode == marcxml)
1262 convert_to_marcxml(p->u.databaseOrSurDiagnostics,
1265 convert_records_charset(p->u.databaseOrSurDiagnostics,
1267 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1271 if (sr->resultCount)
1273 yaz_log(YLOG_LOG, "%s%d hits", m_session_str,
1275 if (*sr->resultCount < 0)
1277 m_invalid_session = 1;
1280 *sr->searchStatus = 0;
1282 create_nonSurrogateDiagnostics(odr_encode(), 2, 0);
1283 *sr->resultCount = 0;
1288 else if (apdu->which == Z_APDU_presentResponse)
1290 Z_PresentResponse *sr = apdu->u.presentResponse;
1291 Z_Records *p = sr->records;
1292 if (p && p->which == Z_Records_NSD)
1294 Z_DiagRec dr, *dr_p = &dr;
1295 dr.which = Z_DiagRec_defaultFormat;
1296 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1297 if (*sr->presentStatus == Z_PresentStatus_success)
1298 *sr->presentStatus = Z_PresentStatus_failure;
1299 display_diagrecs(&dr_p, 1);
1301 if (p && p->which == Z_Records_DBOSD)
1305 || m_usemarcon_ini_stage1 || m_usemarcon_ini_stage2
1308 convert_to_frontend_type(p->u.databaseOrSurDiagnostics);
1309 if (m_marcxml_mode == marcxml)
1310 convert_to_marcxml(p->u.databaseOrSurDiagnostics,
1313 convert_records_charset(p->u.databaseOrSurDiagnostics,
1315 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1319 else if (apdu->which == Z_APDU_initResponse)
1321 //Get and check negotiation record
1322 //from init response.
1323 handle_charset_lang_negotiation(apdu);
1325 if (m_initRequest_options)
1328 (Odr_bitmask *)odr_malloc(odr_encode(),
1329 sizeof(Odr_bitmask));
1330 ODR_MASK_ZERO(nopt);
1333 for (i = 0; i<24; i++)
1334 if (ODR_MASK_GET(m_initRequest_options, i) &&
1335 ODR_MASK_GET(apdu->u.initResponse->options, i))
1336 ODR_MASK_SET(nopt, i);
1337 apdu->u.initResponse->options = nopt;
1339 if (m_initRequest_version)
1341 Z_ProtocolVersion *nopt =
1342 (Odr_bitmask *)odr_malloc(odr_encode(),
1343 sizeof(Odr_bitmask));
1344 ODR_MASK_ZERO(nopt);
1347 for (i = 0; i<8; i++)
1348 if (ODR_MASK_GET(m_initRequest_version, i) &&
1349 ODR_MASK_GET(apdu->u.initResponse->protocolVersion, i))
1350 ODR_MASK_SET(nopt, i);
1351 apdu->u.initResponse->protocolVersion = nopt;
1353 apdu->u.initResponse->preferredMessageSize =
1354 odr_intdup(odr_encode(),
1355 m_client->m_initResponse_preferredMessageSize >
1356 m_initRequest_preferredMessageSize ?
1357 m_initRequest_preferredMessageSize :
1358 m_client->m_initResponse_preferredMessageSize);
1359 apdu->u.initResponse->maximumRecordSize =
1360 odr_intdup(odr_encode(),
1361 m_client->m_initResponse_maximumRecordSize >
1362 m_initRequest_maximumRecordSize ?
1363 m_initRequest_maximumRecordSize :
1364 m_client->m_initResponse_maximumRecordSize);
1367 int r = send_PDU_convert(apdu);
1374 m_parent->pre_init();
1379 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
1382 const char *apdu_name_tmp = apdu_name(apdu);
1383 int r = send_Z_PDU(apdu, &len);
1384 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
1385 yaz_log (YLOG_LOG, "%sSending %s to %s %d bytes",
1387 apdu_name_tmp, get_hostname(), len);
1388 m_bytes_sent += len;
1392 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
1394 if (apdu->which == Z_APDU_presentRequest)
1396 Z_PresentRequest *pr = apdu->u.presentRequest;
1397 int toget = *pr->numberOfRecordsRequested;
1398 int start = *pr->resultSetStartPoint;
1400 yaz_log(YLOG_LOG, "%sPresent %s %d+%d", m_session_str,
1401 pr->resultSetId, start, toget);
1403 if (*m_parent->m_optimize == '0')
1406 if (!m_client->m_last_resultSetId)
1408 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1409 new_apdu->u.presentResponse->records =
1410 create_nonSurrogateDiagnostics(odr_encode(), 30,
1412 send_to_client(new_apdu);
1415 if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
1417 if (start+toget-1 > m_client->m_last_resultCount)
1419 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1420 new_apdu->u.presentResponse->records =
1421 create_nonSurrogateDiagnostics(odr_encode(), 13, 0);
1422 send_to_client(new_apdu);
1425 Z_NamePlusRecordList *npr;
1427 yaz_log(YLOG_LOG, "%sCache lookup %d+%d syntax=%s",
1428 m_session_str, start, toget, yaz_z3950oid_to_str(
1429 pr->preferredRecordSyntax, &oclass));
1431 if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
1432 pr->preferredRecordSyntax,
1433 pr->recordComposition))
1435 yaz_log (YLOG_LOG, "%sReturned cached records for present request",
1437 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1438 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1440 new_apdu->u.presentResponse->numberOfRecordsReturned
1441 = odr_intdup(odr_encode(), toget);
1443 new_apdu->u.presentResponse->records = (Z_Records*)
1444 odr_malloc(odr_encode(), sizeof(Z_Records));
1445 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
1446 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
1447 new_apdu->u.presentResponse->nextResultSetPosition =
1448 odr_intdup(odr_encode(), start+toget);
1450 send_to_client(new_apdu);
1456 if (apdu->which != Z_APDU_searchRequest)
1458 Z_SearchRequest *sr = apdu->u.searchRequest;
1459 Yaz_Z_Query *this_query = new Yaz_Z_Query;
1460 Yaz_Z_Databases this_databases;
1462 this_databases.set(sr->num_databaseNames, (const char **)
1465 this_query->set_Z_Query(sr->query);
1467 char query_str[120];
1468 this_query->print(query_str, sizeof(query_str)-1);
1469 yaz_log(YLOG_LOG, "%sSearch %s", m_session_str, query_str);
1471 if (*m_parent->m_optimize != '0' &&
1472 m_client->m_last_ok && m_client->m_last_query &&
1473 m_client->m_last_query->match(this_query) &&
1474 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
1475 m_client->m_last_databases.match(this_databases))
1478 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
1479 m_client->m_last_resultCount < *sr->largeSetLowerBound)
1481 Z_NamePlusRecordList *npr;
1482 int toget = *sr->mediumSetPresentNumber;
1483 Z_RecordComposition *comp = 0;
1485 if (toget > m_client->m_last_resultCount)
1486 toget = m_client->m_last_resultCount;
1488 if (sr->mediumSetElementSetNames)
1490 comp = (Z_RecordComposition *)
1491 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1492 comp->which = Z_RecordComp_simple;
1493 comp->u.simple = sr->mediumSetElementSetNames;
1496 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1497 sr->preferredRecordSyntax, comp))
1499 yaz_log (YLOG_LOG, "%sReturned cached records for medium set",
1501 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1502 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1503 new_apdu->u.searchResponse->resultCount =
1504 &m_client->m_last_resultCount;
1506 new_apdu->u.searchResponse->numberOfRecordsReturned
1507 = odr_intdup(odr_encode(), toget);
1509 new_apdu->u.searchResponse->presentStatus =
1510 odr_intdup(odr_encode(), Z_PresentStatus_success);
1511 new_apdu->u.searchResponse->records = (Z_Records*)
1512 odr_malloc(odr_encode(), sizeof(Z_Records));
1513 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1514 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1515 new_apdu->u.searchResponse->nextResultSetPosition =
1516 odr_intdup(odr_encode(), toget+1);
1517 send_to_client(new_apdu);
1523 // send present request (medium size)
1524 yaz_log (YLOG_LOG, "%sOptimizing search for medium set",
1527 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1528 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1529 pr->referenceId = sr->referenceId;
1530 pr->resultSetId = sr->resultSetName;
1531 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1532 *pr->numberOfRecordsRequested = toget;
1533 pr->recordComposition = comp;
1534 m_client->m_sr_transform = 1;
1538 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
1539 m_client->m_last_resultCount <= 0)
1541 // large set. Return pseudo-search response immediately
1542 yaz_log (YLOG_LOG, "%sOptimizing search for large set",
1544 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1545 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1546 new_apdu->u.searchResponse->resultCount =
1547 &m_client->m_last_resultCount;
1548 send_to_client(new_apdu);
1553 Z_NamePlusRecordList *npr;
1554 int toget = m_client->m_last_resultCount;
1555 Z_RecordComposition *comp = 0;
1557 // send a present request (small set)
1559 if (sr->smallSetElementSetNames)
1561 comp = (Z_RecordComposition *)
1562 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1563 comp->which = Z_RecordComp_simple;
1564 comp->u.simple = sr->smallSetElementSetNames;
1567 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1568 sr->preferredRecordSyntax, comp))
1570 yaz_log (YLOG_LOG, "%sReturned cached records for small set",
1572 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1573 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1574 new_apdu->u.searchResponse->resultCount =
1575 &m_client->m_last_resultCount;
1577 new_apdu->u.searchResponse->numberOfRecordsReturned
1578 = odr_intdup(odr_encode(), toget);
1580 new_apdu->u.searchResponse->presentStatus =
1581 odr_intdup(odr_encode(), Z_PresentStatus_success);
1582 new_apdu->u.searchResponse->records = (Z_Records*)
1583 odr_malloc(odr_encode(), sizeof(Z_Records));
1584 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1585 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1586 new_apdu->u.searchResponse->nextResultSetPosition =
1587 odr_intdup(odr_encode(), toget+1);
1588 send_to_client(new_apdu);
1593 yaz_log (YLOG_LOG, "%sOptimizing search for small set",
1595 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1596 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1597 pr->referenceId = sr->referenceId;
1598 pr->resultSetId = sr->resultSetName;
1599 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1600 *pr->numberOfRecordsRequested = toget;
1601 pr->recordComposition = comp;
1602 m_client->m_sr_transform = 1;
1607 else // query doesn't match
1609 delete m_client->m_last_query;
1610 m_client->m_last_query = this_query;
1611 m_client->m_last_ok = 0;
1612 m_client->m_cache.clear();
1613 m_client->m_resultSetStartPoint = 0;
1615 xfree (m_client->m_last_resultSetId);
1616 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
1618 m_client->m_last_databases.set(sr->num_databaseNames,
1619 (const char **) sr->databaseNames);
1625 void Yaz_Proxy::inc_request_no()
1627 char *cp = strchr(m_session_str, ' ');
1630 sprintf(cp+1, "%d ", m_request_no);
1633 void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
1637 m_bytes_recv += len;
1639 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
1640 yaz_log (YLOG_LOG, "%sReceiving %s from client %d bytes",
1641 m_session_str, gdu_name(apdu), len);
1643 if (m_bw_hold_PDU) // double incoming PDU. shutdown now.
1646 m_bw_stat.add_bytes(len);
1647 m_pdu_stat.add_bytes(1);
1649 #if HAVE_GETTIMEOFDAY
1650 gettimeofday((struct timeval *) m_time_tv, 0);
1653 int bw_total = m_bw_stat.get_total();
1654 int pdu_total = m_pdu_stat.get_total();
1659 if (bw_total > m_bw_max)
1661 reduce = (bw_total/m_bw_max);
1666 if (pdu_total > m_pdu_max)
1668 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
1669 reduce = (reduce > nreduce) ? reduce : nreduce;
1675 yaz_log(YLOG_LOG, "%sdelay=%d bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
1676 m_session_str, reduce, bw_total, pdu_total,
1677 m_bw_max, m_pdu_max);
1679 m_bw_hold_PDU = apdu; // save PDU and signal "on hold"
1680 timeout(reduce); // call us reduce seconds later
1682 else if (apdu->which == Z_GDU_Z3950)
1683 handle_incoming_Z_PDU(apdu->u.z3950);
1684 else if (apdu->which == Z_GDU_HTTP_Request)
1685 handle_incoming_HTTP(apdu->u.HTTP_Request);
1688 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
1690 if (m_max_record_retrieve)
1692 if (apdu->which == Z_APDU_presentRequest)
1694 Z_PresentRequest *pr = apdu->u.presentRequest;
1695 if (pr->numberOfRecordsRequested &&
1696 *pr->numberOfRecordsRequested > m_max_record_retrieve)
1697 *pr->numberOfRecordsRequested = m_max_record_retrieve;
1701 void Yaz_Proxy::handle_charset_lang_negotiation(Z_APDU *apdu)
1703 if (apdu->which == Z_APDU_initRequest)
1705 yaz_log(YLOG_LOG, "%shandle_charset_lang_negotiation",
1707 if (m_initRequest_options &&
1708 !ODR_MASK_GET(m_initRequest_options, Z_Options_negotiationModel) &&
1709 (m_proxy_negotiation_charset || m_proxy_negotiation_lang))
1711 // There is no negotiation proposal from
1712 // client's side. OK. The proxy negotiation
1714 Z_InitRequest *initRequest = apdu->u.initRequest;
1715 Z_OtherInformation **otherInfo;
1716 Z_OtherInformationUnit *oi;
1717 get_otherInfoAPDU(apdu, &otherInfo);
1718 oi = update_otherInformation(otherInfo, 1, NULL, 0, 0);
1721 ODR_MASK_SET(initRequest->options,
1722 Z_Options_negotiationModel);
1723 oi->which = Z_OtherInfo_externallyDefinedInfo;
1724 oi->information.externallyDefinedInfo =
1725 yaz_set_proposal_charneg(odr_encode(),
1726 (const char**)&m_proxy_negotiation_charset,
1727 m_proxy_negotiation_charset ? 1:0,
1728 (const char**)&m_proxy_negotiation_lang,
1729 m_proxy_negotiation_lang ? 1:0,
1733 else if (m_initRequest_options &&
1734 ODR_MASK_GET(m_initRequest_options,
1735 Z_Options_negotiationModel) &&
1736 m_charset_converter->get_target_query_charset())
1738 yaz_log(YLOG_LOG, "%sManaged charset negotiation: charset=%s",
1740 m_charset_converter->get_target_query_charset());
1741 Z_InitRequest *initRequest = apdu->u.initRequest;
1742 Z_CharSetandLanguageNegotiation *negotiation =
1743 yaz_get_charneg_record (initRequest->otherInfo);
1745 negotiation->which == Z_CharSetandLanguageNegotiation_proposal)
1747 NMEM nmem = nmem_create();
1748 char **charsets = 0;
1749 int num_charsets = 0;
1753 yaz_get_proposal_charneg (nmem, negotiation,
1754 &charsets, &num_charsets,
1755 &langs, &num_langs, &selected);
1757 for (i = 0; i<num_charsets; i++)
1758 yaz_log(YLOG_LOG, "%scharset %s", m_session_str,
1760 for (i = 0; i<num_langs; i++)
1761 yaz_log(YLOG_LOG, "%slang %s", m_session_str,
1764 const char *t_charset =
1765 m_charset_converter->get_target_query_charset();
1766 // sweep through charsets and pick the first supported
1768 for (i = 0; i<num_charsets; i++)
1770 const char *c_charset = charsets[i];
1771 if (!odr_set_charset(odr_decode(), t_charset, c_charset))
1774 if (i != num_charsets)
1776 // got one .. set up ODR for reverse direction
1777 const char *c_charset = charsets[i];
1778 odr_set_charset(odr_encode(), c_charset, t_charset);
1779 m_charset_converter->set_client_query_charset(c_charset);
1780 m_charset_converter->set_client_charset_selected(selected);
1783 ODR_MASK_CLEAR(m_initRequest_options,
1784 Z_Options_negotiationModel);
1785 yaz_del_charneg_record(&initRequest->otherInfo);
1789 yaz_log(YLOG_WARN, "%sUnable to decode charset package",
1794 else if (apdu->which == Z_APDU_initResponse)
1796 Z_InitResponse *initResponse = apdu->u.initResponse;
1797 Z_OtherInformation **otherInfo;
1799 if (ODR_MASK_GET(initResponse->options, Z_Options_negotiationModel))
1805 get_otherInfoAPDU(apdu, &otherInfo);
1807 if (!otherInfo && !(*otherInfo))
1810 Z_CharSetandLanguageNegotiation *charneg =
1811 yaz_get_charneg_record(*otherInfo);
1816 yaz_get_response_charneg(m_referenceId_mem, charneg,
1817 &charset, &lang, &selected);
1819 yaz_log(YLOG_LOG, "%sAccepted charset - '%s' and lang - '%s'",
1820 m_session_str, (charset)?charset:"none", (lang)?lang:"none");
1822 if (m_initRequest_options &&
1823 ODR_MASK_GET(m_initRequest_options, Z_Options_negotiationModel))
1825 yaz_log(YLOG_LOG, "%sClient's negotiation record in use",
1828 else if (m_proxy_negotiation_charset || m_proxy_negotiation_lang)
1830 // negotiation-charset, negotiation-lang
1831 // elements of config file in use.
1833 yaz_log(YLOG_LOG, "%sProxy's negotiation record in use",
1836 // clear negotiation option.
1837 ODR_MASK_CLEAR(initResponse->options, Z_Options_negotiationModel);
1839 // Delete negotiation (charneg-3) entry.
1840 yaz_del_charneg_record(otherInfo);
1845 if (m_proxy_negotiation_charset || m_proxy_negotiation_lang)
1847 yaz_log(YLOG_LOG, "%sTarget did not honor negotiation",
1850 else if (m_charset_converter->get_client_query_charset())
1852 Z_OtherInformation **otherInfo;
1853 Z_OtherInformationUnit *oi;
1854 get_otherInfoAPDU(apdu, &otherInfo);
1855 oi = update_otherInformation(otherInfo, 1, NULL, 0, 0);
1858 ODR_MASK_SET(initResponse->options,
1859 Z_Options_negotiationModel);
1860 ODR_MASK_SET(m_initRequest_options,
1861 Z_Options_negotiationModel);
1863 oi->which = Z_OtherInfo_externallyDefinedInfo;
1864 oi->information.externallyDefinedInfo =
1865 yaz_set_response_charneg(
1867 m_charset_converter->get_client_query_charset(),
1869 m_charset_converter->get_client_charset_selected());
1876 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
1878 const char *addinfo)
1880 Z_Records *rec = (Z_Records *)
1881 odr_malloc (odr, sizeof(*rec));
1883 odr_malloc (odr, sizeof(*err));
1884 Z_DiagRec *drec = (Z_DiagRec *)
1885 odr_malloc (odr, sizeof(*drec));
1886 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1887 odr_malloc (odr, sizeof(*dr));
1889 rec->which = Z_Records_NSD;
1890 rec->u.nonSurrogateDiagnostic = dr;
1891 dr->diagnosticSetId =
1892 yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
1893 dr->condition = err;
1894 dr->which = Z_DefaultDiagFormat_v2Addinfo;
1895 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
1899 Z_APDU *Yaz_Proxy::handle_query_transformation(Z_APDU *apdu)
1901 if (apdu->which == Z_APDU_searchRequest &&
1902 apdu->u.searchRequest->query &&
1903 apdu->u.searchRequest->query->which == Z_Query_type_104 &&
1904 apdu->u.searchRequest->query->u.type_104->which == Z_External_CQL)
1906 Z_RPNQuery *rpnquery = 0;
1907 Z_SearchRequest *sr = apdu->u.searchRequest;
1910 yaz_log(YLOG_LOG, "%sCQL: %s", m_session_str,
1911 sr->query->u.type_104->u.cql);
1913 int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
1914 &rpnquery, odr_encode(),
1917 yaz_log(YLOG_LOG, "%sNo CQL to RPN table", m_session_str);
1920 yaz_log(YLOG_LOG, "%sCQL Conversion error %d", m_session_str, r);
1921 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1923 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1924 new_apdu->u.searchResponse->records =
1925 create_nonSurrogateDiagnostics(odr_encode(),
1926 yaz_diag_srw_to_bib1(r),
1928 *new_apdu->u.searchResponse->searchStatus = 0;
1930 send_to_client(new_apdu);
1936 sr->query->which = Z_Query_type_1;
1937 sr->query->u.type_1 = rpnquery;
1944 Z_APDU *Yaz_Proxy::handle_target_charset_conversion(Z_APDU *apdu)
1946 if (apdu->which == Z_APDU_searchRequest &&
1947 apdu->u.searchRequest->query)
1949 if (apdu->u.searchRequest->query->which == Z_Query_type_1
1950 || apdu->u.searchRequest->query->which == Z_Query_type_101)
1953 m_charset_converter->set_client_query_charset("UTF-8");
1954 Z_RPNQuery *rpnquery = apdu->u.searchRequest->query->u.type_1;
1955 m_charset_converter->convert_type_1(rpnquery, odr_encode());
1962 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
1964 if (apdu->which == Z_APDU_searchRequest)
1966 Z_SearchRequest *sr = apdu->u.searchRequest;
1970 Yaz_ProxyConfig *cfg = check_reconfigure();
1972 err = cfg->check_query(odr_encode(), m_default_target,
1973 sr->query, &addinfo);
1976 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1978 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1979 new_apdu->u.searchResponse->records =
1980 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1981 *new_apdu->u.searchResponse->searchStatus = 0;
1983 send_to_client(new_apdu);
1991 int Yaz_Proxy::handle_authentication(Z_APDU *apdu)
1993 if (apdu->which != Z_APDU_initRequest)
1994 return 1; // pass if no init request
1995 Z_InitRequest *req = apdu->u.initRequest;
1997 Yaz_ProxyConfig *cfg = check_reconfigure();
1999 return 1; // pass if no config
2002 if (req->idAuthentication == 0)
2004 ret = cfg->client_authentication(m_default_target, 0, 0, 0);
2006 else if (req->idAuthentication->which == Z_IdAuthentication_idPass)
2008 ret = cfg->client_authentication(m_default_target,
2009 req->idAuthentication->u.idPass->userId,
2010 req->idAuthentication->u.idPass->groupId,
2011 req->idAuthentication->u.idPass->password);
2013 else if (req->idAuthentication->which == Z_IdAuthentication_open)
2015 char user[64], pass[64];
2018 sscanf(req->idAuthentication->u.open, "%63[^/]/%63s", user, pass);
2019 ret = cfg->client_authentication(m_default_target, user, 0, pass);
2022 ret = cfg->client_authentication(m_default_target, 0, 0, 0);
2024 cfg->target_authentication(m_default_target, odr_encode(), req);
2029 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
2031 m_marcxml_mode = none;
2032 if (apdu->which == Z_APDU_searchRequest)
2034 Z_SearchRequest *sr = apdu->u.searchRequest;
2037 Yaz_ProxyConfig *cfg = check_reconfigure();
2039 Z_RecordComposition rc_temp, *rc = 0;
2040 if (sr->smallSetElementSetNames)
2042 rc_temp.which = Z_RecordComp_simple;
2043 rc_temp.u.simple = sr->smallSetElementSetNames;
2047 if (sr->preferredRecordSyntax)
2050 ent = oid_getentbyoid(sr->preferredRecordSyntax);
2051 m_frontend_type = ent->value;
2054 m_frontend_type = VAL_NONE;
2056 char *stylesheet_name = 0;
2058 err = cfg->check_syntax(odr_encode(),
2060 sr->preferredRecordSyntax, rc,
2061 &addinfo, &stylesheet_name, &m_schema,
2062 &m_backend_type, &m_backend_charset,
2063 &m_usemarcon_ini_stage1,
2064 &m_usemarcon_ini_stage2);
2065 if (stylesheet_name)
2067 m_parent->low_socket_close();
2070 if (m_stylesheet_xsp)
2071 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
2072 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
2075 m_stylesheet_offset = 0;
2076 xfree(stylesheet_name);
2078 m_parent->low_socket_open();
2082 sr->smallSetElementSetNames = 0;
2083 sr->mediumSetElementSetNames = 0;
2084 m_marcxml_mode = marcxml;
2088 sr->preferredRecordSyntax =
2089 yaz_str_to_z3950oid(odr_encode(), CLASS_RECSYN,
2093 sr->preferredRecordSyntax =
2094 yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN,
2099 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2101 new_apdu->u.searchResponse->referenceId = sr->referenceId;
2102 new_apdu->u.searchResponse->records =
2103 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
2104 *new_apdu->u.searchResponse->searchStatus = 0;
2106 send_to_client(new_apdu);
2110 else if (m_backend_type)
2112 sr->preferredRecordSyntax =
2113 yaz_str_to_z3950oid(odr_encode(), CLASS_RECSYN, m_backend_type);
2116 else if (apdu->which == Z_APDU_presentRequest)
2118 Z_PresentRequest *pr = apdu->u.presentRequest;
2121 Yaz_ProxyConfig *cfg = check_reconfigure();
2123 if (pr->preferredRecordSyntax)
2126 ent = oid_getentbyoid(pr->preferredRecordSyntax);
2127 m_frontend_type = ent->value;
2130 m_frontend_type = VAL_NONE;
2132 char *stylesheet_name = 0;
2134 err = cfg->check_syntax(odr_encode(), m_default_target,
2135 pr->preferredRecordSyntax,
2136 pr->recordComposition,
2137 &addinfo, &stylesheet_name, &m_schema,
2138 &m_backend_type, &m_backend_charset,
2139 &m_usemarcon_ini_stage1,
2140 &m_usemarcon_ini_stage2
2142 if (stylesheet_name)
2144 m_parent->low_socket_close();
2147 if (m_stylesheet_xsp)
2148 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
2149 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
2152 m_stylesheet_offset = 0;
2153 xfree(stylesheet_name);
2155 m_parent->low_socket_open();
2159 pr->recordComposition = 0;
2160 m_marcxml_mode = marcxml;
2164 pr->preferredRecordSyntax =
2165 yaz_str_to_z3950oid(odr_encode(), CLASS_RECSYN,
2169 pr->preferredRecordSyntax =
2170 yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN,
2175 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
2177 new_apdu->u.presentResponse->referenceId = pr->referenceId;
2178 new_apdu->u.presentResponse->records =
2179 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
2180 *new_apdu->u.presentResponse->presentStatus =
2181 Z_PresentStatus_failure;
2183 send_to_client(new_apdu);
2187 else if (m_backend_type)
2189 pr->preferredRecordSyntax =
2190 yaz_str_to_z3950oid(odr_encode(), CLASS_RECSYN, m_backend_type);
2196 Z_ElementSetNames *Yaz_Proxy::mk_esn_from_schema(ODR o, const char *schema)
2200 Z_ElementSetNames *esn = (Z_ElementSetNames *)
2201 odr_malloc(o, sizeof(Z_ElementSetNames));
2202 esn->which = Z_ElementSetNames_generic;
2203 esn->u.generic = odr_strdup(o, schema);
2207 void Yaz_Proxy::srw_get_client(const char *db, const char **backend_db)
2210 Yaz_ProxyConfig *cfg = check_reconfigure();
2212 t = cfg->get_explain_name(db, backend_db);
2214 if (m_client && m_default_target && t && strcmp(m_default_target, t))
2221 xfree(m_default_target);
2222 m_default_target = xstrdup(t);
2226 int Yaz_Proxy::file_access(Z_HTTP_Request *hreq)
2229 if (strcmp(hreq->method, "GET"))
2231 if (hreq->path[0] != '/')
2233 yaz_log(YLOG_WARN, "Bad path: %s", hreq->path);
2236 const char *cp = hreq->path;
2239 if (*cp == '/' && strchr("/.", cp[1]))
2241 yaz_log(YLOG_WARN, "Bad path: %s", hreq->path);
2246 const char *fname = hreq->path+1;
2247 if (stat(fname, &sbuf))
2249 yaz_log(YLOG_WARN|YLOG_ERRNO, "%s: stat failed", fname);
2252 if ((sbuf.st_mode & S_IFMT) != S_IFREG)
2254 yaz_log(YLOG_WARN, "%s: not a regular file", fname);
2257 if (sbuf.st_size > (off_t) 1000000)
2259 yaz_log(YLOG_WARN, "%s: too large for transfer", fname);
2263 ODR o = odr_encode();
2264 Yaz_ProxyConfig *cfg = check_reconfigure();
2265 const char *ctype = cfg->check_mime_type(fname);
2266 Z_GDU *gdu = z_get_HTTP_Response(o, 200);
2267 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
2269 hres->version = odr_strdup(o, m_http_version);
2270 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
2271 if (m_http_keepalive)
2272 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
2276 hres->content_len = sbuf.st_size;
2277 hres->content_buf = (char*) odr_malloc(o, hres->content_len);
2278 FILE *f = fopen(fname, "rb");
2281 fread(hres->content_buf, 1, hres->content_len, f);
2288 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
2290 yaz_log (YLOG_LOG, "%sSending file %s to client", m_session_str,
2294 send_GDU(gdu, &len);
2298 void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
2302 odr_destroy(m_s2z_odr_init);
2305 if (m_s2z_odr_search)
2307 odr_destroy(m_s2z_odr_search);
2308 m_s2z_odr_search = 0;
2311 m_http_keepalive = 0;
2313 if (!strcmp(hreq->version, "1.0"))
2315 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
2316 if (v && !strcmp(v, "Keep-Alive"))
2317 m_http_keepalive = 1;
2319 m_http_keepalive = 0;
2320 m_http_version = "1.0";
2324 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
2325 if (v && !strcmp(v, "close"))
2326 m_http_keepalive = 0;
2328 m_http_keepalive = 1;
2329 m_http_version = "1.1";
2332 Z_SRW_PDU *srw_pdu = 0;
2333 Z_SOAP *soap_package = 0;
2335 Z_SRW_diagnostic *diagnostic = 0;
2336 int num_diagnostic = 0;
2338 if (file_access(hreq))
2342 else if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
2344 || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
2345 &charset, &diagnostic, &num_diagnostic) == 0)
2347 m_s2z_odr_init = odr_createmem(ODR_ENCODE);
2348 m_s2z_odr_search = odr_createmem(ODR_ENCODE);
2349 m_soap_ns = odr_strdup(m_s2z_odr_search, soap_package->ns);
2350 m_s2z_init_apdu = 0;
2351 m_s2z_search_apdu = 0;
2352 m_s2z_present_apdu = 0;
2354 m_s2z_stylesheet = 0;
2356 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
2358 Z_SRW_searchRetrieveRequest *srw_req = srw_pdu->u.request;
2360 const char *backend_db = srw_req->database;
2361 srw_get_client(srw_req->database, &backend_db);
2363 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
2364 // recordXPath unsupported.
2365 if (srw_req->recordXPath)
2367 yaz_add_srw_diagnostic(odr_decode(),
2368 &diagnostic, &num_diagnostic,
2372 if (srw_req->sort_type != Z_SRW_sort_type_none)
2374 yaz_add_srw_diagnostic(odr_decode(),
2375 &diagnostic, &num_diagnostic,
2379 if (srw_req->stylesheet)
2381 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
2383 // set packing for response records ..
2384 if (srw_req->recordPacking &&
2385 !strcmp(srw_req->recordPacking, "xml"))
2386 m_s2z_packing = Z_SRW_recordPacking_XML;
2388 m_s2z_packing = Z_SRW_recordPacking_string;
2392 Z_SRW_PDU *srw_pdu =
2393 yaz_srw_get(odr_encode(),
2394 Z_SRW_searchRetrieve_response);
2395 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
2397 srw_res->diagnostics = diagnostic;
2398 srw_res->num_diagnostics = num_diagnostic;
2399 send_srw_response(srw_pdu);
2403 // prepare search PDU
2404 m_s2z_search_apdu = zget_APDU(m_s2z_odr_search,
2405 Z_APDU_searchRequest);
2406 Z_SearchRequest *z_searchRequest =
2407 m_s2z_search_apdu->u.searchRequest;
2409 z_searchRequest->num_databaseNames = 1;
2410 z_searchRequest->databaseNames = (char**)
2411 odr_malloc(m_s2z_odr_search, sizeof(char *));
2412 z_searchRequest->databaseNames[0] = odr_strdup(m_s2z_odr_search,
2415 // query transformation
2416 Z_Query *query = (Z_Query *)
2417 odr_malloc(m_s2z_odr_search, sizeof(Z_Query));
2418 z_searchRequest->query = query;
2420 if (srw_req->query_type == Z_SRW_query_type_cql)
2422 Z_External *ext = (Z_External *)
2423 odr_malloc(m_s2z_odr_search, sizeof(*ext));
2424 ext->direct_reference =
2425 odr_getoidbystr(m_s2z_odr_search, "1.2.840.10003.16.2");
2426 ext->indirect_reference = 0;
2427 ext->descriptor = 0;
2428 ext->which = Z_External_CQL;
2429 ext->u.cql = srw_req->query.cql;
2431 query->which = Z_Query_type_104;
2432 query->u.type_104 = ext;
2434 else if (srw_req->query_type == Z_SRW_query_type_pqf)
2436 Z_RPNQuery *RPNquery;
2437 YAZ_PQF_Parser pqf_parser;
2439 pqf_parser = yaz_pqf_create ();
2441 RPNquery = yaz_pqf_parse (pqf_parser, m_s2z_odr_search,
2442 srw_req->query.pqf);
2445 const char *pqf_msg;
2447 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
2448 yaz_log(YLOG_LOG, "%*s^\n", off+4, "");
2449 yaz_log(YLOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
2451 send_to_srw_client_error(10, 0);
2454 query->which = Z_Query_type_1;
2455 query->u.type_1 = RPNquery;
2457 yaz_pqf_destroy (pqf_parser);
2461 send_to_srw_client_error(7, "query");
2466 m_s2z_present_apdu = 0;
2468 if (srw_req->maximumRecords)
2469 max = *srw_req->maximumRecords;
2471 if (srw_req->startRecord)
2472 start = *srw_req->startRecord;
2475 // Some backend, such as Voyager doesn't honor piggyback
2476 // So we use present always (0 &&).
2477 if (0 && start <= 1) // Z39.50 piggyback
2479 *z_searchRequest->smallSetUpperBound = max;
2480 *z_searchRequest->mediumSetPresentNumber = max;
2481 *z_searchRequest->largeSetLowerBound = 2000000000; // 2e9
2483 z_searchRequest->preferredRecordSyntax =
2484 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
2486 if (srw_req->recordSchema)
2488 z_searchRequest->smallSetElementSetNames =
2489 z_searchRequest->mediumSetElementSetNames =
2490 mk_esn_from_schema(m_s2z_odr_search,
2491 srw_req->recordSchema);
2494 else // Z39.50 present
2496 m_s2z_present_apdu = zget_APDU(m_s2z_odr_search,
2497 Z_APDU_presentRequest);
2498 Z_PresentRequest *z_presentRequest =
2499 m_s2z_present_apdu->u.presentRequest;
2500 *z_presentRequest->resultSetStartPoint = start;
2501 *z_presentRequest->numberOfRecordsRequested = max;
2502 z_presentRequest->preferredRecordSyntax =
2503 yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
2505 if (srw_req->recordSchema)
2507 z_presentRequest->recordComposition =
2508 (Z_RecordComposition *)
2509 odr_malloc(m_s2z_odr_search,
2510 sizeof(Z_RecordComposition));
2511 z_presentRequest->recordComposition->which =
2512 Z_RecordComp_simple;
2513 z_presentRequest->recordComposition->u.simple =
2514 mk_esn_from_schema(m_s2z_odr_search,
2515 srw_req->recordSchema);
2521 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
2522 Z_APDU_initRequest);
2524 // prevent m_initRequest_apdu memory from being grabbed
2525 // in Yaz_Proxy::handle_incoming_Z_PDU
2526 m_initRequest_apdu = m_s2z_init_apdu;
2527 handle_incoming_Z_PDU(m_s2z_init_apdu);
2532 handle_incoming_Z_PDU(m_s2z_search_apdu);
2536 else if (srw_pdu->which == Z_SRW_explain_request)
2538 Z_SRW_explainRequest *srw_req = srw_pdu->u.explain_request;
2540 const char *backend_db = srw_req->database;
2541 srw_get_client(srw_req->database, &backend_db);
2543 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
2546 if (srw_req->stylesheet)
2548 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
2550 if (srw_req->recordPacking &&
2551 !strcmp(srw_req->recordPacking, "xml"))
2552 m_s2z_packing = Z_SRW_recordPacking_XML;
2554 m_s2z_packing = Z_SRW_recordPacking_string;
2558 send_srw_explain_response(diagnostic, num_diagnostic);
2564 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
2565 Z_APDU_initRequest);
2567 // prevent m_initRequest_apdu memory from being grabbed
2568 // in Yaz_Proxy::handle_incoming_Z_PDU
2569 m_initRequest_apdu = m_s2z_init_apdu;
2570 handle_incoming_Z_PDU(m_s2z_init_apdu);
2573 send_srw_explain_response(0, 0);
2576 else if (srw_pdu->which == Z_SRW_scan_request)
2578 m_s2z_database = odr_strdup(m_s2z_odr_init,
2579 srw_pdu->u.scan_request->database);
2581 yaz_add_srw_diagnostic(odr_decode(),
2582 &diagnostic, &num_diagnostic,
2584 Z_SRW_PDU *srw_pdu =
2585 yaz_srw_get(odr_encode(),
2586 Z_SRW_scan_response);
2587 Z_SRW_scanResponse *srw_res = srw_pdu->u.scan_response;
2589 srw_res->diagnostics = diagnostic;
2590 srw_res->num_diagnostics = num_diagnostic;
2591 send_srw_response(srw_pdu);
2598 send_to_srw_client_error(4, 0);
2601 send_http_response(400);
2604 void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
2606 Z_ReferenceId **refid = get_referenceIdP(apdu);
2607 nmem_reset(m_referenceId_mem);
2608 if (refid && *refid)
2610 m_referenceId = (Z_ReferenceId *)
2611 nmem_malloc(m_referenceId_mem, sizeof(*m_referenceId));
2612 m_referenceId->len = m_referenceId->size = (*refid)->len;
2613 m_referenceId->buf = (unsigned char *)
2614 nmem_malloc(m_referenceId_mem, (*refid)->len);
2615 memcpy(m_referenceId->buf, (*refid)->buf, (*refid)->len);
2620 if (!m_client && m_invalid_session)
2622 m_apdu_invalid_session = apdu;
2623 m_mem_invalid_session = odr_extract_mem(odr_decode());
2624 apdu = m_initRequest_apdu;
2627 // Determine our client.
2628 Z_OtherInformation **oi;
2629 get_otherInfoAPDU(apdu, &oi);
2630 m_client = get_client(apdu, get_cookie(oi), get_proxy(oi));
2635 send_http_response(404);
2645 m_client->m_server = this;
2647 if (apdu->which == Z_APDU_initRequest)
2649 if (apdu->u.initRequest->implementationId)
2650 yaz_log(YLOG_LOG, "%simplementationId: %s",
2651 m_session_str, apdu->u.initRequest->implementationId);
2652 if (apdu->u.initRequest->implementationName)
2653 yaz_log(YLOG_LOG, "%simplementationName: %s",
2654 m_session_str, apdu->u.initRequest->implementationName);
2655 if (apdu->u.initRequest->implementationVersion)
2656 yaz_log(YLOG_LOG, "%simplementationVersion: %s",
2657 m_session_str, apdu->u.initRequest->implementationVersion);
2658 if (m_initRequest_apdu == 0)
2660 if (m_initRequest_mem)
2661 nmem_destroy(m_initRequest_mem);
2663 m_initRequest_apdu = apdu;
2664 m_initRequest_mem = odr_extract_mem(odr_decode());
2666 m_initRequest_preferredMessageSize = *apdu->u.initRequest->
2667 preferredMessageSize;
2668 *apdu->u.initRequest->preferredMessageSize = 1024*1024;
2669 m_initRequest_maximumRecordSize = *apdu->u.initRequest->
2671 *apdu->u.initRequest->maximumRecordSize = 1024*1024;
2673 // Save proposal charsets and langs.
2674 if (ODR_MASK_GET(apdu->u.initRequest->options,
2675 Z_Options_negotiationModel))
2677 Z_CharSetandLanguageNegotiation *charSetandLangRecord =
2678 yaz_get_charneg_record(*oi);
2680 yaz_get_proposal_charneg(m_referenceId_mem,
2681 charSetandLangRecord,
2682 &m_initRequest_oi_negotiation_charsets,
2683 &m_initRequest_oi_negotiation_num_charsets,
2684 &m_initRequest_oi_negotiation_langs,
2685 &m_initRequest_oi_negotiation_num_langs,
2686 &m_initRequest_oi_negotiation_selected);
2688 for (int i=0; i<m_initRequest_oi_negotiation_num_charsets; i++)
2690 yaz_log(YLOG_LOG, "%scharacters set proposal: %s",
2691 m_session_str,(m_initRequest_oi_negotiation_charsets[i])?
2692 m_initRequest_oi_negotiation_charsets[i]:"none");
2694 for (int i=0; i<m_initRequest_oi_negotiation_num_langs; i++)
2696 yaz_log(YLOG_LOG, "%slanguages proposal: %s",
2697 m_session_str, (m_initRequest_oi_negotiation_langs[i])?
2698 m_initRequest_oi_negotiation_langs[i]:"none");
2700 yaz_log(YLOG_LOG, "%sselected proposal: %d (boolean)",
2701 m_session_str, m_initRequest_oi_negotiation_selected);
2703 // save init options for the response..
2704 m_initRequest_options = apdu->u.initRequest->options;
2706 apdu->u.initRequest->options =
2707 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2708 sizeof(Odr_bitmask));
2709 ODR_MASK_ZERO(apdu->u.initRequest->options);
2711 for (i = 0; i<= 24; i++)
2712 ODR_MASK_SET(apdu->u.initRequest->options, i);
2713 // check negotiation option
2714 if (!ODR_MASK_GET(m_initRequest_options,
2715 Z_Options_negotiationModel))
2717 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2718 Z_Options_negotiationModel);
2720 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2721 Z_Options_concurrentOperations);
2723 m_initRequest_version = apdu->u.initRequest->protocolVersion;
2724 apdu->u.initRequest->protocolVersion =
2725 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2726 sizeof(Odr_bitmask));
2727 ODR_MASK_ZERO(apdu->u.initRequest->protocolVersion);
2729 for (i = 0; i<= 8; i++)
2730 ODR_MASK_SET(apdu->u.initRequest->protocolVersion, i);
2732 if (m_client->m_init_flag)
2734 if (handle_init_response_for_invalid_session(apdu))
2736 if (m_client->m_initResponse)
2738 Z_APDU *apdu2 = m_client->m_initResponse;
2739 apdu2->u.initResponse->otherInfo = 0;
2740 if (m_client->m_cookie && *m_client->m_cookie)
2741 set_otherInformationString(apdu2, VAL_COOKIE, 1,
2742 m_client->m_cookie);
2743 apdu2->u.initResponse->referenceId =
2744 apdu->u.initRequest->referenceId;
2745 apdu2->u.initResponse->options = m_client->m_initResponse_options;
2746 apdu2->u.initResponse->protocolVersion =
2747 m_client->m_initResponse_version;
2749 send_to_client(apdu2);
2753 m_client->m_init_flag = 1;
2756 if (!handle_authentication(apdu))
2758 Z_APDU *apdu_reject = zget_APDU(odr_encode(), Z_APDU_initResponse);
2759 *apdu_reject->u.initResponse->result = 0;
2760 send_to_client(apdu_reject);
2766 handle_max_record_retrieve(apdu);
2769 apdu = handle_syntax_validation(apdu);
2772 apdu = handle_query_transformation(apdu);
2775 apdu = handle_target_charset_conversion(apdu);
2778 apdu = handle_query_validation(apdu);
2781 apdu = result_set_optimize(apdu);
2785 m_client->timeout(m_target_idletime); // mark it active even
2786 // though we didn't use it
2789 // Add otherInformation entry in APDU if
2790 // negotiatoin in use.
2792 handle_charset_lang_negotiation(apdu);
2794 // delete other info construct completely if 0 elements
2795 get_otherInfoAPDU(apdu, &oi);
2796 if (oi && *oi && (*oi)->num_elements == 0)
2799 if (apdu->which == Z_APDU_presentRequest &&
2800 m_client->m_resultSetStartPoint == 0)
2802 Z_PresentRequest *pr = apdu->u.presentRequest;
2803 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
2804 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
2806 m_client->m_resultSetStartPoint = 0;
2808 if (m_client->send_to_target(apdu) < 0)
2815 m_client->m_waiting = 1;
2818 void Yaz_Proxy::connectNotify()
2822 void Yaz_Proxy::releaseClient()
2824 xfree(m_proxyTarget);
2826 m_invalid_session = 0;
2827 // only keep if keep_alive flag is set...
2829 m_client->m_pdu_recv < m_keepalive_limit_pdu &&
2830 m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
2831 m_client->m_waiting == 0)
2833 yaz_log(YLOG_LOG, "%sShutdown (client to proxy) keepalive %s",
2835 m_client->get_hostname());
2836 yaz_log(YLOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
2837 m_session_str, m_client->m_pdu_recv,
2838 m_client->m_bytes_sent + m_client->m_bytes_recv,
2839 m_keepalive_limit_bw, m_keepalive_limit_pdu);
2840 assert (m_client->m_waiting != 2);
2841 // Tell client (if any) that no server connection is there..
2842 m_client->m_server = 0;
2847 yaz_log (YLOG_LOG, "%sShutdown (client to proxy) close %s",
2849 m_client->get_hostname());
2850 assert (m_client->m_waiting != 2);
2856 yaz_log (YLOG_LOG, "%sshutdown (client to proxy) bad state",
2862 yaz_log (YLOG_LOG, "%sShutdown (client to proxy)",
2866 m_parent->pre_init();
2869 void Yaz_Proxy::shutdown()
2875 const char *Yaz_ProxyClient::get_session_str()
2879 return m_server->get_session_str();
2882 void Yaz_ProxyClient::shutdown()
2884 yaz_log (YLOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
2890 void Yaz_Proxy::failNotify()
2893 yaz_log (YLOG_LOG, "%sConnection closed by client",
2898 void Yaz_ProxyClient::failNotify()
2901 m_server->inc_request_no();
2902 yaz_log (YLOG_LOG, "%sConnection closed by target %s",
2903 get_session_str(), get_hostname());
2907 void Yaz_ProxyClient::connectNotify()
2909 const char *s = get_session_str();
2910 const char *h = get_hostname();
2911 yaz_log (YLOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
2913 timeout(m_target_idletime);
2918 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
2919 *the_PDU_Observable, int fd)
2921 return new Yaz_ProxyClient(the_PDU_Observable, 0);
2924 Yaz_ProxyClient::~Yaz_ProxyClient()
2929 m_next->m_prev = m_prev;
2930 m_waiting = 2; // for debugging purposes only.
2931 odr_destroy(m_init_odr);
2932 delete m_last_query;
2933 xfree (m_last_resultSetId);
2937 void Yaz_ProxyClient::pre_init_client()
2939 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
2940 Z_InitRequest *req = apdu->u.initRequest;
2943 for (i = 0; i<= 24; i++)
2944 ODR_MASK_SET(req->options, i);
2945 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2946 Z_Options_negotiationModel);
2947 ODR_MASK_CLEAR(apdu->u.initRequest->options,
2948 Z_Options_concurrentOperations);
2949 for (i = 0; i<= 10; i++)
2950 ODR_MASK_SET(req->protocolVersion, i);
2952 if (send_to_target(apdu) < 0)
2963 void Yaz_Proxy::pre_init()
2966 const char *name = 0;
2967 const char *zurl_in_use[MAX_ZURL_PLEX];
2968 int limit_bw, limit_pdu, limit_req;
2969 int target_idletime, client_idletime;
2971 int keepalive_limit_bw, keepalive_limit_pdu;
2973 const char *cql2rpn = 0;
2974 const char *authentication = 0;
2975 const char *negotiation_charset = 0;
2976 const char *negotiation_lang = 0;
2978 Yaz_ProxyConfig *cfg = check_reconfigure();
2982 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
2987 for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
2988 &limit_bw, &limit_pdu, &limit_req,
2989 &target_idletime, &client_idletime,
2991 &keepalive_limit_bw,
2992 &keepalive_limit_pdu,
2996 &negotiation_charset,
3003 for (j = 0; zurl_in_use[j]; j++)
3007 int spare_waiting = 0;
3010 for (c = m_clientPool; c; c = c->m_next)
3012 if (!strcmp(zurl_in_use[j], c->get_hostname()))
3014 if (c->m_cookie == 0)
3016 if (c->m_server == 0)
3028 yaz_log(YLOG_LOG, "%spre-init %s %s use=%d other=%d spare=%d "
3029 "sparew=%d preinit=%d",m_session_str,
3030 name, zurl_in_use[j], in_use, other,
3031 spare, spare_waiting, pre_init);
3032 if (spare + spare_waiting < pre_init)
3034 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
3035 c->m_next = m_clientPool;
3037 c->m_next->m_prev = &c->m_next;
3039 c->m_prev = &m_clientPool;
3041 if (m_log_mask & PROXY_LOG_APDU_SERVER)
3042 c->set_APDU_yazlog(1);
3044 c->set_APDU_yazlog(0);
3046 if (c->client(zurl_in_use[j]))
3054 c->m_target_idletime = target_idletime;
3055 c->m_seqno = m_seqno++;
3062 void Yaz_Proxy::timeoutNotify()
3068 timeout(m_client_idletime);
3069 Z_GDU *apdu = m_bw_hold_PDU;
3072 if (apdu->which == Z_GDU_Z3950)
3073 handle_incoming_Z_PDU(apdu->u.z3950);
3074 else if (apdu->which == Z_GDU_HTTP_Request)
3075 handle_incoming_HTTP(apdu->u.HTTP_Request);
3077 else if (m_stylesheet_nprl)
3078 convert_xsl_delay();
3083 yaz_log (YLOG_LOG, "%sTimeout (client to proxy)", m_session_str);
3094 void Yaz_Proxy::markInvalid()
3097 m_invalid_session = 1;
3100 void Yaz_ProxyClient::timeoutNotify()
3103 m_server->inc_request_no();
3105 yaz_log (YLOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
3109 if (m_server && m_init_flag)
3111 // target timed out in a session that was properly initialized
3112 // server object stay alive but we mark it as invalid so it
3113 // gets initialized again
3114 m_server->markInvalid();
3120 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
3121 Yaz_Proxy *parent) :
3122 Yaz_Z_Assoc (the_PDU_Observable)
3129 m_last_resultSetId = 0;
3130 m_last_resultCount = 0;
3134 m_init_odr = odr_createmem (ODR_DECODE);
3136 m_initResponse_options = 0;
3137 m_initResponse_version = 0;
3138 m_initResponse_preferredMessageSize = 0;
3139 m_initResponse_maximumRecordSize = 0;
3140 m_resultSetStartPoint = 0;
3141 m_bytes_sent = m_bytes_recv = 0;
3145 m_target_idletime = 600;
3149 const char *Yaz_Proxy::option(const char *name, const char *value)
3151 if (!strcmp (name, "optimize")) {
3154 m_optimize = xstrdup (value);
3161 void Yaz_ProxyClient::recv_HTTP_response(Z_HTTP_Response *apdu, int len)
3166 void Yaz_ProxyClient::recv_GDU(Z_GDU *apdu, int len)
3168 if (apdu->which == Z_GDU_Z3950)
3169 recv_Z_PDU(apdu->u.z3950, len);
3170 else if (apdu->which == Z_GDU_HTTP_Response)
3171 recv_HTTP_response(apdu->u.HTTP_Response, len);
3176 int Yaz_Proxy::handle_init_response_for_invalid_session(Z_APDU *apdu)
3178 if (!m_invalid_session)
3180 m_invalid_session = 0;
3181 handle_incoming_Z_PDU(m_apdu_invalid_session);
3182 assert (m_mem_invalid_session);
3183 nmem_destroy(m_mem_invalid_session);
3184 m_mem_invalid_session = 0;
3188 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
3190 m_bytes_recv += len;
3194 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
3195 yaz_log (YLOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
3196 apdu_name(apdu), get_hostname(), len);
3197 if (apdu->which == Z_APDU_initResponse)
3199 if (!m_server) // if this is a pre init session , check for more
3201 NMEM nmem = odr_extract_mem (odr_decode());
3202 odr_reset (m_init_odr);
3203 nmem_transfer (m_init_odr->mem, nmem);
3204 m_initResponse = apdu;
3205 m_initResponse_options = apdu->u.initResponse->options;
3206 m_initResponse_version = apdu->u.initResponse->protocolVersion;
3207 m_initResponse_preferredMessageSize =
3208 *apdu->u.initResponse->preferredMessageSize;
3209 m_initResponse_maximumRecordSize =
3210 *apdu->u.initResponse->maximumRecordSize;
3212 Z_InitResponse *ir = apdu->u.initResponse;
3213 char *im0 = ir->implementationName;
3216 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
3223 strcat(im1, "(YAZ Proxy)");
3224 ir->implementationName = im1;
3226 nmem_destroy (nmem);
3228 if (m_server && m_server->handle_init_response_for_invalid_session(apdu))
3231 if (apdu->which == Z_APDU_searchResponse)
3233 Z_SearchResponse *sr = apdu->u.searchResponse;
3234 m_last_resultCount = *sr->resultCount;
3235 int status = *sr->searchStatus;
3236 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
3240 if (sr->records && sr->records->which == Z_Records_DBOSD)
3242 m_cache.add(odr_decode(),
3243 sr->records->u.databaseOrSurDiagnostics, 1,
3248 if (apdu->which == Z_APDU_presentResponse)
3250 Z_PresentResponse *pr = apdu->u.presentResponse;
3254 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
3255 Z_SearchResponse *sr = new_apdu->u.searchResponse;
3256 sr->referenceId = pr->referenceId;
3257 *sr->resultCount = m_last_resultCount;
3258 sr->records = pr->records;
3259 sr->nextResultSetPosition = pr->nextResultSetPosition;
3260 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
3264 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
3266 m_cache.add(odr_decode(),
3267 pr->records->u.databaseOrSurDiagnostics,
3268 m_resultSetStartPoint, -1);
3269 m_resultSetStartPoint = 0;
3273 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
3276 m_server->send_to_client(apdu);
3278 if (apdu->which == Z_APDU_close)
3284 void Yaz_Proxy::low_socket_close()
3289 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
3290 if (m_lo_fd[i] >= 0)
3291 ::close(m_lo_fd[i]);
3295 void Yaz_Proxy::low_socket_open()
3300 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
3301 m_lo_fd[i] = open("/dev/null", O_RDONLY);
3305 int Yaz_Proxy::server(const char *addr)
3307 int r = Yaz_Z_Assoc::server(addr);
3310 yaz_log(YLOG_LOG, "%sStarted proxy "
3314 " on %s", m_session_str, addr);