X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=http_command.c;h=d3939dc7f7e73d451c99ba67800743eabae51b8f;hb=0187cede3b9446093af8dd1a198bdc9a6a08a5f3;hp=07210ec7917d9cae4e5b347e84976a0f9d90f776;hpb=3dbff6d94b35be949f03bcd922129458747bcda2;p=pazpar2-moved-to-github.git diff --git a/http_command.c b/http_command.c index 07210ec..d3939dc 100644 --- a/http_command.c +++ b/http_command.c @@ -1,5 +1,5 @@ /* - * $Id: http_command.c,v 1.1 2006-11-21 18:46:43 quinn Exp $ + * $Id: http_command.c,v 1.9 2006-12-17 13:42:47 quinn Exp $ */ #include @@ -9,6 +9,7 @@ #include #include #include +#include #include @@ -19,23 +20,40 @@ #include "http.h" #include "http_command.h" +extern struct parameters global_parameters; +extern IOCHAN channel_list; + struct http_session { + IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket struct session *psession; - char session_id[128]; + unsigned int session_id; int timestamp; struct http_session *next; }; static struct http_session *session_list = 0; +void http_session_destroy(struct http_session *s); + +static void session_timeout(IOCHAN i, int event) +{ + struct http_session *s = iochan_getdata(i); + http_session_destroy(s); +} + struct http_session *http_session_create() { struct http_session *r = xmalloc(sizeof(*r)); - r->psession = 0; - *r->session_id = '\0'; + r->psession = new_session(); + r->session_id = 0; r->timestamp = 0; r->next = session_list; session_list = r; + r->timeout_iochan = iochan_create(-1, session_timeout, 0); + iochan_setdata(r->timeout_iochan, r); + iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout); + r->timeout_iochan->next = channel_list; + channel_list = r->timeout_iochan; return r; } @@ -49,7 +67,8 @@ void http_session_destroy(struct http_session *s) *p = (*p)->next; break; } - session_destroy(s->psession); + iochan_destroy(s->timeout_iochan); + destroy_session(s->psession); xfree(s); } @@ -64,52 +83,315 @@ static void error(struct http_response *rs, char *code, char *msg, char *txt) strcpy(rs->code, code); sprintf(tmp, "%s", txt); rs->payload = nmem_strdup(c->nmem, tmp); + http_send_response(c); } -static void cmd_init(struct http_request *rq, struct http_response *rs) +unsigned int make_sessionid() { + struct timeval t; + unsigned int res; + static int seq = 0; + + seq++; + if (gettimeofday(&t, 0) < 0) + abort(); + res = t.tv_sec; + res = ((res << 8) | (seq & 0xff)) & ((unsigned int) (1 << 31) - 1); + return res; } -static void cmd_stat(struct http_request *rq, struct http_response *rs) +static struct http_session *locate_session(struct http_request *rq, struct http_response *rs) { + struct http_session *p; + char *session = http_argbyname(rq, "session"); + unsigned int id; + + if (!session) + { + error(rs, "417", "Must supply session", 0); + return 0; + } + id = atoi(session); + for (p = session_list; p; p = p->next) + if (id == p->session_id) + { + iochan_activity(p->timeout_iochan); + return p; + } + error(rs, "417", "Session does not exist, or it has expired", 0); + return 0; } -static void cmd_load(struct http_request *rq, struct http_response *rs) +static void cmd_exit(struct http_channel *c) { + yaz_log(YLOG_WARN, "exit"); + exit(0); } + +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, "OK%u", 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, ""); + p = termlist(s->psession, &len); + if (p) + for (i = 0; i < len; i++) + { + wrbuf_puts(c->wrbuf, "\n"); + wrbuf_printf(c->wrbuf, "%s", p[i]->term); + wrbuf_printf(c->wrbuf, "%d", p[i]->frequency); + wrbuf_puts(c->wrbuf, ""); + } + wrbuf_puts(c->wrbuf, ""); + 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, "OK"); + + for (i = 0; i < count; i++) + { + wrbuf_puts(c->wrbuf, "\n"); + wrbuf_printf(c->wrbuf, "%s\n", ht[i].id); + wrbuf_printf(c->wrbuf, "%d\n", ht[i].hits); + wrbuf_printf(c->wrbuf, "%d\n", ht[i].diagnostic); + wrbuf_printf(c->wrbuf, "%d\n", ht[i].records); + wrbuf_printf(c->wrbuf, "%s\n", ht[i].state); + wrbuf_puts(c->wrbuf, ""); + } + + wrbuf_puts(c->wrbuf, ""); + 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, "\nOK\n"); + wrbuf_printf(c->wrbuf, "%d\n", total); + wrbuf_printf(c->wrbuf, "%d\n", total_hits); + wrbuf_printf(c->wrbuf, "%d\n", startn); + wrbuf_printf(c->wrbuf, "%d\n", numn); + + for (i = 0; i < numn; i++) + { + int ccount; + struct record *p; + + wrbuf_puts(c->wrbuf, "\n"); + wrbuf_printf(c->wrbuf, "%s\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, "%d\n", ccount); + wrbuf_puts(c->wrbuf, "\n"); + } + + wrbuf_puts(c->wrbuf, "\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 = "OK"; + 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 = "OK"; + 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, ""); + wrbuf_printf(c->wrbuf, "%d\n", stat.num_hits); + wrbuf_printf(c->wrbuf, "%d\n", stat.num_records); + wrbuf_printf(c->wrbuf, "%d\n", stat.num_clients); + wrbuf_printf(c->wrbuf, "%d\n", stat.num_no_connection); + wrbuf_printf(c->wrbuf, "%d\n", stat.num_connecting); + wrbuf_printf(c->wrbuf, "%d\n", stat.num_initializing); + wrbuf_printf(c->wrbuf, "%d\n", stat.num_searching); + wrbuf_printf(c->wrbuf, "%d\n", stat.num_presenting); + wrbuf_printf(c->wrbuf, "%d\n", stat.num_idle); + wrbuf_printf(c->wrbuf, "%d\n", stat.num_failed); + wrbuf_printf(c->wrbuf, "%d\n", stat.num_error); + wrbuf_puts(c->wrbuf, ""); + rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf)); + http_send_response(c); +} + + struct { char *name; - void (*fun)(struct http_request *rq, struct http_response *rs); + void (*fun)(struct http_channel *c); } commands[] = { { "init", cmd_init }, { "stat", cmd_stat }, - { "load", cmd_load }, + { "bytarget", cmd_bytarget }, + { "show", cmd_show }, + { "search", cmd_search }, + { "termlist", cmd_termlist }, + { "exit", cmd_exit }, + { "ping", cmd_ping }, {0,0} }; -struct http_response *http_command(struct http_request *rq) +void http_command(struct http_channel *c) { - char *command = argbyname(rq, "command"); - struct http_channel *c = rq->channel; + char *command = http_argbyname(c->request, "command"); struct http_response *rs = http_create_response(c); int i; + c->response = rs; if (!command) { error(rs, "417", "Must supply command", 0); - return rs; + return; } for (i = 0; commands[i].name; i++) if (!strcmp(commands[i].name, command)) { - (*commands[i].fun)(rq, rs); + (*commands[i].fun)(c); break; } if (!commands[i].name) error(rs, "417", "Unknown command", 0); - return rs; + return; } /*