1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements ZOOM C interface.
15 #include <yaz/yaz-util.h>
16 #include <yaz/xmalloc.h>
17 #include <yaz/otherinfo.h>
19 #include <yaz/pquery.h>
20 #include <yaz/marcdisp.h>
21 #include <yaz/diagbib1.h>
22 #include <yaz/charneg.h>
27 #include <yaz/query-charset.h>
28 #include <yaz/copy_types.h>
29 #include <yaz/snprintf.h>
31 #include <yaz/shptr.h>
37 static int log_api = 0;
38 static int log_details = 0;
45 static void resultset_destroy(ZOOM_resultset r);
46 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c);
47 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out);
48 static char *cql2pqf(ZOOM_connection c, const char *cql);
50 ZOOM_API(const char *) ZOOM_get_event_str(int event)
52 static const char *ar[] = {
69 * This wrapper is just for logging failed lookups. It would be nicer
70 * if it could cause failure when a lookup fails, but that's hard.
72 static Odr_oid *zoom_yaz_str_to_z3950oid(ZOOM_connection c,
73 oid_class oid_class, const char *str) {
74 Odr_oid *res = yaz_string_to_oid_odr(yaz_oid_std(), oid_class, str,
77 yaz_log(YLOG_WARN, "%p OID lookup (%d, '%s') failed",
78 c, (int) oid_class, str);
83 static void initlog(void)
85 static int log_level_initialized = 0;
86 if (!log_level_initialized)
88 log_api = yaz_log_module_level("zoom");
89 log_details = yaz_log_module_level("zoomdetails");
90 log_level_initialized = 1;
94 static ZOOM_Event ZOOM_Event_create(int kind)
96 ZOOM_Event event = (ZOOM_Event) xmalloc(sizeof(*event));
100 yaz_log(log_details, "ZOOM_Event_create(kind=%d)", kind);
104 static void ZOOM_Event_destroy(ZOOM_Event event)
109 static void ZOOM_connection_put_event(ZOOM_connection c, ZOOM_Event event)
113 c->m_queue_back->prev = event;
114 assert(c->m_queue_front);
118 assert(!c->m_queue_front);
119 c->m_queue_front = event;
121 event->next = c->m_queue_back;
123 c->m_queue_back = event;
126 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
128 ZOOM_Event event = c->m_queue_front;
131 c->last_event = ZOOM_EVENT_NONE;
134 assert(c->m_queue_back);
135 c->m_queue_front = event->prev;
136 if (c->m_queue_front)
138 assert(c->m_queue_back);
139 c->m_queue_front->next = 0;
143 c->last_event = event->kind;
147 static void ZOOM_connection_remove_events(ZOOM_connection c)
150 while ((event = ZOOM_connection_get_event(c)))
151 ZOOM_Event_destroy(event);
154 ZOOM_API(int) ZOOM_connection_peek_event(ZOOM_connection c)
156 ZOOM_Event event = c->m_queue_front;
158 return event ? event->kind : ZOOM_EVENT_NONE;
161 void ZOOM_connection_remove_tasks(ZOOM_connection c);
163 static void set_dset_error(ZOOM_connection c, int error,
165 const char *addinfo, const char *addinfo2)
172 if (!c->diagset || strcmp(dset, c->diagset))
175 c->diagset = xstrdup(dset);
176 /* remove integer part from SRW diagset .. */
177 if ((cp = strrchr(c->diagset, '/')))
180 if (addinfo && addinfo2)
182 c->addinfo = (char*) xmalloc(strlen(addinfo) + strlen(addinfo2) + 2);
183 strcpy(c->addinfo, addinfo);
184 strcat(c->addinfo, addinfo2);
187 c->addinfo = xstrdup(addinfo);
188 if (error != ZOOM_ERROR_NONE)
190 yaz_log(log_api, "%p set_dset_error %s %s:%d %s %s",
191 c, c->host_port ? c->host_port : "<>", dset, error,
192 addinfo ? addinfo : "",
193 addinfo2 ? addinfo2 : "");
194 ZOOM_connection_remove_tasks(c);
198 static int uri_to_code(const char *uri)
202 if ((cp = strrchr(uri, '/')))
208 static void set_HTTP_error(ZOOM_connection c, int error,
209 const char *addinfo, const char *addinfo2)
211 set_dset_error(c, error, "HTTP", addinfo, addinfo2);
214 static void set_SRU_error(ZOOM_connection c, Z_SRW_diagnostic *d)
216 const char *uri = d->uri;
218 set_dset_error(c, uri_to_code(uri), uri, d->details, 0);
224 static void set_ZOOM_error(ZOOM_connection c, int error,
227 set_dset_error(c, error, "ZOOM", addinfo, 0);
230 static void clear_error(ZOOM_connection c)
233 * If an error is tied to an operation then it's ok to clear: for
234 * example, a diagnostic returned from a search is cleared by a
235 * subsequent search. However, problems such as Connection Lost
236 * or Init Refused are not cleared, because they are not
237 * recoverable: doing another search doesn't help.
240 ZOOM_connection_remove_events(c);
243 case ZOOM_ERROR_CONNECT:
244 case ZOOM_ERROR_MEMORY:
245 case ZOOM_ERROR_DECODE:
246 case ZOOM_ERROR_CONNECTION_LOST:
247 case ZOOM_ERROR_INIT:
248 case ZOOM_ERROR_INTERNAL:
249 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
252 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
256 void ZOOM_connection_show_task(ZOOM_task task)
260 case ZOOM_TASK_SEARCH:
261 yaz_log(YLOG_LOG, "search p=%p", task);
263 case ZOOM_TASK_RETRIEVE:
264 yaz_log(YLOG_LOG, "retrieve p=%p", task);
266 case ZOOM_TASK_CONNECT:
267 yaz_log(YLOG_LOG, "connect p=%p", task);
270 yaz_log(YLOG_LOG, "scan p=%p", task);
275 void ZOOM_connection_show_tasks(ZOOM_connection c)
278 yaz_log(YLOG_LOG, "connection p=%p tasks", c);
279 for (task = c->tasks; task; task = task->next)
280 ZOOM_connection_show_task(task);
283 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
285 ZOOM_task *taskp = &c->tasks;
287 taskp = &(*taskp)->next;
288 *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
289 (*taskp)->running = 0;
290 (*taskp)->which = which;
296 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
298 return c->tasks ? 0 : 1;
301 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
303 ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
305 task->next = c->tasks;
314 void ZOOM_connection_remove_task(ZOOM_connection c)
316 ZOOM_task task = c->tasks;
320 c->tasks = task->next;
323 case ZOOM_TASK_SEARCH:
324 resultset_destroy(task->u.search.resultset);
325 xfree(task->u.search.syntax);
326 xfree(task->u.search.elementSetName);
328 case ZOOM_TASK_RETRIEVE:
329 resultset_destroy(task->u.retrieve.resultset);
330 xfree(task->u.retrieve.syntax);
331 xfree(task->u.retrieve.elementSetName);
333 case ZOOM_TASK_CONNECT:
336 ZOOM_scanset_destroy(task->u.scan.scan);
338 case ZOOM_TASK_PACKAGE:
339 ZOOM_package_destroy(task->u.package);
342 resultset_destroy(task->u.sort.resultset);
343 ZOOM_query_destroy(task->u.sort.q);
352 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
353 ZOOM_connection_put_event(c, event);
358 void ZOOM_connection_remove_tasks(ZOOM_connection c)
361 ZOOM_connection_remove_task(c);
364 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
366 const char *elementSetName);
368 ZOOM_API(ZOOM_connection)
369 ZOOM_connection_create(ZOOM_options options)
371 ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
375 yaz_log(log_api, "%p ZOOM_connection_create", c);
377 c->proto = PROTO_Z3950;
379 ZOOM_connection_set_mask(c, 0);
381 c->state = STATE_IDLE;
384 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
391 c->options = ZOOM_options_create_with_parent(options);
397 c->charset = c->lang = 0;
408 c->maximum_record_size = 0;
409 c->preferred_message_size = 0;
411 c->odr_in = odr_createmem(ODR_DECODE);
412 c->odr_out = odr_createmem(ODR_ENCODE);
416 c->support_named_resultsets = 0;
417 c->last_event = ZOOM_EVENT_NONE;
419 c->m_queue_front = 0;
428 /* set database names. Take local databases (if set); otherwise
429 take databases given in ZURL (if set); otherwise use Default */
430 static char **set_DatabaseNames(ZOOM_connection con, ZOOM_options options,
433 char **databaseNames;
434 const char *cp = ZOOM_options_get(options, "databaseName");
436 if ((!cp || !*cp) && con->host_port)
438 if (strncmp(con->host_port, "unix:", 5) == 0)
439 cp = strchr(con->host_port+5, ':');
441 cp = strchr(con->host_port, '/');
447 nmem_strsplit(odr_getmem(odr), "+", cp, &databaseNames, num);
448 return databaseNames;
451 ZOOM_API(ZOOM_connection)
452 ZOOM_connection_new(const char *host, int portnum)
454 ZOOM_connection c = ZOOM_connection_create(0);
456 ZOOM_connection_connect(c, host, portnum);
460 static zoom_sru_mode get_sru_mode_from_string(const char *s)
463 return zoom_sru_soap;
464 if (!yaz_matchstr(s, "soap"))
465 return zoom_sru_soap;
466 else if (!yaz_matchstr(s, "get"))
468 else if (!yaz_matchstr(s, "post"))
469 return zoom_sru_post;
470 return zoom_sru_error;
474 ZOOM_connection_connect(ZOOM_connection c,
475 const char *host, int portnum)
482 yaz_log(log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
483 c, host ? host : "null", portnum);
485 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
486 ZOOM_connection_remove_tasks(c);
490 odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
491 odr_destroy(c->odr_print);
493 if (ZOOM_options_get_bool(c->options, "apdulog", 0))
495 c->odr_print = odr_createmem(ODR_PRINT);
496 odr_setprint(c->odr_print, yaz_log_file());
503 yaz_log(log_details, "%p ZOOM_connection_connect reconnect ok", c);
507 yaz_log(log_details, "%p ZOOM_connection_connect connect", c);
510 val = ZOOM_options_get(c->options, "proxy");
513 yaz_log(log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
514 c->proxy = xstrdup(val);
519 val = ZOOM_options_get(c->options, "charset");
522 yaz_log(log_details, "%p ZOOM_connection_connect charset=%s", c, val);
523 c->charset = xstrdup(val);
527 val = ZOOM_options_get(c->options, "lang");
530 yaz_log(log_details, "%p ZOOM_connection_connect lang=%s", c, val);
531 c->lang = xstrdup(val);
542 sprintf(hostn, "%.80s:%d", host, portnum);
543 c->host_port = xstrdup(hostn);
546 c->host_port = xstrdup(host);
551 * If the "<scheme>:" part of the host string is preceded by one
552 * or more comma-separated <name>=<value> pairs, these are taken
553 * to be options to be set on the connection object. Among other
554 * applications, this facility can be used to embed authentication
556 * user=admin,password=secret,tcp:localhost:9999
558 char *remainder = c->host_port;
559 char *pcolon = strchr(remainder, ':');
562 while ((pcomma = strchr(remainder, ',')) != 0 &&
563 (pcolon == 0 || pcomma < pcolon)) {
565 if ((pequals = strchr(remainder, '=')) != 0) {
567 /*printf("# setting '%s'='%s'\n", remainder, pequals+1);*/
568 ZOOM_connection_option_set(c, remainder, pequals+1);
570 remainder = pcomma+1;
573 if (remainder != c->host_port) {
575 c->host_port = xstrdup(remainder);
576 /*printf("# reset hp='%s'\n", remainder);*/
580 val = ZOOM_options_get(c->options, "sru");
581 c->sru_mode = get_sru_mode_from_string(val);
583 xfree(c->sru_version);
584 val = ZOOM_options_get(c->options, "sru_version");
585 c->sru_version = xstrdup(val ? val : "1.2");
587 ZOOM_options_set(c->options, "host", c->host_port);
589 xfree(c->cookie_out);
591 val = ZOOM_options_get(c->options, "cookie");
594 yaz_log(log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
595 c->cookie_out = xstrdup(val);
600 val = ZOOM_options_get(c->options, "clientIP");
603 yaz_log(log_details, "%p ZOOM_connection_connect clientIP=%s",
605 c->client_IP = xstrdup(val);
610 val = ZOOM_options_get(c->options, "group");
612 c->group = xstrdup(val);
616 val = ZOOM_options_get(c->options, "user");
618 c->user = xstrdup(val);
622 val = ZOOM_options_get(c->options, "password");
624 val = ZOOM_options_get(c->options, "pass");
627 c->password = xstrdup(val);
629 c->maximum_record_size =
630 ZOOM_options_get_int(c->options, "maximumRecordSize", 1024*1024);
631 c->preferred_message_size =
632 ZOOM_options_get_int(c->options, "preferredMessageSize", 1024*1024);
634 c->async = ZOOM_options_get_bool(c->options, "async", 0);
635 yaz_log(log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
637 task = ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
641 while (ZOOM_event(1, &c))
647 ZOOM_query_create(void)
649 ZOOM_query s = (ZOOM_query) xmalloc(sizeof(*s));
651 yaz_log(log_details, "%p ZOOM_query_create", s);
655 s->odr = odr_createmem(ODR_ENCODE);
662 ZOOM_query_destroy(ZOOM_query s)
668 yaz_log(log_details, "%p ZOOM_query_destroy count=%d", s, s->refcount);
669 if (s->refcount == 0)
677 ZOOM_query_prefix(ZOOM_query s, const char *str)
679 s->query_string = odr_strdup(s->odr, str);
680 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
681 s->z_query->which = Z_Query_type_1;
682 s->z_query->u.type_1 = p_query_rpn(s->odr, str);
683 if (!s->z_query->u.type_1)
685 yaz_log(log_details, "%p ZOOM_query_prefix str=%s failed", s, str);
689 yaz_log(log_details, "%p ZOOM_query_prefix str=%s", s, str);
694 ZOOM_query_cql(ZOOM_query s, const char *str)
698 s->query_string = odr_strdup(s->odr, str);
700 ext = (Z_External *) odr_malloc(s->odr, sizeof(*ext));
701 ext->direct_reference = odr_oiddup(s->odr, yaz_oid_userinfo_cql);
702 ext->indirect_reference = 0;
704 ext->which = Z_External_CQL;
705 ext->u.cql = s->query_string;
707 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
708 s->z_query->which = Z_Query_type_104;
709 s->z_query->u.type_104 = ext;
711 yaz_log(log_details, "%p ZOOM_query_cql str=%s", s, str);
717 * Translate the CQL string client-side into RPN which is passed to
718 * the server. This is useful for server's that don't themselves
719 * support CQL, for which ZOOM_query_cql() is useless. `conn' is used
720 * only as a place to stash diagnostics if compilation fails; if this
721 * information is not needed, a null pointer may be used.
724 ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn)
728 ZOOM_connection freeme = 0;
730 yaz_log(log_details, "%p ZOOM_query_cql2rpn str=%s conn=%p", s, str, conn);
732 conn = freeme = ZOOM_connection_create(0);
734 rpn = cql2pqf(conn, str);
736 ZOOM_connection_destroy(freeme);
740 ret = ZOOM_query_prefix(s, rpn);
746 * Analogous in every way to ZOOM_query_cql2rpn(), except that there
747 * is no analogous ZOOM_query_ccl() that just sends uninterpreted CCL
748 * to the server, as the YAZ GFS doesn't know how to handle this.
751 ZOOM_query_ccl2rpn(ZOOM_query s, const char *str, const char *config,
752 int *ccl_error, const char **error_string,
756 struct ccl_rpn_node *rpn;
757 CCL_bibset bibset = ccl_qual_mk();
760 ccl_qual_buf(bibset, config);
762 rpn = ccl_find_str(bibset, str, ccl_error, error_pos);
765 *error_string = ccl_err_msg(*ccl_error);
770 WRBUF wr = wrbuf_alloc();
773 ret = ZOOM_query_prefix(s, wrbuf_cstr(wr));
776 ccl_qual_rm(&bibset);
781 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
783 s->sort_spec = yaz_sort_spec(s->odr, criteria);
786 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s failed",
790 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s", s, criteria);
794 static zoom_ret do_write(ZOOM_connection c);
797 ZOOM_connection_destroy(ZOOM_connection c)
799 #if ZOOM_RESULT_LISTS
800 ZOOM_resultsets list;
806 yaz_log(log_api, "%p ZOOM_connection_destroy", c);
810 #if ZOOM_RESULT_LISTS
811 // Remove the connection's usage of resultsets
812 list = c->resultsets;
814 ZOOM_resultsets removed = list;
815 ZOOM_resultset_destroy(list->resultset);
820 for (r = c->resultsets; r; r = r->next)
827 odr_destroy(c->odr_in);
828 odr_destroy(c->odr_out);
831 odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
832 odr_destroy(c->odr_print);
834 ZOOM_options_destroy(c->options);
835 ZOOM_connection_remove_tasks(c);
836 ZOOM_connection_remove_events(c);
842 xfree(c->cookie_out);
848 xfree(c->sru_version);
852 void ZOOM_resultset_addref(ZOOM_resultset r)
856 yaz_mutex_enter(r->mutex);
858 yaz_log(log_details, "%p ZOOM_resultset_addref count=%d",
860 yaz_mutex_leave(r->mutex);
864 ZOOM_resultset ZOOM_resultset_create(void)
867 ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
871 yaz_log(log_details, "%p ZOOM_resultset_create", r);
874 r->odr = odr_createmem(ODR_ENCODE);
879 for (i = 0; i<RECORD_HASH_SIZE; i++)
880 r->record_hash[i] = 0;
884 r->databaseNames = 0;
885 r->num_databaseNames = 0;
887 yaz_mutex_create(&r->mutex);
890 WRBUF w = wrbuf_alloc();
891 YAZ_SHPTR_INIT(r->record_wrbuf, w);
897 ZOOM_API(ZOOM_resultset)
898 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
901 ZOOM_query s = ZOOM_query_create();
903 ZOOM_query_prefix(s, q);
905 r = ZOOM_connection_search(c, s);
906 ZOOM_query_destroy(s);
910 ZOOM_API(ZOOM_resultset)
911 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
913 ZOOM_resultset r = ZOOM_resultset_create();
917 const char *syntax, *elementSetName;
918 #if ZOOM_RESULT_LISTS
922 yaz_log(log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
923 r->r_sort_spec = q->sort_spec;
926 r->options = ZOOM_options_create_with_parent(c->options);
928 start = ZOOM_options_get_int(r->options, "start", 0);
929 count = ZOOM_options_get_int(r->options, "count", 0);
931 /* If "presentChunk" is defined use that; otherwise "step" */
932 const char *cp = ZOOM_options_get(r->options, "presentChunk");
933 r->step = ZOOM_options_get_int(r->options,
934 (cp != 0 ? "presentChunk": "step"), 0);
936 r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
937 cp = ZOOM_options_get(r->options, "setname");
939 r->setname = xstrdup(cp);
940 cp = ZOOM_options_get(r->options, "schema");
942 r->schema = xstrdup(cp);
944 r->databaseNames = set_DatabaseNames(c, c->options, &r->num_databaseNames,
949 #if ZOOM_RESULT_LISTS
950 yaz_log(log_details, "%p ZOOM_connection_search: Adding new resultset (%p) to resultsets (%p) ", c, r, c->resultsets);
951 set = xmalloc(sizeof(*set));
952 ZOOM_resultset_addref(r);
954 set->next = c->resultsets;
957 r->next = c->resultsets;
960 if (c->host_port && c->proto == PROTO_HTTP)
964 yaz_log(log_details, "ZOOM_connection_search: no comstack");
965 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
969 yaz_log(log_details, "ZOOM_connection_search: reconnect");
974 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
975 task->u.search.resultset = r;
976 task->u.search.start = start;
977 task->u.search.count = count;
978 task->u.search.recv_search_fired = 0;
980 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
981 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
982 elementSetName = ZOOM_options_get(r->options, "elementSetName");
983 task->u.search.elementSetName = elementSetName
984 ? xstrdup(elementSetName) : 0;
986 ZOOM_resultset_addref(r);
992 while (ZOOM_event(1, &c))
999 ZOOM_resultset_sort(ZOOM_resultset r,
1000 const char *sort_type, const char *sort_spec)
1002 (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
1006 ZOOM_resultset_sort1(ZOOM_resultset r,
1007 const char *sort_type, const char *sort_spec)
1009 ZOOM_connection c = r->connection;
1013 newq = ZOOM_query_create();
1014 if (ZOOM_query_sortby(newq, sort_spec) < 0)
1017 yaz_log(log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
1018 r, r, sort_type, sort_spec);
1022 if (c->host_port && c->proto == PROTO_HTTP)
1026 yaz_log(log_details, "%p ZOOM_resultset_sort: no comstack", r);
1027 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
1031 yaz_log(log_details, "%p ZOOM_resultset_sort: prepare reconnect",
1033 c->reconnect_ok = 1;
1037 ZOOM_resultset_cache_reset(r);
1038 task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
1039 task->u.sort.resultset = r;
1040 task->u.sort.q = newq;
1042 ZOOM_resultset_addref(r);
1046 while (ZOOM_event(1, &c))
1053 static void ZOOM_record_release(ZOOM_record rec)
1059 if (rec->record_wrbuf)
1060 YAZ_SHPTR_DEC(rec->record_wrbuf, wrbuf_destroy);
1063 wrbuf_destroy(rec->wrbuf);
1068 xmlFree(rec->xml_mem);
1071 odr_destroy(rec->odr);
1075 ZOOM_resultset_cache_reset(ZOOM_resultset r)
1078 for (i = 0; i<RECORD_HASH_SIZE; i++)
1080 ZOOM_record_cache rc;
1081 for (rc = r->record_hash[i]; rc; rc = rc->next)
1083 ZOOM_record_release(&rc->rec);
1085 r->record_hash[i] = 0;
1090 ZOOM_resultset_destroy(ZOOM_resultset r)
1092 resultset_destroy(r);
1095 static void resultset_destroy(ZOOM_resultset r)
1099 yaz_mutex_enter(r->mutex);
1101 yaz_log(log_details, "%p ZOOM_resultset_destroy r=%p count=%d",
1103 if (r->refcount == 0)
1105 yaz_mutex_leave(r->mutex);
1107 yaz_log(log_details, "%p ZOOM_connection resultset_destroy: Deleting resultset (%p) ", r->connection, r);
1108 ZOOM_resultset_cache_reset(r);
1109 #if ZOOM_RESULT_LISTS
1113 /* remove ourselves from the resultsets in connection */
1114 ZOOM_resultset *rp = &r->connection->resultsets;
1117 assert(*rp); /* we must be in this list!! */
1119 { /* OK, we're here - take us out of it */
1127 ZOOM_query_destroy(r->query);
1128 ZOOM_options_destroy(r->options);
1129 odr_destroy(r->odr);
1132 yaz_mutex_destroy(&r->mutex);
1134 YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy);
1139 yaz_mutex_leave(r->mutex);
1143 ZOOM_resultset_size(ZOOM_resultset r)
1145 yaz_log(log_details, "ZOOM_resultset_size r=%p count=" ODR_INT_PRINTF,
1150 static void do_close(ZOOM_connection c)
1155 ZOOM_connection_set_mask(c, 0);
1156 c->state = STATE_IDLE;
1159 static int ZOOM_test_reconnect(ZOOM_connection c)
1163 if (!c->reconnect_ok)
1166 c->reconnect_ok = 0;
1167 c->tasks->running = 0;
1168 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1170 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1171 ZOOM_connection_put_event(c, event);
1176 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
1177 int force_sync, int start, int count)
1182 const char *syntax, *elementSetName;
1186 yaz_log(log_details, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
1187 " count=%d", r, force_sync, start, count);
1192 if (c->host_port && c->proto == PROTO_HTTP)
1196 yaz_log(log_details, "%p ZOOM_resultset_retrieve: no comstack", r);
1197 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
1201 yaz_log(log_details, "%p ZOOM_resultset_retrieve: prepare "
1203 c->reconnect_ok = 1;
1206 task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
1207 task->u.retrieve.resultset = r;
1208 task->u.retrieve.start = start;
1209 task->u.retrieve.count = count;
1211 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
1212 task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
1213 elementSetName = ZOOM_options_get(r->options, "elementSetName");
1214 task->u.retrieve.elementSetName = elementSetName
1215 ? xstrdup(elementSetName) : 0;
1217 cp = ZOOM_options_get(r->options, "schema");
1220 if (!r->schema || strcmp(r->schema, cp))
1223 r->schema = xstrdup(cp);
1227 ZOOM_resultset_addref(r);
1229 if (!r->connection->async || force_sync)
1230 while (r->connection && ZOOM_event(1, &r->connection))
1235 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
1236 size_t start, size_t count)
1238 int force_present = 0;
1242 yaz_log(log_api, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
1243 r, r, (long) start, (long) count);
1246 ZOOM_resultset_retrieve(r, force_present, start, count);
1250 for (i = 0; i< count; i++)
1251 recs[i] = ZOOM_resultset_record_immediate(r, i+start);
1255 static void get_cert(ZOOM_connection c)
1260 if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1262 ZOOM_connection_option_setl(c, "sslPeerCert",
1263 cert_buf, cert_len);
1268 static zoom_ret do_connect_host(ZOOM_connection c,
1269 const char *effective_host,
1270 const char *logical_url);
1272 static zoom_ret do_connect(ZOOM_connection c)
1274 const char *effective_host;
1277 effective_host = c->proxy;
1279 effective_host = c->host_port;
1280 return do_connect_host(c, effective_host, c->host_port);
1283 static zoom_ret do_connect_host(ZOOM_connection c, const char *effective_host,
1284 const char *logical_url)
1288 yaz_log(log_details, "%p do_connect effective_host=%s", c, effective_host);
1292 c->cs = cs_create_host(effective_host, 0, &add);
1294 if (c->cs && c->cs->protocol == PROTO_HTTP)
1301 c->proto = PROTO_HTTP;
1302 cs_get_host_args(logical_url, &db);
1305 c->path = xmalloc(strlen(db) * 3 + 2);
1306 yaz_encode_sru_dbpath_buf(c->path, db);
1309 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1311 return zoom_complete;
1316 int ret = cs_connect(c->cs, add);
1319 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1320 ZOOM_connection_put_event(c, event);
1322 if (c->proto == PROTO_Z3950)
1323 ZOOM_connection_send_init(c);
1326 /* no init request for SRW .. */
1327 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1328 ZOOM_connection_remove_task(c);
1329 ZOOM_connection_set_mask(c, 0);
1330 ZOOM_connection_exec_task(c);
1332 c->state = STATE_ESTABLISHED;
1333 return zoom_pending;
1337 int mask = ZOOM_SELECT_EXCEPT;
1338 if (c->cs->io_pending & CS_WANT_WRITE)
1339 mask += ZOOM_SELECT_WRITE;
1340 if (c->cs->io_pending & CS_WANT_READ)
1341 mask += ZOOM_SELECT_READ;
1342 ZOOM_connection_set_mask(c, mask);
1343 c->state = STATE_CONNECTING;
1344 return zoom_pending;
1347 c->state = STATE_IDLE;
1348 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, logical_url);
1349 return zoom_complete;
1352 static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
1355 for (i = 0; i<200; i++)
1359 Z_OtherInformation **oi;
1364 sprintf(buf, "otherInfo%d", i);
1365 val = ZOOM_options_get(c->options, buf);
1368 cp = strchr(val, ':');
1372 if (len >= sizeof(buf))
1373 len = sizeof(buf)-1;
1374 memcpy(buf, val, len);
1377 oid = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_USERINFO,
1382 yaz_oi_APDU(a, &oi);
1383 yaz_oi_set_string_oid(oi, out, oid, 1, cp+1);
1387 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
1392 Z_OtherInformation **oi;
1393 yaz_oi_APDU(a, &oi);
1394 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_cookie,
1399 Z_OtherInformation **oi;
1400 yaz_oi_APDU(a, &oi);
1401 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_client_ip,
1404 otherInfo_attach(c, a, out);
1405 if (!z_APDU(out, &a, 0, 0))
1407 FILE *outf = fopen("/tmp/apdu.txt", "a");
1410 ODR odr_pr = odr_createmem(ODR_PRINT);
1411 fprintf(outf, "a=%p\n", a);
1412 odr_setprint(odr_pr, outf);
1413 z_APDU(odr_pr, &a, 0, 0);
1414 odr_destroy(odr_pr);
1416 yaz_log(log_api, "%p encoding_APDU: encoding failed", c);
1417 set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
1422 z_APDU(c->odr_print, &a, 0, 0);
1423 yaz_log(log_details, "%p encoding_APDU encoding OK", c);
1427 static zoom_ret send_APDU(ZOOM_connection c, Z_APDU *a)
1431 if (encode_APDU(c, a, c->odr_out))
1432 return zoom_complete;
1433 yaz_log(log_details, "%p send APDU type=%d", c, a->which);
1434 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1435 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1436 ZOOM_connection_put_event(c, event);
1437 odr_reset(c->odr_out);
1441 /* returns 1 if PDU was sent OK (still pending )
1442 0 if PDU was not sent OK (nothing to wait for)
1445 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
1447 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
1448 Z_InitRequest *ireq = apdu->u.initRequest;
1449 Z_IdAuthentication *auth = (Z_IdAuthentication *)
1450 odr_malloc(c->odr_out, sizeof(*auth));
1452 ODR_MASK_SET(ireq->options, Z_Options_search);
1453 ODR_MASK_SET(ireq->options, Z_Options_present);
1454 ODR_MASK_SET(ireq->options, Z_Options_scan);
1455 ODR_MASK_SET(ireq->options, Z_Options_sort);
1456 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
1457 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
1459 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
1460 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
1461 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
1463 ireq->implementationId =
1464 odr_prepend(c->odr_out,
1465 ZOOM_options_get(c->options, "implementationId"),
1466 ireq->implementationId);
1468 ireq->implementationName =
1469 odr_prepend(c->odr_out,
1470 ZOOM_options_get(c->options, "implementationName"),
1471 odr_prepend(c->odr_out, "ZOOM-C",
1472 ireq->implementationName));
1474 ireq->implementationVersion =
1475 odr_prepend(c->odr_out,
1476 ZOOM_options_get(c->options, "implementationVersion"),
1477 ireq->implementationVersion);
1479 *ireq->maximumRecordSize = c->maximum_record_size;
1480 *ireq->preferredMessageSize = c->preferred_message_size;
1482 if (c->group || c->password)
1484 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
1485 pass->groupId = odr_strdup_null(c->odr_out, c->group);
1486 pass->userId = odr_strdup_null(c->odr_out, c->user);
1487 pass->password = odr_strdup_null(c->odr_out, c->password);
1488 auth->which = Z_IdAuthentication_idPass;
1489 auth->u.idPass = pass;
1490 ireq->idAuthentication = auth;
1494 auth->which = Z_IdAuthentication_open;
1495 auth->u.open = odr_strdup(c->odr_out, c->user);
1496 ireq->idAuthentication = auth;
1500 yaz_oi_set_string_oid(&ireq->otherInfo, c->odr_out,
1501 yaz_oid_userinfo_proxy, 1, c->host_port);
1503 if (c->charset || c->lang)
1505 Z_OtherInformation **oi;
1506 Z_OtherInformationUnit *oi_unit;
1508 yaz_oi_APDU(apdu, &oi);
1510 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
1512 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
1513 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
1514 oi_unit->information.externallyDefinedInfo =
1515 yaz_set_proposal_charneg_list(c->odr_out, " ",
1516 c->charset, c->lang, 1);
1520 return send_APDU(c, apdu);
1524 static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
1528 const char *database = ZOOM_options_get(c->options, "databaseName");
1529 char *fdatabase = 0;
1532 fdatabase = yaz_encode_sru_dbpath_odr(c->odr_out, database);
1533 gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port,
1534 fdatabase ? fdatabase : c->path);
1536 if (c->sru_mode == zoom_sru_get)
1538 yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1540 else if (c->sru_mode == zoom_sru_post)
1542 yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1544 else if (c->sru_mode == zoom_sru_soap)
1546 yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1548 if (!z_GDU(c->odr_out, &gdu, 0, 0))
1549 return zoom_complete;
1551 z_GDU(c->odr_print, &gdu, 0, 0);
1552 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1554 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1555 ZOOM_connection_put_event(c, event);
1556 odr_reset(c->odr_out);
1562 static Z_SRW_PDU *ZOOM_srw_get_pdu(ZOOM_connection c, int type)
1564 Z_SRW_PDU *sr = yaz_srw_get_pdu(c->odr_out, type, c->sru_version);
1565 sr->username = c->user;
1566 sr->password = c->password;
1572 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1576 ZOOM_resultset resultset = 0;
1578 const char *option_val = 0;
1580 if (c->error) /* don't continue on error */
1581 return zoom_complete;
1583 switch(c->tasks->which)
1585 case ZOOM_TASK_SEARCH:
1586 resultset = c->tasks->u.search.resultset;
1587 if (!resultset->setname)
1588 resultset->setname = xstrdup("default");
1589 ZOOM_options_set(resultset->options, "setname", resultset->setname);
1590 start = &c->tasks->u.search.start;
1591 count = &c->tasks->u.search.count;
1593 case ZOOM_TASK_RETRIEVE:
1594 resultset = c->tasks->u.retrieve.resultset;
1596 start = &c->tasks->u.retrieve.start;
1597 count = &c->tasks->u.retrieve.count;
1599 if (*start >= resultset->size)
1600 return zoom_complete;
1601 if (*start + *count > resultset->size)
1602 *count = resultset->size - *start;
1604 for (i = 0; i < *count; i++)
1607 record_cache_lookup(resultset, i + *start,
1608 c->tasks->u.retrieve.syntax,
1609 c->tasks->u.retrieve.elementSetName);
1614 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
1615 ZOOM_connection_put_event(c, event);
1622 return zoom_complete;
1625 return zoom_complete;
1627 assert(resultset->query);
1629 sr = ZOOM_srw_get_pdu(c, Z_SRW_searchRetrieve_request);
1630 if (resultset->query->z_query->which == Z_Query_type_104
1631 && resultset->query->z_query->u.type_104->which == Z_External_CQL)
1633 sr->u.request->query_type = Z_SRW_query_type_cql;
1634 sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql;
1636 else if (resultset->query->z_query->which == Z_Query_type_1 &&
1637 resultset->query->z_query->u.type_1)
1639 sr->u.request->query_type = Z_SRW_query_type_pqf;
1640 sr->u.request->query.pqf = resultset->query->query_string;
1644 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
1645 return zoom_complete;
1647 sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
1648 sr->u.request->maximumRecords = odr_intdup(
1649 c->odr_out, (resultset->step > 0 && resultset->step < *count) ?
1650 resultset->step : *count);
1651 sr->u.request->recordSchema = resultset->schema;
1653 option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
1655 sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
1657 option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
1658 yaz_encode_sru_extra(sr, c->odr_out, option_val);
1659 return send_srw(c, sr);
1662 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1664 return zoom_complete;
1668 static zoom_ret ZOOM_connection_send_search(ZOOM_connection c)
1671 int lslb, ssub, mspn;
1673 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
1674 Z_SearchRequest *search_req = apdu->u.searchRequest;
1675 const char *elementSetName;
1676 const char *smallSetElementSetName;
1677 const char *mediumSetElementSetName;
1680 assert(c->tasks->which == ZOOM_TASK_SEARCH);
1682 r = c->tasks->u.search.resultset;
1684 yaz_log(log_details, "%p ZOOM_connection_send_search set=%p", c, r);
1687 ZOOM_options_get(r->options, "elementSetName");
1688 smallSetElementSetName =
1689 ZOOM_options_get(r->options, "smallSetElementSetName");
1690 mediumSetElementSetName =
1691 ZOOM_options_get(r->options, "mediumSetElementSetName");
1693 if (!smallSetElementSetName)
1694 smallSetElementSetName = elementSetName;
1696 if (!mediumSetElementSetName)
1697 mediumSetElementSetName = elementSetName;
1702 /* prepare query for the search request */
1703 search_req->query = r->query->z_query;
1704 if (!search_req->query)
1706 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
1707 return zoom_complete;
1709 if (r->query->z_query->which == Z_Query_type_1 ||
1710 r->query->z_query->which == Z_Query_type_101)
1712 const char *cp = ZOOM_options_get(r->options, "rpnCharset");
1715 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
1719 search_req->query = yaz_copy_Z_Query(search_req->query,
1722 r = yaz_query_charset_convert_rpnquery_check(
1723 search_req->query->u.type_1,
1725 yaz_iconv_close(cd);
1727 { /* query could not be char converted */
1728 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
1729 return zoom_complete;
1734 search_req->databaseNames = r->databaseNames;
1735 search_req->num_databaseNames = r->num_databaseNames;
1737 /* get syntax (no need to provide unless piggyback is in effect) */
1738 syntax = c->tasks->u.search.syntax;
1740 lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
1741 ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
1742 mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
1743 if (lslb != -1 && ssub != -1 && mspn != -1)
1745 /* So're a Z39.50 expert? Let's hope you don't do sort */
1746 *search_req->largeSetLowerBound = lslb;
1747 *search_req->smallSetUpperBound = ssub;
1748 *search_req->mediumSetPresentNumber = mspn;
1750 else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
1751 && r->piggyback && !r->r_sort_spec && !r->schema)
1753 /* Regular piggyback - do it unless we're going to do sort */
1754 *search_req->largeSetLowerBound = 2000000000;
1755 *search_req->smallSetUpperBound = 1;
1756 *search_req->mediumSetPresentNumber =
1757 r->step>0 ? r->step : c->tasks->u.search.count;
1761 /* non-piggyback. Need not provide elementsets or syntaxes .. */
1762 smallSetElementSetName = 0;
1763 mediumSetElementSetName = 0;
1766 if (smallSetElementSetName && *smallSetElementSetName)
1768 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1769 odr_malloc(c->odr_out, sizeof(*esn));
1771 esn->which = Z_ElementSetNames_generic;
1772 esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
1773 search_req->smallSetElementSetNames = esn;
1775 if (mediumSetElementSetName && *mediumSetElementSetName)
1777 Z_ElementSetNames *esn =(Z_ElementSetNames *)
1778 odr_malloc(c->odr_out, sizeof(*esn));
1780 esn->which = Z_ElementSetNames_generic;
1781 esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
1782 search_req->mediumSetElementSetNames = esn;
1785 search_req->preferredRecordSyntax =
1786 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
1790 if (c->support_named_resultsets)
1794 /* find the lowest unused ordinal so that we re-use
1795 result sets on the server. */
1796 for (ord = 1; ; ord++)
1798 #if ZOOM_RESULT_LISTS
1799 ZOOM_resultsets rsp;
1800 sprintf(setname, "%d", ord);
1801 for (rsp = c->resultsets; rsp; rsp = rsp->next)
1802 if (rsp->resultset->setname && !strcmp(rsp->resultset->setname, setname))
1808 sprintf(setname, "%d", ord);
1809 for (rp = c->resultsets; rp; rp = rp->next)
1810 if (rp->setname && !strcmp(rp->setname, setname))
1817 r->setname = xstrdup(setname);
1818 yaz_log(log_details, "%p ZOOM_connection_send_search: allocating "
1819 "set %s", c, r->setname);
1823 yaz_log(log_details, "%p ZOOM_connection_send_search: using "
1825 r->setname = xstrdup("default");
1827 ZOOM_options_set(r->options, "setname", r->setname);
1829 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
1830 return send_APDU(c, apdu);
1833 static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)
1835 char oid_name_buf[OID_STR_MAX];
1836 const char *oid_name;
1839 oid_name = yaz_oid_to_string_buf(r->diagnosticSetId, 0, oid_name_buf);
1842 case Z_DefaultDiagFormat_v2Addinfo:
1843 addinfo = r->u.v2Addinfo;
1845 case Z_DefaultDiagFormat_v3Addinfo:
1846 addinfo = r->u.v3Addinfo;
1851 set_dset_error(c, *r->condition, oid_name, addinfo, 0);
1854 static void response_diag(ZOOM_connection c, Z_DiagRec *p)
1856 if (p->which != Z_DiagRec_defaultFormat)
1857 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1859 response_default_diag(c, p->u.defaultFormat);
1862 ZOOM_API(ZOOM_record)
1863 ZOOM_record_clone(ZOOM_record srec)
1870 odr_enc = odr_createmem(ODR_ENCODE);
1871 if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
1873 buf = odr_getbuf(odr_enc, &size, 0);
1875 nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
1876 yaz_log(log_details, "ZOOM_record create");
1877 nrec->odr = odr_createmem(ODR_DECODE);
1879 nrec->record_wrbuf = 0;
1887 odr_setbuf(nrec->odr, buf, size, 0);
1888 z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
1890 nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
1891 nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
1892 nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
1893 nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
1894 nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
1895 odr_destroy(odr_enc);
1899 ZOOM_API(ZOOM_record)
1900 ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1902 const char *syntax =
1903 ZOOM_options_get(s->options, "preferredRecordSyntax");
1904 const char *elementSetName =
1905 ZOOM_options_get(s->options, "elementSetName");
1907 return record_cache_lookup(s, pos, syntax, elementSetName);
1910 ZOOM_API(ZOOM_record)
1911 ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1913 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1918 * MIKE: I think force_sync should always be zero, but I don't
1919 * want to make this change until I get the go-ahead from
1920 * Adam, in case something depends on the old synchronous
1924 if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1925 ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1926 rec = ZOOM_resultset_record_immediate(r, pos);
1932 ZOOM_record_destroy(ZOOM_record rec)
1934 ZOOM_record_release(rec);
1935 yaz_log(log_details, "ZOOM_record destroy");
1940 static yaz_iconv_t iconv_create_charset(const char *record_charset)
1947 strcpy(to, "UTF-8");
1948 if (record_charset && *record_charset)
1950 /* Use "from,to" or just "from" */
1951 const char *cp = strchr(record_charset, ',');
1952 size_t clen = strlen(record_charset);
1955 strncpy( to, cp+1, sizeof(to)-1);
1956 to[sizeof(to)-1] = '\0';
1957 clen = cp - record_charset;
1959 if (clen > sizeof(from)-1)
1960 clen = sizeof(from)-1;
1963 strncpy(from, record_charset, clen);
1967 cd = yaz_iconv_open(to, from);
1971 static const char *return_marc_record(ZOOM_record rec, WRBUF wrbuf,
1974 const char *buf, int sz,
1975 const char *record_charset)
1977 yaz_iconv_t cd = iconv_create_charset(record_charset);
1978 yaz_marc_t mt = yaz_marc_create();
1979 const char *ret_string = 0;
1982 yaz_marc_iconv(mt, cd);
1983 yaz_marc_xml(mt, marc_type);
1984 if (yaz_marc_decode_wrbuf(mt, buf, sz, wrbuf) > 0)
1987 *len = wrbuf_len(wrbuf);
1988 ret_string = wrbuf_cstr(wrbuf);
1990 yaz_marc_destroy(mt);
1992 yaz_iconv_close(cd);
1996 static const char *return_opac_record(ZOOM_record rec, WRBUF wrbuf,
1999 Z_OPACRecord *opac_rec,
2000 const char *record_charset)
2002 yaz_iconv_t cd = iconv_create_charset(record_charset);
2003 yaz_marc_t mt = yaz_marc_create();
2006 yaz_marc_iconv(mt, cd);
2007 yaz_marc_xml(mt, marc_type);
2009 yaz_opac_decode_wrbuf(mt, opac_rec, wrbuf);
2010 yaz_marc_destroy(mt);
2013 yaz_iconv_close(cd);
2015 *len = wrbuf_len(wrbuf);
2016 return wrbuf_cstr(wrbuf);
2019 static const char *return_string_record(ZOOM_record rec, WRBUF wrbuf,
2021 const char *buf, int sz,
2022 const char *record_charset)
2024 yaz_iconv_t cd = iconv_create_charset(record_charset);
2028 wrbuf_iconv_write(wrbuf, cd, buf, sz);
2029 wrbuf_iconv_reset(wrbuf, cd);
2031 buf = wrbuf_cstr(wrbuf);
2032 sz = wrbuf_len(wrbuf);
2033 yaz_iconv_close(cd);
2040 static const char *return_record(ZOOM_record rec, int *len,
2041 Z_NamePlusRecord *npr,
2042 int marctype, const char *charset)
2044 Z_External *r = (Z_External *) npr->u.databaseRecord;
2045 const Odr_oid *oid = r->direct_reference;
2049 if (!rec->record_wrbuf)
2051 WRBUF w = wrbuf_alloc();
2052 YAZ_SHPTR_INIT(rec->record_wrbuf, w);
2054 wrbuf = rec->record_wrbuf->ptr;
2057 rec->wrbuf = wrbuf_alloc();
2060 wrbuf_rewind(wrbuf);
2061 /* render bibliographic record .. */
2062 if (r->which == Z_External_OPAC)
2064 return return_opac_record(rec, wrbuf, marctype, len,
2065 r->u.opac, charset);
2067 if (r->which == Z_External_sutrs)
2068 return return_string_record(rec, wrbuf, len,
2069 (char*) r->u.sutrs->buf,
2072 else if (r->which == Z_External_octet)
2074 if (yaz_oid_is_iso2709(oid))
2076 const char *ret_buf = return_marc_record(
2077 rec, wrbuf, marctype, len,
2078 (const char *) r->u.octet_aligned->buf,
2079 r->u.octet_aligned->len,
2083 /* bad ISO2709. Return fail unless raw (ISO2709) is wanted */
2084 if (marctype != YAZ_MARC_ISO2709)
2087 return return_string_record(rec, wrbuf, len,
2088 (const char *) r->u.octet_aligned->buf,
2089 r->u.octet_aligned->len,
2092 else if (r->which == Z_External_grs1)
2094 yaz_display_grs1(wrbuf, r->u.grs1, 0);
2095 return return_string_record(rec, wrbuf, len,
2105 ZOOM_record_error(ZOOM_record rec, const char **cp,
2106 const char **addinfo, const char **diagset)
2108 Z_NamePlusRecord *npr;
2117 *cp = rec->diag_message;
2119 *addinfo = rec->diag_details;
2121 *diagset = rec->diag_set;
2122 return uri_to_code(rec->diag_uri);
2124 if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
2126 Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
2127 int error = YAZ_BIB1_UNSPECIFIED_ERROR;
2128 const char *add = 0;
2130 if (diag_rec->which == Z_DiagRec_defaultFormat)
2132 Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
2135 error = *ddf->condition;
2138 case Z_DefaultDiagFormat_v2Addinfo:
2139 add = ddf->u.v2Addinfo;
2141 case Z_DefaultDiagFormat_v3Addinfo:
2142 add = ddf->u.v3Addinfo;
2147 yaz_oid_to_string(yaz_oid_std(),
2148 ddf->diagnosticSetId, &oclass);
2156 *addinfo = add ? add : "";
2158 *cp = diagbib1_str(error);
2164 static const char *get_record_format(ZOOM_record rec, int *len,
2165 Z_NamePlusRecord *npr,
2166 int marctype, const char *charset,
2169 const char *res = return_record(rec, len, npr, marctype, charset);
2171 if (*format == '1' && len)
2173 /* try to XML format res */
2175 xmlKeepBlanksDefault(0); /* get get xmlDocFormatMemory to work! */
2176 doc = xmlParseMemory(res, *len);
2180 xmlFree(rec->xml_mem);
2181 xmlDocDumpFormatMemory(doc, &rec->xml_mem, &rec->xml_size, 1);
2183 res = (char *) rec->xml_mem;
2184 *len = rec->xml_size;
2192 ZOOM_API(const char *)
2193 ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
2200 Z_NamePlusRecord *npr;
2203 *len = 0; /* default return */
2212 for (i = 0; cp[i] && cp[i] != ';' && cp[i] != ' ' && i < sizeof(type)-1;
2220 while (cp[i] == ' ')
2225 while (cp[i] == ' ')
2227 if (!strncmp(cp + i, "charset=", 8))
2230 i = i + 8; /* skip charset= */
2231 for (j = 0; cp[i] && cp[i] != ';' && cp[i] != ' '; i++)
2233 if (j < sizeof(charset)-1)
2234 charset[j++] = cp[i];
2238 else if (!strncmp(cp + i, "format=", 7))
2242 for (j = 0; cp[i] && cp[i] != ';' && cp[i] != ' '; i++)
2244 if (j < sizeof(format)-1)
2245 format[j++] = cp[i];
2250 if (!strcmp(type, "database"))
2253 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
2254 return npr->databaseName;
2256 else if (!strcmp(type, "schema"))
2259 *len = rec->schema ? strlen(rec->schema) : 0;
2262 else if (!strcmp(type, "syntax"))
2264 const char *desc = 0;
2265 if (npr->which == Z_NamePlusRecord_databaseRecord)
2267 Z_External *r = (Z_External *) npr->u.databaseRecord;
2268 desc = yaz_oid_to_string(yaz_oid_std(), r->direct_reference, 0);
2273 *len = strlen(desc);
2276 if (npr->which != Z_NamePlusRecord_databaseRecord)
2279 /* from now on - we have a database record .. */
2280 if (!strcmp(type, "render"))
2282 return get_record_format(rec, len, npr, YAZ_MARC_LINE, charset, format);
2284 else if (!strcmp(type, "xml"))
2286 return get_record_format(rec, len, npr, YAZ_MARC_MARCXML, charset,
2289 else if (!strcmp(type, "txml"))
2291 return get_record_format(rec, len, npr, YAZ_MARC_TURBOMARC, charset,
2294 else if (!strcmp(type, "raw"))
2296 return get_record_format(rec, len, npr, YAZ_MARC_ISO2709, charset,
2299 else if (!strcmp(type, "ext"))
2302 return (const char *) npr->u.databaseRecord;
2304 else if (!strcmp(type, "opac"))
2306 if (npr->u.databaseRecord->which == Z_External_OPAC)
2307 return get_record_format(rec, len, npr, YAZ_MARC_MARCXML, charset,
2313 static int strcmp_null(const char *v1, const char *v2)
2319 return strcmp(v1, v2);
2322 static size_t record_hash(int pos)
2326 return pos % RECORD_HASH_SIZE;
2329 static void record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
2331 const char *syntax, const char *elementSetName,
2333 Z_SRW_diagnostic *diag)
2335 ZOOM_record_cache rc = 0;
2337 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2338 ZOOM_connection_put_event(r->connection, event);
2340 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2343 && strcmp_null(r->schema, rc->schema) == 0
2344 && strcmp_null(elementSetName,rc->elementSetName) == 0
2345 && strcmp_null(syntax, rc->syntax) == 0)
2350 rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
2353 YAZ_SHPTR_INC(r->record_wrbuf);
2354 rc->rec.record_wrbuf = r->record_wrbuf;
2359 rc->rec.xml_mem = 0;
2361 rc->elementSetName = odr_strdup_null(r->odr, elementSetName);
2363 rc->syntax = odr_strdup_null(r->odr, syntax);
2365 rc->schema = odr_strdup_null(r->odr, r->schema);
2368 rc->next = r->record_hash[record_hash(pos)];
2369 r->record_hash[record_hash(pos)] = rc;
2372 rc->rec.schema = odr_strdup_null(r->odr, schema);
2373 rc->rec.diag_set = 0;
2374 rc->rec.diag_uri = 0;
2375 rc->rec.diag_message = 0;
2376 rc->rec.diag_details = 0;
2382 rc->rec.diag_set = odr_strdup(r->odr, diag->uri);
2383 if ((cp = strrchr(rc->rec.diag_set, '/')))
2385 rc->rec.diag_uri = odr_strdup(r->odr, diag->uri);
2387 rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);
2388 rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
2392 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
2394 const char *elementSetName)
2396 ZOOM_record_cache rc;
2398 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2402 if (strcmp_null(r->schema, rc->schema))
2404 if (strcmp_null(elementSetName,rc->elementSetName))
2406 if (strcmp_null(syntax, rc->syntax))
2414 static void handle_records(ZOOM_connection c, Z_Records *sr,
2417 ZOOM_resultset resultset;
2419 const char *syntax = 0, *elementSetName = 0;
2423 switch (c->tasks->which)
2425 case ZOOM_TASK_SEARCH:
2426 resultset = c->tasks->u.search.resultset;
2427 start = &c->tasks->u.search.start;
2428 count = &c->tasks->u.search.count;
2429 syntax = c->tasks->u.search.syntax;
2430 elementSetName = c->tasks->u.search.elementSetName;
2432 case ZOOM_TASK_RETRIEVE:
2433 resultset = c->tasks->u.retrieve.resultset;
2434 start = &c->tasks->u.retrieve.start;
2435 count = &c->tasks->u.retrieve.count;
2436 syntax = c->tasks->u.retrieve.syntax;
2437 elementSetName = c->tasks->u.retrieve.elementSetName;
2442 if (sr && sr->which == Z_Records_NSD)
2443 response_default_diag(c, sr->u.nonSurrogateDiagnostic);
2444 else if (sr && sr->which == Z_Records_multipleNSD)
2446 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
2447 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
2449 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2453 if (*count + *start > resultset->size)
2454 *count = resultset->size - *start;
2457 if (sr && sr->which == Z_Records_DBOSD)
2460 NMEM nmem = odr_extract_mem(c->odr_in);
2461 Z_NamePlusRecordList *p =
2462 sr->u.databaseOrSurDiagnostics;
2463 for (i = 0; i<p->num_records; i++)
2465 record_cache_add(resultset, p->records[i], i + *start,
2466 syntax, elementSetName,
2473 yaz_log(log_details,
2474 "handle_records resultset=%p start=%d count=%d",
2475 resultset, *start, *count);
2477 /* transfer our response to search_nmem .. we need it later */
2478 nmem_transfer(odr_getmem(resultset->odr), nmem);
2480 if (present_phase && p->num_records == 0)
2482 /* present response and we didn't get any records! */
2483 Z_NamePlusRecord *myrec =
2484 zget_surrogateDiagRec(
2486 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
2487 "ZOOM C generated. Present phase and no records");
2488 record_cache_add(resultset, myrec, *start,
2489 syntax, elementSetName, 0, 0);
2492 else if (present_phase)
2494 /* present response and we didn't get any records! */
2495 Z_NamePlusRecord *myrec =
2496 zget_surrogateDiagRec(
2498 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
2499 "ZOOM C generated: Present response and no records");
2500 record_cache_add(resultset, myrec, *start, syntax, elementSetName,
2506 static void handle_present_response(ZOOM_connection c, Z_PresentResponse *pr)
2508 handle_records(c, pr->records, 1);
2511 static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
2514 switch (term->which)
2516 case Z_Term_general:
2517 ZOOM_options_setl(opt, name,
2518 (const char *)(term->u.general->buf),
2519 term->u.general->len);
2521 case Z_Term_characterString:
2522 ZOOM_options_set(opt, name, term->u.characterString);
2524 case Z_Term_numeric:
2525 ZOOM_options_set_int(opt, name, *term->u.numeric);
2530 static void handle_queryExpression(ZOOM_options opt, const char *name,
2531 Z_QueryExpression *exp)
2537 case Z_QueryExpression_term:
2538 if (exp->u.term && exp->u.term->queryTerm)
2540 sprintf(opt_name, "%s.term", name);
2541 handle_queryExpressionTerm(opt, opt_name, exp->u.term->queryTerm);
2544 case Z_QueryExpression_query:
2549 static void handle_searchResult(ZOOM_connection c, ZOOM_resultset resultset,
2550 Z_OtherInformation *o)
2553 for (i = 0; o && i < o->num_elements; i++)
2555 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
2557 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
2559 if (ext->which == Z_External_searchResult1)
2562 Z_SearchInfoReport *sr = ext->u.searchResult1;
2565 ZOOM_options_set_int(
2566 resultset->options, "searchresult.size", sr->num);
2568 for (j = 0; j < sr->num; j++)
2570 Z_SearchInfoReport_s *ent =
2571 ext->u.searchResult1->elements[j];
2574 sprintf(pref, "searchresult.%d", j);
2576 if (ent->subqueryId)
2579 sprintf(opt_name, "%s.id", pref);
2580 ZOOM_options_set(resultset->options, opt_name,
2583 if (ent->subqueryExpression)
2586 sprintf(opt_name, "%s.subquery", pref);
2587 handle_queryExpression(resultset->options, opt_name,
2588 ent->subqueryExpression);
2590 if (ent->subqueryInterpretation)
2593 sprintf(opt_name, "%s.interpretation", pref);
2594 handle_queryExpression(resultset->options, opt_name,
2595 ent->subqueryInterpretation);
2597 if (ent->subqueryRecommendation)
2600 sprintf(opt_name, "%s.recommendation", pref);
2601 handle_queryExpression(resultset->options, opt_name,
2602 ent->subqueryRecommendation);
2604 if (ent->subqueryCount)
2607 sprintf(opt_name, "%s.count", pref);
2608 ZOOM_options_set_int(resultset->options, opt_name,
2609 *ent->subqueryCount);
2617 static void handle_search_response(ZOOM_connection c, Z_SearchResponse *sr)
2619 ZOOM_resultset resultset;
2622 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
2625 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
2626 ZOOM_connection_put_event(c, event);
2628 resultset = c->tasks->u.search.resultset;
2630 if (sr->resultSetStatus)
2632 ZOOM_options_set_int(resultset->options, "resultSetStatus",
2633 *sr->resultSetStatus);
2635 if (sr->presentStatus)
2637 ZOOM_options_set_int(resultset->options, "presentStatus",
2638 *sr->presentStatus);
2640 handle_searchResult(c, resultset, sr->additionalSearchInfo);
2642 resultset->size = *sr->resultCount;
2643 handle_records(c, sr->records, 0);
2646 static void sort_response(ZOOM_connection c, Z_SortResponse *res)
2648 if (res->diagnostics && res->num_diagnostics > 0)
2649 response_diag(c, res->diagnostics[0]);
2652 static int scan_response(ZOOM_connection c, Z_ScanResponse *res)
2654 NMEM nmem = odr_extract_mem(c->odr_in);
2657 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
2659 scan = c->tasks->u.scan.scan;
2661 if (res->entries && res->entries->nonsurrogateDiagnostics)
2662 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
2663 scan->scan_response = res;
2664 scan->srw_scan_response = 0;
2665 nmem_transfer(odr_getmem(scan->odr), nmem);
2667 ZOOM_options_set_int(scan->options, "stepSize", *res->stepSize);
2668 if (res->positionOfTerm)
2669 ZOOM_options_set_int(scan->options, "position", *res->positionOfTerm);
2670 if (res->scanStatus)
2671 ZOOM_options_set_int(scan->options, "scanStatus", *res->scanStatus);
2672 if (res->numberOfEntriesReturned)
2673 ZOOM_options_set_int(scan->options, "number",
2674 *res->numberOfEntriesReturned);
2679 static zoom_ret send_sort(ZOOM_connection c,
2680 ZOOM_resultset resultset)
2683 resultset->r_sort_spec = 0;
2684 if (resultset->r_sort_spec)
2686 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
2687 Z_SortRequest *req = apdu->u.sortRequest;
2689 req->num_inputResultSetNames = 1;
2690 req->inputResultSetNames = (Z_InternationalString **)
2691 odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
2692 req->inputResultSetNames[0] =
2693 odr_strdup(c->odr_out, resultset->setname);
2694 req->sortedResultSetName = odr_strdup(c->odr_out, resultset->setname);
2695 req->sortSequence = resultset->r_sort_spec;
2696 resultset->r_sort_spec = 0;
2697 return send_APDU(c, apdu);
2699 return zoom_complete;
2702 static zoom_ret send_present(ZOOM_connection c)
2705 Z_PresentRequest *req = 0;
2707 const char *syntax = 0;
2708 const char *elementSetName = 0;
2709 ZOOM_resultset resultset;
2714 yaz_log(log_details, "%p send_present no tasks", c);
2715 return zoom_complete;
2718 switch (c->tasks->which)
2720 case ZOOM_TASK_SEARCH:
2721 resultset = c->tasks->u.search.resultset;
2722 start = &c->tasks->u.search.start;
2723 count = &c->tasks->u.search.count;
2724 syntax = c->tasks->u.search.syntax;
2725 elementSetName = c->tasks->u.search.elementSetName;
2727 case ZOOM_TASK_RETRIEVE:
2728 resultset = c->tasks->u.retrieve.resultset;
2729 start = &c->tasks->u.retrieve.start;
2730 count = &c->tasks->u.retrieve.count;
2731 syntax = c->tasks->u.retrieve.syntax;
2732 elementSetName = c->tasks->u.retrieve.elementSetName;
2735 return zoom_complete;
2737 yaz_log(log_details, "%p send_present start=%d count=%d",
2740 if (*start < 0 || *count < 0 || *start + *count > resultset->size)
2742 set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
2745 if (c->error) /* don't continue on error */
2746 return zoom_complete;
2747 yaz_log(log_details, "send_present resultset=%p start=%d count=%d",
2748 resultset, *start, *count);
2750 for (i = 0; i < *count; i++)
2753 record_cache_lookup(resultset, i + *start, syntax, elementSetName);
2758 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2759 ZOOM_connection_put_event(c, event);
2767 yaz_log(log_details, "%p send_present skip=%d no more to fetch", c, i);
2768 return zoom_complete;
2771 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
2772 req = apdu->u.presentRequest;
2775 yaz_log(log_details, "%p send_present skip=%d", c, i);
2777 *req->resultSetStartPoint = *start + 1;
2779 if (resultset->step > 0 && resultset->step < *count)
2780 *req->numberOfRecordsRequested = resultset->step;
2782 *req->numberOfRecordsRequested = *count;
2784 if (*req->numberOfRecordsRequested + *start > resultset->size)
2785 *req->numberOfRecordsRequested = resultset->size - *start;
2786 assert(*req->numberOfRecordsRequested > 0);
2788 if (syntax && *syntax)
2789 req->preferredRecordSyntax =
2790 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
2792 if (resultset->schema && *resultset->schema)
2794 Z_RecordComposition *compo = (Z_RecordComposition *)
2795 odr_malloc(c->odr_out, sizeof(*compo));
2797 req->recordComposition = compo;
2798 compo->which = Z_RecordComp_complex;
2799 compo->u.complex = (Z_CompSpec *)
2800 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
2801 compo->u.complex->selectAlternativeSyntax = (bool_t *)
2802 odr_malloc(c->odr_out, sizeof(bool_t));
2803 *compo->u.complex->selectAlternativeSyntax = 0;
2805 compo->u.complex->generic = (Z_Specification *)
2806 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
2808 compo->u.complex->generic->which = Z_Schema_oid;
2809 compo->u.complex->generic->schema.oid = (Odr_oid *)
2810 zoom_yaz_str_to_z3950oid (c, CLASS_SCHEMA, resultset->schema);
2812 if (!compo->u.complex->generic->schema.oid)
2814 /* OID wasn't a schema! Try record syntax instead. */
2816 compo->u.complex->generic->schema.oid = (Odr_oid *)
2817 zoom_yaz_str_to_z3950oid (c, CLASS_RECSYN, resultset->schema);
2819 if (elementSetName && *elementSetName)
2821 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
2822 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
2823 compo->u.complex->generic->elementSpec->which =
2824 Z_ElementSpec_elementSetName;
2825 compo->u.complex->generic->elementSpec->u.elementSetName =
2826 odr_strdup(c->odr_out, elementSetName);
2829 compo->u.complex->generic->elementSpec = 0;
2830 compo->u.complex->num_dbSpecific = 0;
2831 compo->u.complex->dbSpecific = 0;
2832 compo->u.complex->num_recordSyntax = 0;
2833 compo->u.complex->recordSyntax = 0;
2835 else if (elementSetName && *elementSetName)
2837 Z_ElementSetNames *esn = (Z_ElementSetNames *)
2838 odr_malloc(c->odr_out, sizeof(*esn));
2839 Z_RecordComposition *compo = (Z_RecordComposition *)
2840 odr_malloc(c->odr_out, sizeof(*compo));
2842 esn->which = Z_ElementSetNames_generic;
2843 esn->u.generic = odr_strdup(c->odr_out, elementSetName);
2844 compo->which = Z_RecordComp_simple;
2845 compo->u.simple = esn;
2846 req->recordComposition = compo;
2848 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
2849 return send_APDU(c, apdu);
2852 ZOOM_API(ZOOM_scanset)
2853 ZOOM_connection_scan(ZOOM_connection c, const char *start)
2856 ZOOM_query q = ZOOM_query_create();
2858 ZOOM_query_prefix(q, start);
2860 s = ZOOM_connection_scan1(c, q);
2861 ZOOM_query_destroy(q);
2866 ZOOM_API(ZOOM_scanset)
2867 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
2869 ZOOM_scanset scan = 0;
2873 scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
2874 scan->connection = c;
2875 scan->odr = odr_createmem(ODR_DECODE);
2876 scan->options = ZOOM_options_create_with_parent(c->options);
2878 scan->scan_response = 0;
2879 scan->srw_scan_response = 0;
2883 scan->databaseNames = set_DatabaseNames(c, c->options,
2884 &scan->num_databaseNames,
2889 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
2890 task->u.scan.scan = scan;
2895 while (ZOOM_event(1, &c))
2903 ZOOM_scanset_destroy(ZOOM_scanset scan)
2908 if (scan->refcount == 0)
2910 ZOOM_query_destroy(scan->query);
2912 odr_destroy(scan->odr);
2914 ZOOM_options_destroy(scan->options);
2919 static zoom_ret send_package(ZOOM_connection c)
2923 yaz_log(log_details, "%p send_package", c);
2925 return zoom_complete;
2926 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
2928 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
2929 ZOOM_connection_put_event(c, event);
2931 c->buf_out = c->tasks->u.package->buf_out;
2932 c->len_out = c->tasks->u.package->len_out;
2937 static zoom_ret ZOOM_connection_send_scan(ZOOM_connection c)
2940 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
2941 Z_ScanRequest *req = apdu->u.scanRequest;
2943 yaz_log(log_details, "%p send_scan", c);
2945 return zoom_complete;
2946 assert (c->tasks->which == ZOOM_TASK_SCAN);
2947 scan = c->tasks->u.scan.scan;
2949 /* Z39.50 scan can only carry RPN */
2950 if (scan->query->z_query->which == Z_Query_type_1 ||
2951 scan->query->z_query->which == Z_Query_type_101)
2953 Z_RPNQuery *rpn = scan->query->z_query->u.type_1;
2954 const char *cp = ZOOM_options_get(scan->options, "rpnCharset");
2957 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
2960 rpn = yaz_copy_z_RPNQuery(rpn, c->odr_out);
2962 yaz_query_charset_convert_rpnquery(
2963 rpn, c->odr_out, cd);
2964 yaz_iconv_close(cd);
2967 req->attributeSet = rpn->attributeSetId;
2968 if (!req->attributeSet)
2969 req->attributeSet = odr_oiddup(c->odr_out, yaz_oid_attset_bib_1);
2970 if (rpn->RPNStructure->which == Z_RPNStructure_simple &&
2971 rpn->RPNStructure->u.simple->which == Z_Operand_APT)
2973 req->termListAndStartPoint =
2974 rpn->RPNStructure->u.simple->u.attributesPlusTerm;
2978 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
2979 return zoom_complete;
2984 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
2985 return zoom_complete;
2988 *req->numberOfTermsRequested =
2989 ZOOM_options_get_int(scan->options, "number", 20);
2991 req->preferredPositionInResponse =
2992 odr_intdup(c->odr_out,
2993 ZOOM_options_get_int(scan->options, "position", 1));
2996 odr_intdup(c->odr_out,
2997 ZOOM_options_get_int(scan->options, "stepSize", 0));
2999 req->databaseNames = scan->databaseNames;
3000 req->num_databaseNames = scan->num_databaseNames;
3002 return send_APDU(c, apdu);
3006 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
3010 const char *option_val = 0;
3013 return zoom_complete;
3014 assert (c->tasks->which == ZOOM_TASK_SCAN);
3015 scan = c->tasks->u.scan.scan;
3017 sr = ZOOM_srw_get_pdu(c, Z_SRW_scan_request);
3019 /* SRU scan can only carry CQL and PQF */
3020 if (scan->query->z_query->which == Z_Query_type_104)
3022 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
3023 sr->u.scan_request->scanClause.cql = scan->query->query_string;
3025 else if (scan->query->z_query->which == Z_Query_type_1
3026 || scan->query->z_query->which == Z_Query_type_101)
3028 sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
3029 sr->u.scan_request->scanClause.pqf = scan->query->query_string;
3033 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
3034 return zoom_complete;
3037 sr->u.scan_request->maximumTerms = odr_intdup(
3038 c->odr_out, ZOOM_options_get_int(scan->options, "number", 10));
3040 sr->u.scan_request->responsePosition = odr_intdup(
3041 c->odr_out, ZOOM_options_get_int(scan->options, "position", 1));
3043 option_val = ZOOM_options_get(scan->options, "extraArgs");
3044 yaz_encode_sru_extra(sr, c->odr_out, option_val);
3045 return send_srw(c, sr);
3048 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
3050 return zoom_complete;
3056 ZOOM_scanset_size(ZOOM_scanset scan)
3061 if (scan->scan_response && scan->scan_response->entries)
3062 return scan->scan_response->entries->num_entries;
3063 else if (scan->srw_scan_response)
3064 return scan->srw_scan_response->num_terms;
3068 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
3070 const char **value_term, size_t *value_len,
3071 const char **disp_term, size_t *disp_len)
3073 size_t noent = ZOOM_scanset_size(scan);
3084 if (scan->scan_response)
3086 Z_ScanResponse *res = scan->scan_response;
3087 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
3089 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
3091 *value_term = (const char *) t->term->u.general->buf;
3092 *value_len = t->term->u.general->len;
3095 *disp_term = t->displayTerm;
3096 *disp_len = strlen(*disp_term);
3098 else if (t->term->which == Z_Term_general)
3100 *disp_term = (const char *) t->term->u.general->buf;
3101 *disp_len = t->term->u.general->len;
3103 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
3106 if (scan->srw_scan_response)
3108 Z_SRW_scanResponse *res = scan->srw_scan_response;
3109 Z_SRW_scanTerm *t = res->terms + pos;
3112 *value_term = t->value;
3113 *value_len = strlen(*value_term);
3116 *disp_term = t->displayTerm;
3118 *disp_term = t->value;
3119 *disp_len = strlen(*disp_term);
3120 *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
3125 ZOOM_API(const char *)
3126 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
3127 size_t *occ, size_t *len)
3129 const char *value_term = 0;
3130 size_t value_len = 0;
3131 const char *disp_term = 0;
3132 size_t disp_len = 0;
3134 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
3135 &disp_term, &disp_len);
3141 ZOOM_API(const char *)
3142 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
3143 size_t *occ, size_t *len)
3145 const char *value_term = 0;
3146 size_t value_len = 0;
3147 const char *disp_term = 0;
3148 size_t disp_len = 0;
3150 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
3151 &disp_term, &disp_len);
3157 ZOOM_API(const char *)
3158 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
3160 return ZOOM_options_get(scan->options, key);
3164 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
3167 ZOOM_options_set(scan->options, key, val);
3170 static Z_APDU *create_es_package(ZOOM_package p, const Odr_oid *oid)
3173 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
3174 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3176 str = ZOOM_options_get(p->options, "package-name");
3178 req->packageName = odr_strdup(p->odr_out, str);
3180 str = ZOOM_options_get(p->options, "user-id");
3182 req->userId = odr_strdup_null(p->odr_out, str);
3184 req->packageType = odr_oiddup(p->odr_out, oid);
3186 str = ZOOM_options_get(p->options, "function");
3189 if (!strcmp (str, "create"))
3190 *req->function = Z_ExtendedServicesRequest_create;
3191 if (!strcmp (str, "delete"))
3192 *req->function = Z_ExtendedServicesRequest_delete;
3193 if (!strcmp (str, "modify"))
3194 *req->function = Z_ExtendedServicesRequest_modify;
3197 str = ZOOM_options_get(p->options, "waitAction");
3200 if (!strcmp (str, "wait"))
3201 *req->waitAction = Z_ExtendedServicesRequest_wait;
3202 if (!strcmp (str, "waitIfPossible"))
3203 *req->waitAction = Z_ExtendedServicesRequest_waitIfPossible;
3204 if (!strcmp (str, "dontWait"))
3205 *req->waitAction = Z_ExtendedServicesRequest_dontWait;
3206 if (!strcmp (str, "dontReturnPackage"))
3207 *req->waitAction = Z_ExtendedServicesRequest_dontReturnPackage;
3212 static const char *ill_array_lookup(void *clientData, const char *idx)
3214 ZOOM_package p = (ZOOM_package) clientData;
3215 return ZOOM_options_get(p->options, idx+4);
3218 static Z_External *encode_ill_request(ZOOM_package p)
3220 ODR out = p->odr_out;
3223 struct ill_get_ctl ctl;
3225 ctl.odr = p->odr_out;
3227 ctl.f = ill_array_lookup;
3229 req = ill_get_ILLRequest(&ctl, "ill", 0);
3231 if (!ill_Request(out, &req, 0, 0))
3233 int ill_request_size;
3234 char *ill_request_buf = odr_getbuf(out, &ill_request_size, 0);
3235 if (ill_request_buf)
3236 odr_setbuf(out, ill_request_buf, ill_request_size, 1);
3241 int illRequest_size = 0;
3242 char *illRequest_buf = odr_getbuf(out, &illRequest_size, 0);
3244 r = (Z_External *) odr_malloc(out, sizeof(*r));
3245 r->direct_reference = odr_oiddup(out, yaz_oid_general_isoill_1);
3246 r->indirect_reference = 0;
3248 r->which = Z_External_single;
3250 r->u.single_ASN1_type =
3251 odr_create_Odr_oct(out,
3252 (unsigned char *)illRequest_buf,
3258 static Z_ItemOrder *encode_item_order(ZOOM_package p)
3260 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc(p->odr_out, sizeof(*req));
3264 req->which = Z_IOItemOrder_esRequest;
3265 req->u.esRequest = (Z_IORequest *)
3266 odr_malloc(p->odr_out,sizeof(Z_IORequest));
3268 /* to keep part ... */
3269 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
3270 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
3271 req->u.esRequest->toKeep->supplDescription = 0;
3272 req->u.esRequest->toKeep->contact = (Z_IOContact *)
3273 odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
3275 str = ZOOM_options_get(p->options, "contact-name");
3276 req->u.esRequest->toKeep->contact->name =
3277 odr_strdup_null(p->odr_out, str);
3279 str = ZOOM_options_get(p->options, "contact-phone");
3280 req->u.esRequest->toKeep->contact->phone =
3281 odr_strdup_null(p->odr_out, str);
3283 str = ZOOM_options_get(p->options, "contact-email");
3284 req->u.esRequest->toKeep->contact->email =
3285 odr_strdup_null(p->odr_out, str);
3287 req->u.esRequest->toKeep->addlBilling = 0;
3289 /* not to keep part ... */
3290 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
3291 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
3293 str = ZOOM_options_get(p->options, "itemorder-setname");
3298 req->u.esRequest->notToKeep->resultSetItem = 0;
3301 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
3302 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
3304 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
3305 odr_strdup(p->odr_out, str);
3306 req->u.esRequest->notToKeep->resultSetItem->item =
3307 odr_intdup(p->odr_out, 0);
3309 str = ZOOM_options_get(p->options, "itemorder-item");
3310 *req->u.esRequest->notToKeep->resultSetItem->item =
3311 (str ? atoi(str) : 1);
3314 str = ZOOM_options_getl(p->options, "doc", &len);
3317 req->u.esRequest->notToKeep->itemRequest =
3318 z_ext_record_xml(p->odr_out, str, len);
3321 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
3326 Z_APDU *create_admin_package(ZOOM_package p, int type,
3327 Z_ESAdminOriginPartToKeep **toKeepP,
3328 Z_ESAdminOriginPartNotToKeep **notToKeepP)
3330 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_admin);
3333 Z_ESAdminOriginPartToKeep *toKeep;
3334 Z_ESAdminOriginPartNotToKeep *notToKeep;
3335 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3336 const char *first_db = "Default";
3338 char **db = set_DatabaseNames(p->connection, p->options, &num_db,
3343 r->direct_reference = odr_oiddup(p->odr_out, yaz_oid_extserv_admin);
3345 r->indirect_reference = 0;
3346 r->which = Z_External_ESAdmin;
3348 r->u.adminService = (Z_Admin *)
3349 odr_malloc(p->odr_out, sizeof(*r->u.adminService));
3350 r->u.adminService->which = Z_Admin_esRequest;
3351 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
3352 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest));
3354 toKeep = r->u.adminService->u.esRequest->toKeep =
3355 (Z_ESAdminOriginPartToKeep *)
3356 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep));
3357 toKeep->which = type;
3358 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3359 toKeep->u.create = odr_nullval();
3360 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3362 r->u.adminService->u.esRequest->notToKeep = notToKeep =
3363 (Z_ESAdminOriginPartNotToKeep *)
3364 odr_malloc(p->odr_out,
3365 sizeof(*r->u.adminService->u.esRequest->notToKeep));
3366 notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
3367 notToKeep->u.recordsWillFollow = odr_nullval();
3371 *notToKeepP = notToKeep;
3376 static Z_APDU *create_xmlupdate_package(ZOOM_package p)
3378 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_xml_es);
3379 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3380 Z_External *ext = (Z_External *) odr_malloc(p->odr_out, sizeof(*ext));
3382 const char *doc = ZOOM_options_getl(p->options, "doc", &len);
3390 req->taskSpecificParameters = ext;
3391 ext->direct_reference = req->packageType;
3392 ext->descriptor = 0;
3393 ext->indirect_reference = 0;
3395 ext->which = Z_External_octet;
3396 ext->u.single_ASN1_type =
3397 odr_create_Odr_oct(p->odr_out, (const unsigned char *) doc, len);
3401 static Z_APDU *create_update_package(ZOOM_package p)
3404 const char *first_db = "Default";
3406 char **db = set_DatabaseNames(p->connection, p->options, &num_db, p->odr_out);
3407 const char *action = ZOOM_options_get(p->options, "action");
3408 int recordIdOpaque_len;
3409 const char *recordIdOpaque = ZOOM_options_getl(p->options, "recordIdOpaque",
3410 &recordIdOpaque_len);
3411 const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
3413 const char *record_buf = ZOOM_options_getl(p->options, "record",
3415 int recordOpaque_len;
3416 const char *recordOpaque_buf = ZOOM_options_getl(p->options, "recordOpaque",
3418 const char *syntax_str = ZOOM_options_get(p->options, "syntax");
3419 const char *version = ZOOM_options_get(p->options, "updateVersion");
3421 const char *correlationInfo_note =
3422 ZOOM_options_get(p->options, "correlationInfo.note");
3423 const char *correlationInfo_id =
3424 ZOOM_options_get(p->options, "correlationInfo.id");
3426 Odr_oid *syntax_oid = 0;
3427 const Odr_oid *package_oid = yaz_oid_extserv_database_update;
3433 if (!record_buf && !recordOpaque_buf)
3435 record_buf = "void";
3437 syntax_str = "SUTRS";
3442 syntax_oid = yaz_string_to_oid_odr(yaz_oid_std(),
3443 CLASS_RECSYN, syntax_str,
3455 package_oid = yaz_oid_extserv_database_update_first_version;
3456 /* old update does not support specialUpdate */
3458 action = "recordInsert";
3462 action = "specialUpdate";
3463 package_oid = yaz_oid_extserv_database_update_second_version;
3467 action = "specialUpdate";
3468 package_oid = yaz_oid_extserv_database_update;
3474 if (!strcmp(action, "recordInsert"))
3475 action_no = Z_IUOriginPartToKeep_recordInsert;
3476 else if (!strcmp(action, "recordReplace"))
3477 action_no = Z_IUOriginPartToKeep_recordReplace;
3478 else if (!strcmp(action, "recordDelete"))
3479 action_no = Z_IUOriginPartToKeep_recordDelete;
3480 else if (!strcmp(action, "elementUpdate"))
3481 action_no = Z_IUOriginPartToKeep_elementUpdate;
3482 else if (!strcmp(action, "specialUpdate"))
3483 action_no = Z_IUOriginPartToKeep_specialUpdate;
3487 apdu = create_es_package(p, package_oid);
3490 Z_IUOriginPartToKeep *toKeep;
3491 Z_IUSuppliedRecords *notToKeep;
3492 Z_External *r = (Z_External *)
3493 odr_malloc(p->odr_out, sizeof(*r));
3494 const char *elementSetName =
3495 ZOOM_options_get(p->options, "elementSetName");
3497 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3499 r->direct_reference = odr_oiddup(p->odr_out, package_oid);
3501 r->which = Z_External_update;
3502 r->indirect_reference = 0;
3503 r->u.update = (Z_IUUpdate *)
3504 odr_malloc(p->odr_out, sizeof(*r->u.update));
3506 r->u.update->which = Z_IUUpdate_esRequest;
3507 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
3508 odr_malloc(p->odr_out, sizeof(*r->u.update->u.esRequest));
3509 toKeep = r->u.update->u.esRequest->toKeep =
3510 (Z_IUOriginPartToKeep *)
3511 odr_malloc(p->odr_out, sizeof(*toKeep));
3513 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3516 toKeep->elementSetName = odr_strdup_null(p->odr_out, elementSetName);
3518 toKeep->actionQualifier = 0;
3519 toKeep->action = odr_intdup(p->odr_out, action_no);
3521 notToKeep = r->u.update->u.esRequest->notToKeep =
3522 (Z_IUSuppliedRecords *)
3523 odr_malloc(p->odr_out, sizeof(*notToKeep));
3525 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
3526 odr_malloc(p->odr_out, sizeof(*notToKeep->elements));
3527 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
3528 odr_malloc(p->odr_out, sizeof(**notToKeep->elements));
3529 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
3532 notToKeep->elements[0]->u.opaque =
3533 odr_create_Odr_oct(p->odr_out,
3534 (const unsigned char *) recordIdOpaque,
3535 recordIdOpaque_len);
3537 else if (recordIdNumber)
3539 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number;
3541 notToKeep->elements[0]->u.number =
3542 odr_intdup(p->odr_out, atoi(recordIdNumber));
3545 notToKeep->elements[0]->u.opaque = 0;
3546 notToKeep->elements[0]->supplementalId = 0;
3547 if (correlationInfo_note || correlationInfo_id)
3549 Z_IUCorrelationInfo *ci;
3550 ci = notToKeep->elements[0]->correlationInfo =
3551 (Z_IUCorrelationInfo *) odr_malloc(p->odr_out, sizeof(*ci));
3552 ci->note = odr_strdup_null(p->odr_out, correlationInfo_note);
3553 ci->id = correlationInfo_id ?
3554 odr_intdup(p->odr_out, atoi(correlationInfo_id)) : 0;
3557 notToKeep->elements[0]->correlationInfo = 0;
3558 if (recordOpaque_buf)
3560 notToKeep->elements[0]->record =
3561 z_ext_record_oid_any(p->odr_out, syntax_oid,
3562 recordOpaque_buf, recordOpaque_len);
3566 notToKeep->elements[0]->record =
3567 z_ext_record_oid(p->odr_out, syntax_oid,
3568 record_buf, record_len);
3573 ODR print = odr_createmem(ODR_PRINT);
3575 z_APDU(print, &apdu, 0, 0);
3582 ZOOM_package_send(ZOOM_package p, const char *type)
3589 odr_reset(p->odr_out);
3592 if (!strcmp(type, "itemorder"))
3594 apdu = create_es_package(p, yaz_oid_extserv_item_order);
3597 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3599 r->direct_reference =
3600 odr_oiddup(p->odr_out, yaz_oid_extserv_item_order);
3602 r->which = Z_External_itemOrder;
3603 r->indirect_reference = 0;
3604 r->u.itemOrder = encode_item_order(p);
3606 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3609 else if (!strcmp(type, "create")) /* create database */
3611 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
3614 else if (!strcmp(type, "drop")) /* drop database */
3616 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
3619 else if (!strcmp(type, "commit")) /* commit changes */
3621 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit,
3624 else if (!strcmp(type, "update")) /* update record(s) */
3626 apdu = create_update_package(p);
3628 else if (!strcmp(type, "xmlupdate"))
3630 apdu = create_xmlupdate_package(p);
3634 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
3638 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_PACKAGE);
3639 task->u.package = p;
3640 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
3641 p->buf_out = (char *) xmalloc(p->len_out);
3642 memcpy(p->buf_out, buf, p->len_out);
3647 while (ZOOM_event(1, &c))
3654 ZOOM_API(ZOOM_package)
3655 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
3657 ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
3660 p->odr_out = odr_createmem(ODR_ENCODE);
3661 p->options = ZOOM_options_create_with_parent2(options, c->options);
3669 ZOOM_package_destroy(ZOOM_package p)
3674 if (p->refcount == 0)
3676 odr_destroy(p->odr_out);
3679 ZOOM_options_destroy(p->options);
3684 ZOOM_API(const char *)
3685 ZOOM_package_option_get(ZOOM_package p, const char *key)
3687 return ZOOM_options_get(p->options, key);
3690 ZOOM_API(const char *)
3691 ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
3693 return ZOOM_options_getl(p->options, key, lenp);
3697 ZOOM_package_option_set(ZOOM_package p, const char *key,
3700 ZOOM_options_set(p->options, key, val);
3704 ZOOM_package_option_setl(ZOOM_package p, const char *key,
3705 const char *val, int len)
3707 ZOOM_options_setl(p->options, key, val, len);
3711 ZOOM_connection_exec_task(ZOOM_connection c)
3713 ZOOM_task task = c->tasks;
3714 zoom_ret ret = zoom_complete;
3718 yaz_log(log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
3719 c, task->which, task->running);
3720 if (c->error != ZOOM_ERROR_NONE)
3722 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3723 "removing tasks because of error = %d", c, c->error);
3724 ZOOM_connection_remove_tasks(c);
3729 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3730 "task already running", c);
3734 ret = zoom_complete;
3735 if (c->cs || task->which == ZOOM_TASK_CONNECT)
3737 switch (task->which)
3739 case ZOOM_TASK_SEARCH:
3740 if (c->proto == PROTO_HTTP)
3741 ret = ZOOM_connection_srw_send_search(c);
3743 ret = ZOOM_connection_send_search(c);
3745 case ZOOM_TASK_RETRIEVE:
3746 if (c->proto == PROTO_HTTP)
3747 ret = ZOOM_connection_srw_send_search(c);
3749 ret = send_present(c);
3751 case ZOOM_TASK_CONNECT:
3752 ret = do_connect(c);
3754 case ZOOM_TASK_SCAN:
3755 if (c->proto == PROTO_HTTP)
3756 ret = ZOOM_connection_srw_send_scan(c);
3758 ret = ZOOM_connection_send_scan(c);
3760 case ZOOM_TASK_PACKAGE:
3761 ret = send_package(c);
3763 case ZOOM_TASK_SORT:
3764 c->tasks->u.sort.resultset->r_sort_spec =
3765 c->tasks->u.sort.q->sort_spec;
3766 ret = send_sort(c, c->tasks->u.sort.resultset);
3772 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3773 "remove tasks because no connection exist", c);
3774 ZOOM_connection_remove_tasks(c);
3776 if (ret == zoom_complete)
3778 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3779 "task removed (complete)", c);
3780 ZOOM_connection_remove_task(c);
3783 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3788 static zoom_ret send_sort_present(ZOOM_connection c)
3790 zoom_ret r = zoom_complete;
3792 if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
3793 r = send_sort(c, c->tasks->u.search.resultset);
3794 if (r == zoom_complete)
3795 r = send_present(c);
3799 static int es_response_taskpackage_update(ZOOM_connection c,
3800 Z_IUUpdateTaskPackage *utp)
3802 if (utp && utp->targetPart)
3804 Z_IUTargetPart *targetPart = utp->targetPart;
3805 switch ( *targetPart->updateStatus ) {
3806 case Z_IUTargetPart_success:
3807 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "success");
3809 case Z_IUTargetPart_partial:
3810 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "partial");
3812 case Z_IUTargetPart_failure:
3813 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "failure");
3814 if (targetPart->globalDiagnostics && targetPart->num_globalDiagnostics > 0)
3815 response_diag(c, targetPart->globalDiagnostics[0]);
3818 // NOTE: Individual record status, surrogate diagnostics, and supplemental diagnostics ARE NOT REPORTED.
3823 static int es_response_taskpackage(ZOOM_connection c,
3824 Z_TaskPackage *taskPackage)
3827 Odr_oct *id = taskPackage->targetReference;
3829 ZOOM_options_setl(c->tasks->u.package->options,
3830 "targetReference", (char*) id->buf, id->len);
3833 switch ( *taskPackage->taskStatus ) {
3834 case Z_TaskPackage_pending:
3835 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "pending");
3837 case Z_TaskPackage_active:
3838 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "active");
3840 case Z_TaskPackage_complete:
3841 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "complete");
3843 case Z_TaskPackage_aborted:
3844 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "aborted");
3845 if ( taskPackage->num_packageDiagnostics && taskPackage->packageDiagnostics )
3846 response_diag(c, taskPackage->packageDiagnostics[0]);
3850 // taskSpecificParameters
3851 // NOTE: Only Update implemented, no others.
3852 if ( taskPackage->taskSpecificParameters->which == Z_External_update ) {
3853 Z_IUUpdateTaskPackage *utp = taskPackage->taskSpecificParameters->u.update->u.taskPackage;
3854 es_response_taskpackage_update(c, utp);
3860 static int es_response(ZOOM_connection c,
3861 Z_ExtendedServicesResponse *res)
3863 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
3865 switch (*res->operationStatus) {
3866 case Z_ExtendedServicesResponse_done:
3867 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "done");
3869 case Z_ExtendedServicesResponse_accepted:
3870 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "accepted");
3872 case Z_ExtendedServicesResponse_failure:
3873 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "failure");
3874 if (res->diagnostics && res->num_diagnostics > 0)
3875 response_diag(c, res->diagnostics[0]);
3878 if (res->taskPackage &&
3879 res->taskPackage->which == Z_External_extendedService)
3881 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
3882 es_response_taskpackage(c, taskPackage);
3884 if (res->taskPackage &&
3885 res->taskPackage->which == Z_External_octet)
3887 Odr_oct *doc = res->taskPackage->u.octet_aligned;
3888 ZOOM_options_setl(c->tasks->u.package->options,
3889 "xmlUpdateDoc", (char*) doc->buf, doc->len);
3894 static void interpret_init_diag(ZOOM_connection c,
3895 Z_DiagnosticFormat *diag)
3899 Z_DiagnosticFormat_s *ds = diag->elements[0];
3900 if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
3901 response_default_diag(c, ds->u.defaultDiagRec);
3906 static void interpret_otherinformation_field(ZOOM_connection c,
3907 Z_OtherInformation *ui)
3910 for (i = 0; i < ui->num_elements; i++)
3912 Z_OtherInformationUnit *unit = ui->list[i];
3913 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
3914 unit->information.externallyDefinedInfo &&
3915 unit->information.externallyDefinedInfo->which ==
3918 interpret_init_diag(c, unit->information.externallyDefinedInfo->u.diag1);
3924 static void set_init_option(const char *name, void *clientData) {
3925 ZOOM_connection c = (ZOOM_connection) clientData;
3928 sprintf(buf, "init_opt_%.70s", name);
3929 ZOOM_connection_option_set(c, buf, "1");
3933 static void recv_apdu(ZOOM_connection c, Z_APDU *apdu)
3935 Z_InitResponse *initrs;
3937 ZOOM_connection_set_mask(c, 0);
3938 yaz_log(log_details, "%p recv_apdu apdu->which=%d", c, apdu->which);
3941 case Z_APDU_initResponse:
3942 yaz_log(log_api, "%p recv_apdu: Received Init response", c);
3943 initrs = apdu->u.initResponse;
3944 ZOOM_connection_option_set(c, "serverImplementationId",
3945 initrs->implementationId ?
3946 initrs->implementationId : "");
3947 ZOOM_connection_option_set(c, "serverImplementationName",
3948 initrs->implementationName ?
3949 initrs->implementationName : "");
3950 ZOOM_connection_option_set(c, "serverImplementationVersion",
3951 initrs->implementationVersion ?
3952 initrs->implementationVersion : "");
3953 /* Set the three old options too, for old applications */
3954 ZOOM_connection_option_set(c, "targetImplementationId",
3955 initrs->implementationId ?
3956 initrs->implementationId : "");
3957 ZOOM_connection_option_set(c, "targetImplementationName",
3958 initrs->implementationName ?
3959 initrs->implementationName : "");
3960 ZOOM_connection_option_set(c, "targetImplementationVersion",
3961 initrs->implementationVersion ?
3962 initrs->implementationVersion : "");
3964 /* Make initrs->options available as ZOOM-level options */
3965 yaz_init_opt_decode(initrs->options, set_init_option, (void*) c);
3967 if (!*initrs->result)
3969 Z_External *uif = initrs->userInformationField;
3971 set_ZOOM_error(c, ZOOM_ERROR_INIT, 0); /* default error */
3973 if (uif && uif->which == Z_External_userInfo1)
3974 interpret_otherinformation_field(c, uif->u.userInfo1);
3979 yaz_oi_get_string_oid(&apdu->u.initResponse->otherInfo,
3980 yaz_oid_userinfo_cookie, 1, 0);
3981 xfree(c->cookie_in);
3984 c->cookie_in = xstrdup(cookie);
3985 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
3986 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
3987 c->support_named_resultsets = 1;
3990 assert(c->tasks->which == ZOOM_TASK_CONNECT);
3991 ZOOM_connection_remove_task(c);
3993 ZOOM_connection_exec_task(c);
3995 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
3997 NMEM tmpmem = nmem_create();
3998 Z_CharSetandLanguageNegotiation *p =
3999 yaz_get_charneg_record(initrs->otherInfo);
4003 char *charset = NULL, *lang = NULL;
4006 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
4007 yaz_log(log_details, "%p recv_apdu target accepted: "
4008 "charset %s, language %s, select %d",
4010 charset ? charset : "none", lang ? lang : "none", sel);
4012 ZOOM_connection_option_set(c, "negotiation-charset",
4015 ZOOM_connection_option_set(c, "negotiation-lang",
4018 ZOOM_connection_option_set(
4019 c, "negotiation-charset-in-effect-for-records",
4020 (sel != 0) ? "1" : "0");
4021 nmem_destroy(tmpmem);
4025 case Z_APDU_searchResponse:
4026 yaz_log(log_api, "%p recv_apdu Search response", c);
4027 handle_search_response(c, apdu->u.searchResponse);
4028 if (send_sort_present(c) == zoom_complete)
4029 ZOOM_connection_remove_task(c);
4031 case Z_APDU_presentResponse:
4032 yaz_log(log_api, "%p recv_apdu Present response", c);
4033 handle_present_response(c, apdu->u.presentResponse);
4034 if (send_present(c) == zoom_complete)
4035 ZOOM_connection_remove_task(c);
4037 case Z_APDU_sortResponse:
4038 yaz_log(log_api, "%p recv_apdu Sort response", c);
4039 sort_response(c, apdu->u.sortResponse);
4040 if (send_present(c) == zoom_complete)
4041 ZOOM_connection_remove_task(c);
4043 case Z_APDU_scanResponse:
4044 yaz_log(log_api, "%p recv_apdu Scan response", c);
4045 scan_response(c, apdu->u.scanResponse);
4046 ZOOM_connection_remove_task(c);
4048 case Z_APDU_extendedServicesResponse:
4049 yaz_log(log_api, "%p recv_apdu Extended Services response", c);
4050 es_response(c, apdu->u.extendedServicesResponse);
4051 ZOOM_connection_remove_task(c);
4054 yaz_log(log_api, "%p recv_apdu Close PDU", c);
4055 if (!ZOOM_test_reconnect(c))
4057 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4062 yaz_log(log_api, "%p Received unknown PDU", c);
4063 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
4069 static zoom_ret handle_srw_response(ZOOM_connection c,
4070 Z_SRW_searchRetrieveResponse *res)
4072 ZOOM_resultset resultset = 0;
4077 const char *syntax, *elementSetName;
4080 return zoom_complete;
4082 switch(c->tasks->which)
4084 case ZOOM_TASK_SEARCH:
4085 resultset = c->tasks->u.search.resultset;
4086 start = &c->tasks->u.search.start;
4087 count = &c->tasks->u.search.count;
4088 syntax = c->tasks->u.search.syntax;
4089 elementSetName = c->tasks->u.search.elementSetName;
4091 if (!c->tasks->u.search.recv_search_fired)
4093 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
4094 ZOOM_connection_put_event(c, event);
4095 c->tasks->u.search.recv_search_fired = 1;
4098 case ZOOM_TASK_RETRIEVE:
4099 resultset = c->tasks->u.retrieve.resultset;
4100 start = &c->tasks->u.retrieve.start;
4101 count = &c->tasks->u.retrieve.count;
4102 syntax = c->tasks->u.retrieve.syntax;
4103 elementSetName = c->tasks->u.retrieve.elementSetName;
4106 return zoom_complete;
4109 resultset->size = 0;
4111 if (res->resultSetId)
4112 ZOOM_resultset_option_set(resultset, "resultSetId", res->resultSetId);
4114 yaz_log(log_details, "%p handle_srw_response got SRW response OK", c);
4116 if (res->num_diagnostics > 0)
4118 set_SRU_error(c, &res->diagnostics[0]);
4122 if (res->numberOfRecords)
4123 resultset->size = *res->numberOfRecords;
4124 for (i = 0; i<res->num_records; i++)
4127 Z_SRW_record *sru_rec;
4128 Z_SRW_diagnostic *diag = 0;
4131 Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
4132 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
4134 if (res->records[i].recordPosition &&
4135 *res->records[i].recordPosition > 0)
4136 pos = *res->records[i].recordPosition - 1;
4140 sru_rec = &res->records[i];
4142 npr->databaseName = 0;
4143 npr->which = Z_NamePlusRecord_databaseRecord;
4144 npr->u.databaseRecord = (Z_External *)
4145 odr_malloc(c->odr_in, sizeof(Z_External));
4146 npr->u.databaseRecord->descriptor = 0;
4147 npr->u.databaseRecord->direct_reference =
4148 odr_oiddup(c->odr_in, yaz_oid_recsyn_xml);
4149 npr->u.databaseRecord->which = Z_External_octet;
4151 npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
4152 odr_malloc(c->odr_in, sizeof(Odr_oct));
4153 npr->u.databaseRecord->u.octet_aligned->buf = (unsigned char*)
4154 sru_rec->recordData_buf;
4155 npr->u.databaseRecord->u.octet_aligned->len =
4156 npr->u.databaseRecord->u.octet_aligned->size =
4157 sru_rec->recordData_len;
4159 if (sru_rec->recordSchema
4160 && !strcmp(sru_rec->recordSchema,
4161 "info:srw/schema/1/diagnostics-v1.1"))
4163 sru_decode_surrogate_diagnostics(sru_rec->recordData_buf,
4164 sru_rec->recordData_len,
4168 record_cache_add(resultset, npr, pos, syntax, elementSetName,
4169 sru_rec->recordSchema, diag);
4173 if (*count + *start > resultset->size)
4174 *count = resultset->size - *start;
4178 nmem = odr_extract_mem(c->odr_in);
4179 nmem_transfer(odr_getmem(resultset->odr), nmem);
4183 return ZOOM_connection_srw_send_search(c);
4185 return zoom_complete;
4190 static void handle_srw_scan_response(ZOOM_connection c,
4191 Z_SRW_scanResponse *res)
4193 NMEM nmem = odr_extract_mem(c->odr_in);
4196 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
4198 scan = c->tasks->u.scan.scan;
4200 if (res->num_diagnostics > 0)
4201 set_SRU_error(c, &res->diagnostics[0]);
4203 scan->scan_response = 0;
4204 scan->srw_scan_response = res;
4205 nmem_transfer(odr_getmem(scan->odr), nmem);
4207 ZOOM_options_set_int(scan->options, "number", res->num_terms);
4213 static Z_GDU *get_HTTP_Request_url(ODR odr, const char *url)
4215 Z_GDU *p = z_get_HTTP_Request(odr);
4216 const char *host = url;
4217 const char *cp0 = strstr(host, "://");
4218 const char *cp1 = 0;
4224 cp1 = strchr(cp0, '/');
4226 cp1 = cp0 + strlen(cp0);
4230 char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
4231 memcpy (h, cp0, cp1 - cp0);
4233 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers, "Host", h);
4235 p->u.HTTP_Request->path = odr_strdup(odr, *cp1 ? cp1 : "/");
4239 static zoom_ret send_SRW_redirect(ZOOM_connection c, const char *uri,
4240 Z_HTTP_Response *cookie_hres)
4242 struct Z_HTTP_Header *h;
4243 Z_GDU *gdu = get_HTTP_Request_url(c->odr_out, uri);
4245 gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET");
4246 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept",
4249 for (h = cookie_hres->headers; h; h = h->next)
4251 if (!strcmp(h->name, "Set-Cookie"))
4252 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
4253 "Cookie", h->value);
4255 if (c->user && c->password)
4257 z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
4258 c->user, c->password);
4260 if (!z_GDU(c->odr_out, &gdu, 0, 0))
4261 return zoom_complete;
4263 z_GDU(c->odr_print, &gdu, 0, 0);
4264 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
4266 odr_reset(c->odr_out);
4270 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
4272 zoom_ret cret = zoom_complete;
4274 const char *addinfo = 0;
4275 const char *connection_head = z_HTTP_header_lookup(hres->headers,
4277 const char *location;
4279 ZOOM_connection_set_mask(c, 0);
4280 yaz_log(log_details, "%p handle_http", c);
4282 if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get
4283 && (location = z_HTTP_header_lookup(hres->headers, "Location")))
4286 if (c->no_redirects > 10)
4288 set_HTTP_error(c, hres->code, 0, 0);
4289 c->no_redirects = 0;
4294 /* since redirect may change host we just reconnect. A smarter
4295 implementation might check whether it's the same server */
4296 do_connect_host(c, location, 0);
4297 send_SRW_redirect(c, location, hres);
4298 /* we're OK for now. Operation is not really complete */
4300 cret = zoom_pending;
4304 { /* not redirect (normal response) */
4305 if (!yaz_srw_check_content_type(hres))
4306 addinfo = "content-type";
4309 Z_SOAP *soap_package = 0;
4311 Z_SOAP_Handler soap_handlers[2] = {
4312 {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
4315 ret = z_soap_codec(o, &soap_package,
4316 &hres->content_buf, &hres->content_len,
4318 if (!ret && soap_package->which == Z_SOAP_generic &&
4319 soap_package->u.generic->no == 0)
4321 Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
4323 ZOOM_options_set(c->options, "sru_version", sr->srw_version);
4324 ZOOM_options_setl(c->options, "sru_extra_response_data",
4325 sr->extraResponseData_buf, sr->extraResponseData_len);
4326 if (sr->which == Z_SRW_searchRetrieve_response)
4327 cret = handle_srw_response(c, sr->u.response);
4328 else if (sr->which == Z_SRW_scan_response)
4329 handle_srw_scan_response(c, sr->u.scan_response);
4333 else if (!ret && (soap_package->which == Z_SOAP_fault
4334 || soap_package->which == Z_SOAP_error))
4336 set_HTTP_error(c, hres->code,
4337 soap_package->u.fault->fault_code,
4338 soap_package->u.fault->fault_string);
4345 if (c->no_redirects) /* end of redirect. change hosts again */
4348 c->no_redirects = 0;
4352 if (hres->code != 200)
4353 set_HTTP_error(c, hres->code, 0, 0);
4355 set_ZOOM_error(c, ZOOM_ERROR_DECODE, addinfo);
4358 if (cret == zoom_complete)
4359 ZOOM_connection_remove_task(c);
4360 if (!strcmp(hres->version, "1.0"))
4362 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
4363 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
4368 /* HTTP 1.1: only if no close we stay alive .. */
4369 if (connection_head && !strcmp(connection_head, "close"))
4375 static int do_read(ZOOM_connection c)
4380 event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
4381 ZOOM_connection_put_event(c, event);
4383 r = cs_get(c->cs, &c->buf_in, &c->len_in);
4384 more = cs_more(c->cs);
4385 yaz_log(log_details, "%p do_read len=%d more=%d", c, r, more);
4390 if (!ZOOM_test_reconnect(c))
4392 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4401 odr_reset(c->odr_in);
4402 odr_setbuf(c->odr_in, c->buf_in, r, 0);
4403 event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
4404 ZOOM_connection_put_event(c, event);
4406 if (!z_GDU(c->odr_in, &gdu, 0, 0))
4409 int err = odr_geterrorx(c->odr_in, &x);
4411 const char *element = odr_getelement(c->odr_in);
4412 yaz_snprintf(msg, sizeof(msg),
4413 "ODR code %d:%d element=%s offset=%d",
4414 err, x, element ? element : "<unknown>",
4415 odr_offset(c->odr_in));
4416 set_ZOOM_error(c, ZOOM_ERROR_DECODE, msg);
4419 FILE *ber_file = yaz_log_file();
4421 odr_dumpBER(ber_file, c->buf_in, r);
4428 z_GDU(c->odr_print, &gdu, 0, 0);
4429 if (gdu->which == Z_GDU_Z3950)
4430 recv_apdu(c, gdu->u.z3950);
4431 else if (gdu->which == Z_GDU_HTTP_Response)
4434 handle_http(c, gdu->u.HTTP_Response);
4436 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
4441 c->reconnect_ok = 0;
4446 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
4451 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
4452 ZOOM_connection_put_event(c, event);
4454 yaz_log(log_details, "%p do_write_ex len=%d", c, len_out);
4455 if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
4457 yaz_log(log_details, "%p do_write_ex write failed", c);
4458 if (ZOOM_test_reconnect(c))
4460 return zoom_pending;
4462 if (c->state == STATE_CONNECTING)
4463 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4465 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4467 return zoom_complete;
4471 int mask = ZOOM_SELECT_EXCEPT;
4472 if (c->cs->io_pending & CS_WANT_WRITE)
4473 mask += ZOOM_SELECT_WRITE;
4474 if (c->cs->io_pending & CS_WANT_READ)
4475 mask += ZOOM_SELECT_READ;
4476 ZOOM_connection_set_mask(c, mask);
4477 yaz_log(log_details, "%p do_write_ex write incomplete mask=%d",
4482 ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
4483 yaz_log(log_details, "%p do_write_ex write complete mask=%d",
4486 return zoom_pending;
4489 static zoom_ret do_write(ZOOM_connection c)
4491 return do_write_ex(c, c->buf_out, c->len_out);
4495 ZOOM_API(const char *)
4496 ZOOM_connection_option_get(ZOOM_connection c, const char *key)
4498 return ZOOM_options_get(c->options, key);
4501 ZOOM_API(const char *)
4502 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
4504 return ZOOM_options_getl(c->options, key, lenp);
4508 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
4511 ZOOM_options_set(c->options, key, val);
4515 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
4516 const char *val, int len)
4518 ZOOM_options_setl(c->options, key, val, len);
4521 ZOOM_API(const char *)
4522 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
4524 return ZOOM_options_get(r->options, key);
4528 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
4531 ZOOM_options_set(r->options, key, val);
4536 ZOOM_connection_errcode(ZOOM_connection c)
4538 return ZOOM_connection_error(c, 0, 0);
4541 ZOOM_API(const char *)
4542 ZOOM_connection_errmsg(ZOOM_connection c)
4545 ZOOM_connection_error(c, &msg, 0);
4549 ZOOM_API(const char *)
4550 ZOOM_connection_addinfo(ZOOM_connection c)
4552 const char *addinfo;
4553 ZOOM_connection_error(c, 0, &addinfo);
4557 ZOOM_API(const char *)
4558 ZOOM_connection_diagset(ZOOM_connection c)
4560 const char *diagset;
4561 ZOOM_connection_error_x(c, 0, 0, &diagset);
4565 ZOOM_API(const char *)
4566 ZOOM_diag_str(int error)
4570 case ZOOM_ERROR_NONE:
4572 case ZOOM_ERROR_CONNECT:
4573 return "Connect failed";
4574 case ZOOM_ERROR_MEMORY:
4575 return "Out of memory";
4576 case ZOOM_ERROR_ENCODE:
4577 return "Encoding failed";
4578 case ZOOM_ERROR_DECODE:
4579 return "Decoding failed";
4580 case ZOOM_ERROR_CONNECTION_LOST:
4581 return "Connection lost";
4582 case ZOOM_ERROR_INIT:
4583 return "Init rejected";
4584 case ZOOM_ERROR_INTERNAL:
4585 return "Internal failure";
4586 case ZOOM_ERROR_TIMEOUT:
4588 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
4589 return "Unsupported protocol";
4590 case ZOOM_ERROR_UNSUPPORTED_QUERY:
4591 return "Unsupported query type";
4592 case ZOOM_ERROR_INVALID_QUERY:
4593 return "Invalid query";
4594 case ZOOM_ERROR_CQL_PARSE:
4595 return "CQL parsing error";
4596 case ZOOM_ERROR_CQL_TRANSFORM:
4597 return "CQL transformation error";
4598 case ZOOM_ERROR_CCL_CONFIG:
4599 return "CCL configuration error";
4600 case ZOOM_ERROR_CCL_PARSE:
4601 return "CCL parsing error";
4603 return diagbib1_str(error);
4608 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
4609 const char **addinfo, const char **diagset)
4611 int error = c->error;
4614 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
4615 *cp = ZOOM_diag_str(error);
4616 else if (!strcmp(c->diagset, "HTTP"))
4617 *cp = z_HTTP_errmsg(c->error);
4618 else if (!strcmp(c->diagset, "Bib-1"))
4619 *cp = ZOOM_diag_str(error);
4620 else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
4621 *cp = yaz_diag_srw_str(c->error);
4623 *cp = "Unknown error and diagnostic set";
4626 *addinfo = c->addinfo ? c->addinfo : "";
4628 *diagset = c->diagset ? c->diagset : "";
4633 ZOOM_connection_error(ZOOM_connection c, const char **cp,
4634 const char **addinfo)
4636 return ZOOM_connection_error_x(c, cp, addinfo, 0);
4639 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
4641 ZOOM_Event event = 0;
4642 int r = cs_look(c->cs);
4643 yaz_log(log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
4648 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4649 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4651 ZOOM_connection_put_event(c, event);
4653 else if (r == CS_CONNECT)
4655 int ret = ret = cs_rcvconnect(c->cs);
4656 yaz_log(log_details, "%p ZOOM_connection_do_io "
4657 "cs_rcvconnect returned %d", c, ret);
4660 int mask = ZOOM_SELECT_EXCEPT;
4661 if (c->cs->io_pending & CS_WANT_WRITE)
4662 mask += ZOOM_SELECT_WRITE;
4663 if (c->cs->io_pending & CS_WANT_READ)
4664 mask += ZOOM_SELECT_READ;
4665 ZOOM_connection_set_mask(c, mask);
4666 event = ZOOM_Event_create(ZOOM_EVENT_NONE);
4667 ZOOM_connection_put_event(c, event);
4671 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4672 ZOOM_connection_put_event(c, event);
4674 if (c->proto == PROTO_Z3950)
4675 ZOOM_connection_send_init(c);
4678 /* no init request for SRW .. */
4679 assert(c->tasks->which == ZOOM_TASK_CONNECT);
4680 ZOOM_connection_remove_task(c);
4681 ZOOM_connection_set_mask(c, 0);
4682 ZOOM_connection_exec_task(c);
4684 c->state = STATE_ESTABLISHED;
4688 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4694 if (mask & ZOOM_SELECT_EXCEPT)
4696 if (!ZOOM_test_reconnect(c))
4698 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4703 if (mask & ZOOM_SELECT_READ)
4705 if (c->cs && (mask & ZOOM_SELECT_WRITE))
4711 ZOOM_connection_last_event(ZOOM_connection cs)
4714 return ZOOM_EVENT_NONE;
4715 return cs->last_event;
4719 static void cql2pqf_wrbuf_puts(const char *buf, void *client_data)
4721 WRBUF wrbuf = (WRBUF) client_data;
4722 wrbuf_puts(wrbuf, buf);
4726 * Returns an xmalloc()d string containing RPN that corresponds to the
4727 * CQL passed in. On error, sets the Connection object's error state
4728 * and returns a null pointer.
4729 * ### We could cache CQL parser and/or transformer in Connection.
4731 static char *cql2pqf(ZOOM_connection c, const char *cql)
4735 const char *cqlfile;
4736 cql_transform_t trans;
4739 parser = cql_parser_create();
4740 if ((error = cql_parser_string(parser, cql)) != 0) {
4741 cql_parser_destroy(parser);
4742 set_ZOOM_error(c, ZOOM_ERROR_CQL_PARSE, cql);
4746 cqlfile = ZOOM_connection_option_get(c, "cqlfile");
4749 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, "no CQL transform file");
4751 else if ((trans = cql_transform_open_fname(cqlfile)) == 0)
4754 sprintf(buf, "can't open CQL transform file '%.200s': %.200s",
4755 cqlfile, strerror(errno));
4756 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4760 WRBUF wrbuf_result = wrbuf_alloc();
4761 error = cql_transform(trans, cql_parser_result(parser),
4762 cql2pqf_wrbuf_puts, wrbuf_result);
4765 const char *addinfo;
4766 error = cql_transform_error(trans, &addinfo);
4767 sprintf(buf, "%.200s (addinfo=%.200s)",
4768 cql_strerror(error), addinfo);
4769 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4773 result = xstrdup(wrbuf_cstr(wrbuf_result));
4775 cql_transform_close(trans);
4776 wrbuf_destroy(wrbuf_result);
4778 cql_parser_destroy(parser);
4782 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
4786 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
4787 /* timeout and this connection was waiting */
4788 set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
4790 ZOOM_connection_put_event(c, event);
4796 ZOOM_connection_process(ZOOM_connection c)
4802 event = ZOOM_connection_get_event(c);
4805 ZOOM_Event_destroy(event);
4808 ZOOM_connection_exec_task(c);
4809 event = ZOOM_connection_get_event(c);
4812 ZOOM_Event_destroy(event);
4819 ZOOM_event_nonblock(int no, ZOOM_connection *cs)
4823 yaz_log(log_details, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
4825 for (i = 0; i<no; i++)
4827 ZOOM_connection c = cs[i];
4829 if (c && ZOOM_connection_process(c))
4835 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
4837 if (c->mask && mask)
4838 ZOOM_connection_do_io(c, mask);
4842 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
4845 return cs_fileno(c->cs);
4849 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
4857 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
4864 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
4866 return ZOOM_options_get_int(c->options, "timeout", 30);
4869 ZOOM_API(void) ZOOM_connection_close(ZOOM_connection c)
4877 * c-file-style: "Stroustrup"
4878 * indent-tabs-mode: nil
4880 * vim: shiftwidth=4 tabstop=8 expandtab