X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Ffilter_zoom.cpp;h=2062b1c0ce5621b57314403680fc9fbfabef1646;hb=414a181466c1f86c049758a2f99e195af6287951;hp=018f532a12094d59c2eb3053a4fcc45827b5b082;hpb=fa3bd4d2a13db989a57262b42f105a4d4ba28025;p=metaproxy-moved-to-github.git diff --git a/src/filter_zoom.cpp b/src/filter_zoom.cpp index 018f532..2062b1c 100644 --- a/src/filter_zoom.cpp +++ b/src/filter_zoom.cpp @@ -1,5 +1,5 @@ /* This file is part of Metaproxy. - Copyright (C) 2005-2013 Index Data + Copyright (C) Index Data Metaproxy is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -51,6 +51,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include #include +#include namespace mp = metaproxy_1; namespace yf = mp::filter; @@ -60,6 +61,7 @@ namespace metaproxy_1 { class Zoom::Searchable : boost::noncopyable { public: std::string authentication; + std::string authenticationMode; std::string cfAuth; std::string cfProxy; std::string cfSubDB; @@ -107,10 +109,11 @@ namespace metaproxy_1 { void connect(std::string zurl, int *error, char **addinfo, ODR odr); void search(ZOOM_query q, Odr_int *hits, - int *error, char **addinfo, ODR odr); + int *error, char **addinfo, Z_FacetList **fl, ODR odr); void present(Odr_int start, Odr_int number, ZOOM_record *recs, int *error, char **addinfo, ODR odr); void set_option(const char *name, const char *value); + void set_option(const char *name, const char *value, size_t l); void set_option(const char *name, std::string value); const char *get_option(const char *name); void get_zoom_error(int *error, char **addinfo, ODR odr); @@ -125,7 +128,6 @@ namespace metaproxy_1 { BackendPtr m_backend; void handle_package(mp::Package &package); void handle_search(mp::Package &package); - void auth(mp::Package &package, Z_InitRequest *req, int *error, char **addinfo, ODR odr); @@ -370,15 +372,84 @@ void yf::Zoom::Backend::connect(std::string zurl, } void yf::Zoom::Backend::search(ZOOM_query q, Odr_int *hits, - int *error, char **addinfo, ODR odr) + int *error, char **addinfo, Z_FacetList **flp, + ODR odr) { ZOOM_resultset_destroy(m_resultset); + m_resultset = 0; + if (*flp) + { + WRBUF w = wrbuf_alloc(); + yaz_facet_list_to_wrbuf(w, *flp); + set_option("facets", wrbuf_cstr(w)); + wrbuf_destroy(w); + } m_resultset = ZOOM_connection_search(m_connection, q); get_zoom_error(error, addinfo, odr); if (*error == 0) *hits = ZOOM_resultset_size(m_resultset); else *hits = 0; + *flp = 0; + size_t num_facets = ZOOM_resultset_facets_size(m_resultset); + if (num_facets > 0) + { + size_t i; + Z_FacetList *fl = (Z_FacetList *) odr_malloc(odr, sizeof(*fl)); + fl->elements = (Z_FacetField **) + odr_malloc(odr, num_facets * sizeof(*fl->elements)); + for (i = 0; i < num_facets; i++) + { + ZOOM_facet_field ff = + ZOOM_resultset_get_facet_field_by_index(m_resultset, i); + if (!ff) + break; + Z_AttributeList *al = (Z_AttributeList *) + odr_malloc(odr, sizeof(*al)); + al->num_attributes = 1; + al->attributes = (Z_AttributeElement **) + odr_malloc(odr, sizeof(*al->attributes)); + Z_AttributeElement *ae = al->attributes[0] = (Z_AttributeElement *) + odr_malloc(odr, sizeof(**al->attributes)); + ae->attributeSet = 0; + ae->attributeType = odr_intdup(odr, 1); + ae->which = Z_AttributeValue_complex; + ae->value.complex = (Z_ComplexAttribute *) + odr_malloc(odr, sizeof(*ae->value.complex)); + ae->value.complex->num_list = 1; + ae->value.complex->list = (Z_StringOrNumeric **) + odr_malloc(odr, sizeof(**ae->value.complex->list)); + ae->value.complex->list[0] = (Z_StringOrNumeric *) + odr_malloc(odr, sizeof(**ae->value.complex->list)); + ae->value.complex->list[0]->which = Z_StringOrNumeric_string; + ae->value.complex->list[0]->u.string = + odr_strdup(odr, ZOOM_facet_field_name(ff)); + ae->value.complex->num_semanticAction = 0; + ae->value.complex->semanticAction = 0; + + int num_terms = ZOOM_facet_field_term_count(ff); + fl->elements[i] = (Z_FacetField *) + odr_malloc(odr, sizeof(Z_FacetField)); + fl->elements[i]->attributes = al; + fl->elements[i]->num_terms = num_terms; + fl->elements[i]->terms = (Z_FacetTerm **) + odr_malloc(odr, num_terms * sizeof(Z_FacetTerm *)); + int j; + for (j = 0; j < num_terms; j++) + { + int freq; + const char *a_term = ZOOM_facet_field_get_term(ff, j, &freq); + Z_FacetTerm *ft = (Z_FacetTerm *) odr_malloc(odr, sizeof(*ft)); + ft->term = z_Term_create(odr, Z_Term_general, a_term, + strlen(a_term)); + ft->count = odr_intdup(odr, freq); + + fl->elements[i]->terms[j] = ft; + } + } + fl->num = i; + *flp = fl; + } } void yf::Zoom::Backend::present(Odr_int start, Odr_int number, @@ -389,6 +460,12 @@ void yf::Zoom::Backend::present(Odr_int start, Odr_int number, get_zoom_error(error, addinfo, odr); } + +void yf::Zoom::Backend::set_option(const char *name, const char *value, size_t l) +{ + ZOOM_connection_option_setl(m_connection, name, value, l); +} + void yf::Zoom::Backend::set_option(const char *name, const char *value) { ZOOM_connection_option_set(m_connection, name, value); @@ -508,6 +585,11 @@ yf::Zoom::SearchablePtr yf::Zoom::Impl::parse_torus_record(const xmlNode *ptr) s->authentication = mp::xml::get_text(ptr); } else if (!strcmp((const char *) ptr->name, + "authenticationMode")) + { + s->authenticationMode = mp::xml::get_text(ptr); + } + else if (!strcmp((const char *) ptr->name, "cfAuth")) { s->cfAuth = mp::xml::get_text(ptr); @@ -1345,8 +1427,7 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( if (sptr->query_encoding.length()) b->set_option("rpnCharset", sptr->query_encoding); - if (sptr->extraArgs.length()) - b->set_option("extraArgs", sptr->extraArgs); + std::string extraArgs = sptr->extraArgs; b->set_option("timeout", m_p->zoom_timeout.c_str()); @@ -1399,6 +1480,9 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( out_names[no_out_args] = "subdatabase"; out_values[no_out_args++] = odr_strdup(odr, sptr->cfSubDB.c_str()); } + if (!param_nocproxy && b->sptr->contentConnector.length()) + param_nocproxy = "1"; + if (param_nocproxy) { out_names[no_out_args] = "nocproxy"; @@ -1407,62 +1491,37 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( } else { - if (sptr->sru.length() == 0) - b->set_option("user", authentication); /* Z39.50 */ + const char *auth = authentication.c_str(); + const char *cp1 = strchr(auth, ' '); + if (!cp1 && sptr->sru.length()) + cp1 = strchr(auth, '/'); + if (!cp1) + { + /* Z39.50 user/password style, or no password for SRU */ + b->set_option("user", auth); + } else { - std::string user; - std::string password; - std::string authtype; - { - const char *cstr = authentication.c_str(); - const char *cp1 = strchr(cstr, '/'); - const char *cp2 = 0; - if (cp1) - { - cp2 = strchr(cp1 + 1, '/'); - if (cp2) - { - password.assign(cp1 + 1, cp2 - cp1 - 1); - authtype.assign(cp2 + 1); - } - else - password.assign(cp1 + 1); - user.assign(cstr, cp1 - cstr); - } - else - user.assign(cstr); - } + /* now consider group as well */ + const char *cp2 = strchr(cp1 + 1, ' '); - if (authtype.compare("url") == 0) - { - /* SRU URL encoding of auth stuff */ - ODR o = odr_createmem(ODR_ENCODE); - char *path = 0; - const char *names[3]; - const char *values[3]; - - names[0] = "x-username"; - values[0] = user.c_str(); - names[1] = "x-password"; - values[1] = password.c_str(); - names[2] = 0; - values[2] = 0; - - yaz_array_to_uri(&path, o, (char **) names, (char **) values); - b->set_option("extraArgs", path); - odr_destroy(o); - } + b->set_option("user", auth, cp1 - auth); + if (!cp2) + b->set_option("password", cp1 + 1); else { - b->set_option("user", user); - if (password.length()) - b->set_option("password", password); + b->set_option("group", cp1 + 1, cp2 - cp1 - 1); + b->set_option("password", cp2 + 1); } } + if (sptr->authenticationMode.length()) + b->set_option("authenticationMode", sptr->authenticationMode); if (proxy.length()) b->set_option("proxy", proxy); } + if (extraArgs.length()) + b->set_option("extraArgs", extraArgs); + std::string url(sptr->target); if (sptr->sru.length()) { @@ -2308,6 +2367,14 @@ next_proxy: ZOOM_query q = ZOOM_query_create(); ZOOM_query_sortby2(q, b->sptr->sortStrategy.c_str(), sortkeys.c_str()); + Z_FacetList *fl = 0; + + // Facets for request.. And later for reponse + if (!fl) + fl = yaz_oi_get_facetlist(&sr->otherInfo); + if (!fl) + fl = yaz_oi_get_facetlist(&sr->additionalSearchInfo); + if (b->get_option("sru")) { int status = 0; @@ -2331,7 +2398,7 @@ next_proxy: { ZOOM_query_cql(q, wrbuf_cstr(wrb)); package.log("zoom", YLOG_LOG, "CQL: %s", wrbuf_cstr(wrb)); - b->search(q, &hits, &error, &addinfo, odr); + b->search(q, &hits, &error, &addinfo, &fl, odr); } ZOOM_query_destroy(q); @@ -2349,7 +2416,7 @@ next_proxy: { ZOOM_query_prefix(q, wrbuf_cstr(pqf_wrbuf)); package.log("zoom", YLOG_LOG, "search PQF: %s", wrbuf_cstr(pqf_wrbuf)); - b->search(q, &hits, &error, &addinfo, odr); + b->search(q, &hits, &error, &addinfo, &fl, odr); ZOOM_query_destroy(q); } @@ -2381,6 +2448,9 @@ next_proxy: odr_intdup(odr, number_of_records_returned); } apdu_res->u.searchResponse->resultCount = odr_intdup(odr, hits); + if (fl) + yaz_oi_set_facetlist(&apdu_res->u.searchResponse->additionalSearchInfo, + odr, fl); package.response() = apdu_res; } @@ -2545,8 +2615,13 @@ void yf::Zoom::Frontend::auth(mp::Package &package, Z_InitRequest *req, } } - std::string ip = package.origin().get_address(); - yaz_log(YLOG_LOG, "IP=%s", ip.c_str()); + Z_OtherInformation **oi = &req->otherInfo; + const char *ip = + yaz_oi_get_string_oid(oi, yaz_oid_userinfo_client_ip, 1, 0); + if (!ip) + ip = package.origin().get_address().c_str(); + + yaz_log(YLOG_LOG, "IP=%s", ip); std::string torus_query; int failure_code; @@ -2559,13 +2634,8 @@ void yf::Zoom::Frontend::auth(mp::Package &package, Z_InitRequest *req, } else { - const char *ip_cstr = ip.c_str(); - const char *cp = strchr(ip_cstr, ':'); - if (cp) - ip_cstr = cp + 1; - torus_query = "ip encloses/net.ipaddress \""; - torus_query += escape_cql_term(std::string(ip_cstr)); + torus_query += escape_cql_term(std::string(ip)); torus_query += "\""; failure_code = YAZ_BIB1_INIT_AC_BLOCKED_NETWORK_ADDRESS; }