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(struct conf_service *service)
67 NMEM nmem = nmem_create();
68 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
70 r->psession = new_session(nmem, service);
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 const char *service_name = http_argbyname(c->request, "service");
249 struct conf_service *service = locate_service(service_name);
250 struct http_session *s = http_session_create(service);
251 struct http_response *rs = c->response;
255 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "service");
259 yaz_log(YLOG_DEBUG, "HTTP Session init");
260 if (!clear || *clear == '0')
261 session_init_databases(s->psession);
263 yaz_log(YLOG_LOG, "No databases preloaded");
264 sesid = make_sessionid();
265 s->session_id = sesid;
266 if (process_settings(s->psession, c->request, c->response) < 0)
268 sprintf(buf, "<init><status>OK</status><session>%u</session>"
269 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>", sesid);
270 rs->payload = nmem_strdup(c->nmem, buf);
271 http_send_response(c);
274 static void cmd_settings(struct http_channel *c)
276 struct http_response *rs = c->response;
277 struct http_request *rq = c->request;
278 struct http_session *s = locate_session(rq, rs);
283 if (process_settings(s->psession, rq, rs) < 0)
285 rs->payload = "<settings><status>OK</status></settings>";
286 http_send_response(c);
289 // Compares two hitsbytarget nodes by hitcount
290 static int cmp_ht(const void *p1, const void *p2)
292 const struct hitsbytarget *h1 = p1;
293 const struct hitsbytarget *h2 = p2;
294 return h2->hits - h1->hits;
297 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
298 static void targets_termlist(WRBUF wrbuf, struct session *se, int num,
301 struct hitsbytarget *ht;
304 ht = hitsbytarget(se, &count, nmem);
305 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
306 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
309 // do only print terms which have display names
311 wrbuf_puts(wrbuf, "<term>\n");
313 wrbuf_puts(wrbuf, "<id>");
314 wrbuf_xmlputs(wrbuf, ht[i].id);
315 wrbuf_puts(wrbuf, "</id>\n");
317 wrbuf_puts(wrbuf, "<name>");
318 if (!ht[i].name || !ht[i].name[0])
319 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
321 wrbuf_xmlputs(wrbuf, ht[i].name);
322 wrbuf_puts(wrbuf, "</name>\n");
324 wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
326 wrbuf_puts(wrbuf, "<state>");
327 wrbuf_xmlputs(wrbuf, ht[i].state);
328 wrbuf_puts(wrbuf, "</state>\n");
330 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
332 wrbuf_puts(wrbuf, "</term>\n");
336 static void cmd_termlist(struct http_channel *c)
338 struct http_response *rs = c->response;
339 struct http_request *rq = c->request;
340 struct http_session *s = locate_session(rq, rs);
341 struct termlist_score **p;
344 char *name = http_argbyname(rq, "name");
345 char *nums = http_argbyname(rq, "num");
352 status = session_active_clients(s->psession);
356 if (strlen(name) > 255)
361 wrbuf_rewind(c->wrbuf);
363 wrbuf_puts(c->wrbuf, "<termlist>\n");
364 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
370 if (!(tp = strchr(name, ',')))
371 tp = name + strlen(name);
372 strncpy(tname, name, tp - name);
373 tname[tp - name] = '\0';
375 wrbuf_puts(c->wrbuf, "<list name=\"");
376 wrbuf_xmlputs(c->wrbuf, tname);
377 wrbuf_puts(c->wrbuf, "\">\n");
378 if (!strcmp(tname, "xtargets"))
379 targets_termlist(c->wrbuf, s->psession, num, c->nmem);
382 p = termlist(s->psession, tname, &len);
384 for (i = 0; i < len && i < num; i++){
385 // prevnt sending empty term elements
386 if (!p[i]->term || !p[i]->term[0])
389 wrbuf_puts(c->wrbuf, "<term>");
390 wrbuf_puts(c->wrbuf, "<name>");
391 wrbuf_xmlputs(c->wrbuf, p[i]->term);
392 wrbuf_puts(c->wrbuf, "</name>");
394 wrbuf_printf(c->wrbuf,
395 "<frequency>%d</frequency>",
397 wrbuf_puts(c->wrbuf, "</term>\n");
400 wrbuf_puts(c->wrbuf, "</list>\n");
405 wrbuf_puts(c->wrbuf, "</termlist>\n");
406 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
407 http_send_response(c);
411 static void cmd_bytarget(struct http_channel *c)
413 struct http_response *rs = c->response;
414 struct http_request *rq = c->request;
415 struct http_session *s = locate_session(rq, rs);
416 struct hitsbytarget *ht;
421 ht = hitsbytarget(s->psession, &count, c->nmem);
422 wrbuf_rewind(c->wrbuf);
423 wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
425 for (i = 0; i < count; i++)
427 wrbuf_puts(c->wrbuf, "\n<target>");
429 wrbuf_puts(c->wrbuf, "<id>");
430 wrbuf_xmlputs(c->wrbuf, ht[i].id);
431 wrbuf_puts(c->wrbuf, "</id>\n");
433 if (ht[i].name && ht[i].name[0])
435 wrbuf_puts(c->wrbuf, "<name>");
436 wrbuf_xmlputs(c->wrbuf, ht[i].name);
437 wrbuf_puts(c->wrbuf, "</name>\n");
440 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
441 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
442 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
444 wrbuf_puts(c->wrbuf, "<state>");
445 wrbuf_xmlputs(c->wrbuf, ht[i].state);
446 wrbuf_puts(c->wrbuf, "</state>\n");
448 wrbuf_puts(c->wrbuf, "</target>");
451 wrbuf_puts(c->wrbuf, "</bytarget>");
452 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
453 http_send_response(c);
456 static void write_metadata(WRBUF w, struct conf_service *service,
457 struct record_metadata **ml, int full)
461 for (imeta = 0; imeta < service->num_metadata; imeta++)
463 struct conf_metadata *cmd = &service->metadata[imeta];
464 struct record_metadata *md;
465 if (!cmd->brief && !full)
467 for (md = ml[imeta]; md; md = md->next)
469 wrbuf_printf(w, "\n<md-%s>", cmd->name);
473 case Metadata_type_generic:
474 wrbuf_xmlputs(w, md->data.text.disp);
476 case Metadata_type_year:
477 wrbuf_printf(w, "%d", md->data.number.min);
478 if (md->data.number.min != md->data.number.max)
479 wrbuf_printf(w, "-%d", md->data.number.max);
482 wrbuf_puts(w, "[can't represent]");
484 wrbuf_printf(w, "</md-%s>", cmd->name);
489 static void write_subrecord(struct record *r, WRBUF w,
490 struct conf_service *service, int show_details)
492 const char *name = session_setting_oneval(
493 client_get_database(r->client), PZ_NAME);
495 wrbuf_puts(w, "<location id=\"");
496 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
497 wrbuf_puts(w, "\" ");
499 wrbuf_puts(w, "name=\"");
500 wrbuf_xmlputs(w, *name ? name : "Unknown");
501 wrbuf_puts(w, "\">");
503 write_metadata(w, service, r->metadata, show_details);
504 wrbuf_puts(w, "</location>\n");
507 static void show_raw_record_error(void *data, const char *addinfo)
509 http_channel_observer_t obs = data;
510 struct http_channel *c = http_channel_observer_chan(obs);
511 struct http_response *rs = c->response;
513 http_remove_observer(obs);
515 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
518 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
520 http_channel_observer_t obs = data;
521 struct http_channel *c = http_channel_observer_chan(obs);
522 struct http_response *rs = c->response;
524 http_remove_observer(obs);
526 wrbuf_write(c->wrbuf, buf, sz);
527 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
528 http_send_response(c);
532 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
534 http_channel_observer_t obs = data;
535 struct http_channel *c = http_channel_observer_chan(obs);
536 struct http_response *rs = c->response;
538 http_remove_observer(obs);
540 wrbuf_write(c->wrbuf, buf, sz);
541 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
543 rs->content_type = "application/octet-stream";
544 http_send_response(c);
548 void show_raw_reset(void *data, struct http_channel *c, void *data2)
550 //struct client *client = data;
551 //client_show_raw_remove(client, data2);
554 static void cmd_record_ready(void *data);
556 static void cmd_record(struct http_channel *c)
558 struct http_response *rs = c->response;
559 struct http_request *rq = c->request;
560 struct http_session *s = locate_session(rq, rs);
561 struct record_cluster *rec, *prev_r, *next_r;
563 struct conf_service *service = s->psession->service;
564 const char *idstr = http_argbyname(rq, "id");
565 const char *offsetstr = http_argbyname(rq, "offset");
566 const char *binarystr = http_argbyname(rq, "binary");
572 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
575 wrbuf_rewind(c->wrbuf);
576 if (!(rec = show_single(s->psession, idstr, &prev_r, &next_r)))
578 if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
579 cmd_record_ready, c, c) != 0)
581 error(rs, PAZPAR2_RECORD_MISSING, idstr);
587 int offset = atoi(offsetstr);
588 const char *syntax = http_argbyname(rq, "syntax");
589 const char *esn = http_argbyname(rq, "esn");
591 struct record*r = rec->records;
594 if (binarystr && *binarystr != '0')
597 for (i = 0; i < offset && r; r = r->next, i++)
601 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
606 http_channel_observer_t obs =
607 http_add_observer(c, r->client, show_raw_reset);
608 int ret = client_show_raw_begin(r->client, r->position,
611 show_raw_record_error,
613 show_raw_record_ok_binary :
618 http_remove_observer(obs);
619 error(rs, PAZPAR2_NO_SESSION, 0);
625 wrbuf_puts(c->wrbuf, "<record>\n");
626 wrbuf_puts(c->wrbuf, "<recid>");
627 wrbuf_xmlputs(c->wrbuf, rec->recid);
628 wrbuf_puts(c->wrbuf, "</recid>\n");
631 wrbuf_puts(c->wrbuf, "<prevrecid>");
632 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
633 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
637 wrbuf_puts(c->wrbuf, "<nextrecid>");
638 wrbuf_xmlputs(c->wrbuf, next_r->recid);
639 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
641 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
642 session_active_clients(s->psession));
643 write_metadata(c->wrbuf, service, rec->metadata, 1);
644 for (r = rec->records; r; r = r->next)
645 write_subrecord(r, c->wrbuf, service, 1);
646 wrbuf_puts(c->wrbuf, "</record>\n");
647 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
648 http_send_response(c);
652 static void cmd_record_ready(void *data)
654 struct http_channel *c = (struct http_channel *) data;
659 static void show_records(struct http_channel *c, int active)
661 struct http_request *rq = c->request;
662 struct http_response *rs = c->response;
663 struct http_session *s = locate_session(rq, rs);
664 struct record_cluster **rl;
665 struct reclist_sortparms *sp;
666 char *start = http_argbyname(rq, "start");
667 char *num = http_argbyname(rq, "num");
668 char *sort = http_argbyname(rq, "sort");
678 // We haven't counted clients yet if we're called on a block release
680 active = session_active_clients(s->psession);
683 startn = atoi(start);
688 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
690 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
694 rl = show(s->psession, sp, startn, &numn, &total, &total_hits, c->nmem);
696 wrbuf_rewind(c->wrbuf);
697 wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
698 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
699 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
700 wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
701 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
702 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
704 for (i = 0; i < numn; i++)
708 struct record_cluster *rec = rl[i];
709 struct conf_service *service = s->psession->service;
711 wrbuf_puts(c->wrbuf, "<hit>\n");
712 write_metadata(c->wrbuf, service, rec->metadata, 0);
713 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
714 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
716 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
717 wrbuf_puts(c->wrbuf, "<recid>");
718 wrbuf_xmlputs(c->wrbuf, rec->recid);
719 wrbuf_puts(c->wrbuf, "</recid>\n");
720 wrbuf_puts(c->wrbuf, "</hit>\n");
723 wrbuf_puts(c->wrbuf, "</show>\n");
724 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
725 http_send_response(c);
728 static void show_records_ready(void *data)
730 struct http_channel *c = (struct http_channel *) data;
735 static void cmd_show(struct http_channel *c)
737 struct http_request *rq = c->request;
738 struct http_response *rs = c->response;
739 struct http_session *s = locate_session(rq, rs);
740 char *block = http_argbyname(rq, "block");
746 status = session_active_clients(s->psession);
750 if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
752 // if there is already a watch/block. we do not block this one
753 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
754 show_records_ready, c, c) != 0)
756 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
762 show_records(c, status);
765 static void cmd_ping(struct http_channel *c)
767 struct http_request *rq = c->request;
768 struct http_response *rs = c->response;
769 struct http_session *s = locate_session(rq, rs);
772 rs->payload = "<ping><status>OK</status></ping>";
773 http_send_response(c);
776 static int utf_8_valid(const char *str)
778 yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
781 /* check that query is UTF-8 encoded */
782 char *inbuf = (char *) str; /* we know iconv does not alter this */
783 size_t inbytesleft = strlen(inbuf);
785 size_t outbytesleft = strlen(inbuf) + 10;
786 char *out = xmalloc(outbytesleft);
788 size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
790 /* if OK, try flushing the rest */
791 if (r != (size_t) (-1))
792 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
795 if (r == (size_t) (-1))
801 static void cmd_search(struct http_channel *c)
803 struct http_request *rq = c->request;
804 struct http_response *rs = c->response;
805 struct http_session *s = locate_session(rq, rs);
806 char *query = http_argbyname(rq, "query");
807 char *filter = http_argbyname(rq, "filter");
808 enum pazpar2_error_code code;
809 const char *addinfo = 0;
815 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
818 if (!utf_8_valid(query))
820 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
823 code = search(s->psession, query, filter, &addinfo);
826 error(rs, code, addinfo);
829 rs->payload = "<search><status>OK</status></search>";
830 http_send_response(c);
834 static void cmd_stat(struct http_channel *c)
836 struct http_request *rq = c->request;
837 struct http_response *rs = c->response;
838 struct http_session *s = locate_session(rq, rs);
839 struct statistics stat;
847 clients = session_active_clients(s->psession);
848 statistics(s->psession, &stat);
850 if (stat.num_clients > 0) {
851 progress = (stat.num_clients - clients) / (float)stat.num_clients;
854 wrbuf_rewind(c->wrbuf);
855 wrbuf_puts(c->wrbuf, "<stat>");
856 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
857 wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
858 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
859 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
860 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
861 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
862 wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
863 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
864 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
865 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
866 wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
867 wrbuf_puts(c->wrbuf, "</stat>");
868 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
869 http_send_response(c);
872 static void cmd_info(struct http_channel *c)
874 char yaz_version_str[20];
875 struct http_response *rs = c->response;
877 wrbuf_rewind(c->wrbuf);
878 wrbuf_puts(c->wrbuf, "<info>\n");
879 wrbuf_puts(c->wrbuf, " <version>\n");
880 wrbuf_puts(c->wrbuf, "<pazpar2");
881 #ifdef PAZPAR2_VERSION_SHA1
882 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
884 wrbuf_puts(c->wrbuf, ">");
885 wrbuf_xmlputs(c->wrbuf, VERSION);
886 wrbuf_puts(c->wrbuf, "</pazpar2>");
889 yaz_version(yaz_version_str, 0);
890 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
891 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
892 wrbuf_puts(c->wrbuf, "\">");
893 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
894 wrbuf_puts(c->wrbuf, "</yaz>\n");
896 wrbuf_puts(c->wrbuf, " </version>\n");
898 wrbuf_puts(c->wrbuf, "</info>");
899 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
900 http_send_response(c);
905 void (*fun)(struct http_channel *c);
907 { "init", cmd_init },
908 { "settings", cmd_settings },
909 { "stat", cmd_stat },
910 { "bytarget", cmd_bytarget },
911 { "show", cmd_show },
912 { "search", cmd_search },
913 { "termlist", cmd_termlist },
914 { "exit", cmd_exit },
915 { "ping", cmd_ping },
916 { "record", cmd_record },
917 { "info", cmd_info },
921 void http_command(struct http_channel *c)
923 char *command = http_argbyname(c->request, "command");
924 struct http_response *rs = http_create_response(c);
929 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
930 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
934 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
937 for (i = 0; commands[i].name; i++)
938 if (!strcmp(commands[i].name, command))
940 (*commands[i].fun)(c);
943 if (!commands[i].name)
944 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
952 * c-file-style: "Stroustrup"
953 * indent-tabs-mode: nil
955 * vim: shiftwidth=4 tabstop=8 expandtab