2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: zoom-c.c,v 1.126 2007-04-24 09:29:34 adam Exp $
9 * \brief Implements ZOOM C interface.
17 #include <yaz/yaz-util.h>
18 #include <yaz/xmalloc.h>
19 #include <yaz/otherinfo.h>
21 #include <yaz/pquery.h>
22 #include <yaz/marcdisp.h>
23 #include <yaz/diagbib1.h>
24 #include <yaz/charneg.h>
30 static int log_api = 0;
31 static int log_details = 0;
38 static void resultset_destroy(ZOOM_resultset r);
39 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c);
40 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out);
41 static char *cql2pqf(ZOOM_connection c, const char *cql);
45 * This wrapper is just for logging failed lookups. It would be nicer
46 * if it could cause failure when a lookup fails, but that's hard.
48 static int *zoom_yaz_str_to_z3950oid(ZOOM_connection c,
49 int oid_class, const char *str) {
50 int *res = yaz_string_to_oid_odr(yaz_oid_std(), oid_class, str,
53 yaz_log(YLOG_WARN, "%p OID lookup (%d, '%s') failed",
54 c, (int) oid_class, str);
59 static void initlog(void)
61 static int log_level_initialized = 0;
62 if (!log_level_initialized)
64 log_api = yaz_log_module_level("zoom");
65 log_details = yaz_log_module_level("zoomdetails");
66 log_level_initialized = 1;
70 static ZOOM_Event ZOOM_Event_create(int kind)
72 ZOOM_Event event = (ZOOM_Event) xmalloc(sizeof(*event));
76 yaz_log(log_details, "ZOOM_Event_create(kind=%d)", kind);
80 static void ZOOM_Event_destroy(ZOOM_Event event)
85 static void ZOOM_connection_put_event(ZOOM_connection c, ZOOM_Event event)
89 c->m_queue_back->prev = event;
90 assert(c->m_queue_front);
94 assert(!c->m_queue_front);
95 c->m_queue_front = event;
97 event->next = c->m_queue_back;
99 c->m_queue_back = event;
102 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
104 ZOOM_Event event = c->m_queue_front;
107 c->last_event = ZOOM_EVENT_NONE;
110 assert(c->m_queue_back);
111 c->m_queue_front = event->prev;
112 if (c->m_queue_front)
114 assert(c->m_queue_back);
115 c->m_queue_front->next = 0;
119 c->last_event = event->kind;
123 static void ZOOM_connection_remove_events(ZOOM_connection c)
126 while ((event = ZOOM_connection_get_event(c)))
127 ZOOM_Event_destroy(event);
130 ZOOM_API(int) ZOOM_connection_peek_event(ZOOM_connection c)
132 ZOOM_Event event = c->m_queue_front;
134 return event ? event->kind : ZOOM_EVENT_NONE;
137 void ZOOM_connection_remove_tasks(ZOOM_connection c);
139 static void set_dset_error(ZOOM_connection c, int error,
141 const char *addinfo, const char *addinfo2)
148 if (!c->diagset || strcmp(dset, c->diagset))
151 c->diagset = xstrdup(dset);
152 /* remove integer part from SRW diagset .. */
153 if ((cp = strrchr(c->diagset, '/')))
156 if (addinfo && addinfo2)
158 c->addinfo = (char*) xmalloc(strlen(addinfo) + strlen(addinfo2) + 2);
159 strcpy(c->addinfo, addinfo);
160 strcat(c->addinfo, addinfo2);
163 c->addinfo = xstrdup(addinfo);
164 if (error != ZOOM_ERROR_NONE)
166 yaz_log(log_api, "%p set_dset_error %s %s:%d %s %s",
167 c, c->host_port ? c->host_port : "<>", dset, error,
168 addinfo ? addinfo : "",
169 addinfo2 ? addinfo2 : "");
170 ZOOM_connection_remove_tasks(c);
175 static void set_HTTP_error(ZOOM_connection c, int error,
176 const char *addinfo, const char *addinfo2)
178 set_dset_error(c, error, "HTTP", addinfo, addinfo2);
183 static void set_ZOOM_error(ZOOM_connection c, int error,
186 set_dset_error(c, error, "ZOOM", addinfo, 0);
189 static void clear_error(ZOOM_connection c)
192 * If an error is tied to an operation then it's ok to clear: for
193 * example, a diagnostic returned from a search is cleared by a
194 * subsequent search. However, problems such as Connection Lost
195 * or Init Refused are not cleared, because they are not
196 * recoverable: doing another search doesn't help.
199 ZOOM_connection_remove_events(c);
202 case ZOOM_ERROR_CONNECT:
203 case ZOOM_ERROR_MEMORY:
204 case ZOOM_ERROR_DECODE:
205 case ZOOM_ERROR_CONNECTION_LOST:
206 case ZOOM_ERROR_INIT:
207 case ZOOM_ERROR_INTERNAL:
208 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
211 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
215 void ZOOM_connection_show_task(ZOOM_task task)
219 case ZOOM_TASK_SEARCH:
220 yaz_log(YLOG_LOG, "search p=%p", task);
222 case ZOOM_TASK_RETRIEVE:
223 yaz_log(YLOG_LOG, "retrieve p=%p", task);
225 case ZOOM_TASK_CONNECT:
226 yaz_log(YLOG_LOG, "connect p=%p", task);
229 yaz_log(YLOG_LOG, "scant p=%p", task);
234 void ZOOM_connection_show_tasks(ZOOM_connection c)
237 yaz_log(YLOG_LOG, "connection p=%p tasks", c);
238 for (task = c->tasks; task; task = task->next)
239 ZOOM_connection_show_task(task);
242 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
244 ZOOM_task *taskp = &c->tasks;
246 taskp = &(*taskp)->next;
247 *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
248 (*taskp)->running = 0;
249 (*taskp)->which = which;
255 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
257 return c->tasks ? 0 : 1;
260 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
262 ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
264 task->next = c->tasks;
273 void ZOOM_connection_remove_task(ZOOM_connection c)
275 ZOOM_task task = c->tasks;
279 c->tasks = task->next;
282 case ZOOM_TASK_SEARCH:
283 resultset_destroy(task->u.search.resultset);
284 xfree(task->u.search.syntax);
285 xfree(task->u.search.elementSetName);
287 case ZOOM_TASK_RETRIEVE:
288 resultset_destroy(task->u.retrieve.resultset);
289 xfree(task->u.retrieve.syntax);
290 xfree(task->u.retrieve.elementSetName);
292 case ZOOM_TASK_CONNECT:
295 ZOOM_scanset_destroy(task->u.scan.scan);
297 case ZOOM_TASK_PACKAGE:
298 ZOOM_package_destroy(task->u.package);
301 resultset_destroy(task->u.sort.resultset);
302 ZOOM_query_destroy(task->u.sort.q);
311 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
312 ZOOM_connection_put_event(c, event);
317 static int ZOOM_connection_exec_task(ZOOM_connection c);
319 void ZOOM_connection_remove_tasks(ZOOM_connection c)
322 ZOOM_connection_remove_task(c);
325 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
327 const char *elementSetName);
329 ZOOM_API(ZOOM_connection)
330 ZOOM_connection_create(ZOOM_options options)
332 ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
336 yaz_log(log_api, "%p ZOOM_connection_create", c);
338 c->proto = PROTO_Z3950;
340 ZOOM_connection_set_mask(c, 0);
342 c->state = STATE_IDLE;
345 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
352 c->options = ZOOM_options_create_with_parent(options);
358 c->charset = c->lang = 0;
365 c->odr_in = odr_createmem(ODR_DECODE);
366 c->odr_out = odr_createmem(ODR_ENCODE);
369 c->support_named_resultsets = 0;
370 c->last_event = ZOOM_EVENT_NONE;
372 c->m_queue_front = 0;
378 /* set database names. Take local databases (if set); otherwise
379 take databases given in ZURL (if set); otherwise use Default */
380 static char **set_DatabaseNames(ZOOM_connection con, ZOOM_options options,
383 char **databaseNames;
384 const char *cp = ZOOM_options_get(options, "databaseName");
386 if ((!cp || !*cp) && con->host_port)
388 if (strncmp(con->host_port, "unix:", 5) == 0)
389 cp = strchr(con->host_port+5, ':');
391 cp = strchr(con->host_port, '/');
397 nmem_strsplit(odr_getmem(odr), "+", cp, &databaseNames, num);
398 return databaseNames;
401 ZOOM_API(ZOOM_connection)
402 ZOOM_connection_new(const char *host, int portnum)
404 ZOOM_connection c = ZOOM_connection_create(0);
406 ZOOM_connection_connect(c, host, portnum);
410 static zoom_sru_mode get_sru_mode_from_string(const char *s)
413 return zoom_sru_soap;
414 if (!yaz_matchstr(s, "soap"))
415 return zoom_sru_soap;
416 else if (!yaz_matchstr(s, "get"))
418 else if (!yaz_matchstr(s, "post"))
419 return zoom_sru_post;
420 return zoom_sru_error;
424 ZOOM_connection_connect(ZOOM_connection c,
425 const char *host, int portnum)
432 yaz_log(log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
433 c, host ? host : "null", portnum);
435 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
436 ZOOM_connection_remove_tasks(c);
440 yaz_log(log_details, "%p ZOOM_connection_connect reconnect ok", c);
444 yaz_log(log_details, "%p ZOOM_connection_connect connect", c);
446 val = ZOOM_options_get(c->options, "proxy");
449 yaz_log(log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
450 c->proxy = xstrdup(val);
456 val = ZOOM_options_get(c->options, "charset");
459 yaz_log(log_details, "%p ZOOM_connection_connect charset=%s", c, val);
460 c->charset = xstrdup(val);
466 val = ZOOM_options_get(c->options, "lang");
469 yaz_log(log_details, "%p ZOOM_connection_connect lang=%s", c, val);
470 c->lang = xstrdup(val);
481 sprintf(hostn, "%.80s:%d", host, portnum);
482 c->host_port = xstrdup(hostn);
485 c->host_port = xstrdup(host);
490 * If the "<scheme>:" part of the host string is preceded by one
491 * or more comma-separated <name>=<value> pairs, these are taken
492 * to be options to be set on the connection object. Among other
493 * applications, this facility can be used to embed authentication
495 * user=admin,password=secret,tcp:localhost:9999
497 char *remainder = c->host_port;
498 char *pcolon = strchr(remainder, ':');
501 while ((pcomma = strchr(remainder, ',')) != 0 &&
502 (pcolon == 0 || pcomma < pcolon)) {
504 if ((pequals = strchr(remainder, '=')) != 0) {
506 /*printf("# setting '%s'='%s'\n", remainder, pequals+1);*/
507 ZOOM_connection_option_set(c, remainder, pequals+1);
509 remainder = pcomma+1;
512 if (remainder != c->host_port) {
514 c->host_port = xstrdup(remainder);
515 /*printf("# reset hp='%s'\n", remainder);*/
519 val = ZOOM_options_get(c->options, "sru");
520 c->sru_mode = get_sru_mode_from_string(val);
522 ZOOM_options_set(c->options, "host", c->host_port);
524 val = ZOOM_options_get(c->options, "cookie");
527 yaz_log(log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
528 c->cookie_out = xstrdup(val);
531 val = ZOOM_options_get(c->options, "clientIP");
534 yaz_log(log_details, "%p ZOOM_connection_connect clientIP=%s",
536 c->client_IP = xstrdup(val);
539 c->async = ZOOM_options_get_bool(c->options, "async", 0);
540 yaz_log(log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
542 task = ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
546 while (ZOOM_event(1, &c))
552 ZOOM_query_create(void)
554 ZOOM_query s = (ZOOM_query) xmalloc(sizeof(*s));
556 yaz_log(log_details, "%p ZOOM_query_create", s);
560 s->odr = odr_createmem(ODR_ENCODE);
567 ZOOM_query_destroy(ZOOM_query s)
573 yaz_log(log_details, "%p ZOOM_query_destroy count=%d", s, s->refcount);
574 if (s->refcount == 0)
582 ZOOM_query_prefix(ZOOM_query s, const char *str)
584 s->query_string = odr_strdup(s->odr, str);
585 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
586 s->z_query->which = Z_Query_type_1;
587 s->z_query->u.type_1 = p_query_rpn(s->odr, str);
588 if (!s->z_query->u.type_1)
590 yaz_log(log_details, "%p ZOOM_query_prefix str=%s failed", s, str);
594 yaz_log(log_details, "%p ZOOM_query_prefix str=%s", s, str);
599 ZOOM_query_cql(ZOOM_query s, const char *str)
603 s->query_string = odr_strdup(s->odr, str);
605 ext = (Z_External *) odr_malloc(s->odr, sizeof(*ext));
606 ext->direct_reference = odr_getoidbystr(s->odr, "1.2.840.10003.16.2");
607 ext->indirect_reference = 0;
609 ext->which = Z_External_CQL;
610 ext->u.cql = s->query_string;
612 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
613 s->z_query->which = Z_Query_type_104;
614 s->z_query->u.type_104 = ext;
616 yaz_log(log_details, "%p ZOOM_query_cql str=%s", s, str);
622 * Translate the CQL string client-side into RPN which is passed to
623 * the server. This is useful for server's that don't themselves
624 * support CQL, for which ZOOM_query_cql() is useless. `conn' is used
625 * only as a place to stash diagnostics if compilation fails; if this
626 * information is not needed, a null pointer may be used.
629 ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn)
633 ZOOM_connection freeme = 0;
635 yaz_log(log_details, "%p ZOOM_query_cql2rpn str=%s conn=%p", s, str, conn);
637 conn = freeme = ZOOM_connection_create(0);
639 rpn = cql2pqf(conn, str);
641 ZOOM_connection_destroy(freeme);
645 ret = ZOOM_query_prefix(s, rpn);
651 * Analogous in every way to ZOOM_query_cql2rpn(), except that there
652 * is no analogous ZOOM_query_ccl() that just sends uninterpreted CCL
653 * to the server, as the YAZ GFS doesn't know how to handle this.
656 ZOOM_query_ccl2rpn(ZOOM_query s, const char *str, const char *config,
657 int *ccl_error, const char **error_string,
661 struct ccl_rpn_node *rpn;
662 CCL_bibset bibset = ccl_qual_mk();
665 ccl_qual_buf(bibset, config);
667 rpn = ccl_find_str(bibset, str, ccl_error, error_pos);
670 *error_string = ccl_err_msg(*ccl_error);
675 WRBUF wr = wrbuf_alloc();
678 ret = ZOOM_query_prefix(s, wrbuf_cstr(wr));
681 ccl_qual_rm(&bibset);
686 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
688 s->sort_spec = yaz_sort_spec(s->odr, criteria);
691 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s failed",
695 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s", s, criteria);
699 static zoom_ret do_write(ZOOM_connection c);
702 ZOOM_connection_destroy(ZOOM_connection c)
707 yaz_log(log_api, "%p ZOOM_connection_destroy", c);
710 for (r = c->resultsets; r; r = r->next)
716 odr_destroy(c->odr_in);
717 odr_destroy(c->odr_out);
718 ZOOM_options_destroy(c->options);
719 ZOOM_connection_remove_tasks(c);
720 ZOOM_connection_remove_events(c);
726 xfree(c->cookie_out);
732 void ZOOM_resultset_addref(ZOOM_resultset r)
737 yaz_log(log_details, "%p ZOOM_resultset_addref count=%d",
742 ZOOM_resultset ZOOM_resultset_create(void)
745 ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
749 yaz_log(log_details, "%p ZOOM_resultset_create", r);
752 r->odr = odr_createmem(ODR_ENCODE);
757 for (i = 0; i<RECORD_HASH_SIZE; i++)
758 r->record_hash[i] = 0;
763 r->databaseNames = 0;
764 r->num_databaseNames = 0;
768 ZOOM_API(ZOOM_resultset)
769 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
772 ZOOM_query s = ZOOM_query_create();
774 ZOOM_query_prefix(s, q);
776 r = ZOOM_connection_search(c, s);
777 ZOOM_query_destroy(s);
781 ZOOM_API(ZOOM_resultset)
782 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
784 ZOOM_resultset r = ZOOM_resultset_create();
788 const char *syntax, *elementSetName;
790 yaz_log(log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
791 r->r_sort_spec = q->sort_spec;
794 r->options = ZOOM_options_create_with_parent(c->options);
796 start = ZOOM_options_get_int(r->options, "start", 0);
797 count = ZOOM_options_get_int(r->options, "count", 0);
799 /* If "presentChunk" is defined use that; otherwise "step" */
800 const char *cp = ZOOM_options_get(r->options, "presentChunk");
801 r->step = ZOOM_options_get_int(r->options,
802 (cp != 0 ? "presentChunk": "step"), 0);
804 r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
805 cp = ZOOM_options_get(r->options, "setname");
807 r->setname = xstrdup(cp);
808 cp = ZOOM_options_get(r->options, "schema");
810 r->schema = xstrdup(cp);
812 r->databaseNames = set_DatabaseNames(c, c->options, &r->num_databaseNames,
817 r->next = c->resultsets;
820 if (c->host_port && c->proto == PROTO_HTTP)
824 yaz_log(log_details, "ZOOM_connection_search: no comstack");
825 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
829 yaz_log(log_details, "ZOOM_connection_search: reconnect");
834 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
835 task->u.search.resultset = r;
836 task->u.search.start = start;
837 task->u.search.count = count;
839 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
840 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
841 elementSetName = ZOOM_options_get(r->options, "elementSetName");
842 task->u.search.elementSetName = elementSetName
843 ? xstrdup(elementSetName) : 0;
845 ZOOM_resultset_addref(r);
851 while (ZOOM_event(1, &c))
858 ZOOM_resultset_sort(ZOOM_resultset r,
859 const char *sort_type, const char *sort_spec)
861 (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
865 ZOOM_resultset_sort1(ZOOM_resultset r,
866 const char *sort_type, const char *sort_spec)
868 ZOOM_connection c = r->connection;
872 newq = ZOOM_query_create();
873 if (ZOOM_query_sortby(newq, sort_spec) < 0)
876 yaz_log(log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
877 r, r, sort_type, sort_spec);
881 if (c->host_port && c->proto == PROTO_HTTP)
885 yaz_log(log_details, "%p ZOOM_resultset_sort: no comstack", r);
886 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
890 yaz_log(log_details, "%p ZOOM_resultset_sort: prepare reconnect",
896 ZOOM_resultset_cache_reset(r);
897 task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
898 task->u.sort.resultset = r;
899 task->u.sort.q = newq;
901 ZOOM_resultset_addref(r);
905 while (ZOOM_event(1, &c))
913 ZOOM_resultset_cache_reset(ZOOM_resultset r)
916 for (i = 0; i<RECORD_HASH_SIZE; i++)
918 ZOOM_record_cache rc;
919 for (rc = r->record_hash[i]; rc; rc = rc->next)
921 if (rc->rec.wrbuf_marc)
922 wrbuf_destroy(rc->rec.wrbuf_marc);
923 if (rc->rec.wrbuf_iconv)
924 wrbuf_destroy(rc->rec.wrbuf_iconv);
925 if (rc->rec.wrbuf_opac)
926 wrbuf_destroy(rc->rec.wrbuf_opac);
928 r->record_hash[i] = 0;
933 ZOOM_resultset_destroy(ZOOM_resultset r)
935 resultset_destroy(r);
938 static void resultset_destroy(ZOOM_resultset r)
943 yaz_log(log_details, "%p ZOOM_resultset_destroy r=%p count=%d",
945 if (r->refcount == 0)
947 ZOOM_resultset_cache_reset(r);
951 /* remove ourselves from the resultsets in connection */
952 ZOOM_resultset *rp = &r->connection->resultsets;
955 assert(*rp); /* we must be in this list!! */
957 { /* OK, we're here - take us out of it */
964 ZOOM_query_destroy(r->query);
965 ZOOM_options_destroy(r->options);
974 ZOOM_resultset_size(ZOOM_resultset r)
976 yaz_log(log_details, "ZOOM_resultset_size r=%p count=%d",
981 static void do_close(ZOOM_connection c)
986 ZOOM_connection_set_mask(c, 0);
987 c->state = STATE_IDLE;
990 static int ZOOM_test_reconnect(ZOOM_connection c)
994 if (!c->reconnect_ok)
998 c->tasks->running = 0;
999 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1001 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1002 ZOOM_connection_put_event(c, event);
1007 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
1008 int force_sync, int start, int count)
1013 const char *syntax, *elementSetName;
1017 yaz_log(log_details, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
1018 " count=%d", r, force_sync, start, count);
1023 if (c->host_port && c->proto == PROTO_HTTP)
1027 yaz_log(log_details, "%p ZOOM_resultset_retrieve: no comstack", r);
1028 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
1032 yaz_log(log_details, "%p ZOOM_resultset_retrieve: prepare "
1034 c->reconnect_ok = 1;
1037 task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
1038 task->u.retrieve.resultset = r;
1039 task->u.retrieve.start = start;
1040 task->u.retrieve.count = count;
1042 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
1043 task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
1044 elementSetName = ZOOM_options_get(r->options, "elementSetName");
1045 task->u.retrieve.elementSetName = elementSetName
1046 ? xstrdup(elementSetName) : 0;
1048 cp = ZOOM_options_get(r->options, "schema");
1051 if (!r->schema || strcmp(r->schema, cp))
1054 r->schema = xstrdup(cp);
1058 ZOOM_resultset_addref(r);
1060 if (!r->connection->async || force_sync)
1061 while (r->connection && ZOOM_event(1, &r->connection))
1066 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
1067 size_t start, size_t count)
1069 int force_present = 0;
1073 yaz_log(log_api, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
1074 r, r, (long) start, (long) count);
1077 ZOOM_resultset_retrieve(r, force_present, start, count);
1081 for (i = 0; i< count; i++)
1082 recs[i] = ZOOM_resultset_record_immediate(r, i+start);
1086 static void get_cert(ZOOM_connection c)
1091 if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1093 ZOOM_connection_option_setl(c, "sslPeerCert",
1094 cert_buf, cert_len);
1099 static zoom_ret do_connect(ZOOM_connection c)
1102 const char *effective_host;
1105 effective_host = c->proxy;
1107 effective_host = c->host_port;
1109 yaz_log(log_details, "%p do_connect effective_host=%s", c, effective_host);
1113 c->cs = cs_create_host(effective_host, 0, &add);
1115 if (c->cs && c->cs->protocol == PROTO_HTTP)
1118 const char *path = 0;
1120 c->proto = PROTO_HTTP;
1121 cs_get_host_args(c->host_port, &path);
1123 c->path = (char*) xmalloc(strlen(path)+2);
1125 strcpy(c->path+1, path);
1127 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1129 return zoom_complete;
1134 int ret = cs_connect(c->cs, add);
1137 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1138 ZOOM_connection_put_event(c, event);
1140 if (c->proto == PROTO_Z3950)
1141 ZOOM_connection_send_init(c);
1144 /* no init request for SRW .. */
1145 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1146 ZOOM_connection_remove_task(c);
1147 ZOOM_connection_set_mask(c, 0);
1148 ZOOM_connection_exec_task(c);
1150 c->state = STATE_ESTABLISHED;
1151 return zoom_pending;
1155 int mask = ZOOM_SELECT_EXCEPT;
1156 if (c->cs->io_pending & CS_WANT_WRITE)
1157 mask += ZOOM_SELECT_WRITE;
1158 if (c->cs->io_pending & CS_WANT_READ)
1159 mask += ZOOM_SELECT_READ;
1160 ZOOM_connection_set_mask(c, mask);
1161 c->state = STATE_CONNECTING;
1162 return zoom_pending;
1165 c->state = STATE_IDLE;
1166 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1167 return zoom_complete;
1170 static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
1173 for (i = 0; i<200; i++)
1177 Z_OtherInformation **oi;
1182 sprintf(buf, "otherInfo%d", i);
1183 val = ZOOM_options_get(c->options, buf);
1186 cp = strchr(val, ':');
1190 if (len >= sizeof(buf))
1191 len = sizeof(buf)-1;
1192 memcpy(buf, val, len);
1195 oid = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_USERINFO,
1200 yaz_oi_APDU(a, &oi);
1201 yaz_oi_set_string_oid(oi, out, oid, 1, cp+1);
1205 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
1210 Z_OtherInformation **oi;
1211 yaz_oi_APDU(a, &oi);
1212 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_cookie,
1217 Z_OtherInformation **oi;
1218 yaz_oi_APDU(a, &oi);
1219 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_client_ip,
1222 otherInfo_attach(c, a, out);
1223 if (!z_APDU(out, &a, 0, 0))
1225 FILE *outf = fopen("/tmp/apdu.txt", "a");
1228 ODR odr_pr = odr_createmem(ODR_PRINT);
1229 fprintf(outf, "a=%p\n", a);
1230 odr_setprint(odr_pr, outf);
1231 z_APDU(odr_pr, &a, 0, 0);
1232 odr_destroy(odr_pr);
1234 yaz_log(log_api, "%p encoding_APDU: encoding failed", c);
1235 set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
1239 yaz_log(log_details, "%p encoding_APDU encoding OK", c);
1243 static zoom_ret send_APDU(ZOOM_connection c, Z_APDU *a)
1247 if (encode_APDU(c, a, c->odr_out))
1248 return zoom_complete;
1249 yaz_log(log_details, "%p send APDU type=%d", c, a->which);
1250 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1251 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1252 ZOOM_connection_put_event(c, event);
1253 odr_reset(c->odr_out);
1257 /* returns 1 if PDU was sent OK (still pending )
1258 0 if PDU was not sent OK (nothing to wait for)
1261 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
1263 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
1264 Z_InitRequest *ireq = apdu->u.initRequest;
1265 Z_IdAuthentication *auth = (Z_IdAuthentication *)
1266 odr_malloc(c->odr_out, sizeof(*auth));
1267 const char *auth_groupId = ZOOM_options_get(c->options, "group");
1268 const char *auth_userId = ZOOM_options_get(c->options, "user");
1269 const char *auth_password = ZOOM_options_get(c->options, "password");
1272 /* support the pass for backwards compatibility */
1274 auth_password = ZOOM_options_get(c->options, "pass");
1276 ODR_MASK_SET(ireq->options, Z_Options_search);
1277 ODR_MASK_SET(ireq->options, Z_Options_present);
1278 ODR_MASK_SET(ireq->options, Z_Options_scan);
1279 ODR_MASK_SET(ireq->options, Z_Options_sort);
1280 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
1281 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
1283 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
1284 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
1285 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
1287 /* Index Data's Z39.50 Implementor Id is 81 */
1288 ireq->implementationId =
1289 odr_prepend(c->odr_out,
1290 ZOOM_options_get(c->options, "implementationId"),
1291 odr_prepend(c->odr_out, "81", ireq->implementationId));
1293 ireq->implementationName =
1294 odr_prepend(c->odr_out,
1295 ZOOM_options_get(c->options, "implementationName"),
1296 odr_prepend(c->odr_out, "ZOOM-C",
1297 ireq->implementationName));
1299 version = odr_strdup(c->odr_out, "$Revision: 1.126 $");
1300 if (strlen(version) > 10) /* check for unexpanded CVS strings */
1301 version[strlen(version)-2] = '\0';
1302 ireq->implementationVersion =
1303 odr_prepend(c->odr_out,
1304 ZOOM_options_get(c->options, "implementationVersion"),
1305 odr_prepend(c->odr_out, &version[11],
1306 ireq->implementationVersion));
1308 *ireq->maximumRecordSize =
1309 ZOOM_options_get_int(c->options, "maximumRecordSize", 1024*1024);
1310 *ireq->preferredMessageSize =
1311 ZOOM_options_get_int(c->options, "preferredMessageSize", 1024*1024);
1313 if (auth_groupId || auth_password)
1315 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
1318 if (auth_groupId && *auth_groupId)
1320 pass->groupId = (char *)
1321 odr_malloc(c->odr_out, strlen(auth_groupId)+1);
1322 strcpy(pass->groupId, auth_groupId);
1326 if (auth_userId && *auth_userId)
1328 pass->userId = (char *)
1329 odr_malloc(c->odr_out, strlen(auth_userId)+1);
1330 strcpy(pass->userId, auth_userId);
1334 if (auth_password && *auth_password)
1336 pass->password = (char *)
1337 odr_malloc(c->odr_out, strlen(auth_password)+1);
1338 strcpy(pass->password, auth_password);
1343 auth->which = Z_IdAuthentication_idPass;
1344 auth->u.idPass = pass;
1345 ireq->idAuthentication = auth;
1348 else if (auth_userId)
1350 auth->which = Z_IdAuthentication_open;
1351 auth->u.open = (char *)
1352 odr_malloc(c->odr_out, strlen(auth_userId)+1);
1353 strcpy(auth->u.open, auth_userId);
1354 ireq->idAuthentication = auth;
1358 yaz_oi_set_string_oid(&ireq->otherInfo, c->odr_out,
1359 yaz_oid_userinfo_proxy, 1, c->host_port);
1361 if (c->charset || c->lang)
1363 Z_OtherInformation **oi;
1364 Z_OtherInformationUnit *oi_unit;
1366 yaz_oi_APDU(apdu, &oi);
1368 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
1370 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
1371 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
1372 oi_unit->information.externallyDefinedInfo =
1373 yaz_set_proposal_charneg_list(c->odr_out, " ",
1374 c->charset, c->lang, 1);
1378 return send_APDU(c, apdu);
1382 static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
1387 gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port, c->path);
1389 if (c->sru_mode == zoom_sru_get)
1391 yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1393 else if (c->sru_mode == zoom_sru_post)
1395 yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1397 else if (c->sru_mode == zoom_sru_soap)
1399 yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1401 if (!z_GDU(c->odr_out, &gdu, 0, 0))
1402 return zoom_complete;
1403 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1405 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1406 ZOOM_connection_put_event(c, event);
1407 odr_reset(c->odr_out);
1413 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1417 ZOOM_resultset resultset = 0;
1419 const char *option_val = 0;
1421 if (c->error) /* don't continue on error */
1422 return zoom_complete;
1424 switch(c->tasks->which)
1426 case ZOOM_TASK_SEARCH:
1427 resultset = c->tasks->u.search.resultset;
1428 resultset->setname = xstrdup("default");
1429 ZOOM_options_set(resultset->options, "setname", resultset->setname);
1430 start = &c->tasks->u.search.start;
1431 count = &c->tasks->u.search.count;
1433 case ZOOM_TASK_RETRIEVE:
1434 resultset = c->tasks->u.retrieve.resultset;
1436 start = &c->tasks->u.retrieve.start;
1437 count = &c->tasks->u.retrieve.count;
1439 if (*start >= resultset->size)
1440 return zoom_complete;
1441 if (*start + *count > resultset->size)
1442 *count = resultset->size - *start;
1444 for (i = 0; i < *count; i++)
1447 record_cache_lookup(resultset, i + *start,
1448 c->tasks->u.retrieve.syntax,
1449 c->tasks->u.retrieve.elementSetName);
1454 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
1455 ZOOM_connection_put_event(c, event);
1462 return zoom_complete;
1465 return zoom_complete;
1467 assert(resultset->query);
1469 sr = yaz_srw_get(c->odr_out, Z_SRW_searchRetrieve_request);
1471 if (resultset->query->z_query->which == Z_Query_type_104
1472 && resultset->query->z_query->u.type_104->which == Z_External_CQL)
1474 sr->u.request->query_type = Z_SRW_query_type_cql;
1475 sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql;
1477 else if (resultset->query->z_query->which == Z_Query_type_1 &&
1478 resultset->query->z_query->u.type_1)
1480 sr->u.request->query_type = Z_SRW_query_type_pqf;
1481 sr->u.request->query.pqf = resultset->query->query_string;
1485 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
1486 return zoom_complete;
1488 sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
1489 sr->u.request->maximumRecords = odr_intdup(
1490 c->odr_out, resultset->step>0 ? resultset->step : *count);
1491 sr->u.request->recordSchema = resultset->schema;
1493 option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
1495 sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
1497 option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
1499 sr->extra_args = odr_strdup(c->odr_out, option_val);
1500 return send_srw(c, sr);
1503 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1505 return zoom_complete;
1509 static zoom_ret ZOOM_connection_send_search(ZOOM_connection c)
1512 int lslb, ssub, mspn;
1514 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
1515 Z_SearchRequest *search_req = apdu->u.searchRequest;
1516 const char *elementSetName;
1517 const char *smallSetElementSetName;
1518 const char *mediumSetElementSetName;
1521 assert(c->tasks->which == ZOOM_TASK_SEARCH);
1523 r = c->tasks->u.search.resultset;
1525 yaz_log(log_details, "%p ZOOM_connection_send_search set=%p", c, r);
1528 ZOOM_options_get(r->options, "elementSetName");
1529 smallSetElementSetName =
1530 ZOOM_options_get(r->options, "smallSetElementSetName");
1531 mediumSetElementSetName =
1532 ZOOM_options_get(r->options, "mediumSetElementSetName");
1534 if (!smallSetElementSetName)
1535 smallSetElementSetName = elementSetName;
1537 if (!mediumSetElementSetName)
1538 mediumSetElementSetName = elementSetName;
1543 /* prepare query for the search request */
1544 search_req->query = r->query->z_query;
1545 if (!search_req->query)
1547 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
1548 return zoom_complete;
1551 search_req->databaseNames = r->databaseNames;
1552 search_req->num_databaseNames = r->num_databaseNames;
1554 /* get syntax (no need to provide unless piggyback is in effect) */
1555 syntax = c->tasks->u.search.syntax;
1557 lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
1558 ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
1559 mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
1560 if (lslb != -1 && ssub != -1 && mspn != -1)
1562 /* So're a Z39.50 expert? Let's hope you don't do sort */
1563 *search_req->largeSetLowerBound = lslb;
1564 *search_req->smallSetUpperBound = ssub;
1565 *search_req->mediumSetPresentNumber = mspn;
1567 else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
1568 && r->piggyback && !r->r_sort_spec && !r->schema)
1570 /* Regular piggyback - do it unless we're going to do sort */
1571 *search_req->largeSetLowerBound = 2000000000;
1572 *search_req->smallSetUpperBound = 1;
1573 *search_req->mediumSetPresentNumber =
1574 r->step>0 ? r->step : c->tasks->u.search.count;
1578 /* non-piggyback. Need not provide elementsets or syntaxes .. */
1579 smallSetElementSetName = 0;
1580 mediumSetElementSetName = 0;
1583 if (smallSetElementSetName && *smallSetElementSetName)
1585 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1586 odr_malloc(c->odr_out, sizeof(*esn));
1588 esn->which = Z_ElementSetNames_generic;
1589 esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
1590 search_req->smallSetElementSetNames = esn;
1592 if (mediumSetElementSetName && *mediumSetElementSetName)
1594 Z_ElementSetNames *esn =(Z_ElementSetNames *)
1595 odr_malloc(c->odr_out, sizeof(*esn));
1597 esn->which = Z_ElementSetNames_generic;
1598 esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
1599 search_req->mediumSetElementSetNames = esn;
1602 search_req->preferredRecordSyntax =
1603 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
1607 if (c->support_named_resultsets)
1611 /* find the lowest unused ordinal so that we re-use
1612 result sets on the server. */
1613 for (ord = 1; ; ord++)
1616 sprintf(setname, "%d", ord);
1617 for (rp = c->resultsets; rp; rp = rp->next)
1618 if (rp->setname && !strcmp(rp->setname, setname))
1623 r->setname = xstrdup(setname);
1624 yaz_log(log_details, "%p ZOOM_connection_send_search: allocating "
1625 "set %s", c, r->setname);
1629 yaz_log(log_details, "%p ZOOM_connection_send_search: using "
1631 r->setname = xstrdup("default");
1633 ZOOM_options_set(r->options, "setname", r->setname);
1635 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
1636 return send_APDU(c, apdu);
1639 static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)
1646 case Z_DefaultDiagFormat_v2Addinfo:
1647 addinfo = r->u.v2Addinfo;
1649 case Z_DefaultDiagFormat_v3Addinfo:
1650 addinfo = r->u.v3Addinfo;
1655 set_dset_error(c, *r->condition,
1656 yaz_oid_to_string(yaz_oid_std(),
1657 r->diagnosticSetId, &oclass),
1661 static void response_diag(ZOOM_connection c, Z_DiagRec *p)
1663 if (p->which != Z_DiagRec_defaultFormat)
1664 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1666 response_default_diag(c, p->u.defaultFormat);
1669 ZOOM_API(ZOOM_record)
1670 ZOOM_record_clone(ZOOM_record srec)
1677 odr_enc = odr_createmem(ODR_ENCODE);
1678 if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
1680 buf = odr_getbuf(odr_enc, &size, 0);
1682 nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
1683 nrec->odr = odr_createmem(ODR_DECODE);
1684 nrec->wrbuf_marc = 0;
1685 nrec->wrbuf_iconv = 0;
1686 nrec->wrbuf_opac = 0;
1687 odr_setbuf(nrec->odr, buf, size, 0);
1688 z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
1690 odr_destroy(odr_enc);
1694 ZOOM_API(ZOOM_record)
1695 ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1697 const char *syntax =
1698 ZOOM_options_get(s->options, "preferredRecordSyntax");
1699 const char *elementSetName =
1700 ZOOM_options_get(s->options, "elementSetName");
1702 return record_cache_lookup(s, pos, syntax, elementSetName);
1705 ZOOM_API(ZOOM_record)
1706 ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1708 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1713 * MIKE: I think force_sync should always be zero, but I don't
1714 * want to make this change until I get the go-ahead from
1715 * Adam, in case something depends on the old synchronous
1719 if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1720 ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1721 rec = ZOOM_resultset_record_immediate(r, pos);
1727 ZOOM_record_destroy(ZOOM_record rec)
1731 if (rec->wrbuf_marc)
1732 wrbuf_destroy(rec->wrbuf_marc);
1733 if (rec->wrbuf_iconv)
1734 wrbuf_destroy(rec->wrbuf_iconv);
1735 if (rec->wrbuf_opac)
1736 wrbuf_destroy(rec->wrbuf_opac);
1737 odr_destroy(rec->odr);
1741 static const char *marc_iconv_return(ZOOM_record rec, int marc_type,
1743 const char *buf, int sz,
1744 const char *record_charset)
1749 yaz_marc_t mt = yaz_marc_create();
1752 strcpy(to, "UTF-8");
1753 if (record_charset && *record_charset)
1755 /* Use "from,to" or just "from" */
1756 const char *cp = strchr(record_charset, ',');
1757 int clen = strlen(record_charset);
1760 strncpy( to, cp+1, sizeof(to)-1);
1761 to[sizeof(to)-1] = '\0';
1762 clen = cp - record_charset;
1764 if (clen > sizeof(from)-1)
1765 clen = sizeof(from)-1;
1768 strncpy(from, record_charset, clen);
1774 cd = yaz_iconv_open(to, from);
1775 yaz_marc_iconv(mt, cd);
1778 yaz_marc_xml(mt, marc_type);
1779 if (!rec->wrbuf_marc)
1780 rec->wrbuf_marc = wrbuf_alloc();
1781 wrbuf_rewind(rec->wrbuf_marc);
1782 if (yaz_marc_decode_wrbuf(mt, buf, sz, rec->wrbuf_marc) > 0)
1784 yaz_marc_destroy(mt);
1786 yaz_iconv_close(cd);
1788 *len = wrbuf_len(rec->wrbuf_marc);
1789 return wrbuf_cstr(rec->wrbuf_marc);
1791 yaz_marc_destroy(mt);
1793 yaz_iconv_close(cd);
1797 static const char *record_iconv_return(ZOOM_record rec, int *len,
1798 const char *buf, int sz,
1799 const char *record_charset)
1806 strcpy(to, "UTF-8");
1808 if (record_charset && *record_charset)
1810 /* Use "from,to" or just "from" */
1811 const char *cp = strchr(record_charset, ',');
1812 int clen = strlen(record_charset);
1815 strncpy( to, cp+1, sizeof(to)-1);
1816 to[sizeof(to)-1] = '\0';
1817 clen = cp - record_charset;
1819 if (clen > sizeof(from)-1)
1820 clen = sizeof(from)-1;
1823 strncpy(from, record_charset, clen);
1827 if (*from && *to && (cd = yaz_iconv_open(to, from)))
1829 if (!rec->wrbuf_iconv)
1830 rec->wrbuf_iconv = wrbuf_alloc();
1832 wrbuf_rewind(rec->wrbuf_iconv);
1834 wrbuf_iconv_write(rec->wrbuf_iconv, cd, buf, sz);
1835 wrbuf_iconv_reset(rec->wrbuf_iconv, cd);
1837 buf = wrbuf_cstr(rec->wrbuf_iconv);
1838 sz = wrbuf_len(rec->wrbuf_iconv);
1839 yaz_iconv_close(cd);
1847 ZOOM_record_error(ZOOM_record rec, const char **cp,
1848 const char **addinfo, const char **diagset)
1850 Z_NamePlusRecord *npr;
1855 if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
1857 Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
1858 int error = YAZ_BIB1_UNSPECIFIED_ERROR;
1859 const char *add = 0;
1861 if (diag_rec->which == Z_DiagRec_defaultFormat)
1863 Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
1866 error = *ddf->condition;
1869 case Z_DefaultDiagFormat_v2Addinfo:
1870 add = ddf->u.v2Addinfo;
1872 case Z_DefaultDiagFormat_v3Addinfo:
1873 add = ddf->u.v3Addinfo;
1878 yaz_oid_to_string(yaz_oid_std(),
1879 ddf->diagnosticSetId, &oclass);
1887 *addinfo = add ? add : "";
1889 *cp = diagbib1_str(error);
1895 ZOOM_API(const char *)
1896 ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
1903 Z_NamePlusRecord *npr;
1906 *len = 0; /* default return */
1915 for (i = 0; cp[i] && i < sizeof(type)-1; i++)
1917 if (cp[i] == ';' || cp[i] == ' ')
1923 while (type_spec[i] == ';')
1926 while (type_spec[i] == ' ')
1928 if (!strncmp(type_spec+i, "charset=", 8))
1931 i = i + 8; /* skip charset= */
1932 for (j = 0; type_spec[i] && j < sizeof(charset)-1; i++, j++)
1934 if (type_spec[i] == ';' || type_spec[i] == ' ')
1940 else if (!strncmp(type_spec+i, "xpath=", 6))
1944 for (j = 0; type_spec[i] && j < sizeof(xpath)-1; i++, j++)
1948 while (type_spec[i] == ' ')
1951 if (!strcmp(type, "database"))
1954 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
1955 return npr->databaseName;
1957 else if (!strcmp(type, "syntax"))
1959 const char *desc = 0;
1960 if (npr->which == Z_NamePlusRecord_databaseRecord)
1962 Z_External *r = (Z_External *) npr->u.databaseRecord;
1963 desc = yaz_oid_to_string(yaz_oid_std(), r->direct_reference, 0);
1968 *len = strlen(desc);
1971 if (npr->which != Z_NamePlusRecord_databaseRecord)
1974 /* from now on - we have a database record .. */
1975 if (!strcmp(type, "render"))
1977 Z_External *r = (Z_External *) npr->u.databaseRecord;
1978 const int *oid = r->direct_reference;
1980 /* render bibliographic record .. */
1981 if (r->which == Z_External_OPAC)
1983 r = r->u.opac->bibliographicRecord;
1986 oid = r->direct_reference;
1988 if (r->which == Z_External_sutrs)
1989 return record_iconv_return(rec, len,
1990 (char*) r->u.sutrs->buf,
1993 else if (r->which == Z_External_octet)
1995 if (yaz_oid_is_iso2709(oid))
1997 const char *ret_buf = marc_iconv_return(
1998 rec, YAZ_MARC_LINE, len,
1999 (const char *) r->u.octet_aligned->buf,
2000 r->u.octet_aligned->len,
2005 return record_iconv_return(rec, len,
2006 (const char *) r->u.octet_aligned->buf,
2007 r->u.octet_aligned->len,
2010 else if (r->which == Z_External_grs1)
2012 if (!rec->wrbuf_marc)
2013 rec->wrbuf_marc = wrbuf_alloc();
2014 wrbuf_rewind(rec->wrbuf_marc);
2015 yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
2016 return record_iconv_return(rec, len,
2017 wrbuf_buf(rec->wrbuf_marc),
2018 wrbuf_len(rec->wrbuf_marc),
2023 else if (!strcmp(type, "xml"))
2025 Z_External *r = (Z_External *) npr->u.databaseRecord;
2026 const int *oid = r->direct_reference;
2028 /* render bibliographic record .. */
2029 if (r->which == Z_External_OPAC)
2031 r = r->u.opac->bibliographicRecord;
2034 oid = r->direct_reference;
2037 if (r->which == Z_External_sutrs)
2038 return record_iconv_return(rec, len,
2039 (const char *) r->u.sutrs->buf,
2042 else if (r->which == Z_External_octet)
2044 int marc_decode_type = YAZ_MARC_MARCXML;
2045 if (yaz_oid_is_iso2709(oid))
2047 const char *ret_buf = marc_iconv_return(
2048 rec, marc_decode_type, len,
2049 (const char *) r->u.octet_aligned->buf,
2050 r->u.octet_aligned->len,
2055 return record_iconv_return(rec, len,
2056 (const char *) r->u.octet_aligned->buf,
2057 r->u.octet_aligned->len,
2060 else if (r->which == Z_External_grs1)
2067 else if (!strcmp(type, "raw"))
2069 Z_External *r = (Z_External *) npr->u.databaseRecord;
2071 if (r->which == Z_External_sutrs)
2073 if (len) *len = r->u.sutrs->len;
2074 return (const char *) r->u.sutrs->buf;
2076 else if (r->which == Z_External_octet)
2078 if (len) *len = r->u.octet_aligned->len;
2079 return (const char *) r->u.octet_aligned->buf;
2081 else /* grs-1, explain, OPAC, ... */
2084 return (const char *) npr->u.databaseRecord;
2088 else if (!strcmp (type, "ext"))
2091 return (const char *) npr->u.databaseRecord;
2093 else if (!strcmp (type, "opac"))
2096 Z_External *r = (Z_External *) npr->u.databaseRecord;
2097 if (r->which == Z_External_OPAC)
2099 if (!rec->wrbuf_opac)
2100 rec->wrbuf_opac = wrbuf_alloc();
2101 wrbuf_rewind(rec->wrbuf_opac);
2102 yaz_display_OPAC(rec->wrbuf_opac, r->u.opac, 0);
2103 return record_iconv_return(rec, len,
2104 wrbuf_buf(rec->wrbuf_opac),
2105 wrbuf_len(rec->wrbuf_opac),
2112 static int strcmp_null(const char *v1, const char *v2)
2118 return strcmp(v1, v2);
2121 static size_t record_hash(int pos)
2125 return pos % RECORD_HASH_SIZE;
2128 static void record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
2130 const char *syntax, const char *elementSetName)
2132 ZOOM_record_cache rc;
2134 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2135 ZOOM_connection_put_event(r->connection, event);
2137 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2141 if (strcmp_null(r->schema, rc->schema))
2143 if (strcmp_null(elementSetName,rc->elementSetName))
2145 if (strcmp_null(syntax, rc->syntax))
2147 /* not destroying rc->npr (it's handled by nmem )*/
2149 /* keeping wrbuf_marc too */
2153 rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
2156 rc->rec.wrbuf_marc = 0;
2157 rc->rec.wrbuf_iconv = 0;
2158 rc->rec.wrbuf_opac = 0;
2160 rc->elementSetName = odr_strdup(r->odr, elementSetName);
2162 rc->elementSetName = 0;
2165 rc->syntax = odr_strdup(r->odr, syntax);
2170 rc->schema = odr_strdup(r->odr, r->schema);
2175 rc->next = r->record_hash[record_hash(pos)];
2176 r->record_hash[record_hash(pos)] = rc;
2179 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
2181 const char *elementSetName)
2183 ZOOM_record_cache rc;
2185 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2189 if (strcmp_null(r->schema, rc->schema))
2191 if (strcmp_null(elementSetName,rc->elementSetName))
2193 if (strcmp_null(syntax, rc->syntax))
2201 static void handle_records(ZOOM_connection c, Z_Records *sr,
2204 ZOOM_resultset resultset;
2206 const char *syntax = 0, *elementSetName = 0;
2210 switch (c->tasks->which)
2212 case ZOOM_TASK_SEARCH:
2213 resultset = c->tasks->u.search.resultset;
2214 start = &c->tasks->u.search.start;
2215 count = &c->tasks->u.search.count;
2216 syntax = c->tasks->u.search.syntax;
2217 elementSetName = c->tasks->u.search.elementSetName;
2219 case ZOOM_TASK_RETRIEVE:
2220 resultset = c->tasks->u.retrieve.resultset;
2221 start = &c->tasks->u.retrieve.start;
2222 count = &c->tasks->u.retrieve.count;
2223 syntax = c->tasks->u.retrieve.syntax;
2224 elementSetName = c->tasks->u.retrieve.elementSetName;
2229 if (sr && sr->which == Z_Records_NSD)
2230 response_default_diag(c, sr->u.nonSurrogateDiagnostic);
2231 else if (sr && sr->which == Z_Records_multipleNSD)
2233 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
2234 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
2236 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2240 if (*count + *start > resultset->size)
2241 *count = resultset->size - *start;
2244 if (sr && sr->which == Z_Records_DBOSD)
2247 NMEM nmem = odr_extract_mem(c->odr_in);
2248 Z_NamePlusRecordList *p =
2249 sr->u.databaseOrSurDiagnostics;
2250 for (i = 0; i<p->num_records; i++)
2252 record_cache_add(resultset, p->records[i], i + *start,
2253 syntax, elementSetName);
2259 yaz_log(log_details,
2260 "handle_records resultset=%p start=%d count=%d",
2261 resultset, *start, *count);
2263 /* transfer our response to search_nmem .. we need it later */
2264 nmem_transfer(odr_getmem(resultset->odr), nmem);
2266 if (present_phase && p->num_records == 0)
2268 /* present response and we didn't get any records! */
2269 Z_NamePlusRecord *myrec =
2270 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2271 record_cache_add(resultset, myrec, *start,
2272 syntax, elementSetName);
2275 else if (present_phase)
2277 /* present response and we didn't get any records! */
2278 Z_NamePlusRecord *myrec =
2279 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2280 record_cache_add(resultset, myrec, *start, syntax, elementSetName);
2285 static void handle_present_response(ZOOM_connection c, Z_PresentResponse *pr)
2287 handle_records(c, pr->records, 1);
2290 static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
2293 switch (term->which)
2295 case Z_Term_general:
2296 ZOOM_options_setl(opt, name,
2297 (const char *)(term->u.general->buf),
2298 term->u.general->len);
2300 case Z_Term_characterString:
2301 ZOOM_options_set(opt, name, term->u.characterString);
2303 case Z_Term_numeric:
2304 ZOOM_options_set_int(opt, name, *term->u.numeric);
2309 static void handle_queryExpression(ZOOM_options opt, const char *name,
2310 Z_QueryExpression *exp)
2316 case Z_QueryExpression_term:
2317 if (exp->u.term && exp->u.term->queryTerm)
2319 sprintf(opt_name, "%s.term", name);
2320 handle_queryExpressionTerm(opt, opt_name, exp->u.term->queryTerm);
2323 case Z_QueryExpression_query:
2328 static void handle_searchResult(ZOOM_connection c, ZOOM_resultset resultset,
2329 Z_OtherInformation *o)
2332 for (i = 0; o && i < o->num_elements; i++)
2334 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
2336 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
2338 if (ext->which == Z_External_searchResult1)
2341 Z_SearchInfoReport *sr = ext->u.searchResult1;
2344 ZOOM_options_set_int(
2345 resultset->options, "searchresult.size", sr->num);
2347 for (j = 0; j < sr->num; j++)
2349 Z_SearchInfoReport_s *ent =
2350 ext->u.searchResult1->elements[j];
2353 sprintf(pref, "searchresult.%d", j);
2355 if (ent->subqueryId)
2358 sprintf(opt_name, "%s.id", pref);
2359 ZOOM_options_set(resultset->options, opt_name,
2362 if (ent->subqueryExpression)
2365 sprintf(opt_name, "%s.subquery", pref);
2366 handle_queryExpression(resultset->options, opt_name,
2367 ent->subqueryExpression);
2369 if (ent->subqueryInterpretation)
2372 sprintf(opt_name, "%s.interpretation", pref);
2373 handle_queryExpression(resultset->options, opt_name,
2374 ent->subqueryInterpretation);
2376 if (ent->subqueryRecommendation)
2379 sprintf(opt_name, "%s.recommendation", pref);
2380 handle_queryExpression(resultset->options, opt_name,
2381 ent->subqueryRecommendation);
2383 if (ent->subqueryCount)
2386 sprintf(opt_name, "%s.count", pref);
2387 ZOOM_options_set_int(resultset->options, opt_name,
2388 *ent->subqueryCount);
2396 static void handle_search_response(ZOOM_connection c, Z_SearchResponse *sr)
2398 ZOOM_resultset resultset;
2401 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
2404 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
2405 ZOOM_connection_put_event(c, event);
2407 resultset = c->tasks->u.search.resultset;
2409 if (sr->resultSetStatus)
2411 ZOOM_options_set_int(resultset->options, "resultSetStatus",
2412 *sr->resultSetStatus);
2414 if (sr->presentStatus)
2416 ZOOM_options_set_int(resultset->options, "presentStatus",
2417 *sr->presentStatus);
2419 handle_searchResult(c, resultset, sr->additionalSearchInfo);
2421 resultset->size = *sr->resultCount;
2422 handle_records(c, sr->records, 0);
2425 static void sort_response(ZOOM_connection c, Z_SortResponse *res)
2427 if (res->diagnostics && res->num_diagnostics > 0)
2428 response_diag(c, res->diagnostics[0]);
2431 static int scan_response(ZOOM_connection c, Z_ScanResponse *res)
2433 NMEM nmem = odr_extract_mem(c->odr_in);
2436 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
2438 scan = c->tasks->u.scan.scan;
2440 if (res->entries && res->entries->nonsurrogateDiagnostics)
2441 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
2442 scan->scan_response = res;
2443 nmem_transfer(odr_getmem(scan->odr), nmem);
2445 ZOOM_options_set_int(scan->options, "stepSize", *res->stepSize);
2446 if (res->positionOfTerm)
2447 ZOOM_options_set_int(scan->options, "position", *res->positionOfTerm);
2448 if (res->scanStatus)
2449 ZOOM_options_set_int(scan->options, "scanStatus", *res->scanStatus);
2450 if (res->numberOfEntriesReturned)
2451 ZOOM_options_set_int(scan->options, "number",
2452 *res->numberOfEntriesReturned);
2457 static zoom_ret send_sort(ZOOM_connection c,
2458 ZOOM_resultset resultset)
2461 resultset->r_sort_spec = 0;
2462 if (resultset->r_sort_spec)
2464 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
2465 Z_SortRequest *req = apdu->u.sortRequest;
2467 req->num_inputResultSetNames = 1;
2468 req->inputResultSetNames = (Z_InternationalString **)
2469 odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
2470 req->inputResultSetNames[0] =
2471 odr_strdup(c->odr_out, resultset->setname);
2472 req->sortedResultSetName = odr_strdup(c->odr_out, resultset->setname);
2473 req->sortSequence = resultset->r_sort_spec;
2474 resultset->r_sort_spec = 0;
2475 return send_APDU(c, apdu);
2477 return zoom_complete;
2480 static zoom_ret send_present(ZOOM_connection c)
2483 Z_PresentRequest *req = 0;
2485 const char *syntax = 0;
2486 const char *elementSetName = 0;
2487 ZOOM_resultset resultset;
2492 yaz_log(log_details, "%p send_present no tasks", c);
2493 return zoom_complete;
2496 switch (c->tasks->which)
2498 case ZOOM_TASK_SEARCH:
2499 resultset = c->tasks->u.search.resultset;
2500 start = &c->tasks->u.search.start;
2501 count = &c->tasks->u.search.count;
2502 syntax = c->tasks->u.search.syntax;
2503 elementSetName = c->tasks->u.search.elementSetName;
2505 case ZOOM_TASK_RETRIEVE:
2506 resultset = c->tasks->u.retrieve.resultset;
2507 start = &c->tasks->u.retrieve.start;
2508 count = &c->tasks->u.retrieve.count;
2509 syntax = c->tasks->u.retrieve.syntax;
2510 elementSetName = c->tasks->u.retrieve.elementSetName;
2513 return zoom_complete;
2515 yaz_log(log_details, "%p send_present start=%d count=%d",
2518 if (*start < 0 || *count < 0 || *start + *count > resultset->size)
2520 set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
2523 if (c->error) /* don't continue on error */
2524 return zoom_complete;
2525 yaz_log(log_details, "send_present resultset=%p start=%d count=%d",
2526 resultset, *start, *count);
2528 for (i = 0; i < *count; i++)
2531 record_cache_lookup(resultset, i + *start, syntax, elementSetName);
2536 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2537 ZOOM_connection_put_event(c, event);
2545 yaz_log(log_details, "%p send_present skip=%d no more to fetch", c, i);
2546 return zoom_complete;
2549 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
2550 req = apdu->u.presentRequest;
2553 yaz_log(log_details, "%p send_present skip=%d", c, i);
2555 *req->resultSetStartPoint = *start + 1;
2556 *req->numberOfRecordsRequested = resultset->step>0 ?
2557 resultset->step : *count;
2558 if (*req->numberOfRecordsRequested + *start > resultset->size)
2559 *req->numberOfRecordsRequested = resultset->size - *start;
2560 assert(*req->numberOfRecordsRequested > 0);
2562 if (syntax && *syntax)
2563 req->preferredRecordSyntax =
2564 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
2566 if (resultset->schema && *resultset->schema)
2568 Z_RecordComposition *compo = (Z_RecordComposition *)
2569 odr_malloc(c->odr_out, sizeof(*compo));
2571 req->recordComposition = compo;
2572 compo->which = Z_RecordComp_complex;
2573 compo->u.complex = (Z_CompSpec *)
2574 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
2575 compo->u.complex->selectAlternativeSyntax = (bool_t *)
2576 odr_malloc(c->odr_out, sizeof(bool_t));
2577 *compo->u.complex->selectAlternativeSyntax = 0;
2579 compo->u.complex->generic = (Z_Specification *)
2580 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
2582 compo->u.complex->generic->which = Z_Schema_oid;
2583 compo->u.complex->generic->schema.oid = (Odr_oid *)
2584 zoom_yaz_str_to_z3950oid (c, CLASS_SCHEMA, resultset->schema);
2586 if (!compo->u.complex->generic->schema.oid)
2588 /* OID wasn't a schema! Try record syntax instead. */
2590 compo->u.complex->generic->schema.oid = (Odr_oid *)
2591 zoom_yaz_str_to_z3950oid (c, CLASS_RECSYN, resultset->schema);
2593 if (elementSetName && *elementSetName)
2595 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
2596 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
2597 compo->u.complex->generic->elementSpec->which =
2598 Z_ElementSpec_elementSetName;
2599 compo->u.complex->generic->elementSpec->u.elementSetName =
2600 odr_strdup(c->odr_out, elementSetName);
2603 compo->u.complex->generic->elementSpec = 0;
2604 compo->u.complex->num_dbSpecific = 0;
2605 compo->u.complex->dbSpecific = 0;
2606 compo->u.complex->num_recordSyntax = 0;
2607 compo->u.complex->recordSyntax = 0;
2609 else if (elementSetName && *elementSetName)
2611 Z_ElementSetNames *esn = (Z_ElementSetNames *)
2612 odr_malloc(c->odr_out, sizeof(*esn));
2613 Z_RecordComposition *compo = (Z_RecordComposition *)
2614 odr_malloc(c->odr_out, sizeof(*compo));
2616 esn->which = Z_ElementSetNames_generic;
2617 esn->u.generic = odr_strdup(c->odr_out, elementSetName);
2618 compo->which = Z_RecordComp_simple;
2619 compo->u.simple = esn;
2620 req->recordComposition = compo;
2622 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
2623 return send_APDU(c, apdu);
2626 ZOOM_API(ZOOM_scanset)
2627 ZOOM_connection_scan(ZOOM_connection c, const char *start)
2630 ZOOM_query q = ZOOM_query_create();
2632 ZOOM_query_prefix(q, start);
2634 s = ZOOM_connection_scan1(c, q);
2635 ZOOM_query_destroy(q);
2640 ZOOM_API(ZOOM_scanset)
2641 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
2645 ZOOM_scanset scan = 0;
2648 * We need to check the query-type, so we can recognise CQL and
2649 * CCL and compile them into a form that we can use here. The
2650 * ZOOM_query structure has no explicit `type' member, but
2651 * inspection of the ZOOM_query_prefix() and ZOOM_query_cql()
2652 * functions shows how the structure is set up in each case.
2656 else if (q->z_query->which == Z_Query_type_1)
2658 yaz_log(log_api, "%p ZOOM_connection_scan1 q=%p PQF '%s'",
2659 c, q, q->query_string);
2660 start = q->query_string;
2662 else if (q->z_query->which == Z_Query_type_104)
2664 yaz_log(log_api, "%p ZOOM_connection_scan1 q=%p CQL '%s'",
2665 c, q, q->query_string);
2666 start = freeme = cql2pqf(c, q->query_string);
2672 yaz_log(YLOG_FATAL, "%p ZOOM_connection_scan1 q=%p unknown type '%s'",
2673 c, q, q->query_string);
2677 scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
2678 scan->connection = c;
2679 scan->odr = odr_createmem(ODR_DECODE);
2680 scan->options = ZOOM_options_create_with_parent(c->options);
2682 scan->scan_response = 0;
2683 scan->termListAndStartPoint =
2684 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet, start);
2687 scan->databaseNames = set_DatabaseNames(c, c->options,
2688 &scan->num_databaseNames,
2690 if (scan->termListAndStartPoint != 0)
2692 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
2693 task->u.scan.scan = scan;
2698 while (ZOOM_event(1, &c))
2706 ZOOM_scanset_destroy(ZOOM_scanset scan)
2711 if (scan->refcount == 0)
2713 odr_destroy(scan->odr);
2715 ZOOM_options_destroy(scan->options);
2720 static zoom_ret send_package(ZOOM_connection c)
2724 yaz_log(log_details, "%p send_package", c);
2726 return zoom_complete;
2727 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
2729 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
2730 ZOOM_connection_put_event(c, event);
2732 c->buf_out = c->tasks->u.package->buf_out;
2733 c->len_out = c->tasks->u.package->len_out;
2738 static zoom_ret send_scan(ZOOM_connection c)
2741 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
2742 Z_ScanRequest *req = apdu->u.scanRequest;
2744 yaz_log(log_details, "%p send_scan", c);
2746 return zoom_complete;
2747 assert (c->tasks->which == ZOOM_TASK_SCAN);
2748 scan = c->tasks->u.scan.scan;
2750 req->termListAndStartPoint = scan->termListAndStartPoint;
2751 req->attributeSet = scan->attributeSet;
2753 *req->numberOfTermsRequested =
2754 ZOOM_options_get_int(scan->options, "number", 10);
2756 req->preferredPositionInResponse =
2757 odr_intdup(c->odr_out,
2758 ZOOM_options_get_int(scan->options, "position", 1));
2761 odr_intdup(c->odr_out,
2762 ZOOM_options_get_int(scan->options, "stepSize", 0));
2764 req->databaseNames = scan->databaseNames;
2765 req->num_databaseNames = scan->num_databaseNames;
2767 return send_APDU(c, apdu);
2771 ZOOM_scanset_size(ZOOM_scanset scan)
2773 if (!scan || !scan->scan_response || !scan->scan_response->entries)
2775 return scan->scan_response->entries->num_entries;
2778 ZOOM_API(const char *)
2779 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
2782 const char *term = 0;
2783 size_t noent = ZOOM_scanset_size(scan);
2784 Z_ScanResponse *res = scan->scan_response;
2790 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
2792 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
2794 if (t->term->which == Z_Term_general)
2796 term = (const char *) t->term->u.general->buf;
2797 *len = t->term->u.general->len;
2799 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
2804 ZOOM_API(const char *)
2805 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
2808 const char *term = 0;
2809 size_t noent = ZOOM_scanset_size(scan);
2810 Z_ScanResponse *res = scan->scan_response;
2816 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
2818 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
2822 term = t->displayTerm;
2823 *len = strlen(term);
2825 else if (t->term->which == Z_Term_general)
2827 term = (const char *) t->term->u.general->buf;
2828 *len = t->term->u.general->len;
2830 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
2835 ZOOM_API(const char *)
2836 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
2838 return ZOOM_options_get(scan->options, key);
2842 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
2845 ZOOM_options_set(scan->options, key, val);
2848 static Z_APDU *create_es_package(ZOOM_package p, const int *oid)
2851 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
2852 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
2854 *req->function = Z_ExtendedServicesRequest_create;
2856 str = ZOOM_options_get(p->options, "package-name");
2858 req->packageName = odr_strdup(p->odr_out, str);
2860 str = ZOOM_options_get(p->options, "user-id");
2862 req->userId = odr_strdup(p->odr_out, str);
2864 req->packageType = odr_oiddup(p->odr_out, oid);
2866 str = ZOOM_options_get(p->options, "function");
2869 if (!strcmp (str, "create"))
2871 if (!strcmp (str, "delete"))
2873 if (!strcmp (str, "modify"))
2879 static const char *ill_array_lookup(void *clientData, const char *idx)
2881 ZOOM_package p = (ZOOM_package) clientData;
2882 return ZOOM_options_get(p->options, idx+4);
2885 static Z_External *encode_ill_request(ZOOM_package p)
2887 ODR out = p->odr_out;
2890 struct ill_get_ctl ctl;
2892 ctl.odr = p->odr_out;
2894 ctl.f = ill_array_lookup;
2896 req = ill_get_ILLRequest(&ctl, "ill", 0);
2898 if (!ill_Request(out, &req, 0, 0))
2900 int ill_request_size;
2901 char *ill_request_buf = odr_getbuf(out, &ill_request_size, 0);
2902 if (ill_request_buf)
2903 odr_setbuf(out, ill_request_buf, ill_request_size, 1);
2908 int illRequest_size = 0;
2909 char *illRequest_buf = odr_getbuf(out, &illRequest_size, 0);
2911 r = (Z_External *) odr_malloc(out, sizeof(*r));
2912 r->direct_reference = odr_oiddup(out, yaz_oid_general_isoill_1);
2913 r->indirect_reference = 0;
2915 r->which = Z_External_single;
2917 r->u.single_ASN1_type =
2918 odr_create_Odr_oct(out,
2919 (unsigned char *)illRequest_buf,
2925 static Z_ItemOrder *encode_item_order(ZOOM_package p)
2927 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc(p->odr_out, sizeof(*req));
2930 req->which = Z_IOItemOrder_esRequest;
2931 req->u.esRequest = (Z_IORequest *)
2932 odr_malloc(p->odr_out,sizeof(Z_IORequest));
2934 /* to keep part ... */
2935 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
2936 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
2937 req->u.esRequest->toKeep->supplDescription = 0;
2938 req->u.esRequest->toKeep->contact = (Z_IOContact *)
2939 odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
2941 str = ZOOM_options_get(p->options, "contact-name");
2942 req->u.esRequest->toKeep->contact->name = str ?
2943 odr_strdup(p->odr_out, str) : 0;
2945 str = ZOOM_options_get(p->options, "contact-phone");
2946 req->u.esRequest->toKeep->contact->phone = str ?
2947 odr_strdup(p->odr_out, str) : 0;
2949 str = ZOOM_options_get(p->options, "contact-email");
2950 req->u.esRequest->toKeep->contact->email = str ?
2951 odr_strdup(p->odr_out, str) : 0;
2953 req->u.esRequest->toKeep->addlBilling = 0;
2955 /* not to keep part ... */
2956 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
2957 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
2959 str = ZOOM_options_get(p->options, "itemorder-setname");
2964 req->u.esRequest->notToKeep->resultSetItem = 0;
2967 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
2968 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
2970 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
2971 odr_strdup(p->odr_out, str);
2972 req->u.esRequest->notToKeep->resultSetItem->item =
2973 (int *) odr_malloc(p->odr_out, sizeof(int));
2975 str = ZOOM_options_get(p->options, "itemorder-item");
2976 *req->u.esRequest->notToKeep->resultSetItem->item =
2977 (str ? atoi(str) : 1);
2980 str = ZOOM_options_get(p->options, "doc");
2983 req->u.esRequest->notToKeep->itemRequest =
2984 z_ext_record_xml(p->odr_out, str, strlen(str));
2987 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
2992 Z_APDU *create_admin_package(ZOOM_package p, int type,
2993 Z_ESAdminOriginPartToKeep **toKeepP,
2994 Z_ESAdminOriginPartNotToKeep **notToKeepP)
2996 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_admin);
2999 Z_ESAdminOriginPartToKeep *toKeep;
3000 Z_ESAdminOriginPartNotToKeep *notToKeep;
3001 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3002 const char *first_db = "Default";
3004 char **db = set_DatabaseNames(p->connection, p->options, &num_db,
3009 r->direct_reference = odr_oiddup(p->odr_out, yaz_oid_extserv_admin);
3011 r->indirect_reference = 0;
3012 r->which = Z_External_ESAdmin;
3014 r->u.adminService = (Z_Admin *)
3015 odr_malloc(p->odr_out, sizeof(*r->u.adminService));
3016 r->u.adminService->which = Z_Admin_esRequest;
3017 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
3018 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest));
3020 toKeep = r->u.adminService->u.esRequest->toKeep =
3021 (Z_ESAdminOriginPartToKeep *)
3022 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep));
3023 toKeep->which = type;
3024 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3025 toKeep->u.create = odr_nullval();
3026 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3028 r->u.adminService->u.esRequest->notToKeep = notToKeep =
3029 (Z_ESAdminOriginPartNotToKeep *)
3030 odr_malloc(p->odr_out,
3031 sizeof(*r->u.adminService->u.esRequest->notToKeep));
3032 notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
3033 notToKeep->u.recordsWillFollow = odr_nullval();
3037 *notToKeepP = notToKeep;
3042 static Z_APDU *create_xmlupdate_package(ZOOM_package p)
3044 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_xml_es);
3045 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3046 Z_External *ext = (Z_External *) odr_malloc(p->odr_out, sizeof(*ext));
3047 const char *doc = ZOOM_options_get(p->options, "doc");
3052 req->taskSpecificParameters = ext;
3053 ext->direct_reference = req->packageType;
3054 ext->descriptor = 0;
3055 ext->indirect_reference = 0;
3057 ext->which = Z_External_octet;
3058 ext->u.single_ASN1_type =
3059 odr_create_Odr_oct(p->odr_out, (const unsigned char *) doc,
3064 static Z_APDU *create_update_package(ZOOM_package p)
3067 const char *first_db = "Default";
3069 char **db = set_DatabaseNames(p->connection, p->options, &num_db, p->odr_out);
3070 const char *action = ZOOM_options_get(p->options, "action");
3071 const char *recordIdOpaque = ZOOM_options_get(p->options, "recordIdOpaque");
3072 const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
3073 const char *record_buf = ZOOM_options_get(p->options, "record");
3074 const char *syntax_str = ZOOM_options_get(p->options, "syntax");
3076 int *syntax_oid = 0;
3082 record_buf = "void";
3083 syntax_str = "SUTRS";
3088 syntax_oid = yaz_string_to_oid_odr(yaz_oid_std(),
3089 CLASS_RECSYN, syntax_str,
3099 action = "specialUpdate";
3101 if (!strcmp(action, "recordInsert"))
3102 action_no = Z_IUOriginPartToKeep_recordInsert;
3103 else if (!strcmp(action, "recordReplace"))
3104 action_no = Z_IUOriginPartToKeep_recordReplace;
3105 else if (!strcmp(action, "recordDelete"))
3106 action_no = Z_IUOriginPartToKeep_recordDelete;
3107 else if (!strcmp(action, "elementUpdate"))
3108 action_no = Z_IUOriginPartToKeep_elementUpdate;
3109 else if (!strcmp(action, "specialUpdate"))
3110 action_no = Z_IUOriginPartToKeep_specialUpdate;
3114 apdu = create_es_package(p, yaz_oid_extserv_database_update);
3117 Z_IUOriginPartToKeep *toKeep;
3118 Z_IUSuppliedRecords *notToKeep;
3119 Z_External *r = (Z_External *)
3120 odr_malloc(p->odr_out, sizeof(*r));
3122 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3125 r->direct_reference = odr_oiddup(p->odr_out,
3126 yaz_oid_extserv_database_update);
3128 r->which = Z_External_update;
3129 r->indirect_reference = 0;
3130 r->u.update = (Z_IUUpdate *)
3131 odr_malloc(p->odr_out, sizeof(*r->u.update));
3133 r->u.update->which = Z_IUUpdate_esRequest;
3134 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
3135 odr_malloc(p->odr_out, sizeof(*r->u.update->u.esRequest));
3136 toKeep = r->u.update->u.esRequest->toKeep =
3137 (Z_IUOriginPartToKeep *)
3138 odr_malloc(p->odr_out, sizeof(*toKeep));
3140 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3142 toKeep->elementSetName = 0;
3143 toKeep->actionQualifier = 0;
3144 toKeep->action = odr_intdup(p->odr_out, action_no);
3146 notToKeep = r->u.update->u.esRequest->notToKeep =
3147 (Z_IUSuppliedRecords *)
3148 odr_malloc(p->odr_out, sizeof(*notToKeep));
3150 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
3151 odr_malloc(p->odr_out, sizeof(*notToKeep->elements));
3152 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
3153 odr_malloc(p->odr_out, sizeof(**notToKeep->elements));
3154 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
3157 notToKeep->elements[0]->u.opaque =
3158 odr_create_Odr_oct(p->odr_out,
3159 (const unsigned char *) recordIdOpaque,
3160 strlen(recordIdOpaque));
3162 else if (recordIdNumber)
3164 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number;
3166 notToKeep->elements[0]->u.number =
3167 odr_intdup(p->odr_out, atoi(recordIdNumber));
3170 notToKeep->elements[0]->u.opaque = 0;
3171 notToKeep->elements[0]->supplementalId = 0;
3172 notToKeep->elements[0]->correlationInfo = 0;
3173 notToKeep->elements[0]->record =
3174 z_ext_record_oid(p->odr_out, syntax_oid,
3175 record_buf, strlen(record_buf));
3179 ODR print = odr_createmem(ODR_PRINT);
3181 z_APDU(print, &apdu, 0, 0);
3188 ZOOM_package_send(ZOOM_package p, const char *type)
3195 odr_reset(p->odr_out);
3198 if (!strcmp(type, "itemorder"))
3200 apdu = create_es_package(p, yaz_oid_extserv_item_order);
3203 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3205 r->direct_reference =
3206 odr_oiddup(p->odr_out, yaz_oid_extserv_item_order);
3208 r->which = Z_External_itemOrder;
3209 r->indirect_reference = 0;
3210 r->u.itemOrder = encode_item_order(p);
3212 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3215 else if (!strcmp(type, "create")) /* create database */
3217 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
3220 else if (!strcmp(type, "drop")) /* drop database */
3222 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
3225 else if (!strcmp(type, "commit")) /* commit changes */
3227 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit,
3230 else if (!strcmp(type, "update")) /* update record(s) */
3232 apdu = create_update_package(p);
3234 else if (!strcmp(type, "xmlupdate"))
3236 apdu = create_xmlupdate_package(p);
3240 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
3244 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_PACKAGE);
3245 task->u.package = p;
3246 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
3247 p->buf_out = (char *) xmalloc(p->len_out);
3248 memcpy(p->buf_out, buf, p->len_out);
3253 while (ZOOM_event(1, &c))
3260 ZOOM_API(ZOOM_package)
3261 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
3263 ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
3266 p->odr_out = odr_createmem(ODR_ENCODE);
3267 p->options = ZOOM_options_create_with_parent2(options, c->options);
3275 ZOOM_package_destroy(ZOOM_package p)
3280 if (p->refcount == 0)
3282 odr_destroy(p->odr_out);
3285 ZOOM_options_destroy(p->options);
3290 ZOOM_API(const char *)
3291 ZOOM_package_option_get(ZOOM_package p, const char *key)
3293 return ZOOM_options_get(p->options, key);
3298 ZOOM_package_option_set(ZOOM_package p, const char *key,
3301 ZOOM_options_set(p->options, key, val);
3304 static int ZOOM_connection_exec_task(ZOOM_connection c)
3306 ZOOM_task task = c->tasks;
3307 zoom_ret ret = zoom_complete;
3311 yaz_log(log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
3312 c, task->which, task->running);
3313 if (c->error != ZOOM_ERROR_NONE)
3315 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3316 "removing tasks because of error = %d", c, c->error);
3317 ZOOM_connection_remove_tasks(c);
3322 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3323 "task already running", c);
3327 ret = zoom_complete;
3328 if (c->cs || task->which == ZOOM_TASK_CONNECT)
3330 switch (task->which)
3332 case ZOOM_TASK_SEARCH:
3333 if (c->proto == PROTO_HTTP)
3334 ret = ZOOM_connection_srw_send_search(c);
3336 ret = ZOOM_connection_send_search(c);
3338 case ZOOM_TASK_RETRIEVE:
3339 if (c->proto == PROTO_HTTP)
3340 ret = ZOOM_connection_srw_send_search(c);
3342 ret = send_present(c);
3344 case ZOOM_TASK_CONNECT:
3345 ret = do_connect(c);
3347 case ZOOM_TASK_SCAN:
3350 case ZOOM_TASK_PACKAGE:
3351 ret = send_package(c);
3353 case ZOOM_TASK_SORT:
3354 c->tasks->u.sort.resultset->r_sort_spec =
3355 c->tasks->u.sort.q->sort_spec;
3356 ret = send_sort(c, c->tasks->u.sort.resultset);
3362 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3363 "remove tasks because no connection exist", c);
3364 ZOOM_connection_remove_tasks(c);
3366 if (ret == zoom_complete)
3368 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3369 "task removed (complete)", c);
3370 ZOOM_connection_remove_task(c);
3373 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3378 static zoom_ret send_sort_present(ZOOM_connection c)
3380 zoom_ret r = zoom_complete;
3382 if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
3383 r = send_sort(c, c->tasks->u.search.resultset);
3384 if (r == zoom_complete)
3385 r = send_present(c);
3389 static int es_response(ZOOM_connection c,
3390 Z_ExtendedServicesResponse *res)
3392 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
3394 if (res->diagnostics && res->num_diagnostics > 0)
3395 response_diag(c, res->diagnostics[0]);
3396 if (res->taskPackage &&
3397 res->taskPackage->which == Z_External_extendedService)
3399 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
3400 Odr_oct *id = taskPackage->targetReference;
3403 ZOOM_options_setl(c->tasks->u.package->options,
3404 "targetReference", (char*) id->buf, id->len);
3406 if (res->taskPackage &&
3407 res->taskPackage->which == Z_External_octet)
3409 Odr_oct *doc = res->taskPackage->u.octet_aligned;
3410 ZOOM_options_setl(c->tasks->u.package->options,
3411 "xmlUpdateDoc", (char*) doc->buf, doc->len);
3416 static void interpret_init_diag(ZOOM_connection c,
3417 Z_DiagnosticFormat *diag)
3421 Z_DiagnosticFormat_s *ds = diag->elements[0];
3422 if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
3423 response_default_diag(c, ds->u.defaultDiagRec);
3428 static void interpret_otherinformation_field(ZOOM_connection c,
3429 Z_OtherInformation *ui)
3432 for (i = 0; i < ui->num_elements; i++)
3434 Z_OtherInformationUnit *unit = ui->list[i];
3435 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
3436 unit->information.externallyDefinedInfo &&
3437 unit->information.externallyDefinedInfo->which ==
3440 interpret_init_diag(c, unit->information.externallyDefinedInfo->u.diag1);
3446 static void set_init_option(const char *name, void *clientData) {
3447 ZOOM_connection c = clientData;
3450 sprintf(buf, "init_opt_%.70s", name);
3451 ZOOM_connection_option_set(c, buf, "1");
3455 static void recv_apdu(ZOOM_connection c, Z_APDU *apdu)
3457 Z_InitResponse *initrs;
3459 ZOOM_connection_set_mask(c, 0);
3460 yaz_log(log_details, "%p recv_apdu apdu->which=%d", c, apdu->which);
3463 case Z_APDU_initResponse:
3464 yaz_log(log_api, "%p recv_apd: Received Init response", c);
3465 initrs = apdu->u.initResponse;
3466 ZOOM_connection_option_set(c, "serverImplementationId",
3467 initrs->implementationId ?
3468 initrs->implementationId : "");
3469 ZOOM_connection_option_set(c, "serverImplementationName",
3470 initrs->implementationName ?
3471 initrs->implementationName : "");
3472 ZOOM_connection_option_set(c, "serverImplementationVersion",
3473 initrs->implementationVersion ?
3474 initrs->implementationVersion : "");
3475 /* Set the three old options too, for old applications */
3476 ZOOM_connection_option_set(c, "targetImplementationId",
3477 initrs->implementationId ?
3478 initrs->implementationId : "");
3479 ZOOM_connection_option_set(c, "targetImplementationName",
3480 initrs->implementationName ?
3481 initrs->implementationName : "");
3482 ZOOM_connection_option_set(c, "targetImplementationVersion",
3483 initrs->implementationVersion ?
3484 initrs->implementationVersion : "");
3486 /* Make initrs->options available as ZOOM-level options */
3487 yaz_init_opt_decode(initrs->options, set_init_option, (void*) c);
3489 if (!*initrs->result)
3491 Z_External *uif = initrs->userInformationField;
3493 set_ZOOM_error(c, ZOOM_ERROR_INIT, 0); /* default error */
3495 if (uif && uif->which == Z_External_userInfo1)
3496 interpret_otherinformation_field(c, uif->u.userInfo1);
3501 yaz_oi_get_string_oid(&apdu->u.initResponse->otherInfo,
3502 yaz_oid_userinfo_cookie, 1, 0);
3503 xfree(c->cookie_in);
3506 c->cookie_in = xstrdup(cookie);
3507 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
3508 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
3509 c->support_named_resultsets = 1;
3512 assert(c->tasks->which == ZOOM_TASK_CONNECT);
3513 ZOOM_connection_remove_task(c);
3515 ZOOM_connection_exec_task(c);
3517 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
3519 NMEM tmpmem = nmem_create();
3520 Z_CharSetandLanguageNegotiation *p =
3521 yaz_get_charneg_record(initrs->otherInfo);
3525 char *charset = NULL, *lang = NULL;
3528 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
3529 yaz_log(log_details, "%p recv_apdu target accepted: "
3530 "charset %s, language %s, select %d",
3532 charset ? charset : "none", lang ? lang : "none", sel);
3534 ZOOM_connection_option_set(c, "negotiation-charset",
3537 ZOOM_connection_option_set(c, "negotiation-lang",
3540 ZOOM_connection_option_set(
3541 c, "negotiation-charset-in-effect-for-records",
3542 (sel != 0) ? "1" : "0");
3543 nmem_destroy(tmpmem);
3547 case Z_APDU_searchResponse:
3548 yaz_log(log_api, "%p recv_apdu Search response", c);
3549 handle_search_response(c, apdu->u.searchResponse);
3550 if (send_sort_present(c) == zoom_complete)
3551 ZOOM_connection_remove_task(c);
3553 case Z_APDU_presentResponse:
3554 yaz_log(log_api, "%p recv_apdu Present response", c);
3555 handle_present_response(c, apdu->u.presentResponse);
3556 if (send_present(c) == zoom_complete)
3557 ZOOM_connection_remove_task(c);
3559 case Z_APDU_sortResponse:
3560 yaz_log(log_api, "%p recv_apdu Sort response", c);
3561 sort_response(c, apdu->u.sortResponse);
3562 if (send_present(c) == zoom_complete)
3563 ZOOM_connection_remove_task(c);
3565 case Z_APDU_scanResponse:
3566 yaz_log(log_api, "%p recv_apdu Scan response", c);
3567 scan_response(c, apdu->u.scanResponse);
3568 ZOOM_connection_remove_task(c);
3570 case Z_APDU_extendedServicesResponse:
3571 yaz_log(log_api, "%p recv_apdu Extended Services response", c);
3572 es_response(c, apdu->u.extendedServicesResponse);
3573 ZOOM_connection_remove_task(c);
3576 yaz_log(log_api, "%p recv_apdu Close PDU", c);
3577 if (!ZOOM_test_reconnect(c))
3579 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
3584 yaz_log(log_api, "%p Received unknown PDU", c);
3585 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
3591 static void handle_srw_response(ZOOM_connection c,
3592 Z_SRW_searchRetrieveResponse *res)
3594 ZOOM_resultset resultset = 0;
3599 const char *syntax, *elementSetName;
3604 switch(c->tasks->which)
3606 case ZOOM_TASK_SEARCH:
3607 resultset = c->tasks->u.search.resultset;
3608 start = &c->tasks->u.search.start;
3609 syntax = c->tasks->u.search.syntax;
3610 elementSetName = c->tasks->u.search.elementSetName;
3612 case ZOOM_TASK_RETRIEVE:
3613 resultset = c->tasks->u.retrieve.resultset;
3614 start = &c->tasks->u.retrieve.start;
3615 syntax = c->tasks->u.retrieve.syntax;
3616 elementSetName = c->tasks->u.retrieve.elementSetName;
3621 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
3622 ZOOM_connection_put_event(c, event);
3624 resultset->size = 0;
3626 yaz_log(log_details, "%p handle_srw_response got SRW response OK", c);
3628 if (res->numberOfRecords)
3629 resultset->size = *res->numberOfRecords;
3631 for (i = 0; i<res->num_records; i++)
3635 Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
3636 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
3638 if (res->records[i].recordPosition &&
3639 *res->records[i].recordPosition > 0)
3640 pos = *res->records[i].recordPosition - 1;
3644 npr->databaseName = 0;
3645 npr->which = Z_NamePlusRecord_databaseRecord;
3646 npr->u.databaseRecord = (Z_External *)
3647 odr_malloc(c->odr_in, sizeof(Z_External));
3648 npr->u.databaseRecord->descriptor = 0;
3649 npr->u.databaseRecord->direct_reference =
3650 odr_oiddup(c->odr_in, yaz_oid_recsyn_xml);
3651 npr->u.databaseRecord->which = Z_External_octet;
3653 npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
3654 odr_malloc(c->odr_in, sizeof(Odr_oct));
3655 npr->u.databaseRecord->u.octet_aligned->buf = (unsigned char*)
3656 res->records[i].recordData_buf;
3657 npr->u.databaseRecord->u.octet_aligned->len =
3658 npr->u.databaseRecord->u.octet_aligned->size =
3659 res->records[i].recordData_len;
3660 record_cache_add(resultset, npr, pos, syntax, elementSetName);
3662 if (res->num_diagnostics > 0)
3664 const char *uri = res->diagnostics[0].uri;
3669 if ((cp = strrchr(uri, '/')))
3671 set_dset_error(c, code, uri,
3672 res->diagnostics[0].details, 0);
3675 nmem = odr_extract_mem(c->odr_in);
3676 nmem_transfer(odr_getmem(resultset->odr), nmem);
3682 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
3685 const char *content_type = z_HTTP_header_lookup(hres->headers,
3687 const char *connection_head = z_HTTP_header_lookup(hres->headers,
3689 ZOOM_connection_set_mask(c, 0);
3690 yaz_log(log_details, "%p handle_http", c);
3692 if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
3694 Z_SOAP *soap_package = 0;
3696 Z_SOAP_Handler soap_handlers[2] = {
3697 {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
3700 ret = z_soap_codec(o, &soap_package,
3701 &hres->content_buf, &hres->content_len,
3703 if (!ret && soap_package->which == Z_SOAP_generic &&
3704 soap_package->u.generic->no == 0)
3706 Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
3707 if (sr->which == Z_SRW_searchRetrieve_response)
3708 handle_srw_response(c, sr->u.response);
3712 else if (!ret && (soap_package->which == Z_SOAP_fault
3713 || soap_package->which == Z_SOAP_error))
3715 set_HTTP_error(c, hres->code,
3716 soap_package->u.fault->fault_code,
3717 soap_package->u.fault->fault_string);
3724 if (hres->code != 200)
3725 set_HTTP_error(c, hres->code, 0, 0);
3727 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
3730 ZOOM_connection_remove_task(c);
3731 if (!strcmp(hres->version, "1.0"))
3733 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
3734 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
3739 /* HTTP 1.1: only if no close we stay alive .. */
3740 if (connection_head && !strcmp(connection_head, "close"))
3746 static int do_read(ZOOM_connection c)
3751 event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
3752 ZOOM_connection_put_event(c, event);
3754 r = cs_get(c->cs, &c->buf_in, &c->len_in);
3755 more = cs_more(c->cs);
3756 yaz_log(log_details, "%p do_read len=%d more=%d", c, r, more);
3761 if (!ZOOM_test_reconnect(c))
3763 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
3772 odr_reset(c->odr_in);
3773 odr_setbuf(c->odr_in, c->buf_in, r, 0);
3774 event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
3775 ZOOM_connection_put_event(c, event);
3777 if (!z_GDU(c->odr_in, &gdu, 0, 0))
3780 int err = odr_geterrorx(c->odr_in, &x);
3782 const char *element = odr_getelement(c->odr_in);
3783 sprintf(msg, "ODR code %d:%d element=%-20s",
3784 err, x, element ? element : "<unknown>");
3785 set_ZOOM_error(c, ZOOM_ERROR_DECODE, msg);
3788 else if (gdu->which == Z_GDU_Z3950)
3789 recv_apdu(c, gdu->u.z3950);
3790 else if (gdu->which == Z_GDU_HTTP_Response)
3793 handle_http(c, gdu->u.HTTP_Response);
3795 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
3799 c->reconnect_ok = 0;
3804 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
3809 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
3810 ZOOM_connection_put_event(c, event);
3812 yaz_log(log_details, "%p do_write_ex len=%d", c, len_out);
3813 if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
3815 yaz_log(log_details, "%p do_write_ex write failed", c);
3816 if (ZOOM_test_reconnect(c))
3818 return zoom_pending;
3820 if (c->state == STATE_CONNECTING)
3821 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
3823 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
3825 return zoom_complete;
3829 int mask = ZOOM_SELECT_EXCEPT;
3830 if (c->cs->io_pending & CS_WANT_WRITE)
3831 mask += ZOOM_SELECT_WRITE;
3832 if (c->cs->io_pending & CS_WANT_READ)
3833 mask += ZOOM_SELECT_READ;
3834 ZOOM_connection_set_mask(c, mask);
3835 yaz_log(log_details, "%p do_write_ex write incomplete mask=%d",
3840 ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
3841 yaz_log(log_details, "%p do_write_ex write complete mask=%d",
3844 return zoom_pending;
3847 static zoom_ret do_write(ZOOM_connection c)
3849 return do_write_ex(c, c->buf_out, c->len_out);
3853 ZOOM_API(const char *)
3854 ZOOM_connection_option_get(ZOOM_connection c, const char *key)
3856 return ZOOM_options_get(c->options, key);
3859 ZOOM_API(const char *)
3860 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
3862 return ZOOM_options_getl(c->options, key, lenp);
3866 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
3869 ZOOM_options_set(c->options, key, val);
3873 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
3874 const char *val, int len)
3876 ZOOM_options_setl(c->options, key, val, len);
3879 ZOOM_API(const char *)
3880 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
3882 return ZOOM_options_get(r->options, key);
3886 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
3889 ZOOM_options_set(r->options, key, val);
3894 ZOOM_connection_errcode(ZOOM_connection c)
3896 return ZOOM_connection_error(c, 0, 0);
3899 ZOOM_API(const char *)
3900 ZOOM_connection_errmsg(ZOOM_connection c)
3903 ZOOM_connection_error(c, &msg, 0);
3907 ZOOM_API(const char *)
3908 ZOOM_connection_addinfo(ZOOM_connection c)
3910 const char *addinfo;
3911 ZOOM_connection_error(c, 0, &addinfo);
3915 ZOOM_API(const char *)
3916 ZOOM_connection_diagset(ZOOM_connection c)
3918 const char *diagset;
3919 ZOOM_connection_error_x(c, 0, 0, &diagset);
3923 ZOOM_API(const char *)
3924 ZOOM_diag_str(int error)
3928 case ZOOM_ERROR_NONE:
3930 case ZOOM_ERROR_CONNECT:
3931 return "Connect failed";
3932 case ZOOM_ERROR_MEMORY:
3933 return "Out of memory";
3934 case ZOOM_ERROR_ENCODE:
3935 return "Encoding failed";
3936 case ZOOM_ERROR_DECODE:
3937 return "Decoding failed";
3938 case ZOOM_ERROR_CONNECTION_LOST:
3939 return "Connection lost";
3940 case ZOOM_ERROR_INIT:
3941 return "Init rejected";
3942 case ZOOM_ERROR_INTERNAL:
3943 return "Internal failure";
3944 case ZOOM_ERROR_TIMEOUT:
3946 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
3947 return "Unsupported protocol";
3948 case ZOOM_ERROR_UNSUPPORTED_QUERY:
3949 return "Unsupported query type";
3950 case ZOOM_ERROR_INVALID_QUERY:
3951 return "Invalid query";
3952 case ZOOM_ERROR_CQL_PARSE:
3953 return "CQL parsing error";
3954 case ZOOM_ERROR_CQL_TRANSFORM:
3955 return "CQL transformation error";
3956 case ZOOM_ERROR_CCL_CONFIG:
3957 return "CCL configuration error";
3958 case ZOOM_ERROR_CCL_PARSE:
3959 return "CCL parsing error";
3961 return diagbib1_str(error);
3966 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
3967 const char **addinfo, const char **diagset)
3969 int error = c->error;
3972 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
3973 *cp = ZOOM_diag_str(error);
3974 else if (!strcmp(c->diagset, "HTTP"))
3975 *cp = z_HTTP_errmsg(c->error);
3976 else if (!strcmp(c->diagset, "Bib-1"))
3977 *cp = ZOOM_diag_str(error);
3978 else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
3979 *cp = yaz_diag_srw_str(c->error);
3981 *cp = "Unknown error and diagnostic set";
3984 *addinfo = c->addinfo ? c->addinfo : "";
3986 *diagset = c->diagset ? c->diagset : "";
3991 ZOOM_connection_error(ZOOM_connection c, const char **cp,
3992 const char **addinfo)
3994 return ZOOM_connection_error_x(c, cp, addinfo, 0);
3997 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
3999 ZOOM_Event event = 0;
4000 int r = cs_look(c->cs);
4001 yaz_log(log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
4006 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4007 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4009 ZOOM_connection_put_event(c, event);
4011 else if (r == CS_CONNECT)
4013 int ret = ret = cs_rcvconnect(c->cs);
4014 yaz_log(log_details, "%p ZOOM_connection_do_io "
4015 "cs_rcvconnect returned %d", c, ret);
4018 int mask = ZOOM_SELECT_EXCEPT;
4019 if (c->cs->io_pending & CS_WANT_WRITE)
4020 mask += ZOOM_SELECT_WRITE;
4021 if (c->cs->io_pending & CS_WANT_READ)
4022 mask += ZOOM_SELECT_READ;
4023 ZOOM_connection_set_mask(c, mask);
4027 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4028 ZOOM_connection_put_event(c, event);
4030 if (c->proto == PROTO_Z3950)
4031 ZOOM_connection_send_init(c);
4034 /* no init request for SRW .. */
4035 assert(c->tasks->which == ZOOM_TASK_CONNECT);
4036 ZOOM_connection_remove_task(c);
4037 ZOOM_connection_set_mask(c, 0);
4038 ZOOM_connection_exec_task(c);
4040 c->state = STATE_ESTABLISHED;
4044 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4050 if (mask & ZOOM_SELECT_EXCEPT)
4052 if (!ZOOM_test_reconnect(c))
4054 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4059 if (mask & ZOOM_SELECT_READ)
4061 if (c->cs && (mask & ZOOM_SELECT_WRITE))
4067 ZOOM_connection_last_event(ZOOM_connection cs)
4070 return ZOOM_EVENT_NONE;
4071 return cs->last_event;
4075 static void cql2pqf_wrbuf_puts(const char *buf, void *client_data)
4077 WRBUF wrbuf = client_data;
4078 wrbuf_puts(wrbuf, buf);
4082 * Returns an xmalloc()d string containing RPN that corresponds to the
4083 * CQL passed in. On error, sets the Connection object's error state
4084 * and returns a null pointer.
4085 * ### We could cache CQL parser and/or transformer in Connection.
4087 static char *cql2pqf(ZOOM_connection c, const char *cql)
4091 const char *cqlfile;
4092 cql_transform_t trans;
4095 parser = cql_parser_create();
4096 if ((error = cql_parser_string(parser, cql)) != 0) {
4097 cql_parser_destroy(parser);
4098 set_ZOOM_error(c, ZOOM_ERROR_CQL_PARSE, cql);
4102 cqlfile = ZOOM_connection_option_get(c, "cqlfile");
4105 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, "no CQL transform file");
4107 else if ((trans = cql_transform_open_fname(cqlfile)) == 0)
4110 sprintf(buf, "can't open CQL transform file '%.200s': %.200s",
4111 cqlfile, strerror(errno));
4112 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4116 WRBUF wrbuf_result = wrbuf_alloc();
4117 error = cql_transform(trans, cql_parser_result(parser),
4118 cql2pqf_wrbuf_puts, wrbuf_result);
4121 const char *addinfo;
4122 error = cql_transform_error(trans, &addinfo);
4123 sprintf(buf, "%.200s (addinfo=%.200s)",
4124 cql_strerror(error), addinfo);
4125 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4129 result = xstrdup(wrbuf_cstr(wrbuf_result));
4131 cql_transform_close(trans);
4132 wrbuf_destroy(wrbuf_result);
4134 cql_parser_destroy(parser);
4138 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
4142 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
4143 /* timeout and this connection was waiting */
4144 set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
4146 ZOOM_connection_put_event(c, event);
4152 ZOOM_connection_process(ZOOM_connection c)
4158 event = ZOOM_connection_get_event(c);
4161 ZOOM_Event_destroy(event);
4164 ZOOM_connection_exec_task(c);
4165 event = ZOOM_connection_get_event(c);
4168 ZOOM_Event_destroy(event);
4175 ZOOM_event_nonblock(int no, ZOOM_connection *cs)
4179 yaz_log(log_details, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
4181 for (i = 0; i<no; i++)
4183 ZOOM_connection c = cs[i];
4185 if (c && ZOOM_connection_process(c))
4191 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
4193 if (c->mask && mask)
4194 ZOOM_connection_do_io(c, mask);
4198 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
4201 return cs_fileno(c->cs);
4205 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
4213 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
4220 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
4222 return ZOOM_options_get_int(c->options, "timeout", 30);
4228 * indent-tabs-mode: nil
4230 * vim: shiftwidth=4 tabstop=8 expandtab