Null pointer dereference in several cmd_ functions arose when session had
[pazpar2-moved-to-github.git] / src / http_command.c
1 /*
2  * $Id: http_command.c,v 1.8 2007-01-06 05:32:23 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 "util.h"
17 #include "eventl.h"
18 #include "pazpar2.h"
19 #include "http.h"
20 #include "http_command.h"
21
22 extern struct parameters global_parameters;
23 extern IOCHAN channel_list;
24
25 struct http_session {
26     IOCHAN timeout_iochan;     // NOTE: This is NOT associated with a socket
27     struct session *psession;
28     unsigned int session_id;
29     int timestamp;
30     struct http_session *next;
31 };
32
33 static struct http_session *session_list = 0;
34
35 void http_session_destroy(struct http_session *s);
36
37 static void session_timeout(IOCHAN i, int event)
38 {
39     struct http_session *s = iochan_getdata(i);
40     http_session_destroy(s);
41 }
42
43 struct http_session *http_session_create()
44 {
45     struct http_session *r = xmalloc(sizeof(*r));
46     r->psession = new_session();
47     r->session_id = 0;
48     r->timestamp = 0;
49     r->next = session_list;
50     session_list = r;
51     r->timeout_iochan = iochan_create(-1, session_timeout, 0);
52     iochan_setdata(r->timeout_iochan, r);
53     iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout);
54     r->timeout_iochan->next = channel_list;
55     channel_list = r->timeout_iochan;
56     return r;
57 }
58
59 void http_session_destroy(struct http_session *s)
60 {
61     struct http_session **p;
62
63     for (p = &session_list; *p; p = &(*p)->next)
64         if (*p == s)
65         {
66             *p = (*p)->next;
67             break;
68         }
69     iochan_destroy(s->timeout_iochan);
70     destroy_session(s->psession);
71     xfree(s);
72 }
73
74 static void error(struct http_response *rs, char *code, char *msg, char *txt)
75 {
76     struct http_channel *c = rs->channel;
77     char tmp[1024];
78
79     if (!txt)
80         txt = msg;
81     rs->msg = nmem_strdup(c->nmem, msg);
82     strcpy(rs->code, code);
83     sprintf(tmp, "<error code=\"general\">%s</error>", txt);
84     rs->payload = nmem_strdup(c->nmem, tmp);
85     http_send_response(c);
86 }
87
88 unsigned int make_sessionid()
89 {
90     struct timeval t;
91     unsigned int res;
92     static int seq = 0;
93
94     seq++;
95     if (gettimeofday(&t, 0) < 0)
96         abort();
97     res = t.tv_sec;
98     res = ((res << 8) | (seq & 0xff)) & ((unsigned int) (1 << 31) - 1);
99     return res;
100 }
101
102 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
103 {
104     struct http_session *p;
105     char *session = http_argbyname(rq, "session");
106     unsigned int id;
107
108     if (!session)
109     {
110         error(rs, "417", "Must supply session", 0);
111         return 0;
112     }
113     id = atoi(session);
114     for (p = session_list; p; p = p->next)
115         if (id == p->session_id)
116         {
117             iochan_activity(p->timeout_iochan);
118             return p;
119         }
120     error(rs, "417", "Session does not exist, or it has expired", 0);
121     return 0;
122 }
123
124 static void cmd_exit(struct http_channel *c)
125 {
126     yaz_log(YLOG_WARN, "exit");
127     exit(0);
128 }
129
130
131 static void cmd_init(struct http_channel *c)
132 {
133     unsigned int sesid;
134     char buf[1024];
135     struct http_session *s = http_session_create();
136     struct http_response *rs = c->response;
137
138     yaz_log(YLOG_DEBUG, "HTTP Session init");
139     sesid = make_sessionid();
140     s->session_id = sesid;
141     sprintf(buf, "<init><status>OK</status><session>%u</session></init>", sesid);
142     rs->payload = nmem_strdup(c->nmem, buf);
143     http_send_response(c);
144 }
145
146 // Compares two hitsbytarget nodes by hitcount
147 static int cmp_ht(const void *p1, const void *p2)
148 {
149     const struct hitsbytarget *h1 = p1;
150     const struct hitsbytarget *h2 = p2;
151     return h2->hits - h1->hits;
152 }
153
154 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
155 static void targets_termlist(WRBUF wrbuf, struct session *se)
156 {
157     struct hitsbytarget *ht;
158     int count, i;
159
160     if (!(ht = hitsbytarget(se, &count)))
161         return;
162     qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
163     for (i = 0; i < count && i < 15; i++)
164     {
165         wrbuf_puts(wrbuf, "\n<term>\n");
166         wrbuf_printf(wrbuf, "<name>%s</name>\n", ht[i].id);
167         wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
168         wrbuf_printf(wrbuf, "<state>%s</state>\n", ht[i].state);
169         wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
170         wrbuf_puts(wrbuf, "\n</term>\n");
171     }
172 }
173
174 static void cmd_termlist(struct http_channel *c)
175 {
176     struct http_response *rs = c->response;
177     struct http_request *rq = c->request;
178     struct http_session *s = locate_session(rq, rs);
179     struct termlist_score **p;
180     int len;
181     int i;
182     char *name = http_argbyname(rq, "name");
183     int status;
184
185     if (!s)
186         return;
187
188     status = session_active_clients(s->psession);
189
190     if (!name)
191         name = "subject";
192     if (strlen(name) > 255)
193         return;
194
195     wrbuf_rewind(c->wrbuf);
196
197     wrbuf_puts(c->wrbuf, "<termlist>");
198     wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>", status);
199     while (*name)
200     {
201         char tname[256];
202         char *tp;
203
204         if (!(tp = strchr(name, ',')))
205             tp = name + strlen(name);
206         strncpy(tname, name, tp - name);
207         tname[tp - name] = '\0';
208
209         wrbuf_printf(c->wrbuf, "\n<list name=\"%s\">\n", tname);
210         if (!strcmp(tname, "xtargets"))
211             targets_termlist(c->wrbuf, s->psession);
212         else
213         {
214             p = termlist(s->psession, tname, &len);
215             if (p)
216                 for (i = 0; i < len; i++)
217                 {
218                     wrbuf_puts(c->wrbuf, "\n<term>");
219                     wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
220                     wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
221                     wrbuf_puts(c->wrbuf, "</term>");
222                 }
223         }
224         wrbuf_puts(c->wrbuf, "\n</list>");
225         name = tp;
226         if (*name == ',')
227             name++;
228     }
229     wrbuf_puts(c->wrbuf, "</termlist>");
230     rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_buf(c->wrbuf));
231     http_send_response(c);
232 }
233
234
235 static void cmd_bytarget(struct http_channel *c)
236 {
237     struct http_response *rs = c->response;
238     struct http_request *rq = c->request;
239     struct http_session *s = locate_session(rq, rs);
240     struct hitsbytarget *ht;
241     int count, i;
242
243     if (!s)
244         return;
245     if (!(ht = hitsbytarget(s->psession, &count)))
246     {
247         error(rs, "500", "Failed to retrieve hitcounts", 0);
248         return;
249     }
250     wrbuf_rewind(c->wrbuf);
251     wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
252
253     for (i = 0; i < count; i++)
254     {
255         wrbuf_puts(c->wrbuf, "\n<target>");
256         wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
257         wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
258         wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
259         wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
260         wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
261         wrbuf_puts(c->wrbuf, "</target>");
262     }
263
264     wrbuf_puts(c->wrbuf, "</bytarget>");
265     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
266     http_send_response(c);
267 }
268
269 static void show_records(struct http_channel *c, int active)
270 {
271     struct http_request *rq = c->request;
272     struct http_response *rs = c->response;
273     struct http_session *s = locate_session(rq, rs);
274     struct record **rl;
275     NMEM nmem_show;
276     char *start = http_argbyname(rq, "start");
277     char *num = http_argbyname(rq, "num");
278     int startn = 0;
279     int numn = 20;
280     int total;
281     int total_hits;
282     int i;
283
284     if (!s)
285         return;
286
287     // We haven't counted clients yet if we're called on a block release
288     if (active < 0)
289         active = session_active_clients(s->psession);
290
291     if (start)
292         startn = atoi(start);
293     if (num)
294         numn = atoi(num);
295
296     nmem_show = nmem_create();
297     rl = show(s->psession, startn, &numn, &total, &total_hits, nmem_show);
298
299     wrbuf_rewind(c->wrbuf);
300     wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
301     wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
302     wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
303     wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
304     wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
305     wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
306
307     for (i = 0; i < numn; i++)
308     {
309         int ccount;
310         struct record *p;
311
312         wrbuf_puts(c->wrbuf, "<hit>\n");
313         wrbuf_printf(c->wrbuf, "<title>%s</title>\n", rl[i]->title);
314         for (ccount = 1, p = rl[i]->next_cluster; p;  p = p->next_cluster, ccount++)
315             ;
316         if (ccount > 1)
317             wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
318         wrbuf_puts(c->wrbuf, "</hit>\n");
319     }
320
321     wrbuf_puts(c->wrbuf, "</show>\n");
322     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
323     http_send_response(c);
324     nmem_destroy(nmem_show);
325 }
326
327 static void show_records_ready(void *data)
328 {
329     struct http_channel *c = (struct http_channel *) data;
330
331     show_records(c, -1);
332 }
333
334 static void cmd_show(struct http_channel *c)
335 {
336     struct http_request *rq = c->request;
337     struct http_response *rs = c->response;
338     struct http_session *s = locate_session(rq, rs);
339     char *block = http_argbyname(rq, "block");
340     int status;
341
342     if (!s)
343         return;
344
345     status = session_active_clients(s->psession);
346
347     if (block)
348     {
349         if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
350         {
351             session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
352             yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
353             return;
354         }
355     }
356
357     show_records(c, status);
358 }
359
360 static void cmd_ping(struct http_channel *c)
361 {
362     struct http_request *rq = c->request;
363     struct http_response *rs = c->response;
364     struct http_session *s = locate_session(rq, rs);
365     if (!s)
366         return;
367     rs->payload = "<ping><status>OK</status></ping>";
368     http_send_response(c);
369 }
370
371 static void cmd_search(struct http_channel *c)
372 {
373     struct http_request *rq = c->request;
374     struct http_response *rs = c->response;
375     struct http_session *s = locate_session(rq, rs);
376     char *query = http_argbyname(rq, "query");
377     char *res;
378
379     if (!s)
380         return;
381     if (!query)
382     {
383         error(rs, "417", "Must supply query", 0);
384         return;
385     }
386     res = search(s->psession, query);
387     if (res)
388     {
389         error(rs, "417", res, res);
390         return;
391     }
392     rs->payload = "<search><status>OK</status></search>";
393     http_send_response(c);
394 }
395
396
397 static void cmd_stat(struct http_channel *c)
398 {
399     struct http_request *rq = c->request;
400     struct http_response *rs = c->response;
401     struct http_session *s = locate_session(rq, rs);
402     struct statistics stat;
403     int clients;
404
405     if (!s)
406         return;
407
408     clients = session_active_clients(s->psession);
409     statistics(s->psession, &stat);
410
411     wrbuf_rewind(c->wrbuf);
412     wrbuf_puts(c->wrbuf, "<stat>");
413     wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
414     wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
415     wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
416     wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
417     wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
418     wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
419     wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
420     wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
421     wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
422     wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
423     wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
424     wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
425     wrbuf_puts(c->wrbuf, "</stat>");
426     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
427     http_send_response(c);
428 }
429
430
431 struct {
432     char *name;
433     void (*fun)(struct http_channel *c);
434 } commands[] = {
435     { "init", cmd_init },
436     { "stat", cmd_stat },
437     { "bytarget", cmd_bytarget },
438     { "show", cmd_show },
439     { "search", cmd_search },
440     { "termlist", cmd_termlist },
441     { "exit", cmd_exit },
442     { "ping", cmd_ping },
443     {0,0}
444 };
445
446 void http_command(struct http_channel *c)
447 {
448     char *command = http_argbyname(c->request, "command");
449     struct http_response *rs = http_create_response(c);
450     int i;
451
452     c->response = rs;
453     if (!command)
454     {
455         error(rs, "417", "Must supply command", 0);
456         return;
457     }
458     for (i = 0; commands[i].name; i++)
459         if (!strcmp(commands[i].name, command))
460         {
461             (*commands[i].fun)(c);
462             break;
463         }
464     if (!commands[i].name)
465         error(rs, "417", "Unknown command", 0);
466
467     return;
468 }
469
470 /*
471  * Local variables:
472  * c-basic-offset: 4
473  * indent-tabs-mode: nil
474  * End:
475  * vim: shiftwidth=4 tabstop=8 expandtab
476  */