X-Git-Url: http://sru.miketaylor.org.uk/?a=blobdiff_plain;f=src%2Futil.cpp;h=1ea62d3bf05106ba4581f3ab883d3f5fa311746c;hb=HEAD;hp=245bf479977911f92778b7b60b01d5b2ae88c7a7;hpb=73f37c91c144b070020df2f27472c09b62367acf;p=metaproxy-moved-to-github.git diff --git a/src/util.cpp b/src/util.cpp index 245bf47..1ea62d3 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,5 +1,5 @@ /* This file is part of Metaproxy. - Copyright (C) 2005-2012 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 @@ -26,6 +26,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include #include +#include + +#include #include @@ -163,6 +166,26 @@ std::string mp_util::database_name_normalize(const std::string &s) } +Z_RecordComposition *mp_util::piggyback_to_RecordComposition( + ODR odr, Odr_int result_set_size, Z_SearchRequest *sreq) +{ + Z_RecordComposition *comp = 0; + Odr_int present_dummy; + const char *element_set_name = 0; + mp::util::piggyback_sr(sreq, result_set_size, + present_dummy, &element_set_name); + if (element_set_name) + { + comp = (Z_RecordComposition *) odr_malloc(odr, sizeof(*comp)); + comp->which = Z_RecordComp_simple; + comp->u.simple = (Z_ElementSetNames *) + odr_malloc(odr, sizeof(Z_ElementSetNames)); + comp->u.simple->which = Z_ElementSetNames_generic; + comp->u.simple->u.generic = odr_strdup(odr, element_set_name); + } + return comp; +} + void mp_util::piggyback_sr(Z_SearchRequest *sreq, Odr_int result_set_size, Odr_int &number_to_present, @@ -316,33 +339,11 @@ void mp_util::get_default_diag(Z_DefaultDiagFormat *r, void mp_util::get_init_diagnostics( Z_InitResponse *initrs, int &error_code, std::string &addinfo) { - Z_External *uif = initrs->userInformationField; - if (uif && uif->which == Z_External_userInfo1) - { - Z_OtherInformation *ui = uif->u.userInfo1; - int i; - for (i = 0; i < ui->num_elements; i++) - { - Z_OtherInformationUnit *unit = ui->list[i]; - if (unit->which == Z_OtherInfo_externallyDefinedInfo && - unit->information.externallyDefinedInfo && - unit->information.externallyDefinedInfo->which == - Z_External_diag1) - { - Z_DiagnosticFormat *diag = - unit->information.externallyDefinedInfo->u.diag1; - - if (diag->num > 0) - { - Z_DiagnosticFormat_s *ds = diag->elements[0]; - if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec) - mp::util::get_default_diag(ds->u.defaultDiagRec, - error_code, addinfo); - } - } - } - } + Z_DefaultDiagFormat *df = yaz_decode_init_diag(0, initrs); + + if (df) + get_default_diag(df, error_code, addinfo); } int mp_util::get_or_remove_vhost_otherinfo( @@ -502,15 +503,9 @@ void mp_util::transfer_referenceId(ODR odr, const Z_APDU *src, Z_APDU *dst) if (src) { Z_ReferenceId **id_from = mp::util::get_referenceId(src); - if (id_from && *id_from && id_to) - { - *id_to = (Z_ReferenceId*) odr_malloc (odr, sizeof(**id_to)); - (*id_to)->size = (*id_to)->len = (*id_from)->len; - (*id_to)->buf = (unsigned char*) odr_malloc(odr, (*id_to)->len); - memcpy((*id_to)->buf, (*id_from)->buf, (*id_to)->len); - } - else if (id_to) - *id_to = 0; + if (id_from && *id_from) + *id_to = odr_create_Odr_oct(odr, (*id_from)->buf, + (*id_from)->len); } } @@ -595,8 +590,9 @@ Z_APDU *mp::odr::create_scanResponse(const Z_APDU *in_apdu, return apdu; } -Z_GDU *mp::odr::create_HTTP_Response(mp::Session &session, - Z_HTTP_Request *hreq, int code) +Z_GDU *mp::odr::create_HTTP_Response_details(mp::Session &session, + Z_HTTP_Request *hreq, int code, + const char *details) { const char *response_version = "1.0"; bool keepalive = false; @@ -619,15 +615,23 @@ Z_GDU *mp::odr::create_HTTP_Response(mp::Session &session, response_version = "1.1"; } - Z_GDU *gdu = z_get_HTTP_Response(m_odr, code); + Z_GDU *gdu = z_get_HTTP_Response_server( + m_odr, code, details, "Metaproxy/" VERSION, + "http://www.indexdata.com/metaproxy"); Z_HTTP_Response *hres = gdu->u.HTTP_Response; hres->version = odr_strdup(m_odr, response_version); if (keepalive) z_HTTP_header_add(m_odr, &hres->headers, "Connection", "Keep-Alive"); - return gdu; } +Z_GDU *mp::odr::create_HTTP_Response(mp::Session &session, + Z_HTTP_Request *hreq, int code) +{ + return create_HTTP_Response_details(session, hreq, code, 0); + +} + Z_ReferenceId **mp_util::get_referenceId(const Z_APDU *apdu) { switch (apdu->which) @@ -726,6 +730,36 @@ const char *mp::wrbuf::buf() return wrbuf_buf(m_wrbuf); } +const char *mp::wrbuf::c_str() +{ + return wrbuf_cstr(m_wrbuf); +} + +const char *mp::wrbuf::c_str_null() +{ + return wrbuf_cstr_null(m_wrbuf); +} + +bool mp::util::match_ip(const std::string &pattern, const std::string &value) +{ + std::vector globitems; + // split may produce empty strings as results - in particular + // the empty pattern produces one empty string (vector size 1) + boost::split(globitems, pattern, boost::is_any_of(" ")); + bool ret_value = true; // for now (if only empty values) + std::vector::const_iterator it = globitems.begin(); + for (; it != globitems.end(); it++) + { + const char *c_str = (*it).c_str(); + if (*c_str) + { + ret_value = false; // at least one non-empty value + if (yaz_match_glob(c_str, value.c_str())) + return true; + } + } + return ret_value; +} /* * Local variables: