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