1 /* $Id: http_command.c,v 1.35 2007-04-15 03:26:47 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.35 2007-04-15 03:26:47 quinn Exp $
27 #include <sys/types.h>
39 #include <yaz/yaz-util.h>
46 #include "http_command.h"
49 extern struct parameters global_parameters;
50 extern IOCHAN channel_list;
53 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
54 struct session *psession;
55 unsigned int session_id;
58 struct http_session *next;
61 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);
85 r->timeout_iochan->next = channel_list;
86 channel_list = 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 iochan_destroy(s->timeout_iochan);
101 destroy_session(s->psession);
102 nmem_destroy(s->nmem);
105 static void error(struct http_response *rs, char *code, char *msg, char *txt)
107 struct http_channel *c = rs->channel;
112 rs->msg = nmem_strdup(c->nmem, msg);
113 strcpy(rs->code, code);
114 sprintf(tmp, "<error code=\"general\">%s</error>", txt);
115 rs->payload = nmem_strdup(c->nmem, tmp);
116 http_send_response(c);
119 unsigned int make_sessionid()
126 if (gettimeofday(&t, 0) < 0)
129 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
133 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
135 struct http_session *p;
136 char *session = http_argbyname(rq, "session");
141 error(rs, "417", "Must supply session", 0);
145 for (p = session_list; p; p = p->next)
146 if (id == p->session_id)
148 iochan_activity(p->timeout_iochan);
151 error(rs, "417", "Session does not exist, or it has expired", 0);
155 // Decode settings parameters and apply to session
156 // Syntax: setting[target]=value
157 static int process_settings(struct session *se, struct http_request *rq,
158 struct http_response *rs)
160 struct http_argument *a;
162 for (a = rq->arguments; a; a = a->next)
163 if (strchr(a->name, '['))
170 // Nmem_strsplit *rules*!!!
171 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
174 error(rs, "417", "Malformed setting argument", 0);
175 yaz_log(YLOG_WARN, "Malformed setting: %s", a->name);
180 session_apply_setting(se, dbname, setting,
181 nmem_strdup(se->session_nmem, a->value));
186 static void cmd_exit(struct http_channel *c)
188 yaz_log(YLOG_WARN, "exit");
192 static void cmd_init(struct http_channel *c)
196 struct http_session *s = http_session_create();
197 struct http_response *rs = c->response;
199 yaz_log(YLOG_DEBUG, "HTTP Session init");
200 sesid = make_sessionid();
201 s->session_id = sesid;
202 if (process_settings(s->psession, c->request, c->response) < 0)
204 sprintf(buf, "<init><status>OK</status><session>%u</session></init>", sesid);
205 rs->payload = nmem_strdup(c->nmem, buf);
206 http_send_response(c);
209 static void cmd_settings(struct http_channel *c)
211 struct http_response *rs = c->response;
212 struct http_request *rq = c->request;
213 struct http_session *s = locate_session(rq, rs);
218 if (process_settings(s->psession, rq, rs) < 0)
220 rs->payload = "<settings><status>OK</status></settings>";
221 http_send_response(c);
224 // Compares two hitsbytarget nodes by hitcount
225 static int cmp_ht(const void *p1, const void *p2)
227 const struct hitsbytarget *h1 = p1;
228 const struct hitsbytarget *h2 = p2;
229 return h2->hits - h1->hits;
232 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
233 static void targets_termlist(WRBUF wrbuf, struct session *se, int num)
235 struct hitsbytarget *ht;
238 if (!(ht = hitsbytarget(se, &count)))
240 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
241 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
243 wrbuf_puts(wrbuf, "\n<term>\n");
244 wrbuf_printf(wrbuf, "<id>%s</id>\n", ht[i].id);
245 wrbuf_printf(wrbuf, "<name>%s</name>\n", ht[i].name);
246 wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
247 wrbuf_printf(wrbuf, "<state>%s</state>\n", ht[i].state);
248 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
249 wrbuf_puts(wrbuf, "\n</term>\n");
253 static void cmd_termlist(struct http_channel *c)
255 struct http_response *rs = c->response;
256 struct http_request *rq = c->request;
257 struct http_session *s = locate_session(rq, rs);
258 struct termlist_score **p;
261 char *name = http_argbyname(rq, "name");
262 char *nums = http_argbyname(rq, "num");
269 status = session_active_clients(s->psession);
273 if (strlen(name) > 255)
278 wrbuf_rewind(c->wrbuf);
280 wrbuf_puts(c->wrbuf, "<termlist>");
281 wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>", status);
287 if (!(tp = strchr(name, ',')))
288 tp = name + strlen(name);
289 strncpy(tname, name, tp - name);
290 tname[tp - name] = '\0';
292 wrbuf_printf(c->wrbuf, "\n<list name=\"%s\">\n", tname);
293 if (!strcmp(tname, "xtargets"))
294 targets_termlist(c->wrbuf, s->psession, num);
297 p = termlist(s->psession, tname, &len);
299 for (i = 0; i < len && i < num; i++)
301 wrbuf_puts(c->wrbuf, "\n<term>");
302 wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
303 wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
304 wrbuf_puts(c->wrbuf, "</term>");
307 wrbuf_puts(c->wrbuf, "\n</list>");
312 wrbuf_puts(c->wrbuf, "</termlist>");
313 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
314 http_send_response(c);
318 static void cmd_bytarget(struct http_channel *c)
320 struct http_response *rs = c->response;
321 struct http_request *rq = c->request;
322 struct http_session *s = locate_session(rq, rs);
323 struct hitsbytarget *ht;
328 if (!(ht = hitsbytarget(s->psession, &count)))
330 error(rs, "500", "Failed to retrieve hitcounts", 0);
333 wrbuf_rewind(c->wrbuf);
334 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
336 for (i = 0; i < count; i++)
338 wrbuf_puts(c->wrbuf, "\n<target>");
339 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
340 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
341 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
342 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
343 wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
344 wrbuf_puts(c->wrbuf, "</target>");
347 wrbuf_puts(c->wrbuf, "</bytarget>");
348 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
349 http_send_response(c);
352 static void write_metadata(WRBUF w, struct conf_service *service,
353 struct record_metadata **ml, int full)
357 for (imeta = 0; imeta < service->num_metadata; imeta++)
359 struct conf_metadata *cmd = &service->metadata[imeta];
360 struct record_metadata *md;
361 if (!cmd->brief && !full)
363 for (md = ml[imeta]; md; md = md->next)
365 wrbuf_printf(w, "<md-%s>", cmd->name);
368 case Metadata_type_generic:
369 wrbuf_puts(w, md->data.text);
371 case Metadata_type_year:
372 wrbuf_printf(w, "%d", md->data.number.min);
373 if (md->data.number.min != md->data.number.max)
374 wrbuf_printf(w, "-%d", md->data.number.max);
377 wrbuf_puts(w, "[can't represent]");
379 wrbuf_printf(w, "</md-%s>", cmd->name);
384 static void write_subrecord(struct record *r, WRBUF w,
385 struct conf_service *service, int show_details)
387 char *name = session_setting_oneval(r->client->database, PZ_NAME);
389 wrbuf_printf(w, "<location id=\"%s\" name=\"%s\">\n",
390 r->client->database->database->url,
391 *name ? name : "Unknown");
393 write_metadata(w, service, r->metadata, 1);
394 wrbuf_puts(w, "</location>\n");
397 static void cmd_record(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 record_cluster *rec;
404 struct conf_service *service = global_parameters.server->service;
405 char *idstr = http_argbyname(rq, "id");
412 error(rs, "417", "Must supply id", 0);
415 wrbuf_rewind(c->wrbuf);
417 if (!(rec = show_single(s->psession, id)))
419 error(rs, "500", "Record missing", 0);
422 wrbuf_puts(c->wrbuf, "<record>\n");
423 wrbuf_printf(c->wrbuf, "<recid>%d</recid>", rec->recid);
424 write_metadata(c->wrbuf, service, rec->metadata, 1);
425 for (r = rec->records; r; r = r->next)
426 write_subrecord(r, c->wrbuf, service, 1);
427 wrbuf_puts(c->wrbuf, "</record>\n");
428 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
429 http_send_response(c);
432 static void show_records(struct http_channel *c, int active)
434 struct http_request *rq = c->request;
435 struct http_response *rs = c->response;
436 struct http_session *s = locate_session(rq, rs);
437 struct record_cluster **rl;
438 struct reclist_sortparms *sp;
439 char *start = http_argbyname(rq, "start");
440 char *num = http_argbyname(rq, "num");
441 char *sort = http_argbyname(rq, "sort");
451 // We haven't counted clients yet if we're called on a block release
453 active = session_active_clients(s->psession);
456 startn = atoi(start);
461 if (!(sp = reclist_parse_sortparms(c->nmem, sort)))
463 error(rs, "500", "Bad sort parameters", 0);
467 rl = show(s->psession, sp, startn, &numn, &total, &total_hits, c->nmem);
469 wrbuf_rewind(c->wrbuf);
470 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
471 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
472 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
473 wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
474 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
475 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
477 for (i = 0; i < numn; i++)
481 struct record_cluster *rec = rl[i];
482 struct conf_service *service = global_parameters.server->service;
484 wrbuf_puts(c->wrbuf, "<hit>\n");
485 write_metadata(c->wrbuf, service, rec->metadata, 0);
486 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
487 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
489 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
490 wrbuf_printf(c->wrbuf, "<recid>%d</recid>\n", rec->recid);
491 wrbuf_puts(c->wrbuf, "</hit>\n");
494 wrbuf_puts(c->wrbuf, "</show>\n");
495 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
496 http_send_response(c);
499 static void show_records_ready(void *data)
501 struct http_channel *c = (struct http_channel *) data;
506 static void cmd_show(struct http_channel *c)
508 struct http_request *rq = c->request;
509 struct http_response *rs = c->response;
510 struct http_session *s = locate_session(rq, rs);
511 char *block = http_argbyname(rq, "block");
517 status = session_active_clients(s->psession);
521 if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
523 session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
524 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
529 show_records(c, status);
532 static void cmd_ping(struct http_channel *c)
534 struct http_request *rq = c->request;
535 struct http_response *rs = c->response;
536 struct http_session *s = locate_session(rq, rs);
539 rs->payload = "<ping><status>OK</status></ping>";
540 http_send_response(c);
543 static void cmd_search(struct http_channel *c)
545 struct http_request *rq = c->request;
546 struct http_response *rs = c->response;
547 struct http_session *s = locate_session(rq, rs);
548 char *query = http_argbyname(rq, "query");
549 char *filter = http_argbyname(rq, "filter");
556 error(rs, "417", "Must supply query", 0);
559 res = search(s->psession, query, filter);
562 error(rs, "417", res, res);
565 rs->payload = "<search><status>OK</status></search>";
566 http_send_response(c);
570 static void cmd_stat(struct http_channel *c)
572 struct http_request *rq = c->request;
573 struct http_response *rs = c->response;
574 struct http_session *s = locate_session(rq, rs);
575 struct statistics stat;
581 clients = session_active_clients(s->psession);
582 statistics(s->psession, &stat);
584 wrbuf_rewind(c->wrbuf);
585 wrbuf_puts(c->wrbuf, "<stat>");
586 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
587 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
588 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
589 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
590 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
591 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
592 wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
593 wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
594 wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
595 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
596 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
597 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
598 wrbuf_puts(c->wrbuf, "</stat>");
599 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
600 http_send_response(c);
603 static void cmd_info(struct http_channel *c)
605 char yaz_version_str[20];
606 struct http_response *rs = c->response;
608 wrbuf_rewind(c->wrbuf);
609 wrbuf_puts(c->wrbuf, "<info>\n");
610 wrbuf_printf(c->wrbuf, " <version>\n");
611 wrbuf_printf(c->wrbuf, " <pazpar2>%s</pazpar2>\n", VERSION);
613 yaz_version(yaz_version_str, 0);
614 wrbuf_printf(c->wrbuf, " <yaz compiled=\"%s\">%s</yaz>\n",
615 YAZ_VERSION, yaz_version_str);
616 wrbuf_printf(c->wrbuf, " </version>\n");
618 wrbuf_puts(c->wrbuf, "</info>");
619 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
620 http_send_response(c);
625 void (*fun)(struct http_channel *c);
627 { "init", cmd_init },
628 { "settings", cmd_settings },
629 { "stat", cmd_stat },
630 { "bytarget", cmd_bytarget },
631 { "show", cmd_show },
632 { "search", cmd_search },
633 { "termlist", cmd_termlist },
634 { "exit", cmd_exit },
635 { "ping", cmd_ping },
636 { "record", cmd_record },
637 { "info", cmd_info },
641 void http_command(struct http_channel *c)
643 char *command = http_argbyname(c->request, "command");
644 struct http_response *rs = http_create_response(c);
649 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
650 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
654 error(rs, "417", "Must supply command", 0);
657 for (i = 0; commands[i].name; i++)
658 if (!strcmp(commands[i].name, command))
660 (*commands[i].fun)(c);
663 if (!commands[i].name)
664 error(rs, "417", "Unknown command", 0);
672 * indent-tabs-mode: nil
674 * vim: shiftwidth=4 tabstop=8 expandtab