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;
56 http_sessions_t http_sessions;
57 struct http_session *next;
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));
70 yaz_mutex_create(&hs->mutex);
74 void http_sessions_destroy(http_sessions_t hs)
78 struct http_session *s = hs->session_list;
81 struct http_session *s_next = s->next;
82 iochan_destroy(s->timeout_iochan);
83 destroy_session(s->psession);
84 nmem_destroy(s->nmem);
87 yaz_mutex_destroy(&hs->mutex);
92 void http_session_destroy(struct http_session *s);
94 static void session_timeout(IOCHAN i, int event)
96 struct http_session *s = iochan_getdata(i);
97 http_session_destroy(s);
100 struct http_session *http_session_create(struct conf_service *service,
101 http_sessions_t http_sessions)
103 NMEM nmem = nmem_create();
104 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
106 r->psession = new_session(nmem, service);
110 r->destroy_counter = r->activity_counter = 0;
111 r->http_sessions = http_sessions;
113 yaz_mutex_enter(http_sessions->mutex);
114 r->next = http_sessions->session_list;
115 http_sessions->session_list = r;
116 yaz_mutex_leave(http_sessions->mutex);
118 r->timeout_iochan = iochan_create(-1, session_timeout, 0, "http_session_timeout");
119 iochan_setdata(r->timeout_iochan, r);
120 yaz_log(YLOG_LOG, "timeout=%d", service->session_timeout);
121 iochan_settimeout(r->timeout_iochan, service->session_timeout);
123 iochan_add(service->server->iochan_man, r->timeout_iochan);
127 void http_session_destroy(struct http_session *s)
129 int must_destroy = 1;
131 http_sessions_t http_sessions = s->http_sessions;
133 yaz_log(YLOG_LOG, "http_session_destroy %u", s->session_id);
134 yaz_mutex_enter(http_sessions->mutex);
136 /* only if http_session destroy was already called, we will allow it
138 if (s->destroy_counter != s->activity_counter)
141 /* only if there are no active Z39.50 clients we will allow it to be
143 if (session_active_clients(s->psession))
146 s->destroy_counter = s->activity_counter = 0;
149 struct http_session **p = 0;
150 for (p = &http_sessions->session_list; *p; p = &(*p)->next)
157 yaz_mutex_leave(http_sessions->mutex);
159 { /* destroying for real */
160 yaz_log(YLOG_LOG, "Destroying session %u", s->session_id);
161 iochan_destroy(s->timeout_iochan);
162 destroy_session(s->psession);
163 nmem_destroy(s->nmem);
167 static const char *get_msg(enum pazpar2_error_code code)
169 struct pazpar2_error_msg {
170 enum pazpar2_error_code code;
173 static const struct pazpar2_error_msg ar[] = {
174 { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
175 { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
176 { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
177 { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
178 { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
179 { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
180 { PAZPAR2_RECORD_MISSING, "Record missing"},
181 { PAZPAR2_NO_TARGETS, "No targets"},
182 { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
183 { PAZPAR2_RECORD_FAIL, "Record command failed"},
184 { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"},
185 { PAZPAR2_NO_SERVICE, "No service"},
186 { PAZPAR2_LAST_ERROR, "Last error"},
192 if (code == ar[i].code)
199 static void error(struct http_response *rs,
200 enum pazpar2_error_code code,
203 struct http_channel *c = rs->channel;
204 WRBUF text = wrbuf_alloc();
205 const char *http_status = "417";
206 const char *msg = get_msg(code);
208 rs->msg = nmem_strdup(c->nmem, msg);
209 strcpy(rs->code, http_status);
211 wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code,
214 wrbuf_xmlputs(text, addinfo);
215 wrbuf_puts(text, "</error>");
217 yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
218 msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
219 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
221 http_send_response(c);
224 unsigned int make_sessionid(void)
226 static int seq = 0; /* thread pr */
230 if (global_parameters.debug_mode)
239 if (gettimeofday(&t, 0) < 0)
241 yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
244 /* at most 256 sessions per second ..
245 (long long would be more appropriate)*/
247 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
253 static struct http_session *locate_session(struct http_channel *c)
255 struct http_response *rs = c->response;
256 struct http_request *rq = c->request;
257 struct http_session *p;
258 const char *session = http_argbyname(rq, "session");
259 http_sessions_t http_sessions = c->http_sessions;
264 error(rs, PAZPAR2_MISSING_PARAMETER, "session");
268 yaz_mutex_enter(http_sessions->mutex);
269 for (p = http_sessions->session_list; p; p = p->next)
270 if (id == p->session_id)
273 p->activity_counter++;
274 yaz_mutex_leave(http_sessions->mutex);
276 iochan_activity(p->timeout_iochan);
278 error(rs, PAZPAR2_NO_SESSION, session);
282 // Decode settings parameters and apply to session
283 // Syntax: setting[target]=value
284 static int process_settings(struct session *se, struct http_request *rq,
285 struct http_response *rs)
287 struct http_argument *a;
289 for (a = rq->arguments; a; a = a->next)
290 if (strchr(a->name, '['))
297 // Nmem_strsplit *rules*!!!
298 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
301 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
306 session_apply_setting(se, dbname, setting,
307 nmem_strdup(se->session_nmem, a->value));
312 static void cmd_exit(struct http_channel *c)
314 yaz_log(YLOG_WARN, "exit");
315 http_close_server(c->server);
318 static void cmd_init(struct http_channel *c)
321 struct http_request *r = c->request;
322 const char *clear = http_argbyname(r, "clear");
323 const char *content_type = http_lookup_header(r->headers, "Content-Type");
325 struct http_session *s;
326 struct http_response *rs = c->response;
327 struct conf_service *service = 0; /* no service (yet) */
329 if (r->content_len && content_type &&
330 !yaz_strcmp_del("text/xml", content_type, "; "))
332 xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
336 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
339 root_n = xmlDocGetRootElement(doc);
340 service = service_create(c->server, root_n);
344 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
351 const char *service_name = http_argbyname(c->request, "service");
352 service = locate_service(c->server, service_name);
355 error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
359 s = http_session_create(service, c->http_sessions);
361 yaz_log(YLOG_DEBUG, "HTTP Session init");
362 if (!clear || *clear == '0')
363 session_init_databases(s->psession);
365 yaz_log(YLOG_LOG, "No databases preloaded");
367 sesid = make_sessionid();
368 s->session_id = sesid;
369 if (process_settings(s->psession, c->request, c->response) < 0)
372 sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX
373 "<init><status>OK</status><session>%u", sesid);
374 if (c->server->server_id)
377 strcat(buf, c->server->server_id);
379 strcat(buf, "</session>"
380 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>");
381 rs->payload = nmem_strdup(c->nmem, buf);
382 http_send_response(c);
385 static void apply_local_setting(void *client_data,
388 struct session *se = (struct session *) client_data;
390 session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
391 nmem_strdup(se->session_nmem, set->name),
392 nmem_strdup(se->session_nmem, set->value));
395 static void cmd_settings(struct http_channel *c)
397 struct http_response *rs = c->response;
398 struct http_request *rq = c->request;
399 struct http_session *s = locate_session(c);
400 const char *content_type = http_lookup_header(rq->headers, "Content-Type");
405 if (rq->content_len && content_type &&
406 !yaz_strcmp_del("text/xml", content_type, "; "))
408 xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
412 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
415 root_n = xmlDocGetRootElement(doc);
417 settings_read_node_x(root_n, s->psession, apply_local_setting);
421 if (process_settings(s->psession, rq, rs) < 0)
423 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<settings><status>OK</status></settings>";
424 http_send_response(c);
427 // Compares two hitsbytarget nodes by hitcount
428 static int cmp_ht(const void *p1, const void *p2)
430 const struct hitsbytarget *h1 = p1;
431 const struct hitsbytarget *h2 = p2;
432 return h2->hits - h1->hits;
435 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
436 static void targets_termlist(WRBUF wrbuf, struct session *se, int num,
439 struct hitsbytarget *ht;
442 ht = hitsbytarget(se, &count, nmem);
443 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
444 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
447 // do only print terms which have display names
449 wrbuf_puts(wrbuf, "<term>\n");
451 wrbuf_puts(wrbuf, "<id>");
452 wrbuf_xmlputs(wrbuf, ht[i].id);
453 wrbuf_puts(wrbuf, "</id>\n");
455 wrbuf_puts(wrbuf, "<name>");
456 if (!ht[i].name || !ht[i].name[0])
457 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
459 wrbuf_xmlputs(wrbuf, ht[i].name);
460 wrbuf_puts(wrbuf, "</name>\n");
462 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
465 wrbuf_puts(wrbuf, "<state>");
466 wrbuf_xmlputs(wrbuf, ht[i].state);
467 wrbuf_puts(wrbuf, "</state>\n");
469 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
471 wrbuf_puts(wrbuf, "</term>\n");
475 static void cmd_termlist(struct http_channel *c)
477 struct http_response *rs = c->response;
478 struct http_request *rq = c->request;
479 struct http_session *s = locate_session(c);
480 struct termlist_score **p;
483 const char *name = http_argbyname(rq, "name");
484 const char *nums = http_argbyname(rq, "num");
491 status = session_active_clients(s->psession);
495 if (strlen(name) > 255)
500 wrbuf_rewind(c->wrbuf);
502 wrbuf_puts(c->wrbuf, "<termlist>\n");
503 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
509 if (!(tp = strchr(name, ',')))
510 tp = name + strlen(name);
511 strncpy(tname, name, tp - name);
512 tname[tp - name] = '\0';
514 wrbuf_puts(c->wrbuf, "<list name=\"");
515 wrbuf_xmlputs(c->wrbuf, tname);
516 wrbuf_puts(c->wrbuf, "\">\n");
517 if (!strcmp(tname, "xtargets"))
518 targets_termlist(c->wrbuf, s->psession, num, c->nmem);
521 p = termlist(s->psession, tname, &len);
523 for (i = 0; i < len && i < num; i++){
524 // prevnt sending empty term elements
525 if (!p[i]->term || !p[i]->term[0])
528 wrbuf_puts(c->wrbuf, "<term>");
529 wrbuf_puts(c->wrbuf, "<name>");
530 wrbuf_xmlputs(c->wrbuf, p[i]->term);
531 wrbuf_puts(c->wrbuf, "</name>");
533 wrbuf_printf(c->wrbuf,
534 "<frequency>%d</frequency>",
536 wrbuf_puts(c->wrbuf, "</term>\n");
539 wrbuf_puts(c->wrbuf, "</list>\n");
544 wrbuf_puts(c->wrbuf, "</termlist>\n");
545 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
546 http_send_response(c);
550 static void cmd_bytarget(struct http_channel *c)
552 struct http_response *rs = c->response;
553 struct http_request *rq = c->request;
554 struct http_session *s = locate_session(c);
555 struct hitsbytarget *ht;
556 const char *settings = http_argbyname(rq, "settings");
561 ht = hitsbytarget(s->psession, &count, c->nmem);
562 wrbuf_rewind(c->wrbuf);
563 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<bytarget><status>OK</status>");
565 for (i = 0; i < count; i++)
567 wrbuf_puts(c->wrbuf, "\n<target>");
569 wrbuf_puts(c->wrbuf, "<id>");
570 wrbuf_xmlputs(c->wrbuf, ht[i].id);
571 wrbuf_puts(c->wrbuf, "</id>\n");
573 if (ht[i].name && ht[i].name[0])
575 wrbuf_puts(c->wrbuf, "<name>");
576 wrbuf_xmlputs(c->wrbuf, ht[i].name);
577 wrbuf_puts(c->wrbuf, "</name>\n");
580 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
581 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
582 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
584 wrbuf_puts(c->wrbuf, "<state>");
585 wrbuf_xmlputs(c->wrbuf, ht[i].state);
586 wrbuf_puts(c->wrbuf, "</state>\n");
587 if (settings && *settings == '1')
589 wrbuf_puts(c->wrbuf, "<settings>\n");
590 wrbuf_puts(c->wrbuf, wrbuf_cstr(ht[i].settings_xml));
591 wrbuf_puts(c->wrbuf, "</settings>\n");
593 wrbuf_puts(c->wrbuf, "</target>");
594 wrbuf_destroy(ht[i].settings_xml);
597 wrbuf_puts(c->wrbuf, "</bytarget>");
598 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
599 http_send_response(c);
602 static void write_metadata(WRBUF w, struct conf_service *service,
603 struct record_metadata **ml, int full)
607 for (imeta = 0; imeta < service->num_metadata; imeta++)
609 struct conf_metadata *cmd = &service->metadata[imeta];
610 struct record_metadata *md;
611 if (!cmd->brief && !full)
613 for (md = ml[imeta]; md; md = md->next)
615 struct record_metadata_attr *attr = md->attributes;
616 wrbuf_printf(w, "\n<md-%s", cmd->name);
618 for (; attr; attr = attr->next)
620 wrbuf_printf(w, " %s=\"", attr->name);
621 wrbuf_xmlputs(w, attr->value);
627 case Metadata_type_generic:
628 wrbuf_xmlputs(w, md->data.text.disp);
630 case Metadata_type_year:
631 wrbuf_printf(w, "%d", md->data.number.min);
632 if (md->data.number.min != md->data.number.max)
633 wrbuf_printf(w, "-%d", md->data.number.max);
636 wrbuf_puts(w, "[can't represent]");
638 wrbuf_printf(w, "</md-%s>", cmd->name);
643 static void write_subrecord(struct record *r, WRBUF w,
644 struct conf_service *service, int show_details)
646 const char *name = session_setting_oneval(
647 client_get_database(r->client), PZ_NAME);
649 wrbuf_puts(w, "<location id=\"");
650 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
651 wrbuf_puts(w, "\" ");
653 wrbuf_puts(w, "name=\"");
654 wrbuf_xmlputs(w, *name ? name : "Unknown");
655 wrbuf_puts(w, "\">");
657 write_metadata(w, service, r->metadata, show_details);
658 wrbuf_puts(w, "</location>\n");
661 static void show_raw_record_error(void *data, const char *addinfo)
663 http_channel_observer_t obs = data;
664 struct http_channel *c = http_channel_observer_chan(obs);
665 struct http_response *rs = c->response;
667 http_remove_observer(obs);
669 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
672 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
674 http_channel_observer_t obs = data;
675 struct http_channel *c = http_channel_observer_chan(obs);
676 struct http_response *rs = c->response;
678 http_remove_observer(obs);
680 wrbuf_write(c->wrbuf, buf, sz);
681 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
682 http_send_response(c);
686 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
688 http_channel_observer_t obs = data;
689 struct http_channel *c = http_channel_observer_chan(obs);
690 struct http_response *rs = c->response;
692 http_remove_observer(obs);
694 wrbuf_write(c->wrbuf, buf, sz);
695 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
697 rs->content_type = "application/octet-stream";
698 http_send_response(c);
702 void show_raw_reset(void *data, struct http_channel *c, void *data2)
704 //struct client *client = data;
705 //client_show_raw_remove(client, data2);
708 static void cmd_record_ready(void *data);
710 static void cmd_record(struct http_channel *c)
712 struct http_response *rs = c->response;
713 struct http_request *rq = c->request;
714 struct http_session *s = locate_session(c);
715 struct record_cluster *rec, *prev_r, *next_r;
717 struct conf_service *service;
718 const char *idstr = http_argbyname(rq, "id");
719 const char *offsetstr = http_argbyname(rq, "offset");
720 const char *binarystr = http_argbyname(rq, "binary");
724 service = s->psession->service;
727 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
730 wrbuf_rewind(c->wrbuf);
731 if (!(rec = show_single_start(s->psession, idstr, &prev_r, &next_r)))
733 if (session_active_clients(s->psession) == 0)
735 error(rs, PAZPAR2_RECORD_MISSING, idstr);
737 else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
738 cmd_record_ready, c, c) != 0)
740 error(rs, PAZPAR2_RECORD_MISSING, idstr);
746 int offset = atoi(offsetstr);
747 const char *syntax = http_argbyname(rq, "syntax");
748 const char *esn = http_argbyname(rq, "esn");
750 struct record*r = rec->records;
753 if (binarystr && *binarystr != '0')
756 for (i = 0; i < offset && r; r = r->next, i++)
760 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
764 http_channel_observer_t obs =
765 http_add_observer(c, r->client, show_raw_reset);
766 int ret = client_show_raw_begin(r->client, r->position,
769 show_raw_record_error,
771 show_raw_record_ok_binary :
776 http_remove_observer(obs);
777 error(rs, PAZPAR2_NO_SESSION, 0);
783 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<record>\n");
784 wrbuf_puts(c->wrbuf, "<recid>");
785 wrbuf_xmlputs(c->wrbuf, rec->recid);
786 wrbuf_puts(c->wrbuf, "</recid>\n");
789 wrbuf_puts(c->wrbuf, "<prevrecid>");
790 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
791 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
795 wrbuf_puts(c->wrbuf, "<nextrecid>");
796 wrbuf_xmlputs(c->wrbuf, next_r->recid);
797 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
799 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
800 session_active_clients(s->psession));
801 write_metadata(c->wrbuf, service, rec->metadata, 1);
802 for (r = rec->records; r; r = r->next)
803 write_subrecord(r, c->wrbuf, service, 1);
804 wrbuf_puts(c->wrbuf, "</record>\n");
805 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
806 http_send_response(c);
808 show_single_stop(s->psession, rec);
811 static void cmd_record_ready(void *data)
813 struct http_channel *c = (struct http_channel *) data;
818 static void show_records(struct http_channel *c, int active)
820 struct http_request *rq = c->request;
821 struct http_response *rs = c->response;
822 struct http_session *s = locate_session(c);
823 struct record_cluster **rl;
824 struct reclist_sortparms *sp;
825 const char *start = http_argbyname(rq, "start");
826 const char *num = http_argbyname(rq, "num");
827 const char *sort = http_argbyname(rq, "sort");
837 // We haven't counted clients yet if we're called on a block release
839 active = session_active_clients(s->psession);
842 startn = atoi(start);
847 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
849 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
854 rl = show_range_start(s->psession, sp, startn, &numn, &total, &total_hits);
856 wrbuf_rewind(c->wrbuf);
857 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<show>\n<status>OK</status>\n");
858 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
859 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
860 wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
861 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
862 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
864 for (i = 0; i < numn; i++)
868 struct record_cluster *rec = rl[i];
869 struct conf_service *service = s->psession->service;
871 wrbuf_puts(c->wrbuf, "<hit>\n");
872 write_metadata(c->wrbuf, service, rec->metadata, 0);
873 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
874 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
876 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
877 if (strstr(sort, "relevance"))
878 wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n",
879 rec->relevance_score);
880 wrbuf_puts(c->wrbuf, "<recid>");
881 wrbuf_xmlputs(c->wrbuf, rec->recid);
882 wrbuf_puts(c->wrbuf, "</recid>\n");
883 wrbuf_puts(c->wrbuf, "</hit>\n");
886 show_range_stop(s->psession, rl);
888 wrbuf_puts(c->wrbuf, "</show>\n");
889 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
890 http_send_response(c);
893 static void show_records_ready(void *data)
895 struct http_channel *c = (struct http_channel *) data;
900 static void cmd_show(struct http_channel *c)
902 struct http_request *rq = c->request;
903 struct http_session *s = locate_session(c);
904 const char *block = http_argbyname(rq, "block");
910 status = session_active_clients(s->psession);
914 if (status && reclist_get_num_records(s->psession->reclist) == 0)
916 // if there is already a watch/block. we do not block this one
917 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
918 show_records_ready, c, c) != 0)
920 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
926 show_records(c, status);
929 static void cmd_ping(struct http_channel *c)
931 struct http_response *rs = c->response;
932 struct http_session *s = locate_session(c);
935 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<ping><status>OK</status></ping>";
936 http_send_response(c);
939 static int utf_8_valid(const char *str)
941 yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
944 /* check that query is UTF-8 encoded */
945 char *inbuf = (char *) str; /* we know iconv does not alter this */
946 size_t inbytesleft = strlen(inbuf);
948 size_t outbytesleft = strlen(inbuf) + 10;
949 char *out = xmalloc(outbytesleft);
951 size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
953 /* if OK, try flushing the rest */
954 if (r != (size_t) (-1))
955 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
958 if (r == (size_t) (-1))
964 static void cmd_search(struct http_channel *c)
966 struct http_request *rq = c->request;
967 struct http_response *rs = c->response;
968 struct http_session *s = locate_session(c);
969 const char *query = http_argbyname(rq, "query");
970 const char *filter = http_argbyname(rq, "filter");
971 const char *maxrecs = http_argbyname(rq, "maxrecs");
972 const char *startrecs = http_argbyname(rq, "startrecs");
973 enum pazpar2_error_code code;
974 const char *addinfo = 0;
980 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
983 if (!utf_8_valid(query))
985 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
988 code = search(s->psession, query, startrecs, maxrecs, filter, &addinfo);
991 error(rs, code, addinfo);
994 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<search><status>OK</status></search>";
995 http_send_response(c);
999 static void cmd_stat(struct http_channel *c)
1001 struct http_response *rs = c->response;
1002 struct http_session *s = locate_session(c);
1003 struct statistics stat;
1011 clients = session_active_clients(s->psession);
1012 statistics(s->psession, &stat);
1014 if (stat.num_clients > 0) {
1015 progress = (stat.num_clients - clients) / (float)stat.num_clients;
1018 wrbuf_rewind(c->wrbuf);
1019 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<stat>");
1020 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
1021 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
1022 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
1023 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
1024 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
1025 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
1026 wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
1027 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
1028 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
1029 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
1030 wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
1031 wrbuf_puts(c->wrbuf, "</stat>");
1032 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1033 http_send_response(c);
1036 static void cmd_info(struct http_channel *c)
1038 char yaz_version_str[20];
1039 struct http_response *rs = c->response;
1041 wrbuf_rewind(c->wrbuf);
1042 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<info>\n");
1043 wrbuf_puts(c->wrbuf, " <version>\n");
1044 wrbuf_puts(c->wrbuf, "<pazpar2");
1045 #ifdef PAZPAR2_VERSION_SHA1
1046 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
1048 wrbuf_puts(c->wrbuf, ">");
1049 wrbuf_xmlputs(c->wrbuf, VERSION);
1050 wrbuf_puts(c->wrbuf, "</pazpar2>");
1053 yaz_version(yaz_version_str, 0);
1054 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
1055 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
1056 wrbuf_puts(c->wrbuf, "\">");
1057 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
1058 wrbuf_puts(c->wrbuf, "</yaz>\n");
1060 wrbuf_puts(c->wrbuf, " </version>\n");
1062 wrbuf_puts(c->wrbuf, "</info>");
1063 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1064 http_send_response(c);
1069 void (*fun)(struct http_channel *c);
1071 { "init", cmd_init },
1072 { "settings", cmd_settings },
1073 { "stat", cmd_stat },
1074 { "bytarget", cmd_bytarget },
1075 { "show", cmd_show },
1076 { "search", cmd_search },
1077 { "termlist", cmd_termlist },
1078 { "exit", cmd_exit },
1079 { "ping", cmd_ping },
1080 { "record", cmd_record },
1081 { "info", cmd_info },
1085 void http_command(struct http_channel *c)
1087 const char *command = http_argbyname(c->request, "command");
1088 struct http_response *rs = http_create_response(c);
1093 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1094 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1098 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1101 for (i = 0; commands[i].name; i++)
1102 if (!strcmp(commands[i].name, command))
1104 (*commands[i].fun)(c);
1107 if (!commands[i].name)
1108 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1116 * c-file-style: "Stroustrup"
1117 * indent-tabs-mode: nil
1119 * vim: shiftwidth=4 tabstop=8 expandtab