Move yaz-proxy to separate sub directory.
[yazpp-moved-to-github.git] / proxy / yaz-proxy.cpp
1 /* $Id: yaz-proxy.cpp,v 1.1 2004-03-29 22:46:51 adam Exp $
2    Copyright (c) 1998-2004, Index Data.
3
4 This file is part of the yaz-proxy.
5
6 Zebra is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Zebra; see the file LICENSE.proxy.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include <unistd.h>
23 #include <assert.h>
24 #include <time.h>
25 #include <sys/types.h>
26 #include <fcntl.h>
27
28 #include <yaz/srw.h>
29 #include <yaz/marcdisp.h>
30 #include <yaz/yaz-iconv.h>
31 #include <yaz/log.h>
32 #include <yaz/diagbib1.h>
33 #include <yaz++/proxy/proxy.h>
34 #include <yaz/pquery.h>
35
36 static const char *apdu_name(Z_APDU *apdu)
37 {
38     switch (apdu->which)
39     {
40     case Z_APDU_initRequest:
41         return "initRequest";
42     case Z_APDU_initResponse:
43         return "initResponse";
44     case Z_APDU_searchRequest:
45         return "searchRequest";
46     case Z_APDU_searchResponse:
47         return "searchResponse";
48     case Z_APDU_presentRequest:
49         return "presentRequest";
50     case Z_APDU_presentResponse:
51         return "presentResponse";
52     case Z_APDU_deleteResultSetRequest:
53         return "deleteResultSetRequest";
54     case Z_APDU_deleteResultSetResponse:
55         return "deleteResultSetResponse";
56     case Z_APDU_scanRequest:
57         return "scanRequest";
58     case Z_APDU_scanResponse:
59         return "scanResponse";
60     case Z_APDU_sortRequest:
61         return "sortRequest";
62     case Z_APDU_sortResponse:
63         return "sortResponse";
64     case Z_APDU_extendedServicesRequest:
65         return "extendedServicesRequest";
66     case Z_APDU_extendedServicesResponse:
67         return "extendedServicesResponse";
68     case Z_APDU_close:
69         return "close";
70     }
71     return "other";
72 }
73
74 static const char *gdu_name(Z_GDU *gdu)
75 {
76     switch(gdu->which)
77     {
78     case Z_GDU_Z3950:
79         return apdu_name(gdu->u.z3950);
80     case Z_GDU_HTTP_Request:
81         return "HTTP Request";
82     case Z_GDU_HTTP_Response:
83         return "HTTP Response";
84     }
85     return "Unknown request/response";
86 }
87
88 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
89                      Yaz_Proxy *parent) :
90     Yaz_Z_Assoc(the_PDU_Observable), m_bw_stat(60), m_pdu_stat(60)
91 {
92     m_PDU_Observable = the_PDU_Observable;
93     m_client = 0;
94     m_parent = parent;
95     m_clientPool = 0;
96     m_seqno = 1;
97     m_keepalive_limit_bw = 500000;
98     m_keepalive_limit_pdu = 1000;
99     m_proxyTarget = 0;
100     m_default_target = 0;
101     m_proxy_authentication = 0;
102     m_max_clients = 150;
103     m_log_mask = 0;
104     m_seed = time(0);
105     m_client_idletime = 600;
106     m_target_idletime = 600;
107     m_optimize = xstrdup ("1");
108     strcpy(m_session_str, "0 ");
109     m_session_no=0;
110     m_bytes_sent = 0;
111     m_bytes_recv = 0;
112     m_bw_hold_PDU = 0;
113     m_bw_max = 0;
114     m_pdu_max = 0;
115     m_max_record_retrieve = 0;
116     m_reconfig_flag = 0;
117     m_config_fname = 0;
118     m_request_no = 0;
119     m_invalid_session = 0;
120     m_referenceId = 0;
121     m_referenceId_mem = nmem_create();
122     m_config = 0;
123     m_marcxml_flag = 0;
124     m_stylesheet_xsp = 0;
125     m_stylesheet_nprl = 0;
126     m_s2z_stylesheet = 0;
127     m_s2z_database = 0;
128     m_schema = 0;
129     m_initRequest_apdu = 0;
130     m_initRequest_mem = 0;
131     m_initRequest_preferredMessageSize = 0;
132     m_initRequest_maximumRecordSize = 0;
133     m_initRequest_options = 0;
134     m_initRequest_version = 0;
135     m_apdu_invalid_session = 0;
136     m_mem_invalid_session = 0;
137     m_s2z_odr_init = 0;
138     m_s2z_odr_search = 0;
139     m_s2z_init_apdu = 0;
140     m_s2z_search_apdu = 0;
141     m_s2z_present_apdu = 0;
142     m_http_keepalive = 0;
143     m_http_version = 0;
144     m_soap_ns = 0;
145     m_s2z_packing = Z_SRW_recordPacking_string;
146     m_time_tv.tv_sec = 0;
147     m_time_tv.tv_usec = 0;
148     if (!m_parent)
149         low_socket_open();
150 }
151
152 Yaz_Proxy::~Yaz_Proxy()
153 {
154     yaz_log(LOG_LOG, "%sClosed %d/%d sent/recv bytes total", m_session_str,
155             m_bytes_sent, m_bytes_recv);
156     nmem_destroy(m_initRequest_mem);
157     nmem_destroy(m_mem_invalid_session);
158     nmem_destroy(m_referenceId_mem);
159
160     xfree (m_proxyTarget);
161     xfree (m_default_target);
162     xfree (m_proxy_authentication);
163     xfree (m_optimize);
164
165     if (m_stylesheet_xsp)
166         xsltFreeStylesheet(m_stylesheet_xsp);
167
168     xfree (m_schema);
169     if (m_s2z_odr_init)
170         odr_destroy(m_s2z_odr_init);
171     if (m_s2z_odr_search)
172         odr_destroy(m_s2z_odr_search);
173     if (!m_parent)
174         low_socket_close();
175     delete m_config;
176 }
177
178 int Yaz_Proxy::set_config(const char *config)
179 {
180     delete m_config;
181     m_config = new Yaz_ProxyConfig();
182     xfree(m_config_fname);
183     m_config_fname = xstrdup(config);
184     int r = m_config->read_xml(config);
185     if (!r)
186         m_config->get_generic_info(&m_log_mask, &m_max_clients);
187     return r;
188 }
189
190 void Yaz_Proxy::set_default_target(const char *target)
191 {
192     xfree (m_default_target);
193     m_default_target = 0;
194     if (target)
195         m_default_target = (char *) xstrdup (target);
196 }
197
198 void Yaz_Proxy::set_proxy_authentication (const char *auth)
199 {
200     xfree (m_proxy_authentication);
201     m_proxy_authentication = 0;
202     if (auth)
203         m_proxy_authentication = (char *) xstrdup (auth);
204 }
205
206 Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
207 {
208     if (m_parent)
209         return m_parent->check_reconfigure();
210
211     Yaz_ProxyConfig *cfg = m_config;
212     if (m_reconfig_flag)
213     {
214         yaz_log(LOG_LOG, "reconfigure");
215         yaz_log_reopen();
216         if (m_config_fname && cfg)
217         {
218             yaz_log(LOG_LOG, "reconfigure config %s", m_config_fname);
219             int r = cfg->read_xml(m_config_fname);
220             if (r)
221                 yaz_log(LOG_WARN, "reconfigure failed");
222             else
223             {
224                 m_log_mask = 0;
225                 cfg->get_generic_info(&m_log_mask, &m_max_clients);
226             }
227         }
228         else
229             yaz_log(LOG_LOG, "reconfigure");
230         m_reconfig_flag = 0;
231     }
232     return cfg;
233 }
234
235 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
236                                             *the_PDU_Observable, int fd)
237 {
238     check_reconfigure();
239     Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable, this);
240     new_proxy->m_config = 0;
241     new_proxy->m_config_fname = 0;
242     new_proxy->timeout(m_client_idletime);
243     new_proxy->m_target_idletime = m_target_idletime;
244     new_proxy->set_default_target(m_default_target);
245     new_proxy->m_max_clients = m_max_clients;
246     new_proxy->m_log_mask = m_log_mask;
247     new_proxy->set_APDU_log(get_APDU_log());
248     if (m_log_mask & PROXY_LOG_APDU_CLIENT)
249         new_proxy->set_APDU_yazlog(1);
250     else
251         new_proxy->set_APDU_yazlog(0);
252     new_proxy->set_proxy_authentication(m_proxy_authentication);
253     sprintf(new_proxy->m_session_str, "%ld:%d ", (long) time(0), m_session_no);
254     m_session_no++;
255     yaz_log (LOG_LOG, "%sNew session %s", new_proxy->m_session_str,
256              the_PDU_Observable->getpeername());
257     return new_proxy;
258 }
259
260 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
261 {
262     int oid[OID_SIZE];
263     Z_OtherInformationUnit *oi;
264     struct oident ent;
265     ent.proto = PROTO_Z3950;
266     ent.oclass = CLASS_USERINFO;
267     ent.value = (oid_value) VAL_COOKIE;
268     assert (oid_ent_to_oid (&ent, oid));
269
270     if (oid_ent_to_oid (&ent, oid) && 
271         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
272         oi->which == Z_OtherInfo_characterInfo)
273         return oi->information.characterInfo;
274     return 0;
275 }
276
277 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
278 {
279     int oid[OID_SIZE];
280     Z_OtherInformationUnit *oi;
281     struct oident ent;
282     ent.proto = PROTO_Z3950;
283     ent.oclass = CLASS_USERINFO;
284     ent.value = (oid_value) VAL_PROXY;
285     if (oid_ent_to_oid (&ent, oid) &&
286         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
287         oi->which == Z_OtherInfo_characterInfo)
288         return oi->information.characterInfo;
289     return 0;
290 }
291
292 const char *Yaz_Proxy::load_balance(const char **url)
293 {
294     int zurl_in_use[MAX_ZURL_PLEX];
295     int zurl_in_spare[MAX_ZURL_PLEX];
296     Yaz_ProxyClient *c;
297     int i;
298
299     for (i = 0; i<MAX_ZURL_PLEX; i++)
300     {
301         zurl_in_use[i] = 0;
302         zurl_in_spare[i] = 0;
303     }
304     for (c = m_parent->m_clientPool; c; c = c->m_next)
305     {
306         for (i = 0; url[i]; i++)
307             if (!strcmp(url[i], c->get_hostname()))
308             {
309                 zurl_in_use[i]++;
310                 if (c->m_cookie == 0 && c->m_server == 0 && c->m_waiting == 0)
311                     zurl_in_spare[i]++;
312             }
313     }
314     int min_use = 100000;
315     int spare_for_min = 0;
316     int max_spare = 0;
317     const char *ret_min = 0;
318     const char *ret_spare = 0;
319     for (i = 0; url[i]; i++)
320     {
321         yaz_log(LOG_DEBUG, "%szurl=%s use=%d spare=%d",
322                 m_session_str, url[i], zurl_in_use[i], zurl_in_spare[i]);
323         if (min_use > zurl_in_use[i])
324         {
325             ret_min = url[i];
326             min_use = zurl_in_use[i];
327             spare_for_min = zurl_in_spare[i];
328         }
329         if (max_spare < zurl_in_spare[i]) 
330         {
331             ret_spare = url[i];
332             max_spare = zurl_in_spare[i];
333         }
334     }
335     // use the one with minimum connections if spare is > 3
336     if (spare_for_min > 3)
337         return ret_min;
338     // use one with most spares (if any)
339     if (max_spare > 0)
340         return ret_spare;
341     return ret_min;
342 }
343
344 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
345                                        const char *proxy_host)
346 {
347     assert (m_parent);
348     Yaz_Proxy *parent = m_parent;
349     Yaz_ProxyClient *c = m_client;
350     
351     if (!m_proxyTarget)
352     {
353         const char *url[MAX_ZURL_PLEX];
354         Yaz_ProxyConfig *cfg = check_reconfigure();
355         if (proxy_host)
356         {
357 #if 1
358 /* only to be enabled for debugging... */
359             if (!strcmp(proxy_host, "stop"))
360                 exit(0);
361 #endif
362             xfree(m_default_target);
363             m_default_target = xstrdup(proxy_host);
364             proxy_host = m_default_target;
365         }
366         int client_idletime = -1;
367         const char *cql2rpn_fname = 0;
368         url[0] = m_default_target;
369         url[1] = 0;
370         if (cfg)
371         {
372             int pre_init = 0;
373             cfg->get_target_info(proxy_host, url, &m_bw_max,
374                                  &m_pdu_max, &m_max_record_retrieve,
375                                  &m_target_idletime, &client_idletime,
376                                  &parent->m_max_clients,
377                                  &m_keepalive_limit_bw,
378                                  &m_keepalive_limit_pdu,
379                                  &pre_init,
380                                  &cql2rpn_fname);
381         }
382         if (client_idletime != -1)
383         {
384             m_client_idletime = client_idletime;
385             timeout(m_client_idletime);
386         }
387         if (cql2rpn_fname)
388             m_cql2rpn.set_pqf_file(cql2rpn_fname);
389         if (!url[0])
390         {
391             yaz_log(LOG_LOG, "%sNo default target", m_session_str);
392             return 0;
393         }
394         // we don't handle multiplexing for cookie session, so we just
395         // pick the first one in this case (anonymous users will be able
396         // to use any backend)
397         if (cookie && *cookie)
398             m_proxyTarget = (char*) xstrdup(url[0]);
399         else
400             m_proxyTarget = (char*) xstrdup(load_balance(url));
401     }
402     if (cookie && *cookie)
403     {   // search in sessions with a cookie
404         for (c = parent->m_clientPool; c; c = c->m_next)
405         {
406             assert (c->m_prev);
407             assert (*c->m_prev == c);
408             if (c->m_cookie && !strcmp(cookie,c->m_cookie) &&
409                 !strcmp(m_proxyTarget, c->get_hostname()))
410             {
411                 // Found it in cache
412                 // The following handles "cancel"
413                 // If connection is busy (waiting for PDU) and
414                 // we have an initRequest we can safely do re-open
415                 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
416                 {
417                     yaz_log (LOG_LOG, "%s REOPEN target=%s", m_session_str,
418                              c->get_hostname());
419                     c->close();
420                     c->m_init_flag = 0;
421                     
422                     c->m_last_ok = 0;
423                     c->m_cache.clear();
424                     c->m_last_resultCount = 0;
425                     c->m_sr_transform = 0;
426                     c->m_waiting = 0;
427                     c->m_resultSetStartPoint = 0;
428                     c->m_target_idletime = m_target_idletime;
429                     if (c->client(m_proxyTarget))
430                     {
431                         delete c;
432                         return 0;
433                     }
434                     c->timeout(30); 
435                 }
436                 c->m_seqno = parent->m_seqno;
437                 if (c->m_server && c->m_server != this)
438                     c->m_server->m_client = 0;
439                 c->m_server = this;
440                 (parent->m_seqno)++;
441                 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
442                 return c;
443             }
444         }
445     }
446     else if (!c)
447     {
448         // don't have a client session yet. Search in session w/o cookie
449         for (c = parent->m_clientPool; c; c = c->m_next)
450         {
451             assert (c->m_prev);
452             assert (*c->m_prev == c);
453             if (c->m_server == 0 && c->m_cookie == 0 && 
454                 c->m_waiting == 0 &&
455                 !strcmp(m_proxyTarget, c->get_hostname()))
456             {
457                 // found it in cache
458                 yaz_log (LOG_LOG, "%sREUSE %d %s",
459                          m_session_str, parent->m_seqno, c->get_hostname());
460                 
461                 c->m_seqno = parent->m_seqno;
462                 assert(c->m_server == 0);
463                 c->m_server = this;
464
465                 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
466                     c->set_APDU_yazlog(1);
467                 else
468                     c->set_APDU_yazlog(0);
469
470                 (parent->m_seqno)++;
471                 
472                 parent->pre_init();
473                 
474                 return c;
475             }
476         }
477     }
478     if (!m_client)
479     {
480         if (apdu->which != Z_APDU_initRequest)
481         {
482             yaz_log (LOG_LOG, "%sno init request as first PDU", m_session_str);
483             return 0;
484         }
485         Z_InitRequest *initRequest = apdu->u.initRequest;
486
487         if (!initRequest->idAuthentication)
488         {
489             if (m_proxy_authentication)
490             {
491                 initRequest->idAuthentication =
492                     (Z_IdAuthentication *)
493                     odr_malloc (odr_encode(),
494                                 sizeof(*initRequest->idAuthentication));
495                 initRequest->idAuthentication->which =
496                     Z_IdAuthentication_open;
497                 initRequest->idAuthentication->u.open =
498                     odr_strdup (odr_encode(), m_proxy_authentication);
499             }
500         }
501         // go through list of clients - and find the lowest/oldest one.
502         Yaz_ProxyClient *c_min = 0;
503         int min_seq = -1;
504         int no_of_clients = 0;
505         if (parent->m_clientPool)
506             yaz_log (LOG_DEBUG, "Existing sessions");
507         for (c = parent->m_clientPool; c; c = c->m_next)
508         {
509             yaz_log (LOG_DEBUG, " Session %-3d wait=%d %s cookie=%s", c->m_seqno,
510                                c->m_waiting, c->get_hostname(),
511                                c->m_cookie ? c->m_cookie : "");
512             no_of_clients++;
513             if (min_seq < 0 || c->m_seqno < min_seq)
514             {
515                 min_seq = c->m_seqno;
516                 c_min = c;
517             }
518         }
519         if (no_of_clients >= parent->m_max_clients)
520         {
521             c = c_min;
522             if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
523             {
524                 yaz_log (LOG_LOG, "%sMAXCLIENTS %d Destroy %d",
525                          m_session_str, parent->m_max_clients, c->m_seqno);
526                 if (c->m_server && c->m_server != this)
527                     delete c->m_server;
528                 c->m_server = 0;
529             }
530             else
531             {
532                 yaz_log (LOG_LOG, "%sMAXCLIENTS %d Reuse %d %d %s",
533                          m_session_str, parent->m_max_clients,
534                          c->m_seqno, parent->m_seqno, c->get_hostname());
535                 xfree (c->m_cookie);
536                 c->m_cookie = 0;
537                 if (cookie)
538                     c->m_cookie = xstrdup(cookie);
539                 c->m_seqno = parent->m_seqno;
540                 if (c->m_server && c->m_server != this)
541                 {
542                     c->m_server->m_client = 0;
543                     delete c->m_server;
544                 }
545                 (parent->m_seqno)++;
546                 c->m_target_idletime = m_target_idletime;
547                 c->timeout(m_target_idletime);
548                 
549                 if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
550                     c->set_APDU_yazlog(1);
551                 else
552                     c->set_APDU_yazlog(0);
553
554                 return c;
555             }
556         }
557         else
558         {
559             yaz_log (LOG_LOG, "%sNEW %d %s",
560                      m_session_str, parent->m_seqno, m_proxyTarget);
561             c = new Yaz_ProxyClient(m_PDU_Observable->clone(), parent);
562             c->m_next = parent->m_clientPool;
563             if (c->m_next)
564                 c->m_next->m_prev = &c->m_next;
565             parent->m_clientPool = c;
566             c->m_prev = &parent->m_clientPool;
567         }
568
569         xfree (c->m_cookie);
570         c->m_cookie = 0;
571         if (cookie)
572             c->m_cookie = xstrdup(cookie);
573
574         c->m_seqno = parent->m_seqno;
575         c->m_init_flag = 0;
576         c->m_last_resultCount = 0;
577         c->m_last_ok = 0;
578         c->m_cache.clear();
579         c->m_sr_transform = 0;
580         c->m_waiting = 0;
581         c->m_resultSetStartPoint = 0;
582         (parent->m_seqno)++;
583         if (c->client(m_proxyTarget))
584         {
585             delete c;
586             return 0;
587         }
588         c->m_target_idletime = m_target_idletime;
589         c->timeout(30);
590
591         if (parent->m_log_mask & PROXY_LOG_APDU_SERVER)
592             c->set_APDU_yazlog(1);
593         else
594             c->set_APDU_yazlog(0);
595     }
596     yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
597     return c;
598 }
599
600 void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
601 {
602     int i;
603     for (i = 0; i<num; i++)
604     {
605         oident *ent;
606         Z_DefaultDiagFormat *r;
607         Z_DiagRec *p = pp[i];
608         if (p->which != Z_DiagRec_defaultFormat)
609         {
610             yaz_log(LOG_LOG, "%sError no diagnostics", m_session_str);
611             return;
612         }
613         else
614             r = p->u.defaultFormat;
615         if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
616             ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
617             yaz_log(LOG_LOG, "%sError unknown diagnostic set", m_session_str);
618         switch (r->which)
619         {
620         case Z_DefaultDiagFormat_v2Addinfo:
621             yaz_log(LOG_LOG, "%sError %d %s:%s",
622                     m_session_str,
623                     *r->condition, diagbib1_str(*r->condition),
624                     r->u.v2Addinfo);
625             break;
626         case Z_DefaultDiagFormat_v3Addinfo:
627             yaz_log(LOG_LOG, "%sError %d %s:%s",
628                     m_session_str,
629                     *r->condition, diagbib1_str(*r->condition),
630                     r->u.v3Addinfo);
631             break;
632         }
633     }
634 }
635
636 int Yaz_Proxy::convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu)
637 {
638     if (!m_stylesheet_xsp || p->num_records <= 0)
639         return 0;  /* no XSLT to be done ... */
640
641     m_stylesheet_offset = 0;
642     m_stylesheet_nprl = p;
643     m_stylesheet_apdu = apdu;
644     timeout(0);
645     return 1;
646 }
647
648 void Yaz_Proxy::convert_xsl_delay()
649 {
650     Z_NamePlusRecord *npr = m_stylesheet_nprl->records[m_stylesheet_offset];
651     if (npr->which == Z_NamePlusRecord_databaseRecord)
652     {
653         Z_External *r = npr->u.databaseRecord;
654         if (r->which == Z_External_octet)
655         {
656 #if 0
657             fwrite((char*) r->u.octet_aligned->buf, 1, r->u.octet_aligned->len, stdout);
658 #endif
659             xmlDocPtr res, doc = xmlParseMemory(
660                 (char*) r->u.octet_aligned->buf,
661                 r->u.octet_aligned->len);
662
663             
664             yaz_log(LOG_LOG, "%sXSLT convert %d",
665                     m_session_str, m_stylesheet_offset);
666             res = xsltApplyStylesheet(m_stylesheet_xsp, doc, 0);
667
668             if (res)
669             {
670                 xmlChar *out_buf;
671                 int out_len;
672                 xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
673                 
674                 m_stylesheet_nprl->records[m_stylesheet_offset]->
675                     u.databaseRecord = 
676                     z_ext_record(odr_encode(), VAL_TEXT_XML,
677                                  (char*) out_buf, out_len);
678                 xmlFree(out_buf);
679                 xmlFreeDoc(res);
680             }
681
682             xmlFreeDoc(doc);
683         }
684     }
685     m_stylesheet_offset++;
686     if (m_stylesheet_offset == m_stylesheet_nprl->num_records)
687     {
688         m_stylesheet_nprl = 0;
689         if (m_stylesheet_xsp)
690             xsltFreeStylesheet(m_stylesheet_xsp);
691         m_stylesheet_xsp = 0;
692         timeout(m_client_idletime);
693         send_PDU_convert(m_stylesheet_apdu);
694     }
695     else
696         timeout(0);
697 }
698
699 void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
700 {
701     int i;
702
703     yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
704     yaz_marc_t mt = yaz_marc_create();
705     yaz_marc_xml(mt, YAZ_MARC_MARCXML);
706     yaz_marc_iconv(mt, cd);
707     for (i = 0; i < p->num_records; i++)
708     {
709         Z_NamePlusRecord *npr = p->records[i];
710         if (npr->which == Z_NamePlusRecord_databaseRecord)
711         {
712             Z_External *r = npr->u.databaseRecord;
713             if (r->which == Z_External_octet)
714             {
715                 int rlen;
716                 char *result;
717                 if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
718                                         r->u.octet_aligned->len,
719                                         &result, &rlen))
720                 {
721                     npr->u.databaseRecord = z_ext_record(odr_encode(),
722                                                          VAL_TEXT_XML,
723                                                          result, rlen);
724                 }
725             }
726         }
727     }
728     if (cd)
729         yaz_iconv_close(cd);
730     yaz_marc_destroy(mt);
731 }
732
733 void Yaz_Proxy::logtime()
734 {
735     if (m_time_tv.tv_sec)
736     {
737         struct timeval tv;
738         gettimeofday(&tv, 0);
739         long diff = (tv.tv_sec - m_time_tv.tv_sec)*1000000 +
740             (tv.tv_usec - m_time_tv.tv_usec);
741         if (diff >= 0)
742             yaz_log(LOG_LOG, "%sElapsed %ld.%03ld", m_session_str,
743                     diff/1000000, (diff/1000)%1000);
744     }
745     m_time_tv.tv_sec = 0;
746     m_time_tv.tv_usec = 0;
747 }
748
749 int Yaz_Proxy::send_http_response(int code)
750 {
751     ODR o = odr_encode();
752     Z_GDU *gdu = z_get_HTTP_Response(o, code);
753     Z_HTTP_Response *hres = gdu->u.HTTP_Response;
754     if (m_http_version)
755         hres->version = odr_strdup(o, m_http_version);
756     if (m_http_keepalive)
757         z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
758     else
759         timeout(0);
760     
761     if (m_log_mask & PROXY_LOG_REQ_CLIENT)
762     {
763         yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
764                  gdu_name(gdu));
765     }
766     int len;
767     int r = send_GDU(gdu, &len);
768     m_bytes_sent += len;
769     m_bw_stat.add_bytes(len);
770     logtime();
771     return r;
772 }
773
774 int Yaz_Proxy::send_srw_response(Z_SRW_PDU *srw_pdu)
775 {
776     ODR o = odr_encode();
777     const char *ctype = "text/xml";
778     Z_GDU *gdu = z_get_HTTP_Response(o, 200);
779     Z_HTTP_Response *hres = gdu->u.HTTP_Response;
780     if (m_http_version)
781         hres->version = odr_strdup(o, m_http_version);
782     z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
783     if (m_http_keepalive)
784         z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
785     else
786         timeout(0);
787
788     static Z_SOAP_Handler soap_handlers[2] = {
789 #if HAVE_XSLT
790         {"http://www.loc.gov/zing/srw/", 0,
791          (Z_SOAP_fun) yaz_srw_codec},
792 #endif
793         {0, 0, 0}
794     };
795     
796     Z_SOAP *soap_package = (Z_SOAP*) odr_malloc(o, sizeof(Z_SOAP));
797     soap_package->which = Z_SOAP_generic;
798     soap_package->u.generic = 
799         (Z_SOAP_Generic *) odr_malloc(o,  sizeof(*soap_package->u.generic));
800     soap_package->u.generic->no = 0;
801     soap_package->u.generic->ns = soap_handlers[0].ns;
802     soap_package->u.generic->p = (void *) srw_pdu;
803     soap_package->ns = m_soap_ns;
804     z_soap_codec_enc_xsl(o, &soap_package,
805                          &hres->content_buf, &hres->content_len,
806                          soap_handlers, 0, m_s2z_stylesheet);
807     if (m_log_mask & PROXY_LOG_REQ_CLIENT)
808     {
809         yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
810                  gdu_name(gdu));
811     }
812     int len;
813     int r = send_GDU(gdu, &len);
814     m_bytes_sent += len;
815     m_bw_stat.add_bytes(len);
816     logtime();
817     return r;
818 }
819
820 int Yaz_Proxy::send_to_srw_client_error(int srw_error, const char *add)
821 {
822     ODR o = odr_encode();
823     Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
824     Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
825
826     srw_res->num_diagnostics = 1;
827     srw_res->diagnostics = (Z_SRW_diagnostic *)
828         odr_malloc(o, sizeof(*srw_res->diagnostics));
829     yaz_mk_std_diagnostic(o, srw_res->diagnostics, srw_error, add);
830     return send_srw_response(srw_pdu);
831 }
832
833 int Yaz_Proxy::z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
834                              Z_DefaultDiagFormat *ddf)
835 {
836     int bib1_code = *ddf->condition;
837     if (bib1_code == 109)
838         return 404;
839     srw_res->num_diagnostics = 1;
840     srw_res->diagnostics = (Z_SRW_diagnostic *)
841         odr_malloc(o, sizeof(*srw_res->diagnostics));
842     yaz_mk_std_diagnostic(o, srw_res->diagnostics,
843                           yaz_diag_bib1_to_srw(*ddf->condition), 
844                           ddf->u.v2Addinfo);
845     return 0;
846 }
847
848 int Yaz_Proxy::send_to_srw_client_ok(int hits, Z_Records *records, int start)
849 {
850     ODR o = odr_encode();
851     Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
852     Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
853
854     srw_res->numberOfRecords = odr_intdup (o, hits);
855     if (records && records->which == Z_Records_DBOSD)
856     {
857         srw_res->num_records =
858             records->u.databaseOrSurDiagnostics->num_records;
859         int i;
860         srw_res->records = (Z_SRW_record *)
861             odr_malloc(o, srw_res->num_records * sizeof(Z_SRW_record));
862         for (i = 0; i < srw_res->num_records; i++)
863         {
864             Z_NamePlusRecord *npr = records->u.databaseOrSurDiagnostics->records[i];
865             if (npr->which != Z_NamePlusRecord_databaseRecord)
866             {
867                 srw_res->records[i].recordSchema = "diagnostic";
868                 srw_res->records[i].recordPacking = m_s2z_packing;
869                 srw_res->records[i].recordData_buf = "67";
870                 srw_res->records[i].recordData_len = 2;
871                 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
872                 continue;
873             }
874             Z_External *r = npr->u.databaseRecord;
875             oident *ent = oid_getentbyoid(r->direct_reference);
876             if (r->which == Z_External_octet && ent->value == VAL_TEXT_XML)
877             {
878                 srw_res->records[i].recordSchema = m_schema;
879                 srw_res->records[i].recordPacking = m_s2z_packing;
880                 srw_res->records[i].recordData_buf = (char*) 
881                     r->u.octet_aligned->buf;
882                 srw_res->records[i].recordData_len = r->u.octet_aligned->len;
883                 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
884             }
885             else
886             {
887                 srw_res->records[i].recordSchema = "diagnostic";
888                 srw_res->records[i].recordPacking = m_s2z_packing;
889                 srw_res->records[i].recordData_buf = "67";
890                 srw_res->records[i].recordData_len = 2;
891                 srw_res->records[i].recordPosition = odr_intdup(o, i+start);
892             }
893         }
894     }
895     if (records && records->which == Z_Records_NSD)
896     {
897         int http_code;
898         http_code = z_to_srw_diag(odr_encode(), srw_res,
899                                    records->u.nonSurrogateDiagnostic);
900         if (http_code)
901             return send_http_response(http_code);
902     }
903     return send_srw_response(srw_pdu);
904     
905 }
906
907 int Yaz_Proxy::send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
908                                         int num_diagnostics)
909 {
910     Yaz_ProxyConfig *cfg = check_reconfigure();
911     if (cfg)
912     {
913         int len;
914         char *b = cfg->get_explain(odr_encode(), 0 /* target */,
915                                    m_s2z_database, &len);
916         if (b)
917         {
918             Z_SRW_PDU *res = yaz_srw_get(odr_encode(), Z_SRW_explain_response);
919             Z_SRW_explainResponse *er = res->u.explain_response;
920
921             er->record.recordData_buf = b;
922             er->record.recordData_len = len;
923             er->record.recordPacking = m_s2z_packing;
924             er->record.recordSchema = "http://explain.z3950.org/dtd/2.0/";
925
926             er->diagnostics = diagnostics;
927             er->num_diagnostics = num_diagnostics;
928             return send_srw_response(res);
929         }
930     }
931     return send_http_response(404);
932 }
933
934 int Yaz_Proxy::send_PDU_convert(Z_APDU *apdu)
935 {
936     if (m_http_version)
937     {
938         if (apdu->which == Z_APDU_initResponse)
939         {
940             Z_InitResponse *res = apdu->u.initResponse;
941             if (*res->result == 0)
942             {
943                 send_to_srw_client_error(3, 0);
944             }
945             else if (!m_s2z_search_apdu)
946             {
947                 send_srw_explain_response(0, 0);
948             }
949             else
950             {
951                 handle_incoming_Z_PDU(m_s2z_search_apdu);
952             }
953         }
954         else if (m_s2z_search_apdu && apdu->which == Z_APDU_searchResponse)
955         {
956             m_s2z_search_apdu = 0;
957             Z_SearchResponse *res = apdu->u.searchResponse;
958             m_s2z_hit_count = *res->resultCount;
959             if (res->records && res->records->which == Z_Records_NSD)
960             {
961                 send_to_srw_client_ok(0, res->records, 1);
962             }
963             else if (m_s2z_present_apdu && m_s2z_hit_count > 0)
964             {
965                 // adjust 
966                 Z_PresentRequest *pr = m_s2z_present_apdu->u.presentRequest;
967                 
968                 if (*pr->resultSetStartPoint <= m_s2z_hit_count)
969                 {
970                     if (*pr->numberOfRecordsRequested+ *pr->resultSetStartPoint
971                         > m_s2z_hit_count)
972                         *pr->numberOfRecordsRequested =
973                             1 + m_s2z_hit_count - *pr->resultSetStartPoint;
974                 }
975                 handle_incoming_Z_PDU(m_s2z_present_apdu);
976             }
977             else
978             {
979                 m_s2z_present_apdu = 0;
980                 send_to_srw_client_ok(m_s2z_hit_count, res->records, 1);
981             }
982         }
983         else if (m_s2z_present_apdu && apdu->which == Z_APDU_presentResponse)
984         {
985             int start = 
986                 *m_s2z_present_apdu->u.presentRequest->resultSetStartPoint;
987
988             m_s2z_present_apdu = 0;
989             Z_PresentResponse *res = apdu->u.presentResponse;
990             send_to_srw_client_ok(m_s2z_hit_count, res->records, start);
991         }
992     }
993     else
994     {
995         int len = 0;
996         if (m_log_mask & PROXY_LOG_REQ_CLIENT)
997             yaz_log (LOG_LOG, "%sSending %s to client", m_session_str,
998                      apdu_name(apdu));
999         int r = send_Z_PDU(apdu, &len);
1000         m_bytes_sent += len;
1001         m_bw_stat.add_bytes(len);
1002         logtime();
1003         return r;
1004     }
1005     return 0;
1006 }
1007
1008 int Yaz_Proxy::send_to_client(Z_APDU *apdu)
1009 {
1010     int kill_session = 0;
1011     Z_ReferenceId **new_id = get_referenceIdP(apdu);
1012
1013     if (new_id)
1014         *new_id = m_referenceId;
1015     
1016     if (apdu->which == Z_APDU_searchResponse)
1017     {
1018         Z_SearchResponse *sr = apdu->u.searchResponse;
1019         Z_Records *p = sr->records;
1020         if (p && p->which == Z_Records_NSD)
1021         {
1022             Z_DiagRec dr, *dr_p = &dr;
1023             dr.which = Z_DiagRec_defaultFormat;
1024             dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1025
1026             *sr->searchStatus = 0;
1027             display_diagrecs(&dr_p, 1);
1028         }
1029         else
1030         {
1031             if (p && p->which == Z_Records_DBOSD)
1032             {
1033                 if (m_marcxml_flag)
1034                     convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1035                 if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1036                     return 0;
1037                     
1038             }
1039             if (sr->resultCount)
1040             {
1041                 yaz_log(LOG_LOG, "%s%d hits", m_session_str,
1042                         *sr->resultCount);
1043                 if (*sr->resultCount < 0)
1044                 {
1045                     m_invalid_session = 1;
1046                     kill_session = 1;
1047
1048                     *sr->searchStatus = 0;
1049                     sr->records =
1050                         create_nonSurrogateDiagnostics(odr_encode(), 2, 0);
1051                     *sr->resultCount = 0;
1052                 }
1053             }
1054         }
1055     }
1056     else if (apdu->which == Z_APDU_presentResponse)
1057     {
1058         Z_PresentResponse *sr = apdu->u.presentResponse;
1059         Z_Records *p = sr->records;
1060         if (p && p->which == Z_Records_NSD)
1061         {
1062             Z_DiagRec dr, *dr_p = &dr;
1063             dr.which = Z_DiagRec_defaultFormat;
1064             dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1065             if (*sr->presentStatus == Z_PresentStatus_success)
1066                 *sr->presentStatus = Z_PresentStatus_failure;
1067             display_diagrecs(&dr_p, 1);
1068         }
1069         if (p && p->which == Z_Records_DBOSD)
1070         {
1071             if (m_marcxml_flag)
1072                 convert_to_marcxml(p->u.databaseOrSurDiagnostics);
1073             if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
1074                 return 0;
1075         }
1076     }
1077     else if (apdu->which == Z_APDU_initResponse)
1078     {
1079         if (m_initRequest_options)
1080         {
1081             Z_Options *nopt = 
1082                 (Odr_bitmask *)odr_malloc(odr_encode(),
1083                                           sizeof(Odr_bitmask));
1084             ODR_MASK_ZERO(nopt);
1085
1086             int i;
1087             for (i = 0; i<24; i++)
1088                 if (ODR_MASK_GET(m_initRequest_options, i) &&
1089                     ODR_MASK_GET(apdu->u.initResponse->options, i))
1090                     ODR_MASK_SET(nopt, i);
1091             apdu->u.initResponse->options = nopt;           
1092         }
1093         if (m_initRequest_version)
1094         {
1095             Z_ProtocolVersion *nopt = 
1096                 (Odr_bitmask *)odr_malloc(odr_encode(),
1097                                           sizeof(Odr_bitmask));
1098             ODR_MASK_ZERO(nopt);
1099
1100             int i;
1101             for (i = 0; i<8; i++)
1102                 if (ODR_MASK_GET(m_initRequest_version, i) &&
1103                     ODR_MASK_GET(apdu->u.initResponse->protocolVersion, i))
1104                     ODR_MASK_SET(nopt, i);
1105             apdu->u.initResponse->protocolVersion = nopt;           
1106         }
1107         apdu->u.initResponse->preferredMessageSize =
1108             odr_intdup(odr_encode(),
1109                        m_client->m_initResponse_preferredMessageSize >
1110                        m_initRequest_preferredMessageSize ?
1111                        m_initRequest_preferredMessageSize :
1112                        m_client->m_initResponse_preferredMessageSize);
1113         apdu->u.initResponse->maximumRecordSize =
1114             odr_intdup(odr_encode(),
1115                        m_client->m_initResponse_maximumRecordSize >
1116                        m_initRequest_maximumRecordSize ?
1117                        m_initRequest_maximumRecordSize :
1118                        m_client->m_initResponse_maximumRecordSize);
1119     }
1120     int r = send_PDU_convert(apdu);
1121     if (r)
1122         return r;
1123     if (kill_session)
1124     {
1125         delete m_client;
1126         m_client = 0;
1127         m_parent->pre_init();
1128     }
1129     return r;
1130 }
1131
1132 int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
1133 {
1134     int len = 0;
1135     const char *apdu_name_tmp = apdu_name(apdu);
1136     int r = send_Z_PDU(apdu, &len);
1137     if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
1138         yaz_log (LOG_LOG, "%sSending %s to %s %d bytes",
1139                  get_session_str(),
1140                  apdu_name_tmp, get_hostname(), len);
1141     m_bytes_sent += len;
1142     return r;
1143 }
1144
1145 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
1146 {
1147     if (apdu->which == Z_APDU_presentRequest)
1148     {
1149         Z_PresentRequest *pr = apdu->u.presentRequest;
1150         int toget = *pr->numberOfRecordsRequested;
1151         int start = *pr->resultSetStartPoint;
1152
1153         yaz_log(LOG_LOG, "%sPresent %s %d+%d", m_session_str,
1154                 pr->resultSetId, start, toget);
1155
1156         if (*m_parent->m_optimize == '0')
1157             return apdu;
1158
1159         if (!m_client->m_last_resultSetId)
1160         {
1161             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1162             new_apdu->u.presentResponse->records =
1163                 create_nonSurrogateDiagnostics(odr_encode(), 30,
1164                                                pr->resultSetId);
1165             send_to_client(new_apdu);
1166             return 0;
1167         }
1168         if (!strcmp(m_client->m_last_resultSetId, pr->resultSetId))
1169         {
1170             if (start+toget-1 > m_client->m_last_resultCount)
1171             {
1172                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1173                 new_apdu->u.presentResponse->records =
1174                     create_nonSurrogateDiagnostics(odr_encode(), 13, 0);
1175                 send_to_client(new_apdu);
1176                 return 0;
1177             }
1178             Z_NamePlusRecordList *npr;
1179             if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
1180                                           pr->preferredRecordSyntax,
1181                                           pr->recordComposition))
1182             {
1183                 yaz_log (LOG_LOG, "%sReturned cached records for present request", 
1184                          m_session_str);
1185                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1186                 new_apdu->u.presentResponse->referenceId = pr->referenceId;
1187                 
1188                 new_apdu->u.presentResponse->numberOfRecordsReturned
1189                     = odr_intdup(odr_encode(), toget);
1190                                                                  
1191                 new_apdu->u.presentResponse->records = (Z_Records*)
1192                     odr_malloc(odr_encode(), sizeof(Z_Records));
1193                 new_apdu->u.presentResponse->records->which = Z_Records_DBOSD;
1194                 new_apdu->u.presentResponse->records->u.databaseOrSurDiagnostics = npr;
1195                 new_apdu->u.presentResponse->nextResultSetPosition =
1196                     odr_intdup(odr_encode(), start+toget);
1197
1198                 send_to_client(new_apdu);
1199                 return 0;
1200             }
1201         }
1202     }
1203
1204     if (apdu->which != Z_APDU_searchRequest)
1205         return apdu;
1206     Z_SearchRequest *sr = apdu->u.searchRequest;
1207     Yaz_Z_Query *this_query = new Yaz_Z_Query;
1208     Yaz_Z_Databases this_databases;
1209
1210     this_databases.set(sr->num_databaseNames, (const char **)
1211                        sr->databaseNames);
1212     
1213     this_query->set_Z_Query(sr->query);
1214
1215     char query_str[120];
1216     this_query->print(query_str, sizeof(query_str)-1);
1217     yaz_log(LOG_LOG, "%sSearch %s", m_session_str, query_str);
1218
1219     if (*m_parent->m_optimize != '0' &&
1220         m_client->m_last_ok && m_client->m_last_query &&
1221         m_client->m_last_query->match(this_query) &&
1222         !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
1223         m_client->m_last_databases.match(this_databases))
1224     {
1225         delete this_query;
1226         if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
1227             m_client->m_last_resultCount < *sr->largeSetLowerBound)
1228         {
1229             Z_NamePlusRecordList *npr;
1230             int toget = *sr->mediumSetPresentNumber;
1231             Z_RecordComposition *comp = 0;
1232
1233             if (toget > m_client->m_last_resultCount)
1234                 toget = m_client->m_last_resultCount;
1235             
1236             if (sr->mediumSetElementSetNames)
1237             {
1238                 comp = (Z_RecordComposition *)
1239                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1240                 comp->which = Z_RecordComp_simple;
1241                 comp->u.simple = sr->mediumSetElementSetNames;
1242             }
1243  
1244             if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1245                                           sr->preferredRecordSyntax, comp))
1246             {
1247                 yaz_log (LOG_LOG, "%sReturned cached records for medium set",
1248                          m_session_str);
1249                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1250                 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1251                 new_apdu->u.searchResponse->resultCount =
1252                     &m_client->m_last_resultCount;
1253                 
1254                 new_apdu->u.searchResponse->numberOfRecordsReturned
1255                     = odr_intdup(odr_encode(), toget);
1256                                                         
1257                 new_apdu->u.searchResponse->presentStatus =
1258                     odr_intdup(odr_encode(), Z_PresentStatus_success);
1259                 new_apdu->u.searchResponse->records = (Z_Records*)
1260                     odr_malloc(odr_encode(), sizeof(Z_Records));
1261                 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1262                 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1263                 new_apdu->u.searchResponse->nextResultSetPosition =
1264                     odr_intdup(odr_encode(), toget+1);
1265                 send_to_client(new_apdu);
1266                 return 0;
1267             }
1268             else
1269             {
1270                 // medium Set
1271                 // send present request (medium size)
1272                 yaz_log (LOG_LOG, "%sOptimizing search for medium set",
1273                          m_session_str);
1274
1275                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1276                 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1277                 pr->referenceId = sr->referenceId;
1278                 pr->resultSetId = sr->resultSetName;
1279                 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1280                 *pr->numberOfRecordsRequested = toget;
1281                 pr->recordComposition = comp;
1282                 m_client->m_sr_transform = 1;
1283                 return new_apdu;
1284             }
1285         }
1286         else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
1287             m_client->m_last_resultCount <= 0)
1288         {
1289             // large set. Return pseudo-search response immediately
1290             yaz_log (LOG_LOG, "%sOptimizing search for large set",
1291                      m_session_str);
1292             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1293             new_apdu->u.searchResponse->referenceId = sr->referenceId;
1294             new_apdu->u.searchResponse->resultCount =
1295                 &m_client->m_last_resultCount;
1296             send_to_client(new_apdu);
1297             return 0;
1298         }
1299         else
1300         {
1301             Z_NamePlusRecordList *npr;
1302             int toget = m_client->m_last_resultCount;
1303             Z_RecordComposition *comp = 0;
1304             // small set
1305             // send a present request (small set)
1306             
1307             if (sr->smallSetElementSetNames)
1308             {
1309                 comp = (Z_RecordComposition *)
1310                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
1311                 comp->which = Z_RecordComp_simple;
1312                 comp->u.simple = sr->smallSetElementSetNames;
1313             }
1314
1315             if (m_client->m_cache.lookup (odr_encode(), &npr, 1, toget,
1316                                           sr->preferredRecordSyntax, comp))
1317             {
1318                 yaz_log (LOG_LOG, "%sReturned cached records for small set",
1319                          m_session_str);
1320                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1321                 new_apdu->u.searchResponse->referenceId = sr->referenceId;
1322                 new_apdu->u.searchResponse->resultCount =
1323                     &m_client->m_last_resultCount;
1324                 
1325                 new_apdu->u.searchResponse->numberOfRecordsReturned
1326                     = odr_intdup(odr_encode(), toget);
1327                                                                  
1328                 new_apdu->u.searchResponse->presentStatus =
1329                     odr_intdup(odr_encode(), Z_PresentStatus_success);
1330                 new_apdu->u.searchResponse->records = (Z_Records*)
1331                     odr_malloc(odr_encode(), sizeof(Z_Records));
1332                 new_apdu->u.searchResponse->records->which = Z_Records_DBOSD;
1333                 new_apdu->u.searchResponse->records->u.databaseOrSurDiagnostics = npr;
1334                 new_apdu->u.searchResponse->nextResultSetPosition =
1335                     odr_intdup(odr_encode(), toget+1);
1336                 send_to_client(new_apdu);
1337                 return 0;
1338             }
1339             else
1340             {
1341                 yaz_log (LOG_LOG, "%sOptimizing search for small set",
1342                          m_session_str);
1343                 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
1344                 Z_PresentRequest *pr = new_apdu->u.presentRequest;
1345                 pr->referenceId = sr->referenceId;
1346                 pr->resultSetId = sr->resultSetName;
1347                 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
1348                 *pr->numberOfRecordsRequested = toget;
1349                 pr->recordComposition = comp;
1350                 m_client->m_sr_transform = 1;
1351                 return new_apdu;
1352             }
1353         }
1354     }
1355     else  // query doesn't match
1356     {
1357         delete m_client->m_last_query;
1358         m_client->m_last_query = this_query;
1359         m_client->m_last_ok = 0;
1360         m_client->m_cache.clear();
1361         m_client->m_resultSetStartPoint = 0;
1362
1363         xfree (m_client->m_last_resultSetId);
1364         m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
1365
1366         m_client->m_last_databases.set(sr->num_databaseNames,
1367                                        (const char **) sr->databaseNames);
1368     }
1369     return apdu;
1370 }
1371
1372
1373 void Yaz_Proxy::inc_request_no()
1374 {
1375     char *cp = strchr(m_session_str, ' ');
1376     m_request_no++;
1377     if (cp)
1378         sprintf(cp+1, "%d ", m_request_no);
1379 }
1380
1381 void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
1382 {
1383     inc_request_no();
1384
1385     m_bytes_recv += len;
1386     
1387     if (m_log_mask & PROXY_LOG_REQ_CLIENT)
1388         yaz_log (LOG_LOG, "%sReceiving %s from client %d bytes",
1389                  m_session_str, gdu_name(apdu), len);
1390
1391     if (m_bw_hold_PDU)     // double incoming PDU. shutdown now.
1392         shutdown();
1393
1394     m_bw_stat.add_bytes(len);
1395     m_pdu_stat.add_bytes(1);
1396
1397     gettimeofday(&m_time_tv, 0);
1398
1399     int bw_total = m_bw_stat.get_total();
1400     int pdu_total = m_pdu_stat.get_total();
1401
1402     int reduce = 0;
1403     if (m_bw_max)
1404     {
1405         if (bw_total > m_bw_max)
1406         {
1407             reduce = (bw_total/m_bw_max);
1408         }
1409     }
1410     if (m_pdu_max)
1411     {
1412         if (pdu_total > m_pdu_max)
1413         {
1414             int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
1415             reduce = (reduce > nreduce) ? reduce : nreduce;
1416         }
1417     }
1418     if (reduce)  
1419     {
1420         yaz_log(LOG_LOG, "%sdelay=%d bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
1421                 m_session_str, reduce, bw_total, pdu_total,
1422                 m_bw_max, m_pdu_max);
1423         
1424         m_bw_hold_PDU = apdu;  // save PDU and signal "on hold"
1425         timeout(reduce);       // call us reduce seconds later
1426     }
1427     else if (apdu->which == Z_GDU_Z3950)
1428         handle_incoming_Z_PDU(apdu->u.z3950);
1429     else if (apdu->which == Z_GDU_HTTP_Request)
1430         handle_incoming_HTTP(apdu->u.HTTP_Request);
1431 }
1432
1433 void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
1434 {
1435     if (m_max_record_retrieve)
1436     {
1437         if (apdu->which == Z_APDU_presentRequest)
1438         {
1439             Z_PresentRequest *pr = apdu->u.presentRequest;
1440             if (pr->numberOfRecordsRequested && 
1441                 *pr->numberOfRecordsRequested > m_max_record_retrieve)
1442                 *pr->numberOfRecordsRequested = m_max_record_retrieve;
1443         }
1444     }
1445 }
1446
1447 Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
1448                                                      int error,
1449                                                      const char *addinfo)
1450 {
1451     Z_Records *rec = (Z_Records *)
1452         odr_malloc (odr, sizeof(*rec));
1453     int *err = (int *)
1454         odr_malloc (odr, sizeof(*err));
1455     Z_DiagRec *drec = (Z_DiagRec *)
1456         odr_malloc (odr, sizeof(*drec));
1457     Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1458         odr_malloc (odr, sizeof(*dr));
1459     *err = error;
1460     rec->which = Z_Records_NSD;
1461     rec->u.nonSurrogateDiagnostic = dr;
1462     dr->diagnosticSetId =
1463         yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
1464     dr->condition = err;
1465     dr->which = Z_DefaultDiagFormat_v2Addinfo;
1466     dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
1467     return rec;
1468 }
1469
1470 Z_APDU *Yaz_Proxy::handle_query_transformation(Z_APDU *apdu)
1471 {
1472     if (apdu->which == Z_APDU_searchRequest &&
1473         apdu->u.searchRequest->query &&
1474         apdu->u.searchRequest->query->which == Z_Query_type_104 &&
1475         apdu->u.searchRequest->query->u.type_104->which == Z_External_CQL)
1476     {
1477         Z_RPNQuery *rpnquery = 0;
1478         Z_SearchRequest *sr = apdu->u.searchRequest;
1479         char *addinfo = 0;
1480         
1481         yaz_log(LOG_LOG, "%sCQL: %s", m_session_str,
1482                 sr->query->u.type_104->u.cql);
1483
1484         int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
1485                                           &rpnquery, odr_encode(),
1486                                           &addinfo);
1487         if (r == -3)
1488             yaz_log(LOG_LOG, "%sNo CQL to RPN table", m_session_str);
1489         else if (r)
1490         {
1491             yaz_log(LOG_LOG, "%sCQL Conversion error %d", m_session_str, r);
1492             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1493
1494             new_apdu->u.searchResponse->referenceId = sr->referenceId;
1495             new_apdu->u.searchResponse->records =
1496                 create_nonSurrogateDiagnostics(odr_encode(),
1497                                                yaz_diag_srw_to_bib1(r),
1498                                                addinfo);
1499             *new_apdu->u.searchResponse->searchStatus = 0;
1500
1501             send_to_client(new_apdu);
1502
1503             return 0;
1504         }
1505         else
1506         {
1507             sr->query->which = Z_Query_type_1;
1508             sr->query->u.type_1 = rpnquery;
1509         }
1510         return apdu;
1511     }
1512     return apdu;
1513 }
1514
1515 Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
1516 {
1517     if (apdu->which == Z_APDU_searchRequest)
1518     {
1519         Z_SearchRequest *sr = apdu->u.searchRequest;
1520         int err = 0;
1521         char *addinfo = 0;
1522
1523         Yaz_ProxyConfig *cfg = check_reconfigure();
1524         if (cfg)
1525             err = cfg->check_query(odr_encode(), m_default_target,
1526                                    sr->query, &addinfo);
1527         if (err)
1528         {
1529             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1530
1531             new_apdu->u.searchResponse->referenceId = sr->referenceId;
1532             new_apdu->u.searchResponse->records =
1533                 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1534             *new_apdu->u.searchResponse->searchStatus = 0;
1535
1536             send_to_client(new_apdu);
1537
1538             return 0;
1539         }
1540     }
1541     return apdu;
1542 }
1543
1544 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
1545 {
1546     m_marcxml_flag = 0;
1547     if (apdu->which == Z_APDU_searchRequest)
1548     {
1549         Z_SearchRequest *sr = apdu->u.searchRequest;
1550         int err = 0;
1551         char *addinfo = 0;
1552         Yaz_ProxyConfig *cfg = check_reconfigure();
1553
1554         Z_RecordComposition rc_temp, *rc = 0;
1555         if (sr->smallSetElementSetNames)
1556         {
1557             rc_temp.which = Z_RecordComp_simple;
1558             rc_temp.u.simple = sr->smallSetElementSetNames;
1559             rc = &rc_temp;
1560         }
1561
1562         char *stylesheet_name = 0;
1563         if (cfg)
1564             err = cfg->check_syntax(odr_encode(),
1565                                     m_default_target,
1566                                     sr->preferredRecordSyntax, rc,
1567                                     &addinfo, &stylesheet_name, &m_schema);
1568         if (stylesheet_name)
1569         {
1570             m_parent->low_socket_close();
1571
1572             if (m_stylesheet_xsp)
1573                 xsltFreeStylesheet(m_stylesheet_xsp);
1574
1575             m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1576                                                        stylesheet_name);
1577             m_stylesheet_offset = 0;
1578             xfree(stylesheet_name);
1579
1580             m_parent->low_socket_open();
1581         }
1582         if (err == -1)
1583         {
1584             sr->preferredRecordSyntax =
1585                 yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN, VAL_USMARC);
1586             m_marcxml_flag = 1;
1587         }
1588         else if (err)
1589         {
1590             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
1591             
1592             new_apdu->u.searchResponse->referenceId = sr->referenceId;
1593             new_apdu->u.searchResponse->records =
1594                 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1595             *new_apdu->u.searchResponse->searchStatus = 0;
1596             
1597             send_to_client(new_apdu);
1598             
1599             return 0;
1600         }
1601     }
1602     else if (apdu->which == Z_APDU_presentRequest)
1603     {
1604         Z_PresentRequest *pr = apdu->u.presentRequest;
1605         int err = 0;
1606         char *addinfo = 0;
1607         Yaz_ProxyConfig *cfg = check_reconfigure();
1608
1609         char *stylesheet_name = 0;
1610         if (cfg)
1611             err = cfg->check_syntax(odr_encode(), m_default_target,
1612                                     pr->preferredRecordSyntax,
1613                                     pr->recordComposition,
1614                                     &addinfo, &stylesheet_name, &m_schema);
1615         if (stylesheet_name)
1616         {
1617             m_parent->low_socket_close();
1618
1619             if (m_stylesheet_xsp)
1620                 xsltFreeStylesheet(m_stylesheet_xsp);
1621
1622             m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
1623                                                        stylesheet_name);
1624             m_stylesheet_offset = 0;
1625             xfree(stylesheet_name);
1626
1627             m_parent->low_socket_open();
1628         }
1629         if (err == -1)
1630         {
1631             pr->preferredRecordSyntax =
1632                 yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN, VAL_USMARC);
1633             m_marcxml_flag = 1;
1634         }
1635         else if (err)
1636         {
1637             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
1638             
1639             new_apdu->u.presentResponse->referenceId = pr->referenceId;
1640             new_apdu->u.presentResponse->records =
1641                 create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
1642             *new_apdu->u.presentResponse->presentStatus =
1643                 Z_PresentStatus_failure;
1644             
1645             send_to_client(new_apdu);
1646             
1647             return 0;
1648         }
1649     }
1650     return apdu;
1651 }
1652
1653 Z_ElementSetNames *Yaz_Proxy::mk_esn_from_schema(ODR o, const char *schema)
1654 {
1655     if (!schema)
1656         return 0;
1657     Z_ElementSetNames *esn = (Z_ElementSetNames *)
1658         odr_malloc(o, sizeof(Z_ElementSetNames));
1659     esn->which = Z_ElementSetNames_generic;
1660     esn->u.generic = odr_strdup(o, schema);
1661     return esn;
1662 }
1663
1664 void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
1665 {
1666     if (m_s2z_odr_init)
1667     {
1668         odr_destroy(m_s2z_odr_init);
1669         m_s2z_odr_init = 0;
1670     }
1671     if (m_s2z_odr_search)
1672     {
1673         odr_destroy(m_s2z_odr_search);
1674         m_s2z_odr_search = 0;
1675     }
1676
1677     m_http_keepalive = 0;
1678     m_http_version = 0;
1679     if (!strcmp(hreq->version, "1.0")) 
1680     {
1681         const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1682         if (v && !strcmp(v, "Keep-Alive"))
1683             m_http_keepalive = 1;
1684         else
1685             m_http_keepalive = 0;
1686         m_http_version = "1.0";
1687     }
1688     else
1689     {
1690         const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1691         if (v && !strcmp(v, "close"))
1692             m_http_keepalive = 0;
1693         else
1694             m_http_keepalive = 1;
1695         m_http_version = "1.1";
1696     }
1697
1698     Z_SRW_PDU *srw_pdu = 0;
1699     Z_SOAP *soap_package = 0;
1700     char *charset = 0;
1701     Z_SRW_diagnostic *diagnostic = 0;
1702     int num_diagnostic = 0;
1703     if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1704                        &charset) == 0
1705         || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(),
1706                           &charset, &diagnostic, &num_diagnostic) == 0)
1707     {
1708         m_s2z_odr_init = odr_createmem(ODR_ENCODE);
1709         m_s2z_odr_search = odr_createmem(ODR_ENCODE);
1710         m_soap_ns = odr_strdup(m_s2z_odr_search, soap_package->ns);
1711         m_s2z_init_apdu = 0;
1712         m_s2z_search_apdu = 0;
1713         m_s2z_present_apdu = 0;
1714
1715         m_s2z_stylesheet = 0;
1716         
1717         if (srw_pdu->which == Z_SRW_searchRetrieve_request)
1718         {
1719             Z_SRW_searchRetrieveRequest *srw_req = srw_pdu->u.request;
1720
1721             m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1722             // recordXPath unsupported.
1723             if (srw_req->recordXPath)
1724             {
1725                 yaz_add_srw_diagnostic(odr_decode(),
1726                                        &diagnostic, &num_diagnostic,
1727                                        72, 0);
1728             }
1729             // must have a query
1730             if (!srw_req->query.cql)
1731             {
1732                 yaz_add_srw_diagnostic(odr_decode(),
1733                                        &diagnostic, &num_diagnostic,
1734                                        7, "query");
1735             }
1736             // sort unsupported
1737             if (srw_req->sort_type != Z_SRW_sort_type_none)
1738             {
1739                 yaz_add_srw_diagnostic(odr_decode(),
1740                                        &diagnostic, &num_diagnostic,
1741                                        80, 0);
1742             }
1743             // save stylesheet
1744             if (srw_req->stylesheet)
1745                 m_s2z_stylesheet =
1746                     odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1747                                               
1748             // set packing for response records ..
1749             if (srw_req->recordPacking &&
1750                 !strcmp(srw_req->recordPacking, "xml"))
1751                 m_s2z_packing = Z_SRW_recordPacking_XML;
1752             else
1753                 m_s2z_packing = Z_SRW_recordPacking_string;
1754
1755             if (num_diagnostic)
1756             {
1757                 Z_SRW_PDU *srw_pdu =
1758                     yaz_srw_get(odr_encode(),
1759                                 Z_SRW_searchRetrieve_response);
1760                 Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
1761                 
1762                 srw_res->diagnostics = diagnostic;
1763                 srw_res->num_diagnostics = num_diagnostic;
1764                 send_srw_response(srw_pdu);
1765                 return;
1766             }
1767
1768             // prepare search PDU
1769             m_s2z_search_apdu = zget_APDU(m_s2z_odr_search,
1770                                           Z_APDU_searchRequest);
1771             Z_SearchRequest *z_searchRequest =
1772                 m_s2z_search_apdu->u.searchRequest;
1773
1774             z_searchRequest->num_databaseNames = 1;
1775             z_searchRequest->databaseNames = (char**)
1776                 odr_malloc(m_s2z_odr_search, sizeof(char *));
1777             z_searchRequest->databaseNames[0] = odr_strdup(m_s2z_odr_search,
1778                                                            srw_req->database);
1779             
1780             // query transformation
1781             Z_Query *query = (Z_Query *)
1782                 odr_malloc(m_s2z_odr_search, sizeof(Z_Query));
1783             z_searchRequest->query = query;
1784             
1785             if (srw_req->query_type == Z_SRW_query_type_cql)
1786             {
1787                 Z_External *ext = (Z_External *) 
1788                     odr_malloc(m_s2z_odr_search, sizeof(*ext));
1789                 ext->direct_reference = 
1790                     odr_getoidbystr(m_s2z_odr_search, "1.2.840.10003.16.2");
1791                 ext->indirect_reference = 0;
1792                 ext->descriptor = 0;
1793                 ext->which = Z_External_CQL;
1794                 ext->u.cql = srw_req->query.cql;
1795                 
1796                 query->which = Z_Query_type_104;
1797                 query->u.type_104 =  ext;
1798             }
1799             else if (srw_req->query_type == Z_SRW_query_type_pqf)
1800             {
1801                 Z_RPNQuery *RPNquery;
1802                 YAZ_PQF_Parser pqf_parser;
1803                 
1804                 pqf_parser = yaz_pqf_create ();
1805                 
1806                 RPNquery = yaz_pqf_parse (pqf_parser, m_s2z_odr_search,
1807                                           srw_req->query.pqf);
1808                 if (!RPNquery)
1809                 {
1810                     const char *pqf_msg;
1811                     size_t off;
1812                     int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1813                     yaz_log(LOG_LOG, "%*s^\n", off+4, "");
1814                     yaz_log(LOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
1815                     
1816                     send_to_srw_client_error(10, 0);
1817                     return;
1818                 }
1819                 query->which = Z_Query_type_1;
1820                 query->u.type_1 =  RPNquery;
1821                 
1822                 yaz_pqf_destroy (pqf_parser);
1823             }
1824             else
1825             {
1826                 send_to_srw_client_error(7, "query");
1827                 return;
1828             }
1829
1830             // present
1831             m_s2z_present_apdu = 0;
1832             int max = 0;
1833             if (srw_req->maximumRecords)
1834                 max = *srw_req->maximumRecords;
1835             int start = 1;
1836             if (srw_req->startRecord)
1837                 start = *srw_req->startRecord;
1838             if (max > 0)
1839             {
1840                 // Some backend, such as Voyager doesn't honor piggyback
1841                 // So we use present always (0 &&).
1842                 if (0 && start <= 1)  // Z39.50 piggyback
1843                 {
1844                     *z_searchRequest->smallSetUpperBound = max;
1845                     *z_searchRequest->mediumSetPresentNumber = max;
1846                     *z_searchRequest->largeSetLowerBound = 2000000000; // 2e9
1847
1848                     z_searchRequest->preferredRecordSyntax =
1849                         yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1850                                                VAL_TEXT_XML);
1851                     if (srw_req->recordSchema)
1852                     {
1853                         z_searchRequest->smallSetElementSetNames =
1854                             z_searchRequest->mediumSetElementSetNames =
1855                             mk_esn_from_schema(m_s2z_odr_search,
1856                                                srw_req->recordSchema);
1857                     }
1858                 }
1859                 else   // Z39.50 present
1860                 {
1861                     m_s2z_present_apdu = zget_APDU(m_s2z_odr_search, 
1862                                                    Z_APDU_presentRequest);
1863                     Z_PresentRequest *z_presentRequest = 
1864                         m_s2z_present_apdu->u.presentRequest;
1865                     *z_presentRequest->resultSetStartPoint = start;
1866                     *z_presentRequest->numberOfRecordsRequested = max;
1867                     z_presentRequest->preferredRecordSyntax =
1868                         yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN,
1869                                                VAL_TEXT_XML);
1870                     if (srw_req->recordSchema)
1871                     {
1872                         z_presentRequest->recordComposition =
1873                             (Z_RecordComposition *)
1874                             odr_malloc(m_s2z_odr_search,
1875                                        sizeof(Z_RecordComposition));
1876                         z_presentRequest->recordComposition->which = 
1877                             Z_RecordComp_simple;                    
1878                         z_presentRequest->recordComposition->u.simple =
1879                             mk_esn_from_schema(m_s2z_odr_search,
1880                                                srw_req->recordSchema);
1881                     }
1882                 }
1883             }
1884             if (!m_client)
1885             {
1886                 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1887                                             Z_APDU_initRequest);
1888                 
1889                 // prevent m_initRequest_apdu memory from being grabbed
1890                 // in Yaz_Proxy::handle_incoming_Z_PDU
1891                 m_initRequest_apdu = m_s2z_init_apdu;
1892                 handle_incoming_Z_PDU(m_s2z_init_apdu);
1893                 return;
1894             }
1895             else
1896             {
1897                 handle_incoming_Z_PDU(m_s2z_search_apdu);
1898                 return;
1899             }
1900         }
1901         else if (srw_pdu->which == Z_SRW_explain_request)
1902         {
1903             Z_SRW_explainRequest *srw_req = srw_pdu->u.explain_request;
1904
1905             m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
1906
1907             // save stylesheet
1908             if (srw_req->stylesheet)
1909                 m_s2z_stylesheet =
1910                     odr_strdup(m_s2z_odr_init, srw_req->stylesheet);
1911
1912             if (srw_req->recordPacking &&
1913                 !strcmp(srw_req->recordPacking, "xml"))
1914                 m_s2z_packing = Z_SRW_recordPacking_XML;
1915             else
1916                 m_s2z_packing = Z_SRW_recordPacking_string;
1917
1918             if (num_diagnostic)
1919             {
1920                 send_srw_explain_response(diagnostic, num_diagnostic);
1921                 return;
1922             }
1923
1924             if (!m_client)
1925             {
1926                 m_s2z_init_apdu = zget_APDU(m_s2z_odr_init,
1927                                             Z_APDU_initRequest);
1928                 
1929                 // prevent m_initRequest_apdu memory from being grabbed
1930                 // in Yaz_Proxy::handle_incoming_Z_PDU
1931                 m_initRequest_apdu = m_s2z_init_apdu;
1932                 handle_incoming_Z_PDU(m_s2z_init_apdu);
1933             }
1934             else
1935                 send_srw_explain_response(0, 0);
1936             return;
1937         }
1938         else if (srw_pdu->which == Z_SRW_scan_request)
1939         {
1940             m_s2z_database = odr_strdup(m_s2z_odr_init,
1941                                         srw_pdu->u.scan_request->database);
1942
1943             yaz_add_srw_diagnostic(odr_decode(),
1944                                    &diagnostic, &num_diagnostic,
1945                                    4, "scan");
1946             Z_SRW_PDU *srw_pdu =
1947                 yaz_srw_get(odr_encode(),
1948                             Z_SRW_scan_response);
1949             Z_SRW_scanResponse *srw_res = srw_pdu->u.scan_response;
1950             
1951             srw_res->diagnostics = diagnostic;
1952             srw_res->num_diagnostics = num_diagnostic;
1953             send_srw_response(srw_pdu);
1954             return;
1955         }
1956         else
1957         {
1958             m_s2z_database = 0;
1959
1960             send_to_srw_client_error(4, 0);
1961         }
1962     }
1963     int len = 0;
1964     Z_GDU *p = z_get_HTTP_Response(odr_encode(), 400);
1965     timeout(0);
1966     send_GDU(p, &len);
1967 }
1968
1969 void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
1970 {
1971     Z_ReferenceId **refid = get_referenceIdP(apdu);
1972     nmem_reset(m_referenceId_mem);
1973     if (refid && *refid)
1974     {
1975         m_referenceId = (Z_ReferenceId *)
1976             nmem_malloc(m_referenceId_mem, sizeof(*m_referenceId));
1977         m_referenceId->len = m_referenceId->size = (*refid)->len;
1978         m_referenceId->buf = (unsigned char *)
1979             nmem_malloc(m_referenceId_mem, (*refid)->len);
1980         memcpy(m_referenceId->buf, (*refid)->buf, (*refid)->len);
1981     }
1982     else
1983         m_referenceId = 0;
1984
1985     if (!m_client && m_invalid_session)
1986     {
1987         m_apdu_invalid_session = apdu;
1988         m_mem_invalid_session = odr_extract_mem(odr_decode());
1989         apdu = m_initRequest_apdu;
1990     }
1991     
1992     // Determine our client.
1993     Z_OtherInformation **oi;
1994     get_otherInfoAPDU(apdu, &oi);
1995     m_client = get_client(apdu, get_cookie(oi), get_proxy(oi));
1996     if (!m_client)
1997     {
1998         delete this;
1999         return;
2000     }
2001     m_client->m_server = this;
2002
2003     if (apdu->which == Z_APDU_initRequest)
2004     {
2005         if (apdu->u.initRequest->implementationId)
2006             yaz_log(LOG_LOG, "%simplementationId: %s",
2007                     m_session_str, apdu->u.initRequest->implementationId);
2008         if (apdu->u.initRequest->implementationName)
2009             yaz_log(LOG_LOG, "%simplementationName: %s",
2010                     m_session_str, apdu->u.initRequest->implementationName);
2011         if (apdu->u.initRequest->implementationVersion)
2012             yaz_log(LOG_LOG, "%simplementationVersion: %s",
2013                     m_session_str, apdu->u.initRequest->implementationVersion);
2014         if (m_initRequest_apdu == 0)
2015         {
2016             if (m_initRequest_mem)
2017                 nmem_destroy(m_initRequest_mem);
2018             m_initRequest_apdu = apdu;
2019             m_initRequest_mem = odr_extract_mem(odr_decode());
2020
2021             m_initRequest_preferredMessageSize = *apdu->u.initRequest->
2022                 preferredMessageSize;
2023             *apdu->u.initRequest->preferredMessageSize = 1024*1024;
2024             m_initRequest_maximumRecordSize = *apdu->u.initRequest->
2025                 maximumRecordSize;
2026             *apdu->u.initRequest->maximumRecordSize = 1024*1024;
2027
2028             // save init options for the response..
2029             m_initRequest_options = apdu->u.initRequest->options;
2030             
2031             apdu->u.initRequest->options = 
2032                 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2033                                            sizeof(Odr_bitmask));
2034             ODR_MASK_ZERO(apdu->u.initRequest->options);
2035             int i;
2036             for (i = 0; i<= 24; i++)
2037                 ODR_MASK_SET(apdu->u.initRequest->options, i);
2038             ODR_MASK_CLEAR(apdu->u.initRequest->options,
2039                            Z_Options_negotiationModel);
2040             ODR_MASK_CLEAR(apdu->u.initRequest->options,
2041                            Z_Options_concurrentOperations);
2042
2043             // make new version
2044             m_initRequest_version = apdu->u.initRequest->protocolVersion;
2045             apdu->u.initRequest->protocolVersion = 
2046                 (Odr_bitmask *)nmem_malloc(m_initRequest_mem,
2047                                            sizeof(Odr_bitmask));
2048             ODR_MASK_ZERO(apdu->u.initRequest->protocolVersion);
2049
2050             for (i = 0; i<= 8; i++)
2051                 ODR_MASK_SET(apdu->u.initRequest->protocolVersion, i);
2052         }
2053         if (m_client->m_init_flag)
2054         {
2055             if (handle_init_response_for_invalid_session(apdu))
2056                 return;
2057             if (m_client->m_initResponse)
2058             {
2059                 Z_APDU *apdu2 = m_client->m_initResponse;
2060                 apdu2->u.initResponse->otherInfo = 0;
2061                 if (m_client->m_cookie && *m_client->m_cookie)
2062                     set_otherInformationString(apdu2, VAL_COOKIE, 1,
2063                                                m_client->m_cookie);
2064                 apdu2->u.initResponse->referenceId =
2065                     apdu->u.initRequest->referenceId;
2066                 apdu2->u.initResponse->options = m_client->m_initResponse_options;
2067                 apdu2->u.initResponse->protocolVersion = 
2068                     m_client->m_initResponse_version;
2069                 
2070                 send_to_client(apdu2);
2071                 return;
2072             }
2073         }
2074         m_client->m_init_flag = 1;
2075     }
2076     handle_max_record_retrieve(apdu);
2077
2078     if (apdu)
2079         apdu = handle_syntax_validation(apdu);
2080
2081     if (apdu)
2082         apdu = handle_query_transformation(apdu);
2083
2084     if (apdu)
2085         apdu = handle_query_validation(apdu);
2086
2087     if (apdu)
2088         apdu = result_set_optimize(apdu);
2089     if (!apdu)
2090     {
2091         m_client->timeout(m_target_idletime);  // mark it active even 
2092         // though we didn't use it
2093         return;
2094     }
2095
2096     // delete other info part from PDU before sending to target
2097     get_otherInfoAPDU(apdu, &oi);
2098     if (oi)
2099         *oi = 0;
2100
2101     if (apdu->which == Z_APDU_presentRequest &&
2102         m_client->m_resultSetStartPoint == 0)
2103     {
2104         Z_PresentRequest *pr = apdu->u.presentRequest;
2105         m_client->m_resultSetStartPoint = *pr->resultSetStartPoint;
2106         m_client->m_cache.copy_presentRequest(apdu->u.presentRequest);
2107     } else {
2108         m_client->m_resultSetStartPoint = 0;
2109     }
2110     if (m_client->send_to_target(apdu) < 0)
2111     {
2112         delete m_client;
2113         m_client = 0;
2114         delete this;
2115     }
2116     else
2117         m_client->m_waiting = 1;
2118 }
2119
2120 void Yaz_Proxy::connectNotify()
2121 {
2122 }
2123
2124 void Yaz_Proxy::shutdown()
2125 {
2126     m_invalid_session = 0;
2127     // only keep if keep_alive flag is set...
2128     if (m_client && 
2129         m_client->m_pdu_recv < m_keepalive_limit_pdu &&
2130         m_client->m_bytes_recv+m_client->m_bytes_sent < m_keepalive_limit_bw &&
2131         m_client->m_waiting == 0)
2132     {
2133         yaz_log(LOG_LOG, "%sShutdown (client to proxy) keepalive %s",
2134                  m_session_str,
2135                  m_client->get_hostname());
2136         yaz_log(LOG_LOG, "%sbw=%d pdu=%d limit-bw=%d limit-pdu=%d",
2137                 m_session_str, m_client->m_pdu_recv,
2138                 m_client->m_bytes_sent + m_client->m_bytes_recv,
2139                 m_keepalive_limit_bw, m_keepalive_limit_pdu);
2140         assert (m_client->m_waiting != 2);
2141         // Tell client (if any) that no server connection is there..
2142         m_client->m_server = 0;
2143         m_invalid_session = 0;
2144     }
2145     else if (m_client)
2146     {
2147         yaz_log (LOG_LOG, "%sShutdown (client to proxy) close %s",
2148                  m_session_str,
2149                  m_client->get_hostname());
2150         assert (m_client->m_waiting != 2);
2151         delete m_client;
2152     }
2153     else if (!m_parent)
2154     {
2155         yaz_log (LOG_LOG, "%sshutdown (client to proxy) bad state",
2156                  m_session_str);
2157         assert (m_parent);
2158     }
2159     else 
2160     {
2161         yaz_log (LOG_LOG, "%sShutdown (client to proxy)",
2162                  m_session_str);
2163     }
2164     if (m_parent)
2165         m_parent->pre_init();
2166     delete this;
2167 }
2168
2169 const char *Yaz_ProxyClient::get_session_str() 
2170 {
2171     if (!m_server)
2172         return "0 ";
2173     return m_server->get_session_str();
2174 }
2175
2176 void Yaz_ProxyClient::shutdown()
2177 {
2178     yaz_log (LOG_LOG, "%sShutdown (proxy to target) %s", get_session_str(),
2179              get_hostname());
2180     delete m_server;
2181     delete this;
2182 }
2183
2184 void Yaz_Proxy::failNotify()
2185 {
2186     inc_request_no();
2187     yaz_log (LOG_LOG, "%sConnection closed by client",
2188              get_session_str());
2189     shutdown();
2190 }
2191
2192 void Yaz_ProxyClient::failNotify()
2193 {
2194     if (m_server)
2195         m_server->inc_request_no();
2196     yaz_log (LOG_LOG, "%sConnection closed by target %s", 
2197              get_session_str(), get_hostname());
2198     shutdown();
2199 }
2200
2201 void Yaz_ProxyClient::connectNotify()
2202 {
2203     const char *s = get_session_str();
2204     const char *h = get_hostname();
2205     yaz_log (LOG_LOG, "%sConnection accepted by %s timeout=%d", s, h,
2206              m_target_idletime);
2207     timeout(m_target_idletime);
2208     if (!m_server)
2209         pre_init_client();
2210 }
2211
2212 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
2213                                                   *the_PDU_Observable, int fd)
2214 {
2215     return new Yaz_ProxyClient(the_PDU_Observable, 0);
2216 }
2217
2218 Yaz_ProxyClient::~Yaz_ProxyClient()
2219 {
2220     if (m_prev)
2221         *m_prev = m_next;
2222     if (m_next)
2223         m_next->m_prev = m_prev;
2224     m_waiting = 2;     // for debugging purposes only.
2225     odr_destroy(m_init_odr);
2226     delete m_last_query;
2227     xfree (m_last_resultSetId);
2228     xfree (m_cookie);
2229 }
2230
2231 void Yaz_ProxyClient::pre_init_client()
2232 {
2233     Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
2234     Z_InitRequest *req = apdu->u.initRequest;
2235     
2236     int i;
2237     for (i = 0; i<= 24; i++)
2238         ODR_MASK_SET(req->options, i);
2239     ODR_MASK_CLEAR(apdu->u.initRequest->options,
2240                    Z_Options_negotiationModel);
2241     ODR_MASK_CLEAR(apdu->u.initRequest->options,
2242                    Z_Options_concurrentOperations);
2243     for (i = 0; i<= 10; i++)
2244         ODR_MASK_SET(req->protocolVersion, i);
2245
2246     if (send_to_target(apdu) < 0)
2247     {
2248         delete this;
2249     }
2250     else
2251     {
2252         m_waiting = 1;
2253         m_init_flag = 1;
2254     }
2255 }
2256
2257 void Yaz_Proxy::pre_init()
2258 {
2259     int i;
2260     const char *name = 0;
2261     const char *zurl_in_use[MAX_ZURL_PLEX];
2262     int limit_bw, limit_pdu, limit_req;
2263     int target_idletime, client_idletime;
2264     int max_clients;
2265     int keepalive_limit_bw, keepalive_limit_pdu;
2266     int pre_init;
2267     const char *cql2rpn = 0;
2268
2269     Yaz_ProxyConfig *cfg = check_reconfigure();
2270
2271     zurl_in_use[0] = 0;
2272
2273     if (m_log_mask & PROXY_LOG_APDU_CLIENT)
2274         set_APDU_yazlog(1);
2275     else
2276         set_APDU_yazlog(0);
2277
2278     for (i = 0; cfg && cfg->get_target_no(i, &name, zurl_in_use,
2279                                           &limit_bw, &limit_pdu, &limit_req,
2280                                           &target_idletime, &client_idletime,
2281                                           &max_clients, 
2282                                           &keepalive_limit_bw,
2283                                           &keepalive_limit_pdu,
2284                                           &pre_init,
2285                                           &cql2rpn) ; i++)
2286     {
2287         if (pre_init)
2288         {
2289             int j;
2290             for (j = 0; zurl_in_use[j]; j++)
2291             {
2292                 Yaz_ProxyClient *c;
2293                 int spare = 0;
2294                 int spare_waiting = 0;
2295                 int in_use = 0;
2296                 int other = 0;
2297                 for (c = m_clientPool; c; c = c->m_next)
2298                 {
2299                     if (!strcmp(zurl_in_use[j], c->get_hostname()))
2300                     {
2301                         if (c->m_cookie == 0)
2302                         {
2303                             if (c->m_server == 0)
2304                                 if (c->m_waiting)
2305                                     spare_waiting++;
2306                                 else
2307                                     spare++;
2308                             else
2309                                 in_use++;
2310                         }
2311                         else
2312                             other++;
2313                     }
2314                 }
2315                 yaz_log(LOG_LOG, "%spre-init %s %s use=%d other=%d spare=%d "
2316                         "sparew=%d preinit=%d",m_session_str,
2317                         name, zurl_in_use[j], in_use, other,
2318                         spare, spare_waiting, pre_init);
2319                 if (spare + spare_waiting < pre_init)
2320                 {
2321                     c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
2322                     c->m_next = m_clientPool;
2323                     if (c->m_next)
2324                         c->m_next->m_prev = &c->m_next;
2325                     m_clientPool = c;
2326                     c->m_prev = &m_clientPool;
2327                     
2328                     if (m_log_mask & PROXY_LOG_APDU_SERVER)
2329                         c->set_APDU_yazlog(1);
2330                     else
2331                         c->set_APDU_yazlog(0);
2332
2333                     if (c->client(zurl_in_use[j]))
2334                     {
2335                         timeout(60);
2336                         delete c;
2337                         return;
2338                     }
2339                     c->timeout(30);
2340                     c->m_waiting = 1;
2341                     c->m_target_idletime = target_idletime;
2342                     c->m_seqno = m_seqno++;
2343                 }
2344             }
2345         }
2346     }
2347 }
2348
2349 void Yaz_Proxy::timeoutNotify()
2350 {
2351     if (m_parent)
2352     {
2353         if (m_bw_hold_PDU)
2354         {
2355             timeout(m_client_idletime);
2356             Z_GDU *apdu = m_bw_hold_PDU;
2357             m_bw_hold_PDU = 0;
2358             
2359             if (apdu->which == Z_GDU_Z3950)
2360                 handle_incoming_Z_PDU(apdu->u.z3950);
2361             else if (apdu->which == Z_GDU_HTTP_Request)
2362                 handle_incoming_HTTP(apdu->u.HTTP_Request);
2363         }
2364         else if (m_stylesheet_nprl)
2365             convert_xsl_delay();
2366         else
2367         {
2368             inc_request_no();
2369
2370             yaz_log (LOG_LOG, "%sTimeout (client to proxy)", m_session_str);
2371             shutdown();
2372         }
2373     }
2374     else
2375     {
2376         timeout(600);
2377         pre_init();
2378     }
2379 }
2380
2381 void Yaz_Proxy::markInvalid()
2382 {
2383     m_client = 0;
2384     m_invalid_session = 1;
2385 }
2386
2387 void Yaz_ProxyClient::timeoutNotify()
2388 {
2389     if (m_server)
2390         m_server->inc_request_no();
2391
2392     yaz_log (LOG_LOG, "%sTimeout (proxy to target) %s", get_session_str(),
2393              get_hostname());
2394     m_waiting = 1;
2395     m_root->pre_init();
2396     if (m_server && m_init_flag)
2397     {
2398         // target timed out in a session that was properly initialized
2399         // server object stay alive but we mark it as invalid so it
2400         // gets initialized again
2401         m_server->markInvalid();
2402         m_server = 0;
2403     }
2404     shutdown();
2405 }
2406
2407 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
2408                                  Yaz_Proxy *parent) :
2409     Yaz_Z_Assoc (the_PDU_Observable)
2410 {
2411     m_cookie = 0;
2412     m_next = 0;
2413     m_prev = 0;
2414     m_init_flag = 0;
2415     m_last_query = 0;
2416     m_last_resultSetId = 0;
2417     m_last_resultCount = 0;
2418     m_last_ok = 0;
2419     m_sr_transform = 0;
2420     m_waiting = 0;
2421     m_init_odr = odr_createmem (ODR_DECODE);
2422     m_initResponse = 0;
2423     m_initResponse_options = 0;
2424     m_initResponse_version = 0;
2425     m_initResponse_preferredMessageSize = 0;
2426     m_initResponse_maximumRecordSize = 0;
2427     m_resultSetStartPoint = 0;
2428     m_bytes_sent = m_bytes_recv = 0;
2429     m_pdu_recv = 0;
2430     m_server = 0;
2431     m_seqno = 0;
2432     m_target_idletime = 600;
2433     m_root = parent;
2434 }
2435
2436 const char *Yaz_Proxy::option(const char *name, const char *value)
2437 {
2438     if (!strcmp (name, "optimize")) {
2439         if (value) {
2440             xfree (m_optimize); 
2441             m_optimize = xstrdup (value);
2442         }
2443         return m_optimize;
2444     }
2445     return 0;
2446 }
2447
2448 void Yaz_ProxyClient::recv_HTTP_response(Z_HTTP_Response *apdu, int len)
2449 {
2450
2451 }
2452
2453 void Yaz_ProxyClient::recv_GDU(Z_GDU *apdu, int len)
2454 {
2455     if (apdu->which == Z_GDU_Z3950)
2456         recv_Z_PDU(apdu->u.z3950, len);
2457     else if (apdu->which == Z_GDU_HTTP_Response)
2458         recv_HTTP_response(apdu->u.HTTP_Response, len);
2459     else
2460         shutdown();
2461 }
2462
2463 int Yaz_Proxy::handle_init_response_for_invalid_session(Z_APDU *apdu)
2464 {
2465     if (!m_invalid_session)
2466         return 0;
2467     m_invalid_session = 0;
2468     handle_incoming_Z_PDU(m_apdu_invalid_session);
2469     assert (m_mem_invalid_session);
2470     nmem_destroy(m_mem_invalid_session);
2471     m_mem_invalid_session = 0;
2472     return 1;
2473 }
2474
2475 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
2476 {
2477     m_bytes_recv += len;
2478
2479     m_pdu_recv++;
2480     m_waiting = 0;
2481     if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
2482         yaz_log (LOG_LOG, "%sReceiving %s from %s %d bytes", get_session_str(),
2483                  apdu_name(apdu), get_hostname(), len);
2484     if (apdu->which == Z_APDU_initResponse)
2485     {
2486         if (!m_server)  // if this is a pre init session , check for more
2487             m_root->pre_init();
2488         NMEM nmem = odr_extract_mem (odr_decode());
2489         odr_reset (m_init_odr);
2490         nmem_transfer (m_init_odr->mem, nmem);
2491         m_initResponse = apdu;
2492         m_initResponse_options = apdu->u.initResponse->options;
2493         m_initResponse_version = apdu->u.initResponse->protocolVersion;
2494         m_initResponse_preferredMessageSize = 
2495             *apdu->u.initResponse->preferredMessageSize;
2496         m_initResponse_maximumRecordSize = 
2497             *apdu->u.initResponse->maximumRecordSize;
2498
2499         Z_InitResponse *ir = apdu->u.initResponse;
2500         char *im0 = ir->implementationName;
2501         
2502         char *im1 = (char*) 
2503             odr_malloc(m_init_odr, 20 + (im0 ? strlen(im0) : 0));
2504         *im1 = '\0';
2505         if (im0)
2506         {
2507             strcat(im1, im0);
2508             strcat(im1, " ");
2509         }
2510         strcat(im1, "(YAZ Proxy)");
2511         ir->implementationName = im1;
2512
2513         nmem_destroy (nmem);
2514
2515         if (m_server && m_server->handle_init_response_for_invalid_session(apdu))
2516             return;
2517     }
2518     if (apdu->which == Z_APDU_searchResponse)
2519     {
2520         Z_SearchResponse *sr = apdu->u.searchResponse;
2521         m_last_resultCount = *sr->resultCount;
2522         int status = *sr->searchStatus;
2523         if (status && (!sr->records || sr->records->which == Z_Records_DBOSD))
2524         {
2525             m_last_ok = 1;
2526             
2527             if (sr->records && sr->records->which == Z_Records_DBOSD)
2528             {
2529                 m_cache.add(odr_decode(),
2530                             sr->records->u.databaseOrSurDiagnostics, 1,
2531                             *sr->resultCount);
2532             }
2533         }
2534     }
2535     if (apdu->which == Z_APDU_presentResponse)
2536     {
2537         Z_PresentResponse *pr = apdu->u.presentResponse;
2538         if (m_sr_transform)
2539         {
2540             m_sr_transform = 0;
2541             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
2542             Z_SearchResponse *sr = new_apdu->u.searchResponse;
2543             sr->referenceId = pr->referenceId;
2544             *sr->resultCount = m_last_resultCount;
2545             sr->records = pr->records;
2546             sr->nextResultSetPosition = pr->nextResultSetPosition;
2547             sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
2548             apdu = new_apdu;
2549         }
2550         if (pr->records && 
2551             pr->records->which == Z_Records_DBOSD && m_resultSetStartPoint)
2552         {
2553             m_cache.add(odr_decode(),
2554                         pr->records->u.databaseOrSurDiagnostics,
2555                         m_resultSetStartPoint, -1);
2556             m_resultSetStartPoint = 0;
2557         }
2558     }
2559     if (m_cookie)
2560         set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
2561     if (m_server)
2562     {
2563         m_server->send_to_client(apdu);
2564     }
2565     if (apdu->which == Z_APDU_close)
2566     {
2567         shutdown();
2568     }
2569 }
2570
2571 void Yaz_Proxy::low_socket_close()
2572 {
2573     int i;
2574     for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
2575         if  (m_lo_fd[i] >= 0)
2576             ::close(m_lo_fd[i]);
2577 }
2578
2579 void Yaz_Proxy::low_socket_open()
2580 {
2581     int i;
2582     for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
2583         m_lo_fd[i] = open("/dev/null", O_RDONLY);
2584 }
2585
2586 int Yaz_Proxy::server(const char *addr)
2587 {
2588     int r = Yaz_Z_Assoc::server(addr);
2589     if (!r)
2590     {
2591         yaz_log(LOG_LOG, "%sStarted proxy " VERSION " on %s", m_session_str, addr);
2592         timeout(1);
2593     }
2594     return r;
2595 }
2596