1 /* $Id: http_command.c,v 1.46 2007-06-05 13:36:40 marc 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.46 2007-06-05 13:36:40 marc 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;
64 void http_session_destroy(struct http_session *s);
66 static void session_timeout(IOCHAN i, int event)
68 struct http_session *s = iochan_getdata(i);
69 http_session_destroy(s);
72 struct http_session *http_session_create()
74 NMEM nmem = nmem_create();
75 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
77 r->psession = new_session(nmem);
81 r->next = session_list;
83 r->timeout_iochan = iochan_create(-1, session_timeout, 0);
84 iochan_setdata(r->timeout_iochan, r);
85 iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout);
87 pazpar2_add_channel(r->timeout_iochan);
91 void http_session_destroy(struct http_session *s)
93 struct http_session **p;
95 for (p = &session_list; *p; p = &(*p)->next)
101 iochan_destroy(s->timeout_iochan);
102 destroy_session(s->psession);
103 nmem_destroy(s->nmem);
106 static void error(struct http_response *rs,
107 const char *code, const char *msg, const char *extra)
109 struct http_channel *c = rs->channel;
111 char *sep = extra ? ": " : "";
113 rs->msg = nmem_strdup(c->nmem, msg);
114 strcpy(rs->code, code);
116 yaz_snprintf(text, sizeof(text),
117 "<error code=\"general\">%s%s%s</error>", msg, sep,
120 yaz_log(YLOG_WARN, "HTTP %s %s%s%s", code, msg, sep,
122 rs->payload = nmem_strdup(c->nmem, text);
123 http_send_response(c);
126 unsigned int make_sessionid()
132 if (global_parameters.debug_mode)
138 if (gettimeofday(&t, 0) < 0)
140 yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
143 /* at most 256 sessions per second ..
144 (long long would be more appropriate)*/
146 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
151 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
153 struct http_session *p;
154 char *session = http_argbyname(rq, "session");
159 error(rs, "417", "Must supply session", 0);
163 for (p = session_list; p; p = p->next)
164 if (id == p->session_id)
166 iochan_activity(p->timeout_iochan);
169 error(rs, "417", "Session does not exist, or it has expired", 0);
173 // Decode settings parameters and apply to session
174 // Syntax: setting[target]=value
175 static int process_settings(struct session *se, struct http_request *rq,
176 struct http_response *rs)
178 struct http_argument *a;
180 for (a = rq->arguments; a; a = a->next)
181 if (strchr(a->name, '['))
188 // Nmem_strsplit *rules*!!!
189 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
192 error(rs, "417", "Malformed setting argument", a->name);
197 session_apply_setting(se, dbname, setting,
198 nmem_strdup(se->session_nmem, a->value));
203 static void cmd_exit(struct http_channel *c)
205 yaz_log(YLOG_WARN, "exit");
209 static void cmd_init(struct http_channel *c)
213 struct http_session *s = http_session_create();
214 struct http_response *rs = c->response;
216 yaz_log(YLOG_DEBUG, "HTTP Session init");
217 sesid = make_sessionid();
218 s->session_id = sesid;
219 if (process_settings(s->psession, c->request, c->response) < 0)
221 sprintf(buf, "<init><status>OK</status><session>%u</session>"
222 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>", sesid);
223 rs->payload = nmem_strdup(c->nmem, buf);
224 http_send_response(c);
227 static void cmd_settings(struct http_channel *c)
229 struct http_response *rs = c->response;
230 struct http_request *rq = c->request;
231 struct http_session *s = locate_session(rq, rs);
236 if (process_settings(s->psession, rq, rs) < 0)
238 rs->payload = "<settings><status>OK</status></settings>";
239 http_send_response(c);
242 // Compares two hitsbytarget nodes by hitcount
243 static int cmp_ht(const void *p1, const void *p2)
245 const struct hitsbytarget *h1 = p1;
246 const struct hitsbytarget *h2 = p2;
247 return h2->hits - h1->hits;
250 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
251 static void targets_termlist(WRBUF wrbuf, struct session *se, int num)
253 struct hitsbytarget *ht;
256 if (!(ht = hitsbytarget(se, &count)))
258 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
259 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
261 wrbuf_puts(wrbuf, "<term>\n");
263 //wrbuf_printf(wrbuf, "<id>%s</id>\n", ht[i].id);
264 wrbuf_puts(wrbuf, "<id>");
265 wrbuf_xmlputs(wrbuf, ht[i].id);
266 wrbuf_puts(wrbuf, "</id>\n");
268 wrbuf_puts(wrbuf, "<name>");
269 wrbuf_xmlputs(wrbuf, ht[i].name);
270 wrbuf_puts(wrbuf, "</name>\n");
272 wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
274 //wrbuf_printf(wrbuf, "<state>%s</state>\n", ht[i].state);
275 wrbuf_puts(wrbuf, "<state>");
276 wrbuf_xmlputs(wrbuf, ht[i].state);
277 wrbuf_puts(wrbuf, "</state>\n");
279 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
280 wrbuf_puts(wrbuf, "</term>\n");
284 static void cmd_termlist(struct http_channel *c)
286 struct http_response *rs = c->response;
287 struct http_request *rq = c->request;
288 struct http_session *s = locate_session(rq, rs);
289 struct termlist_score **p;
292 char *name = http_argbyname(rq, "name");
293 char *nums = http_argbyname(rq, "num");
300 status = session_active_clients(s->psession);
304 if (strlen(name) > 255)
309 wrbuf_rewind(c->wrbuf);
311 wrbuf_puts(c->wrbuf, "<termlist>");
312 wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>", status);
318 if (!(tp = strchr(name, ',')))
319 tp = name + strlen(name);
320 strncpy(tname, name, tp - name);
321 tname[tp - name] = '\0';
323 wrbuf_puts(c->wrbuf, "\n<list name=\"");
324 wrbuf_xmlputs(c->wrbuf, tname);
325 wrbuf_puts(c->wrbuf, "\">\n");
326 if (!strcmp(tname, "xtargets"))
327 targets_termlist(c->wrbuf, s->psession, num);
330 p = termlist(s->psession, tname, &len);
332 for (i = 0; i < len && i < num; i++)
334 wrbuf_puts(c->wrbuf, "\n<term>");
335 wrbuf_puts(c->wrbuf, "<name>");
336 wrbuf_xmlputs(c->wrbuf, p[i]->term);
337 wrbuf_puts(c->wrbuf, "</name>");
339 wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
340 wrbuf_puts(c->wrbuf, "</term>");
343 wrbuf_puts(c->wrbuf, "\n</list>");
348 wrbuf_puts(c->wrbuf, "</termlist>");
349 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
350 http_send_response(c);
354 static void cmd_bytarget(struct http_channel *c)
356 struct http_response *rs = c->response;
357 struct http_request *rq = c->request;
358 struct http_session *s = locate_session(rq, rs);
359 struct hitsbytarget *ht;
364 if (!(ht = hitsbytarget(s->psession, &count)))
366 error(rs, "500", "Failed to retrieve hitcounts", 0);
369 wrbuf_rewind(c->wrbuf);
370 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
372 for (i = 0; i < count; i++)
374 wrbuf_puts(c->wrbuf, "\n<target>");
376 //wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
377 wrbuf_puts(c->wrbuf, "<id>");
378 wrbuf_xmlputs(c->wrbuf, ht[i].id);
379 wrbuf_puts(c->wrbuf, "</id>\n");
381 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
382 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
383 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
385 //wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
386 wrbuf_puts(c->wrbuf, "<state>");
387 wrbuf_xmlputs(c->wrbuf, ht[i].state);
388 wrbuf_puts(c->wrbuf, "</state\n");
390 wrbuf_puts(c->wrbuf, "</target>");
393 wrbuf_puts(c->wrbuf, "</bytarget>");
394 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
395 http_send_response(c);
398 static void write_metadata(WRBUF w, struct conf_service *service,
399 struct record_metadata **ml, int full)
403 for (imeta = 0; imeta < service->num_metadata; imeta++)
405 struct conf_metadata *cmd = &service->metadata[imeta];
406 struct record_metadata *md;
407 if (!cmd->brief && !full)
409 for (md = ml[imeta]; md; md = md->next)
411 wrbuf_printf(w, "\n<md-%s>", cmd->name);
415 case Metadata_type_generic:
416 wrbuf_puts(w, md->data.text);
418 case Metadata_type_year:
419 wrbuf_printf(w, "%d", md->data.number.min);
420 if (md->data.number.min != md->data.number.max)
421 wrbuf_printf(w, "-%d", md->data.number.max);
424 wrbuf_puts(w, "[can't represent]");
426 wrbuf_printf(w, "</md-%s>", cmd->name);
431 static void write_subrecord(struct record *r, WRBUF w,
432 struct conf_service *service, int show_details)
434 char *name = session_setting_oneval(client_get_database(r->client), PZ_NAME);
436 wrbuf_puts(w, "<location id=\"");
437 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
438 wrbuf_puts(w, "\" ");
440 wrbuf_puts(w, "name=\"");
441 wrbuf_xmlputs(w, *name ? name : "Unknown");
442 wrbuf_puts(w, "\">");
445 write_metadata(w, service, r->metadata, 1);
446 wrbuf_puts(w, "</location>\n");
449 static void cmd_record(struct http_channel *c)
451 struct http_response *rs = c->response;
452 struct http_request *rq = c->request;
453 struct http_session *s = locate_session(rq, rs);
454 struct record_cluster *rec;
456 struct conf_service *service = global_parameters.server->service;
457 char *idstr = http_argbyname(rq, "id");
464 error(rs, "417", "Must supply id", 0);
467 wrbuf_rewind(c->wrbuf);
469 if (!(rec = show_single(s->psession, id)))
471 error(rs, "500", "Record missing", 0);
474 wrbuf_puts(c->wrbuf, "<record>\n");
475 wrbuf_printf(c->wrbuf, "<recid>%d</recid>", rec->recid);
476 write_metadata(c->wrbuf, service, rec->metadata, 1);
477 for (r = rec->records; r; r = r->next)
478 write_subrecord(r, c->wrbuf, service, 1);
479 wrbuf_puts(c->wrbuf, "</record>\n");
480 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
481 http_send_response(c);
484 static void show_records(struct http_channel *c, int active)
486 struct http_request *rq = c->request;
487 struct http_response *rs = c->response;
488 struct http_session *s = locate_session(rq, rs);
489 struct record_cluster **rl;
490 struct reclist_sortparms *sp;
491 char *start = http_argbyname(rq, "start");
492 char *num = http_argbyname(rq, "num");
493 char *sort = http_argbyname(rq, "sort");
503 // We haven't counted clients yet if we're called on a block release
505 active = session_active_clients(s->psession);
508 startn = atoi(start);
513 if (!(sp = reclist_parse_sortparms(c->nmem, sort)))
515 error(rs, "500", "Bad sort parameters", 0);
519 rl = show(s->psession, sp, startn, &numn, &total, &total_hits, c->nmem);
521 wrbuf_rewind(c->wrbuf);
522 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
523 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
524 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
525 wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
526 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
527 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
529 for (i = 0; i < numn; i++)
533 struct record_cluster *rec = rl[i];
534 struct conf_service *service = global_parameters.server->service;
536 wrbuf_puts(c->wrbuf, "<hit>\n");
537 write_metadata(c->wrbuf, service, rec->metadata, 0);
538 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
539 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
541 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
542 wrbuf_printf(c->wrbuf, "<recid>%d</recid>\n", rec->recid);
543 wrbuf_puts(c->wrbuf, "</hit>\n");
546 wrbuf_puts(c->wrbuf, "</show>\n");
547 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
548 http_send_response(c);
551 static void show_records_ready(void *data)
553 struct http_channel *c = (struct http_channel *) data;
558 static void cmd_show(struct http_channel *c)
560 struct http_request *rq = c->request;
561 struct http_response *rs = c->response;
562 struct http_session *s = locate_session(rq, rs);
563 char *block = http_argbyname(rq, "block");
569 status = session_active_clients(s->psession);
573 if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
575 session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
576 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
581 show_records(c, status);
584 static void cmd_ping(struct http_channel *c)
586 struct http_request *rq = c->request;
587 struct http_response *rs = c->response;
588 struct http_session *s = locate_session(rq, rs);
591 rs->payload = "<ping><status>OK</status></ping>";
592 http_send_response(c);
595 static int utf_8_valid(const char *str)
597 yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
600 /* check that query is UTF-8 encoded */
601 char *inbuf = (char *) str; /* we know iconv does not alter this */
602 size_t inbytesleft = strlen(inbuf);
604 size_t outbytesleft = strlen(inbuf) + 10;
605 char *out = xmalloc(outbytesleft);
607 size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
609 /* if OK, try flushing the rest */
610 if (r != (size_t) (-1))
611 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
614 if (r == (size_t) (-1))
620 static void cmd_search(struct http_channel *c)
622 struct http_request *rq = c->request;
623 struct http_response *rs = c->response;
624 struct http_session *s = locate_session(rq, rs);
625 char *query = http_argbyname(rq, "query");
626 char *filter = http_argbyname(rq, "filter");
633 error(rs, "417", "Must supply query", 0);
636 if (!utf_8_valid(query))
638 error(rs, "417", "Query not UTF-8 encoded", 0);
641 res = search(s->psession, query, filter);
644 error(rs, "417", res, 0);
647 rs->payload = "<search><status>OK</status></search>";
648 http_send_response(c);
652 static void cmd_stat(struct http_channel *c)
654 struct http_request *rq = c->request;
655 struct http_response *rs = c->response;
656 struct http_session *s = locate_session(rq, rs);
657 struct statistics stat;
663 clients = session_active_clients(s->psession);
664 statistics(s->psession, &stat);
666 wrbuf_rewind(c->wrbuf);
667 wrbuf_puts(c->wrbuf, "<stat>");
668 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
669 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
670 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
671 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
672 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
673 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
674 wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
675 wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
676 wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
677 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
678 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
679 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
680 wrbuf_puts(c->wrbuf, "</stat>");
681 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
682 http_send_response(c);
685 static void cmd_info(struct http_channel *c)
687 char yaz_version_str[20];
688 struct http_response *rs = c->response;
690 wrbuf_rewind(c->wrbuf);
691 wrbuf_puts(c->wrbuf, "<info>\n");
692 wrbuf_puts(c->wrbuf, " <version>\n");
693 //wrbuf_printf(c->wrbuf, " <pazpar2>%s</pazpar2>\n", VERSION);
694 wrbuf_puts(c->wrbuf, "<pazpar2>");
695 wrbuf_xmlputs(c->wrbuf, VERSION);
696 wrbuf_puts(c->wrbuf, "</pazpar2>");
699 yaz_version(yaz_version_str, 0);
700 //wrbuf_printf(c->wrbuf, " <yaz compiled=\"%s\">%s</yaz>\n",
701 // YAZ_VERSION, yaz_version_str);
702 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
703 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
704 wrbuf_puts(c->wrbuf, "\">");
705 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
706 wrbuf_puts(c->wrbuf, "</yaz>\n");
708 wrbuf_puts(c->wrbuf, " </version>\n");
710 wrbuf_puts(c->wrbuf, "</info>");
711 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
712 http_send_response(c);
717 void (*fun)(struct http_channel *c);
719 { "init", cmd_init },
720 { "settings", cmd_settings },
721 { "stat", cmd_stat },
722 { "bytarget", cmd_bytarget },
723 { "show", cmd_show },
724 { "search", cmd_search },
725 { "termlist", cmd_termlist },
726 { "exit", cmd_exit },
727 { "ping", cmd_ping },
728 { "record", cmd_record },
729 { "info", cmd_info },
733 void http_command(struct http_channel *c)
735 char *command = http_argbyname(c->request, "command");
736 struct http_response *rs = http_create_response(c);
741 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
742 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
746 error(rs, "417", "Must supply command", 0);
749 for (i = 0; commands[i].name; i++)
750 if (!strcmp(commands[i].name, command))
752 (*commands[i].fun)(c);
755 if (!commands[i].name)
756 error(rs, "417", "Unknown command", command);
764 * indent-tabs-mode: nil
766 * vim: shiftwidth=4 tabstop=8 expandtab