+ if (ptr->type != XML_ELEMENT_NODE)
+ continue;
+ if (!strcmp((const char *) ptr->name, "explain"))
+ {
+ int len;
+ xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
+ xmlChar *buf_out;
+ char *content;
+
+ ptr = xmlCopyNode(ptr, 1);
+
+ xmlDocSetRootElement(doc, ptr);
+
+ xmlDocDumpMemory(doc, &buf_out, &len);
+ content = (char*) odr_malloc(rr->stream, 1+len);
+ memcpy(content, buf_out, len);
+ content[len] = '\0';
+
+ xmlFree(buf_out);
+ xmlFreeDoc(doc);
+ rr->explain_buf = content;
+ return 0;
+ }
+ }
+#endif
+ return 0;
+}
+
+static void srw_bend_explain(association *assoc, request *req,
+ Z_SRW_PDU *sr,
+ Z_SRW_explainResponse *srw_res,
+ int *http_code)
+{
+ Z_SRW_explainRequest *srw_req = sr->u.explain_request;
+ yaz_log(log_requestdetail, "Got SRW ExplainRequest");
+ *http_code = 404;
+ srw_bend_init(assoc, &srw_res->diagnostics, &srw_res->num_diagnostics, sr);
+ if (assoc->init)
+ {
+ bend_explain_rr rr;
+
+ rr.stream = assoc->encode;
+ rr.decode = assoc->decode;
+ rr.print = assoc->print;
+ rr.explain_buf = 0;
+ rr.database = srw_req->database;
+ if (assoc->server)
+ rr.server_node_ptr = assoc->server->server_node_ptr;
+ else
+ rr.server_node_ptr = 0;
+ rr.schema = "http://explain.z3950.org/dtd/2.0/";
+ if (assoc->init->bend_explain)
+ (*assoc->init->bend_explain)(assoc->backend, &rr);
+ else
+ srw_bend_explain_default(assoc->backend, &rr);
+
+ if (rr.explain_buf)
+ {
+ int packing = Z_SRW_recordPacking_string;
+ if (srw_req->recordPacking)
+ {
+ packing =
+ yaz_srw_str_to_pack(srw_req->recordPacking);
+ if (packing == -1)
+ packing = Z_SRW_recordPacking_string;
+ }
+ srw_res->record.recordSchema = rr.schema;
+ srw_res->record.recordPacking = packing;
+ srw_res->record.recordData_buf = rr.explain_buf;
+ srw_res->record.recordData_len = strlen(rr.explain_buf);
+ srw_res->record.recordPosition = 0;
+ *http_code = 200;
+ }
+ }
+}
+
+static void srw_bend_scan(association *assoc, request *req,
+ Z_SRW_PDU *sr,
+ Z_SRW_scanResponse *srw_res,
+ int *http_code)
+{
+ Z_SRW_scanRequest *srw_req = sr->u.scan_request;
+ yaz_log(log_requestdetail, "Got SRW ScanRequest");
+
+ *http_code = 200;
+ srw_bend_init(assoc, &srw_res->diagnostics, &srw_res->num_diagnostics, sr);
+ if (srw_res->num_diagnostics == 0 && assoc->init)
+ {
+ struct scan_entry *save_entries;
+
+ bend_scan_rr *bsrr = (bend_scan_rr *)
+ odr_malloc (assoc->encode, sizeof(*bsrr));
+ bsrr->num_bases = 1;
+ bsrr->basenames = &srw_req->database;
+
+ bsrr->num_entries = srw_req->maximumTerms ?
+ *srw_req->maximumTerms : 10;
+ bsrr->term_position = srw_req->responsePosition ?
+ *srw_req->responsePosition : 1;
+
+ bsrr->errcode = 0;
+ bsrr->errstring = 0;
+ bsrr->referenceId = 0;
+ bsrr->stream = assoc->encode;
+ bsrr->print = assoc->print;
+ bsrr->step_size = odr_intdup(assoc->decode, 0);
+ bsrr->entries = 0;
+ bsrr->setname = 0;
+
+ if (bsrr->num_entries > 0)
+ {
+ int i;
+ bsrr->entries = (struct scan_entry *)
+ odr_malloc(assoc->decode, sizeof(*bsrr->entries) *
+ bsrr->num_entries);
+ for (i = 0; i<bsrr->num_entries; i++)
+ {
+ bsrr->entries[i].term = 0;
+ bsrr->entries[i].occurrences = 0;
+ bsrr->entries[i].errcode = 0;
+ bsrr->entries[i].errstring = 0;
+ bsrr->entries[i].display_term = 0;
+ }
+ }
+ save_entries = bsrr->entries; /* save it so we can compare later */
+
+ if (srw_req->query_type == Z_SRW_query_type_pqf &&
+ assoc->init->bend_scan)
+ {
+ YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
+
+ bsrr->term = yaz_pqf_scan(pqf_parser, assoc->decode,
+ &bsrr->attributeset,
+ srw_req->scanClause.pqf);
+ yaz_pqf_destroy(pqf_parser);
+ bsrr->scanClause = 0;
+ ((int (*)(void *, bend_scan_rr *))
+ (*assoc->init->bend_scan))(assoc->backend, bsrr);
+ }
+ else if (srw_req->query_type == Z_SRW_query_type_cql
+ && assoc->init->bend_scan && assoc->server
+ && assoc->server->cql_transform)
+ {
+ int srw_error;
+ bsrr->scanClause = 0;
+ bsrr->attributeset = 0;
+ bsrr->term = (Z_AttributesPlusTerm *)
+ odr_malloc(assoc->decode, sizeof(*bsrr->term));
+ srw_error = cql2pqf_scan(assoc->encode,
+ srw_req->scanClause.cql,
+ assoc->server->cql_transform,
+ bsrr->term);
+ if (srw_error)
+ yaz_add_srw_diagnostic(assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ srw_error, 0);
+ else
+ {
+ ((int (*)(void *, bend_scan_rr *))
+ (*assoc->init->bend_scan))(assoc->backend, bsrr);
+ }
+ }
+ else if (srw_req->query_type == Z_SRW_query_type_cql
+ && assoc->init->bend_srw_scan)
+ {
+ bsrr->term = 0;
+ bsrr->attributeset = 0;
+ bsrr->scanClause = srw_req->scanClause.cql;
+ ((int (*)(void *, bend_scan_rr *))
+ (*assoc->init->bend_srw_scan))(assoc->backend, bsrr);
+ }
+ else
+ {
+ yaz_add_srw_diagnostic(assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ YAZ_SRW_UNSUPP_OPERATION, "scan");
+ }
+ if (bsrr->errcode)
+ {
+ int srw_error;
+ if (bsrr->errcode == YAZ_BIB1_DATABASE_UNAVAILABLE)
+ {
+ *http_code = 404;
+ return;
+ }
+ srw_error = yaz_diag_bib1_to_srw (bsrr->errcode);
+
+ yaz_add_srw_diagnostic(assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ srw_error, bsrr->errstring);
+ }
+ else if (srw_res->num_diagnostics == 0 && bsrr->num_entries)
+ {
+ int i;
+ srw_res->terms = (Z_SRW_scanTerm*)
+ odr_malloc(assoc->encode, sizeof(*srw_res->terms) *
+ bsrr->num_entries);
+
+ srw_res->num_terms = bsrr->num_entries;
+ for (i = 0; i<bsrr->num_entries; i++)
+ {
+ Z_SRW_scanTerm *t = srw_res->terms + i;
+ t->value = odr_strdup(assoc->encode, bsrr->entries[i].term);
+ t->numberOfRecords =
+ odr_intdup(assoc->encode, bsrr->entries[i].occurrences);
+ t->displayTerm = 0;
+ if (save_entries == bsrr->entries &&
+ bsrr->entries[i].display_term)
+ {
+ /* the entries was _not_ set by the handler. So it's
+ safe to test for new member display_term. It is
+ NULL'ed by us.
+ */
+ t->displayTerm = odr_strdup(assoc->encode,
+ bsrr->entries[i].display_term);
+ }
+ t->whereInList = 0;
+ }
+ }
+ }
+ if (log_request)
+ {
+ WRBUF wr = wrbuf_alloc();
+ const char *querytype = 0;
+ const char *querystr = 0;
+
+ switch(srw_req->query_type)
+ {
+ case Z_SRW_query_type_pqf:
+ querytype = "PQF";
+ querystr = srw_req->scanClause.pqf;
+ break;
+ case Z_SRW_query_type_cql:
+ querytype = "CQL";
+ querystr = srw_req->scanClause.cql;
+ break;
+ default:
+ querytype = "UNKNOWN";
+ querystr = "";
+ }