Implemented 'block' option to 'show' function to reduce need for
[pazpar2-moved-to-github.git] / http_command.c
1 /*
2  * $Id: http_command.c,v 1.9 2006-12-17 13:42:47 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     unsigned 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 unsigned int make_sessionid()
90 {
91     struct timeval t;
92     unsigned 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)) & ((unsigned int) (1 << 31) - 1);
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     unsigned 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     unsigned int sesid;
135     char buf[1024];
136     struct http_session *s = http_session_create();
137     struct http_response *rs = c->response;
138
139     yaz_log(YLOG_DEBUG, "HTTP Session init");
140     sesid = make_sessionid();
141     s->session_id = sesid;
142     sprintf(buf, "<init><status>OK</status><session>%u</session></init>", sesid);
143     rs->payload = nmem_strdup(c->nmem, buf);
144     http_send_response(c);
145 }
146
147 static void cmd_termlist(struct http_channel *c)
148 {
149     struct http_response *rs = c->response;
150     struct http_request *rq = c->request;
151     struct http_session *s = locate_session(rq, rs);
152     struct termlist_score **p;
153     int len;
154     int i;
155
156     if (!s)
157         return;
158     wrbuf_rewind(c->wrbuf);
159
160     wrbuf_puts(c->wrbuf, "<termlist>");
161     p = termlist(s->psession, &len);
162     if (p)
163         for (i = 0; i < len; i++)
164         {
165             wrbuf_puts(c->wrbuf, "\n<term>");
166             wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
167             wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
168             wrbuf_puts(c->wrbuf, "</term>");
169         }
170     wrbuf_puts(c->wrbuf, "</termlist>");
171     rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_buf(c->wrbuf));
172     http_send_response(c);
173 }
174
175
176 static void cmd_bytarget(struct http_channel *c)
177 {
178     struct http_response *rs = c->response;
179     struct http_request *rq = c->request;
180     struct http_session *s = locate_session(rq, rs);
181     struct hitsbytarget *ht;
182     int count, i;
183
184     if (!s)
185         return;
186     if (!(ht = hitsbytarget(s->psession, &count)))
187     {
188         error(rs, "500", "Failed to retrieve hitcounts", 0);
189         return;
190     }
191     wrbuf_rewind(c->wrbuf);
192     wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
193
194     for (i = 0; i < count; i++)
195     {
196         wrbuf_puts(c->wrbuf, "\n<target>");
197         wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
198         wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
199         wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
200         wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
201         wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
202         wrbuf_puts(c->wrbuf, "</target>");
203     }
204
205     wrbuf_puts(c->wrbuf, "</bytarget>");
206     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
207     http_send_response(c);
208 }
209
210 static void show_records(struct http_channel *c)
211 {
212     struct http_request *rq = c->request;
213     struct http_response *rs = c->response;
214     struct http_session *s = locate_session(rq, rs);
215     struct record **rl;
216     char *start = http_argbyname(rq, "start");
217     char *num = http_argbyname(rq, "num");
218     int startn = 0;
219     int numn = 20;
220     int total;
221     int total_hits;
222     int i;
223
224     if (!s)
225         return;
226
227     if (start)
228         startn = atoi(start);
229     if (num)
230         numn = atoi(num);
231
232     rl = show(s->psession, startn, &numn, &total, &total_hits);
233
234     wrbuf_rewind(c->wrbuf);
235     wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
236     wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
237     wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
238     wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
239     wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
240
241     for (i = 0; i < numn; i++)
242     {
243         int ccount;
244         struct record *p;
245
246         wrbuf_puts(c->wrbuf, "<hit>\n");
247         wrbuf_printf(c->wrbuf, "<title>%s</title>\n", rl[i]->title);
248         for (ccount = 1, p = rl[i]->next_cluster; p;  p = p->next_cluster, ccount++)
249             ;
250         if (ccount > 1)
251             wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
252         wrbuf_puts(c->wrbuf, "</hit>\n");
253     }
254
255     wrbuf_puts(c->wrbuf, "</show>\n");
256     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
257     http_send_response(c);
258 }
259
260 static void show_records_ready(void *data)
261 {
262     struct http_channel *c = (struct http_channel *) data;
263
264     show_records(c);
265 }
266
267 static void cmd_show(struct http_channel *c)
268 {
269     struct http_request *rq = c->request;
270     struct http_response *rs = c->response;
271     struct http_session *s = locate_session(rq, rs);
272     char *block = http_argbyname(rq, "block");
273
274     if (!s)
275         return;
276
277     if (block)
278     {
279         if (!s->psession->reclist || !s->psession->reclist->num_records)
280         {
281             session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
282             yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
283             return;
284         }
285     }
286
287     show_records(c);
288 }
289
290 static void cmd_ping(struct http_channel *c)
291 {
292     struct http_request *rq = c->request;
293     struct http_response *rs = c->response;
294     struct http_session *s = locate_session(rq, rs);
295     if (!s)
296         return;
297     rs->payload = "<ping><status>OK</status></ping>";
298     http_send_response(c);
299 }
300
301 static void cmd_search(struct http_channel *c)
302 {
303     struct http_request *rq = c->request;
304     struct http_response *rs = c->response;
305     struct http_session *s = locate_session(rq, rs);
306     char *query = http_argbyname(rq, "query");
307     char *res;
308
309     if (!s)
310         return;
311     if (!query)
312     {
313         error(rs, "417", "Must supply query", 0);
314         return;
315     }
316     res = search(s->psession, query);
317     if (res)
318     {
319         error(rs, "417", res, res);
320         return;
321     }
322     rs->payload = "<search><status>OK</status></search>";
323     http_send_response(c);
324 }
325
326
327 static void cmd_stat(struct http_channel *c)
328 {
329     struct http_request *rq = c->request;
330     struct http_response *rs = c->response;
331     struct http_session *s = locate_session(rq, rs);
332     struct statistics stat;
333
334     if (!s)
335         return;
336
337     statistics(s->psession, &stat);
338
339     wrbuf_rewind(c->wrbuf);
340     wrbuf_puts(c->wrbuf, "<stat>");
341     wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
342     wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
343     wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
344     wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
345     wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
346     wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
347     wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
348     wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
349     wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
350     wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
351     wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
352     wrbuf_puts(c->wrbuf, "</stat>");
353     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
354     http_send_response(c);
355 }
356
357
358 struct {
359     char *name;
360     void (*fun)(struct http_channel *c);
361 } commands[] = {
362     { "init", cmd_init },
363     { "stat", cmd_stat },
364     { "bytarget", cmd_bytarget },
365     { "show", cmd_show },
366     { "search", cmd_search },
367     { "termlist", cmd_termlist },
368     { "exit", cmd_exit },
369     { "ping", cmd_ping },
370     {0,0}
371 };
372
373 void http_command(struct http_channel *c)
374 {
375     char *command = http_argbyname(c->request, "command");
376     struct http_response *rs = http_create_response(c);
377     int i;
378
379     c->response = rs;
380     if (!command)
381     {
382         error(rs, "417", "Must supply command", 0);
383         return;
384     }
385     for (i = 0; commands[i].name; i++)
386         if (!strcmp(commands[i].name, command))
387         {
388             (*commands[i].fun)(c);
389             break;
390         }
391     if (!commands[i].name)
392         error(rs, "417", "Unknown command", 0);
393
394     return;
395 }
396
397 /*
398  * Local variables:
399  * c-basic-offset: 4
400  * indent-tabs-mode: nil
401  * End:
402  * vim: shiftwidth=4 tabstop=8 expandtab
403  */