2 * $Id: http_command.c,v 1.3 2007-01-03 06:23:44 quinn Exp $
14 #include <yaz/yaz-util.h>
21 #include "http_command.h"
23 extern struct parameters global_parameters;
24 extern IOCHAN channel_list;
27 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
28 struct session *psession;
29 unsigned int session_id;
31 struct http_session *next;
34 static struct http_session *session_list = 0;
36 void http_session_destroy(struct http_session *s);
38 static void session_timeout(IOCHAN i, int event)
40 struct http_session *s = iochan_getdata(i);
41 http_session_destroy(s);
44 struct http_session *http_session_create()
46 struct http_session *r = xmalloc(sizeof(*r));
47 r->psession = new_session();
50 r->next = session_list;
52 r->timeout_iochan = iochan_create(-1, session_timeout, 0);
53 iochan_setdata(r->timeout_iochan, r);
54 iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout);
55 r->timeout_iochan->next = channel_list;
56 channel_list = r->timeout_iochan;
60 void http_session_destroy(struct http_session *s)
62 struct http_session **p;
64 for (p = &session_list; *p; p = &(*p)->next)
70 iochan_destroy(s->timeout_iochan);
71 destroy_session(s->psession);
75 static void error(struct http_response *rs, char *code, char *msg, char *txt)
77 struct http_channel *c = rs->channel;
82 rs->msg = nmem_strdup(c->nmem, msg);
83 strcpy(rs->code, code);
84 sprintf(tmp, "<error code=\"general\">%s</error>", txt);
85 rs->payload = nmem_strdup(c->nmem, tmp);
86 http_send_response(c);
89 unsigned int make_sessionid()
96 if (gettimeofday(&t, 0) < 0)
99 res = ((res << 8) | (seq & 0xff)) & ((unsigned int) (1 << 31) - 1);
103 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
105 struct http_session *p;
106 char *session = http_argbyname(rq, "session");
111 error(rs, "417", "Must supply session", 0);
115 for (p = session_list; p; p = p->next)
116 if (id == p->session_id)
118 iochan_activity(p->timeout_iochan);
121 error(rs, "417", "Session does not exist, or it has expired", 0);
125 static void cmd_exit(struct http_channel *c)
127 yaz_log(YLOG_WARN, "exit");
132 static void cmd_init(struct http_channel *c)
136 struct http_session *s = http_session_create();
137 struct http_response *rs = c->response;
139 yaz_log(YLOG_DEBUG, "HTTP Session init");
140 sesid = make_sessionid();
141 s->session_id = sesid;
142 sprintf(buf, "<init><status>OK</status><session>%u</session></init>", sesid);
143 rs->payload = nmem_strdup(c->nmem, buf);
144 http_send_response(c);
147 static void cmd_termlist(struct http_channel *c)
149 struct http_response *rs = c->response;
150 struct http_request *rq = c->request;
151 struct http_session *s = locate_session(rq, rs);
152 struct termlist_score **p;
155 char *name = http_argbyname(rq, "name");
162 if (strlen(name) > 255)
165 wrbuf_rewind(c->wrbuf);
167 wrbuf_puts(c->wrbuf, "<termlist>");
173 if (!(tp = strchr(name, ',')))
174 tp = name + strlen(name);
175 strncpy(tname, name, tp - name);
176 tname[tp - name] = '\0';
178 p = termlist(s->psession, tname, &len);
179 wrbuf_printf(c->wrbuf, "\n<list name=\"%s\">\n", tname);
181 for (i = 0; i < len; i++)
183 wrbuf_puts(c->wrbuf, "\n<term>");
184 wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
185 wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
186 wrbuf_puts(c->wrbuf, "</term>");
188 wrbuf_puts(c->wrbuf, "\n</list>");
193 wrbuf_puts(c->wrbuf, "</termlist>");
194 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_buf(c->wrbuf));
195 http_send_response(c);
199 static void cmd_bytarget(struct http_channel *c)
201 struct http_response *rs = c->response;
202 struct http_request *rq = c->request;
203 struct http_session *s = locate_session(rq, rs);
204 struct hitsbytarget *ht;
209 if (!(ht = hitsbytarget(s->psession, &count)))
211 error(rs, "500", "Failed to retrieve hitcounts", 0);
214 wrbuf_rewind(c->wrbuf);
215 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
217 for (i = 0; i < count; i++)
219 wrbuf_puts(c->wrbuf, "\n<target>");
220 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
221 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
222 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
223 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
224 wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
225 wrbuf_puts(c->wrbuf, "</target>");
228 wrbuf_puts(c->wrbuf, "</bytarget>");
229 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
230 http_send_response(c);
233 static void show_records(struct http_channel *c)
235 struct http_request *rq = c->request;
236 struct http_response *rs = c->response;
237 struct http_session *s = locate_session(rq, rs);
240 char *start = http_argbyname(rq, "start");
241 char *num = http_argbyname(rq, "num");
252 startn = atoi(start);
256 nmem_show = nmem_create();
257 rl = show(s->psession, startn, &numn, &total, &total_hits, nmem_show);
259 wrbuf_rewind(c->wrbuf);
260 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
261 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
262 wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
263 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
264 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
266 for (i = 0; i < numn; i++)
271 wrbuf_puts(c->wrbuf, "<hit>\n");
272 wrbuf_printf(c->wrbuf, "<title>%s</title>\n", rl[i]->title);
273 for (ccount = 1, p = rl[i]->next_cluster; p; p = p->next_cluster, ccount++)
276 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
277 wrbuf_puts(c->wrbuf, "</hit>\n");
280 wrbuf_puts(c->wrbuf, "</show>\n");
281 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
282 http_send_response(c);
283 nmem_destroy(nmem_show);
286 static void show_records_ready(void *data)
288 struct http_channel *c = (struct http_channel *) data;
293 static void cmd_show(struct http_channel *c)
295 struct http_request *rq = c->request;
296 struct http_response *rs = c->response;
297 struct http_session *s = locate_session(rq, rs);
298 char *block = http_argbyname(rq, "block");
305 if (!s->psession->reclist || !s->psession->reclist->num_records)
307 session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
308 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
316 static void cmd_ping(struct http_channel *c)
318 struct http_request *rq = c->request;
319 struct http_response *rs = c->response;
320 struct http_session *s = locate_session(rq, rs);
323 rs->payload = "<ping><status>OK</status></ping>";
324 http_send_response(c);
327 static void cmd_search(struct http_channel *c)
329 struct http_request *rq = c->request;
330 struct http_response *rs = c->response;
331 struct http_session *s = locate_session(rq, rs);
332 char *query = http_argbyname(rq, "query");
339 error(rs, "417", "Must supply query", 0);
342 res = search(s->psession, query);
345 error(rs, "417", res, res);
348 rs->payload = "<search><status>OK</status></search>";
349 http_send_response(c);
353 static void cmd_stat(struct http_channel *c)
355 struct http_request *rq = c->request;
356 struct http_response *rs = c->response;
357 struct http_session *s = locate_session(rq, rs);
358 struct statistics stat;
363 statistics(s->psession, &stat);
365 wrbuf_rewind(c->wrbuf);
366 wrbuf_puts(c->wrbuf, "<stat>");
367 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
368 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
369 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
370 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
371 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
372 wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
373 wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
374 wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
375 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
376 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
377 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
378 wrbuf_puts(c->wrbuf, "</stat>");
379 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
380 http_send_response(c);
386 void (*fun)(struct http_channel *c);
388 { "init", cmd_init },
389 { "stat", cmd_stat },
390 { "bytarget", cmd_bytarget },
391 { "show", cmd_show },
392 { "search", cmd_search },
393 { "termlist", cmd_termlist },
394 { "exit", cmd_exit },
395 { "ping", cmd_ping },
399 void http_command(struct http_channel *c)
401 char *command = http_argbyname(c->request, "command");
402 struct http_response *rs = http_create_response(c);
408 error(rs, "417", "Must supply command", 0);
411 for (i = 0; commands[i].name; i++)
412 if (!strcmp(commands[i].name, command))
414 (*commands[i].fun)(c);
417 if (!commands[i].name)
418 error(rs, "417", "Unknown command", 0);
426 * indent-tabs-mode: nil
428 * vim: shiftwidth=4 tabstop=8 expandtab