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