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