1 /* $Id: http_command.c,v 1.33 2007-04-11 02:14:15 quinn 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.33 2007-04-11 02:14:15 quinn Exp $
27 #include <sys/types.h>
39 #include <yaz/yaz-util.h>
46 #include "http_command.h"
48 extern struct parameters global_parameters;
49 extern IOCHAN channel_list;
52 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
53 struct session *psession;
54 unsigned int session_id;
57 struct http_session *next;
60 static struct http_session *session_list = 0;
62 void http_session_destroy(struct http_session *s);
64 static void session_timeout(IOCHAN i, int event)
66 struct http_session *s = iochan_getdata(i);
67 http_session_destroy(s);
70 struct http_session *http_session_create()
72 NMEM nmem = nmem_create();
73 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
75 r->psession = new_session(nmem);
79 r->next = session_list;
81 r->timeout_iochan = iochan_create(-1, session_timeout, 0);
82 iochan_setdata(r->timeout_iochan, r);
83 iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout);
84 r->timeout_iochan->next = channel_list;
85 channel_list = r->timeout_iochan;
89 void http_session_destroy(struct http_session *s)
91 struct http_session **p;
93 for (p = &session_list; *p; p = &(*p)->next)
99 iochan_destroy(s->timeout_iochan);
100 destroy_session(s->psession);
101 nmem_destroy(s->nmem);
104 static void error(struct http_response *rs, char *code, char *msg, char *txt)
106 struct http_channel *c = rs->channel;
111 rs->msg = nmem_strdup(c->nmem, msg);
112 strcpy(rs->code, code);
113 sprintf(tmp, "<error code=\"general\">%s</error>", txt);
114 rs->payload = nmem_strdup(c->nmem, tmp);
115 http_send_response(c);
118 unsigned int make_sessionid()
125 if (gettimeofday(&t, 0) < 0)
128 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
132 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
134 struct http_session *p;
135 char *session = http_argbyname(rq, "session");
140 error(rs, "417", "Must supply session", 0);
144 for (p = session_list; p; p = p->next)
145 if (id == p->session_id)
147 iochan_activity(p->timeout_iochan);
150 error(rs, "417", "Session does not exist, or it has expired", 0);
154 // Decode settings parameters and apply to session
155 // Syntax: setting[target]=value
156 static int process_settings(struct session *se, struct http_request *rq,
157 struct http_response *rs)
159 struct http_argument *a;
161 for (a = rq->arguments; a; a = a->next)
162 if (strchr(a->name, '['))
169 // Nmem_strsplit *rules*!!!
170 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
173 error(rs, "417", "Malformed setting argument", 0);
174 yaz_log(YLOG_WARN, "Malformed setting: %s", a->name);
179 session_apply_setting(se, dbname, setting,
180 nmem_strdup(se->session_nmem, a->value));
185 static void cmd_exit(struct http_channel *c)
187 yaz_log(YLOG_WARN, "exit");
191 static void cmd_init(struct http_channel *c)
195 struct http_session *s = http_session_create();
196 struct http_response *rs = c->response;
198 yaz_log(YLOG_DEBUG, "HTTP Session init");
199 sesid = make_sessionid();
200 s->session_id = sesid;
201 if (process_settings(s->psession, c->request, c->response) < 0)
203 sprintf(buf, "<init><status>OK</status><session>%u</session></init>", sesid);
204 rs->payload = nmem_strdup(c->nmem, buf);
205 http_send_response(c);
208 static void cmd_settings(struct http_channel *c)
210 struct http_response *rs = c->response;
211 struct http_request *rq = c->request;
212 struct http_session *s = locate_session(rq, rs);
217 if (process_settings(s->psession, rq, rs) < 0)
219 rs->payload = "<settings><status>OK</status></settings>";
220 http_send_response(c);
223 // Compares two hitsbytarget nodes by hitcount
224 static int cmp_ht(const void *p1, const void *p2)
226 const struct hitsbytarget *h1 = p1;
227 const struct hitsbytarget *h2 = p2;
228 return h2->hits - h1->hits;
231 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
232 static void targets_termlist(WRBUF wrbuf, struct session *se, int num)
234 struct hitsbytarget *ht;
237 if (!(ht = hitsbytarget(se, &count)))
239 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
240 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
242 wrbuf_puts(wrbuf, "\n<term>\n");
243 wrbuf_printf(wrbuf, "<id>%s</id>\n", ht[i].id);
244 wrbuf_printf(wrbuf, "<name>%s</name>\n", ht[i].name);
245 wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
246 wrbuf_printf(wrbuf, "<state>%s</state>\n", ht[i].state);
247 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
248 wrbuf_puts(wrbuf, "\n</term>\n");
252 static void cmd_termlist(struct http_channel *c)
254 struct http_response *rs = c->response;
255 struct http_request *rq = c->request;
256 struct http_session *s = locate_session(rq, rs);
257 struct termlist_score **p;
260 char *name = http_argbyname(rq, "name");
261 char *nums = http_argbyname(rq, "num");
268 status = session_active_clients(s->psession);
272 if (strlen(name) > 255)
277 wrbuf_rewind(c->wrbuf);
279 wrbuf_puts(c->wrbuf, "<termlist>");
280 wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>", status);
286 if (!(tp = strchr(name, ',')))
287 tp = name + strlen(name);
288 strncpy(tname, name, tp - name);
289 tname[tp - name] = '\0';
291 wrbuf_printf(c->wrbuf, "\n<list name=\"%s\">\n", tname);
292 if (!strcmp(tname, "xtargets"))
293 targets_termlist(c->wrbuf, s->psession, num);
296 p = termlist(s->psession, tname, &len);
298 for (i = 0; i < len && i < num; i++)
300 wrbuf_puts(c->wrbuf, "\n<term>");
301 wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
302 wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
303 wrbuf_puts(c->wrbuf, "</term>");
306 wrbuf_puts(c->wrbuf, "\n</list>");
311 wrbuf_puts(c->wrbuf, "</termlist>");
312 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
313 http_send_response(c);
317 static void cmd_bytarget(struct http_channel *c)
319 struct http_response *rs = c->response;
320 struct http_request *rq = c->request;
321 struct http_session *s = locate_session(rq, rs);
322 struct hitsbytarget *ht;
327 if (!(ht = hitsbytarget(s->psession, &count)))
329 error(rs, "500", "Failed to retrieve hitcounts", 0);
332 wrbuf_rewind(c->wrbuf);
333 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
335 for (i = 0; i < count; i++)
337 wrbuf_puts(c->wrbuf, "\n<target>");
338 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
339 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
340 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
341 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
342 wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
343 wrbuf_puts(c->wrbuf, "</target>");
346 wrbuf_puts(c->wrbuf, "</bytarget>");
347 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
348 http_send_response(c);
351 static void write_metadata(WRBUF w, struct conf_service *service,
352 struct record_metadata **ml, int full)
356 for (imeta = 0; imeta < service->num_metadata; imeta++)
358 struct conf_metadata *cmd = &service->metadata[imeta];
359 struct record_metadata *md;
360 if (!cmd->brief && !full)
362 for (md = ml[imeta]; md; md = md->next)
364 wrbuf_printf(w, "<md-%s>", cmd->name);
367 case Metadata_type_generic:
368 wrbuf_puts(w, md->data.text);
370 case Metadata_type_year:
371 wrbuf_printf(w, "%d", md->data.number.min);
372 if (md->data.number.min != md->data.number.max)
373 wrbuf_printf(w, "-%d", md->data.number.max);
376 wrbuf_puts(w, "[can't represent]");
378 wrbuf_printf(w, "</md-%s>", cmd->name);
383 static void write_subrecord(struct record *r, WRBUF w, struct conf_service *service)
385 wrbuf_printf(w, "<location id=\"%s\" name=\"%s\">\n",
386 r->client->database->database->url,
387 r->client->database->database->name ? r->client->database->database->name : "");
388 write_metadata(w, service, r->metadata, 1);
389 wrbuf_puts(w, "</location>\n");
392 static void cmd_record(struct http_channel *c)
394 struct http_response *rs = c->response;
395 struct http_request *rq = c->request;
396 struct http_session *s = locate_session(rq, rs);
397 struct record_cluster *rec;
399 struct conf_service *service = global_parameters.server->service;
400 char *idstr = http_argbyname(rq, "id");
407 error(rs, "417", "Must supply id", 0);
410 wrbuf_rewind(c->wrbuf);
412 if (!(rec = show_single(s->psession, id)))
414 error(rs, "500", "Record missing", 0);
417 wrbuf_puts(c->wrbuf, "<record>\n");
418 wrbuf_printf(c->wrbuf, "<recid>%d</recid>", rec->recid);
419 write_metadata(c->wrbuf, service, rec->metadata, 1);
420 for (r = rec->records; r; r = r->next)
421 write_subrecord(r, c->wrbuf, service);
422 wrbuf_puts(c->wrbuf, "</record>\n");
423 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
424 http_send_response(c);
427 static void show_records(struct http_channel *c, int active)
429 struct http_request *rq = c->request;
430 struct http_response *rs = c->response;
431 struct http_session *s = locate_session(rq, rs);
432 struct record_cluster **rl;
433 struct reclist_sortparms *sp;
434 char *start = http_argbyname(rq, "start");
435 char *num = http_argbyname(rq, "num");
436 char *sort = http_argbyname(rq, "sort");
446 // We haven't counted clients yet if we're called on a block release
448 active = session_active_clients(s->psession);
451 startn = atoi(start);
456 if (!(sp = reclist_parse_sortparms(c->nmem, sort)))
458 error(rs, "500", "Bad sort parameters", 0);
462 rl = show(s->psession, sp, startn, &numn, &total, &total_hits, c->nmem);
464 wrbuf_rewind(c->wrbuf);
465 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
466 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
467 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
468 wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
469 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
470 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
472 for (i = 0; i < numn; i++)
476 struct record_cluster *rec = rl[i];
477 struct conf_service *service = global_parameters.server->service;
479 wrbuf_puts(c->wrbuf, "<hit>\n");
480 write_metadata(c->wrbuf, service, rec->metadata, 0);
481 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
484 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
485 wrbuf_printf(c->wrbuf, "<recid>%d</recid>\n", rec->recid);
486 wrbuf_puts(c->wrbuf, "</hit>\n");
489 wrbuf_puts(c->wrbuf, "</show>\n");
490 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
491 http_send_response(c);
494 static void show_records_ready(void *data)
496 struct http_channel *c = (struct http_channel *) data;
501 static void cmd_show(struct http_channel *c)
503 struct http_request *rq = c->request;
504 struct http_response *rs = c->response;
505 struct http_session *s = locate_session(rq, rs);
506 char *block = http_argbyname(rq, "block");
512 status = session_active_clients(s->psession);
516 if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
518 session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
519 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
524 show_records(c, status);
527 static void cmd_ping(struct http_channel *c)
529 struct http_request *rq = c->request;
530 struct http_response *rs = c->response;
531 struct http_session *s = locate_session(rq, rs);
534 rs->payload = "<ping><status>OK</status></ping>";
535 http_send_response(c);
538 static void cmd_search(struct http_channel *c)
540 struct http_request *rq = c->request;
541 struct http_response *rs = c->response;
542 struct http_session *s = locate_session(rq, rs);
543 char *query = http_argbyname(rq, "query");
544 char *filter = http_argbyname(rq, "filter");
551 error(rs, "417", "Must supply query", 0);
554 res = search(s->psession, query, filter);
557 error(rs, "417", res, res);
560 rs->payload = "<search><status>OK</status></search>";
561 http_send_response(c);
565 static void cmd_stat(struct http_channel *c)
567 struct http_request *rq = c->request;
568 struct http_response *rs = c->response;
569 struct http_session *s = locate_session(rq, rs);
570 struct statistics stat;
576 clients = session_active_clients(s->psession);
577 statistics(s->psession, &stat);
579 wrbuf_rewind(c->wrbuf);
580 wrbuf_puts(c->wrbuf, "<stat>");
581 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
582 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
583 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
584 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
585 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
586 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
587 wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
588 wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
589 wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
590 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
591 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
592 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
593 wrbuf_puts(c->wrbuf, "</stat>");
594 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
595 http_send_response(c);
598 static void cmd_info(struct http_channel *c)
600 char yaz_version_str[20];
601 struct http_response *rs = c->response;
603 wrbuf_rewind(c->wrbuf);
604 wrbuf_puts(c->wrbuf, "<info>\n");
605 wrbuf_printf(c->wrbuf, " <version>\n");
606 wrbuf_printf(c->wrbuf, " <pazpar2>%s</pazpar2>\n", VERSION);
608 yaz_version(yaz_version_str, 0);
609 wrbuf_printf(c->wrbuf, " <yaz compiled=\"%s\">%s</yaz>\n",
610 YAZ_VERSION, yaz_version_str);
611 wrbuf_printf(c->wrbuf, " </version>\n");
613 wrbuf_puts(c->wrbuf, "</info>");
614 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
615 http_send_response(c);
620 void (*fun)(struct http_channel *c);
622 { "init", cmd_init },
623 { "settings", cmd_settings },
624 { "stat", cmd_stat },
625 { "bytarget", cmd_bytarget },
626 { "show", cmd_show },
627 { "search", cmd_search },
628 { "termlist", cmd_termlist },
629 { "exit", cmd_exit },
630 { "ping", cmd_ping },
631 { "record", cmd_record },
632 { "info", cmd_info },
636 void http_command(struct http_channel *c)
638 char *command = http_argbyname(c->request, "command");
639 struct http_response *rs = http_create_response(c);
644 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
645 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
649 error(rs, "417", "Must supply command", 0);
652 for (i = 0; commands[i].name; i++)
653 if (!strcmp(commands[i].name, command))
655 (*commands[i].fun)(c);
658 if (!commands[i].name)
659 error(rs, "417", "Unknown command", 0);
667 * indent-tabs-mode: nil
669 * vim: shiftwidth=4 tabstop=8 expandtab