2 * $Id: http_command.c,v 1.31 2007-04-10 00:53:24 quinn Exp $
18 #include <yaz/yaz-util.h>
25 #include "http_command.h"
27 extern struct parameters global_parameters;
28 extern IOCHAN channel_list;
31 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
32 struct session *psession;
33 unsigned int session_id;
36 struct http_session *next;
39 static struct http_session *session_list = 0;
41 void http_session_destroy(struct http_session *s);
43 static void session_timeout(IOCHAN i, int event)
45 struct http_session *s = iochan_getdata(i);
46 http_session_destroy(s);
49 struct http_session *http_session_create()
51 NMEM nmem = nmem_create();
52 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
54 r->psession = new_session(nmem);
58 r->next = session_list;
60 r->timeout_iochan = iochan_create(-1, session_timeout, 0);
61 iochan_setdata(r->timeout_iochan, r);
62 iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout);
63 r->timeout_iochan->next = channel_list;
64 channel_list = r->timeout_iochan;
68 void http_session_destroy(struct http_session *s)
70 struct http_session **p;
72 for (p = &session_list; *p; p = &(*p)->next)
78 iochan_destroy(s->timeout_iochan);
79 destroy_session(s->psession);
80 nmem_destroy(s->nmem);
83 static void error(struct http_response *rs, char *code, char *msg, char *txt)
85 struct http_channel *c = rs->channel;
90 rs->msg = nmem_strdup(c->nmem, msg);
91 strcpy(rs->code, code);
92 sprintf(tmp, "<error code=\"general\">%s</error>", txt);
93 rs->payload = nmem_strdup(c->nmem, tmp);
94 http_send_response(c);
97 unsigned int make_sessionid()
104 if (gettimeofday(&t, 0) < 0)
107 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
111 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
113 struct http_session *p;
114 char *session = http_argbyname(rq, "session");
119 error(rs, "417", "Must supply session", 0);
123 for (p = session_list; p; p = p->next)
124 if (id == p->session_id)
126 iochan_activity(p->timeout_iochan);
129 error(rs, "417", "Session does not exist, or it has expired", 0);
133 static void cmd_exit(struct http_channel *c)
135 yaz_log(YLOG_WARN, "exit");
140 static void cmd_init(struct http_channel *c)
144 struct http_session *s = http_session_create();
145 struct http_response *rs = c->response;
147 yaz_log(YLOG_DEBUG, "HTTP Session init");
148 sesid = make_sessionid();
149 s->session_id = sesid;
150 sprintf(buf, "<init><status>OK</status><session>%u</session></init>", sesid);
151 rs->payload = nmem_strdup(c->nmem, buf);
152 http_send_response(c);
155 // Compares two hitsbytarget nodes by hitcount
156 static int cmp_ht(const void *p1, const void *p2)
158 const struct hitsbytarget *h1 = p1;
159 const struct hitsbytarget *h2 = p2;
160 return h2->hits - h1->hits;
163 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
164 static void targets_termlist(WRBUF wrbuf, struct session *se, int num)
166 struct hitsbytarget *ht;
169 if (!(ht = hitsbytarget(se, &count)))
171 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
172 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
174 wrbuf_puts(wrbuf, "\n<term>\n");
175 wrbuf_printf(wrbuf, "<id>%s</id>\n", ht[i].id);
176 wrbuf_printf(wrbuf, "<name>%s</name>\n", ht[i].name);
177 wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
178 wrbuf_printf(wrbuf, "<state>%s</state>\n", ht[i].state);
179 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
180 wrbuf_puts(wrbuf, "\n</term>\n");
184 static void cmd_termlist(struct http_channel *c)
186 struct http_response *rs = c->response;
187 struct http_request *rq = c->request;
188 struct http_session *s = locate_session(rq, rs);
189 struct termlist_score **p;
192 char *name = http_argbyname(rq, "name");
193 char *nums = http_argbyname(rq, "num");
200 status = session_active_clients(s->psession);
204 if (strlen(name) > 255)
209 wrbuf_rewind(c->wrbuf);
211 wrbuf_puts(c->wrbuf, "<termlist>");
212 wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>", status);
218 if (!(tp = strchr(name, ',')))
219 tp = name + strlen(name);
220 strncpy(tname, name, tp - name);
221 tname[tp - name] = '\0';
223 wrbuf_printf(c->wrbuf, "\n<list name=\"%s\">\n", tname);
224 if (!strcmp(tname, "xtargets"))
225 targets_termlist(c->wrbuf, s->psession, num);
228 p = termlist(s->psession, tname, &len);
230 for (i = 0; i < len && i < num; i++)
232 wrbuf_puts(c->wrbuf, "\n<term>");
233 wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
234 wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
235 wrbuf_puts(c->wrbuf, "</term>");
238 wrbuf_puts(c->wrbuf, "\n</list>");
243 wrbuf_puts(c->wrbuf, "</termlist>");
244 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
245 http_send_response(c);
249 static void cmd_bytarget(struct http_channel *c)
251 struct http_response *rs = c->response;
252 struct http_request *rq = c->request;
253 struct http_session *s = locate_session(rq, rs);
254 struct hitsbytarget *ht;
259 if (!(ht = hitsbytarget(s->psession, &count)))
261 error(rs, "500", "Failed to retrieve hitcounts", 0);
264 wrbuf_rewind(c->wrbuf);
265 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
267 for (i = 0; i < count; i++)
269 wrbuf_puts(c->wrbuf, "\n<target>");
270 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
271 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
272 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
273 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
274 wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
275 wrbuf_puts(c->wrbuf, "</target>");
278 wrbuf_puts(c->wrbuf, "</bytarget>");
279 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
280 http_send_response(c);
283 static void write_metadata(WRBUF w, struct conf_service *service,
284 struct record_metadata **ml, int full)
288 for (imeta = 0; imeta < service->num_metadata; imeta++)
290 struct conf_metadata *cmd = &service->metadata[imeta];
291 struct record_metadata *md;
292 if (!cmd->brief && !full)
294 for (md = ml[imeta]; md; md = md->next)
296 wrbuf_printf(w, "<md-%s>", cmd->name);
299 case Metadata_type_generic:
300 wrbuf_puts(w, md->data.text);
302 case Metadata_type_year:
303 wrbuf_printf(w, "%d", md->data.number.min);
304 if (md->data.number.min != md->data.number.max)
305 wrbuf_printf(w, "-%d", md->data.number.max);
308 wrbuf_puts(w, "[can't represent]");
310 wrbuf_printf(w, "</md-%s>", cmd->name);
315 static void write_subrecord(struct record *r, WRBUF w, struct conf_service *service)
317 wrbuf_printf(w, "<location id=\"%s\" name=\"%s\">\n",
318 r->client->database->url,
319 r->client->database->name ? r->client->database->name : "");
320 write_metadata(w, service, r->metadata, 1);
321 wrbuf_puts(w, "</location>\n");
324 static void cmd_record(struct http_channel *c)
326 struct http_response *rs = c->response;
327 struct http_request *rq = c->request;
328 struct http_session *s = locate_session(rq, rs);
329 struct record_cluster *rec;
331 struct conf_service *service = global_parameters.server->service;
332 char *idstr = http_argbyname(rq, "id");
339 error(rs, "417", "Must supply id", 0);
342 wrbuf_rewind(c->wrbuf);
344 if (!(rec = show_single(s->psession, id)))
346 error(rs, "500", "Record missing", 0);
349 wrbuf_puts(c->wrbuf, "<record>\n");
350 wrbuf_printf(c->wrbuf, "<recid>%d</recid>", rec->recid);
351 write_metadata(c->wrbuf, service, rec->metadata, 1);
352 for (r = rec->records; r; r = r->next)
353 write_subrecord(r, c->wrbuf, service);
354 wrbuf_puts(c->wrbuf, "</record>\n");
355 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
356 http_send_response(c);
359 static void show_records(struct http_channel *c, int active)
361 struct http_request *rq = c->request;
362 struct http_response *rs = c->response;
363 struct http_session *s = locate_session(rq, rs);
364 struct record_cluster **rl;
365 struct reclist_sortparms *sp;
366 char *start = http_argbyname(rq, "start");
367 char *num = http_argbyname(rq, "num");
368 char *sort = http_argbyname(rq, "sort");
378 // We haven't counted clients yet if we're called on a block release
380 active = session_active_clients(s->psession);
383 startn = atoi(start);
388 if (!(sp = reclist_parse_sortparms(c->nmem, sort)))
390 error(rs, "500", "Bad sort parameters", 0);
394 rl = show(s->psession, sp, startn, &numn, &total, &total_hits, c->nmem);
396 wrbuf_rewind(c->wrbuf);
397 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
398 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
399 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
400 wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
401 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
402 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
404 for (i = 0; i < numn; i++)
408 struct record_cluster *rec = rl[i];
409 struct conf_service *service = global_parameters.server->service;
411 wrbuf_puts(c->wrbuf, "<hit>\n");
412 write_metadata(c->wrbuf, service, rec->metadata, 0);
413 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
416 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
417 wrbuf_printf(c->wrbuf, "<recid>%d</recid>\n", rec->recid);
418 wrbuf_puts(c->wrbuf, "</hit>\n");
421 wrbuf_puts(c->wrbuf, "</show>\n");
422 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
423 http_send_response(c);
426 static void show_records_ready(void *data)
428 struct http_channel *c = (struct http_channel *) data;
433 static void cmd_show(struct http_channel *c)
435 struct http_request *rq = c->request;
436 struct http_response *rs = c->response;
437 struct http_session *s = locate_session(rq, rs);
438 char *block = http_argbyname(rq, "block");
444 status = session_active_clients(s->psession);
448 if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
450 session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
451 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
456 show_records(c, status);
459 static void cmd_ping(struct http_channel *c)
461 struct http_request *rq = c->request;
462 struct http_response *rs = c->response;
463 struct http_session *s = locate_session(rq, rs);
466 rs->payload = "<ping><status>OK</status></ping>";
467 http_send_response(c);
470 static void cmd_search(struct http_channel *c)
472 struct http_request *rq = c->request;
473 struct http_response *rs = c->response;
474 struct http_session *s = locate_session(rq, rs);
475 char *query = http_argbyname(rq, "query");
476 char *filter = http_argbyname(rq, "filter");
483 error(rs, "417", "Must supply query", 0);
486 res = search(s->psession, query, filter);
489 error(rs, "417", res, res);
492 rs->payload = "<search><status>OK</status></search>";
493 http_send_response(c);
497 static void cmd_stat(struct http_channel *c)
499 struct http_request *rq = c->request;
500 struct http_response *rs = c->response;
501 struct http_session *s = locate_session(rq, rs);
502 struct statistics stat;
508 clients = session_active_clients(s->psession);
509 statistics(s->psession, &stat);
511 wrbuf_rewind(c->wrbuf);
512 wrbuf_puts(c->wrbuf, "<stat>");
513 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
514 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
515 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
516 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
517 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
518 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
519 wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
520 wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
521 wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
522 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
523 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
524 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
525 wrbuf_puts(c->wrbuf, "</stat>");
526 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
527 http_send_response(c);
530 static void cmd_info(struct http_channel *c)
532 char yaz_version_str[20];
533 struct http_response *rs = c->response;
535 wrbuf_rewind(c->wrbuf);
536 wrbuf_puts(c->wrbuf, "<info>\n");
537 wrbuf_printf(c->wrbuf, " <version>\n");
538 wrbuf_printf(c->wrbuf, " <pazpar2>%s</pazpar2>\n", VERSION);
540 yaz_version(yaz_version_str, 0);
541 wrbuf_printf(c->wrbuf, " <yaz compiled=\"%s\">%s</yaz>\n",
542 YAZ_VERSION, yaz_version_str);
543 wrbuf_printf(c->wrbuf, " </version>\n");
545 wrbuf_puts(c->wrbuf, "</info>");
546 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
547 http_send_response(c);
552 void (*fun)(struct http_channel *c);
554 { "init", cmd_init },
555 { "stat", cmd_stat },
556 { "bytarget", cmd_bytarget },
557 { "show", cmd_show },
558 { "search", cmd_search },
559 { "termlist", cmd_termlist },
560 { "exit", cmd_exit },
561 { "ping", cmd_ping },
562 { "record", cmd_record },
563 { "info", cmd_info },
567 void http_command(struct http_channel *c)
569 char *command = http_argbyname(c->request, "command");
570 struct http_response *rs = http_create_response(c);
575 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
576 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
580 error(rs, "417", "Must supply command", 0);
583 for (i = 0; commands[i].name; i++)
584 if (!strcmp(commands[i].name, command))
586 (*commands[i].fun)(c);
589 if (!commands[i].name)
590 error(rs, "417", "Unknown command", 0);
598 * indent-tabs-mode: nil
600 * vim: shiftwidth=4 tabstop=8 expandtab