+
+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;
+
+ http_remove_observer(obs);
+
+ wrbuf_write(c->wrbuf, buf, sz);
+ rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
+
+ rs->content_type = "application/octet-stream";
+ http_send_response(c);
+}
+
+
+void show_raw_reset(void *data, struct http_channel *c, void *data2)
+{
+ //struct client *client = data;
+ //client_show_raw_remove(client, data2);
+}
+
+static void cmd_record_ready(void *data);
+
+static void cmd_record(struct http_channel *c)
+{
+ struct http_response *rs = c->response;
+ struct http_request *rq = c->request;
+ struct http_session *s = locate_session(c);
+ struct record_cluster *rec, *prev_r, *next_r;
+ struct record *r;
+ struct conf_service *service;
+ const char *idstr = http_argbyname(rq, "id");
+ const char *offsetstr = http_argbyname(rq, "offset");
+ const char *binarystr = http_argbyname(rq, "binary");
+
+ if (!s)
+ return;
+ service = s->psession->service;
+ if (!idstr)
+ {
+ error(rs, PAZPAR2_MISSING_PARAMETER, "id");
+ return;
+ }
+ wrbuf_rewind(c->wrbuf);
+ if (!(rec = show_single(s->psession, idstr, &prev_r, &next_r)))
+ {
+ if (session_active_clients(s->psession) == 0)
+ {
+ error(rs, PAZPAR2_RECORD_MISSING, idstr);
+ }
+ else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
+ cmd_record_ready, c, c) != 0)
+ {
+ error(rs, PAZPAR2_RECORD_MISSING, idstr);
+ }
+ return;
+ }
+ if (offsetstr)
+ {
+ int offset = atoi(offsetstr);
+ const char *syntax = http_argbyname(rq, "syntax");
+ const char *esn = http_argbyname(rq, "esn");
+ int i;
+ struct record*r = rec->records;
+ int binary = 0;
+
+ if (binarystr && *binarystr != '0')
+ binary = 1;
+
+ for (i = 0; i < offset && r; r = r->next, i++)
+ ;
+ if (!r)
+ {
+ error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
+ return;
+ }
+ else
+ {
+ http_channel_observer_t obs =
+ http_add_observer(c, r->client, show_raw_reset);
+ int ret = client_show_raw_begin(r->client, r->position,
+ syntax, esn,
+ obs /* data */,
+ show_raw_record_error,
+ (binary ?
+ show_raw_record_ok_binary :
+ show_raw_record_ok),
+ (binary ? 1 : 0));
+ if (ret == -1)
+ {
+ http_remove_observer(obs);
+ error(rs, PAZPAR2_NO_SESSION, 0);
+ }
+ }
+ }
+ else
+ {
+ wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<record>\n");
+ wrbuf_puts(c->wrbuf, "<recid>");
+ wrbuf_xmlputs(c->wrbuf, rec->recid);
+ wrbuf_puts(c->wrbuf, "</recid>\n");
+ if (prev_r)
+ {
+ wrbuf_puts(c->wrbuf, "<prevrecid>");
+ wrbuf_xmlputs(c->wrbuf, prev_r->recid);
+ wrbuf_puts(c->wrbuf, "</prevrecid>\n");
+ }
+ if (next_r)
+ {
+ wrbuf_puts(c->wrbuf, "<nextrecid>");
+ wrbuf_xmlputs(c->wrbuf, next_r->recid);
+ wrbuf_puts(c->wrbuf, "</nextrecid>\n");
+ }
+ wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
+ session_active_clients(s->psession));
+ write_metadata(c->wrbuf, service, rec->metadata, 1);
+ for (r = rec->records; r; r = r->next)
+ write_subrecord(r, c->wrbuf, service, 1);
+ wrbuf_puts(c->wrbuf, "</record>\n");
+ rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
+ http_send_response(c);
+ }
+}
+
+static void cmd_record_ready(void *data)
+{
+ struct http_channel *c = (struct http_channel *) data;
+
+ cmd_record(c);
+}
+