2 * $Id: http_command.c,v 1.9 2007-01-08 12:43:41 adam Exp $
18 #include <yaz/yaz-util.h>
24 #include "http_command.h"
26 extern struct parameters global_parameters;
27 extern IOCHAN channel_list;
30 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
31 struct session *psession;
32 unsigned int session_id;
34 struct http_session *next;
37 static struct http_session *session_list = 0;
39 void http_session_destroy(struct http_session *s);
41 static void session_timeout(IOCHAN i, int event)
43 struct http_session *s = iochan_getdata(i);
44 http_session_destroy(s);
47 struct http_session *http_session_create()
49 struct http_session *r = xmalloc(sizeof(*r));
50 r->psession = new_session();
53 r->next = session_list;
55 r->timeout_iochan = iochan_create(-1, session_timeout, 0);
56 iochan_setdata(r->timeout_iochan, r);
57 iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout);
58 r->timeout_iochan->next = channel_list;
59 channel_list = r->timeout_iochan;
63 void http_session_destroy(struct http_session *s)
65 struct http_session **p;
67 for (p = &session_list; *p; p = &(*p)->next)
73 iochan_destroy(s->timeout_iochan);
74 destroy_session(s->psession);
78 static void error(struct http_response *rs, char *code, char *msg, char *txt)
80 struct http_channel *c = rs->channel;
85 rs->msg = nmem_strdup(c->nmem, msg);
86 strcpy(rs->code, code);
87 sprintf(tmp, "<error code=\"general\">%s</error>", txt);
88 rs->payload = nmem_strdup(c->nmem, tmp);
89 http_send_response(c);
92 unsigned int make_sessionid()
99 if (gettimeofday(&t, 0) < 0)
102 res = ((res << 8) | (seq & 0xff)) & ((unsigned int) (1 << 31) - 1);
106 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
108 struct http_session *p;
109 char *session = http_argbyname(rq, "session");
114 error(rs, "417", "Must supply session", 0);
118 for (p = session_list; p; p = p->next)
119 if (id == p->session_id)
121 iochan_activity(p->timeout_iochan);
124 error(rs, "417", "Session does not exist, or it has expired", 0);
128 static void cmd_exit(struct http_channel *c)
130 yaz_log(YLOG_WARN, "exit");
135 static void cmd_init(struct http_channel *c)
139 struct http_session *s = http_session_create();
140 struct http_response *rs = c->response;
142 yaz_log(YLOG_DEBUG, "HTTP Session init");
143 sesid = make_sessionid();
144 s->session_id = sesid;
145 sprintf(buf, "<init><status>OK</status><session>%u</session></init>", sesid);
146 rs->payload = nmem_strdup(c->nmem, buf);
147 http_send_response(c);
150 // Compares two hitsbytarget nodes by hitcount
151 static int cmp_ht(const void *p1, const void *p2)
153 const struct hitsbytarget *h1 = p1;
154 const struct hitsbytarget *h2 = p2;
155 return h2->hits - h1->hits;
158 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
159 static void targets_termlist(WRBUF wrbuf, struct session *se)
161 struct hitsbytarget *ht;
164 if (!(ht = hitsbytarget(se, &count)))
166 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
167 for (i = 0; i < count && i < 15; i++)
169 wrbuf_puts(wrbuf, "\n<term>\n");
170 wrbuf_printf(wrbuf, "<name>%s</name>\n", ht[i].id);
171 wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
172 wrbuf_printf(wrbuf, "<state>%s</state>\n", ht[i].state);
173 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
174 wrbuf_puts(wrbuf, "\n</term>\n");
178 static void cmd_termlist(struct http_channel *c)
180 struct http_response *rs = c->response;
181 struct http_request *rq = c->request;
182 struct http_session *s = locate_session(rq, rs);
183 struct termlist_score **p;
186 char *name = http_argbyname(rq, "name");
192 status = session_active_clients(s->psession);
196 if (strlen(name) > 255)
199 wrbuf_rewind(c->wrbuf);
201 wrbuf_puts(c->wrbuf, "<termlist>");
202 wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>", status);
208 if (!(tp = strchr(name, ',')))
209 tp = name + strlen(name);
210 strncpy(tname, name, tp - name);
211 tname[tp - name] = '\0';
213 wrbuf_printf(c->wrbuf, "\n<list name=\"%s\">\n", tname);
214 if (!strcmp(tname, "xtargets"))
215 targets_termlist(c->wrbuf, s->psession);
218 p = termlist(s->psession, tname, &len);
220 for (i = 0; i < len; i++)
222 wrbuf_puts(c->wrbuf, "\n<term>");
223 wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
224 wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
225 wrbuf_puts(c->wrbuf, "</term>");
228 wrbuf_puts(c->wrbuf, "\n</list>");
233 wrbuf_puts(c->wrbuf, "</termlist>");
234 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_buf(c->wrbuf));
235 http_send_response(c);
239 static void cmd_bytarget(struct http_channel *c)
241 struct http_response *rs = c->response;
242 struct http_request *rq = c->request;
243 struct http_session *s = locate_session(rq, rs);
244 struct hitsbytarget *ht;
249 if (!(ht = hitsbytarget(s->psession, &count)))
251 error(rs, "500", "Failed to retrieve hitcounts", 0);
254 wrbuf_rewind(c->wrbuf);
255 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
257 for (i = 0; i < count; i++)
259 wrbuf_puts(c->wrbuf, "\n<target>");
260 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
261 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
262 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
263 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
264 wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
265 wrbuf_puts(c->wrbuf, "</target>");
268 wrbuf_puts(c->wrbuf, "</bytarget>");
269 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
270 http_send_response(c);
273 static void show_records(struct http_channel *c, int active)
275 struct http_request *rq = c->request;
276 struct http_response *rs = c->response;
277 struct http_session *s = locate_session(rq, rs);
280 char *start = http_argbyname(rq, "start");
281 char *num = http_argbyname(rq, "num");
291 // We haven't counted clients yet if we're called on a block release
293 active = session_active_clients(s->psession);
296 startn = atoi(start);
300 nmem_show = nmem_create();
301 rl = show(s->psession, startn, &numn, &total, &total_hits, nmem_show);
303 wrbuf_rewind(c->wrbuf);
304 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
305 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
306 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
307 wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
308 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
309 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
311 for (i = 0; i < numn; i++)
316 wrbuf_puts(c->wrbuf, "<hit>\n");
317 wrbuf_printf(c->wrbuf, "<title>%s</title>\n", rl[i]->title);
318 for (ccount = 1, p = rl[i]->next_cluster; p; p = p->next_cluster, ccount++)
321 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
322 wrbuf_puts(c->wrbuf, "</hit>\n");
325 wrbuf_puts(c->wrbuf, "</show>\n");
326 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
327 http_send_response(c);
328 nmem_destroy(nmem_show);
331 static void show_records_ready(void *data)
333 struct http_channel *c = (struct http_channel *) data;
338 static void cmd_show(struct http_channel *c)
340 struct http_request *rq = c->request;
341 struct http_response *rs = c->response;
342 struct http_session *s = locate_session(rq, rs);
343 char *block = http_argbyname(rq, "block");
349 status = session_active_clients(s->psession);
353 if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
355 session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
356 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
361 show_records(c, status);
364 static void cmd_ping(struct http_channel *c)
366 struct http_request *rq = c->request;
367 struct http_response *rs = c->response;
368 struct http_session *s = locate_session(rq, rs);
371 rs->payload = "<ping><status>OK</status></ping>";
372 http_send_response(c);
375 static void cmd_search(struct http_channel *c)
377 struct http_request *rq = c->request;
378 struct http_response *rs = c->response;
379 struct http_session *s = locate_session(rq, rs);
380 char *query = http_argbyname(rq, "query");
387 error(rs, "417", "Must supply query", 0);
390 res = search(s->psession, query);
393 error(rs, "417", res, res);
396 rs->payload = "<search><status>OK</status></search>";
397 http_send_response(c);
401 static void cmd_stat(struct http_channel *c)
403 struct http_request *rq = c->request;
404 struct http_response *rs = c->response;
405 struct http_session *s = locate_session(rq, rs);
406 struct statistics stat;
412 clients = session_active_clients(s->psession);
413 statistics(s->psession, &stat);
415 wrbuf_rewind(c->wrbuf);
416 wrbuf_puts(c->wrbuf, "<stat>");
417 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
418 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
419 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
420 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
421 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
422 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
423 wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
424 wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
425 wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
426 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
427 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
428 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
429 wrbuf_puts(c->wrbuf, "</stat>");
430 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
431 http_send_response(c);
437 void (*fun)(struct http_channel *c);
439 { "init", cmd_init },
440 { "stat", cmd_stat },
441 { "bytarget", cmd_bytarget },
442 { "show", cmd_show },
443 { "search", cmd_search },
444 { "termlist", cmd_termlist },
445 { "exit", cmd_exit },
446 { "ping", cmd_ping },
450 void http_command(struct http_channel *c)
452 char *command = http_argbyname(c->request, "command");
453 struct http_response *rs = http_create_response(c);
459 error(rs, "417", "Must supply command", 0);
462 for (i = 0; commands[i].name; i++)
463 if (!strcmp(commands[i].name, command))
465 (*commands[i].fun)(c);
468 if (!commands[i].name)
469 error(rs, "417", "Unknown command", 0);
477 * indent-tabs-mode: nil
479 * vim: shiftwidth=4 tabstop=8 expandtab