X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Futil.cpp;h=64fd0b8f5bc860d513e891b1f80548d5db2c654d;hb=16346103d5c9e44c7e62f2989af9486e217042a5;hp=dcd751ba4c1e5f04b3555ed17dedef71d67993e4;hpb=1e61b0aa05e2351e33d909f7503eaf936a2d9bb0;p=metaproxy-moved-to-github.git diff --git a/src/util.cpp b/src/util.cpp index dcd751b..64fd0b8 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,24 +1,118 @@ -/* $Id: util.cpp,v 1.17 2006-06-10 14:29:13 adam Exp $ - Copyright (c) 2005-2006, Index Data. +/* $Id: util.cpp,v 1.26 2007-03-20 07:57:54 adam Exp $ + Copyright (c) 2005-2007, Index Data. See the LICENSE file for details */ #include "config.hpp" +#include "util.hpp" #include #include #include #include // for yaz_query_to_wrbuf() -#include "util.hpp" -//#include +#include namespace mp = metaproxy_1; // Doxygen doesn't like mp::util, so we use this instead namespace mp_util = metaproxy_1::util; +const char * +mp_util::record_composition_to_esn(Z_RecordComposition *comp) +{ + if (comp && comp->which == Z_RecordComp_complex) + { + if (comp->u.complex->generic + && comp->u.complex->generic->elementSpec + && (comp->u.complex->generic->elementSpec->which == + Z_ElementSpec_elementSetName)) + return comp->u.complex->generic->elementSpec->u.elementSetName; + } + else if (comp && comp->which == Z_RecordComp_simple && + comp->u.simple->which == Z_ElementSetNames_generic) + return comp->u.simple->u.generic; + return 0; +} + + + +std::string mp_util::http_header_value(const Z_HTTP_Header* header, + const std::string name) +{ + while (header && header->name + && std::string(header->name) != name) + header = header->next; + + if (header && header->name && std::string(header->name) == name + && header->value) + return std::string(header->value); + + return std::string(); +} + +std::string mp_util::http_headers_debug(const Z_HTTP_Request &http_req) +{ + std::string message("\n\n

" + "Metaproxy SRUtoZ3950 filter" + "

\n"); + + message += "

HTTP Info


\n"; + message += "

\n"; + message += "Method: " + std::string(http_req.method) + "
\n"; + message += "Version: " + std::string(http_req.version) + "
\n"; + message += "Path: " + std::string(http_req.path) + "
\n"; + + message += "Content-Type:" + + mp_util::http_header_value(http_req.headers, "Content-Type") + + "
\n"; + message += "Content-Length:" + + mp_util::http_header_value(http_req.headers, "Content-Length") + + "
\n"; + message += "

\n"; + + message += "

Headers


\n"; + message += "

\n"; + Z_HTTP_Header* header = http_req.headers; + while (header){ + message += "Header: " + + std::string(header->name) + ": " + + std::string(header->value) + "
\n"; + header = header->next; + } + message += "

\n"; + message += "\n\n"; + return message; +} + + +void mp_util::http_response(metaproxy_1::Package &package, + const std::string &content, + int http_code) +{ + + Z_GDU *zgdu_req = package.request().get(); + Z_GDU *zgdu_res = 0; + mp::odr odr; + zgdu_res + = odr.create_HTTP_Response(package.session(), + zgdu_req->u.HTTP_Request, + http_code); + + zgdu_res->u.HTTP_Response->content_len = content.size(); + zgdu_res->u.HTTP_Response->content_buf + = (char*) odr_malloc(odr, zgdu_res->u.HTTP_Response->content_len); + + strncpy(zgdu_res->u.HTTP_Response->content_buf, + content.c_str(), zgdu_res->u.HTTP_Response->content_len); + + //z_HTTP_header_add(odr, &hres->headers, + // "Content-Type", content_type.c_str()); + package.response() = zgdu_res; +} + + int mp_util::memcmp2(const void *buf1, int len1, const void *buf2, int len2) { @@ -121,7 +215,7 @@ std::string mp_util::zQueryToString(Z_Query *query) query_str = std::string(wrbuf_buf(w), wrbuf_len(w)); // destroy wrbuf - wrbuf_free(w, 1); + wrbuf_destroy(w); } } @@ -191,7 +285,7 @@ void mp_util::get_init_diagnostics( } } -int mp_util::get_vhost_otherinfo( +int mp_util::get_or_remove_vhost_otherinfo( Z_OtherInformation **otherInformation, bool remove_flag, std::list &vhosts) @@ -213,6 +307,20 @@ int mp_util::get_vhost_otherinfo( return cat; } +void mp_util::get_vhost_otherinfo( + Z_OtherInformation *otherInformation, + std::list &vhosts) +{ + get_or_remove_vhost_otherinfo(&otherInformation, false, vhosts); +} + +int mp_util::remove_vhost_otherinfo( + Z_OtherInformation **otherInformation, + std::list &vhosts) +{ + return get_or_remove_vhost_otherinfo(otherInformation, true, vhosts); +} + void mp_util::set_vhost_otherinfo( Z_OtherInformation **otherInformation, ODR odr, const std::list &vhosts) @@ -226,6 +334,14 @@ void mp_util::set_vhost_otherinfo( } } +void mp_util::set_vhost_otherinfo( + Z_OtherInformation **otherInformation, ODR odr, + const std::string vhost, const int cat) +{ + yaz_oi_set_string_oidval(otherInformation, odr, + VAL_PROXY, cat, vhost.c_str()); +} + void mp_util::split_zurl(std::string zurl, std::string &host, std::list &db) { @@ -296,8 +412,8 @@ mp::odr::operator ODR() const return m_odr; } -Z_APDU *mp::odr::create_close(Z_APDU *in_apdu, - int reason, const char *addinfo) +Z_APDU *mp::odr::create_close(const Z_APDU *in_apdu, + int reason, const char *addinfo) { Z_APDU *apdu = create_APDU(Z_APDU_close, in_apdu); @@ -307,20 +423,25 @@ Z_APDU *mp::odr::create_close(Z_APDU *in_apdu, return apdu; } -Z_APDU *mp::odr::create_APDU(int type, Z_APDU *in_apdu) +Z_APDU *mp::odr::create_APDU(int type, const Z_APDU *in_apdu) { return mp::util::create_APDU(m_odr, type, in_apdu); } -Z_APDU *mp_util::create_APDU(ODR odr, int type, Z_APDU *in_apdu) +Z_APDU *mp_util::create_APDU(ODR odr, int type, const Z_APDU *in_apdu) { Z_APDU *out_apdu = zget_APDU(odr, type); + transfer_referenceId(odr, in_apdu, out_apdu); + return out_apdu; +} - Z_ReferenceId **id_to = mp::util::get_referenceId(out_apdu); +void mp_util::transfer_referenceId(ODR odr, const Z_APDU *src, Z_APDU *dst) +{ + Z_ReferenceId **id_to = mp::util::get_referenceId(dst); *id_to = 0; - if (in_apdu) + if (src) { - Z_ReferenceId **id_from = mp::util::get_referenceId(in_apdu); + 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)); @@ -331,11 +452,10 @@ Z_APDU *mp_util::create_APDU(ODR odr, int type, Z_APDU *in_apdu) else if (id_to) *id_to = 0; } - return out_apdu; } -Z_APDU *mp::odr::create_initResponse(Z_APDU *in_apdu, - int error, const char *addinfo) +Z_APDU *mp::odr::create_initResponse(const Z_APDU *in_apdu, + int error, const char *addinfo) { Z_APDU *apdu = create_APDU(Z_APDU_initResponse, in_apdu); if (error) @@ -347,8 +467,8 @@ Z_APDU *mp::odr::create_initResponse(Z_APDU *in_apdu, return apdu; } -Z_APDU *mp::odr::create_searchResponse(Z_APDU *in_apdu, - int error, const char *addinfo) +Z_APDU *mp::odr::create_searchResponse(const Z_APDU *in_apdu, + int error, const char *addinfo) { Z_APDU *apdu = create_APDU(Z_APDU_searchResponse, in_apdu); if (error) @@ -364,8 +484,8 @@ Z_APDU *mp::odr::create_searchResponse(Z_APDU *in_apdu, return apdu; } -Z_APDU *mp::odr::create_presentResponse(Z_APDU *in_apdu, - int error, const char *addinfo) +Z_APDU *mp::odr::create_presentResponse(const Z_APDU *in_apdu, + int error, const char *addinfo) { Z_APDU *apdu = create_APDU(Z_APDU_presentResponse, in_apdu); if (error) @@ -381,8 +501,8 @@ Z_APDU *mp::odr::create_presentResponse(Z_APDU *in_apdu, return apdu; } -Z_APDU *mp::odr::create_scanResponse(Z_APDU *in_apdu, - int error, const char *addinfo) +Z_APDU *mp::odr::create_scanResponse(const Z_APDU *in_apdu, + int error, const char *addinfo) { Z_APDU *apdu = create_APDU(Z_APDU_scanResponse, in_apdu); Z_ScanResponse *res = apdu->u.scanResponse; @@ -409,7 +529,7 @@ Z_APDU *mp::odr::create_scanResponse(Z_APDU *in_apdu, } Z_GDU *mp::odr::create_HTTP_Response(mp::Session &session, - Z_HTTP_Request *hreq, int code) + Z_HTTP_Request *hreq, int code) { const char *response_version = "1.0"; bool keepalive = false; @@ -441,7 +561,7 @@ Z_GDU *mp::odr::create_HTTP_Response(mp::Session &session, return gdu; } -Z_ReferenceId **mp_util::get_referenceId(Z_APDU *apdu) +Z_ReferenceId **mp_util::get_referenceId(const Z_APDU *apdu) { switch (apdu->which) {