194cb8c2088de09381bbc778ae4a3a866c607b32
[pazpar2-moved-to-github.git] / http_command.c
1 /*_response(c, rs);
2  * $Id: http_command.c,v 1.8 2006-12-12 02:36:24 quinn Exp $
3  */
4
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include <sys/uio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <strings.h>
11 #include <ctype.h>
12 #include <sys/time.h>
13
14 #include <yaz/yaz-util.h>
15
16 #include "command.h"
17 #include "util.h"
18 #include "eventl.h"
19 #include "pazpar2.h"
20 #include "http.h"
21 #include "http_command.h"
22
23 extern struct parameters global_parameters;
24 extern IOCHAN channel_list;
25
26 struct http_session {
27     IOCHAN timeout_iochan;     // NOTE: This is NOT associated with a socket
28     struct session *psession;
29     int session_id;
30     int timestamp;
31     struct http_session *next;
32 };
33
34 static struct http_session *session_list = 0;
35
36 void http_session_destroy(struct http_session *s);
37
38 static void session_timeout(IOCHAN i, int event)
39 {
40     struct http_session *s = iochan_getdata(i);
41     http_session_destroy(s);
42 }
43
44 struct http_session *http_session_create()
45 {
46     struct http_session *r = xmalloc(sizeof(*r));
47     r->psession = new_session();
48     r->session_id = 0;
49     r->timestamp = 0;
50     r->next = session_list;
51     session_list = r;
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;
57     return r;
58 }
59
60 void http_session_destroy(struct http_session *s)
61 {
62     struct http_session **p;
63
64     for (p = &session_list; *p; p = &(*p)->next)
65         if (*p == s)
66         {
67             *p = (*p)->next;
68             break;
69         }
70     iochan_destroy(s->timeout_iochan);
71     destroy_session(s->psession);
72     xfree(s);
73 }
74
75 static void error(struct http_response *rs, char *code, char *msg, char *txt)
76 {
77     struct http_channel *c = rs->channel;
78     char tmp[1024];
79
80     if (!txt)
81         txt = msg;
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);
87 }
88
89 int make_sessionid()
90 {
91     struct timeval t;
92     int res;
93     static int seq = 0;
94
95     seq++;
96     if (gettimeofday(&t, 0) < 0)
97         abort();
98     res = t.tv_sec;
99     res = (res << 8) | (seq & 0xff);
100     return res;
101 }
102
103 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
104 {
105     struct http_session *p;
106     char *session = http_argbyname(rq, "session");
107     int id;
108
109     if (!session)
110     {
111         error(rs, "417", "Must supply session", 0);
112         return 0;
113     }
114     id = atoi(session);
115     for (p = session_list; p; p = p->next)
116         if (id == p->session_id)
117         {
118             iochan_activity(p->timeout_iochan);
119             return p;
120         }
121     error(rs, "417", "Session does not exist, or it has expired", 0);
122     return 0;
123 }
124
125 static void cmd_exit(struct http_channel *c)
126 {
127     yaz_log(YLOG_WARN, "exit");
128     exit(0);
129 }
130
131
132 static void cmd_init(struct http_channel *c)
133 {
134     int sesid;
135     char buf[1024];
136     struct http_session *s = http_session_create();
137     struct http_response *rs = c->response;
138
139     // FIXME create a pazpar2 session
140     yaz_log(YLOG_DEBUG, "HTTP Session init");
141     sesid = make_sessionid();
142     s->session_id = sesid;
143     sprintf(buf, "<init><status>OK</status><session>%d</session></init>", sesid);
144     rs->payload = nmem_strdup(c->nmem, buf);
145     http_send_response(c);
146 }
147
148 static void cmd_termlist(struct http_channel *c)
149 {
150     struct http_response *rs = c->response;
151     struct http_request *rq = c->request;
152     struct http_session *s = locate_session(rq, rs);
153     struct termlist_score **p;
154     int len;
155     int i;
156
157     if (!s)
158         return;
159     wrbuf_rewind(c->wrbuf);
160
161     wrbuf_puts(c->wrbuf, "<termlist>");
162     p = termlist(s->psession, &len);
163     if (p)
164         for (i = 0; i < len; i++)
165         {
166             wrbuf_puts(c->wrbuf, "\n<term>");
167             wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
168             wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
169             wrbuf_puts(c->wrbuf, "</term>");
170         }
171     wrbuf_puts(c->wrbuf, "</termlist>");
172     rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_buf(c->wrbuf));
173     http_send_response(c);
174 }
175
176
177 static void cmd_bytarget(struct http_channel *c)
178 {
179     struct http_response *rs = c->response;
180     struct http_request *rq = c->request;
181     struct http_session *s = locate_session(rq, rs);
182     struct hitsbytarget *ht;
183     int count, i;
184
185     if (!s)
186         return;
187     if (!(ht = hitsbytarget(s->psession, &count)))
188     {
189         error(rs, "500", "Failed to retrieve hitcounts", 0);
190         return;
191     }
192     wrbuf_rewind(c->wrbuf);
193     wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
194
195     for (i = 0; i < count; i++)
196     {
197         wrbuf_puts(c->wrbuf, "\n<target>");
198         wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
199         wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
200         wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
201         wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
202         wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
203         wrbuf_puts(c->wrbuf, "</target>");
204     }
205
206     wrbuf_puts(c->wrbuf, "</bytarget>");
207     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
208     http_send_response(c);
209 }
210
211 static void cmd_show(struct http_channel *c)
212 {
213     struct http_request *rq = c->request;
214     struct http_response *rs = c->response;
215     struct http_session *s = locate_session(rq, rs);
216     struct record **rl;
217     char *start = http_argbyname(rq, "start");
218     char *num = http_argbyname(rq, "num");
219     int startn = 0;
220     int numn = 20;
221     int total;
222     int total_hits;
223     int i;
224
225     if (!s)
226         return;
227
228     if (start)
229         startn = atoi(start);
230     if (num)
231         numn = atoi(num);
232
233     rl = show(s->psession, startn, &numn, &total, &total_hits);
234
235     wrbuf_rewind(c->wrbuf);
236     wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
237     wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
238     wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
239     wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
240     wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
241
242     for (i = 0; i < numn; i++)
243     {
244         int ccount;
245         struct record *p;
246
247         wrbuf_puts(c->wrbuf, "<hit>\n");
248         wrbuf_printf(c->wrbuf, "<title>%s</title>\n", rl[i]->title);
249         for (ccount = 1, p = rl[i]->next_cluster; p;  p = p->next_cluster, ccount++)
250             ;
251         if (ccount > 1)
252             wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
253         wrbuf_puts(c->wrbuf, "</hit>\n");
254     }
255
256     wrbuf_puts(c->wrbuf, "</show>\n");
257     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
258     http_send_response(c);
259 }
260
261 static void cmd_ping(struct http_channel *c)
262 {
263     struct http_request *rq = c->request;
264     struct http_response *rs = c->response;
265     struct http_session *s = locate_session(rq, rs);
266     if (!s)
267         return;
268     rs->payload = "<ping><status>OK</status></ping>";
269     http_send_response(c);
270 }
271
272 static void cmd_search(struct http_channel *c)
273 {
274     struct http_request *rq = c->request;
275     struct http_response *rs = c->response;
276     struct http_session *s = locate_session(rq, rs);
277     char *query = http_argbyname(rq, "query");
278     char *res;
279
280     if (!s)
281         return;
282     if (!query)
283     {
284         error(rs, "417", "Must supply query", 0);
285         return;
286     }
287     res = search(s->psession, query);
288     if (res)
289     {
290         error(rs, "417", res, res);
291         return;
292     }
293     rs->payload = "<search><status>OK</status></search>";
294     http_send_response(c);
295 }
296
297
298 static void cmd_stat(struct http_channel *c)
299 {
300     struct http_request *rq = c->request;
301     struct http_response *rs = c->response;
302     struct http_session *s = locate_session(rq, rs);
303     struct statistics stat;
304
305     if (!s)
306         return;
307
308     statistics(s->psession, &stat);
309
310     wrbuf_rewind(c->wrbuf);
311     wrbuf_puts(c->wrbuf, "<stat>");
312     wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
313     wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
314     wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
315     wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
316     wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
317     wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
318     wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
319     wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
320     wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
321     wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
322     wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
323     wrbuf_puts(c->wrbuf, "</stat>");
324     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
325     http_send_response(c);
326 }
327
328
329 struct {
330     char *name;
331     void (*fun)(struct http_channel *c);
332 } commands[] = {
333     { "init", cmd_init },
334     { "stat", cmd_stat },
335     { "bytarget", cmd_bytarget },
336     { "show", cmd_show },
337     { "search", cmd_search },
338     { "termlist", cmd_termlist },
339     { "exit", cmd_exit },
340     { "ping", cmd_ping },
341     {0,0}
342 };
343
344 void http_command(struct http_channel *c)
345 {
346     char *command = http_argbyname(c->request, "command");
347     struct http_response *rs = http_create_response(c);
348     int i;
349
350     c->response = rs;
351     if (!command)
352     {
353         error(rs, "417", "Must supply command", 0);
354         return;
355     }
356     for (i = 0; commands[i].name; i++)
357         if (!strcmp(commands[i].name, command))
358         {
359             (*commands[i].fun)(c);
360             break;
361         }
362     if (!commands[i].name)
363         error(rs, "417", "Unknown command", 0);
364
365     return;
366 }
367
368 /*
369  * Local variables:
370  * c-basic-offset: 4
371  * indent-tabs-mode: nil
372  * End:
373  * vim: shiftwidth=4 tabstop=8 expandtab
374  */