+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("<html>\n<body>\n<h1>"
+ "Metaproxy SRUtoZ3950 filter"
+ "</h1>\n");
+
+ message += "<h3>HTTP Info</h3><br/>\n";
+ message += "<p>\n";
+ message += "<b>Method: </b> " + std::string(http_req.method) + "<br/>\n";
+ message += "<b>Version:</b> " + std::string(http_req.version) + "<br/>\n";
+ message += "<b>Path: </b> " + std::string(http_req.path) + "<br/>\n";
+
+ message += "<b>Content-Type:</b>"
+ + mp_util::http_header_value(http_req.headers, "Content-Type")
+ + "<br/>\n";
+ message += "<b>Content-Length:</b>"
+ + mp_util::http_header_value(http_req.headers, "Content-Length")
+ + "<br/>\n";
+ message += "</p>\n";
+
+ message += "<h3>Headers</h3><br/>\n";
+ message += "<p>\n";
+ Z_HTTP_Header* header = http_req.headers;
+ while (header){
+ message += "<b>Header: </b> <i>"
+ + std::string(header->name) + ":</i> "
+ + std::string(header->value) + "<br/>\n";
+ header = header->next;
+ }
+ message += "</p>\n";
+ message += "</body>\n</html>\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)
+{
+ int d = len1 - len2;
+
+ // compare buffer (common length)
+ int c = memcmp(buf1, buf2, d > 0 ? len2 : len1);
+ if (c > 0)
+ return 1;
+ else if (c < 0)
+ return -1;
+
+ // compare (remaining bytes)
+ if (d > 0)
+ return 1;
+ else if (d < 0)
+ return -1;
+ return 0;
+}
+
+
+std::string mp_util::database_name_normalize(const std::string &s)
+{
+ std::string r = s;
+ size_t i;
+ for (i = 0; i < r.length(); i++)
+ {
+ int ch = r[i];
+ if (ch >= 'A' && ch <= 'Z')
+ r[i] = ch + 'a' - 'A';
+ }
+ return r;
+
+}
+
+void mp_util::piggyback_sr(Z_SearchRequest *sreq,
+ Odr_int result_set_size,
+ Odr_int &number_to_present,
+ const char **element_set_name)
+{
+ Z_ElementSetNames *esn;
+ const char *smallSetElementSetNames = 0;
+ const char *mediumSetElementSetNames = 0;
+
+ esn = sreq->smallSetElementSetNames;
+ if (esn && esn->which == Z_ElementSetNames_generic)
+ smallSetElementSetNames = esn->u.generic;
+
+ esn = sreq->mediumSetElementSetNames;
+ if (esn && esn->which == Z_ElementSetNames_generic)
+ mediumSetElementSetNames = esn->u.generic;
+
+ piggyback(*sreq->smallSetUpperBound,
+ *sreq->largeSetLowerBound,
+ *sreq->mediumSetPresentNumber,
+ smallSetElementSetNames,
+ mediumSetElementSetNames,
+ result_set_size,
+ number_to_present,
+ element_set_name);
+}
+
+void mp_util::piggyback(int smallSetUpperBound,
+ int largeSetLowerBound,
+ int mediumSetPresentNumber,
+ int result_set_size,
+ int &number_to_present)
+{
+ Odr_int tmp = number_to_present;
+ piggyback(smallSetUpperBound, largeSetLowerBound, mediumSetPresentNumber,
+ 0, 0, result_set_size, tmp, 0);
+ number_to_present = tmp;
+}
+
+void mp_util::piggyback(Odr_int smallSetUpperBound,
+ Odr_int largeSetLowerBound,
+ Odr_int mediumSetPresentNumber,
+ const char *smallSetElementSetNames,
+ const char *mediumSetElementSetNames,
+ Odr_int result_set_size,
+ Odr_int &number_to_present,
+ const char **element_set_name)
+{
+ // deal with piggyback
+
+ if (result_set_size < smallSetUpperBound)
+ {
+ // small set . Return all records in set
+ number_to_present = result_set_size;
+ if (element_set_name && smallSetElementSetNames)
+ *element_set_name = smallSetElementSetNames;
+
+ }
+ else if (result_set_size > largeSetLowerBound)
+ {
+ // large set . Return no records
+ number_to_present = 0;
+ if (element_set_name)
+ *element_set_name = 0;
+ }
+ else
+ {
+ // medium set . Return mediumSetPresentNumber records
+ number_to_present = mediumSetPresentNumber;
+ if (number_to_present > result_set_size)
+ number_to_present = result_set_size;
+ if (element_set_name && mediumSetElementSetNames)
+ *element_set_name = mediumSetElementSetNames;
+ }
+}
+
+bool mp_util::pqf(ODR odr, Z_APDU *apdu, const std::string &q)
+{