1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <sys/types.h>
33 #include <yaz/snprintf.h>
34 #include <yaz/yaz-util.h>
40 #include "http_command.h"
44 // Update this when the protocol changes
45 #define PAZPAR2_PROTOCOL_VERSION "1"
48 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
49 struct session *psession;
50 unsigned int session_id;
53 struct http_session *next;
56 static struct http_session *session_list = 0;
57 void http_session_destroy(struct http_session *s);
59 static void session_timeout(IOCHAN i, int event)
61 struct http_session *s = iochan_getdata(i);
62 http_session_destroy(s);
65 struct http_session *http_session_create(void)
67 NMEM nmem = nmem_create();
68 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
70 r->psession = new_session(nmem);
74 r->next = session_list;
76 r->timeout_iochan = iochan_create(-1, session_timeout, 0);
77 iochan_setdata(r->timeout_iochan, r);
78 iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout);
80 pazpar2_add_channel(r->timeout_iochan);
84 void http_session_destroy(struct http_session *s)
86 struct http_session **p;
88 for (p = &session_list; *p; p = &(*p)->next)
94 yaz_log(YLOG_LOG, "Destroying session %u", s->session_id);
95 iochan_destroy(s->timeout_iochan);
96 destroy_session(s->psession);
97 nmem_destroy(s->nmem);
100 static const char *get_msg(enum pazpar2_error_code code)
102 struct pazpar2_error_msg {
103 enum pazpar2_error_code code;
106 static const struct pazpar2_error_msg ar[] = {
107 { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
108 { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
109 { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
110 { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
111 { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
112 { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
113 { PAZPAR2_RECORD_MISSING, "Record missing"},
114 { PAZPAR2_NO_TARGETS, "No targets"},
115 { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
116 { PAZPAR2_RECORD_FAIL, "Record command failed"},
117 { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"},
118 { PAZPAR2_LAST_ERROR, "Last error"},
124 if (code == ar[i].code)
131 static void error(struct http_response *rs,
132 enum pazpar2_error_code code,
135 struct http_channel *c = rs->channel;
136 WRBUF text = wrbuf_alloc();
137 const char *http_status = "417";
138 const char *msg = get_msg(code);
140 rs->msg = nmem_strdup(c->nmem, msg);
141 strcpy(rs->code, http_status);
143 wrbuf_printf(text, "<error code=\"%d\" msg=\"%s\">", (int) code,
146 wrbuf_xmlputs(text, addinfo);
147 wrbuf_puts(text, "</error>");
149 yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
150 msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
151 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
153 http_send_response(c);
156 unsigned int make_sessionid(void)
162 if (global_parameters.debug_mode)
171 if (gettimeofday(&t, 0) < 0)
173 yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
176 /* at most 256 sessions per second ..
177 (long long would be more appropriate)*/
179 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
185 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
187 struct http_session *p;
188 char *session = http_argbyname(rq, "session");
193 error(rs, PAZPAR2_MISSING_PARAMETER, "session");
197 for (p = session_list; p; p = p->next)
198 if (id == p->session_id)
200 iochan_activity(p->timeout_iochan);
203 error(rs, PAZPAR2_NO_SESSION, session);
207 // Decode settings parameters and apply to session
208 // Syntax: setting[target]=value
209 static int process_settings(struct session *se, struct http_request *rq,
210 struct http_response *rs)
212 struct http_argument *a;
214 for (a = rq->arguments; a; a = a->next)
215 if (strchr(a->name, '['))
222 // Nmem_strsplit *rules*!!!
223 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
226 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
231 session_apply_setting(se, dbname, setting,
232 nmem_strdup(se->session_nmem, a->value));
237 static void cmd_exit(struct http_channel *c)
239 yaz_log(YLOG_WARN, "exit");
243 static void cmd_init(struct http_channel *c)
247 const char *clear = http_argbyname(c->request, "clear");
248 struct http_session *s = http_session_create();
249 struct http_response *rs = c->response;
251 yaz_log(YLOG_DEBUG, "HTTP Session init");
252 if (!clear || *clear == '0')
253 session_init_databases(s->psession);
255 yaz_log(YLOG_LOG, "No databases preloaded");
256 sesid = make_sessionid();
257 s->session_id = sesid;
258 if (process_settings(s->psession, c->request, c->response) < 0)
260 sprintf(buf, "<init><status>OK</status><session>%u</session>"
261 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>", sesid);
262 rs->payload = nmem_strdup(c->nmem, buf);
263 http_send_response(c);
266 static void cmd_settings(struct http_channel *c)
268 struct http_response *rs = c->response;
269 struct http_request *rq = c->request;
270 struct http_session *s = locate_session(rq, rs);
275 if (process_settings(s->psession, rq, rs) < 0)
277 rs->payload = "<settings><status>OK</status></settings>";
278 http_send_response(c);
281 // Compares two hitsbytarget nodes by hitcount
282 static int cmp_ht(const void *p1, const void *p2)
284 const struct hitsbytarget *h1 = p1;
285 const struct hitsbytarget *h2 = p2;
286 return h2->hits - h1->hits;
289 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
290 static void targets_termlist(WRBUF wrbuf, struct session *se, int num,
293 struct hitsbytarget *ht;
296 ht = hitsbytarget(se, &count, nmem);
297 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
298 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
301 // do only print terms which have display names
303 wrbuf_puts(wrbuf, "<term>\n");
305 wrbuf_puts(wrbuf, "<id>");
306 wrbuf_xmlputs(wrbuf, ht[i].id);
307 wrbuf_puts(wrbuf, "</id>\n");
309 wrbuf_puts(wrbuf, "<name>");
310 if (!ht[i].name || !ht[i].name[0])
311 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
313 wrbuf_xmlputs(wrbuf, ht[i].name);
314 wrbuf_puts(wrbuf, "</name>\n");
316 wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
318 wrbuf_puts(wrbuf, "<state>");
319 wrbuf_xmlputs(wrbuf, ht[i].state);
320 wrbuf_puts(wrbuf, "</state>\n");
322 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
324 wrbuf_puts(wrbuf, "</term>\n");
328 static void cmd_termlist(struct http_channel *c)
330 struct http_response *rs = c->response;
331 struct http_request *rq = c->request;
332 struct http_session *s = locate_session(rq, rs);
333 struct termlist_score **p;
336 char *name = http_argbyname(rq, "name");
337 char *nums = http_argbyname(rq, "num");
344 status = session_active_clients(s->psession);
348 if (strlen(name) > 255)
353 wrbuf_rewind(c->wrbuf);
355 wrbuf_puts(c->wrbuf, "<termlist>\n");
356 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
362 if (!(tp = strchr(name, ',')))
363 tp = name + strlen(name);
364 strncpy(tname, name, tp - name);
365 tname[tp - name] = '\0';
367 wrbuf_puts(c->wrbuf, "<list name=\"");
368 wrbuf_xmlputs(c->wrbuf, tname);
369 wrbuf_puts(c->wrbuf, "\">\n");
370 if (!strcmp(tname, "xtargets"))
371 targets_termlist(c->wrbuf, s->psession, num, c->nmem);
374 p = termlist(s->psession, tname, &len);
376 for (i = 0; i < len && i < num; i++){
377 // prevnt sending empty term elements
378 if (!p[i]->term || !p[i]->term[0])
381 wrbuf_puts(c->wrbuf, "<term>");
382 wrbuf_puts(c->wrbuf, "<name>");
383 wrbuf_xmlputs(c->wrbuf, p[i]->term);
384 wrbuf_puts(c->wrbuf, "</name>");
386 wrbuf_printf(c->wrbuf,
387 "<frequency>%d</frequency>",
389 wrbuf_puts(c->wrbuf, "</term>\n");
392 wrbuf_puts(c->wrbuf, "</list>\n");
397 wrbuf_puts(c->wrbuf, "</termlist>\n");
398 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
399 http_send_response(c);
403 static void cmd_bytarget(struct http_channel *c)
405 struct http_response *rs = c->response;
406 struct http_request *rq = c->request;
407 struct http_session *s = locate_session(rq, rs);
408 struct hitsbytarget *ht;
413 ht = hitsbytarget(s->psession, &count, c->nmem);
414 wrbuf_rewind(c->wrbuf);
415 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
417 for (i = 0; i < count; i++)
419 wrbuf_puts(c->wrbuf, "\n<target>");
421 wrbuf_puts(c->wrbuf, "<id>");
422 wrbuf_xmlputs(c->wrbuf, ht[i].id);
423 wrbuf_puts(c->wrbuf, "</id>\n");
425 if (ht[i].name && ht[i].name[0])
427 wrbuf_puts(c->wrbuf, "<name>");
428 wrbuf_xmlputs(c->wrbuf, ht[i].name);
429 wrbuf_puts(c->wrbuf, "</name>\n");
432 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
433 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
434 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
436 wrbuf_puts(c->wrbuf, "<state>");
437 wrbuf_xmlputs(c->wrbuf, ht[i].state);
438 wrbuf_puts(c->wrbuf, "</state>\n");
440 wrbuf_puts(c->wrbuf, "</target>");
443 wrbuf_puts(c->wrbuf, "</bytarget>");
444 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
445 http_send_response(c);
448 static void write_metadata(WRBUF w, struct conf_service *service,
449 struct record_metadata **ml, int full)
453 for (imeta = 0; imeta < service->num_metadata; imeta++)
455 struct conf_metadata *cmd = &service->metadata[imeta];
456 struct record_metadata *md;
457 if (!cmd->brief && !full)
459 for (md = ml[imeta]; md; md = md->next)
461 wrbuf_printf(w, "\n<md-%s>", cmd->name);
465 case Metadata_type_generic:
466 wrbuf_xmlputs(w, md->data.text.disp);
468 case Metadata_type_year:
469 wrbuf_printf(w, "%d", md->data.number.min);
470 if (md->data.number.min != md->data.number.max)
471 wrbuf_printf(w, "-%d", md->data.number.max);
474 wrbuf_puts(w, "[can't represent]");
476 wrbuf_printf(w, "</md-%s>", cmd->name);
481 static void write_subrecord(struct record *r, WRBUF w,
482 struct conf_service *service, int show_details)
484 const char *name = session_setting_oneval(
485 client_get_database(r->client), PZ_NAME);
487 wrbuf_puts(w, "<location id=\"");
488 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
489 wrbuf_puts(w, "\" ");
491 wrbuf_puts(w, "name=\"");
492 wrbuf_xmlputs(w, *name ? name : "Unknown");
493 wrbuf_puts(w, "\">");
495 write_metadata(w, service, r->metadata, show_details);
496 wrbuf_puts(w, "</location>\n");
499 static void show_raw_record_error(void *data, const char *addinfo)
501 http_channel_observer_t obs = data;
502 struct http_channel *c = http_channel_observer_chan(obs);
503 struct http_response *rs = c->response;
505 http_remove_observer(obs);
507 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
510 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
512 http_channel_observer_t obs = data;
513 struct http_channel *c = http_channel_observer_chan(obs);
514 struct http_response *rs = c->response;
516 http_remove_observer(obs);
518 wrbuf_write(c->wrbuf, buf, sz);
519 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
520 http_send_response(c);
524 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
526 http_channel_observer_t obs = data;
527 struct http_channel *c = http_channel_observer_chan(obs);
528 struct http_response *rs = c->response;
530 http_remove_observer(obs);
532 wrbuf_write(c->wrbuf, buf, sz);
533 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
535 rs->content_type = "application/octet-stream";
536 http_send_response(c);
540 void show_raw_reset(void *data, struct http_channel *c, void *data2)
542 //struct client *client = data;
543 //client_show_raw_remove(client, data2);
546 static void cmd_record_ready(void *data);
548 static void cmd_record(struct http_channel *c)
550 struct http_response *rs = c->response;
551 struct http_request *rq = c->request;
552 struct http_session *s = locate_session(rq, rs);
553 struct record_cluster *rec, *prev_r, *next_r;
555 struct conf_service *service = global_parameters.server->service;
556 const char *idstr = http_argbyname(rq, "id");
557 const char *offsetstr = http_argbyname(rq, "offset");
558 const char *binarystr = http_argbyname(rq, "binary");
564 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
567 wrbuf_rewind(c->wrbuf);
568 if (!(rec = show_single(s->psession, idstr, &prev_r, &next_r)))
570 if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
571 cmd_record_ready, c, c) != 0)
573 error(rs, PAZPAR2_RECORD_MISSING, idstr);
579 int offset = atoi(offsetstr);
580 const char *syntax = http_argbyname(rq, "syntax");
581 const char *esn = http_argbyname(rq, "esn");
583 struct record*r = rec->records;
586 if (binarystr && *binarystr != '0')
589 for (i = 0; i < offset && r; r = r->next, i++)
593 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
598 http_channel_observer_t obs =
599 http_add_observer(c, r->client, show_raw_reset);
600 int ret = client_show_raw_begin(r->client, r->position,
603 show_raw_record_error,
605 show_raw_record_ok_binary :
610 http_remove_observer(obs);
611 error(rs, PAZPAR2_NO_SESSION, 0);
617 wrbuf_puts(c->wrbuf, "<record>\n");
618 wrbuf_puts(c->wrbuf, "<recid>");
619 wrbuf_xmlputs(c->wrbuf, rec->recid);
620 wrbuf_puts(c->wrbuf, "</recid>\n");
623 wrbuf_puts(c->wrbuf, "<prevrecid>");
624 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
625 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
629 wrbuf_puts(c->wrbuf, "<nextrecid>");
630 wrbuf_xmlputs(c->wrbuf, next_r->recid);
631 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
633 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
634 session_active_clients(s->psession));
635 write_metadata(c->wrbuf, service, rec->metadata, 1);
636 for (r = rec->records; r; r = r->next)
637 write_subrecord(r, c->wrbuf, service, 1);
638 wrbuf_puts(c->wrbuf, "</record>\n");
639 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
640 http_send_response(c);
644 static void cmd_record_ready(void *data)
646 struct http_channel *c = (struct http_channel *) data;
651 static void show_records(struct http_channel *c, int active)
653 struct http_request *rq = c->request;
654 struct http_response *rs = c->response;
655 struct http_session *s = locate_session(rq, rs);
656 struct record_cluster **rl;
657 struct reclist_sortparms *sp;
658 char *start = http_argbyname(rq, "start");
659 char *num = http_argbyname(rq, "num");
660 char *sort = http_argbyname(rq, "sort");
670 // We haven't counted clients yet if we're called on a block release
672 active = session_active_clients(s->psession);
675 startn = atoi(start);
680 if (!(sp = reclist_parse_sortparms(c->nmem, sort)))
682 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
686 rl = show(s->psession, sp, startn, &numn, &total, &total_hits, c->nmem);
688 wrbuf_rewind(c->wrbuf);
689 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
690 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
691 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
692 wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
693 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
694 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
696 for (i = 0; i < numn; i++)
700 struct record_cluster *rec = rl[i];
701 struct conf_service *service = global_parameters.server->service;
703 wrbuf_puts(c->wrbuf, "<hit>\n");
704 write_metadata(c->wrbuf, service, rec->metadata, 0);
705 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
706 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
708 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
709 wrbuf_puts(c->wrbuf, "<recid>");
710 wrbuf_xmlputs(c->wrbuf, rec->recid);
711 wrbuf_puts(c->wrbuf, "</recid>\n");
712 wrbuf_puts(c->wrbuf, "</hit>\n");
715 wrbuf_puts(c->wrbuf, "</show>\n");
716 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
717 http_send_response(c);
720 static void show_records_ready(void *data)
722 struct http_channel *c = (struct http_channel *) data;
727 static void cmd_show(struct http_channel *c)
729 struct http_request *rq = c->request;
730 struct http_response *rs = c->response;
731 struct http_session *s = locate_session(rq, rs);
732 char *block = http_argbyname(rq, "block");
738 status = session_active_clients(s->psession);
742 if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
744 // if there is already a watch/block. we do not block this one
745 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
746 show_records_ready, c, c) != 0)
748 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
754 show_records(c, status);
757 static void cmd_ping(struct http_channel *c)
759 struct http_request *rq = c->request;
760 struct http_response *rs = c->response;
761 struct http_session *s = locate_session(rq, rs);
764 rs->payload = "<ping><status>OK</status></ping>";
765 http_send_response(c);
768 static int utf_8_valid(const char *str)
770 yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
773 /* check that query is UTF-8 encoded */
774 char *inbuf = (char *) str; /* we know iconv does not alter this */
775 size_t inbytesleft = strlen(inbuf);
777 size_t outbytesleft = strlen(inbuf) + 10;
778 char *out = xmalloc(outbytesleft);
780 size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
782 /* if OK, try flushing the rest */
783 if (r != (size_t) (-1))
784 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
787 if (r == (size_t) (-1))
793 static void cmd_search(struct http_channel *c)
795 struct http_request *rq = c->request;
796 struct http_response *rs = c->response;
797 struct http_session *s = locate_session(rq, rs);
798 char *query = http_argbyname(rq, "query");
799 char *filter = http_argbyname(rq, "filter");
800 enum pazpar2_error_code code;
801 const char *addinfo = 0;
807 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
810 if (!utf_8_valid(query))
812 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
815 code = search(s->psession, query, filter, &addinfo);
818 error(rs, code, addinfo);
821 rs->payload = "<search><status>OK</status></search>";
822 http_send_response(c);
826 static void cmd_stat(struct http_channel *c)
828 struct http_request *rq = c->request;
829 struct http_response *rs = c->response;
830 struct http_session *s = locate_session(rq, rs);
831 struct statistics stat;
839 clients = session_active_clients(s->psession);
840 statistics(s->psession, &stat);
842 if (stat.num_clients > 0) {
843 progress = (stat.num_clients - clients) / (float)stat.num_clients;
846 wrbuf_rewind(c->wrbuf);
847 wrbuf_puts(c->wrbuf, "<stat>");
848 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
849 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
850 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
851 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
852 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
853 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
854 wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
855 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
856 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
857 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
858 wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
859 wrbuf_puts(c->wrbuf, "</stat>");
860 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
861 http_send_response(c);
864 static void cmd_info(struct http_channel *c)
866 char yaz_version_str[20];
867 struct http_response *rs = c->response;
869 wrbuf_rewind(c->wrbuf);
870 wrbuf_puts(c->wrbuf, "<info>\n");
871 wrbuf_puts(c->wrbuf, " <version>\n");
872 wrbuf_puts(c->wrbuf, "<pazpar2");
873 #ifdef PAZPAR2_VERSION_SHA1
874 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
876 wrbuf_puts(c->wrbuf, ">");
877 wrbuf_xmlputs(c->wrbuf, VERSION);
878 wrbuf_puts(c->wrbuf, "</pazpar2>");
881 yaz_version(yaz_version_str, 0);
882 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
883 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
884 wrbuf_puts(c->wrbuf, "\">");
885 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
886 wrbuf_puts(c->wrbuf, "</yaz>\n");
888 wrbuf_puts(c->wrbuf, " </version>\n");
890 wrbuf_puts(c->wrbuf, "</info>");
891 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
892 http_send_response(c);
897 void (*fun)(struct http_channel *c);
899 { "init", cmd_init },
900 { "settings", cmd_settings },
901 { "stat", cmd_stat },
902 { "bytarget", cmd_bytarget },
903 { "show", cmd_show },
904 { "search", cmd_search },
905 { "termlist", cmd_termlist },
906 { "exit", cmd_exit },
907 { "ping", cmd_ping },
908 { "record", cmd_record },
909 { "info", cmd_info },
913 void http_command(struct http_channel *c)
915 char *command = http_argbyname(c->request, "command");
916 struct http_response *rs = http_create_response(c);
921 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
922 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
926 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
929 for (i = 0; commands[i].name; i++)
930 if (!strcmp(commands[i].name, command))
932 (*commands[i].fun)(c);
935 if (!commands[i].name)
936 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
944 * c-file-style: "Stroustrup"
945 * indent-tabs-mode: nil
947 * vim: shiftwidth=4 tabstop=8 expandtab