2 * $Id: http_command.c,v 1.7 2006-12-08 21:40:58 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);
68 http_send_response(c);
78 if (gettimeofday(&t, 0) < 0)
81 res = (res << 8) | (seq & 0xff);
85 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
87 struct http_session *p;
88 char *session = http_argbyname(rq, "session");
93 error(rs, "417", "Must supply session", 0);
97 for (p = session_list; p; p = p->next)
98 if (id == p->session_id)
100 error(rs, "417", "Session does not exist, or it has expired", 0);
104 static void cmd_exit(struct http_channel *c)
106 yaz_log(YLOG_WARN, "exit");
110 static void cmd_init(struct http_channel *c)
114 struct http_session *s = http_session_create();
115 struct http_response *rs = c->response;
117 // FIXME create a pazpar2 session
118 yaz_log(YLOG_DEBUG, "HTTP Session init");
119 sesid = make_sessionid();
120 s->session_id = sesid;
121 sprintf(buf, "<init><status>OK</status><session>%d</session></init>", sesid);
122 rs->payload = nmem_strdup(c->nmem, buf);
123 http_send_response(c);
126 static void cmd_termlist(struct http_channel *c)
128 struct http_response *rs = c->response;
129 struct http_request *rq = c->request;
130 struct http_session *s = locate_session(rq, rs);
131 struct termlist_score **p;
137 wrbuf_rewind(c->wrbuf);
139 wrbuf_puts(c->wrbuf, "<termlist>");
140 p = termlist(s->psession, &len);
142 for (i = 0; i < len; i++)
144 wrbuf_puts(c->wrbuf, "\n<term>");
145 wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
146 wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
147 wrbuf_puts(c->wrbuf, "</term>");
149 wrbuf_puts(c->wrbuf, "</termlist>");
150 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_buf(c->wrbuf));
151 http_send_response(c);
155 static void cmd_bytarget(struct http_channel *c)
157 struct http_response *rs = c->response;
158 struct http_request *rq = c->request;
159 struct http_session *s = locate_session(rq, rs);
160 struct hitsbytarget *ht;
165 if (!(ht = hitsbytarget(s->psession, &count)))
167 error(rs, "500", "Failed to retrieve hitcounts", 0);
170 wrbuf_rewind(c->wrbuf);
171 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
173 for (i = 0; i < count; i++)
175 wrbuf_puts(c->wrbuf, "\n<target>");
176 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
177 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
178 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
179 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
180 wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
181 wrbuf_puts(c->wrbuf, "</target>");
184 wrbuf_puts(c->wrbuf, "</bytarget>");
185 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
186 http_send_response(c);
189 static void cmd_show(struct http_channel *c)
191 struct http_request *rq = c->request;
192 struct http_response *rs = c->response;
193 struct http_session *s = locate_session(rq, rs);
195 char *start = http_argbyname(rq, "start");
196 char *num = http_argbyname(rq, "num");
207 startn = atoi(start);
211 rl = show(s->psession, startn, &numn, &total, &total_hits);
213 wrbuf_rewind(c->wrbuf);
214 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
215 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
216 wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
217 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
218 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
220 for (i = 0; i < numn; i++)
225 wrbuf_puts(c->wrbuf, "<hit>\n");
226 wrbuf_printf(c->wrbuf, "<title>%s</title>\n", rl[i]->title);
227 for (ccount = 1, p = rl[i]->next_cluster; p; p = p->next_cluster, ccount++)
230 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
231 wrbuf_puts(c->wrbuf, "</hit>\n");
234 wrbuf_puts(c->wrbuf, "</show>\n");
235 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
236 http_send_response(c);
239 static void cmd_search(struct http_channel *c)
241 struct http_request *rq = c->request;
242 struct http_response *rs = c->response;
243 struct http_session *s = locate_session(rq, rs);
244 char *query = http_argbyname(rq, "query");
251 error(rs, "417", "Must supply query", 0);
254 res = search(s->psession, query);
257 error(rs, "417", res, res);
260 rs->payload = "<search><status>OK</status></search>";
261 http_send_response(c);
265 static void cmd_stat(struct http_channel *c)
267 struct http_request *rq = c->request;
268 struct http_response *rs = c->response;
269 struct http_session *s = locate_session(rq, rs);
270 struct statistics stat;
275 statistics(s->psession, &stat);
277 wrbuf_rewind(c->wrbuf);
278 wrbuf_puts(c->wrbuf, "<stat>");
279 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
280 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
281 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
282 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
283 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
284 wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
285 wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
286 wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
287 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
288 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
289 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
290 wrbuf_puts(c->wrbuf, "</stat>");
291 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
292 http_send_response(c);
298 void (*fun)(struct http_channel *c);
300 { "init", cmd_init },
301 { "stat", cmd_stat },
303 { "load", cmd_load },
305 { "bytarget", cmd_bytarget },
306 { "show", cmd_show },
307 { "search", cmd_search },
308 { "termlist", cmd_termlist },
309 { "exit", cmd_exit },
313 void http_command(struct http_channel *c)
315 char *command = http_argbyname(c->request, "command");
316 struct http_response *rs = http_create_response(c);
322 error(rs, "417", "Must supply command", 0);
325 for (i = 0; commands[i].name; i++)
326 if (!strcmp(commands[i].name, command))
328 (*commands[i].fun)(c);
331 if (!commands[i].name)
332 error(rs, "417", "Unknown command", 0);
340 * indent-tabs-mode: nil
342 * vim: shiftwidth=4 tabstop=8 expandtab