Start work on log options
[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.61 2003-10-16 13:40:41 adam Exp $
6  */
7
8 #include <assert.h>
9 #include <time.h>
10
11 #include <yaz/marcdisp.h>
12 #include <yaz/yaz-iconv.h>
13 #include <yaz/log.h>
14 #include <yaz/diagbib1.h>
15 #include <yaz++/proxy.h>
16
17 static const char *apdu_name(Z_APDU *apdu)
18 {
19     switch (apdu->which)
20     {
21     case Z_APDU_initRequest:
22         return "initRequest";
23     case Z_APDU_initResponse:
24         return "initResponse";
25     case Z_APDU_searchRequest:
26         return "searchRequest";
27     case Z_APDU_searchResponse:
28         return "searchResponse";
29     case Z_APDU_presentRequest:
30         return "presentRequest";
31     case Z_APDU_presentResponse:
32         return "presentResponse";
33     case Z_APDU_deleteResultSetRequest:
34         return "deleteResultSetRequest";
35     case Z_APDU_deleteResultSetResponse:
36         return "deleteResultSetResponse";
37     case Z_APDU_scanRequest:
38         return "scanRequest";
39     case Z_APDU_scanResponse:
40         return "scanResponse";
41     case Z_APDU_sortRequest:
42         return "sortRequest";
43     case Z_APDU_sortResponse:
44         return "sortResponse";
45     case Z_APDU_extendedServicesRequest:
46         return "extendedServicesRequest";
47     case Z_APDU_extendedServicesResponse:
48         return "extendedServicesResponse";
49     case Z_APDU_close:
50         return "close";
51     }
52     return "other";
53 }
54
55 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
56                      Yaz_Proxy *parent) :
57     Yaz_Z_Assoc(the_PDU_Observable), m_bw_stat(60), m_pdu_stat(60)
58 {
59     m_PDU_Observable = the_PDU_Observable;
60     m_client = 0;
61     m_parent = parent;
62     m_clientPool = 0;
63     m_seqno = 1;
64     m_keepalive_limit_bw = 500000;
65     m_keepalive_limit_pdu = 1000;
66     m_proxyTarget = 0;
67     m_default_target = 0;
68     m_proxy_authentication = 0;
69     m_max_clients = 150;
70     m_seed = time(0);
71     m_client_idletime = 600;
72     m_target_idletime = 600;
73     m_optimize = xstrdup ("1");
74     strcpy(m_session_str, "x");
75     m_session_no=0;
76     m_bytes_sent = m_bytes_recv = 0;
77     m_bw_hold_PDU = 0;
78     m_bw_max = 0;
79     m_pdu_max = 0;
80     m_max_record_retrieve = 0;
81     m_reconfig_flag = 0;
82     m_config_fname = 0;
83     m_request_no = 0;
84     m_invalid_session = 0;
85     m_config = 0;
86     m_marcxml_flag = 0;
87 }
88
89 Yaz_Proxy::~Yaz_Proxy()
90 {
91     yaz_log(LOG_LOG, "%sClosed %d/%d sent/recv bytes total", m_session_str,
92             m_bytes_sent, m_bytes_recv);
93     xfree (m_proxyTarget);
94     xfree (m_default_target);
95     xfree (m_proxy_authentication);
96     xfree (m_optimize);
97     delete m_config;
98 }
99
100 int Yaz_Proxy::set_config(const char *config)
101 {
102     delete m_config;
103     m_config = new Yaz_ProxyConfig();
104     xfree(m_config_fname);
105     m_config_fname = xstrdup(config);
106     int r = m_config->read_xml(config);
107     return r;
108 }
109
110 void Yaz_Proxy::set_default_target(const char *target)
111 {
112     xfree (m_default_target);
113     m_default_target = 0;
114     if (target)
115         m_default_target = (char *) xstrdup (target);
116 }
117
118 void Yaz_Proxy::set_proxy_authentication (const char *auth)
119 {
120     xfree (m_proxy_authentication);
121     m_proxy_authentication = 0;
122     if (auth)
123         m_proxy_authentication = (char *) xstrdup (auth);
124 }
125
126 Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
127 {
128     if (m_parent)
129         return m_parent->check_reconfigure();
130
131     Yaz_ProxyConfig *cfg = m_config;
132     if (m_reconfig_flag)
133     {
134         yaz_log(LOG_LOG, "reconfigure");
135         yaz_log_reopen();
136         if (m_config_fname && cfg)
137         {
138             yaz_log(LOG_LOG, "reconfigure config %s", m_config_fname);
139             int r = cfg->read_xml(m_config_fname);
140             if (r)
141                 yaz_log(LOG_WARN, "reconfigure failed");
142         }
143         else
144             yaz_log(LOG_LOG, "reconfigure");
145         m_reconfig_flag = 0;
146     }
147     return cfg;
148 }
149
150 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
151                                             *the_PDU_Observable, int fd)
152 {
153     check_reconfigure();
154     Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable, this);
155     new_proxy->m_config = 0;
156     new_proxy->m_config_fname = 0;
157     new_proxy->timeout(m_client_idletime);
158     new_proxy->m_target_idletime = m_target_idletime;
159     new_proxy->set_default_target(m_default_target);
160     new_proxy->set_APDU_log(get_APDU_log());
161     new_proxy->set_proxy_authentication(m_proxy_authentication);
162     sprintf(new_proxy->m_session_str, "%ld:%d ", (long) time(0), m_session_no);
163     m_session_no++;
164     yaz_log (LOG_LOG, "%sNew session %s", new_proxy->m_session_str,
165              the_PDU_Observable->getpeername());
166     return new_proxy;
167 }
168
169 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
170 {
171     int oid[OID_SIZE];
172     Z_OtherInformationUnit *oi;
173     struct oident ent;
174     ent.proto = PROTO_Z3950;
175     ent.oclass = CLASS_USERINFO;
176     ent.value = (oid_value) VAL_COOKIE;
177     assert (oid_ent_to_oid (&ent, oid));
178
179     if (oid_ent_to_oid (&ent, oid) && 
180         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
181         oi->which == Z_OtherInfo_characterInfo)
182         return oi->information.characterInfo;
183     return 0;
184 }
185
186 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
187 {
188     int oid[OID_SIZE];
189     Z_OtherInformationUnit *oi;
190     struct oident ent;
191     ent.proto = PROTO_Z3950;
192     ent.oclass = CLASS_USERINFO;
193     ent.value = (oid_value) VAL_PROXY;
194     if (oid_ent_to_oid (&ent, oid) &&
195         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
196         oi->which == Z_OtherInfo_characterInfo)
197         return oi->information.characterInfo;
198     return 0;
199 }
200
201 const char *Yaz_Proxy::load_balance(const char **url)
202 {
203     int zurl_in_use[MAX_ZURL_PLEX];
204     Yaz_ProxyClient *c;
205     int i;
206
207     for (i = 0; i<MAX_ZURL_PLEX; i++)
208         zurl_in_use[i] = 0;
209     for (c = m_parent->m_clientPool; c; c = c->m_next)
210     {
211         for (i = 0; url[i]; i++)
212             if (!strcmp(url[i], c->get_hostname()))
213                 zurl_in_use[i]++;
214     }
215     int min = 100000;
216     const char *ret = 0;
217     for (i = 0; url[i]; i++)
218     {
219         yaz_log(LOG_DEBUG, "%szurl=%s use=%d",
220                 m_session_str, url[i], zurl_in_use[i]);
221         if (min > zurl_in_use[i])
222         {
223             ret = url[i];
224             min = zurl_in_use[i];
225         }
226     }
227     return ret;
228 }
229
230 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
231 {
232     assert (m_parent);
233     Yaz_Proxy *parent = m_parent;
234     Z_OtherInformation **oi;
235     Yaz_ProxyClient *c = m_client;
236     
237     get_otherInfoAPDU(apdu, &oi);
238     char *cookie = get_cookie(oi);
239
240     if (!m_proxyTarget)
241     {
242         const char *url[MAX_ZURL_PLEX];
243         const char *proxy_host = get_proxy(oi);
244         Yaz_ProxyConfig *cfg = check_reconfigure();
245         if (proxy_host)
246         {
247             xfree(m_default_target);
248             m_default_target = xstrdup(proxy_host);
249             proxy_host = m_default_target;
250         }
251         int client_idletime = -1;
252         url[0] = m_default_target;
253         url[1] = 0;
254         if (cfg)
255         {
256             int pre_init = 0;
257             int log_mask = 0;
258             cfg->get_target_info(proxy_host, url, &m_bw_max,
259                                  &m_pdu_max, &m_max_record_retrieve,
260                                  &m_target_idletime, &client_idletime,
261                                  &parent->m_max_clients,
262                                  &m_keepalive_limit_bw,
263                                  &m_keepalive_limit_pdu,
264                                  &pre_init, &log_mask);
265         }
266         if (client_idletime != -1)
267         {
268             m_client_idletime = client_idletime;
269             timeout(m_client_idletime);
270         }
271         if (!url[0])
272         {
273             yaz_log(LOG_LOG, "%sNo default target", m_session_str);
274             return 0;
275         }
276         // we don't handle multiplexing for cookie session, so we just
277         // pick the first one in this case (anonymous users will be able
278         // to use any backend)
279         if (cookie && *cookie)
280             m_proxyTarget = (char*) xstrdup(url[0]);
281         else
282             m_proxyTarget = (char*) xstrdup(load_balance(url));
283     }
284     if (cookie && *cookie)
285     {
286         Yaz_ProxyClient *cc = 0;
287         
288         for (c = parent->m_clientPool; c; c = c->m_next)
289         {
290             assert (c->m_prev);
291             assert (*c->m_prev == c);
292             if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
293                 !strcmp(m_proxyTarget, c->get_hostname()))
294             {
295                 cc = c;
296             }
297         }
298         if (cc)
299         {
300             // found it in cache
301             c = cc;
302             // The following handles "cancel"
303             // If connection is busy (waiting for PDU) and
304             // we have an initRequest we can safely do re-open
305             if (c->m_waiting && apdu->which == Z_APDU_initRequest)
306             {
307                 yaz_log (LOG_LOG, "%s REOPEN target=%s", m_session_str,
308                          c->get_hostname());
309                 c->close();
310                 c->m_init_flag = 0;
311
312                 c->m_last_ok = 0;
313                 c->m_cache.clear();
314                 c->m_last_resultCount = 0;
315                 c->m_sr_transform = 0;
316                 c->m_waiting = 0;
317                 c->m_resultSetStartPoint = 0;
318                 c->m_target_idletime = m_target_idletime;
319                 if (c->client(m_proxyTarget))
320                 {
321                     delete c;
322                     return 0;
323                 }
324                 c->timeout(30); 
325             }
326             c->m_seqno = parent->m_seqno;
327             if (c->m_server && c->m_server != this)
328                 c->m_server->m_client = 0;
329             c->m_server = this;
330             (parent->m_seqno)++;
331             yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
332             return c;
333         }
334     }
335     else if (!c)
336     {
337         Yaz_ProxyClient *cc = 0;
338         
339         for (c = parent->m_clientPool; c; c = c->m_next)
340         {
341             assert (c->m_prev);
342             assert (*c->m_prev == c);
343             if (c->m_server == 0 && c->m_cookie == 0 && 
344                 c->m_waiting == 0 &&
345                 !strcmp(m_proxyTarget, c->get_hostname()))
346             {
347                 cc = c;
348             }
349         }
350         if (cc)
351         {
352             // found it in cache
353             c = cc;
354
355             yaz_log (LOG_LOG, "%sREUSE %d %d %s",
356                      m_session_str,
357                      c->m_seqno, parent->m_seqno, c->get_hostname());
358
359             c->m_seqno = parent->m_seqno;
360             assert(c->m_server == 0);
361             c->m_server = this;
362             
363             (parent->m_seqno)++;
364
365             parent->pre_init();
366
367             return c;
368         }
369     }
370     if (!m_client)
371     {
372         if (apdu->which != Z_APDU_initRequest)
373         {
374             yaz_log (LOG_LOG, "no first INIT!");
375             return 0;
376         }
377         Z_InitRequest *initRequest = apdu->u.initRequest;
378
379         if (!initRequest->idAuthentication)
380         {
381             if (m_proxy_authentication)
382             {
383                 initRequest->idAuthentication =
384                     (Z_IdAuthentication *)
385                     odr_malloc (odr_encode(),
386                                 sizeof(*initRequest->idAuthentication));
387                 initRequest->idAuthentication->which =
388                     Z_IdAuthentication_open;
389                 initRequest->idAuthentication->u.open =
390                     odr_strdup (odr_encode(), m_proxy_authentication);
391             }
392         }
393
394         // go through list of clients - and find the lowest/oldest one.
395         Yaz_ProxyClient *c_min = 0;
396         int min_seq = -1;
397         int no_of_clients = 0;
398         if (parent->m_clientPool)
399             yaz_log (LOG_DEBUG, "Existing sessions");
400         for (c = parent->m_clientPool; c; c = c->m_next)
401         {
402             yaz_log (LOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
403                                c->m_waiting, c->get_hostname(),
404                                c->m_cookie ? c->m_cookie : "");
405             no_of_clients++;
406             if (min_seq < 0 || c->m_seqno < min_seq)
407             {
408                 min_seq = c->m_seqno;
409                 c_min = c;
410             }
411         }
412         if (no_of_clients >= parent->m_max_clients)
413         {
414             c = c_min;
415             if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
416             {
417                 yaz_log (LOG_LOG, "%sMAXCLIENTS Destroy %d",
418                          m_session_str, c->m_seqno);
419                 if (c->m_server && c->m_server != this)
420                     delete c->m_server;
421                 c->m_server = 0;
422             }
423             else
424             {
425                 yaz_log (LOG_LOG, "%sMAXCLIENTS Reuse %d %d %s",
426                          m_session_str,
427                          c->m_seqno, parent->m_seqno, c->get_hostname());
428                 xfree (c->m_cookie);
429                 c->m_cookie = 0;
430                 if (cookie)
431                     c->m_cookie = xstrdup(cookie);
432                 c->m_seqno = parent->m_seqno;
433                 if (c->m_server && c->m_server != this)
434                 {
435                     c->m_server->m_client = 0;
436                     delete c->m_server;
437                 }
438                 (parent->m_seqno)++;
439                 c->m_target_idletime = m_target_idletime;
440                 c->timeout(m_target_idletime);
441                 return c;
442             }
443         }
444         else
445         {
446             yaz_log (LOG_LOG, "%sNEW %d %s",
447                      m_session_str, parent->m_seqno, m_proxyTarget);
448             c = new Yaz_ProxyClient(m_PDU_Observable->clone(), parent);
449             c->m_next = parent->m_clientPool;
450             if (c->m_next)
451                 c->m_next->m_prev = &c->m_next;
452             parent->m_clientPool = c;
453             c->m_prev = &parent->m_clientPool;
454         }
455
456         xfree (c->m_cookie);
457         c->m_cookie = 0;
458         if (cookie)
459             c->m_cookie = xstrdup(cookie);
460
461         c->m_seqno = parent->m_seqno;
462         c->m_init_flag = 0;
463         c->m_last_resultCount = 0;
464         c->m_last_ok = 0;
465         c->m_cache.clear();
466         c->m_sr_transform = 0;
467         c->m_waiting = 0;
468         c->m_resultSetStartPoint = 0;
469         (parent->m_seqno)++;
470         if (c->client(m_proxyTarget))
471         {
472             delete c;
473             return 0;
474         }
475         c->m_target_idletime = m_target_idletime;
476         c->timeout(30);
477
478     }
479     yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
480     return c;
481 }
482
483 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
484 {
485     int i;
486     for (i = 0; i<num; i++)
487     {
488         oident *ent;
489         Z_DefaultDiagFormat *r;
490         Z_DiagRec *p = pp[i];
491         if (p->which != Z_DiagRec_defaultFormat)
492         {
493             yaz_log(LOG_LOG, "%sError no diagnostics", m_session_str);
494             return;
495         }
496         else
497             r = p->u.defaultFormat;
498         if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
499             ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
500             yaz_log(LOG_LOG, "%sError unknown diagnostic set", m_session_str);
501         switch (r->which)
502         {
503         case Z_DefaultDiagFormat_v2Addinfo:
504             yaz_log(LOG_LOG, "%sError %d %s:%s",
505                     m_session_str,
506                     *r->condition, diagbib1_str(*r->condition),
507                     r->u.v2Addinfo);
508             break;
509         case Z_DefaultDiagFormat_v3Addinfo:
510             yaz_log(LOG_LOG, "%sError %d %s:%s",
511                     m_session_str,
512                     *r->condition, diagbib1_str(*r->condition),
513                     r->u.v3Addinfo);
514             break;
515         }
516     }
517 }
518
519 void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
520 {
521     int i;
522
523     yaz_marc_t mt = yaz_marc_create();
524     yaz_marc_xml(mt, YAZ_MARC_MARCXML);
525     for (i = 0; i < p->num_records; i++)
526     {
527         Z_NamePlusRecord *npr = p->records[i];
528         if (npr->which == Z_NamePlusRecord_databaseRecord)
529         {
530             Z_External *r = npr->u.databaseRecord;
531             if (r->which == Z_External_octet)
532             {
533                 int rlen;
534                 char *result;
535                 if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
536                                         r->u.octet_aligned->len,
537                                         &result, &rlen))
538                 {
539                     yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
540                     WRBUF wrbuf = wrbuf_alloc();
541                     
542                     char outbuf[120];
543                     size_t inbytesleft = rlen;
544                     const char *inp = result;
545                     while (cd && inbytesleft)
546                     {
547                         size_t outbytesleft = sizeof(outbuf);
548                         char *outp = outbuf;
549                         size_t r;
550                         
551                         r = yaz_iconv (cd, (char**) &inp,
552                                        &inbytesleft,
553                                        &outp, &outbytesleft);
554                         if (r == (size_t) (-1))
555                         {
556                             int e = yaz_iconv_error(cd);
557                             if (e != YAZ_ICONV_E2BIG)
558                             {
559                                 yaz_log(LOG_WARN, "conversion failure");
560                                 break;
561                             }
562                         }
563                         wrbuf_write(wrbuf, outbuf, outp - outbuf);
564                     }
565                     if (cd)
566                         yaz_iconv_close(cd);
567
568                     npr->u.databaseRecord = z_ext_record(odr_encode(),
569                                                          VAL_TEXT_XML,
570                                                          wrbuf_buf(wrbuf),
571                                                          wrbuf_len(wrbuf));
572                     wrbuf_free(wrbuf, 1);
573                 }
574             }
575         }
576     }
577     yaz_marc_destroy(mt);
578 }
579
580 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
581 {
582     int len = 0;
583     if (apdu->which == Z_APDU_searchResponse)
584     {
585         Z_SearchResponse *sr = apdu->u.searchResponse;
586         Z_Records *p = sr->records;
587         if (p && p->which == Z_Records_NSD)
588         {
589             Z_DiagRec dr, *dr_p = &dr;
590             dr.which = Z_DiagRec_defaultFormat;
591             dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
592
593             display_diagrecs(&dr_p, 1);
594         }
595         else
596         {
597             if (m_marcxml_flag && p && p->which == Z_Records_DBOSD)
598                 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
599             if (sr->resultCount)
600             {
601                 yaz_log(LOG_LOG, "%s%d hits", m_session_str,
602                         *sr->resultCount);
603                 if (*sr->resultCount < 0)
604                     m_invalid_session = 1;
605             }
606         }
607     }
608     else if (apdu->which == Z_APDU_presentResponse)
609     {
610         Z_PresentResponse *sr = apdu->u.presentResponse;
611         Z_Records *p = sr->records;
612         if (p && p->which == Z_Records_NSD)
613         {
614             Z_DiagRec dr, *dr_p = &dr;
615             dr.which = Z_DiagRec_defaultFormat;
616             dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
617
618             display_diagrecs(&dr_p, 1);
619         }
620         if (m_marcxml_flag && p && p->which == Z_Records_DBOSD)
621             convert_to_marcxml(p->u.databaseOrSurDiagnostics);
622     }
623     int r = send_Z_PDU(apdu, &len);
624     yaz_log (LOG_DEBUG, "%sSending %s to client %d bytes", m_session_str,
625              apdu_name(apdu), len);
626     m_bytes_sent += len;
627     m_bw_stat.add_bytes(len);
628     return r;
629 }
630
631 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
632 {
633     int len = 0;
634     int r = send_Z_PDU(apdu, &len);
635     yaz_log (LOG_LOG, "%sSending %s to %s %d bytes",
636              get_session_str(),
637              apdu_name(apdu), get_hostname(), len);
638     m_bytes_sent += len;
639     return r;
640 }
641
642 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
643 {
644     if (apdu->which == Z_APDU_presentRequest)
645     {
646         Z_PresentRequest *pr = apdu->u.presentRequest;
647         Z_NamePlusRecordList *npr;
648         int toget = *pr->numberOfRecordsRequested;
649         int start = *pr->resultSetStartPoint;
650
651         yaz_log(LOG_LOG, "%sPresent %s %d+%d", m_session_str,
652                 pr->resultSetId, start, toget);
653
654         if (*m_parent->m_optimize == '0')
655             return apdu;
656
657         if (!m_client->m_last_resultSetId)
658         {
659             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
660             new_apdu->u.presentResponse->records =
661                 create_nonSurrogateDiagnostics(odr_encode(), 30,
662                                                pr->resultSetId);
663             send_to_client(new_apdu);
664             return 0;
665         }
666         if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
667         {
668             if (start+toget-1 > m_client->m_last_resultCount)
669             {
670                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
671                 new_apdu->u.presentResponse->records =
672                     create_nonSurrogateDiagnostics(odr_encode(), 13, 0);
673                 send_to_client(new_apdu);
674                 return 0;
675             }
676             if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
677                                           pr->preferredRecordSyntax,
678                                           pr->recordComposition))
679             {
680                 yaz_log (LOG_LOG, "%sReturned cached records for present request", 
681                          m_session_str);
682                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
683                 new_apdu->u.presentResponse->referenceId = pr->referenceId;
684                 
685                 new_apdu->u.presentResponse->numberOfRecordsReturned
686                     = odr_intdup(odr_encode(), toget);
687                                                                  
688                 new_apdu->u.presentResponse->records = (Z_Records*)
689                     odr_malloc(odr_encode(), sizeof(Z_Records));
690                 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
691                 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
692                 new_apdu->u.presentResponse->nextResultSetPosition =
693                     odr_intdup(odr_encode(), start+toget);
694
695                 send_to_client(new_apdu);
696                 return 0;
697             }
698         }
699     }
700
701     if (apdu->which != Z_APDU_searchRequest)
702         return apdu;
703     Z_SearchRequest *sr = apdu->u.searchRequest;
704     Yaz_Z_Query *this_query = new Yaz_Z_Query;
705     Yaz_Z_Databases this_databases;
706
707     this_databases.set(sr->num_databaseNames, (const char **)
708                        sr->databaseNames);
709     
710     this_query->set_Z_Query(sr->query);
711
712     char query_str[120];
713     this_query->print(query_str, sizeof(query_str)-1);
714     yaz_log(LOG_LOG, "%sSearch %s", m_session_str, query_str);
715
716     if (*m_parent->m_optimize != '0' &&
717         m_client->m_last_ok && m_client->m_last_query &&
718         m_client->m_last_query->match(this_query) &&
719         !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
720         m_client->m_last_databases.match(this_databases))
721     {
722         delete this_query;
723         if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
724             m_client->m_last_resultCount < *sr->largeSetLowerBound)
725         {
726             Z_NamePlusRecordList *npr;
727             int toget = *sr->mediumSetPresentNumber;
728             Z_RecordComposition *comp = 0;
729
730             if (toget > m_client->m_last_resultCount)
731                 toget = m_client->m_last_resultCount;
732             
733             if (sr->mediumSetElementSetNames)
734             {
735                 comp = (Z_RecordComposition *)
736                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
737                 comp->which = Z_RecordComp_simple;
738                 comp->u.simple = sr->mediumSetElementSetNames;
739             }
740  
741             if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
742                                           sr->preferredRecordSyntax, comp))
743             {
744                 yaz_log (LOG_LOG, "%sReturned cached records for medium set",
745                          m_session_str);
746                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
747                 new_apdu->u.searchResponse->referenceId = sr->referenceId;
748                 new_apdu->u.searchResponse->resultCount =
749                     &m_client->m_last_resultCount;
750                 
751                 new_apdu->u.searchResponse->numberOfRecordsReturned
752                     = odr_intdup(odr_encode(), toget);
753                                                         
754                 new_apdu->u.searchResponse->presentStatus =
755                     odr_intdup(odr_encode(), Z_PresentStatus_success);
756                 new_apdu->u.searchResponse->records = (Z_Records*)
757                     odr_malloc(odr_encode(), sizeof(Z_Records));
758                 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
759                 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
760                 new_apdu->u.searchResponse->nextResultSetPosition =
761                     odr_intdup(odr_encode(), toget+1);
762                 send_to_client(new_apdu);
763                 return 0;
764             }
765             else
766             {
767                 // medium Set
768                 // send present request (medium size)
769                 yaz_log (LOG_LOG, "%sOptimizing search for medium set",
770                          m_session_str);
771
772                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
773                 Z_PresentRequest *pr = new_apdu->u.presentRequest;
774                 pr->referenceId = sr->referenceId;
775                 pr->resultSetId = sr->resultSetName;
776                 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
777                 *pr->numberOfRecordsRequested = toget;
778                 pr->recordComposition = comp;
779                 m_client->m_sr_transform = 1;
780                 return new_apdu;
781             }
782         }
783         else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
784             m_client->m_last_resultCount <= 0)
785         {
786             // large set. Return pseudo-search response immediately
787             yaz_log (LOG_LOG, "%sOptimizing search for large set",
788                      m_session_str);
789             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
790             new_apdu->u.searchResponse->referenceId = sr->referenceId;
791             new_apdu->u.searchResponse->resultCount =
792                 &m_client->m_last_resultCount;
793             send_to_client(new_apdu);
794             return 0;
795         }
796         else
797         {
798             Z_NamePlusRecordList *npr;
799             int toget = m_client->m_last_resultCount;
800             Z_RecordComposition *comp = 0;
801             // small set
802             // send a present request (small set)
803             
804             if (sr->smallSetElementSetNames)
805             {
806                 comp = (Z_RecordComposition *)
807                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
808                 comp->which = Z_RecordComp_simple;
809                 comp->u.simple = sr->smallSetElementSetNames;
810             }
811
812             if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
813                                           sr->preferredRecordSyntax, comp))
814             {
815                 yaz_log (LOG_LOG, "%sReturned cached records for small set",
816                          m_session_str);
817                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
818                 new_apdu->u.searchResponse->referenceId = sr->referenceId;
819                 new_apdu->u.searchResponse->resultCount =
820                     &m_client->m_last_resultCount;
821                 
822                 new_apdu->u.searchResponse->numberOfRecordsReturned
823                     = odr_intdup(odr_encode(), toget);
824                                                                  
825                 new_apdu->u.searchResponse->presentStatus =
826                     odr_intdup(odr_encode(), Z_PresentStatus_success);
827                 new_apdu->u.searchResponse->records = (Z_Records*)
828                     odr_malloc(odr_encode(), sizeof(Z_Records));
829                 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
830                 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
831                 new_apdu->u.searchResponse->nextResultSetPosition =
832                     odr_intdup(odr_encode(), toget+1);
833                 send_to_client(new_apdu);
834                 return 0;
835             }
836             else
837             {
838                 yaz_log (LOG_LOG, "%sOptimizing search for small set",
839                          m_session_str);
840                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
841                 Z_PresentRequest *pr = new_apdu->u.presentRequest;
842                 pr->referenceId = sr->referenceId;
843                 pr->resultSetId = sr->resultSetName;
844                 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
845                 *pr->numberOfRecordsRequested = toget;
846                 pr->recordComposition = comp;
847                 m_client->m_sr_transform = 1;
848                 return new_apdu;
849             }
850         }
851     }
852     else  // query doesn't match
853     {
854         delete m_client->m_last_query;
855         m_client->m_last_query = this_query;
856         m_client->m_last_ok = 0;
857         m_client->m_cache.clear();
858         m_client->m_resultSetStartPoint = 0;
859
860         xfree (m_client->m_last_resultSetId);
861         m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
862
863         m_client->m_last_databases.set(sr->num_databaseNames,
864                                        (const char **) sr->databaseNames);
865     }
866     return apdu;
867 }
868
869
870 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu, int len)
871 {
872     char *cp = strchr(m_session_str, ' ');
873     m_request_no++;
874     if (cp)
875         sprintf(cp+1, "%d ", m_request_no);
876
877     int reduce = 0;
878     m_bytes_recv += len;
879     
880     yaz_log (LOG_DEBUG, "%sReceiving %s from client %d bytes", m_session_str,
881              apdu_name(apdu), len);
882
883     if (m_bw_hold_PDU)     // double incoming PDU. shutdown now.
884         shutdown();
885
886     m_bw_stat.add_bytes(len);
887     m_pdu_stat.add_bytes(1);
888
889     int bw_total = m_bw_stat.get_total();
890     int pdu_total = m_pdu_stat.get_total();
891
892     yaz_log(LOG_LOG, "%sstat bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
893             m_session_str, bw_total, pdu_total, m_bw_max, m_pdu_max);
894     if (m_bw_max)
895     {
896         if (bw_total > m_bw_max)
897         {
898             reduce = (bw_total/m_bw_max);
899         }
900     }
901     if (m_pdu_max)
902     {
903         if (pdu_total > m_pdu_max)
904         {
905             int nreduce = (60/m_pdu_max);
906             reduce = (reduce > nreduce) ? reduce : nreduce;
907         }
908     }
909     if (reduce)  
910     {
911         yaz_log(LOG_LOG, "%sLimit delay=%d", m_session_str, reduce);
912         m_bw_hold_PDU = apdu;  // save PDU and signal "on hold"
913         timeout(reduce);       // call us reduce seconds later
914     }
915     else
916         recv_Z_PDU_0(apdu);    // all fine. Proceed receive PDU as usual
917 }
918
919 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
920 {
921     if (m_max_record_retrieve)
922     {
923         if (apdu->which == Z_APDU_presentRequest)
924         {
925             Z_PresentRequest *pr = apdu->u.presentRequest;
926             if (pr->numberOfRecordsRequested && 
927                 *pr->numberOfRecordsRequested > m_max_record_retrieve)
928                 *pr->numberOfRecordsRequested = m_max_record_retrieve;
929         }
930     }
931 }
932
933 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
934                                                      int error,
935                                                      const char *addinfo)
936 {
937     Z_Records *rec = (Z_Records *)
938         odr_malloc (odr, sizeof(*rec));
939     int *err = (int *)
940         odr_malloc (odr, sizeof(*err));
941     Z_DiagRec *drec = (Z_DiagRec *)
942         odr_malloc (odr, sizeof(*drec));
943     Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
944         odr_malloc (odr, sizeof(*dr));
945     *err = error;
946     rec->which = Z_Records_NSD;
947     rec->u.nonSurrogateDiagnostic = dr;
948     dr->diagnosticSetId =
949         yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
950     dr->condition = err;
951     dr->which = Z_DefaultDiagFormat_v2Addinfo;
952     dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
953     return rec;
954 }
955
956 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
957 {
958     if (apdu->which == Z_APDU_searchRequest)
959     {
960         Z_SearchRequest *sr = apdu->u.searchRequest;
961         int err = 0;
962         char *addinfo = 0;
963
964         Yaz_ProxyConfig *cfg = check_reconfigure();
965         if (cfg)
966             err = cfg->check_query(odr_encode(), m_default_target,
967                                    sr->query, &addinfo);
968         if (err)
969         {
970             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
971
972             new_apdu->u.searchResponse->referenceId = sr->referenceId;
973             new_apdu->u.searchResponse->records =
974                 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
975             *new_apdu->u.searchResponse->searchStatus = 0;
976
977             send_to_client(new_apdu);
978
979             return 0;
980         }
981     }
982     return apdu;
983 }
984
985 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
986 {
987     m_marcxml_flag = 0;
988     if (apdu->which == Z_APDU_searchRequest)
989     {
990         Z_SearchRequest *sr = apdu->u.searchRequest;
991         if (*sr->smallSetUpperBound > 0 || *sr->mediumSetPresentNumber > 0)
992         {
993             int err = 0;
994             char *addinfo = 0;
995             Yaz_ProxyConfig *cfg = check_reconfigure();
996
997             if (cfg)
998                 err = cfg->check_syntax(odr_encode(),
999                                         m_default_target,
1000                                         sr->preferredRecordSyntax,
1001                                         &addinfo);
1002             if (err == -1)
1003             {
1004                 sr->preferredRecordSyntax =
1005                     yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN,
1006                                            VAL_USMARC);
1007                 m_marcxml_flag = 1;
1008             }
1009             else if (err)
1010             {
1011                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1012                 
1013                 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1014                 new_apdu->u.searchResponse->records =
1015                     create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1016                 *new_apdu->u.searchResponse->searchStatus = 0;
1017                 
1018                 send_to_client(new_apdu);
1019                 
1020                 return 0;
1021             }
1022         }
1023     }
1024     else if (apdu->which == Z_APDU_presentRequest)
1025     {
1026         Z_PresentRequest *pr = apdu->u.presentRequest;
1027         int err = 0;
1028         char *addinfo = 0;
1029         Yaz_ProxyConfig *cfg = check_reconfigure();
1030
1031         if (cfg)
1032             err = cfg->check_syntax(odr_encode(), m_default_target,
1033                                     pr->preferredRecordSyntax,
1034                                     &addinfo);
1035         if (err == -1)
1036         {
1037             pr->preferredRecordSyntax =
1038                 yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN,
1039                                        VAL_USMARC);
1040             m_marcxml_flag = 1;
1041         }
1042         else if (err)
1043         {
1044             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1045             
1046             new_apdu->u.presentResponse->referenceId = pr->referenceId;
1047             new_apdu->u.presentResponse->records =
1048                 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1049             *new_apdu->u.presentResponse->presentStatus =
1050                 Z_PresentStatus_failure;
1051             
1052             send_to_client(new_apdu);
1053             
1054             return 0;
1055         }
1056     }
1057     return apdu;
1058 }
1059
1060 void Yaz_Proxy::recv_Z_PDU_0(Z_APDU *apdu)
1061 {
1062     // Determine our client.
1063     m_client = get_client(apdu);
1064     if (!m_client)
1065     {
1066         delete this;
1067         return;
1068     }
1069     m_client->m_server = this;
1070
1071     if (apdu->which == Z_APDU_initRequest)
1072     {
1073         if (apdu->u.initRequest->implementationId)
1074             yaz_log(LOG_LOG, "%simplementationId: %s",
1075                     m_session_str, apdu->u.initRequest->implementationId);
1076         if (apdu->u.initRequest->implementationName)
1077             yaz_log(LOG_LOG, "%simplementationName: %s",
1078                     m_session_str, apdu->u.initRequest->implementationName);
1079         if (apdu->u.initRequest->implementationVersion)
1080             yaz_log(LOG_LOG, "%simplementationVersion: %s",
1081                     m_session_str, apdu->u.initRequest->implementationVersion);
1082         if (m_client->m_init_flag)
1083         {
1084             Z_APDU *apdu = m_client->m_initResponse;
1085             apdu->u.initResponse->otherInfo = 0;
1086             if (m_client->m_cookie && *m_client->m_cookie)
1087                 set_otherInformationString(apdu, VAL_COOKIE, 1,
1088                                            m_client->m_cookie);
1089             send_to_client(apdu);
1090             return;
1091         }
1092         m_client->m_init_flag = 1;
1093     }
1094     handle_max_record_retrieve(apdu);
1095
1096     if (apdu)
1097         apdu = handle_syntax_validation(apdu);
1098
1099     if (apdu)
1100         apdu = handle_query_validation(apdu);
1101
1102     if (apdu)
1103         apdu = result_set_optimize(apdu);
1104     if (!apdu)
1105     {
1106         m_client->timeout(m_target_idletime);  // mark it active even 
1107         // though we didn't use it
1108         return;
1109     }
1110
1111     // delete other info part from PDU before sending to target
1112     Z_OtherInformation **oi;
1113     get_otherInfoAPDU(apdu, &oi);
1114     if (oi)
1115         *oi = 0;
1116
1117     if (apdu->which == Z_APDU_presentRequest &&
1118         m_client->m_resultSetStartPoint == 0)
1119     {
1120         Z_PresentRequest *pr = apdu->u.presentRequest;
1121         m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
1122         m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
1123     } else {
1124         m_client->m_resultSetStartPoint = 0;
1125     }
1126     if (m_client->send_to_target(apdu) < 0)
1127     {
1128         delete m_client;
1129         m_client = 0;
1130         delete this;
1131     }
1132     else
1133         m_client->m_waiting = 1;
1134 }
1135
1136 void Yaz_Proxy::connectNotify()
1137 {
1138 }
1139
1140 void Yaz_Proxy::shutdown()
1141 {
1142     // only keep if keep_alive flag is set...
1143     if (m_client && 
1144         !m_invalid_session &&
1145         m_client->m_pdu_recv < m_keepalive_limit_pdu &&
1146         m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
1147         m_client->m_waiting == 0)
1148     {
1149         yaz_log(LOG_LOG, "%sShutdown (client to proxy) keepalive %s",
1150                  m_session_str,
1151                  m_client->get_hostname());
1152         yaz_log(LOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
1153                 m_session_str, m_client->m_pdu_recv,
1154                 m_client->m_bytes_sent + m_client->m_bytes_recv,
1155                 m_keepalive_limit_bw, m_keepalive_limit_pdu);
1156         assert (m_client->m_waiting != 2);
1157         // Tell client (if any) that no server connection is there..
1158         m_client->m_server = 0;
1159     }
1160     else if (m_client)
1161     {
1162         yaz_log (LOG_LOG, "%sShutdown (client to proxy) close %s",
1163                  m_session_str,
1164                  m_client->get_hostname());
1165         assert (m_client->m_waiting != 2);
1166         delete m_client;
1167     }
1168     else if (!m_parent)
1169     {
1170         yaz_log (LOG_LOG, "%sshutdown (client to proxy) bad state",
1171                  m_session_str);
1172         assert (m_parent);
1173     }
1174     else 
1175     {
1176         yaz_log (LOG_LOG, "%sShutdown (client to proxy)",
1177                  m_session_str);
1178     }
1179     if (m_parent)
1180         m_parent->pre_init();
1181     delete this;
1182 }
1183
1184 const char *Yaz_ProxyClient::get_session_str() 
1185 {
1186     if (!m_server)
1187         return "0 ";
1188     return m_server->get_session_str();
1189 }
1190
1191 void Yaz_ProxyClient::shutdown()
1192 {
1193     yaz_log (LOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
1194              get_hostname());
1195     delete m_server;
1196     delete this;
1197 }
1198
1199 void Yaz_Proxy::failNotify()
1200 {
1201     yaz_log (LOG_LOG, "%sConnection closed by client",
1202              get_session_str());
1203     shutdown();
1204 }
1205
1206 void Yaz_ProxyClient::failNotify()
1207 {
1208     yaz_log (LOG_LOG, "%sConnection closed by target %s", 
1209              get_session_str(), get_hostname());
1210     shutdown();
1211 }
1212
1213 void Yaz_ProxyClient::connectNotify()
1214 {
1215     const char *s = get_session_str();
1216     const char *h = get_hostname();
1217     yaz_log (LOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
1218              m_target_idletime);
1219     timeout(m_target_idletime);
1220     if (!m_server)
1221         pre_init_client();
1222 }
1223
1224 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
1225                                                   *the_PDU_Observable, int fd)
1226 {
1227     return new Yaz_ProxyClient(the_PDU_Observable, 0);
1228 }
1229
1230 Yaz_ProxyClient::~Yaz_ProxyClient()
1231 {
1232     if (m_prev)
1233         *m_prev = m_next;
1234     if (m_next)
1235         m_next->m_prev = m_prev;
1236     m_waiting = 2;     // for debugging purposes only.
1237     odr_destroy(m_init_odr);
1238     delete m_last_query;
1239     xfree (m_last_resultSetId);
1240     xfree (m_cookie);
1241 }
1242
1243 void Yaz_ProxyClient::pre_init_client()
1244 {
1245     Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
1246     Z_InitRequest *req = apdu->u.initRequest;
1247     
1248     ODR_MASK_SET(req->options, Z_Options_search);
1249     ODR_MASK_SET(req->options, Z_Options_present);
1250     ODR_MASK_SET(req->options, Z_Options_namedResultSets);
1251     ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
1252     ODR_MASK_SET(req->options, Z_Options_scan);
1253     ODR_MASK_SET(req->options, Z_Options_sort);
1254     ODR_MASK_SET(req->options, Z_Options_extendedServices);
1255     ODR_MASK_SET(req->options, Z_Options_delSet);
1256     
1257     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
1258     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
1259     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
1260     
1261     if (send_to_target(apdu) < 0)
1262     {
1263         delete this;
1264     }
1265     else
1266     {
1267         m_waiting = 1;
1268         m_init_flag = 1;
1269     }
1270 }
1271
1272 void Yaz_Proxy::pre_init()
1273 {
1274     int i;
1275     const char *name = 0;
1276     const char *zurl_in_use[MAX_ZURL_PLEX];
1277     int limit_bw, limit_pdu, limit_req;
1278     int target_idletime, client_idletime;
1279     int max_clients;
1280     int keepalive_limit_bw, keepalive_limit_pdu;
1281     int pre_init;
1282     int log_mask = 0;
1283
1284     Yaz_ProxyConfig *cfg = check_reconfigure();
1285
1286     yaz_log(LOG_LOG, "pre_init");
1287     zurl_in_use[0] = 0;
1288     for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
1289                                           &limit_bw, &limit_pdu, &limit_req,
1290                                           &target_idletime, &client_idletime,
1291                                           &max_clients, 
1292                                           &keepalive_limit_bw,
1293                                           &keepalive_limit_pdu,
1294                                           &pre_init, &log_mask) ; i++)
1295     {
1296         if (pre_init)
1297         {
1298             int j;
1299             for (j = 0; zurl_in_use[j]; j++)
1300             {
1301                 Yaz_ProxyClient *c;
1302                 int spare = 0;
1303                 for (c = m_clientPool; c; c = c->m_next)
1304                 {
1305                     if (!strcmp(zurl_in_use[j], c->get_hostname())
1306                         && c->m_server == 0 && c->m_cookie == 0)
1307                         spare++;
1308                 }
1309                 yaz_log(LOG_LOG, "pre_init %s %s spare=%d pre_init=%d",
1310                         name, zurl_in_use[j], spare, pre_init);
1311                 if (spare < pre_init)
1312                 {
1313                     c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
1314                     c->m_next = m_clientPool;
1315                     if (c->m_next)
1316                         c->m_next->m_prev = &c->m_next;
1317                     m_clientPool = c;
1318                     c->m_prev = &m_clientPool;
1319                     
1320                     if (c->client(zurl_in_use[j]))
1321                     {
1322                         timeout(60);
1323                         delete c;
1324                         return;
1325                     }
1326                     c->timeout(30);
1327                     c->m_waiting = 1;
1328                     c->m_target_idletime = target_idletime;
1329                     yaz_log(LOG_LOG, "pre_init name=%s zurl=%s timeout=%d", name,
1330                             zurl_in_use[j], target_idletime);
1331                     c->m_seqno = m_seqno++;
1332                 }
1333             }
1334         }
1335     }
1336 }
1337
1338 void Yaz_Proxy::timeoutNotify()
1339 {
1340     if (m_parent)
1341     {
1342         if (m_bw_hold_PDU)
1343         {
1344             timeout(m_client_idletime);
1345             Z_APDU *apdu = m_bw_hold_PDU;
1346             m_bw_hold_PDU = 0;
1347             recv_Z_PDU_0(apdu);
1348         }
1349         else
1350         {
1351             yaz_log (LOG_LOG, "%sTimeout (client to proxy)", m_session_str);
1352             shutdown();
1353         }
1354     }
1355     else
1356     {
1357         timeout(600);
1358         pre_init();
1359     }
1360 }
1361
1362 void Yaz_ProxyClient::timeoutNotify()
1363 {
1364     yaz_log (LOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
1365              get_hostname());
1366     m_waiting = 1;
1367     m_root->pre_init();
1368     shutdown();
1369 }
1370
1371 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
1372                                  Yaz_Proxy *parent) :
1373     Yaz_Z_Assoc (the_PDU_Observable)
1374 {
1375     m_cookie = 0;
1376     m_next = 0;
1377     m_prev = 0;
1378     m_init_flag = 0;
1379     m_last_query = 0;
1380     m_last_resultSetId = 0;
1381     m_last_resultCount = 0;
1382     m_last_ok = 0;
1383     m_sr_transform = 0;
1384     m_waiting = 0;
1385     m_init_odr = odr_createmem (ODR_DECODE);
1386     m_initResponse = 0;
1387     m_resultSetStartPoint = 0;
1388     m_bytes_sent = m_bytes_recv = 0;
1389     m_pdu_recv = 0;
1390     m_server = 0;
1391     m_seqno = 0;
1392     m_target_idletime = 600;
1393     m_root = parent;
1394 }
1395
1396 const char *Yaz_Proxy::option(const char *name, const char *value)
1397 {
1398     if (!strcmp (name, "optimize")) {
1399         if (value) {
1400             xfree (m_optimize); 
1401             m_optimize = xstrdup (value);
1402         }
1403         return m_optimize;
1404     }
1405     return 0;
1406 }
1407
1408 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
1409 {
1410     m_bytes_recv += len;
1411     m_pdu_recv++;
1412     m_waiting = 0;
1413     yaz_log (LOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
1414              apdu_name(apdu), get_hostname(), len);
1415     if (apdu->which == Z_APDU_initResponse)
1416     {
1417         if (!m_server)  // if this is a pre init session , check for more
1418             m_root->pre_init();
1419         NMEM nmem = odr_extract_mem (odr_decode());
1420         odr_reset (m_init_odr);
1421         nmem_transfer (m_init_odr->mem, nmem);
1422         m_initResponse = apdu;
1423
1424         Z_InitResponse *ir = apdu->u.initResponse;
1425         char *im0 = ir->implementationName;
1426         
1427         char *im1 = (char*) 
1428             odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
1429         *im1 = '\0';
1430         if (im0)
1431         {
1432             strcat(im1, im0);
1433             strcat(im1, " ");
1434         }
1435         strcat(im1, "(YAZ Proxy)");
1436         ir->implementationName = im1;
1437
1438         nmem_destroy (nmem);
1439     }
1440     if (apdu->which == Z_APDU_searchResponse)
1441     {
1442         Z_SearchResponse *sr = apdu->u.searchResponse;
1443         m_last_resultCount = *sr->resultCount;
1444         int status = *sr->searchStatus;
1445         if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
1446         {
1447             m_last_ok = 1;
1448             
1449             if (sr->records && sr->records->which == Z_Records_DBOSD)
1450             {
1451                 m_cache.add(odr_decode(),
1452                             sr->records->u.databaseOrSurDiagnostics, 1,
1453                             *sr->resultCount);
1454             }
1455         }
1456     }
1457     if (apdu->which == Z_APDU_presentResponse)
1458     {
1459         Z_PresentResponse *pr = apdu->u.presentResponse;
1460         if (m_sr_transform)
1461         {
1462             m_sr_transform = 0;
1463             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1464             Z_SearchResponse *sr = new_apdu->u.searchResponse;
1465             sr->referenceId = pr->referenceId;
1466             *sr->resultCount = m_last_resultCount;
1467             sr->records = pr->records;
1468             sr->nextResultSetPosition = pr->nextResultSetPosition;
1469             sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
1470             apdu = new_apdu;
1471         }
1472         if (pr->records && 
1473             pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
1474         {
1475             m_cache.add(odr_decode(),
1476                         pr->records->u.databaseOrSurDiagnostics,
1477                         m_resultSetStartPoint, -1);
1478             m_resultSetStartPoint = 0;
1479         }
1480     }
1481     if (m_cookie)
1482         set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
1483     if (m_server)
1484     {
1485         m_server->send_to_client(apdu);
1486     }
1487     if (apdu->which == Z_APDU_close)
1488     {
1489         shutdown();
1490     }
1491 }
1492
1493 void Yaz_Proxy::server(const char *addr)
1494 {
1495     Yaz_Z_Assoc::server(addr);
1496
1497     timeout(1);
1498 }
1499