Fixed bug #1187: command=show crashes if used before a search is
[pazpar2-moved-to-github.git] / src / http_command.c
1 /* $Id: http_command.c,v 1.52 2007-06-13 13:04:34 adam Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 /*
23  * $Id: http_command.c,v 1.52 2007-06-13 13:04:34 adam Exp $
24  */
25
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/uio.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <strings.h>
32 #include <ctype.h>
33 #include <sys/time.h>
34 #include <yaz/snprintf.h>
35 #if HAVE_CONFIG_H
36 #include <cconfig.h>
37 #endif
38
39 #include <yaz/yaz-util.h>
40
41 #include "config.h"
42 #include "util.h"
43 #include "eventl.h"
44 #include "pazpar2.h"
45 #include "http.h"
46 #include "http_command.h"
47 #include "settings.h"
48 #include "client.h"
49
50 // Update this when the protocol changes
51 #define PAZPAR2_PROTOCOL_VERSION "1"
52
53 struct http_session {
54     IOCHAN timeout_iochan;     // NOTE: This is NOT associated with a socket
55     struct session *psession;
56     unsigned int session_id;
57     int timestamp;
58     NMEM nmem;
59     struct http_session *next;
60 };
61
62 static struct http_session *session_list = 0;
63 void http_session_destroy(struct http_session *s);
64
65 static void session_timeout(IOCHAN i, int event)
66 {
67     struct http_session *s = iochan_getdata(i);
68     http_session_destroy(s);
69 }
70
71 struct http_session *http_session_create()
72 {
73     NMEM nmem = nmem_create();
74     struct http_session *r = nmem_malloc(nmem, sizeof(*r));
75
76     r->psession = new_session(nmem);
77     r->session_id = 0;
78     r->timestamp = 0;
79     r->nmem = nmem;
80     r->next = session_list;
81     session_list = r;
82     r->timeout_iochan = iochan_create(-1, session_timeout, 0);
83     iochan_setdata(r->timeout_iochan, r);
84     iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout);
85
86     pazpar2_add_channel(r->timeout_iochan);
87     return r;
88 }
89
90 void http_session_destroy(struct http_session *s)
91 {
92     struct http_session **p;
93
94     for (p = &session_list; *p; p = &(*p)->next)
95         if (*p == s)
96         {
97             *p = (*p)->next;
98             break;
99         }
100     yaz_log(YLOG_LOG, "Destroying session %u", s->session_id);
101     iochan_destroy(s->timeout_iochan);
102     destroy_session(s->psession);
103     nmem_destroy(s->nmem);
104 }
105
106 static const char *get_msg(enum pazpar2_error_code code)
107 {
108     struct pazpar2_error_msg {
109         enum pazpar2_error_code code;
110         const char *msg;
111     };
112     static const struct pazpar2_error_msg ar[] = {
113         { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
114         { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
115         { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
116         { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
117         { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
118         { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
119         { PAZPAR2_RECORD_MISSING, "Record missing"},
120         { PAZPAR2_NO_TARGETS, "No targets"},
121         { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
122         { 0, 0 }
123     };
124     int i = 0;
125     while (ar[i].msg)
126     {
127         if (code == ar[i].code)
128             return ar[i].msg;
129         i++;
130     }
131     return "No error";
132 }
133
134 static void error(struct http_response *rs, 
135                   enum pazpar2_error_code code,
136                   const char *addinfo)
137 {
138     struct http_channel *c = rs->channel;
139     char text[1024];
140     const char *http_status = "417";
141     const char *msg = get_msg(code);
142
143     rs->msg = nmem_strdup(c->nmem, msg);
144     strcpy(rs->code, http_status);
145
146     yaz_snprintf(text, sizeof(text),
147                  "<error code=\"%d\" msg=\"%s\">%s</error>", (int) code,
148                  msg, addinfo ? addinfo : "");
149
150     yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
151             msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
152     rs->payload = nmem_strdup(c->nmem, text);
153     http_send_response(c);
154 }
155
156 unsigned int make_sessionid()
157 {
158     static int seq = 0;
159     unsigned int res;
160
161     seq++;
162     if (global_parameters.debug_mode)
163         res = seq;
164     else
165     {
166         struct timeval t;
167
168         if (gettimeofday(&t, 0) < 0)
169         {
170             yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
171             exit(1);
172         }
173         /* at most 256 sessions per second .. 
174            (long long would be more appropriate)*/
175         res = t.tv_sec;
176         res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
177     }
178     return res;
179 }
180
181 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
182 {
183     struct http_session *p;
184     char *session = http_argbyname(rq, "session");
185     unsigned int id;
186
187     if (!session)
188     {
189         error(rs, PAZPAR2_MISSING_PARAMETER, "session");
190         return 0;
191     }
192     id = atoi(session);
193     for (p = session_list; p; p = p->next)
194         if (id == p->session_id)
195         {
196             iochan_activity(p->timeout_iochan);
197             return p;
198         }
199     error(rs, PAZPAR2_NO_SESSION, session);
200     return 0;
201 }
202
203 // Decode settings parameters and apply to session
204 // Syntax: setting[target]=value
205 static int process_settings(struct session *se, struct http_request *rq,
206         struct http_response *rs)
207 {
208     struct http_argument *a;
209
210     for (a = rq->arguments; a; a = a->next)
211         if (strchr(a->name, '['))
212         {
213             char **res;
214             int num;
215             char *dbname;
216             char *setting;
217
218             // Nmem_strsplit *rules*!!!
219             nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
220             if (num != 2)
221             {
222                 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
223                 return -1;
224             }
225             setting = res[0];
226             dbname = res[1];
227             session_apply_setting(se, dbname, setting,
228                     nmem_strdup(se->session_nmem, a->value));
229         }
230     return 0;
231 }
232
233 static void cmd_exit(struct http_channel *c)
234 {
235     yaz_log(YLOG_WARN, "exit");
236     exit(0);
237 }
238
239 static void cmd_init(struct http_channel *c)
240 {
241     unsigned int sesid;
242     char buf[1024];
243     struct http_session *s = http_session_create();
244     struct http_response *rs = c->response;
245
246     yaz_log(YLOG_DEBUG, "HTTP Session init");
247     sesid = make_sessionid();
248     s->session_id = sesid;
249     if (process_settings(s->psession, c->request, c->response) < 0)
250         return;
251     sprintf(buf, "<init><status>OK</status><session>%u</session>"
252             "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>", sesid);
253     rs->payload = nmem_strdup(c->nmem, buf);
254     http_send_response(c);
255 }
256
257 static void cmd_settings(struct http_channel *c)
258 {
259     struct http_response *rs = c->response;
260     struct http_request *rq = c->request;
261     struct http_session *s = locate_session(rq, rs);
262
263     if (!s)
264         return;
265
266     if (process_settings(s->psession, rq, rs) < 0)
267         return;
268     rs->payload = "<settings><status>OK</status></settings>";
269     http_send_response(c);
270 }
271
272 // Compares two hitsbytarget nodes by hitcount
273 static int cmp_ht(const void *p1, const void *p2)
274 {
275     const struct hitsbytarget *h1 = p1;
276     const struct hitsbytarget *h2 = p2;
277     return h2->hits - h1->hits;
278 }
279
280 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
281 static void targets_termlist(WRBUF wrbuf, struct session *se, int num)
282 {
283     struct hitsbytarget *ht;
284     int count, i;
285
286     if (!(ht = hitsbytarget(se, &count)))
287         return;
288     qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
289     for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
290     {
291
292         // do only print terms which have display names
293     
294         wrbuf_puts(wrbuf, "<term>\n");
295
296         wrbuf_puts(wrbuf, "<id>");
297         wrbuf_xmlputs(wrbuf, ht[i].id);
298         wrbuf_puts(wrbuf, "</id>\n");
299         
300         wrbuf_puts(wrbuf, "<name>");
301         if (!ht[i].name || !ht[i].name[0])
302             wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
303         else
304             wrbuf_xmlputs(wrbuf, ht[i].name);
305         wrbuf_puts(wrbuf, "</name>\n");
306         
307         wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
308         
309         wrbuf_puts(wrbuf, "<state>");
310         wrbuf_xmlputs(wrbuf, ht[i].state);
311         wrbuf_puts(wrbuf, "</state>\n");
312         
313         wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n", 
314                      ht[i].diagnostic);
315         wrbuf_puts(wrbuf, "</term>\n");
316     }
317 }
318
319 static void cmd_termlist(struct http_channel *c)
320 {
321     struct http_response *rs = c->response;
322     struct http_request *rq = c->request;
323     struct http_session *s = locate_session(rq, rs);
324     struct termlist_score **p;
325     int len;
326     int i;
327     char *name = http_argbyname(rq, "name");
328     char *nums = http_argbyname(rq, "num");
329     int num = 15;
330     int status;
331
332     if (!s)
333         return;
334
335     status = session_active_clients(s->psession);
336
337     if (!name)
338         name = "subject";
339     if (strlen(name) > 255)
340         return;
341     if (nums)
342         num = atoi(nums);
343
344     wrbuf_rewind(c->wrbuf);
345
346     wrbuf_puts(c->wrbuf, "<termlist>\n");
347     wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
348     while (*name)
349     {
350         char tname[256];
351         char *tp;
352
353         if (!(tp = strchr(name, ',')))
354             tp = name + strlen(name);
355         strncpy(tname, name, tp - name);
356         tname[tp - name] = '\0';
357
358         wrbuf_puts(c->wrbuf, "<list name=\"");
359         wrbuf_xmlputs(c->wrbuf, tname);
360         wrbuf_puts(c->wrbuf, "\">\n");
361         if (!strcmp(tname, "xtargets"))
362             targets_termlist(c->wrbuf, s->psession, num);
363         else
364         {
365             p = termlist(s->psession, tname, &len);
366             if (p)
367                 for (i = 0; i < len && i < num; i++){
368                     // prevnt sending empty term elements
369                     if (!p[i]->term || !p[i]->term[0])
370                         continue;
371
372                     wrbuf_puts(c->wrbuf, "<term>");
373                     wrbuf_puts(c->wrbuf, "<name>");
374                     wrbuf_xmlputs(c->wrbuf, p[i]->term);
375                     wrbuf_puts(c->wrbuf, "</name>");
376                         
377                     wrbuf_printf(c->wrbuf, 
378                                  "<frequency>%d</frequency>", 
379                                  p[i]->frequency);
380                     wrbuf_puts(c->wrbuf, "</term>\n");
381                }
382         }
383         wrbuf_puts(c->wrbuf, "</list>\n");
384         name = tp;
385         if (*name == ',')
386             name++;
387     }
388     wrbuf_puts(c->wrbuf, "</termlist>\n");
389     rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
390     http_send_response(c);
391 }
392
393
394 static void cmd_bytarget(struct http_channel *c)
395 {
396     struct http_response *rs = c->response;
397     struct http_request *rq = c->request;
398     struct http_session *s = locate_session(rq, rs);
399     struct hitsbytarget *ht;
400     int count, i;
401
402     if (!s)
403         return;
404     if (!(ht = hitsbytarget(s->psession, &count)))
405     {
406         error(rs, PAZPAR2_HITCOUNTS_FAILED, 0);
407         return;
408     }
409     wrbuf_rewind(c->wrbuf);
410     wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
411
412     for (i = 0; i < count; i++)
413     {
414         wrbuf_puts(c->wrbuf, "\n<target>");
415
416         wrbuf_puts(c->wrbuf, "<id>");
417         wrbuf_xmlputs(c->wrbuf, ht[i].id);
418         wrbuf_puts(c->wrbuf, "</id>\n");
419
420         wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
421         wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
422         wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
423
424         wrbuf_puts(c->wrbuf, "<state>");
425         wrbuf_xmlputs(c->wrbuf, ht[i].state);
426         wrbuf_puts(c->wrbuf, "</state>\n");
427
428         wrbuf_puts(c->wrbuf, "</target>");
429     }
430
431     wrbuf_puts(c->wrbuf, "</bytarget>");
432     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
433     http_send_response(c);
434 }
435
436 static void write_metadata(WRBUF w, struct conf_service *service,
437         struct record_metadata **ml, int full)
438 {
439     int imeta;
440
441     for (imeta = 0; imeta < service->num_metadata; imeta++)
442     {
443         struct conf_metadata *cmd = &service->metadata[imeta];
444         struct record_metadata *md;
445         if (!cmd->brief && !full)
446             continue;
447         for (md = ml[imeta]; md; md = md->next)
448         {
449             wrbuf_printf(w, "\n<md-%s>", cmd->name);
450
451             switch (cmd->type)
452             {
453                 case Metadata_type_generic:
454                     wrbuf_xmlputs(w, md->data.text);
455                     break;
456                 case Metadata_type_year:
457                     wrbuf_printf(w, "%d", md->data.number.min);
458                     if (md->data.number.min != md->data.number.max)
459                         wrbuf_printf(w, "-%d", md->data.number.max);
460                     break;
461                 default:
462                     wrbuf_puts(w, "[can't represent]");
463             }
464             wrbuf_printf(w, "</md-%s>", cmd->name);
465         }
466     }
467 }
468
469 static void write_subrecord(struct record *r, WRBUF w,
470         struct conf_service *service, int show_details)
471 {
472     char *name = session_setting_oneval(client_get_database(r->client), PZ_NAME);
473
474     wrbuf_puts(w, "<location id=\"");
475     wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
476     wrbuf_puts(w, "\" ");
477
478     wrbuf_puts(w, "name=\"");
479     wrbuf_xmlputs(w,  *name ? name : "Unknown");
480     wrbuf_puts(w, "\">");
481
482     if (show_details)
483         write_metadata(w, service, r->metadata, 1);
484     wrbuf_puts(w, "</location>\n");
485 }
486
487 static void cmd_record(struct http_channel *c)
488 {
489     struct http_response *rs = c->response;
490     struct http_request *rq = c->request;
491     struct http_session *s = locate_session(rq, rs);
492     struct record_cluster *rec;
493     struct record *r;
494     struct conf_service *service = global_parameters.server->service;
495     char *idstr = http_argbyname(rq, "id");
496     int id;
497
498     if (!s)
499         return;
500     if (!idstr)
501     {
502         error(rs, PAZPAR2_MISSING_PARAMETER, "id");
503         return;
504     }
505     wrbuf_rewind(c->wrbuf);
506     id = atoi(idstr);
507     if (!(rec = show_single(s->psession, id)))
508     {
509         error(rs, PAZPAR2_RECORD_MISSING, idstr);
510         return;
511     }
512     wrbuf_puts(c->wrbuf, "<record>\n");
513     wrbuf_printf(c->wrbuf, "<recid>%d</recid>\n", rec->recid);
514     write_metadata(c->wrbuf, service, rec->metadata, 1);
515     for (r = rec->records; r; r = r->next)
516         write_subrecord(r, c->wrbuf, service, 1);
517     wrbuf_puts(c->wrbuf, "</record>\n");
518     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
519     http_send_response(c);
520 }
521
522 static void show_records(struct http_channel *c, int active)
523 {
524     struct http_request *rq = c->request;
525     struct http_response *rs = c->response;
526     struct http_session *s = locate_session(rq, rs);
527     struct record_cluster **rl;
528     struct reclist_sortparms *sp;
529     char *start = http_argbyname(rq, "start");
530     char *num = http_argbyname(rq, "num");
531     char *sort = http_argbyname(rq, "sort");
532     int startn = 0;
533     int numn = 20;
534     int total;
535     int total_hits;
536     int i;
537
538     if (!s)
539         return;
540
541     // We haven't counted clients yet if we're called on a block release
542     if (active < 0)
543         active = session_active_clients(s->psession);
544
545     if (start)
546         startn = atoi(start);
547     if (num)
548         numn = atoi(num);
549     if (!sort)
550         sort = "relevance";
551     if (!(sp = reclist_parse_sortparms(c->nmem, sort)))
552     {
553         error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
554         return;
555     }
556
557     rl = show(s->psession, sp, startn, &numn, &total, &total_hits, c->nmem);
558
559     wrbuf_rewind(c->wrbuf);
560     wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
561     wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
562     wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
563     wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
564     wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
565     wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
566
567     for (i = 0; i < numn; i++)
568     {
569         int ccount;
570         struct record *p;
571         struct record_cluster *rec = rl[i];
572         struct conf_service *service = global_parameters.server->service;
573
574         wrbuf_puts(c->wrbuf, "<hit>\n");
575         write_metadata(c->wrbuf, service, rec->metadata, 0);
576         for (ccount = 0, p = rl[i]->records; p;  p = p->next, ccount++)
577             write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
578         if (ccount > 1)
579             wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
580         wrbuf_printf(c->wrbuf, "<recid>%d</recid>\n", rec->recid);
581         wrbuf_puts(c->wrbuf, "</hit>\n");
582     }
583
584     wrbuf_puts(c->wrbuf, "</show>\n");
585     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
586     http_send_response(c);
587 }
588
589 static void show_records_ready(void *data)
590 {
591     struct http_channel *c = (struct http_channel *) data;
592
593     show_records(c, -1);
594 }
595
596 static void cmd_show(struct http_channel *c)
597 {
598     struct http_request *rq = c->request;
599     struct http_response *rs = c->response;
600     struct http_session *s = locate_session(rq, rs);
601     char *block = http_argbyname(rq, "block");
602     int status;
603
604     if (!s)
605         return;
606
607     status = session_active_clients(s->psession);
608
609     if (block)
610     {
611         if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
612         {
613             session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
614             yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
615             return;
616         }
617     }
618
619     show_records(c, status);
620 }
621
622 static void cmd_ping(struct http_channel *c)
623 {
624     struct http_request *rq = c->request;
625     struct http_response *rs = c->response;
626     struct http_session *s = locate_session(rq, rs);
627     if (!s)
628         return;
629     rs->payload = "<ping><status>OK</status></ping>";
630     http_send_response(c);
631 }
632
633 static int utf_8_valid(const char *str)
634 {
635     yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
636     if (cd)
637     {
638         /* check that query is UTF-8 encoded */
639         char *inbuf = (char *) str; /* we know iconv does not alter this */
640         size_t inbytesleft = strlen(inbuf);
641
642         size_t outbytesleft = strlen(inbuf) + 10;
643         char *out = xmalloc(outbytesleft);
644         char *outbuf = out;
645         size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
646
647         /* if OK, try flushing the rest  */
648         if (r != (size_t) (-1))
649             r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
650         yaz_iconv_close(cd);
651         xfree(out);
652         if (r == (size_t) (-1))
653             return 0;
654     }
655     return 1;
656 }
657
658 static void cmd_search(struct http_channel *c)
659 {
660     struct http_request *rq = c->request;
661     struct http_response *rs = c->response;
662     struct http_session *s = locate_session(rq, rs);
663     char *query = http_argbyname(rq, "query");
664     char *filter = http_argbyname(rq, "filter");
665     enum pazpar2_error_code code;
666     const char *addinfo = 0;
667
668     if (!s)
669         return;
670     if (!query)
671     {
672         error(rs, PAZPAR2_MISSING_PARAMETER, "query");
673         return;
674     }
675     if (!utf_8_valid(query))
676     {
677         error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
678         return;
679     }
680     code = search(s->psession, query, filter, &addinfo);
681     if (code)
682     {
683         error(rs, code, addinfo);
684         return;
685     }
686     rs->payload = "<search><status>OK</status></search>";
687     http_send_response(c);
688 }
689
690
691 static void cmd_stat(struct http_channel *c)
692 {
693     struct http_request *rq = c->request;
694     struct http_response *rs = c->response;
695     struct http_session *s = locate_session(rq, rs);
696     struct statistics stat;
697     int clients;
698
699     if (!s)
700         return;
701
702     clients = session_active_clients(s->psession);
703     statistics(s->psession, &stat);
704
705     wrbuf_rewind(c->wrbuf);
706     wrbuf_puts(c->wrbuf, "<stat>");
707     wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
708     wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
709     wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
710     wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
711     wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
712     wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
713     wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
714     wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
715     wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
716     wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
717     wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
718     wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
719     wrbuf_puts(c->wrbuf, "</stat>");
720     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
721     http_send_response(c);
722 }
723
724 static void cmd_info(struct http_channel *c)
725 {
726     char yaz_version_str[20];
727     struct http_response *rs = c->response;
728
729     wrbuf_rewind(c->wrbuf);
730     wrbuf_puts(c->wrbuf, "<info>\n");
731     wrbuf_puts(c->wrbuf, " <version>\n");
732     wrbuf_puts(c->wrbuf, "<pazpar2>");
733     wrbuf_xmlputs(c->wrbuf, VERSION);
734     wrbuf_puts(c->wrbuf, "</pazpar2>");
735
736
737     yaz_version(yaz_version_str, 0);
738     wrbuf_puts(c->wrbuf, "  <yaz compiled=\"");
739     wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
740     wrbuf_puts(c->wrbuf, "\">");
741     wrbuf_xmlputs(c->wrbuf, yaz_version_str);
742     wrbuf_puts(c->wrbuf, "</yaz>\n");
743
744     wrbuf_puts(c->wrbuf, " </version>\n");
745     
746     wrbuf_puts(c->wrbuf, "</info>");
747     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
748     http_send_response(c);
749 }
750
751 struct {
752     char *name;
753     void (*fun)(struct http_channel *c);
754 } commands[] = {
755     { "init", cmd_init },
756     { "settings", cmd_settings },
757     { "stat", cmd_stat },
758     { "bytarget", cmd_bytarget },
759     { "show", cmd_show },
760     { "search", cmd_search },
761     { "termlist", cmd_termlist },
762     { "exit", cmd_exit },
763     { "ping", cmd_ping },
764     { "record", cmd_record },
765     { "info", cmd_info },
766     {0,0}
767 };
768
769 void http_command(struct http_channel *c)
770 {
771     char *command = http_argbyname(c->request, "command");
772     struct http_response *rs = http_create_response(c);
773     int i;
774
775     c->response = rs;
776
777     http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
778     http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
779
780     if (!command)
781     {
782         error(rs, PAZPAR2_MISSING_PARAMETER, "command");
783         return;
784     }
785     for (i = 0; commands[i].name; i++)
786         if (!strcmp(commands[i].name, command))
787         {
788             (*commands[i].fun)(c);
789             break;
790         }
791     if (!commands[i].name)
792         error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
793
794     return;
795 }
796
797 /*
798  * Local variables:
799  * c-basic-offset: 4
800  * indent-tabs-mode: nil
801  * End:
802  * vim: shiftwidth=4 tabstop=8 expandtab
803  */