+void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num)
+{
+ int i;
+ for (i = 0; i<num; i++)
+ {
+ oident *ent;
+ Z_DefaultDiagFormat *r;
+ Z_DiagRec *p = pp[i];
+ if (p->which != Z_DiagRec_defaultFormat)
+ {
+ yaz_log(LOG_LOG, "%sError no diagnostics", m_session_str);
+ return;
+ }
+ else
+ r = p->u.defaultFormat;
+ if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
+ ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
+ yaz_log(LOG_LOG, "%sError unknown diagnostic set", m_session_str);
+ switch (r->which)
+ {
+ case Z_DefaultDiagFormat_v2Addinfo:
+ yaz_log(LOG_LOG, "%sError %d %s:%s",
+ m_session_str,
+ *r->condition, diagbib1_str(*r->condition),
+ r->u.v2Addinfo);
+ break;
+ case Z_DefaultDiagFormat_v3Addinfo:
+ yaz_log(LOG_LOG, "%sError %d %s:%s",
+ m_session_str,
+ *r->condition, diagbib1_str(*r->condition),
+ r->u.v3Addinfo);
+ break;
+ }
+ }
+}
+
+void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
+{
+ int i;
+
+ yaz_marc_t mt = yaz_marc_create();
+ yaz_marc_xml(mt, YAZ_MARC_MARCXML);
+ for (i = 0; i < p->num_records; i++)
+ {
+ Z_NamePlusRecord *npr = p->records[i];
+ if (npr->which == Z_NamePlusRecord_databaseRecord)
+ {
+ Z_External *r = npr->u.databaseRecord;
+ if (r->which == Z_External_octet)
+ {
+ int rlen;
+ char *result;
+ if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf,
+ r->u.octet_aligned->len,
+ &result, &rlen))
+ {
+ yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
+ WRBUF wrbuf = wrbuf_alloc();
+
+ char outbuf[120];
+ size_t inbytesleft = rlen;
+ const char *inp = result;
+ while (cd && inbytesleft)
+ {
+ size_t outbytesleft = sizeof(outbuf);
+ char *outp = outbuf;
+ size_t r;
+
+ r = yaz_iconv (cd, (char**) &inp,
+ &inbytesleft,
+ &outp, &outbytesleft);
+ if (r == (size_t) (-1))
+ {
+ int e = yaz_iconv_error(cd);
+ if (e != YAZ_ICONV_E2BIG)
+ {
+ yaz_log(LOG_WARN, "conversion failure");
+ break;
+ }
+ }
+ wrbuf_write(wrbuf, outbuf, outp - outbuf);
+ }
+ if (cd)
+ yaz_iconv_close(cd);
+
+ npr->u.databaseRecord = z_ext_record(odr_encode(),
+ VAL_TEXT_XML,
+ wrbuf_buf(wrbuf),
+ wrbuf_len(wrbuf));
+ wrbuf_free(wrbuf, 1);
+ }
+ }
+ }
+ }
+ yaz_marc_destroy(mt);
+}
+
+int Yaz_Proxy::send_http_response(int code)
+{
+ ODR o = odr_encode();
+ const char *ctype = "text/xml";
+ Z_GDU *gdu = z_get_HTTP_Response(o, code);
+ Z_HTTP_Response *hres = gdu->u.HTTP_Response;
+ if (m_http_version)
+ hres->version = odr_strdup(o, m_http_version);
+ m_http_keepalive = 0;
+ int len;
+ return send_GDU(gdu, &len);
+}
+
+int Yaz_Proxy::send_srw_response(Z_SRW_PDU *srw_pdu)
+{
+ ODR o = odr_encode();
+ const char *ctype = "text/xml";
+ Z_GDU *gdu = z_get_HTTP_Response(o, 200);
+ Z_HTTP_Response *hres = gdu->u.HTTP_Response;
+ if (m_http_version)
+ hres->version = odr_strdup(o, m_http_version);
+ z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype);
+ if (m_http_keepalive)
+ z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive");
+
+ static Z_SOAP_Handler soap_handlers[2] = {
+#if HAVE_XML2
+ {"http://www.loc.gov/zing/srw/", 0,
+ (Z_SOAP_fun) yaz_srw_codec},
+#endif
+ {0, 0, 0}
+ };
+
+ Z_SOAP *soap_package = (Z_SOAP*) odr_malloc(o, sizeof(Z_SOAP));
+ soap_package->which = Z_SOAP_generic;
+ soap_package->u.generic =
+ (Z_SOAP_Generic *) odr_malloc(o, sizeof(*soap_package->u.generic));
+ soap_package->u.generic->no = 0;
+ soap_package->u.generic->ns = soap_handlers[0].ns;
+ soap_package->u.generic->p = (void *) srw_pdu;
+ soap_package->ns = m_soap_ns;
+ int ret = z_soap_codec_enc(o, &soap_package,
+ &hres->content_buf, &hres->content_len,
+ soap_handlers, 0);
+ int len;
+ return send_GDU(gdu, &len);
+}
+
+int Yaz_Proxy::send_to_srw_client_error(int srw_error)
+{
+ ODR o = odr_encode();
+ Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
+ Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
+
+ srw_res->num_diagnostics = 1;
+ srw_res->diagnostics = (Z_SRW_diagnostic *)
+ odr_malloc(o, sizeof(*srw_res->diagnostics));
+ srw_res->diagnostics[0].code = odr_intdup(o, srw_error);
+ srw_res->diagnostics[0].details = 0;
+ return send_srw_response(srw_pdu);
+}
+
+int Yaz_Proxy::z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
+ Z_DefaultDiagFormat *ddf)
+{
+ int bib1_code = *ddf->condition;
+ if (bib1_code == 109)
+ return 404;
+ srw_res->num_diagnostics = 1;
+ srw_res->diagnostics = (Z_SRW_diagnostic *)
+ odr_malloc(o, sizeof(*srw_res->diagnostics));
+ srw_res->diagnostics[0].code =
+ odr_intdup(o, yaz_diag_bib1_to_srw(*ddf->condition));
+ srw_res->diagnostics[0].details = ddf->u.v2Addinfo;
+ return 0;
+}
+
+int Yaz_Proxy::send_to_srw_client_ok(int hits, Z_Records *records, int start)
+{
+ ODR o = odr_encode();
+ Z_SRW_PDU *srw_pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
+ Z_SRW_searchRetrieveResponse *srw_res = srw_pdu->u.response;
+
+ srw_res->numberOfRecords = odr_intdup (o, hits);
+ if (records && records->which == Z_Records_DBOSD)
+ {
+ srw_res->num_records =
+ records->u.databaseOrSurDiagnostics->num_records;
+ int i;
+ srw_res->records = (Z_SRW_record *)
+ odr_malloc(o, srw_res->num_records * sizeof(Z_SRW_record));
+ for (i = 0; i < srw_res->num_records; i++)
+ {
+ Z_NamePlusRecord *npr = records->u.databaseOrSurDiagnostics->records[i];
+ if (npr->which != Z_NamePlusRecord_databaseRecord)
+ {
+ srw_res->records[i].recordSchema = "diagnostic";
+ srw_res->records[i].recordPacking = m_s2z_packing;
+ srw_res->records[i].recordData_buf = "67";
+ srw_res->records[i].recordData_len = 2;
+ srw_res->records[i].recordPosition = odr_intdup(o, i+start);
+ continue;
+ }
+ Z_External *r = npr->u.databaseRecord;
+ oident *ent = oid_getentbyoid(r->direct_reference);
+ if (r->which == Z_External_octet && ent->value == VAL_TEXT_XML)
+ {
+ srw_res->records[i].recordSchema = "http://www.loc.gov/marcxml/";
+ srw_res->records[i].recordPacking = m_s2z_packing;
+ srw_res->records[i].recordData_buf = (char*)
+ r->u.octet_aligned->buf;
+ srw_res->records[i].recordData_len = r->u.octet_aligned->len;
+ srw_res->records[i].recordPosition = odr_intdup(o, i+start);
+ }
+ else
+ {
+ srw_res->records[i].recordSchema = "diagnostic";
+ srw_res->records[i].recordPacking = m_s2z_packing;
+ srw_res->records[i].recordData_buf = "67";
+ srw_res->records[i].recordData_len = 2;
+ srw_res->records[i].recordPosition = odr_intdup(o, i+start);
+ }
+ }
+ }
+ if (records && records->which == Z_Records_NSD)
+ {
+ int http_code;
+ http_code = z_to_srw_diag(odr_encode(), srw_res,
+ records->u.nonSurrogateDiagnostic);
+ if (http_code)
+ return send_http_response(http_code);
+ }
+ return send_srw_response(srw_pdu);
+
+}
+
+int Yaz_Proxy::send_srw_explain()
+{
+ Z_SRW_PDU *res = yaz_srw_get(odr_encode(), Z_SRW_explain_response);
+ Z_SRW_explainResponse *er = res->u.explain_response;
+
+ if (m_zeerex_fname)
+ {
+ FILE *inf = fopen(m_zeerex_fname, "rb");
+ if (inf)
+ {
+ fseek(inf, 0L, SEEK_END);
+ long sz = ftell(inf);
+ fseek(inf, 0L, SEEK_SET);
+ if (sz > 0)
+ {
+ er->record.recordData_buf =
+ (char*) odr_malloc(odr_encode(), sz);
+ size_t s = fread(er->record.recordData_buf, 1,sz, inf);
+ if (s > 0)
+ er->record.recordData_len = s;
+ }
+ else
+ yaz_log(LOG_WARN|LOG_ERRNO, "zeerex file: ftell");
+ fclose(inf);
+ }
+ else
+ yaz_log(LOG_WARN|LOG_ERRNO, "zeerex file: fopen");
+ }
+ else
+ yaz_log(LOG_LOG, "zeerex file: not defined");
+ return send_srw_response(res);
+}
+
+int Yaz_Proxy::send_PDU_convert(Z_APDU *apdu, int *len)
+{
+ if (m_http_version)
+ {
+ if (apdu->which == Z_APDU_initResponse)
+ {
+ Z_InitResponse *res = apdu->u.initResponse;
+ if (*res->result == 0)
+ {
+ send_to_srw_client_error(3);
+ }
+ else if (!m_s2z_search_apdu)
+ {
+ send_srw_explain();
+ }
+ else
+ {
+ handle_incoming_Z_PDU(m_s2z_search_apdu);
+ }
+ }
+ else if (m_s2z_search_apdu && apdu->which == Z_APDU_searchResponse)
+ {
+ m_s2z_search_apdu = 0;
+ Z_SearchResponse *res = apdu->u.searchResponse;
+ m_s2z_hit_count = *res->resultCount;
+ if (res->records && res->records->which == Z_Records_NSD)
+ {
+ send_to_srw_client_ok(0, res->records, 1);
+ }
+ else if (m_s2z_present_apdu)
+ {
+ handle_incoming_Z_PDU(m_s2z_present_apdu);
+ }
+ else
+ {
+ send_to_srw_client_ok(m_s2z_hit_count, res->records, 1);
+ }
+ }
+ else if (m_s2z_present_apdu && apdu->which == Z_APDU_presentResponse)
+ {
+ int start =
+ *m_s2z_present_apdu->u.presentRequest->resultSetStartPoint;
+
+ m_s2z_present_apdu = 0;
+ Z_PresentResponse *res = apdu->u.presentResponse;
+ send_to_srw_client_ok(m_s2z_hit_count, res->records, start);
+ }
+ }
+ else
+ return send_Z_PDU(apdu, len);
+ return 0;
+}
+
+int Yaz_Proxy::send_to_client(Z_APDU *apdu)
+{
+ int len = 0;
+ int kill_session = 0;
+ if (apdu->which == Z_APDU_searchResponse)
+ {
+ Z_SearchResponse *sr = apdu->u.searchResponse;
+ Z_Records *p = sr->records;
+ if (p && p->which == Z_Records_NSD)
+ {
+ Z_DiagRec dr, *dr_p = &dr;
+ dr.which = Z_DiagRec_defaultFormat;
+ dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
+
+ *sr->searchStatus = 0;
+ display_diagrecs(&dr_p, 1);
+ }
+ else
+ {
+ if (m_marcxml_flag && p && p->which == Z_Records_DBOSD)
+ convert_to_marcxml(p->u.databaseOrSurDiagnostics);
+ if (sr->resultCount)
+ {
+ yaz_log(LOG_LOG, "%s%d hits", m_session_str,
+ *sr->resultCount);
+ if (*sr->resultCount < 0)
+ {
+ m_invalid_session = 1;
+ kill_session = 1;
+
+ *sr->searchStatus = 0;
+ sr->records =
+ create_nonSurrogateDiagnostics(odr_encode(), 2, 0);
+ *sr->resultCount = 0;
+ }
+ }
+ }
+ }
+ else if (apdu->which == Z_APDU_presentResponse)
+ {
+ Z_PresentResponse *sr = apdu->u.presentResponse;
+ Z_Records *p = sr->records;
+ if (p && p->which == Z_Records_NSD)
+ {
+ Z_DiagRec dr, *dr_p = &dr;
+ dr.which = Z_DiagRec_defaultFormat;
+ dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
+ if (*sr->presentStatus == Z_PresentStatus_success)
+ *sr->presentStatus = Z_PresentStatus_failure;
+ display_diagrecs(&dr_p, 1);
+ }
+ if (m_marcxml_flag && p && p->which == Z_Records_DBOSD)
+ convert_to_marcxml(p->u.databaseOrSurDiagnostics);
+ }
+ int r = send_PDU_convert(apdu, &len);
+ if (r)
+ return r;
+ if (m_log_mask & PROXY_LOG_APDU_CLIENT)
+ yaz_log (LOG_DEBUG, "%sSending %s to client %d bytes", m_session_str,
+ apdu_name(apdu), len);
+ m_bytes_sent += len;
+ m_bw_stat.add_bytes(len);
+ if (kill_session)
+ {
+ delete m_client;
+ m_client = 0;
+ m_parent->pre_init();
+ }
+ if (m_http_version)
+ {
+ if (!m_http_keepalive)
+ {
+#if 1
+ timeout(1);
+#else
+ shutdown();
+ return -1;
+#endif
+ }
+ }
+ return r;
+}
+
+int Yaz_ProxyClient::send_to_target(Z_APDU *apdu)
+{
+ int len = 0;
+ int r = send_Z_PDU(apdu, &len);
+ if (m_root->get_log_mask() & PROXY_LOG_REQ_SERVER)
+ yaz_log (LOG_LOG, "%sSending %s to %s %d bytes",
+ get_session_str(),
+ apdu_name(apdu), get_hostname(), len);
+ m_bytes_sent += len;
+ return r;
+}
+