2 * $Id: http_command.c,v 1.7 2007-01-06 04:54:58 quinn Exp $
14 #include <yaz/yaz-util.h>
20 #include "http_command.h"
22 extern struct parameters global_parameters;
23 extern IOCHAN channel_list;
26 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
27 struct session *psession;
28 unsigned int session_id;
30 struct http_session *next;
33 static struct http_session *session_list = 0;
35 void http_session_destroy(struct http_session *s);
37 static void session_timeout(IOCHAN i, int event)
39 struct http_session *s = iochan_getdata(i);
40 http_session_destroy(s);
43 struct http_session *http_session_create()
45 struct http_session *r = xmalloc(sizeof(*r));
46 r->psession = new_session();
49 r->next = session_list;
51 r->timeout_iochan = iochan_create(-1, session_timeout, 0);
52 iochan_setdata(r->timeout_iochan, r);
53 iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout);
54 r->timeout_iochan->next = channel_list;
55 channel_list = r->timeout_iochan;
59 void http_session_destroy(struct http_session *s)
61 struct http_session **p;
63 for (p = &session_list; *p; p = &(*p)->next)
69 iochan_destroy(s->timeout_iochan);
70 destroy_session(s->psession);
74 static void error(struct http_response *rs, char *code, char *msg, char *txt)
76 struct http_channel *c = rs->channel;
81 rs->msg = nmem_strdup(c->nmem, msg);
82 strcpy(rs->code, code);
83 sprintf(tmp, "<error code=\"general\">%s</error>", txt);
84 rs->payload = nmem_strdup(c->nmem, tmp);
85 http_send_response(c);
88 unsigned int make_sessionid()
95 if (gettimeofday(&t, 0) < 0)
98 res = ((res << 8) | (seq & 0xff)) & ((unsigned int) (1 << 31) - 1);
102 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
104 struct http_session *p;
105 char *session = http_argbyname(rq, "session");
110 error(rs, "417", "Must supply session", 0);
114 for (p = session_list; p; p = p->next)
115 if (id == p->session_id)
117 iochan_activity(p->timeout_iochan);
120 error(rs, "417", "Session does not exist, or it has expired", 0);
124 static void cmd_exit(struct http_channel *c)
126 yaz_log(YLOG_WARN, "exit");
131 static void cmd_init(struct http_channel *c)
135 struct http_session *s = http_session_create();
136 struct http_response *rs = c->response;
138 yaz_log(YLOG_DEBUG, "HTTP Session init");
139 sesid = make_sessionid();
140 s->session_id = sesid;
141 sprintf(buf, "<init><status>OK</status><session>%u</session></init>", sesid);
142 rs->payload = nmem_strdup(c->nmem, buf);
143 http_send_response(c);
146 // Compares two hitsbytarget nodes by hitcount
147 static int cmp_ht(const void *p1, const void *p2)
149 const struct hitsbytarget *h1 = p1;
150 const struct hitsbytarget *h2 = p2;
151 return h2->hits - h1->hits;
154 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
155 static void targets_termlist(WRBUF wrbuf, struct session *se)
157 struct hitsbytarget *ht;
160 if (!(ht = hitsbytarget(se, &count)))
162 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
163 for (i = 0; i < count && i < 15; i++)
165 wrbuf_puts(wrbuf, "\n<term>\n");
166 wrbuf_printf(wrbuf, "<name>%s</name>\n", ht[i].id);
167 wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
168 wrbuf_printf(wrbuf, "<state>%s</state>\n", ht[i].state);
169 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
170 wrbuf_puts(wrbuf, "\n</term>\n");
174 static void cmd_termlist(struct http_channel *c)
176 struct http_response *rs = c->response;
177 struct http_request *rq = c->request;
178 struct http_session *s = locate_session(rq, rs);
179 struct termlist_score **p;
182 char *name = http_argbyname(rq, "name");
183 int status = session_active_clients(s->psession);
190 if (strlen(name) > 255)
193 wrbuf_rewind(c->wrbuf);
195 wrbuf_puts(c->wrbuf, "<termlist>");
196 wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>", status);
202 if (!(tp = strchr(name, ',')))
203 tp = name + strlen(name);
204 strncpy(tname, name, tp - name);
205 tname[tp - name] = '\0';
207 wrbuf_printf(c->wrbuf, "\n<list name=\"%s\">\n", tname);
208 if (!strcmp(tname, "xtargets"))
209 targets_termlist(c->wrbuf, s->psession);
212 p = termlist(s->psession, tname, &len);
214 for (i = 0; i < len; i++)
216 wrbuf_puts(c->wrbuf, "\n<term>");
217 wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
218 wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
219 wrbuf_puts(c->wrbuf, "</term>");
222 wrbuf_puts(c->wrbuf, "\n</list>");
227 wrbuf_puts(c->wrbuf, "</termlist>");
228 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_buf(c->wrbuf));
229 http_send_response(c);
233 static void cmd_bytarget(struct http_channel *c)
235 struct http_response *rs = c->response;
236 struct http_request *rq = c->request;
237 struct http_session *s = locate_session(rq, rs);
238 struct hitsbytarget *ht;
243 if (!(ht = hitsbytarget(s->psession, &count)))
245 error(rs, "500", "Failed to retrieve hitcounts", 0);
248 wrbuf_rewind(c->wrbuf);
249 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
251 for (i = 0; i < count; i++)
253 wrbuf_puts(c->wrbuf, "\n<target>");
254 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
255 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
256 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
257 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
258 wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
259 wrbuf_puts(c->wrbuf, "</target>");
262 wrbuf_puts(c->wrbuf, "</bytarget>");
263 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
264 http_send_response(c);
267 static void show_records(struct http_channel *c, int active)
269 struct http_request *rq = c->request;
270 struct http_response *rs = c->response;
271 struct http_session *s = locate_session(rq, rs);
274 char *start = http_argbyname(rq, "start");
275 char *num = http_argbyname(rq, "num");
285 // We haven't counted clients yet if we're called on a block release
287 active = session_active_clients(s->psession);
290 startn = atoi(start);
294 nmem_show = nmem_create();
295 rl = show(s->psession, startn, &numn, &total, &total_hits, nmem_show);
297 wrbuf_rewind(c->wrbuf);
298 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
299 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
300 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
301 wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
302 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
303 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
305 for (i = 0; i < numn; i++)
310 wrbuf_puts(c->wrbuf, "<hit>\n");
311 wrbuf_printf(c->wrbuf, "<title>%s</title>\n", rl[i]->title);
312 for (ccount = 1, p = rl[i]->next_cluster; p; p = p->next_cluster, ccount++)
315 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
316 wrbuf_puts(c->wrbuf, "</hit>\n");
319 wrbuf_puts(c->wrbuf, "</show>\n");
320 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
321 http_send_response(c);
322 nmem_destroy(nmem_show);
325 static void show_records_ready(void *data)
327 struct http_channel *c = (struct http_channel *) data;
332 static void cmd_show(struct http_channel *c)
334 struct http_request *rq = c->request;
335 struct http_response *rs = c->response;
336 struct http_session *s = locate_session(rq, rs);
337 char *block = http_argbyname(rq, "block");
338 int status = session_active_clients(s->psession);
345 if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
347 session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
348 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
353 show_records(c, status);
356 static void cmd_ping(struct http_channel *c)
358 struct http_request *rq = c->request;
359 struct http_response *rs = c->response;
360 struct http_session *s = locate_session(rq, rs);
363 rs->payload = "<ping><status>OK</status></ping>";
364 http_send_response(c);
367 static void cmd_search(struct http_channel *c)
369 struct http_request *rq = c->request;
370 struct http_response *rs = c->response;
371 struct http_session *s = locate_session(rq, rs);
372 char *query = http_argbyname(rq, "query");
379 error(rs, "417", "Must supply query", 0);
382 res = search(s->psession, query);
385 error(rs, "417", res, res);
388 rs->payload = "<search><status>OK</status></search>";
389 http_send_response(c);
393 static void cmd_stat(struct http_channel *c)
395 struct http_request *rq = c->request;
396 struct http_response *rs = c->response;
397 struct http_session *s = locate_session(rq, rs);
398 struct statistics stat;
399 int clients = session_active_clients(s->psession);
404 statistics(s->psession, &stat);
406 wrbuf_rewind(c->wrbuf);
407 wrbuf_puts(c->wrbuf, "<stat>");
408 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
409 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
410 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
411 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
412 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
413 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
414 wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
415 wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
416 wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
417 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
418 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
419 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
420 wrbuf_puts(c->wrbuf, "</stat>");
421 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
422 http_send_response(c);
428 void (*fun)(struct http_channel *c);
430 { "init", cmd_init },
431 { "stat", cmd_stat },
432 { "bytarget", cmd_bytarget },
433 { "show", cmd_show },
434 { "search", cmd_search },
435 { "termlist", cmd_termlist },
436 { "exit", cmd_exit },
437 { "ping", cmd_ping },
441 void http_command(struct http_channel *c)
443 char *command = http_argbyname(c->request, "command");
444 struct http_response *rs = http_create_response(c);
450 error(rs, "417", "Must supply command", 0);
453 for (i = 0; commands[i].name; i++)
454 if (!strcmp(commands[i].name, command))
456 (*commands[i].fun)(c);
459 if (!commands[i].name)
460 error(rs, "417", "Unknown command", 0);
468 * indent-tabs-mode: nil
470 * vim: shiftwidth=4 tabstop=8 expandtab