2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.36 2002-10-09 12:50:26 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;
27 m_optimize = xstrdup ("1");
30 Yaz_Proxy::~Yaz_Proxy()
32 xfree (m_proxyTarget);
33 xfree (m_proxy_authentication);
37 void Yaz_Proxy::set_proxy_target(const char *target)
39 xfree (m_proxyTarget);
42 m_proxyTarget = (char *) xstrdup (target);
45 void Yaz_Proxy::set_proxy_authentication (const char *auth)
47 xfree (m_proxy_authentication);
48 m_proxy_authentication = 0;
50 m_proxy_authentication = (char *) xstrdup (auth);
53 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
54 *the_PDU_Observable, int fd)
56 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
57 new_proxy->m_parent = this;
58 new_proxy->timeout(500);
59 new_proxy->set_proxy_target(m_proxyTarget);
60 new_proxy->set_APDU_log(get_APDU_log());
61 new_proxy->set_proxy_authentication(m_proxy_authentication);
62 yaz_log (LOG_LOG, "New session p=%p", new_proxy);
66 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
69 Z_OtherInformationUnit *oi;
71 ent.proto = PROTO_Z3950;
72 ent.oclass = CLASS_USERINFO;
73 ent.value = (oid_value) VAL_COOKIE;
74 assert (oid_ent_to_oid (&ent, oid));
76 if (oid_ent_to_oid (&ent, oid) &&
77 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
78 oi->which == Z_OtherInfo_characterInfo)
79 return oi->information.characterInfo;
83 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
86 Z_OtherInformationUnit *oi;
88 ent.proto = PROTO_Z3950;
89 ent.oclass = CLASS_USERINFO;
90 ent.value = (oid_value) VAL_PROXY;
91 if (oid_ent_to_oid (&ent, oid) &&
92 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
93 oi->which == Z_OtherInfo_characterInfo)
94 return oi->information.characterInfo;
98 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
101 Yaz_Proxy *parent = m_parent;
102 Z_OtherInformation **oi;
103 Yaz_ProxyClient *c = m_client;
105 get_otherInfoAPDU(apdu, &oi);
106 char *cookie = get_cookie(oi);
108 const char *proxy_host = get_proxy(oi);
110 set_proxy_target(proxy_host);
112 // no target specified at all?
116 if (!strcmp(m_proxyTarget, "stop"))
118 if (cookie && *cookie)
120 Yaz_ProxyClient *cc = 0;
122 for (c = parent->m_clientPool; c; c = c->m_next)
125 assert (*c->m_prev == c);
126 if (!strcmp(cookie,c->m_cookie) &&
127 !strcmp(m_proxyTarget, c->get_hostname()))
136 // The following handles "cancel"
137 // If connection is busy (waiting for PDU) and
138 // we have an initRequest we can safely do re-open
139 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
141 yaz_log (LOG_LOG, "reopen target=%s", c->get_hostname());
143 c->client(m_proxyTarget);
147 c->m_last_resultCount = 0;
148 c->m_sr_transform = 0;
152 c->m_seqno = parent->m_seqno;
153 if (c->m_server && c->m_server != this)
154 c->m_server->m_client = 0;
156 c->m_seqno = parent->m_seqno;
158 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
164 if (apdu->which != Z_APDU_initRequest)
166 yaz_log (LOG_LOG, "no first INIT!");
169 Z_InitRequest *initRequest = apdu->u.initRequest;
171 if (!initRequest->idAuthentication)
173 if (m_proxy_authentication)
175 initRequest->idAuthentication =
176 (Z_IdAuthentication *)
177 odr_malloc (odr_encode(),
178 sizeof(*initRequest->idAuthentication));
179 initRequest->idAuthentication->which =
180 Z_IdAuthentication_open;
181 initRequest->idAuthentication->u.open =
182 odr_strdup (odr_encode(), m_proxy_authentication);
186 // go through list of clients - and find the lowest/oldest one.
187 Yaz_ProxyClient *c_min = 0;
189 int no_of_clients = 0;
190 yaz_log (LOG_LOG, "Existing sessions");
191 for (c = parent->m_clientPool; c; c = c->m_next)
193 yaz_log (LOG_LOG, " Session %-3d wait=%d %s", c->m_seqno,
194 c->m_waiting, c->get_hostname());
196 if (min_seq < 0 || c->m_seqno < min_seq)
198 min_seq = c->m_seqno;
202 if (no_of_clients >= parent->m_max_clients)
205 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
207 yaz_log (LOG_LOG, "Replace session %d",
209 if (c->m_server && c->m_server != this)
215 yaz_log (LOG_LOG, "Reuse session %d to %d",
216 c->m_seqno, parent->m_seqno);
218 strcpy (c->m_cookie, cookie);
220 c->m_cookie[0] = '\0';
221 c->m_seqno = parent->m_seqno;
222 if (c->m_server && c->m_server != this)
224 c->m_server->m_client = 0;
228 yaz_log (LOG_DEBUG, "get_client 2 %p %p", this, c);
234 yaz_log (LOG_LOG, "Making session %d %s", parent->m_seqno,
236 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
237 c->m_next = parent->m_clientPool;
239 c->m_next->m_prev = &c->m_next;
240 parent->m_clientPool = c;
241 c->m_prev = &parent->m_clientPool;
244 strcpy (c->m_cookie, cookie);
246 c->m_cookie[0] = '\0';
247 yaz_log (LOG_LOG, "Connecting to %s", m_proxyTarget);
248 c->m_seqno = parent->m_seqno;
249 c->client(m_proxyTarget);
251 c->m_last_resultCount = 0;
253 c->m_sr_transform = 0;
259 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
263 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
265 if (apdu->which != Z_APDU_searchRequest)
267 if (*m_parent->m_optimize != '1')
269 Z_SearchRequest *sr = apdu->u.searchRequest;
270 Yaz_Z_Query *this_query = new Yaz_Z_Query;
271 Yaz_Z_Databases this_databases;
273 this_databases.set(sr->num_databaseNames, (const char **)
276 this_query->set_Z_Query(sr->query);
278 if (m_client->m_last_ok && m_client->m_last_query &&
279 m_client->m_last_query->match(this_query) &&
280 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
281 m_client->m_last_databases.match(this_databases))
284 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
285 m_client->m_last_resultCount < *sr->largeSetLowerBound)
288 // send present request (medium size)
289 yaz_log (LOG_LOG, "Optimizing search for medium set");
290 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
291 Z_PresentRequest *pr = new_apdu->u.presentRequest;
292 pr->referenceId = sr->referenceId;
293 pr->resultSetId = sr->resultSetName;
294 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
295 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
296 if (sr->mediumSetElementSetNames)
298 pr->recordComposition = (Z_RecordComposition *)
299 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
300 pr->recordComposition->which = Z_RecordComp_simple;
301 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
303 m_client->m_sr_transform = 1;
306 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
307 m_client->m_last_resultCount <= 0)
309 // large set. Return pseudo-search response immediately
310 yaz_log (LOG_LOG, "Optimizing search for large set");
311 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
312 new_apdu->u.searchResponse->referenceId = sr->referenceId;
313 new_apdu->u.searchResponse->resultCount =
314 &m_client->m_last_resultCount;
315 send_Z_PDU(new_apdu);
321 // send a present request (small set)
322 yaz_log (LOG_LOG, "Optimizing search for small set");
323 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
324 Z_PresentRequest *pr = new_apdu->u.presentRequest;
325 pr->referenceId = sr->referenceId;
326 pr->resultSetId = sr->resultSetName;
327 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
328 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
329 if (sr->smallSetElementSetNames)
331 pr->recordComposition = (Z_RecordComposition *)
332 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
333 pr->recordComposition->which = Z_RecordComp_simple;
334 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
336 m_client->m_sr_transform = 1;
342 delete m_client->m_last_query;
343 m_client->m_last_query = this_query;
344 m_client->m_last_ok = 0;
346 xfree (m_client->m_last_resultSetId);
347 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
349 m_client->m_last_databases.set(sr->num_databaseNames,
350 (const char **) sr->databaseNames);
355 static const char *apdu_name(Z_APDU *apdu)
359 case Z_APDU_initRequest:
360 return "initRequest";
361 case Z_APDU_initResponse:
362 return "initResponse";
363 case Z_APDU_searchRequest:
364 return "searchRequest";
365 case Z_APDU_searchResponse:
366 return "searchResponse";
367 case Z_APDU_presentRequest:
368 return "presentRequest";
369 case Z_APDU_presentResponse:
370 return "presentResponse";
375 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
377 yaz_log (LOG_LOG, "Receiving %s from client", apdu_name(apdu));
378 // Determine our client.
379 m_client = get_client(apdu);
385 m_client->m_server = this;
387 if (apdu->which == Z_APDU_initRequest)
389 if (m_client->m_init_flag)
391 Z_APDU *apdu = m_client->m_initResponse;
392 apdu->u.initResponse->otherInfo = 0;
393 if (m_client->m_cookie)
394 set_otherInformationString(apdu, VAL_COOKIE, 1,
399 m_client->m_init_flag = 1;
401 apdu = result_set_optimize(apdu);
405 yaz_log (LOG_LOG, "Sending %s to %s",
406 apdu_name(apdu), m_client->get_hostname());
408 // delete other info part from PDU before sending to target
409 Z_OtherInformation **oi;
410 get_otherInfoAPDU(apdu, &oi);
414 if (m_client->send_Z_PDU(apdu) < 0)
421 m_client->m_waiting = 1;
424 void Yaz_Proxy::connectNotify()
428 void Yaz_Proxy::shutdown()
430 // only keep if keep_alive flag and cookie is set...
431 if (m_keepalive && m_client && m_client->m_cookie[0])
433 if (m_client->m_waiting == 2)
435 // Tell client (if any) that no server connection is there..
436 m_client->m_server = 0;
437 yaz_log (LOG_LOG, "shutdown (client to proxy) keepalive %s", m_client->get_hostname());
441 if (m_client->m_waiting == 2)
443 yaz_log (LOG_LOG, "shutdown (client to proxy) close %s", m_client->get_hostname());
448 yaz_log (LOG_LOG, "shutdown (client to proxy) bad state");
453 yaz_log (LOG_LOG, "shutdown (client to proxy)");
458 void Yaz_ProxyClient::shutdown()
460 yaz_log (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
465 void Yaz_Proxy::failNotify()
467 yaz_log (LOG_LOG, "Yaz_Proxy connection closed by client");
471 void Yaz_ProxyClient::failNotify()
473 yaz_log (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
477 void Yaz_ProxyClient::connectNotify()
479 yaz_log (LOG_LOG, "Connection accepted by %s", get_hostname());
483 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
484 *the_PDU_Observable, int fd)
486 return new Yaz_ProxyClient(the_PDU_Observable);
489 Yaz_ProxyClient::~Yaz_ProxyClient()
494 m_next->m_prev = m_prev;
495 m_waiting = 2; // for debugging purposes only.
496 odr_destroy(m_init_odr);
498 xfree (m_last_resultSetId);
501 void Yaz_Proxy::timeoutNotify()
503 yaz_log (LOG_LOG, "timeout (client to proxy)");
507 void Yaz_ProxyClient::timeoutNotify()
509 yaz_log (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
513 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
514 Yaz_Z_Assoc (the_PDU_Observable)
521 m_last_resultSetId = 0;
522 m_last_resultCount = 0;
526 m_init_odr = odr_createmem (ODR_DECODE);
530 const char *Yaz_Proxy::option(const char *name, const char *value)
532 if (!strcmp (name, "optimize")) {
535 m_optimize = xstrdup (value);
542 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
545 yaz_log (LOG_LOG, "Receiving %s from %s", apdu_name(apdu),
547 if (apdu->which == Z_APDU_initResponse)
549 NMEM nmem = odr_extract_mem (odr_decode());
550 odr_reset (m_init_odr);
551 nmem_transfer (m_init_odr->mem, nmem);
552 m_initResponse = apdu;
555 if (apdu->which == Z_APDU_searchResponse)
557 m_last_resultCount = *apdu->u.searchResponse->resultCount;
558 int status = *apdu->u.searchResponse->searchStatus;
560 (!apdu->u.searchResponse->records ||
561 apdu->u.searchResponse->records->which == Z_Records_DBOSD))
564 if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
567 Z_PresentResponse *pr = apdu->u.presentResponse;
568 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
569 Z_SearchResponse *sr = new_apdu->u.searchResponse;
570 sr->referenceId = pr->referenceId;
571 *sr->resultCount = m_last_resultCount;
572 sr->records = pr->records;
573 sr->nextResultSetPosition = pr->nextResultSetPosition;
574 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
577 if (m_cookie && *m_cookie)
578 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
581 yaz_log (LOG_LOG, "Sending %s to client", apdu_name(apdu));
582 m_server->send_Z_PDU(apdu);