Record cache. syntax used for matching. Compspec is not (yet)
[yazpp-moved-to-github.git] / src / yaz-proxy.cpp
1 /*
2  * Copyright (c) 1998-2003, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-proxy.cpp,v 1.44 2003-07-18 13:27:20 adam Exp $
6  */
7
8 #include <assert.h>
9 #include <time.h>
10
11 #include <yaz/log.h>
12 #include <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_idletime = 600;
28     m_optimize = xstrdup ("1");
29 }
30
31 Yaz_Proxy::~Yaz_Proxy()
32 {
33     xfree (m_proxyTarget);
34     xfree (m_proxy_authentication);
35     xfree (m_optimize);
36 }
37
38 void Yaz_Proxy::set_proxy_target(const char *target)
39 {
40     xfree (m_proxyTarget);
41     m_proxyTarget = 0;
42     if (target)
43         m_proxyTarget = (char *) xstrdup (target);
44 }
45
46 void Yaz_Proxy::set_proxy_authentication (const char *auth)
47 {
48     xfree (m_proxy_authentication);
49     m_proxy_authentication = 0;
50     if (auth)
51         m_proxy_authentication = (char *) xstrdup (auth);
52 }
53
54 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
55                                             *the_PDU_Observable, int fd)
56 {
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);
64     return new_proxy;
65 }
66
67 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
68 {
69     int oid[OID_SIZE];
70     Z_OtherInformationUnit *oi;
71     struct oident ent;
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));
76
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;
81     return 0;
82 }
83
84 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
85 {
86     int oid[OID_SIZE];
87     Z_OtherInformationUnit *oi;
88     struct oident ent;
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;
96     return 0;
97 }
98
99 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
100 {
101     assert (m_parent);
102     Yaz_Proxy *parent = m_parent;
103     Z_OtherInformation **oi;
104     Yaz_ProxyClient *c = m_client;
105     
106     get_otherInfoAPDU(apdu, &oi);
107     char *cookie = get_cookie(oi);
108
109     const char *proxy_host = get_proxy(oi);
110     if (proxy_host)
111         set_proxy_target(proxy_host);
112     
113     // no target specified at all?
114     if (!m_proxyTarget)
115         return 0;
116
117     if (!strcmp(m_proxyTarget, "stop"))
118         exit (0);
119     if (cookie && *cookie)
120     {
121         Yaz_ProxyClient *cc = 0;
122         
123         for (c = parent->m_clientPool; c; c = c->m_next)
124         {
125             assert (c->m_prev);
126             assert (*c->m_prev == c);
127             if (!strcmp(cookie,c->m_cookie) &&
128                 !strcmp(m_proxyTarget, c->get_hostname()))
129             {
130                 cc = c;
131             }
132         }
133         if (cc)
134         {
135             // found it in cache
136             c = cc;
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)
141             {
142                 yaz_log (LOG_LOG, "reopen target=%s", c->get_hostname());
143                 c->close();
144                 c->client(m_proxyTarget);
145                 c->m_init_flag = 0;
146
147                 c->m_last_ok = 0;
148                 c->m_cache.clear();
149                 c->m_last_resultCount = 0;
150                 c->m_sr_transform = 0;
151                 c->m_waiting = 0;
152                 c->m_resultSetStartPoint = 0;
153                 c->timeout(m_idletime); 
154             }
155             c->m_seqno = parent->m_seqno;
156             if (c->m_server && c->m_server != this)
157                 c->m_server->m_client = 0;
158             c->m_server = this;
159             (parent->m_seqno)++;
160             yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
161             return c;
162         }
163     }
164     else if (!c)
165     {
166         Yaz_ProxyClient *cc = 0;
167         
168         for (c = parent->m_clientPool; c; c = c->m_next)
169         {
170             assert (c->m_prev);
171             assert (*c->m_prev == c);
172             if (c->m_server == 0 && c->m_cookie[0] == 0 && 
173                 !strcmp(m_proxyTarget, c->get_hostname()))
174             {
175                 cc = c;
176             }
177         }
178         if (cc)
179         {
180             // found it in cache
181             c = cc;
182
183             yaz_log (LOG_LOG, "Reuse session %d to %d %s",
184                      c->m_seqno, parent->m_seqno, c->get_hostname());
185
186             c->m_seqno = parent->m_seqno;
187             assert(c->m_server == 0);
188             c->m_server = this;
189             
190             (parent->m_seqno)++;
191             return c;
192         }
193     }
194     if (!m_client)
195     {
196         if (apdu->which != Z_APDU_initRequest)
197         {
198             yaz_log (LOG_LOG, "no first INIT!");
199             return 0;
200         }
201         Z_InitRequest *initRequest = apdu->u.initRequest;
202
203         if (!initRequest->idAuthentication)
204         {
205             if (m_proxy_authentication)
206             {
207                 initRequest->idAuthentication =
208                     (Z_IdAuthentication *)
209                     odr_malloc (odr_encode(),
210                                 sizeof(*initRequest->idAuthentication));
211                 initRequest->idAuthentication->which =
212                     Z_IdAuthentication_open;
213                 initRequest->idAuthentication->u.open =
214                     odr_strdup (odr_encode(), m_proxy_authentication);
215             }
216         }
217
218         // go through list of clients - and find the lowest/oldest one.
219         Yaz_ProxyClient *c_min = 0;
220         int min_seq = -1;
221         int no_of_clients = 0;
222         if (parent->m_clientPool)
223             yaz_log (LOG_LOG, "Existing sessions");
224         for (c = parent->m_clientPool; c; c = c->m_next)
225         {
226             yaz_log (LOG_LOG, " Session %-3d wait=%d %s", c->m_seqno,
227                                c->m_waiting, c->get_hostname());
228             no_of_clients++;
229             if (min_seq < 0 || c->m_seqno < min_seq)
230             {
231                 min_seq = c->m_seqno;
232                 c_min = c;
233             }
234         }
235         if (no_of_clients >= parent->m_max_clients)
236         {
237             c = c_min;
238             if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
239             {
240                 yaz_log (LOG_LOG, "Replace session %d",
241                       c->m_seqno);
242                 if (c->m_server && c->m_server != this)
243                     delete c->m_server;
244                 c->m_server = 0;
245             }
246             else
247             {
248                 yaz_log (LOG_LOG, "Move session %d to %d %s",
249                       c->m_seqno, parent->m_seqno, c->get_hostname());
250                 if (cookie)
251                     strcpy (c->m_cookie, cookie);
252                 else
253                     c->m_cookie[0] = '\0';
254                 c->m_seqno = parent->m_seqno;
255                 if (c->m_server && c->m_server != this)
256                 {
257                     c->m_server->m_client = 0;
258                     delete c->m_server;
259                 }
260                 (parent->m_seqno)++;
261                 yaz_log (LOG_DEBUG, "get_client 2 %p %p", this, c);
262                 return c;
263             }
264         }
265         else
266         {
267             yaz_log (LOG_LOG, "Making session %d %s", parent->m_seqno,
268                             m_proxyTarget);
269             c = new Yaz_ProxyClient(m_PDU_Observable->clone());
270             c->m_next = parent->m_clientPool;
271             if (c->m_next)
272                 c->m_next->m_prev = &c->m_next;
273             parent->m_clientPool = c;
274             c->m_prev = &parent->m_clientPool;
275         }
276         if (cookie)
277             strcpy (c->m_cookie, cookie);
278         else
279             c->m_cookie[0] = '\0';
280         yaz_log (LOG_LOG, "Connecting to %s", m_proxyTarget);
281         c->m_seqno = parent->m_seqno;
282         c->client(m_proxyTarget);
283         c->m_init_flag = 0;
284         c->m_last_resultCount = 0;
285         c->m_last_ok = 0;
286         c->m_cache.clear();
287         c->m_sr_transform = 0;
288         c->m_waiting = 0;
289         c->m_resultSetStartPoint = 0;
290         c->timeout(20);
291
292         (parent->m_seqno)++;
293     }
294     yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
295     return c;
296 }
297
298 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
299 {
300     if (*m_parent->m_optimize == '0')
301         return apdu;      // don't optimize result sets..
302     if (apdu->which == Z_APDU_presentRequest)
303     {
304         Z_PresentRequest *pr = apdu->u.presentRequest;
305         Z_NamePlusRecordList *npr;
306         int toget = *pr->numberOfRecordsRequested;
307         int start = *pr->resultSetStartPoint;
308
309         if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
310         {
311             if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
312                                           pr->preferredRecordSyntax))
313             {
314                 yaz_log (LOG_LOG, "Returned cache records for present request");
315                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
316                 new_apdu->u.presentResponse->referenceId = pr->referenceId;
317                 
318                 new_apdu->u.presentResponse->numberOfRecordsReturned
319                     = odr_intdup(odr_encode(), toget);
320                                                                  
321                 new_apdu->u.presentResponse->records = (Z_Records*)
322                     odr_malloc(odr_encode(), sizeof(Z_Records));
323                 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
324                 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
325                 new_apdu->u.presentResponse->nextResultSetPosition =
326                     odr_intdup(odr_encode(), start+toget);
327                 send_Z_PDU(new_apdu);
328                 return 0;
329             }
330         }
331     }
332
333     if (apdu->which != Z_APDU_searchRequest)
334         return apdu;
335     Z_SearchRequest *sr = apdu->u.searchRequest;
336     Yaz_Z_Query *this_query = new Yaz_Z_Query;
337     Yaz_Z_Databases this_databases;
338
339     this_databases.set(sr->num_databaseNames, (const char **)
340                        sr->databaseNames);
341     
342     this_query->set_Z_Query(sr->query);
343     
344     if (m_client->m_last_ok && m_client->m_last_query &&
345         m_client->m_last_query->match(this_query) &&
346         !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
347         m_client->m_last_databases.match(this_databases))
348     {
349         delete this_query;
350         if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
351             m_client->m_last_resultCount < *sr->largeSetLowerBound)
352         {
353             Z_NamePlusRecordList *npr;
354             int toget = *sr->mediumSetPresentNumber;
355
356             if (toget > m_client->m_last_resultCount)
357                 toget = m_client->m_last_resultCount;
358  
359             if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
360                                           sr->preferredRecordSyntax))
361             {
362                 yaz_log (LOG_LOG, "Returned cache records for medium set");
363                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
364                 new_apdu->u.searchResponse->referenceId = sr->referenceId;
365                 new_apdu->u.searchResponse->resultCount =
366                     &m_client->m_last_resultCount;
367                 
368                 new_apdu->u.searchResponse->numberOfRecordsReturned
369                     = odr_intdup(odr_encode(), toget);
370                                                                  
371                 new_apdu->u.searchResponse->records = (Z_Records*)
372                     odr_malloc(odr_encode(), sizeof(Z_Records));
373                 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
374                 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
375                 new_apdu->u.searchResponse->nextResultSetPosition =
376                     odr_intdup(odr_encode(), toget+1);
377                 send_Z_PDU(new_apdu);
378                 return 0;
379             }
380             else
381             {
382                 // medium Set
383                 // send present request (medium size)
384                 yaz_log (LOG_LOG, "Optimizing search for medium set");
385
386                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
387                 Z_PresentRequest *pr = new_apdu->u.presentRequest;
388                 pr->referenceId = sr->referenceId;
389                 pr->resultSetId = sr->resultSetName;
390                 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
391                 *pr->numberOfRecordsRequested = toget;
392                 if (sr->mediumSetElementSetNames)
393                 {
394                     pr->recordComposition = (Z_RecordComposition *)
395                         odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
396                     pr->recordComposition->which = Z_RecordComp_simple;
397                     pr->recordComposition->u.simple =
398                         sr->mediumSetElementSetNames;
399                 }
400                 m_client->m_sr_transform = 1;
401                 return new_apdu;
402             }
403         }
404         else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
405             m_client->m_last_resultCount <= 0)
406         {
407             // large set. Return pseudo-search response immediately
408             yaz_log (LOG_LOG, "Optimizing search for large set");
409             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
410             new_apdu->u.searchResponse->referenceId = sr->referenceId;
411             new_apdu->u.searchResponse->resultCount =
412                 &m_client->m_last_resultCount;
413             send_Z_PDU(new_apdu);
414             return 0;
415         }
416         else
417         {
418             Z_NamePlusRecordList *npr;
419             int toget = m_client->m_last_resultCount;
420             // small set
421             // send a present request (small set)
422
423             if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
424                                           sr->preferredRecordSyntax))
425             {
426                 yaz_log (LOG_LOG, "Returned cache records for small set");
427                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
428                 new_apdu->u.searchResponse->referenceId = sr->referenceId;
429                 new_apdu->u.searchResponse->resultCount =
430                     &m_client->m_last_resultCount;
431                 
432                 new_apdu->u.searchResponse->numberOfRecordsReturned
433                     = odr_intdup(odr_encode(), toget);
434                                                                  
435                 new_apdu->u.searchResponse->records = (Z_Records*)
436                     odr_malloc(odr_encode(), sizeof(Z_Records));
437                 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
438                 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
439                 new_apdu->u.searchResponse->nextResultSetPosition =
440                     odr_intdup(odr_encode(), toget+1);
441                 send_Z_PDU(new_apdu);
442                 return 0;
443             }
444             else
445             {
446                 yaz_log (LOG_LOG, "Optimizing search for small set");
447                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
448                 Z_PresentRequest *pr = new_apdu->u.presentRequest;
449                 pr->referenceId = sr->referenceId;
450                 pr->resultSetId = sr->resultSetName;
451                 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
452                 *pr->numberOfRecordsRequested = toget;
453                 if (sr->smallSetElementSetNames)
454                 {
455                     pr->recordComposition = (Z_RecordComposition *)
456                         odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
457                     pr->recordComposition->which = Z_RecordComp_simple;
458                     pr->recordComposition->u.simple = sr->smallSetElementSetNames;
459                 }
460                 m_client->m_sr_transform = 1;
461                 return new_apdu;
462             }
463         }
464     }
465     else
466     {
467         delete m_client->m_last_query;
468         m_client->m_last_query = this_query;
469         m_client->m_last_ok = 0;
470         m_client->m_cache.clear();
471         m_client->m_resultSetStartPoint = 0;
472
473         xfree (m_client->m_last_resultSetId);
474         m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
475
476         m_client->m_last_databases.set(sr->num_databaseNames,
477                                        (const char **) sr->databaseNames);
478     }
479     return apdu;
480 }
481
482 static const char *apdu_name(Z_APDU *apdu)
483 {
484     switch (apdu->which)
485     {
486     case Z_APDU_initRequest:
487         return "initRequest";
488     case Z_APDU_initResponse:
489         return "initResponse";
490     case Z_APDU_searchRequest:
491         return "searchRequest";
492     case Z_APDU_searchResponse:
493         return "searchResponse";
494     case Z_APDU_presentRequest:
495         return "presentRequest";
496     case Z_APDU_presentResponse:
497         return "presentResponse";
498     case Z_APDU_deleteResultSetRequest:
499         return "deleteResultSetRequest";
500     case Z_APDU_deleteResultSetResponse:
501         return "deleteResultSetResponse";
502     case Z_APDU_scanRequest:
503         return "scanRequest";
504     case Z_APDU_scanResponse:
505         return "scanResponse";
506     case Z_APDU_sortRequest:
507         return "sortRequest";
508     case Z_APDU_sortResponse:
509         return "sortResponse";
510     case Z_APDU_extendedServicesRequest:
511         return "extendedServicesRequest";
512     case Z_APDU_extendedServicesResponse:
513         return "extendedServicesResponse";
514     case Z_APDU_close:
515         return "close";
516     }
517     return "other";
518 }
519
520 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
521 {
522     yaz_log (LOG_LOG, "Receiving %s from client", apdu_name(apdu));
523     // Determine our client.
524     m_client = get_client(apdu);
525     if (!m_client)
526     {
527         delete this;
528         return;
529     }
530     m_client->m_server = this;
531
532     if (apdu->which == Z_APDU_initRequest)
533     {
534         if (m_client->m_init_flag)
535         {
536             Z_APDU *apdu = m_client->m_initResponse;
537             apdu->u.initResponse->otherInfo = 0;
538             if (m_client->m_cookie && *m_client->m_cookie)
539                 set_otherInformationString(apdu, VAL_COOKIE, 1,
540                                            m_client->m_cookie);
541             send_Z_PDU(apdu);
542             return;
543         }
544         m_client->m_init_flag = 1;
545     }
546     apdu = result_set_optimize(apdu);
547     if (!apdu)
548         return;
549
550     yaz_log (LOG_LOG, "Sending %s to %s",
551                     apdu_name(apdu), m_client->get_hostname());
552
553     // delete other info part from PDU before sending to target
554     Z_OtherInformation **oi;
555     get_otherInfoAPDU(apdu, &oi);
556     if (oi)
557         *oi = 0;
558
559     if (apdu->which == Z_APDU_presentRequest &&
560         m_client->m_resultSetStartPoint == 0)
561     {
562         Z_PresentRequest *pr = apdu->u.presentRequest;
563         m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
564     } else {
565         m_client->m_resultSetStartPoint = 0;
566     }
567     if (m_client->send_Z_PDU(apdu) < 0)
568     {
569         delete m_client;
570         m_client = 0;
571         delete this;
572     }
573     else
574         m_client->m_waiting = 1;
575 }
576
577 void Yaz_Proxy::connectNotify()
578 {
579 }
580
581 void Yaz_Proxy::shutdown()
582 {
583     // only keep if keep_alive flag is set...
584     if (m_keepalive && m_client && m_client->m_waiting == 0)
585     {
586         yaz_log (LOG_LOG, "shutdown (client to proxy) keepalive %s",
587                  m_client->get_hostname());
588         assert (m_client->m_waiting != 2);
589         // Tell client (if any) that no server connection is there..
590         m_client->m_server = 0;
591     }
592     else if (m_client)
593     {
594         yaz_log (LOG_LOG, "shutdown (client to proxy) close %s",
595                  m_client->get_hostname());
596         assert (m_client->m_waiting != 2);
597         delete m_client;
598     }
599     else if (!m_parent)
600     {
601         yaz_log (LOG_LOG, "shutdown (client to proxy) bad state");
602         assert (m_parent);
603     }
604     else 
605     {
606         yaz_log (LOG_LOG, "shutdown (client to proxy)");
607     }
608     delete this;
609 }
610
611 void Yaz_ProxyClient::shutdown()
612 {
613     yaz_log (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
614     delete m_server;
615     delete this;
616 }
617
618 void Yaz_Proxy::failNotify()
619 {
620     yaz_log (LOG_LOG, "Yaz_Proxy connection closed by client");
621     shutdown();
622 }
623
624 void Yaz_ProxyClient::failNotify()
625 {
626     yaz_log (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
627     shutdown();
628 }
629
630 void Yaz_ProxyClient::connectNotify()
631 {
632     yaz_log (LOG_LOG, "Connection accepted by %s", get_hostname());
633     timeout(600);
634 }
635
636 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
637                                                   *the_PDU_Observable, int fd)
638 {
639     return new Yaz_ProxyClient(the_PDU_Observable);
640 }
641
642 Yaz_ProxyClient::~Yaz_ProxyClient()
643 {
644     if (m_prev)
645         *m_prev = m_next;
646     if (m_next)
647         m_next->m_prev = m_prev;
648     m_waiting = 2;     // for debugging purposes only.
649     odr_destroy(m_init_odr);
650     delete m_last_query;
651     xfree (m_last_resultSetId);
652 }
653
654 void Yaz_Proxy::timeoutNotify()
655 {
656     yaz_log (LOG_LOG, "timeout (client to proxy)");
657     shutdown();
658 }
659
660 void Yaz_ProxyClient::timeoutNotify()
661 {
662     yaz_log (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
663     shutdown();
664 }
665
666 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
667     Yaz_Z_Assoc (the_PDU_Observable)
668 {
669     m_cookie[0] = 0;
670     m_next = 0;
671     m_prev = 0;
672     m_init_flag = 0;
673     m_last_query = 0;
674     m_last_resultSetId = 0;
675     m_last_resultCount = 0;
676     m_last_ok = 0;
677     m_sr_transform = 0;
678     m_waiting = 0;
679     m_init_odr = odr_createmem (ODR_DECODE);
680     m_initResponse = 0;
681     m_resultSetStartPoint = 0;
682 }
683
684 const char *Yaz_Proxy::option(const char *name, const char *value)
685 {
686     if (!strcmp (name, "optimize")) {
687         if (value) {
688             xfree (m_optimize); 
689             m_optimize = xstrdup (value);
690         }
691         return m_optimize;
692     }
693     return 0;
694 }
695
696 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
697 {
698     m_waiting = 0;
699     yaz_log (LOG_LOG, "Receiving %s from %s", apdu_name(apdu),
700                      get_hostname());
701     if (apdu->which == Z_APDU_initResponse)
702     {
703         NMEM nmem = odr_extract_mem (odr_decode());
704         odr_reset (m_init_odr);
705         nmem_transfer (m_init_odr->mem, nmem);
706         m_initResponse = apdu;
707         nmem_destroy (nmem);
708     }
709     if (apdu->which == Z_APDU_searchResponse)
710     {
711         Z_SearchResponse *sr = apdu->u.searchResponse;
712         m_last_resultCount = *sr->resultCount;
713         int status = *sr->searchStatus;
714         if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
715         {
716             m_last_ok = 1;
717             
718             if (sr->records && sr->records->which == Z_Records_DBOSD)
719             {
720                 m_cache.add(odr_decode(),
721                             sr->records->u.databaseOrSurDiagnostics, 1);
722             }
723         }
724     }
725     if (apdu->which == Z_APDU_presentResponse)
726     {
727         Z_PresentResponse *pr = apdu->u.presentResponse;
728         if (m_sr_transform)
729         {
730             m_sr_transform = 0;
731             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
732             Z_SearchResponse *sr = new_apdu->u.searchResponse;
733             sr->referenceId = pr->referenceId;
734             *sr->resultCount = m_last_resultCount;
735             sr->records = pr->records;
736             sr->nextResultSetPosition = pr->nextResultSetPosition;
737             sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
738             apdu = new_apdu;
739         }
740         if (pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
741         {
742             m_cache.add(odr_decode(),
743                         pr->records->u.databaseOrSurDiagnostics,
744                         m_resultSetStartPoint);
745             m_resultSetStartPoint = 0;
746         }
747     }
748     if (m_cookie && *m_cookie)
749         set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
750     if (m_server)
751     {
752         yaz_log (LOG_LOG, "Sending %s to client", apdu_name(apdu));
753         m_server->send_Z_PDU(apdu);
754     }
755     if (apdu->which == Z_APDU_close)
756     {
757         shutdown();
758     }
759 }