1 /* This file is part of Pazpar2.
2 Copyright (C) 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"
47 void print_meminfo(WRBUF wrbuf)
49 struct mallinfo minfo;
51 wrbuf_printf(wrbuf, " <memory>\n"
52 " <arena>%d</arena><!-- Non-mmapped space allocated (bytes) -->\n"
53 " <ordblks>%d</ordblks><!-- Number of free chunks -->\n"
54 " <smblks>%d</smblks><!-- Number of free fastbin blocks -->\n"
55 " <hblks>%d</hblks><!-- Number of mmapped regions -->\n"
56 " <hblkhd>%d</hblkhd><!-- Space allocated in mmapped regions (bytes) -->\n"
57 " <usmblks>%d</usmblks><!-- Maximum total allocated space (bytes) -->\n"
58 " <fsmblks>%d</fsmblks><!-- Space in freed fastbin blocks (bytes) -->\n"
59 " <uordblks>%d</uordblks><!-- Total allocated space (bytes) -->\n"
60 " <fordblks>%d</fordblks><!-- Total free space (bytes) -->\n"
61 " <keepcost>%d</keepcost><!-- Top-most, releasable space (bytes) -->\n"
75 #define print_meminfo(x)
79 // Update this when the protocol changes
80 #define PAZPAR2_PROTOCOL_VERSION "1"
82 #define HTTP_COMMAND_RESPONSE_PREFIX "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
85 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
86 struct session *psession;
87 unsigned int session_id;
92 http_sessions_t http_sessions;
93 struct http_session *next;
96 struct http_sessions {
97 struct http_session *session_list;
102 static YAZ_MUTEX g_http_session_mutex = 0;
103 static int g_http_sessions = 0;
105 static void show_records_ready(void *data);
107 int get_version(struct http_request *rq) {
108 const char *version = http_argbyname(rq, "version");
110 if (version && strcmp(version, "")) {
111 version_no = atoi(version);
117 int http_session_use(int delta)
120 if (!g_http_session_mutex)
121 yaz_mutex_create(&g_http_session_mutex);
122 yaz_mutex_enter(g_http_session_mutex);
123 g_http_sessions += delta;
124 sessions = g_http_sessions;
125 yaz_mutex_leave(g_http_session_mutex);
126 yaz_log(YLOG_DEBUG, "%s http_sessions=%d", delta == 0 ? "" :
127 (delta > 0 ? "INC" : "DEC"), sessions);
132 http_sessions_t http_sessions_create(void)
134 http_sessions_t hs = xmalloc(sizeof(*hs));
135 hs->session_list = 0;
137 pazpar2_mutex_create(&hs->mutex, "http_sessions");
138 hs->log_level = yaz_log_module_level("http");
142 void http_sessions_destroy(http_sessions_t hs)
146 struct http_session *s = hs->session_list;
149 struct http_session *s_next = s->next;
150 iochan_destroy(s->timeout_iochan);
151 session_destroy(s->psession);
152 nmem_destroy(s->nmem);
155 yaz_mutex_destroy(&hs->mutex);
160 void http_session_destroy(struct http_session *s);
162 static void session_timeout(IOCHAN i, int event)
164 struct http_session *s = iochan_getdata(i);
165 http_session_destroy(s);
168 struct http_session *http_session_create(struct conf_service *service,
169 http_sessions_t http_sessions,
172 NMEM nmem = nmem_create();
173 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
176 sprintf(tmp_str, "session#%u", sesid);
177 r->psession = session_create(nmem, service, sesid);
178 r->session_id = sesid;
181 r->destroy_counter = r->activity_counter = 0;
182 r->http_sessions = http_sessions;
184 yaz_mutex_enter(http_sessions->mutex);
185 r->next = http_sessions->session_list;
186 http_sessions->session_list = r;
187 yaz_mutex_leave(http_sessions->mutex);
189 r->timeout_iochan = iochan_create(-1, session_timeout, 0,
190 "http_session_timeout");
191 iochan_setdata(r->timeout_iochan, r);
193 session_log(r->psession, http_sessions->log_level,
194 "HTTP session create. timeout chan=%p ses=%d",
195 r->timeout_iochan, service->session_timeout);
196 iochan_settimeout(r->timeout_iochan, service->session_timeout);
198 iochan_add(service->server->iochan_man, r->timeout_iochan, -1);
203 void http_session_destroy(struct http_session *s)
205 int must_destroy = 0;
207 http_sessions_t http_sessions = s->http_sessions;
209 session_log(s->psession, http_sessions->log_level,
210 "HTTP session destroy");
211 yaz_mutex_enter(http_sessions->mutex);
212 /* only if http_session has no active http sessions on it can be destroyed */
213 if (s->destroy_counter == s->activity_counter)
215 struct http_session **p = 0;
217 for (p = &http_sessions->session_list; *p; p = &(*p)->next)
224 yaz_mutex_leave(http_sessions->mutex);
226 { /* destroying for real */
227 session_log(s->psession, http_sessions->log_level, "About to destroyd");
228 iochan_destroy(s->timeout_iochan);
229 session_destroy(s->psession);
230 http_session_use(-1);
231 nmem_destroy(s->nmem);
235 session_log(s->psession, http_sessions->log_level,
236 "Destroy delayed. Active clients (%d-%d)",
237 s->activity_counter, s->destroy_counter);
242 static const char *get_msg(enum pazpar2_error_code code)
244 struct pazpar2_error_msg {
245 enum pazpar2_error_code code;
248 static const struct pazpar2_error_msg ar[] = {
249 { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
250 { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
251 { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
252 { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
253 { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
254 { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
255 { PAZPAR2_RECORD_MISSING, "Record missing"},
256 { PAZPAR2_NO_TARGETS, "No targets"},
257 { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
258 { PAZPAR2_RECORD_FAIL, "Record command failed"},
259 { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"},
260 { PAZPAR2_NO_SERVICE, "No service"},
261 { PAZPAR2_ALREADY_BLOCKED, "Already blocked in session on: "},
262 { PAZPAR2_LAST_ERROR, "Last error"},
268 if (code == ar[i].code)
275 static void error2(struct http_response *rs,
276 enum pazpar2_error_code code,
277 const char *addinfo, const char *addinfo2)
279 struct http_channel *c = rs->channel;
280 WRBUF text = wrbuf_alloc();
281 const char *http_status = "417";
282 const char *msg = get_msg(code);
284 rs->msg = nmem_strdup(c->nmem, msg);
285 strcpy(rs->code, http_status);
287 wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX
288 "<error code=\"%d\" msg=\"%s\">", (int) code, msg);
291 wrbuf_xmlputs(text, addinfo);
294 wrbuf_xmlputs(text, ": ");
295 wrbuf_xmlputs(text, addinfo2);
298 wrbuf_puts(text, "</error>");
300 yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
301 msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
302 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
304 http_send_response(c);
307 static void error(struct http_response *rs,
308 enum pazpar2_error_code code,
311 error2(rs, code, addinfo, 0);
314 static void response_open_command(struct http_channel *c, const char *command)
316 wrbuf_rewind(c->wrbuf);
317 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX);
319 wrbuf_printf(c->wrbuf, "<%s>", command);
322 static void response_open_ok(struct http_channel *c, const char *command)
324 response_open_command(c, command);
325 wrbuf_puts(c->wrbuf, "<status>OK</status>");
328 static void response_close(struct http_channel *c, const char *command)
330 struct http_response *rs = c->response;
333 wrbuf_printf(c->wrbuf, "</%s>", command);
334 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
335 http_send_response(c);
338 unsigned int make_sessionid(void)
340 static int seq = 0; /* thread pr */
344 if (global_parameters.predictable_sessions)
353 if (gettimeofday(&t, 0) < 0)
355 yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
358 /* at most 256 sessions per second ..
359 (long long would be more appropriate)*/
361 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
367 static struct http_session *locate_session(struct http_channel *c)
369 struct http_request *rq = c->request;
370 struct http_response *rs = c->response;
371 struct http_session *p;
372 const char *session = http_argbyname(rq, "session");
373 http_sessions_t http_sessions = c->http_sessions;
378 error(rs, PAZPAR2_MISSING_PARAMETER, "session");
382 yaz_mutex_enter(http_sessions->mutex);
383 for (p = http_sessions->session_list; p; p = p->next)
384 if (id == p->session_id)
387 p->activity_counter++;
388 yaz_mutex_leave(http_sessions->mutex);
390 iochan_activity(p->timeout_iochan);
392 error(rs, PAZPAR2_NO_SESSION, session);
396 // Call after use of locate_session, in order to increment the destroy_counter
397 static void release_session(struct http_channel *c,
398 struct http_session *session)
400 http_sessions_t http_sessions = c->http_sessions;
401 yaz_mutex_enter(http_sessions->mutex);
403 session->destroy_counter++;
404 yaz_mutex_leave(http_sessions->mutex);
407 // Decode settings parameters and apply to session
408 // Syntax: setting[target]=value
409 static int process_settings(struct session *se, struct http_request *rq,
410 struct http_response *rs)
412 struct http_argument *a;
413 NMEM nmem = nmem_create();
415 for (a = rq->arguments; a; a = a->next)
416 if (strchr(a->name, '['))
423 // Nmem_strsplit *rules*!!!
424 nmem_strsplit(nmem, "[]", a->name, &res, &num);
427 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
433 session_apply_setting(se, dbname, setting, a->value);
439 static void cmd_exit(struct http_channel *c)
441 yaz_log(YLOG_WARN, "exit");
443 response_open_ok(c, "exit");
444 response_close(c, "exit");
445 if (global_parameters.debug_mode)
446 http_close_server(c->server);
449 static void cmd_init(struct http_channel *c)
451 struct http_request *r = c->request;
452 const char *clear = http_argbyname(r, "clear");
453 const char *content_type = http_lookup_header(r->headers, "Content-Type");
455 struct http_session *s;
456 struct http_response *rs = c->response;
457 struct conf_service *service = 0; /* no service (yet) */
459 if (r->content_len && content_type &&
460 !yaz_strcmp_del("text/xml", content_type, "; "))
462 xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
466 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
469 root_n = xmlDocGetRootElement(doc);
470 service = service_create(c->server, root_n);
474 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
481 const char *service_name = http_argbyname(c->request, "service");
482 service = locate_service(c->server, service_name);
485 error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
489 sesid = make_sessionid();
490 s = http_session_create(service, c->http_sessions, sesid);
492 if (!clear || *clear == '0')
493 session_init_databases(s->psession);
495 session_log(s->psession, YLOG_LOG, "No databases preloaded");
497 if (process_settings(s->psession, c->request, c->response) < 0)
500 response_open_ok(c, "init");
501 wrbuf_printf(c->wrbuf, "<session>%d", sesid);
502 if (c->server->server_id)
504 wrbuf_puts(c->wrbuf, ".");
505 wrbuf_puts(c->wrbuf, c->server->server_id);
507 wrbuf_puts(c->wrbuf, "</session>"
508 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol>");
510 wrbuf_printf(c->wrbuf, "<keepAlive>%d</keepAlive>\n",
511 1000 * ((s->psession->service->session_timeout >= 20) ?
512 (s->psession->service->session_timeout - 10) : 50));
513 response_close(c, "init");
516 static void apply_local_setting(void *client_data,
519 struct session *se = (struct session *) client_data;
521 session_apply_setting(se, set->target, set->name, set->value);
524 static void cmd_settings(struct http_channel *c)
526 struct http_response *rs = c->response;
527 struct http_request *rq = c->request;
528 struct http_session *s = locate_session(c);
529 const char *content_type = http_lookup_header(rq->headers, "Content-Type");
534 if (rq->content_len && content_type &&
535 !yaz_strcmp_del("text/xml", content_type, "; "))
537 xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
542 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
543 release_session(c, s);
546 root_n = xmlDocGetRootElement(doc);
547 ret = settings_read_node_x(root_n, s->psession, apply_local_setting);
551 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
552 release_session(c, s);
556 if (process_settings(s->psession, rq, rs) < 0)
558 release_session(c, s);
561 response_open_ok(c, "settings");
562 response_close(c, "settings");
563 release_session(c, s);
566 static void termlist_response(struct http_channel *c, struct http_session *s,
567 const char *cmd_status)
569 struct http_request *rq = c->request;
570 const char *name = http_argbyname(rq, "name");
571 const char *nums = http_argbyname(rq, "num");
572 int version = get_version(rq);
579 status = session_active_clients(s->psession);
581 response_open_command(c, "termlist");
582 /* new protocol add a status to response. Triggered by a status parameter */
584 wrbuf_printf(c->wrbuf, "<status>%s</status>\n", cmd_status);
585 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
587 perform_termlist(c, s->psession, name, num, version);
589 response_close(c, "termlist");
592 static void termlist_result_ready(void *data)
594 struct http_channel *c = (struct http_channel *) data;
595 struct http_request *rq = c->request;
596 const char *report = http_argbyname(rq, "report");
597 const char *status = 0;
598 struct http_session *s = locate_session(c);
599 if (report && !strcmp("status", report))
603 session_log(s->psession, c->http_sessions->log_level,
604 "termlist watch released");
605 termlist_response(c, s, status);
606 release_session(c, s);
610 static void cmd_termlist(struct http_channel *c)
612 struct http_request *rq = c->request;
613 struct http_response *rs = c->response;
614 struct http_session *s = locate_session(c);
615 const char *block = http_argbyname(rq, "block");
616 const char *report = http_argbyname(rq, "report");
617 int report_status = 0;
618 int report_error = 0;
619 const char *status_message = 0;
622 if (report && !strcmp("error", report))
625 status_message = "OK";
627 if (report && !strcmp("status", report))
630 status_message = "OK";
635 active_clients = session_active_clients(s->psession);
636 if (block && !strcmp("1", block) && active_clients)
638 // if there is already a watch/block. we do not block this one
639 if (session_set_watch(s->psession, SESSION_WATCH_TERMLIST,
640 termlist_result_ready, c, c) != 0)
642 session_log(s->psession, YLOG_WARN, "Attempt to block "
643 "multiple times on termlist block. Not supported!");
645 error(rs, PAZPAR2_ALREADY_BLOCKED, "termlist");
646 release_session(c, s);
649 else if (report_status)
650 status_message = "WARNING (Already blocked on termlist)";
652 session_log(s->psession, YLOG_WARN,
653 "Ignoring termlist block. Return current result");
657 session_log(s->psession, c->http_sessions->log_level,
658 "Blocking on command termlist");
659 release_session(c, s);
664 termlist_response(c, s, status_message);
665 release_session(c, s);
668 size_t session_get_memory_status(struct session *session);
670 static void session_status(struct http_channel *c, struct http_session *s)
673 wrbuf_printf(c->wrbuf, "<http_count>%u</http_count>\n",
674 s->activity_counter);
675 wrbuf_printf(c->wrbuf, "<http_nmem>%zu</http_nmem>\n",
676 nmem_total(s->nmem) );
677 session_nmem = session_get_memory_status(s->psession);
678 wrbuf_printf(c->wrbuf, "<session_nmem>%zu</session_nmem>\n", session_nmem);
681 static void cmd_service(struct http_channel *c)
683 struct http_session *s = locate_session(c);
687 response_open_command(c, 0);
688 if (s->psession->service->xml_node)
689 wrbuf_puts(c->wrbuf, s->psession->service->xml_node);
690 response_close(c, 0);
691 release_session(c, s);
694 static void cmd_session_status(struct http_channel *c)
696 struct http_session *s = locate_session(c);
700 response_open_ok(c, "session-status");
701 session_status(c, s);
702 response_close(c, "session-status");
703 release_session(c, s);
706 static void bytarget_response(struct http_channel *c, struct http_session *s,
707 const char *cmd_status)
710 struct http_request *rq = c->request;
711 const char *settings = http_argbyname(rq, "settings");
712 int version = get_version(rq);
713 struct hitsbytarget *ht = get_hitsbytarget(s->psession, &count, c->nmem);
716 /* Old protocol, always ok */
717 response_open_ok(c, "bytarget");
720 /* New protocol, OK or WARNING (...)*/
721 response_open_command(c, "bytarget");
722 wrbuf_printf(c->wrbuf, "<status>%s</status>", cmd_status);
726 session_log(s->psession, YLOG_WARN,
727 "Empty bytarget Response. No targets found!");
728 for (i = 0; i < count; i++)
730 wrbuf_puts(c->wrbuf, "\n<target>");
732 wrbuf_puts(c->wrbuf, "<id>");
733 wrbuf_xmlputs(c->wrbuf, ht[i].id);
734 wrbuf_puts(c->wrbuf, "</id>\n");
736 if (ht[i].name && ht[i].name[0])
738 wrbuf_puts(c->wrbuf, "<name>");
739 wrbuf_xmlputs(c->wrbuf, ht[i].name);
740 wrbuf_puts(c->wrbuf, "</name>\n");
743 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
744 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n",
746 if (ht[i].diagnostic)
748 wrbuf_puts(c->wrbuf, "<message>");
749 wrbuf_xmlputs(c->wrbuf, ht[i].message);
750 wrbuf_puts(c->wrbuf, "</message>\n");
751 wrbuf_puts(c->wrbuf, "<addinfo>");
753 wrbuf_xmlputs(c->wrbuf, ht[i].addinfo);
754 wrbuf_puts(c->wrbuf, "</addinfo>\n");
757 wrbuf_printf(c->wrbuf, "<records>%d</records>\n",
758 ht[i].records - ht[i].filtered);
759 wrbuf_printf(c->wrbuf, "<filtered>%d</filtered>\n", ht[i].filtered);
762 wrbuf_printf(c->wrbuf, "<approximation>" ODR_INT_PRINTF
763 "</approximation>\n", ht[i].approximation);
765 wrbuf_puts(c->wrbuf, "<state>");
766 wrbuf_xmlputs(c->wrbuf, ht[i].state);
767 wrbuf_puts(c->wrbuf, "</state>\n");
768 if (settings && *settings == '1')
770 wrbuf_puts(c->wrbuf, "<settings>\n");
771 wrbuf_puts(c->wrbuf, ht[i].settings_xml);
772 wrbuf_puts(c->wrbuf, "</settings>\n");
774 if (ht[i].suggestions_xml && ht[i].suggestions_xml[0])
776 wrbuf_puts(c->wrbuf, "<suggestions>");
777 wrbuf_puts(c->wrbuf, ht[i].suggestions_xml);
778 wrbuf_puts(c->wrbuf, "</suggestions>");
780 if (ht[i].query_data)
782 wrbuf_puts(c->wrbuf, "<query_type>");
783 wrbuf_xmlputs(c->wrbuf, ht[i].query_type);
784 wrbuf_puts(c->wrbuf, "</query_type>\n");
785 wrbuf_puts(c->wrbuf, "<query_data>");
786 wrbuf_xmlputs(c->wrbuf, ht[i].query_data);
787 wrbuf_puts(c->wrbuf, "</query_data>\n");
789 wrbuf_puts(c->wrbuf, "</target>");
791 response_close(c, "bytarget");
794 static void bytarget_result_ready(void *data)
796 struct http_channel *c = (struct http_channel *) data;
797 struct http_session *s = locate_session(c);
798 const char *status_message = "OK";
801 session_log(s->psession, c->http_sessions->log_level,
802 "bytarget watch released");
803 bytarget_response(c, s, status_message);
804 release_session(c, s);
807 yaz_log(c->http_sessions->log_level,
808 "No Session found for released bytarget watch");
812 static void cmd_bytarget(struct http_channel *c)
814 struct http_request *rq = c->request;
815 struct http_response *rs = c->response;
816 struct http_session *s = locate_session(c);
817 const char *block = http_argbyname(rq, "block");
818 const char *report = http_argbyname(rq, "report");
819 int report_error = 0;
820 int report_status = 0;
821 const char *status_message = "OK";
824 if (report && !strcmp("error", report))
826 if (report && !strcmp("status", report))
832 no_active = session_active_clients(s->psession);
833 if (block && !strcmp("1", block) && no_active)
835 // if there is already a watch/block. we do not block this one
836 if (session_set_watch(s->psession, SESSION_WATCH_BYTARGET,
837 bytarget_result_ready, c, c) != 0)
839 session_log(s->psession, YLOG_WARN, "Attempt to block "
840 "multiple times on bytarget block. Not supported!");
843 error(rs, PAZPAR2_ALREADY_BLOCKED, "bytarget");
844 release_session(c, s);
847 else if (report_status)
848 status_message = "WARNING (Already blocked on bytarget)";
850 session_log(s->psession, YLOG_WARN, "Ignoring bytarget block."
851 " Return current result.");
855 session_log(s->psession, c->http_sessions->log_level,
856 "Blocking on command bytarget");
857 release_session(c, s);
861 bytarget_response(c, s, status_message);
862 release_session(c, s);
865 static void write_metadata(WRBUF w, struct conf_service *service,
866 struct record_metadata **ml, unsigned flags,
871 for (imeta = 0; imeta < service->num_metadata; imeta++)
873 struct conf_metadata *cmd = &service->metadata[imeta];
874 struct record_metadata *md;
875 if (!cmd->brief && !(flags & 1))
877 for (md = ml[imeta]; md; md = md->next)
879 struct record_metadata_attr *attr = md->attributes;
881 for (i = 0; i < indent; i++)
883 wrbuf_printf(w, "<md-%s", cmd->name);
885 for (; attr; attr = attr->next)
887 wrbuf_printf(w, " %s=\"", attr->name);
888 wrbuf_xmlputs(w, attr->value);
894 case Metadata_type_generic:
895 if (md->data.text.snippet && (flags & 2))
896 wrbuf_puts(w, md->data.text.snippet);
898 wrbuf_xmlputs(w, md->data.text.disp);
900 case Metadata_type_year:
901 wrbuf_printf(w, "%d", md->data.number.min);
902 if (md->data.number.min != md->data.number.max)
903 wrbuf_printf(w, "-%d", md->data.number.max);
905 case Metadata_type_float:
906 wrbuf_printf(w, "%f", md->data.fnumber);
909 wrbuf_puts(w, "[can't represent]");
912 wrbuf_printf(w, "</md-%s>\n", cmd->name);
917 static void write_subrecord(struct record *r, WRBUF w,
918 struct conf_service *service, unsigned flags,
921 const char *name = session_setting_oneval(
922 client_get_database(r->client), PZ_NAME);
924 wrbuf_puts(w, " <location id=\"");
925 wrbuf_xmlputs(w, client_get_id(r->client));
926 wrbuf_puts(w, "\"\n");
928 wrbuf_puts(w, " name=\"");
929 wrbuf_xmlputs(w, *name ? name : "Unknown");
930 wrbuf_puts(w, "\" ");
932 wrbuf_puts(w, "checksum=\"");
933 wrbuf_printf(w, "%u", r->checksum);
934 wrbuf_puts(w, "\">\n");
936 write_metadata(w, service, r->metadata, flags, indent);
937 wrbuf_puts(w, " </location>\n");
940 static void show_raw_record_error(void *data, const char *addinfo)
942 http_channel_observer_t obs = data;
943 struct http_channel *c = http_channel_observer_chan(obs);
944 struct http_response *rs = c->response;
946 http_remove_observer(obs);
948 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
951 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
953 http_channel_observer_t obs = data;
954 struct http_channel *c = http_channel_observer_chan(obs);
955 struct http_response *rs = c->response;
957 http_remove_observer(obs);
959 wrbuf_write(c->wrbuf, buf, sz);
960 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
961 http_send_response(c);
965 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
967 http_channel_observer_t obs = data;
968 struct http_channel *c = http_channel_observer_chan(obs);
969 struct http_response *rs = c->response;
971 http_remove_observer(obs);
973 wrbuf_write(c->wrbuf, buf, sz);
974 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
976 rs->content_type = "application/octet-stream";
977 http_send_response(c);
981 void show_raw_reset(void *data, struct http_channel *c, void *data2)
983 //struct client *client = data;
984 //client_show_raw_remove(client, data2);
987 static void cmd_record_ready(void *data);
989 static void show_record(struct http_channel *c, struct http_session *s)
991 struct http_response *rs = c->response;
992 struct http_request *rq = c->request;
993 struct record_cluster *rec, *prev_r, *next_r;
994 struct conf_service *service;
995 const char *idstr = http_argbyname(rq, "id");
996 const char *offsetstr = http_argbyname(rq, "offset");
997 const char *binarystr = http_argbyname(rq, "binary");
998 const char *checksumstr = http_argbyname(rq, "checksum");
999 const char *snippets = http_argbyname(rq, "snippets");
1000 unsigned flags = (snippets && *snippets == '1') ? 3 : 1;
1004 service = s->psession->service;
1007 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
1010 wrbuf_rewind(c->wrbuf);
1011 if (!(rec = show_single_start(s->psession, idstr, &prev_r, &next_r)))
1013 if (session_active_clients(s->psession) == 0)
1015 error(rs, PAZPAR2_RECORD_MISSING, idstr);
1017 else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
1018 cmd_record_ready, c, c) != 0)
1020 error(rs, PAZPAR2_RECORD_MISSING, idstr);
1024 if (offsetstr || checksumstr)
1026 const char *syntax = http_argbyname(rq, "syntax");
1027 const char *esn = http_argbyname(rq, "esn");
1029 struct record*r = rec->records;
1031 const char *nativesyntax = http_argbyname(rq, "nativesyntax");
1033 if (binarystr && *binarystr != '0')
1038 unsigned v = strtoul(checksumstr, 0, 10);
1039 for (i = 0; r; r = r->next)
1040 if (v == r->checksum)
1043 error(rs, PAZPAR2_RECORD_FAIL, "no record");
1047 int offset = atoi(offsetstr);
1048 for (i = 0; i < offset && r; r = r->next, i++)
1051 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
1055 http_channel_observer_t obs =
1056 http_add_observer(c, r->client, show_raw_reset);
1057 int ret = client_show_raw_begin(r->client, r->position,
1060 show_raw_record_error,
1062 show_raw_record_ok_binary :
1063 show_raw_record_ok),
1068 http_remove_observer(obs);
1069 error(rs, PAZPAR2_NO_SESSION, 0);
1076 response_open_command(c, "record");
1077 wrbuf_puts(c->wrbuf, "\n <recid>");
1078 wrbuf_xmlputs(c->wrbuf, rec->recid);
1079 wrbuf_puts(c->wrbuf, "</recid>\n");
1082 wrbuf_puts(c->wrbuf, " <prevrecid>");
1083 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
1084 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
1088 wrbuf_puts(c->wrbuf, " <nextrecid>");
1089 wrbuf_xmlputs(c->wrbuf, next_r->recid);
1090 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
1092 wrbuf_printf(c->wrbuf, " <activeclients>%d</activeclients>\n",
1093 session_active_clients(s->psession));
1094 write_metadata(c->wrbuf, service, rec->metadata, flags, 1);
1095 for (r = rec->records; r; r = r->next)
1096 write_subrecord(r, c->wrbuf, service, flags, 2);
1097 response_close(c, "record");
1099 show_single_stop(s->psession, rec);
1102 static void cmd_record_ready(void *data)
1104 struct http_channel *c = (struct http_channel *) data;
1105 struct http_session *s = locate_session(c);
1108 session_log(s->psession, c->http_sessions->log_level,
1109 "record watch released");
1111 release_session(c, s);
1115 static void cmd_record(struct http_channel *c)
1117 struct http_session *s = locate_session(c);
1121 release_session(c, s);
1126 static void show_records(struct http_channel *c, struct http_session *s,
1129 struct http_request *rq = c->request;
1130 struct http_response *rs = c->response;
1131 struct record_cluster **rl;
1132 struct reclist_sortparms *sp;
1133 const char *start = http_argbyname(rq, "start");
1134 const char *num = http_argbyname(rq, "num");
1135 const char *sort = http_argbyname(rq, "sort");
1136 int version = get_version(rq);
1137 const char *snippets = http_argbyname(rq, "snippets");
1138 unsigned flags = (snippets && *snippets == '1') ? 2 : 0;
1144 Odr_int approx_hits;
1146 struct conf_service *service = 0;
1150 // We haven't counted clients yet if we're called on a block release
1152 active = session_active_clients(s->psession);
1155 startn = atoi(start);
1159 service = s->psession->service;
1161 sort = service->default_sort;
1162 if (!(sp = reclist_parse_sortparms(c->nmem, sort, service)))
1164 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
1169 rl = show_range_start(s->psession, sp, startn, &numn, &total,
1170 &total_hits, &approx_hits, show_records_ready, c);
1174 response_open_ok(c, "show");
1175 wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>\n", active);
1176 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
1177 wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
1179 wrbuf_printf(c->wrbuf, "<approximation>" ODR_INT_PRINTF
1180 "</approximation>\n", approx_hits);
1182 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
1183 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
1185 for (i = 0; i < numn; i++)
1189 struct record_cluster *rec = rl[i];
1190 struct conf_service *service = s->psession->service;
1192 wrbuf_puts(c->wrbuf, "<hit>\n");
1193 write_metadata(c->wrbuf, service, rec->metadata, flags, 1);
1194 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
1195 write_subrecord(p, c->wrbuf, service, flags, 2);
1196 wrbuf_printf(c->wrbuf, " <count>%d</count>\n", ccount);
1197 if (strstr(sort, "relevance"))
1199 wrbuf_printf(c->wrbuf, " <relevance>%d</relevance>\n",
1200 rec->relevance_score);
1201 if (service->rank_debug)
1203 wrbuf_printf(c->wrbuf, " <relevance_info>\n");
1204 wrbuf_xmlputs(c->wrbuf, wrbuf_cstr(rec->relevance_explain1));
1205 wrbuf_xmlputs(c->wrbuf, wrbuf_cstr(rec->relevance_explain2));
1206 wrbuf_printf(c->wrbuf, " </relevance_info>\n");
1209 wrbuf_puts(c->wrbuf, " <recid>");
1210 wrbuf_xmlputs(c->wrbuf, rec->recid);
1211 wrbuf_puts(c->wrbuf, "</recid>\n");
1212 wrbuf_puts(c->wrbuf, "</hit>\n");
1215 show_range_stop(s->psession, rl);
1217 response_close(c, "show");
1220 static void show_records_ready(void *data)
1222 struct http_channel *c = (struct http_channel *) data;
1223 struct http_session *s = locate_session(c);
1226 session_log(s->psession, c->http_sessions->log_level,
1227 "show watch released");
1228 show_records(c, s, -1);
1231 /* some error message */
1233 release_session(c, s);
1236 static void cmd_show(struct http_channel *c)
1238 struct http_request *rq = c->request;
1239 struct http_response *rs = c->response;
1240 struct http_session *s = locate_session(c);
1241 const char *block = http_argbyname(rq, "block");
1242 const char *sort = http_argbyname(rq, "sort");
1243 const char *block_error = http_argbyname(rq, "report");
1244 const char *mergekey = http_argbyname(rq, "mergekey");
1245 const char *rank = http_argbyname(rq, "rank");
1246 struct conf_service *service = 0;
1247 struct reclist_sortparms *sp;
1249 int report_error = 0;
1251 if (block_error && !strcmp("1", block_error))
1255 service = s->psession->service;
1257 sort = service->default_sort;
1258 if (!(sp = reclist_parse_sortparms(c->nmem, sort, service)))
1260 error(c->response, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
1261 release_session(c, s);
1264 session_sort(s->psession, sp, mergekey, rank);
1266 status = session_active_clients(s->psession);
1268 if (block && reclist_get_num_records(s->psession->reclist) == 0)
1270 if (!strcmp(block, "preferred")
1271 && !session_is_preferred_clients_ready(s->psession)
1272 && reclist_get_num_records(s->psession->reclist) == 0)
1274 // if there is already a watch/block. we do not block this one
1275 if (session_set_watch(s->psession, SESSION_WATCH_SHOW_PREF,
1276 show_records_ready, c, c) == 0)
1278 session_log(s->psession, c->http_sessions->log_level,
1279 "Blocking on command show (preferred targets)");
1280 release_session(c, s);
1285 session_log(s->psession, YLOG_WARN, "Attempt to block"
1286 " multiple times on show (preferred targets) block."
1290 error(rs, PAZPAR2_ALREADY_BLOCKED,
1291 "show (preferred targets)");
1292 release_session(c, s);
1296 session_log(s->psession, YLOG_WARN,
1297 "Ignoring show(preferred) block."
1298 " Returning current result");
1304 // if there is already a watch/block. we do not block this one
1305 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
1306 show_records_ready, c, c) != 0)
1308 session_log(s->psession, YLOG_WARN, "Attempt to block"
1309 " multiple times on show block. Not supported!");
1312 error(rs, PAZPAR2_ALREADY_BLOCKED, "show");
1313 release_session(c, s);
1317 session_log(s->psession, YLOG_WARN, "Ignoring show block."
1318 " Returning current result");
1322 session_log(s->psession, c->http_sessions->log_level,
1323 "Blocking on command show");
1324 release_session(c, s);
1329 show_records(c, s, status);
1330 release_session(c, s);
1333 static void cmd_ping(struct http_channel *c)
1335 struct http_session *s = locate_session(c);
1338 response_open_ok(c, "ping");
1339 response_close(c, "ping");
1340 release_session(c, s);
1343 static void cmd_search(struct http_channel *c)
1345 struct http_request *rq = c->request;
1346 struct http_response *rs = c->response;
1347 struct http_session *s = locate_session(c);
1348 const char *query = http_argbyname(rq, "query");
1349 const char *filter = http_argbyname(rq, "filter");
1350 const char *maxrecs = http_argbyname(rq, "maxrecs");
1351 const char *startrecs = http_argbyname(rq, "startrecs");
1352 const char *limit = http_argbyname(rq, "limit");
1353 const char *sort = http_argbyname(rq, "sort");
1354 const char *mergekey = http_argbyname(rq, "mergekey");
1355 const char *rank = http_argbyname(rq, "rank");
1356 enum pazpar2_error_code code;
1357 const char *addinfo = 0;
1358 const char *addinfo2 = 0;
1359 struct reclist_sortparms *sp;
1360 struct conf_service *service = 0;
1367 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
1368 release_session(c, s);
1371 if (!yaz_utf8_check(query))
1373 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
1374 release_session(c, s);
1377 service = s->psession->service;
1379 sort = service->default_sort;
1381 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
1383 error(c->response, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
1384 release_session(c, s);
1388 code = session_search(s->psession, query, startrecs, maxrecs, filter, limit,
1389 &addinfo, &addinfo2, sp, mergekey, rank);
1392 error2(rs, code, addinfo, addinfo2);
1393 release_session(c, s);
1396 response_open_ok(c, "search");
1397 response_close(c, "search");
1398 release_session(c, s);
1402 static void cmd_stat(struct http_channel *c)
1404 struct http_session *s = locate_session(c);
1405 struct statistics stat;
1413 clients = session_active_clients(s->psession);
1414 statistics(s->psession, &stat);
1416 if (stat.num_clients > 0)
1418 progress = (stat.num_clients - clients) / (float)stat.num_clients;
1421 response_open_command(c, "stat");
1422 wrbuf_printf(c->wrbuf, "\n <activeclients>%d</activeclients>\n", clients);
1423 wrbuf_printf(c->wrbuf, " <hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
1424 wrbuf_printf(c->wrbuf, " <records>%d</records>\n", stat.num_records);
1425 wrbuf_printf(c->wrbuf, " <clients>%d</clients>\n", stat.num_clients);
1426 wrbuf_printf(c->wrbuf, " <unconnected>%d</unconnected>\n", stat.num_no_connection);
1427 wrbuf_printf(c->wrbuf, " <connecting>%d</connecting>\n", stat.num_connecting);
1428 wrbuf_printf(c->wrbuf, " <working>%d</working>\n", stat.num_working);
1429 wrbuf_printf(c->wrbuf, " <idle>%d</idle>\n", stat.num_idle);
1430 wrbuf_printf(c->wrbuf, " <failed>%d</failed>\n", stat.num_failed);
1431 wrbuf_printf(c->wrbuf, " <error>%d</error>\n", stat.num_error);
1432 wrbuf_printf(c->wrbuf, " <progress>%.2f</progress>\n", progress);
1433 response_close(c, "stat");
1434 release_session(c, s);
1437 static void cmd_stop(struct http_channel *c)
1439 struct http_session *s = locate_session(c);
1442 response_open_ok(c, "stop");
1443 session_stop(s->psession);
1444 response_close(c, "stop");
1445 release_session(c, s);
1448 static void cmd_info(struct http_channel *c)
1450 char yaz_version_str[20];
1451 char yaz_sha1_str[42];
1453 response_open_command(c, "info");
1454 wrbuf_puts(c->wrbuf, "\n <version>\n");
1455 wrbuf_puts(c->wrbuf, " <pazpar2");
1456 #ifdef PAZPAR2_VERSION_SHA1
1457 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
1459 wrbuf_puts(c->wrbuf, ">");
1460 wrbuf_xmlputs(c->wrbuf, VERSION);
1461 wrbuf_puts(c->wrbuf, "</pazpar2>\n");
1463 yaz_version(yaz_version_str, yaz_sha1_str);
1464 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
1465 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
1466 wrbuf_puts(c->wrbuf, "\" sha1=\"");
1467 wrbuf_xmlputs(c->wrbuf, yaz_sha1_str);
1468 wrbuf_puts(c->wrbuf, "\">");
1469 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
1470 wrbuf_puts(c->wrbuf, "</yaz>\n");
1472 wrbuf_puts(c->wrbuf, " </version>\n");
1475 char hostname_str[64];
1476 if (gethostname(hostname_str, sizeof(hostname_str)) == 0)
1478 wrbuf_puts(c->wrbuf, " <host>");
1479 wrbuf_xmlputs(c->wrbuf, hostname_str);
1480 wrbuf_puts(c->wrbuf, "</host>\n");
1484 wrbuf_printf(c->wrbuf, " <sessions>%d</sessions>\n", sessions_get_count());
1485 wrbuf_printf(c->wrbuf, " <clients>%d</clients>\n", clients_get_count());
1486 print_meminfo(c->wrbuf);
1487 info_services(c->server, c->wrbuf);
1489 response_close(c, "info");
1494 void (*fun)(struct http_channel *c);
1496 { "init", cmd_init },
1497 { "settings", cmd_settings },
1498 { "stat", cmd_stat },
1499 { "bytarget", cmd_bytarget },
1500 { "show", cmd_show },
1501 { "search", cmd_search },
1502 { "termlist", cmd_termlist },
1503 { "exit", cmd_exit },
1504 { "session-status", cmd_session_status },
1505 { "service", cmd_service },
1506 { "ping", cmd_ping },
1507 { "record", cmd_record },
1508 { "info", cmd_info },
1509 { "stop", cmd_stop },
1513 void http_command(struct http_channel *c)
1515 const char *command = http_argbyname(c->request, "command");
1516 struct http_response *rs = http_create_response(c);
1521 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1522 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1526 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1529 for (i = 0; commands[i].name; i++)
1530 if (!strcmp(commands[i].name, command))
1532 (*commands[i].fun)(c);
1535 if (!commands[i].name)
1536 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1544 * c-file-style: "Stroustrup"
1545 * indent-tabs-mode: nil
1547 * vim: shiftwidth=4 tabstop=8 expandtab