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>
38 #include "parameters.h"
44 // Update this when the protocol changes
45 #define PAZPAR2_PROTOCOL_VERSION "1"
47 #define HTTP_COMMAND_RESPONSE_PREFIX "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
50 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
51 struct session *psession;
52 unsigned int session_id;
57 http_sessions_t http_sessions;
58 struct http_session *next;
61 struct http_sessions {
62 struct http_session *session_list;
66 http_sessions_t http_sessions_create(void)
68 http_sessions_t hs = xmalloc(sizeof(*hs));
71 pazpar2_mutex_create(&hs->mutex, "http_sessions");
75 void http_sessions_destroy(http_sessions_t hs)
79 struct http_session *s = hs->session_list;
82 struct http_session *s_next = s->next;
83 iochan_destroy(s->timeout_iochan);
84 destroy_session(s->psession);
85 nmem_destroy(s->nmem);
88 yaz_mutex_destroy(&hs->mutex);
93 void http_session_destroy(struct http_session *s);
95 static void session_timeout(IOCHAN i, int event)
97 struct http_session *s = iochan_getdata(i);
98 http_session_destroy(s);
101 struct http_session *http_session_create(struct conf_service *service,
102 http_sessions_t http_sessions,
105 NMEM nmem = nmem_create();
106 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
109 sprintf(tmp_str, "session#%u", sesid);
110 r->psession = new_session(nmem, service, tmp_str);
111 r->session_id = sesid;
114 r->destroy_counter = r->activity_counter = 0;
115 r->http_sessions = http_sessions;
117 yaz_mutex_enter(http_sessions->mutex);
118 r->next = http_sessions->session_list;
119 http_sessions->session_list = r;
120 yaz_mutex_leave(http_sessions->mutex);
122 r->timeout_iochan = iochan_create(-1, session_timeout, 0, "http_session_timeout");
123 iochan_setdata(r->timeout_iochan, r);
124 yaz_log(YLOG_LOG, "timeout=%d", service->session_timeout);
125 iochan_settimeout(r->timeout_iochan, service->session_timeout);
127 iochan_add(service->server->iochan_man, r->timeout_iochan);
131 void http_session_destroy(struct http_session *s)
133 int must_destroy = 1;
135 http_sessions_t http_sessions = s->http_sessions;
137 yaz_log(YLOG_LOG, "http_session_destroy %u", s->session_id);
138 yaz_mutex_enter(http_sessions->mutex);
140 /* only if http_session destroy was already called, we will allow it
142 if (s->destroy_counter != s->activity_counter)
145 /* only if there are no active Z39.50 clients we will allow it to be
147 if (session_active_clients(s->psession))
150 s->destroy_counter = s->activity_counter = 0;
153 struct http_session **p = 0;
154 for (p = &http_sessions->session_list; *p; p = &(*p)->next)
161 yaz_mutex_leave(http_sessions->mutex);
163 { /* destroying for real */
164 yaz_log(YLOG_LOG, "Destroying session %u", s->session_id);
165 iochan_destroy(s->timeout_iochan);
166 destroy_session(s->psession);
167 nmem_destroy(s->nmem);
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 sesid = make_sessionid();
364 s = http_session_create(service, c->http_sessions, sesid);
366 yaz_log(YLOG_DEBUG, "HTTP Session init");
367 if (!clear || *clear == '0')
368 session_init_databases(s->psession);
370 yaz_log(YLOG_LOG, "No databases preloaded");
372 if (process_settings(s->psession, c->request, c->response) < 0)
375 sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX
376 "<init><status>OK</status><session>%d", sesid);
377 if (c->server->server_id)
380 strcat(buf, c->server->server_id);
382 strcat(buf, "</session>"
383 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>");
384 rs->payload = nmem_strdup(c->nmem, buf);
385 http_send_response(c);
388 static void apply_local_setting(void *client_data,
391 struct session *se = (struct session *) client_data;
393 session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
394 nmem_strdup(se->session_nmem, set->name),
395 nmem_strdup(se->session_nmem, set->value));
398 static void cmd_settings(struct http_channel *c)
400 struct http_response *rs = c->response;
401 struct http_request *rq = c->request;
402 struct http_session *s = locate_session(c);
403 const char *content_type = http_lookup_header(rq->headers, "Content-Type");
408 if (rq->content_len && content_type &&
409 !yaz_strcmp_del("text/xml", content_type, "; "))
411 xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
415 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
418 root_n = xmlDocGetRootElement(doc);
420 settings_read_node_x(root_n, s->psession, apply_local_setting);
424 if (process_settings(s->psession, rq, rs) < 0)
426 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<settings><status>OK</status></settings>";
427 http_send_response(c);
430 // Compares two hitsbytarget nodes by hitcount
431 static int cmp_ht(const void *p1, const void *p2)
433 const struct hitsbytarget *h1 = p1;
434 const struct hitsbytarget *h2 = p2;
435 return h2->hits - h1->hits;
438 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
439 static void targets_termlist(WRBUF wrbuf, struct session *se, int num,
442 struct hitsbytarget *ht;
445 ht = hitsbytarget(se, &count, nmem);
446 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
447 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
450 // do only print terms which have display names
452 wrbuf_puts(wrbuf, "<term>\n");
454 wrbuf_puts(wrbuf, "<id>");
455 wrbuf_xmlputs(wrbuf, ht[i].id);
456 wrbuf_puts(wrbuf, "</id>\n");
458 wrbuf_puts(wrbuf, "<name>");
459 if (!ht[i].name || !ht[i].name[0])
460 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
462 wrbuf_xmlputs(wrbuf, ht[i].name);
463 wrbuf_puts(wrbuf, "</name>\n");
465 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
468 wrbuf_puts(wrbuf, "<state>");
469 wrbuf_xmlputs(wrbuf, ht[i].state);
470 wrbuf_puts(wrbuf, "</state>\n");
472 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
474 wrbuf_puts(wrbuf, "</term>\n");
478 static void cmd_termlist(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(c);
483 struct termlist_score **p;
486 const char *name = http_argbyname(rq, "name");
487 const char *nums = http_argbyname(rq, "num");
494 status = session_active_clients(s->psession);
498 if (strlen(name) > 255)
503 wrbuf_rewind(c->wrbuf);
505 wrbuf_puts(c->wrbuf, "<termlist>\n");
506 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
512 if (!(tp = strchr(name, ',')))
513 tp = name + strlen(name);
514 strncpy(tname, name, tp - name);
515 tname[tp - name] = '\0';
517 wrbuf_puts(c->wrbuf, "<list name=\"");
518 wrbuf_xmlputs(c->wrbuf, tname);
519 wrbuf_puts(c->wrbuf, "\">\n");
520 if (!strcmp(tname, "xtargets"))
521 targets_termlist(c->wrbuf, s->psession, num, c->nmem);
524 p = termlist(s->psession, tname, &len);
526 for (i = 0; i < len && i < num; i++){
527 // prevnt sending empty term elements
528 if (!p[i]->term || !p[i]->term[0])
531 wrbuf_puts(c->wrbuf, "<term>");
532 wrbuf_puts(c->wrbuf, "<name>");
533 wrbuf_xmlputs(c->wrbuf, p[i]->term);
534 wrbuf_puts(c->wrbuf, "</name>");
536 wrbuf_printf(c->wrbuf,
537 "<frequency>%d</frequency>",
539 wrbuf_puts(c->wrbuf, "</term>\n");
542 wrbuf_puts(c->wrbuf, "</list>\n");
547 wrbuf_puts(c->wrbuf, "</termlist>\n");
548 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
549 http_send_response(c);
553 static void cmd_bytarget(struct http_channel *c)
555 struct http_response *rs = c->response;
556 struct http_request *rq = c->request;
557 struct http_session *s = locate_session(c);
558 struct hitsbytarget *ht;
559 const char *settings = http_argbyname(rq, "settings");
564 ht = hitsbytarget(s->psession, &count, c->nmem);
565 wrbuf_rewind(c->wrbuf);
566 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<bytarget><status>OK</status>");
568 for (i = 0; i < count; i++)
570 wrbuf_puts(c->wrbuf, "\n<target>");
572 wrbuf_puts(c->wrbuf, "<id>");
573 wrbuf_xmlputs(c->wrbuf, ht[i].id);
574 wrbuf_puts(c->wrbuf, "</id>\n");
576 if (ht[i].name && ht[i].name[0])
578 wrbuf_puts(c->wrbuf, "<name>");
579 wrbuf_xmlputs(c->wrbuf, ht[i].name);
580 wrbuf_puts(c->wrbuf, "</name>\n");
583 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
584 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
585 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
587 wrbuf_puts(c->wrbuf, "<state>");
588 wrbuf_xmlputs(c->wrbuf, ht[i].state);
589 wrbuf_puts(c->wrbuf, "</state>\n");
590 if (settings && *settings == '1')
592 wrbuf_puts(c->wrbuf, "<settings>\n");
593 wrbuf_puts(c->wrbuf, wrbuf_cstr(ht[i].settings_xml));
594 wrbuf_puts(c->wrbuf, "</settings>\n");
596 wrbuf_puts(c->wrbuf, "</target>");
597 wrbuf_destroy(ht[i].settings_xml);
600 wrbuf_puts(c->wrbuf, "</bytarget>");
601 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
602 http_send_response(c);
605 static void write_metadata(WRBUF w, struct conf_service *service,
606 struct record_metadata **ml, int full)
610 for (imeta = 0; imeta < service->num_metadata; imeta++)
612 struct conf_metadata *cmd = &service->metadata[imeta];
613 struct record_metadata *md;
614 if (!cmd->brief && !full)
616 for (md = ml[imeta]; md; md = md->next)
618 struct record_metadata_attr *attr = md->attributes;
619 wrbuf_printf(w, "\n<md-%s", cmd->name);
621 for (; attr; attr = attr->next)
623 wrbuf_printf(w, " %s=\"", attr->name);
624 wrbuf_xmlputs(w, attr->value);
630 case Metadata_type_generic:
631 wrbuf_xmlputs(w, md->data.text.disp);
633 case Metadata_type_year:
634 wrbuf_printf(w, "%d", md->data.number.min);
635 if (md->data.number.min != md->data.number.max)
636 wrbuf_printf(w, "-%d", md->data.number.max);
639 wrbuf_puts(w, "[can't represent]");
641 wrbuf_printf(w, "</md-%s>", cmd->name);
646 static void write_subrecord(struct record *r, WRBUF w,
647 struct conf_service *service, int show_details)
649 const char *name = session_setting_oneval(
650 client_get_database(r->client), PZ_NAME);
652 wrbuf_puts(w, "<location id=\"");
653 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
654 wrbuf_puts(w, "\" ");
656 wrbuf_puts(w, "name=\"");
657 wrbuf_xmlputs(w, *name ? name : "Unknown");
658 wrbuf_puts(w, "\">");
660 write_metadata(w, service, r->metadata, show_details);
661 wrbuf_puts(w, "</location>\n");
664 static void show_raw_record_error(void *data, const char *addinfo)
666 http_channel_observer_t obs = data;
667 struct http_channel *c = http_channel_observer_chan(obs);
668 struct http_response *rs = c->response;
670 http_remove_observer(obs);
672 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
675 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
677 http_channel_observer_t obs = data;
678 struct http_channel *c = http_channel_observer_chan(obs);
679 struct http_response *rs = c->response;
681 http_remove_observer(obs);
683 wrbuf_write(c->wrbuf, buf, sz);
684 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
685 http_send_response(c);
689 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
691 http_channel_observer_t obs = data;
692 struct http_channel *c = http_channel_observer_chan(obs);
693 struct http_response *rs = c->response;
695 http_remove_observer(obs);
697 wrbuf_write(c->wrbuf, buf, sz);
698 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
700 rs->content_type = "application/octet-stream";
701 http_send_response(c);
705 void show_raw_reset(void *data, struct http_channel *c, void *data2)
707 //struct client *client = data;
708 //client_show_raw_remove(client, data2);
711 static void cmd_record_ready(void *data);
713 static void cmd_record(struct http_channel *c)
715 struct http_response *rs = c->response;
716 struct http_request *rq = c->request;
717 struct http_session *s = locate_session(c);
718 struct record_cluster *rec, *prev_r, *next_r;
720 struct conf_service *service;
721 const char *idstr = http_argbyname(rq, "id");
722 const char *offsetstr = http_argbyname(rq, "offset");
723 const char *binarystr = http_argbyname(rq, "binary");
727 service = s->psession->service;
730 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
733 wrbuf_rewind(c->wrbuf);
734 if (!(rec = show_single_start(s->psession, idstr, &prev_r, &next_r)))
736 if (session_active_clients(s->psession) == 0)
738 error(rs, PAZPAR2_RECORD_MISSING, idstr);
740 else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
741 cmd_record_ready, c, c) != 0)
743 error(rs, PAZPAR2_RECORD_MISSING, idstr);
749 int offset = atoi(offsetstr);
750 const char *syntax = http_argbyname(rq, "syntax");
751 const char *esn = http_argbyname(rq, "esn");
753 struct record*r = rec->records;
756 if (binarystr && *binarystr != '0')
759 for (i = 0; i < offset && r; r = r->next, i++)
763 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
767 http_channel_observer_t obs =
768 http_add_observer(c, r->client, show_raw_reset);
769 int ret = client_show_raw_begin(r->client, r->position,
772 show_raw_record_error,
774 show_raw_record_ok_binary :
779 http_remove_observer(obs);
780 error(rs, PAZPAR2_NO_SESSION, 0);
786 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<record>\n");
787 wrbuf_puts(c->wrbuf, "<recid>");
788 wrbuf_xmlputs(c->wrbuf, rec->recid);
789 wrbuf_puts(c->wrbuf, "</recid>\n");
792 wrbuf_puts(c->wrbuf, "<prevrecid>");
793 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
794 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
798 wrbuf_puts(c->wrbuf, "<nextrecid>");
799 wrbuf_xmlputs(c->wrbuf, next_r->recid);
800 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
802 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
803 session_active_clients(s->psession));
804 write_metadata(c->wrbuf, service, rec->metadata, 1);
805 for (r = rec->records; r; r = r->next)
806 write_subrecord(r, c->wrbuf, service, 1);
807 wrbuf_puts(c->wrbuf, "</record>\n");
808 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
809 http_send_response(c);
811 show_single_stop(s->psession, rec);
814 static void cmd_record_ready(void *data)
816 struct http_channel *c = (struct http_channel *) data;
821 static void show_records(struct http_channel *c, int active)
823 struct http_request *rq = c->request;
824 struct http_response *rs = c->response;
825 struct http_session *s = locate_session(c);
826 struct record_cluster **rl;
827 struct reclist_sortparms *sp;
828 const char *start = http_argbyname(rq, "start");
829 const char *num = http_argbyname(rq, "num");
830 const char *sort = http_argbyname(rq, "sort");
840 // We haven't counted clients yet if we're called on a block release
842 active = session_active_clients(s->psession);
845 startn = atoi(start);
850 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
852 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
857 rl = show_range_start(s->psession, sp, startn, &numn, &total, &total_hits);
859 wrbuf_rewind(c->wrbuf);
860 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<show>\n<status>OK</status>\n");
861 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
862 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
863 wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
864 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
865 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
867 for (i = 0; i < numn; i++)
871 struct record_cluster *rec = rl[i];
872 struct conf_service *service = s->psession->service;
874 wrbuf_puts(c->wrbuf, "<hit>\n");
875 write_metadata(c->wrbuf, service, rec->metadata, 0);
876 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
877 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
879 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
880 if (strstr(sort, "relevance"))
881 wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n",
882 rec->relevance_score);
883 wrbuf_puts(c->wrbuf, "<recid>");
884 wrbuf_xmlputs(c->wrbuf, rec->recid);
885 wrbuf_puts(c->wrbuf, "</recid>\n");
886 wrbuf_puts(c->wrbuf, "</hit>\n");
889 show_range_stop(s->psession, rl);
891 wrbuf_puts(c->wrbuf, "</show>\n");
892 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
893 http_send_response(c);
896 static void show_records_ready(void *data)
898 struct http_channel *c = (struct http_channel *) data;
903 static void cmd_show(struct http_channel *c)
905 struct http_request *rq = c->request;
906 struct http_session *s = locate_session(c);
907 const char *block = http_argbyname(rq, "block");
913 status = session_active_clients(s->psession);
917 if (status && reclist_get_num_records(s->psession->reclist) == 0)
919 // if there is already a watch/block. we do not block this one
920 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
921 show_records_ready, c, c) != 0)
923 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
929 show_records(c, status);
932 static void cmd_ping(struct http_channel *c)
934 struct http_response *rs = c->response;
935 struct http_session *s = locate_session(c);
938 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<ping><status>OK</status></ping>";
939 http_send_response(c);
942 static int utf_8_valid(const char *str)
944 yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
947 /* check that query is UTF-8 encoded */
948 char *inbuf = (char *) str; /* we know iconv does not alter this */
949 size_t inbytesleft = strlen(inbuf);
951 size_t outbytesleft = strlen(inbuf) + 10;
952 char *out = xmalloc(outbytesleft);
954 size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
956 /* if OK, try flushing the rest */
957 if (r != (size_t) (-1))
958 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
961 if (r == (size_t) (-1))
967 static void cmd_search(struct http_channel *c)
969 struct http_request *rq = c->request;
970 struct http_response *rs = c->response;
971 struct http_session *s = locate_session(c);
972 const char *query = http_argbyname(rq, "query");
973 const char *filter = http_argbyname(rq, "filter");
974 const char *maxrecs = http_argbyname(rq, "maxrecs");
975 const char *startrecs = http_argbyname(rq, "startrecs");
976 enum pazpar2_error_code code;
977 const char *addinfo = 0;
983 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
986 if (!utf_8_valid(query))
988 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
991 code = search(s->psession, query, startrecs, maxrecs, filter, &addinfo);
994 error(rs, code, addinfo);
997 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<search><status>OK</status></search>";
998 http_send_response(c);
1002 static void cmd_stat(struct http_channel *c)
1004 struct http_response *rs = c->response;
1005 struct http_session *s = locate_session(c);
1006 struct statistics stat;
1014 clients = session_active_clients(s->psession);
1015 statistics(s->psession, &stat);
1017 if (stat.num_clients > 0) {
1018 progress = (stat.num_clients - clients) / (float)stat.num_clients;
1021 wrbuf_rewind(c->wrbuf);
1022 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<stat>");
1023 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
1024 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
1025 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
1026 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
1027 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
1028 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
1029 wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
1030 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
1031 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
1032 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
1033 wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
1034 wrbuf_puts(c->wrbuf, "</stat>");
1035 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1036 http_send_response(c);
1039 static void cmd_info(struct http_channel *c)
1041 char yaz_version_str[20];
1042 struct http_response *rs = c->response;
1044 wrbuf_rewind(c->wrbuf);
1045 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<info>\n");
1046 wrbuf_puts(c->wrbuf, " <version>\n");
1047 wrbuf_puts(c->wrbuf, "<pazpar2");
1048 #ifdef PAZPAR2_VERSION_SHA1
1049 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
1051 wrbuf_puts(c->wrbuf, ">");
1052 wrbuf_xmlputs(c->wrbuf, VERSION);
1053 wrbuf_puts(c->wrbuf, "</pazpar2>");
1056 yaz_version(yaz_version_str, 0);
1057 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
1058 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
1059 wrbuf_puts(c->wrbuf, "\">");
1060 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
1061 wrbuf_puts(c->wrbuf, "</yaz>\n");
1063 wrbuf_puts(c->wrbuf, " </version>\n");
1065 wrbuf_puts(c->wrbuf, "</info>");
1066 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1067 http_send_response(c);
1072 void (*fun)(struct http_channel *c);
1074 { "init", cmd_init },
1075 { "settings", cmd_settings },
1076 { "stat", cmd_stat },
1077 { "bytarget", cmd_bytarget },
1078 { "show", cmd_show },
1079 { "search", cmd_search },
1080 { "termlist", cmd_termlist },
1081 { "exit", cmd_exit },
1082 { "ping", cmd_ping },
1083 { "record", cmd_record },
1084 { "info", cmd_info },
1088 void http_command(struct http_channel *c)
1090 const char *command = http_argbyname(c->request, "command");
1091 struct http_response *rs = http_create_response(c);
1096 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1097 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1101 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1104 for (i = 0; commands[i].name; i++)
1105 if (!strcmp(commands[i].name, command))
1107 (*commands[i].fun)(c);
1110 if (!commands[i].name)
1111 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1119 * c-file-style: "Stroustrup"
1120 * indent-tabs-mode: nil
1122 * vim: shiftwidth=4 tabstop=8 expandtab