More type casts. Modify CQL tree - bool is C++ reserved name.
[yaz-moved-to-github.git] / server / seshigh.c
1 /*
2  * Copyright (c) 1995-2003, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: seshigh.c,v 1.135 2003-02-14 18:49:24 adam Exp $
6  */
7
8 /*
9  * Frontend server logic.
10  *
11  * This code receives incoming APDUs, and handles client requests by means
12  * of the backend API.
13  *
14  * Some of the code is getting quite involved, compared to simpler servers -
15  * primarily because it is asynchronous both in the communication with
16  * the user and the backend. We think the complexity will pay off in
17  * the form of greater flexibility when more asynchronous facilities
18  * are implemented.
19  *
20  * Memory management has become somewhat involved. In the simple case, where
21  * only one PDU is pending at a time, it will simply reuse the same memory,
22  * once it has found its working size. When we enable multiple concurrent
23  * operations, perhaps even with multiple parallel calls to the backend, it
24  * will maintain a pool of buffers for encoding and decoding, trying to
25  * minimize memory allocation/deallocation during normal operation.
26  *
27  */
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #ifdef WIN32
33 #include <io.h>
34 #define S_ISREG(x) (x & _S_IFREG)
35 #include <process.h>
36 #else
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #endif
40 #include <assert.h>
41
42 #include <yaz/yconfig.h>
43 #include <yaz/xmalloc.h>
44 #include <yaz/comstack.h>
45 #include "eventl.h"
46 #include "session.h"
47 #include <yaz/proto.h>
48 #include <yaz/oid.h>
49 #include <yaz/log.h>
50 #include <yaz/logrpn.h>
51 #include <yaz/statserv.h>
52 #include <yaz/diagbib1.h>
53 #include <yaz/charneg.h>
54 #include <yaz/otherinfo.h>
55 #include <yaz/yaz-util.h>
56
57 #include <yaz/srw.h>
58 #include <yaz/backend.h>
59
60 static void process_gdu_request(association *assoc, request *req);
61 static int process_z_request(association *assoc, request *req, char **msg);
62 void backend_response(IOCHAN i, int event);
63 static int process_gdu_response(association *assoc, request *req, Z_GDU *res);
64 static int process_z_response(association *assoc, request *req, Z_APDU *res);
65 static Z_APDU *process_initRequest(association *assoc, request *reqb);
66 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
67     int *fd);
68 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
69     bend_search_rr *bsrr, int *fd);
70 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
71     int *fd);
72 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
73 static Z_APDU *process_sortRequest(association *assoc, request *reqb, int *fd);
74 static void process_close(association *assoc, request *reqb);
75 void save_referenceId (request *reqb, Z_ReferenceId *refid);
76 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
77     int *fd);
78 static Z_APDU *process_segmentRequest (association *assoc, request *reqb);
79
80 static FILE *apduf = 0; /* for use in static mode */
81 static statserv_options_block *control_block = 0;
82
83 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd);
84
85 /*
86  * Create and initialize a new association-handle.
87  *  channel  : iochannel for the current line.
88  *  link     : communications channel.
89  * Returns: 0 or a new association handle.
90  */
91 association *create_association(IOCHAN channel, COMSTACK link)
92 {
93     association *anew;
94
95     if (!control_block)
96         control_block = statserv_getcontrol();
97     if (!(anew = (association *)xmalloc(sizeof(*anew))))
98         return 0;
99     anew->init = 0;
100     anew->client_chan = channel;
101     anew->client_link = link;
102     anew->cs_get_mask = 0;
103     anew->cs_put_mask = 0;
104     anew->cs_accept_mask = 0;
105     if (!(anew->decode = odr_createmem(ODR_DECODE)) ||
106         !(anew->encode = odr_createmem(ODR_ENCODE)))
107         return 0;
108     if (*control_block->apdufile)
109     {
110         char filename[256];
111         FILE *f;
112
113         strcpy(filename, control_block->apdufile);
114         if (!(anew->print = odr_createmem(ODR_PRINT)))
115             return 0;
116         if (*control_block->apdufile == '@')
117         {
118             odr_setprint(anew->print, yaz_log_file());
119         }       
120         else if (*control_block->apdufile != '-')
121         {
122             strcpy(filename, control_block->apdufile);
123             if (!control_block->dynamic)
124             {
125                 if (!apduf)
126                 {
127                     if (!(apduf = fopen(filename, "w")))
128                     {
129                         yaz_log(LOG_WARN|LOG_ERRNO, "%s", filename);
130                         return 0;
131                     }
132                     setvbuf(apduf, 0, _IONBF, 0);
133                 }
134                 f = apduf;
135             }
136             else 
137             {
138                 sprintf(filename + strlen(filename), ".%d", getpid());
139                 if (!(f = fopen(filename, "w")))
140                 {
141                     yaz_log(LOG_WARN|LOG_ERRNO, "%s", filename);
142                     return 0;
143                 }
144                 setvbuf(f, 0, _IONBF, 0);
145             }
146             odr_setprint(anew->print, f);
147         }
148     }
149     else
150         anew->print = 0;
151     anew->input_buffer = 0;
152     anew->input_buffer_len = 0;
153     anew->backend = 0;
154     anew->state = ASSOC_NEW;
155     request_initq(&anew->incoming);
156     request_initq(&anew->outgoing);
157     anew->proto = cs_getproto(link);
158     return anew;
159 }
160
161 /*
162  * Free association and release resources.
163  */
164 void destroy_association(association *h)
165 {
166     statserv_options_block *cb = statserv_getcontrol();
167
168     xfree(h->init);
169     odr_destroy(h->decode);
170     odr_destroy(h->encode);
171     if (h->print)
172         odr_destroy(h->print);
173     if (h->input_buffer)
174     xfree(h->input_buffer);
175     if (h->backend)
176         (*cb->bend_close)(h->backend);
177     while (request_deq(&h->incoming));
178     while (request_deq(&h->outgoing));
179     request_delq(&h->incoming);
180     request_delq(&h->outgoing);
181     xfree(h);
182     xmalloc_trav("session closed");
183     if (control_block && control_block->one_shot)
184         exit (0);
185 }
186
187 static void do_close_req(association *a, int reason, char *message,
188                          request *req)
189 {
190     Z_APDU apdu;
191     Z_Close *cls = zget_Close(a->encode);
192     
193     /* Purge request queue */
194     while (request_deq(&a->incoming));
195     while (request_deq(&a->outgoing));
196     if (a->version >= 3)
197     {
198         yaz_log(LOG_LOG, "Sending Close PDU, reason=%d, message=%s",
199             reason, message ? message : "none");
200         apdu.which = Z_APDU_close;
201         apdu.u.close = cls;
202         *cls->closeReason = reason;
203         cls->diagnosticInformation = message;
204         process_z_response(a, req, &apdu);
205         iochan_settimeout(a->client_chan, 60);
206     }
207     else
208     {
209         yaz_log(LOG_DEBUG, "v2 client. No Close PDU");
210         iochan_setevent(a->client_chan, EVENT_TIMEOUT); /* force imm close */
211     }
212     a->state = ASSOC_DEAD;
213 }
214
215 static void do_close(association *a, int reason, char *message)
216 {
217     do_close_req (a, reason, message, request_get(&a->outgoing));
218 }
219
220 /*
221  * This is where PDUs from the client are read and the further
222  * processing is initiated. Flow of control moves down through the
223  * various process_* functions below, until the encoded result comes back up
224  * to the output handler in here.
225  * 
226  *  h     : the I/O channel that has an outstanding event.
227  *  event : the current outstanding event.
228  */
229 void ir_session(IOCHAN h, int event)
230 {
231     int res;
232     association *assoc = (association *)iochan_getdata(h);
233     COMSTACK conn = assoc->client_link;
234     request *req;
235
236     assert(h && conn && assoc);
237     if (event == EVENT_TIMEOUT)
238     {
239         if (assoc->state != ASSOC_UP)
240         {
241             yaz_log(LOG_LOG, "Final timeout - closing connection.");
242             cs_close(conn);
243             destroy_association(assoc);
244             iochan_destroy(h);
245         }
246         else
247         {
248             yaz_log(LOG_LOG, "Session idle too long. Sending close.");
249             do_close(assoc, Z_Close_lackOfActivity, 0);
250         }
251         return;
252     }
253     if (event & assoc->cs_accept_mask)
254     {
255         yaz_log (LOG_DEBUG, "ir_session (accept)");
256         if (!cs_accept (conn))
257         {
258             yaz_log (LOG_LOG, "accept failed");
259             destroy_association(assoc);
260             iochan_destroy(h);
261         }
262         iochan_clearflag (h, EVENT_OUTPUT|EVENT_OUTPUT);
263         if (conn->io_pending) 
264         {   /* cs_accept didn't complete */
265             assoc->cs_accept_mask = 
266                 ((conn->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
267                 ((conn->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
268
269             iochan_setflag (h, assoc->cs_accept_mask);
270         }
271         else
272         {   /* cs_accept completed. Prepare for reading (cs_get) */
273             assoc->cs_accept_mask = 0;
274             assoc->cs_get_mask = EVENT_INPUT;
275             iochan_setflag (h, assoc->cs_get_mask);
276         }
277         return;
278     }
279     if ((event & assoc->cs_get_mask) || (event & EVENT_WORK)) /* input */
280     {
281         if ((assoc->cs_put_mask & EVENT_INPUT) == 0 && (event & assoc->cs_get_mask))
282         {
283             yaz_log(LOG_DEBUG, "ir_session (input)");
284             /* We aren't speaking to this fellow */
285             if (assoc->state == ASSOC_DEAD)
286             {
287                 yaz_log(LOG_LOG, "Closed connection after reject");
288                 cs_close(conn);
289                 destroy_association(assoc);
290                 iochan_destroy(h);
291                 return;
292             }
293             assoc->cs_get_mask = EVENT_INPUT;
294             if ((res = cs_get(conn, &assoc->input_buffer,
295                 &assoc->input_buffer_len)) <= 0)
296             {
297                 yaz_log(LOG_LOG, "Connection closed by client");
298                 cs_close(conn);
299                 destroy_association(assoc);
300                 iochan_destroy(h);
301                 return;
302             }
303             else if (res == 1) /* incomplete read - wait for more  */
304             {
305                 if (conn->io_pending & CS_WANT_WRITE)
306                     assoc->cs_get_mask |= EVENT_OUTPUT;
307                 iochan_setflag(h, assoc->cs_get_mask);
308                 return;
309             }
310             if (cs_more(conn)) /* more stuff - call us again later, please */
311                 iochan_setevent(h, EVENT_INPUT);
312                 
313             /* we got a complete PDU. Let's decode it */
314             yaz_log(LOG_DEBUG, "Got PDU, %d bytes: lead=%02X %02X %02X", res,
315                             assoc->input_buffer[0] & 0xff,
316                             assoc->input_buffer[1] & 0xff,
317                             assoc->input_buffer[2] & 0xff);
318             req = request_get(&assoc->incoming); /* get a new request structure */
319             odr_reset(assoc->decode);
320             odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
321             if (!z_GDU(assoc->decode, &req->gdu_request, 0, 0))
322             {
323                 yaz_log(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
324                         odr_errmsg(odr_geterror(assoc->decode)),
325                         odr_offset(assoc->decode));
326                 if (assoc->decode->error != OHTTP)
327                 {
328                     yaz_log(LOG_LOG, "PDU dump:");
329                     odr_dumpBER(yaz_log_file(), assoc->input_buffer, res);
330                     do_close(assoc, Z_Close_protocolError, "Malformed package");
331                 }
332                 else
333                 {
334                     Z_GDU *p = z_get_HTTP_Response(assoc->encode, 400);
335                     assoc->state = ASSOC_DEAD;
336                     process_gdu_response(assoc, req, p);
337                 }
338                 return;
339             }
340             req->request_mem = odr_extract_mem(assoc->decode);
341             if (assoc->print && !z_GDU(assoc->print, &req->gdu_request, 0, 0))
342             {
343                 yaz_log(LOG_WARN, "ODR print error: %s", 
344                     odr_errmsg(odr_geterror(assoc->print)));
345                 odr_reset(assoc->print);
346             }
347             request_enq(&assoc->incoming, req);
348         }
349
350         /* can we do something yet? */
351         req = request_head(&assoc->incoming);
352         if (req->state == REQUEST_IDLE)
353         {
354             request_deq(&assoc->incoming);
355             process_gdu_request(assoc, req);
356         }
357     }
358     if (event & assoc->cs_put_mask)
359     {
360         request *req = request_head(&assoc->outgoing);
361
362         assoc->cs_put_mask = 0;
363         yaz_log(LOG_DEBUG, "ir_session (output)");
364         req->state = REQUEST_PENDING;
365         switch (res = cs_put(conn, req->response, req->len_response))
366         {
367         case -1:
368             yaz_log(LOG_LOG, "Connection closed by client");
369             cs_close(conn);
370             destroy_association(assoc);
371             iochan_destroy(h);
372             break;
373         case 0: /* all sent - release the request structure */
374             yaz_log(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
375 #if 0
376             yaz_log(LOG_DEBUG, "HTTP out:\n%.*s", req->len_response,
377                     req->response);
378 #endif
379             nmem_destroy(req->request_mem);
380             request_deq(&assoc->outgoing);
381             request_release(req);
382             if (!request_head(&assoc->outgoing))
383             {   /* restore mask for cs_get operation ... */
384                 iochan_clearflag(h, EVENT_OUTPUT|EVENT_INPUT);
385                 iochan_setflag(h, assoc->cs_get_mask);
386                 yaz_log(LOG_LOG, "queue empty mask=%d", assoc->cs_get_mask);
387                 if (assoc->state == ASSOC_DEAD)
388                     iochan_setevent(assoc->client_chan, EVENT_TIMEOUT);
389             }
390             else
391             {
392                 assoc->cs_put_mask = EVENT_OUTPUT;
393                 yaz_log(LOG_LOG, "queue not empty");
394             }
395             break;
396         default:
397             if (conn->io_pending & CS_WANT_WRITE)
398                 assoc->cs_put_mask |= EVENT_OUTPUT;
399             if (conn->io_pending & CS_WANT_READ)
400                 assoc->cs_put_mask |= EVENT_INPUT;
401             iochan_setflag(h, assoc->cs_put_mask);
402         }
403     }
404     if (event & EVENT_EXCEPT)
405     {
406         yaz_log(LOG_LOG, "ir_session (exception)");
407         cs_close(conn);
408         destroy_association(assoc);
409         iochan_destroy(h);
410     }
411 }
412
413 static int process_z_request(association *assoc, request *req, char **msg);
414
415 static int srw_bend_init(association *assoc)
416 {
417     bend_initresult *binitres;
418     statserv_options_block *cb = statserv_getcontrol();
419     
420     assoc->init = (bend_initrequest *) xmalloc (sizeof(*assoc->init));
421
422     assoc->init->stream = assoc->encode;
423     assoc->init->print = assoc->print;
424     assoc->init->auth = 0;
425     assoc->init->referenceId = 0;
426     assoc->init->implementation_version = 0;
427     assoc->init->implementation_id = 0;
428     assoc->init->implementation_name = 0;
429     assoc->init->bend_sort = NULL;
430     assoc->init->bend_search = NULL;
431     assoc->init->bend_present = NULL;
432     assoc->init->bend_esrequest = NULL;
433     assoc->init->bend_delete = NULL;
434     assoc->init->bend_scan = NULL;
435     assoc->init->bend_segment = NULL;
436     assoc->init->bend_fetch = NULL;
437     assoc->init->charneg_request = NULL;
438     assoc->init->charneg_response = NULL;
439     assoc->init->decode = assoc->decode;
440
441     assoc->init->peer_name =
442         odr_strdup (assoc->encode, cs_addrstr(assoc->client_link));
443     if (!(binitres = (*cb->bend_init)(assoc->init)))
444     {
445         yaz_log(LOG_WARN, "Bad response from backend.");
446         return 0;
447     }
448     assoc->backend = binitres->handle;
449     return 1;
450 }
451
452 static void srw_bend_fetch(association *assoc, int pos,
453                            Z_SRW_searchRetrieveRequest *srw_req,
454                            Z_SRW_record *record)
455 {
456     bend_fetch_rr rr;
457     ODR o = assoc->encode;
458
459     rr.setname = "default";
460     rr.number = pos;
461     rr.referenceId = 0;
462     rr.request_format = VAL_TEXT_XML;
463     rr.request_format_raw = yaz_oidval_to_z3950oid(assoc->decode,
464                                                    CLASS_TRANSYN,
465                                                    VAL_TEXT_XML);
466     rr.comp = (Z_RecordComposition *)
467             odr_malloc(assoc->decode, sizeof(*rr.comp));
468     rr.comp->which = Z_RecordComp_complex;
469     rr.comp->u.complex = (Z_CompSpec *)
470             odr_malloc(assoc->decode, sizeof(Z_CompSpec));
471     rr.comp->u.complex->selectAlternativeSyntax = (bool_t *)
472         odr_malloc(assoc->encode, sizeof(bool_t));
473     *rr.comp->u.complex->selectAlternativeSyntax = 0;    
474     rr.comp->u.complex->num_dbSpecific = 0;
475     rr.comp->u.complex->dbSpecific = 0;
476     rr.comp->u.complex->num_recordSyntax = 0; 
477     rr.comp->u.complex->recordSyntax = 0;
478
479     rr.comp->u.complex->generic = (Z_Specification *) 
480             odr_malloc(assoc->decode, sizeof(Z_Specification));
481     rr.comp->u.complex->generic->which = Z_Specification_uri;
482     rr.comp->u.complex->generic->u.uri = srw_req->recordSchema;
483     rr.comp->u.complex->generic->elementSpec = 0;
484     
485     rr.stream = assoc->encode;
486     rr.print = assoc->print;
487
488     rr.basename = 0;
489     rr.len = 0;
490     rr.record = 0;
491     rr.last_in_set = 0;
492     rr.output_format = VAL_TEXT_XML;
493     rr.output_format_raw = 0;
494     rr.errcode = 0;
495     rr.errstring = 0;
496     rr.surrogate_flag = 0;
497     
498     (*assoc->init->bend_fetch)(assoc->backend, &rr);
499
500     if (rr.len >= 0)
501     {
502         record->recordData_buf = rr.record;
503         record->recordData_len = rr.len;
504         record->recordPosition = odr_intdup(o, pos);
505         record->recordSchema = odr_strdup(o, srw_req->recordSchema);
506     }
507 }
508
509 static void srw_bend_search(association *assoc, request *req,
510                             Z_SRW_searchRetrieveRequest *srw_req,
511                             Z_SRW_searchRetrieveResponse *srw_res)
512 {
513     char *base = "Default";
514     bend_search_rr rr;
515     Z_External *ext;
516
517     if (!assoc->init)
518         srw_bend_init(assoc);
519
520     rr.setname = "default";
521     rr.replace_set = 1;
522     rr.num_bases = 1;
523     rr.basenames = &base;
524     rr.referenceId = 0;
525
526     ext = (Z_External *) odr_malloc(assoc->decode, sizeof(*ext));
527     ext->direct_reference = odr_getoidbystr(assoc->decode, 
528                                             "1.2.840.10003.16.2");
529     ext->indirect_reference = 0;
530     ext->descriptor = 0;
531     ext->which = Z_External_CQL;
532     if (srw_req->query)
533         ext->u.cql = srw_req->query;
534     else
535         ext->u.cql = "noterm";
536     
537     rr.query = (Z_Query *) odr_malloc (assoc->decode, sizeof(*rr.query));
538     rr.query->which = Z_Query_type_104;
539     rr.query->u.type_104 =  ext;
540     
541     rr.stream = assoc->encode;
542     rr.decode = assoc->decode;
543     rr.print = assoc->print;
544     rr.request = req;
545     rr.association = assoc;
546     rr.fd = 0;
547     rr.hits = 0;
548     rr.errcode = 0;
549     rr.errstring = 0;
550     rr.search_info = 0;
551     (assoc->init->bend_search)(assoc->backend, &rr);
552     srw_res->numberOfRecords = odr_intdup(assoc->encode, rr.hits);
553     if (rr.errcode)
554     {
555         srw_res->num_diagnostics = 1;
556         srw_res->diagnostics = (Z_SRW_diagnostic *)
557             odr_malloc(assoc->encode, sizeof(*srw_res->diagnostics));
558         srw_res->diagnostics[0].code = 
559             odr_intdup(assoc->encode, rr.errcode);
560     }
561     else
562     {
563         srw_res->numberOfRecords = odr_intdup(assoc->encode, rr.hits);
564         if (srw_req->maximumRecords && *srw_req->maximumRecords > 0)
565         {
566             int number = *srw_req->maximumRecords;
567             int start = 1;
568             int i;
569             if (srw_req->startRecord)
570                 start = *srw_req->startRecord;
571             if (start <= rr.hits)
572             {
573                 int j = 0;
574                 if (start + number > rr.hits)
575                     number = rr.hits - start + 1;
576                 srw_res->records = (Z_SRW_record *)
577                     odr_malloc(assoc->encode,
578                                number * sizeof(*srw_res->records));
579                 for (i = 0; i<number; i++)
580                 {
581                     srw_res->records[j].recordData_buf = 0;
582                     srw_bend_fetch(assoc, i+start, srw_req,
583                                    srw_res->records + j);
584                     if (srw_res->records[j].recordData_buf)
585                         j++;
586                 }
587                 srw_res->num_records = j;
588                 if (!j)
589                     srw_res->records = 0;
590                 yaz_log(LOG_LOG, "got %d records", j);
591             }
592         }
593     }
594 }
595
596 static void process_http_request(association *assoc, request *req)
597 {
598     Z_HTTP_Request *hreq = req->gdu_request->u.HTTP_Request;
599     ODR o = assoc->encode;
600     Z_GDU *p = 0;
601     Z_HTTP_Response *hres = 0;
602     int keepalive = 1;
603
604     if (!strcmp(hreq->method, "GET"))
605     {
606 #ifdef DOCDIR
607         if (strlen(hreq->path) >= 5 && strlen(hreq->path) < 80 &&
608                          !memcmp(hreq->path, "/doc/", 5))
609         {
610             FILE *f;
611             char fpath[120];
612
613             strcpy(fpath, DOCDIR);
614             strcat(fpath, hreq->path+4);
615             f = fopen(fpath, "rb");
616             if (f) {
617                 struct stat sbuf;
618                 if (fstat(fileno(f), &sbuf) || !S_ISREG(sbuf.st_mode))
619                 {
620                     fclose(f);
621                     f = 0;
622                 }
623             }
624             if (f)
625             {
626                 long sz;
627                 fseek(f, 0L, SEEK_END);
628                 sz = ftell(f);
629                 if (sz >= 0 && sz < 500000)
630                 {
631                     const char *ctype = "application/octet-stream";
632                     const char *cp;
633
634                     p = z_get_HTTP_Response(o, 200);
635                     hres = p->u.HTTP_Response;
636                     hres->content_buf = (char *) odr_malloc(o, sz + 1);
637                     hres->content_len = sz;
638                     fseek(f, 0L, SEEK_SET);
639                     fread(hres->content_buf, 1, sz, f);
640                     if ((cp = strrchr(fpath, '.'))) {
641                         cp++;
642                         if (!strcmp(cp, "png"))
643                             ctype = "image/png";
644                         else if (!strcmp(cp, "gif"))
645                             ctype = "image/gif";
646                         else if (!strcmp(cp, "xml"))
647                             ctype = "text/xml";
648                         else if (!strcmp(cp, "html"))
649                             ctype = "text/html";
650                     }
651                     z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
652                     yaz_log(LOG_LOG, "OK send page %s size=%ld", fpath, sz);
653                 }
654                 fclose(f);
655             }
656         }
657 #endif
658         if (!strcmp(hreq->path, "/")) 
659         {
660             struct stat sbuf;
661             const char *doclink = "";
662             p = z_get_HTTP_Response(o, 200);
663             hres = p->u.HTTP_Response;
664             hres->content_buf = (char *) odr_malloc(o, 400);
665 #ifdef DOCDIR
666             if (stat(DOCDIR "/yaz.html", &sbuf) == 0 && S_ISREG(sbuf.st_mode))
667                 doclink = "<P><A HREF=\"/doc/yaz.html\">Documentation</A></P>";
668 #endif
669             sprintf (hres->content_buf, 
670                      "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
671                      "<HTML>\n"
672                      " <HEAD>\n"
673                      "  <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
674                      " </HEAD>\n"
675                      " <BODY>\n"
676                      "  <P><A HREF=\"http://www.indexdata.dk/yaz/\">YAZ</A> " 
677                      YAZ_VERSION "</P>\n"
678                      "%s"
679                      " </BODY>\n"
680                      "</HTML>\n", doclink);
681             hres->content_len = strlen(hres->content_buf);
682             z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
683         }
684         if (!p)
685         {
686             p = z_get_HTTP_Response(o, 404);
687         }
688     }
689     else if (!strcmp(hreq->method, "POST"))
690     {
691 #if HAVE_XSLT
692         const char *content_type = z_HTTP_header_lookup(hreq->headers,
693                                                         "Content-Type");
694         const char *soap_action = z_HTTP_header_lookup(hreq->headers,
695                                                        "SOAPAction");
696         if (content_type && soap_action && 
697             !yaz_strcmp_del("text/xml", content_type, "; "))
698         {
699             Z_SOAP *soap_package = 0;
700             int ret;
701             int http_code = 500;
702
703             static Z_SOAP_Handler soap_handlers[2] = {
704                 {"http://www.loc.gov/zing/srw/v1.0/", 0,
705                                          (Z_SOAP_fun) yaz_srw_codec},
706                 {0, 0, 0}
707             };
708
709             ret = z_soap_codec(assoc->decode, &soap_package, 
710                                &hreq->content_buf, &hreq->content_len,
711                                soap_handlers);
712             
713             if (!ret && soap_package->which == Z_SOAP_generic &&
714                 soap_package->u.generic->no == 0)
715             {
716                 /* SRW package */
717                 Z_SRW_searchRetrieve *sr = soap_package->u.generic->p;
718                 
719                 if (sr->which == Z_SRW_searchRetrieve_request)
720                 {
721                     Z_SRW_searchRetrieve *res =
722                         yaz_srw_get(assoc->encode,
723                                     Z_SRW_searchRetrieve_response);
724                     
725                     srw_bend_search(assoc, req, sr->u.request, res->u.response);
726                     
727                     soap_package->u.generic->p = res;
728                     http_code = 200;
729                 }
730             }
731
732             p = z_get_HTTP_Response(o, 200);
733             hres = p->u.HTTP_Response;
734             ret = z_soap_codec(assoc->encode, &soap_package,
735                                &hres->content_buf, &hres->content_len,
736                                soap_handlers);
737             hres->code = http_code;
738         }
739 #endif
740         if (!p) /* still no response ? */
741             p = z_get_HTTP_Response(o, 500);
742     }
743     else
744     {
745         p = z_get_HTTP_Response(o, 405);
746         hres = p->u.HTTP_Response;
747
748         z_HTTP_header_add(o, &hres->headers, "Allow", "GET, POST");
749     }
750     hres = p->u.HTTP_Response;
751     if (!strcmp(hreq->version, "1.0")) 
752     {
753         const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
754         if (v && !strcmp(v, "Keep-Alive"))
755             keepalive = 1;
756         else
757             keepalive = 0;
758         hres->version = "1.0";
759     }
760     else
761     {
762         const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
763         if (v && !strcmp(v, "close"))
764             keepalive = 0;
765         else
766             keepalive = 1;
767         hres->version = "1.1";
768     }
769     if (!keepalive)
770     {
771         z_HTTP_header_add(o, &hres->headers, "Connection", "close");
772         assoc->state = ASSOC_DEAD;
773     }
774     else
775     {
776         z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
777     }
778     process_gdu_response(assoc, req, p);
779 }
780
781 static void process_gdu_request(association *assoc, request *req)
782 {
783     if (req->gdu_request->which == Z_GDU_Z3950)
784     {
785         char *msg = 0;
786         req->apdu_request = req->gdu_request->u.z3950;
787         if (process_z_request(assoc, req, &msg) < 0)
788             do_close_req(assoc, Z_Close_systemProblem, msg, req);
789     }
790     else if (req->gdu_request->which == Z_GDU_HTTP_Request)
791         process_http_request(assoc, req);
792     else
793     {
794         do_close_req(assoc, Z_Close_systemProblem, "bad protocol packet", req);
795     }
796 }
797
798 /*
799  * Initiate request processing.
800  */
801 static int process_z_request(association *assoc, request *req, char **msg)
802 {
803     int fd = -1;
804     Z_APDU *res;
805     int retval;
806     
807     *msg = "Unknown Error";
808     assert(req && req->state == REQUEST_IDLE);
809     if (req->apdu_request->which != Z_APDU_initRequest && !assoc->init)
810     {
811         *msg = "Missing InitRequest";
812         return -1;
813     }
814     switch (req->apdu_request->which)
815     {
816     case Z_APDU_initRequest:
817         res = process_initRequest(assoc, req); break;
818     case Z_APDU_searchRequest:
819         res = process_searchRequest(assoc, req, &fd); break;
820     case Z_APDU_presentRequest:
821         res = process_presentRequest(assoc, req, &fd); break;
822     case Z_APDU_scanRequest:
823         if (assoc->init->bend_scan)
824             res = process_scanRequest(assoc, req, &fd);
825         else
826         {
827             *msg = "Cannot handle Scan APDU";
828             return -1;
829         }
830         break;
831     case Z_APDU_extendedServicesRequest:
832         if (assoc->init->bend_esrequest)
833             res = process_ESRequest(assoc, req, &fd);
834         else
835         {
836             *msg = "Cannot handle Extended Services APDU";
837             return -1;
838         }
839         break;
840     case Z_APDU_sortRequest:
841         if (assoc->init->bend_sort)
842             res = process_sortRequest(assoc, req, &fd);
843         else
844         {
845             *msg = "Cannot handle Sort APDU";
846             return -1;
847         }
848         break;
849     case Z_APDU_close:
850         process_close(assoc, req);
851         return 0;
852     case Z_APDU_deleteResultSetRequest:
853         if (assoc->init->bend_delete)
854             res = process_deleteRequest(assoc, req, &fd);
855         else
856         {
857             *msg = "Cannot handle Delete APDU";
858             return -1;
859         }
860         break;
861     case Z_APDU_segmentRequest:
862         if (assoc->init->bend_segment)
863         {
864             res = process_segmentRequest (assoc, req);
865         }
866         else
867         {
868             *msg = "Cannot handle Segment APDU";
869             return -1;
870         }
871         break;
872     default:
873         *msg = "Bad APDU received";
874         return -1;
875     }
876     if (res)
877     {
878         yaz_log(LOG_DEBUG, "  result immediately available");
879         retval = process_z_response(assoc, req, res);
880     }
881     else if (fd < 0)
882     {
883         yaz_log(LOG_DEBUG, "  result unavailble");
884         retval = 0;
885     }
886     else /* no result yet - one will be provided later */
887     {
888         IOCHAN chan;
889
890         /* Set up an I/O handler for the fd supplied by the backend */
891
892         yaz_log(LOG_DEBUG, "   establishing handler for result");
893         req->state = REQUEST_PENDING;
894         if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
895             abort();
896         iochan_setdata(chan, assoc);
897         retval = 0;
898     }
899     return retval;
900 }
901
902 /*
903  * Handle message from the backend.
904  */
905 void backend_response(IOCHAN i, int event)
906 {
907     association *assoc = (association *)iochan_getdata(i);
908     request *req = request_head(&assoc->incoming);
909     Z_APDU *res;
910     int fd;
911
912     yaz_log(LOG_DEBUG, "backend_response");
913     assert(assoc && req && req->state != REQUEST_IDLE);
914     /* determine what it is we're waiting for */
915     switch (req->apdu_request->which)
916     {
917         case Z_APDU_searchRequest:
918             res = response_searchRequest(assoc, req, 0, &fd); break;
919 #if 0
920         case Z_APDU_presentRequest:
921             res = response_presentRequest(assoc, req, 0, &fd); break;
922         case Z_APDU_scanRequest:
923             res = response_scanRequest(assoc, req, 0, &fd); break;
924 #endif
925         default:
926             yaz_log(LOG_WARN, "Serious programmer's lapse or bug");
927             abort();
928     }
929     if ((res && process_z_response(assoc, req, res) < 0) || fd < 0)
930     {
931         yaz_log(LOG_LOG, "Fatal error when talking to backend");
932         do_close(assoc, Z_Close_systemProblem, 0);
933         iochan_destroy(i);
934         return;
935     }
936     else if (!res) /* no result yet - try again later */
937     {
938         yaz_log(LOG_DEBUG, "   no result yet");
939         iochan_setfd(i, fd); /* in case fd has changed */
940     }
941 }
942
943 /*
944  * Encode response, and transfer the request structure to the outgoing queue.
945  */
946 static int process_gdu_response(association *assoc, request *req, Z_GDU *res)
947 {
948     odr_setbuf(assoc->encode, req->response, req->size_response, 1);
949
950     if (assoc->print && !z_GDU(assoc->print, &res, 0, 0))
951     {
952         yaz_log(LOG_WARN, "ODR print error: %s", 
953             odr_errmsg(odr_geterror(assoc->print)));
954         odr_reset(assoc->print);
955     }
956     if (!z_GDU(assoc->encode, &res, 0, 0))
957     {
958         yaz_log(LOG_WARN, "ODR error when encoding response: %s",
959             odr_errmsg(odr_geterror(assoc->decode)));
960         return -1;
961     }
962     req->response = odr_getbuf(assoc->encode, &req->len_response,
963         &req->size_response);
964     odr_setbuf(assoc->encode, 0, 0, 0); /* don'txfree if we abort later */
965     odr_reset(assoc->encode);
966     req->state = REQUEST_IDLE;
967     request_enq(&assoc->outgoing, req);
968     /* turn the work over to the ir_session handler */
969     iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
970     assoc->cs_put_mask = EVENT_OUTPUT;
971     /* Is there more work to be done? give that to the input handler too */
972 #if 1
973     if (request_head(&assoc->incoming))
974     {
975         yaz_log (LOG_DEBUG, "more work to be done");
976         iochan_setevent(assoc->client_chan, EVENT_WORK);
977     }
978 #endif
979     return 0;
980 }
981
982 /*
983  * Encode response, and transfer the request structure to the outgoing queue.
984  */
985 static int process_z_response(association *assoc, request *req, Z_APDU *res)
986 {
987     odr_setbuf(assoc->encode, req->response, req->size_response, 1);
988
989     if (assoc->print && !z_APDU(assoc->print, &res, 0, 0))
990     {
991         yaz_log(LOG_WARN, "ODR print error: %s", 
992             odr_errmsg(odr_geterror(assoc->print)));
993         odr_reset(assoc->print);
994     }
995     if (!z_APDU(assoc->encode, &res, 0, 0))
996     {
997         yaz_log(LOG_WARN, "ODR error when encoding response: %s",
998             odr_errmsg(odr_geterror(assoc->decode)));
999         return -1;
1000     }
1001     req->response = odr_getbuf(assoc->encode, &req->len_response,
1002         &req->size_response);
1003     odr_setbuf(assoc->encode, 0, 0, 0); /* don'txfree if we abort later */
1004     odr_reset(assoc->encode);
1005     req->state = REQUEST_IDLE;
1006     request_enq(&assoc->outgoing, req);
1007     /* turn the work over to the ir_session handler */
1008     iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
1009     assoc->cs_put_mask = EVENT_OUTPUT;
1010     /* Is there more work to be done? give that to the input handler too */
1011 #if 1
1012     if (request_head(&assoc->incoming))
1013     {
1014         yaz_log (LOG_DEBUG, "more work to be done");
1015         iochan_setevent(assoc->client_chan, EVENT_WORK);
1016     }
1017 #endif
1018     return 0;
1019 }
1020
1021 /*
1022  * Handle init request.
1023  * At the moment, we don't check the options
1024  * anywhere else in the code - we just try not to do anything that would
1025  * break a naive client. We'll toss 'em into the association block when
1026  * we need them there.
1027  */
1028 static Z_APDU *process_initRequest(association *assoc, request *reqb)
1029 {
1030     statserv_options_block *cb = statserv_getcontrol();
1031     Z_InitRequest *req = reqb->apdu_request->u.initRequest;
1032     Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_initResponse);
1033     Z_InitResponse *resp = apdu->u.initResponse;
1034     bend_initresult *binitres;
1035
1036     char options[140];
1037
1038     xfree (assoc->init);
1039     assoc->init = (bend_initrequest *) xmalloc (sizeof(*assoc->init));
1040
1041     yaz_log(LOG_LOG, "Got initRequest");
1042     if (req->implementationId)
1043         yaz_log(LOG_LOG, "Id:        %s", req->implementationId);
1044     if (req->implementationName)
1045         yaz_log(LOG_LOG, "Name:      %s", req->implementationName);
1046     if (req->implementationVersion)
1047         yaz_log(LOG_LOG, "Version:   %s", req->implementationVersion);
1048
1049     assoc->init->stream = assoc->encode;
1050     assoc->init->print = assoc->print;
1051     assoc->init->auth = req->idAuthentication;
1052     assoc->init->referenceId = req->referenceId;
1053     assoc->init->implementation_version = 0;
1054     assoc->init->implementation_id = 0;
1055     assoc->init->implementation_name = 0;
1056     assoc->init->bend_sort = NULL;
1057     assoc->init->bend_search = NULL;
1058     assoc->init->bend_present = NULL;
1059     assoc->init->bend_esrequest = NULL;
1060     assoc->init->bend_delete = NULL;
1061     assoc->init->bend_scan = NULL;
1062     assoc->init->bend_segment = NULL;
1063     assoc->init->bend_fetch = NULL;
1064     assoc->init->charneg_request = NULL;
1065     assoc->init->charneg_response = NULL;
1066     assoc->init->decode = assoc->decode;
1067
1068     if (ODR_MASK_GET(req->options, Z_Options_negotiationModel))
1069     {
1070         Z_CharSetandLanguageNegotiation *negotiation =
1071             yaz_get_charneg_record (req->otherInfo);
1072         if (negotiation->which == Z_CharSetandLanguageNegotiation_proposal)
1073             assoc->init->charneg_request = negotiation;
1074     }
1075     
1076     assoc->init->peer_name =
1077         odr_strdup (assoc->encode, cs_addrstr(assoc->client_link));
1078     if (!(binitres = (*cb->bend_init)(assoc->init)))
1079     {
1080         yaz_log(LOG_WARN, "Bad response from backend.");
1081         return 0;
1082     }
1083
1084     assoc->backend = binitres->handle;
1085     if ((assoc->init->bend_sort))
1086         yaz_log (LOG_DEBUG, "Sort handler installed");
1087     if ((assoc->init->bend_search))
1088         yaz_log (LOG_DEBUG, "Search handler installed");
1089     if ((assoc->init->bend_present))
1090         yaz_log (LOG_DEBUG, "Present handler installed");   
1091     if ((assoc->init->bend_esrequest))
1092         yaz_log (LOG_DEBUG, "ESRequest handler installed");   
1093     if ((assoc->init->bend_delete))
1094         yaz_log (LOG_DEBUG, "Delete handler installed");   
1095     if ((assoc->init->bend_scan))
1096         yaz_log (LOG_DEBUG, "Scan handler installed");   
1097     if ((assoc->init->bend_segment))
1098         yaz_log (LOG_DEBUG, "Segment handler installed");   
1099     
1100     resp->referenceId = req->referenceId;
1101     *options = '\0';
1102     /* let's tell the client what we can do */
1103     if (ODR_MASK_GET(req->options, Z_Options_search))
1104     {
1105         ODR_MASK_SET(resp->options, Z_Options_search);
1106         strcat(options, "srch");
1107     }
1108     if (ODR_MASK_GET(req->options, Z_Options_present))
1109     {
1110         ODR_MASK_SET(resp->options, Z_Options_present);
1111         strcat(options, " prst");
1112     }
1113     if (ODR_MASK_GET(req->options, Z_Options_delSet) &&
1114         assoc->init->bend_delete)
1115     {
1116         ODR_MASK_SET(resp->options, Z_Options_delSet);
1117         strcat(options, " del");
1118     }
1119     if (ODR_MASK_GET(req->options, Z_Options_extendedServices) &&
1120         assoc->init->bend_esrequest)
1121     {
1122         ODR_MASK_SET(resp->options, Z_Options_extendedServices);
1123         strcat (options, " extendedServices");
1124     }
1125     if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
1126     {
1127         ODR_MASK_SET(resp->options, Z_Options_namedResultSets);
1128         strcat(options, " namedresults");
1129     }
1130     if (ODR_MASK_GET(req->options, Z_Options_scan) && assoc->init->bend_scan)
1131     {
1132         ODR_MASK_SET(resp->options, Z_Options_scan);
1133         strcat(options, " scan");
1134     }
1135     if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
1136     {
1137         ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
1138         strcat(options, " concurrop");
1139     }
1140     if (ODR_MASK_GET(req->options, Z_Options_sort) && assoc->init->bend_sort)
1141     {
1142         ODR_MASK_SET(resp->options, Z_Options_sort);
1143         strcat(options, " sort");
1144     }
1145
1146     if (ODR_MASK_GET(req->options, Z_Options_negotiationModel)
1147         && assoc->init->charneg_response)
1148     {
1149         Z_OtherInformation **p;
1150         Z_OtherInformationUnit *p0;
1151         
1152         yaz_oi_APDU(apdu, &p);
1153         
1154         if ((p0=yaz_oi_update(p, assoc->encode, NULL, 0, 0))) {
1155             ODR_MASK_SET(resp->options, Z_Options_negotiationModel);
1156             
1157             p0->which = Z_OtherInfo_externallyDefinedInfo;
1158             p0->information.externallyDefinedInfo =
1159                 assoc->init->charneg_response;
1160         }
1161         ODR_MASK_SET(resp->options, Z_Options_negotiationModel);
1162         strcat(options, " negotiation");
1163     }
1164
1165     if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
1166     {
1167         ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
1168         assoc->version = 2; /* 1 & 2 are equivalent */
1169     }
1170     if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
1171     {
1172         ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
1173         assoc->version = 2;
1174     }
1175     if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_3))
1176     {
1177         ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_3);
1178         assoc->version = 3;
1179     }
1180
1181     yaz_log(LOG_LOG, "Negotiated to v%d: %s", assoc->version, options);
1182     assoc->maximumRecordSize = *req->maximumRecordSize;
1183     if (assoc->maximumRecordSize > control_block->maxrecordsize)
1184         assoc->maximumRecordSize = control_block->maxrecordsize;
1185     assoc->preferredMessageSize = *req->preferredMessageSize;
1186     if (assoc->preferredMessageSize > assoc->maximumRecordSize)
1187         assoc->preferredMessageSize = assoc->maximumRecordSize;
1188
1189 #if 0
1190     assoc->maximumRecordSize = 3000000;
1191     assoc->preferredMessageSize = 3000000;
1192 #endif
1193
1194     resp->preferredMessageSize = &assoc->preferredMessageSize;
1195     resp->maximumRecordSize = &assoc->maximumRecordSize;
1196
1197     resp->implementationName = "GFS/YAZ";
1198
1199     if (assoc->init->implementation_id)
1200     {
1201         char *nv = (char *)
1202             odr_malloc (assoc->encode,
1203                         strlen(assoc->init->implementation_id) + 10 + 
1204                                strlen(resp->implementationId));
1205         sprintf (nv, "%s / %s",
1206                  resp->implementationId, assoc->init->implementation_id);
1207         resp->implementationId = nv;
1208     }
1209     if (assoc->init->implementation_name)
1210     {
1211         char *nv = (char *)
1212             odr_malloc (assoc->encode,
1213                         strlen(assoc->init->implementation_name) + 10 + 
1214                                strlen(resp->implementationName));
1215         sprintf (nv, "%s / %s",
1216                  resp->implementationName, assoc->init->implementation_name);
1217         resp->implementationName = nv;
1218     }
1219     if (assoc->init->implementation_version)
1220     {
1221         char *nv = (char *)
1222             odr_malloc (assoc->encode,
1223                         strlen(assoc->init->implementation_version) + 10 + 
1224                                strlen(resp->implementationVersion));
1225         sprintf (nv, "YAZ %s / %s",
1226                  resp->implementationVersion,
1227                  assoc->init->implementation_version);
1228         resp->implementationVersion = nv;
1229     }
1230
1231     if (binitres->errcode)
1232     {
1233         yaz_log(LOG_LOG, "Connection rejected by backend.");
1234         *resp->result = 0;
1235         assoc->state = ASSOC_DEAD;
1236     }
1237     else
1238         assoc->state = ASSOC_UP;
1239     return apdu;
1240 }
1241
1242 /*
1243  * These functions should be merged.
1244  */
1245
1246 static void set_addinfo (Z_DefaultDiagFormat *dr, char *addinfo, ODR odr)
1247 {
1248     dr->which = Z_DefaultDiagFormat_v2Addinfo;
1249     dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
1250 }
1251
1252 /*
1253  * nonsurrogate diagnostic record.
1254  */
1255 static Z_Records *diagrec(association *assoc, int error, char *addinfo)
1256 {
1257     Z_Records *rec = (Z_Records *)
1258         odr_malloc (assoc->encode, sizeof(*rec));
1259     int *err = odr_intdup(assoc->encode, error);
1260     Z_DiagRec *drec = (Z_DiagRec *)
1261         odr_malloc (assoc->encode, sizeof(*drec));
1262     Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1263         odr_malloc (assoc->encode, sizeof(*dr));
1264
1265     yaz_log(LOG_LOG, "[%d] %s %s%s", error, diagbib1_str(error),
1266         addinfo ? " -- " : "", addinfo ? addinfo : "");
1267     rec->which = Z_Records_NSD;
1268     rec->u.nonSurrogateDiagnostic = dr;
1269     dr->diagnosticSetId =
1270         yaz_oidval_to_z3950oid (assoc->encode, CLASS_DIAGSET, VAL_BIB1);
1271     dr->condition = err;
1272     set_addinfo (dr, addinfo, assoc->encode);
1273     return rec;
1274 }
1275
1276 /*
1277  * surrogate diagnostic.
1278  */
1279 static Z_NamePlusRecord *surrogatediagrec(association *assoc, char *dbname,
1280                                           int error, char *addinfo)
1281 {
1282     Z_NamePlusRecord *rec = (Z_NamePlusRecord *)
1283         odr_malloc (assoc->encode, sizeof(*rec));
1284     int *err = odr_intdup(assoc->encode, error);
1285     Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
1286     Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1287         odr_malloc (assoc->encode, sizeof(*dr));
1288     
1289     yaz_log(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
1290     rec->databaseName = dbname;
1291     rec->which = Z_NamePlusRecord_surrogateDiagnostic;
1292     rec->u.surrogateDiagnostic = drec;
1293     drec->which = Z_DiagRec_defaultFormat;
1294     drec->u.defaultFormat = dr;
1295     dr->diagnosticSetId =
1296         yaz_oidval_to_z3950oid (assoc->encode, CLASS_DIAGSET, VAL_BIB1);
1297     dr->condition = err;
1298     set_addinfo (dr, addinfo, assoc->encode);
1299
1300     return rec;
1301 }
1302
1303 /*
1304  * multiple nonsurrogate diagnostics.
1305  */
1306 static Z_DiagRecs *diagrecs(association *assoc, int error, char *addinfo)
1307 {
1308     Z_DiagRecs *recs = (Z_DiagRecs *)odr_malloc (assoc->encode, sizeof(*recs));
1309     int *err = odr_intdup(assoc->encode, error);
1310     Z_DiagRec **recp = (Z_DiagRec **)odr_malloc (assoc->encode, sizeof(*recp));
1311     Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
1312     Z_DefaultDiagFormat *rec = (Z_DefaultDiagFormat *)
1313         odr_malloc (assoc->encode, sizeof(*rec));
1314
1315     yaz_log(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo ? addinfo : "");
1316
1317     recs->num_diagRecs = 1;
1318     recs->diagRecs = recp;
1319     recp[0] = drec;
1320     drec->which = Z_DiagRec_defaultFormat;
1321     drec->u.defaultFormat = rec;
1322
1323     rec->diagnosticSetId =
1324         yaz_oidval_to_z3950oid (assoc->encode, CLASS_DIAGSET, VAL_BIB1);
1325     rec->condition = err;
1326
1327     rec->which = Z_DefaultDiagFormat_v2Addinfo;
1328     rec->u.v2Addinfo = odr_strdup (assoc->encode, addinfo ? addinfo : "");
1329     return recs;
1330 }
1331
1332 static Z_Records *pack_records(association *a, char *setname, int start,
1333                                int *num, Z_RecordComposition *comp,
1334                                int *next, int *pres, oid_value format,
1335                                Z_ReferenceId *referenceId,
1336                                int *oid)
1337 {
1338     int recno, total_length = 0, toget = *num, dumped_records = 0;
1339     Z_Records *records =
1340         (Z_Records *) odr_malloc (a->encode, sizeof(*records));
1341     Z_NamePlusRecordList *reclist =
1342         (Z_NamePlusRecordList *) odr_malloc (a->encode, sizeof(*reclist));
1343     Z_NamePlusRecord **list =
1344         (Z_NamePlusRecord **) odr_malloc (a->encode, sizeof(*list) * toget);
1345
1346     records->which = Z_Records_DBOSD;
1347     records->u.databaseOrSurDiagnostics = reclist;
1348     reclist->num_records = 0;
1349     reclist->records = list;
1350     *pres = Z_PRES_SUCCESS;
1351     *num = 0;
1352     *next = 0;
1353
1354     yaz_log(LOG_LOG, "Request to pack %d+%d+%s", start, toget, setname);
1355     yaz_log(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
1356         a->maximumRecordSize);
1357     for (recno = start; reclist->num_records < toget; recno++)
1358     {
1359         bend_fetch_rr freq;
1360         Z_NamePlusRecord *thisrec;
1361         int this_length = 0;
1362         /*
1363          * we get the number of bytes allocated on the stream before any
1364          * allocation done by the backend - this should give us a reasonable
1365          * idea of the total size of the data so far.
1366          */
1367         total_length = odr_total(a->encode) - dumped_records;
1368         freq.errcode = 0;
1369         freq.errstring = 0;
1370         freq.basename = 0;
1371         freq.len = 0;
1372         freq.record = 0;
1373         freq.last_in_set = 0;
1374         freq.setname = setname;
1375         freq.surrogate_flag = 0;
1376         freq.number = recno;
1377         freq.comp = comp;
1378         freq.request_format = format;
1379         freq.request_format_raw = oid;
1380         freq.output_format = format;
1381         freq.output_format_raw = 0;
1382         freq.stream = a->encode;
1383         freq.print = a->print;
1384         freq.surrogate_flag = 0;
1385         freq.referenceId = referenceId;
1386         (*a->init->bend_fetch)(a->backend, &freq);
1387         /* backend should be able to signal whether error is system-wide
1388            or only pertaining to current record */
1389         if (freq.errcode)
1390         {
1391             if (!freq.surrogate_flag)
1392             {
1393                 char s[20];
1394                 *pres = Z_PRES_FAILURE;
1395                 /* for 'present request out of range',
1396                    set addinfo to record position if not set */
1397                 if (freq.errcode == 13 && freq.errstring == 0)
1398                 {
1399                     sprintf (s, "%d", recno);
1400                     freq.errstring = s;
1401                 }
1402                 return diagrec(a, freq.errcode, freq.errstring);
1403             }
1404             reclist->records[reclist->num_records] =
1405                 surrogatediagrec(a, freq.basename, freq.errcode,
1406                                  freq.errstring);
1407             reclist->num_records++;
1408             *next = freq.last_in_set ? 0 : recno + 1;
1409             continue;
1410         }
1411         if (freq.len >= 0)
1412             this_length = freq.len;
1413         else
1414             this_length = odr_total(a->encode) - total_length;
1415         yaz_log(LOG_DEBUG, "  fetched record, len=%d, total=%d",
1416             this_length, total_length);
1417         if (this_length + total_length > a->preferredMessageSize)
1418         {
1419             /* record is small enough, really */
1420             if (this_length <= a->preferredMessageSize)
1421             {
1422                 yaz_log(LOG_DEBUG, "  Dropped last normal-sized record");
1423                 *pres = Z_PRES_PARTIAL_2;
1424                 break;
1425             }
1426             /* record can only be fetched by itself */
1427             if (this_length < a->maximumRecordSize)
1428             {
1429                 yaz_log(LOG_DEBUG, "  Record > prefmsgsz");
1430                 if (toget > 1)
1431                 {
1432                     yaz_log(LOG_DEBUG, "  Dropped it");
1433                     reclist->records[reclist->num_records] =
1434                          surrogatediagrec(a, freq.basename, 16, 0);
1435                     reclist->num_records++;
1436                     *next = freq.last_in_set ? 0 : recno + 1;
1437                     dumped_records += this_length;
1438                     continue;
1439                 }
1440             }
1441             else /* too big entirely */
1442             {
1443                 yaz_log(LOG_LOG, "Record > maxrcdsz this=%d max=%d", this_length, a->maximumRecordSize);
1444                 reclist->records[reclist->num_records] =
1445                     surrogatediagrec(a, freq.basename, 17, 0);
1446                 reclist->num_records++;
1447                 *next = freq.last_in_set ? 0 : recno + 1;
1448                 dumped_records += this_length;
1449                 continue;
1450             }
1451         }
1452
1453         if (!(thisrec = (Z_NamePlusRecord *)
1454               odr_malloc(a->encode, sizeof(*thisrec))))
1455             return 0;
1456         if (!(thisrec->databaseName = (char *)odr_malloc(a->encode,
1457             strlen(freq.basename) + 1)))
1458             return 0;
1459         strcpy(thisrec->databaseName, freq.basename);
1460         thisrec->which = Z_NamePlusRecord_databaseRecord;
1461
1462         if (freq.output_format_raw)
1463         {
1464             struct oident *ident = oid_getentbyoid(freq.output_format_raw);
1465             freq.output_format = ident->value;
1466         }
1467         thisrec->u.databaseRecord = z_ext_record(a->encode, freq.output_format,
1468                                                  freq.record, freq.len);
1469         if (!thisrec->u.databaseRecord)
1470             return 0;
1471         reclist->records[reclist->num_records] = thisrec;
1472         reclist->num_records++;
1473         *next = freq.last_in_set ? 0 : recno + 1;
1474     }
1475     *num = reclist->num_records;
1476     return records;
1477 }
1478
1479 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
1480     int *fd)
1481 {
1482     Z_SearchRequest *req = reqb->apdu_request->u.searchRequest;
1483     bend_search_rr *bsrr = 
1484         (bend_search_rr *)nmem_malloc (reqb->request_mem, sizeof(*bsrr));
1485     
1486     yaz_log(LOG_LOG, "Got SearchRequest.");
1487     bsrr->fd = fd;
1488     bsrr->request = reqb;
1489     bsrr->association = assoc;
1490     bsrr->referenceId = req->referenceId;
1491     save_referenceId (reqb, bsrr->referenceId);
1492
1493     yaz_log (LOG_LOG, "ResultSet '%s'", req->resultSetName);
1494     if (req->databaseNames)
1495     {
1496         int i;
1497         for (i = 0; i < req->num_databaseNames; i++)
1498             yaz_log (LOG_LOG, "Database '%s'", req->databaseNames[i]);
1499     }
1500     yaz_log_zquery(req->query);
1501
1502     if (assoc->init->bend_search)
1503     {
1504         bsrr->setname = req->resultSetName;
1505         bsrr->replace_set = *req->replaceIndicator;
1506         bsrr->num_bases = req->num_databaseNames;
1507         bsrr->basenames = req->databaseNames;
1508         bsrr->query = req->query;
1509         bsrr->stream = assoc->encode;
1510         bsrr->decode = assoc->decode;
1511         bsrr->print = assoc->print;
1512         bsrr->errcode = 0;
1513         bsrr->hits = 0;
1514         bsrr->errstring = NULL;
1515         bsrr->search_info = NULL;
1516         (assoc->init->bend_search)(assoc->backend, bsrr);
1517         if (!bsrr->request)
1518             return 0;
1519     }
1520     return response_searchRequest(assoc, reqb, bsrr, fd);
1521 }
1522
1523 int bend_searchresponse(void *handle, bend_search_rr *bsrr) {return 0;}
1524
1525 /*
1526  * Prepare a searchresponse based on the backend results. We probably want
1527  * to look at making the fetching of records nonblocking as well, but
1528  * so far, we'll keep things simple.
1529  * If bsrt is null, that means we're called in response to a communications
1530  * event, and we'll have to get the response for ourselves.
1531  */
1532 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
1533     bend_search_rr *bsrt, int *fd)
1534 {
1535     Z_SearchRequest *req = reqb->apdu_request->u.searchRequest;
1536     Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1537     Z_SearchResponse *resp = (Z_SearchResponse *)
1538         odr_malloc (assoc->encode, sizeof(*resp));
1539     int *nulint = odr_intdup (assoc->encode, 0);
1540     bool_t *sr = odr_intdup(assoc->encode, 1);
1541     int *next = odr_intdup(assoc->encode, 0);
1542     int *none = odr_intdup(assoc->encode, Z_RES_NONE);
1543
1544     apdu->which = Z_APDU_searchResponse;
1545     apdu->u.searchResponse = resp;
1546     resp->referenceId = req->referenceId;
1547     resp->additionalSearchInfo = 0;
1548     resp->otherInfo = 0;
1549     *fd = -1;
1550     if (!bsrt && !bend_searchresponse(assoc->backend, bsrt))
1551     {
1552         yaz_log(LOG_FATAL, "Bad result from backend");
1553         return 0;
1554     }
1555     else if (bsrt->errcode)
1556     {
1557         resp->records = diagrec(assoc, bsrt->errcode, bsrt->errstring);
1558         resp->resultCount = nulint;
1559         resp->numberOfRecordsReturned = nulint;
1560         resp->nextResultSetPosition = nulint;
1561         resp->searchStatus = nulint;
1562         resp->resultSetStatus = none;
1563         resp->presentStatus = 0;
1564     }
1565     else
1566     {
1567         int *toget = odr_intdup(assoc->encode, 0);
1568         int *presst = odr_intdup(assoc->encode, 0);
1569         Z_RecordComposition comp, *compp = 0;
1570
1571         yaz_log (LOG_LOG, "resultCount: %d", bsrt->hits);
1572
1573         resp->records = 0;
1574         resp->resultCount = &bsrt->hits;
1575
1576         comp.which = Z_RecordComp_simple;
1577         /* how many records does the user agent want, then? */
1578         if (bsrt->hits <= *req->smallSetUpperBound)
1579         {
1580             *toget = bsrt->hits;
1581             if ((comp.u.simple = req->smallSetElementSetNames))
1582                 compp = &comp;
1583         }
1584         else if (bsrt->hits < *req->largeSetLowerBound)
1585         {
1586             *toget = *req->mediumSetPresentNumber;
1587             if (*toget > bsrt->hits)
1588                 *toget = bsrt->hits;
1589             if ((comp.u.simple = req->mediumSetElementSetNames))
1590                 compp = &comp;
1591         }
1592         else
1593             *toget = 0;
1594
1595         if (*toget && !resp->records)
1596         {
1597             oident *prefformat;
1598             oid_value form;
1599
1600             if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)))
1601                 form = VAL_NONE;
1602             else
1603                 form = prefformat->value;
1604             resp->records = pack_records(assoc, req->resultSetName, 1,
1605                 toget, compp, next, presst, form, req->referenceId,
1606                                          req->preferredRecordSyntax);
1607             if (!resp->records)
1608                 return 0;
1609             resp->numberOfRecordsReturned = toget;
1610             resp->nextResultSetPosition = next;
1611             resp->searchStatus = sr;
1612             resp->resultSetStatus = 0;
1613             resp->presentStatus = presst;
1614         }
1615         else
1616         {
1617             if (*resp->resultCount)
1618                 *next = 1;
1619             resp->numberOfRecordsReturned = nulint;
1620             resp->nextResultSetPosition = next;
1621             resp->searchStatus = sr;
1622             resp->resultSetStatus = 0;
1623             resp->presentStatus = 0;
1624         }
1625     }
1626     resp->additionalSearchInfo = bsrt->search_info;
1627     return apdu;
1628 }
1629
1630 /*
1631  * Maybe we got a little over-friendly when we designed bend_fetch to
1632  * get only one record at a time. Some backends can optimise multiple-record
1633  * fetches, and at any rate, there is some overhead involved in
1634  * all that selecting and hopping around. Problem is, of course, that the
1635  * frontend can't know ahead of time how many records it'll need to
1636  * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
1637  * is downright lousy as a bulk data transfer protocol.
1638  *
1639  * To start with, we'll do the fetching of records from the backend
1640  * in one operation: To save some trips in and out of the event-handler,
1641  * and to simplify the interface to pack_records. At any rate, asynch
1642  * operation is more fun in operations that have an unpredictable execution
1643  * speed - which is normally more true for search than for present.
1644  */
1645 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
1646                                       int *fd)
1647 {
1648     Z_PresentRequest *req = reqb->apdu_request->u.presentRequest;
1649     oident *prefformat;
1650     oid_value form;
1651     Z_APDU *apdu;
1652     Z_PresentResponse *resp;
1653     int *next;
1654     int *num;
1655
1656     yaz_log(LOG_LOG, "Got PresentRequest.");
1657
1658     if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)))
1659         form = VAL_NONE;
1660     else
1661         form = prefformat->value;
1662     resp = (Z_PresentResponse *)odr_malloc (assoc->encode, sizeof(*resp));
1663     resp->records = 0;
1664     resp->presentStatus = odr_intdup(assoc->encode, 0);
1665     if (assoc->init->bend_present)
1666     {
1667         bend_present_rr *bprr = (bend_present_rr *)
1668             nmem_malloc (reqb->request_mem, sizeof(*bprr));
1669         bprr->setname = req->resultSetId;
1670         bprr->start = *req->resultSetStartPoint;
1671         bprr->number = *req->numberOfRecordsRequested;
1672         bprr->format = form;
1673         bprr->comp = req->recordComposition;
1674         bprr->referenceId = req->referenceId;
1675         bprr->stream = assoc->encode;
1676         bprr->print = assoc->print;
1677         bprr->request = reqb;
1678         bprr->association = assoc;
1679         bprr->errcode = 0;
1680         bprr->errstring = NULL;
1681         (*assoc->init->bend_present)(assoc->backend, bprr);
1682         
1683         if (!bprr->request)
1684             return 0;
1685         if (bprr->errcode)
1686         {
1687             resp->records = diagrec(assoc, bprr->errcode, bprr->errstring);
1688             *resp->presentStatus = Z_PRES_FAILURE;
1689         }
1690     }
1691     apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1692     next = odr_intdup(assoc->encode, 0);
1693     num = odr_intdup(assoc->encode, 0);
1694     
1695     apdu->which = Z_APDU_presentResponse;
1696     apdu->u.presentResponse = resp;
1697     resp->referenceId = req->referenceId;
1698     resp->otherInfo = 0;
1699     
1700     if (!resp->records)
1701     {
1702         *num = *req->numberOfRecordsRequested;
1703         resp->records =
1704             pack_records(assoc, req->resultSetId, *req->resultSetStartPoint,
1705                      num, req->recordComposition, next, resp->presentStatus,
1706                          form, req->referenceId, req->preferredRecordSyntax);
1707     }
1708     if (!resp->records)
1709         return 0;
1710     resp->numberOfRecordsReturned = num;
1711     resp->nextResultSetPosition = next;
1712     
1713     return apdu;
1714 }
1715
1716 /*
1717  * Scan was implemented rather in a hurry, and with support for only the basic
1718  * elements of the service in the backend API. Suggestions are welcome.
1719  */
1720 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
1721 {
1722     Z_ScanRequest *req = reqb->apdu_request->u.scanRequest;
1723     Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1724     Z_ScanResponse *res = (Z_ScanResponse *)
1725         odr_malloc (assoc->encode, sizeof(*res));
1726     int *scanStatus = odr_intdup(assoc->encode, Z_Scan_failure);
1727     int *numberOfEntriesReturned = odr_intdup(assoc->encode, 0);
1728     Z_ListEntries *ents = (Z_ListEntries *)
1729         odr_malloc (assoc->encode, sizeof(*ents));
1730     Z_DiagRecs *diagrecs_p = NULL;
1731     oident *attset;
1732     bend_scan_rr *bsrr = (bend_scan_rr *)
1733         odr_malloc (assoc->encode, sizeof(*bsrr));
1734
1735     yaz_log(LOG_LOG, "Got ScanRequest");
1736
1737     apdu->which = Z_APDU_scanResponse;
1738     apdu->u.scanResponse = res;
1739     res->referenceId = req->referenceId;
1740
1741     /* if step is absent, set it to 0 */
1742     res->stepSize = odr_intdup(assoc->encode, 0);
1743     if (req->stepSize)
1744         *res->stepSize = *req->stepSize;
1745
1746     res->scanStatus = scanStatus;
1747     res->numberOfEntriesReturned = numberOfEntriesReturned;
1748     res->positionOfTerm = 0;
1749     res->entries = ents;
1750     ents->num_entries = 0;
1751     ents->entries = NULL;
1752     ents->num_nonsurrogateDiagnostics = 0;
1753     ents->nonsurrogateDiagnostics = NULL;
1754     res->attributeSet = 0;
1755     res->otherInfo = 0;
1756
1757     if (req->databaseNames)
1758     {
1759         int i;
1760         for (i = 0; i < req->num_databaseNames; i++)
1761             yaz_log (LOG_LOG, "Database '%s'", req->databaseNames[i]);
1762     }
1763     bsrr->num_bases = req->num_databaseNames;
1764     bsrr->basenames = req->databaseNames;
1765     bsrr->num_entries = *req->numberOfTermsRequested;
1766     bsrr->term = req->termListAndStartPoint;
1767     bsrr->referenceId = req->referenceId;
1768     bsrr->stream = assoc->encode;
1769     bsrr->print = assoc->print;
1770     bsrr->step_size = res->stepSize;
1771     if (req->attributeSet &&
1772         (attset = oid_getentbyoid(req->attributeSet)) &&
1773         (attset->oclass == CLASS_ATTSET || attset->oclass == CLASS_GENERAL))
1774         bsrr->attributeset = attset->value;
1775     else
1776         bsrr->attributeset = VAL_NONE;
1777     log_scan_term (req->termListAndStartPoint, bsrr->attributeset);
1778     bsrr->term_position = req->preferredPositionInResponse ?
1779         *req->preferredPositionInResponse : 1;
1780     ((int (*)(void *, bend_scan_rr *))
1781      (*assoc->init->bend_scan))(assoc->backend, bsrr);
1782     if (bsrr->errcode)
1783         diagrecs_p = diagrecs(assoc, bsrr->errcode, bsrr->errstring);
1784     else
1785     {
1786         int i;
1787         Z_Entry **tab = (Z_Entry **)
1788             odr_malloc (assoc->encode, sizeof(*tab) * bsrr->num_entries);
1789         
1790         if (bsrr->status == BEND_SCAN_PARTIAL)
1791             *scanStatus = Z_Scan_partial_5;
1792         else
1793             *scanStatus = Z_Scan_success;
1794         ents->entries = tab;
1795         ents->num_entries = bsrr->num_entries;
1796         res->numberOfEntriesReturned = &ents->num_entries;          
1797         res->positionOfTerm = &bsrr->term_position;
1798         for (i = 0; i < bsrr->num_entries; i++)
1799         {
1800             Z_Entry *e;
1801             Z_TermInfo *t;
1802             Odr_oct *o;
1803             
1804             tab[i] = e = (Z_Entry *)odr_malloc(assoc->encode, sizeof(*e));
1805             if (bsrr->entries[i].occurrences >= 0)
1806             {
1807                 e->which = Z_Entry_termInfo;
1808                 e->u.termInfo = t = (Z_TermInfo *)
1809                     odr_malloc(assoc->encode, sizeof(*t));
1810                 t->suggestedAttributes = 0;
1811                 t->displayTerm = 0;
1812                 t->alternativeTerm = 0;
1813                 t->byAttributes = 0;
1814                 t->otherTermInfo = 0;
1815                 t->globalOccurrences = &bsrr->entries[i].occurrences;
1816                 t->term = (Z_Term *)
1817                     odr_malloc(assoc->encode, sizeof(*t->term));
1818                 t->term->which = Z_Term_general;
1819                 t->term->u.general = o =
1820                     (Odr_oct *)odr_malloc(assoc->encode, sizeof(Odr_oct));
1821                 o->buf = (unsigned char *)
1822                     odr_malloc(assoc->encode, o->len = o->size =
1823                                strlen(bsrr->entries[i].term));
1824                 memcpy(o->buf, bsrr->entries[i].term, o->len);
1825                 yaz_log(LOG_DEBUG, "  term #%d: '%s' (%d)", i,
1826                          bsrr->entries[i].term, bsrr->entries[i].occurrences);
1827             }
1828             else
1829             {
1830                 Z_DiagRecs *drecs = diagrecs (assoc,
1831                                               bsrr->entries[i].errcode,
1832                                               bsrr->entries[i].errstring);
1833                 assert (drecs->num_diagRecs == 1);
1834                 e->which = Z_Entry_surrogateDiagnostic;
1835                 assert (drecs->diagRecs[0]);
1836                 e->u.surrogateDiagnostic = drecs->diagRecs[0];
1837             }
1838         }
1839     }
1840     if (diagrecs_p)
1841     {
1842         ents->num_nonsurrogateDiagnostics = diagrecs_p->num_diagRecs;
1843         ents->nonsurrogateDiagnostics = diagrecs_p->diagRecs;
1844     }
1845     return apdu;
1846 }
1847
1848 static Z_APDU *process_sortRequest(association *assoc, request *reqb,
1849     int *fd)
1850 {
1851     Z_SortRequest *req = reqb->apdu_request->u.sortRequest;
1852     Z_SortResponse *res = (Z_SortResponse *)
1853         odr_malloc (assoc->encode, sizeof(*res));
1854     bend_sort_rr *bsrr = (bend_sort_rr *)
1855         odr_malloc (assoc->encode, sizeof(*bsrr));
1856
1857     Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1858
1859     yaz_log(LOG_LOG, "Got SortRequest.");
1860
1861     bsrr->num_input_setnames = req->num_inputResultSetNames;
1862     bsrr->input_setnames = req->inputResultSetNames;
1863     bsrr->referenceId = req->referenceId;
1864     bsrr->output_setname = req->sortedResultSetName;
1865     bsrr->sort_sequence = req->sortSequence;
1866     bsrr->stream = assoc->encode;
1867     bsrr->print = assoc->print;
1868
1869     bsrr->sort_status = Z_SortStatus_failure;
1870     bsrr->errcode = 0;
1871     bsrr->errstring = 0;
1872     
1873     (*assoc->init->bend_sort)(assoc->backend, bsrr);
1874     
1875     res->referenceId = bsrr->referenceId;
1876     res->sortStatus = odr_intdup(assoc->encode, bsrr->sort_status);
1877     res->resultSetStatus = 0;
1878     if (bsrr->errcode)
1879     {
1880         Z_DiagRecs *dr = diagrecs (assoc, bsrr->errcode, bsrr->errstring);
1881         res->diagnostics = dr->diagRecs;
1882         res->num_diagnostics = dr->num_diagRecs;
1883     }
1884     else
1885     {
1886         res->num_diagnostics = 0;
1887         res->diagnostics = 0;
1888     }
1889     res->otherInfo = 0;
1890
1891     apdu->which = Z_APDU_sortResponse;
1892     apdu->u.sortResponse = res;
1893     return apdu;
1894 }
1895
1896 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
1897     int *fd)
1898 {
1899     Z_DeleteResultSetRequest *req =
1900         reqb->apdu_request->u.deleteResultSetRequest;
1901     Z_DeleteResultSetResponse *res = (Z_DeleteResultSetResponse *)
1902         odr_malloc (assoc->encode, sizeof(*res));
1903     bend_delete_rr *bdrr = (bend_delete_rr *)
1904         odr_malloc (assoc->encode, sizeof(*bdrr));
1905     Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1906
1907     yaz_log(LOG_LOG, "Got DeleteRequest.");
1908
1909     bdrr->num_setnames = req->num_resultSetList;
1910     bdrr->setnames = req->resultSetList;
1911     bdrr->stream = assoc->encode;
1912     bdrr->print = assoc->print;
1913     bdrr->function = *req->deleteFunction;
1914     bdrr->referenceId = req->referenceId;
1915     bdrr->statuses = 0;
1916     if (bdrr->num_setnames > 0)
1917     {
1918         int i;
1919         bdrr->statuses = (int*) 
1920             odr_malloc(assoc->encode, sizeof(*bdrr->statuses) *
1921                        bdrr->num_setnames);
1922         for (i = 0; i < bdrr->num_setnames; i++)
1923             bdrr->statuses[i] = 0;
1924     }
1925     (*assoc->init->bend_delete)(assoc->backend, bdrr);
1926     
1927     res->referenceId = req->referenceId;
1928
1929     res->deleteOperationStatus = odr_intdup(assoc->encode,bdrr->delete_status);
1930
1931     res->deleteListStatuses = 0;
1932     if (bdrr->num_setnames > 0)
1933     {
1934         int i;
1935         res->deleteListStatuses = (Z_ListStatuses *)
1936             odr_malloc(assoc->encode, sizeof(*res->deleteListStatuses));
1937         res->deleteListStatuses->num = bdrr->num_setnames;
1938         res->deleteListStatuses->elements =
1939             (Z_ListStatus **)
1940             odr_malloc (assoc->encode, 
1941                         sizeof(*res->deleteListStatuses->elements) *
1942                         bdrr->num_setnames);
1943         for (i = 0; i<bdrr->num_setnames; i++)
1944         {
1945             res->deleteListStatuses->elements[i] =
1946                 (Z_ListStatus *)
1947                 odr_malloc (assoc->encode,
1948                             sizeof(**res->deleteListStatuses->elements));
1949             res->deleteListStatuses->elements[i]->status = bdrr->statuses+i;
1950             res->deleteListStatuses->elements[i]->id =
1951                 odr_strdup (assoc->encode, bdrr->setnames[i]);
1952             
1953         }
1954     }
1955     res->numberNotDeleted = 0;
1956     res->bulkStatuses = 0;
1957     res->deleteMessage = 0;
1958     res->otherInfo = 0;
1959
1960     apdu->which = Z_APDU_deleteResultSetResponse;
1961     apdu->u.deleteResultSetResponse = res;
1962     return apdu;
1963 }
1964
1965 static void process_close(association *assoc, request *reqb)
1966 {
1967     Z_Close *req = reqb->apdu_request->u.close;
1968     static char *reasons[] =
1969     {
1970         "finished",
1971         "shutdown",
1972         "systemProblem",
1973         "costLimit",
1974         "resources",
1975         "securityViolation",
1976         "protocolError",
1977         "lackOfActivity",
1978         "peerAbort",
1979         "unspecified"
1980     };
1981
1982     yaz_log(LOG_LOG, "Got Close, reason %s, message %s",
1983         reasons[*req->closeReason], req->diagnosticInformation ?
1984         req->diagnosticInformation : "NULL");
1985     if (assoc->version < 3) /* to make do_force respond with close */
1986         assoc->version = 3;
1987     do_close_req(assoc, Z_Close_finished,
1988                  "Association terminated by client", reqb);
1989 }
1990
1991 void save_referenceId (request *reqb, Z_ReferenceId *refid)
1992 {
1993     if (refid)
1994     {
1995         reqb->len_refid = refid->len;
1996         reqb->refid = (char *)nmem_malloc (reqb->request_mem, refid->len);
1997         memcpy (reqb->refid, refid->buf, refid->len);
1998     }
1999     else
2000     {
2001         reqb->len_refid = 0;
2002         reqb->refid = NULL;
2003     }
2004 }
2005
2006 void bend_request_send (bend_association a, bend_request req, Z_APDU *res)
2007 {
2008     process_z_response (a, req, res);
2009 }
2010
2011 bend_request bend_request_mk (bend_association a)
2012 {
2013     request *nreq = request_get (&a->outgoing);
2014     nreq->request_mem = nmem_create ();
2015     return nreq;
2016 }
2017
2018 Z_ReferenceId *bend_request_getid (ODR odr, bend_request req)
2019 {
2020     Z_ReferenceId *id;
2021     if (!req->refid)
2022         return 0;
2023     id = (Odr_oct *)odr_malloc (odr, sizeof(*odr));
2024     id->buf = (unsigned char *)odr_malloc (odr, req->len_refid);
2025     id->len = id->size = req->len_refid;
2026     memcpy (id->buf, req->refid, req->len_refid);
2027     return id;
2028 }
2029
2030 void bend_request_destroy (bend_request *req)
2031 {
2032     nmem_destroy((*req)->request_mem);
2033     request_release(*req);
2034     *req = NULL;
2035 }
2036
2037 int bend_backend_respond (bend_association a, bend_request req)
2038 {
2039     char *msg;
2040     int r;
2041     r = process_z_request (a, req, &msg);
2042     if (r < 0)
2043         yaz_log (LOG_WARN, "%s", msg);
2044     return r;
2045 }
2046
2047 void bend_request_setdata(bend_request r, void *p)
2048 {
2049     r->clientData = p;
2050 }
2051
2052 void *bend_request_getdata(bend_request r)
2053 {
2054     return r->clientData;
2055 }
2056
2057 static Z_APDU *process_segmentRequest (association *assoc, request *reqb)
2058 {
2059     bend_segment_rr req;
2060
2061     req.segment = reqb->apdu_request->u.segmentRequest;
2062     req.stream = assoc->encode;
2063     req.decode = assoc->decode;
2064     req.print = assoc->print;
2065     req.association = assoc;
2066     
2067     (*assoc->init->bend_segment)(assoc->backend, &req);
2068
2069     return 0;
2070 }
2071
2072 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd)
2073 {
2074     bend_esrequest_rr esrequest;
2075
2076     Z_ExtendedServicesRequest *req =
2077         reqb->apdu_request->u.extendedServicesRequest;
2078     Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_extendedServicesResponse);
2079
2080     Z_ExtendedServicesResponse *resp = apdu->u.extendedServicesResponse;
2081
2082     yaz_log(LOG_DEBUG,"inside Process esRequest");
2083
2084     esrequest.esr = reqb->apdu_request->u.extendedServicesRequest;
2085     esrequest.stream = assoc->encode;
2086     esrequest.decode = assoc->decode;
2087     esrequest.print = assoc->print;
2088     esrequest.errcode = 0;
2089     esrequest.errstring = NULL;
2090     esrequest.request = reqb;
2091     esrequest.association = assoc;
2092     esrequest.taskPackage = 0;
2093     esrequest.referenceId = req->referenceId;
2094     
2095     (*assoc->init->bend_esrequest)(assoc->backend, &esrequest);
2096     
2097     /* If the response is being delayed, return NULL */
2098     if (esrequest.request == NULL)
2099         return(NULL);
2100
2101     resp->referenceId = req->referenceId;
2102
2103     if (esrequest.errcode == -1)
2104     {
2105         /* Backend service indicates request will be processed */
2106         yaz_log(LOG_DEBUG,"Request could be processed...Accepted !");
2107         *resp->operationStatus = Z_ExtendedServicesResponse_accepted;
2108     }
2109     else if (esrequest.errcode == 0)
2110     {
2111         /* Backend service indicates request will be processed */
2112         yaz_log(LOG_DEBUG,"Request could be processed...Done !");
2113         *resp->operationStatus = Z_ExtendedServicesResponse_done;
2114     }
2115     else
2116     {
2117         Z_DiagRecs *diagRecs = diagrecs (assoc, esrequest.errcode,
2118                                          esrequest.errstring);
2119
2120         /* Backend indicates error, request will not be processed */
2121         yaz_log(LOG_DEBUG,"Request could not be processed...failure !");
2122         *resp->operationStatus = Z_ExtendedServicesResponse_failure;
2123         resp->num_diagnostics = diagRecs->num_diagRecs;
2124         resp->diagnostics = diagRecs->diagRecs;
2125     }
2126     /* Do something with the members of bend_extendedservice */
2127     if (esrequest.taskPackage)
2128         resp->taskPackage = z_ext_record (assoc->encode, VAL_EXTENDED,
2129                                          (const char *)  esrequest.taskPackage,
2130                                           -1);
2131     yaz_log(LOG_DEBUG,"Send the result apdu");
2132     return apdu;
2133 }
2134