2 * stat->num_hits = s->total_hits;
3 * stat->num_records = s->total_records;
4 * $Id: http_command.c,v 1.3 2006-11-27 14:35:15 quinn Exp $
16 #include <yaz/yaz-util.h>
23 #include "http_command.h"
26 struct session *psession;
29 struct http_session *next;
32 static struct http_session *session_list = 0;
34 struct http_session *http_session_create()
36 struct http_session *r = xmalloc(sizeof(*r));
37 r->psession = new_session();
40 r->next = session_list;
45 void http_session_destroy(struct http_session *s)
47 struct http_session **p;
49 for (p = &session_list; *p; p = &(*p)->next)
55 session_destroy(s->psession);
59 static void error(struct http_response *rs, char *code, char *msg, char *txt)
61 struct http_channel *c = rs->channel;
66 rs->msg = nmem_strdup(c->nmem, msg);
67 strcpy(rs->code, code);
68 sprintf(tmp, "<error code=\"general\">%s</error>", txt);
69 rs->payload = nmem_strdup(c->nmem, tmp);
79 if (gettimeofday(&t, 0) < 0)
82 res = (res << 8) | (seq & 0xff);
86 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
88 struct http_session *p;
89 char *session = http_argbyname(rq, "session");
94 error(rs, "417", "Must supply session", 0);
98 for (p = session_list; p; p = p->next)
99 if (id == p->session_id)
101 error(rs, "417", "Session does not exist, or it has expired", 0);
105 static void cmd_exit(struct http_request *rq, struct http_response *rs)
107 yaz_log(YLOG_WARN, "exit");
111 static void cmd_init(struct http_request *rq, struct http_response *rs)
115 struct http_session *s = http_session_create();
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(rq->channel->nmem, buf);
125 static void cmd_termlist(struct http_request *rq, struct http_response *rs)
127 struct http_session *s = locate_session(rq, rs);
128 struct http_channel *c = rq->channel;
129 struct termlist_score **p;
135 wrbuf_rewind(c->wrbuf);
137 wrbuf_puts(c->wrbuf, "<termlist>");
138 p = termlist(s->psession, &len);
140 for (i = 0; i < len; i++)
142 wrbuf_puts(c->wrbuf, "\n<term>");
143 wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
144 wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
145 wrbuf_puts(c->wrbuf, "</term>");
147 wrbuf_puts(c->wrbuf, "</termlist>");
148 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_buf(c->wrbuf));
152 static void cmd_bytarget(struct http_request *rq, struct http_response *rs)
154 struct http_session *s = locate_session(rq, rs);
155 struct http_channel *c = rq->channel;
156 struct hitsbytarget *ht;
161 if (!(ht = hitsbytarget(s->psession, &count)))
163 error(rs, "500", "Failed to retrieve hitcounts", 0);
166 wrbuf_rewind(c->wrbuf);
167 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
169 for (i = 0; i < count; i++)
171 wrbuf_puts(c->wrbuf, "\n<target>");
172 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
173 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
174 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
175 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
176 wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
177 wrbuf_puts(c->wrbuf, "</target>");
180 wrbuf_puts(c->wrbuf, "</bytarget>");
181 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
184 static void cmd_show(struct http_request *rq, struct http_response *rs)
186 struct http_session *s = locate_session(rq, rs);
187 struct http_channel *c = rq->channel;
189 char *start = http_argbyname(rq, "start");
190 char *num = http_argbyname(rq, "num");
199 startn = atoi(start);
203 rl = show(s->psession, startn, &numn);
205 wrbuf_rewind(c->wrbuf);
206 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
208 for (i = 0; i < numn; i++)
213 wrbuf_puts(c->wrbuf, "<hit>\n");
214 wrbuf_printf(c->wrbuf, "<title>%s</title>\n", rl[i]->title);
215 for (ccount = 1, p = rl[i]->next_cluster; p; p = p->next_cluster, ccount++)
218 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
219 wrbuf_puts(c->wrbuf, "</hit>\n");
222 wrbuf_puts(c->wrbuf, "</show>\n");
223 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
226 static void cmd_search(struct http_request *rq, struct http_response *rs)
228 struct http_session *s = locate_session(rq, rs);
229 char *query = http_argbyname(rq, "query");
236 error(rs, "417", "Must supply query", 0);
239 res = search(s->psession, query);
242 error(rs, "417", res, res);
245 rs->payload = "<search><status>OK</status></search>";
249 static void cmd_stat(struct http_request *rq, struct http_response *rs)
251 struct http_session *s = locate_session(rq, rs);
252 struct http_channel *c = rq->channel;
253 struct statistics stat;
258 statistics(s->psession, &stat);
260 wrbuf_rewind(c->wrbuf);
261 wrbuf_puts(c->wrbuf, "<stat>");
262 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
263 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
264 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
265 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
266 wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
267 wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
268 wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
269 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
270 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
271 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
272 wrbuf_puts(c->wrbuf, "</stat>");
273 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
276 static void cmd_load(struct http_request *rq, struct http_response *rs)
278 struct http_session *s = locate_session(rq, rs);
279 char *fn = http_argbyname(rq, "name");
285 error(rs, "417", "Must suppply name", 0);
288 if (load_targets(s->psession, fn) < 0)
289 error(rs, "417", "Failed to find targets", "Possibly wrong filename");
291 rs->payload = "<load><status>OK</status></load>";
296 void (*fun)(struct http_request *rq, struct http_response *rs);
298 { "init", cmd_init },
299 { "stat", cmd_stat },
300 { "load", cmd_load },
301 { "bytarget", cmd_bytarget },
302 { "show", cmd_show },
303 { "search", cmd_search },
304 { "termlist", cmd_termlist },
305 { "exit", cmd_exit },
309 struct http_response *http_command(struct http_request *rq)
311 char *command = http_argbyname(rq, "command");
312 struct http_channel *c = rq->channel;
313 struct http_response *rs = http_create_response(c);
318 error(rs, "417", "Must supply command", 0);
321 for (i = 0; commands[i].name; i++)
322 if (!strcmp(commands[i].name, command))
324 (*commands[i].fun)(rq, rs);
327 if (!commands[i].name)
328 error(rs, "417", "Unknown command", 0);
336 * indent-tabs-mode: nil
338 * vim: shiftwidth=4 tabstop=8 expandtab