2 * $Id: http_command.c,v 1.22 2007-01-15 04:34:28 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;
35 struct http_session *next;
38 static struct http_session *session_list = 0;
40 void http_session_destroy(struct http_session *s);
42 static void session_timeout(IOCHAN i, int event)
44 struct http_session *s = iochan_getdata(i);
45 http_session_destroy(s);
48 struct http_session *http_session_create()
50 struct http_session *r = xmalloc(sizeof(*r));
51 r->psession = new_session();
54 r->next = session_list;
56 r->timeout_iochan = iochan_create(-1, session_timeout, 0);
57 iochan_setdata(r->timeout_iochan, r);
58 iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout);
59 r->timeout_iochan->next = channel_list;
60 channel_list = r->timeout_iochan;
64 void http_session_destroy(struct http_session *s)
66 struct http_session **p;
68 for (p = &session_list; *p; p = &(*p)->next)
74 iochan_destroy(s->timeout_iochan);
75 destroy_session(s->psession);
79 static void error(struct http_response *rs, char *code, char *msg, char *txt)
81 struct http_channel *c = rs->channel;
86 rs->msg = nmem_strdup(c->nmem, msg);
87 strcpy(rs->code, code);
88 sprintf(tmp, "<error code=\"general\">%s</error>", txt);
89 rs->payload = nmem_strdup(c->nmem, tmp);
90 http_send_response(c);
93 unsigned int make_sessionid()
100 if (gettimeofday(&t, 0) < 0)
103 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
107 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
109 struct http_session *p;
110 char *session = http_argbyname(rq, "session");
115 error(rs, "417", "Must supply session", 0);
119 for (p = session_list; p; p = p->next)
120 if (id == p->session_id)
122 iochan_activity(p->timeout_iochan);
125 error(rs, "417", "Session does not exist, or it has expired", 0);
129 static void cmd_exit(struct http_channel *c)
131 yaz_log(YLOG_WARN, "exit");
136 static void cmd_init(struct http_channel *c)
140 struct http_session *s = http_session_create();
141 struct http_response *rs = c->response;
143 yaz_log(YLOG_DEBUG, "HTTP Session init");
144 sesid = make_sessionid();
145 s->session_id = sesid;
146 sprintf(buf, "<init><status>OK</status><session>%u</session></init>", sesid);
147 rs->payload = nmem_strdup(c->nmem, buf);
148 http_send_response(c);
151 // Compares two hitsbytarget nodes by hitcount
152 static int cmp_ht(const void *p1, const void *p2)
154 const struct hitsbytarget *h1 = p1;
155 const struct hitsbytarget *h2 = p2;
156 return h2->hits - h1->hits;
159 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
160 static void targets_termlist(WRBUF wrbuf, struct session *se)
162 struct hitsbytarget *ht;
165 if (!(ht = hitsbytarget(se, &count)))
167 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
168 for (i = 0; i < count && i < 15; i++)
170 wrbuf_puts(wrbuf, "\n<term>\n");
171 wrbuf_printf(wrbuf, "<name>%s</name>\n", ht[i].id);
172 wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
173 wrbuf_printf(wrbuf, "<state>%s</state>\n", ht[i].state);
174 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
175 wrbuf_puts(wrbuf, "\n</term>\n");
179 static void cmd_termlist(struct http_channel *c)
181 struct http_response *rs = c->response;
182 struct http_request *rq = c->request;
183 struct http_session *s = locate_session(rq, rs);
184 struct termlist_score **p;
187 char *name = http_argbyname(rq, "name");
193 status = session_active_clients(s->psession);
197 if (strlen(name) > 255)
200 wrbuf_rewind(c->wrbuf);
202 wrbuf_puts(c->wrbuf, "<termlist>");
203 wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>", status);
209 if (!(tp = strchr(name, ',')))
210 tp = name + strlen(name);
211 strncpy(tname, name, tp - name);
212 tname[tp - name] = '\0';
214 wrbuf_printf(c->wrbuf, "\n<list name=\"%s\">\n", tname);
215 if (!strcmp(tname, "xtargets"))
216 targets_termlist(c->wrbuf, s->psession);
219 p = termlist(s->psession, tname, &len);
221 for (i = 0; i < len; i++)
223 wrbuf_puts(c->wrbuf, "\n<term>");
224 wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
225 wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
226 wrbuf_puts(c->wrbuf, "</term>");
229 wrbuf_puts(c->wrbuf, "\n</list>");
234 wrbuf_puts(c->wrbuf, "</termlist>");
235 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_buf(c->wrbuf));
236 http_send_response(c);
240 static void cmd_bytarget(struct http_channel *c)
242 struct http_response *rs = c->response;
243 struct http_request *rq = c->request;
244 struct http_session *s = locate_session(rq, rs);
245 struct hitsbytarget *ht;
250 if (!(ht = hitsbytarget(s->psession, &count)))
252 error(rs, "500", "Failed to retrieve hitcounts", 0);
255 wrbuf_rewind(c->wrbuf);
256 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
258 for (i = 0; i < count; i++)
260 wrbuf_puts(c->wrbuf, "\n<target>");
261 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
262 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
263 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
264 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
265 wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
266 wrbuf_puts(c->wrbuf, "</target>");
269 wrbuf_puts(c->wrbuf, "</bytarget>");
270 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
271 http_send_response(c);
274 static void write_metadata(WRBUF w, struct conf_service *service,
275 struct record_metadata **ml, int full)
279 for (imeta = 0; imeta < service->num_metadata; imeta++)
281 struct conf_metadata *cmd = &service->metadata[imeta];
282 struct record_metadata *md;
283 if (!cmd->brief && !full)
285 for (md = ml[imeta]; md; md = md->next)
287 wrbuf_printf(w, "<md-%s>", cmd->name);
290 case Metadata_type_generic:
291 wrbuf_puts(w, md->data.text);
293 case Metadata_type_year:
294 wrbuf_printf(w, "%d", md->data.number.min);
295 if (md->data.number.min != md->data.number.max)
296 wrbuf_printf(w, "-%d", md->data.number.max);
299 wrbuf_puts(w, "[can't represent]");
301 wrbuf_printf(w, "</md-%s>", cmd->name);
306 static void cmd_record(struct http_channel *c)
308 struct http_response *rs = c->response;
309 struct http_request *rq = c->request;
310 struct http_session *s = locate_session(rq, rs);
311 struct record_cluster *rec;
312 struct conf_service *service = global_parameters.server->service;
313 char *idstr = http_argbyname(rq, "id");
320 error(rs, "417", "Must supply id", 0);
323 wrbuf_rewind(c->wrbuf);
325 if (!(rec = show_single(s->psession, id)))
327 error(rs, "500", "Record missing", 0);
330 wrbuf_puts(c->wrbuf, "<record>\n");
331 wrbuf_printf(c->wrbuf, "<recid>%d</recid>", rec->recid);
332 write_metadata(c->wrbuf, service, rec->metadata, 1);
333 wrbuf_puts(c->wrbuf, "</record>\n");
334 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
335 http_send_response(c);
338 static void show_records(struct http_channel *c, int active)
340 struct http_request *rq = c->request;
341 struct http_response *rs = c->response;
342 struct http_session *s = locate_session(rq, rs);
343 struct record_cluster **rl;
344 struct reclist_sortparms *sp;
345 char *start = http_argbyname(rq, "start");
346 char *num = http_argbyname(rq, "num");
347 char *sort = http_argbyname(rq, "sort");
357 // We haven't counted clients yet if we're called on a block release
359 active = session_active_clients(s->psession);
362 startn = atoi(start);
367 if (!(sp = reclist_parse_sortparms(c->nmem, sort)))
369 error(rs, "500", "Bad sort parameters", 0);
373 rl = show(s->psession, sp, startn, &numn, &total, &total_hits, c->nmem);
375 wrbuf_rewind(c->wrbuf);
376 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
377 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
378 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
379 wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
380 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
381 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
383 for (i = 0; i < numn; i++)
387 struct record_cluster *rec = rl[i];
388 struct conf_service *service = global_parameters.server->service;
390 wrbuf_puts(c->wrbuf, "<hit>\n");
391 write_metadata(c->wrbuf, service, rec->metadata, 0);
392 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
395 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
396 wrbuf_printf(c->wrbuf, "<recid>%d</recid>\n", rec->recid);
397 wrbuf_puts(c->wrbuf, "</hit>\n");
400 wrbuf_puts(c->wrbuf, "</show>\n");
401 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
402 http_send_response(c);
405 static void show_records_ready(void *data)
407 struct http_channel *c = (struct http_channel *) data;
412 static void cmd_show(struct http_channel *c)
414 struct http_request *rq = c->request;
415 struct http_response *rs = c->response;
416 struct http_session *s = locate_session(rq, rs);
417 char *block = http_argbyname(rq, "block");
423 status = session_active_clients(s->psession);
427 if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
429 session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
430 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
435 show_records(c, status);
438 static void cmd_ping(struct http_channel *c)
440 struct http_request *rq = c->request;
441 struct http_response *rs = c->response;
442 struct http_session *s = locate_session(rq, rs);
445 rs->payload = "<ping><status>OK</status></ping>";
446 http_send_response(c);
449 static void cmd_search(struct http_channel *c)
451 struct http_request *rq = c->request;
452 struct http_response *rs = c->response;
453 struct http_session *s = locate_session(rq, rs);
454 char *query = http_argbyname(rq, "query");
461 error(rs, "417", "Must supply query", 0);
464 res = search(s->psession, query);
467 error(rs, "417", res, res);
470 rs->payload = "<search><status>OK</status></search>";
471 http_send_response(c);
475 static void cmd_stat(struct http_channel *c)
477 struct http_request *rq = c->request;
478 struct http_response *rs = c->response;
479 struct http_session *s = locate_session(rq, rs);
480 struct statistics stat;
486 clients = session_active_clients(s->psession);
487 statistics(s->psession, &stat);
489 wrbuf_rewind(c->wrbuf);
490 wrbuf_puts(c->wrbuf, "<stat>");
491 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
492 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
493 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
494 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
495 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
496 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
497 wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
498 wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
499 wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
500 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
501 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
502 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
503 wrbuf_puts(c->wrbuf, "</stat>");
504 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
505 http_send_response(c);
508 static void cmd_info(struct http_channel *c)
510 char yaz_version_str[20];
511 struct http_response *rs = c->response;
513 wrbuf_rewind(c->wrbuf);
514 wrbuf_puts(c->wrbuf, "<info>\n");
515 wrbuf_printf(c->wrbuf, " <version>\n");
516 wrbuf_printf(c->wrbuf, " <pazpar2>%s</pazpar2>\n", VERSION);
518 yaz_version(yaz_version_str, 0);
519 wrbuf_printf(c->wrbuf, " <yaz compiled=\"%s\">%s</yaz>\n",
520 YAZ_VERSION, yaz_version_str);
521 wrbuf_printf(c->wrbuf, " </version>\n");
523 wrbuf_puts(c->wrbuf, "</info>");
524 rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
525 http_send_response(c);
530 void (*fun)(struct http_channel *c);
532 { "init", cmd_init },
533 { "stat", cmd_stat },
534 { "bytarget", cmd_bytarget },
535 { "show", cmd_show },
536 { "search", cmd_search },
537 { "termlist", cmd_termlist },
538 { "exit", cmd_exit },
539 { "ping", cmd_ping },
540 { "record", cmd_record },
541 { "info", cmd_info },
545 void http_command(struct http_channel *c)
547 char *command = http_argbyname(c->request, "command");
548 struct http_response *rs = http_create_response(c);
553 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
554 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
558 error(rs, "417", "Must supply command", 0);
561 for (i = 0; commands[i].name; i++)
562 if (!strcmp(commands[i].name, command))
564 (*commands[i].fun)(c);
567 if (!commands[i].name)
568 error(rs, "417", "Unknown command", 0);
576 * indent-tabs-mode: nil
578 * vim: shiftwidth=4 tabstop=8 expandtab