+ std::string authentication;
+ std::string content_authentication;
+ std::string content_proxy;
+ std::string realm = session_realm;
+ if (realm.length() == 0)
+ realm = m_p->default_realm;
+
+ const char *param_user = 0;
+ const char *param_password = 0;
+ const char *param_content_user = 0;
+ const char *param_content_password = 0;
+ const char *param_nocproxy = 0;
+ int no_parms = 0;
+
+ char **names;
+ char **values;
+ int no_out_args = 0;
+ if (input_args.length())
+ no_parms = yaz_uri_to_array(input_args.c_str(),
+ odr, &names, &values);
+ // adding 10 because we'll be adding other URL args
+ const char **out_names = (const char **)
+ odr_malloc(odr, (10 + no_parms) * sizeof(*out_names));
+ const char **out_values = (const char **)
+ odr_malloc(odr, (10 + no_parms) * sizeof(*out_values));
+
+ // may be changed if it's a content connection
+ std::string torus_url = m_p->torus_searchable_url;
+ int i;
+ for (i = 0; i < no_parms; i++)
+ {
+ const char *name = names[i];
+ const char *value = values[i];
+ assert(name);
+ assert(value);
+ if (!strcmp(name, "user"))
+ param_user = value;
+ else if (!strcmp(name, "password"))
+ param_password = value;
+ else if (!strcmp(name, "content-user"))
+ param_content_user = value;
+ else if (!strcmp(name, "content-password"))
+ param_content_password = value;
+ else if (!strcmp(name, "content-proxy"))
+ content_proxy = value;
+ else if (!strcmp(name, "nocproxy"))
+ param_nocproxy = value;
+ else if (!strcmp(name, "proxy"))
+ {
+ char **dstr;
+ int dnum = 0;
+ nmem_strsplit(((ODR) odr)->mem, ",", value, &dstr, &dnum);
+ if (connection_reuse)
+ {
+ // find the step after our current proxy
+ int i;
+ for (i = 0; i < dnum; i++)
+ if (!strcmp(proxy.c_str(), dstr[i]))
+ break;
+ if (i >= dnum - 1)
+ *proxy_step = 0;
+ else
+ *proxy_step = i + 1;
+ }
+ else
+ {
+ // step is known.. Guess our proxy from it
+ if (*proxy_step >= dnum)
+ *proxy_step = 0;
+ else
+ {
+ proxy = dstr[*proxy_step];
+
+ (*proxy_step)++;
+ if (*proxy_step == dnum)
+ *proxy_step = 0;
+ }
+ }
+ }
+ else if (!strcmp(name, "cproxysession"))
+ {
+ out_names[no_out_args] = name;
+ out_values[no_out_args++] = value;
+ torus_url = m_p->torus_content_url;
+ }
+ else if (!strcmp(name, "realm") && session_realm.length() == 0)
+ realm = value;
+ else if (!strcmp(name, "torus_url") && session_realm.length() == 0)
+ torus_url = value;
+ else if (name[0] == 'x' && name[1] == '-')
+ {
+ out_names[no_out_args] = name;
+ out_values[no_out_args++] = value;
+ }
+ else
+ {
+ BackendPtr notfound;
+ char *msg = (char*) odr_malloc(odr, strlen(name) + 30);
+ *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
+ sprintf(msg, "Bad database argument: %s", name);
+ *addinfo = msg;
+ return notfound;
+ }
+ }
+ if (proxy.length())
+ package.log("zoom", YLOG_LOG, "proxy: %s", proxy.c_str());
+
+ if (connection_reuse)
+ {
+ m_backend->connect("", error, addinfo, odr);
+ return m_backend;
+ }
+
+ if (param_user)
+ {
+ authentication = std::string(param_user);
+ if (param_password)
+ authentication += "/" + std::string(param_password);
+ }
+ if (param_content_user)
+ {
+ content_authentication = std::string(param_content_user);
+ if (param_content_password)
+ content_authentication += "/" + std::string(param_content_password);
+ }
+
+ if (torus_db.compare("IR-Explain---1") == 0)
+ return explain_search(package, database, error, addinfo, odr, torus_url,
+ torus_db, realm);
+
+ SearchablePtr sptr;
+
+ std::map<std::string,SearchablePtr>::iterator it;
+ it = m_p->s_map.find(torus_db);
+ if (it != m_p->s_map.end())
+ sptr = it->second;
+ else if (torus_url.length() > 0)
+ {
+ std::string torus_query = "udb==" + torus_db;
+ xmlDoc *doc = mp::get_searchable(package,torus_url, torus_db,
+ torus_query,
+ realm, m_p->proxy);
+ if (!doc)
+ {
+ *error = YAZ_BIB1_UNSPECIFIED_ERROR;
+ *addinfo = odr_strdup(odr, "Torus server unavailable or "
+ "incorrectly configured");
+ BackendPtr b;
+ return b;
+ }
+ const xmlNode *ptr = xmlDocGetRootElement(doc);
+ if (ptr && ptr->type == XML_ELEMENT_NODE)
+ {
+ if (!strcmp((const char *) ptr->name, "record"))
+ {
+ sptr = m_p->parse_torus_record(ptr);
+ }
+ else if (!strcmp((const char *) ptr->name, "records"))
+ {
+ for (ptr = ptr->children; ptr; ptr = ptr->next)
+ {
+ if (ptr->type == XML_ELEMENT_NODE
+ && !strcmp((const char *) ptr->name, "record"))
+ {
+ if (sptr)
+ {
+ *error = YAZ_BIB1_UNSPECIFIED_ERROR;
+ *addinfo = (char*)
+ odr_malloc(odr, 40 + torus_db.length());
+ sprintf(*addinfo, "multiple records for udb=%s",
+ database.c_str());
+ xmlFreeDoc(doc);
+ BackendPtr b;
+ return b;
+ }
+ sptr = m_p->parse_torus_record(ptr);
+ }
+ }
+ }
+ else
+ {
+ *error = YAZ_BIB1_UNSPECIFIED_ERROR;
+ *addinfo = (char*) odr_malloc(
+ odr, 40 + strlen((const char *) ptr->name));
+ sprintf(*addinfo, "bad root element for torus: %s", ptr->name);
+ xmlFreeDoc(doc);
+ BackendPtr b;
+ return b;
+ }
+ }
+ xmlFreeDoc(doc);
+ }
+
+ if (!sptr)
+ {
+ *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
+ *addinfo = odr_strdup(odr, torus_db.c_str());
+ BackendPtr b;
+ return b;
+ }
+
+ xsltStylesheetPtr xsp = 0;
+ if (sptr->transform_xsl_content.length())
+ {
+ xmlDoc *xsp_doc = xmlParseMemory(sptr->transform_xsl_content.c_str(),
+ sptr->transform_xsl_content.length());
+ if (!xsp_doc)
+ {
+ *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
+ *addinfo = (char *) odr_malloc(odr, 40);
+ sprintf(*addinfo, "xmlParseMemory failed");
+ BackendPtr b;
+ return b;
+ }
+ xsp = xsltParseStylesheetDoc(xsp_doc);
+ if (!xsp)
+ {
+ *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
+ *addinfo = odr_strdup(odr, "xsltParseStylesheetDoc failed");
+ BackendPtr b;
+ xmlFreeDoc(xsp_doc);
+ return b;
+ }
+ }
+ else if (sptr->transform_xsl_fname.length())
+ {
+ const char *path = 0;
+
+ if (m_p->xsldir.length())
+ path = m_p->xsldir.c_str();
+ else
+ path = m_p->file_path.c_str();
+ std::string fname;
+
+ char fullpath[1024];
+ char *cp = yaz_filepath_resolve(sptr->transform_xsl_fname.c_str(),
+ path, 0, fullpath);
+ if (cp)
+ fname.assign(cp);
+ else
+ {
+ *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
+ *addinfo = (char *)
+ odr_malloc(odr, 40 + sptr->transform_xsl_fname.length());
+ sprintf(*addinfo, "File could not be read: %s",
+ sptr->transform_xsl_fname.c_str());
+ BackendPtr b;
+ return b;
+ }
+ xmlDoc *xsp_doc = xmlParseFile(fname.c_str());
+ if (!xsp_doc)
+ {
+ *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
+ *addinfo = (char *) odr_malloc(odr, 40 + fname.length());
+ sprintf(*addinfo, "xmlParseFile failed. File: %s", fname.c_str());
+ BackendPtr b;
+ return b;
+ }
+ xsp = xsltParseStylesheetDoc(xsp_doc);
+ if (!xsp)
+ {
+ *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
+ *addinfo = odr_strdup(odr, "xsltParseStylesheetDoc failed");
+ BackendPtr b;
+ xmlFreeDoc(xsp_doc);
+ return b;
+ }
+ }
+
+ cql_transform_t cqlt = 0;
+ if (sptr->rpn2cql_fname.length())
+ {
+ char fullpath[1024];
+ char *cp = yaz_filepath_resolve(sptr->rpn2cql_fname.c_str(),
+ m_p->file_path.c_str(), 0, fullpath);
+ if (cp)
+ cqlt = cql_transform_open_fname(fullpath);
+ }
+ else
+ cqlt = cql_transform_create();
+
+ if (!cqlt)
+ {
+ *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
+ *addinfo = odr_strdup(odr, "Missing/invalid cql2rpn file");
+ BackendPtr b;
+ xsltFreeStylesheet(xsp);
+ return b;
+ }
+
+ m_backend.reset();
+
+ BackendPtr b(new Backend);
+
+ b->cqlt = cqlt;
+ b->sptr = sptr;
+ b->xsp = xsp;
+ b->m_frontend_database = database;
+ b->enable_cproxy = param_nocproxy ? false : true;
+
+ if (sptr->query_encoding.length())
+ b->set_option("rpnCharset", sptr->query_encoding);
+
+ b->set_option("timeout", m_p->zoom_timeout.c_str());
+
+ if (m_p->apdu_log)
+ b->set_option("apdulog", "1");
+
+ if (sptr->piggyback && sptr->sru.length())
+ b->set_option("count", "1"); /* some SRU servers INSIST on getting
+ maximumRecords > 0 */
+ b->set_option("piggyback", sptr->piggyback ? "1" : "0");
+
+ if (authentication.length() == 0)
+ authentication = sptr->authentication;
+
+ if (proxy.length() == 0)
+ proxy = sptr->cfProxy;
+ b->m_proxy = proxy;
+
+ if (sptr->cfAuth.length())
+ {
+ // A CF target
+ b->set_option("user", sptr->cfAuth);
+ if (authentication.length())
+ {
+ size_t found = authentication.find('/');
+ if (found != std::string::npos)
+ {
+ out_names[no_out_args] = "user";
+ out_values[no_out_args++] =
+ odr_strdup(odr, authentication.substr(0, found).c_str());
+
+ out_names[no_out_args] = "password";
+ out_values[no_out_args++] =
+ odr_strdup(odr, authentication.substr(found+1).c_str());
+ }
+ else
+ {
+ out_names[no_out_args] = "user";
+ out_values[no_out_args++] =
+ odr_strdup(odr, authentication.c_str());
+ }
+ }
+ if (proxy.length())
+ {
+ out_names[no_out_args] = "proxy";
+ out_values[no_out_args++] = odr_strdup(odr, proxy.c_str());
+ }
+ if (sptr->cfSubDB.length())
+ {
+ out_names[no_out_args] = "subdatabase";
+ out_values[no_out_args++] = odr_strdup(odr, sptr->cfSubDB.c_str());
+ }
+ if (param_nocproxy)
+ {
+ out_names[no_out_args] = "nocproxy";
+ out_values[no_out_args++] = odr_strdup(odr, param_nocproxy);
+ }
+ }
+ else
+ {
+ size_t found = authentication.find('/');
+
+ if (sptr->sru.length() && found != std::string::npos)
+ {
+ b->set_option("user", authentication.substr(0, found));
+ b->set_option("password", authentication.substr(found+1));
+ }
+ else
+ b->set_option("user", authentication);
+
+ if (proxy.length())
+ b->set_option("proxy", proxy);
+ }
+ std::string url;
+ if (sptr->sru.length())
+ {
+ url = "http://" + sptr->target;
+ b->set_option("sru", sptr->sru);
+
+ if (sptr->sru_version.length())
+ b->set_option("sru_version", sptr->sru_version);
+ }
+ else
+ {
+ url = sptr->target;
+ }
+ if (no_out_args)
+ {
+ char *x_args = 0;
+ out_names[no_out_args] = 0; // terminate list
+
+ yaz_array_to_uri(&x_args, odr, (char **) out_names,
+ (char **) out_values);
+ url += "," + std::string(x_args);
+ }
+ package.log("zoom", YLOG_LOG, "url: %s", url.c_str());
+ b->connect(url, error, addinfo, odr);
+ if (*error == 0 && b->enable_cproxy)
+ create_content_session(package, b, error, addinfo, odr,
+ content_authentication.length() ?
+ content_authentication : authentication,
+ content_proxy.length() ? content_proxy : proxy,
+ realm);
+ if (*error == 0)
+ m_backend = b;
+ return b;
+}
+
+void yf::Zoom::Frontend::prepare_elements(BackendPtr b,
+ Odr_oid *preferredRecordSyntax,
+ const char *element_set_name,
+ bool &enable_pz2_retrieval,
+ bool &enable_pz2_transform,
+ bool &enable_record_transform,
+ bool &assume_marc8_charset)
+{
+ char oid_name_str[OID_STR_MAX];
+ const char *syntax_name = 0;
+
+ if (preferredRecordSyntax &&
+ !oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_xml))
+ {
+ if (element_set_name &&
+ !strcmp(element_set_name, m_p->element_transform.c_str()))
+ {
+ enable_pz2_retrieval = true;
+ enable_pz2_transform = true;
+ }
+ else if (element_set_name &&
+ !strcmp(element_set_name, m_p->element_raw.c_str()))
+ {
+ enable_pz2_retrieval = true;
+ }
+ else if (m_p->record_xsp)
+ {
+ enable_pz2_retrieval = true;
+ enable_pz2_transform = true;
+ enable_record_transform = true;
+ }
+ }
+
+ if (enable_pz2_retrieval)
+ {
+ std::string configured_request_syntax = b->sptr->request_syntax;
+ if (configured_request_syntax.length())
+ {
+ syntax_name = configured_request_syntax.c_str();
+ const Odr_oid *syntax_oid =
+ yaz_string_to_oid(yaz_oid_std(), CLASS_RECSYN, syntax_name);
+ if (!oid_oidcmp(syntax_oid, yaz_oid_recsyn_usmarc)
+ || !oid_oidcmp(syntax_oid, yaz_oid_recsyn_opac))
+ assume_marc8_charset = true;
+ }
+ }
+ else if (preferredRecordSyntax)
+ syntax_name =
+ yaz_oid_to_string_buf(preferredRecordSyntax, 0, oid_name_str);
+
+ if (b->sptr->sru.length())
+ syntax_name = "XML";
+
+ b->set_option("preferredRecordSyntax", syntax_name);
+
+ if (enable_pz2_retrieval)
+ {
+ element_set_name = 0;
+ if (b->sptr->element_set.length())
+ element_set_name = b->sptr->element_set.c_str();
+ }
+
+ b->set_option("elementSetName", element_set_name);
+ if (b->sptr->sru.length() && element_set_name)
+ b->set_option("schema", element_set_name);
+}
+
+Z_Records *yf::Zoom::Frontend::get_explain_records(
+ mp::Package &package,
+ Odr_int start,
+ Odr_int number_to_present,
+ int *error,
+ char **addinfo,
+ Odr_int *number_of_records_returned,
+ ODR odr,
+ BackendPtr b,
+ Odr_oid *preferredRecordSyntax,
+ const char *element_set_name)
+{
+ Odr_int i;
+ Z_Records *records = 0;
+
+ if (!b->explain_doc)
+ {
+ return records;
+ }
+ if (number_to_present > 10000)
+ number_to_present = 10000;
+
+ xmlNode *ptr = xmlDocGetRootElement(b->explain_doc);
+
+ Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
+ odr_malloc(odr, sizeof(*npl));
+ npl->records = (Z_NamePlusRecord **)
+ odr_malloc(odr, number_to_present * sizeof(*npl->records));
+
+ for (i = 0; i < number_to_present; i++)
+ {
+ int num = 0;
+ xmlNode *res = xml_node_search(ptr, &num, start + i + 1);
+ if (!res)
+ break;
+ xmlBufferPtr xml_buf = xmlBufferCreate();
+ xmlNode *tmp_node = xmlCopyNode(res->children, 1);
+ xmlNodeDump(xml_buf, tmp_node->doc, tmp_node, 0, 0);
+
+ Z_NamePlusRecord *npr =
+ (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
+ npr->databaseName = odr_strdup(odr, b->m_frontend_database.c_str());
+ npr->which = Z_NamePlusRecord_databaseRecord;
+ npr->u.databaseRecord =
+ z_ext_record_xml(odr,
+ (const char *) xml_buf->content, xml_buf->use);
+ npl->records[i] = npr;
+ xmlFreeNode(tmp_node);
+ xmlBufferFree(xml_buf);
+ }
+ records = (Z_Records*) odr_malloc(odr, sizeof(*records));
+ records->which = Z_Records_DBOSD;
+ records->u.databaseOrSurDiagnostics = npl;
+
+ npl->num_records = i;
+ *number_of_records_returned = i;
+ return records;
+}
+
+
+Z_Records *yf::Zoom::Frontend::get_records(mp::Package &package,
+ Odr_int start,
+ Odr_int number_to_present,
+ int *error,
+ char **addinfo,
+ Odr_int *number_of_records_returned,
+ ODR odr,
+ BackendPtr b,
+ Odr_oid *preferredRecordSyntax,
+ const char *element_set_name)
+{
+ *number_of_records_returned = 0;
+ Z_Records *records = 0;
+ bool enable_pz2_retrieval = false; // whether target profile is used
+ bool enable_pz2_transform = false; // whether XSLT is used as well
+ bool assume_marc8_charset = false;
+ bool enable_record_transform = false;
+
+ prepare_elements(b, preferredRecordSyntax,
+ element_set_name,
+ enable_pz2_retrieval,
+ enable_pz2_transform,
+ enable_record_transform,
+ assume_marc8_charset);
+
+ package.log("zoom", YLOG_LOG, "pz2_retrieval: %s . pz2_transform: %s",
+ enable_pz2_retrieval ? "yes" : "no",
+ enable_pz2_transform ? "yes" : "no");
+
+ if (start < 0 || number_to_present <=0)
+ return records;
+
+ if (number_to_present > 10000)
+ number_to_present = 10000;
+
+ ZOOM_record *recs = (ZOOM_record *)
+ odr_malloc(odr, (size_t) number_to_present * sizeof(*recs));
+
+ b->present(start, number_to_present, recs, error, addinfo, odr);
+
+ int i = 0;
+ if (!*error)
+ {
+ for (i = 0; i < number_to_present; i++)
+ if (!recs[i])
+ break;
+ }
+ if (i > 0)
+ { // only return records if no error and at least one record
+
+ const char *xsl_parms[3];
+ mp::wrbuf cproxy_host;
+
+ if (b->enable_cproxy && b->cproxy_host.length())
+ {
+ wrbuf_puts(cproxy_host, "\"");
+ wrbuf_puts(cproxy_host, b->cproxy_host.c_str());
+ wrbuf_puts(cproxy_host, "/\"");
+
+ xsl_parms[0] = "cproxyhost";
+ xsl_parms[1] = wrbuf_cstr(cproxy_host);
+ xsl_parms[2] = 0;
+ }
+ else
+ {
+ xsl_parms[0] = 0;
+ }
+
+ char *odr_database = odr_strdup(odr,
+ b->m_frontend_database.c_str());
+ Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
+ odr_malloc(odr, sizeof(*npl));
+ *number_of_records_returned = i;
+ npl->num_records = i;
+ npl->records = (Z_NamePlusRecord **)
+ odr_malloc(odr, i * sizeof(*npl->records));
+ for (i = 0; i < number_to_present; i++)
+ {
+ Z_NamePlusRecord *npr = 0;
+ const char *addinfo;
+
+ int sur_error = ZOOM_record_error(recs[i], 0 /* msg */,
+ &addinfo, 0 /* diagset */);
+
+ if (sur_error)
+ {
+ log_diagnostic(package, sur_error, addinfo);
+ npr = zget_surrogateDiagRec(odr, odr_database, sur_error,
+ addinfo);
+ }
+ else if (enable_pz2_retrieval)
+ {
+ char rec_type_str[100];
+ const char *record_encoding = 0;
+
+ if (b->sptr->record_encoding.length())
+ record_encoding = b->sptr->record_encoding.c_str();
+ else if (assume_marc8_charset)
+ record_encoding = "marc8";
+
+ strcpy(rec_type_str, b->sptr->use_turbomarc ? "txml" : "xml");
+ if (record_encoding)
+ {
+ strcat(rec_type_str, "; charset=");
+ strcat(rec_type_str, record_encoding);
+ }
+
+ package.log("zoom", YLOG_LOG, "Getting record of type %s",
+ rec_type_str);
+ int rec_len;
+ xmlChar *xmlrec_buf = 0;
+ const char *rec_buf = ZOOM_record_get(recs[i], rec_type_str,
+ &rec_len);
+ if (!rec_buf && !npr)
+ {
+ std::string addinfo("ZOOM_record_get failed for type ");
+
+ int error = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+ addinfo += rec_type_str;
+ log_diagnostic(package, error, addinfo.c_str());
+ npr = zget_surrogateDiagRec(odr, odr_database,
+ error, addinfo.c_str());
+ }
+ else
+ {
+ package.log_write(rec_buf, rec_len);
+ package.log_write("\r\n", 2);
+ }
+
+ if (rec_buf && b->xsp && enable_pz2_transform)
+ {
+ xmlDoc *rec_doc = xmlParseMemory(rec_buf, rec_len);
+ if (!rec_doc)
+ {
+ const char *addinfo = "xml parse failed for record";
+ int error = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+ log_diagnostic(package, error, addinfo);
+ npr = zget_surrogateDiagRec(
+ odr, odr_database, error, addinfo);
+ }
+ else
+ {
+ // first stage XSLT - per target
+ xsltStylesheetPtr xsp = b->xsp;
+ xmlDoc *rec_res = xsltApplyStylesheet(xsp, rec_doc,
+ xsl_parms);
+ // insert generated-url
+ if (rec_res)
+ {
+ std::string res =
+ mp::xml::url_recipe_handle(rec_res,
+ b->sptr->urlRecipe);
+ if (res.length())
+ {
+ xmlNode *ptr = xmlDocGetRootElement(rec_res);
+ while (ptr && ptr->type != XML_ELEMENT_NODE)
+ ptr = ptr->next;
+ xmlNode *c =
+ xmlNewChild(ptr, 0, BAD_CAST "metadata", 0);
+ xmlNewProp(c, BAD_CAST "type", BAD_CAST
+ "generated-url");
+ xmlNode * t = xmlNewText(BAD_CAST res.c_str());
+ xmlAddChild(c, t);
+ }
+ }
+ // second stage XSLT - common
+ if (rec_res && m_p->record_xsp &&
+ enable_record_transform)
+ {
+ xmlDoc *tmp_doc = rec_res;
+
+ xsp = m_p->record_xsp;
+ rec_res = xsltApplyStylesheet(xsp, tmp_doc,
+ xsl_parms);
+ xmlFreeDoc(tmp_doc);
+ }
+ // get result out of it
+ if (rec_res)
+ {
+ xsltSaveResultToString(&xmlrec_buf, &rec_len,
+ rec_res, xsp);
+ rec_buf = (const char *) xmlrec_buf;
+ package.log_write(rec_buf, rec_len);
+
+ xmlFreeDoc(rec_res);
+ }
+ if (!rec_buf)
+ {
+ std::string addinfo;
+ int error =
+ YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+
+ addinfo = "xslt apply failed for "
+ + b->sptr->transform_xsl_fname;
+ log_diagnostic(package, error, addinfo.c_str());
+ npr = zget_surrogateDiagRec(
+ odr, odr_database, error, addinfo.c_str());
+ }
+ xmlFreeDoc(rec_doc);
+ }
+ }
+
+ if (!npr)
+ {
+ if (!rec_buf)
+ npr = zget_surrogateDiagRec(
+ odr, odr_database,
+ YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
+ rec_type_str);
+ else
+ {
+ npr = (Z_NamePlusRecord *)
+ odr_malloc(odr, sizeof(*npr));
+ npr->databaseName = odr_database;
+ npr->which = Z_NamePlusRecord_databaseRecord;
+ npr->u.databaseRecord =
+ z_ext_record_xml(odr, rec_buf, rec_len);
+ }
+ }
+ if (xmlrec_buf)
+ xmlFree(xmlrec_buf);
+ }
+ else
+ {
+ Z_External *ext =
+ (Z_External *) ZOOM_record_get(recs[i], "ext", 0);
+ if (ext)
+ {
+ npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
+ npr->databaseName = odr_database;
+ npr->which = Z_NamePlusRecord_databaseRecord;
+ npr->u.databaseRecord = ext;
+ }
+ else
+ {
+ npr = zget_surrogateDiagRec(
+ odr, odr_database,
+ YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
+ "ZOOM_record, type ext");
+ }
+ }
+ npl->records[i] = npr;
+ }
+ records = (Z_Records*) odr_malloc(odr, sizeof(*records));
+ records->which = Z_Records_DBOSD;
+ records->u.databaseOrSurDiagnostics = npl;
+ }
+ return records;
+}
+
+struct cql_node *yf::Zoom::Impl::convert_cql_fields(struct cql_node *cn,
+ ODR odr)
+{
+ struct cql_node *r = 0;
+ if (!cn)
+ return 0;
+ switch (cn->which)
+ {
+ case CQL_NODE_ST:
+ if (cn->u.st.index)
+ {
+ std::map<std::string,std::string>::const_iterator it;
+ it = fieldmap.find(cn->u.st.index);
+ if (it == fieldmap.end())
+ return cn;
+ if (it->second.length())
+ cn->u.st.index = odr_strdup(odr, it->second.c_str());
+ else
+ cn->u.st.index = 0;
+ }
+ break;
+ case CQL_NODE_BOOL:
+ r = convert_cql_fields(cn->u.boolean.left, odr);
+ if (!r)
+ r = convert_cql_fields(cn->u.boolean.right, odr);
+ break;
+ case CQL_NODE_SORT:
+ r = convert_cql_fields(cn->u.sort.search, odr);
+ break;
+ }
+ return r;
+}
+
+void yf::Zoom::Frontend::log_diagnostic(mp::Package &package,
+ int error, const char *addinfo)
+{
+ const char *err_msg = yaz_diag_bib1_str(error);
+ if (addinfo)
+ package.log("zoom", YLOG_WARN, "Diagnostic %d %s: %s",
+ error, err_msg, addinfo);
+ else
+ package.log("zoom", YLOG_WARN, "Diagnostic %d %s:",
+ error, err_msg);
+}
+
+yf::Zoom::BackendPtr yf::Zoom::Frontend::explain_search(mp::Package &package,
+ std::string &database,
+ int *error,
+ char **addinfo,
+ mp::odr &odr,
+ std::string torus_url,
+ std::string &torus_db,
+ std::string &realm)
+{
+ m_backend.reset();
+
+ BackendPtr b(new Backend);
+
+ b->m_frontend_database = database;
+ b->enable_explain = true;
+
+ Z_GDU *gdu = package.request().get();
+ Z_APDU *apdu_req = gdu->u.z3950;
+ Z_SearchRequest *sr = apdu_req->u.searchRequest;
+ Z_Query *query = sr->query;
+
+ if (!m_p->explain_xsp)
+ {
+ *error = YAZ_BIB1_UNSPECIFIED_ERROR;
+ *addinfo =
+ odr_strdup(odr, "IR-Explain---1 unsupported. "
+ "Torus explain_xsl not defined");
+ return m_backend;
+ }
+ else if (query->which == Z_Query_type_104 &&
+ query->u.type_104->which == Z_External_CQL)
+ {
+ std::string torus_query(query->u.type_104->u.cql);
+ xmlDoc *doc = mp::get_searchable(package, torus_url, "",
+ torus_query,
+ realm, m_p->proxy);
+ if (m_p->explain_xsp)
+ {
+ xmlDoc *rec_res = xsltApplyStylesheet(m_p->explain_xsp, doc, 0);
+
+ xmlFreeDoc(doc);
+ doc = rec_res;
+ }
+ if (!doc)
+ {
+ *error = YAZ_BIB1_UNSPECIFIED_ERROR;
+ *addinfo = odr_strdup(odr, "Torus server unavailable or "
+ "incorrectly configured");
+ }
+ else
+ {
+ xmlNode *ptr = xmlDocGetRootElement(doc);
+ int hits = 0;
+
+ xml_node_search(ptr, &hits, 0);
+
+ Z_APDU *apdu_res = odr.create_searchResponse(apdu_req, 0, 0);
+ apdu_res->u.searchResponse->resultCount = odr_intdup(odr, hits);
+ package.response() = apdu_res;
+ m_backend = b;
+ }
+ if (b->explain_doc)
+ xmlFreeDoc(b->explain_doc);
+ b->explain_doc = doc;
+ return m_backend;
+ }
+ else
+ {
+ *error = YAZ_BIB1_QUERY_TYPE_UNSUPP;
+ *addinfo = odr_strdup(odr, "IR-Explain---1 only supports CQL");
+ return m_backend;
+ }
+}
+
+static bool wait_conn(COMSTACK cs, int secs)
+{
+ struct yaz_poll_fd pfd;
+
+ yaz_poll_add(pfd.input_mask, yaz_poll_except);
+ if (cs->io_pending && CS_WANT_WRITE)
+ yaz_poll_add(pfd.input_mask, yaz_poll_write);
+ if (cs->io_pending & CS_WANT_READ)
+ yaz_poll_add(pfd.input_mask, yaz_poll_read);
+
+ pfd.fd = cs_fileno(cs);
+ pfd.client_data = 0;
+
+ int ret = yaz_poll(&pfd, 1, secs, 0);
+ return ret > 0;
+}
+
+bool yf::Zoom::Impl::check_proxy(const char *proxy)
+{
+ COMSTACK conn = 0;
+ const char *uri = "http://localhost/";
+ void *add;
+ mp::odr odr;
+ bool outcome = false;
+ conn = cs_create_host_proxy(uri, 0, &add, proxy);
+
+ if (!conn)
+ return false;
+
+ Z_GDU *gdu = z_get_HTTP_Request_uri(odr, uri, 0, 1);
+ gdu->u.HTTP_Request->method = odr_strdup(odr, "GET");
+
+ if (z_GDU(odr, &gdu, 0, 0))
+ {
+ int len;
+ char *buf = odr_getbuf(odr, &len, 0);
+
+ int ret = cs_connect(conn, add);
+ if (ret > 0 || (ret == 0 && wait_conn(conn, 1)))
+ {
+ while (1)
+ {
+ ret = cs_put(conn, buf, len);
+ if (ret != 1)
+ break;
+ if (!wait_conn(conn, proxy_timeout))
+ break;
+ }
+ if (ret == 0)
+ outcome = true;
+ }
+ }
+ cs_close(conn);
+ return outcome;
+}
+
+bool yf::Zoom::Frontend::retry(mp::Package &package,
+ mp::odr &odr,
+ BackendPtr b,
+ int &error, char **addinfo,
+ int &proxy_step, int &same_retries,
+ int &proxy_retries)
+{
+ if (b && b->m_proxy.length() && !m_p->check_proxy(b->m_proxy.c_str()))
+ {
+ log_diagnostic(package, error, *addinfo);
+ package.log("zoom", YLOG_LOG, "proxy %s fails", b->m_proxy.c_str());
+ m_backend.reset();
+ if (proxy_step) // there is a failover
+ {
+ proxy_retries++;
+ package.log("zoom", YLOG_WARN, "search failed: trying next proxy");
+ return true;
+ }
+ error = YAZ_BIB1_PROXY_FAILURE;
+ *addinfo = odr_strdup(odr, b->m_proxy.c_str());
+ }
+ else if (same_retries == 0 && proxy_retries == 0)
+ {
+ log_diagnostic(package, error, *addinfo);
+ same_retries++;
+ package.log("zoom", YLOG_WARN, "search failed: retry");
+ m_backend.reset();
+ proxy_step = 0;
+ return true;
+ }
+ return false;
+}
+
+void yf::Zoom::Frontend::handle_search(mp::Package &package)
+{
+ Z_GDU *gdu = package.request().get();
+ Z_APDU *apdu_req = gdu->u.z3950;