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