Added 'virtual' facet named 'xtargets' to termlist command, which returns
[pazpar2-moved-to-github.git] / src / http_command.c
1 /*
2  * $Id: http_command.c,v 1.6 2007-01-04 20:00:58 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 // Compares two hitsbytarget nodes by hitcount
148 static int cmp_ht(const void *p1, const void *p2)
149 {
150     const struct hitsbytarget *h1 = p1;
151     const struct hitsbytarget *h2 = p2;
152     return h2->hits - h1->hits;
153 }
154
155 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
156 static void targets_termlist(WRBUF wrbuf, struct session *se)
157 {
158     struct hitsbytarget *ht;
159     int count, i;
160
161     if (!(ht = hitsbytarget(se, &count)))
162         return;
163     qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
164     for (i = 0; i < count && i < 15; i++)
165     {
166         wrbuf_puts(wrbuf, "\n<term>\n");
167         wrbuf_printf(wrbuf, "<name>%s</name>\n", ht[i].id);
168         wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
169         wrbuf_printf(wrbuf, "<state>%s</state>\n", ht[i].state);
170         wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
171         wrbuf_puts(wrbuf, "\n</term>\n");
172     }
173 }
174
175 static void cmd_termlist(struct http_channel *c)
176 {
177     struct http_response *rs = c->response;
178     struct http_request *rq = c->request;
179     struct http_session *s = locate_session(rq, rs);
180     struct termlist_score **p;
181     int len;
182     int i;
183     char *name = http_argbyname(rq, "name");
184     int status = session_active_clients(s->psession);
185
186     if (!s)
187         return;
188
189     if (!name)
190         name = "subject";
191     if (strlen(name) > 255)
192         return;
193
194     wrbuf_rewind(c->wrbuf);
195
196     wrbuf_puts(c->wrbuf, "<termlist>");
197     wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>", status);
198     while (*name)
199     {
200         char tname[256];
201         char *tp;
202
203         if (!(tp = strchr(name, ',')))
204             tp = name + strlen(name);
205         strncpy(tname, name, tp - name);
206         tname[tp - name] = '\0';
207
208         wrbuf_printf(c->wrbuf, "\n<list name=\"%s\">\n", tname);
209         if (!strcmp(tname, "xtargets"))
210             targets_termlist(c->wrbuf, s->psession);
211         else
212         {
213             p = termlist(s->psession, tname, &len);
214             if (p)
215                 for (i = 0; i < len; i++)
216                 {
217                     wrbuf_puts(c->wrbuf, "\n<term>");
218                     wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
219                     wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
220                     wrbuf_puts(c->wrbuf, "</term>");
221                 }
222         }
223         wrbuf_puts(c->wrbuf, "\n</list>");
224         name = tp;
225         if (*name == ',')
226             name++;
227     }
228     wrbuf_puts(c->wrbuf, "</termlist>");
229     rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_buf(c->wrbuf));
230     http_send_response(c);
231 }
232
233
234 static void cmd_bytarget(struct http_channel *c)
235 {
236     struct http_response *rs = c->response;
237     struct http_request *rq = c->request;
238     struct http_session *s = locate_session(rq, rs);
239     struct hitsbytarget *ht;
240     int count, i;
241
242     if (!s)
243         return;
244     if (!(ht = hitsbytarget(s->psession, &count)))
245     {
246         error(rs, "500", "Failed to retrieve hitcounts", 0);
247         return;
248     }
249     wrbuf_rewind(c->wrbuf);
250     wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
251
252     for (i = 0; i < count; i++)
253     {
254         wrbuf_puts(c->wrbuf, "\n<target>");
255         wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
256         wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
257         wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
258         wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
259         wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
260         wrbuf_puts(c->wrbuf, "</target>");
261     }
262
263     wrbuf_puts(c->wrbuf, "</bytarget>");
264     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
265     http_send_response(c);
266 }
267
268 static void show_records(struct http_channel *c, int active)
269 {
270     struct http_request *rq = c->request;
271     struct http_response *rs = c->response;
272     struct http_session *s = locate_session(rq, rs);
273     struct record **rl;
274     NMEM nmem_show;
275     char *start = http_argbyname(rq, "start");
276     char *num = http_argbyname(rq, "num");
277     int startn = 0;
278     int numn = 20;
279     int total;
280     int total_hits;
281     int i;
282
283     if (!s)
284         return;
285
286     // We haven't counted clients yet if we're called on a block release
287     if (active < 0)
288         active = session_active_clients(s->psession);
289
290     if (start)
291         startn = atoi(start);
292     if (num)
293         numn = atoi(num);
294
295     nmem_show = nmem_create();
296     rl = show(s->psession, startn, &numn, &total, &total_hits, nmem_show);
297
298     wrbuf_rewind(c->wrbuf);
299     wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
300     wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
301     wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
302     wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
303     wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
304     wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
305
306     for (i = 0; i < numn; i++)
307     {
308         int ccount;
309         struct record *p;
310
311         wrbuf_puts(c->wrbuf, "<hit>\n");
312         wrbuf_printf(c->wrbuf, "<title>%s</title>\n", rl[i]->title);
313         for (ccount = 1, p = rl[i]->next_cluster; p;  p = p->next_cluster, ccount++)
314             ;
315         if (ccount > 1)
316             wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
317         wrbuf_puts(c->wrbuf, "</hit>\n");
318     }
319
320     wrbuf_puts(c->wrbuf, "</show>\n");
321     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
322     http_send_response(c);
323     nmem_destroy(nmem_show);
324 }
325
326 static void show_records_ready(void *data)
327 {
328     struct http_channel *c = (struct http_channel *) data;
329
330     show_records(c, -1);
331 }
332
333 static void cmd_show(struct http_channel *c)
334 {
335     struct http_request *rq = c->request;
336     struct http_response *rs = c->response;
337     struct http_session *s = locate_session(rq, rs);
338     char *block = http_argbyname(rq, "block");
339     int status = session_active_clients(s->psession);
340
341     if (!s)
342         return;
343
344     if (block)
345     {
346         if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
347         {
348             session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
349             yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
350             return;
351         }
352     }
353
354     show_records(c, status);
355 }
356
357 static void cmd_ping(struct http_channel *c)
358 {
359     struct http_request *rq = c->request;
360     struct http_response *rs = c->response;
361     struct http_session *s = locate_session(rq, rs);
362     if (!s)
363         return;
364     rs->payload = "<ping><status>OK</status></ping>";
365     http_send_response(c);
366 }
367
368 static void cmd_search(struct http_channel *c)
369 {
370     struct http_request *rq = c->request;
371     struct http_response *rs = c->response;
372     struct http_session *s = locate_session(rq, rs);
373     char *query = http_argbyname(rq, "query");
374     char *res;
375
376     if (!s)
377         return;
378     if (!query)
379     {
380         error(rs, "417", "Must supply query", 0);
381         return;
382     }
383     res = search(s->psession, query);
384     if (res)
385     {
386         error(rs, "417", res, res);
387         return;
388     }
389     rs->payload = "<search><status>OK</status></search>";
390     http_send_response(c);
391 }
392
393
394 static void cmd_stat(struct http_channel *c)
395 {
396     struct http_request *rq = c->request;
397     struct http_response *rs = c->response;
398     struct http_session *s = locate_session(rq, rs);
399     struct statistics stat;
400     int clients = session_active_clients(s->psession);
401
402     if (!s)
403         return;
404
405     statistics(s->psession, &stat);
406
407     wrbuf_rewind(c->wrbuf);
408     wrbuf_puts(c->wrbuf, "<stat>");
409     wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
410     wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
411     wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
412     wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
413     wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
414     wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
415     wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
416     wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
417     wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
418     wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
419     wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
420     wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
421     wrbuf_puts(c->wrbuf, "</stat>");
422     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
423     http_send_response(c);
424 }
425
426
427 struct {
428     char *name;
429     void (*fun)(struct http_channel *c);
430 } commands[] = {
431     { "init", cmd_init },
432     { "stat", cmd_stat },
433     { "bytarget", cmd_bytarget },
434     { "show", cmd_show },
435     { "search", cmd_search },
436     { "termlist", cmd_termlist },
437     { "exit", cmd_exit },
438     { "ping", cmd_ping },
439     {0,0}
440 };
441
442 void http_command(struct http_channel *c)
443 {
444     char *command = http_argbyname(c->request, "command");
445     struct http_response *rs = http_create_response(c);
446     int i;
447
448     c->response = rs;
449     if (!command)
450     {
451         error(rs, "417", "Must supply command", 0);
452         return;
453     }
454     for (i = 0; commands[i].name; i++)
455         if (!strcmp(commands[i].name, command))
456         {
457             (*commands[i].fun)(c);
458             break;
459         }
460     if (!commands[i].name)
461         error(rs, "417", "Unknown command", 0);
462
463     return;
464 }
465
466 /*
467  * Local variables:
468  * c-basic-offset: 4
469  * indent-tabs-mode: nil
470  * End:
471  * vim: shiftwidth=4 tabstop=8 expandtab
472  */