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 */
60 struct http_sessions {
61 struct http_session *session_list;
65 http_sessions_t http_sessions_create(void)
67 http_sessions_t hs = xmalloc(sizeof(*hs));
69 yaz_mutex_create(&hs->mutex);
73 void http_sessions_destroy(http_sessions_t hs)
77 yaz_mutex_destroy(&hs->mutex);
78 /* should remove session_list too */
83 void http_session_destroy(struct http_session *s);
85 static void session_timeout(IOCHAN i, int event)
87 struct http_session *s = iochan_getdata(i);
88 http_session_destroy(s);
91 struct http_session *http_session_create(struct conf_service *service)
93 NMEM nmem = nmem_create();
94 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
96 r->psession = new_session(nmem, service);
100 r->next = session_list;
102 r->timeout_iochan = iochan_create(-1, session_timeout, 0);
103 iochan_setdata(r->timeout_iochan, r);
104 iochan_settimeout(r->timeout_iochan, service->session_timeout);
106 pazpar2_add_channel(r->timeout_iochan);
110 void http_session_destroy(struct http_session *s)
112 struct http_session **p;
114 for (p = &session_list; *p; p = &(*p)->next)
120 yaz_log(YLOG_LOG, "Destroying session %u", s->session_id);
121 iochan_destroy(s->timeout_iochan);
122 destroy_session(s->psession);
123 nmem_destroy(s->nmem);
126 static const char *get_msg(enum pazpar2_error_code code)
128 struct pazpar2_error_msg {
129 enum pazpar2_error_code code;
132 static const struct pazpar2_error_msg ar[] = {
133 { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
134 { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
135 { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
136 { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
137 { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
138 { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
139 { PAZPAR2_RECORD_MISSING, "Record missing"},
140 { PAZPAR2_NO_TARGETS, "No targets"},
141 { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
142 { PAZPAR2_RECORD_FAIL, "Record command failed"},
143 { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"},
144 { PAZPAR2_NO_SERVICE, "No service"},
145 { PAZPAR2_LAST_ERROR, "Last error"},
151 if (code == ar[i].code)
158 static void error(struct http_response *rs,
159 enum pazpar2_error_code code,
162 struct http_channel *c = rs->channel;
163 WRBUF text = wrbuf_alloc();
164 const char *http_status = "417";
165 const char *msg = get_msg(code);
167 rs->msg = nmem_strdup(c->nmem, msg);
168 strcpy(rs->code, http_status);
170 wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code,
173 wrbuf_xmlputs(text, addinfo);
174 wrbuf_puts(text, "</error>");
176 yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
177 msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
178 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
180 http_send_response(c);
183 unsigned int make_sessionid(void)
185 static int seq = 0; /* thread pr */
189 if (global_parameters.debug_mode)
198 if (gettimeofday(&t, 0) < 0)
200 yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
203 /* at most 256 sessions per second ..
204 (long long would be more appropriate)*/
206 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
212 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
214 struct http_session *p;
215 const char *session = http_argbyname(rq, "session");
220 error(rs, PAZPAR2_MISSING_PARAMETER, "session");
224 for (p = session_list; p; p = p->next)
225 if (id == p->session_id)
227 iochan_activity(p->timeout_iochan);
230 error(rs, PAZPAR2_NO_SESSION, session);
234 // Decode settings parameters and apply to session
235 // Syntax: setting[target]=value
236 static int process_settings(struct session *se, struct http_request *rq,
237 struct http_response *rs)
239 struct http_argument *a;
241 for (a = rq->arguments; a; a = a->next)
242 if (strchr(a->name, '['))
249 // Nmem_strsplit *rules*!!!
250 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
253 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
258 session_apply_setting(se, dbname, setting,
259 nmem_strdup(se->session_nmem, a->value));
264 static void cmd_exit(struct http_channel *c)
266 yaz_log(YLOG_WARN, "exit");
267 http_close_server(c->server);
270 static void cmd_init(struct http_channel *c)
273 struct http_request *r = c->request;
274 const char *clear = http_argbyname(r, "clear");
275 const char *content_type = http_lookup_header(r->headers, "Content-Type");
277 struct http_session *s;
278 struct http_response *rs = c->response;
279 struct conf_service *service = 0; /* no service (yet) */
281 if (r->content_len && content_type &&
282 !yaz_strcmp_del("text/xml", content_type, "; "))
284 xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
288 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
291 root_n = xmlDocGetRootElement(doc);
292 service = service_create(c->server, root_n);
296 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
303 const char *service_name = http_argbyname(c->request, "service");
304 service = locate_service(c->server, service_name);
307 error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
311 s = http_session_create(service);
313 yaz_log(YLOG_DEBUG, "HTTP Session init");
314 if (!clear || *clear == '0')
315 session_init_databases(s->psession);
317 yaz_log(YLOG_LOG, "No databases preloaded");
319 sesid = make_sessionid();
320 s->session_id = sesid;
321 if (process_settings(s->psession, c->request, c->response) < 0)
324 sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX
325 "<init><status>OK</status><session>%u", sesid);
326 if (c->server->server_id)
329 strcat(buf, c->server->server_id);
331 strcat(buf, "</session>"
332 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>");
333 rs->payload = nmem_strdup(c->nmem, buf);
334 http_send_response(c);
337 static void apply_local_setting(void *client_data,
340 struct session *se = (struct session *) client_data;
342 session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
343 nmem_strdup(se->session_nmem, set->name),
344 nmem_strdup(se->session_nmem, set->value));
347 static void cmd_settings(struct http_channel *c)
349 struct http_response *rs = c->response;
350 struct http_request *rq = c->request;
351 struct http_session *s = locate_session(rq, rs);
352 const char *content_type = http_lookup_header(rq->headers, "Content-Type");
357 if (rq->content_len && content_type &&
358 !yaz_strcmp_del("text/xml", content_type, "; "))
360 xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
364 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
367 root_n = xmlDocGetRootElement(doc);
369 settings_read_node_x(root_n, s->psession, apply_local_setting);
373 if (process_settings(s->psession, rq, rs) < 0)
375 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<settings><status>OK</status></settings>";
376 http_send_response(c);
379 // Compares two hitsbytarget nodes by hitcount
380 static int cmp_ht(const void *p1, const void *p2)
382 const struct hitsbytarget *h1 = p1;
383 const struct hitsbytarget *h2 = p2;
384 return h2->hits - h1->hits;
387 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
388 static void targets_termlist(WRBUF wrbuf, struct session *se, int num,
391 struct hitsbytarget *ht;
394 ht = hitsbytarget(se, &count, nmem);
395 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
396 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
399 // do only print terms which have display names
401 wrbuf_puts(wrbuf, "<term>\n");
403 wrbuf_puts(wrbuf, "<id>");
404 wrbuf_xmlputs(wrbuf, ht[i].id);
405 wrbuf_puts(wrbuf, "</id>\n");
407 wrbuf_puts(wrbuf, "<name>");
408 if (!ht[i].name || !ht[i].name[0])
409 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
411 wrbuf_xmlputs(wrbuf, ht[i].name);
412 wrbuf_puts(wrbuf, "</name>\n");
414 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
417 wrbuf_puts(wrbuf, "<state>");
418 wrbuf_xmlputs(wrbuf, ht[i].state);
419 wrbuf_puts(wrbuf, "</state>\n");
421 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
423 wrbuf_puts(wrbuf, "</term>\n");
427 static void cmd_termlist(struct http_channel *c)
429 struct http_response *rs = c->response;
430 struct http_request *rq = c->request;
431 struct http_session *s = locate_session(rq, rs);
432 struct termlist_score **p;
435 const char *name = http_argbyname(rq, "name");
436 const char *nums = http_argbyname(rq, "num");
443 status = session_active_clients(s->psession);
447 if (strlen(name) > 255)
452 wrbuf_rewind(c->wrbuf);
454 wrbuf_puts(c->wrbuf, "<termlist>\n");
455 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
461 if (!(tp = strchr(name, ',')))
462 tp = name + strlen(name);
463 strncpy(tname, name, tp - name);
464 tname[tp - name] = '\0';
466 wrbuf_puts(c->wrbuf, "<list name=\"");
467 wrbuf_xmlputs(c->wrbuf, tname);
468 wrbuf_puts(c->wrbuf, "\">\n");
469 if (!strcmp(tname, "xtargets"))
470 targets_termlist(c->wrbuf, s->psession, num, c->nmem);
473 p = termlist(s->psession, tname, &len);
475 for (i = 0; i < len && i < num; i++){
476 // prevnt sending empty term elements
477 if (!p[i]->term || !p[i]->term[0])
480 wrbuf_puts(c->wrbuf, "<term>");
481 wrbuf_puts(c->wrbuf, "<name>");
482 wrbuf_xmlputs(c->wrbuf, p[i]->term);
483 wrbuf_puts(c->wrbuf, "</name>");
485 wrbuf_printf(c->wrbuf,
486 "<frequency>%d</frequency>",
488 wrbuf_puts(c->wrbuf, "</term>\n");
491 wrbuf_puts(c->wrbuf, "</list>\n");
496 wrbuf_puts(c->wrbuf, "</termlist>\n");
497 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
498 http_send_response(c);
502 static void cmd_bytarget(struct http_channel *c)
504 struct http_response *rs = c->response;
505 struct http_request *rq = c->request;
506 struct http_session *s = locate_session(rq, rs);
507 struct hitsbytarget *ht;
508 const char *settings = http_argbyname(rq, "settings");
513 ht = hitsbytarget(s->psession, &count, c->nmem);
514 wrbuf_rewind(c->wrbuf);
515 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<bytarget><status>OK</status>");
517 for (i = 0; i < count; i++)
519 wrbuf_puts(c->wrbuf, "\n<target>");
521 wrbuf_puts(c->wrbuf, "<id>");
522 wrbuf_xmlputs(c->wrbuf, ht[i].id);
523 wrbuf_puts(c->wrbuf, "</id>\n");
525 if (ht[i].name && ht[i].name[0])
527 wrbuf_puts(c->wrbuf, "<name>");
528 wrbuf_xmlputs(c->wrbuf, ht[i].name);
529 wrbuf_puts(c->wrbuf, "</name>\n");
532 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
533 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
534 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
536 wrbuf_puts(c->wrbuf, "<state>");
537 wrbuf_xmlputs(c->wrbuf, ht[i].state);
538 wrbuf_puts(c->wrbuf, "</state>\n");
539 if (settings && *settings == '1')
541 wrbuf_puts(c->wrbuf, "<settings>\n");
542 wrbuf_puts(c->wrbuf, wrbuf_cstr(ht[i].settings_xml));
543 wrbuf_puts(c->wrbuf, "</settings>\n");
545 wrbuf_puts(c->wrbuf, "</target>");
546 wrbuf_destroy(ht[i].settings_xml);
549 wrbuf_puts(c->wrbuf, "</bytarget>");
550 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
551 http_send_response(c);
554 static void write_metadata(WRBUF w, struct conf_service *service,
555 struct record_metadata **ml, int full)
559 for (imeta = 0; imeta < service->num_metadata; imeta++)
561 struct conf_metadata *cmd = &service->metadata[imeta];
562 struct record_metadata *md;
563 if (!cmd->brief && !full)
565 for (md = ml[imeta]; md; md = md->next)
567 struct record_metadata_attr *attr = md->attributes;
568 wrbuf_printf(w, "\n<md-%s", cmd->name);
570 for (; attr; attr = attr->next)
572 wrbuf_printf(w, " %s=\"", attr->name);
573 wrbuf_xmlputs(w, attr->value);
579 case Metadata_type_generic:
580 wrbuf_xmlputs(w, md->data.text.disp);
582 case Metadata_type_year:
583 wrbuf_printf(w, "%d", md->data.number.min);
584 if (md->data.number.min != md->data.number.max)
585 wrbuf_printf(w, "-%d", md->data.number.max);
588 wrbuf_puts(w, "[can't represent]");
590 wrbuf_printf(w, "</md-%s>", cmd->name);
595 static void write_subrecord(struct record *r, WRBUF w,
596 struct conf_service *service, int show_details)
598 const char *name = session_setting_oneval(
599 client_get_database(r->client), PZ_NAME);
601 wrbuf_puts(w, "<location id=\"");
602 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
603 wrbuf_puts(w, "\" ");
605 wrbuf_puts(w, "name=\"");
606 wrbuf_xmlputs(w, *name ? name : "Unknown");
607 wrbuf_puts(w, "\">");
609 write_metadata(w, service, r->metadata, show_details);
610 wrbuf_puts(w, "</location>\n");
613 static void show_raw_record_error(void *data, const char *addinfo)
615 http_channel_observer_t obs = data;
616 struct http_channel *c = http_channel_observer_chan(obs);
617 struct http_response *rs = c->response;
619 http_remove_observer(obs);
621 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
624 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
626 http_channel_observer_t obs = data;
627 struct http_channel *c = http_channel_observer_chan(obs);
628 struct http_response *rs = c->response;
630 http_remove_observer(obs);
632 wrbuf_write(c->wrbuf, buf, sz);
633 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
634 http_send_response(c);
638 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
640 http_channel_observer_t obs = data;
641 struct http_channel *c = http_channel_observer_chan(obs);
642 struct http_response *rs = c->response;
644 http_remove_observer(obs);
646 wrbuf_write(c->wrbuf, buf, sz);
647 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
649 rs->content_type = "application/octet-stream";
650 http_send_response(c);
654 void show_raw_reset(void *data, struct http_channel *c, void *data2)
656 //struct client *client = data;
657 //client_show_raw_remove(client, data2);
660 static void cmd_record_ready(void *data);
662 static void cmd_record(struct http_channel *c)
664 struct http_response *rs = c->response;
665 struct http_request *rq = c->request;
666 struct http_session *s = locate_session(rq, rs);
667 struct record_cluster *rec, *prev_r, *next_r;
669 struct conf_service *service;
670 const char *idstr = http_argbyname(rq, "id");
671 const char *offsetstr = http_argbyname(rq, "offset");
672 const char *binarystr = http_argbyname(rq, "binary");
676 service = s->psession->service;
679 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
682 wrbuf_rewind(c->wrbuf);
683 if (!(rec = show_single(s->psession, idstr, &prev_r, &next_r)))
685 if (session_active_clients(s->psession) == 0)
687 error(rs, PAZPAR2_RECORD_MISSING, idstr);
689 else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
690 cmd_record_ready, c, c) != 0)
692 error(rs, PAZPAR2_RECORD_MISSING, idstr);
698 int offset = atoi(offsetstr);
699 const char *syntax = http_argbyname(rq, "syntax");
700 const char *esn = http_argbyname(rq, "esn");
702 struct record*r = rec->records;
705 if (binarystr && *binarystr != '0')
708 for (i = 0; i < offset && r; r = r->next, i++)
712 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
717 http_channel_observer_t obs =
718 http_add_observer(c, r->client, show_raw_reset);
719 int ret = client_show_raw_begin(r->client, r->position,
722 show_raw_record_error,
724 show_raw_record_ok_binary :
729 http_remove_observer(obs);
730 error(rs, PAZPAR2_NO_SESSION, 0);
736 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<record>\n");
737 wrbuf_puts(c->wrbuf, "<recid>");
738 wrbuf_xmlputs(c->wrbuf, rec->recid);
739 wrbuf_puts(c->wrbuf, "</recid>\n");
742 wrbuf_puts(c->wrbuf, "<prevrecid>");
743 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
744 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
748 wrbuf_puts(c->wrbuf, "<nextrecid>");
749 wrbuf_xmlputs(c->wrbuf, next_r->recid);
750 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
752 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
753 session_active_clients(s->psession));
754 write_metadata(c->wrbuf, service, rec->metadata, 1);
755 for (r = rec->records; r; r = r->next)
756 write_subrecord(r, c->wrbuf, service, 1);
757 wrbuf_puts(c->wrbuf, "</record>\n");
758 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
759 http_send_response(c);
763 static void cmd_record_ready(void *data)
765 struct http_channel *c = (struct http_channel *) data;
770 static void show_records(struct http_channel *c, int active)
772 struct http_request *rq = c->request;
773 struct http_response *rs = c->response;
774 struct http_session *s = locate_session(rq, rs);
775 struct record_cluster **rl;
776 struct reclist_sortparms *sp;
777 const char *start = http_argbyname(rq, "start");
778 const char *num = http_argbyname(rq, "num");
779 const char *sort = http_argbyname(rq, "sort");
789 // We haven't counted clients yet if we're called on a block release
791 active = session_active_clients(s->psession);
794 startn = atoi(start);
799 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
801 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
805 rl = show(s->psession, sp, startn, &numn, &total, &total_hits, c->nmem);
807 wrbuf_rewind(c->wrbuf);
808 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<show>\n<status>OK</status>\n");
809 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
810 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
811 wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
812 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
813 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
815 for (i = 0; i < numn; i++)
819 struct record_cluster *rec = rl[i];
820 struct conf_service *service = s->psession->service;
822 wrbuf_puts(c->wrbuf, "<hit>\n");
823 write_metadata(c->wrbuf, service, rec->metadata, 0);
824 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
825 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
827 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
828 if (strstr(sort, "relevance"))
829 wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n", rec->relevance);
830 wrbuf_puts(c->wrbuf, "<recid>");
831 wrbuf_xmlputs(c->wrbuf, rec->recid);
832 wrbuf_puts(c->wrbuf, "</recid>\n");
833 wrbuf_puts(c->wrbuf, "</hit>\n");
836 wrbuf_puts(c->wrbuf, "</show>\n");
837 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
838 http_send_response(c);
841 static void show_records_ready(void *data)
843 struct http_channel *c = (struct http_channel *) data;
848 static void cmd_show(struct http_channel *c)
850 struct http_request *rq = c->request;
851 struct http_response *rs = c->response;
852 struct http_session *s = locate_session(rq, rs);
853 const char *block = http_argbyname(rq, "block");
859 status = session_active_clients(s->psession);
863 if (status && reclist_get_num_records(s->psession->reclist) == 0)
865 // if there is already a watch/block. we do not block this one
866 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
867 show_records_ready, c, c) != 0)
869 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
875 show_records(c, status);
878 static void cmd_ping(struct http_channel *c)
880 struct http_request *rq = c->request;
881 struct http_response *rs = c->response;
882 struct http_session *s = locate_session(rq, rs);
885 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<ping><status>OK</status></ping>";
886 http_send_response(c);
889 static int utf_8_valid(const char *str)
891 yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
894 /* check that query is UTF-8 encoded */
895 char *inbuf = (char *) str; /* we know iconv does not alter this */
896 size_t inbytesleft = strlen(inbuf);
898 size_t outbytesleft = strlen(inbuf) + 10;
899 char *out = xmalloc(outbytesleft);
901 size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
903 /* if OK, try flushing the rest */
904 if (r != (size_t) (-1))
905 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
908 if (r == (size_t) (-1))
914 static void cmd_search(struct http_channel *c)
916 struct http_request *rq = c->request;
917 struct http_response *rs = c->response;
918 struct http_session *s = locate_session(rq, rs);
919 const char *query = http_argbyname(rq, "query");
920 const char *filter = http_argbyname(rq, "filter");
921 const char *maxrecs = http_argbyname(rq, "maxrecs");
922 const char *startrecs = http_argbyname(rq, "startrecs");
923 enum pazpar2_error_code code;
924 const char *addinfo = 0;
930 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
933 if (!utf_8_valid(query))
935 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
938 code = search(s->psession, query, startrecs, maxrecs, filter, &addinfo);
941 error(rs, code, addinfo);
944 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<search><status>OK</status></search>";
945 http_send_response(c);
949 static void cmd_stat(struct http_channel *c)
951 struct http_request *rq = c->request;
952 struct http_response *rs = c->response;
953 struct http_session *s = locate_session(rq, rs);
954 struct statistics stat;
962 clients = session_active_clients(s->psession);
963 statistics(s->psession, &stat);
965 if (stat.num_clients > 0) {
966 progress = (stat.num_clients - clients) / (float)stat.num_clients;
969 wrbuf_rewind(c->wrbuf);
970 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<stat>");
971 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
972 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
973 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
974 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
975 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
976 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
977 wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
978 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
979 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
980 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
981 wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
982 wrbuf_puts(c->wrbuf, "</stat>");
983 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
984 http_send_response(c);
987 static void cmd_info(struct http_channel *c)
989 char yaz_version_str[20];
990 struct http_response *rs = c->response;
992 wrbuf_rewind(c->wrbuf);
993 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<info>\n");
994 wrbuf_puts(c->wrbuf, " <version>\n");
995 wrbuf_puts(c->wrbuf, "<pazpar2");
996 #ifdef PAZPAR2_VERSION_SHA1
997 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
999 wrbuf_puts(c->wrbuf, ">");
1000 wrbuf_xmlputs(c->wrbuf, VERSION);
1001 wrbuf_puts(c->wrbuf, "</pazpar2>");
1004 yaz_version(yaz_version_str, 0);
1005 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
1006 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
1007 wrbuf_puts(c->wrbuf, "\">");
1008 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
1009 wrbuf_puts(c->wrbuf, "</yaz>\n");
1011 wrbuf_puts(c->wrbuf, " </version>\n");
1013 wrbuf_puts(c->wrbuf, "</info>");
1014 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1015 http_send_response(c);
1020 void (*fun)(struct http_channel *c);
1022 { "init", cmd_init },
1023 { "settings", cmd_settings },
1024 { "stat", cmd_stat },
1025 { "bytarget", cmd_bytarget },
1026 { "show", cmd_show },
1027 { "search", cmd_search },
1028 { "termlist", cmd_termlist },
1029 { "exit", cmd_exit },
1030 { "ping", cmd_ping },
1031 { "record", cmd_record },
1032 { "info", cmd_info },
1036 void http_command(struct http_channel *c)
1038 const char *command = http_argbyname(c->request, "command");
1039 struct http_response *rs = http_create_response(c);
1044 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1045 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1049 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1052 for (i = 0; commands[i].name; i++)
1053 if (!strcmp(commands[i].name, command))
1055 (*commands[i].fun)(c);
1058 if (!commands[i].name)
1059 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1067 * c-file-style: "Stroustrup"
1068 * indent-tabs-mode: nil
1070 * vim: shiftwidth=4 tabstop=8 expandtab