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