2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.39 2003-06-09 22:20:39 adam Exp $
12 #include <yaz++/proxy.h>
14 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
15 Yaz_Z_Assoc(the_PDU_Observable)
17 m_PDU_Observable = the_PDU_Observable;
24 m_proxy_authentication = 0;
28 m_optimize = xstrdup ("1");
31 Yaz_Proxy::~Yaz_Proxy()
33 xfree (m_proxyTarget);
34 xfree (m_proxy_authentication);
38 void Yaz_Proxy::set_proxy_target(const char *target)
40 xfree (m_proxyTarget);
43 m_proxyTarget = (char *) xstrdup (target);
46 void Yaz_Proxy::set_proxy_authentication (const char *auth)
48 xfree (m_proxy_authentication);
49 m_proxy_authentication = 0;
51 m_proxy_authentication = (char *) xstrdup (auth);
54 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
55 *the_PDU_Observable, int fd)
57 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
58 new_proxy->m_parent = this;
59 new_proxy->timeout(m_idletime);
60 new_proxy->set_proxy_target(m_proxyTarget);
61 new_proxy->set_APDU_log(get_APDU_log());
62 new_proxy->set_proxy_authentication(m_proxy_authentication);
63 yaz_log (LOG_LOG, "New session p=%p", new_proxy);
67 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
70 Z_OtherInformationUnit *oi;
72 ent.proto = PROTO_Z3950;
73 ent.oclass = CLASS_USERINFO;
74 ent.value = (oid_value) VAL_COOKIE;
75 assert (oid_ent_to_oid (&ent, oid));
77 if (oid_ent_to_oid (&ent, oid) &&
78 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
79 oi->which == Z_OtherInfo_characterInfo)
80 return oi->information.characterInfo;
84 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
87 Z_OtherInformationUnit *oi;
89 ent.proto = PROTO_Z3950;
90 ent.oclass = CLASS_USERINFO;
91 ent.value = (oid_value) VAL_PROXY;
92 if (oid_ent_to_oid (&ent, oid) &&
93 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
94 oi->which == Z_OtherInfo_characterInfo)
95 return oi->information.characterInfo;
99 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
102 Yaz_Proxy *parent = m_parent;
103 Z_OtherInformation **oi;
104 Yaz_ProxyClient *c = m_client;
106 get_otherInfoAPDU(apdu, &oi);
107 char *cookie = get_cookie(oi);
109 const char *proxy_host = get_proxy(oi);
111 set_proxy_target(proxy_host);
113 // no target specified at all?
117 if (!strcmp(m_proxyTarget, "stop"))
119 if (cookie && *cookie)
121 Yaz_ProxyClient *cc = 0;
123 for (c = parent->m_clientPool; c; c = c->m_next)
126 assert (*c->m_prev == c);
127 if (!strcmp(cookie,c->m_cookie) &&
128 !strcmp(m_proxyTarget, c->get_hostname()))
137 // The following handles "cancel"
138 // If connection is busy (waiting for PDU) and
139 // we have an initRequest we can safely do re-open
140 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
142 yaz_log (LOG_LOG, "reopen target=%s", c->get_hostname());
144 c->client(m_proxyTarget);
148 c->m_last_resultCount = 0;
149 c->m_sr_transform = 0;
151 c->timeout(m_idletime);
153 c->m_seqno = parent->m_seqno;
154 if (c->m_server && c->m_server != this)
155 c->m_server->m_client = 0;
157 c->m_seqno = parent->m_seqno;
159 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
165 Yaz_ProxyClient *cc = 0;
167 for (c = parent->m_clientPool; c; c = c->m_next)
170 assert (*c->m_prev == c);
171 if (c->m_server == 0 && c->m_cookie[0] == 0 &&
172 !strcmp(m_proxyTarget, c->get_hostname()))
182 c->m_seqno = parent->m_seqno;
183 assert(c->m_server == 0);
185 c->m_seqno = parent->m_seqno;
187 yaz_log (LOG_LOG, "get_client 10 %p %p", this, c);
193 if (apdu->which != Z_APDU_initRequest)
195 yaz_log (LOG_LOG, "no first INIT!");
198 Z_InitRequest *initRequest = apdu->u.initRequest;
200 if (!initRequest->idAuthentication)
202 if (m_proxy_authentication)
204 initRequest->idAuthentication =
205 (Z_IdAuthentication *)
206 odr_malloc (odr_encode(),
207 sizeof(*initRequest->idAuthentication));
208 initRequest->idAuthentication->which =
209 Z_IdAuthentication_open;
210 initRequest->idAuthentication->u.open =
211 odr_strdup (odr_encode(), m_proxy_authentication);
215 // go through list of clients - and find the lowest/oldest one.
216 Yaz_ProxyClient *c_min = 0;
218 int no_of_clients = 0;
219 yaz_log (LOG_LOG, "Existing sessions");
220 for (c = parent->m_clientPool; c; c = c->m_next)
222 yaz_log (LOG_LOG, " Session %-3d wait=%d %s", c->m_seqno,
223 c->m_waiting, c->get_hostname());
225 if (min_seq < 0 || c->m_seqno < min_seq)
227 min_seq = c->m_seqno;
231 if (no_of_clients >= parent->m_max_clients)
234 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
236 yaz_log (LOG_LOG, "Replace session %d",
238 if (c->m_server && c->m_server != this)
244 yaz_log (LOG_LOG, "Reuse session %d to %d",
245 c->m_seqno, parent->m_seqno);
247 strcpy (c->m_cookie, cookie);
249 c->m_cookie[0] = '\0';
250 c->m_seqno = parent->m_seqno;
251 if (c->m_server && c->m_server != this)
253 c->m_server->m_client = 0;
257 yaz_log (LOG_DEBUG, "get_client 2 %p %p", this, c);
263 yaz_log (LOG_LOG, "Making session %d %s", parent->m_seqno,
265 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
266 c->m_next = parent->m_clientPool;
268 c->m_next->m_prev = &c->m_next;
269 parent->m_clientPool = c;
270 c->m_prev = &parent->m_clientPool;
273 strcpy (c->m_cookie, cookie);
275 c->m_cookie[0] = '\0';
276 yaz_log (LOG_LOG, "Connecting to %s", m_proxyTarget);
277 c->m_seqno = parent->m_seqno;
278 c->client(m_proxyTarget);
280 c->m_last_resultCount = 0;
282 c->m_sr_transform = 0;
288 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
292 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
294 if (apdu->which != Z_APDU_searchRequest)
296 if (*m_parent->m_optimize == '0')
297 return apdu; // don't optimize result sets..
298 Z_SearchRequest *sr = apdu->u.searchRequest;
299 Yaz_Z_Query *this_query = new Yaz_Z_Query;
300 Yaz_Z_Databases this_databases;
302 this_databases.set(sr->num_databaseNames, (const char **)
305 this_query->set_Z_Query(sr->query);
307 if (m_client->m_last_ok && m_client->m_last_query &&
308 m_client->m_last_query->match(this_query) &&
309 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
310 m_client->m_last_databases.match(this_databases))
313 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
314 m_client->m_last_resultCount < *sr->largeSetLowerBound)
317 // send present request (medium size)
318 yaz_log (LOG_LOG, "Optimizing search for medium set");
319 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
320 Z_PresentRequest *pr = new_apdu->u.presentRequest;
321 pr->referenceId = sr->referenceId;
322 pr->resultSetId = sr->resultSetName;
323 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
324 if (*sr->mediumSetPresentNumber < m_client->m_last_resultCount)
325 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
327 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
328 if (sr->mediumSetElementSetNames)
330 pr->recordComposition = (Z_RecordComposition *)
331 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
332 pr->recordComposition->which = Z_RecordComp_simple;
333 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
335 m_client->m_sr_transform = 1;
338 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
339 m_client->m_last_resultCount <= 0)
341 // large set. Return pseudo-search response immediately
342 yaz_log (LOG_LOG, "Optimizing search for large set");
343 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
344 new_apdu->u.searchResponse->referenceId = sr->referenceId;
345 new_apdu->u.searchResponse->resultCount =
346 &m_client->m_last_resultCount;
347 send_Z_PDU(new_apdu);
353 // send a present request (small set)
354 yaz_log (LOG_LOG, "Optimizing search for small set");
355 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
356 Z_PresentRequest *pr = new_apdu->u.presentRequest;
357 pr->referenceId = sr->referenceId;
358 pr->resultSetId = sr->resultSetName;
359 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
360 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
361 if (sr->smallSetElementSetNames)
363 pr->recordComposition = (Z_RecordComposition *)
364 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
365 pr->recordComposition->which = Z_RecordComp_simple;
366 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
368 m_client->m_sr_transform = 1;
374 delete m_client->m_last_query;
375 m_client->m_last_query = this_query;
376 m_client->m_last_ok = 0;
378 xfree (m_client->m_last_resultSetId);
379 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
381 m_client->m_last_databases.set(sr->num_databaseNames,
382 (const char **) sr->databaseNames);
387 static const char *apdu_name(Z_APDU *apdu)
391 case Z_APDU_initRequest:
392 return "initRequest";
393 case Z_APDU_initResponse:
394 return "initResponse";
395 case Z_APDU_searchRequest:
396 return "searchRequest";
397 case Z_APDU_searchResponse:
398 return "searchResponse";
399 case Z_APDU_presentRequest:
400 return "presentRequest";
401 case Z_APDU_presentResponse:
402 return "presentResponse";
407 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
409 yaz_log (LOG_LOG, "Receiving %s from client", apdu_name(apdu));
410 // Determine our client.
411 m_client = get_client(apdu);
417 m_client->m_server = this;
419 if (apdu->which == Z_APDU_initRequest)
421 if (m_client->m_init_flag)
423 Z_APDU *apdu = m_client->m_initResponse;
424 apdu->u.initResponse->otherInfo = 0;
425 if (m_client->m_cookie)
426 set_otherInformationString(apdu, VAL_COOKIE, 1,
431 m_client->m_init_flag = 1;
433 apdu = result_set_optimize(apdu);
437 yaz_log (LOG_LOG, "Sending %s to %s",
438 apdu_name(apdu), m_client->get_hostname());
440 // delete other info part from PDU before sending to target
441 Z_OtherInformation **oi;
442 get_otherInfoAPDU(apdu, &oi);
446 if (m_client->send_Z_PDU(apdu) < 0)
453 m_client->m_waiting = 1;
456 void Yaz_Proxy::connectNotify()
460 void Yaz_Proxy::shutdown()
462 // only keep if keep_alive flag is set...
463 if (m_keepalive && m_client)
465 yaz_log (LOG_LOG, "shutdown (client to proxy) keepalive %s",
466 m_client->get_hostname());
467 assert (m_client->m_waiting != 2);
468 // Tell client (if any) that no server connection is there..
469 m_client->m_server = 0;
473 yaz_log (LOG_LOG, "shutdown (client to proxy) close %s",
474 m_client->get_hostname());
475 assert (m_client->m_waiting != 2);
480 yaz_log (LOG_LOG, "shutdown (client to proxy) bad state");
485 yaz_log (LOG_LOG, "shutdown (client to proxy)");
490 void Yaz_ProxyClient::shutdown()
492 yaz_log (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
497 void Yaz_Proxy::failNotify()
499 yaz_log (LOG_LOG, "Yaz_Proxy connection closed by client");
503 void Yaz_ProxyClient::failNotify()
505 yaz_log (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
509 void Yaz_ProxyClient::connectNotify()
511 yaz_log (LOG_LOG, "Connection accepted by %s", get_hostname());
515 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
516 *the_PDU_Observable, int fd)
518 return new Yaz_ProxyClient(the_PDU_Observable);
521 Yaz_ProxyClient::~Yaz_ProxyClient()
526 m_next->m_prev = m_prev;
527 m_waiting = 2; // for debugging purposes only.
528 odr_destroy(m_init_odr);
530 xfree (m_last_resultSetId);
533 void Yaz_Proxy::timeoutNotify()
535 yaz_log (LOG_LOG, "timeout (client to proxy)");
539 void Yaz_ProxyClient::timeoutNotify()
541 yaz_log (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
545 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
546 Yaz_Z_Assoc (the_PDU_Observable)
553 m_last_resultSetId = 0;
554 m_last_resultCount = 0;
558 m_init_odr = odr_createmem (ODR_DECODE);
562 const char *Yaz_Proxy::option(const char *name, const char *value)
564 if (!strcmp (name, "optimize")) {
567 m_optimize = xstrdup (value);
574 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
577 yaz_log (LOG_LOG, "Receiving %s from %s", apdu_name(apdu),
579 if (apdu->which == Z_APDU_initResponse)
581 NMEM nmem = odr_extract_mem (odr_decode());
582 odr_reset (m_init_odr);
583 nmem_transfer (m_init_odr->mem, nmem);
584 m_initResponse = apdu;
587 if (apdu->which == Z_APDU_searchResponse)
589 m_last_resultCount = *apdu->u.searchResponse->resultCount;
590 int status = *apdu->u.searchResponse->searchStatus;
592 (!apdu->u.searchResponse->records ||
593 apdu->u.searchResponse->records->which == Z_Records_DBOSD))
596 if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
599 Z_PresentResponse *pr = apdu->u.presentResponse;
600 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
601 Z_SearchResponse *sr = new_apdu->u.searchResponse;
602 sr->referenceId = pr->referenceId;
603 *sr->resultCount = m_last_resultCount;
604 sr->records = pr->records;
605 sr->nextResultSetPosition = pr->nextResultSetPosition;
606 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
609 if (m_cookie && *m_cookie)
610 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
613 yaz_log (LOG_LOG, "Sending %s to client", apdu_name(apdu));
614 m_server->send_Z_PDU(apdu);