+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)
+ {
+ Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
+ *apdu->u.searchResponse->searchStatus = 0;
+ apdu->u.searchResponse->records = rec;
+ rec->which = Z_Records_NSD;
+ rec->u.nonSurrogateDiagnostic =
+ zget_DefaultDiagFormat(m_odr, error, addinfo);
+
+ }
+ return apdu;
+}
+
+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)
+ {
+ Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
+ apdu->u.presentResponse->records = rec;
+
+ rec->which = Z_Records_NSD;
+ rec->u.nonSurrogateDiagnostic =
+ zget_DefaultDiagFormat(m_odr, error, addinfo);
+ *apdu->u.presentResponse->presentStatus = Z_PresentStatus_failure;
+ }
+ return apdu;
+}
+
+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;
+ res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
+ res->entries->num_entries = 0;
+ res->entries->entries = 0;
+
+ if (error)
+ {
+ *res->scanStatus = Z_Scan_failure;
+
+ res->entries->num_nonsurrogateDiagnostics = 1;
+ res->entries->nonsurrogateDiagnostics = (Z_DiagRec **)
+ odr_malloc(m_odr, sizeof(Z_DiagRec *));
+ res->entries->nonsurrogateDiagnostics[0] =
+ zget_DiagRec(m_odr, error, addinfo);
+ }
+ else
+ {
+ res->entries->num_nonsurrogateDiagnostics = 0;
+ res->entries->nonsurrogateDiagnostics = 0;
+ }
+ return apdu;
+}
+
+Z_GDU *mp::odr::create_HTTP_Response(mp::Session &session,
+ Z_HTTP_Request *hreq, int code)
+{
+ const char *response_version = "1.0";
+ bool keepalive = false;
+ if (!strcmp(hreq->version, "1.0"))
+ {
+ const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
+ if (v && !strcmp(v, "Keep-Alive"))
+ keepalive = true;
+ else
+ session.close();
+ response_version = "1.0";
+ }
+ else
+ {
+ const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
+ if (v && !strcmp(v, "close"))
+ session.close();
+ else
+ keepalive = true;
+ response_version = "1.1";
+ }
+
+ Z_GDU *gdu = z_get_HTTP_Response(m_odr, code);
+ 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_ReferenceId **mp_util::get_referenceId(const Z_APDU *apdu)
+{
+ switch (apdu->which)
+ {
+ case Z_APDU_initRequest:
+ return &apdu->u.initRequest->referenceId;
+ case Z_APDU_initResponse:
+ return &apdu->u.initResponse->referenceId;
+ case Z_APDU_searchRequest:
+ return &apdu->u.searchRequest->referenceId;
+ case Z_APDU_searchResponse:
+ return &apdu->u.searchResponse->referenceId;
+ case Z_APDU_presentRequest:
+ return &apdu->u.presentRequest->referenceId;
+ case Z_APDU_presentResponse:
+ return &apdu->u.presentResponse->referenceId;
+ case Z_APDU_deleteResultSetRequest:
+ return &apdu->u.deleteResultSetRequest->referenceId;
+ case Z_APDU_deleteResultSetResponse:
+ return &apdu->u.deleteResultSetResponse->referenceId;
+ case Z_APDU_accessControlRequest:
+ return &apdu->u.accessControlRequest->referenceId;
+ case Z_APDU_accessControlResponse:
+ return &apdu->u.accessControlResponse->referenceId;
+ case Z_APDU_resourceControlRequest:
+ return &apdu->u.resourceControlRequest->referenceId;
+ case Z_APDU_resourceControlResponse:
+ return &apdu->u.resourceControlResponse->referenceId;
+ case Z_APDU_triggerResourceControlRequest:
+ return &apdu->u.triggerResourceControlRequest->referenceId;
+ case Z_APDU_resourceReportRequest:
+ return &apdu->u.resourceReportRequest->referenceId;
+ case Z_APDU_resourceReportResponse:
+ return &apdu->u.resourceReportResponse->referenceId;
+ case Z_APDU_scanRequest:
+ return &apdu->u.scanRequest->referenceId;
+ case Z_APDU_scanResponse:
+ return &apdu->u.scanResponse->referenceId;
+ case Z_APDU_sortRequest:
+ return &apdu->u.sortRequest->referenceId;
+ case Z_APDU_sortResponse:
+ return &apdu->u.sortResponse->referenceId;
+ case Z_APDU_segmentRequest:
+ return &apdu->u.segmentRequest->referenceId;
+ case Z_APDU_extendedServicesRequest:
+ return &apdu->u.extendedServicesRequest->referenceId;
+ case Z_APDU_extendedServicesResponse:
+ return &apdu->u.extendedServicesResponse->referenceId;
+ case Z_APDU_close:
+ return &apdu->u.close->referenceId;
+ }
+ return 0;
+}
+
+std::string mp_util::uri_encode(std::string s)
+{
+ char *x = (char *) xmalloc(1 + s.length() * 3);
+ yaz_encode_uri_component(x, s.c_str());
+ std::string result(x);
+ xfree(x);
+ return result;
+}
+
+
+std::string mp_util::uri_decode(std::string s)
+{
+ char *x = (char *) xmalloc(1 + s.length());
+ yaz_decode_uri_component(x, s.c_str(), s.length());
+ std::string result(x);
+ xfree(x);
+ return result;
+}
+
+