Fix: proxy doesn't cache searches/result sets when errors occur.
[yazpp-moved-to-github.git] / src / yaz-proxy.cpp
1 /*
2  * Copyright (c) 1998-2001, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-proxy.cpp,v 1.31 2002-01-14 12:01:28 adam Exp $
6  */
7
8 #include <assert.h>
9 #include <time.h>
10
11 #include <yaz/log.h>
12 #include <yaz++/yaz-proxy.h>
13
14 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
15     Yaz_Z_Assoc(the_PDU_Observable)
16 {
17     m_PDU_Observable = the_PDU_Observable;
18     m_client = 0;
19     m_parent = 0;
20     m_clientPool = 0;
21     m_seqno = 1;
22     m_keepalive = 1;
23     m_proxyTarget = 0;
24     m_proxy_authentication = 0;
25     m_max_clients = 50;
26     m_seed = time(0);
27     m_optimize = xstrdup ("1");
28 }
29
30 Yaz_Proxy::~Yaz_Proxy()
31 {
32     xfree (m_proxyTarget);
33     xfree (m_proxy_authentication);
34     xfree (m_optimize);
35 }
36
37 void Yaz_Proxy::set_proxy_target(const char *target)
38 {
39     xfree (m_proxyTarget);
40     m_proxyTarget = 0;
41     if (target)
42         m_proxyTarget = (char *) xstrdup (target);
43 }
44
45 void Yaz_Proxy::set_proxy_authentication (const char *auth)
46 {
47     xfree (m_proxy_authentication);
48     m_proxy_authentication = 0;
49     if (auth)
50         m_proxy_authentication = (char *) xstrdup (auth);
51 }
52
53 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
54                                             *the_PDU_Observable, int fd)
55 {
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     return new_proxy;
63 }
64
65 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
66 {
67     int oid[OID_SIZE];
68     Z_OtherInformationUnit *oi;
69     struct oident ent;
70     ent.proto = PROTO_Z3950;
71     ent.oclass = CLASS_USERINFO;
72     ent.value = (oid_value) VAL_COOKIE;
73     assert (oid_ent_to_oid (&ent, oid));
74
75     if (oid_ent_to_oid (&ent, oid) && 
76         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
77         oi->which == Z_OtherInfo_characterInfo)
78         return oi->information.characterInfo;
79     return 0;
80 }
81
82 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
83 {
84     int oid[OID_SIZE];
85     Z_OtherInformationUnit *oi;
86     struct oident ent;
87     ent.proto = PROTO_Z3950;
88     ent.oclass = CLASS_USERINFO;
89     ent.value = (oid_value) VAL_PROXY;
90     if (oid_ent_to_oid (&ent, oid) &&
91         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
92         oi->which == Z_OtherInfo_characterInfo)
93         return oi->information.characterInfo;
94     return 0;
95 }
96
97 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
98 {
99     assert (m_parent);
100     Yaz_Proxy *parent = m_parent;
101     Z_OtherInformation **oi;
102     Yaz_ProxyClient *c = m_client;
103     
104     get_otherInfoAPDU(apdu, &oi);
105     char *cookie = get_cookie(oi);
106
107     const char *proxy_host = get_proxy(oi);
108     if (proxy_host)
109         set_proxy_target(proxy_host);
110     
111     // no target specified at all?
112     if (!m_proxyTarget)
113         return 0;
114
115     if (!strcmp(m_proxyTarget, "stop"))
116         exit (0);
117     if (cookie && *cookie)
118     {
119         Yaz_ProxyClient *cc = 0;
120         
121         for (c = parent->m_clientPool; c; c = c->m_next)
122         {
123             assert (c->m_prev);
124             assert (*c->m_prev == c);
125             if (!strcmp(cookie,c->m_cookie) &&
126                 !strcmp(m_proxyTarget, c->get_hostname()))
127             {
128                 cc = c;
129             }
130         }
131         if (cc)
132         {
133             // found it in cache
134             c = cc;
135             // The following handles "cancel"
136             // If connection is busy (waiting for PDU) and
137             // we have an initRequest we can safely do re-open
138             if (c->m_waiting && apdu->which == Z_APDU_initRequest)
139             {
140                 yaz_log (LOG_LOG, "reopen target=%s", c->get_hostname());
141                 c->close();
142                 c->client(m_proxyTarget);
143                 c->m_init_flag = 0;
144
145                 c->m_last_ok = 0;
146                 c->m_last_resultCount = 0;
147                 c->m_sr_transform = 0;
148                 c->m_waiting = 0;
149                 c->timeout(600); 
150             }
151             c->m_seqno = parent->m_seqno;
152             if (c->m_server && c->m_server != this)
153                 c->m_server->m_client = 0;
154             c->m_server = this;
155             c->m_seqno = parent->m_seqno;
156             (parent->m_seqno)++;
157             yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
158             return c;
159         }
160     }
161     if (!m_client)
162     {
163         if (apdu->which != Z_APDU_initRequest)
164         {
165             yaz_log (LOG_LOG, "no first INIT!");
166             return 0;
167         }
168         yaz_log (LOG_LOG, "got InitRequest");
169         Z_InitRequest *initRequest = apdu->u.initRequest;
170
171         if (!initRequest->idAuthentication)
172         {
173             if (m_proxy_authentication)
174             {
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);
183             }
184         }
185
186         // go through list of clients - and find the lowest/oldest one.
187         Yaz_ProxyClient *c_min = 0;
188         int min_seq = -1;
189         int no_of_clients = 0;
190         for (c = parent->m_clientPool; c; c = c->m_next)
191         {
192             no_of_clients++;
193             if (min_seq < 0 || c->m_seqno < min_seq)
194             {
195                 min_seq = c->m_seqno;
196                 c_min = c;
197             }
198         }
199         if (no_of_clients >= parent->m_max_clients)
200         {
201             c = c_min;
202             if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
203             {
204                 yaz_log (LOG_LOG, "Yaz_Proxy::get_client re-init session %d",
205                       c->m_seqno);
206                 if (c->m_server && c->m_server != this)
207                     delete c->m_server;
208                 c->m_server = 0;
209             }
210             else
211             {
212                 yaz_log (LOG_LOG,
213                          "Yaz_Proxy::get_client re-use session %d to %d",
214                       c->m_seqno, parent->m_seqno);
215                 if (cookie)
216                     strcpy (c->m_cookie, cookie);
217                 else
218                     c->m_cookie[0] = '\0';
219                 c->m_seqno = parent->m_seqno;
220                 if (c->m_server && c->m_server != this)
221                 {
222                     c->m_server->m_client = 0;
223                     delete c->m_server;
224                 }
225                 (parent->m_seqno)++;
226                 yaz_log (LOG_DEBUG, "get_client 2 %p %p", this, c);
227                 return c;
228             }
229         }
230         else
231         {
232             yaz_log (LOG_LOG, "Yaz_Proxy::get_client making session %d",
233                   parent->m_seqno);
234             c = new Yaz_ProxyClient(m_PDU_Observable->clone());
235             c->m_next = parent->m_clientPool;
236             if (c->m_next)
237                 c->m_next->m_prev = &c->m_next;
238             parent->m_clientPool = c;
239             c->m_prev = &parent->m_clientPool;
240         }
241         if (cookie)
242             strcpy (c->m_cookie, cookie);
243         else
244             c->m_cookie[0] = '\0';
245         yaz_log (LOG_LOG, "Yaz_Proxy::get_client connect to %s",
246                  m_proxyTarget);
247         c->m_seqno = parent->m_seqno;
248         c->client(m_proxyTarget);
249         c->m_init_flag = 0;
250         c->m_last_resultCount = 0;
251         c->m_last_ok = 0;
252         c->m_sr_transform = 0;
253         c->m_waiting = 0;
254         c->timeout(10);
255
256         (parent->m_seqno)++;
257     }
258     yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
259     return c;
260 }
261
262 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
263 {
264     if (apdu->which != Z_APDU_searchRequest)
265         return apdu;
266     if (*m_parent->m_optimize != '1')
267         return apdu;
268     Z_SearchRequest *sr = apdu->u.searchRequest;
269     Yaz_Z_Query *this_query = new Yaz_Z_Query;
270     Yaz_Z_Databases this_databases;
271
272     this_databases.set(sr->num_databaseNames, (const char **)
273                        sr->databaseNames);
274     
275     this_query->set_Z_Query(sr->query);
276     
277     if (m_client->m_last_ok && m_client->m_last_query &&
278         m_client->m_last_query->match(this_query) &&
279         m_client->m_last_databases.match(this_databases))
280     {
281         delete this_query;
282         if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
283             m_client->m_last_resultCount < *sr->largeSetLowerBound)
284         {
285             // medium Set
286             // send present request (medium size)
287             yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize medium set");
288             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
289             Z_PresentRequest *pr = new_apdu->u.presentRequest;
290             pr->referenceId = sr->referenceId;
291             pr->resultSetId = sr->resultSetName;
292             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
293             *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
294             if (sr->mediumSetElementSetNames)
295             {
296                 pr->recordComposition = (Z_RecordComposition *)
297                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
298                 pr->recordComposition->which = Z_RecordComp_simple;
299                 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
300             }
301             m_client->m_sr_transform = 1;
302             return new_apdu;
303         }
304         else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
305             m_client->m_last_resultCount == 0)
306         {
307             // large set. Return pseudo-search response immediately
308             yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize large set");
309             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
310             new_apdu->u.searchResponse->referenceId = sr->referenceId;
311             new_apdu->u.searchResponse->resultCount =
312                 &m_client->m_last_resultCount;
313             send_Z_PDU(new_apdu);
314             return 0;
315         }
316         else
317         {
318             // small set
319             // send a present request (small set)
320             yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize small set");
321             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
322             Z_PresentRequest *pr = new_apdu->u.presentRequest;
323             pr->referenceId = sr->referenceId;
324             pr->resultSetId = sr->resultSetName;
325             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
326             *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
327             if (sr->smallSetElementSetNames)
328             {
329                 pr->recordComposition = (Z_RecordComposition *)
330                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
331                 pr->recordComposition->which = Z_RecordComp_simple;
332                 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
333             }
334             m_client->m_sr_transform = 1;
335             return new_apdu;
336         }
337     }
338     else
339     {
340         yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize new set");
341         delete m_client->m_last_query;
342         m_client->m_last_query = this_query;
343         m_client->m_last_ok = 0;
344         m_client->m_last_databases.set(sr->num_databaseNames,
345                                        (const char **) sr->databaseNames);
346     }
347     return apdu;
348 }
349
350 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
351 {
352     yaz_log (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
353     // Determine our client.
354     m_client = get_client(apdu);
355     if (!m_client)
356     {
357         delete this;
358         return;
359     }
360     m_client->m_server = this;
361
362     if (apdu->which == Z_APDU_initRequest)
363     {
364         if (m_client->m_init_flag)
365         {
366             Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
367             if (m_client->m_cookie)
368                 set_otherInformationString(apdu, VAL_COOKIE, 1,
369                                            m_client->m_cookie);
370             send_Z_PDU(apdu);
371             return;
372         }
373         m_client->m_init_flag = 1;
374     }
375     apdu = result_set_optimize(apdu);
376     if (!apdu)
377         return;
378
379     yaz_log (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU %s",
380              m_client->get_hostname());
381
382     // delete other info part from PDU before sending to target
383     Z_OtherInformation **oi;
384     get_otherInfoAPDU(apdu, &oi);
385     if (oi)
386         *oi = 0;
387
388     if (m_client->send_Z_PDU(apdu) < 0)
389     {
390         delete m_client;
391         m_client = 0;
392         delete this;
393     }
394     else
395         m_client->m_waiting = 1;
396 }
397
398 void Yaz_Proxy::connectNotify()
399 {
400 }
401
402 void Yaz_Proxy::shutdown()
403 {
404     // only keep if keep_alive flag and cookie is set...
405     if (m_keepalive && m_client && m_client->m_cookie[0])
406     {
407         if (m_client->m_waiting == 2)
408             abort();
409         // Tell client (if any) that no server connection is there..
410         m_client->m_server = 0;
411         yaz_log (LOG_LOG, "shutdown (client to proxy) keepalive %s", m_client->get_hostname());
412     }
413     else if (m_client)
414     {
415         if (m_client->m_waiting == 2)
416             abort();
417         yaz_log (LOG_LOG, "shutdown (client to proxy) close %s", m_client->get_hostname());
418         delete m_client;
419     }
420     else if (!m_parent)
421     {
422         yaz_log (LOG_LOG, "shutdown (client to proxy) bad state");
423         abort();
424     }
425     else 
426     {
427         yaz_log (LOG_LOG, "shutdown (client to proxy)");
428     }
429     delete this;
430 }
431
432 void Yaz_ProxyClient::shutdown()
433 {
434     yaz_log (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
435     delete m_server;
436     delete this;
437 }
438
439 void Yaz_Proxy::failNotify()
440 {
441     yaz_log (LOG_LOG, "Yaz_Proxy connection closed by client");
442     shutdown();
443 }
444
445 void Yaz_ProxyClient::failNotify()
446 {
447     yaz_log (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
448     shutdown();
449 }
450
451 void Yaz_ProxyClient::connectNotify()
452 {
453     yaz_log (LOG_LOG, "Yaz_ProxyClient connection accepted by %s",
454           get_hostname());
455     timeout(600);
456 }
457
458 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
459                                                   *the_PDU_Observable, int fd)
460 {
461     return new Yaz_ProxyClient(the_PDU_Observable);
462 }
463
464 Yaz_ProxyClient::~Yaz_ProxyClient()
465 {
466     if (m_prev)
467         *m_prev = m_next;
468     if (m_next)
469         m_next->m_prev = m_prev;
470     m_waiting = 2;     // for debugging purposes only.
471     delete m_last_query;
472 }
473
474 void Yaz_Proxy::timeoutNotify()
475 {
476     yaz_log (LOG_LOG, "timeout (client to proxy)");
477     shutdown();
478 }
479
480 void Yaz_ProxyClient::timeoutNotify()
481 {
482     yaz_log (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
483     shutdown();
484 }
485
486 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
487     Yaz_Z_Assoc (the_PDU_Observable)
488 {
489     m_cookie[0] = 0;
490     m_next = 0;
491     m_prev = 0;
492     m_init_flag = 0;
493     m_last_query = 0;
494     m_last_resultCount = 0;
495     m_last_ok = 0;
496     m_sr_transform = 0;
497     m_waiting = 0;
498 }
499
500 const char *Yaz_Proxy::option(const char *name, const char *value)
501 {
502     if (!strcmp (name, "optimize")) {
503         if (value) {
504             xfree (m_optimize); 
505             m_optimize = xstrdup (value);
506         }
507         return m_optimize;
508     }
509     return 0;
510 }
511
512 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
513 {
514     m_waiting = 0;
515     yaz_log (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU %s", get_hostname());
516     if (apdu->which == Z_APDU_searchResponse)
517     {
518         m_last_resultCount = *apdu->u.searchResponse->resultCount;
519         int status = *apdu->u.searchResponse->searchStatus;
520         if (status && 
521                 (!apdu->u.searchResponse->records ||
522                  apdu->u.searchResponse->records->which == Z_Records_DBOSD))
523             m_last_ok = 1;
524     }
525     if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
526     {
527         m_sr_transform = 0;
528         Z_PresentResponse *pr = apdu->u.presentResponse;
529         Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
530         Z_SearchResponse *sr = new_apdu->u.searchResponse;
531         sr->referenceId = pr->referenceId;
532         *sr->resultCount = m_last_resultCount;
533         sr->records = pr->records;
534         sr->nextResultSetPosition = pr->nextResultSetPosition;
535         sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
536         apdu = new_apdu;
537     }
538     if (m_cookie && *m_cookie)
539         set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
540     if (m_server)
541     {
542         yaz_log (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
543         m_server->send_Z_PDU(apdu);
544     }
545 }