+ response_close(c, "bytarget");
+}
+
+static void bytarget_result_ready(void *data)
+{
+ struct http_channel *c = (struct http_channel *) data;
+ struct http_session *s = locate_session(c);
+ const char *status_message = "OK";
+ if (s) {
+ yaz_log(c->http_sessions->log_level, "Session %u: bytarget watch released", s->session_id);
+ bytarget_response(c, s, status_message);
+ release_session(c, s);
+ }
+ else {
+ yaz_log(c->http_sessions->log_level, "No Session found for released bytarget watch");
+ }
+}
+
+
+static void cmd_bytarget(struct http_channel *c)
+{
+ struct http_request *rq = c->request;
+ struct http_response *rs = c->response;
+ struct http_session *s = locate_session(c);
+ const char *block = http_argbyname(rq, "block");
+ const char *report = http_argbyname(rq, "report");
+ int block_error = 0;
+ int report_status = 0;
+ const char *status_message = "OK";
+ if (report && !strcmp("error", report)) {
+ block_error = 1;
+ }
+ if (report && !strcmp("status", report)) {
+ report_status = 1;
+ }
+
+ int no_active;
+
+ if (!s)
+ return;
+
+ no_active = session_active_clients(s->psession);
+ if (block && !strcmp("1",block) && no_active)
+ {
+ // if there is already a watch/block. we do not block this one
+ if (session_set_watch(s->psession, SESSION_WATCH_BYTARGET,
+ bytarget_result_ready, c, c) != 0)
+ {
+ yaz_log(YLOG_WARN, "Session %u: Attempt to block multiple times on bytarget block. Not supported!", s->session_id);
+ if (block_error) {
+ error(rs, PAZPAR2_ALREADY_BLOCKED, "bytarget");
+ release_session(c, s);
+ return;
+ }
+ if (report_status) {
+ status_message = "WARNING (Already blocked on bytarget)";
+ }
+ }
+ else
+ {
+ yaz_log(c->http_sessions->log_level, "Session %u: Blocking on command bytarget", s->session_id);
+ release_session(c, s);
+ return;
+ }
+ }
+ bytarget_response(c, s, status_message);
+ release_session(c, s);
+}
+
+static void write_metadata(WRBUF w, struct conf_service *service,
+ struct record_metadata **ml, int full)
+{
+ int imeta;
+
+ for (imeta = 0; imeta < service->num_metadata; imeta++)
+ {
+ struct conf_metadata *cmd = &service->metadata[imeta];
+ struct record_metadata *md;
+ if (!cmd->brief && !full)
+ continue;
+ for (md = ml[imeta]; md; md = md->next)
+ {
+ struct record_metadata_attr *attr = md->attributes;
+ wrbuf_printf(w, "\n<md-%s", cmd->name);
+
+ for (; attr; attr = attr->next)
+ {
+ wrbuf_printf(w, " %s=\"", attr->name);
+ wrbuf_xmlputs(w, attr->value);
+ wrbuf_puts(w, "\"");
+ }
+ wrbuf_puts(w, ">");
+ switch (cmd->type)
+ {
+ case Metadata_type_generic:
+ wrbuf_xmlputs(w, md->data.text.disp);
+ break;
+ case Metadata_type_year:
+ wrbuf_printf(w, "%d", md->data.number.min);
+ if (md->data.number.min != md->data.number.max)
+ wrbuf_printf(w, "-%d", md->data.number.max);
+ break;
+ default:
+ wrbuf_puts(w, "[can't represent]");
+ break;
+ }
+ wrbuf_printf(w, "</md-%s>", cmd->name);
+ }
+ }
+}
+
+static void write_subrecord(struct record *r, WRBUF w,
+ struct conf_service *service, int show_details)
+{
+ const char *name = session_setting_oneval(
+ client_get_database(r->client), PZ_NAME);
+
+ wrbuf_puts(w, "<location id=\"");
+ wrbuf_xmlputs(w, client_get_id(r->client));
+ wrbuf_puts(w, "\" ");
+
+ wrbuf_puts(w, "name=\"");
+ wrbuf_xmlputs(w, *name ? name : "Unknown");
+ wrbuf_puts(w, "\">");
+
+ write_metadata(w, service, r->metadata, show_details);
+ wrbuf_puts(w, "</location>\n");
+}
+
+static void show_raw_record_error(void *data, const char *addinfo)
+{
+ http_channel_observer_t obs = data;
+ struct http_channel *c = http_channel_observer_chan(obs);
+ struct http_response *rs = c->response;
+
+ http_remove_observer(obs);
+
+ error(rs, PAZPAR2_RECORD_FAIL, addinfo);
+}
+
+static void show_raw_record_ok(void *data, const char *buf, size_t sz)
+{
+ http_channel_observer_t obs = data;
+ struct http_channel *c = http_channel_observer_chan(obs);
+ struct http_response *rs = c->response;
+
+ http_remove_observer(obs);
+
+ wrbuf_write(c->wrbuf, buf, sz);
+ rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
+ http_send_response(c);
+}
+
+
+static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
+{
+ http_channel_observer_t obs = data;
+ struct http_channel *c = http_channel_observer_chan(obs);
+ struct http_response *rs = c->response;