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 s->destroy_counter = s->activity_counter = 0;
148 struct http_session **p = 0;
149 for (p = &http_sessions->session_list; *p; p = &(*p)->next)
156 yaz_mutex_leave(http_sessions->mutex);
158 { /* destroying for real */
159 yaz_log(YLOG_LOG, "Destroying session %u", s->session_id);
160 iochan_destroy(s->timeout_iochan);
161 destroy_session(s->psession);
162 nmem_destroy(s->nmem);
165 yaz_log(YLOG_LOG, "Active clients on session %u. Waiting for new timeout.", s->session_id);
170 static const char *get_msg(enum pazpar2_error_code code)
172 struct pazpar2_error_msg {
173 enum pazpar2_error_code code;
176 static const struct pazpar2_error_msg ar[] = {
177 { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
178 { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
179 { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
180 { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
181 { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
182 { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
183 { PAZPAR2_RECORD_MISSING, "Record missing"},
184 { PAZPAR2_NO_TARGETS, "No targets"},
185 { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
186 { PAZPAR2_RECORD_FAIL, "Record command failed"},
187 { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"},
188 { PAZPAR2_NO_SERVICE, "No service"},
189 { PAZPAR2_LAST_ERROR, "Last error"},
195 if (code == ar[i].code)
202 static void error(struct http_response *rs,
203 enum pazpar2_error_code code,
206 struct http_channel *c = rs->channel;
207 WRBUF text = wrbuf_alloc();
208 const char *http_status = "417";
209 const char *msg = get_msg(code);
211 rs->msg = nmem_strdup(c->nmem, msg);
212 strcpy(rs->code, http_status);
214 wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code,
217 wrbuf_xmlputs(text, addinfo);
218 wrbuf_puts(text, "</error>");
220 yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
221 msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
222 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
224 http_send_response(c);
227 unsigned int make_sessionid(void)
229 static int seq = 0; /* thread pr */
233 if (global_parameters.debug_mode)
242 if (gettimeofday(&t, 0) < 0)
244 yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
247 /* at most 256 sessions per second ..
248 (long long would be more appropriate)*/
250 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
256 static struct http_session *locate_session(struct http_channel *c)
258 struct http_response *rs = c->response;
259 struct http_request *rq = c->request;
260 struct http_session *p;
261 const char *session = http_argbyname(rq, "session");
262 http_sessions_t http_sessions = c->http_sessions;
267 error(rs, PAZPAR2_MISSING_PARAMETER, "session");
271 yaz_mutex_enter(http_sessions->mutex);
272 for (p = http_sessions->session_list; p; p = p->next)
273 if (id == p->session_id)
276 p->activity_counter++;
277 yaz_mutex_leave(http_sessions->mutex);
279 iochan_activity(p->timeout_iochan);
281 error(rs, PAZPAR2_NO_SESSION, session);
285 // Decode settings parameters and apply to session
286 // Syntax: setting[target]=value
287 static int process_settings(struct session *se, struct http_request *rq,
288 struct http_response *rs)
290 struct http_argument *a;
292 for (a = rq->arguments; a; a = a->next)
293 if (strchr(a->name, '['))
300 // Nmem_strsplit *rules*!!!
301 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
304 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
309 session_apply_setting(se, dbname, setting,
310 nmem_strdup(se->session_nmem, a->value));
315 static void cmd_exit(struct http_channel *c)
317 yaz_log(YLOG_WARN, "exit");
318 http_close_server(c->server);
321 static void cmd_init(struct http_channel *c)
324 struct http_request *r = c->request;
325 const char *clear = http_argbyname(r, "clear");
326 const char *content_type = http_lookup_header(r->headers, "Content-Type");
328 struct http_session *s;
329 struct http_response *rs = c->response;
330 struct conf_service *service = 0; /* no service (yet) */
332 if (r->content_len && content_type &&
333 !yaz_strcmp_del("text/xml", content_type, "; "))
335 xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
339 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
342 root_n = xmlDocGetRootElement(doc);
343 service = service_create(c->server, root_n);
347 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
354 const char *service_name = http_argbyname(c->request, "service");
355 service = locate_service(c->server, service_name);
358 error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
362 sesid = make_sessionid();
363 s = http_session_create(service, c->http_sessions, sesid);
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 if (process_settings(s->psession, c->request, c->response) < 0)
374 sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX
375 "<init><status>OK</status><session>%d", sesid);
376 if (c->server->server_id)
379 strcat(buf, c->server->server_id);
381 strcat(buf, "</session>"
382 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>");
383 rs->payload = nmem_strdup(c->nmem, buf);
384 http_send_response(c);
387 static void apply_local_setting(void *client_data,
390 struct session *se = (struct session *) client_data;
392 session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
393 nmem_strdup(se->session_nmem, set->name),
394 nmem_strdup(se->session_nmem, set->value));
397 static void cmd_settings(struct http_channel *c)
399 struct http_response *rs = c->response;
400 struct http_request *rq = c->request;
401 struct http_session *s = locate_session(c);
402 const char *content_type = http_lookup_header(rq->headers, "Content-Type");
407 if (rq->content_len && content_type &&
408 !yaz_strcmp_del("text/xml", content_type, "; "))
410 xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
414 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
417 root_n = xmlDocGetRootElement(doc);
419 settings_read_node_x(root_n, s->psession, apply_local_setting);
423 if (process_settings(s->psession, rq, rs) < 0)
425 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<settings><status>OK</status></settings>";
426 http_send_response(c);
429 // Compares two hitsbytarget nodes by hitcount
430 static int cmp_ht(const void *p1, const void *p2)
432 const struct hitsbytarget *h1 = p1;
433 const struct hitsbytarget *h2 = p2;
434 return h2->hits - h1->hits;
437 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
438 static void targets_termlist(WRBUF wrbuf, struct session *se, int num,
441 struct hitsbytarget *ht;
444 ht = hitsbytarget(se, &count, nmem);
445 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
446 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
449 // do only print terms which have display names
451 wrbuf_puts(wrbuf, "<term>\n");
453 wrbuf_puts(wrbuf, "<id>");
454 wrbuf_xmlputs(wrbuf, ht[i].id);
455 wrbuf_puts(wrbuf, "</id>\n");
457 wrbuf_puts(wrbuf, "<name>");
458 if (!ht[i].name || !ht[i].name[0])
459 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
461 wrbuf_xmlputs(wrbuf, ht[i].name);
462 wrbuf_puts(wrbuf, "</name>\n");
464 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
467 wrbuf_puts(wrbuf, "<state>");
468 wrbuf_xmlputs(wrbuf, ht[i].state);
469 wrbuf_puts(wrbuf, "</state>\n");
471 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
473 wrbuf_puts(wrbuf, "</term>\n");
477 static void cmd_termlist(struct http_channel *c)
479 struct http_response *rs = c->response;
480 struct http_request *rq = c->request;
481 struct http_session *s = locate_session(c);
482 struct termlist_score **p;
485 const char *name = http_argbyname(rq, "name");
486 const char *nums = http_argbyname(rq, "num");
493 status = session_active_clients(s->psession);
497 if (strlen(name) > 255)
502 wrbuf_rewind(c->wrbuf);
504 wrbuf_puts(c->wrbuf, "<termlist>\n");
505 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
511 if (!(tp = strchr(name, ',')))
512 tp = name + strlen(name);
513 strncpy(tname, name, tp - name);
514 tname[tp - name] = '\0';
516 wrbuf_puts(c->wrbuf, "<list name=\"");
517 wrbuf_xmlputs(c->wrbuf, tname);
518 wrbuf_puts(c->wrbuf, "\">\n");
519 if (!strcmp(tname, "xtargets"))
520 targets_termlist(c->wrbuf, s->psession, num, c->nmem);
523 p = termlist(s->psession, tname, &len);
525 for (i = 0; i < len && i < num; i++){
526 // prevnt sending empty term elements
527 if (!p[i]->term || !p[i]->term[0])
530 wrbuf_puts(c->wrbuf, "<term>");
531 wrbuf_puts(c->wrbuf, "<name>");
532 wrbuf_xmlputs(c->wrbuf, p[i]->term);
533 wrbuf_puts(c->wrbuf, "</name>");
535 wrbuf_printf(c->wrbuf,
536 "<frequency>%d</frequency>",
538 wrbuf_puts(c->wrbuf, "</term>\n");
541 wrbuf_puts(c->wrbuf, "</list>\n");
546 wrbuf_puts(c->wrbuf, "</termlist>\n");
547 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
548 http_send_response(c);
552 static void cmd_bytarget(struct http_channel *c)
554 struct http_response *rs = c->response;
555 struct http_request *rq = c->request;
556 struct http_session *s = locate_session(c);
557 struct hitsbytarget *ht;
558 const char *settings = http_argbyname(rq, "settings");
563 ht = hitsbytarget(s->psession, &count, c->nmem);
564 wrbuf_rewind(c->wrbuf);
565 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<bytarget><status>OK</status>");
567 for (i = 0; i < count; i++)
569 wrbuf_puts(c->wrbuf, "\n<target>");
571 wrbuf_puts(c->wrbuf, "<id>");
572 wrbuf_xmlputs(c->wrbuf, ht[i].id);
573 wrbuf_puts(c->wrbuf, "</id>\n");
575 if (ht[i].name && ht[i].name[0])
577 wrbuf_puts(c->wrbuf, "<name>");
578 wrbuf_xmlputs(c->wrbuf, ht[i].name);
579 wrbuf_puts(c->wrbuf, "</name>\n");
582 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
583 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
584 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
586 wrbuf_puts(c->wrbuf, "<state>");
587 wrbuf_xmlputs(c->wrbuf, ht[i].state);
588 wrbuf_puts(c->wrbuf, "</state>\n");
589 if (settings && *settings == '1')
591 wrbuf_puts(c->wrbuf, "<settings>\n");
592 wrbuf_puts(c->wrbuf, wrbuf_cstr(ht[i].settings_xml));
593 wrbuf_puts(c->wrbuf, "</settings>\n");
595 wrbuf_puts(c->wrbuf, "</target>");
596 wrbuf_destroy(ht[i].settings_xml);
599 wrbuf_puts(c->wrbuf, "</bytarget>");
600 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
601 http_send_response(c);
604 static void write_metadata(WRBUF w, struct conf_service *service,
605 struct record_metadata **ml, int full)
609 for (imeta = 0; imeta < service->num_metadata; imeta++)
611 struct conf_metadata *cmd = &service->metadata[imeta];
612 struct record_metadata *md;
613 if (!cmd->brief && !full)
615 for (md = ml[imeta]; md; md = md->next)
617 struct record_metadata_attr *attr = md->attributes;
618 wrbuf_printf(w, "\n<md-%s", cmd->name);
620 for (; attr; attr = attr->next)
622 wrbuf_printf(w, " %s=\"", attr->name);
623 wrbuf_xmlputs(w, attr->value);
629 case Metadata_type_generic:
630 wrbuf_xmlputs(w, md->data.text.disp);
632 case Metadata_type_year:
633 wrbuf_printf(w, "%d", md->data.number.min);
634 if (md->data.number.min != md->data.number.max)
635 wrbuf_printf(w, "-%d", md->data.number.max);
638 wrbuf_puts(w, "[can't represent]");
640 wrbuf_printf(w, "</md-%s>", cmd->name);
645 static void write_subrecord(struct record *r, WRBUF w,
646 struct conf_service *service, int show_details)
648 const char *name = session_setting_oneval(
649 client_get_database(r->client), PZ_NAME);
651 wrbuf_puts(w, "<location id=\"");
652 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
653 wrbuf_puts(w, "\" ");
655 wrbuf_puts(w, "name=\"");
656 wrbuf_xmlputs(w, *name ? name : "Unknown");
657 wrbuf_puts(w, "\">");
659 write_metadata(w, service, r->metadata, show_details);
660 wrbuf_puts(w, "</location>\n");
663 static void show_raw_record_error(void *data, const char *addinfo)
665 http_channel_observer_t obs = data;
666 struct http_channel *c = http_channel_observer_chan(obs);
667 struct http_response *rs = c->response;
669 http_remove_observer(obs);
671 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
674 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
676 http_channel_observer_t obs = data;
677 struct http_channel *c = http_channel_observer_chan(obs);
678 struct http_response *rs = c->response;
680 http_remove_observer(obs);
682 wrbuf_write(c->wrbuf, buf, sz);
683 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
684 http_send_response(c);
688 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
690 http_channel_observer_t obs = data;
691 struct http_channel *c = http_channel_observer_chan(obs);
692 struct http_response *rs = c->response;
694 http_remove_observer(obs);
696 wrbuf_write(c->wrbuf, buf, sz);
697 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
699 rs->content_type = "application/octet-stream";
700 http_send_response(c);
704 void show_raw_reset(void *data, struct http_channel *c, void *data2)
706 //struct client *client = data;
707 //client_show_raw_remove(client, data2);
710 static void cmd_record_ready(void *data);
712 static void cmd_record(struct http_channel *c)
714 struct http_response *rs = c->response;
715 struct http_request *rq = c->request;
716 struct http_session *s = locate_session(c);
717 struct record_cluster *rec, *prev_r, *next_r;
719 struct conf_service *service;
720 const char *idstr = http_argbyname(rq, "id");
721 const char *offsetstr = http_argbyname(rq, "offset");
722 const char *binarystr = http_argbyname(rq, "binary");
726 service = s->psession->service;
729 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
732 wrbuf_rewind(c->wrbuf);
733 if (!(rec = show_single_start(s->psession, idstr, &prev_r, &next_r)))
735 if (session_active_clients(s->psession) == 0)
737 error(rs, PAZPAR2_RECORD_MISSING, idstr);
739 else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
740 cmd_record_ready, c, c) != 0)
742 error(rs, PAZPAR2_RECORD_MISSING, idstr);
748 int offset = atoi(offsetstr);
749 const char *syntax = http_argbyname(rq, "syntax");
750 const char *esn = http_argbyname(rq, "esn");
752 struct record*r = rec->records;
755 if (binarystr && *binarystr != '0')
758 for (i = 0; i < offset && r; r = r->next, i++)
762 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
766 http_channel_observer_t obs =
767 http_add_observer(c, r->client, show_raw_reset);
768 int ret = client_show_raw_begin(r->client, r->position,
771 show_raw_record_error,
773 show_raw_record_ok_binary :
778 http_remove_observer(obs);
779 error(rs, PAZPAR2_NO_SESSION, 0);
785 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<record>\n");
786 wrbuf_puts(c->wrbuf, "<recid>");
787 wrbuf_xmlputs(c->wrbuf, rec->recid);
788 wrbuf_puts(c->wrbuf, "</recid>\n");
791 wrbuf_puts(c->wrbuf, "<prevrecid>");
792 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
793 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
797 wrbuf_puts(c->wrbuf, "<nextrecid>");
798 wrbuf_xmlputs(c->wrbuf, next_r->recid);
799 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
801 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
802 session_active_clients(s->psession));
803 write_metadata(c->wrbuf, service, rec->metadata, 1);
804 for (r = rec->records; r; r = r->next)
805 write_subrecord(r, c->wrbuf, service, 1);
806 wrbuf_puts(c->wrbuf, "</record>\n");
807 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
808 http_send_response(c);
810 show_single_stop(s->psession, rec);
813 static void cmd_record_ready(void *data)
815 struct http_channel *c = (struct http_channel *) data;
820 static void show_records(struct http_channel *c, int active)
822 struct http_request *rq = c->request;
823 struct http_response *rs = c->response;
824 struct http_session *s = locate_session(c);
825 struct record_cluster **rl;
826 struct reclist_sortparms *sp;
827 const char *start = http_argbyname(rq, "start");
828 const char *num = http_argbyname(rq, "num");
829 const char *sort = http_argbyname(rq, "sort");
839 // We haven't counted clients yet if we're called on a block release
841 active = session_active_clients(s->psession);
844 startn = atoi(start);
849 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
851 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
856 rl = show_range_start(s->psession, sp, startn, &numn, &total, &total_hits);
858 wrbuf_rewind(c->wrbuf);
859 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<show>\n<status>OK</status>\n");
860 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
861 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
862 wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
863 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
864 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
866 for (i = 0; i < numn; i++)
870 struct record_cluster *rec = rl[i];
871 struct conf_service *service = s->psession->service;
873 wrbuf_puts(c->wrbuf, "<hit>\n");
874 write_metadata(c->wrbuf, service, rec->metadata, 0);
875 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
876 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
878 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
879 if (strstr(sort, "relevance"))
880 wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n",
881 rec->relevance_score);
882 wrbuf_puts(c->wrbuf, "<recid>");
883 wrbuf_xmlputs(c->wrbuf, rec->recid);
884 wrbuf_puts(c->wrbuf, "</recid>\n");
885 wrbuf_puts(c->wrbuf, "</hit>\n");
888 show_range_stop(s->psession, rl);
890 wrbuf_puts(c->wrbuf, "</show>\n");
891 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
892 http_send_response(c);
895 static void show_records_ready(void *data)
897 struct http_channel *c = (struct http_channel *) data;
902 static void cmd_show(struct http_channel *c)
904 struct http_request *rq = c->request;
905 struct http_session *s = locate_session(c);
906 const char *block = http_argbyname(rq, "block");
912 status = session_active_clients(s->psession);
916 if (status && reclist_get_num_records(s->psession->reclist) == 0)
918 // if there is already a watch/block. we do not block this one
919 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
920 show_records_ready, c, c) != 0)
922 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
928 show_records(c, status);
931 static void cmd_ping(struct http_channel *c)
933 struct http_response *rs = c->response;
934 struct http_session *s = locate_session(c);
937 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<ping><status>OK</status></ping>";
938 http_send_response(c);
941 static int utf_8_valid(const char *str)
943 yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
946 /* check that query is UTF-8 encoded */
947 char *inbuf = (char *) str; /* we know iconv does not alter this */
948 size_t inbytesleft = strlen(inbuf);
950 size_t outbytesleft = strlen(inbuf) + 10;
951 char *out = xmalloc(outbytesleft);
953 size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
955 /* if OK, try flushing the rest */
956 if (r != (size_t) (-1))
957 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
960 if (r == (size_t) (-1))
966 static void cmd_search(struct http_channel *c)
968 struct http_request *rq = c->request;
969 struct http_response *rs = c->response;
970 struct http_session *s = locate_session(c);
971 const char *query = http_argbyname(rq, "query");
972 const char *filter = http_argbyname(rq, "filter");
973 const char *maxrecs = http_argbyname(rq, "maxrecs");
974 const char *startrecs = http_argbyname(rq, "startrecs");
975 enum pazpar2_error_code code;
976 const char *addinfo = 0;
982 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
985 if (!utf_8_valid(query))
987 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
990 code = search(s->psession, query, startrecs, maxrecs, filter, &addinfo);
993 error(rs, code, addinfo);
996 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<search><status>OK</status></search>";
997 http_send_response(c);
1001 static void cmd_stat(struct http_channel *c)
1003 struct http_response *rs = c->response;
1004 struct http_session *s = locate_session(c);
1005 struct statistics stat;
1013 clients = session_active_clients(s->psession);
1014 statistics(s->psession, &stat);
1016 if (stat.num_clients > 0) {
1017 progress = (stat.num_clients - clients) / (float)stat.num_clients;
1020 wrbuf_rewind(c->wrbuf);
1021 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<stat>");
1022 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
1023 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
1024 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
1025 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
1026 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
1027 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
1028 wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
1029 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
1030 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
1031 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
1032 wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
1033 wrbuf_puts(c->wrbuf, "</stat>");
1034 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1035 http_send_response(c);
1038 static void cmd_info(struct http_channel *c)
1040 char yaz_version_str[20];
1041 struct http_response *rs = c->response;
1043 wrbuf_rewind(c->wrbuf);
1044 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<info>\n");
1045 wrbuf_puts(c->wrbuf, " <version>\n");
1046 wrbuf_puts(c->wrbuf, "<pazpar2");
1047 #ifdef PAZPAR2_VERSION_SHA1
1048 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
1050 wrbuf_puts(c->wrbuf, ">");
1051 wrbuf_xmlputs(c->wrbuf, VERSION);
1052 wrbuf_puts(c->wrbuf, "</pazpar2>");
1055 yaz_version(yaz_version_str, 0);
1056 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
1057 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
1058 wrbuf_puts(c->wrbuf, "\">");
1059 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
1060 wrbuf_puts(c->wrbuf, "</yaz>\n");
1062 wrbuf_puts(c->wrbuf, " </version>\n");
1064 wrbuf_puts(c->wrbuf, "</info>");
1065 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1066 http_send_response(c);
1071 void (*fun)(struct http_channel *c);
1073 { "init", cmd_init },
1074 { "settings", cmd_settings },
1075 { "stat", cmd_stat },
1076 { "bytarget", cmd_bytarget },
1077 { "show", cmd_show },
1078 { "search", cmd_search },
1079 { "termlist", cmd_termlist },
1080 { "exit", cmd_exit },
1081 { "ping", cmd_ping },
1082 { "record", cmd_record },
1083 { "info", cmd_info },
1087 void http_command(struct http_channel *c)
1089 const char *command = http_argbyname(c->request, "command");
1090 struct http_response *rs = http_create_response(c);
1095 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1096 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1100 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1103 for (i = 0; commands[i].name; i++)
1104 if (!strcmp(commands[i].name, command))
1106 (*commands[i].fun)(c);
1109 if (!commands[i].name)
1110 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1118 * c-file-style: "Stroustrup"
1119 * indent-tabs-mode: nil
1121 * vim: shiftwidth=4 tabstop=8 expandtab