1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2010 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>
37 #include "parameters.h"
43 // Update this when the protocol changes
44 #define PAZPAR2_PROTOCOL_VERSION "1"
46 #define HTTP_COMMAND_RESPONSE_PREFIX "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
49 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
50 struct session *psession;
51 unsigned int session_id;
54 struct http_session *next;
57 static struct http_session *session_list = 0; /* thread pr */
59 void http_session_destroy(struct http_session *s);
61 static void session_timeout(IOCHAN i, int event)
63 struct http_session *s = iochan_getdata(i);
64 http_session_destroy(s);
67 struct http_session *http_session_create(struct conf_service *service)
69 NMEM nmem = nmem_create();
70 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
72 r->psession = new_session(nmem, service);
76 r->next = session_list;
78 r->timeout_iochan = iochan_create(-1, session_timeout, 0);
79 iochan_setdata(r->timeout_iochan, r);
80 iochan_settimeout(r->timeout_iochan, service->session_timeout);
82 pazpar2_add_channel(r->timeout_iochan);
86 void http_session_destroy(struct http_session *s)
88 struct http_session **p;
90 for (p = &session_list; *p; p = &(*p)->next)
96 yaz_log(YLOG_LOG, "Destroying session %u", s->session_id);
97 iochan_destroy(s->timeout_iochan);
98 destroy_session(s->psession);
99 nmem_destroy(s->nmem);
102 static const char *get_msg(enum pazpar2_error_code code)
104 struct pazpar2_error_msg {
105 enum pazpar2_error_code code;
108 static const struct pazpar2_error_msg ar[] = {
109 { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
110 { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
111 { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
112 { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
113 { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
114 { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
115 { PAZPAR2_RECORD_MISSING, "Record missing"},
116 { PAZPAR2_NO_TARGETS, "No targets"},
117 { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
118 { PAZPAR2_RECORD_FAIL, "Record command failed"},
119 { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"},
120 { PAZPAR2_NO_SERVICE, "No service"},
121 { PAZPAR2_LAST_ERROR, "Last error"},
127 if (code == ar[i].code)
134 static void error(struct http_response *rs,
135 enum pazpar2_error_code code,
138 struct http_channel *c = rs->channel;
139 WRBUF text = wrbuf_alloc();
140 const char *http_status = "417";
141 const char *msg = get_msg(code);
143 rs->msg = nmem_strdup(c->nmem, msg);
144 strcpy(rs->code, http_status);
146 wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code,
149 wrbuf_xmlputs(text, addinfo);
150 wrbuf_puts(text, "</error>");
152 yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
153 msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
154 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
156 http_send_response(c);
159 unsigned int make_sessionid(void)
161 static int seq = 0; /* thread pr */
165 if (global_parameters.debug_mode)
174 if (gettimeofday(&t, 0) < 0)
176 yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
179 /* at most 256 sessions per second ..
180 (long long would be more appropriate)*/
182 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
188 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
190 struct http_session *p;
191 const char *session = http_argbyname(rq, "session");
196 error(rs, PAZPAR2_MISSING_PARAMETER, "session");
200 for (p = session_list; p; p = p->next)
201 if (id == p->session_id)
203 iochan_activity(p->timeout_iochan);
206 error(rs, PAZPAR2_NO_SESSION, session);
210 // Decode settings parameters and apply to session
211 // Syntax: setting[target]=value
212 static int process_settings(struct session *se, struct http_request *rq,
213 struct http_response *rs)
215 struct http_argument *a;
217 for (a = rq->arguments; a; a = a->next)
218 if (strchr(a->name, '['))
225 // Nmem_strsplit *rules*!!!
226 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
229 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
234 session_apply_setting(se, dbname, setting,
235 nmem_strdup(se->session_nmem, a->value));
240 static void cmd_exit(struct http_channel *c)
242 yaz_log(YLOG_WARN, "exit");
243 http_close_server(c->server);
246 static void cmd_init(struct http_channel *c)
249 struct http_request *r = c->request;
250 const char *clear = http_argbyname(r, "clear");
251 const char *content_type = http_lookup_header(r->headers, "Content-Type");
253 struct http_session *s;
254 struct http_response *rs = c->response;
255 struct conf_service *service = 0; /* no service (yet) */
257 if (r->content_len && content_type &&
258 !yaz_strcmp_del("text/xml", content_type, "; "))
260 xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
264 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
267 root_n = xmlDocGetRootElement(doc);
268 service = service_create(c->server, root_n);
272 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
279 const char *service_name = http_argbyname(c->request, "service");
280 service = locate_service(c->server, service_name);
283 error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
287 s = http_session_create(service);
289 yaz_log(YLOG_DEBUG, "HTTP Session init");
290 if (!clear || *clear == '0')
291 session_init_databases(s->psession);
293 yaz_log(YLOG_LOG, "No databases preloaded");
295 sesid = make_sessionid();
296 s->session_id = sesid;
297 if (process_settings(s->psession, c->request, c->response) < 0)
300 sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX
301 "<init><status>OK</status><session>%u", sesid);
302 if (c->server->server_id)
305 strcat(buf, c->server->server_id);
307 strcat(buf, "</session>"
308 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>");
309 rs->payload = nmem_strdup(c->nmem, buf);
310 http_send_response(c);
313 static void apply_local_setting(void *client_data,
316 struct session *se = (struct session *) client_data;
318 session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
319 nmem_strdup(se->session_nmem, set->name),
320 nmem_strdup(se->session_nmem, set->value));
323 static void cmd_settings(struct http_channel *c)
325 struct http_response *rs = c->response;
326 struct http_request *rq = c->request;
327 struct http_session *s = locate_session(rq, rs);
328 const char *content_type = http_lookup_header(rq->headers, "Content-Type");
333 if (rq->content_len && content_type &&
334 !yaz_strcmp_del("text/xml", content_type, "; "))
336 xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
340 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
343 root_n = xmlDocGetRootElement(doc);
345 settings_read_node_x(root_n, s->psession, apply_local_setting);
349 if (process_settings(s->psession, rq, rs) < 0)
351 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<settings><status>OK</status></settings>";
352 http_send_response(c);
355 // Compares two hitsbytarget nodes by hitcount
356 static int cmp_ht(const void *p1, const void *p2)
358 const struct hitsbytarget *h1 = p1;
359 const struct hitsbytarget *h2 = p2;
360 return h2->hits - h1->hits;
363 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
364 static void targets_termlist(WRBUF wrbuf, struct session *se, int num,
367 struct hitsbytarget *ht;
370 ht = hitsbytarget(se, &count, nmem);
371 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
372 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
375 // do only print terms which have display names
377 wrbuf_puts(wrbuf, "<term>\n");
379 wrbuf_puts(wrbuf, "<id>");
380 wrbuf_xmlputs(wrbuf, ht[i].id);
381 wrbuf_puts(wrbuf, "</id>\n");
383 wrbuf_puts(wrbuf, "<name>");
384 if (!ht[i].name || !ht[i].name[0])
385 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
387 wrbuf_xmlputs(wrbuf, ht[i].name);
388 wrbuf_puts(wrbuf, "</name>\n");
390 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
393 wrbuf_puts(wrbuf, "<state>");
394 wrbuf_xmlputs(wrbuf, ht[i].state);
395 wrbuf_puts(wrbuf, "</state>\n");
397 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
399 wrbuf_puts(wrbuf, "</term>\n");
403 static void cmd_termlist(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 termlist_score **p;
411 const char *name = http_argbyname(rq, "name");
412 const char *nums = http_argbyname(rq, "num");
419 status = session_active_clients(s->psession);
423 if (strlen(name) > 255)
428 wrbuf_rewind(c->wrbuf);
430 wrbuf_puts(c->wrbuf, "<termlist>\n");
431 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
437 if (!(tp = strchr(name, ',')))
438 tp = name + strlen(name);
439 strncpy(tname, name, tp - name);
440 tname[tp - name] = '\0';
442 wrbuf_puts(c->wrbuf, "<list name=\"");
443 wrbuf_xmlputs(c->wrbuf, tname);
444 wrbuf_puts(c->wrbuf, "\">\n");
445 if (!strcmp(tname, "xtargets"))
446 targets_termlist(c->wrbuf, s->psession, num, c->nmem);
449 p = termlist(s->psession, tname, &len);
451 for (i = 0; i < len && i < num; i++){
452 // prevnt sending empty term elements
453 if (!p[i]->term || !p[i]->term[0])
456 wrbuf_puts(c->wrbuf, "<term>");
457 wrbuf_puts(c->wrbuf, "<name>");
458 wrbuf_xmlputs(c->wrbuf, p[i]->term);
459 wrbuf_puts(c->wrbuf, "</name>");
461 wrbuf_printf(c->wrbuf,
462 "<frequency>%d</frequency>",
464 wrbuf_puts(c->wrbuf, "</term>\n");
467 wrbuf_puts(c->wrbuf, "</list>\n");
472 wrbuf_puts(c->wrbuf, "</termlist>\n");
473 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
474 http_send_response(c);
478 static void cmd_bytarget(struct http_channel *c)
480 struct http_response *rs = c->response;
481 struct http_request *rq = c->request;
482 struct http_session *s = locate_session(rq, rs);
483 struct hitsbytarget *ht;
484 const char *settings = http_argbyname(rq, "settings");
489 ht = hitsbytarget(s->psession, &count, c->nmem);
490 wrbuf_rewind(c->wrbuf);
491 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<bytarget><status>OK</status>");
493 for (i = 0; i < count; i++)
495 wrbuf_puts(c->wrbuf, "\n<target>");
497 wrbuf_puts(c->wrbuf, "<id>");
498 wrbuf_xmlputs(c->wrbuf, ht[i].id);
499 wrbuf_puts(c->wrbuf, "</id>\n");
501 if (ht[i].name && ht[i].name[0])
503 wrbuf_puts(c->wrbuf, "<name>");
504 wrbuf_xmlputs(c->wrbuf, ht[i].name);
505 wrbuf_puts(c->wrbuf, "</name>\n");
508 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
509 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
510 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
512 wrbuf_puts(c->wrbuf, "<state>");
513 wrbuf_xmlputs(c->wrbuf, ht[i].state);
514 wrbuf_puts(c->wrbuf, "</state>\n");
515 if (settings && *settings == '1')
517 wrbuf_puts(c->wrbuf, "<settings>\n");
518 wrbuf_puts(c->wrbuf, wrbuf_cstr(ht[i].settings_xml));
519 wrbuf_puts(c->wrbuf, "</settings>\n");
521 wrbuf_puts(c->wrbuf, "</target>");
522 wrbuf_destroy(ht[i].settings_xml);
525 wrbuf_puts(c->wrbuf, "</bytarget>");
526 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
527 http_send_response(c);
530 static void write_metadata(WRBUF w, struct conf_service *service,
531 struct record_metadata **ml, int full)
535 for (imeta = 0; imeta < service->num_metadata; imeta++)
537 struct conf_metadata *cmd = &service->metadata[imeta];
538 struct record_metadata *md;
539 if (!cmd->brief && !full)
541 for (md = ml[imeta]; md; md = md->next)
543 struct record_metadata_attr *attr = md->attributes;
544 wrbuf_printf(w, "\n<md-%s", cmd->name);
546 for (; attr; attr = attr->next)
548 wrbuf_printf(w, " %s=\"", attr->name);
549 wrbuf_xmlputs(w, attr->value);
555 case Metadata_type_generic:
556 wrbuf_xmlputs(w, md->data.text.disp);
558 case Metadata_type_year:
559 wrbuf_printf(w, "%d", md->data.number.min);
560 if (md->data.number.min != md->data.number.max)
561 wrbuf_printf(w, "-%d", md->data.number.max);
564 wrbuf_puts(w, "[can't represent]");
566 wrbuf_printf(w, "</md-%s>", cmd->name);
571 static void write_subrecord(struct record *r, WRBUF w,
572 struct conf_service *service, int show_details)
574 const char *name = session_setting_oneval(
575 client_get_database(r->client), PZ_NAME);
577 wrbuf_puts(w, "<location id=\"");
578 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
579 wrbuf_puts(w, "\" ");
581 wrbuf_puts(w, "name=\"");
582 wrbuf_xmlputs(w, *name ? name : "Unknown");
583 wrbuf_puts(w, "\">");
585 write_metadata(w, service, r->metadata, show_details);
586 wrbuf_puts(w, "</location>\n");
589 static void show_raw_record_error(void *data, const char *addinfo)
591 http_channel_observer_t obs = data;
592 struct http_channel *c = http_channel_observer_chan(obs);
593 struct http_response *rs = c->response;
595 http_remove_observer(obs);
597 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
600 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
602 http_channel_observer_t obs = data;
603 struct http_channel *c = http_channel_observer_chan(obs);
604 struct http_response *rs = c->response;
606 http_remove_observer(obs);
608 wrbuf_write(c->wrbuf, buf, sz);
609 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
610 http_send_response(c);
614 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
616 http_channel_observer_t obs = data;
617 struct http_channel *c = http_channel_observer_chan(obs);
618 struct http_response *rs = c->response;
620 http_remove_observer(obs);
622 wrbuf_write(c->wrbuf, buf, sz);
623 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
625 rs->content_type = "application/octet-stream";
626 http_send_response(c);
630 void show_raw_reset(void *data, struct http_channel *c, void *data2)
632 //struct client *client = data;
633 //client_show_raw_remove(client, data2);
636 static void cmd_record_ready(void *data);
638 static void cmd_record(struct http_channel *c)
640 struct http_response *rs = c->response;
641 struct http_request *rq = c->request;
642 struct http_session *s = locate_session(rq, rs);
643 struct record_cluster *rec, *prev_r, *next_r;
645 struct conf_service *service;
646 const char *idstr = http_argbyname(rq, "id");
647 const char *offsetstr = http_argbyname(rq, "offset");
648 const char *binarystr = http_argbyname(rq, "binary");
652 service = s->psession->service;
655 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
658 wrbuf_rewind(c->wrbuf);
659 if (!(rec = show_single(s->psession, idstr, &prev_r, &next_r)))
661 if (session_active_clients(s->psession) == 0)
663 error(rs, PAZPAR2_RECORD_MISSING, idstr);
665 else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
666 cmd_record_ready, c, c) != 0)
668 error(rs, PAZPAR2_RECORD_MISSING, idstr);
674 int offset = atoi(offsetstr);
675 const char *syntax = http_argbyname(rq, "syntax");
676 const char *esn = http_argbyname(rq, "esn");
678 struct record*r = rec->records;
681 if (binarystr && *binarystr != '0')
684 for (i = 0; i < offset && r; r = r->next, i++)
688 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
693 http_channel_observer_t obs =
694 http_add_observer(c, r->client, show_raw_reset);
695 int ret = client_show_raw_begin(r->client, r->position,
698 show_raw_record_error,
700 show_raw_record_ok_binary :
705 http_remove_observer(obs);
706 error(rs, PAZPAR2_NO_SESSION, 0);
712 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<record>\n");
713 wrbuf_puts(c->wrbuf, "<recid>");
714 wrbuf_xmlputs(c->wrbuf, rec->recid);
715 wrbuf_puts(c->wrbuf, "</recid>\n");
718 wrbuf_puts(c->wrbuf, "<prevrecid>");
719 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
720 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
724 wrbuf_puts(c->wrbuf, "<nextrecid>");
725 wrbuf_xmlputs(c->wrbuf, next_r->recid);
726 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
728 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
729 session_active_clients(s->psession));
730 write_metadata(c->wrbuf, service, rec->metadata, 1);
731 for (r = rec->records; r; r = r->next)
732 write_subrecord(r, c->wrbuf, service, 1);
733 wrbuf_puts(c->wrbuf, "</record>\n");
734 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
735 http_send_response(c);
739 static void cmd_record_ready(void *data)
741 struct http_channel *c = (struct http_channel *) data;
746 static void show_records(struct http_channel *c, int active)
748 struct http_request *rq = c->request;
749 struct http_response *rs = c->response;
750 struct http_session *s = locate_session(rq, rs);
751 struct record_cluster **rl;
752 struct reclist_sortparms *sp;
753 const char *start = http_argbyname(rq, "start");
754 const char *num = http_argbyname(rq, "num");
755 const char *sort = http_argbyname(rq, "sort");
765 // We haven't counted clients yet if we're called on a block release
767 active = session_active_clients(s->psession);
770 startn = atoi(start);
775 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
777 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
781 rl = show(s->psession, sp, startn, &numn, &total, &total_hits, c->nmem);
783 wrbuf_rewind(c->wrbuf);
784 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<show>\n<status>OK</status>\n");
785 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
786 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
787 wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
788 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
789 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
791 for (i = 0; i < numn; i++)
795 struct record_cluster *rec = rl[i];
796 struct conf_service *service = s->psession->service;
798 wrbuf_puts(c->wrbuf, "<hit>\n");
799 write_metadata(c->wrbuf, service, rec->metadata, 0);
800 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
801 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
803 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
804 if (strstr(sort, "relevance"))
805 wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n", rec->relevance);
806 wrbuf_puts(c->wrbuf, "<recid>");
807 wrbuf_xmlputs(c->wrbuf, rec->recid);
808 wrbuf_puts(c->wrbuf, "</recid>\n");
809 wrbuf_puts(c->wrbuf, "</hit>\n");
812 wrbuf_puts(c->wrbuf, "</show>\n");
813 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
814 http_send_response(c);
817 static void show_records_ready(void *data)
819 struct http_channel *c = (struct http_channel *) data;
824 static void cmd_show(struct http_channel *c)
826 struct http_request *rq = c->request;
827 struct http_response *rs = c->response;
828 struct http_session *s = locate_session(rq, rs);
829 const char *block = http_argbyname(rq, "block");
835 status = session_active_clients(s->psession);
839 if (status && reclist_get_num_records(s->psession->reclist) == 0)
841 // if there is already a watch/block. we do not block this one
842 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
843 show_records_ready, c, c) != 0)
845 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
851 show_records(c, status);
854 static void cmd_ping(struct http_channel *c)
856 struct http_request *rq = c->request;
857 struct http_response *rs = c->response;
858 struct http_session *s = locate_session(rq, rs);
861 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<ping><status>OK</status></ping>";
862 http_send_response(c);
865 static int utf_8_valid(const char *str)
867 yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
870 /* check that query is UTF-8 encoded */
871 char *inbuf = (char *) str; /* we know iconv does not alter this */
872 size_t inbytesleft = strlen(inbuf);
874 size_t outbytesleft = strlen(inbuf) + 10;
875 char *out = xmalloc(outbytesleft);
877 size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
879 /* if OK, try flushing the rest */
880 if (r != (size_t) (-1))
881 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
884 if (r == (size_t) (-1))
890 static void cmd_search(struct http_channel *c)
892 struct http_request *rq = c->request;
893 struct http_response *rs = c->response;
894 struct http_session *s = locate_session(rq, rs);
895 const char *query = http_argbyname(rq, "query");
896 const char *filter = http_argbyname(rq, "filter");
897 const char *maxrecs = http_argbyname(rq, "maxrecs");
898 const char *startrecs = http_argbyname(rq, "startrecs");
899 enum pazpar2_error_code code;
900 const char *addinfo = 0;
906 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
909 if (!utf_8_valid(query))
911 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
914 code = search(s->psession, query, startrecs, maxrecs, filter, &addinfo);
917 error(rs, code, addinfo);
920 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<search><status>OK</status></search>";
921 http_send_response(c);
925 static void cmd_stat(struct http_channel *c)
927 struct http_request *rq = c->request;
928 struct http_response *rs = c->response;
929 struct http_session *s = locate_session(rq, rs);
930 struct statistics stat;
938 clients = session_active_clients(s->psession);
939 statistics(s->psession, &stat);
941 if (stat.num_clients > 0) {
942 progress = (stat.num_clients - clients) / (float)stat.num_clients;
945 wrbuf_rewind(c->wrbuf);
946 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<stat>");
947 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
948 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
949 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
950 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
951 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
952 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
953 wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
954 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
955 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
956 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
957 wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
958 wrbuf_puts(c->wrbuf, "</stat>");
959 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
960 http_send_response(c);
963 static void cmd_info(struct http_channel *c)
965 char yaz_version_str[20];
966 struct http_response *rs = c->response;
968 wrbuf_rewind(c->wrbuf);
969 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<info>\n");
970 wrbuf_puts(c->wrbuf, " <version>\n");
971 wrbuf_puts(c->wrbuf, "<pazpar2");
972 #ifdef PAZPAR2_VERSION_SHA1
973 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
975 wrbuf_puts(c->wrbuf, ">");
976 wrbuf_xmlputs(c->wrbuf, VERSION);
977 wrbuf_puts(c->wrbuf, "</pazpar2>");
980 yaz_version(yaz_version_str, 0);
981 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
982 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
983 wrbuf_puts(c->wrbuf, "\">");
984 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
985 wrbuf_puts(c->wrbuf, "</yaz>\n");
987 wrbuf_puts(c->wrbuf, " </version>\n");
989 wrbuf_puts(c->wrbuf, "</info>");
990 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
991 http_send_response(c);
996 void (*fun)(struct http_channel *c);
998 { "init", cmd_init },
999 { "settings", cmd_settings },
1000 { "stat", cmd_stat },
1001 { "bytarget", cmd_bytarget },
1002 { "show", cmd_show },
1003 { "search", cmd_search },
1004 { "termlist", cmd_termlist },
1005 { "exit", cmd_exit },
1006 { "ping", cmd_ping },
1007 { "record", cmd_record },
1008 { "info", cmd_info },
1012 void http_command(struct http_channel *c)
1014 const char *command = http_argbyname(c->request, "command");
1015 struct http_response *rs = http_create_response(c);
1020 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1021 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1025 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1028 for (i = 0; commands[i].name; i++)
1029 if (!strcmp(commands[i].name, command))
1031 (*commands[i].fun)(c);
1034 if (!commands[i].name)
1035 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1043 * c-file-style: "Stroustrup"
1044 * indent-tabs-mode: nil
1046 * vim: shiftwidth=4 tabstop=8 expandtab