+
+static void cmd_init(struct http_channel *c)
+{
+ unsigned int sesid;
+ char buf[1024];
+ struct http_session *s = http_session_create();
+ struct http_response *rs = c->response;
+
+ yaz_log(YLOG_DEBUG, "HTTP Session init");
+ sesid = make_sessionid();
+ s->session_id = sesid;
+ sprintf(buf, "<init><status>OK</status><session>%u</session></init>", sesid);
+ rs->payload = nmem_strdup(c->nmem, buf);
+ http_send_response(c);
+}
+
+static void cmd_termlist(struct http_channel *c)
+{
+ struct http_response *rs = c->response;
+ struct http_request *rq = c->request;
+ struct http_session *s = locate_session(rq, rs);
+ struct termlist_score **p;
+ int len;
+ int i;
+
+ if (!s)
+ return;
+ wrbuf_rewind(c->wrbuf);
+
+ wrbuf_puts(c->wrbuf, "<termlist>");
+ p = termlist(s->psession, &len);
+ if (p)
+ for (i = 0; i < len; i++)
+ {
+ wrbuf_puts(c->wrbuf, "\n<term>");
+ wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
+ wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
+ wrbuf_puts(c->wrbuf, "</term>");
+ }
+ wrbuf_puts(c->wrbuf, "</termlist>");
+ rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_buf(c->wrbuf));
+ http_send_response(c);
+}
+
+
+static void cmd_bytarget(struct http_channel *c)
+{
+ struct http_response *rs = c->response;
+ struct http_request *rq = c->request;
+ struct http_session *s = locate_session(rq, rs);
+ struct hitsbytarget *ht;
+ int count, i;
+
+ if (!s)
+ return;
+ if (!(ht = hitsbytarget(s->psession, &count)))
+ {
+ error(rs, "500", "Failed to retrieve hitcounts", 0);
+ return;
+ }
+ wrbuf_rewind(c->wrbuf);
+ wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
+
+ for (i = 0; i < count; i++)
+ {
+ wrbuf_puts(c->wrbuf, "\n<target>");
+ wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
+ wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
+ wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
+ wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
+ wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
+ wrbuf_puts(c->wrbuf, "</target>");
+ }
+
+ wrbuf_puts(c->wrbuf, "</bytarget>");
+ rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
+ http_send_response(c);
+}
+
+static void show_records(struct http_channel *c)
+{
+ struct http_request *rq = c->request;
+ struct http_response *rs = c->response;
+ struct http_session *s = locate_session(rq, rs);
+ struct record **rl;
+ char *start = http_argbyname(rq, "start");
+ char *num = http_argbyname(rq, "num");
+ int startn = 0;
+ int numn = 20;
+ int total;
+ int total_hits;
+ int i;
+
+ if (!s)
+ return;
+
+ if (start)
+ startn = atoi(start);
+ if (num)
+ numn = atoi(num);
+
+ rl = show(s->psession, startn, &numn, &total, &total_hits);
+
+ wrbuf_rewind(c->wrbuf);
+ wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
+ wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
+ wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
+ wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
+ wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
+
+ for (i = 0; i < numn; i++)
+ {
+ int ccount;
+ struct record *p;
+
+ wrbuf_puts(c->wrbuf, "<hit>\n");
+ wrbuf_printf(c->wrbuf, "<title>%s</title>\n", rl[i]->title);
+ for (ccount = 1, p = rl[i]->next_cluster; p; p = p->next_cluster, ccount++)
+ ;
+ if (ccount > 1)
+ wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
+ wrbuf_puts(c->wrbuf, "</hit>\n");
+ }
+
+ wrbuf_puts(c->wrbuf, "</show>\n");
+ rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
+ http_send_response(c);
+}
+
+static void show_records_ready(void *data)
+{
+ struct http_channel *c = (struct http_channel *) data;
+
+ show_records(c);
+}
+
+static void cmd_show(struct http_channel *c)
+{
+ struct http_request *rq = c->request;
+ struct http_response *rs = c->response;
+ struct http_session *s = locate_session(rq, rs);
+ char *block = http_argbyname(rq, "block");
+
+ if (!s)
+ return;
+
+ if (block)
+ {
+ if (!s->psession->reclist || !s->psession->reclist->num_records)
+ {
+ session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
+ yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
+ return;
+ }
+ }
+
+ show_records(c);
+}
+
+static void cmd_ping(struct http_channel *c)
+{
+ struct http_request *rq = c->request;
+ struct http_response *rs = c->response;
+ struct http_session *s = locate_session(rq, rs);
+ if (!s)
+ return;
+ rs->payload = "<ping><status>OK</status></ping>";
+ http_send_response(c);
+}
+
+static void cmd_search(struct http_channel *c)
+{
+ struct http_request *rq = c->request;
+ struct http_response *rs = c->response;
+ struct http_session *s = locate_session(rq, rs);
+ char *query = http_argbyname(rq, "query");
+ char *res;
+
+ if (!s)
+ return;
+ if (!query)
+ {
+ error(rs, "417", "Must supply query", 0);
+ return;
+ }
+ res = search(s->psession, query);
+ if (res)
+ {
+ error(rs, "417", res, res);
+ return;
+ }
+ rs->payload = "<search><status>OK</status></search>";
+ http_send_response(c);
+}
+
+
+static void cmd_stat(struct http_channel *c)
+{
+ struct http_request *rq = c->request;
+ struct http_response *rs = c->response;
+ struct http_session *s = locate_session(rq, rs);
+ struct statistics stat;
+
+ if (!s)
+ return;
+
+ statistics(s->psession, &stat);
+
+ wrbuf_rewind(c->wrbuf);
+ wrbuf_puts(c->wrbuf, "<stat>");
+ wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
+ wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
+ wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
+ wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
+ wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
+ wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
+ wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
+ wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
+ wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
+ wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
+ wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
+ wrbuf_puts(c->wrbuf, "</stat>");
+ rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
+ http_send_response(c);
+}
+
+