Lookup version and parse it on to perform_termlist
[pazpar2-moved-to-github.git] / src / http_command.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2012 Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <stdio.h>
24 #include <sys/types.h>
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #include <stdlib.h>
29 #include <string.h>
30 #if HAVE_SYS_TIME_H
31 #include <sys/time.h>
32 #endif
33 #include <yaz/snprintf.h>
34 #include <yaz/yaz-util.h>
35
36 #include "ppmutex.h"
37 #include "eventl.h"
38 #include "parameters.h"
39 #include "session.h"
40 #include "http.h"
41 #include "settings.h"
42 #include "client.h"
43
44 #ifdef HAVE_MALLINFO
45 #include <malloc.h>
46
47 void print_meminfo(WRBUF wrbuf)
48 {
49     struct mallinfo minfo;
50     minfo = mallinfo();
51     wrbuf_printf(wrbuf, "  <memory>\n"
52                         "   <arena>%d</arena>\n" 
53                         "   <uordblks>%d</uordblks>\n"
54                         "   <fordblks>%d</fordblks>\n"
55                         "   <ordblks>%d</ordblks>\n"
56                         "   <keepcost>%d</keepcost>\n"
57                         "   <hblks>%d</hblks>\n" 
58                         "   <hblkhd>%d</hblkhd>\n"
59                         "   <virt>%d</virt>\n"
60                         "   <virtuse>%d</virtuse>\n"
61                         "  </memory>\n", 
62                  minfo.arena, minfo.uordblks, minfo.fordblks,minfo.ordblks, minfo.keepcost, minfo.hblks, minfo.hblkhd, minfo.arena + minfo.hblkhd, minfo.uordblks + minfo.hblkhd);
63
64 }
65 #else
66 #define print_meminfo(x)
67 #endif
68
69
70 // Update this when the protocol changes
71 #define PAZPAR2_PROTOCOL_VERSION "1"
72
73 #define HTTP_COMMAND_RESPONSE_PREFIX "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
74
75 struct http_session {
76     IOCHAN timeout_iochan;     // NOTE: This is NOT associated with a socket
77     struct session *psession;
78     unsigned int session_id;
79     int timestamp;
80     int destroy_counter;
81     int activity_counter;
82     NMEM nmem;
83     http_sessions_t http_sessions;
84     struct http_session *next;
85 };
86
87 struct http_sessions {
88     struct http_session *session_list;
89     YAZ_MUTEX mutex;
90     int log_level;
91 };
92
93 static YAZ_MUTEX g_http_session_mutex = 0;
94 static int g_http_sessions = 0;
95
96 int http_session_use(int delta)
97 {
98     int sessions;
99     if (!g_http_session_mutex)
100         yaz_mutex_create(&g_http_session_mutex);
101     yaz_mutex_enter(g_http_session_mutex);
102     g_http_sessions += delta;
103     sessions = g_http_sessions;
104     yaz_mutex_leave(g_http_session_mutex);
105     yaz_log(YLOG_DEBUG, "%s sessions=%d", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), sessions);
106     return sessions;
107
108 }
109
110 http_sessions_t http_sessions_create(void)
111 {
112     http_sessions_t hs = xmalloc(sizeof(*hs));
113     hs->session_list = 0;
114     hs->mutex = 0;
115     pazpar2_mutex_create(&hs->mutex, "http_sessions");
116     hs->log_level = yaz_log_module_level("HTTP");
117     return hs;
118 }
119
120 void http_sessions_destroy(http_sessions_t hs)
121 {
122     if (hs)
123     {
124         struct http_session *s = hs->session_list;
125         while (s)
126         {
127             struct http_session *s_next = s->next;
128             iochan_destroy(s->timeout_iochan);
129             session_destroy(s->psession);
130             nmem_destroy(s->nmem);
131             s = s_next;
132         }
133         yaz_mutex_destroy(&hs->mutex);
134         xfree(hs);
135     }
136 }
137
138 void http_session_destroy(struct http_session *s);
139
140 static void session_timeout(IOCHAN i, int event)
141 {
142     struct http_session *s = iochan_getdata(i);
143     http_session_destroy(s);
144 }
145
146 struct http_session *http_session_create(struct conf_service *service,
147                                          http_sessions_t http_sessions,
148                                          unsigned int sesid)
149 {
150     NMEM nmem = nmem_create();
151     struct http_session *r = nmem_malloc(nmem, sizeof(*r));
152     char tmp_str[50];
153
154     sprintf(tmp_str, "session#%u", sesid);
155     r->psession = new_session(nmem, service, sesid);
156     r->session_id = sesid;
157     r->timestamp = 0;
158     r->nmem = nmem;
159     r->destroy_counter = r->activity_counter = 0;
160     r->http_sessions = http_sessions;
161
162     yaz_mutex_enter(http_sessions->mutex);
163     r->next = http_sessions->session_list;
164     http_sessions->session_list = r;
165     yaz_mutex_leave(http_sessions->mutex);
166
167     r->timeout_iochan = iochan_create(-1, session_timeout, 0, "http_session_timeout");
168     iochan_setdata(r->timeout_iochan, r);
169     yaz_log(http_sessions->log_level, "Session %u created. timeout chan=%p timeout=%d", sesid, r->timeout_iochan, service->session_timeout);
170     iochan_settimeout(r->timeout_iochan, service->session_timeout);
171
172     iochan_add(service->server->iochan_man, r->timeout_iochan);
173     http_session_use(1);
174     return r;
175 }
176
177 void http_session_destroy(struct http_session *s)
178 {
179     int must_destroy = 0;
180
181     http_sessions_t http_sessions = s->http_sessions;
182
183     yaz_log(http_sessions->log_level, "Session %u destroy", s->session_id);
184     yaz_mutex_enter(http_sessions->mutex);
185     /* only if http_session has no active http sessions on it can be destroyed */
186     if (s->destroy_counter == s->activity_counter)
187     {
188         struct http_session **p = 0;
189         must_destroy = 1;
190         for (p = &http_sessions->session_list; *p; p = &(*p)->next)
191             if (*p == s)
192             {
193                 *p = (*p)->next;
194                 break;
195             }
196     }
197     yaz_mutex_leave(http_sessions->mutex);
198     if (must_destroy)
199     {   /* destroying for real */
200         yaz_log(http_sessions->log_level, "Session %u destroyed", s->session_id);
201         iochan_destroy(s->timeout_iochan);
202         session_destroy(s->psession);
203         http_session_use(-1);
204         nmem_destroy(s->nmem);
205     }
206     else {
207         yaz_log(http_sessions->log_level, "Session %u destroying delayed. Active clients (%d-%d). Waiting for new timeout.",
208                 s->session_id, s->activity_counter, s->destroy_counter);
209     }
210
211 }
212
213 static const char *get_msg(enum pazpar2_error_code code)
214 {
215     struct pazpar2_error_msg {
216         enum pazpar2_error_code code;
217         const char *msg;
218     };
219     static const struct pazpar2_error_msg ar[] = {
220         { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
221         { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
222         { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
223         { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
224         { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
225         { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
226         { PAZPAR2_RECORD_MISSING, "Record missing"},
227         { PAZPAR2_NO_TARGETS, "No targets"},
228         { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
229         { PAZPAR2_RECORD_FAIL, "Record command failed"},
230         { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"},
231         { PAZPAR2_NO_SERVICE, "No service"},
232         { PAZPAR2_ALREADY_BLOCKED, "Already blocked in session on: "},
233         { PAZPAR2_LAST_ERROR, "Last error"},
234         { 0, 0 }
235     };
236     int i = 0;
237     while (ar[i].msg)
238     {
239         if (code == ar[i].code)
240             return ar[i].msg;
241         i++;
242     }
243     return "No error";
244 }
245
246 static void error(struct http_response *rs, 
247                   enum pazpar2_error_code code,
248                   const char *addinfo)
249 {
250     struct http_channel *c = rs->channel;
251     WRBUF text = wrbuf_alloc();
252     const char *http_status = "417";
253     const char *msg = get_msg(code);
254     
255     rs->msg = nmem_strdup(c->nmem, msg);
256     strcpy(rs->code, http_status);
257
258     wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code,
259                msg);
260     if (addinfo)
261         wrbuf_xmlputs(text, addinfo);
262     wrbuf_puts(text, "</error>");
263
264     yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
265             msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
266     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
267     wrbuf_destroy(text);
268     http_send_response(c);
269 }
270
271 static void response_open_no_status(struct http_channel *c, const char *command)
272 {
273     wrbuf_rewind(c->wrbuf);
274     wrbuf_printf(c->wrbuf, "%s<%s>",
275                  HTTP_COMMAND_RESPONSE_PREFIX, command);
276 }
277
278 static void response_open(struct http_channel *c, const char *command)
279 {
280     response_open_no_status(c, command);
281     wrbuf_puts(c->wrbuf, "<status>OK</status>");
282 }
283
284 static void response_close(struct http_channel *c, const char *command)
285 {
286     struct http_response *rs = c->response;
287
288     wrbuf_printf(c->wrbuf, "</%s>", command);
289     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
290     http_send_response(c);
291 }
292
293 unsigned int make_sessionid(void)
294 {
295     static int seq = 0; /* thread pr */
296     unsigned int res;
297
298     seq++;
299     if (global_parameters.predictable_sessions)
300         res = seq;
301     else
302     {
303 #ifdef WIN32
304         res = seq;
305 #else
306         struct timeval t;
307
308         if (gettimeofday(&t, 0) < 0)
309         {
310             yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
311             exit(1);
312         }
313         /* at most 256 sessions per second .. 
314            (long long would be more appropriate)*/
315         res = t.tv_sec;
316         res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
317 #endif
318     }
319     return res;
320 }
321
322 static struct http_session *locate_session(struct http_channel *c)
323 {
324     struct http_request *rq = c->request;
325     struct http_response *rs = c->response;
326     struct http_session *p;
327     const char *session = http_argbyname(rq, "session");
328     http_sessions_t http_sessions = c->http_sessions;
329     unsigned int id;
330
331     if (!session)
332     {
333         error(rs, PAZPAR2_MISSING_PARAMETER, "session");
334         return 0;
335     }
336     id = atoi(session);
337     yaz_mutex_enter(http_sessions->mutex);
338     for (p = http_sessions->session_list; p; p = p->next)
339         if (id == p->session_id)
340             break;
341     if (p)
342         p->activity_counter++;
343     yaz_mutex_leave(http_sessions->mutex);
344     if (p)
345         iochan_activity(p->timeout_iochan);
346     else
347         error(rs, PAZPAR2_NO_SESSION, session);
348     return p;
349 }
350
351 // Call after use of locate_session, in order to increment the destroy_counter
352 static void release_session(struct http_channel *c,
353                             struct http_session *session)
354 {
355     http_sessions_t http_sessions = c->http_sessions;
356     yaz_mutex_enter(http_sessions->mutex);
357     if (session)
358         session->destroy_counter++;
359     yaz_mutex_leave(http_sessions->mutex);
360 }
361
362 // Decode settings parameters and apply to session
363 // Syntax: setting[target]=value
364 static int process_settings(struct session *se, struct http_request *rq,
365                             struct http_response *rs)
366 {
367     struct http_argument *a;
368
369     for (a = rq->arguments; a; a = a->next)
370         if (strchr(a->name, '['))
371         {
372             char **res;
373             int num;
374             char *dbname;
375             char *setting;
376
377             // Nmem_strsplit *rules*!!!
378             nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
379             if (num != 2)
380             {
381                 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
382                 return -1;
383             }
384             setting = res[0];
385             dbname = res[1];
386             session_apply_setting(se, dbname, setting,
387                     nmem_strdup(se->session_nmem, a->value));
388         }
389     return 0;
390 }
391
392 static void cmd_exit(struct http_channel *c)
393 {
394     yaz_log(YLOG_WARN, "exit");
395
396     response_open(c, "exit");
397     response_close(c, "exit");
398     http_close_server(c->server);
399 }
400
401 static void cmd_init(struct http_channel *c)
402 {
403     struct http_request *r = c->request;
404     const char *clear = http_argbyname(r, "clear");
405     const char *content_type = http_lookup_header(r->headers, "Content-Type");
406     unsigned int sesid;
407     struct http_session *s;
408     struct http_response *rs = c->response;
409     struct conf_service *service = 0; /* no service (yet) */
410     
411     if (r->content_len && content_type && 
412         !yaz_strcmp_del("text/xml", content_type, "; "))
413     {
414         xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
415         xmlNode *root_n;
416         if (!doc)
417         {
418             error(rs, PAZPAR2_MALFORMED_SETTING, 0);
419             return;
420         }
421         root_n = xmlDocGetRootElement(doc);
422         service = service_create(c->server, root_n);
423         xmlFreeDoc(doc);
424         if (!service)
425         {
426             error(rs, PAZPAR2_MALFORMED_SETTING, 0);
427             return;
428         }
429     }
430     
431     if (!service)
432     {
433         const char *service_name = http_argbyname(c->request, "service");
434         service = locate_service(c->server, service_name);
435         if (!service)
436         {
437             error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
438             return;
439         }
440     }
441     sesid = make_sessionid();
442     s = http_session_create(service, c->http_sessions, sesid);
443     
444     yaz_log(c->http_sessions->log_level, "Session init %u ", sesid);
445     if (!clear || *clear == '0')
446         session_init_databases(s->psession);
447     else
448         yaz_log(YLOG_LOG, "Session %u init: No databases preloaded", sesid);
449     
450     if (process_settings(s->psession, c->request, c->response) < 0)
451         return;
452
453     response_open(c, "init");
454     wrbuf_printf(c->wrbuf, "<session>%d", sesid);
455     if (c->server->server_id)
456     {
457         wrbuf_puts(c->wrbuf, ".");
458         wrbuf_puts(c->wrbuf, c->server->server_id);
459     }
460     wrbuf_puts(c->wrbuf, "</session>"
461                "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol>");
462     
463     response_close(c, "init");
464 }
465
466 static void apply_local_setting(void *client_data,
467                                 struct setting *set)
468 {
469     struct session *se =  (struct session *) client_data;
470
471     session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
472                           nmem_strdup(se->session_nmem, set->name),
473                           nmem_strdup(se->session_nmem, set->value));
474 }
475
476 static void cmd_settings(struct http_channel *c)
477 {
478     struct http_response *rs = c->response;
479     struct http_request *rq = c->request;
480     struct http_session *s = locate_session(c);
481     const char *content_type = http_lookup_header(rq->headers, "Content-Type");
482
483     if (!s)
484         return;
485
486     if (rq->content_len && content_type && 
487         !yaz_strcmp_del("text/xml", content_type, "; "))
488     {
489         xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
490         xmlNode *root_n;
491         if (!doc)
492         {
493             error(rs, PAZPAR2_MALFORMED_SETTING, 0);
494             release_session(c,s);
495             return;
496         }
497         root_n = xmlDocGetRootElement(doc);
498
499         settings_read_node_x(root_n, s->psession, apply_local_setting);
500
501         xmlFreeDoc(doc);
502     }
503     if (process_settings(s->psession, rq, rs) < 0)
504     {
505         release_session(c, s);
506         return;
507     }
508     response_open(c, "settings");
509     response_close(c, "settings");
510     release_session(c, s);
511 }
512
513 static void termlist_response(struct http_channel *c, struct http_session *s, const char *cmd_status)
514 {
515     struct http_request *rq = c->request;
516     const char *name    = http_argbyname(rq, "name");
517     const char *nums    = http_argbyname(rq, "num");
518     const char *version = http_argbyname(rq, "version");
519     int version_no = 0;
520     if (version && strcmp(version, "")) {
521         version_no = atoi(version);
522     }
523     int num = 15;
524     int status;
525
526     if (nums)
527         num = atoi(nums);
528
529     status = session_active_clients(s->psession);
530
531     response_open_no_status(c, "termlist");
532     /* new protocol add a status to response. Triggered by a status parameter */
533     if (cmd_status != 0) {
534         wrbuf_printf(c->wrbuf, "<status>%s</status>\n", cmd_status);
535     }
536     wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
537
538     perform_termlist(c, s->psession, name, num, version_no);
539
540     response_close(c, "termlist");
541 }
542
543 static void termlist_result_ready(void *data)
544 {
545     struct http_channel *c = (struct http_channel *) data;
546     struct http_request *rq = c->request;
547     const char *report = http_argbyname(rq, "report");
548     const char *status = 0;
549     struct http_session *s = locate_session(c);
550     if (report && !strcmp("status", report))
551         status = "OK";
552     if (s) {
553         yaz_log(c->http_sessions->log_level, "Session %u termlist watch released", s->session_id);
554         termlist_response(c, s, status);
555         release_session(c,s);
556     }
557 }
558
559 static void cmd_termlist(struct http_channel *c)
560 {
561     struct http_request *rq = c->request;
562     struct http_response *rs = c->response;
563     struct http_session *s = locate_session(c);
564     const char *block = http_argbyname(rq, "block");
565     const char *report = http_argbyname(rq, "report");
566     int report_status = 0;
567     int report_error = 0;
568     const char *status_message = 0;
569     int active_clients;
570     if (report  && !strcmp("error", report)) {
571         report_error = 1;
572         status_message = "OK";
573     }
574     if (report  && !strcmp("status", report)) {
575         report_status = 1;
576         status_message = "OK";
577     }
578     if (!s)
579         return;
580
581     active_clients = session_active_clients(s->psession);
582     if (block && !strcmp("1", block) && active_clients)
583     {
584         // if there is already a watch/block. we do not block this one
585         if (session_set_watch(s->psession, SESSION_WATCH_TERMLIST,
586                               termlist_result_ready, c, c) != 0)
587         {
588             yaz_log(YLOG_WARN, "Session %u: Attempt to block multiple times on termlist block. Not supported!", s->session_id);
589             if (report_error) {
590                 error(rs, PAZPAR2_ALREADY_BLOCKED, "termlist");
591                 release_session(c, s);
592                 return;
593             }
594             else if (report_status) {
595                 status_message = "WARNING (Already blocked on termlist)";
596             }
597             else {
598                 yaz_log(YLOG_WARN, "Session %u: Ignoring termlist block. Return current result", s->session_id);
599             }
600         }
601         else
602         {
603             yaz_log(c->http_sessions->log_level, "Session %u: Blocking on command termlist", s->session_id);
604             release_session(c, s);
605             return;
606         }
607     }
608
609     termlist_response(c, s, status_message);
610     release_session(c, s);
611 }
612
613 size_t session_get_memory_status(struct session *session);
614
615 static void session_status(struct http_channel *c, struct http_session *s)
616 {
617     size_t session_nmem;
618     wrbuf_printf(c->wrbuf, "<http_count>%u</http_count>\n", s->activity_counter);
619     wrbuf_printf(c->wrbuf, "<http_nmem>%zu</http_nmem>\n", nmem_total(s->nmem) );
620     session_nmem = session_get_memory_status(s->psession);
621     wrbuf_printf(c->wrbuf, "<session_nmem>%zu</session_nmem>\n", session_nmem);
622 }
623
624 static void cmd_session_status(struct http_channel *c)
625 {
626     struct http_session *s = locate_session(c);
627     if (!s)
628         return;
629
630     response_open(c, "session-status");
631     session_status(c, s);
632     response_close(c, "session-status");
633     release_session(c, s);
634 }
635
636 int sessions_count(void);
637 int clients_count(void);
638 #ifdef HAVE_RESULTSETS_COUNT
639 int resultsets_count(void);
640 #else
641 #define resultsets_count()      0
642 #endif
643
644 static void cmd_server_status(struct http_channel *c)
645 {
646     int sessions   = sessions_count();
647     int clients    = clients_count();
648     int resultsets = resultsets_count();
649
650     response_open(c, "server-status");
651     wrbuf_printf(c->wrbuf, "\n  <sessions>%u</sessions>\n", sessions);
652     wrbuf_printf(c->wrbuf, "  <clients>%u</clients>\n",   clients);
653     /* Only works if yaz has been compiled with enabling of this */
654     wrbuf_printf(c->wrbuf, "  <resultsets>%u</resultsets>\n",resultsets);
655     print_meminfo(c->wrbuf);
656
657 /* TODO add all sessions status                         */
658 /*    http_sessions_t http_sessions = c->http_sessions; */
659 /*    struct http_session *p;                           */
660 /*
661     yaz_mutex_enter(http_sessions->mutex);
662     for (p = http_sessions->session_list; p; p = p->next)
663     {
664         p->activity_counter++;
665         wrbuf_puts(c->wrbuf, "<session-status>\n");
666         wrbuf_printf(c->wrbuf, "<id>%s</id>\n", p->session_id);
667         yaz_mutex_leave(http_sessions->mutex);
668         session_status(c, p);
669         wrbuf_puts(c->wrbuf, "</session-status>\n");
670         yaz_mutex_enter(http_sessions->mutex);
671         p->activity_counter--;
672     }
673     yaz_mutex_leave(http_sessions->mutex);
674 */
675     response_close(c, "server-status");
676     xmalloc_trav(0);
677 }
678
679 static void bytarget_response(struct http_channel *c, struct http_session *s, const char *cmd_status) {
680     int count, i;
681     struct hitsbytarget *ht;
682     struct http_request *rq = c->request;
683     const char *settings = http_argbyname(rq, "settings");
684     const char *protocol_version = http_argbyname(rq, "version");
685     int version = 0;
686     if (protocol_version && strcmp(protocol_version,"")) {
687         version = atoi(protocol_version);
688     }
689     ht = get_hitsbytarget(s->psession, &count, c->nmem);
690     if (!cmd_status)
691         /* Old protocol, always ok */
692         response_open(c, "bytarget");
693     else {
694         /* New protocol, OK or WARNING (...)*/
695         response_open_no_status(c, "bytarget");
696         wrbuf_printf(c->wrbuf, "<status>%s</status>", cmd_status);
697     }
698
699     if (count == 0)
700         yaz_log(YLOG_WARN, "Empty bytarget Response. No targets found!");
701     for (i = 0; i < count; i++)
702     {
703         wrbuf_puts(c->wrbuf, "\n<target>");
704
705         wrbuf_puts(c->wrbuf, "<id>");
706         wrbuf_xmlputs(c->wrbuf, ht[i].id);
707         wrbuf_puts(c->wrbuf, "</id>\n");
708
709         if (ht[i].name && ht[i].name[0]) 
710         {
711             wrbuf_puts(c->wrbuf, "<name>");
712             wrbuf_xmlputs(c->wrbuf, ht[i].name);
713             wrbuf_puts(c->wrbuf, "</name>\n");
714         }
715
716         wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
717         wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
718         if (ht[i].diagnostic)
719         {
720             wrbuf_puts(c->wrbuf, "<addinfo>");
721             if (ht[i].addinfo)
722                 wrbuf_xmlputs(c->wrbuf, ht[i].addinfo);
723             wrbuf_puts(c->wrbuf, "</addinfo>\n");
724         }
725
726         wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records - ht[i].filtered);
727         if (version >= 2)
728             wrbuf_printf(c->wrbuf, "<filtered>%d</filtered>\n", ht[i].filtered);
729         wrbuf_puts(c->wrbuf, "<state>");
730         wrbuf_xmlputs(c->wrbuf, ht[i].state);
731         wrbuf_puts(c->wrbuf, "</state>\n");
732         if (settings && *settings == '1')
733         {
734             wrbuf_puts(c->wrbuf, "<settings>\n");
735             wrbuf_puts(c->wrbuf, ht[i].settings_xml);
736             wrbuf_puts(c->wrbuf, "</settings>\n");
737         }
738         if (ht[i].suggestions_xml && ht[i].suggestions_xml[0]) {
739             wrbuf_puts(c->wrbuf, "<suggestions>");
740             wrbuf_puts(c->wrbuf, ht[i].suggestions_xml);
741             wrbuf_puts(c->wrbuf, "</suggestions>");
742         }
743         wrbuf_puts(c->wrbuf, "</target>");
744     }
745     response_close(c, "bytarget");
746 }
747
748 static void bytarget_result_ready(void *data)
749 {
750     struct http_channel *c = (struct http_channel *) data;
751     struct http_session *s = locate_session(c);
752     const char *status_message = "OK";
753     if (s) {
754         yaz_log(c->http_sessions->log_level, "Session %u: bytarget watch released", s->session_id);
755         bytarget_response(c, s, status_message);
756         release_session(c, s);
757     }
758     else {
759         yaz_log(c->http_sessions->log_level, "No Session found for released bytarget watch");
760     }
761 }
762
763
764 static void cmd_bytarget(struct http_channel *c)
765 {
766     struct http_request *rq = c->request;
767     struct http_response *rs = c->response;
768     struct http_session *s = locate_session(c);
769     const char *block = http_argbyname(rq, "block");
770     const char *report = http_argbyname(rq, "report");
771     int report_error = 0;
772     int report_status = 0;
773     const char *status_message = "OK";
774     int no_active;
775
776     if (report && !strcmp("error", report)) {
777         report_error = 1;
778     }
779     if (report && !strcmp("status", report)) {
780         report_status = 1;
781     }
782
783     if (!s)
784         return;
785
786     no_active = session_active_clients(s->psession);
787     if (block && !strcmp("1",block) && no_active)
788     {
789         // if there is already a watch/block. we do not block this one
790         if (session_set_watch(s->psession, SESSION_WATCH_BYTARGET,
791                               bytarget_result_ready, c, c) != 0)
792         {
793             yaz_log(YLOG_WARN, "Session %u: Attempt to block multiple times on bytarget block. Not supported!", s->session_id);
794             if (report_error) {
795                 error(rs, PAZPAR2_ALREADY_BLOCKED, "bytarget");
796                 release_session(c, s);
797                 return;
798             }
799             else if (report_status) {
800                 status_message = "WARNING (Already blocked on bytarget)";
801             }
802             else {
803                 yaz_log(YLOG_WARN, "Session %u: Ignoring bytarget block. Return current result.", s->session_id);
804             }
805         }
806         else
807         {
808             yaz_log(c->http_sessions->log_level, "Session %u: Blocking on command bytarget", s->session_id);
809             release_session(c, s);
810             return;
811         }
812     }
813     bytarget_response(c, s, status_message);
814     release_session(c, s);
815 }
816
817 static void write_metadata(WRBUF w, struct conf_service *service,
818         struct record_metadata **ml, int full)
819 {
820     int imeta;
821
822     for (imeta = 0; imeta < service->num_metadata; imeta++)
823     {
824         struct conf_metadata *cmd = &service->metadata[imeta];
825         struct record_metadata *md;
826         if (!cmd->brief && !full)
827             continue;
828         for (md = ml[imeta]; md; md = md->next)
829         {
830             struct record_metadata_attr *attr = md->attributes;
831             wrbuf_printf(w, "\n<md-%s", cmd->name);
832
833             for (; attr; attr = attr->next)
834             {
835                 wrbuf_printf(w, " %s=\"", attr->name);
836                 wrbuf_xmlputs(w, attr->value);
837                 wrbuf_puts(w, "\"");
838             }
839             wrbuf_puts(w, ">");
840             switch (cmd->type)
841             {
842                 case Metadata_type_generic:
843                     wrbuf_xmlputs(w, md->data.text.disp);
844                     break;
845                 case Metadata_type_year:
846                     wrbuf_printf(w, "%d", md->data.number.min);
847                     if (md->data.number.min != md->data.number.max)
848                         wrbuf_printf(w, "-%d", md->data.number.max);
849                     break;
850                 default:
851                     wrbuf_puts(w, "[can't represent]");
852                     break;
853             }
854             wrbuf_printf(w, "</md-%s>", cmd->name);
855         }
856     }
857 }
858
859 static void write_subrecord(struct record *r, WRBUF w,
860         struct conf_service *service, int show_details)
861 {
862     const char *name = session_setting_oneval(
863         client_get_database(r->client), PZ_NAME);
864
865     wrbuf_puts(w, "<location id=\"");
866     wrbuf_xmlputs(w, client_get_id(r->client));
867     wrbuf_puts(w, "\" ");
868
869     wrbuf_puts(w, "name=\"");
870     wrbuf_xmlputs(w,  *name ? name : "Unknown");
871     wrbuf_puts(w, "\">");
872
873     write_metadata(w, service, r->metadata, show_details);
874     wrbuf_puts(w, "</location>\n");
875 }
876
877 static void show_raw_record_error(void *data, const char *addinfo)
878 {
879     http_channel_observer_t obs = data;
880     struct http_channel *c = http_channel_observer_chan(obs);
881     struct http_response *rs = c->response;
882
883     http_remove_observer(obs);
884
885     error(rs, PAZPAR2_RECORD_FAIL, addinfo);
886 }
887
888 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
889 {
890     http_channel_observer_t obs = data;
891     struct http_channel *c = http_channel_observer_chan(obs);
892     struct http_response *rs = c->response;
893
894     http_remove_observer(obs);
895
896     wrbuf_write(c->wrbuf, buf, sz);
897     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
898     http_send_response(c);
899 }
900
901
902 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
903 {
904     http_channel_observer_t obs = data;
905     struct http_channel *c = http_channel_observer_chan(obs);
906     struct http_response *rs = c->response;
907
908     http_remove_observer(obs);
909
910     wrbuf_write(c->wrbuf, buf, sz);
911     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
912
913     rs->content_type = "application/octet-stream";
914     http_send_response(c);
915 }
916
917
918 void show_raw_reset(void *data, struct http_channel *c, void *data2)
919 {
920     //struct client *client = data;
921     //client_show_raw_remove(client, data2);
922 }
923
924 static void cmd_record_ready(void *data);
925
926 static void show_record(struct http_channel *c, struct http_session *s)
927 {
928     struct http_response *rs = c->response;
929     struct http_request *rq = c->request;
930     struct record_cluster *rec, *prev_r, *next_r;
931     struct record *r;
932     struct conf_service *service;
933     const char *idstr = http_argbyname(rq, "id");
934     const char *offsetstr = http_argbyname(rq, "offset");
935     const char *binarystr = http_argbyname(rq, "binary");
936     
937     if (!s)
938         return;
939     service = s->psession->service;
940     if (!idstr)
941     {
942         error(rs, PAZPAR2_MISSING_PARAMETER, "id");
943         return;
944     }
945     wrbuf_rewind(c->wrbuf);
946     if (!(rec = show_single_start(s->psession, idstr, &prev_r, &next_r)))
947     {
948         if (session_active_clients(s->psession) == 0)
949         {
950             error(rs, PAZPAR2_RECORD_MISSING, idstr);
951         }
952         else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
953                               cmd_record_ready, c, c) != 0)
954         {
955             error(rs, PAZPAR2_RECORD_MISSING, idstr);
956         }
957         return;
958     }
959     if (offsetstr)
960     {
961         int offset = atoi(offsetstr);
962         const char *syntax = http_argbyname(rq, "syntax");
963         const char *esn = http_argbyname(rq, "esn");
964         int i;
965         struct record*r = rec->records;
966         int binary = 0;
967         const char *nativesyntax = http_argbyname(rq, "nativesyntax");
968         
969         if (binarystr && *binarystr != '0')
970             binary = 1;
971
972         for (i = 0; i < offset && r; r = r->next, i++)
973             ;
974         if (!r)
975         {
976             error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
977         }
978         else
979         {
980             http_channel_observer_t obs =
981                 http_add_observer(c, r->client, show_raw_reset);
982             int ret = client_show_raw_begin(r->client, r->position,
983                                             syntax, esn,
984                                             obs /* data */,
985                                             show_raw_record_error,
986                                             (binary ? 
987                                              show_raw_record_ok_binary : 
988                                              show_raw_record_ok),
989                                             (binary ? 1 : 0),
990                                             nativesyntax);
991             if (ret == -1)
992             {
993                 http_remove_observer(obs);
994                 error(rs, PAZPAR2_NO_SESSION, 0);
995             }
996         }
997     }
998     else
999     {
1000         response_open_no_status(c, "record");
1001         wrbuf_puts(c->wrbuf, "\n<recid>");
1002         wrbuf_xmlputs(c->wrbuf, rec->recid);
1003         wrbuf_puts(c->wrbuf, "</recid>\n");
1004         if (prev_r)
1005         {
1006             wrbuf_puts(c->wrbuf, "<prevrecid>");
1007             wrbuf_xmlputs(c->wrbuf, prev_r->recid);
1008             wrbuf_puts(c->wrbuf, "</prevrecid>\n");
1009         }
1010         if (next_r)
1011         {
1012             wrbuf_puts(c->wrbuf, "<nextrecid>");
1013             wrbuf_xmlputs(c->wrbuf, next_r->recid);
1014             wrbuf_puts(c->wrbuf, "</nextrecid>\n");
1015         }
1016         wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", 
1017                      session_active_clients(s->psession));
1018         write_metadata(c->wrbuf, service, rec->metadata, 1);
1019         for (r = rec->records; r; r = r->next)
1020             write_subrecord(r, c->wrbuf, service, 1);
1021         response_close(c, "record");
1022     }
1023     show_single_stop(s->psession, rec);
1024 }
1025
1026 static void cmd_record_ready(void *data)
1027 {
1028     struct http_channel *c = (struct http_channel *) data;
1029     struct http_session *s = locate_session(c);
1030     if (s) {
1031         yaz_log(c->http_sessions->log_level, "Session %u: record watch released", s->session_id);
1032         show_record(c, s);
1033         release_session(c,s);
1034     }
1035 }
1036
1037 static void cmd_record(struct http_channel *c)
1038 {
1039     struct http_session *s = locate_session(c);
1040     if (s) {
1041         show_record(c, s);
1042         release_session(c,s);
1043     }
1044 }
1045
1046
1047 static void show_records(struct http_channel *c, struct http_session *s, int active)
1048 {
1049     struct http_request *rq = c->request;
1050     struct http_response *rs = c->response;
1051     struct record_cluster **rl;
1052     struct reclist_sortparms *sp;
1053     const char *start = http_argbyname(rq, "start");
1054     const char *num = http_argbyname(rq, "num");
1055     const char *sort = http_argbyname(rq, "sort");
1056     int startn = 0;
1057     int numn = 20;
1058     int total;
1059     Odr_int total_hits;
1060     int i;
1061
1062     if (!s)
1063         return;
1064
1065     // We haven't counted clients yet if we're called on a block release
1066     if (active < 0)
1067         active = session_active_clients(s->psession);
1068
1069     if (start)
1070         startn = atoi(start);
1071     if (num)
1072         numn = atoi(num);
1073     if (!sort)
1074         sort = "relevance";
1075     if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
1076     {
1077         error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
1078         return;
1079
1080     }
1081     
1082     rl = show_range_start(s->psession, sp, startn, &numn, &total, &total_hits);
1083
1084     response_open(c, "show");
1085     wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>\n", active);
1086     wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
1087     wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
1088     wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
1089     wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
1090
1091     for (i = 0; i < numn; i++)
1092     {
1093         int ccount;
1094         struct record *p;
1095         struct record_cluster *rec = rl[i];
1096         struct conf_service *service = s->psession->service;
1097
1098         wrbuf_puts(c->wrbuf, "<hit>\n");
1099         write_metadata(c->wrbuf, service, rec->metadata, 0);
1100         for (ccount = 0, p = rl[i]->records; p;  p = p->next, ccount++)
1101             write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
1102         if (ccount > 1)
1103             wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
1104         if (strstr(sort, "relevance"))
1105             wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n",
1106                          rec->relevance_score);
1107         wrbuf_puts(c->wrbuf, "<recid>");
1108         wrbuf_xmlputs(c->wrbuf, rec->recid);
1109         wrbuf_puts(c->wrbuf, "</recid>\n");
1110         wrbuf_puts(c->wrbuf, "</hit>\n");
1111     }
1112
1113     show_range_stop(s->psession, rl);
1114
1115     response_close(c, "show");
1116 }
1117
1118 static void show_records_ready(void *data)
1119 {
1120     struct http_channel *c = (struct http_channel *) data;
1121     struct http_session *s = locate_session(c);
1122     if (s) {
1123         yaz_log(c->http_sessions->log_level, "Session %u: show watch released", s->session_id);
1124         show_records(c, s, -1);
1125     }
1126     else {
1127         /* some error message  */
1128     }
1129     release_session(c,s);
1130 }
1131
1132 static void cmd_show(struct http_channel *c)
1133 {
1134     struct http_request  *rq = c->request;
1135     struct http_response *rs = c->response;
1136     struct http_session *s = locate_session(c);
1137     const char *block = http_argbyname(rq, "block");
1138     const char *sort = http_argbyname(rq, "sort");
1139     const char *block_error = http_argbyname(rq, "report");
1140     struct reclist_sortparms *sp;
1141     int status;
1142     int report_error = 0;
1143     if (block_error && !strcmp("1", block_error)) {
1144         report_error = 1;
1145     }    
1146     if (!s)
1147         return;
1148
1149     if (!sort)
1150         sort = "relevance";
1151     
1152     if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
1153     {
1154         error(c->response, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
1155         release_session(c, s);
1156         return;
1157     }
1158     session_sort(s->psession, sp->name, sp->increasing);
1159
1160     status = session_active_clients(s->psession);
1161
1162     if (block)
1163     {
1164         if (!strcmp(block, "preferred") && !session_is_preferred_clients_ready(s->psession) && reclist_get_num_records(s->psession->reclist) == 0)
1165         {
1166             // if there is already a watch/block. we do not block this one
1167             if (session_set_watch(s->psession, SESSION_WATCH_SHOW_PREF,
1168                                   show_records_ready, c, c) == 0)
1169             {
1170                 yaz_log(c->http_sessions->log_level,
1171                         "Session %u: Blocking on command show (preferred targets)", s->session_id);
1172                 release_session(c, s);
1173                 return;
1174             }
1175             else
1176             {
1177                 yaz_log(YLOG_WARN, "Session %u: Attempt to block multiple times on show (preferred targets) block. Not supported!", 
1178                     s->session_id);
1179                 if (report_error) {
1180                     error(rs, PAZPAR2_ALREADY_BLOCKED, "show (preferred targets)");
1181                     release_session(c, s);
1182                     return;
1183                 }
1184                 else {
1185                     yaz_log(YLOG_WARN, "Session %u: Ignoring show(preferred) block. Returning current result.", s->session_id);
1186                 }
1187             }
1188
1189         }
1190         else if (status)
1191         {
1192             // if there is already a watch/block. we do not block this one
1193             if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
1194                                   show_records_ready, c, c) != 0)
1195             {
1196                 yaz_log(YLOG_WARN, "Session %u: Attempt to block multiple times on show block. Not supported!", s->session_id);
1197                 if (report_error) {
1198                     error(rs, PAZPAR2_ALREADY_BLOCKED, "show");
1199                     release_session(c, s);
1200                     return;
1201                 }
1202                 else {
1203                     yaz_log(YLOG_WARN, "Session %u: Ignoring show block. Returning current result.", s->session_id);
1204                 }
1205             }
1206             else
1207             {
1208                 yaz_log(c->http_sessions->log_level, "Session %u: Blocking on command show", s->session_id);
1209                 release_session(c, s);
1210                 return;
1211             }
1212         }
1213     }
1214     show_records(c, s, status);
1215     release_session(c, s);
1216 }
1217
1218 static void cmd_ping(struct http_channel *c)
1219 {
1220     struct http_session *s = locate_session(c);
1221     if (!s)
1222         return;
1223     response_open(c, "ping");
1224     response_close(c, "ping");
1225     release_session(c, s);
1226 }
1227
1228 static void cmd_search(struct http_channel *c)
1229 {
1230     struct http_request *rq = c->request;
1231     struct http_response *rs = c->response;
1232     struct http_session *s = locate_session(c);
1233     const char *query = http_argbyname(rq, "query");
1234     const char *filter = http_argbyname(rq, "filter");
1235     const char *maxrecs = http_argbyname(rq, "maxrecs");
1236     const char *startrecs = http_argbyname(rq, "startrecs");
1237     const char *limit = http_argbyname(rq, "limit");
1238     enum pazpar2_error_code code;
1239     const char *addinfo = 0;
1240
1241     if (!s)
1242         return;
1243     if (!query)
1244     {
1245         error(rs, PAZPAR2_MISSING_PARAMETER, "query");
1246         release_session(c, s);
1247         return;
1248     }
1249     if (!yaz_utf8_check(query))
1250     {
1251         error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
1252         release_session(c, s);
1253         return;
1254     }
1255     code = session_search(s->psession, query, startrecs, maxrecs, filter, limit,
1256                           &addinfo, "relevance", 0);
1257     if (code)
1258     {
1259         error(rs, code, addinfo);
1260         release_session(c, s);
1261         return;
1262     }
1263     response_open(c, "search");
1264     response_close(c, "search");
1265     release_session(c, s);
1266 }
1267
1268
1269 static void cmd_stat(struct http_channel *c)
1270 {
1271     struct http_session *s = locate_session(c);
1272     struct statistics stat;
1273     int clients;
1274
1275     float progress = 0;
1276
1277     if (!s)
1278         return;
1279
1280     clients = session_active_clients(s->psession);
1281     statistics(s->psession, &stat);
1282
1283     if (stat.num_clients > 0)
1284     {
1285         progress = (stat.num_clients  - clients) / (float)stat.num_clients;
1286     }
1287
1288     response_open_no_status(c, "stat");
1289     wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
1290     wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
1291     wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
1292     wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
1293     wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
1294     wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
1295     wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
1296     wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
1297     wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
1298     wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
1299     wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
1300     response_close(c, "stat");
1301     release_session(c, s);
1302 }
1303
1304 static void cmd_info(struct http_channel *c)
1305 {
1306     char yaz_version_str[20];
1307
1308     response_open_no_status(c, "info");
1309     wrbuf_puts(c->wrbuf, " <version>\n");
1310     wrbuf_puts(c->wrbuf, "  <pazpar2");
1311 #ifdef PAZPAR2_VERSION_SHA1
1312     wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
1313 #endif
1314     wrbuf_puts(c->wrbuf, ">");
1315     wrbuf_xmlputs(c->wrbuf, VERSION);
1316     wrbuf_puts(c->wrbuf, "</pazpar2>");
1317
1318     yaz_version(yaz_version_str, 0);
1319     wrbuf_puts(c->wrbuf, "  <yaz compiled=\"");
1320     wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
1321     wrbuf_puts(c->wrbuf, "\">");
1322     wrbuf_xmlputs(c->wrbuf, yaz_version_str);
1323     wrbuf_puts(c->wrbuf, "</yaz>\n");
1324
1325     wrbuf_puts(c->wrbuf, " </version>\n");
1326     
1327     info_services(c->server, c->wrbuf);
1328
1329     response_close(c, "info");
1330 }
1331
1332 struct {
1333     char *name;
1334     void (*fun)(struct http_channel *c);
1335 } commands[] = {
1336     { "init", cmd_init },
1337     { "settings", cmd_settings },
1338     { "stat", cmd_stat },
1339     { "bytarget", cmd_bytarget },
1340     { "show", cmd_show },
1341     { "search", cmd_search },
1342     { "termlist", cmd_termlist },
1343     { "exit", cmd_exit },
1344     { "session-status", cmd_session_status },
1345     { "server-status", cmd_server_status },
1346     { "ping", cmd_ping },
1347     { "record", cmd_record },
1348     { "info", cmd_info },
1349     {0,0}
1350 };
1351
1352 void http_command(struct http_channel *c)
1353 {
1354     const char *command = http_argbyname(c->request, "command");
1355     struct http_response *rs = http_create_response(c);
1356     int i;
1357
1358     c->response = rs;
1359
1360     http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1361     http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1362
1363     if (!command)
1364     {
1365         error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1366         return;
1367     }
1368     for (i = 0; commands[i].name; i++)
1369         if (!strcmp(commands[i].name, command))
1370         {
1371             (*commands[i].fun)(c);
1372             break;
1373         }
1374     if (!commands[i].name)
1375         error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1376
1377     return;
1378 }
1379
1380 /*
1381  * Local variables:
1382  * c-basic-offset: 4
1383  * c-file-style: "Stroustrup"
1384  * indent-tabs-mode: nil
1385  * End:
1386  * vim: shiftwidth=4 tabstop=8 expandtab
1387  */
1388