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