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