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