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;
67 http_sessions_t http_sessions_create(void)
69 http_sessions_t hs = xmalloc(sizeof(*hs));
72 pazpar2_mutex_create(&hs->mutex, "http_sessions");
73 hs->log_level = yaz_log_module_level("HTTP");
77 void http_sessions_destroy(http_sessions_t hs)
81 struct http_session *s = hs->session_list;
84 struct http_session *s_next = s->next;
85 iochan_destroy(s->timeout_iochan);
86 destroy_session(s->psession);
87 nmem_destroy(s->nmem);
90 yaz_mutex_destroy(&hs->mutex);
95 void http_session_destroy(struct http_session *s);
97 static void session_timeout(IOCHAN i, int event)
99 struct http_session *s = iochan_getdata(i);
100 http_session_destroy(s);
103 struct http_session *http_session_create(struct conf_service *service,
104 http_sessions_t http_sessions,
107 NMEM nmem = nmem_create();
108 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
111 sprintf(tmp_str, "session#%u", sesid);
112 r->psession = new_session(nmem, service, tmp_str);
113 r->session_id = sesid;
116 r->destroy_counter = r->activity_counter = 0;
117 r->http_sessions = http_sessions;
119 yaz_mutex_enter(http_sessions->mutex);
120 r->next = http_sessions->session_list;
121 http_sessions->session_list = r;
122 yaz_mutex_leave(http_sessions->mutex);
124 r->timeout_iochan = iochan_create(-1, session_timeout, 0, "http_session_timeout");
125 iochan_setdata(r->timeout_iochan, r);
126 yaz_log(http_sessions->log_level, "%p Session %u created. timeout chan=%p timeout=%d", r, sesid, r->timeout_iochan, service->session_timeout);
127 iochan_settimeout(r->timeout_iochan, service->session_timeout);
129 iochan_add(service->server->iochan_man, r->timeout_iochan);
133 void http_session_destroy(struct http_session *s)
135 int must_destroy = 0;
137 http_sessions_t http_sessions = s->http_sessions;
139 yaz_log(http_sessions->log_level, "%p Session %u destroyed", s, s->session_id);
140 yaz_mutex_enter(http_sessions->mutex);
141 /* only if http_session has no active http sessions on it can be destroyed */
142 if (s->destroy_counter == s->activity_counter) {
143 struct http_session **p = 0;
145 for (p = &http_sessions->session_list; *p; p = &(*p)->next)
152 yaz_mutex_leave(http_sessions->mutex);
154 { /* destroying for real */
155 yaz_log(http_sessions->log_level, "%p Session %u destroyed", s, s->session_id);
156 iochan_destroy(s->timeout_iochan);
157 destroy_session(s->psession);
158 nmem_destroy(s->nmem);
161 yaz_log(http_sessions->log_level, "%p Session %u destroyed delayed. Active clients (%d-%d). Waiting for new timeout.",
162 s, s->session_id, s->activity_counter, s->destroy_counter);
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 // Call after use of locate_session, in order to increment the destroy_counter
283 static void release_session(struct http_channel *c, struct http_session *session) {
284 http_sessions_t http_sessions = c->http_sessions;
285 yaz_mutex_enter(http_sessions->mutex);
287 session->destroy_counter++;
288 yaz_mutex_leave(http_sessions->mutex);
291 // Decode settings parameters and apply to session
292 // Syntax: setting[target]=value
293 static int process_settings(struct session *se, struct http_request *rq,
294 struct http_response *rs)
296 struct http_argument *a;
298 for (a = rq->arguments; a; a = a->next)
299 if (strchr(a->name, '['))
306 // Nmem_strsplit *rules*!!!
307 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
310 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
315 session_apply_setting(se, dbname, setting,
316 nmem_strdup(se->session_nmem, a->value));
321 static void cmd_exit(struct http_channel *c)
323 yaz_log(YLOG_WARN, "exit");
324 http_close_server(c->server);
327 static void cmd_init(struct http_channel *c)
330 struct http_request *r = c->request;
331 const char *clear = http_argbyname(r, "clear");
332 const char *content_type = http_lookup_header(r->headers, "Content-Type");
334 struct http_session *s;
335 struct http_response *rs = c->response;
336 struct conf_service *service = 0; /* no service (yet) */
338 if (r->content_len && content_type &&
339 !yaz_strcmp_del("text/xml", content_type, "; "))
341 xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
345 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
348 root_n = xmlDocGetRootElement(doc);
349 service = service_create(c->server, root_n);
353 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
360 const char *service_name = http_argbyname(c->request, "service");
361 service = locate_service(c->server, service_name);
364 error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
368 sesid = make_sessionid();
369 s = http_session_create(service, c->http_sessions, sesid);
371 yaz_log(c->http_sessions->log_level, "%p Session init %u ", s, sesid);
372 if (!clear || *clear == '0')
373 session_init_databases(s->psession);
375 yaz_log(YLOG_LOG, "HTTP Session %u init: No databases preloaded", sesid);
377 if (process_settings(s->psession, c->request, c->response) < 0)
380 sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX
381 "<init><status>OK</status><session>%d", sesid);
382 if (c->server->server_id)
385 strcat(buf, c->server->server_id);
387 strcat(buf, "</session>"
388 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>");
389 rs->payload = nmem_strdup(c->nmem, buf);
390 http_send_response(c);
393 static void apply_local_setting(void *client_data,
396 struct session *se = (struct session *) client_data;
398 session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
399 nmem_strdup(se->session_nmem, set->name),
400 nmem_strdup(se->session_nmem, set->value));
403 static void cmd_settings(struct http_channel *c)
405 struct http_response *rs = c->response;
406 struct http_request *rq = c->request;
407 struct http_session *s = locate_session(c);
408 const char *content_type = http_lookup_header(rq->headers, "Content-Type");
413 if (rq->content_len && content_type &&
414 !yaz_strcmp_del("text/xml", content_type, "; "))
416 xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
420 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
423 root_n = xmlDocGetRootElement(doc);
425 settings_read_node_x(root_n, s->psession, apply_local_setting);
429 if (process_settings(s->psession, rq, rs) < 0) {
430 release_session(c,s);
433 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<settings><status>OK</status></settings>";
434 http_send_response(c);
435 release_session(c,s);
438 // Compares two hitsbytarget nodes by hitcount
439 static int cmp_ht(const void *p1, const void *p2)
441 const struct hitsbytarget *h1 = p1;
442 const struct hitsbytarget *h2 = p2;
443 return h2->hits - h1->hits;
446 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
447 static void targets_termlist(WRBUF wrbuf, struct session *se, int num,
450 struct hitsbytarget *ht;
453 ht = hitsbytarget(se, &count, nmem);
454 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
455 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
458 // do only print terms which have display names
460 wrbuf_puts(wrbuf, "<term>\n");
462 wrbuf_puts(wrbuf, "<id>");
463 wrbuf_xmlputs(wrbuf, ht[i].id);
464 wrbuf_puts(wrbuf, "</id>\n");
466 wrbuf_puts(wrbuf, "<name>");
467 if (!ht[i].name || !ht[i].name[0])
468 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
470 wrbuf_xmlputs(wrbuf, ht[i].name);
471 wrbuf_puts(wrbuf, "</name>\n");
473 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
476 wrbuf_puts(wrbuf, "<state>");
477 wrbuf_xmlputs(wrbuf, ht[i].state);
478 wrbuf_puts(wrbuf, "</state>\n");
480 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
482 wrbuf_puts(wrbuf, "</term>\n");
486 static void cmd_termlist(struct http_channel *c)
488 struct http_response *rs = c->response;
489 struct http_request *rq = c->request;
490 struct http_session *s = locate_session(c);
491 struct termlist_score **p;
494 const char *name = http_argbyname(rq, "name");
495 const char *nums = http_argbyname(rq, "num");
502 status = session_active_clients(s->psession);
506 if (strlen(name) > 255)
511 wrbuf_rewind(c->wrbuf);
513 wrbuf_puts(c->wrbuf, "<termlist>\n");
514 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
520 if (!(tp = strchr(name, ',')))
521 tp = name + strlen(name);
522 strncpy(tname, name, tp - name);
523 tname[tp - name] = '\0';
525 wrbuf_puts(c->wrbuf, "<list name=\"");
526 wrbuf_xmlputs(c->wrbuf, tname);
527 wrbuf_puts(c->wrbuf, "\">\n");
528 if (!strcmp(tname, "xtargets"))
529 targets_termlist(c->wrbuf, s->psession, num, c->nmem);
532 p = termlist(s->psession, tname, &len);
534 for (i = 0; i < len && i < num; i++){
535 // prevnt sending empty term elements
536 if (!p[i]->term || !p[i]->term[0])
539 wrbuf_puts(c->wrbuf, "<term>");
540 wrbuf_puts(c->wrbuf, "<name>");
541 wrbuf_xmlputs(c->wrbuf, p[i]->term);
542 wrbuf_puts(c->wrbuf, "</name>");
544 wrbuf_printf(c->wrbuf,
545 "<frequency>%d</frequency>",
547 wrbuf_puts(c->wrbuf, "</term>\n");
550 wrbuf_puts(c->wrbuf, "</list>\n");
555 wrbuf_puts(c->wrbuf, "</termlist>\n");
556 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
557 http_send_response(c);
558 release_session(c,s);
562 static void cmd_bytarget(struct http_channel *c)
564 struct http_response *rs = c->response;
565 struct http_request *rq = c->request;
566 struct http_session *s = locate_session(c);
567 struct hitsbytarget *ht;
568 const char *settings = http_argbyname(rq, "settings");
573 ht = hitsbytarget(s->psession, &count, c->nmem);
574 wrbuf_rewind(c->wrbuf);
575 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<bytarget><status>OK</status>");
577 for (i = 0; i < count; i++)
579 wrbuf_puts(c->wrbuf, "\n<target>");
581 wrbuf_puts(c->wrbuf, "<id>");
582 wrbuf_xmlputs(c->wrbuf, ht[i].id);
583 wrbuf_puts(c->wrbuf, "</id>\n");
585 if (ht[i].name && ht[i].name[0])
587 wrbuf_puts(c->wrbuf, "<name>");
588 wrbuf_xmlputs(c->wrbuf, ht[i].name);
589 wrbuf_puts(c->wrbuf, "</name>\n");
592 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
593 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
594 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
596 wrbuf_puts(c->wrbuf, "<state>");
597 wrbuf_xmlputs(c->wrbuf, ht[i].state);
598 wrbuf_puts(c->wrbuf, "</state>\n");
599 if (settings && *settings == '1')
601 wrbuf_puts(c->wrbuf, "<settings>\n");
602 wrbuf_puts(c->wrbuf, wrbuf_cstr(ht[i].settings_xml));
603 wrbuf_puts(c->wrbuf, "</settings>\n");
605 wrbuf_puts(c->wrbuf, "</target>");
606 wrbuf_destroy(ht[i].settings_xml);
609 wrbuf_puts(c->wrbuf, "</bytarget>");
610 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
611 http_send_response(c);
612 release_session(c,s);
615 static void write_metadata(WRBUF w, struct conf_service *service,
616 struct record_metadata **ml, int full)
620 for (imeta = 0; imeta < service->num_metadata; imeta++)
622 struct conf_metadata *cmd = &service->metadata[imeta];
623 struct record_metadata *md;
624 if (!cmd->brief && !full)
626 for (md = ml[imeta]; md; md = md->next)
628 struct record_metadata_attr *attr = md->attributes;
629 wrbuf_printf(w, "\n<md-%s", cmd->name);
631 for (; attr; attr = attr->next)
633 wrbuf_printf(w, " %s=\"", attr->name);
634 wrbuf_xmlputs(w, attr->value);
640 case Metadata_type_generic:
641 wrbuf_xmlputs(w, md->data.text.disp);
643 case Metadata_type_year:
644 wrbuf_printf(w, "%d", md->data.number.min);
645 if (md->data.number.min != md->data.number.max)
646 wrbuf_printf(w, "-%d", md->data.number.max);
649 wrbuf_puts(w, "[can't represent]");
651 wrbuf_printf(w, "</md-%s>", cmd->name);
656 static void write_subrecord(struct record *r, WRBUF w,
657 struct conf_service *service, int show_details)
659 const char *name = session_setting_oneval(
660 client_get_database(r->client), PZ_NAME);
662 wrbuf_puts(w, "<location id=\"");
663 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
664 wrbuf_puts(w, "\" ");
666 wrbuf_puts(w, "name=\"");
667 wrbuf_xmlputs(w, *name ? name : "Unknown");
668 wrbuf_puts(w, "\">");
670 write_metadata(w, service, r->metadata, show_details);
671 wrbuf_puts(w, "</location>\n");
674 static void show_raw_record_error(void *data, const char *addinfo)
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 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
685 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
687 http_channel_observer_t obs = data;
688 struct http_channel *c = http_channel_observer_chan(obs);
689 struct http_response *rs = c->response;
691 http_remove_observer(obs);
693 wrbuf_write(c->wrbuf, buf, sz);
694 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
695 http_send_response(c);
699 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
701 http_channel_observer_t obs = data;
702 struct http_channel *c = http_channel_observer_chan(obs);
703 struct http_response *rs = c->response;
705 http_remove_observer(obs);
707 wrbuf_write(c->wrbuf, buf, sz);
708 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
710 rs->content_type = "application/octet-stream";
711 http_send_response(c);
715 void show_raw_reset(void *data, struct http_channel *c, void *data2)
717 //struct client *client = data;
718 //client_show_raw_remove(client, data2);
721 static void cmd_record_ready(void *data);
723 static void cmd_record(struct http_channel *c)
725 struct http_response *rs = c->response;
726 struct http_request *rq = c->request;
727 struct http_session *s = locate_session(c);
728 struct record_cluster *rec, *prev_r, *next_r;
730 struct conf_service *service;
731 const char *idstr = http_argbyname(rq, "id");
732 const char *offsetstr = http_argbyname(rq, "offset");
733 const char *binarystr = http_argbyname(rq, "binary");
737 service = s->psession->service;
740 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
743 wrbuf_rewind(c->wrbuf);
744 if (!(rec = show_single_start(s->psession, idstr, &prev_r, &next_r)))
746 if (session_active_clients(s->psession) == 0)
748 error(rs, PAZPAR2_RECORD_MISSING, idstr);
750 else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
751 cmd_record_ready, c, c) != 0)
753 error(rs, PAZPAR2_RECORD_MISSING, idstr);
755 release_session(c, s);
760 int offset = atoi(offsetstr);
761 const char *syntax = http_argbyname(rq, "syntax");
762 const char *esn = http_argbyname(rq, "esn");
764 struct record*r = rec->records;
767 if (binarystr && *binarystr != '0')
770 for (i = 0; i < offset && r; r = r->next, i++)
774 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
778 http_channel_observer_t obs =
779 http_add_observer(c, r->client, show_raw_reset);
780 int ret = client_show_raw_begin(r->client, r->position,
783 show_raw_record_error,
785 show_raw_record_ok_binary :
790 http_remove_observer(obs);
791 error(rs, PAZPAR2_NO_SESSION, 0);
797 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<record>\n");
798 wrbuf_puts(c->wrbuf, "<recid>");
799 wrbuf_xmlputs(c->wrbuf, rec->recid);
800 wrbuf_puts(c->wrbuf, "</recid>\n");
803 wrbuf_puts(c->wrbuf, "<prevrecid>");
804 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
805 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
809 wrbuf_puts(c->wrbuf, "<nextrecid>");
810 wrbuf_xmlputs(c->wrbuf, next_r->recid);
811 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
813 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
814 session_active_clients(s->psession));
815 write_metadata(c->wrbuf, service, rec->metadata, 1);
816 for (r = rec->records; r; r = r->next)
817 write_subrecord(r, c->wrbuf, service, 1);
818 wrbuf_puts(c->wrbuf, "</record>\n");
819 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
820 http_send_response(c);
822 show_single_stop(s->psession, rec);
823 release_session(c, s);
826 static void cmd_record_ready(void *data)
828 struct http_channel *c = (struct http_channel *) data;
833 static void show_records(struct http_channel *c, int active)
835 struct http_request *rq = c->request;
836 struct http_response *rs = c->response;
837 struct http_session *s = locate_session(c);
838 struct record_cluster **rl;
839 struct reclist_sortparms *sp;
840 const char *start = http_argbyname(rq, "start");
841 const char *num = http_argbyname(rq, "num");
842 const char *sort = http_argbyname(rq, "sort");
852 // We haven't counted clients yet if we're called on a block release
854 active = session_active_clients(s->psession);
857 startn = atoi(start);
862 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
864 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
865 release_session(c, s);
870 rl = show_range_start(s->psession, sp, startn, &numn, &total, &total_hits);
872 wrbuf_rewind(c->wrbuf);
873 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<show>\n<status>OK</status>\n");
874 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
875 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
876 wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
877 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
878 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
880 for (i = 0; i < numn; i++)
884 struct record_cluster *rec = rl[i];
885 struct conf_service *service = s->psession->service;
887 wrbuf_puts(c->wrbuf, "<hit>\n");
888 write_metadata(c->wrbuf, service, rec->metadata, 0);
889 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
890 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
892 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
893 if (strstr(sort, "relevance"))
894 wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n",
895 rec->relevance_score);
896 wrbuf_puts(c->wrbuf, "<recid>");
897 wrbuf_xmlputs(c->wrbuf, rec->recid);
898 wrbuf_puts(c->wrbuf, "</recid>\n");
899 wrbuf_puts(c->wrbuf, "</hit>\n");
902 show_range_stop(s->psession, rl);
904 wrbuf_puts(c->wrbuf, "</show>\n");
905 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
906 http_send_response(c);
907 release_session(c, s);
910 static void show_records_ready(void *data)
912 struct http_channel *c = (struct http_channel *) data;
917 static void cmd_show(struct http_channel *c)
919 struct http_request *rq = c->request;
920 struct http_session *s = locate_session(c);
921 const char *block = http_argbyname(rq, "block");
927 status = session_active_clients(s->psession);
931 if (status && reclist_get_num_records(s->psession->reclist) == 0)
933 // if there is already a watch/block. we do not block this one
934 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
935 show_records_ready, c, c) != 0)
937 yaz_log(c->http_sessions->log_level, "%p Session %u: Blocking on cmd_show", s, s->session_id);
939 release_session(c,s);
943 show_records(c, status);
944 release_session(c,s);
947 static void cmd_ping(struct http_channel *c)
949 struct http_response *rs = c->response;
950 struct http_session *s = locate_session(c);
953 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<ping><status>OK</status></ping>";
954 http_send_response(c);
955 release_session(c, s);
958 static int utf_8_valid(const char *str)
960 yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
963 /* check that query is UTF-8 encoded */
964 char *inbuf = (char *) str; /* we know iconv does not alter this */
965 size_t inbytesleft = strlen(inbuf);
967 size_t outbytesleft = strlen(inbuf) + 10;
968 char *out = xmalloc(outbytesleft);
970 size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
972 /* if OK, try flushing the rest */
973 if (r != (size_t) (-1))
974 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
977 if (r == (size_t) (-1))
983 static void cmd_search(struct http_channel *c)
985 struct http_request *rq = c->request;
986 struct http_response *rs = c->response;
987 struct http_session *s = locate_session(c);
988 const char *query = http_argbyname(rq, "query");
989 const char *filter = http_argbyname(rq, "filter");
990 const char *maxrecs = http_argbyname(rq, "maxrecs");
991 const char *startrecs = http_argbyname(rq, "startrecs");
992 enum pazpar2_error_code code;
993 const char *addinfo = 0;
999 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
1000 release_session(c,s);
1003 if (!utf_8_valid(query))
1005 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
1006 release_session(c,s);
1009 code = search(s->psession, query, startrecs, maxrecs, filter, &addinfo);
1012 error(rs, code, addinfo);
1013 release_session(c,s);
1016 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<search><status>OK</status></search>";
1017 http_send_response(c);
1018 release_session(c,s);
1022 static void cmd_stat(struct http_channel *c)
1024 struct http_response *rs = c->response;
1025 struct http_session *s = locate_session(c);
1026 struct statistics stat;
1034 clients = session_active_clients(s->psession);
1035 statistics(s->psession, &stat);
1037 if (stat.num_clients > 0) {
1038 progress = (stat.num_clients - clients) / (float)stat.num_clients;
1041 wrbuf_rewind(c->wrbuf);
1042 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<stat>");
1043 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
1044 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
1045 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
1046 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
1047 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
1048 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
1049 wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
1050 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
1051 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
1052 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
1053 wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
1054 wrbuf_puts(c->wrbuf, "</stat>");
1055 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1056 http_send_response(c);
1057 release_session(c,s);
1060 static void cmd_info(struct http_channel *c)
1062 char yaz_version_str[20];
1063 struct http_response *rs = c->response;
1065 wrbuf_rewind(c->wrbuf);
1066 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<info>\n");
1067 wrbuf_puts(c->wrbuf, " <version>\n");
1068 wrbuf_puts(c->wrbuf, "<pazpar2");
1069 #ifdef PAZPAR2_VERSION_SHA1
1070 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
1072 wrbuf_puts(c->wrbuf, ">");
1073 wrbuf_xmlputs(c->wrbuf, VERSION);
1074 wrbuf_puts(c->wrbuf, "</pazpar2>");
1077 yaz_version(yaz_version_str, 0);
1078 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
1079 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
1080 wrbuf_puts(c->wrbuf, "\">");
1081 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
1082 wrbuf_puts(c->wrbuf, "</yaz>\n");
1084 wrbuf_puts(c->wrbuf, " </version>\n");
1086 wrbuf_puts(c->wrbuf, "</info>");
1087 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1088 http_send_response(c);
1093 void (*fun)(struct http_channel *c);
1095 { "init", cmd_init },
1096 { "settings", cmd_settings },
1097 { "stat", cmd_stat },
1098 { "bytarget", cmd_bytarget },
1099 { "show", cmd_show },
1100 { "search", cmd_search },
1101 { "termlist", cmd_termlist },
1102 { "exit", cmd_exit },
1103 { "ping", cmd_ping },
1104 { "record", cmd_record },
1105 { "info", cmd_info },
1109 void http_command(struct http_channel *c)
1111 const char *command = http_argbyname(c->request, "command");
1112 struct http_response *rs = http_create_response(c);
1117 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1118 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1122 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1125 for (i = 0; commands[i].name; i++)
1126 if (!strcmp(commands[i].name, command))
1128 (*commands[i].fun)(c);
1131 if (!commands[i].name)
1132 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1140 * c-file-style: "Stroustrup"
1141 * indent-tabs-mode: nil
1143 * vim: shiftwidth=4 tabstop=8 expandtab