1 /* $Id: http_command.c,v 1.53 2007-06-15 06:45:39 adam Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 * $Id: http_command.c,v 1.53 2007-06-15 06:45:39 adam Exp $
27 #include <sys/types.h>
34 #include <yaz/snprintf.h>
39 #include <yaz/yaz-util.h>
46 #include "http_command.h"
50 // Update this when the protocol changes
51 #define PAZPAR2_PROTOCOL_VERSION "1"
54 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
55 struct session *psession;
56 unsigned int session_id;
59 struct http_session *next;
62 static struct http_session *session_list = 0;
63 void http_session_destroy(struct http_session *s);
65 static void session_timeout(IOCHAN i, int event)
67 struct http_session *s = iochan_getdata(i);
68 http_session_destroy(s);
71 struct http_session *http_session_create()
73 NMEM nmem = nmem_create();
74 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
76 r->psession = new_session(nmem);
80 r->next = session_list;
82 r->timeout_iochan = iochan_create(-1, session_timeout, 0);
83 iochan_setdata(r->timeout_iochan, r);
84 iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout);
86 pazpar2_add_channel(r->timeout_iochan);
90 void http_session_destroy(struct http_session *s)
92 struct http_session **p;
94 for (p = &session_list; *p; p = &(*p)->next)
100 yaz_log(YLOG_LOG, "Destroying session %u", s->session_id);
101 iochan_destroy(s->timeout_iochan);
102 destroy_session(s->psession);
103 nmem_destroy(s->nmem);
106 static const char *get_msg(enum pazpar2_error_code code)
108 struct pazpar2_error_msg {
109 enum pazpar2_error_code code;
112 static const struct pazpar2_error_msg ar[] = {
113 { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
114 { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
115 { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
116 { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
117 { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
118 { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
119 { PAZPAR2_RECORD_MISSING, "Record missing"},
120 { PAZPAR2_NO_TARGETS, "No targets"},
121 { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
122 { PAZPAR2_RECORD_FAIL, "Record command failed"},
123 { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"},
124 { PAZPAR2_LAST_ERROR, "Last error"},
130 if (code == ar[i].code)
137 static void error(struct http_response *rs,
138 enum pazpar2_error_code code,
141 struct http_channel *c = rs->channel;
143 const char *http_status = "417";
144 const char *msg = get_msg(code);
146 rs->msg = nmem_strdup(c->nmem, msg);
147 strcpy(rs->code, http_status);
149 yaz_snprintf(text, sizeof(text),
150 "<error code=\"%d\" msg=\"%s\">%s</error>", (int) code,
151 msg, addinfo ? addinfo : "");
153 yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
154 msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
155 rs->payload = nmem_strdup(c->nmem, text);
156 http_send_response(c);
159 unsigned int make_sessionid()
165 if (global_parameters.debug_mode)
171 if (gettimeofday(&t, 0) < 0)
173 yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
176 /* at most 256 sessions per second ..
177 (long long would be more appropriate)*/
179 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
184 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
186 struct http_session *p;
187 char *session = http_argbyname(rq, "session");
192 error(rs, PAZPAR2_MISSING_PARAMETER, "session");
196 for (p = session_list; p; p = p->next)
197 if (id == p->session_id)
199 iochan_activity(p->timeout_iochan);
202 error(rs, PAZPAR2_NO_SESSION, session);
206 // Decode settings parameters and apply to session
207 // Syntax: setting[target]=value
208 static int process_settings(struct session *se, struct http_request *rq,
209 struct http_response *rs)
211 struct http_argument *a;
213 for (a = rq->arguments; a; a = a->next)
214 if (strchr(a->name, '['))
221 // Nmem_strsplit *rules*!!!
222 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
225 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
230 session_apply_setting(se, dbname, setting,
231 nmem_strdup(se->session_nmem, a->value));
236 static void cmd_exit(struct http_channel *c)
238 yaz_log(YLOG_WARN, "exit");
242 static void cmd_init(struct http_channel *c)
246 struct http_session *s = http_session_create();
247 struct http_response *rs = c->response;
249 yaz_log(YLOG_DEBUG, "HTTP Session init");
250 sesid = make_sessionid();
251 s->session_id = sesid;
252 if (process_settings(s->psession, c->request, c->response) < 0)
254 sprintf(buf, "<init><status>OK</status><session>%u</session>"
255 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>", sesid);
256 rs->payload = nmem_strdup(c->nmem, buf);
257 http_send_response(c);
260 static void cmd_settings(struct http_channel *c)
262 struct http_response *rs = c->response;
263 struct http_request *rq = c->request;
264 struct http_session *s = locate_session(rq, rs);
269 if (process_settings(s->psession, rq, rs) < 0)
271 rs->payload = "<settings><status>OK</status></settings>";
272 http_send_response(c);
275 // Compares two hitsbytarget nodes by hitcount
276 static int cmp_ht(const void *p1, const void *p2)
278 const struct hitsbytarget *h1 = p1;
279 const struct hitsbytarget *h2 = p2;
280 return h2->hits - h1->hits;
283 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
284 static void targets_termlist(WRBUF wrbuf, struct session *se, int num)
286 struct hitsbytarget *ht;
289 if (!(ht = hitsbytarget(se, &count)))
291 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
292 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
295 // do only print terms which have display names
297 wrbuf_puts(wrbuf, "<term>\n");
299 wrbuf_puts(wrbuf, "<id>");
300 wrbuf_xmlputs(wrbuf, ht[i].id);
301 wrbuf_puts(wrbuf, "</id>\n");
303 wrbuf_puts(wrbuf, "<name>");
304 if (!ht[i].name || !ht[i].name[0])
305 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
307 wrbuf_xmlputs(wrbuf, ht[i].name);
308 wrbuf_puts(wrbuf, "</name>\n");
310 wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
312 wrbuf_puts(wrbuf, "<state>");
313 wrbuf_xmlputs(wrbuf, ht[i].state);
314 wrbuf_puts(wrbuf, "</state>\n");
316 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
318 wrbuf_puts(wrbuf, "</term>\n");
322 static void cmd_termlist(struct http_channel *c)
324 struct http_response *rs = c->response;
325 struct http_request *rq = c->request;
326 struct http_session *s = locate_session(rq, rs);
327 struct termlist_score **p;
330 char *name = http_argbyname(rq, "name");
331 char *nums = http_argbyname(rq, "num");
338 status = session_active_clients(s->psession);
342 if (strlen(name) > 255)
347 wrbuf_rewind(c->wrbuf);
349 wrbuf_puts(c->wrbuf, "<termlist>\n");
350 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
356 if (!(tp = strchr(name, ',')))
357 tp = name + strlen(name);
358 strncpy(tname, name, tp - name);
359 tname[tp - name] = '\0';
361 wrbuf_puts(c->wrbuf, "<list name=\"");
362 wrbuf_xmlputs(c->wrbuf, tname);
363 wrbuf_puts(c->wrbuf, "\">\n");
364 if (!strcmp(tname, "xtargets"))
365 targets_termlist(c->wrbuf, s->psession, num);
368 p = termlist(s->psession, tname, &len);
370 for (i = 0; i < len && i < num; i++){
371 // prevnt sending empty term elements
372 if (!p[i]->term || !p[i]->term[0])
375 wrbuf_puts(c->wrbuf, "<term>");
376 wrbuf_puts(c->wrbuf, "<name>");
377 wrbuf_xmlputs(c->wrbuf, p[i]->term);
378 wrbuf_puts(c->wrbuf, "</name>");
380 wrbuf_printf(c->wrbuf,
381 "<frequency>%d</frequency>",
383 wrbuf_puts(c->wrbuf, "</term>\n");
386 wrbuf_puts(c->wrbuf, "</list>\n");
391 wrbuf_puts(c->wrbuf, "</termlist>\n");
392 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
393 http_send_response(c);
397 static void cmd_bytarget(struct http_channel *c)
399 struct http_response *rs = c->response;
400 struct http_request *rq = c->request;
401 struct http_session *s = locate_session(rq, rs);
402 struct hitsbytarget *ht;
407 if (!(ht = hitsbytarget(s->psession, &count)))
409 error(rs, PAZPAR2_HITCOUNTS_FAILED, 0);
412 wrbuf_rewind(c->wrbuf);
413 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
415 for (i = 0; i < count; i++)
417 wrbuf_puts(c->wrbuf, "\n<target>");
419 wrbuf_puts(c->wrbuf, "<id>");
420 wrbuf_xmlputs(c->wrbuf, ht[i].id);
421 wrbuf_puts(c->wrbuf, "</id>\n");
423 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
424 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
425 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
427 wrbuf_puts(c->wrbuf, "<state>");
428 wrbuf_xmlputs(c->wrbuf, ht[i].state);
429 wrbuf_puts(c->wrbuf, "</state>\n");
431 wrbuf_puts(c->wrbuf, "</target>");
434 wrbuf_puts(c->wrbuf, "</bytarget>");
435 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
436 http_send_response(c);
439 static void write_metadata(WRBUF w, struct conf_service *service,
440 struct record_metadata **ml, int full)
444 for (imeta = 0; imeta < service->num_metadata; imeta++)
446 struct conf_metadata *cmd = &service->metadata[imeta];
447 struct record_metadata *md;
448 if (!cmd->brief && !full)
450 for (md = ml[imeta]; md; md = md->next)
452 wrbuf_printf(w, "\n<md-%s>", cmd->name);
456 case Metadata_type_generic:
457 wrbuf_xmlputs(w, md->data.text);
459 case Metadata_type_year:
460 wrbuf_printf(w, "%d", md->data.number.min);
461 if (md->data.number.min != md->data.number.max)
462 wrbuf_printf(w, "-%d", md->data.number.max);
465 wrbuf_puts(w, "[can't represent]");
467 wrbuf_printf(w, "</md-%s>", cmd->name);
472 static void write_subrecord(struct record *r, WRBUF w,
473 struct conf_service *service, int show_details)
475 char *name = session_setting_oneval(client_get_database(r->client), PZ_NAME);
477 wrbuf_puts(w, "<location id=\"");
478 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
479 wrbuf_puts(w, "\" ");
481 wrbuf_puts(w, "name=\"");
482 wrbuf_xmlputs(w, *name ? name : "Unknown");
483 wrbuf_puts(w, "\">");
486 write_metadata(w, service, r->metadata, 1);
487 wrbuf_puts(w, "</location>\n");
490 static void show_raw_record_error(void *data, const char *addinfo)
492 struct http_channel *c = (struct http_channel *) data;
493 struct http_response *rs = c->response;
495 error(rs, PAZPAR2_NOT_IMPLEMENTED, addinfo);
498 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
500 struct http_channel *c = (struct http_channel *) data;
501 struct http_response *rs = c->response;
503 wrbuf_write(c->wrbuf, buf, sz);
504 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
505 http_send_response(c);
508 static void cmd_record(struct http_channel *c)
510 struct http_response *rs = c->response;
511 struct http_request *rq = c->request;
512 struct http_session *s = locate_session(rq, rs);
513 struct record_cluster *rec;
515 struct conf_service *service = global_parameters.server->service;
516 const char *idstr = http_argbyname(rq, "id");
517 const char *offsetstr = http_argbyname(rq, "offset");
525 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
528 wrbuf_rewind(c->wrbuf);
530 if (!(rec = show_single(s->psession, id)))
532 error(rs, PAZPAR2_RECORD_MISSING, idstr);
537 int offset = atoi(offsetstr);
538 const char *syntax = http_argbyname(rq, "syntax");
539 const char *esn = http_argbyname(rq, "esn");
541 struct record*r = rec->records;
543 for (i = 0; i < offset && r; r = r->next, i++)
547 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
550 if (client_show_raw(r->client, r->position, syntax, esn,
552 show_raw_record_error,
555 error(rs, PAZPAR2_RECORD_FAIL, "invalid parameters");
561 wrbuf_puts(c->wrbuf, "<record>\n");
562 wrbuf_printf(c->wrbuf, "<recid>%d</recid>\n", rec->recid);
563 write_metadata(c->wrbuf, service, rec->metadata, 1);
564 for (r = rec->records; r; r = r->next)
565 write_subrecord(r, c->wrbuf, service, 1);
566 wrbuf_puts(c->wrbuf, "</record>\n");
567 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
568 http_send_response(c);
572 static void show_records(struct http_channel *c, int active)
574 struct http_request *rq = c->request;
575 struct http_response *rs = c->response;
576 struct http_session *s = locate_session(rq, rs);
577 struct record_cluster **rl;
578 struct reclist_sortparms *sp;
579 char *start = http_argbyname(rq, "start");
580 char *num = http_argbyname(rq, "num");
581 char *sort = http_argbyname(rq, "sort");
591 // We haven't counted clients yet if we're called on a block release
593 active = session_active_clients(s->psession);
596 startn = atoi(start);
601 if (!(sp = reclist_parse_sortparms(c->nmem, sort)))
603 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
607 rl = show(s->psession, sp, startn, &numn, &total, &total_hits, c->nmem);
609 wrbuf_rewind(c->wrbuf);
610 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
611 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
612 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
613 wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
614 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
615 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
617 for (i = 0; i < numn; i++)
621 struct record_cluster *rec = rl[i];
622 struct conf_service *service = global_parameters.server->service;
624 wrbuf_puts(c->wrbuf, "<hit>\n");
625 write_metadata(c->wrbuf, service, rec->metadata, 0);
626 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
627 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
629 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
630 wrbuf_printf(c->wrbuf, "<recid>%d</recid>\n", rec->recid);
631 wrbuf_puts(c->wrbuf, "</hit>\n");
634 wrbuf_puts(c->wrbuf, "</show>\n");
635 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
636 http_send_response(c);
639 static void show_records_ready(void *data)
641 struct http_channel *c = (struct http_channel *) data;
646 static void cmd_show(struct http_channel *c)
648 struct http_request *rq = c->request;
649 struct http_response *rs = c->response;
650 struct http_session *s = locate_session(rq, rs);
651 char *block = http_argbyname(rq, "block");
657 status = session_active_clients(s->psession);
661 if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
663 session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
664 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
669 show_records(c, status);
672 static void cmd_ping(struct http_channel *c)
674 struct http_request *rq = c->request;
675 struct http_response *rs = c->response;
676 struct http_session *s = locate_session(rq, rs);
679 rs->payload = "<ping><status>OK</status></ping>";
680 http_send_response(c);
683 static int utf_8_valid(const char *str)
685 yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
688 /* check that query is UTF-8 encoded */
689 char *inbuf = (char *) str; /* we know iconv does not alter this */
690 size_t inbytesleft = strlen(inbuf);
692 size_t outbytesleft = strlen(inbuf) + 10;
693 char *out = xmalloc(outbytesleft);
695 size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
697 /* if OK, try flushing the rest */
698 if (r != (size_t) (-1))
699 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
702 if (r == (size_t) (-1))
708 static void cmd_search(struct http_channel *c)
710 struct http_request *rq = c->request;
711 struct http_response *rs = c->response;
712 struct http_session *s = locate_session(rq, rs);
713 char *query = http_argbyname(rq, "query");
714 char *filter = http_argbyname(rq, "filter");
715 enum pazpar2_error_code code;
716 const char *addinfo = 0;
722 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
725 if (!utf_8_valid(query))
727 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
730 code = search(s->psession, query, filter, &addinfo);
733 error(rs, code, addinfo);
736 rs->payload = "<search><status>OK</status></search>";
737 http_send_response(c);
741 static void cmd_stat(struct http_channel *c)
743 struct http_request *rq = c->request;
744 struct http_response *rs = c->response;
745 struct http_session *s = locate_session(rq, rs);
746 struct statistics stat;
752 clients = session_active_clients(s->psession);
753 statistics(s->psession, &stat);
755 wrbuf_rewind(c->wrbuf);
756 wrbuf_puts(c->wrbuf, "<stat>");
757 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
758 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
759 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
760 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
761 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
762 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
763 wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
764 wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
765 wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
766 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
767 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
768 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
769 wrbuf_puts(c->wrbuf, "</stat>");
770 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
771 http_send_response(c);
774 static void cmd_info(struct http_channel *c)
776 char yaz_version_str[20];
777 struct http_response *rs = c->response;
779 wrbuf_rewind(c->wrbuf);
780 wrbuf_puts(c->wrbuf, "<info>\n");
781 wrbuf_puts(c->wrbuf, " <version>\n");
782 wrbuf_puts(c->wrbuf, "<pazpar2>");
783 wrbuf_xmlputs(c->wrbuf, VERSION);
784 wrbuf_puts(c->wrbuf, "</pazpar2>");
787 yaz_version(yaz_version_str, 0);
788 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
789 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
790 wrbuf_puts(c->wrbuf, "\">");
791 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
792 wrbuf_puts(c->wrbuf, "</yaz>\n");
794 wrbuf_puts(c->wrbuf, " </version>\n");
796 wrbuf_puts(c->wrbuf, "</info>");
797 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
798 http_send_response(c);
803 void (*fun)(struct http_channel *c);
805 { "init", cmd_init },
806 { "settings", cmd_settings },
807 { "stat", cmd_stat },
808 { "bytarget", cmd_bytarget },
809 { "show", cmd_show },
810 { "search", cmd_search },
811 { "termlist", cmd_termlist },
812 { "exit", cmd_exit },
813 { "ping", cmd_ping },
814 { "record", cmd_record },
815 { "info", cmd_info },
819 void http_command(struct http_channel *c)
821 char *command = http_argbyname(c->request, "command");
822 struct http_response *rs = http_create_response(c);
827 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
828 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
832 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
835 for (i = 0; commands[i].name; i++)
836 if (!strcmp(commands[i].name, command))
838 (*commands[i].fun)(c);
841 if (!commands[i].name)
842 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
850 * indent-tabs-mode: nil
852 * vim: shiftwidth=4 tabstop=8 expandtab