2 * $Id: http_command.c,v 1.2 2006-11-24 20:29:07 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);
104 static void cmd_init(struct http_request *rq, struct http_response *rs)
108 struct http_session *s = http_session_create();
110 // FIXME create a pazpar2 session
111 yaz_log(YLOG_DEBUG, "HTTP Session init");
112 sesid = make_sessionid();
113 s->session_id = sesid;
114 sprintf(buf, "<init><status>OK</status><session>%d</session></init>", sesid);
115 rs->payload = nmem_strdup(rq->channel->nmem, buf);
118 static void cmd_termlist(struct http_request *rq, struct http_response *rs)
120 struct http_session *s = locate_session(rq, rs);
121 struct http_channel *c = rq->channel;
122 struct termlist_score **p;
128 wrbuf_rewind(c->wrbuf);
130 wrbuf_puts(c->wrbuf, "<termlist>");
131 p = termlist(s->psession, &len);
133 for (i = 0; i < len; i++)
135 wrbuf_puts(c->wrbuf, "\n<term>");
136 wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
137 wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
138 wrbuf_puts(c->wrbuf, "</term>");
140 wrbuf_puts(c->wrbuf, "</termlist>");
141 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_buf(c->wrbuf));
145 static void cmd_bytarget(struct http_request *rq, struct http_response *rs)
147 struct http_session *s = locate_session(rq, rs);
148 struct http_channel *c = rq->channel;
149 struct hitsbytarget *ht;
154 if (!(ht = hitsbytarget(s->psession, &count)))
156 error(rs, "500", "Failed to retrieve hitcounts", 0);
159 wrbuf_rewind(c->wrbuf);
160 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
162 for (i = 0; i < count; i++)
164 wrbuf_puts(c->wrbuf, "\n<target>");
165 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
166 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
167 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
168 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
169 wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
170 wrbuf_puts(c->wrbuf, "</target>");
173 wrbuf_puts(c->wrbuf, "</bytarget>");
174 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
177 static void cmd_show(struct http_request *rq, struct http_response *rs)
179 struct http_session *s = locate_session(rq, rs);
180 struct http_channel *c = rq->channel;
182 char *start = http_argbyname(rq, "start");
183 char *num = http_argbyname(rq, "num");
192 startn = atoi(start);
196 rl = show(s->psession, startn, &numn);
198 wrbuf_rewind(c->wrbuf);
199 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
201 for (i = 0; i < numn; i++)
206 wrbuf_puts(c->wrbuf, "<hit>\n");
207 wrbuf_printf(c->wrbuf, "<merge_key>%s</merge_key>\n", rl[i]->merge_key);
208 for (ccount = 1, p = rl[i]->next_cluster; p; p = p->next_cluster, ccount++)
211 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
212 wrbuf_puts(c->wrbuf, "</hit>\n");
215 wrbuf_puts(c->wrbuf, "</show>\n");
216 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
219 static void cmd_search(struct http_request *rq, struct http_response *rs)
221 struct http_session *s = locate_session(rq, rs);
222 char *query = http_argbyname(rq, "query");
228 error(rs, "417", "Must supply query", 0);
231 search(s->psession, query);
232 rs->payload = "<search><status>OK</status></search>";
236 static void cmd_stat(struct http_request *rq, struct http_response *rs)
240 static void cmd_load(struct http_request *rq, struct http_response *rs)
242 struct http_session *s = locate_session(rq, rs);
243 char *fn = http_argbyname(rq, "name");
249 error(rs, "417", "Must suppply name", 0);
252 if (load_targets(s->psession, fn) < 0)
253 error(rs, "417", "Failed to find targets", "Possibly wrong filename");
255 rs->payload = "<load><status>OK</status></load>";
260 void (*fun)(struct http_request *rq, struct http_response *rs);
262 { "init", cmd_init },
263 { "stat", cmd_stat },
264 { "load", cmd_load },
265 { "bytarget", cmd_bytarget },
266 { "show", cmd_show },
267 { "search", cmd_search },
268 { "termlist", cmd_termlist },
272 struct http_response *http_command(struct http_request *rq)
274 char *command = http_argbyname(rq, "command");
275 struct http_channel *c = rq->channel;
276 struct http_response *rs = http_create_response(c);
281 error(rs, "417", "Must supply command", 0);
284 for (i = 0; commands[i].name; i++)
285 if (!strcmp(commands[i].name, command))
287 (*commands[i].fun)(rq, rs);
290 if (!commands[i].name)
291 error(rs, "417", "Unknown command", 0);
299 * indent-tabs-mode: nil
301 * vim: shiftwidth=4 tabstop=8 expandtab