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);
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);
166 yaz_log(YLOG_LOG, "Active clients on session %u. Waiting for new timeout.", s->session_id);
171 static const char *get_msg(enum pazpar2_error_code code)
173 struct pazpar2_error_msg {
174 enum pazpar2_error_code code;
177 static const struct pazpar2_error_msg ar[] = {
178 { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
179 { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
180 { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
181 { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
182 { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
183 { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
184 { PAZPAR2_RECORD_MISSING, "Record missing"},
185 { PAZPAR2_NO_TARGETS, "No targets"},
186 { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
187 { PAZPAR2_RECORD_FAIL, "Record command failed"},
188 { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"},
189 { PAZPAR2_NO_SERVICE, "No service"},
190 { PAZPAR2_LAST_ERROR, "Last error"},
196 if (code == ar[i].code)
203 static void error(struct http_response *rs,
204 enum pazpar2_error_code code,
207 struct http_channel *c = rs->channel;
208 WRBUF text = wrbuf_alloc();
209 const char *http_status = "417";
210 const char *msg = get_msg(code);
212 rs->msg = nmem_strdup(c->nmem, msg);
213 strcpy(rs->code, http_status);
215 wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code,
218 wrbuf_xmlputs(text, addinfo);
219 wrbuf_puts(text, "</error>");
221 yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
222 msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
223 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
225 http_send_response(c);
228 unsigned int make_sessionid(void)
230 static int seq = 0; /* thread pr */
234 if (global_parameters.debug_mode)
243 if (gettimeofday(&t, 0) < 0)
245 yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
248 /* at most 256 sessions per second ..
249 (long long would be more appropriate)*/
251 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
257 static struct http_session *locate_session(struct http_channel *c)
259 struct http_response *rs = c->response;
260 struct http_request *rq = c->request;
261 struct http_session *p;
262 const char *session = http_argbyname(rq, "session");
263 http_sessions_t http_sessions = c->http_sessions;
268 error(rs, PAZPAR2_MISSING_PARAMETER, "session");
272 yaz_mutex_enter(http_sessions->mutex);
273 for (p = http_sessions->session_list; p; p = p->next)
274 if (id == p->session_id)
277 p->activity_counter++;
278 yaz_mutex_leave(http_sessions->mutex);
280 iochan_activity(p->timeout_iochan);
282 error(rs, PAZPAR2_NO_SESSION, session);
286 // Decode settings parameters and apply to session
287 // Syntax: setting[target]=value
288 static int process_settings(struct session *se, struct http_request *rq,
289 struct http_response *rs)
291 struct http_argument *a;
293 for (a = rq->arguments; a; a = a->next)
294 if (strchr(a->name, '['))
301 // Nmem_strsplit *rules*!!!
302 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
305 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
310 session_apply_setting(se, dbname, setting,
311 nmem_strdup(se->session_nmem, a->value));
316 static void cmd_exit(struct http_channel *c)
318 yaz_log(YLOG_WARN, "exit");
319 http_close_server(c->server);
322 static void cmd_init(struct http_channel *c)
325 struct http_request *r = c->request;
326 const char *clear = http_argbyname(r, "clear");
327 const char *content_type = http_lookup_header(r->headers, "Content-Type");
329 struct http_session *s;
330 struct http_response *rs = c->response;
331 struct conf_service *service = 0; /* no service (yet) */
333 if (r->content_len && content_type &&
334 !yaz_strcmp_del("text/xml", content_type, "; "))
336 xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
340 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
343 root_n = xmlDocGetRootElement(doc);
344 service = service_create(c->server, root_n);
348 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
355 const char *service_name = http_argbyname(c->request, "service");
356 service = locate_service(c->server, service_name);
359 error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
363 s = http_session_create(service, c->http_sessions);
365 yaz_log(YLOG_DEBUG, "HTTP Session init");
366 if (!clear || *clear == '0')
367 session_init_databases(s->psession);
369 yaz_log(YLOG_LOG, "No databases preloaded");
371 sesid = make_sessionid();
372 s->session_id = sesid;
373 if (process_settings(s->psession, c->request, c->response) < 0)
376 sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX
377 "<init><status>OK</status><session>%u", sesid);
378 if (c->server->server_id)
381 strcat(buf, c->server->server_id);
383 strcat(buf, "</session>"
384 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>");
385 rs->payload = nmem_strdup(c->nmem, buf);
386 http_send_response(c);
389 static void apply_local_setting(void *client_data,
392 struct session *se = (struct session *) client_data;
394 session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
395 nmem_strdup(se->session_nmem, set->name),
396 nmem_strdup(se->session_nmem, set->value));
399 static void cmd_settings(struct http_channel *c)
401 struct http_response *rs = c->response;
402 struct http_request *rq = c->request;
403 struct http_session *s = locate_session(c);
404 const char *content_type = http_lookup_header(rq->headers, "Content-Type");
409 if (rq->content_len && content_type &&
410 !yaz_strcmp_del("text/xml", content_type, "; "))
412 xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
416 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
419 root_n = xmlDocGetRootElement(doc);
421 settings_read_node_x(root_n, s->psession, apply_local_setting);
425 if (process_settings(s->psession, rq, rs) < 0)
427 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<settings><status>OK</status></settings>";
428 http_send_response(c);
431 // Compares two hitsbytarget nodes by hitcount
432 static int cmp_ht(const void *p1, const void *p2)
434 const struct hitsbytarget *h1 = p1;
435 const struct hitsbytarget *h2 = p2;
436 return h2->hits - h1->hits;
439 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
440 static void targets_termlist(WRBUF wrbuf, struct session *se, int num,
443 struct hitsbytarget *ht;
446 ht = hitsbytarget(se, &count, nmem);
447 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
448 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
451 // do only print terms which have display names
453 wrbuf_puts(wrbuf, "<term>\n");
455 wrbuf_puts(wrbuf, "<id>");
456 wrbuf_xmlputs(wrbuf, ht[i].id);
457 wrbuf_puts(wrbuf, "</id>\n");
459 wrbuf_puts(wrbuf, "<name>");
460 if (!ht[i].name || !ht[i].name[0])
461 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
463 wrbuf_xmlputs(wrbuf, ht[i].name);
464 wrbuf_puts(wrbuf, "</name>\n");
466 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
469 wrbuf_puts(wrbuf, "<state>");
470 wrbuf_xmlputs(wrbuf, ht[i].state);
471 wrbuf_puts(wrbuf, "</state>\n");
473 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
475 wrbuf_puts(wrbuf, "</term>\n");
479 static void cmd_termlist(struct http_channel *c)
481 struct http_response *rs = c->response;
482 struct http_request *rq = c->request;
483 struct http_session *s = locate_session(c);
484 struct termlist_score **p;
487 const char *name = http_argbyname(rq, "name");
488 const char *nums = http_argbyname(rq, "num");
495 status = session_active_clients(s->psession);
499 if (strlen(name) > 255)
504 wrbuf_rewind(c->wrbuf);
506 wrbuf_puts(c->wrbuf, "<termlist>\n");
507 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
513 if (!(tp = strchr(name, ',')))
514 tp = name + strlen(name);
515 strncpy(tname, name, tp - name);
516 tname[tp - name] = '\0';
518 wrbuf_puts(c->wrbuf, "<list name=\"");
519 wrbuf_xmlputs(c->wrbuf, tname);
520 wrbuf_puts(c->wrbuf, "\">\n");
521 if (!strcmp(tname, "xtargets"))
522 targets_termlist(c->wrbuf, s->psession, num, c->nmem);
525 p = termlist(s->psession, tname, &len);
527 for (i = 0; i < len && i < num; i++){
528 // prevnt sending empty term elements
529 if (!p[i]->term || !p[i]->term[0])
532 wrbuf_puts(c->wrbuf, "<term>");
533 wrbuf_puts(c->wrbuf, "<name>");
534 wrbuf_xmlputs(c->wrbuf, p[i]->term);
535 wrbuf_puts(c->wrbuf, "</name>");
537 wrbuf_printf(c->wrbuf,
538 "<frequency>%d</frequency>",
540 wrbuf_puts(c->wrbuf, "</term>\n");
543 wrbuf_puts(c->wrbuf, "</list>\n");
548 wrbuf_puts(c->wrbuf, "</termlist>\n");
549 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
550 http_send_response(c);
554 static void cmd_bytarget(struct http_channel *c)
556 struct http_response *rs = c->response;
557 struct http_request *rq = c->request;
558 struct http_session *s = locate_session(c);
559 struct hitsbytarget *ht;
560 const char *settings = http_argbyname(rq, "settings");
565 ht = hitsbytarget(s->psession, &count, c->nmem);
566 wrbuf_rewind(c->wrbuf);
567 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<bytarget><status>OK</status>");
569 for (i = 0; i < count; i++)
571 wrbuf_puts(c->wrbuf, "\n<target>");
573 wrbuf_puts(c->wrbuf, "<id>");
574 wrbuf_xmlputs(c->wrbuf, ht[i].id);
575 wrbuf_puts(c->wrbuf, "</id>\n");
577 if (ht[i].name && ht[i].name[0])
579 wrbuf_puts(c->wrbuf, "<name>");
580 wrbuf_xmlputs(c->wrbuf, ht[i].name);
581 wrbuf_puts(c->wrbuf, "</name>\n");
584 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
585 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
586 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
588 wrbuf_puts(c->wrbuf, "<state>");
589 wrbuf_xmlputs(c->wrbuf, ht[i].state);
590 wrbuf_puts(c->wrbuf, "</state>\n");
591 if (settings && *settings == '1')
593 wrbuf_puts(c->wrbuf, "<settings>\n");
594 wrbuf_puts(c->wrbuf, wrbuf_cstr(ht[i].settings_xml));
595 wrbuf_puts(c->wrbuf, "</settings>\n");
597 wrbuf_puts(c->wrbuf, "</target>");
598 wrbuf_destroy(ht[i].settings_xml);
601 wrbuf_puts(c->wrbuf, "</bytarget>");
602 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
603 http_send_response(c);
606 static void write_metadata(WRBUF w, struct conf_service *service,
607 struct record_metadata **ml, int full)
611 for (imeta = 0; imeta < service->num_metadata; imeta++)
613 struct conf_metadata *cmd = &service->metadata[imeta];
614 struct record_metadata *md;
615 if (!cmd->brief && !full)
617 for (md = ml[imeta]; md; md = md->next)
619 struct record_metadata_attr *attr = md->attributes;
620 wrbuf_printf(w, "\n<md-%s", cmd->name);
622 for (; attr; attr = attr->next)
624 wrbuf_printf(w, " %s=\"", attr->name);
625 wrbuf_xmlputs(w, attr->value);
631 case Metadata_type_generic:
632 wrbuf_xmlputs(w, md->data.text.disp);
634 case Metadata_type_year:
635 wrbuf_printf(w, "%d", md->data.number.min);
636 if (md->data.number.min != md->data.number.max)
637 wrbuf_printf(w, "-%d", md->data.number.max);
640 wrbuf_puts(w, "[can't represent]");
642 wrbuf_printf(w, "</md-%s>", cmd->name);
647 static void write_subrecord(struct record *r, WRBUF w,
648 struct conf_service *service, int show_details)
650 const char *name = session_setting_oneval(
651 client_get_database(r->client), PZ_NAME);
653 wrbuf_puts(w, "<location id=\"");
654 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
655 wrbuf_puts(w, "\" ");
657 wrbuf_puts(w, "name=\"");
658 wrbuf_xmlputs(w, *name ? name : "Unknown");
659 wrbuf_puts(w, "\">");
661 write_metadata(w, service, r->metadata, show_details);
662 wrbuf_puts(w, "</location>\n");
665 static void show_raw_record_error(void *data, const char *addinfo)
667 http_channel_observer_t obs = data;
668 struct http_channel *c = http_channel_observer_chan(obs);
669 struct http_response *rs = c->response;
671 http_remove_observer(obs);
673 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
676 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
678 http_channel_observer_t obs = data;
679 struct http_channel *c = http_channel_observer_chan(obs);
680 struct http_response *rs = c->response;
682 http_remove_observer(obs);
684 wrbuf_write(c->wrbuf, buf, sz);
685 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
686 http_send_response(c);
690 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
692 http_channel_observer_t obs = data;
693 struct http_channel *c = http_channel_observer_chan(obs);
694 struct http_response *rs = c->response;
696 http_remove_observer(obs);
698 wrbuf_write(c->wrbuf, buf, sz);
699 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
701 rs->content_type = "application/octet-stream";
702 http_send_response(c);
706 void show_raw_reset(void *data, struct http_channel *c, void *data2)
708 //struct client *client = data;
709 //client_show_raw_remove(client, data2);
712 static void cmd_record_ready(void *data);
714 static void cmd_record(struct http_channel *c)
716 struct http_response *rs = c->response;
717 struct http_request *rq = c->request;
718 struct http_session *s = locate_session(c);
719 struct record_cluster *rec, *prev_r, *next_r;
721 struct conf_service *service;
722 const char *idstr = http_argbyname(rq, "id");
723 const char *offsetstr = http_argbyname(rq, "offset");
724 const char *binarystr = http_argbyname(rq, "binary");
728 service = s->psession->service;
731 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
734 wrbuf_rewind(c->wrbuf);
735 if (!(rec = show_single_start(s->psession, idstr, &prev_r, &next_r)))
737 if (session_active_clients(s->psession) == 0)
739 error(rs, PAZPAR2_RECORD_MISSING, idstr);
741 else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
742 cmd_record_ready, c, c) != 0)
744 error(rs, PAZPAR2_RECORD_MISSING, idstr);
750 int offset = atoi(offsetstr);
751 const char *syntax = http_argbyname(rq, "syntax");
752 const char *esn = http_argbyname(rq, "esn");
754 struct record*r = rec->records;
757 if (binarystr && *binarystr != '0')
760 for (i = 0; i < offset && r; r = r->next, i++)
764 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
768 http_channel_observer_t obs =
769 http_add_observer(c, r->client, show_raw_reset);
770 int ret = client_show_raw_begin(r->client, r->position,
773 show_raw_record_error,
775 show_raw_record_ok_binary :
780 http_remove_observer(obs);
781 error(rs, PAZPAR2_NO_SESSION, 0);
787 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<record>\n");
788 wrbuf_puts(c->wrbuf, "<recid>");
789 wrbuf_xmlputs(c->wrbuf, rec->recid);
790 wrbuf_puts(c->wrbuf, "</recid>\n");
793 wrbuf_puts(c->wrbuf, "<prevrecid>");
794 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
795 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
799 wrbuf_puts(c->wrbuf, "<nextrecid>");
800 wrbuf_xmlputs(c->wrbuf, next_r->recid);
801 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
803 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
804 session_active_clients(s->psession));
805 write_metadata(c->wrbuf, service, rec->metadata, 1);
806 for (r = rec->records; r; r = r->next)
807 write_subrecord(r, c->wrbuf, service, 1);
808 wrbuf_puts(c->wrbuf, "</record>\n");
809 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
810 http_send_response(c);
812 show_single_stop(s->psession, rec);
815 static void cmd_record_ready(void *data)
817 struct http_channel *c = (struct http_channel *) data;
822 static void show_records(struct http_channel *c, int active)
824 struct http_request *rq = c->request;
825 struct http_response *rs = c->response;
826 struct http_session *s = locate_session(c);
827 struct record_cluster **rl;
828 struct reclist_sortparms *sp;
829 const char *start = http_argbyname(rq, "start");
830 const char *num = http_argbyname(rq, "num");
831 const char *sort = http_argbyname(rq, "sort");
841 // We haven't counted clients yet if we're called on a block release
843 active = session_active_clients(s->psession);
846 startn = atoi(start);
851 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
853 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
858 rl = show_range_start(s->psession, sp, startn, &numn, &total, &total_hits);
860 wrbuf_rewind(c->wrbuf);
861 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<show>\n<status>OK</status>\n");
862 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
863 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
864 wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
865 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
866 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
868 for (i = 0; i < numn; i++)
872 struct record_cluster *rec = rl[i];
873 struct conf_service *service = s->psession->service;
875 wrbuf_puts(c->wrbuf, "<hit>\n");
876 write_metadata(c->wrbuf, service, rec->metadata, 0);
877 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
878 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
880 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
881 if (strstr(sort, "relevance"))
882 wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n",
883 rec->relevance_score);
884 wrbuf_puts(c->wrbuf, "<recid>");
885 wrbuf_xmlputs(c->wrbuf, rec->recid);
886 wrbuf_puts(c->wrbuf, "</recid>\n");
887 wrbuf_puts(c->wrbuf, "</hit>\n");
890 show_range_stop(s->psession, rl);
892 wrbuf_puts(c->wrbuf, "</show>\n");
893 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
894 http_send_response(c);
897 static void show_records_ready(void *data)
899 struct http_channel *c = (struct http_channel *) data;
904 static void cmd_show(struct http_channel *c)
906 struct http_request *rq = c->request;
907 struct http_session *s = locate_session(c);
908 const char *block = http_argbyname(rq, "block");
914 status = session_active_clients(s->psession);
918 if (status && reclist_get_num_records(s->psession->reclist) == 0)
920 // if there is already a watch/block. we do not block this one
921 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
922 show_records_ready, c, c) != 0)
924 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
930 show_records(c, status);
933 static void cmd_ping(struct http_channel *c)
935 struct http_response *rs = c->response;
936 struct http_session *s = locate_session(c);
939 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<ping><status>OK</status></ping>";
940 http_send_response(c);
943 static int utf_8_valid(const char *str)
945 yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
948 /* check that query is UTF-8 encoded */
949 char *inbuf = (char *) str; /* we know iconv does not alter this */
950 size_t inbytesleft = strlen(inbuf);
952 size_t outbytesleft = strlen(inbuf) + 10;
953 char *out = xmalloc(outbytesleft);
955 size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
957 /* if OK, try flushing the rest */
958 if (r != (size_t) (-1))
959 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
962 if (r == (size_t) (-1))
968 static void cmd_search(struct http_channel *c)
970 struct http_request *rq = c->request;
971 struct http_response *rs = c->response;
972 struct http_session *s = locate_session(c);
973 const char *query = http_argbyname(rq, "query");
974 const char *filter = http_argbyname(rq, "filter");
975 const char *maxrecs = http_argbyname(rq, "maxrecs");
976 const char *startrecs = http_argbyname(rq, "startrecs");
977 enum pazpar2_error_code code;
978 const char *addinfo = 0;
984 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
987 if (!utf_8_valid(query))
989 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
992 code = search(s->psession, query, startrecs, maxrecs, filter, &addinfo);
995 error(rs, code, addinfo);
998 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<search><status>OK</status></search>";
999 http_send_response(c);
1003 static void cmd_stat(struct http_channel *c)
1005 struct http_response *rs = c->response;
1006 struct http_session *s = locate_session(c);
1007 struct statistics stat;
1015 clients = session_active_clients(s->psession);
1016 statistics(s->psession, &stat);
1018 if (stat.num_clients > 0) {
1019 progress = (stat.num_clients - clients) / (float)stat.num_clients;
1022 wrbuf_rewind(c->wrbuf);
1023 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<stat>");
1024 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
1025 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
1026 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
1027 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
1028 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
1029 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
1030 wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
1031 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
1032 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
1033 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
1034 wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
1035 wrbuf_puts(c->wrbuf, "</stat>");
1036 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1037 http_send_response(c);
1040 static void cmd_info(struct http_channel *c)
1042 char yaz_version_str[20];
1043 struct http_response *rs = c->response;
1045 wrbuf_rewind(c->wrbuf);
1046 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<info>\n");
1047 wrbuf_puts(c->wrbuf, " <version>\n");
1048 wrbuf_puts(c->wrbuf, "<pazpar2");
1049 #ifdef PAZPAR2_VERSION_SHA1
1050 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
1052 wrbuf_puts(c->wrbuf, ">");
1053 wrbuf_xmlputs(c->wrbuf, VERSION);
1054 wrbuf_puts(c->wrbuf, "</pazpar2>");
1057 yaz_version(yaz_version_str, 0);
1058 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
1059 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
1060 wrbuf_puts(c->wrbuf, "\">");
1061 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
1062 wrbuf_puts(c->wrbuf, "</yaz>\n");
1064 wrbuf_puts(c->wrbuf, " </version>\n");
1066 wrbuf_puts(c->wrbuf, "</info>");
1067 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1068 http_send_response(c);
1073 void (*fun)(struct http_channel *c);
1075 { "init", cmd_init },
1076 { "settings", cmd_settings },
1077 { "stat", cmd_stat },
1078 { "bytarget", cmd_bytarget },
1079 { "show", cmd_show },
1080 { "search", cmd_search },
1081 { "termlist", cmd_termlist },
1082 { "exit", cmd_exit },
1083 { "ping", cmd_ping },
1084 { "record", cmd_record },
1085 { "info", cmd_info },
1089 void http_command(struct http_channel *c)
1091 const char *command = http_argbyname(c->request, "command");
1092 struct http_response *rs = http_create_response(c);
1097 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1098 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1102 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1105 for (i = 0; commands[i].name; i++)
1106 if (!strcmp(commands[i].name, command))
1108 (*commands[i].fun)(c);
1111 if (!commands[i].name)
1112 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1120 * c-file-style: "Stroustrup"
1121 * indent-tabs-mode: nil
1123 * vim: shiftwidth=4 tabstop=8 expandtab