+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;
+
+ if (bsrr->num_entries > 0)
+ {
+ int i;
+ bsrr->entries = 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)
+ {
+ Odr_oid *scan_attributeSet = 0;
+ oident *attset;
+ YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
+
+ bsrr->term = yaz_pqf_scan(pqf_parser, assoc->decode,
+ &scan_attributeSet,
+ srw_req->scanClause.pqf);
+ if (scan_attributeSet &&
+ (attset = oid_getentbyoid(scan_attributeSet)) &&
+ (attset->oclass == CLASS_ATTSET ||
+ attset->oclass == CLASS_GENERAL))
+ bsrr->attributeset = attset->value;
+ else
+ bsrr->attributeset = VAL_NONE;
+ 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 = VAL_NONE;
+ bsrr->term = 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 = VAL_NONE;
+ 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 = "";
+ }
+
+ wrbuf_printf(wr, "SRWScan ");
+ wrbuf_printf(wr, srw_req->database);
+ wrbuf_printf(wr, " ");
+
+ if (srw_res->num_diagnostics)
+ wrbuf_printf(wr, "ERROR %s - ", srw_res->diagnostics[0].uri);
+ else if (srw_res->num_terms)
+ wrbuf_printf(wr, "OK %d - ", srw_res->num_terms);
+ else
+ wrbuf_printf(wr, "OK - - ");
+
+ wrbuf_printf(wr, "%d+%d+0 ",
+ (srw_req->responsePosition ?
+ *srw_req->responsePosition : 1),
+ (srw_req->maximumTerms ?
+ *srw_req->maximumTerms : 1));
+ /* there is no step size in SRU/W ??? */
+ wrbuf_printf(wr, "%s: %s ", querytype, querystr);
+ yaz_log(log_request, "%s ", wrbuf_cstr(wr) );
+ wrbuf_destroy(wr);
+ }
+
+}
+
+static void srw_bend_update(association *assoc, request *req,
+ Z_SRW_PDU *sr,
+ Z_SRW_updateResponse *srw_res,
+ int *http_code)
+{
+ Z_SRW_updateRequest *srw_req = sr->u.update_request;
+ yaz_log(log_session, "SRWUpdate action=%s", srw_req->operation);
+ yaz_log(YLOG_DEBUG, "num_diag = %d", srw_res->num_diagnostics );
+ *http_code = 404;
+ srw_bend_init(assoc, &srw_res->diagnostics, &srw_res->num_diagnostics, sr);
+ if (assoc->init)
+ {
+ bend_update_rr rr;
+ Z_SRW_extra_record *extra = srw_req->extra_record;
+
+ rr.stream = assoc->encode;
+ rr.print = assoc->print;
+ rr.num_bases = 1;
+ rr.basenames = &srw_req->database;
+ rr.operation = srw_req->operation;
+ rr.operation_status = "failed";
+ rr.record_id = 0;
+ rr.record_versions = 0;
+ rr.num_versions = 0;
+ rr.record_packing = "string";
+ rr.record_schema = 0;
+ rr.record_data = 0;
+ rr.extra_record_data = 0;
+ rr.extra_request_data = 0;
+ rr.extra_response_data = 0;
+ rr.uri = 0;
+ rr.message = 0;
+ rr.details = 0;
+
+ *http_code = 200;
+ if (rr.operation == 0)
+ {
+ yaz_add_sru_update_diagnostic(
+ assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ YAZ_SRU_UPDATE_MISSING_MANDATORY_ELEMENT_RECORD_REJECTED,
+ "action" );
+ return;
+ }
+ yaz_log(YLOG_DEBUG, "basename = %s", rr.basenames[0] );
+ yaz_log(YLOG_DEBUG, "Operation = %s", rr.operation );
+ if (!strcmp( rr.operation, "delete"))
+ {
+ if (srw_req->record && !srw_req->record->recordSchema)
+ {
+ rr.record_schema = odr_strdup(
+ assoc->encode,
+ srw_req->record->recordSchema);
+ }
+ if (srw_req->record)
+ {
+ rr.record_data = odr_strdupn(
+ assoc->encode,
+ srw_req->record->recordData_buf,
+ srw_req->record->recordData_len );
+ }
+ if (extra && extra->extraRecordData_len)
+ {
+ rr.extra_record_data = odr_strdupn(
+ assoc->encode,
+ extra->extraRecordData_buf,
+ extra->extraRecordData_len );
+ }
+ if (srw_req->recordId)
+ rr.record_id = srw_req->recordId;
+ else if (extra && extra->recordIdentifier)
+ rr.record_id = extra->recordIdentifier;
+ }
+ else if (!strcmp(rr.operation, "replace"))
+ {
+ if (srw_req->recordId)
+ rr.record_id = srw_req->recordId;
+ else if (extra && extra->recordIdentifier)
+ rr.record_id = extra->recordIdentifier;
+ else
+ {
+ yaz_add_sru_update_diagnostic(
+ assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ YAZ_SRU_UPDATE_MISSING_MANDATORY_ELEMENT_RECORD_REJECTED,
+ "recordIdentifier");
+ }
+ if (!srw_req->record)
+ {
+ yaz_add_sru_update_diagnostic(
+ assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ YAZ_SRU_UPDATE_MISSING_MANDATORY_ELEMENT_RECORD_REJECTED,
+ "record");
+ }
+ else
+ {
+ if (srw_req->record->recordSchema)
+ rr.record_schema = odr_strdup(
+ assoc->encode, srw_req->record->recordSchema);
+ if (srw_req->record->recordData_len )
+ {
+ rr.record_data = odr_strdupn(assoc->encode,
+ srw_req->record->recordData_buf,
+ srw_req->record->recordData_len );
+ }
+ else
+ {
+ yaz_add_sru_update_diagnostic(
+ assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ YAZ_SRU_UPDATE_MISSING_MANDATORY_ELEMENT_RECORD_REJECTED,
+ "recordData" );
+ }
+ }
+ if (extra && extra->extraRecordData_len)
+ {
+ rr.extra_record_data = odr_strdupn(
+ assoc->encode,
+ extra->extraRecordData_buf,
+ extra->extraRecordData_len );
+ }
+ }
+ else if (!strcmp(rr.operation, "insert"))
+ {
+ if (srw_req->recordId)
+ rr.record_id = srw_req->recordId;
+ else if (extra)
+ rr.record_id = extra->recordIdentifier;
+
+ if (srw_req->record)
+ {
+ if (srw_req->record->recordSchema)
+ rr.record_schema = odr_strdup(
+ assoc->encode, srw_req->record->recordSchema);
+
+ if (srw_req->record->recordData_len)
+ rr.record_data = odr_strdupn(
+ assoc->encode,
+ srw_req->record->recordData_buf,
+ srw_req->record->recordData_len );
+ }
+ if (extra && extra->extraRecordData_len)
+ {
+ rr.extra_record_data = odr_strdupn(
+ assoc->encode,
+ extra->extraRecordData_buf,
+ extra->extraRecordData_len );
+ }
+ }
+ else
+ yaz_add_sru_update_diagnostic(assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ YAZ_SRU_UPDATE_INVALID_ACTION,
+ rr.operation );
+
+ if (srw_req->record)
+ {
+ const char *pack_str =
+ yaz_srw_pack_to_str(srw_req->record->recordPacking);
+ if (pack_str)
+ rr.record_packing = odr_strdup(assoc->encode, pack_str);
+ }
+
+ if (srw_req->num_recordVersions)
+ {
+ rr.record_versions = srw_req->recordVersions;
+ rr.num_versions = srw_req->num_recordVersions;
+ }
+ if (srw_req->extraRequestData_len)
+ {
+ rr.extra_request_data = odr_strdupn(assoc->encode,
+ srw_req->extraRequestData_buf,
+ srw_req->extraRequestData_len );
+ }
+ if (srw_res->num_diagnostics == 0)
+ {
+ if ( assoc->init->bend_srw_update)
+ (*assoc->init->bend_srw_update)(assoc->backend, &rr);
+ else
+ yaz_add_sru_update_diagnostic(
+ assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ YAZ_SRU_UPDATE_UNSPECIFIED_DATABASE_ERROR,
+ "No Update backend handler");
+ }
+
+ if (rr.uri)
+ yaz_add_srw_diagnostic_uri(assoc->encode,
+ &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ rr.uri,
+ rr.message,
+ rr.details);
+ srw_res->recordId = rr.record_id;
+ srw_res->operationStatus = rr.operation_status;
+ srw_res->recordVersions = rr.record_versions;
+ srw_res->num_recordVersions = rr.num_versions;
+ if (srw_res->extraResponseData_len)
+ {
+ srw_res->extraResponseData_buf = rr.extra_response_data;
+ srw_res->extraResponseData_len = strlen(rr.extra_response_data);
+ }
+ if (srw_res->num_diagnostics == 0 && rr.record_data)
+ {
+ srw_res->record = yaz_srw_get_record(assoc->encode);
+ srw_res->record->recordSchema = rr.record_schema;
+ if (rr.record_packing)
+ {
+ int pack = yaz_srw_str_to_pack(rr.record_packing);
+
+ if (pack == -1)
+ {
+ pack = Z_SRW_recordPacking_string;
+ yaz_log(YLOG_WARN, "Back packing %s from backend",
+ rr.record_packing);
+ }
+ srw_res->record->recordPacking = pack;
+ }
+ srw_res->record->recordData_buf = rr.record_data;
+ srw_res->record->recordData_len = strlen(rr.record_data);
+ if (rr.extra_record_data)
+ {
+ Z_SRW_extra_record *ex =
+ yaz_srw_get_extra_record(assoc->encode);
+ srw_res->extra_record = ex;
+ ex->extraRecordData_buf = rr.extra_record_data;
+ ex->extraRecordData_len = strlen(rr.extra_record_data);
+ }
+ }
+ }
+}
+
+/* check if path is OK (1); BAD (0) */
+static int check_path(const char *path)
+{
+ if (*path != '/')
+ return 0;
+ if (strstr(path, ".."))
+ return 0;
+ return 1;
+}
+
+static char *read_file(const char *fname, ODR o, int *sz)
+{
+ char *buf;
+ FILE *inf = fopen(fname, "rb");
+ if (!inf)
+ return 0;
+
+ fseek(inf, 0L, SEEK_END);
+ *sz = ftell(inf);
+ rewind(inf);
+ buf = odr_malloc(o, *sz);
+ fread(buf, 1, *sz, inf);
+ fclose(inf);
+ return buf;
+}
+