1 /* This file is part of YAZ proxy
2 Copyright (C) 1998-2011 Index Data
4 YAZ proxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #define HAVE_SYS_STAT_H 1
21 #define HAVE_SYS_TYPES_H 1
31 #include <sys/types.h>
43 #include <yaz/marcdisp.h>
44 #include <yaz/yaz-iconv.h>
46 #include <yaz/diagbib1.h>
48 #include <yaz/pquery.h>
49 #include <yaz/otherinfo.h>
50 #include <yaz/charneg.h>
51 #include <yaz/oid_db.h>
52 #include "msg-thread.h"
54 #if YAZ_VERSIONL >= 0x03001D
55 #define YAZ_HAS_MK_SURROGATE 1
57 #define YAZ_HAS_MK_SURROGATE 0
60 using namespace yazpp_1;
63 #define strncasecmp _strnicmp
66 class YAZ_EXPORT Auth_Msg : public IMsg_Thread {
69 IMsg_Thread *handle();
81 m_nmem = nmem_create();
89 IMsg_Thread *Auth_Msg::handle()
91 ODR decode = odr_createmem(ODR_DECODE);
94 odr_setbuf(decode, m_apdu_buf, m_apdu_len, 0);
95 int r = z_APDU(decode, &apdu, 0, 0);
98 yaz_log(YLOG_WARN, "decode failed in Auth_Msg::handle");
102 m_ret = m_proxy->handle_authentication(apdu);
108 void Auth_Msg::result()
110 if (m_proxy->dec_ref())
112 yaz_log(YLOG_LOG, "Auth_Msg::proxy deleted meanwhile");
116 odr_setbuf(m_proxy->odr_decode(), m_apdu_buf, m_apdu_len, 0);
118 int r = z_APDU(m_proxy->odr_decode(), &apdu, 0, 0);
120 yaz_log(YLOG_LOG, "Auth_Msg::result z_APDU failed");
121 m_proxy->result_authentication(apdu, m_ret);
126 void Yaz_Proxy::result_authentication(Z_APDU *apdu, int ret)
128 if (apdu == 0 || ret == 0)
130 Z_APDU *apdu_reject = zget_APDU(odr_encode(), Z_APDU_initResponse);
131 *apdu_reject->u.initResponse->result = 0;
132 send_to_client(apdu_reject);
137 if (apdu->which == Z_APDU_initRequest)
139 Yaz_ProxyConfig *cfg = check_reconfigure();
141 cfg->target_authentication(m_default_target, odr_encode(),
142 apdu->u.initRequest);
144 handle_incoming_Z_PDU_2(apdu);
148 static const char *apdu_name(Z_APDU *apdu)
152 case Z_APDU_initRequest:
153 return "initRequest";
154 case Z_APDU_initResponse:
155 return "initResponse";
156 case Z_APDU_searchRequest:
157 return "searchRequest";
158 case Z_APDU_searchResponse:
159 return "searchResponse";
160 case Z_APDU_presentRequest:
161 return "presentRequest";
162 case Z_APDU_presentResponse:
163 return "presentResponse";
164 case Z_APDU_deleteResultSetRequest:
165 return "deleteResultSetRequest";
166 case Z_APDU_deleteResultSetResponse:
167 return "deleteResultSetResponse";
168 case Z_APDU_scanRequest:
169 return "scanRequest";
170 case Z_APDU_scanResponse:
171 return "scanResponse";
172 case Z_APDU_sortRequest:
173 return "sortRequest";
174 case Z_APDU_sortResponse:
175 return "sortResponse";
176 case Z_APDU_extendedServicesRequest:
177 return "extendedServicesRequest";
178 case Z_APDU_extendedServicesResponse:
179 return "extendedServicesResponse";
186 static const char *gdu_name(Z_GDU *gdu)
191 return apdu_name(gdu->u.z3950);
192 case Z_GDU_HTTP_Request:
193 return "HTTP Request";
194 case Z_GDU_HTTP_Response:
195 return "HTTP Response";
197 return "Unknown request/response";
200 Yaz_Proxy::Yaz_Proxy(IPDU_Observable *the_PDU_Observable,
201 ISocketObservable *the_socket_observable,
204 Z_Assoc(the_PDU_Observable),
205 m_bw_stat(60), m_pdu_stat(60), m_search_stat(60)
207 m_PDU_Observable = the_PDU_Observable;
208 m_socket_observable = the_socket_observable;
213 m_keepalive_limit_bw = 500000;
214 m_keepalive_limit_pdu = 1000;
216 m_default_target = 0;
217 m_proxy_negotiation_charset = 0;
218 m_proxy_negotiation_lang = 0;
219 m_proxy_negotiation_default_charset = 0;
220 m_charset_converter = new Yaz_CharsetConverter;
224 m_client_idletime = 600;
225 m_target_idletime = 600;
226 m_max_sockets = 1024;
227 m_optimize = xstrdup("1");
228 strcpy(m_session_str, "0 ");
236 m_max_connect_period = 0;
238 m_limit_connect_period = 0;
239 m_timeout_mode = timeout_normal;
241 m_max_record_retrieve = 0;
245 m_flag_invalid_session = 0;
247 m_referenceId_mem = nmem_create();
249 m_marcxml_mode = none;
250 m_stylesheet_xsp = 0;
251 m_stylesheet_nprl = 0;
252 m_stylesheet_apdu = 0;
253 m_s2z_stylesheet = 0;
257 m_backend_charset = 0;
258 m_frontend_type[0] = -1;
259 m_initRequest_apdu = 0;
260 m_initRequest_mem = 0;
261 m_initRequest_preferredMessageSize = 0;
262 m_initRequest_maximumRecordSize = 0;
263 m_initRequest_options = 0;
264 m_initRequest_version = 0;
265 m_initRequest_oi_negotiation_charsets = 0;
266 m_initRequest_oi_negotiation_num_charsets = 0;
267 m_initRequest_oi_negotiation_langs = 0;
268 m_initRequest_oi_negotiation_num_langs = 0;
269 m_initRequest_oi_negotiation_selected = 0;
270 m_apdu_invalid_session = 0;
271 m_mem_invalid_session = 0;
273 m_s2z_odr_search = 0;
276 m_s2z_search_apdu = 0;
277 m_s2z_present_apdu = 0;
279 m_http_keepalive = 0;
282 m_s2z_packing = Z_SRW_recordPacking_string;
283 #if HAVE_GETTIMEOFDAY
284 m_time_tv = xmalloc(sizeof(struct timeval));
285 struct timeval *tv = (struct timeval *) m_time_tv;
291 m_usemarcon_ini_stage1 = 0;
292 m_usemarcon_ini_stage2 = 0;
293 m_usemarcon = new Yaz_usemarcon();
296 m_backend_elementset = 0;
299 m_main_ptr_dec = false;
301 m_num_msg_threads = 0;
304 void Yaz_Proxy::inc_ref()
309 Yaz_Proxy::~Yaz_Proxy()
311 yaz_log(YLOG_LOG, "%sClosed %d/%d sent/recv bytes total", m_session_str,
312 m_bytes_sent, m_bytes_recv);
313 nmem_destroy(m_initRequest_mem);
314 nmem_destroy(m_mem_invalid_session);
315 nmem_destroy(m_referenceId_mem);
317 xfree(m_proxyTarget);
318 xfree(m_default_target);
319 xfree(m_proxy_negotiation_charset);
320 xfree(m_proxy_negotiation_lang);
321 xfree(m_proxy_negotiation_default_charset);
322 delete m_charset_converter;
326 if (m_stylesheet_xsp)
327 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
333 xfree(m_backend_type);
334 xfree(m_backend_charset);
335 xfree(m_usemarcon_ini_stage1);
336 xfree(m_usemarcon_ini_stage2);
337 xfree(m_backend_elementset);
340 odr_destroy(m_s2z_odr_init);
341 if (m_s2z_odr_search)
342 odr_destroy(m_s2z_odr_search);
350 void Yaz_Proxy::set_debug_mode(int mode)
355 int Yaz_Proxy::set_config(const char *config)
358 m_config = new Yaz_ProxyConfig();
359 xfree(m_config_fname);
360 m_config_fname = xstrdup(config);
361 int r = m_config->read_xml(config);
365 m_config->get_generic_info(&m_log_mask, &m_max_clients,
366 &m_max_connect, &m_limit_connect, &period,
368 m_connect.set_period(period);
373 void Yaz_Proxy::set_default_target(const char *target)
375 xfree(m_default_target);
376 m_default_target = 0;
378 m_default_target = (char *) xstrdup(target);
381 void Yaz_Proxy::set_proxy_negotiation(const char *charset, const char *lang,
382 const char *default_charset)
384 yaz_log(YLOG_DEBUG, "%sSet the proxy negotiation: charset to '%s', "
385 "default charset to '%s', language to '%s'", m_session_str,
386 charset?charset:"none",
387 default_charset?default_charset:"none",
389 xfree(m_proxy_negotiation_charset);
390 xfree(m_proxy_negotiation_lang);
391 m_proxy_negotiation_charset = m_proxy_negotiation_lang = 0;
393 m_proxy_negotiation_charset = (char *) xstrdup(charset);
395 m_proxy_negotiation_lang = (char *) xstrdup(lang);
397 m_proxy_negotiation_default_charset =
398 (char *) xstrdup(default_charset);
401 Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
404 return m_parent->check_reconfigure();
406 Yaz_ProxyConfig *cfg = m_config;
409 yaz_log(YLOG_LOG, "reconfigure");
410 if (m_config_fname && cfg)
412 yaz_log(YLOG_LOG, "reconfigure config %s", m_config_fname);
413 int r = cfg->read_xml(m_config_fname);
415 yaz_log(YLOG_WARN, "reconfigure failed");
420 cfg->get_generic_info(&m_log_mask, &m_max_clients,
421 &m_max_connect, &m_limit_connect,
422 &period, &m_num_msg_threads);
423 m_connect.set_period(period);
427 yaz_log(YLOG_LOG, "reconfigure");
433 IPDU_Observer *Yaz_Proxy::sessionNotify(IPDU_Observable
434 *the_PDU_Observable, int fd)
438 char session_str[200];
439 const char *peername = the_PDU_Observable->getpeername();
441 peername = "nullpeer";
443 if (m_log_mask & PROXY_LOG_IP_CLIENT)
444 sprintf(session_str, "%ld:%d %.80s %d ",
445 (long) time(0), m_session_no, peername, 0);
447 sprintf(session_str, "%ld:%d %d ",
448 (long) time(0), m_session_no, 0);
451 yaz_log(YLOG_LOG, "%sNew session %s", session_str, peername);
453 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable,
454 m_socket_observable, this);
456 new_proxy->m_config = 0;
457 new_proxy->m_config_fname = 0;
458 new_proxy->timeout(m_client_idletime);
459 new_proxy->m_target_idletime = m_target_idletime;
460 new_proxy->set_default_target(m_default_target);
461 new_proxy->m_max_clients = m_max_clients;
462 new_proxy->m_log_mask = m_log_mask;
463 new_proxy->m_session_no = m_session_no;
464 new_proxy->m_num_msg_threads = m_num_msg_threads;
467 // in case we want to watch a particular client..
468 if (!strcmp(peername, "tcp:163.121.19.82")) // NIS GROUP
469 new_proxy->m_log_mask = 255;
472 new_proxy->set_APDU_log(get_APDU_log());
473 if (new_proxy->m_log_mask & PROXY_LOG_APDU_CLIENT)
474 new_proxy->set_APDU_yazlog(1);
476 new_proxy->set_APDU_yazlog(0);
477 strcpy(new_proxy->m_session_str, session_str);
478 new_proxy->m_peername = xstrdup(peername);
479 new_proxy->set_proxy_negotiation(m_proxy_negotiation_charset,
480 m_proxy_negotiation_lang, m_proxy_negotiation_default_charset);
481 // create thread object the first time we get an incoming connection
482 if (!m_my_thread && m_num_msg_threads > 0)
484 yaz_log(YLOG_LOG, "%sStarting message thread management. number=%d",
485 session_str, m_num_msg_threads);
486 m_my_thread = new Msg_Thread(m_socket_observable, m_num_msg_threads);
488 new_proxy->m_my_thread = m_my_thread;
492 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
494 Z_OtherInformationUnit *oi =
495 update_otherInformation(otherInfo, 0, yaz_oid_userinfo_cookie, 1, 1);
497 if (oi && oi->which == Z_OtherInfo_characterInfo)
498 return oi->information.characterInfo;
502 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
504 Z_OtherInformationUnit *oi =
505 update_otherInformation(otherInfo, 0, yaz_oid_userinfo_proxy, 1, 1);
507 if (oi && oi->which == Z_OtherInfo_characterInfo)
508 return oi->information.characterInfo;
511 const char *Yaz_Proxy::load_balance(const char **url)
513 int zurl_in_use[MAX_ZURL_PLEX];
514 int zurl_in_spare[MAX_ZURL_PLEX];
518 for (i = 0; i<MAX_ZURL_PLEX; i++)
521 zurl_in_spare[i] = 0;
523 for (c = m_parent->m_clientPool; c; c = c->m_next)
525 for (i = 0; url[i]; i++)
526 if (!strcmp(url[i], c->get_hostname()))
529 if (c->m_cookie == 0 && c->m_server == 0 && c->m_waiting == 0)
533 int min_use = 100000;
534 int spare_for_min = 0;
536 const char *ret_min = 0;
537 const char *ret_spare = 0;
538 for (i = 0; url[i]; i++)
540 yaz_log(YLOG_DEBUG, "%szurl=%s use=%d spare=%d",
541 m_session_str, url[i], zurl_in_use[i], zurl_in_spare[i]);
542 if (min_use > zurl_in_use[i])
545 min_use = zurl_in_use[i];
546 spare_for_min = zurl_in_spare[i];
548 if (max_spare < zurl_in_spare[i])
551 max_spare = zurl_in_spare[i];
557 int Yaz_Proxy::get_number_of_connections()
559 int no_connections = 0;
562 for (c = m_parent->m_clientPool; c; c = c->m_next)
565 assert(*c->m_prev == c);
566 if (!strcmp(m_proxyTarget, c->get_hostname()))
571 yaz_log(YLOG_LOG, "%sExisting %s connections: %d", m_session_str, m_proxyTarget,
573 return no_connections;
576 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
577 const char *proxy_host, int *http_code)
580 Yaz_Proxy *parent = m_parent;
581 Yaz_ProxyClient *c = m_client;
585 const char *url[MAX_ZURL_PLEX];
586 Yaz_ProxyConfig *cfg = check_reconfigure();
589 if (parent && parent->m_debug_mode)
591 // only to be enabled for debugging...
592 if (!strcmp(proxy_host, "stop"))
595 xfree(m_default_target);
596 m_default_target = xstrdup(proxy_host);
598 proxy_host = m_default_target;
599 int client_idletime = -1;
600 const char *cql2rpn_fname = 0;
601 const char *negotiation_charset = 0;
602 const char *negotiation_lang = 0;
603 const char *query_charset = 0;
604 const char *default_client_query_charset = 0;
605 url[0] = m_default_target;
610 cfg->get_target_info(proxy_host, url, &m_bw_max,
611 &m_pdu_max, &m_max_record_retrieve,
613 &m_target_idletime, &client_idletime,
615 &parent->m_max_clients,
616 &m_keepalive_limit_bw,
617 &m_keepalive_limit_pdu,
620 &negotiation_charset,
623 &default_client_query_charset);
625 if (client_idletime != -1)
627 m_client_idletime = client_idletime;
628 timeout(m_client_idletime);
631 // get those FILE descriptors available
632 m_parent->low_socket_close();
634 m_cql2rpn.set_pqf_file(cql2rpn_fname);
635 // reserve them again
636 m_parent->low_socket_open();
638 if (negotiation_charset || negotiation_lang || default_client_query_charset)
640 set_proxy_negotiation(negotiation_charset,
641 negotiation_lang, default_client_query_charset);
643 m_charset_converter->set_target_query_charset(query_charset);
646 yaz_log(YLOG_LOG, "%sNo default target", m_session_str);
649 // we don't handle multiplexing for cookie session, so we just
650 // pick the first one in this case (anonymous users will be able
651 // to use any backend)
652 if (cookie && *cookie)
653 m_proxyTarget = (char*) xstrdup(url[0]);
655 m_proxyTarget = (char*) xstrdup(load_balance(url));
657 if (cookie && *cookie)
658 { // search in sessions with a cookie
659 for (c = parent->m_clientPool; c; c = c->m_next)
662 assert(*c->m_prev == c);
663 if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
664 !strcmp(m_proxyTarget, c->get_hostname()))
667 // The following handles "cancel"
668 // If connection is busy (waiting for PDU) and
669 // we have an initRequest we can safely do re-open
670 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
672 yaz_log(YLOG_LOG, "%s REOPEN target=%s", m_session_str,
679 c->m_last_resultCount = 0;
680 c->m_sr_transform = 0;
682 c->m_resultSetStartPoint = 0;
683 c->m_target_idletime = m_target_idletime;
684 if (c->client(m_proxyTarget))
691 c->m_seqno = parent->m_seqno;
692 if (c->m_server && c->m_server != this)
693 c->m_server->m_client = 0;
696 yaz_log(YLOG_DEBUG, "get_client 1 %p %p", this, c);
701 else if (!c && apdu->which == Z_APDU_initRequest )
703 // anonymous sessions without cookie.
704 // if authentication is set it is NOT anonymous se we can't share them.
705 // If charset and lang negotiation is use it is NOT anonymous session too.
706 for (c = parent->m_clientPool; c; c = c->m_next)
709 assert(*c->m_prev == c);
710 if (c->m_server == 0 && c->m_cookie == 0 && c->m_waiting == 0
711 && c->compare_idAuthentication(apdu)
712 && c->compare_charset(apdu)
713 && !strcmp(m_proxyTarget, c->get_hostname()))
716 yaz_log(YLOG_LOG, "%sREUSE %d %s",
717 m_session_str, parent->m_seqno, c->get_hostname());
719 c->m_seqno = parent->m_seqno;
720 assert(c->m_server == 0);
723 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
724 c->set_APDU_yazlog(1);
726 c->set_APDU_yazlog(0);
738 if (apdu->which != Z_APDU_initRequest)
740 yaz_log(YLOG_LOG, "%sno init request as first PDU", m_session_str);
745 int no_in_use = get_number_of_connections();
746 if (no_in_use >= m_max_sockets)
748 yaz_log(YLOG_LOG, "%smax sockets reached %d", m_session_str,
753 // go through list of clients - and find the lowest/oldest one.
754 Yaz_ProxyClient *c_min = 0;
756 int no_of_clients = 0;
757 if (parent->m_clientPool)
758 yaz_log(YLOG_DEBUG, "Existing sessions");
759 for (c = parent->m_clientPool; c; c = c->m_next)
761 yaz_log(YLOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
762 c->m_waiting, c->get_hostname(),
763 c->m_cookie ? c->m_cookie : "");
765 if (min_seq < 0 || c->m_seqno < min_seq)
767 min_seq = c->m_seqno;
771 if (no_of_clients >= parent->m_max_clients)
774 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
776 yaz_log(YLOG_LOG, "%sMAXCLIENTS %d Destroy %d",
777 m_session_str, parent->m_max_clients, c->m_seqno);
778 if (c->m_server && c->m_server != this)
779 c->m_server->dec_ref();
783 yaz_log(YLOG_LOG, "%sMAXCLIENTS %d Reuse %d %d %s",
784 m_session_str, parent->m_max_clients,
785 c->m_seqno, parent->m_seqno, c->get_hostname());
789 c->m_cookie = xstrdup(cookie);
790 c->m_seqno = parent->m_seqno;
791 if (c->m_server && c->m_server != this)
793 c->m_server->m_client = 0;
794 c->m_server->dec_ref();
797 c->m_target_idletime = m_target_idletime;
798 c->timeout(m_target_idletime);
800 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
801 c->set_APDU_yazlog(1);
803 c->set_APDU_yazlog(0);
811 yaz_log(YLOG_LOG, "%sNEW %d %s",
812 m_session_str, parent->m_seqno, m_proxyTarget);
813 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), parent);
814 c->m_next = parent->m_clientPool;
816 c->m_next->m_prev = &c->m_next;
817 parent->m_clientPool = c;
818 c->m_prev = &parent->m_clientPool;
824 c->m_cookie = xstrdup(cookie);
826 c->m_seqno = parent->m_seqno;
828 c->m_last_resultCount = 0;
831 c->m_sr_transform = 0;
833 c->m_resultSetStartPoint = 0;
835 if (c->client(m_proxyTarget))
840 c->m_target_idletime = m_target_idletime;
843 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
844 c->set_APDU_yazlog(1);
846 c->set_APDU_yazlog(0);
848 c->set_idAuthentication(apdu);
850 yaz_log(YLOG_DEBUG, "get_client 3 %p %p", this, c);
854 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
857 for (i = 0; i<num; i++)
859 Z_DefaultDiagFormat *r;
860 Z_DiagRec *p = pp[i];
861 if (p->which != Z_DiagRec_defaultFormat)
863 yaz_log(YLOG_LOG, "%sError no diagnostics", m_session_str);
867 r = p->u.defaultFormat;
870 case Z_DefaultDiagFormat_v2Addinfo:
871 yaz_log(YLOG_LOG, "%sError " ODR_INT_PRINTF " %s:%s",
873 *r->condition, diagbib1_str(*r->condition),
876 case Z_DefaultDiagFormat_v3Addinfo:
877 yaz_log(YLOG_LOG, "%sError " ODR_INT_PRINTF " %s:%s",
879 *r->condition, diagbib1_str(*r->condition),
886 int Yaz_Proxy::convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu)
888 if (!m_stylesheet_xsp || p->num_records <= 0)
890 return 0; /* no XSLT to be done ... */
893 m_stylesheet_offset = 0;
894 m_stylesheet_nprl = p;
895 m_stylesheet_apdu = apdu;
896 m_timeout_mode = timeout_xsl;
902 void Yaz_Proxy::convert_xsl_delay()
905 Z_NamePlusRecord *npr = m_stylesheet_nprl->records[m_stylesheet_offset];
906 if (npr->which == Z_NamePlusRecord_databaseRecord)
908 Z_External *r = npr->u.databaseRecord;
909 if (r->which == Z_External_octet)
912 fwrite((char*) r->u.octet_aligned->buf, 1, r->u.octet_aligned->len, stdout);
914 xmlDocPtr res, doc = xmlParseMemory(
915 (char*) r->u.octet_aligned->buf,
916 r->u.octet_aligned->len);
919 yaz_log(YLOG_LOG, "%sXSLT convert %d",
920 m_session_str, m_stylesheet_offset);
921 res = xsltApplyStylesheet((xsltStylesheetPtr) m_stylesheet_xsp,
928 xmlDocDumpFormatMemory(res, &out_buf, &out_len, 1);
929 m_stylesheet_nprl->records[m_stylesheet_offset]->
931 z_ext_record_oid(odr_encode(), yaz_oid_recsyn_xml,
932 (char*) out_buf, out_len);
941 m_stylesheet_offset++;
942 if (m_stylesheet_offset == m_stylesheet_nprl->num_records)
944 m_timeout_mode = timeout_normal;
945 m_stylesheet_nprl = 0;
947 if (m_stylesheet_xsp)
948 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
950 m_stylesheet_xsp = 0;
951 timeout(m_client_idletime);
952 send_PDU_convert(m_stylesheet_apdu);
958 void Yaz_Proxy::convert_to_frontend_type(Z_NamePlusRecordList *p)
960 if (m_frontend_type[0] != -1)
963 for (i = 0; i < p->num_records; i++)
965 Z_NamePlusRecord *npr = p->records[i];
966 if (npr->which == Z_NamePlusRecord_databaseRecord)
968 Z_External *r = npr->u.databaseRecord;
969 if (r->which == Z_External_octet)
972 if (m_usemarcon_ini_stage1 && *m_usemarcon_ini_stage1)
973 yaz_log(YLOG_LOG, "%sError: USEMARCON requested but not available",
977 yaz_log(YLOG_DEBUG, "%sUSEMARCON stage1=%s stage2=%s",
979 m_usemarcon_ini_stage1 ? m_usemarcon_ini_stage1 : "(none)",
980 m_usemarcon_ini_stage2 ? m_usemarcon_ini_stage2 : "(none)");
983 if (m_usemarcon->convert(m_usemarcon_ini_stage1, m_usemarcon_ini_stage2,
984 (char*) r->u.octet_aligned->buf, r->u.octet_aligned->len,
985 &converted, &convlen))
987 npr->u.databaseRecord =
988 z_ext_record_oid(odr_encode(),
997 npr->u.databaseRecord =
998 z_ext_record_oid(odr_encode(),
1000 (char*) r->u.octet_aligned->buf,
1001 r->u.octet_aligned->len);
1008 void Yaz_Proxy::convert_records_charset(Z_NamePlusRecordList *p,
1009 const char *backend_charset)
1011 int sel = m_charset_converter->get_client_charset_selected();
1012 const char *client_record_charset =
1013 m_charset_converter->get_client_query_charset();
1014 if (sel && backend_charset && client_record_charset &&
1015 strcmp(backend_charset, client_record_charset))
1018 yaz_iconv_t cd = yaz_iconv_open(client_record_charset,
1020 yaz_marc_t mt = yaz_marc_create();
1021 yaz_marc_xml(mt, YAZ_MARC_ISO2709);
1022 yaz_marc_iconv(mt, cd);
1023 for (i = 0; i < p->num_records; i++)
1025 Z_NamePlusRecord *npr = p->records[i];
1026 if (npr->which == Z_NamePlusRecord_databaseRecord)
1028 Z_External *r = npr->u.databaseRecord;
1029 const Odr_oid *oid = r->direct_reference;
1033 if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
1035 WRBUF w = wrbuf_alloc();
1037 wrbuf_iconv_write(w, cd, (char*) r->u.octet_aligned->buf,
1038 r->u.octet_aligned->len);
1039 npr->u.databaseRecord =
1040 z_ext_record_oid(odr_encode(), oid, wrbuf_buf(w),
1044 else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
1048 else if (r->which == Z_External_octet)
1052 if (yaz_marc_decode_buf(mt,
1053 (char*) r->u.octet_aligned->buf,
1054 r->u.octet_aligned->len,
1057 npr->u.databaseRecord =
1058 z_ext_record_oid(odr_encode(), oid, result, rlen);
1059 yaz_log(YLOG_LOG, "%sRecoding MARC record",
1066 yaz_iconv_close(cd);
1067 yaz_marc_destroy(mt);
1071 void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p,
1072 const char *backend_charset)
1075 if (!backend_charset)
1076 backend_charset = "MARC-8";
1077 yaz_iconv_t cd = yaz_iconv_open("UTF-8", backend_charset);
1078 yaz_marc_t mt = yaz_marc_create();
1079 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
1080 yaz_marc_iconv(mt, cd);
1081 for (i = 0; i < p->num_records; i++)
1083 Z_NamePlusRecord *npr = p->records[i];
1084 if (npr->which == Z_NamePlusRecord_databaseRecord)
1086 Z_External *r = npr->u.databaseRecord;
1087 if (r->which == Z_External_OPAC)
1089 WRBUF w = wrbuf_alloc();
1091 yaz_opac_decode_wrbuf(mt, r->u.opac, w);
1092 npr->u.databaseRecord = z_ext_record_oid(
1093 odr_encode(), yaz_oid_recsyn_xml,
1094 wrbuf_buf(w), wrbuf_len(w));
1097 else if (r->which == Z_External_octet)
1101 if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
1102 r->u.octet_aligned->len,
1105 npr->u.databaseRecord =
1106 z_ext_record_oid(odr_encode(), yaz_oid_recsyn_xml,
1113 yaz_iconv_close(cd);
1114 yaz_marc_destroy(mt);
1117 void Yaz_Proxy::logtime()
1119 #if HAVE_GETTIMEOFDAY
1120 struct timeval *tv = (struct timeval*) m_time_tv;
1124 gettimeofday(&tv1, 0);
1125 long diff = (tv1.tv_sec - tv->tv_sec)*1000000 +
1126 (tv1.tv_usec - tv->tv_usec);
1128 yaz_log(YLOG_LOG, "%sElapsed %ld.%03ld", m_session_str,
1129 diff/1000000, (diff/1000)%1000);
1136 int Yaz_Proxy::send_http_response(int code)
1138 ODR o = odr_encode();
1139 Z_GDU *gdu = z_get_HTTP_Response(o, code);
1140 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
1142 hres->version = odr_strdup(o, m_http_version);
1143 if (m_http_keepalive)
1144 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
1148 z_HTTP_header_add(o, &hres->headers, "WWW-Authenticate",
1149 "Basic realm=\"YAZ Proxy\"");
1152 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
1154 yaz_log (YLOG_LOG, "%sSending %s to client", m_session_str,
1158 int r = send_GDU(gdu, &len);
1159 m_bytes_sent += len;
1160 m_bw_stat.add_bytes(len);
1163 recv_GDU_more(true);
1168 int Yaz_Proxy::send_srw_response(Z_SRW_PDU *srw_pdu, int http_code /* = 200 */)
1170 ODR o = odr_encode();
1171 const char *ctype = "text/xml";
1172 Z_GDU *gdu = z_get_HTTP_Response(o, http_code);
1173 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
1175 hres->version = odr_strdup(o, m_http_version);
1176 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
1177 if (m_http_keepalive)
1178 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
1181 if (http_code == 401)
1182 z_HTTP_header_add(o, &hres->headers, "WWW-Authenticate", "Basic realm=\"YAZ Proxy\"");
1184 static Z_SOAP_Handler soap_handlers[2] = {
1186 { (char*) "http://www.loc.gov/zing/srw/", 0,
1187 (Z_SOAP_fun) yaz_srw_codec},
1192 Z_SOAP *soap_package = (Z_SOAP*) odr_malloc(o, sizeof(Z_SOAP));
1193 soap_package->which = Z_SOAP_generic;
1194 soap_package->u.generic =
1195 (Z_SOAP_Generic *) odr_malloc(o, sizeof(*soap_package->u.generic));
1196 soap_package->u.generic->no = 0;
1197 soap_package->u.generic->ns = soap_handlers[0].ns;
1198 soap_package->u.generic->p = (void *) srw_pdu;
1199 soap_package->ns = m_soap_ns;
1200 z_soap_codec_enc_xsl(o, &soap_package,
1201 &hres->content_buf, &hres->content_len,
1202 soap_handlers, 0, m_s2z_stylesheet);
1203 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
1205 yaz_log(YLOG_LOG, "%sSending %s to client", m_session_str,
1209 int r = send_GDU(gdu, &len);
1210 m_bytes_sent += len;
1211 m_bw_stat.add_bytes(len);
1214 recv_GDU_more(true);
1219 int Yaz_Proxy::send_to_srw_client_error(int srw_error, const char *add)
1221 ODR o = odr_encode();
1222 Z_SRW_diagnostic *diagnostic = (Z_SRW_diagnostic *)
1223 odr_malloc(o, sizeof(*diagnostic));
1224 int num_diagnostic = 1;
1225 yaz_mk_std_diagnostic(o, diagnostic, srw_error, add);
1226 return send_srw_search_response(diagnostic, num_diagnostic, srw_error == 3 ? 401 : 200);
1229 int Yaz_Proxy::z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
1230 Z_DefaultDiagFormat *ddf)
1232 int bib1_code = *ddf->condition;
1233 if (bib1_code == 109)
1235 srw_res->num_diagnostics = 1;
1236 srw_res->diagnostics = (Z_SRW_diagnostic *)
1237 odr_malloc(o, sizeof(*srw_res->diagnostics));
1238 yaz_mk_std_diagnostic(o, srw_res->diagnostics,
1239 yaz_diag_bib1_to_srw(*ddf->condition),
1244 int Yaz_Proxy::z_to_srw_diag(ODR o, Z_SRW_scanResponse *srw_res,
1247 if (dr->which == Z_DiagRec_defaultFormat)
1249 int bib1_code = *dr->u.defaultFormat->condition;
1250 if (bib1_code == 109)
1252 srw_res->num_diagnostics = 1;
1253 srw_res->diagnostics = (Z_SRW_diagnostic *)
1254 odr_malloc(o, sizeof(*srw_res->diagnostics));
1255 yaz_mk_std_diagnostic(o, srw_res->diagnostics,
1256 yaz_diag_bib1_to_srw(*dr->u.defaultFormat->condition),
1257 dr->u.defaultFormat->u.v2Addinfo);
1262 #if YAZ_HAS_MK_SURROGATE
1264 static void yazproxy_mk_sru_surrogate(ODR o, Z_SRW_record *record, int pos,
1265 int code, const char *details)
1267 const char *message = yaz_diag_srw_str(code);
1270 len += strlen(message);
1272 len += strlen(details);
1274 record->recordData_buf = (char *) odr_malloc(o, len);
1276 sprintf(record->recordData_buf, "<diagnostic "
1277 "xmlns=\"http://www.loc.gov/zing/srw/diagnostic/\">\n"
1278 " <uri>info:srw/diagnostic/1/%d</uri>\n", code);
1280 sprintf(record->recordData_buf + strlen(record->recordData_buf),
1281 " <details>%s</details>\n", details);
1283 sprintf(record->recordData_buf + strlen(record->recordData_buf),
1284 " <message>%s</message>\n", message);
1285 sprintf(record->recordData_buf + strlen(record->recordData_buf),
1287 record->recordData_len = strlen(record->recordData_buf);
1288 record->recordPosition = odr_intdup(o, pos);
1289 record->recordSchema = odr_strdup(o, "info:srw/schema/1/diagnostics-v1.1");
1293 int Yaz_Proxy::send_to_srw_client_ok(int hits, Z_Records *records, int start)
1295 ODR o = odr_encode();
1296 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
1297 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1299 srw_res->numberOfRecords = odr_intdup (o, hits);
1300 if (records && records->which == Z_Records_DBOSD)
1302 srw_res->num_records =
1303 records->u.databaseOrSurDiagnostics->num_records;
1305 srw_res->records = (Z_SRW_record *)
1306 odr_malloc(o, srw_res->num_records * sizeof(Z_SRW_record));
1307 for (i = 0; i < srw_res->num_records; i++)
1309 Z_NamePlusRecord *npr = records->u.databaseOrSurDiagnostics->records[i];
1310 if (npr->which != Z_NamePlusRecord_databaseRecord)
1312 #if YAZ_HAS_MK_SURROGATE
1313 yaz_mk_sru_surrogate(
1314 o, srw_res->records + i, i+start,
1315 YAZ_SRW_RECORD_NOT_AVAILABLE_IN_THIS_SCHEMA, 0);
1317 yazproxy_mk_sru_surrogate(
1318 o, srw_res->records + i, i+start,
1319 YAZ_SRW_RECORD_NOT_AVAILABLE_IN_THIS_SCHEMA, 0);
1323 Z_External *r = npr->u.databaseRecord;
1325 if (r->which == Z_External_octet
1326 && !oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
1328 srw_res->records[i].recordSchema = m_schema;
1329 srw_res->records[i].recordPacking = m_s2z_packing;
1330 srw_res->records[i].recordData_buf = (char*)
1331 r->u.octet_aligned->buf;
1332 srw_res->records[i].recordData_len = r->u.octet_aligned->len;
1333 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
1337 #if YAZ_HAS_MK_SURROGATE
1338 yaz_mk_sru_surrogate(
1339 o, srw_res->records + i, i+start,
1340 YAZ_SRW_RECORD_NOT_AVAILABLE_IN_THIS_SCHEMA, 0);
1342 yazproxy_mk_sru_surrogate(
1343 o, srw_res->records + i, i+start,
1344 YAZ_SRW_RECORD_NOT_AVAILABLE_IN_THIS_SCHEMA, 0);
1349 if (records && records->which == Z_Records_NSD)
1352 http_code = z_to_srw_diag(odr_encode(), srw_res,
1353 records->u.nonSurrogateDiagnostic);
1355 return send_http_response(http_code);
1357 return send_srw_response(srw_pdu);
1361 int Yaz_Proxy::send_to_srw_client_ok(Z_ListEntries *entries)
1363 ODR o = odr_encode();
1364 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_scan_response);
1365 Z_SRW_scanResponse *srw_res = srw_pdu->u.scan_response;
1367 if (entries && entries->num_entries > 0)
1369 srw_res->num_terms = entries->num_entries;
1371 srw_res->terms = (Z_SRW_scanTerm *)
1372 odr_malloc(o, srw_res->num_terms * sizeof(Z_SRW_scanTerm));
1373 for (i = 0; i < srw_res->num_terms; i++)
1375 if (entries->entries[i]->which == Z_Entry_termInfo)
1377 switch(entries->entries[i]->u.termInfo->term->which)
1379 case Z_Term_general:
1380 srw_res->terms[i].value = odr_strdupn(o,
1381 (char *) entries->entries[i]->u.termInfo->term->u.general->buf,
1382 entries->entries[i]->u.termInfo->term->u.general->len);
1385 srw_res->terms[i].value = NULL;
1387 if (entries->entries[i]->u.termInfo->globalOccurrences != NULL)
1388 srw_res->terms[i].numberOfRecords = odr_intdup(o,
1389 *entries->entries[i]->u.termInfo->globalOccurrences);
1391 srw_res->terms[i].numberOfRecords = NULL;
1392 if (entries->entries[i]->u.termInfo->displayTerm != NULL)
1393 srw_res->terms[i].displayTerm = odr_strdup(o,
1394 entries->entries[i]->u.termInfo->displayTerm);
1396 srw_res->terms[i].displayTerm = NULL;
1397 srw_res->terms[i].whereInList = NULL;
1401 if (entries && entries->num_nonsurrogateDiagnostics > 0)
1404 http_code = z_to_srw_diag(odr_encode(), srw_res,
1405 entries->nonsurrogateDiagnostics[0]);
1407 return send_http_response(http_code);
1409 return send_srw_response(srw_pdu);
1413 int Yaz_Proxy::send_srw_search_response(Z_SRW_diagnostic *diagnostics,
1414 int num_diagnostics, int http_code /* = 200 */)
1416 ODR o = odr_encode();
1417 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
1418 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1420 srw_res->num_diagnostics = num_diagnostics;
1421 srw_res->diagnostics = diagnostics;
1422 return send_srw_response(srw_pdu, http_code);
1425 int Yaz_Proxy::send_srw_scan_response(Z_SRW_diagnostic *diagnostics,
1426 int num_diagnostics, int http_code /* = 200 */)
1428 ODR o = odr_encode();
1429 Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_scan_response);
1430 Z_SRW_scanResponse *srw_res = srw_pdu->u.scan_response;
1432 srw_res->num_diagnostics = num_diagnostics;
1433 srw_res->diagnostics = diagnostics;
1434 return send_srw_response(srw_pdu, http_code);
1437 int Yaz_Proxy::send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
1438 int num_diagnostics)
1440 int http_status = 404;
1441 Yaz_ProxyConfig *cfg = check_reconfigure();
1445 char *b = cfg->get_explain_doc(odr_encode(), 0 /* target */,
1446 m_s2z_database, &len, &http_status);
1449 Z_SRW_PDU *res = yaz_srw_get(odr_encode(), Z_SRW_explain_response);
1450 Z_SRW_explainResponse *er = res->u.explain_response;
1452 er->record.recordData_buf = b;
1453 er->record.recordData_len = len;
1454 er->record.recordPacking = m_s2z_packing;
1455 er->record.recordSchema = odr_strdup(odr_encode(),
1456 "http://explain.z3950.org/dtd/2.0/");
1458 er->diagnostics = diagnostics;
1459 er->num_diagnostics = num_diagnostics;
1460 return send_srw_response(res);
1463 return send_http_response(http_status);
1466 int Yaz_Proxy::send_PDU_convert(Z_APDU *apdu)
1470 if (apdu->which == Z_APDU_initResponse)
1472 Z_InitResponse *res = apdu->u.initResponse;
1473 if (*res->result == 0)
1475 send_to_srw_client_error(3, 0);
1477 else if (m_s2z_search_apdu)
1479 handle_incoming_Z_PDU(m_s2z_search_apdu);
1481 else if (m_s2z_scan_apdu)
1483 handle_incoming_Z_PDU(m_s2z_scan_apdu);
1487 send_srw_explain_response(0, 0);
1490 else if (m_s2z_search_apdu && apdu->which == Z_APDU_searchResponse)
1492 m_s2z_search_apdu = 0;
1493 Z_SearchResponse *res = apdu->u.searchResponse;
1494 m_s2z_hit_count = *res->resultCount;
1495 if (res->records && res->records->which == Z_Records_NSD)
1497 send_to_srw_client_ok(0, res->records, 1);
1499 else if (m_s2z_present_apdu && m_s2z_hit_count > 0)
1502 Z_PresentRequest *pr = m_s2z_present_apdu->u.presentRequest;
1504 if (*pr->resultSetStartPoint <= m_s2z_hit_count)
1506 if (*pr->numberOfRecordsRequested+ *pr->resultSetStartPoint
1508 *pr->numberOfRecordsRequested =
1509 1 + m_s2z_hit_count - *pr->resultSetStartPoint;
1511 handle_incoming_Z_PDU(m_s2z_present_apdu);
1515 m_s2z_present_apdu = 0;
1516 send_to_srw_client_ok(m_s2z_hit_count, res->records, 1);
1519 else if (m_s2z_present_apdu && apdu->which == Z_APDU_presentResponse)
1522 *m_s2z_present_apdu->u.presentRequest->resultSetStartPoint;
1524 m_s2z_present_apdu = 0;
1525 Z_PresentResponse *res = apdu->u.presentResponse;
1526 send_to_srw_client_ok(m_s2z_hit_count, res->records, start);
1528 else if (m_s2z_scan_apdu && apdu->which == Z_APDU_scanResponse)
1530 Z_ScanResponse *res = apdu->u.scanResponse;
1531 send_to_srw_client_ok(res->entries);
1537 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
1538 yaz_log(YLOG_LOG, "%sSending %s to client", m_session_str,
1540 int r = send_Z_PDU(apdu, &len);
1541 m_bytes_sent += len;
1542 m_bw_stat.add_bytes(len);
1549 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
1551 int kill_session = 0;
1552 Z_ReferenceId **new_id = get_referenceIdP(apdu);
1555 *new_id = m_referenceId;
1557 if (apdu->which == Z_APDU_searchResponse)
1559 Z_SearchResponse *sr = apdu->u.searchResponse;
1560 Z_Records *p = sr->records;
1561 if (p && p->which == Z_Records_NSD)
1563 Z_DiagRec dr, *dr_p = &dr;
1564 dr.which = Z_DiagRec_defaultFormat;
1565 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1567 *sr->searchStatus = 0;
1568 display_diagrecs(&dr_p, 1);
1572 if (p && p->which == Z_Records_DBOSD)
1576 || m_usemarcon_ini_stage1 || m_usemarcon_ini_stage2
1579 convert_to_frontend_type(p->u.databaseOrSurDiagnostics);
1580 if (m_marcxml_mode == marcxml)
1581 convert_to_marcxml(p->u.databaseOrSurDiagnostics,
1584 convert_records_charset(p->u.databaseOrSurDiagnostics,
1586 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1590 if (sr->resultCount)
1592 yaz_log(YLOG_LOG, "%s" ODR_INT_PRINTF " hits", m_session_str,
1594 if (*sr->resultCount < 0)
1596 m_flag_invalid_session = 1;
1599 *sr->searchStatus = 0;
1601 create_nonSurrogateDiagnostics(odr_encode(), 2, 0);
1602 *sr->resultCount = 0;
1607 else if (apdu->which == Z_APDU_presentResponse)
1609 Z_PresentResponse *sr = apdu->u.presentResponse;
1610 Z_Records *p = sr->records;
1611 if (p && p->which == Z_Records_NSD)
1613 Z_DiagRec dr, *dr_p = &dr;
1614 dr.which = Z_DiagRec_defaultFormat;
1615 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1616 if (*sr->presentStatus == Z_PresentStatus_success)
1617 *sr->presentStatus = Z_PresentStatus_failure;
1618 display_diagrecs(&dr_p, 1);
1620 if (p && p->which == Z_Records_DBOSD)
1624 || m_usemarcon_ini_stage1 || m_usemarcon_ini_stage2
1627 convert_to_frontend_type(p->u.databaseOrSurDiagnostics);
1628 if (m_marcxml_mode == marcxml)
1629 convert_to_marcxml(p->u.databaseOrSurDiagnostics,
1632 convert_records_charset(p->u.databaseOrSurDiagnostics,
1634 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1638 else if (apdu->which == Z_APDU_initResponse)
1640 //Get and check negotiation record
1641 //from init response.
1642 handle_charset_lang_negotiation(apdu);
1644 if (m_initRequest_options)
1647 (Odr_bitmask *)odr_malloc(odr_encode(),
1648 sizeof(Odr_bitmask));
1649 ODR_MASK_ZERO(nopt);
1652 for (i = 0; i<24; i++)
1653 if (ODR_MASK_GET(m_initRequest_options, i) &&
1654 ODR_MASK_GET(apdu->u.initResponse->options, i))
1655 ODR_MASK_SET(nopt, i);
1656 apdu->u.initResponse->options = nopt;
1658 if (m_initRequest_version)
1660 Z_ProtocolVersion *nopt =
1661 (Odr_bitmask *)odr_malloc(odr_encode(),
1662 sizeof(Odr_bitmask));
1663 ODR_MASK_ZERO(nopt);
1666 for (i = 0; i<8; i++)
1667 if (ODR_MASK_GET(m_initRequest_version, i) &&
1668 ODR_MASK_GET(apdu->u.initResponse->protocolVersion, i))
1669 ODR_MASK_SET(nopt, i);
1670 apdu->u.initResponse->protocolVersion = nopt;
1672 apdu->u.initResponse->preferredMessageSize =
1673 odr_intdup(odr_encode(),
1674 m_client->m_initResponse_preferredMessageSize >
1675 m_initRequest_preferredMessageSize ?
1676 m_initRequest_preferredMessageSize :
1677 m_client->m_initResponse_preferredMessageSize);
1678 apdu->u.initResponse->maximumRecordSize =
1679 odr_intdup(odr_encode(),
1680 m_client->m_initResponse_maximumRecordSize >
1681 m_initRequest_maximumRecordSize ?
1682 m_initRequest_maximumRecordSize :
1683 m_client->m_initResponse_maximumRecordSize);
1686 int r = send_PDU_convert(apdu);
1693 m_parent->pre_init();
1698 void Yaz_ProxyClient::set_idAuthentication(Z_APDU *apdu)
1700 Z_IdAuthentication *t = apdu->u.initRequest->idAuthentication;
1702 odr_reset(m_idAuthentication_odr);
1703 z_IdAuthentication(m_idAuthentication_odr, &t, 1, 0);
1704 m_idAuthentication_ber_buf =
1705 odr_getbuf(m_idAuthentication_odr,
1706 &m_idAuthentication_ber_size, 0);
1709 bool Yaz_ProxyClient::compare_charset(Z_APDU *apdu)
1714 bool Yaz_ProxyClient::compare_idAuthentication(Z_APDU *apdu)
1716 Z_IdAuthentication *t = apdu->u.initRequest->idAuthentication;
1717 ODR odr = odr_createmem(ODR_ENCODE);
1719 z_IdAuthentication(odr, &t, 1, 0);
1721 char *buf = odr_getbuf(odr, &sz, 0);
1722 if (buf && m_idAuthentication_ber_buf
1723 && sz == m_idAuthentication_ber_size
1724 && !memcmp(m_idAuthentication_ber_buf, buf, sz))
1730 if (!buf && !m_idAuthentication_ber_buf)
1735 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
1738 const char *apdu_name_tmp = apdu_name(apdu);
1739 int r = send_Z_PDU(apdu, &len);
1740 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
1741 yaz_log(YLOG_LOG, "%sSending %s to %s %d bytes",
1743 apdu_name_tmp, get_hostname(), len);
1744 m_bytes_sent += len;
1748 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
1750 if (apdu->which == Z_APDU_presentRequest)
1752 Z_PresentRequest *pr = apdu->u.presentRequest;
1753 int toget = *pr->numberOfRecordsRequested;
1754 int start = *pr->resultSetStartPoint;
1756 yaz_log(YLOG_LOG, "%sPresent %s %d+%d", m_session_str,
1757 pr->resultSetId, start, toget);
1759 if (*m_parent->m_optimize == '0')
1762 if (!m_client->m_last_resultSetId)
1764 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1765 new_apdu->u.presentResponse->records =
1766 create_nonSurrogateDiagnostics(
1768 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1770 send_to_client(new_apdu);
1773 if (start < 1 || toget < 0)
1775 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1776 new_apdu->u.presentResponse->records =
1777 create_nonSurrogateDiagnostics(
1779 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1781 send_to_client(new_apdu);
1784 if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
1786 if (start+toget-1 > m_client->m_last_resultCount)
1788 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1789 new_apdu->u.presentResponse->records =
1790 create_nonSurrogateDiagnostics(
1792 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1794 send_to_client(new_apdu);
1797 Z_NamePlusRecordList *npr;
1799 yaz_log(YLOG_LOG, "%sCache lookup %d+%d syntax=%s",
1800 m_session_str, start, toget, yaz_z3950oid_to_str(
1801 pr->preferredRecordSyntax, &oclass));
1803 if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
1804 pr->preferredRecordSyntax,
1805 pr->recordComposition))
1807 yaz_log(YLOG_LOG, "%sReturned cached records for present request",
1809 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1810 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1812 new_apdu->u.presentResponse->numberOfRecordsReturned
1813 = odr_intdup(odr_encode(), toget);
1815 new_apdu->u.presentResponse->records = (Z_Records*)
1816 odr_malloc(odr_encode(), sizeof(Z_Records));
1817 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
1818 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
1819 new_apdu->u.presentResponse->nextResultSetPosition =
1820 odr_intdup(odr_encode(), start+toget);
1822 send_to_client(new_apdu);
1828 if (apdu->which != Z_APDU_searchRequest)
1830 Z_SearchRequest *sr = apdu->u.searchRequest;
1831 Yaz_Z_Query *this_query = new Yaz_Z_Query;
1832 Yaz_Z_Databases this_databases;
1834 this_databases.set(sr->num_databaseNames, (const char **)
1837 this_query->set_Z_Query(sr->query);
1839 // Check for non-negative piggyback params.
1840 if (*sr->smallSetUpperBound < 0
1841 || *sr->largeSetLowerBound < 0
1842 || *sr->mediumSetPresentNumber < 0)
1844 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1845 // Not a present request.. But can't find better diagnostic
1846 new_apdu->u.searchResponse->records =
1847 create_nonSurrogateDiagnostics(
1849 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, 0);
1850 send_to_client(new_apdu);
1854 char query_str[4096];
1855 this_query->print(query_str, sizeof(query_str)-1);
1856 yaz_log(YLOG_LOG, "%sSearch %s", m_session_str, query_str);
1858 if (*m_parent->m_optimize != '0' &&
1859 m_client->m_last_ok && m_client->m_last_query &&
1860 m_client->m_last_query->match(this_query) &&
1861 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
1862 m_client->m_last_databases.match(this_databases))
1865 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
1866 m_client->m_last_resultCount < *sr->largeSetLowerBound)
1868 Z_NamePlusRecordList *npr;
1869 int toget = *sr->mediumSetPresentNumber;
1870 Z_RecordComposition *comp = 0;
1872 if (toget > m_client->m_last_resultCount)
1873 toget = m_client->m_last_resultCount;
1875 if (sr->mediumSetElementSetNames)
1877 comp = (Z_RecordComposition *)
1878 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1879 comp->which = Z_RecordComp_simple;
1880 comp->u.simple = sr->mediumSetElementSetNames;
1883 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1884 sr->preferredRecordSyntax, comp))
1886 yaz_log(YLOG_LOG, "%sReturned cached records for medium set",
1888 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1889 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1890 new_apdu->u.searchResponse->resultCount =
1891 &m_client->m_last_resultCount;
1893 new_apdu->u.searchResponse->numberOfRecordsReturned
1894 = odr_intdup(odr_encode(), toget);
1896 new_apdu->u.searchResponse->presentStatus =
1897 odr_intdup(odr_encode(), Z_PresentStatus_success);
1898 new_apdu->u.searchResponse->records = (Z_Records*)
1899 odr_malloc(odr_encode(), sizeof(Z_Records));
1900 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1901 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1902 new_apdu->u.searchResponse->nextResultSetPosition =
1903 odr_intdup(odr_encode(), toget+1);
1904 send_to_client(new_apdu);
1910 // send present request (medium size)
1911 yaz_log(YLOG_LOG, "%sOptimizing search for medium set",
1914 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1915 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1916 pr->referenceId = sr->referenceId;
1917 pr->resultSetId = sr->resultSetName;
1918 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1919 *pr->numberOfRecordsRequested = toget;
1920 pr->recordComposition = comp;
1921 m_client->m_sr_transform = 1;
1925 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
1926 m_client->m_last_resultCount <= 0)
1928 // large set. Return pseudo-search response immediately
1929 yaz_log(YLOG_LOG, "%sOptimizing search for large set",
1931 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1932 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1933 new_apdu->u.searchResponse->resultCount =
1934 &m_client->m_last_resultCount;
1935 send_to_client(new_apdu);
1940 Z_NamePlusRecordList *npr;
1941 int toget = m_client->m_last_resultCount;
1942 Z_RecordComposition *comp = 0;
1944 // send a present request (small set)
1946 if (sr->smallSetElementSetNames)
1948 comp = (Z_RecordComposition *)
1949 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1950 comp->which = Z_RecordComp_simple;
1951 comp->u.simple = sr->smallSetElementSetNames;
1954 if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1955 sr->preferredRecordSyntax, comp))
1957 yaz_log(YLOG_LOG, "%sReturned cached records for small set",
1959 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1960 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1961 new_apdu->u.searchResponse->resultCount =
1962 &m_client->m_last_resultCount;
1964 new_apdu->u.searchResponse->numberOfRecordsReturned
1965 = odr_intdup(odr_encode(), toget);
1967 new_apdu->u.searchResponse->presentStatus =
1968 odr_intdup(odr_encode(), Z_PresentStatus_success);
1969 new_apdu->u.searchResponse->records = (Z_Records*)
1970 odr_malloc(odr_encode(), sizeof(Z_Records));
1971 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1972 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1973 new_apdu->u.searchResponse->nextResultSetPosition =
1974 odr_intdup(odr_encode(), toget+1);
1975 send_to_client(new_apdu);
1980 yaz_log(YLOG_LOG, "%sOptimizing search for small set",
1982 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1983 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1984 pr->referenceId = sr->referenceId;
1985 pr->resultSetId = sr->resultSetName;
1986 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1987 *pr->numberOfRecordsRequested = toget;
1988 pr->recordComposition = comp;
1989 m_client->m_sr_transform = 1;
1994 else // query doesn't match
1996 delete m_client->m_last_query;
1997 m_client->m_last_query = this_query;
1998 m_client->m_last_ok = 0;
1999 m_client->m_cache.clear();
2000 m_client->m_resultSetStartPoint = 0;
2002 xfree(m_client->m_last_resultSetId);
2003 m_client->m_last_resultSetId = xstrdup(sr->resultSetName);
2005 m_client->m_last_databases.set(sr->num_databaseNames,
2006 (const char **) sr->databaseNames);
2012 void Yaz_Proxy::inc_request_no()
2015 char *cp = m_session_str + strlen(m_session_str)-1;
2018 while (*cp && *cp != ' ')
2021 sprintf(cp+1, "%d ", m_request_no);
2024 void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
2028 m_bytes_recv += len;
2030 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
2031 yaz_log(YLOG_LOG, "%sReceiving %s from client %d bytes",
2032 m_session_str, gdu_name(apdu), len);
2035 // try to make a _bad_ attribute set ID .. Don't enable this in prod.
2036 if (apdu->which == Z_GDU_Z3950
2037 && apdu->u.z3950->which == Z_APDU_searchRequest)
2039 Z_SearchRequest *req = apdu->u.z3950->u.searchRequest;
2040 if (req->query && req->query->which == Z_Query_type_1)
2042 Z_RPNQuery *rpnquery = req->query->u.type_1;
2043 if (rpnquery->attributeSetId)
2045 rpnquery->attributeSetId[0] = -2;
2046 rpnquery->attributeSetId[1] = -1;
2047 yaz_log(YLOG_WARN, "%sBAD FIXUP TEST", m_session_str);
2053 #if HAVE_GETTIMEOFDAY
2054 gettimeofday((struct timeval *) m_time_tv, 0);
2056 m_bw_stat.add_bytes(len);
2057 m_pdu_stat.add_bytes(1);
2059 GDU *gdu = new GDU(apdu);
2061 if (gdu->get() == 0)
2064 yaz_log(YLOG_LOG, "%sUnable to encode package", m_session_str);
2069 m_in_queue.enqueue(gdu);
2070 recv_GDU_more(false);
2073 void Yaz_Proxy::HTTP_Forwarded(Z_GDU *z_gdu)
2075 if (z_gdu->which == Z_GDU_HTTP_Request)
2077 Z_HTTP_Request *hreq = z_gdu->u.HTTP_Request;
2078 const char *x_forwarded_for =
2079 z_HTTP_header_lookup(hreq->headers, "X-Forwarded-For");
2080 if (x_forwarded_for)
2083 m_peername = (char*) xmalloc(strlen(x_forwarded_for)+5);
2084 sprintf(m_peername, "tcp:%s", x_forwarded_for);
2086 yaz_log(YLOG_LOG, "%sHTTP Forwarded from %s", m_session_str,
2088 if (m_log_mask & PROXY_LOG_IP_CLIENT)
2089 sprintf(m_session_str, "%ld:%d %.80s %d ",
2090 (long) time(0), m_session_no, m_peername, m_request_no);
2092 sprintf(m_session_str, "%ld:%d %d ",
2093 (long) time(0), m_session_no, m_request_no);
2098 void Yaz_Proxy::connect_stat(bool &block, int &reduce)
2101 m_parent->m_connect.cleanup(false);
2102 m_parent->m_connect.add_connect(m_peername);
2104 int connect_total = m_parent->m_connect.get_total(m_peername);
2105 int max_connect = m_parent->m_max_connect;
2107 if (max_connect && connect_total > max_connect)
2109 yaz_log(YLOG_LOG, "%sconnect not accepted total=%d max=%d",
2110 m_session_str, connect_total, max_connect);
2115 yaz_log(YLOG_LOG, "%sconnect accepted total=%d", m_session_str,
2118 int limit_connect = m_parent->m_limit_connect;
2120 reduce = connect_total / limit_connect;
2125 void Yaz_Proxy::recv_GDU_reduce(GDU *gdu)
2127 HTTP_Forwarded(gdu->get());
2131 if (m_request_no == 1)
2135 connect_stat(block, reduce);
2139 m_timeout_mode = timeout_busy;
2145 int bw_total = m_bw_stat.get_total();
2146 int pdu_total = m_pdu_stat.get_total();
2147 int search_total = m_search_stat.get_total();
2149 assert(m_timeout_mode == timeout_busy);
2150 assert(m_timeout_gdu == 0);
2153 reduce += search_total / m_search_max;
2155 reduce += (bw_total/m_bw_max);
2158 if (pdu_total > m_pdu_max)
2160 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
2161 reduce = (reduce > nreduce) ? reduce : nreduce;
2167 /* uncomment to force a big reduce */
2168 m_timeout_mode = timeout_reduce;
2169 m_timeout_gdu = gdu;
2170 timeout(3); // call us reduce seconds later
2175 yaz_log(YLOG_LOG, "%sdelay=%d bw=%d pdu=%d search=%d limit-bw=%d limit-pdu=%d limit-search=%d",
2176 m_session_str, reduce, bw_total, pdu_total, search_total,
2177 m_bw_max, m_pdu_max, m_search_max);
2179 m_timeout_mode = timeout_reduce;
2180 m_timeout_gdu = gdu;
2181 timeout(reduce); // call us reduce seconds later
2184 recv_GDU_normal(gdu);
2187 void Yaz_Proxy::recv_GDU_more(bool normal)
2190 if (normal && m_timeout_mode == timeout_busy)
2191 m_timeout_mode = timeout_normal;
2192 while (m_timeout_mode == timeout_normal && (g = m_in_queue.dequeue()))
2194 m_timeout_mode = timeout_busy;
2202 void Yaz_Proxy::recv_GDU_normal(GDU *gdu)
2205 gdu->move_away_gdu(odr_decode(), &apdu);
2208 if (apdu->which == Z_GDU_Z3950)
2209 handle_incoming_Z_PDU(apdu->u.z3950);
2210 else if (apdu->which == Z_GDU_HTTP_Request)
2211 handle_incoming_HTTP(apdu->u.HTTP_Request);
2214 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
2216 if (m_max_record_retrieve)
2218 if (apdu->which == Z_APDU_presentRequest)
2220 Z_PresentRequest *pr = apdu->u.presentRequest;
2221 if (pr->numberOfRecordsRequested &&
2222 *pr->numberOfRecordsRequested > m_max_record_retrieve)
2223 *pr->numberOfRecordsRequested = m_max_record_retrieve;
2228 void Yaz_Proxy::handle_charset_lang_negotiation(Z_APDU *apdu)
2230 if (apdu->which == Z_APDU_initRequest)
2232 if (m_initRequest_options &&
2233 !ODR_MASK_GET(m_initRequest_options, Z_Options_negotiationModel) &&
2234 (m_proxy_negotiation_charset || m_proxy_negotiation_lang))
2236 // There is no negotiation proposal from
2237 // client's side. OK. The proxy negotiation
2239 Z_InitRequest *initRequest = apdu->u.initRequest;
2240 Z_OtherInformation **otherInfo;
2241 Z_OtherInformationUnit *oi;
2242 get_otherInfoAPDU(apdu, &otherInfo);
2243 oi = update_otherInformation(otherInfo, 1, NULL, 0, 0);
2246 ODR_MASK_SET(initRequest->options,
2247 Z_Options_negotiationModel);
2248 oi->which = Z_OtherInfo_externallyDefinedInfo;
2249 oi->information.externallyDefinedInfo =
2250 yaz_set_proposal_charneg(odr_encode(),
2251 (const char**)&m_proxy_negotiation_charset,
2252 m_proxy_negotiation_charset ? 1:0,
2253 (const char**)&m_proxy_negotiation_lang,
2254 m_proxy_negotiation_lang ? 1:0,
2258 else if (m_initRequest_options &&
2259 ODR_MASK_GET(m_initRequest_options,
2260 Z_Options_negotiationModel) &&
2261 m_charset_converter->get_target_query_charset())
2263 yaz_log(YLOG_LOG, "%sManaged charset negotiation: charset=%s",
2265 m_charset_converter->get_target_query_charset());
2266 Z_InitRequest *initRequest = apdu->u.initRequest;
2267 Z_CharSetandLanguageNegotiation *negotiation =
2268 yaz_get_charneg_record (initRequest->otherInfo);
2270 negotiation->which == Z_CharSetandLanguageNegotiation_proposal)
2272 NMEM nmem = nmem_create();
2273 char **charsets = 0;
2274 int num_charsets = 0;
2278 yaz_get_proposal_charneg (nmem, negotiation,
2279 &charsets, &num_charsets,
2280 &langs, &num_langs, &selected);
2282 for (i = 0; i<num_charsets; i++)
2283 yaz_log(YLOG_LOG, "%scharset %s", m_session_str,
2285 for (i = 0; i<num_langs; i++)
2286 yaz_log(YLOG_LOG, "%slang %s", m_session_str,
2289 const char *t_charset =
2290 m_charset_converter->get_target_query_charset();
2291 // sweep through charsets and pick the first supported
2293 for (i = 0; i<num_charsets; i++)
2295 const char *c_charset = charsets[i];
2296 if (!odr_set_charset(odr_decode(), t_charset, c_charset))
2299 if (i != num_charsets)
2301 // got one .. set up ODR for reverse direction
2302 const char *c_charset = charsets[i];
2303 odr_set_charset(odr_encode(), c_charset, t_charset);
2304 m_charset_converter->set_client_query_charset(c_charset);
2305 m_charset_converter->set_client_charset_selected(selected);
2308 ODR_MASK_CLEAR(m_initRequest_options,
2309 Z_Options_negotiationModel);
2310 yaz_del_charneg_record(&initRequest->otherInfo);
2314 yaz_log(YLOG_WARN, "%sUnable to decode charset package",
2318 else if (m_charset_converter->get_target_query_charset() &&
2319 m_proxy_negotiation_default_charset)
2321 m_charset_converter->
2322 set_client_query_charset(m_proxy_negotiation_default_charset);
2325 else if (apdu->which == Z_APDU_initResponse)
2327 Z_InitResponse *initResponse = apdu->u.initResponse;
2328 Z_OtherInformation **otherInfo;
2329 get_otherInfoAPDU(apdu, &otherInfo);
2331 Z_CharSetandLanguageNegotiation *charneg = 0;
2333 if (otherInfo && *otherInfo &&
2334 ODR_MASK_GET(initResponse->options, Z_Options_negotiationModel)
2335 && (charneg = yaz_get_charneg_record(*otherInfo)))
2341 yaz_get_response_charneg(m_referenceId_mem, charneg,
2342 &charset, &lang, &selected);
2344 yaz_log(YLOG_LOG, "%sAccepted charset - '%s' and lang - '%s'",
2345 m_session_str, (charset)?charset:"none", (lang)?lang:"none");
2347 if (m_initRequest_options &&
2348 ODR_MASK_GET(m_initRequest_options, Z_Options_negotiationModel))
2350 yaz_log(YLOG_LOG, "%sClient's negotiation record in use",
2353 else if (m_proxy_negotiation_charset || m_proxy_negotiation_lang)
2355 // negotiation-charset, negotiation-lang
2356 // elements of config file in use.
2358 yaz_log(YLOG_LOG, "%sProxy's negotiation record in use",
2361 // clear negotiation option.
2362 ODR_MASK_CLEAR(initResponse->options, Z_Options_negotiationModel);
2364 // Delete negotiation (charneg-3) entry.
2365 yaz_del_charneg_record(otherInfo);
2370 if (m_proxy_negotiation_charset || m_proxy_negotiation_lang)
2372 yaz_log(YLOG_LOG, "%sTarget did not honor negotiation",
2375 else if (m_charset_converter->get_client_query_charset())
2377 Z_OtherInformation **otherInfo;
2378 Z_OtherInformationUnit *oi;
2379 get_otherInfoAPDU(apdu, &otherInfo);
2380 oi = update_otherInformation(otherInfo, 1, NULL, 0, 0);
2383 ODR_MASK_SET(initResponse->options,
2384 Z_Options_negotiationModel);
2385 if (m_initRequest_options)
2386 ODR_MASK_SET(m_initRequest_options,
2387 Z_Options_negotiationModel);
2389 oi->which = Z_OtherInfo_externallyDefinedInfo;
2390 oi->information.externallyDefinedInfo =
2391 yaz_set_response_charneg(
2393 m_charset_converter->get_client_query_charset(),
2395 m_charset_converter->get_client_charset_selected());
2402 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
2404 const char *addinfo)
2406 Z_Records *rec = (Z_Records *)
2407 odr_malloc (odr, sizeof(*rec));
2408 Odr_int *err = (Odr_int *)
2409 odr_malloc (odr, sizeof(*err));
2410 Z_DiagRec *drec = (Z_DiagRec *)
2411 odr_malloc (odr, sizeof(*drec));
2412 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
2413 odr_malloc (odr, sizeof(*dr));
2415 rec->which = Z_Records_NSD;
2416 rec->u.nonSurrogateDiagnostic = dr;
2417 dr->diagnosticSetId = odr_oiddup(odr, yaz_oid_diagset_bib_1);
2418 dr->condition = err;
2419 dr->which = Z_DefaultDiagFormat_v2Addinfo;
2420 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
2424 Z_ListEntries *Yaz_Proxy::create_nonSurrogateDiagnostics2(ODR odr,
2426 const char *addinfo)
2428 Z_ListEntries *rec = (Z_ListEntries *)
2429 odr_malloc (odr, sizeof(*rec));
2430 Odr_int *err = (Odr_int *)
2431 odr_malloc (odr, sizeof(*err));
2432 Z_DiagRec *drec = (Z_DiagRec *)
2433 odr_malloc (odr, sizeof(*drec));
2434 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
2435 odr_malloc (odr, sizeof(*dr));
2437 drec->which = Z_DiagRec_defaultFormat;
2438 drec->u.defaultFormat = dr;
2439 rec->num_entries = 0;
2440 rec->entries = NULL;
2441 rec->num_nonsurrogateDiagnostics = 1;
2442 rec->nonsurrogateDiagnostics =
2443 (Z_DiagRec **)odr_malloc(odr, sizeof(Z_DiagRec *));
2444 rec->nonsurrogateDiagnostics[0] = drec;
2445 dr->diagnosticSetId = odr_oiddup(odr, yaz_oid_diagset_bib_1);
2446 dr->condition = err;
2447 dr->which = Z_DefaultDiagFormat_v2Addinfo;
2448 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
2452 Z_APDU *Yaz_Proxy::handle_query_transformation(Z_APDU *apdu)
2454 if (apdu->which == Z_APDU_searchRequest &&
2455 apdu->u.searchRequest->query &&
2456 apdu->u.searchRequest->query->which == Z_Query_type_104 &&
2457 apdu->u.searchRequest->query->u.type_104->which == Z_External_CQL)
2459 Z_RPNQuery *rpnquery = 0;
2460 Z_SearchRequest *sr = apdu->u.searchRequest;
2463 yaz_log(YLOG_LOG, "%sCQL: %s", m_session_str,
2464 sr->query->u.type_104->u.cql);
2466 int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
2467 &rpnquery, odr_encode(),
2470 yaz_log(YLOG_LOG, "%sNo CQL to RPN table", m_session_str);
2473 yaz_log(YLOG_LOG, "%sCQL Conversion error %d", m_session_str, r);
2474 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2476 new_apdu->u.searchResponse->referenceId = sr->referenceId;
2477 new_apdu->u.searchResponse->records =
2478 create_nonSurrogateDiagnostics(odr_encode(),
2479 yaz_diag_srw_to_bib1(r),
2481 *new_apdu->u.searchResponse->searchStatus = 0;
2483 send_to_client(new_apdu);
2489 sr->query->which = Z_Query_type_1;
2490 sr->query->u.type_1 = rpnquery;
2494 else if (apdu->which == Z_APDU_scanRequest)
2496 Z_RPNQuery *rpnquery = 0;
2497 Z_ScanRequest *sr = apdu->u.scanRequest;
2500 yaz_log(YLOG_LOG, "%sCQL: %s", m_session_str,
2501 sr->termListAndStartPoint->term->u.characterString);
2503 int r = m_cql2rpn.query_transform(sr->termListAndStartPoint->term->u.characterString,
2504 &rpnquery, odr_encode(),
2507 yaz_log(YLOG_LOG, "%sNo CQL to RPN table", m_session_str);
2510 yaz_log(YLOG_LOG, "%sCQL Conversion error %d", m_session_str, r);
2511 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_scanResponse);
2513 new_apdu->u.scanResponse->referenceId = sr->referenceId;
2514 new_apdu->u.scanResponse->entries =
2515 create_nonSurrogateDiagnostics2(odr_encode(),
2516 yaz_diag_srw_to_bib1(r),
2518 *new_apdu->u.scanResponse->scanStatus = Z_Scan_failure;
2520 send_to_client(new_apdu);
2526 sr->attributeSet = rpnquery->attributeSetId;
2527 if (rpnquery->RPNStructure->which == Z_RPNStructure_simple)
2528 sr->termListAndStartPoint = rpnquery->RPNStructure->u.simple->u.attributesPlusTerm;
2535 Z_APDU *Yaz_Proxy::handle_target_charset_conversion(Z_APDU *apdu)
2537 if (apdu->which == Z_APDU_searchRequest &&
2538 apdu->u.searchRequest->query)
2540 if (apdu->u.searchRequest->query->which == Z_Query_type_1
2541 || apdu->u.searchRequest->query->which == Z_Query_type_101)
2544 m_charset_converter->set_client_query_charset("UTF-8");
2545 Z_RPNQuery *rpnquery = apdu->u.searchRequest->query->u.type_1;
2546 m_charset_converter->convert_type_1(rpnquery, odr_encode());
2549 else if (apdu->which == Z_APDU_scanRequest &&
2550 apdu->u.scanRequest->termListAndStartPoint)
2552 if (apdu->u.scanRequest->termListAndStartPoint->term)
2554 m_charset_converter->set_client_query_charset("UTF-8");
2555 Z_Term *term = apdu->u.scanRequest->termListAndStartPoint->term;
2556 m_charset_converter->convert_term(term, odr_encode());
2562 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
2564 if (apdu->which == Z_APDU_searchRequest)
2566 Z_SearchRequest *sr = apdu->u.searchRequest;
2570 Yaz_ProxyConfig *cfg = check_reconfigure();
2572 err = cfg->check_query(odr_encode(), m_default_target,
2573 sr->query, &addinfo);
2576 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2578 new_apdu->u.searchResponse->referenceId = sr->referenceId;
2579 new_apdu->u.searchResponse->records =
2580 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
2581 *new_apdu->u.searchResponse->searchStatus = 0;
2583 send_to_client(new_apdu);
2588 else if (apdu->which == Z_APDU_scanRequest)
2590 Z_ScanRequest *sr = apdu->u.scanRequest;
2594 Yaz_ProxyConfig *cfg = check_reconfigure();
2595 // Something like this needs to be implemented later:
2598 err = cfg->check_type_1_attributes(odr_encode(), m_default_target,
2599 sr->termListAndStartPoint->attributes, &addinfo);
2603 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_scanResponse);
2605 new_apdu->u.scanResponse->referenceId = sr->referenceId;
2606 new_apdu->u.scanResponse->entries =
2607 create_nonSurrogateDiagnostics2(odr_encode(), err, addinfo);
2608 *new_apdu->u.scanResponse->scanStatus = Z_Scan_failure;
2610 send_to_client(new_apdu);
2619 int Yaz_Proxy::handle_authentication(Z_APDU *apdu)
2621 if (apdu->which != Z_APDU_initRequest)
2622 return 1; // pass if no init request
2623 Z_InitRequest *req = apdu->u.initRequest;
2625 Yaz_ProxyConfig *cfg = check_reconfigure();
2627 return 1; // pass if no config
2630 if (req->idAuthentication == 0)
2632 ret = cfg->client_authentication(m_default_target, 0, 0, 0,
2635 else if (req->idAuthentication->which == Z_IdAuthentication_idPass)
2637 ret = cfg->client_authentication(
2639 req->idAuthentication->u.idPass->userId,
2640 req->idAuthentication->u.idPass->groupId,
2641 req->idAuthentication->u.idPass->password,
2644 else if (req->idAuthentication->which == Z_IdAuthentication_open)
2646 char user[64], pass[64];
2649 sscanf(req->idAuthentication->u.open, "%63[^/]/%63s", user, pass);
2650 ret = cfg->client_authentication(m_default_target, user, 0, pass,
2654 ret = cfg->client_authentication(m_default_target, 0, 0, 0,
2659 int Yaz_Proxy::handle_global_authentication(Z_APDU *apdu)
2661 if (apdu->which != Z_APDU_initRequest)
2662 return 1; // pass if no init request
2663 Z_InitRequest *req = apdu->u.initRequest;
2665 Yaz_ProxyConfig *cfg = check_reconfigure();
2667 return 1; // pass if no config
2670 if (req->idAuthentication == 0)
2672 ret = cfg->global_client_authentication(0, 0, 0,
2675 else if (req->idAuthentication->which == Z_IdAuthentication_idPass)
2677 ret = cfg->global_client_authentication(
2678 req->idAuthentication->u.idPass->userId,
2679 req->idAuthentication->u.idPass->groupId,
2680 req->idAuthentication->u.idPass->password,
2683 else if (req->idAuthentication->which == Z_IdAuthentication_open)
2685 char user[64], pass[64];
2688 sscanf(req->idAuthentication->u.open, "%63[^/]/%63s", user, pass);
2689 ret = cfg->global_client_authentication(user, 0, pass,
2693 ret = cfg->global_client_authentication(0, 0, 0, m_peername);
2697 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
2699 m_marcxml_mode = none;
2700 if (apdu->which == Z_APDU_searchRequest)
2702 Z_SearchRequest *sr = apdu->u.searchRequest;
2705 Yaz_ProxyConfig *cfg = check_reconfigure();
2707 Z_RecordComposition rc_temp, *rc = 0;
2708 if (sr->smallSetElementSetNames)
2710 rc_temp.which = Z_RecordComp_simple;
2711 rc_temp.u.simple = sr->smallSetElementSetNames;
2715 if (sr->preferredRecordSyntax)
2716 oid_oidcpy(m_frontend_type, sr->preferredRecordSyntax);
2718 m_frontend_type[0] = -1;
2720 char *stylesheet_name = 0;
2722 err = cfg->check_syntax(odr_encode(),
2724 sr->preferredRecordSyntax, rc,
2725 &addinfo, &stylesheet_name, &m_schema,
2726 &m_backend_type, &m_backend_charset,
2727 &m_usemarcon_ini_stage1,
2728 &m_usemarcon_ini_stage2,
2729 &m_backend_elementset);
2730 if (stylesheet_name)
2732 m_parent->low_socket_close();
2735 if (m_stylesheet_xsp)
2736 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
2737 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
2740 m_stylesheet_offset = 0;
2741 xfree(stylesheet_name);
2743 m_parent->low_socket_open();
2747 sr->smallSetElementSetNames = 0;
2748 sr->mediumSetElementSetNames = 0;
2749 m_marcxml_mode = marcxml;
2750 sr->preferredRecordSyntax =
2751 yaz_string_to_oid_odr(
2752 yaz_oid_std(), CLASS_RECSYN,
2753 m_backend_type ? m_backend_type : "usmarc",
2758 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2760 new_apdu->u.searchResponse->referenceId = sr->referenceId;
2761 new_apdu->u.searchResponse->records =
2762 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
2763 *new_apdu->u.searchResponse->searchStatus = 0;
2765 send_to_client(new_apdu);
2769 else if (m_backend_type)
2771 sr->preferredRecordSyntax =
2772 yaz_string_to_oid_odr(yaz_oid_std(), CLASS_RECSYN,
2773 m_backend_type, odr_encode());
2775 if (m_backend_elementset)
2777 Z_ElementSetNames *esn = mk_esn_from_schema(odr_encode(),
2778 m_backend_elementset);
2779 sr->smallSetElementSetNames = esn;
2780 sr->mediumSetElementSetNames = esn;
2783 else if (apdu->which == Z_APDU_presentRequest)
2785 Z_PresentRequest *pr = apdu->u.presentRequest;
2788 Yaz_ProxyConfig *cfg = check_reconfigure();
2790 if (pr->preferredRecordSyntax)
2791 oid_oidcpy(m_frontend_type, pr->preferredRecordSyntax);
2793 m_frontend_type[0] = -1;
2795 char *stylesheet_name = 0;
2797 err = cfg->check_syntax(odr_encode(), m_default_target,
2798 pr->preferredRecordSyntax,
2799 pr->recordComposition,
2800 &addinfo, &stylesheet_name, &m_schema,
2801 &m_backend_type, &m_backend_charset,
2802 &m_usemarcon_ini_stage1,
2803 &m_usemarcon_ini_stage2,
2804 &m_backend_elementset);
2805 if (stylesheet_name)
2807 m_parent->low_socket_close();
2810 if (m_stylesheet_xsp)
2811 xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
2812 m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
2815 m_stylesheet_offset = 0;
2816 xfree(stylesheet_name);
2818 m_parent->low_socket_open();
2822 pr->recordComposition = 0;
2823 m_marcxml_mode = marcxml;
2825 pr->preferredRecordSyntax =
2826 yaz_string_to_oid_odr(
2827 yaz_oid_std(), CLASS_RECSYN,
2828 m_backend_type ? m_backend_type : "usmarc",
2833 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
2835 new_apdu->u.presentResponse->referenceId = pr->referenceId;
2836 new_apdu->u.presentResponse->records =
2837 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
2838 *new_apdu->u.presentResponse->presentStatus =
2839 Z_PresentStatus_failure;
2841 send_to_client(new_apdu);
2845 else if (m_backend_type)
2847 pr->preferredRecordSyntax =
2848 yaz_string_to_oid_odr(yaz_oid_std(),
2849 CLASS_RECSYN, m_backend_type,
2852 if (m_backend_elementset)
2854 Z_ElementSetNames *esn = mk_esn_from_schema(odr_encode(),
2855 m_backend_elementset);
2856 Z_RecordComposition *comp = (Z_RecordComposition *)
2857 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
2858 comp->which = Z_RecordComp_simple;
2859 comp->u.simple = esn;
2860 pr->recordComposition = comp;
2866 Z_ElementSetNames *Yaz_Proxy::mk_esn_from_schema(ODR o, const char *schema)
2870 Z_ElementSetNames *esn = (Z_ElementSetNames *)
2871 odr_malloc(o, sizeof(Z_ElementSetNames));
2872 esn->which = Z_ElementSetNames_generic;
2873 esn->u.generic = odr_strdup(o, schema);
2877 void Yaz_Proxy::srw_get_client(const char *db, const char **backend_db)
2880 Yaz_ProxyConfig *cfg = check_reconfigure();
2882 t = cfg->get_explain_name(db, backend_db);
2884 if (m_client && m_default_target && t && strcmp(m_default_target, t))
2891 xfree(m_default_target);
2892 m_default_target = xstrdup(t);
2896 int Yaz_Proxy::file_access(Z_HTTP_Request *hreq)
2899 if (strcmp(hreq->method, "GET"))
2901 if (hreq->path[0] != '/')
2903 const char *cp = hreq->path;
2906 if (*cp == '/' && strchr("/.", cp[1]))
2911 Yaz_ProxyConfig *cfg = check_reconfigure();
2913 if (!cfg->get_file_access_info(hreq->path+1))
2916 const char *fname = hreq->path+1;
2917 if (stat(fname, &sbuf))
2919 yaz_log(YLOG_LOG|YLOG_ERRNO, "%sstat failed for %s", m_session_str,
2923 if ((sbuf.st_mode & S_IFMT) != S_IFREG)
2925 yaz_log(YLOG_LOG, "%sNot a regular file %s", m_session_str, fname);
2928 if (sbuf.st_size > (off_t) 1000000)
2930 yaz_log(YLOG_WARN, "%sFile %s too large for transfer", m_session_str,
2935 ODR o = odr_encode();
2937 const char *ctype = cfg->check_mime_type(fname);
2938 Z_GDU *gdu = z_get_HTTP_Response(o, 200);
2939 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
2941 hres->version = odr_strdup(o, m_http_version);
2942 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
2943 if (m_http_keepalive)
2944 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
2948 hres->content_len = sbuf.st_size;
2949 hres->content_buf = (char*) odr_malloc(o, hres->content_len);
2950 FILE *f = fopen(fname, "rb");
2953 fread(hres->content_buf, 1, hres->content_len, f);
2960 if (m_log_mask & PROXY_LOG_REQ_CLIENT)
2962 yaz_log(YLOG_LOG, "%sSending file %s to client", m_session_str,
2966 send_GDU(gdu, &len);
2967 recv_GDU_more(true);
2971 void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
2975 odr_destroy(m_s2z_odr_init);
2978 if (m_s2z_odr_search)
2980 odr_destroy(m_s2z_odr_search);
2981 m_s2z_odr_search = 0;
2985 odr_destroy(m_s2z_odr_scan);
2989 m_http_keepalive = 0;
2991 if (!strcmp(hreq->version, "1.0"))
2993 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
2994 if (v && !strcmp(v, "Keep-Alive"))
2995 m_http_keepalive = 1;
2997 m_http_keepalive = 0;
2998 m_http_version = "1.0";
3002 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
3003 if (v && !strcmp(v, "close"))
3004 m_http_keepalive = 0;
3006 m_http_keepalive = 1;
3007 m_http_version = "1.1";
3010 const char *a = z_HTTP_header_lookup(hreq->headers, "Authorization");
3011 char authorization_str[255];
3012 *authorization_str = '\0';
3013 if (a && strncasecmp(a, "Basic ", 6) == 0)
3014 base64_decode(a + 6, authorization_str, 254);
3016 Z_SRW_PDU *srw_pdu = 0;
3017 Z_SOAP *soap_package = 0;
3019 Z_SRW_diagnostic *diagnostic = 0;
3020 int num_diagnostic = 0;
3022 yaz_log(YLOG_LOG, "%s%s %s", m_session_str, hreq->method, hreq->path);
3024 if (file_access(hreq))
3028 else if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
3030 || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
3031 &charset, &diagnostic, &num_diagnostic) == 0)
3033 m_s2z_odr_init = odr_createmem(ODR_ENCODE);
3034 m_s2z_odr_search = odr_createmem(ODR_ENCODE);
3035 m_s2z_odr_scan = odr_createmem(ODR_ENCODE);
3036 m_soap_ns = odr_strdup(m_s2z_odr_search, soap_package->ns);
3037 m_s2z_init_apdu = 0;
3038 m_s2z_search_apdu = 0;
3039 m_s2z_present_apdu = 0;
3040 m_s2z_scan_apdu = 0;
3042 m_s2z_stylesheet = 0;
3044 Z_IdAuthentication *auth = NULL;
3045 if (srw_pdu->username && !srw_pdu->password)
3047 yaz_log(YLOG_LOG, "username: %s\n", srw_pdu->username);
3048 auth = (Z_IdAuthentication *) odr_malloc(m_s2z_odr_init, sizeof(Z_IdAuthentication));
3049 auth->which = Z_IdAuthentication_open;
3050 auth->u.open = odr_strdup(m_s2z_odr_init, srw_pdu->username);
3052 else if (srw_pdu->username && srw_pdu->password)
3054 yaz_log(YLOG_LOG, "username/password: %s/%s\n",
3055 srw_pdu->username, srw_pdu->password);
3056 auth = (Z_IdAuthentication *) odr_malloc(m_s2z_odr_init, sizeof(Z_IdAuthentication));
3057 auth->which = Z_IdAuthentication_idPass;
3058 auth->u.idPass = (Z_IdPass *) odr_malloc(m_s2z_odr_init, sizeof(Z_IdPass));
3059 auth->u.idPass->groupId = NULL;
3060 auth->u.idPass->password = odr_strdup(m_s2z_odr_init, srw_pdu->password);
3061 auth->u.idPass->userId = odr_strdup(m_s2z_odr_init, srw_pdu->username);
3065 if (*authorization_str)
3067 yaz_log(YLOG_LOG, "authorization_str present: %s\n", authorization_str);
3068 auth = (Z_IdAuthentication *) odr_malloc(m_s2z_odr_init, sizeof(Z_IdAuthentication));
3069 auth->which = Z_IdAuthentication_idPass;
3070 auth->u.idPass = (Z_IdPass *) odr_malloc(m_s2z_odr_init, sizeof(Z_IdPass));
3071 auth->u.idPass->groupId = NULL;
3072 char *p = strchr(authorization_str, ':');
3077 auth->u.idPass->password = odr_strdup(m_s2z_odr_init, p);
3079 auth->u.idPass->userId = odr_strdup(m_s2z_odr_init, authorization_str);
3083 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
3086 Z_SRW_searchRetrieveRequest *srw_req = srw_pdu->u.request;
3088 const char *backend_db = srw_req->database;
3089 srw_get_client(srw_req->database, &backend_db);
3091 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
3092 // recordXPath unsupported.
3093 if (srw_req->recordXPath)
3095 yaz_add_srw_diagnostic(odr_decode(),
3096 &diagnostic, &num_diagnostic,
3100 if (srw_req->sort_type != Z_SRW_sort_type_none)
3102 yaz_add_srw_diagnostic(odr_decode(),
3103 &diagnostic, &num_diagnostic,
3107 if (srw_req->stylesheet)
3109 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
3111 // set packing for response records ..
3112 if (srw_req->recordPacking &&
3113 !strcmp(srw_req->recordPacking, "xml"))
3114 m_s2z_packing = Z_SRW_recordPacking_XML;
3116 m_s2z_packing = Z_SRW_recordPacking_string;
3120 Z_SRW_PDU *srw_pdu =
3121 yaz_srw_get(odr_encode(),
3122 Z_SRW_searchRetrieve_response);
3123 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
3125 srw_res->diagnostics = diagnostic;
3126 srw_res->num_diagnostics = num_diagnostic;
3127 send_srw_response(srw_pdu);
3131 // prepare search PDU
3132 m_s2z_search_apdu = zget_APDU(m_s2z_odr_search,
3133 Z_APDU_searchRequest);
3134 Z_SearchRequest *z_searchRequest =
3135 m_s2z_search_apdu->u.searchRequest;
3137 z_searchRequest->num_databaseNames = 1;
3138 z_searchRequest->databaseNames = (char**)
3139 odr_malloc(m_s2z_odr_search, sizeof(char *));
3140 z_searchRequest->databaseNames[0] = odr_strdup(m_s2z_odr_search,
3143 // query transformation
3144 Z_Query *query = (Z_Query *)
3145 odr_malloc(m_s2z_odr_search, sizeof(Z_Query));
3146 z_searchRequest->query = query;
3148 if (srw_req->query_type == Z_SRW_query_type_cql)
3150 Z_External *ext = (Z_External *)
3151 odr_malloc(m_s2z_odr_search, sizeof(*ext));
3152 ext->direct_reference =
3153 odr_getoidbystr(m_s2z_odr_search, "1.2.840.10003.16.2");
3154 ext->indirect_reference = 0;
3155 ext->descriptor = 0;
3156 ext->which = Z_External_CQL;
3157 ext->u.cql = srw_req->query.cql;
3159 query->which = Z_Query_type_104;
3160 query->u.type_104 = ext;
3162 else if (srw_req->query_type == Z_SRW_query_type_pqf)
3164 Z_RPNQuery *RPNquery;
3165 YAZ_PQF_Parser pqf_parser;
3167 pqf_parser = yaz_pqf_create ();
3169 RPNquery = yaz_pqf_parse (pqf_parser, m_s2z_odr_search,
3170 srw_req->query.pqf);
3173 const char *pqf_msg;
3175 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
3177 yaz_log(YLOG_LOG, "%*s^\n", ioff+4, "");
3178 yaz_log(YLOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
3180 send_to_srw_client_error(10, 0);
3183 query->which = Z_Query_type_1;
3184 query->u.type_1 = RPNquery;
3186 yaz_pqf_destroy (pqf_parser);
3190 send_to_srw_client_error(7, "query");
3195 m_s2z_present_apdu = 0;
3197 if (srw_req->maximumRecords)
3198 max = *srw_req->maximumRecords;
3200 if (srw_req->startRecord)
3201 start = *srw_req->startRecord;
3204 // Some backend, such as Voyager doesn't honor piggyback
3205 // So we use present always (0 &&).
3206 if (0 && start <= 1) // Z39.50 piggyback
3208 *z_searchRequest->smallSetUpperBound = max;
3209 *z_searchRequest->mediumSetPresentNumber = max;
3210 *z_searchRequest->largeSetLowerBound = 2000000000; // 2e9
3212 z_searchRequest->preferredRecordSyntax =
3213 odr_oiddup(m_s2z_odr_search, yaz_oid_recsyn_xml);
3215 if (srw_req->recordSchema)
3217 z_searchRequest->smallSetElementSetNames =
3218 z_searchRequest->mediumSetElementSetNames =
3219 mk_esn_from_schema(m_s2z_odr_search,
3220 srw_req->recordSchema);
3223 else // Z39.50 present
3225 m_s2z_present_apdu = zget_APDU(m_s2z_odr_search,
3226 Z_APDU_presentRequest);
3227 Z_PresentRequest *z_presentRequest =
3228 m_s2z_present_apdu->u.presentRequest;
3229 *z_presentRequest->resultSetStartPoint = start;
3230 *z_presentRequest->numberOfRecordsRequested = max;
3232 z_presentRequest->preferredRecordSyntax =
3233 odr_oiddup(m_s2z_odr_search, yaz_oid_recsyn_xml);
3234 if (srw_req->recordSchema)
3236 z_presentRequest->recordComposition =
3237 (Z_RecordComposition *)
3238 odr_malloc(m_s2z_odr_search,
3239 sizeof(Z_RecordComposition));
3240 z_presentRequest->recordComposition->which =
3241 Z_RecordComp_simple;
3242 z_presentRequest->recordComposition->u.simple =
3243 mk_esn_from_schema(m_s2z_odr_search,
3244 srw_req->recordSchema);
3250 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
3251 Z_APDU_initRequest);
3253 ODR_MASK_SET(m_s2z_init_apdu->u.initRequest->options, Z_Options_scan);
3254 m_s2z_init_apdu->u.initRequest->idAuthentication = auth;
3256 // prevent m_initRequest_apdu memory from being grabbed
3257 // in Yaz_Proxy::handle_incoming_Z_PDU
3258 m_initRequest_apdu = m_s2z_init_apdu;
3259 handle_incoming_Z_PDU(m_s2z_init_apdu);
3264 handle_incoming_Z_PDU(m_s2z_search_apdu);
3268 else if (srw_pdu->which == Z_SRW_explain_request)
3270 Z_SRW_explainRequest *srw_req = srw_pdu->u.explain_request;
3272 const char *backend_db = srw_req->database;
3273 srw_get_client(srw_req->database, &backend_db);
3275 m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
3278 if (srw_req->stylesheet)
3280 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
3282 if (srw_req->recordPacking &&
3283 !strcmp(srw_req->recordPacking, "xml"))
3284 m_s2z_packing = Z_SRW_recordPacking_XML;
3286 m_s2z_packing = Z_SRW_recordPacking_string;
3290 send_srw_explain_response(diagnostic, num_diagnostic);
3296 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
3297 Z_APDU_initRequest);
3299 ODR_MASK_SET(m_s2z_init_apdu->u.initRequest->options, Z_Options_scan);
3300 m_s2z_init_apdu->u.initRequest->idAuthentication = auth;
3302 // prevent m_initRequest_apdu memory from being grabbed
3303 // in Yaz_Proxy::handle_incoming_Z_PDU
3304 m_initRequest_apdu = m_s2z_init_apdu;
3305 handle_incoming_Z_PDU(m_s2z_init_apdu);
3308 send_srw_explain_response(0, 0);
3311 else if (srw_pdu->which == Z_SRW_scan_request)
3313 Z_SRW_scanRequest *srw_req = srw_pdu->u.scan_request;
3315 const char *backend_db = srw_req->database;
3316 srw_get_client(srw_req->database, &backend_db);
3318 m_s2z_database = odr_strdup(m_s2z_odr_init,
3321 if (srw_req->stylesheet)
3323 odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
3326 m_s2z_scan_apdu = zget_APDU(m_s2z_odr_scan,
3327 Z_APDU_scanRequest);
3328 Z_ScanRequest *z_scanRequest =
3329 m_s2z_scan_apdu->u.scanRequest;
3331 z_scanRequest->num_databaseNames = 1;
3332 z_scanRequest->databaseNames = (char**)
3333 odr_malloc(m_s2z_odr_scan, sizeof(char *));
3334 z_scanRequest->databaseNames[0] = odr_strdup(m_s2z_odr_scan,
3337 // query transformation
3338 if (srw_req->query_type == Z_SRW_query_type_cql)
3340 z_scanRequest->termListAndStartPoint =
3341 (Z_AttributesPlusTerm *)odr_malloc(m_s2z_odr_scan, sizeof(Z_AttributesPlusTerm));
3342 z_scanRequest->termListAndStartPoint->attributes = NULL;
3343 z_scanRequest->termListAndStartPoint->term =
3344 (Z_Term *)odr_malloc(m_s2z_odr_scan, sizeof(Z_Term));
3345 z_scanRequest->termListAndStartPoint->term->which =
3346 Z_Term_characterString;
3347 z_scanRequest->termListAndStartPoint->term->u.characterString =
3348 odr_strdup(m_s2z_odr_scan, srw_req->scanClause.cql);
3351 if (srw_req->responsePosition)
3352 z_scanRequest->preferredPositionInResponse =
3353 odr_intdup(m_s2z_odr_scan, *srw_req->responsePosition);
3354 if (srw_req->maximumTerms)
3355 *z_scanRequest->numberOfTermsRequested = *srw_req->maximumTerms;
3359 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
3360 Z_APDU_initRequest);
3362 ODR_MASK_SET(m_s2z_init_apdu->u.initRequest->options, Z_Options_scan);
3363 m_s2z_init_apdu->u.initRequest->idAuthentication = auth;
3365 // prevent m_initRequest_apdu memory from being grabbed
3366 // in Yaz_Proxy::handle_incoming_Z_PDU
3367 m_initRequest_apdu = m_s2z_init_apdu;
3368 handle_incoming_Z_PDU(m_s2z_init_apdu);
3373 handle_incoming_Z_PDU(m_s2z_scan_apdu);
3381 send_to_srw_client_error(4, 0);
3384 send_http_response(400);
3387 void Yaz_Proxy::handle_init(Z_APDU *apdu)
3390 Z_OtherInformation **oi;
3391 get_otherInfoAPDU(apdu, &oi);
3393 if (apdu->u.initRequest->implementationId)
3394 yaz_log(YLOG_LOG, "%simplementationId: %s",
3395 m_session_str, apdu->u.initRequest->implementationId);
3396 if (apdu->u.initRequest->implementationName)
3397 yaz_log(YLOG_LOG, "%simplementationName: %s",
3398 m_session_str, apdu->u.initRequest->implementationName);
3399 if (apdu->u.initRequest->implementationVersion)
3400 yaz_log(YLOG_LOG, "%simplementationVersion: %s",
3401 m_session_str, apdu->u.initRequest->implementationVersion);
3402 if (m_initRequest_apdu == 0)
3404 if (m_initRequest_mem)
3405 nmem_destroy(m_initRequest_mem);
3407 m_initRequest_apdu = apdu;
3408 m_initRequest_mem = odr_extract_mem(odr_decode());
3410 m_initRequest_preferredMessageSize = *apdu->u.initRequest->
3411 preferredMessageSize;
3412 *apdu->u.initRequest->preferredMessageSize = 64*1024*1024;
3413 m_initRequest_maximumRecordSize = *apdu->u.initRequest->
3415 *apdu->u.initRequest->maximumRecordSize = 64*1024*1024;
3417 Z_CharSetandLanguageNegotiation *charSetandLangRecord =
3418 yaz_get_charneg_record(*oi);
3420 // Save proposal charsets and langs.
3421 if (ODR_MASK_GET(apdu->u.initRequest->options,
3422 Z_Options_negotiationModel)
3423 && charSetandLangRecord)
3426 yaz_get_proposal_charneg(m_referenceId_mem,
3427 charSetandLangRecord,
3428 &m_initRequest_oi_negotiation_charsets,
3429 &m_initRequest_oi_negotiation_num_charsets,
3430 &m_initRequest_oi_negotiation_langs,
3431 &m_initRequest_oi_negotiation_num_langs,
3432 &m_initRequest_oi_negotiation_selected);
3434 for (int i = 0; i<m_initRequest_oi_negotiation_num_charsets; i++)
3436 yaz_log(YLOG_LOG, "%scharacters set proposal: %s",
3437 m_session_str,(m_initRequest_oi_negotiation_charsets[i])?
3438 m_initRequest_oi_negotiation_charsets[i]:"none");
3440 for (int i=0; i<m_initRequest_oi_negotiation_num_langs; i++)
3442 yaz_log(YLOG_LOG, "%slanguages proposal: %s",
3443 m_session_str, (m_initRequest_oi_negotiation_langs[i])?
3444 m_initRequest_oi_negotiation_langs[i]:"none");
3446 yaz_log(YLOG_LOG, "%sselected proposal: %d (boolean)",
3447 m_session_str, m_initRequest_oi_negotiation_selected);
3449 // save init options for the response..
3450 m_initRequest_options = apdu->u.initRequest->options;
3452 apdu->u.initRequest->options =
3453 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
3454 sizeof(Odr_bitmask));
3455 ODR_MASK_ZERO(apdu->u.initRequest->options);
3457 for (i = 0; i<= 24; i++)
3458 ODR_MASK_SET(apdu->u.initRequest->options, i);
3459 // check negotiation option
3460 if (!ODR_MASK_GET(m_initRequest_options,
3461 Z_Options_negotiationModel))
3463 ODR_MASK_CLEAR(apdu->u.initRequest->options,
3464 Z_Options_negotiationModel);
3466 ODR_MASK_CLEAR(apdu->u.initRequest->options,
3467 Z_Options_concurrentOperations);
3469 m_initRequest_version = apdu->u.initRequest->protocolVersion;
3470 apdu->u.initRequest->protocolVersion =
3471 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
3472 sizeof(Odr_bitmask));
3473 ODR_MASK_ZERO(apdu->u.initRequest->protocolVersion);
3475 for (i = 0; i<= 8; i++)
3476 ODR_MASK_SET(apdu->u.initRequest->protocolVersion, i);
3478 handle_charset_lang_negotiation(apdu);
3479 if (m_client->m_init_flag)
3481 if (handle_init_response_for_invalid_session(apdu))
3483 if (m_client->m_initResponse)
3485 Z_APDU *apdu2 = m_client->m_initResponse;
3486 apdu2->u.initResponse->otherInfo = 0;
3487 if (m_client->m_cookie && *m_client->m_cookie)
3488 set_otherInformationString(apdu2, yaz_oid_userinfo_cookie,
3489 1, m_client->m_cookie);
3490 apdu2->u.initResponse->referenceId =
3491 apdu->u.initRequest->referenceId;
3492 apdu2->u.initResponse->options = m_client->m_initResponse_options;
3493 apdu2->u.initResponse->protocolVersion =
3494 m_client->m_initResponse_version;
3496 handle_charset_lang_negotiation(apdu2);
3498 if (m_timeout_mode == timeout_busy)
3499 m_timeout_mode = timeout_normal;
3500 send_to_client(apdu2);
3504 m_client->m_init_flag = 1;
3506 if (m_num_msg_threads && m_my_thread)
3508 Auth_Msg *m = new Auth_Msg;
3510 z_APDU(odr_encode(), &apdu, 0, "encode");
3511 char *apdu_buf = odr_getbuf(odr_encode(), &m->m_apdu_len, 0);
3512 m->m_apdu_buf = (char*) nmem_malloc(m->m_nmem, m->m_apdu_len);
3513 memcpy(m->m_apdu_buf, apdu_buf, m->m_apdu_len);
3514 odr_reset(odr_encode());
3517 m_my_thread->put(m);
3521 int ret = handle_authentication(apdu);
3522 result_authentication(apdu, ret);
3526 void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
3528 Z_ReferenceId **refid = get_referenceIdP(apdu);
3529 nmem_reset(m_referenceId_mem);
3530 if (refid && *refid)
3532 m_referenceId = (Z_ReferenceId *)
3533 nmem_malloc(m_referenceId_mem, sizeof(*m_referenceId));
3534 m_referenceId->len = m_referenceId->size = (*refid)->len;
3535 m_referenceId->buf = (unsigned char *)
3536 nmem_malloc(m_referenceId_mem, (*refid)->len);
3537 memcpy(m_referenceId->buf, (*refid)->buf, (*refid)->len);
3542 if (!m_client && m_flag_invalid_session)
3544 // Got request for a session that is invalid..
3545 m_apdu_invalid_session = apdu; // save package
3546 m_mem_invalid_session = odr_extract_mem(odr_decode());
3547 apdu = m_initRequest_apdu; // but throw an init to the target
3550 if (apdu->which == Z_APDU_searchRequest)
3551 m_search_stat.add_bytes(1);
3553 // Handle global authentication
3554 if (!handle_global_authentication(apdu))
3557 { // HTTP. Send unauthorized
3558 send_http_response(401);
3563 // Z39.50 just shutdown
3570 // Determine our client.
3571 Z_OtherInformation **oi;
3572 get_otherInfoAPDU(apdu, &oi);
3573 int http_code = 404;
3574 m_client = get_client(apdu, get_cookie(oi), get_proxy(oi), &http_code);
3578 { // HTTP. Send not found
3579 send_http_response(http_code);
3584 // Z39.50 just shutdown
3590 m_client->m_server = this;
3592 if (apdu->which == Z_APDU_initRequest)
3595 handle_incoming_Z_PDU_2(apdu);
3598 void Yaz_Proxy::handle_incoming_Z_PDU_2(Z_APDU *apdu)
3600 handle_max_record_retrieve(apdu);
3603 apdu = handle_syntax_validation(apdu);
3606 apdu = handle_query_transformation(apdu);
3609 apdu = handle_target_charset_conversion(apdu);
3612 apdu = handle_query_validation(apdu);
3615 apdu = result_set_optimize(apdu);
3619 m_client->timeout(m_target_idletime); // mark it active even
3620 recv_GDU_more(true);
3621 // though we didn't use it
3625 // delete other info construct completely if 0 elements
3626 Z_OtherInformation **oi;
3627 get_otherInfoAPDU(apdu, &oi);
3628 if (oi && *oi && (*oi)->num_elements == 0)
3631 if (apdu->which == Z_APDU_presentRequest &&
3632 m_client->m_resultSetStartPoint == 0)
3634 Z_PresentRequest *pr = apdu->u.presentRequest;
3635 m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
3636 m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
3638 m_client->m_resultSetStartPoint = 0;
3640 if (m_client->send_to_target(apdu) < 0)
3642 m_client->shutdown();
3645 m_client->m_waiting = 1;
3648 void Yaz_Proxy::connectNotify()
3652 void Yaz_Proxy::releaseClient()
3654 xfree(m_proxyTarget);
3656 m_flag_invalid_session = 0;
3657 // only keep if keep_alive flag is set...
3659 m_client->m_pdu_recv < m_keepalive_limit_pdu &&
3660 m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
3661 m_client->m_waiting == 0)
3663 yaz_log(YLOG_LOG, "%sShutdown (client to proxy) keepalive %s",
3665 m_client->get_hostname());
3666 yaz_log(YLOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
3667 m_session_str, m_client->m_pdu_recv,
3668 m_client->m_bytes_sent + m_client->m_bytes_recv,
3669 m_keepalive_limit_bw, m_keepalive_limit_pdu);
3670 assert (m_client->m_waiting != 2);
3671 // Tell client (if any) that no server connection is there..
3672 m_client->m_server = 0;
3677 yaz_log(YLOG_LOG, "%sShutdown (client to proxy) close %s",
3679 m_client->get_hostname());
3680 assert (m_client->m_waiting != 2);
3686 yaz_log(YLOG_LOG, "%sshutdown (client to proxy) bad state",
3692 yaz_log(YLOG_LOG, "%sShutdown (client to proxy)",
3696 m_parent->pre_init();
3699 bool Yaz_Proxy::dec_ref()
3701 m_http_keepalive = 0;
3704 if (m_ref_count > 0)
3713 const char *Yaz_ProxyClient::get_session_str()
3717 return m_server->get_session_str();
3720 void Yaz_ProxyClient::shutdown()
3722 yaz_log(YLOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
3727 m_waiting = 1; // ensure it's released from Yaz_Proxy::releaseClient
3728 m_server->dec_ref();
3734 void Yaz_Proxy::failNotify()
3737 yaz_log(YLOG_LOG, "%sConnection closed by client", get_session_str());
3741 void Yaz_Proxy::send_response_fail_client(const char *addr)
3745 Z_SRW_diagnostic *diagnostic = 0;
3746 int num_diagnostic = 0;
3748 yaz_add_srw_diagnostic(odr_encode(),
3749 &diagnostic, &num_diagnostic,
3750 YAZ_SRW_SYSTEM_TEMPORARILY_UNAVAILABLE, addr);
3751 if (m_s2z_search_apdu)
3752 send_srw_search_response(diagnostic, num_diagnostic);
3753 else if (m_s2z_scan_apdu)
3754 send_srw_scan_response(diagnostic, num_diagnostic);
3756 send_srw_explain_response(diagnostic, num_diagnostic);
3760 void Yaz_ProxyClient::failNotify()
3763 m_server->inc_request_no();
3764 yaz_log(YLOG_LOG, "%sConnection closed by target %s",
3765 get_session_str(), get_hostname());
3768 m_server->send_response_fail_client(get_hostname());
3772 void Yaz_ProxyClient::connectNotify()
3774 const char *s = get_session_str();
3775 const char *h = get_hostname();
3776 yaz_log(YLOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
3778 timeout(m_target_idletime);
3783 IPDU_Observer *Yaz_ProxyClient::sessionNotify(IPDU_Observable
3784 *the_PDU_Observable, int fd)
3786 return new Yaz_ProxyClient(the_PDU_Observable, 0);
3789 Yaz_ProxyClient::~Yaz_ProxyClient()
3794 m_next->m_prev = m_prev;
3795 m_waiting = 2; // for debugging purposes only.
3796 odr_destroy(m_init_odr);
3797 odr_destroy(m_idAuthentication_odr);
3798 delete m_last_query;
3799 xfree(m_last_resultSetId);
3803 void Yaz_ProxyClient::pre_init_client()
3805 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
3806 Z_InitRequest *req = apdu->u.initRequest;
3809 for (i = 0; i<= 24; i++)
3810 ODR_MASK_SET(req->options, i);
3811 ODR_MASK_CLEAR(apdu->u.initRequest->options,
3812 Z_Options_negotiationModel);
3813 ODR_MASK_CLEAR(apdu->u.initRequest->options,
3814 Z_Options_concurrentOperations);
3815 for (i = 0; i<= 10; i++)
3816 ODR_MASK_SET(req->protocolVersion, i);
3818 if (send_to_target(apdu) < 0)
3829 void Yaz_Proxy::pre_init()
3832 const char *name = 0;
3833 const char *zurl_in_use[MAX_ZURL_PLEX];
3834 int limit_bw, limit_pdu, limit_req, limit_search;
3835 int target_idletime, client_idletime;
3836 int max_sockets = m_max_sockets;
3838 int keepalive_limit_bw, keepalive_limit_pdu;
3840 const char *cql2rpn = 0;
3841 const char *authentication = 0;
3842 const char *negotiation_charset = 0;
3843 const char *negotiation_lang = 0;
3845 Yaz_ProxyConfig *cfg = check_reconfigure();
3849 if (m_log_mask & PROXY_LOG_APDU_CLIENT)
3854 for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
3855 &limit_bw, &limit_pdu, &limit_req,
3857 &target_idletime, &client_idletime,
3860 &keepalive_limit_bw,
3861 &keepalive_limit_pdu,
3865 &negotiation_charset,
3873 for (j = 0; zurl_in_use[j]; j++)
3877 int spare_waiting = 0;
3880 for (c = m_clientPool; c; c = c->m_next)
3882 if (!strcmp(zurl_in_use[j], c->get_hostname()))
3884 if (c->m_cookie == 0)
3886 if (c->m_server == 0)
3898 yaz_log(YLOG_LOG, "%spre-init %s %s use=%d other=%d spare=%d "
3899 "sparew=%d preinit=%d",m_session_str,
3900 name, zurl_in_use[j], in_use, other,
3901 spare, spare_waiting, pre_init);
3902 if (spare + spare_waiting < pre_init
3903 && in_use + spare + spare_waiting + other < max_sockets)
3905 c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
3906 c->m_next = m_clientPool;
3908 c->m_next->m_prev = &c->m_next;
3910 c->m_prev = &m_clientPool;
3912 if (m_log_mask & PROXY_LOG_APDU_SERVER)
3913 c->set_APDU_yazlog(1);
3915 c->set_APDU_yazlog(0);
3917 if (c->client(zurl_in_use[j]))
3925 c->m_target_idletime = target_idletime;
3926 c->m_seqno = m_seqno++;
3933 void Yaz_Proxy::timeoutNotify()
3938 switch(m_timeout_mode)
3940 case timeout_normal:
3944 yaz_log(YLOG_LOG, "%sTimeout (client to proxy)", m_session_str);
3947 case timeout_reduce:
3948 timeout(m_client_idletime);
3949 m_timeout_mode = timeout_busy;
3950 gdu = m_timeout_gdu;
3952 recv_GDU_normal(gdu);
3955 assert(m_stylesheet_nprl);
3956 convert_xsl_delay();
3957 recv_GDU_more(true);
3967 void Yaz_Proxy::markInvalid()
3970 m_flag_invalid_session = 1;
3973 void Yaz_ProxyClient::timeoutNotify()
3976 m_server->inc_request_no();
3978 yaz_log(YLOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
3982 m_server->send_response_fail_client(get_hostname());
3984 Yaz_Proxy *proxy_root = m_root;
3988 proxy_root->pre_init();
3991 Yaz_ProxyClient::Yaz_ProxyClient(IPDU_Observable *the_PDU_Observable,
3992 Yaz_Proxy *parent) :
3993 Z_Assoc (the_PDU_Observable)
4000 m_last_resultSetId = 0;
4001 m_last_resultCount = 0;
4005 m_init_odr = odr_createmem (ODR_DECODE);
4007 m_initResponse_options = 0;
4008 m_initResponse_version = 0;
4009 m_initResponse_preferredMessageSize = 0;
4010 m_initResponse_maximumRecordSize = 0;
4011 m_resultSetStartPoint = 0;
4012 m_bytes_sent = m_bytes_recv = 0;
4016 m_target_idletime = 600;
4018 m_idAuthentication_odr = odr_createmem(ODR_ENCODE);
4019 m_idAuthentication_ber_buf = 0;
4020 m_idAuthentication_ber_size = 0;
4023 const char *Yaz_Proxy::option(const char *name, const char *value)
4025 if (!strcmp (name, "optimize")) {
4028 m_optimize = xstrdup(value);
4035 void Yaz_ProxyClient::recv_HTTP_response(Z_HTTP_Response *apdu, int len)
4040 void Yaz_ProxyClient::recv_GDU(Z_GDU *apdu, int len)
4042 if (apdu->which == Z_GDU_Z3950)
4043 recv_Z_PDU(apdu->u.z3950, len);
4044 else if (apdu->which == Z_GDU_HTTP_Response)
4045 recv_HTTP_response(apdu->u.HTTP_Response, len);
4050 int Yaz_Proxy::handle_init_response_for_invalid_session(Z_APDU *apdu)
4052 if (!m_flag_invalid_session)
4054 m_flag_invalid_session = 0;
4055 handle_incoming_Z_PDU(m_apdu_invalid_session);
4056 assert (m_mem_invalid_session);
4057 nmem_destroy(m_mem_invalid_session);
4058 m_mem_invalid_session = 0;
4062 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
4064 m_bytes_recv += len;
4068 if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
4069 yaz_log(YLOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
4070 apdu_name(apdu), get_hostname(), len);
4071 if (apdu->which == Z_APDU_initResponse)
4073 if (!m_server) // if this is a pre init session , check for more
4075 NMEM nmem = odr_extract_mem (odr_decode());
4076 odr_reset (m_init_odr);
4077 nmem_transfer (m_init_odr->mem, nmem);
4078 m_initResponse = apdu;
4079 m_initResponse_options = apdu->u.initResponse->options;
4080 m_initResponse_version = apdu->u.initResponse->protocolVersion;
4081 m_initResponse_preferredMessageSize =
4082 *apdu->u.initResponse->preferredMessageSize;
4083 m_initResponse_maximumRecordSize =
4084 *apdu->u.initResponse->maximumRecordSize;
4086 Z_InitResponse *ir = apdu->u.initResponse;
4088 // apply YAZ Proxy version
4089 char *imv0 = ir->implementationVersion;
4090 char *imv1 = (char*)
4091 odr_malloc(m_init_odr, 20 + (imv0 ? strlen(imv0) : 0));
4096 strcat(imv1, "/" VERSION);
4098 ir->implementationVersion = imv1;
4100 // apply YAZ Proxy implementation name
4101 char *im0 = ir->implementationName;
4103 odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
4110 strcat(im1, "(YAZ Proxy)");
4111 ir->implementationName = im1;
4113 nmem_destroy (nmem);
4115 if (m_server && m_server->handle_init_response_for_invalid_session(apdu))
4118 if (apdu->which == Z_APDU_searchResponse)
4120 Z_SearchResponse *sr = apdu->u.searchResponse;
4121 m_last_resultCount = *sr->resultCount;
4122 int status = *sr->searchStatus;
4123 if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
4127 if (sr->records && sr->records->which == Z_Records_DBOSD)
4129 m_cache.add(odr_decode(),
4130 sr->records->u.databaseOrSurDiagnostics, 1,
4135 if (apdu->which == Z_APDU_presentResponse)
4137 Z_PresentResponse *pr = apdu->u.presentResponse;
4141 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
4142 Z_SearchResponse *sr = new_apdu->u.searchResponse;
4143 sr->referenceId = pr->referenceId;
4144 *sr->resultCount = m_last_resultCount;
4145 sr->records = pr->records;
4146 sr->nextResultSetPosition = pr->nextResultSetPosition;
4147 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
4151 pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
4153 m_cache.add(odr_decode(),
4154 pr->records->u.databaseOrSurDiagnostics,
4155 m_resultSetStartPoint, -1);
4156 m_resultSetStartPoint = 0;
4160 set_otherInformationString(apdu, yaz_oid_userinfo_cookie, 1, m_cookie);
4162 Yaz_Proxy *server = m_server; // save it. send_to_client may destroy us
4165 server->send_to_client(apdu);
4166 if (apdu->which == Z_APDU_close)
4169 server->recv_GDU_more(true);
4172 void Yaz_Proxy::low_socket_close()
4177 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
4178 if (m_lo_fd[i] >= 0)
4179 ::close(m_lo_fd[i]);
4183 void Yaz_Proxy::low_socket_open()
4188 for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
4189 m_lo_fd[i] = open("/dev/null", O_RDONLY);
4193 int Yaz_Proxy::server(const char *addr)
4195 int r = Z_Assoc::server(addr);
4198 yaz_log(YLOG_LOG, "%sStarted proxy "
4202 " on %s", m_session_str, addr);
4208 void Yaz_Proxy::base64_decode(const char *base64, char *buf, int buf_len)
4210 const char *base64_chars =
4211 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
4212 int len = strlen(base64);
4216 for (int pos = 0; pos <= len; pos++)
4218 if (base64[pos] == '=' || buf_pos + 1 >= buf_len)
4221 const char *ch_ptr = strchr(base64_chars, base64[pos]);
4224 char ch = (char) (ch_ptr - base64_chars);
4228 buf[buf_pos] = ch << 2;
4231 buf[buf_pos++] += (ch & 0x30) >> 4;
4232 buf[buf_pos] = (ch & 0x0f) << 4;
4235 buf[buf_pos++] += (ch & 0x3c) >> 2;
4236 buf[buf_pos] = (ch & 0x03) << 6;
4239 buf[buf_pos++] += ch;
4246 buf[buf_pos] = '\0';
4252 * c-file-style: "Stroustrup"
4253 * indent-tabs-mode: nil
4255 * vim: shiftwidth=4 tabstop=8 expandtab