1 /* $Id: http_command.c,v 1.32 2007-04-10 08:48:56 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.32 2007-04-10 08:48:56 adam 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 static void cmd_exit(struct http_channel *c)
156 yaz_log(YLOG_WARN, "exit");
161 static void cmd_init(struct http_channel *c)
165 struct http_session *s = http_session_create();
166 struct http_response *rs = c->response;
168 yaz_log(YLOG_DEBUG, "HTTP Session init");
169 sesid = make_sessionid();
170 s->session_id = sesid;
171 sprintf(buf, "<init><status>OK</status><session>%u</session></init>", sesid);
172 rs->payload = nmem_strdup(c->nmem, buf);
173 http_send_response(c);
176 // Compares two hitsbytarget nodes by hitcount
177 static int cmp_ht(const void *p1, const void *p2)
179 const struct hitsbytarget *h1 = p1;
180 const struct hitsbytarget *h2 = p2;
181 return h2->hits - h1->hits;
184 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
185 static void targets_termlist(WRBUF wrbuf, struct session *se, int num)
187 struct hitsbytarget *ht;
190 if (!(ht = hitsbytarget(se, &count)))
192 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
193 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
195 wrbuf_puts(wrbuf, "\n<term>\n");
196 wrbuf_printf(wrbuf, "<id>%s</id>\n", ht[i].id);
197 wrbuf_printf(wrbuf, "<name>%s</name>\n", ht[i].name);
198 wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
199 wrbuf_printf(wrbuf, "<state>%s</state>\n", ht[i].state);
200 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
201 wrbuf_puts(wrbuf, "\n</term>\n");
205 static void cmd_termlist(struct http_channel *c)
207 struct http_response *rs = c->response;
208 struct http_request *rq = c->request;
209 struct http_session *s = locate_session(rq, rs);
210 struct termlist_score **p;
213 char *name = http_argbyname(rq, "name");
214 char *nums = http_argbyname(rq, "num");
221 status = session_active_clients(s->psession);
225 if (strlen(name) > 255)
230 wrbuf_rewind(c->wrbuf);
232 wrbuf_puts(c->wrbuf, "<termlist>");
233 wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>", status);
239 if (!(tp = strchr(name, ',')))
240 tp = name + strlen(name);
241 strncpy(tname, name, tp - name);
242 tname[tp - name] = '\0';
244 wrbuf_printf(c->wrbuf, "\n<list name=\"%s\">\n", tname);
245 if (!strcmp(tname, "xtargets"))
246 targets_termlist(c->wrbuf, s->psession, num);
249 p = termlist(s->psession, tname, &len);
251 for (i = 0; i < len && i < num; i++)
253 wrbuf_puts(c->wrbuf, "\n<term>");
254 wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
255 wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
256 wrbuf_puts(c->wrbuf, "</term>");
259 wrbuf_puts(c->wrbuf, "\n</list>");
264 wrbuf_puts(c->wrbuf, "</termlist>");
265 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
266 http_send_response(c);
270 static void cmd_bytarget(struct http_channel *c)
272 struct http_response *rs = c->response;
273 struct http_request *rq = c->request;
274 struct http_session *s = locate_session(rq, rs);
275 struct hitsbytarget *ht;
280 if (!(ht = hitsbytarget(s->psession, &count)))
282 error(rs, "500", "Failed to retrieve hitcounts", 0);
285 wrbuf_rewind(c->wrbuf);
286 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
288 for (i = 0; i < count; i++)
290 wrbuf_puts(c->wrbuf, "\n<target>");
291 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
292 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
293 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
294 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
295 wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
296 wrbuf_puts(c->wrbuf, "</target>");
299 wrbuf_puts(c->wrbuf, "</bytarget>");
300 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
301 http_send_response(c);
304 static void write_metadata(WRBUF w, struct conf_service *service,
305 struct record_metadata **ml, int full)
309 for (imeta = 0; imeta < service->num_metadata; imeta++)
311 struct conf_metadata *cmd = &service->metadata[imeta];
312 struct record_metadata *md;
313 if (!cmd->brief && !full)
315 for (md = ml[imeta]; md; md = md->next)
317 wrbuf_printf(w, "<md-%s>", cmd->name);
320 case Metadata_type_generic:
321 wrbuf_puts(w, md->data.text);
323 case Metadata_type_year:
324 wrbuf_printf(w, "%d", md->data.number.min);
325 if (md->data.number.min != md->data.number.max)
326 wrbuf_printf(w, "-%d", md->data.number.max);
329 wrbuf_puts(w, "[can't represent]");
331 wrbuf_printf(w, "</md-%s>", cmd->name);
336 static void write_subrecord(struct record *r, WRBUF w, struct conf_service *service)
338 wrbuf_printf(w, "<location id=\"%s\" name=\"%s\">\n",
339 r->client->database->url,
340 r->client->database->name ? r->client->database->name : "");
341 write_metadata(w, service, r->metadata, 1);
342 wrbuf_puts(w, "</location>\n");
345 static void cmd_record(struct http_channel *c)
347 struct http_response *rs = c->response;
348 struct http_request *rq = c->request;
349 struct http_session *s = locate_session(rq, rs);
350 struct record_cluster *rec;
352 struct conf_service *service = global_parameters.server->service;
353 char *idstr = http_argbyname(rq, "id");
360 error(rs, "417", "Must supply id", 0);
363 wrbuf_rewind(c->wrbuf);
365 if (!(rec = show_single(s->psession, id)))
367 error(rs, "500", "Record missing", 0);
370 wrbuf_puts(c->wrbuf, "<record>\n");
371 wrbuf_printf(c->wrbuf, "<recid>%d</recid>", rec->recid);
372 write_metadata(c->wrbuf, service, rec->metadata, 1);
373 for (r = rec->records; r; r = r->next)
374 write_subrecord(r, c->wrbuf, service);
375 wrbuf_puts(c->wrbuf, "</record>\n");
376 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
377 http_send_response(c);
380 static void show_records(struct http_channel *c, int active)
382 struct http_request *rq = c->request;
383 struct http_response *rs = c->response;
384 struct http_session *s = locate_session(rq, rs);
385 struct record_cluster **rl;
386 struct reclist_sortparms *sp;
387 char *start = http_argbyname(rq, "start");
388 char *num = http_argbyname(rq, "num");
389 char *sort = http_argbyname(rq, "sort");
399 // We haven't counted clients yet if we're called on a block release
401 active = session_active_clients(s->psession);
404 startn = atoi(start);
409 if (!(sp = reclist_parse_sortparms(c->nmem, sort)))
411 error(rs, "500", "Bad sort parameters", 0);
415 rl = show(s->psession, sp, startn, &numn, &total, &total_hits, c->nmem);
417 wrbuf_rewind(c->wrbuf);
418 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
419 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
420 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
421 wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
422 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
423 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
425 for (i = 0; i < numn; i++)
429 struct record_cluster *rec = rl[i];
430 struct conf_service *service = global_parameters.server->service;
432 wrbuf_puts(c->wrbuf, "<hit>\n");
433 write_metadata(c->wrbuf, service, rec->metadata, 0);
434 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
437 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
438 wrbuf_printf(c->wrbuf, "<recid>%d</recid>\n", rec->recid);
439 wrbuf_puts(c->wrbuf, "</hit>\n");
442 wrbuf_puts(c->wrbuf, "</show>\n");
443 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
444 http_send_response(c);
447 static void show_records_ready(void *data)
449 struct http_channel *c = (struct http_channel *) data;
454 static void cmd_show(struct http_channel *c)
456 struct http_request *rq = c->request;
457 struct http_response *rs = c->response;
458 struct http_session *s = locate_session(rq, rs);
459 char *block = http_argbyname(rq, "block");
465 status = session_active_clients(s->psession);
469 if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
471 session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
472 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
477 show_records(c, status);
480 static void cmd_ping(struct http_channel *c)
482 struct http_request *rq = c->request;
483 struct http_response *rs = c->response;
484 struct http_session *s = locate_session(rq, rs);
487 rs->payload = "<ping><status>OK</status></ping>";
488 http_send_response(c);
491 static void cmd_search(struct http_channel *c)
493 struct http_request *rq = c->request;
494 struct http_response *rs = c->response;
495 struct http_session *s = locate_session(rq, rs);
496 char *query = http_argbyname(rq, "query");
497 char *filter = http_argbyname(rq, "filter");
504 error(rs, "417", "Must supply query", 0);
507 res = search(s->psession, query, filter);
510 error(rs, "417", res, res);
513 rs->payload = "<search><status>OK</status></search>";
514 http_send_response(c);
518 static void cmd_stat(struct http_channel *c)
520 struct http_request *rq = c->request;
521 struct http_response *rs = c->response;
522 struct http_session *s = locate_session(rq, rs);
523 struct statistics stat;
529 clients = session_active_clients(s->psession);
530 statistics(s->psession, &stat);
532 wrbuf_rewind(c->wrbuf);
533 wrbuf_puts(c->wrbuf, "<stat>");
534 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
535 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
536 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
537 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
538 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
539 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
540 wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
541 wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
542 wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
543 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
544 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
545 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
546 wrbuf_puts(c->wrbuf, "</stat>");
547 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
548 http_send_response(c);
551 static void cmd_info(struct http_channel *c)
553 char yaz_version_str[20];
554 struct http_response *rs = c->response;
556 wrbuf_rewind(c->wrbuf);
557 wrbuf_puts(c->wrbuf, "<info>\n");
558 wrbuf_printf(c->wrbuf, " <version>\n");
559 wrbuf_printf(c->wrbuf, " <pazpar2>%s</pazpar2>\n", VERSION);
561 yaz_version(yaz_version_str, 0);
562 wrbuf_printf(c->wrbuf, " <yaz compiled=\"%s\">%s</yaz>\n",
563 YAZ_VERSION, yaz_version_str);
564 wrbuf_printf(c->wrbuf, " </version>\n");
566 wrbuf_puts(c->wrbuf, "</info>");
567 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
568 http_send_response(c);
573 void (*fun)(struct http_channel *c);
575 { "init", cmd_init },
576 { "stat", cmd_stat },
577 { "bytarget", cmd_bytarget },
578 { "show", cmd_show },
579 { "search", cmd_search },
580 { "termlist", cmd_termlist },
581 { "exit", cmd_exit },
582 { "ping", cmd_ping },
583 { "record", cmd_record },
584 { "info", cmd_info },
588 void http_command(struct http_channel *c)
590 char *command = http_argbyname(c->request, "command");
591 struct http_response *rs = http_create_response(c);
596 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
597 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
601 error(rs, "417", "Must supply command", 0);
604 for (i = 0; commands[i].name; i++)
605 if (!strcmp(commands[i].name, command))
607 (*commands[i].fun)(c);
610 if (!commands[i].name)
611 error(rs, "417", "Unknown command", 0);
619 * indent-tabs-mode: nil
621 * vim: shiftwidth=4 tabstop=8 expandtab