3d4df77037953a6ba8c784330313d51a54267ded
[pazpar2-moved-to-github.git] / src / http_command.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2010 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 "eventl.h"
37 #include "parameters.h"
38 #include "session.h"
39 #include "http.h"
40 #include "settings.h"
41 #include "client.h"
42
43 // Update this when the protocol changes
44 #define PAZPAR2_PROTOCOL_VERSION "1"
45
46 #define HTTP_COMMAND_RESPONSE_PREFIX "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
47
48 struct http_session {
49     IOCHAN timeout_iochan;     // NOTE: This is NOT associated with a socket
50     struct session *psession;
51     unsigned int session_id;
52     int timestamp;
53     int destroy_counter;
54     int activity_counter;
55     NMEM nmem;
56     http_sessions_t http_sessions;
57     struct http_session *next;
58 };
59
60 struct http_sessions {
61     struct http_session *session_list;
62     YAZ_MUTEX mutex;
63 };
64
65 http_sessions_t http_sessions_create(void)
66 {
67     http_sessions_t hs = xmalloc(sizeof(*hs));
68     hs->session_list = 0;
69     hs->mutex = 0;
70     yaz_mutex_create(&hs->mutex);
71     return hs;
72 }
73
74 void http_sessions_destroy(http_sessions_t hs)
75 {
76     if (hs)
77     {
78         struct http_session *s = hs->session_list;
79         while (s)
80         {
81             struct http_session *s_next = s->next;
82             iochan_destroy(s->timeout_iochan);
83             destroy_session(s->psession);
84             nmem_destroy(s->nmem);
85             s = s_next;
86         }
87         yaz_mutex_destroy(&hs->mutex);
88         xfree(hs);
89     }
90 }
91
92 void http_session_destroy(struct http_session *s);
93
94 static void session_timeout(IOCHAN i, int event)
95 {
96     struct http_session *s = iochan_getdata(i);
97     http_session_destroy(s);
98 }
99
100 struct http_session *http_session_create(struct conf_service *service,
101                                          http_sessions_t http_sessions)
102 {
103     NMEM nmem = nmem_create();
104     struct http_session *r = nmem_malloc(nmem, sizeof(*r));
105
106     r->psession = new_session(nmem, service);
107     r->session_id = 0;
108     r->timestamp = 0;
109     r->nmem = nmem;
110     r->destroy_counter = r->activity_counter = 0;
111     r->http_sessions = http_sessions;
112
113     yaz_mutex_enter(http_sessions->mutex);
114     r->next = http_sessions->session_list;
115     http_sessions->session_list = r;
116     yaz_mutex_leave(http_sessions->mutex);
117
118     r->timeout_iochan = iochan_create(-1, session_timeout, 0, "http_session_timeout");
119     iochan_setdata(r->timeout_iochan, r);
120     yaz_log(YLOG_LOG, "timeout=%d", service->session_timeout);
121     iochan_settimeout(r->timeout_iochan, service->session_timeout);
122
123     iochan_add(service->server->iochan_man, r->timeout_iochan);
124     return r;
125 }
126
127 void http_session_destroy(struct http_session *s)
128 {
129     int must_destroy = 1;
130
131     http_sessions_t http_sessions = s->http_sessions;
132
133     yaz_log(YLOG_LOG, "http_session_destroy %u", s->session_id);
134     yaz_mutex_enter(http_sessions->mutex);
135
136     /* only if http_session destroy was already called, we will allow it
137        to be destroyed */
138     if (s->destroy_counter != s->activity_counter)
139         must_destroy = 0;
140
141     /* only if there are no active Z39.50 clients we will allow it to be
142        destroyed */
143     if (session_active_clients(s->psession))
144         must_destroy = 0;
145
146     s->destroy_counter = s->activity_counter = 0;
147     if (must_destroy)
148     {
149         struct http_session **p = 0;
150         for (p = &http_sessions->session_list; *p; p = &(*p)->next)
151             if (*p == s)
152             {
153                 *p = (*p)->next;
154                 break;
155             }
156     }
157     yaz_mutex_leave(http_sessions->mutex);
158     if (must_destroy)
159     {   /* destroying for real */
160         yaz_log(YLOG_LOG, "Destroying session %u", s->session_id);
161         iochan_destroy(s->timeout_iochan);
162         destroy_session(s->psession);
163         nmem_destroy(s->nmem);
164     }
165 }
166
167 static const char *get_msg(enum pazpar2_error_code code)
168 {
169     struct pazpar2_error_msg {
170         enum pazpar2_error_code code;
171         const char *msg;
172     };
173     static const struct pazpar2_error_msg ar[] = {
174         { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
175         { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
176         { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
177         { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
178         { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
179         { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
180         { PAZPAR2_RECORD_MISSING, "Record missing"},
181         { PAZPAR2_NO_TARGETS, "No targets"},
182         { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
183         { PAZPAR2_RECORD_FAIL, "Record command failed"},
184         { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"},
185         { PAZPAR2_NO_SERVICE, "No service"},
186         { PAZPAR2_LAST_ERROR, "Last error"},
187         { 0, 0 }
188     };
189     int i = 0;
190     while (ar[i].msg)
191     {
192         if (code == ar[i].code)
193             return ar[i].msg;
194         i++;
195     }
196     return "No error";
197 }
198
199 static void error(struct http_response *rs, 
200                   enum pazpar2_error_code code,
201                   const char *addinfo)
202 {
203     struct http_channel *c = rs->channel;
204     WRBUF text = wrbuf_alloc();
205     const char *http_status = "417";
206     const char *msg = get_msg(code);
207     
208     rs->msg = nmem_strdup(c->nmem, msg);
209     strcpy(rs->code, http_status);
210
211     wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code,
212                msg);
213     if (addinfo)
214         wrbuf_xmlputs(text, addinfo);
215     wrbuf_puts(text, "</error>");
216
217     yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
218             msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
219     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
220     wrbuf_destroy(text);
221     http_send_response(c);
222 }
223
224 unsigned int make_sessionid(void)
225 {
226     static int seq = 0; /* thread pr */
227     unsigned int res;
228
229     seq++;
230     if (global_parameters.debug_mode)
231         res = seq;
232     else
233     {
234 #ifdef WIN32
235         res = seq;
236 #else
237         struct timeval t;
238
239         if (gettimeofday(&t, 0) < 0)
240         {
241             yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
242             exit(1);
243         }
244         /* at most 256 sessions per second .. 
245            (long long would be more appropriate)*/
246         res = t.tv_sec;
247         res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
248 #endif
249     }
250     return res;
251 }
252
253 static struct http_session *locate_session(struct http_channel *c)
254 {
255     struct http_response *rs = c->response;
256     struct http_request *rq = c->request;
257     struct http_session *p;
258     const char *session = http_argbyname(rq, "session");
259     http_sessions_t http_sessions = c->http_sessions;
260     unsigned int id;
261
262     if (!session)
263     {
264         error(rs, PAZPAR2_MISSING_PARAMETER, "session");
265         return 0;
266     }
267     id = atoi(session);
268     yaz_mutex_enter(http_sessions->mutex);
269     for (p = http_sessions->session_list; p; p = p->next)
270         if (id == p->session_id)
271             break;
272     if (p)
273         p->activity_counter++;
274     yaz_mutex_leave(http_sessions->mutex);
275     if (p)
276         iochan_activity(p->timeout_iochan);
277     else
278         error(rs, PAZPAR2_NO_SESSION, session);
279     return p;
280 }
281
282 // Decode settings parameters and apply to session
283 // Syntax: setting[target]=value
284 static int process_settings(struct session *se, struct http_request *rq,
285                             struct http_response *rs)
286 {
287     struct http_argument *a;
288
289     for (a = rq->arguments; a; a = a->next)
290         if (strchr(a->name, '['))
291         {
292             char **res;
293             int num;
294             char *dbname;
295             char *setting;
296
297             // Nmem_strsplit *rules*!!!
298             nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
299             if (num != 2)
300             {
301                 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
302                 return -1;
303             }
304             setting = res[0];
305             dbname = res[1];
306             session_apply_setting(se, dbname, setting,
307                     nmem_strdup(se->session_nmem, a->value));
308         }
309     return 0;
310 }
311
312 static void cmd_exit(struct http_channel *c)
313 {
314     yaz_log(YLOG_WARN, "exit");
315     http_close_server(c->server);
316 }
317
318 static void cmd_init(struct http_channel *c)
319 {
320     char buf[1024];
321     struct http_request *r = c->request;
322     const char *clear = http_argbyname(r, "clear");
323     const char *content_type = http_lookup_header(r->headers, "Content-Type");
324     unsigned int sesid;
325     struct http_session *s;
326     struct http_response *rs = c->response;
327     struct conf_service *service = 0; /* no service (yet) */
328     
329     if (r->content_len && content_type && 
330         !yaz_strcmp_del("text/xml", content_type, "; "))
331     {
332         xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
333         xmlNode *root_n;
334         if (!doc)
335         {
336             error(rs, PAZPAR2_MALFORMED_SETTING, 0);
337             return;
338         }
339         root_n = xmlDocGetRootElement(doc);
340         service = service_create(c->server, root_n);
341         xmlFreeDoc(doc);
342         if (!service)
343         {
344             error(rs, PAZPAR2_MALFORMED_SETTING, 0);
345             return;
346         }
347     }
348     
349     if (!service)
350     {
351         const char *service_name = http_argbyname(c->request, "service");
352         service = locate_service(c->server, service_name);
353         if (!service)
354         {
355             error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
356             return;
357         }
358     }
359     s = http_session_create(service, c->http_sessions);
360     
361     yaz_log(YLOG_DEBUG, "HTTP Session init");
362     if (!clear || *clear == '0')
363         session_init_databases(s->psession);
364     else
365         yaz_log(YLOG_LOG, "No databases preloaded");
366     
367     sesid = make_sessionid();
368     s->session_id = sesid;
369     if (process_settings(s->psession, c->request, c->response) < 0)
370         return;
371     
372     sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX 
373             "<init><status>OK</status><session>%u", sesid);
374     if (c->server->server_id)
375     {
376         strcat(buf, ".");
377         strcat(buf, c->server->server_id);
378     }
379     strcat(buf, "</session>"
380            "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>");
381     rs->payload = nmem_strdup(c->nmem, buf);
382     http_send_response(c);
383 }
384
385 static void apply_local_setting(void *client_data,
386                                 struct setting *set)
387 {
388     struct session *se =  (struct session *) client_data;
389
390     session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
391                           nmem_strdup(se->session_nmem, set->name),
392                           nmem_strdup(se->session_nmem, set->value));
393 }
394
395 static void cmd_settings(struct http_channel *c)
396 {
397     struct http_response *rs = c->response;
398     struct http_request *rq = c->request;
399     struct http_session *s = locate_session(c);
400     const char *content_type = http_lookup_header(rq->headers, "Content-Type");
401
402     if (!s)
403         return;
404
405     if (rq->content_len && content_type && 
406         !yaz_strcmp_del("text/xml", content_type, "; "))
407     {
408         xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
409         xmlNode *root_n;
410         if (!doc)
411         {
412             error(rs, PAZPAR2_MALFORMED_SETTING, 0);
413             return;
414         }
415         root_n = xmlDocGetRootElement(doc);
416
417         settings_read_node_x(root_n, s->psession, apply_local_setting);
418
419         xmlFreeDoc(doc);
420     }
421     if (process_settings(s->psession, rq, rs) < 0)
422         return;
423     rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<settings><status>OK</status></settings>";
424     http_send_response(c);
425 }
426
427 // Compares two hitsbytarget nodes by hitcount
428 static int cmp_ht(const void *p1, const void *p2)
429 {
430     const struct hitsbytarget *h1 = p1;
431     const struct hitsbytarget *h2 = p2;
432     return h2->hits - h1->hits;
433 }
434
435 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
436 static void targets_termlist(WRBUF wrbuf, struct session *se, int num,
437                              NMEM nmem)
438 {
439     struct hitsbytarget *ht;
440     int count, i;
441
442     ht = hitsbytarget(se, &count, nmem);
443     qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
444     for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
445     {
446
447         // do only print terms which have display names
448     
449         wrbuf_puts(wrbuf, "<term>\n");
450
451         wrbuf_puts(wrbuf, "<id>");
452         wrbuf_xmlputs(wrbuf, ht[i].id);
453         wrbuf_puts(wrbuf, "</id>\n");
454         
455         wrbuf_puts(wrbuf, "<name>");
456         if (!ht[i].name || !ht[i].name[0])
457             wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
458         else
459             wrbuf_xmlputs(wrbuf, ht[i].name);
460         wrbuf_puts(wrbuf, "</name>\n");
461         
462         wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
463                      ht[i].hits);
464         
465         wrbuf_puts(wrbuf, "<state>");
466         wrbuf_xmlputs(wrbuf, ht[i].state);
467         wrbuf_puts(wrbuf, "</state>\n");
468         
469         wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n", 
470                      ht[i].diagnostic);
471         wrbuf_puts(wrbuf, "</term>\n");
472     }
473 }
474
475 static void cmd_termlist(struct http_channel *c)
476 {
477     struct http_response *rs = c->response;
478     struct http_request *rq = c->request;
479     struct http_session *s = locate_session(c);
480     struct termlist_score **p;
481     int len;
482     int i;
483     const char *name = http_argbyname(rq, "name");
484     const char *nums = http_argbyname(rq, "num");
485     int num = 15;
486     int status;
487
488     if (!s)
489         return;
490
491     status = session_active_clients(s->psession);
492
493     if (!name)
494         name = "subject";
495     if (strlen(name) > 255)
496         return;
497     if (nums)
498         num = atoi(nums);
499
500     wrbuf_rewind(c->wrbuf);
501
502     wrbuf_puts(c->wrbuf, "<termlist>\n");
503     wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
504     while (*name)
505     {
506         char tname[256];
507         const char *tp;
508
509         if (!(tp = strchr(name, ',')))
510             tp = name + strlen(name);
511         strncpy(tname, name, tp - name);
512         tname[tp - name] = '\0';
513
514         wrbuf_puts(c->wrbuf, "<list name=\"");
515         wrbuf_xmlputs(c->wrbuf, tname);
516         wrbuf_puts(c->wrbuf, "\">\n");
517         if (!strcmp(tname, "xtargets"))
518             targets_termlist(c->wrbuf, s->psession, num, c->nmem);
519         else
520         {
521             p = termlist(s->psession, tname, &len);
522             if (p)
523                 for (i = 0; i < len && i < num; i++){
524                     // prevnt sending empty term elements
525                     if (!p[i]->term || !p[i]->term[0])
526                         continue;
527
528                     wrbuf_puts(c->wrbuf, "<term>");
529                     wrbuf_puts(c->wrbuf, "<name>");
530                     wrbuf_xmlputs(c->wrbuf, p[i]->term);
531                     wrbuf_puts(c->wrbuf, "</name>");
532                         
533                     wrbuf_printf(c->wrbuf, 
534                                  "<frequency>%d</frequency>", 
535                                  p[i]->frequency);
536                     wrbuf_puts(c->wrbuf, "</term>\n");
537                }
538         }
539         wrbuf_puts(c->wrbuf, "</list>\n");
540         name = tp;
541         if (*name == ',')
542             name++;
543     }
544     wrbuf_puts(c->wrbuf, "</termlist>\n");
545     rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
546     http_send_response(c);
547 }
548
549
550 static void cmd_bytarget(struct http_channel *c)
551 {
552     struct http_response *rs = c->response;
553     struct http_request *rq = c->request;
554     struct http_session *s = locate_session(c);
555     struct hitsbytarget *ht;
556     const char *settings = http_argbyname(rq, "settings");
557     int count, i;
558
559     if (!s)
560         return;
561     ht = hitsbytarget(s->psession, &count, c->nmem);
562     wrbuf_rewind(c->wrbuf);
563     wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<bytarget><status>OK</status>");
564
565     for (i = 0; i < count; i++)
566     {
567         wrbuf_puts(c->wrbuf, "\n<target>");
568
569         wrbuf_puts(c->wrbuf, "<id>");
570         wrbuf_xmlputs(c->wrbuf, ht[i].id);
571         wrbuf_puts(c->wrbuf, "</id>\n");
572
573         if (ht[i].name && ht[i].name[0]) 
574         {
575             wrbuf_puts(c->wrbuf, "<name>");
576             wrbuf_xmlputs(c->wrbuf, ht[i].name);
577             wrbuf_puts(c->wrbuf, "</name>\n");
578         }
579
580         wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
581         wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
582         wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
583
584         wrbuf_puts(c->wrbuf, "<state>");
585         wrbuf_xmlputs(c->wrbuf, ht[i].state);
586         wrbuf_puts(c->wrbuf, "</state>\n");
587         if (settings && *settings == '1')
588         {
589             wrbuf_puts(c->wrbuf, "<settings>\n");
590             wrbuf_puts(c->wrbuf, wrbuf_cstr(ht[i].settings_xml));
591             wrbuf_puts(c->wrbuf, "</settings>\n");
592         }
593         wrbuf_puts(c->wrbuf, "</target>");
594         wrbuf_destroy(ht[i].settings_xml);
595     }
596
597     wrbuf_puts(c->wrbuf, "</bytarget>");
598     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
599     http_send_response(c);
600 }
601
602 static void write_metadata(WRBUF w, struct conf_service *service,
603         struct record_metadata **ml, int full)
604 {
605     int imeta;
606
607     for (imeta = 0; imeta < service->num_metadata; imeta++)
608     {
609         struct conf_metadata *cmd = &service->metadata[imeta];
610         struct record_metadata *md;
611         if (!cmd->brief && !full)
612             continue;
613         for (md = ml[imeta]; md; md = md->next)
614         {
615             struct record_metadata_attr *attr = md->attributes;
616             wrbuf_printf(w, "\n<md-%s", cmd->name);
617
618             for (; attr; attr = attr->next)
619             {
620                 wrbuf_printf(w, " %s=\"", attr->name);
621                 wrbuf_xmlputs(w, attr->value);
622                 wrbuf_puts(w, "\"");
623             }
624             wrbuf_puts(w, ">");
625             switch (cmd->type)
626             {
627                 case Metadata_type_generic:
628                     wrbuf_xmlputs(w, md->data.text.disp);
629                     break;
630                 case Metadata_type_year:
631                     wrbuf_printf(w, "%d", md->data.number.min);
632                     if (md->data.number.min != md->data.number.max)
633                         wrbuf_printf(w, "-%d", md->data.number.max);
634                     break;
635                 default:
636                     wrbuf_puts(w, "[can't represent]");
637             }
638             wrbuf_printf(w, "</md-%s>", cmd->name);
639         }
640     }
641 }
642
643 static void write_subrecord(struct record *r, WRBUF w,
644         struct conf_service *service, int show_details)
645 {
646     const char *name = session_setting_oneval(
647         client_get_database(r->client), PZ_NAME);
648
649     wrbuf_puts(w, "<location id=\"");
650     wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
651     wrbuf_puts(w, "\" ");
652
653     wrbuf_puts(w, "name=\"");
654     wrbuf_xmlputs(w,  *name ? name : "Unknown");
655     wrbuf_puts(w, "\">");
656
657     write_metadata(w, service, r->metadata, show_details);
658     wrbuf_puts(w, "</location>\n");
659 }
660
661 static void show_raw_record_error(void *data, const char *addinfo)
662 {
663     http_channel_observer_t obs = data;
664     struct http_channel *c = http_channel_observer_chan(obs);
665     struct http_response *rs = c->response;
666
667     http_remove_observer(obs);
668
669     error(rs, PAZPAR2_RECORD_FAIL, addinfo);
670 }
671
672 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
673 {
674     http_channel_observer_t obs = data;
675     struct http_channel *c = http_channel_observer_chan(obs);
676     struct http_response *rs = c->response;
677
678     http_remove_observer(obs);
679
680     wrbuf_write(c->wrbuf, buf, sz);
681     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
682     http_send_response(c);
683 }
684
685
686 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
687 {
688     http_channel_observer_t obs = data;
689     struct http_channel *c = http_channel_observer_chan(obs);
690     struct http_response *rs = c->response;
691
692     http_remove_observer(obs);
693
694     wrbuf_write(c->wrbuf, buf, sz);
695     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
696
697     rs->content_type = "application/octet-stream";
698     http_send_response(c);
699 }
700
701
702 void show_raw_reset(void *data, struct http_channel *c, void *data2)
703 {
704     //struct client *client = data;
705     //client_show_raw_remove(client, data2);
706 }
707
708 static void cmd_record_ready(void *data);
709
710 static void cmd_record(struct http_channel *c)
711 {
712     struct http_response *rs = c->response;
713     struct http_request *rq = c->request;
714     struct http_session *s = locate_session(c);
715     struct record_cluster *rec, *prev_r, *next_r;
716     struct record *r;
717     struct conf_service *service;
718     const char *idstr = http_argbyname(rq, "id");
719     const char *offsetstr = http_argbyname(rq, "offset");
720     const char *binarystr = http_argbyname(rq, "binary");
721     
722     if (!s)
723         return;
724     service = s->psession->service;
725     if (!idstr)
726     {
727         error(rs, PAZPAR2_MISSING_PARAMETER, "id");
728         return;
729     }
730     wrbuf_rewind(c->wrbuf);
731     if (!(rec = show_single_start(s->psession, idstr, &prev_r, &next_r)))
732     {
733         if (session_active_clients(s->psession) == 0)
734         {
735             error(rs, PAZPAR2_RECORD_MISSING, idstr);
736         }
737         else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
738                               cmd_record_ready, c, c) != 0)
739         {
740             error(rs, PAZPAR2_RECORD_MISSING, idstr);
741         }
742         return;
743     }
744     if (offsetstr)
745     {
746         int offset = atoi(offsetstr);
747         const char *syntax = http_argbyname(rq, "syntax");
748         const char *esn = http_argbyname(rq, "esn");
749         int i;
750         struct record*r = rec->records;
751         int binary = 0;
752
753         if (binarystr && *binarystr != '0')
754             binary = 1;
755
756         for (i = 0; i < offset && r; r = r->next, i++)
757             ;
758         if (!r)
759         {
760             error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
761         }
762         else
763         {
764             http_channel_observer_t obs =
765                 http_add_observer(c, r->client, show_raw_reset);
766             int ret = client_show_raw_begin(r->client, r->position,
767                                         syntax, esn, 
768                                         obs /* data */,
769                                         show_raw_record_error,
770                                         (binary ? 
771                                          show_raw_record_ok_binary : 
772                                          show_raw_record_ok),
773                                         (binary ? 1 : 0));
774             if (ret == -1)
775             {
776                 http_remove_observer(obs);
777                 error(rs, PAZPAR2_NO_SESSION, 0);
778             }
779         }
780     }
781     else
782     {
783         wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<record>\n");
784         wrbuf_puts(c->wrbuf, "<recid>");
785         wrbuf_xmlputs(c->wrbuf, rec->recid);
786         wrbuf_puts(c->wrbuf, "</recid>\n");
787         if (prev_r)
788         {
789             wrbuf_puts(c->wrbuf, "<prevrecid>");
790             wrbuf_xmlputs(c->wrbuf, prev_r->recid);
791             wrbuf_puts(c->wrbuf, "</prevrecid>\n");
792         }
793         if (next_r)
794         {
795             wrbuf_puts(c->wrbuf, "<nextrecid>");
796             wrbuf_xmlputs(c->wrbuf, next_r->recid);
797             wrbuf_puts(c->wrbuf, "</nextrecid>\n");
798         }
799         wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", 
800                      session_active_clients(s->psession));
801         write_metadata(c->wrbuf, service, rec->metadata, 1);
802         for (r = rec->records; r; r = r->next)
803             write_subrecord(r, c->wrbuf, service, 1);
804         wrbuf_puts(c->wrbuf, "</record>\n");
805         rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
806         http_send_response(c);
807     }
808     show_single_stop(s->psession, rec);
809 }
810
811 static void cmd_record_ready(void *data)
812 {
813     struct http_channel *c = (struct http_channel *) data;
814
815     cmd_record(c);
816 }
817
818 static void show_records(struct http_channel *c, int active)
819 {
820     struct http_request *rq = c->request;
821     struct http_response *rs = c->response;
822     struct http_session *s = locate_session(c);
823     struct record_cluster **rl;
824     struct reclist_sortparms *sp;
825     const char *start = http_argbyname(rq, "start");
826     const char *num = http_argbyname(rq, "num");
827     const char *sort = http_argbyname(rq, "sort");
828     int startn = 0;
829     int numn = 20;
830     int total;
831     Odr_int total_hits;
832     int i;
833
834     if (!s)
835         return;
836
837     // We haven't counted clients yet if we're called on a block release
838     if (active < 0)
839         active = session_active_clients(s->psession);
840
841     if (start)
842         startn = atoi(start);
843     if (num)
844         numn = atoi(num);
845     if (!sort)
846         sort = "relevance";
847     if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
848     {
849         error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
850         return;
851     }
852
853     
854     rl = show_range_start(s->psession, sp, startn, &numn, &total, &total_hits);
855
856     wrbuf_rewind(c->wrbuf);
857     wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<show>\n<status>OK</status>\n");
858     wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
859     wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
860     wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
861     wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
862     wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
863
864     for (i = 0; i < numn; i++)
865     {
866         int ccount;
867         struct record *p;
868         struct record_cluster *rec = rl[i];
869         struct conf_service *service = s->psession->service;
870
871         wrbuf_puts(c->wrbuf, "<hit>\n");
872         write_metadata(c->wrbuf, service, rec->metadata, 0);
873         for (ccount = 0, p = rl[i]->records; p;  p = p->next, ccount++)
874             write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
875         if (ccount > 1)
876             wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
877         if (strstr(sort, "relevance"))
878             wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n",
879                          rec->relevance_score);
880         wrbuf_puts(c->wrbuf, "<recid>");
881         wrbuf_xmlputs(c->wrbuf, rec->recid);
882         wrbuf_puts(c->wrbuf, "</recid>\n");
883         wrbuf_puts(c->wrbuf, "</hit>\n");
884     }
885
886     show_range_stop(s->psession, rl);
887
888     wrbuf_puts(c->wrbuf, "</show>\n");
889     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
890     http_send_response(c);
891 }
892
893 static void show_records_ready(void *data)
894 {
895     struct http_channel *c = (struct http_channel *) data;
896
897     show_records(c, -1);
898 }
899
900 static void cmd_show(struct http_channel *c)
901 {
902     struct http_request *rq = c->request;
903     struct http_session *s = locate_session(c);
904     const char *block = http_argbyname(rq, "block");
905     int status;
906
907     if (!s)
908         return;
909
910     status = session_active_clients(s->psession);
911
912     if (block)
913     {
914         if (status && reclist_get_num_records(s->psession->reclist) == 0)
915         {
916             // if there is already a watch/block. we do not block this one
917             if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
918                                   show_records_ready, c, c) != 0)
919             {
920                 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
921             }
922             return;
923         }
924     }
925
926     show_records(c, status);
927 }
928
929 static void cmd_ping(struct http_channel *c)
930 {
931     struct http_response *rs = c->response;
932     struct http_session *s = locate_session(c);
933     if (!s)
934         return;
935     rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<ping><status>OK</status></ping>";
936     http_send_response(c);
937 }
938
939 static int utf_8_valid(const char *str)
940 {
941     yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
942     if (cd)
943     {
944         /* check that query is UTF-8 encoded */
945         char *inbuf = (char *) str; /* we know iconv does not alter this */
946         size_t inbytesleft = strlen(inbuf);
947
948         size_t outbytesleft = strlen(inbuf) + 10;
949         char *out = xmalloc(outbytesleft);
950         char *outbuf = out;
951         size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
952
953         /* if OK, try flushing the rest  */
954         if (r != (size_t) (-1))
955             r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
956         yaz_iconv_close(cd);
957         xfree(out);
958         if (r == (size_t) (-1))
959             return 0;
960     }
961     return 1;
962 }
963
964 static void cmd_search(struct http_channel *c)
965 {
966     struct http_request *rq = c->request;
967     struct http_response *rs = c->response;
968     struct http_session *s = locate_session(c);
969     const char *query = http_argbyname(rq, "query");
970     const char *filter = http_argbyname(rq, "filter");
971     const char *maxrecs = http_argbyname(rq, "maxrecs");
972     const char *startrecs = http_argbyname(rq, "startrecs");
973     enum pazpar2_error_code code;
974     const char *addinfo = 0;
975
976     if (!s)
977         return;
978     if (!query)
979     {
980         error(rs, PAZPAR2_MISSING_PARAMETER, "query");
981         return;
982     }
983     if (!utf_8_valid(query))
984     {
985         error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
986         return;
987     }
988     code = search(s->psession, query, startrecs, maxrecs, filter, &addinfo);
989     if (code)
990     {
991         error(rs, code, addinfo);
992         return;
993     }
994     rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<search><status>OK</status></search>";
995     http_send_response(c);
996 }
997
998
999 static void cmd_stat(struct http_channel *c)
1000 {
1001     struct http_response *rs = c->response;
1002     struct http_session *s = locate_session(c);
1003     struct statistics stat;
1004     int clients;
1005
1006     float progress = 0;
1007
1008     if (!s)
1009         return;
1010
1011     clients = session_active_clients(s->psession);
1012     statistics(s->psession, &stat);
1013
1014     if (stat.num_clients > 0) {
1015         progress = (stat.num_clients  - clients) / (float)stat.num_clients;
1016     }
1017
1018     wrbuf_rewind(c->wrbuf);
1019     wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<stat>");
1020     wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
1021     wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
1022     wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
1023     wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
1024     wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
1025     wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
1026     wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
1027     wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
1028     wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
1029     wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
1030     wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
1031     wrbuf_puts(c->wrbuf, "</stat>");
1032     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1033     http_send_response(c);
1034 }
1035
1036 static void cmd_info(struct http_channel *c)
1037 {
1038     char yaz_version_str[20];
1039     struct http_response *rs = c->response;
1040
1041     wrbuf_rewind(c->wrbuf);
1042     wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<info>\n");
1043     wrbuf_puts(c->wrbuf, " <version>\n");
1044     wrbuf_puts(c->wrbuf, "<pazpar2");
1045 #ifdef PAZPAR2_VERSION_SHA1
1046     wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
1047 #endif
1048     wrbuf_puts(c->wrbuf, ">");
1049     wrbuf_xmlputs(c->wrbuf, VERSION);
1050     wrbuf_puts(c->wrbuf, "</pazpar2>");
1051
1052
1053     yaz_version(yaz_version_str, 0);
1054     wrbuf_puts(c->wrbuf, "  <yaz compiled=\"");
1055     wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
1056     wrbuf_puts(c->wrbuf, "\">");
1057     wrbuf_xmlputs(c->wrbuf, yaz_version_str);
1058     wrbuf_puts(c->wrbuf, "</yaz>\n");
1059
1060     wrbuf_puts(c->wrbuf, " </version>\n");
1061     
1062     wrbuf_puts(c->wrbuf, "</info>");
1063     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1064     http_send_response(c);
1065 }
1066
1067 struct {
1068     char *name;
1069     void (*fun)(struct http_channel *c);
1070 } commands[] = {
1071     { "init", cmd_init },
1072     { "settings", cmd_settings },
1073     { "stat", cmd_stat },
1074     { "bytarget", cmd_bytarget },
1075     { "show", cmd_show },
1076     { "search", cmd_search },
1077     { "termlist", cmd_termlist },
1078     { "exit", cmd_exit },
1079     { "ping", cmd_ping },
1080     { "record", cmd_record },
1081     { "info", cmd_info },
1082     {0,0}
1083 };
1084
1085 void http_command(struct http_channel *c)
1086 {
1087     const char *command = http_argbyname(c->request, "command");
1088     struct http_response *rs = http_create_response(c);
1089     int i;
1090
1091     c->response = rs;
1092
1093     http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1094     http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1095
1096     if (!command)
1097     {
1098         error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1099         return;
1100     }
1101     for (i = 0; commands[i].name; i++)
1102         if (!strcmp(commands[i].name, command))
1103         {
1104             (*commands[i].fun)(c);
1105             break;
1106         }
1107     if (!commands[i].name)
1108         error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1109
1110     return;
1111 }
1112
1113 /*
1114  * Local variables:
1115  * c-basic-offset: 4
1116  * c-file-style: "Stroustrup"
1117  * indent-tabs-mode: nil
1118  * End:
1119  * vim: shiftwidth=4 tabstop=8 expandtab
1120  */
1121