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