2835262cebe6c9ff9e27c6bc4787b2eeeef56650
[yaz-moved-to-github.git] / src / zoom-z3950.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file zoom-z3950.c
7  * \brief Implements ZOOM Z39.50 handling
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <assert.h>
14 #include <string.h>
15 #include <errno.h>
16 #include "zoom-p.h"
17
18 #include <yaz/yaz-util.h>
19 #include <yaz/xmalloc.h>
20 #include <yaz/otherinfo.h>
21 #include <yaz/log.h>
22 #include <yaz/pquery.h>
23 #include <yaz/marcdisp.h>
24 #include <yaz/diagbib1.h>
25 #include <yaz/charneg.h>
26 #include <yaz/ill.h>
27 #include <yaz/query-charset.h>
28 #include <yaz/copy_types.h>
29 #include <yaz/snprintf.h>
30 #include <yaz/facet.h>
31
32 #include <yaz/shptr.h>
33
34 /*
35  * This wrapper is just for logging failed lookups.  It would be nicer
36  * if it could cause failure when a lookup fails, but that's hard.
37  */
38 static Odr_oid *zoom_yaz_str_to_z3950oid(ZOOM_connection c,
39                                      oid_class oid_class, const char *str)
40 {
41     Odr_oid *res = yaz_string_to_oid_odr(yaz_oid_std(), oid_class, str,
42                                      c->odr_out);
43     if (res == 0)
44         yaz_log(YLOG_WARN, "%p OID lookup (%d, '%s') failed",
45                 c, (int) oid_class, str);
46     return res;
47 }
48
49 static Z_APDU *create_es_package(ZOOM_package p, const Odr_oid *oid)
50 {
51     const char *str;
52     Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
53     Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
54
55     str = ZOOM_options_get(p->options, "package-name");
56     if (str && *str)
57         req->packageName = odr_strdup(p->odr_out, str);
58
59     str = ZOOM_options_get(p->options, "user-id");
60     if (str)
61         req->userId = odr_strdup_null(p->odr_out, str);
62
63     req->packageType = odr_oiddup(p->odr_out, oid);
64
65     str = ZOOM_options_get(p->options, "function");
66     if (str)
67     {
68         if (!strcmp (str, "create"))
69             *req->function = Z_ExtendedServicesRequest_create;
70         if (!strcmp (str, "delete"))
71             *req->function = Z_ExtendedServicesRequest_delete;
72         if (!strcmp (str, "modify"))
73             *req->function = Z_ExtendedServicesRequest_modify;
74     }
75
76     str = ZOOM_options_get(p->options, "waitAction");
77     if (str)
78     {
79         if (!strcmp (str, "wait"))
80             *req->waitAction = Z_ExtendedServicesRequest_wait;
81         if (!strcmp (str, "waitIfPossible"))
82             *req->waitAction = Z_ExtendedServicesRequest_waitIfPossible;
83         if (!strcmp (str, "dontWait"))
84             *req->waitAction = Z_ExtendedServicesRequest_dontWait;
85         if (!strcmp (str, "dontReturnPackage"))
86             *req->waitAction = Z_ExtendedServicesRequest_dontReturnPackage;
87     }
88     return apdu;
89 }
90
91 static const char *ill_array_lookup(void *clientData, const char *idx)
92 {
93     ZOOM_package p = (ZOOM_package) clientData;
94     return ZOOM_options_get(p->options, idx+4);
95 }
96
97 static Z_External *encode_ill_request(ZOOM_package p)
98 {
99     ODR out = p->odr_out;
100     ILL_Request *req;
101     Z_External *r = 0;
102     struct ill_get_ctl ctl;
103
104     ctl.odr = p->odr_out;
105     ctl.clientData = p;
106     ctl.f = ill_array_lookup;
107
108     req = ill_get_ILLRequest(&ctl, "ill", 0);
109
110     if (!ill_Request(out, &req, 0, 0))
111     {
112         int ill_request_size;
113         char *ill_request_buf = odr_getbuf(out, &ill_request_size, 0);
114         if (ill_request_buf)
115             odr_setbuf(out, ill_request_buf, ill_request_size, 1);
116         return 0;
117     }
118     else
119     {
120         int illRequest_size = 0;
121         char *illRequest_buf = odr_getbuf(out, &illRequest_size, 0);
122
123         r = (Z_External *) odr_malloc(out, sizeof(*r));
124         r->direct_reference = odr_oiddup(out, yaz_oid_general_isoill_1);
125         r->indirect_reference = 0;
126         r->descriptor = 0;
127         r->which = Z_External_single;
128
129         r->u.single_ASN1_type =
130             odr_create_Odr_oct(out, illRequest_buf, illRequest_size);
131     }
132     return r;
133 }
134
135 static Z_ItemOrder *encode_item_order(ZOOM_package p)
136 {
137     Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc(p->odr_out, sizeof(*req));
138     const char *str;
139     int len;
140
141     req->which = Z_IOItemOrder_esRequest;
142     req->u.esRequest = (Z_IORequest *)
143         odr_malloc(p->odr_out,sizeof(Z_IORequest));
144
145     /* to keep part ... */
146     req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
147         odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
148     req->u.esRequest->toKeep->supplDescription = 0;
149     req->u.esRequest->toKeep->contact = (Z_IOContact *)
150         odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
151
152     str = ZOOM_options_get(p->options, "contact-name");
153     req->u.esRequest->toKeep->contact->name =
154         odr_strdup_null(p->odr_out, str);
155
156     str = ZOOM_options_get(p->options, "contact-phone");
157     req->u.esRequest->toKeep->contact->phone =
158         odr_strdup_null(p->odr_out, str);
159
160     str = ZOOM_options_get(p->options, "contact-email");
161     req->u.esRequest->toKeep->contact->email =
162         odr_strdup_null(p->odr_out, str);
163
164     req->u.esRequest->toKeep->addlBilling = 0;
165
166     /* not to keep part ... */
167     req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
168         odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
169
170     str = ZOOM_options_get(p->options, "itemorder-setname");
171     if (!str)
172         str = "default";
173
174     if (!*str)
175         req->u.esRequest->notToKeep->resultSetItem = 0;
176     else
177     {
178         req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
179             odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
180
181         req->u.esRequest->notToKeep->resultSetItem->resultSetId =
182             odr_strdup(p->odr_out, str);
183         req->u.esRequest->notToKeep->resultSetItem->item =
184             odr_intdup(p->odr_out, 0);
185
186         str = ZOOM_options_get(p->options, "itemorder-item");
187         *req->u.esRequest->notToKeep->resultSetItem->item =
188             (str ? atoi(str) : 1);
189     }
190
191     str = ZOOM_options_getl(p->options, "doc", &len);
192     if (str)
193     {
194         req->u.esRequest->notToKeep->itemRequest =
195             z_ext_record_xml(p->odr_out, str, len);
196     }
197     else
198         req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
199
200     return req;
201 }
202
203 Z_APDU *create_admin_package(ZOOM_package p, int type,
204                              Z_ESAdminOriginPartToKeep **toKeepP,
205                              Z_ESAdminOriginPartNotToKeep **notToKeepP)
206 {
207     Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_admin);
208     if (apdu)
209     {
210         Z_ESAdminOriginPartToKeep  *toKeep;
211         Z_ESAdminOriginPartNotToKeep  *notToKeep;
212         Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
213         const char *first_db = "Default";
214         int num_db;
215         char **db = ZOOM_connection_get_databases(p->connection,
216                                                   p->options, &num_db,
217                                                   p->odr_out);
218         if (num_db > 0)
219             first_db = db[0];
220
221         r->direct_reference = odr_oiddup(p->odr_out, yaz_oid_extserv_admin);
222         r->descriptor = 0;
223         r->indirect_reference = 0;
224         r->which = Z_External_ESAdmin;
225
226         r->u.adminService = (Z_Admin *)
227             odr_malloc(p->odr_out, sizeof(*r->u.adminService));
228         r->u.adminService->which = Z_Admin_esRequest;
229         r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
230             odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest));
231
232         toKeep = r->u.adminService->u.esRequest->toKeep =
233             (Z_ESAdminOriginPartToKeep *)
234             odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep));
235         toKeep->which = type;
236         toKeep->databaseName = odr_strdup(p->odr_out, first_db);
237         toKeep->u.create = odr_nullval();
238         apdu->u.extendedServicesRequest->taskSpecificParameters = r;
239
240         r->u.adminService->u.esRequest->notToKeep = notToKeep =
241             (Z_ESAdminOriginPartNotToKeep *)
242             odr_malloc(p->odr_out,
243                        sizeof(*r->u.adminService->u.esRequest->notToKeep));
244         notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
245         notToKeep->u.recordsWillFollow = odr_nullval();
246         if (toKeepP)
247             *toKeepP = toKeep;
248         if (notToKeepP)
249             *notToKeepP = notToKeep;
250     }
251     return apdu;
252 }
253
254 static Z_APDU *create_xmlupdate_package(ZOOM_package p)
255 {
256     Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_xml_es);
257     Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
258     Z_External *ext = (Z_External *) odr_malloc(p->odr_out, sizeof(*ext));
259     int len;
260     const char *doc = ZOOM_options_getl(p->options, "doc", &len);
261
262     if (!doc)
263     {
264         doc = "";
265         len = 0;
266     }
267
268     req->taskSpecificParameters = ext;
269     ext->direct_reference = req->packageType;
270     ext->descriptor = 0;
271     ext->indirect_reference = 0;
272
273     ext->which = Z_External_octet;
274     ext->u.single_ASN1_type = odr_create_Odr_oct(p->odr_out, doc, len);
275     return apdu;
276 }
277
278 static Z_APDU *create_update_package(ZOOM_package p)
279 {
280     Z_APDU *apdu = 0;
281     const char *first_db = "Default";
282     int num_db;
283     char **db = ZOOM_connection_get_databases(p->connection, p->options,
284                                               &num_db, p->odr_out);
285     const char *action = ZOOM_options_get(p->options, "action");
286     int recordIdOpaque_len;
287     const char *recordIdOpaque = ZOOM_options_getl(p->options, "recordIdOpaque",
288         &recordIdOpaque_len);
289     const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
290     int record_len;
291     const char *record_buf = ZOOM_options_getl(p->options, "record",
292         &record_len);
293     int recordOpaque_len;
294     const char *recordOpaque_buf = ZOOM_options_getl(p->options, "recordOpaque",
295         &recordOpaque_len);
296     const char *syntax_str = ZOOM_options_get(p->options, "syntax");
297     const char *version = ZOOM_options_get(p->options, "updateVersion");
298
299     const char *correlationInfo_note =
300         ZOOM_options_get(p->options, "correlationInfo.note");
301     const char *correlationInfo_id =
302         ZOOM_options_get(p->options, "correlationInfo.id");
303     int action_no = -1;
304     Odr_oid *syntax_oid = 0;
305     const Odr_oid *package_oid = yaz_oid_extserv_database_update;
306
307     if (!version)
308         version = "3";
309     if (!syntax_str)
310         syntax_str = "xml";
311     if (!record_buf && !recordOpaque_buf)
312     {
313         record_buf = "void";
314         record_len = 4;
315         syntax_str = "SUTRS";
316     }
317
318     if (syntax_str)
319     {
320         syntax_oid = yaz_string_to_oid_odr(yaz_oid_std(),
321                                            CLASS_RECSYN, syntax_str,
322                                            p->odr_out);
323     }
324     if (!syntax_oid)
325     {
326         ZOOM_set_error(p->connection, ZOOM_ERROR_ES_INVALID_SYNTAX, syntax_str);
327         return 0;
328     }
329
330     if (num_db > 0)
331         first_db = db[0];
332
333     switch (*version)
334     {
335     case '1':
336         package_oid = yaz_oid_extserv_database_update_first_version;
337         /* old update does not support specialUpdate */
338         if (!action)
339             action = "recordInsert";
340         break;
341     case '2':
342         if (!action)
343             action = "specialUpdate";
344         package_oid = yaz_oid_extserv_database_update_second_version;
345         break;
346     case '3':
347         if (!action)
348             action = "specialUpdate";
349         package_oid = yaz_oid_extserv_database_update;
350         break;
351     default:
352         ZOOM_set_error(p->connection, ZOOM_ERROR_ES_INVALID_VERSION, version);
353         return 0;
354     }
355
356     if (!strcmp(action, "recordInsert"))
357         action_no = Z_IUOriginPartToKeep_recordInsert;
358     else if (!strcmp(action, "recordReplace"))
359         action_no = Z_IUOriginPartToKeep_recordReplace;
360     else if (!strcmp(action, "recordDelete"))
361         action_no = Z_IUOriginPartToKeep_recordDelete;
362     else if (!strcmp(action, "elementUpdate"))
363         action_no = Z_IUOriginPartToKeep_elementUpdate;
364     else if (!strcmp(action, "specialUpdate"))
365         action_no = Z_IUOriginPartToKeep_specialUpdate;
366     else
367     {
368         ZOOM_set_error(p->connection, ZOOM_ERROR_ES_INVALID_ACTION, action);
369         return 0;
370     }
371
372     apdu = create_es_package(p, package_oid);
373     if (apdu)
374     {
375         Z_IUOriginPartToKeep *toKeep;
376         Z_IUSuppliedRecords *notToKeep;
377         Z_External *r = (Z_External *)
378             odr_malloc(p->odr_out, sizeof(*r));
379         const char *elementSetName =
380             ZOOM_options_get(p->options, "elementSetName");
381
382         apdu->u.extendedServicesRequest->taskSpecificParameters = r;
383
384         r->direct_reference = odr_oiddup(p->odr_out, package_oid);
385         r->descriptor = 0;
386         r->which = Z_External_update;
387         r->indirect_reference = 0;
388         r->u.update = (Z_IUUpdate *)
389             odr_malloc(p->odr_out, sizeof(*r->u.update));
390
391         r->u.update->which = Z_IUUpdate_esRequest;
392         r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
393             odr_malloc(p->odr_out, sizeof(*r->u.update->u.esRequest));
394         toKeep = r->u.update->u.esRequest->toKeep =
395             (Z_IUOriginPartToKeep *)
396             odr_malloc(p->odr_out, sizeof(*toKeep));
397
398         toKeep->databaseName = odr_strdup(p->odr_out, first_db);
399         toKeep->schema = 0;
400
401         toKeep->elementSetName = odr_strdup_null(p->odr_out, elementSetName);
402
403         toKeep->actionQualifier = 0;
404         toKeep->action = odr_intdup(p->odr_out, action_no);
405
406         notToKeep = r->u.update->u.esRequest->notToKeep =
407             (Z_IUSuppliedRecords *)
408             odr_malloc(p->odr_out, sizeof(*notToKeep));
409         notToKeep->num = 1;
410         notToKeep->elements = (Z_IUSuppliedRecords_elem **)
411             odr_malloc(p->odr_out, sizeof(*notToKeep->elements));
412         notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
413             odr_malloc(p->odr_out, sizeof(**notToKeep->elements));
414         notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
415         if (recordIdOpaque)
416         {
417             notToKeep->elements[0]->u.opaque =
418                 odr_create_Odr_oct(p->odr_out, recordIdOpaque,
419                                    recordIdOpaque_len);
420         }
421         else if (recordIdNumber)
422         {
423             notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number;
424
425             notToKeep->elements[0]->u.number =
426                 odr_intdup(p->odr_out, atoi(recordIdNumber));
427         }
428         else
429             notToKeep->elements[0]->u.opaque = 0;
430         notToKeep->elements[0]->supplementalId = 0;
431         if (correlationInfo_note || correlationInfo_id)
432         {
433             Z_IUCorrelationInfo *ci;
434             ci = notToKeep->elements[0]->correlationInfo =
435                 (Z_IUCorrelationInfo *) odr_malloc(p->odr_out, sizeof(*ci));
436             ci->note = odr_strdup_null(p->odr_out, correlationInfo_note);
437             ci->id = correlationInfo_id ?
438                 odr_intdup(p->odr_out, atoi(correlationInfo_id)) : 0;
439         }
440         else
441             notToKeep->elements[0]->correlationInfo = 0;
442         if (recordOpaque_buf)
443         {
444             notToKeep->elements[0]->record =
445                 z_ext_record_oid_any(p->odr_out, syntax_oid,
446                                  recordOpaque_buf, recordOpaque_len);
447         }
448         else
449         {
450             notToKeep->elements[0]->record =
451                 z_ext_record_oid(p->odr_out, syntax_oid,
452                                  record_buf, record_len);
453         }
454     }
455     if (0 && apdu)
456     {
457         ODR print = odr_createmem(ODR_PRINT);
458
459         z_APDU(print, &apdu, 0, 0);
460         odr_destroy(print);
461     }
462     return apdu;
463 }
464
465
466 static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
467 {
468     int i;
469     for (i = 0; i < 200; i++)
470     {
471         size_t len;
472         Odr_oid *oid;
473         Z_OtherInformation **oi;
474         char buf[80];
475         const char *val;
476         const char *cp;
477
478         sprintf(buf, "otherInfo%d", i);
479         val = ZOOM_options_get(c->options, buf);
480         if (!val)
481             break;
482         cp = strchr(val, ':');
483         if (!cp)
484             continue;
485         len = cp - val;
486         if (len >= sizeof(buf))
487             len = sizeof(buf)-1;
488         memcpy(buf, val, len);
489         buf[len] = '\0';
490
491         oid = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_USERINFO,
492                                     buf, out);
493         if (!oid)
494             continue;
495
496         yaz_oi_APDU(a, &oi);
497         yaz_oi_set_string_oid(oi, out, oid, 1, cp+1);
498     }
499 }
500
501
502
503 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
504 {
505     assert(a);
506     if (c->cookie_out)
507     {
508         Z_OtherInformation **oi;
509         yaz_oi_APDU(a, &oi);
510         yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_cookie,
511                               1, c->cookie_out);
512     }
513     if (c->client_IP)
514     {
515         Z_OtherInformation **oi;
516         yaz_oi_APDU(a, &oi);
517         yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_client_ip,
518                               1, c->client_IP);
519     }
520     otherInfo_attach(c, a, out);
521     if (!z_APDU(out, &a, 0, 0))
522     {
523         FILE *outf = fopen("/tmp/apdu.txt", "a");
524         if (a && outf)
525         {
526             ODR odr_pr = odr_createmem(ODR_PRINT);
527             fprintf(outf, "a=%p\n", a);
528             odr_setprint(odr_pr, outf);
529             z_APDU(odr_pr, &a, 0, 0);
530             odr_destroy(odr_pr);
531         }
532         yaz_log(c->log_api, "%p encoding_APDU: encoding failed", c);
533         ZOOM_set_error(c, ZOOM_ERROR_ENCODE, 0);
534         odr_reset(out);
535         return -1;
536     }
537     if (c->odr_print)
538         z_APDU(c->odr_print, &a, 0, 0);
539     if (c->odr_save)
540         z_APDU(c->odr_save, &a, 0, 0);
541     yaz_log(c->log_details, "%p encoding_APDU encoding OK", c);
542     return 0;
543 }
544
545 static zoom_ret send_APDU(ZOOM_connection c, Z_APDU *a)
546 {
547     ZOOM_Event event;
548     assert(a);
549     if (encode_APDU(c, a, c->odr_out))
550         return zoom_complete;
551     yaz_log(c->log_details, "%p send APDU type=%d", c, a->which);
552     c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
553     event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
554     ZOOM_connection_put_event(c, event);
555     odr_reset(c->odr_out);
556     return ZOOM_send_buf(c);
557 }
558
559 zoom_ret ZOOM_connection_Z3950_send_init(ZOOM_connection c)
560 {
561     Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
562     Z_InitRequest *ireq = apdu->u.initRequest;
563     Z_IdAuthentication *auth = (Z_IdAuthentication *)
564         odr_malloc(c->odr_out, sizeof(*auth));
565
566     ODR_MASK_SET(ireq->options, Z_Options_search);
567     ODR_MASK_SET(ireq->options, Z_Options_present);
568     ODR_MASK_SET(ireq->options, Z_Options_scan);
569     ODR_MASK_SET(ireq->options, Z_Options_sort);
570     ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
571     ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
572
573     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
574     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
575     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
576
577     ireq->implementationId =
578         odr_prepend(c->odr_out,
579                     ZOOM_options_get(c->options, "implementationId"),
580                     ireq->implementationId);
581
582     ireq->implementationName =
583         odr_prepend(c->odr_out,
584                     ZOOM_options_get(c->options, "implementationName"),
585                     odr_prepend(c->odr_out, "ZOOM-C",
586                                 ireq->implementationName));
587
588     ireq->implementationVersion =
589         odr_prepend(c->odr_out,
590                     ZOOM_options_get(c->options, "implementationVersion"),
591                                 ireq->implementationVersion);
592
593     *ireq->maximumRecordSize = c->maximum_record_size;
594     *ireq->preferredMessageSize = c->preferred_message_size;
595
596     if (c->group || c->password)
597     {
598         Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
599         pass->groupId = odr_strdup_null(c->odr_out, c->group);
600         pass->userId = odr_strdup_null(c->odr_out, c->user);
601         pass->password = odr_strdup_null(c->odr_out, c->password);
602         auth->which = Z_IdAuthentication_idPass;
603         auth->u.idPass = pass;
604         ireq->idAuthentication = auth;
605     }
606     else if (c->user)
607     {
608         auth->which = Z_IdAuthentication_open;
609         auth->u.open = odr_strdup(c->odr_out, c->user);
610         ireq->idAuthentication = auth;
611     }
612     if (c->proxy)
613     {
614         yaz_oi_set_string_oid(&ireq->otherInfo, c->odr_out,
615                               yaz_oid_userinfo_proxy, 1, c->host_port);
616     }
617     if (c->charset || c->lang)
618     {
619         Z_OtherInformation **oi;
620         Z_OtherInformationUnit *oi_unit;
621
622         yaz_oi_APDU(apdu, &oi);
623
624         if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
625         {
626             ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
627             oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
628             oi_unit->information.externallyDefinedInfo =
629                 yaz_set_proposal_charneg_list(c->odr_out, " ",
630                                               c->charset, c->lang, 1);
631         }
632     }
633     assert(apdu);
634     return send_APDU(c, apdu);
635 }
636
637 static zoom_ret Z3950_send_search(ZOOM_connection c)
638 {
639     ZOOM_resultset r;
640     int lslb, ssub, mspn;
641     const char *syntax;
642     const char *schema;
643     Z_APDU *apdu;
644     Z_SearchRequest *search_req;
645     const char *elementSetName;
646     const char *smallSetElementSetName;
647     const char *mediumSetElementSetName;
648
649     assert(c->tasks);
650     assert(c->tasks->which == ZOOM_TASK_SEARCH);
651     r = c->tasks->u.search.resultset;
652
653     apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
654     search_req = apdu->u.searchRequest;
655
656     yaz_log(c->log_details, "%p Z3950_send_search set=%p", c, r);
657
658     elementSetName = c->tasks->u.search.elementSetName;
659     smallSetElementSetName  =
660         ZOOM_options_get(r->options, "smallSetElementSetName");
661     mediumSetElementSetName =
662         ZOOM_options_get(r->options, "mediumSetElementSetName");
663
664     if (!smallSetElementSetName)
665         smallSetElementSetName = elementSetName;
666
667     if (!mediumSetElementSetName)
668         mediumSetElementSetName = elementSetName;
669
670     if (r->req_facets)
671     {
672         Z_FacetList *facet_list =
673             yaz_pqf_parse_facet_list(c->odr_out, r->req_facets);
674         if (facet_list)
675         {
676             Z_OtherInformation **oi = &search_req->additionalSearchInfo;
677             yaz_oi_set_facetlist(oi, c->odr_out, facet_list);
678         }
679         else
680             yaz_log(YLOG_WARN, "Unable to parse facets: %s", r->req_facets);
681     }
682
683     assert(r);
684     assert(r->query);
685
686     /* prepare query for the search request */
687     search_req->query = ZOOM_query_get_Z_Query(r->query);
688     if (!search_req->query)
689     {
690         ZOOM_set_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
691         return zoom_complete;
692     }
693     if (search_req->query->which == Z_Query_type_1 ||
694         search_req->query->which == Z_Query_type_101)
695     {
696         const char *cp = ZOOM_options_get(r->options, "rpnCharset");
697         if (cp)
698         {
699             yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
700             if (cd)
701             {
702                 int r;
703                 search_req->query = yaz_copy_Z_Query(search_req->query,
704                                                      c->odr_out);
705
706                 r = yaz_query_charset_convert_rpnquery_check(
707                     search_req->query->u.type_1,
708                     c->odr_out, cd);
709                 yaz_iconv_close(cd);
710                 if (r)
711                 {  /* query could not be char converted */
712                     ZOOM_set_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
713                     return zoom_complete;
714                 }
715             }
716         }
717     }
718     search_req->databaseNames = r->databaseNames;
719     search_req->num_databaseNames = r->num_databaseNames;
720
721     /* get syntax (no need to provide unless piggyback is in effect) */
722     syntax = c->tasks->u.search.syntax;
723
724     schema = c->tasks->u.search.schema;
725
726     lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
727     ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
728     mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
729     if (lslb != -1 && ssub != -1 && mspn != -1)
730     {
731         /* So're a Z39.50 expert? Let's hope you don't do sort */
732         *search_req->largeSetLowerBound = lslb;
733         *search_req->smallSetUpperBound = ssub;
734         *search_req->mediumSetPresentNumber = mspn;
735     }
736     else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
737              && r->piggyback && !r->r_sort_spec && !schema)
738     {
739         /* Regular piggyback - do it unless we're going to do sort */
740         *search_req->largeSetLowerBound = 2000000000;
741         *search_req->smallSetUpperBound = 1;
742         *search_req->mediumSetPresentNumber =
743             r->step>0 ? r->step : c->tasks->u.search.count;
744     }
745     else
746     {
747         /* non-piggyback. Need not provide elementsets or syntaxes .. */
748         smallSetElementSetName = 0;
749         mediumSetElementSetName = 0;
750         syntax = 0;
751     }
752     if (smallSetElementSetName && *smallSetElementSetName)
753     {
754         Z_ElementSetNames *esn = (Z_ElementSetNames *)
755             odr_malloc(c->odr_out, sizeof(*esn));
756
757         esn->which = Z_ElementSetNames_generic;
758         esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
759         search_req->smallSetElementSetNames = esn;
760     }
761     if (mediumSetElementSetName && *mediumSetElementSetName)
762     {
763         Z_ElementSetNames *esn =(Z_ElementSetNames *)
764             odr_malloc(c->odr_out, sizeof(*esn));
765
766         esn->which = Z_ElementSetNames_generic;
767         esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
768         search_req->mediumSetElementSetNames = esn;
769     }
770     if (syntax)
771         search_req->preferredRecordSyntax =
772             zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
773
774     if (!r->setname)
775     {
776         if (c->support_named_resultsets)
777         {
778             char setname[14];
779             int ord;
780             /* find the lowest unused ordinal so that we re-use
781                result sets on the server. */
782             for (ord = 1; ; ord++)
783             {
784                 ZOOM_resultset rp;
785                 sprintf(setname, "%d", ord);
786                 for (rp = c->resultsets; rp; rp = rp->next)
787                     if (rp->setname && !strcmp(rp->setname, setname))
788                         break;
789                 if (!rp)
790                     break;
791             }
792             r->setname = odr_strdup(r->odr, setname);
793             yaz_log(c->log_details, "%p ZOOM_connection_send_search: "
794                     "allocating set %s", c, r->setname);
795         }
796         else
797         {
798             yaz_log(c->log_details, "%p ZOOM_connection_send_search: using "
799                     "default set", c);
800             r->setname = odr_strdup(r->odr, "default");
801         }
802         ZOOM_options_set(r->options, "setname", r->setname);
803     }
804     search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
805     return send_APDU(c, apdu);
806 }
807
808 zoom_ret ZOOM_connection_Z3950_send_scan(ZOOM_connection c)
809 {
810     ZOOM_scanset scan;
811     Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
812     Z_ScanRequest *req = apdu->u.scanRequest;
813     Z_Query *z_query;
814
815     yaz_log(c->log_details, "%p send_scan", c);
816     if (!c->tasks)
817         return zoom_complete;
818     assert (c->tasks->which == ZOOM_TASK_SCAN);
819     scan = c->tasks->u.scan.scan;
820
821     z_query = ZOOM_query_get_Z_Query(scan->query);
822
823     /* Z39.50 scan can only carry RPN */
824     if (z_query->which == Z_Query_type_1 ||
825         z_query->which == Z_Query_type_101)
826     {
827         Z_RPNQuery *rpn = z_query->u.type_1;
828         const char *cp = ZOOM_options_get(scan->options, "rpnCharset");
829         if (cp)
830         {
831             yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
832             if (cd)
833             {
834                 rpn = yaz_copy_z_RPNQuery(rpn, c->odr_out);
835
836                 yaz_query_charset_convert_rpnquery(
837                     rpn, c->odr_out, cd);
838                 yaz_iconv_close(cd);
839             }
840         }
841         req->attributeSet = rpn->attributeSetId;
842         if (!req->attributeSet)
843             req->attributeSet = odr_oiddup(c->odr_out, yaz_oid_attset_bib_1);
844         if (rpn->RPNStructure->which == Z_RPNStructure_simple &&
845             rpn->RPNStructure->u.simple->which == Z_Operand_APT)
846         {
847             req->termListAndStartPoint =
848                 rpn->RPNStructure->u.simple->u.attributesPlusTerm;
849         }
850         else
851         {
852             ZOOM_set_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
853             return zoom_complete;
854         }
855     }
856     else
857     {
858         ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
859         return zoom_complete;
860     }
861
862     *req->numberOfTermsRequested =
863         ZOOM_options_get_int(scan->options, "number", 20);
864
865     req->preferredPositionInResponse =
866         odr_intdup(c->odr_out,
867                    ZOOM_options_get_int(scan->options, "position", 1));
868
869     req->stepSize =
870         odr_intdup(c->odr_out,
871                    ZOOM_options_get_int(scan->options, "stepSize", 0));
872
873     req->databaseNames = scan->databaseNames;
874     req->num_databaseNames = scan->num_databaseNames;
875
876     return send_APDU(c, apdu);
877 }
878
879 ZOOM_API(void)
880     ZOOM_package_send(ZOOM_package p, const char *type)
881 {
882     Z_APDU *apdu = 0;
883     ZOOM_connection c;
884     if (!p)
885         return;
886     c = p->connection;
887     odr_reset(p->odr_out);
888     xfree(p->buf_out);
889     p->buf_out = 0;
890     if (!strcmp(type, "itemorder"))
891     {
892         apdu = create_es_package(p, yaz_oid_extserv_item_order);
893         if (apdu)
894         {
895             Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
896
897             r->direct_reference =
898                 odr_oiddup(p->odr_out, yaz_oid_extserv_item_order);
899             r->descriptor = 0;
900             r->which = Z_External_itemOrder;
901             r->indirect_reference = 0;
902             r->u.itemOrder = encode_item_order(p);
903
904             apdu->u.extendedServicesRequest->taskSpecificParameters = r;
905         }
906     }
907     else if (!strcmp(type, "create"))  /* create database */
908     {
909         apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
910                                     0, 0);
911     }
912     else if (!strcmp(type, "drop"))  /* drop database */
913     {
914         apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
915                                     0, 0);
916     }
917     else if (!strcmp(type, "commit"))  /* commit changes */
918     {
919         apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit,
920                                     0, 0);
921     }
922     else if (!strcmp(type, "update")) /* update record(s) */
923     {
924         apdu = create_update_package(p);
925     }
926     else if (!strcmp(type, "xmlupdate"))
927     {
928         apdu = create_xmlupdate_package(p);
929     }
930     if (apdu)
931     {
932         if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
933         {
934             char *buf;
935
936             ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_PACKAGE);
937             task->u.package = p;
938             buf = odr_getbuf(p->odr_out, &p->len_out, 0);
939             p->buf_out = (char *) xmalloc(p->len_out);
940             memcpy(p->buf_out, buf, p->len_out);
941
942             (p->refcount)++;
943             if (!c->async)
944             {
945                 while (ZOOM_event(1, &c))
946                     ;
947             }
948         }
949     }
950 }
951
952 static void handle_Z3950_records(ZOOM_connection c, Z_Records *sr,
953                                  int present_phase);
954
955 static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)
956 {
957     char oid_name_buf[OID_STR_MAX];
958     const char *oid_name;
959     char *addinfo = 0;
960
961     oid_name = yaz_oid_to_string_buf(r->diagnosticSetId, 0, oid_name_buf);
962     switch (r->which)
963     {
964     case Z_DefaultDiagFormat_v2Addinfo:
965         addinfo = r->u.v2Addinfo;
966         break;
967     case Z_DefaultDiagFormat_v3Addinfo:
968         addinfo = r->u.v3Addinfo;
969         break;
970     }
971     xfree(c->addinfo);
972     c->addinfo = 0;
973     ZOOM_set_dset_error(c, *r->condition, oid_name, addinfo, 0);
974 }
975
976 static void response_diag(ZOOM_connection c, Z_DiagRec *p)
977 {
978     if (p->which != Z_DiagRec_defaultFormat)
979         ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
980     else
981         response_default_diag(c, p->u.defaultFormat);
982 }
983
984 static int es_response_taskpackage_update(ZOOM_connection c,
985                                           Z_IUUpdateTaskPackage *utp)
986 {
987     if (utp && utp->targetPart)
988     {
989         Z_IUTargetPart *targetPart = utp->targetPart;
990         switch (*targetPart->updateStatus)
991         {
992         case Z_IUTargetPart_success:
993             ZOOM_options_set(c->tasks->u.package->options, 
994                              "updateStatus", "success");
995             break;
996         case Z_IUTargetPart_partial:
997             ZOOM_options_set(c->tasks->u.package->options,
998                              "updateStatus", "partial");
999             break;
1000         case Z_IUTargetPart_failure:
1001             ZOOM_options_set(c->tasks->u.package->options,
1002                              "updateStatus", "failure");
1003             if (targetPart->globalDiagnostics &&
1004                 targetPart->num_globalDiagnostics > 0)
1005                 response_diag(c, targetPart->globalDiagnostics[0]);
1006             break;
1007         }
1008         /* NOTE: Individual record status, surrogate diagnostics, and supplemental diagnostics ARE NOT REPORTED. */
1009     }
1010     return 1;
1011 }
1012
1013 static int es_response_taskpackage(ZOOM_connection c,
1014                                    Z_TaskPackage *taskPackage)
1015 {
1016     Odr_oct *id = taskPackage->targetReference;
1017     if (id)
1018         ZOOM_options_setl(c->tasks->u.package->options,
1019                           "targetReference", (char*) id->buf, id->len);
1020     switch (*taskPackage->taskStatus)
1021     {
1022     case Z_TaskPackage_pending:
1023         ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "pending");
1024         break;
1025     case Z_TaskPackage_active:
1026         ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "active");
1027         break;
1028     case Z_TaskPackage_complete:
1029         ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "complete");
1030         break;
1031     case Z_TaskPackage_aborted:
1032         ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "aborted");
1033         if (taskPackage->num_packageDiagnostics &&
1034             taskPackage->packageDiagnostics )
1035             response_diag(c, taskPackage->packageDiagnostics[0]);
1036         break;
1037     }
1038     /* NOTE: Only Update implemented, no others. */
1039     if (taskPackage->taskSpecificParameters->which == Z_External_update)
1040     {
1041         Z_IUUpdateTaskPackage *utp =
1042             taskPackage->taskSpecificParameters->u.update->u.taskPackage;
1043         es_response_taskpackage_update(c, utp);
1044     }
1045     return 1;
1046 }
1047
1048 static void handle_Z3950_es_response(ZOOM_connection c,
1049                                      Z_ExtendedServicesResponse *res)
1050 {
1051     if (!c->tasks)
1052         return;
1053     assert(c->tasks->which == ZOOM_TASK_PACKAGE);
1054     switch (*res->operationStatus)
1055     {
1056     case Z_ExtendedServicesResponse_done:
1057         ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "done");
1058         break;
1059     case Z_ExtendedServicesResponse_accepted:
1060         ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "accepted");
1061         break;
1062     case Z_ExtendedServicesResponse_failure:
1063         ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "failure");
1064         if (res->diagnostics && res->num_diagnostics > 0)
1065             response_diag(c, res->diagnostics[0]);
1066         break;
1067     }
1068     if (res->taskPackage &&
1069         res->taskPackage->which == Z_External_extendedService)
1070     {
1071         Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1072         es_response_taskpackage(c, taskPackage);
1073     }
1074     if (res->taskPackage &&
1075         res->taskPackage->which == Z_External_octet)
1076     {
1077         Odr_oct *doc = res->taskPackage->u.octet_aligned;
1078         ZOOM_options_setl(c->tasks->u.package->options,
1079                           "xmlUpdateDoc", (char*) doc->buf, doc->len);
1080     }
1081 }
1082
1083 static char *get_term_cstr(ODR odr, Z_Term *term)
1084 {
1085     switch (term->which)
1086     {
1087     case Z_Term_general:
1088         return odr_strdupn(odr, term->u.general->buf, term->u.general->len);
1089         break;
1090     case Z_Term_characterString:
1091         return odr_strdup(odr, term->u.characterString);
1092     }
1093     return 0;
1094 }
1095
1096 static ZOOM_facet_field get_zoom_facet_field(ODR odr, Z_FacetField *facet)
1097 {
1098     int i;
1099     struct yaz_facet_attr attr_values;
1100     ZOOM_facet_field facet_field = odr_malloc(odr, sizeof(*facet_field));
1101     yaz_facet_attr_init(&attr_values);
1102     yaz_facet_attr_get_z_attributes(facet->attributes, &attr_values);
1103     facet_field->facet_name = odr_strdup(odr, attr_values.useattr);
1104     facet_field->num_terms = facet->num_terms;
1105     yaz_log(YLOG_DEBUG, "ZOOM_facet_field %s %d terms %d",
1106             attr_values.useattr, attr_values.limit, facet->num_terms);
1107     facet_field->facet_terms =
1108         odr_malloc(odr, facet->num_terms * sizeof(*facet_field->facet_terms));
1109     for (i = 0 ; i < facet->num_terms; i++)
1110     {
1111         Z_FacetTerm *facetTerm = facet->terms[i];
1112         facet_field->facet_terms[i].frequency = *facetTerm->count;
1113         facet_field->facet_terms[i].term = get_term_cstr(odr, facetTerm->term);
1114         yaz_log(YLOG_DEBUG, "    term[%d] %s %d",
1115                 i, facet_field->facet_terms[i].term,
1116                 facet_field->facet_terms[i].frequency);
1117     }
1118     return facet_field;
1119 }
1120
1121 /* Can be share with SOLR/SRU/SRW requests */
1122 void ZOOM_handle_facet_list(ZOOM_resultset r, Z_FacetList *fl)
1123 {
1124     int j;
1125     r->num_res_facets = fl->num;
1126     yaz_log(YLOG_DEBUG, "Facets found: %d", fl->num);
1127     r->res_facets =  odr_malloc(r->odr, fl->num * sizeof(*r->res_facets));
1128     r->facets_names =  odr_malloc(r->odr, fl->num * sizeof(*r->facets_names));
1129     for (j = 0; j < fl->num; j++)
1130     {
1131         r->res_facets[j] = get_zoom_facet_field(r->odr, fl->elements[j]);
1132         if (!r->res_facets[j])
1133         {
1134             r->facets_names[j] = 0;
1135             yaz_log(YLOG_DEBUG, "Facet field missing on index %d !", j);
1136         }
1137         else
1138             r->facets_names[j] = (char *)
1139                 ZOOM_facet_field_name(r->res_facets[j]);
1140     }
1141 }
1142
1143 void ZOOM_handle_facet_result(ZOOM_connection c, ZOOM_resultset r,
1144                               Z_OtherInformation *o)
1145 {
1146     int i;
1147     for (i = 0; o && i < o->num_elements; i++)
1148     {
1149         if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
1150         {
1151             Z_External *ext = o->list[i]->information.externallyDefinedInfo;
1152             if (ext->which == Z_External_userFacets)
1153             {
1154                 ZOOM_handle_facet_list(r, ext->u.facetList);
1155             }
1156         }
1157     }
1158 }
1159
1160 static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
1161                                        Z_Term *term)
1162 {
1163     switch (term->which)
1164     {
1165     case Z_Term_general:
1166         ZOOM_options_setl(opt, name,
1167                           term->u.general->buf, term->u.general->len);
1168         break;
1169     case Z_Term_characterString:
1170         ZOOM_options_set(opt, name, term->u.characterString);
1171         break;
1172     case Z_Term_numeric:
1173         ZOOM_options_set_int(opt, name, *term->u.numeric);
1174         break;
1175     }
1176 }
1177
1178 static void handle_queryExpression(ZOOM_options opt, const char *name,
1179                                    Z_QueryExpression *exp)
1180 {
1181     char opt_name[80];
1182
1183     switch (exp->which)
1184     {
1185     case Z_QueryExpression_term:
1186         if (exp->u.term && exp->u.term->queryTerm)
1187         {
1188             sprintf(opt_name, "%s.term", name);
1189             handle_queryExpressionTerm(opt, opt_name, exp->u.term->queryTerm);
1190         }
1191         break;
1192     case Z_QueryExpression_query:
1193         break;
1194     }
1195 }
1196
1197
1198 void ZOOM_handle_search_result(ZOOM_connection c, ZOOM_resultset resultset,
1199                                Z_OtherInformation *o)
1200 {
1201     int i;
1202     for (i = 0; o && i < o->num_elements; i++)
1203     {
1204         if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
1205         {
1206             Z_External *ext = o->list[i]->information.externallyDefinedInfo;
1207
1208             if (ext->which == Z_External_searchResult1)
1209             {
1210                 int j;
1211                 Z_SearchInfoReport *sr = ext->u.searchResult1;
1212
1213                 if (sr->num)
1214                     ZOOM_options_set_int(
1215                         resultset->options, "searchresult.size", sr->num);
1216
1217                 for (j = 0; j < sr->num; j++)
1218                 {
1219                     Z_SearchInfoReport_s *ent =
1220                         ext->u.searchResult1->elements[j];
1221                     char pref[80];
1222
1223                     sprintf(pref, "searchresult.%d", j);
1224
1225                     if (ent->subqueryId)
1226                     {
1227                         char opt_name[80];
1228                         sprintf(opt_name, "%s.id", pref);
1229                         ZOOM_options_set(resultset->options, opt_name,
1230                                          ent->subqueryId);
1231                     }
1232                     if (ent->subqueryExpression)
1233                     {
1234                         char opt_name[80];
1235                         sprintf(opt_name, "%s.subquery", pref);
1236                         handle_queryExpression(resultset->options, opt_name,
1237                                                ent->subqueryExpression);
1238                     }
1239                     if (ent->subqueryInterpretation)
1240                     {
1241                         char opt_name[80];
1242                         sprintf(opt_name, "%s.interpretation", pref);
1243                         handle_queryExpression(resultset->options, opt_name,
1244                                                ent->subqueryInterpretation);
1245                     }
1246                     if (ent->subqueryRecommendation)
1247                     {
1248                         char opt_name[80];
1249                         sprintf(opt_name, "%s.recommendation", pref);
1250                         handle_queryExpression(resultset->options, opt_name,
1251                                                ent->subqueryRecommendation);
1252                     }
1253                     if (ent->subqueryCount)
1254                     {
1255                         char opt_name[80];
1256                         sprintf(opt_name, "%s.count", pref);
1257                         ZOOM_options_set_int(resultset->options, opt_name,
1258                                              *ent->subqueryCount);
1259                     }
1260                 }
1261             }
1262         }
1263     }
1264 }
1265
1266 static void handle_Z3950_search_response(ZOOM_connection c,
1267                                          Z_SearchResponse *sr)
1268 {
1269     ZOOM_resultset resultset;
1270     ZOOM_Event event;
1271     const char *resultCountPrecision = "exact";
1272
1273     if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1274         return;
1275
1276     resultset = c->tasks->u.search.resultset;
1277
1278     if (resultset->live_set == 0)
1279     {
1280         event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
1281         ZOOM_connection_put_event(c, event);
1282     }
1283     if (sr->resultSetStatus)
1284     {
1285         if (*sr->resultSetStatus == Z_SearchResponse_estimate)
1286             resultCountPrecision = "estimate";
1287         ZOOM_options_set_int(resultset->options, "resultSetStatus",
1288                              *sr->resultSetStatus);
1289     }
1290     if (sr->presentStatus)
1291     {
1292         ZOOM_options_set_int(resultset->options, "presentStatus",
1293                              *sr->presentStatus);
1294     }
1295     ZOOM_options_set(resultset->options, "resultCountPrecision",
1296                      resultCountPrecision);
1297     ZOOM_handle_search_result(c, resultset, sr->additionalSearchInfo);
1298
1299     ZOOM_handle_facet_result(c, resultset, sr->additionalSearchInfo);
1300
1301     resultset->size = *sr->resultCount;
1302
1303     ZOOM_memcached_hitcount(c, resultset, sr->additionalSearchInfo,
1304                             resultCountPrecision);
1305     resultset->live_set = 2;
1306     handle_Z3950_records(c, sr->records, 0);
1307 }
1308
1309 static void handle_Z3950_sort_response(ZOOM_connection c, Z_SortResponse *res)
1310 {
1311     if (res->diagnostics && res->num_diagnostics > 0)
1312         response_diag(c, res->diagnostics[0]);
1313 }
1314
1315 static void handle_Z3950_scan_response(ZOOM_connection c, Z_ScanResponse *res)
1316 {
1317     NMEM nmem = odr_extract_mem(c->odr_in);
1318     ZOOM_scanset scan;
1319
1320     if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1321         return;
1322     scan = c->tasks->u.scan.scan;
1323
1324     if (res->entries && res->entries->nonsurrogateDiagnostics)
1325         response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1326     scan->scan_response = res;
1327     scan->srw_scan_response = 0;
1328     nmem_transfer(odr_getmem(scan->odr), nmem);
1329     if (res->stepSize)
1330         ZOOM_options_set_int(scan->options, "stepSize", *res->stepSize);
1331     if (res->positionOfTerm)
1332         ZOOM_options_set_int(scan->options, "position", *res->positionOfTerm);
1333     if (res->scanStatus)
1334         ZOOM_options_set_int(scan->options, "scanStatus", *res->scanStatus);
1335     if (res->numberOfEntriesReturned)
1336         ZOOM_options_set_int(scan->options, "number",
1337                              *res->numberOfEntriesReturned);
1338     nmem_destroy(nmem);
1339 }
1340
1341 static void handle_Z3950_records(ZOOM_connection c, Z_Records *sr,
1342                                  int present_phase)
1343 {
1344     ZOOM_resultset resultset;
1345     int *start, *count;
1346     const char *syntax = 0, *elementSetName = 0, *schema = 0;
1347
1348     if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1349         return ;
1350
1351     resultset = c->tasks->u.search.resultset;
1352     start = &c->tasks->u.search.start;
1353     count = &c->tasks->u.search.count;
1354     syntax = c->tasks->u.search.syntax;
1355     elementSetName = c->tasks->u.search.elementSetName;
1356     schema =  c->tasks->u.search.schema;
1357
1358     if (sr && sr->which == Z_Records_NSD)
1359         response_default_diag(c, sr->u.nonSurrogateDiagnostic);
1360     else if (sr && sr->which == Z_Records_multipleNSD)
1361     {
1362         if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1363             response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1364         else
1365             ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1366     }
1367     else
1368     {
1369         if (*count + *start > resultset->size)
1370             *count = resultset->size - *start;
1371         if (*count < 0)
1372             *count = 0;
1373         if (sr && sr->which == Z_Records_DBOSD)
1374         {
1375             int i;
1376             NMEM nmem = odr_extract_mem(c->odr_in);
1377             Z_NamePlusRecordList *p =
1378                 sr->u.databaseOrSurDiagnostics;
1379             for (i = 0; i < p->num_records; i++)
1380             {
1381                 ZOOM_record_cache_add(resultset, p->records[i], i + *start,
1382                                       syntax, elementSetName, schema, 0);
1383             }
1384             *count -= i;
1385             if (*count < 0)
1386                 *count = 0;
1387             *start += i;
1388             yaz_log(c->log_details,
1389                     "handle_records resultset=%p start=%d count=%d",
1390                     resultset, *start, *count);
1391
1392             /* transfer our response to search_nmem .. we need it later */
1393             nmem_transfer(odr_getmem(resultset->odr), nmem);
1394             nmem_destroy(nmem);
1395             if (present_phase && p->num_records == 0)
1396             {
1397                 /* present response and we didn't get any records! */
1398                 Z_NamePlusRecord *myrec =
1399                     zget_surrogateDiagRec(
1400                         resultset->odr, 0,
1401                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1402                         "ZOOM C generated. Present phase and no records");
1403                 ZOOM_record_cache_add(resultset, myrec, *start,
1404                                       syntax, elementSetName, schema, 0);
1405                 *count = 0;
1406             }
1407         }
1408         else if (present_phase)
1409         {
1410             /* present response and we didn't get any records! */
1411             Z_NamePlusRecord *myrec =
1412                 zget_surrogateDiagRec(
1413                     resultset->odr, 0,
1414                     YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1415                     "ZOOM C generated: Present response and no records");
1416             ZOOM_record_cache_add(resultset, myrec, *start,
1417                                   syntax, elementSetName, schema, 0);
1418             *count = 0;
1419         }
1420     }
1421 }
1422
1423 static void handle_Z3950_present_response(ZOOM_connection c,
1424                                           Z_PresentResponse *pr)
1425 {
1426     handle_Z3950_records(c, pr->records, 1);
1427 }
1428
1429 static void set_init_option(const char *name, void *clientData)
1430 {
1431     ZOOM_connection c = (ZOOM_connection) clientData;
1432     char buf[80];
1433
1434     sprintf(buf, "init_opt_%.70s", name);
1435     ZOOM_connection_option_set(c, buf, "1");
1436 }
1437
1438 zoom_ret send_Z3950_sort(ZOOM_connection c, ZOOM_resultset resultset)
1439 {
1440     if (c->error)
1441         resultset->r_sort_spec = 0;
1442     if (resultset->r_sort_spec)
1443     {
1444         Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1445         Z_SortRequest *req = apdu->u.sortRequest;
1446
1447         yaz_log(c->log_details, "%p send_Z3950_sort set=%p", c, resultset);
1448
1449         req->num_inputResultSetNames = 1;
1450         req->inputResultSetNames = (Z_InternationalString **)
1451             odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
1452         req->inputResultSetNames[0] =
1453             odr_strdup(c->odr_out, resultset->setname);
1454         req->sortedResultSetName = odr_strdup(c->odr_out, resultset->setname);
1455         req->sortSequence = resultset->r_sort_spec;
1456         resultset->r_sort_spec = 0;
1457         return send_APDU(c, apdu);
1458     }
1459     return zoom_complete;
1460 }
1461
1462 static zoom_ret Z3950_send_present(ZOOM_connection c)
1463 {
1464     Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1465     Z_PresentRequest *req = apdu->u.presentRequest;
1466     ZOOM_resultset resultset = c->tasks->u.search.resultset;
1467     const char *syntax = c->tasks->u.search.syntax;
1468     const char *elementSetName = c->tasks->u.search.elementSetName;
1469     const char *schema = c->tasks->u.search.schema;
1470
1471     yaz_log(c->log_details, "%p Z3950_send_present", c);
1472
1473     *req->resultSetStartPoint = c->tasks->u.search.start + 1;
1474
1475     if (resultset->step > 0 && resultset->step < c->tasks->u.search.count)
1476         *req->numberOfRecordsRequested = resultset->step;
1477     else
1478         *req->numberOfRecordsRequested = c->tasks->u.search.count;
1479
1480     if (*req->numberOfRecordsRequested + c->tasks->u.search.start > resultset->size)
1481         *req->numberOfRecordsRequested = resultset->size - c->tasks->u.search.start;
1482     assert(*req->numberOfRecordsRequested > 0);
1483
1484     if (syntax && *syntax)
1485         req->preferredRecordSyntax =
1486             zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
1487
1488     if (schema && *schema)
1489     {
1490         Z_RecordComposition *compo = (Z_RecordComposition *)
1491             odr_malloc(c->odr_out, sizeof(*compo));
1492
1493         req->recordComposition = compo;
1494         compo->which = Z_RecordComp_complex;
1495         compo->u.complex = (Z_CompSpec *)
1496             odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1497         compo->u.complex->selectAlternativeSyntax = (bool_t *)
1498             odr_malloc(c->odr_out, sizeof(bool_t));
1499         *compo->u.complex->selectAlternativeSyntax = 0;
1500
1501         compo->u.complex->generic = (Z_Specification *)
1502             odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1503
1504         compo->u.complex->generic->which = Z_Schema_oid;
1505         compo->u.complex->generic->schema.oid = (Odr_oid *)
1506             zoom_yaz_str_to_z3950oid(c, CLASS_SCHEMA, schema);
1507
1508         if (!compo->u.complex->generic->schema.oid)
1509         {
1510             /* OID wasn't a schema! Try record syntax instead. */
1511
1512             compo->u.complex->generic->schema.oid = (Odr_oid *)
1513                 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, schema);
1514         }
1515         if (elementSetName && *elementSetName)
1516         {
1517             compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1518                 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1519             compo->u.complex->generic->elementSpec->which =
1520                 Z_ElementSpec_elementSetName;
1521             compo->u.complex->generic->elementSpec->u.elementSetName =
1522                 odr_strdup(c->odr_out, elementSetName);
1523         }
1524         else
1525             compo->u.complex->generic->elementSpec = 0;
1526         compo->u.complex->num_dbSpecific = 0;
1527         compo->u.complex->dbSpecific = 0;
1528         compo->u.complex->num_recordSyntax = 0;
1529         compo->u.complex->recordSyntax = 0;
1530     }
1531     else if (elementSetName && *elementSetName)
1532     {
1533         Z_ElementSetNames *esn = (Z_ElementSetNames *)
1534             odr_malloc(c->odr_out, sizeof(*esn));
1535         Z_RecordComposition *compo = (Z_RecordComposition *)
1536             odr_malloc(c->odr_out, sizeof(*compo));
1537
1538         esn->which = Z_ElementSetNames_generic;
1539         esn->u.generic = odr_strdup(c->odr_out, elementSetName);
1540         compo->which = Z_RecordComp_simple;
1541         compo->u.simple = esn;
1542         req->recordComposition = compo;
1543     }
1544     req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1545     return send_APDU(c, apdu);
1546 }
1547
1548 zoom_ret ZOOM_connection_Z3950_search(ZOOM_connection c)
1549 {
1550     int i = 0;
1551     const char *syntax = 0;
1552     const char *elementSetName = 0;
1553     const char *schema = 0;
1554     ZOOM_resultset resultset;
1555     int *start, *count;
1556
1557     if (!c->tasks)
1558         return zoom_complete;
1559     assert(c->tasks->which == ZOOM_TASK_SEARCH);
1560     resultset = c->tasks->u.search.resultset;
1561     start = &c->tasks->u.search.start;
1562     count = &c->tasks->u.search.count;
1563     syntax = c->tasks->u.search.syntax;
1564     elementSetName = c->tasks->u.search.elementSetName;
1565     schema =  c->tasks->u.search.schema;
1566
1567     yaz_log(c->log_details, "%p ZOOM_connection_Z3950_search start=%d count=%d",
1568             c, *start, *count);
1569
1570     ZOOM_memcached_search(c, resultset);
1571
1572     if (*start < 0 || *count < 0)
1573     {
1574         ZOOM_set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
1575                        "start/count < 0", 0);
1576     }
1577
1578     if (resultset->live_set)
1579     {
1580         if (*start + *count > resultset->size)
1581             ZOOM_set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
1582                        0, 0);
1583     }
1584
1585     if (c->error)                  /* don't continue on error */
1586         return zoom_complete;
1587
1588     for (i = 0; i < *count; i++)
1589     {
1590         ZOOM_record rec =
1591             ZOOM_record_cache_lookup(resultset, i + *start,
1592                                      syntax, elementSetName, schema);
1593         if (!rec)
1594             break;
1595     }
1596     *start += i;
1597     *count -= i;
1598
1599     if (*count == 0 && resultset->live_set)
1600         return zoom_complete;
1601
1602     if (resultset->live_set == 2)
1603         return Z3950_send_present(c);
1604     else
1605         return Z3950_send_search(c);
1606 }
1607
1608 static zoom_ret send_Z3950_sort_present(ZOOM_connection c)
1609 {
1610     zoom_ret r = zoom_complete;
1611
1612     if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
1613         r = send_Z3950_sort(c, c->tasks->u.search.resultset);
1614     if (r == zoom_complete)
1615         r = ZOOM_connection_Z3950_search(c);
1616     return r;
1617 }
1618
1619 void ZOOM_handle_Z3950_apdu(ZOOM_connection c, Z_APDU *apdu)
1620 {
1621     Z_InitResponse *initrs;
1622
1623     ZOOM_connection_set_mask(c, 0);
1624     yaz_log(c->log_details, "%p handle_Z3950_apdu apdu->which=%d",
1625             c, apdu->which);
1626     switch (apdu->which)
1627     {
1628     case Z_APDU_initResponse:
1629         yaz_log(c->log_api, "%p handle_Z3950_apdu: Received Init response", c);
1630         initrs = apdu->u.initResponse;
1631         ZOOM_connection_option_set(c, "serverImplementationId",
1632                                    initrs->implementationId ?
1633                                    initrs->implementationId : "");
1634         ZOOM_connection_option_set(c, "serverImplementationName",
1635                                    initrs->implementationName ?
1636                                    initrs->implementationName : "");
1637         ZOOM_connection_option_set(c, "serverImplementationVersion",
1638                                    initrs->implementationVersion ?
1639                                    initrs->implementationVersion : "");
1640         /* Set the three old options too, for old applications */
1641         ZOOM_connection_option_set(c, "targetImplementationId",
1642                                    initrs->implementationId ?
1643                                    initrs->implementationId : "");
1644         ZOOM_connection_option_set(c, "targetImplementationName",
1645                                    initrs->implementationName ?
1646                                    initrs->implementationName : "");
1647         ZOOM_connection_option_set(c, "targetImplementationVersion",
1648                                    initrs->implementationVersion ?
1649                                    initrs->implementationVersion : "");
1650
1651         /* Make initrs->options available as ZOOM-level options */
1652         yaz_init_opt_decode(initrs->options, set_init_option, (void*) c);
1653
1654         if (!*initrs->result)
1655         {
1656             Z_DefaultDiagFormat *df = yaz_decode_init_diag(0, initrs);
1657             if (df)
1658                 response_default_diag(c, df);
1659             else
1660                 ZOOM_set_error(c, ZOOM_ERROR_INIT, 0); /* default error */
1661         }
1662         else
1663         {
1664             char *cookie =
1665                 yaz_oi_get_string_oid(&apdu->u.initResponse->otherInfo,
1666                                       yaz_oid_userinfo_cookie, 1, 0);
1667             xfree(c->cookie_in);
1668             c->cookie_in = 0;
1669             if (cookie)
1670                 c->cookie_in = xstrdup(cookie);
1671             if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1672                 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
1673                 c->support_named_resultsets = 1;
1674             if (c->tasks)
1675             {
1676                 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1677                 ZOOM_connection_remove_task(c);
1678             }
1679             ZOOM_connection_exec_task(c);
1680         }
1681         if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
1682         {
1683             NMEM tmpmem = nmem_create();
1684             Z_CharSetandLanguageNegotiation *p =
1685                 yaz_get_charneg_record(initrs->otherInfo);
1686
1687             if (p)
1688             {
1689                 char *charset = NULL, *lang = NULL;
1690                 int sel;
1691
1692                 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
1693                 yaz_log(c->log_details, "%p handle_Z3950_apdu target accepted: "
1694                         "charset %s, language %s, select %d",
1695                         c,
1696                         charset ? charset : "none", lang ? lang : "none", sel);
1697                 if (charset)
1698                     ZOOM_connection_option_set(c, "negotiation-charset",
1699                                                charset);
1700                 if (lang)
1701                     ZOOM_connection_option_set(c, "negotiation-lang",
1702                                                lang);
1703
1704                 ZOOM_connection_option_set(
1705                     c,  "negotiation-charset-in-effect-for-records",
1706                     (sel != 0) ? "1" : "0");
1707                 nmem_destroy(tmpmem);
1708             }
1709         }
1710         break;
1711     case Z_APDU_searchResponse:
1712         yaz_log(c->log_api, "%p handle_Z3950_apdu Search response", c);
1713         handle_Z3950_search_response(c, apdu->u.searchResponse);
1714         if (send_Z3950_sort_present(c) == zoom_complete)
1715             ZOOM_connection_remove_task(c);
1716         break;
1717     case Z_APDU_presentResponse:
1718         yaz_log(c->log_api, "%p handle_Z3950_apdu Present response", c);
1719         handle_Z3950_present_response(c, apdu->u.presentResponse);
1720         if (ZOOM_connection_Z3950_search(c) == zoom_complete)
1721             ZOOM_connection_remove_task(c);
1722         break;
1723     case Z_APDU_sortResponse:
1724         yaz_log(c->log_api, "%p handle_Z3950_apdu Sort response", c);
1725         handle_Z3950_sort_response(c, apdu->u.sortResponse);
1726         if (ZOOM_connection_Z3950_search(c) == zoom_complete)
1727             ZOOM_connection_remove_task(c);
1728         break;
1729     case Z_APDU_scanResponse:
1730         yaz_log(c->log_api, "%p handle_Z3950_apdu Scan response", c);
1731         handle_Z3950_scan_response(c, apdu->u.scanResponse);
1732         ZOOM_connection_remove_task(c);
1733         break;
1734     case Z_APDU_extendedServicesResponse:
1735         yaz_log(c->log_api, "%p handle_Z3950_apdu Extended Services response", c);
1736         handle_Z3950_es_response(c, apdu->u.extendedServicesResponse);
1737         ZOOM_connection_remove_task(c);
1738         break;
1739     case Z_APDU_close:
1740         yaz_log(c->log_api, "%p handle_Z3950_apdu Close PDU", c);
1741         if (!ZOOM_test_reconnect(c))
1742         {
1743             ZOOM_set_dset_error(c, ZOOM_ERROR_CONNECTION_LOST, "ZOOM", c->host_port, apdu->u.close->diagnosticInformation);
1744             ZOOM_connection_close(c);
1745         }
1746         break;
1747     default:
1748         yaz_log(c->log_api, "%p Received unknown PDU", c);
1749         ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1750         ZOOM_connection_close(c);
1751     }
1752 }
1753
1754 /*
1755  * Local variables:
1756  * c-basic-offset: 4
1757  * c-file-style: "Stroustrup"
1758  * indent-tabs-mode: nil
1759  * End:
1760  * vim: shiftwidth=4 tabstop=8 expandtab
1761  */
1762