2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: seshigh.c,v 1.149 2003-03-18 13:34:36 adam Exp $
9 * Frontend server logic.
11 * This code receives incoming APDUs, and handles client requests by means
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
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.
31 #include <sys/types.h>
34 #define S_ISREG(x) (x & _S_IFREG)
44 #include <yaz/yconfig.h>
45 #include <yaz/xmalloc.h>
46 #include <yaz/comstack.h>
49 #include <yaz/proto.h>
52 #include <yaz/logrpn.h>
53 #include <yaz/statserv.h>
54 #include <yaz/diagbib1.h>
55 #include <yaz/charneg.h>
56 #include <yaz/otherinfo.h>
57 #include <yaz/yaz-util.h>
58 #include <yaz/pquery.h>
61 #include <yaz/backend.h>
63 static void process_gdu_request(association *assoc, request *req);
64 static int process_z_request(association *assoc, request *req, char **msg);
65 void backend_response(IOCHAN i, int event);
66 static int process_gdu_response(association *assoc, request *req, Z_GDU *res);
67 static int process_z_response(association *assoc, request *req, Z_APDU *res);
68 static Z_APDU *process_initRequest(association *assoc, request *reqb);
69 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
71 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
72 bend_search_rr *bsrr, int *fd);
73 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
75 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
76 static Z_APDU *process_sortRequest(association *assoc, request *reqb, int *fd);
77 static void process_close(association *assoc, request *reqb);
78 void save_referenceId (request *reqb, Z_ReferenceId *refid);
79 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
81 static Z_APDU *process_segmentRequest (association *assoc, request *reqb);
83 static FILE *apduf = 0; /* for use in static mode */
84 static statserv_options_block *control_block = 0;
86 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd);
89 * Create and initialize a new association-handle.
90 * channel : iochannel for the current line.
91 * link : communications channel.
92 * Returns: 0 or a new association handle.
94 association *create_association(IOCHAN channel, COMSTACK link)
99 control_block = statserv_getcontrol();
100 if (!(anew = (association *)xmalloc(sizeof(*anew))))
104 anew->client_chan = channel;
105 anew->client_link = link;
106 anew->cs_get_mask = 0;
107 anew->cs_put_mask = 0;
108 anew->cs_accept_mask = 0;
109 if (!(anew->decode = odr_createmem(ODR_DECODE)) ||
110 !(anew->encode = odr_createmem(ODR_ENCODE)))
112 if (*control_block->apdufile)
117 strcpy(filename, control_block->apdufile);
118 if (!(anew->print = odr_createmem(ODR_PRINT)))
120 if (*control_block->apdufile == '@')
122 odr_setprint(anew->print, yaz_log_file());
124 else if (*control_block->apdufile != '-')
126 strcpy(filename, control_block->apdufile);
127 if (!control_block->dynamic)
131 if (!(apduf = fopen(filename, "w")))
133 yaz_log(LOG_WARN|LOG_ERRNO, "%s", filename);
136 setvbuf(apduf, 0, _IONBF, 0);
142 sprintf(filename + strlen(filename), ".%d", getpid());
143 if (!(f = fopen(filename, "w")))
145 yaz_log(LOG_WARN|LOG_ERRNO, "%s", filename);
148 setvbuf(f, 0, _IONBF, 0);
150 odr_setprint(anew->print, f);
155 anew->input_buffer = 0;
156 anew->input_buffer_len = 0;
158 anew->state = ASSOC_NEW;
159 request_initq(&anew->incoming);
160 request_initq(&anew->outgoing);
161 anew->proto = cs_getproto(link);
166 * Free association and release resources.
168 void destroy_association(association *h)
170 statserv_options_block *cb = statserv_getcontrol();
173 odr_destroy(h->decode);
174 odr_destroy(h->encode);
176 odr_destroy(h->print);
178 xfree(h->input_buffer);
180 (*cb->bend_close)(h->backend);
181 while (request_deq(&h->incoming));
182 while (request_deq(&h->outgoing));
183 request_delq(&h->incoming);
184 request_delq(&h->outgoing);
186 xmalloc_trav("session closed");
187 if (control_block && control_block->one_shot)
193 static void do_close_req(association *a, int reason, char *message,
197 Z_Close *cls = zget_Close(a->encode);
199 /* Purge request queue */
200 while (request_deq(&a->incoming));
201 while (request_deq(&a->outgoing));
204 yaz_log(LOG_LOG, "Sending Close PDU, reason=%d, message=%s",
205 reason, message ? message : "none");
206 apdu.which = Z_APDU_close;
208 *cls->closeReason = reason;
209 cls->diagnosticInformation = message;
210 process_z_response(a, req, &apdu);
211 iochan_settimeout(a->client_chan, 20);
215 yaz_log(LOG_DEBUG, "v2 client. No Close PDU");
216 iochan_setevent(a->client_chan, EVENT_TIMEOUT); /* force imm close */
218 a->state = ASSOC_DEAD;
221 static void do_close(association *a, int reason, char *message)
223 do_close_req (a, reason, message, request_get(&a->outgoing));
227 * This is where PDUs from the client are read and the further
228 * processing is initiated. Flow of control moves down through the
229 * various process_* functions below, until the encoded result comes back up
230 * to the output handler in here.
232 * h : the I/O channel that has an outstanding event.
233 * event : the current outstanding event.
235 void ir_session(IOCHAN h, int event)
238 association *assoc = (association *)iochan_getdata(h);
239 COMSTACK conn = assoc->client_link;
242 assert(h && conn && assoc);
243 if (event == EVENT_TIMEOUT)
245 if (assoc->state != ASSOC_UP)
247 yaz_log(LOG_LOG, "Final timeout - closing connection.");
249 destroy_association(assoc);
254 yaz_log(LOG_LOG, "Session idle too long. Sending close.");
255 do_close(assoc, Z_Close_lackOfActivity, 0);
259 if (event & assoc->cs_accept_mask)
261 yaz_log (LOG_DEBUG, "ir_session (accept)");
262 if (!cs_accept (conn))
264 yaz_log (LOG_LOG, "accept failed");
265 destroy_association(assoc);
268 iochan_clearflag (h, EVENT_OUTPUT|EVENT_OUTPUT);
269 if (conn->io_pending)
270 { /* cs_accept didn't complete */
271 assoc->cs_accept_mask =
272 ((conn->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
273 ((conn->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
275 iochan_setflag (h, assoc->cs_accept_mask);
278 { /* cs_accept completed. Prepare for reading (cs_get) */
279 assoc->cs_accept_mask = 0;
280 assoc->cs_get_mask = EVENT_INPUT;
281 iochan_setflag (h, assoc->cs_get_mask);
285 if ((event & assoc->cs_get_mask) || (event & EVENT_WORK)) /* input */
287 if ((assoc->cs_put_mask & EVENT_INPUT) == 0 && (event & assoc->cs_get_mask))
289 yaz_log(LOG_DEBUG, "ir_session (input)");
290 /* We aren't speaking to this fellow */
291 if (assoc->state == ASSOC_DEAD)
293 yaz_log(LOG_LOG, "Connection closed - end of session");
295 destroy_association(assoc);
299 assoc->cs_get_mask = EVENT_INPUT;
300 if ((res = cs_get(conn, &assoc->input_buffer,
301 &assoc->input_buffer_len)) <= 0)
303 yaz_log(LOG_LOG, "Connection closed by client");
305 destroy_association(assoc);
309 else if (res == 1) /* incomplete read - wait for more */
311 if (conn->io_pending & CS_WANT_WRITE)
312 assoc->cs_get_mask |= EVENT_OUTPUT;
313 iochan_setflag(h, assoc->cs_get_mask);
316 if (cs_more(conn)) /* more stuff - call us again later, please */
317 iochan_setevent(h, EVENT_INPUT);
319 /* we got a complete PDU. Let's decode it */
320 yaz_log(LOG_DEBUG, "Got PDU, %d bytes: lead=%02X %02X %02X", res,
321 assoc->input_buffer[0] & 0xff,
322 assoc->input_buffer[1] & 0xff,
323 assoc->input_buffer[2] & 0xff);
324 req = request_get(&assoc->incoming); /* get a new request */
325 odr_reset(assoc->decode);
326 odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
327 if (!z_GDU(assoc->decode, &req->gdu_request, 0, 0))
329 yaz_log(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
330 odr_errmsg(odr_geterror(assoc->decode)),
331 odr_offset(assoc->decode));
332 if (assoc->decode->error != OHTTP)
334 yaz_log(LOG_LOG, "PDU dump:");
335 odr_dumpBER(yaz_log_file(), assoc->input_buffer, res);
336 do_close(assoc, Z_Close_protocolError, "Malformed package");
340 Z_GDU *p = z_get_HTTP_Response(assoc->encode, 400);
341 assoc->state = ASSOC_DEAD;
342 process_gdu_response(assoc, req, p);
346 req->request_mem = odr_extract_mem(assoc->decode);
347 if (assoc->print && !z_GDU(assoc->print, &req->gdu_request, 0, 0))
349 yaz_log(LOG_WARN, "ODR print error: %s",
350 odr_errmsg(odr_geterror(assoc->print)));
351 odr_reset(assoc->print);
353 request_enq(&assoc->incoming, req);
356 /* can we do something yet? */
357 req = request_head(&assoc->incoming);
358 if (req->state == REQUEST_IDLE)
360 request_deq(&assoc->incoming);
361 process_gdu_request(assoc, req);
364 if (event & assoc->cs_put_mask)
366 request *req = request_head(&assoc->outgoing);
368 assoc->cs_put_mask = 0;
369 yaz_log(LOG_DEBUG, "ir_session (output)");
370 req->state = REQUEST_PENDING;
371 switch (res = cs_put(conn, req->response, req->len_response))
374 yaz_log(LOG_LOG, "Connection closed by client");
376 destroy_association(assoc);
379 case 0: /* all sent - release the request structure */
380 yaz_log(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
382 yaz_log(LOG_DEBUG, "HTTP out:\n%.*s", req->len_response,
385 nmem_destroy(req->request_mem);
386 request_deq(&assoc->outgoing);
387 request_release(req);
388 if (!request_head(&assoc->outgoing))
389 { /* restore mask for cs_get operation ... */
390 iochan_clearflag(h, EVENT_OUTPUT|EVENT_INPUT);
391 iochan_setflag(h, assoc->cs_get_mask);
392 if (assoc->state == ASSOC_DEAD)
393 iochan_setevent(assoc->client_chan, EVENT_TIMEOUT);
397 assoc->cs_put_mask = EVENT_OUTPUT;
401 if (conn->io_pending & CS_WANT_WRITE)
402 assoc->cs_put_mask |= EVENT_OUTPUT;
403 if (conn->io_pending & CS_WANT_READ)
404 assoc->cs_put_mask |= EVENT_INPUT;
405 iochan_setflag(h, assoc->cs_put_mask);
408 if (event & EVENT_EXCEPT)
410 yaz_log(LOG_LOG, "ir_session (exception)");
412 destroy_association(assoc);
417 static int process_z_request(association *assoc, request *req, char **msg);
419 static void assoc_init_reset(association *assoc)
422 assoc->init = (bend_initrequest *) xmalloc (sizeof(*assoc->init));
424 assoc->init->stream = assoc->encode;
425 assoc->init->print = assoc->print;
426 assoc->init->auth = 0;
427 assoc->init->referenceId = 0;
428 assoc->init->implementation_version = 0;
429 assoc->init->implementation_id = 0;
430 assoc->init->implementation_name = 0;
431 assoc->init->bend_sort = NULL;
432 assoc->init->bend_search = NULL;
433 assoc->init->bend_present = NULL;
434 assoc->init->bend_esrequest = NULL;
435 assoc->init->bend_delete = NULL;
436 assoc->init->bend_scan = NULL;
437 assoc->init->bend_segment = NULL;
438 assoc->init->bend_fetch = NULL;
439 assoc->init->charneg_request = NULL;
440 assoc->init->charneg_response = NULL;
441 assoc->init->decode = assoc->decode;
442 assoc->init->peer_name =
443 odr_strdup (assoc->encode, cs_addrstr(assoc->client_link));
446 static int srw_bend_init(association *assoc)
448 const char *encoding = "UTF-8";
450 bend_initresult *binitres;
451 statserv_options_block *cb = statserv_getcontrol();
453 assoc_init_reset(assoc);
455 assoc->maximumRecordSize = 3000000;
456 assoc->preferredMessageSize = 3000000;
458 ce = yaz_set_proposal_charneg(assoc->decode, &encoding, 1, 0, 0, 1);
459 assoc->init->charneg_request = ce->u.charNeg3;
461 if (!(binitres = (*cb->bend_init)(assoc->init)))
463 yaz_log(LOG_WARN, "Bad response from backend.");
466 assoc->backend = binitres->handle;
470 static int srw_bend_fetch(association *assoc, int pos,
471 Z_SRW_searchRetrieveRequest *srw_req,
472 Z_SRW_record *record)
475 ODR o = assoc->encode;
477 rr.setname = "default";
480 rr.request_format = VAL_TEXT_XML;
481 rr.request_format_raw = yaz_oidval_to_z3950oid(assoc->decode,
484 rr.comp = (Z_RecordComposition *)
485 odr_malloc(assoc->decode, sizeof(*rr.comp));
486 rr.comp->which = Z_RecordComp_complex;
487 rr.comp->u.complex = (Z_CompSpec *)
488 odr_malloc(assoc->decode, sizeof(Z_CompSpec));
489 rr.comp->u.complex->selectAlternativeSyntax = (bool_t *)
490 odr_malloc(assoc->encode, sizeof(bool_t));
491 *rr.comp->u.complex->selectAlternativeSyntax = 0;
492 rr.comp->u.complex->num_dbSpecific = 0;
493 rr.comp->u.complex->dbSpecific = 0;
494 rr.comp->u.complex->num_recordSyntax = 0;
495 rr.comp->u.complex->recordSyntax = 0;
497 rr.comp->u.complex->generic = (Z_Specification *)
498 odr_malloc(assoc->decode, sizeof(Z_Specification));
499 rr.comp->u.complex->generic->which = Z_Schema_uri;
500 rr.comp->u.complex->generic->schema.uri = srw_req->recordSchema;
501 rr.comp->u.complex->generic->elementSpec = 0;
503 rr.stream = assoc->encode;
504 rr.print = assoc->print;
510 rr.output_format = VAL_TEXT_XML;
511 rr.output_format_raw = 0;
514 rr.surrogate_flag = 0;
516 if (!assoc->init->bend_fetch)
519 (*assoc->init->bend_fetch)(assoc->backend, &rr);
523 record->recordData_buf = rr.record;
524 record->recordData_len = rr.len;
525 record->recordPosition = odr_intdup(o, pos);
526 record->recordSchema = 0;
527 if (srw_req->recordSchema)
528 record->recordSchema = odr_strdup(o, srw_req->recordSchema);
533 static void srw_bend_search(association *assoc, request *req,
534 Z_SRW_searchRetrieveRequest *srw_req,
535 Z_SRW_searchRetrieveResponse *srw_res)
541 yaz_log(LOG_LOG, "Got SRW SearchRetrieveRequest");
542 yaz_log(LOG_DEBUG, "srw_bend_search");
545 yaz_log(LOG_DEBUG, "srw_bend_init");
546 if (!srw_bend_init(assoc))
548 srw_error = 3; /* assume Authentication error */
550 srw_res->num_diagnostics = 1;
551 srw_res->diagnostics = (Z_SRW_diagnostic *)
552 odr_malloc(assoc->encode, sizeof(*srw_res->diagnostics));
553 srw_res->diagnostics[0].code =
554 odr_intdup(assoc->encode, srw_error);
555 srw_res->diagnostics[0].details = 0;
560 rr.setname = "default";
563 rr.basenames = &srw_req->database;
566 rr.query = (Z_Query *) odr_malloc (assoc->decode, sizeof(*rr.query));
568 if (srw_req->query_type == Z_SRW_query_type_cql)
570 ext = (Z_External *) odr_malloc(assoc->decode, sizeof(*ext));
571 ext->direct_reference = odr_getoidbystr(assoc->decode,
572 "1.2.840.10003.16.2");
573 ext->indirect_reference = 0;
575 ext->which = Z_External_CQL;
576 ext->u.cql = srw_req->query.cql;
578 rr.query->which = Z_Query_type_104;
579 rr.query->u.type_104 = ext;
581 else if (srw_req->query_type == Z_SRW_query_type_pqf)
583 Z_RPNQuery *RPNquery;
584 YAZ_PQF_Parser pqf_parser;
586 pqf_parser = yaz_pqf_create ();
588 RPNquery = yaz_pqf_parse (pqf_parser, assoc->decode,
594 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
595 yaz_log(LOG_LOG, "%*s^\n", off+4, "");
596 yaz_log(LOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
601 rr.query->which = Z_Query_type_1;
602 rr.query->u.type_1 = RPNquery;
604 yaz_pqf_destroy (pqf_parser);
609 if (!srw_error && srw_req->sort_type != Z_SRW_sort_type_none)
612 if (!srw_error && !assoc->init->bend_search)
617 yaz_log(LOG_DEBUG, "srw_bend_search returned SRW error %d", srw_error);
618 srw_res->num_diagnostics = 1;
619 srw_res->diagnostics = (Z_SRW_diagnostic *)
620 odr_malloc(assoc->encode, sizeof(*srw_res->diagnostics));
621 srw_res->diagnostics[0].code =
622 odr_intdup(assoc->encode, srw_error);
623 srw_res->diagnostics[0].details = 0;
627 rr.stream = assoc->encode;
628 rr.decode = assoc->decode;
629 rr.print = assoc->print;
631 rr.association = assoc;
637 yaz_log_zquery(rr.query);
638 (assoc->init->bend_search)(assoc->backend, &rr);
639 srw_res->numberOfRecords = odr_intdup(assoc->encode, rr.hits);
642 yaz_log(LOG_DEBUG, "bend_search returned Bib-1 code %d", rr.errcode);
643 srw_res->num_diagnostics = 1;
644 srw_res->diagnostics = (Z_SRW_diagnostic *)
645 odr_malloc(assoc->encode, sizeof(*srw_res->diagnostics));
646 srw_res->diagnostics[0].code =
647 odr_intdup(assoc->encode,
648 yaz_diag_bib1_to_srw (rr.errcode));
649 srw_res->diagnostics[0].details = rr.errstring;
650 yaz_log(LOG_DEBUG, "srw_bend_search returned SRW error %d",
651 srw_res->diagnostics[0].code);
656 srw_res->numberOfRecords = odr_intdup(assoc->encode, rr.hits);
657 if (srw_req->maximumRecords && *srw_req->maximumRecords > 0)
659 int number = *srw_req->maximumRecords;
663 if (srw_req->startRecord)
664 start = *srw_req->startRecord;
666 yaz_log(LOG_DEBUG, "srw_bend_search. start=%d max=%d",
667 start, *srw_req->maximumRecords);
669 if (start <= rr.hits)
672 if (start + number > rr.hits)
673 number = rr.hits - start + 1;
674 srw_res->records = (Z_SRW_record *)
675 odr_malloc(assoc->encode,
676 number * sizeof(*srw_res->records));
677 for (i = 0; i<number; i++)
680 srw_res->records[j].recordData_buf = 0;
681 yaz_log(LOG_DEBUG, "srw_bend_fetch %d", i+start);
682 errcode = srw_bend_fetch(assoc, i+start, srw_req,
683 srw_res->records + j);
686 srw_res->num_diagnostics = 1;
687 srw_res->diagnostics = (Z_SRW_diagnostic *)
688 odr_malloc(assoc->encode,
689 sizeof(*srw_res->diagnostics));
690 srw_res->diagnostics[0].code =
691 odr_intdup(assoc->encode,
692 yaz_diag_bib1_to_srw (errcode));
693 srw_res->diagnostics[0].details = rr.errstring;
696 if (srw_res->records[j].recordData_buf)
699 srw_res->num_records = j;
701 srw_res->records = 0;
707 static int hex_digit (int ch)
709 if (ch >= '0' && ch <= '9')
711 else if (ch >= 'a' && ch <= 'f')
713 else if (ch >= 'A' && ch <= 'F')
718 static char *uri_val(const char *path, const char *name, ODR o)
720 size_t nlen = strlen(name);
724 while (path && *path)
726 const char *p1 = strchr(path, '=');
729 if (p1 - path == nlen && !memcmp(path, name, nlen))
735 p1 = strchr(path, '&');
737 p1 = strlen(path) + path;
738 ret = odr_malloc(o, p1 - path + 1);
739 while (*path && *path != '&')
746 else if (*path == '%' && path[1] && path[2])
748 ret[i++] = hex_digit (path[1])*16 + hex_digit (path[2]);
757 path = strchr(p1, '&');
764 void uri_val_int(const char *path, const char *name, ODR o, int **intp)
766 const char *v = uri_val(path, name, o);
768 *intp = odr_intdup(o, atoi(v));
771 static void process_http_request(association *assoc, request *req)
773 Z_HTTP_Request *hreq = req->gdu_request->u.HTTP_Request;
774 ODR o = assoc->encode;
776 Z_HTTP_Response *hres = 0;
779 if (!strcmp(hreq->method, "GET"))
783 Z_SOAP *soap_package = 0;
784 char *db = "Default";
785 const char *p0 = hreq->path, *p1;
786 static Z_SOAP_Handler soap_handlers[2] = {
788 {"http://www.loc.gov/zing/srw/v1.0/", 0,
789 (Z_SOAP_fun) yaz_srw_codec},
796 p1 = strchr(p0, '?');
798 p1 = p0 + strlen(p0);
801 db = odr_malloc(assoc->decode, p1 - p0 + 1);
802 memcpy (db, p0, p1 - p0);
806 if (p1 && *p1 == '?' && p1[1])
808 Z_SRW_PDU *res = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
809 Z_SRW_PDU *sr = yaz_srw_get(o, Z_SRW_searchRetrieve_request);
810 char *query = uri_val(p1, "query", o);
811 char *pQuery = uri_val(p1, "pQuery", o);
812 char *sortKeys = uri_val(p1, "sortKeys", o);
816 sr->u.request->query_type = Z_SRW_query_type_cql;
817 sr->u.request->query.cql = query;
821 sr->u.request->query_type = Z_SRW_query_type_pqf;
822 sr->u.request->query.pqf = pQuery;
826 sr->u.request->sort_type = Z_SRW_sort_type_sort;
827 sr->u.request->sort.sortKeys = sortKeys;
829 sr->u.request->recordSchema = uri_val(p1, "recordSchema", o);
830 uri_val_int(p1, "maximumRecords", o,
831 &sr->u.request->maximumRecords);
832 uri_val_int(p1, "startRecord", o,
833 &sr->u.request->startRecord);
834 if (sr->u.request->startRecord)
835 yaz_log(LOG_LOG, "startRecord=%d", *sr->u.request->startRecord);
836 sr->u.request->database = db;
837 srw_bend_search(assoc, req, sr->u.request,
840 soap_package = odr_malloc(o, sizeof(*soap_package));
841 soap_package->which = Z_SOAP_generic;
843 soap_package->u.generic =
844 odr_malloc(o, sizeof(*soap_package->u.generic));
846 soap_package->u.generic->p = res;
847 soap_package->u.generic->ns = soap_handlers[0].ns;
848 soap_package->u.generic->no = 0;
850 soap_package->ns = "SRU";
852 p = z_get_HTTP_Response(o, 200);
853 hres = p->u.HTTP_Response;
855 ret = z_soap_codec_enc(assoc->encode, &soap_package,
856 &hres->content_buf, &hres->content_len,
857 soap_handlers, charset);
859 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/xml");
863 strcpy(ctype, "text/xml; charset=");
864 strcat(ctype, charset);
865 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
870 if (strlen(hreq->path) >= 5 && strlen(hreq->path) < 80 &&
871 !memcmp(hreq->path, "/doc/", 5))
876 strcpy(fpath, DOCDIR);
877 strcat(fpath, hreq->path+4);
878 f = fopen(fpath, "rb");
881 if (fstat(fileno(f), &sbuf) || !S_ISREG(sbuf.st_mode))
890 fseek(f, 0L, SEEK_END);
892 if (sz >= 0 && sz < 500000)
894 const char *ctype = "application/octet-stream";
897 p = z_get_HTTP_Response(o, 200);
898 hres = p->u.HTTP_Response;
899 hres->content_buf = (char *) odr_malloc(o, sz + 1);
900 hres->content_len = sz;
901 fseek(f, 0L, SEEK_SET);
902 fread(hres->content_buf, 1, sz, f);
903 if ((cp = strrchr(fpath, '.'))) {
905 if (!strcmp(cp, "png"))
907 else if (!strcmp(cp, "gif"))
909 else if (!strcmp(cp, "xml"))
911 else if (!strcmp(cp, "html"))
914 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
920 if (!strcmp(hreq->path, "/"))
925 const char *doclink = "";
926 p = z_get_HTTP_Response(o, 200);
927 hres = p->u.HTTP_Response;
928 hres->content_buf = (char *) odr_malloc(o, 400);
930 if (stat(DOCDIR "/yaz.html", &sbuf) == 0 && S_ISREG(sbuf.st_mode))
931 doclink = "<P><A HREF=\"/doc/yaz.html\">Documentation</A></P>";
933 sprintf (hres->content_buf,
934 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
937 " <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
940 " <P><A HREF=\"http://www.indexdata.dk/yaz/\">YAZ</A> "
944 "</HTML>\n", doclink);
945 hres->content_len = strlen(hres->content_buf);
946 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
950 p = z_get_HTTP_Response(o, 404);
953 else if (!strcmp(hreq->method, "POST"))
955 const char *content_type = z_HTTP_header_lookup(hreq->headers,
957 const char *soap_action = z_HTTP_header_lookup(hreq->headers,
959 if (content_type && soap_action &&
960 !yaz_strcmp_del("text/xml", content_type, "; "))
962 Z_SOAP *soap_package = 0;
965 const char *charset_p = 0;
968 static Z_SOAP_Handler soap_handlers[2] = {
970 {"http://www.loc.gov/zing/srw/v1.0/", 0,
971 (Z_SOAP_fun) yaz_srw_codec},
975 if ((charset_p = strstr(content_type, "; charset=")))
979 while (i < 20 && charset_p[i] &&
980 !strchr("; \n\r", charset_p[i]))
982 charset = odr_malloc(assoc->encode, i+1);
983 memcpy(charset, charset_p, i);
985 yaz_log(LOG_LOG, "SOAP encoding %s", charset);
987 ret = z_soap_codec(assoc->decode, &soap_package,
988 &hreq->content_buf, &hreq->content_len,
992 if (!ret && soap_package->which == Z_SOAP_generic &&
993 soap_package->u.generic->no == 0)
996 Z_SRW_PDU *sr = soap_package->u.generic->p;
998 if (sr->which == Z_SRW_searchRetrieve_request)
1001 yaz_srw_get(assoc->encode,
1002 Z_SRW_searchRetrieve_response);
1004 if (!sr->u.request->database)
1006 const char *p0 = hreq->path, *p1;
1009 p1 = strchr(p0, '?');
1011 p1 = p0 + strlen(p0);
1014 sr->u.request->database =
1015 odr_malloc(assoc->decode, p1 - p0 + 1);
1016 memcpy (sr->u.request->database, p0, p1 - p0);
1017 sr->u.request->database[p1 - p0] = '\0';
1020 sr->u.request->database = "Default";
1022 srw_bend_search(assoc, req, sr->u.request,
1025 soap_package->u.generic->p = res;
1030 p = z_get_HTTP_Response(o, 200);
1031 hres = p->u.HTTP_Response;
1032 ret = z_soap_codec_enc(assoc->encode, &soap_package,
1033 &hres->content_buf, &hres->content_len,
1034 soap_handlers, charset);
1035 hres->code = http_code;
1037 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/xml");
1041 strcpy(ctype, "text/xml; charset=");
1042 strcat(ctype, charset);
1043 z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
1046 if (!p) /* still no response ? */
1047 p = z_get_HTTP_Response(o, 500);
1051 p = z_get_HTTP_Response(o, 405);
1052 hres = p->u.HTTP_Response;
1054 z_HTTP_header_add(o, &hres->headers, "Allow", "GET, POST");
1056 hres = p->u.HTTP_Response;
1057 if (!strcmp(hreq->version, "1.0"))
1059 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1060 if (v && !strcmp(v, "Keep-Alive"))
1064 hres->version = "1.0";
1068 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
1069 if (v && !strcmp(v, "close"))
1073 hres->version = "1.1";
1077 z_HTTP_header_add(o, &hres->headers, "Connection", "close");
1078 assoc->state = ASSOC_DEAD;
1083 const char *alive = z_HTTP_header_lookup(hreq->headers, "Keep-Alive");
1085 if (alive && isdigit(*alive))
1089 if (t < 0 || t > 3600)
1091 iochan_settimeout(assoc->client_chan,t);
1092 z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
1094 process_gdu_response(assoc, req, p);
1097 static void process_gdu_request(association *assoc, request *req)
1099 if (req->gdu_request->which == Z_GDU_Z3950)
1102 req->apdu_request = req->gdu_request->u.z3950;
1103 if (process_z_request(assoc, req, &msg) < 0)
1104 do_close_req(assoc, Z_Close_systemProblem, msg, req);
1106 else if (req->gdu_request->which == Z_GDU_HTTP_Request)
1107 process_http_request(assoc, req);
1110 do_close_req(assoc, Z_Close_systemProblem, "bad protocol packet", req);
1115 * Initiate request processing.
1117 static int process_z_request(association *assoc, request *req, char **msg)
1123 *msg = "Unknown Error";
1124 assert(req && req->state == REQUEST_IDLE);
1125 if (req->apdu_request->which != Z_APDU_initRequest && !assoc->init)
1127 *msg = "Missing InitRequest";
1130 switch (req->apdu_request->which)
1132 case Z_APDU_initRequest:
1133 iochan_settimeout(assoc->client_chan,
1134 statserv_getcontrol()->idle_timeout * 60);
1135 res = process_initRequest(assoc, req); break;
1136 case Z_APDU_searchRequest:
1137 res = process_searchRequest(assoc, req, &fd); break;
1138 case Z_APDU_presentRequest:
1139 res = process_presentRequest(assoc, req, &fd); break;
1140 case Z_APDU_scanRequest:
1141 if (assoc->init->bend_scan)
1142 res = process_scanRequest(assoc, req, &fd);
1145 *msg = "Cannot handle Scan APDU";
1149 case Z_APDU_extendedServicesRequest:
1150 if (assoc->init->bend_esrequest)
1151 res = process_ESRequest(assoc, req, &fd);
1154 *msg = "Cannot handle Extended Services APDU";
1158 case Z_APDU_sortRequest:
1159 if (assoc->init->bend_sort)
1160 res = process_sortRequest(assoc, req, &fd);
1163 *msg = "Cannot handle Sort APDU";
1168 process_close(assoc, req);
1170 case Z_APDU_deleteResultSetRequest:
1171 if (assoc->init->bend_delete)
1172 res = process_deleteRequest(assoc, req, &fd);
1175 *msg = "Cannot handle Delete APDU";
1179 case Z_APDU_segmentRequest:
1180 if (assoc->init->bend_segment)
1182 res = process_segmentRequest (assoc, req);
1186 *msg = "Cannot handle Segment APDU";
1191 *msg = "Bad APDU received";
1196 yaz_log(LOG_DEBUG, " result immediately available");
1197 retval = process_z_response(assoc, req, res);
1201 yaz_log(LOG_DEBUG, " result unavailble");
1204 else /* no result yet - one will be provided later */
1208 /* Set up an I/O handler for the fd supplied by the backend */
1210 yaz_log(LOG_DEBUG, " establishing handler for result");
1211 req->state = REQUEST_PENDING;
1212 if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
1214 iochan_setdata(chan, assoc);
1221 * Handle message from the backend.
1223 void backend_response(IOCHAN i, int event)
1225 association *assoc = (association *)iochan_getdata(i);
1226 request *req = request_head(&assoc->incoming);
1230 yaz_log(LOG_DEBUG, "backend_response");
1231 assert(assoc && req && req->state != REQUEST_IDLE);
1232 /* determine what it is we're waiting for */
1233 switch (req->apdu_request->which)
1235 case Z_APDU_searchRequest:
1236 res = response_searchRequest(assoc, req, 0, &fd); break;
1238 case Z_APDU_presentRequest:
1239 res = response_presentRequest(assoc, req, 0, &fd); break;
1240 case Z_APDU_scanRequest:
1241 res = response_scanRequest(assoc, req, 0, &fd); break;
1244 yaz_log(LOG_WARN, "Serious programmer's lapse or bug");
1247 if ((res && process_z_response(assoc, req, res) < 0) || fd < 0)
1249 yaz_log(LOG_LOG, "Fatal error when talking to backend");
1250 do_close(assoc, Z_Close_systemProblem, 0);
1254 else if (!res) /* no result yet - try again later */
1256 yaz_log(LOG_DEBUG, " no result yet");
1257 iochan_setfd(i, fd); /* in case fd has changed */
1262 * Encode response, and transfer the request structure to the outgoing queue.
1264 static int process_gdu_response(association *assoc, request *req, Z_GDU *res)
1266 odr_setbuf(assoc->encode, req->response, req->size_response, 1);
1268 if (assoc->print && !z_GDU(assoc->print, &res, 0, 0))
1270 yaz_log(LOG_WARN, "ODR print error: %s",
1271 odr_errmsg(odr_geterror(assoc->print)));
1272 odr_reset(assoc->print);
1274 if (!z_GDU(assoc->encode, &res, 0, 0))
1276 yaz_log(LOG_WARN, "ODR error when encoding response: %s",
1277 odr_errmsg(odr_geterror(assoc->decode)));
1280 req->response = odr_getbuf(assoc->encode, &req->len_response,
1281 &req->size_response);
1282 odr_setbuf(assoc->encode, 0, 0, 0); /* don'txfree if we abort later */
1283 odr_reset(assoc->encode);
1284 req->state = REQUEST_IDLE;
1285 request_enq(&assoc->outgoing, req);
1286 /* turn the work over to the ir_session handler */
1287 iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
1288 assoc->cs_put_mask = EVENT_OUTPUT;
1289 /* Is there more work to be done? give that to the input handler too */
1291 if (request_head(&assoc->incoming))
1293 yaz_log (LOG_DEBUG, "more work to be done");
1294 iochan_setevent(assoc->client_chan, EVENT_WORK);
1301 * Encode response, and transfer the request structure to the outgoing queue.
1303 static int process_z_response(association *assoc, request *req, Z_APDU *res)
1305 Z_GDU *gres = (Z_GDU *) odr_malloc(assoc->encode, sizeof(*res));
1306 gres->which = Z_GDU_Z3950;
1307 gres->u.z3950 = res;
1309 return process_gdu_response(assoc, req, gres);
1314 * Handle init request.
1315 * At the moment, we don't check the options
1316 * anywhere else in the code - we just try not to do anything that would
1317 * break a naive client. We'll toss 'em into the association block when
1318 * we need them there.
1320 static Z_APDU *process_initRequest(association *assoc, request *reqb)
1322 statserv_options_block *cb = statserv_getcontrol();
1323 Z_InitRequest *req = reqb->apdu_request->u.initRequest;
1324 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_initResponse);
1325 Z_InitResponse *resp = apdu->u.initResponse;
1326 bend_initresult *binitres;
1330 yaz_log(LOG_LOG, "Got initRequest");
1331 if (req->implementationId)
1332 yaz_log(LOG_LOG, "Id: %s", req->implementationId);
1333 if (req->implementationName)
1334 yaz_log(LOG_LOG, "Name: %s", req->implementationName);
1335 if (req->implementationVersion)
1336 yaz_log(LOG_LOG, "Version: %s", req->implementationVersion);
1338 assoc_init_reset(assoc);
1340 assoc->init->auth = req->idAuthentication;
1341 assoc->init->referenceId = req->referenceId;
1343 if (ODR_MASK_GET(req->options, Z_Options_negotiationModel))
1345 Z_CharSetandLanguageNegotiation *negotiation =
1346 yaz_get_charneg_record (req->otherInfo);
1347 if (negotiation->which == Z_CharSetandLanguageNegotiation_proposal)
1348 assoc->init->charneg_request = negotiation;
1351 if (!(binitres = (*cb->bend_init)(assoc->init)))
1353 yaz_log(LOG_WARN, "Bad response from backend.");
1357 assoc->backend = binitres->handle;
1358 if ((assoc->init->bend_sort))
1359 yaz_log (LOG_DEBUG, "Sort handler installed");
1360 if ((assoc->init->bend_search))
1361 yaz_log (LOG_DEBUG, "Search handler installed");
1362 if ((assoc->init->bend_present))
1363 yaz_log (LOG_DEBUG, "Present handler installed");
1364 if ((assoc->init->bend_esrequest))
1365 yaz_log (LOG_DEBUG, "ESRequest handler installed");
1366 if ((assoc->init->bend_delete))
1367 yaz_log (LOG_DEBUG, "Delete handler installed");
1368 if ((assoc->init->bend_scan))
1369 yaz_log (LOG_DEBUG, "Scan handler installed");
1370 if ((assoc->init->bend_segment))
1371 yaz_log (LOG_DEBUG, "Segment handler installed");
1373 resp->referenceId = req->referenceId;
1375 /* let's tell the client what we can do */
1376 if (ODR_MASK_GET(req->options, Z_Options_search))
1378 ODR_MASK_SET(resp->options, Z_Options_search);
1379 strcat(options, "srch");
1381 if (ODR_MASK_GET(req->options, Z_Options_present))
1383 ODR_MASK_SET(resp->options, Z_Options_present);
1384 strcat(options, " prst");
1386 if (ODR_MASK_GET(req->options, Z_Options_delSet) &&
1387 assoc->init->bend_delete)
1389 ODR_MASK_SET(resp->options, Z_Options_delSet);
1390 strcat(options, " del");
1392 if (ODR_MASK_GET(req->options, Z_Options_extendedServices) &&
1393 assoc->init->bend_esrequest)
1395 ODR_MASK_SET(resp->options, Z_Options_extendedServices);
1396 strcat (options, " extendedServices");
1398 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
1400 ODR_MASK_SET(resp->options, Z_Options_namedResultSets);
1401 strcat(options, " namedresults");
1403 if (ODR_MASK_GET(req->options, Z_Options_scan) && assoc->init->bend_scan)
1405 ODR_MASK_SET(resp->options, Z_Options_scan);
1406 strcat(options, " scan");
1408 if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
1410 ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
1411 strcat(options, " concurrop");
1413 if (ODR_MASK_GET(req->options, Z_Options_sort) && assoc->init->bend_sort)
1415 ODR_MASK_SET(resp->options, Z_Options_sort);
1416 strcat(options, " sort");
1419 if (ODR_MASK_GET(req->options, Z_Options_negotiationModel)
1420 && assoc->init->charneg_response)
1422 Z_OtherInformation **p;
1423 Z_OtherInformationUnit *p0;
1425 yaz_oi_APDU(apdu, &p);
1427 if ((p0=yaz_oi_update(p, assoc->encode, NULL, 0, 0))) {
1428 ODR_MASK_SET(resp->options, Z_Options_negotiationModel);
1430 p0->which = Z_OtherInfo_externallyDefinedInfo;
1431 p0->information.externallyDefinedInfo =
1432 assoc->init->charneg_response;
1434 ODR_MASK_SET(resp->options, Z_Options_negotiationModel);
1435 strcat(options, " negotiation");
1438 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
1440 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
1441 assoc->version = 2; /* 1 & 2 are equivalent */
1443 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
1445 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
1448 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_3))
1450 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_3);
1454 yaz_log(LOG_LOG, "Negotiated to v%d: %s", assoc->version, options);
1455 assoc->maximumRecordSize = *req->maximumRecordSize;
1456 if (assoc->maximumRecordSize > control_block->maxrecordsize)
1457 assoc->maximumRecordSize = control_block->maxrecordsize;
1458 assoc->preferredMessageSize = *req->preferredMessageSize;
1459 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
1460 assoc->preferredMessageSize = assoc->maximumRecordSize;
1462 resp->preferredMessageSize = &assoc->preferredMessageSize;
1463 resp->maximumRecordSize = &assoc->maximumRecordSize;
1465 resp->implementationName = "GFS/YAZ";
1467 if (assoc->init->implementation_id)
1470 odr_malloc (assoc->encode,
1471 strlen(assoc->init->implementation_id) + 10 +
1472 strlen(resp->implementationId));
1473 sprintf (nv, "%s / %s",
1474 resp->implementationId, assoc->init->implementation_id);
1475 resp->implementationId = nv;
1477 if (assoc->init->implementation_name)
1480 odr_malloc (assoc->encode,
1481 strlen(assoc->init->implementation_name) + 10 +
1482 strlen(resp->implementationName));
1483 sprintf (nv, "%s / %s",
1484 resp->implementationName, assoc->init->implementation_name);
1485 resp->implementationName = nv;
1487 if (assoc->init->implementation_version)
1490 odr_malloc (assoc->encode,
1491 strlen(assoc->init->implementation_version) + 10 +
1492 strlen(resp->implementationVersion));
1493 sprintf (nv, "YAZ %s / %s",
1494 resp->implementationVersion,
1495 assoc->init->implementation_version);
1496 resp->implementationVersion = nv;
1499 if (binitres->errcode)
1501 yaz_log(LOG_LOG, "Connection rejected by backend.");
1503 assoc->state = ASSOC_DEAD;
1506 assoc->state = ASSOC_UP;
1511 * These functions should be merged.
1514 static void set_addinfo (Z_DefaultDiagFormat *dr, char *addinfo, ODR odr)
1516 dr->which = Z_DefaultDiagFormat_v2Addinfo;
1517 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
1521 * nonsurrogate diagnostic record.
1523 static Z_Records *diagrec(association *assoc, int error, char *addinfo)
1525 Z_Records *rec = (Z_Records *)
1526 odr_malloc (assoc->encode, sizeof(*rec));
1527 int *err = odr_intdup(assoc->encode, error);
1528 Z_DiagRec *drec = (Z_DiagRec *)
1529 odr_malloc (assoc->encode, sizeof(*drec));
1530 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1531 odr_malloc (assoc->encode, sizeof(*dr));
1533 yaz_log(LOG_LOG, "[%d] %s %s%s", error, diagbib1_str(error),
1534 addinfo ? " -- " : "", addinfo ? addinfo : "");
1535 rec->which = Z_Records_NSD;
1536 rec->u.nonSurrogateDiagnostic = dr;
1537 dr->diagnosticSetId =
1538 yaz_oidval_to_z3950oid (assoc->encode, CLASS_DIAGSET, VAL_BIB1);
1539 dr->condition = err;
1540 set_addinfo (dr, addinfo, assoc->encode);
1545 * surrogate diagnostic.
1547 static Z_NamePlusRecord *surrogatediagrec(association *assoc, char *dbname,
1548 int error, char *addinfo)
1550 Z_NamePlusRecord *rec = (Z_NamePlusRecord *)
1551 odr_malloc (assoc->encode, sizeof(*rec));
1552 int *err = odr_intdup(assoc->encode, error);
1553 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
1554 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1555 odr_malloc (assoc->encode, sizeof(*dr));
1557 yaz_log(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
1558 rec->databaseName = dbname;
1559 rec->which = Z_NamePlusRecord_surrogateDiagnostic;
1560 rec->u.surrogateDiagnostic = drec;
1561 drec->which = Z_DiagRec_defaultFormat;
1562 drec->u.defaultFormat = dr;
1563 dr->diagnosticSetId =
1564 yaz_oidval_to_z3950oid (assoc->encode, CLASS_DIAGSET, VAL_BIB1);
1565 dr->condition = err;
1566 set_addinfo (dr, addinfo, assoc->encode);
1572 * multiple nonsurrogate diagnostics.
1574 static Z_DiagRecs *diagrecs(association *assoc, int error, char *addinfo)
1576 Z_DiagRecs *recs = (Z_DiagRecs *)odr_malloc (assoc->encode, sizeof(*recs));
1577 int *err = odr_intdup(assoc->encode, error);
1578 Z_DiagRec **recp = (Z_DiagRec **)odr_malloc (assoc->encode, sizeof(*recp));
1579 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
1580 Z_DefaultDiagFormat *rec = (Z_DefaultDiagFormat *)
1581 odr_malloc (assoc->encode, sizeof(*rec));
1583 yaz_log(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo ? addinfo : "");
1585 recs->num_diagRecs = 1;
1586 recs->diagRecs = recp;
1588 drec->which = Z_DiagRec_defaultFormat;
1589 drec->u.defaultFormat = rec;
1591 rec->diagnosticSetId =
1592 yaz_oidval_to_z3950oid (assoc->encode, CLASS_DIAGSET, VAL_BIB1);
1593 rec->condition = err;
1595 rec->which = Z_DefaultDiagFormat_v2Addinfo;
1596 rec->u.v2Addinfo = odr_strdup (assoc->encode, addinfo ? addinfo : "");
1600 static Z_Records *pack_records(association *a, char *setname, int start,
1601 int *num, Z_RecordComposition *comp,
1602 int *next, int *pres, oid_value format,
1603 Z_ReferenceId *referenceId,
1606 int recno, total_length = 0, toget = *num, dumped_records = 0;
1607 Z_Records *records =
1608 (Z_Records *) odr_malloc (a->encode, sizeof(*records));
1609 Z_NamePlusRecordList *reclist =
1610 (Z_NamePlusRecordList *) odr_malloc (a->encode, sizeof(*reclist));
1611 Z_NamePlusRecord **list =
1612 (Z_NamePlusRecord **) odr_malloc (a->encode, sizeof(*list) * toget);
1614 records->which = Z_Records_DBOSD;
1615 records->u.databaseOrSurDiagnostics = reclist;
1616 reclist->num_records = 0;
1617 reclist->records = list;
1618 *pres = Z_PRES_SUCCESS;
1622 yaz_log(LOG_LOG, "Request to pack %d+%d+%s", start, toget, setname);
1623 yaz_log(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
1624 a->maximumRecordSize);
1625 for (recno = start; reclist->num_records < toget; recno++)
1628 Z_NamePlusRecord *thisrec;
1629 int this_length = 0;
1631 * we get the number of bytes allocated on the stream before any
1632 * allocation done by the backend - this should give us a reasonable
1633 * idea of the total size of the data so far.
1635 total_length = odr_total(a->encode) - dumped_records;
1641 freq.last_in_set = 0;
1642 freq.setname = setname;
1643 freq.surrogate_flag = 0;
1644 freq.number = recno;
1646 freq.request_format = format;
1647 freq.request_format_raw = oid;
1648 freq.output_format = format;
1649 freq.output_format_raw = 0;
1650 freq.stream = a->encode;
1651 freq.print = a->print;
1652 freq.surrogate_flag = 0;
1653 freq.referenceId = referenceId;
1654 (*a->init->bend_fetch)(a->backend, &freq);
1655 /* backend should be able to signal whether error is system-wide
1656 or only pertaining to current record */
1659 if (!freq.surrogate_flag)
1662 *pres = Z_PRES_FAILURE;
1663 /* for 'present request out of range',
1664 set addinfo to record position if not set */
1665 if (freq.errcode == 13 && freq.errstring == 0)
1667 sprintf (s, "%d", recno);
1670 return diagrec(a, freq.errcode, freq.errstring);
1672 reclist->records[reclist->num_records] =
1673 surrogatediagrec(a, freq.basename, freq.errcode,
1675 reclist->num_records++;
1676 *next = freq.last_in_set ? 0 : recno + 1;
1680 this_length = freq.len;
1682 this_length = odr_total(a->encode) - total_length;
1683 yaz_log(LOG_DEBUG, " fetched record, len=%d, total=%d",
1684 this_length, total_length);
1685 if (this_length + total_length > a->preferredMessageSize)
1687 /* record is small enough, really */
1688 if (this_length <= a->preferredMessageSize)
1690 yaz_log(LOG_DEBUG, " Dropped last normal-sized record");
1691 *pres = Z_PRES_PARTIAL_2;
1694 /* record can only be fetched by itself */
1695 if (this_length < a->maximumRecordSize)
1697 yaz_log(LOG_DEBUG, " Record > prefmsgsz");
1700 yaz_log(LOG_DEBUG, " Dropped it");
1701 reclist->records[reclist->num_records] =
1702 surrogatediagrec(a, freq.basename, 16, 0);
1703 reclist->num_records++;
1704 *next = freq.last_in_set ? 0 : recno + 1;
1705 dumped_records += this_length;
1709 else /* too big entirely */
1711 yaz_log(LOG_LOG, "Record > maxrcdsz this=%d max=%d", this_length, a->maximumRecordSize);
1712 reclist->records[reclist->num_records] =
1713 surrogatediagrec(a, freq.basename, 17, 0);
1714 reclist->num_records++;
1715 *next = freq.last_in_set ? 0 : recno + 1;
1716 dumped_records += this_length;
1721 if (!(thisrec = (Z_NamePlusRecord *)
1722 odr_malloc(a->encode, sizeof(*thisrec))))
1724 if (!(thisrec->databaseName = (char *)odr_malloc(a->encode,
1725 strlen(freq.basename) + 1)))
1727 strcpy(thisrec->databaseName, freq.basename);
1728 thisrec->which = Z_NamePlusRecord_databaseRecord;
1730 if (freq.output_format_raw)
1732 struct oident *ident = oid_getentbyoid(freq.output_format_raw);
1733 freq.output_format = ident->value;
1735 thisrec->u.databaseRecord = z_ext_record(a->encode, freq.output_format,
1736 freq.record, freq.len);
1737 if (!thisrec->u.databaseRecord)
1739 reclist->records[reclist->num_records] = thisrec;
1740 reclist->num_records++;
1741 *next = freq.last_in_set ? 0 : recno + 1;
1743 *num = reclist->num_records;
1747 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
1750 Z_SearchRequest *req = reqb->apdu_request->u.searchRequest;
1751 bend_search_rr *bsrr =
1752 (bend_search_rr *)nmem_malloc (reqb->request_mem, sizeof(*bsrr));
1754 yaz_log(LOG_LOG, "Got SearchRequest.");
1756 bsrr->request = reqb;
1757 bsrr->association = assoc;
1758 bsrr->referenceId = req->referenceId;
1759 save_referenceId (reqb, bsrr->referenceId);
1761 yaz_log (LOG_LOG, "ResultSet '%s'", req->resultSetName);
1762 if (req->databaseNames)
1765 for (i = 0; i < req->num_databaseNames; i++)
1766 yaz_log (LOG_LOG, "Database '%s'", req->databaseNames[i]);
1768 yaz_log_zquery(req->query);
1770 if (assoc->init->bend_search)
1772 bsrr->setname = req->resultSetName;
1773 bsrr->replace_set = *req->replaceIndicator;
1774 bsrr->num_bases = req->num_databaseNames;
1775 bsrr->basenames = req->databaseNames;
1776 bsrr->query = req->query;
1777 bsrr->stream = assoc->encode;
1778 bsrr->decode = assoc->decode;
1779 bsrr->print = assoc->print;
1782 bsrr->errstring = NULL;
1783 bsrr->search_info = NULL;
1784 (assoc->init->bend_search)(assoc->backend, bsrr);
1788 return response_searchRequest(assoc, reqb, bsrr, fd);
1791 int bend_searchresponse(void *handle, bend_search_rr *bsrr) {return 0;}
1794 * Prepare a searchresponse based on the backend results. We probably want
1795 * to look at making the fetching of records nonblocking as well, but
1796 * so far, we'll keep things simple.
1797 * If bsrt is null, that means we're called in response to a communications
1798 * event, and we'll have to get the response for ourselves.
1800 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
1801 bend_search_rr *bsrt, int *fd)
1803 Z_SearchRequest *req = reqb->apdu_request->u.searchRequest;
1804 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1805 Z_SearchResponse *resp = (Z_SearchResponse *)
1806 odr_malloc (assoc->encode, sizeof(*resp));
1807 int *nulint = odr_intdup (assoc->encode, 0);
1808 bool_t *sr = odr_intdup(assoc->encode, 1);
1809 int *next = odr_intdup(assoc->encode, 0);
1810 int *none = odr_intdup(assoc->encode, Z_RES_NONE);
1812 apdu->which = Z_APDU_searchResponse;
1813 apdu->u.searchResponse = resp;
1814 resp->referenceId = req->referenceId;
1815 resp->additionalSearchInfo = 0;
1816 resp->otherInfo = 0;
1818 if (!bsrt && !bend_searchresponse(assoc->backend, bsrt))
1820 yaz_log(LOG_FATAL, "Bad result from backend");
1823 else if (bsrt->errcode)
1825 resp->records = diagrec(assoc, bsrt->errcode, bsrt->errstring);
1826 resp->resultCount = nulint;
1827 resp->numberOfRecordsReturned = nulint;
1828 resp->nextResultSetPosition = nulint;
1829 resp->searchStatus = nulint;
1830 resp->resultSetStatus = none;
1831 resp->presentStatus = 0;
1835 int *toget = odr_intdup(assoc->encode, 0);
1836 int *presst = odr_intdup(assoc->encode, 0);
1837 Z_RecordComposition comp, *compp = 0;
1839 yaz_log (LOG_LOG, "resultCount: %d", bsrt->hits);
1842 resp->resultCount = &bsrt->hits;
1844 comp.which = Z_RecordComp_simple;
1845 /* how many records does the user agent want, then? */
1846 if (bsrt->hits <= *req->smallSetUpperBound)
1848 *toget = bsrt->hits;
1849 if ((comp.u.simple = req->smallSetElementSetNames))
1852 else if (bsrt->hits < *req->largeSetLowerBound)
1854 *toget = *req->mediumSetPresentNumber;
1855 if (*toget > bsrt->hits)
1856 *toget = bsrt->hits;
1857 if ((comp.u.simple = req->mediumSetElementSetNames))
1863 if (*toget && !resp->records)
1868 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)))
1871 form = prefformat->value;
1872 resp->records = pack_records(assoc, req->resultSetName, 1,
1873 toget, compp, next, presst, form, req->referenceId,
1874 req->preferredRecordSyntax);
1877 resp->numberOfRecordsReturned = toget;
1878 resp->nextResultSetPosition = next;
1879 resp->searchStatus = sr;
1880 resp->resultSetStatus = 0;
1881 resp->presentStatus = presst;
1885 if (*resp->resultCount)
1887 resp->numberOfRecordsReturned = nulint;
1888 resp->nextResultSetPosition = next;
1889 resp->searchStatus = sr;
1890 resp->resultSetStatus = 0;
1891 resp->presentStatus = 0;
1894 resp->additionalSearchInfo = bsrt->search_info;
1899 * Maybe we got a little over-friendly when we designed bend_fetch to
1900 * get only one record at a time. Some backends can optimise multiple-record
1901 * fetches, and at any rate, there is some overhead involved in
1902 * all that selecting and hopping around. Problem is, of course, that the
1903 * frontend can't know ahead of time how many records it'll need to
1904 * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
1905 * is downright lousy as a bulk data transfer protocol.
1907 * To start with, we'll do the fetching of records from the backend
1908 * in one operation: To save some trips in and out of the event-handler,
1909 * and to simplify the interface to pack_records. At any rate, asynch
1910 * operation is more fun in operations that have an unpredictable execution
1911 * speed - which is normally more true for search than for present.
1913 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
1916 Z_PresentRequest *req = reqb->apdu_request->u.presentRequest;
1920 Z_PresentResponse *resp;
1924 yaz_log(LOG_LOG, "Got PresentRequest.");
1926 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)))
1929 form = prefformat->value;
1930 resp = (Z_PresentResponse *)odr_malloc (assoc->encode, sizeof(*resp));
1932 resp->presentStatus = odr_intdup(assoc->encode, 0);
1933 if (assoc->init->bend_present)
1935 bend_present_rr *bprr = (bend_present_rr *)
1936 nmem_malloc (reqb->request_mem, sizeof(*bprr));
1937 bprr->setname = req->resultSetId;
1938 bprr->start = *req->resultSetStartPoint;
1939 bprr->number = *req->numberOfRecordsRequested;
1940 bprr->format = form;
1941 bprr->comp = req->recordComposition;
1942 bprr->referenceId = req->referenceId;
1943 bprr->stream = assoc->encode;
1944 bprr->print = assoc->print;
1945 bprr->request = reqb;
1946 bprr->association = assoc;
1948 bprr->errstring = NULL;
1949 (*assoc->init->bend_present)(assoc->backend, bprr);
1955 resp->records = diagrec(assoc, bprr->errcode, bprr->errstring);
1956 *resp->presentStatus = Z_PRES_FAILURE;
1959 apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1960 next = odr_intdup(assoc->encode, 0);
1961 num = odr_intdup(assoc->encode, 0);
1963 apdu->which = Z_APDU_presentResponse;
1964 apdu->u.presentResponse = resp;
1965 resp->referenceId = req->referenceId;
1966 resp->otherInfo = 0;
1970 *num = *req->numberOfRecordsRequested;
1972 pack_records(assoc, req->resultSetId, *req->resultSetStartPoint,
1973 num, req->recordComposition, next, resp->presentStatus,
1974 form, req->referenceId, req->preferredRecordSyntax);
1978 resp->numberOfRecordsReturned = num;
1979 resp->nextResultSetPosition = next;
1985 * Scan was implemented rather in a hurry, and with support for only the basic
1986 * elements of the service in the backend API. Suggestions are welcome.
1988 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
1990 Z_ScanRequest *req = reqb->apdu_request->u.scanRequest;
1991 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1992 Z_ScanResponse *res = (Z_ScanResponse *)
1993 odr_malloc (assoc->encode, sizeof(*res));
1994 int *scanStatus = odr_intdup(assoc->encode, Z_Scan_failure);
1995 int *numberOfEntriesReturned = odr_intdup(assoc->encode, 0);
1996 Z_ListEntries *ents = (Z_ListEntries *)
1997 odr_malloc (assoc->encode, sizeof(*ents));
1998 Z_DiagRecs *diagrecs_p = NULL;
2000 bend_scan_rr *bsrr = (bend_scan_rr *)
2001 odr_malloc (assoc->encode, sizeof(*bsrr));
2003 yaz_log(LOG_LOG, "Got ScanRequest");
2005 apdu->which = Z_APDU_scanResponse;
2006 apdu->u.scanResponse = res;
2007 res->referenceId = req->referenceId;
2009 /* if step is absent, set it to 0 */
2010 res->stepSize = odr_intdup(assoc->encode, 0);
2012 *res->stepSize = *req->stepSize;
2014 res->scanStatus = scanStatus;
2015 res->numberOfEntriesReturned = numberOfEntriesReturned;
2016 res->positionOfTerm = 0;
2017 res->entries = ents;
2018 ents->num_entries = 0;
2019 ents->entries = NULL;
2020 ents->num_nonsurrogateDiagnostics = 0;
2021 ents->nonsurrogateDiagnostics = NULL;
2022 res->attributeSet = 0;
2025 if (req->databaseNames)
2028 for (i = 0; i < req->num_databaseNames; i++)
2029 yaz_log (LOG_LOG, "Database '%s'", req->databaseNames[i]);
2031 bsrr->num_bases = req->num_databaseNames;
2032 bsrr->basenames = req->databaseNames;
2033 bsrr->num_entries = *req->numberOfTermsRequested;
2034 bsrr->term = req->termListAndStartPoint;
2035 bsrr->referenceId = req->referenceId;
2036 bsrr->stream = assoc->encode;
2037 bsrr->print = assoc->print;
2038 bsrr->step_size = res->stepSize;
2039 if (req->attributeSet &&
2040 (attset = oid_getentbyoid(req->attributeSet)) &&
2041 (attset->oclass == CLASS_ATTSET || attset->oclass == CLASS_GENERAL))
2042 bsrr->attributeset = attset->value;
2044 bsrr->attributeset = VAL_NONE;
2045 log_scan_term (req->termListAndStartPoint, bsrr->attributeset);
2046 bsrr->term_position = req->preferredPositionInResponse ?
2047 *req->preferredPositionInResponse : 1;
2048 ((int (*)(void *, bend_scan_rr *))
2049 (*assoc->init->bend_scan))(assoc->backend, bsrr);
2051 diagrecs_p = diagrecs(assoc, bsrr->errcode, bsrr->errstring);
2055 Z_Entry **tab = (Z_Entry **)
2056 odr_malloc (assoc->encode, sizeof(*tab) * bsrr->num_entries);
2058 if (bsrr->status == BEND_SCAN_PARTIAL)
2059 *scanStatus = Z_Scan_partial_5;
2061 *scanStatus = Z_Scan_success;
2062 ents->entries = tab;
2063 ents->num_entries = bsrr->num_entries;
2064 res->numberOfEntriesReturned = &ents->num_entries;
2065 res->positionOfTerm = &bsrr->term_position;
2066 for (i = 0; i < bsrr->num_entries; i++)
2072 tab[i] = e = (Z_Entry *)odr_malloc(assoc->encode, sizeof(*e));
2073 if (bsrr->entries[i].occurrences >= 0)
2075 e->which = Z_Entry_termInfo;
2076 e->u.termInfo = t = (Z_TermInfo *)
2077 odr_malloc(assoc->encode, sizeof(*t));
2078 t->suggestedAttributes = 0;
2080 t->alternativeTerm = 0;
2081 t->byAttributes = 0;
2082 t->otherTermInfo = 0;
2083 t->globalOccurrences = &bsrr->entries[i].occurrences;
2084 t->term = (Z_Term *)
2085 odr_malloc(assoc->encode, sizeof(*t->term));
2086 t->term->which = Z_Term_general;
2087 t->term->u.general = o =
2088 (Odr_oct *)odr_malloc(assoc->encode, sizeof(Odr_oct));
2089 o->buf = (unsigned char *)
2090 odr_malloc(assoc->encode, o->len = o->size =
2091 strlen(bsrr->entries[i].term));
2092 memcpy(o->buf, bsrr->entries[i].term, o->len);
2093 yaz_log(LOG_DEBUG, " term #%d: '%s' (%d)", i,
2094 bsrr->entries[i].term, bsrr->entries[i].occurrences);
2098 Z_DiagRecs *drecs = diagrecs (assoc,
2099 bsrr->entries[i].errcode,
2100 bsrr->entries[i].errstring);
2101 assert (drecs->num_diagRecs == 1);
2102 e->which = Z_Entry_surrogateDiagnostic;
2103 assert (drecs->diagRecs[0]);
2104 e->u.surrogateDiagnostic = drecs->diagRecs[0];
2110 ents->num_nonsurrogateDiagnostics = diagrecs_p->num_diagRecs;
2111 ents->nonsurrogateDiagnostics = diagrecs_p->diagRecs;
2116 static Z_APDU *process_sortRequest(association *assoc, request *reqb,
2119 Z_SortRequest *req = reqb->apdu_request->u.sortRequest;
2120 Z_SortResponse *res = (Z_SortResponse *)
2121 odr_malloc (assoc->encode, sizeof(*res));
2122 bend_sort_rr *bsrr = (bend_sort_rr *)
2123 odr_malloc (assoc->encode, sizeof(*bsrr));
2125 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
2127 yaz_log(LOG_LOG, "Got SortRequest.");
2129 bsrr->num_input_setnames = req->num_inputResultSetNames;
2130 bsrr->input_setnames = req->inputResultSetNames;
2131 bsrr->referenceId = req->referenceId;
2132 bsrr->output_setname = req->sortedResultSetName;
2133 bsrr->sort_sequence = req->sortSequence;
2134 bsrr->stream = assoc->encode;
2135 bsrr->print = assoc->print;
2137 bsrr->sort_status = Z_SortStatus_failure;
2139 bsrr->errstring = 0;
2141 (*assoc->init->bend_sort)(assoc->backend, bsrr);
2143 res->referenceId = bsrr->referenceId;
2144 res->sortStatus = odr_intdup(assoc->encode, bsrr->sort_status);
2145 res->resultSetStatus = 0;
2148 Z_DiagRecs *dr = diagrecs (assoc, bsrr->errcode, bsrr->errstring);
2149 res->diagnostics = dr->diagRecs;
2150 res->num_diagnostics = dr->num_diagRecs;
2154 res->num_diagnostics = 0;
2155 res->diagnostics = 0;
2159 apdu->which = Z_APDU_sortResponse;
2160 apdu->u.sortResponse = res;
2164 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
2167 Z_DeleteResultSetRequest *req =
2168 reqb->apdu_request->u.deleteResultSetRequest;
2169 Z_DeleteResultSetResponse *res = (Z_DeleteResultSetResponse *)
2170 odr_malloc (assoc->encode, sizeof(*res));
2171 bend_delete_rr *bdrr = (bend_delete_rr *)
2172 odr_malloc (assoc->encode, sizeof(*bdrr));
2173 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
2175 yaz_log(LOG_LOG, "Got DeleteRequest.");
2177 bdrr->num_setnames = req->num_resultSetList;
2178 bdrr->setnames = req->resultSetList;
2179 bdrr->stream = assoc->encode;
2180 bdrr->print = assoc->print;
2181 bdrr->function = *req->deleteFunction;
2182 bdrr->referenceId = req->referenceId;
2184 if (bdrr->num_setnames > 0)
2187 bdrr->statuses = (int*)
2188 odr_malloc(assoc->encode, sizeof(*bdrr->statuses) *
2189 bdrr->num_setnames);
2190 for (i = 0; i < bdrr->num_setnames; i++)
2191 bdrr->statuses[i] = 0;
2193 (*assoc->init->bend_delete)(assoc->backend, bdrr);
2195 res->referenceId = req->referenceId;
2197 res->deleteOperationStatus = odr_intdup(assoc->encode,bdrr->delete_status);
2199 res->deleteListStatuses = 0;
2200 if (bdrr->num_setnames > 0)
2203 res->deleteListStatuses = (Z_ListStatuses *)
2204 odr_malloc(assoc->encode, sizeof(*res->deleteListStatuses));
2205 res->deleteListStatuses->num = bdrr->num_setnames;
2206 res->deleteListStatuses->elements =
2208 odr_malloc (assoc->encode,
2209 sizeof(*res->deleteListStatuses->elements) *
2210 bdrr->num_setnames);
2211 for (i = 0; i<bdrr->num_setnames; i++)
2213 res->deleteListStatuses->elements[i] =
2215 odr_malloc (assoc->encode,
2216 sizeof(**res->deleteListStatuses->elements));
2217 res->deleteListStatuses->elements[i]->status = bdrr->statuses+i;
2218 res->deleteListStatuses->elements[i]->id =
2219 odr_strdup (assoc->encode, bdrr->setnames[i]);
2223 res->numberNotDeleted = 0;
2224 res->bulkStatuses = 0;
2225 res->deleteMessage = 0;
2228 apdu->which = Z_APDU_deleteResultSetResponse;
2229 apdu->u.deleteResultSetResponse = res;
2233 static void process_close(association *assoc, request *reqb)
2235 Z_Close *req = reqb->apdu_request->u.close;
2236 static char *reasons[] =
2243 "securityViolation",
2250 yaz_log(LOG_LOG, "Got Close, reason %s, message %s",
2251 reasons[*req->closeReason], req->diagnosticInformation ?
2252 req->diagnosticInformation : "NULL");
2253 if (assoc->version < 3) /* to make do_force respond with close */
2255 do_close_req(assoc, Z_Close_finished,
2256 "Association terminated by client", reqb);
2259 void save_referenceId (request *reqb, Z_ReferenceId *refid)
2263 reqb->len_refid = refid->len;
2264 reqb->refid = (char *)nmem_malloc (reqb->request_mem, refid->len);
2265 memcpy (reqb->refid, refid->buf, refid->len);
2269 reqb->len_refid = 0;
2274 void bend_request_send (bend_association a, bend_request req, Z_APDU *res)
2276 process_z_response (a, req, res);
2279 bend_request bend_request_mk (bend_association a)
2281 request *nreq = request_get (&a->outgoing);
2282 nreq->request_mem = nmem_create ();
2286 Z_ReferenceId *bend_request_getid (ODR odr, bend_request req)
2291 id = (Odr_oct *)odr_malloc (odr, sizeof(*odr));
2292 id->buf = (unsigned char *)odr_malloc (odr, req->len_refid);
2293 id->len = id->size = req->len_refid;
2294 memcpy (id->buf, req->refid, req->len_refid);
2298 void bend_request_destroy (bend_request *req)
2300 nmem_destroy((*req)->request_mem);
2301 request_release(*req);
2305 int bend_backend_respond (bend_association a, bend_request req)
2309 r = process_z_request (a, req, &msg);
2311 yaz_log (LOG_WARN, "%s", msg);
2315 void bend_request_setdata(bend_request r, void *p)
2320 void *bend_request_getdata(bend_request r)
2322 return r->clientData;
2325 static Z_APDU *process_segmentRequest (association *assoc, request *reqb)
2327 bend_segment_rr req;
2329 req.segment = reqb->apdu_request->u.segmentRequest;
2330 req.stream = assoc->encode;
2331 req.decode = assoc->decode;
2332 req.print = assoc->print;
2333 req.association = assoc;
2335 (*assoc->init->bend_segment)(assoc->backend, &req);
2340 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd)
2342 bend_esrequest_rr esrequest;
2344 Z_ExtendedServicesRequest *req =
2345 reqb->apdu_request->u.extendedServicesRequest;
2346 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_extendedServicesResponse);
2348 Z_ExtendedServicesResponse *resp = apdu->u.extendedServicesResponse;
2350 yaz_log(LOG_DEBUG,"inside Process esRequest");
2352 esrequest.esr = reqb->apdu_request->u.extendedServicesRequest;
2353 esrequest.stream = assoc->encode;
2354 esrequest.decode = assoc->decode;
2355 esrequest.print = assoc->print;
2356 esrequest.errcode = 0;
2357 esrequest.errstring = NULL;
2358 esrequest.request = reqb;
2359 esrequest.association = assoc;
2360 esrequest.taskPackage = 0;
2361 esrequest.referenceId = req->referenceId;
2363 (*assoc->init->bend_esrequest)(assoc->backend, &esrequest);
2365 /* If the response is being delayed, return NULL */
2366 if (esrequest.request == NULL)
2369 resp->referenceId = req->referenceId;
2371 if (esrequest.errcode == -1)
2373 /* Backend service indicates request will be processed */
2374 yaz_log(LOG_DEBUG,"Request could be processed...Accepted !");
2375 *resp->operationStatus = Z_ExtendedServicesResponse_accepted;
2377 else if (esrequest.errcode == 0)
2379 /* Backend service indicates request will be processed */
2380 yaz_log(LOG_DEBUG,"Request could be processed...Done !");
2381 *resp->operationStatus = Z_ExtendedServicesResponse_done;
2385 Z_DiagRecs *diagRecs = diagrecs (assoc, esrequest.errcode,
2386 esrequest.errstring);
2388 /* Backend indicates error, request will not be processed */
2389 yaz_log(LOG_DEBUG,"Request could not be processed...failure !");
2390 *resp->operationStatus = Z_ExtendedServicesResponse_failure;
2391 resp->num_diagnostics = diagRecs->num_diagRecs;
2392 resp->diagnostics = diagRecs->diagRecs;
2394 /* Do something with the members of bend_extendedservice */
2395 if (esrequest.taskPackage)
2396 resp->taskPackage = z_ext_record (assoc->encode, VAL_EXTENDED,
2397 (const char *) esrequest.taskPackage,
2399 yaz_log(LOG_DEBUG,"Send the result apdu");