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