1 /* $Id: http_command.c,v 1.37 2007-04-16 21:11:26 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.37 2007-04-16 21:11:26 quinn Exp $
27 #include <sys/types.h>
39 #include <yaz/yaz-util.h>
46 #include "http_command.h"
49 // Update this when the protocol changes
50 #define PAZPAR2_PROTOCOL_VERSION "1"
52 extern struct parameters global_parameters;
53 extern IOCHAN channel_list;
56 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
57 struct session *psession;
58 unsigned int session_id;
61 struct http_session *next;
64 static struct http_session *session_list = 0;
66 void http_session_destroy(struct http_session *s);
68 static void session_timeout(IOCHAN i, int event)
70 struct http_session *s = iochan_getdata(i);
71 http_session_destroy(s);
74 struct http_session *http_session_create()
76 NMEM nmem = nmem_create();
77 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
79 r->psession = new_session(nmem);
83 r->next = session_list;
85 r->timeout_iochan = iochan_create(-1, session_timeout, 0);
86 iochan_setdata(r->timeout_iochan, r);
87 iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout);
88 r->timeout_iochan->next = channel_list;
89 channel_list = r->timeout_iochan;
93 void http_session_destroy(struct http_session *s)
95 struct http_session **p;
97 for (p = &session_list; *p; p = &(*p)->next)
103 iochan_destroy(s->timeout_iochan);
104 destroy_session(s->psession);
105 nmem_destroy(s->nmem);
108 static void error(struct http_response *rs, char *code, char *msg, char *txt)
110 struct http_channel *c = rs->channel;
115 rs->msg = nmem_strdup(c->nmem, msg);
116 strcpy(rs->code, code);
117 sprintf(tmp, "<error code=\"general\">%s</error>", txt);
118 rs->payload = nmem_strdup(c->nmem, tmp);
119 http_send_response(c);
122 unsigned int make_sessionid()
129 if (gettimeofday(&t, 0) < 0)
132 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
136 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
138 struct http_session *p;
139 char *session = http_argbyname(rq, "session");
144 error(rs, "417", "Must supply session", 0);
148 for (p = session_list; p; p = p->next)
149 if (id == p->session_id)
151 iochan_activity(p->timeout_iochan);
154 error(rs, "417", "Session does not exist, or it has expired", 0);
158 // Decode settings parameters and apply to session
159 // Syntax: setting[target]=value
160 static int process_settings(struct session *se, struct http_request *rq,
161 struct http_response *rs)
163 struct http_argument *a;
165 for (a = rq->arguments; a; a = a->next)
166 if (strchr(a->name, '['))
173 // Nmem_strsplit *rules*!!!
174 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
177 error(rs, "417", "Malformed setting argument", 0);
178 yaz_log(YLOG_WARN, "Malformed setting: %s", a->name);
183 session_apply_setting(se, dbname, setting,
184 nmem_strdup(se->session_nmem, a->value));
189 static void cmd_exit(struct http_channel *c)
191 yaz_log(YLOG_WARN, "exit");
195 static void cmd_init(struct http_channel *c)
199 struct http_session *s = http_session_create();
200 struct http_response *rs = c->response;
202 yaz_log(YLOG_DEBUG, "HTTP Session init");
203 sesid = make_sessionid();
204 s->session_id = sesid;
205 if (process_settings(s->psession, c->request, c->response) < 0)
207 sprintf(buf, "<init><status>OK</status><session>%u</session>"
208 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol>"
209 "<id>$Id: http_command.c,v 1.37 2007-04-16 21:11:26 quinn Exp $</id></init>", sesid);
210 rs->payload = nmem_strdup(c->nmem, buf);
211 http_send_response(c);
214 static void cmd_settings(struct http_channel *c)
216 struct http_response *rs = c->response;
217 struct http_request *rq = c->request;
218 struct http_session *s = locate_session(rq, rs);
223 if (process_settings(s->psession, rq, rs) < 0)
225 rs->payload = "<settings><status>OK</status></settings>";
226 http_send_response(c);
229 // Compares two hitsbytarget nodes by hitcount
230 static int cmp_ht(const void *p1, const void *p2)
232 const struct hitsbytarget *h1 = p1;
233 const struct hitsbytarget *h2 = p2;
234 return h2->hits - h1->hits;
237 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
238 static void targets_termlist(WRBUF wrbuf, struct session *se, int num)
240 struct hitsbytarget *ht;
243 if (!(ht = hitsbytarget(se, &count)))
245 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
246 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
248 wrbuf_puts(wrbuf, "\n<term>\n");
249 wrbuf_printf(wrbuf, "<id>%s</id>\n", ht[i].id);
250 wrbuf_printf(wrbuf, "<name>%s</name>\n", ht[i].name);
251 wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
252 wrbuf_printf(wrbuf, "<state>%s</state>\n", ht[i].state);
253 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
254 wrbuf_puts(wrbuf, "\n</term>\n");
258 static void cmd_termlist(struct http_channel *c)
260 struct http_response *rs = c->response;
261 struct http_request *rq = c->request;
262 struct http_session *s = locate_session(rq, rs);
263 struct termlist_score **p;
266 char *name = http_argbyname(rq, "name");
267 char *nums = http_argbyname(rq, "num");
274 status = session_active_clients(s->psession);
278 if (strlen(name) > 255)
283 wrbuf_rewind(c->wrbuf);
285 wrbuf_puts(c->wrbuf, "<termlist>");
286 wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>", status);
292 if (!(tp = strchr(name, ',')))
293 tp = name + strlen(name);
294 strncpy(tname, name, tp - name);
295 tname[tp - name] = '\0';
297 wrbuf_printf(c->wrbuf, "\n<list name=\"%s\">\n", tname);
298 if (!strcmp(tname, "xtargets"))
299 targets_termlist(c->wrbuf, s->psession, num);
302 p = termlist(s->psession, tname, &len);
304 for (i = 0; i < len && i < num; i++)
306 wrbuf_puts(c->wrbuf, "\n<term>");
307 wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
308 wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
309 wrbuf_puts(c->wrbuf, "</term>");
312 wrbuf_puts(c->wrbuf, "\n</list>");
317 wrbuf_puts(c->wrbuf, "</termlist>");
318 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
319 http_send_response(c);
323 static void cmd_bytarget(struct http_channel *c)
325 struct http_response *rs = c->response;
326 struct http_request *rq = c->request;
327 struct http_session *s = locate_session(rq, rs);
328 struct hitsbytarget *ht;
333 if (!(ht = hitsbytarget(s->psession, &count)))
335 error(rs, "500", "Failed to retrieve hitcounts", 0);
338 wrbuf_rewind(c->wrbuf);
339 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
341 for (i = 0; i < count; i++)
343 wrbuf_puts(c->wrbuf, "\n<target>");
344 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
345 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
346 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
347 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
348 wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
349 wrbuf_puts(c->wrbuf, "</target>");
352 wrbuf_puts(c->wrbuf, "</bytarget>");
353 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
354 http_send_response(c);
357 static void write_metadata(WRBUF w, struct conf_service *service,
358 struct record_metadata **ml, int full)
362 for (imeta = 0; imeta < service->num_metadata; imeta++)
364 struct conf_metadata *cmd = &service->metadata[imeta];
365 struct record_metadata *md;
366 if (!cmd->brief && !full)
368 for (md = ml[imeta]; md; md = md->next)
370 wrbuf_printf(w, "\n<md-%s>", cmd->name);
373 case Metadata_type_generic:
374 wrbuf_puts(w, md->data.text);
376 case Metadata_type_year:
377 wrbuf_printf(w, "%d", md->data.number.min);
378 if (md->data.number.min != md->data.number.max)
379 wrbuf_printf(w, "-%d", md->data.number.max);
382 wrbuf_puts(w, "[can't represent]");
384 wrbuf_printf(w, "</md-%s>", cmd->name);
389 static void write_subrecord(struct record *r, WRBUF w,
390 struct conf_service *service, int show_details)
392 char *name = session_setting_oneval(r->client->database, PZ_NAME);
394 wrbuf_printf(w, "<location id=\"%s\" name=\"%s\">",
395 r->client->database->database->url,
396 *name ? name : "Unknown");
398 write_metadata(w, service, r->metadata, 1);
399 wrbuf_puts(w, "</location>\n");
402 static void cmd_record(struct http_channel *c)
404 struct http_response *rs = c->response;
405 struct http_request *rq = c->request;
406 struct http_session *s = locate_session(rq, rs);
407 struct record_cluster *rec;
409 struct conf_service *service = global_parameters.server->service;
410 char *idstr = http_argbyname(rq, "id");
417 error(rs, "417", "Must supply id", 0);
420 wrbuf_rewind(c->wrbuf);
422 if (!(rec = show_single(s->psession, id)))
424 error(rs, "500", "Record missing", 0);
427 wrbuf_puts(c->wrbuf, "<record>\n");
428 wrbuf_printf(c->wrbuf, "<recid>%d</recid>", rec->recid);
429 write_metadata(c->wrbuf, service, rec->metadata, 1);
430 for (r = rec->records; r; r = r->next)
431 write_subrecord(r, c->wrbuf, service, 1);
432 wrbuf_puts(c->wrbuf, "</record>\n");
433 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
434 http_send_response(c);
437 static void show_records(struct http_channel *c, int active)
439 struct http_request *rq = c->request;
440 struct http_response *rs = c->response;
441 struct http_session *s = locate_session(rq, rs);
442 struct record_cluster **rl;
443 struct reclist_sortparms *sp;
444 char *start = http_argbyname(rq, "start");
445 char *num = http_argbyname(rq, "num");
446 char *sort = http_argbyname(rq, "sort");
456 // We haven't counted clients yet if we're called on a block release
458 active = session_active_clients(s->psession);
461 startn = atoi(start);
466 if (!(sp = reclist_parse_sortparms(c->nmem, sort)))
468 error(rs, "500", "Bad sort parameters", 0);
472 rl = show(s->psession, sp, startn, &numn, &total, &total_hits, c->nmem);
474 wrbuf_rewind(c->wrbuf);
475 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
476 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
477 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
478 wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
479 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
480 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
482 for (i = 0; i < numn; i++)
486 struct record_cluster *rec = rl[i];
487 struct conf_service *service = global_parameters.server->service;
489 wrbuf_puts(c->wrbuf, "<hit>\n");
490 write_metadata(c->wrbuf, service, rec->metadata, 0);
491 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
492 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
494 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
495 wrbuf_printf(c->wrbuf, "<recid>%d</recid>\n", rec->recid);
496 wrbuf_puts(c->wrbuf, "</hit>\n");
499 wrbuf_puts(c->wrbuf, "</show>\n");
500 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
501 http_send_response(c);
504 static void show_records_ready(void *data)
506 struct http_channel *c = (struct http_channel *) data;
511 static void cmd_show(struct http_channel *c)
513 struct http_request *rq = c->request;
514 struct http_response *rs = c->response;
515 struct http_session *s = locate_session(rq, rs);
516 char *block = http_argbyname(rq, "block");
522 status = session_active_clients(s->psession);
526 if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
528 session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
529 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
534 show_records(c, status);
537 static void cmd_ping(struct http_channel *c)
539 struct http_request *rq = c->request;
540 struct http_response *rs = c->response;
541 struct http_session *s = locate_session(rq, rs);
544 rs->payload = "<ping><status>OK</status></ping>";
545 http_send_response(c);
548 static void cmd_search(struct http_channel *c)
550 struct http_request *rq = c->request;
551 struct http_response *rs = c->response;
552 struct http_session *s = locate_session(rq, rs);
553 char *query = http_argbyname(rq, "query");
554 char *filter = http_argbyname(rq, "filter");
561 error(rs, "417", "Must supply query", 0);
564 res = search(s->psession, query, filter);
567 error(rs, "417", res, res);
570 rs->payload = "<search><status>OK</status></search>";
571 http_send_response(c);
575 static void cmd_stat(struct http_channel *c)
577 struct http_request *rq = c->request;
578 struct http_response *rs = c->response;
579 struct http_session *s = locate_session(rq, rs);
580 struct statistics stat;
586 clients = session_active_clients(s->psession);
587 statistics(s->psession, &stat);
589 wrbuf_rewind(c->wrbuf);
590 wrbuf_puts(c->wrbuf, "<stat>");
591 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
592 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
593 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
594 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
595 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
596 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
597 wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
598 wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
599 wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
600 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
601 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
602 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
603 wrbuf_puts(c->wrbuf, "</stat>");
604 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
605 http_send_response(c);
608 static void cmd_info(struct http_channel *c)
610 char yaz_version_str[20];
611 struct http_response *rs = c->response;
613 wrbuf_rewind(c->wrbuf);
614 wrbuf_puts(c->wrbuf, "<info>\n");
615 wrbuf_printf(c->wrbuf, " <version>\n");
616 wrbuf_printf(c->wrbuf, " <pazpar2>%s</pazpar2>\n", VERSION);
618 yaz_version(yaz_version_str, 0);
619 wrbuf_printf(c->wrbuf, " <yaz compiled=\"%s\">%s</yaz>\n",
620 YAZ_VERSION, yaz_version_str);
621 wrbuf_printf(c->wrbuf, " </version>\n");
623 wrbuf_puts(c->wrbuf, "</info>");
624 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
625 http_send_response(c);
630 void (*fun)(struct http_channel *c);
632 { "init", cmd_init },
633 { "settings", cmd_settings },
634 { "stat", cmd_stat },
635 { "bytarget", cmd_bytarget },
636 { "show", cmd_show },
637 { "search", cmd_search },
638 { "termlist", cmd_termlist },
639 { "exit", cmd_exit },
640 { "ping", cmd_ping },
641 { "record", cmd_record },
642 { "info", cmd_info },
646 void http_command(struct http_channel *c)
648 char *command = http_argbyname(c->request, "command");
649 struct http_response *rs = http_create_response(c);
654 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
655 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
659 error(rs, "417", "Must supply command", 0);
662 for (i = 0; commands[i].name; i++)
663 if (!strcmp(commands[i].name, command))
665 (*commands[i].fun)(c);
668 if (!commands[i].name)
669 error(rs, "417", "Unknown command", 0);
677 * indent-tabs-mode: nil
679 * vim: shiftwidth=4 tabstop=8 expandtab