2 * $Id: http_command.c,v 1.6 2006-12-04 02:27:02 quinn Exp $
14 #include <yaz/yaz-util.h>
21 #include "http_command.h"
24 struct session *psession;
27 struct http_session *next;
30 static struct http_session *session_list = 0;
32 struct http_session *http_session_create()
34 struct http_session *r = xmalloc(sizeof(*r));
35 r->psession = new_session();
38 r->next = session_list;
43 void http_session_destroy(struct http_session *s)
45 struct http_session **p;
47 for (p = &session_list; *p; p = &(*p)->next)
53 session_destroy(s->psession);
57 static void error(struct http_response *rs, char *code, char *msg, char *txt)
59 struct http_channel *c = rs->channel;
64 rs->msg = nmem_strdup(c->nmem, msg);
65 strcpy(rs->code, code);
66 sprintf(tmp, "<error code=\"general\">%s</error>", txt);
67 rs->payload = nmem_strdup(c->nmem, tmp);
77 if (gettimeofday(&t, 0) < 0)
80 res = (res << 8) | (seq & 0xff);
84 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
86 struct http_session *p;
87 char *session = http_argbyname(rq, "session");
92 error(rs, "417", "Must supply session", 0);
96 for (p = session_list; p; p = p->next)
97 if (id == p->session_id)
99 error(rs, "417", "Session does not exist, or it has expired", 0);
103 static void cmd_exit(struct http_request *rq, struct http_response *rs)
105 yaz_log(YLOG_WARN, "exit");
109 static void cmd_init(struct http_request *rq, struct http_response *rs)
113 struct http_session *s = http_session_create();
115 // FIXME create a pazpar2 session
116 yaz_log(YLOG_DEBUG, "HTTP Session init");
117 sesid = make_sessionid();
118 s->session_id = sesid;
119 sprintf(buf, "<init><status>OK</status><session>%d</session></init>", sesid);
120 rs->payload = nmem_strdup(rq->channel->nmem, buf);
123 static void cmd_termlist(struct http_request *rq, struct http_response *rs)
125 struct http_session *s = locate_session(rq, rs);
126 struct http_channel *c = rq->channel;
127 struct termlist_score **p;
133 wrbuf_rewind(c->wrbuf);
135 wrbuf_puts(c->wrbuf, "<termlist>");
136 p = termlist(s->psession, &len);
138 for (i = 0; i < len; i++)
140 wrbuf_puts(c->wrbuf, "\n<term>");
141 wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
142 wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
143 wrbuf_puts(c->wrbuf, "</term>");
145 wrbuf_puts(c->wrbuf, "</termlist>");
146 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_buf(c->wrbuf));
150 static void cmd_bytarget(struct http_request *rq, struct http_response *rs)
152 struct http_session *s = locate_session(rq, rs);
153 struct http_channel *c = rq->channel;
154 struct hitsbytarget *ht;
159 if (!(ht = hitsbytarget(s->psession, &count)))
161 error(rs, "500", "Failed to retrieve hitcounts", 0);
164 wrbuf_rewind(c->wrbuf);
165 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
167 for (i = 0; i < count; i++)
169 wrbuf_puts(c->wrbuf, "\n<target>");
170 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
171 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
172 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
173 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
174 wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
175 wrbuf_puts(c->wrbuf, "</target>");
178 wrbuf_puts(c->wrbuf, "</bytarget>");
179 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
182 static void cmd_show(struct http_request *rq, struct http_response *rs)
184 struct http_session *s = locate_session(rq, rs);
185 struct http_channel *c = rq->channel;
187 char *start = http_argbyname(rq, "start");
188 char *num = http_argbyname(rq, "num");
199 startn = atoi(start);
203 rl = show(s->psession, startn, &numn, &total, &total_hits);
205 wrbuf_rewind(c->wrbuf);
206 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
207 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
208 wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
209 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
210 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
212 for (i = 0; i < numn; i++)
217 wrbuf_puts(c->wrbuf, "<hit>\n");
218 wrbuf_printf(c->wrbuf, "<title>%s</title>\n", rl[i]->title);
219 for (ccount = 1, p = rl[i]->next_cluster; p; p = p->next_cluster, ccount++)
222 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
223 wrbuf_puts(c->wrbuf, "</hit>\n");
226 wrbuf_puts(c->wrbuf, "</show>\n");
227 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
230 static void cmd_search(struct http_request *rq, struct http_response *rs)
232 struct http_session *s = locate_session(rq, rs);
233 char *query = http_argbyname(rq, "query");
240 error(rs, "417", "Must supply query", 0);
243 res = search(s->psession, query);
246 error(rs, "417", res, res);
249 rs->payload = "<search><status>OK</status></search>";
253 static void cmd_stat(struct http_request *rq, struct http_response *rs)
255 struct http_session *s = locate_session(rq, rs);
256 struct http_channel *c = rq->channel;
257 struct statistics stat;
262 statistics(s->psession, &stat);
264 wrbuf_rewind(c->wrbuf);
265 wrbuf_puts(c->wrbuf, "<stat>");
266 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
267 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
268 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
269 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
270 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
271 wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
272 wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
273 wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
274 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
275 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
276 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
277 wrbuf_puts(c->wrbuf, "</stat>");
278 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
282 static void cmd_load(struct http_request *rq, struct http_response *rs)
284 struct http_session *s = locate_session(rq, rs);
285 char *fn = http_argbyname(rq, "name");
291 error(rs, "417", "Must suppply name", 0);
294 if (load_targets(s->psession, fn) < 0)
295 error(rs, "417", "Failed to find targets", "Possibly wrong filename");
297 rs->payload = "<load><status>OK</status></load>";
303 void (*fun)(struct http_request *rq, struct http_response *rs);
305 { "init", cmd_init },
306 { "stat", cmd_stat },
308 { "load", cmd_load },
310 { "bytarget", cmd_bytarget },
311 { "show", cmd_show },
312 { "search", cmd_search },
313 { "termlist", cmd_termlist },
314 { "exit", cmd_exit },
318 struct http_response *http_command(struct http_request *rq)
320 char *command = http_argbyname(rq, "command");
321 struct http_channel *c = rq->channel;
322 struct http_response *rs = http_create_response(c);
327 error(rs, "417", "Must supply command", 0);
330 for (i = 0; commands[i].name; i++)
331 if (!strcmp(commands[i].name, command))
333 (*commands[i].fun)(rq, rs);
336 if (!commands[i].name)
337 error(rs, "417", "Unknown command", 0);
345 * indent-tabs-mode: nil
347 * vim: shiftwidth=4 tabstop=8 expandtab