X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;f=src%2Fhttp_command.c;h=7c23404aac6f1ab81f1d4c2fa48ac3aae583b44e;hb=cea5d06318c86c9652cad5c4fefa39fa2e9745ac;hp=84fac1282eafcf88b76b7c9e2fcba4770ba9ffc3;hpb=27b510020a28c83feb607998371d824284fc24de;p=pazpar2-moved-to-github.git
diff --git a/src/http_command.c b/src/http_command.c
index 84fac12..7c23404 100644
--- a/src/http_command.c
+++ b/src/http_command.c
@@ -1,5 +1,5 @@
/* This file is part of Pazpar2.
- Copyright (C) 2006-2012 Index Data
+ Copyright (C) Index Data
Pazpar2 is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
@@ -49,16 +49,16 @@ void print_meminfo(WRBUF wrbuf)
struct mallinfo minfo;
minfo = mallinfo();
wrbuf_printf(wrbuf, " \n"
- " %d\n"
+ " %d\n"
" %d\n"
" %d\n"
" %d\n"
" %d\n"
- " %d\n"
+ " %d\n"
" %d\n"
" %d\n"
" %d\n"
- " \n",
+ " \n",
minfo.arena, minfo.uordblks, minfo.fordblks,minfo.ordblks, minfo.keepcost, minfo.hblks, minfo.hblkhd, minfo.arena + minfo.hblkhd, minfo.uordblks + minfo.hblkhd);
}
@@ -93,6 +93,8 @@ struct http_sessions {
static YAZ_MUTEX g_http_session_mutex = 0;
static int g_http_sessions = 0;
+static void show_records_ready(void *data);
+
int get_version(struct http_request *rq) {
const char *version = http_argbyname(rq, "version");
int version_no = 0;
@@ -112,7 +114,8 @@ int http_session_use(int delta)
g_http_sessions += delta;
sessions = g_http_sessions;
yaz_mutex_leave(g_http_session_mutex);
- yaz_log(YLOG_DEBUG, "%s sessions=%d", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), sessions);
+ yaz_log(YLOG_DEBUG, "%s http_sessions=%d", delta == 0 ? "" :
+ (delta > 0 ? "INC" : "DEC"), sessions);
return sessions;
}
@@ -174,9 +177,13 @@ struct http_session *http_session_create(struct conf_service *service,
http_sessions->session_list = r;
yaz_mutex_leave(http_sessions->mutex);
- r->timeout_iochan = iochan_create(-1, session_timeout, 0, "http_session_timeout");
+ r->timeout_iochan = iochan_create(-1, session_timeout, 0,
+ "http_session_timeout");
iochan_setdata(r->timeout_iochan, r);
- yaz_log(http_sessions->log_level, "Session %u created. timeout chan=%p timeout=%d", sesid, r->timeout_iochan, service->session_timeout);
+
+ session_log(r->psession, http_sessions->log_level,
+ "HTTP session create. timeout chan=%p ses=%d",
+ r->timeout_iochan, service->session_timeout);
iochan_settimeout(r->timeout_iochan, service->session_timeout);
iochan_add(service->server->iochan_man, r->timeout_iochan);
@@ -190,7 +197,8 @@ void http_session_destroy(struct http_session *s)
http_sessions_t http_sessions = s->http_sessions;
- yaz_log(http_sessions->log_level, "Session %u destroy", s->session_id);
+ session_log(s->psession, http_sessions->log_level,
+ "HTTP session destroy");
yaz_mutex_enter(http_sessions->mutex);
/* only if http_session has no active http sessions on it can be destroyed */
if (s->destroy_counter == s->activity_counter)
@@ -207,15 +215,17 @@ void http_session_destroy(struct http_session *s)
yaz_mutex_leave(http_sessions->mutex);
if (must_destroy)
{ /* destroying for real */
- yaz_log(http_sessions->log_level, "Session %u destroyed", s->session_id);
+ session_log(s->psession, http_sessions->log_level, "About to destroyd");
iochan_destroy(s->timeout_iochan);
session_destroy(s->psession);
http_session_use(-1);
nmem_destroy(s->nmem);
}
- else {
- yaz_log(http_sessions->log_level, "Session %u destroying delayed. Active clients (%d-%d). Waiting for new timeout.",
- s->session_id, s->activity_counter, s->destroy_counter);
+ else
+ {
+ session_log(s->psession, http_sessions->log_level,
+ "Destroy delayed. Active clients (%d-%d)",
+ s->activity_counter, s->destroy_counter);
}
}
@@ -253,22 +263,29 @@ static const char *get_msg(enum pazpar2_error_code code)
return "No error";
}
-static void error(struct http_response *rs,
- enum pazpar2_error_code code,
- const char *addinfo)
+static void error2(struct http_response *rs,
+ enum pazpar2_error_code code,
+ const char *addinfo, const char *addinfo2)
{
struct http_channel *c = rs->channel;
WRBUF text = wrbuf_alloc();
const char *http_status = "417";
const char *msg = get_msg(code);
-
+
rs->msg = nmem_strdup(c->nmem, msg);
strcpy(rs->code, http_status);
- wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "", (int) code,
- msg);
+ wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX
+ "", (int) code, msg);
if (addinfo)
+ {
wrbuf_xmlputs(text, addinfo);
+ if (addinfo2)
+ {
+ wrbuf_xmlputs(text, ": ");
+ wrbuf_xmlputs(text, addinfo2);
+ }
+ }
wrbuf_puts(text, "");
yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
@@ -278,16 +295,24 @@ static void error(struct http_response *rs,
http_send_response(c);
}
-static void response_open_no_status(struct http_channel *c, const char *command)
+static void error(struct http_response *rs,
+ enum pazpar2_error_code code,
+ const char *addinfo)
+{
+ error2(rs, code, addinfo, 0);
+}
+
+static void response_open_command(struct http_channel *c, const char *command)
{
wrbuf_rewind(c->wrbuf);
- wrbuf_printf(c->wrbuf, "%s<%s>",
- HTTP_COMMAND_RESPONSE_PREFIX, command);
+ wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX);
+ if (command)
+ wrbuf_printf(c->wrbuf, "<%s>", command);
}
-static void response_open(struct http_channel *c, const char *command)
+static void response_open_ok(struct http_channel *c, const char *command)
{
- response_open_no_status(c, command);
+ response_open_command(c, command);
wrbuf_puts(c->wrbuf, "OK");
}
@@ -295,7 +320,8 @@ static void response_close(struct http_channel *c, const char *command)
{
struct http_response *rs = c->response;
- wrbuf_printf(c->wrbuf, "%s>", command);
+ if (command)
+ wrbuf_printf(c->wrbuf, "%s>", command);
rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
http_send_response(c);
}
@@ -320,7 +346,7 @@ unsigned int make_sessionid(void)
yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
exit(1);
}
- /* at most 256 sessions per second ..
+ /* at most 256 sessions per second ..
(long long would be more appropriate)*/
res = t.tv_sec;
res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
@@ -375,6 +401,7 @@ static int process_settings(struct session *se, struct http_request *rq,
struct http_response *rs)
{
struct http_argument *a;
+ NMEM nmem = nmem_create();
for (a = rq->arguments; a; a = a->next)
if (strchr(a->name, '['))
@@ -385,17 +412,18 @@ static int process_settings(struct session *se, struct http_request *rq,
char *setting;
// Nmem_strsplit *rules*!!!
- nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
+ nmem_strsplit(nmem, "[]", a->name, &res, &num);
if (num != 2)
{
error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
+ nmem_destroy(nmem);
return -1;
}
setting = res[0];
dbname = res[1];
- session_apply_setting(se, dbname, setting,
- nmem_strdup(se->session_nmem, a->value));
+ session_apply_setting(se, dbname, setting, a->value);
}
+ nmem_destroy(nmem);
return 0;
}
@@ -403,9 +431,10 @@ static void cmd_exit(struct http_channel *c)
{
yaz_log(YLOG_WARN, "exit");
- response_open(c, "exit");
+ response_open_ok(c, "exit");
response_close(c, "exit");
- http_close_server(c->server);
+ if (global_parameters.debug_mode)
+ http_close_server(c->server);
}
static void cmd_init(struct http_channel *c)
@@ -417,8 +446,8 @@ static void cmd_init(struct http_channel *c)
struct http_session *s;
struct http_response *rs = c->response;
struct conf_service *service = 0; /* no service (yet) */
-
- if (r->content_len && content_type &&
+
+ if (r->content_len && content_type &&
!yaz_strcmp_del("text/xml", content_type, "; "))
{
xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
@@ -437,7 +466,7 @@ static void cmd_init(struct http_channel *c)
return;
}
}
-
+
if (!service)
{
const char *service_name = http_argbyname(c->request, "service");
@@ -450,17 +479,16 @@ static void cmd_init(struct http_channel *c)
}
sesid = make_sessionid();
s = http_session_create(service, c->http_sessions, sesid);
-
- yaz_log(c->http_sessions->log_level, "Session init %u ", sesid);
+
if (!clear || *clear == '0')
session_init_databases(s->psession);
else
- yaz_log(YLOG_LOG, "Session %u init: No databases preloaded", sesid);
-
+ session_log(s->psession, YLOG_LOG, "No databases preloaded");
+
if (process_settings(s->psession, c->request, c->response) < 0)
return;
- response_open(c, "init");
+ response_open_ok(c, "init");
wrbuf_printf(c->wrbuf, "%d", sesid);
if (c->server->server_id)
{
@@ -469,9 +497,10 @@ static void cmd_init(struct http_channel *c)
}
wrbuf_puts(c->wrbuf, ""
"" PAZPAR2_PROTOCOL_VERSION "");
-
- wrbuf_printf(c->wrbuf, "%d\n", 1000 * ((s->psession->service->session_timeout >= 20) ?
- (s->psession->service->session_timeout - 10) : 50));
+
+ wrbuf_printf(c->wrbuf, "%d\n",
+ 1000 * ((s->psession->service->session_timeout >= 20) ?
+ (s->psession->service->session_timeout - 10) : 50));
response_close(c, "init");
}
@@ -480,9 +509,7 @@ static void apply_local_setting(void *client_data,
{
struct session *se = (struct session *) client_data;
- session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
- nmem_strdup(se->session_nmem, set->name),
- nmem_strdup(se->session_nmem, set->value));
+ session_apply_setting(se, set->target, set->name, set->value);
}
static void cmd_settings(struct http_channel *c)
@@ -495,7 +522,7 @@ static void cmd_settings(struct http_channel *c)
if (!s)
return;
- if (rq->content_len && content_type &&
+ if (rq->content_len && content_type &&
!yaz_strcmp_del("text/xml", content_type, "; "))
{
xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
@@ -504,7 +531,7 @@ static void cmd_settings(struct http_channel *c)
if (!doc)
{
error(rs, PAZPAR2_MALFORMED_SETTING, 0);
- release_session(c,s);
+ release_session(c, s);
return;
}
root_n = xmlDocGetRootElement(doc);
@@ -513,21 +540,22 @@ static void cmd_settings(struct http_channel *c)
if (ret)
{
error(rs, PAZPAR2_MALFORMED_SETTING, 0);
- release_session(c,s);
+ release_session(c, s);
return;
- }
+ }
}
if (process_settings(s->psession, rq, rs) < 0)
{
release_session(c, s);
return;
}
- response_open(c, "settings");
+ response_open_ok(c, "settings");
response_close(c, "settings");
release_session(c, s);
}
-static void termlist_response(struct http_channel *c, struct http_session *s, const char *cmd_status)
+static void termlist_response(struct http_channel *c, struct http_session *s,
+ const char *cmd_status)
{
struct http_request *rq = c->request;
const char *name = http_argbyname(rq, "name");
@@ -541,11 +569,10 @@ static void termlist_response(struct http_channel *c, struct http_session *s, co
status = session_active_clients(s->psession);
- response_open_no_status(c, "termlist");
+ response_open_command(c, "termlist");
/* new protocol add a status to response. Triggered by a status parameter */
- if (cmd_status != 0) {
+ if (cmd_status != 0)
wrbuf_printf(c->wrbuf, "%s\n", cmd_status);
- }
wrbuf_printf(c->wrbuf, "%d\n", status);
perform_termlist(c, s->psession, name, num, version);
@@ -562,10 +589,12 @@ static void termlist_result_ready(void *data)
struct http_session *s = locate_session(c);
if (report && !strcmp("status", report))
status = "OK";
- if (s) {
- yaz_log(c->http_sessions->log_level, "Session %u termlist watch released", s->session_id);
+ if (s)
+ {
+ session_log(s->psession, c->http_sessions->log_level,
+ "termlist watch released");
termlist_response(c, s, status);
- release_session(c,s);
+ release_session(c, s);
}
}
@@ -580,11 +609,14 @@ static void cmd_termlist(struct http_channel *c)
int report_error = 0;
const char *status_message = 0;
int active_clients;
- if (report && !strcmp("error", report)) {
+
+ if (report && !strcmp("error", report))
+ {
report_error = 1;
status_message = "OK";
}
- if (report && !strcmp("status", report)) {
+ if (report && !strcmp("status", report))
+ {
report_status = 1;
status_message = "OK";
}
@@ -598,22 +630,23 @@ static void cmd_termlist(struct http_channel *c)
if (session_set_watch(s->psession, SESSION_WATCH_TERMLIST,
termlist_result_ready, c, c) != 0)
{
- yaz_log(YLOG_WARN, "Session %u: Attempt to block multiple times on termlist block. Not supported!", s->session_id);
+ session_log(s->psession, YLOG_WARN, "Attempt to block "
+ "multiple times on termlist block. Not supported!");
if (report_error) {
error(rs, PAZPAR2_ALREADY_BLOCKED, "termlist");
release_session(c, s);
return;
}
- else if (report_status) {
+ else if (report_status)
status_message = "WARNING (Already blocked on termlist)";
- }
- else {
- yaz_log(YLOG_WARN, "Session %u: Ignoring termlist block. Return current result", s->session_id);
- }
+ else
+ session_log(s->psession, YLOG_WARN,
+ "Ignoring termlist block. Return current result");
}
else
{
- yaz_log(c->http_sessions->log_level, "Session %u: Blocking on command termlist", s->session_id);
+ session_log(s->psession, c->http_sessions->log_level,
+ "Blocking on command termlist");
release_session(c, s);
return;
}
@@ -628,26 +661,39 @@ size_t session_get_memory_status(struct session *session);
static void session_status(struct http_channel *c, struct http_session *s)
{
size_t session_nmem;
- wrbuf_printf(c->wrbuf, "%u\n", s->activity_counter);
- wrbuf_printf(c->wrbuf, "%zu\n", nmem_total(s->nmem) );
+ wrbuf_printf(c->wrbuf, "%u\n",
+ s->activity_counter);
+ wrbuf_printf(c->wrbuf, "%zu\n",
+ nmem_total(s->nmem) );
session_nmem = session_get_memory_status(s->psession);
wrbuf_printf(c->wrbuf, "%zu\n", session_nmem);
}
+static void cmd_service(struct http_channel *c)
+{
+ struct http_session *s = locate_session(c);
+ if (!s)
+ return;
+
+ response_open_command(c, 0);
+ if (s->psession->service->xml_node)
+ wrbuf_puts(c->wrbuf, s->psession->service->xml_node);
+ response_close(c, 0);
+ release_session(c, s);
+}
+
static void cmd_session_status(struct http_channel *c)
{
struct http_session *s = locate_session(c);
if (!s)
return;
- response_open(c, "session-status");
+ response_open_ok(c, "session-status");
session_status(c, s);
response_close(c, "session-status");
release_session(c, s);
}
-int sessions_count(void);
-int clients_count(void);
#ifdef HAVE_RESULTSETS_COUNT
int resultsets_count(void);
#else
@@ -660,7 +706,7 @@ static void cmd_server_status(struct http_channel *c)
int clients = clients_count();
int resultsets = resultsets_count();
- response_open(c, "server-status");
+ response_open_ok(c, "server-status");
wrbuf_printf(c->wrbuf, "\n %u\n", sessions);
wrbuf_printf(c->wrbuf, " %u\n", clients);
/* Only works if yaz has been compiled with enabling of this */
@@ -689,24 +735,28 @@ static void cmd_server_status(struct http_channel *c)
xmalloc_trav(0);
}
-static void bytarget_response(struct http_channel *c, struct http_session *s, const char *cmd_status) {
+static void bytarget_response(struct http_channel *c, struct http_session *s,
+ const char *cmd_status)
+{
int count, i;
- struct hitsbytarget *ht;
struct http_request *rq = c->request;
const char *settings = http_argbyname(rq, "settings");
int version = get_version(rq);
- ht = get_hitsbytarget(s->psession, &count, c->nmem);
+ struct hitsbytarget *ht = get_hitsbytarget(s->psession, &count, c->nmem);
+
if (!cmd_status)
/* Old protocol, always ok */
- response_open(c, "bytarget");
- else {
+ response_open_ok(c, "bytarget");
+ else
+ {
/* New protocol, OK or WARNING (...)*/
- response_open_no_status(c, "bytarget");
+ response_open_command(c, "bytarget");
wrbuf_printf(c->wrbuf, "%s", cmd_status);
}
if (count == 0)
- yaz_log(YLOG_WARN, "Empty bytarget Response. No targets found!");
+ session_log(s->psession, YLOG_WARN,
+ "Empty bytarget Response. No targets found!");
for (i = 0; i < count; i++)
{
wrbuf_puts(c->wrbuf, "\n");
@@ -715,7 +765,7 @@ static void bytarget_response(struct http_channel *c, struct http_session *s, co
wrbuf_xmlputs(c->wrbuf, ht[i].id);
wrbuf_puts(c->wrbuf, "\n");
- if (ht[i].name && ht[i].name[0])
+ if (ht[i].name && ht[i].name[0])
{
wrbuf_puts(c->wrbuf, "");
wrbuf_xmlputs(c->wrbuf, ht[i].name);
@@ -723,19 +773,26 @@ static void bytarget_response(struct http_channel *c, struct http_session *s, co
}
wrbuf_printf(c->wrbuf, "" ODR_INT_PRINTF "\n", ht[i].hits);
- wrbuf_printf(c->wrbuf, "%d\n", ht[i].diagnostic);
+ wrbuf_printf(c->wrbuf, "%d\n",
+ ht[i].diagnostic);
if (ht[i].diagnostic)
{
+ wrbuf_puts(c->wrbuf, "");
+ wrbuf_xmlputs(c->wrbuf, ht[i].message);
+ wrbuf_puts(c->wrbuf, "\n");
wrbuf_puts(c->wrbuf, "");
if (ht[i].addinfo)
wrbuf_xmlputs(c->wrbuf, ht[i].addinfo);
wrbuf_puts(c->wrbuf, "\n");
}
- wrbuf_printf(c->wrbuf, "%d\n", ht[i].records - ht[i].filtered);
- if (version >= 2) {
- wrbuf_printf(c->wrbuf, "%d\n", ht[i].filtered);
- wrbuf_printf(c->wrbuf, "" ODR_INT_PRINTF "\n", ht[i].approximation);
+ wrbuf_printf(c->wrbuf, "%d\n",
+ ht[i].records - ht[i].filtered);
+ wrbuf_printf(c->wrbuf, "%d\n", ht[i].filtered);
+ if (version >= 2)
+ {
+ wrbuf_printf(c->wrbuf, "" ODR_INT_PRINTF
+ "\n", ht[i].approximation);
}
wrbuf_puts(c->wrbuf, "");
wrbuf_xmlputs(c->wrbuf, ht[i].state);
@@ -746,7 +803,8 @@ static void bytarget_response(struct http_channel *c, struct http_session *s, co
wrbuf_puts(c->wrbuf, ht[i].settings_xml);
wrbuf_puts(c->wrbuf, "\n");
}
- if (ht[i].suggestions_xml && ht[i].suggestions_xml[0]) {
+ if (ht[i].suggestions_xml && ht[i].suggestions_xml[0])
+ {
wrbuf_puts(c->wrbuf, "");
wrbuf_puts(c->wrbuf, ht[i].suggestions_xml);
wrbuf_puts(c->wrbuf, "");
@@ -761,14 +819,16 @@ static void bytarget_result_ready(void *data)
struct http_channel *c = (struct http_channel *) data;
struct http_session *s = locate_session(c);
const char *status_message = "OK";
- if (s) {
- yaz_log(c->http_sessions->log_level, "Session %u: bytarget watch released", s->session_id);
+ if (s)
+ {
+ session_log(s->psession, c->http_sessions->log_level,
+ "bytarget watch released");
bytarget_response(c, s, status_message);
release_session(c, s);
}
- else {
- yaz_log(c->http_sessions->log_level, "No Session found for released bytarget watch");
- }
+ else
+ yaz_log(c->http_sessions->log_level,
+ "No Session found for released bytarget watch");
}
@@ -784,39 +844,39 @@ static void cmd_bytarget(struct http_channel *c)
const char *status_message = "OK";
int no_active;
- if (report && !strcmp("error", report)) {
+ if (report && !strcmp("error", report))
report_error = 1;
- }
- if (report && !strcmp("status", report)) {
+ if (report && !strcmp("status", report))
report_status = 1;
- }
if (!s)
return;
no_active = session_active_clients(s->psession);
- if (block && !strcmp("1",block) && no_active)
+ if (block && !strcmp("1", block) && no_active)
{
// if there is already a watch/block. we do not block this one
if (session_set_watch(s->psession, SESSION_WATCH_BYTARGET,
bytarget_result_ready, c, c) != 0)
{
- yaz_log(YLOG_WARN, "Session %u: Attempt to block multiple times on bytarget block. Not supported!", s->session_id);
- if (report_error) {
+ session_log(s->psession, YLOG_WARN, "Attempt to block "
+ "multiple times on bytarget block. Not supported!");
+ if (report_error)
+ {
error(rs, PAZPAR2_ALREADY_BLOCKED, "bytarget");
release_session(c, s);
return;
}
- else if (report_status) {
+ else if (report_status)
status_message = "WARNING (Already blocked on bytarget)";
- }
- else {
- yaz_log(YLOG_WARN, "Session %u: Ignoring bytarget block. Return current result.", s->session_id);
- }
+ else
+ session_log(s->psession, YLOG_WARN, "Ignoring bytarget block."
+ " Return current result.");
}
else
{
- yaz_log(c->http_sessions->log_level, "Session %u: Blocking on command bytarget", s->session_id);
+ session_log(s->psession, c->http_sessions->log_level,
+ "Blocking on command bytarget");
release_session(c, s);
return;
}
@@ -826,7 +886,8 @@ static void cmd_bytarget(struct http_channel *c)
}
static void write_metadata(WRBUF w, struct conf_service *service,
- struct record_metadata **ml, int full)
+ struct record_metadata **ml, unsigned flags,
+ int indent)
{
int imeta;
@@ -834,12 +895,15 @@ static void write_metadata(WRBUF w, struct conf_service *service,
{
struct conf_metadata *cmd = &service->metadata[imeta];
struct record_metadata *md;
- if (!cmd->brief && !full)
+ if (!cmd->brief && !(flags & 1))
continue;
for (md = ml[imeta]; md; md = md->next)
{
struct record_metadata_attr *attr = md->attributes;
- wrbuf_printf(w, "\nname);
+ int i;
+ for (i = 0; i < indent; i++)
+ wrbuf_putc(w, ' ');
+ wrbuf_printf(w, "name);
for (; attr; attr = attr->next)
{
@@ -851,38 +915,49 @@ static void write_metadata(WRBUF w, struct conf_service *service,
switch (cmd->type)
{
case Metadata_type_generic:
- wrbuf_xmlputs(w, md->data.text.disp);
+ if (md->data.text.snippet && (flags & 2))
+ wrbuf_puts(w, md->data.text.snippet);
+ else
+ wrbuf_xmlputs(w, md->data.text.disp);
break;
case Metadata_type_year:
wrbuf_printf(w, "%d", md->data.number.min);
if (md->data.number.min != md->data.number.max)
wrbuf_printf(w, "-%d", md->data.number.max);
break;
+ case Metadata_type_float:
+ wrbuf_printf(w, "%f", md->data.fnumber);
+ break;
default:
wrbuf_puts(w, "[can't represent]");
break;
}
- wrbuf_printf(w, "", cmd->name);
+ wrbuf_printf(w, "\n", cmd->name);
}
}
}
static void write_subrecord(struct record *r, WRBUF w,
- struct conf_service *service, int show_details)
+ struct conf_service *service, unsigned flags,
+ int indent)
{
const char *name = session_setting_oneval(
client_get_database(r->client), PZ_NAME);
- wrbuf_puts(w, "client));
- wrbuf_puts(w, "\" ");
+ wrbuf_puts(w, "\"\n");
- wrbuf_puts(w, "name=\"");
+ wrbuf_puts(w, " name=\"");
wrbuf_xmlputs(w, *name ? name : "Unknown");
- wrbuf_puts(w, "\">");
+ wrbuf_puts(w, "\" ");
- write_metadata(w, service, r->metadata, show_details);
- wrbuf_puts(w, "\n");
+ wrbuf_puts(w, "checksum=\"");
+ wrbuf_printf(w, "%u", r->checksum);
+ wrbuf_puts(w, "\">\n");
+
+ write_metadata(w, service, r->metadata, flags, indent);
+ wrbuf_puts(w, " \n");
}
static void show_raw_record_error(void *data, const char *addinfo)
@@ -939,12 +1014,14 @@ static void show_record(struct http_channel *c, struct http_session *s)
struct http_response *rs = c->response;
struct http_request *rq = c->request;
struct record_cluster *rec, *prev_r, *next_r;
- struct record *r;
struct conf_service *service;
const char *idstr = http_argbyname(rq, "id");
const char *offsetstr = http_argbyname(rq, "offset");
const char *binarystr = http_argbyname(rq, "binary");
-
+ const char *checksumstr = http_argbyname(rq, "checksum");
+ const char *snippets = http_argbyname(rq, "snippets");
+ unsigned flags = (snippets && *snippets == '1') ? 3 : 1;
+
if (!s)
return;
service = s->psession->service;
@@ -967,35 +1044,45 @@ static void show_record(struct http_channel *c, struct http_session *s)
}
return;
}
- if (offsetstr)
+ if (offsetstr || checksumstr)
{
- int offset = atoi(offsetstr);
const char *syntax = http_argbyname(rq, "syntax");
const char *esn = http_argbyname(rq, "esn");
int i;
struct record*r = rec->records;
int binary = 0;
const char *nativesyntax = http_argbyname(rq, "nativesyntax");
-
+
if (binarystr && *binarystr != '0')
binary = 1;
- for (i = 0; i < offset && r; r = r->next, i++)
- ;
- if (!r)
+ if (checksumstr)
{
- error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
+ unsigned v = strtoul(checksumstr, 0, 10);
+ for (i = 0; r; r = r->next)
+ if (v == r->checksum)
+ break;
+ if (!r)
+ error(rs, PAZPAR2_RECORD_FAIL, "no record");
}
else
{
+ int offset = atoi(offsetstr);
+ for (i = 0; i < offset && r; r = r->next, i++)
+ ;
+ if (!r)
+ error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
+ }
+ if (r)
+ {
http_channel_observer_t obs =
http_add_observer(c, r->client, show_raw_reset);
int ret = client_show_raw_begin(r->client, r->position,
syntax, esn,
obs /* data */,
show_raw_record_error,
- (binary ?
- show_raw_record_ok_binary :
+ (binary ?
+ show_raw_record_ok_binary :
show_raw_record_ok),
(binary ? 1 : 0),
nativesyntax);
@@ -1008,27 +1095,28 @@ static void show_record(struct http_channel *c, struct http_session *s)
}
else
{
- response_open_no_status(c, "record");
- wrbuf_puts(c->wrbuf, "\n");
+ struct record *r;
+ response_open_command(c, "record");
+ wrbuf_puts(c->wrbuf, "\n ");
wrbuf_xmlputs(c->wrbuf, rec->recid);
wrbuf_puts(c->wrbuf, "\n");
if (prev_r)
{
- wrbuf_puts(c->wrbuf, "");
+ wrbuf_puts(c->wrbuf, " ");
wrbuf_xmlputs(c->wrbuf, prev_r->recid);
wrbuf_puts(c->wrbuf, "\n");
}
if (next_r)
{
- wrbuf_puts(c->wrbuf, "");
+ wrbuf_puts(c->wrbuf, " ");
wrbuf_xmlputs(c->wrbuf, next_r->recid);
wrbuf_puts(c->wrbuf, "\n");
}
- wrbuf_printf(c->wrbuf, "%d\n",
+ wrbuf_printf(c->wrbuf, " %d\n",
session_active_clients(s->psession));
- write_metadata(c->wrbuf, service, rec->metadata, 1);
+ write_metadata(c->wrbuf, service, rec->metadata, flags, 1);
for (r = rec->records; r; r = r->next)
- write_subrecord(r, c->wrbuf, service, 1);
+ write_subrecord(r, c->wrbuf, service, flags, 2);
response_close(c, "record");
}
show_single_stop(s->psession, rec);
@@ -1038,24 +1126,28 @@ static void cmd_record_ready(void *data)
{
struct http_channel *c = (struct http_channel *) data;
struct http_session *s = locate_session(c);
- if (s) {
- yaz_log(c->http_sessions->log_level, "Session %u: record watch released", s->session_id);
+ if (s)
+ {
+ session_log(s->psession, c->http_sessions->log_level,
+ "record watch released");
show_record(c, s);
- release_session(c,s);
+ release_session(c, s);
}
}
static void cmd_record(struct http_channel *c)
{
struct http_session *s = locate_session(c);
- if (s) {
+ if (s)
+ {
show_record(c, s);
- release_session(c,s);
+ release_session(c, s);
}
}
-static void show_records(struct http_channel *c, struct http_session *s, int active)
+static void show_records(struct http_channel *c, struct http_session *s,
+ int active)
{
struct http_request *rq = c->request;
struct http_response *rs = c->response;
@@ -1065,6 +1157,8 @@ static void show_records(struct http_channel *c, struct http_session *s, int act
const char *num = http_argbyname(rq, "num");
const char *sort = http_argbyname(rq, "sort");
int version = get_version(rq);
+ const char *snippets = http_argbyname(rq, "snippets");
+ unsigned flags = (snippets && *snippets == '1') ? 2 : 0;
int startn = 0;
int numn = 20;
@@ -1072,7 +1166,7 @@ static void show_records(struct http_channel *c, struct http_session *s, int act
Odr_int total_hits;
Odr_int approx_hits;
int i;
-
+ struct conf_service *service = 0;
if (!s)
return;
@@ -1084,24 +1178,30 @@ static void show_records(struct http_channel *c, struct http_session *s, int act
startn = atoi(start);
if (num)
numn = atoi(num);
+
+ service = s->psession->service;
if (!sort)
- sort = "relevance";
- if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
+ sort = service->default_sort;
+ if (!(sp = reclist_parse_sortparms(c->nmem, sort, service)))
{
error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
return;
}
-
- rl = show_range_start(s->psession, sp, startn, &numn, &total, &total_hits, &approx_hits);
- response_open(c, "show");
+ rl = show_range_start(s->psession, sp, startn, &numn, &total,
+ &total_hits, &approx_hits, show_records_ready, c);
+ if (!rl)
+ return;
+
+ response_open_ok(c, "show");
wrbuf_printf(c->wrbuf, "\n%d\n", active);
wrbuf_printf(c->wrbuf, "%d\n", total);
wrbuf_printf(c->wrbuf, "" ODR_INT_PRINTF "\n", total_hits);
- if (version >= 2) {
- wrbuf_printf(c->wrbuf, "" ODR_INT_PRINTF "\n", approx_hits);
- }
+ if (version >= 2)
+ wrbuf_printf(c->wrbuf, "" ODR_INT_PRINTF
+ "\n", approx_hits);
+
wrbuf_printf(c->wrbuf, "%d\n", startn);
wrbuf_printf(c->wrbuf, "%d\n", numn);
@@ -1113,15 +1213,23 @@ static void show_records(struct http_channel *c, struct http_session *s, int act
struct conf_service *service = s->psession->service;
wrbuf_puts(c->wrbuf, "\n");
- write_metadata(c->wrbuf, service, rec->metadata, 0);
+ write_metadata(c->wrbuf, service, rec->metadata, flags, 1);
for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
- write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
- if (ccount > 1)
- wrbuf_printf(c->wrbuf, "%d\n", ccount);
+ write_subrecord(p, c->wrbuf, service, flags, 2);
+ wrbuf_printf(c->wrbuf, " %d\n", ccount);
if (strstr(sort, "relevance"))
- wrbuf_printf(c->wrbuf, "%d\n",
+ {
+ wrbuf_printf(c->wrbuf, " %d\n",
rec->relevance_score);
- wrbuf_puts(c->wrbuf, "");
+ if (service->rank_debug)
+ {
+ wrbuf_printf(c->wrbuf, " \n");
+ wrbuf_xmlputs(c->wrbuf, wrbuf_cstr(rec->relevance_explain1));
+ wrbuf_xmlputs(c->wrbuf, wrbuf_cstr(rec->relevance_explain2));
+ wrbuf_printf(c->wrbuf, " \n");
+ }
+ }
+ wrbuf_puts(c->wrbuf, " ");
wrbuf_xmlputs(c->wrbuf, rec->recid);
wrbuf_puts(c->wrbuf, "\n");
wrbuf_puts(c->wrbuf, "\n");
@@ -1136,14 +1244,16 @@ static void show_records_ready(void *data)
{
struct http_channel *c = (struct http_channel *) data;
struct http_session *s = locate_session(c);
- if (s) {
- yaz_log(c->http_sessions->log_level, "Session %u: show watch released", s->session_id);
+ if (s)
+ {
+ session_log(s->psession, c->http_sessions->log_level,
+ "show watch released");
show_records(c, s, -1);
}
else {
/* some error message */
}
- release_session(c,s);
+ release_session(c, s);
}
static void cmd_show(struct http_channel *c)
@@ -1154,54 +1264,61 @@ static void cmd_show(struct http_channel *c)
const char *block = http_argbyname(rq, "block");
const char *sort = http_argbyname(rq, "sort");
const char *block_error = http_argbyname(rq, "report");
-
+ const char *mergekey = http_argbyname(rq, "mergekey");
+ const char *rank = http_argbyname(rq, "rank");
+ struct conf_service *service = 0;
struct reclist_sortparms *sp;
int status;
int report_error = 0;
- if (block_error && !strcmp("1", block_error)) {
+
+ if (block_error && !strcmp("1", block_error))
report_error = 1;
- }
if (!s)
return;
-
+ service = s->psession->service;
if (!sort)
- sort = "relevance";
-
- if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
+ sort = service->default_sort;
+ if (!(sp = reclist_parse_sortparms(c->nmem, sort, service)))
{
error(c->response, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
release_session(c, s);
return;
}
- session_sort(s->psession, sp->name, sp->increasing);
+ session_sort(s->psession, sp, mergekey, rank);
status = session_active_clients(s->psession);
- if (block)
+ if (block && reclist_get_num_records(s->psession->reclist) == 0)
{
- if (!strcmp(block, "preferred") && !session_is_preferred_clients_ready(s->psession) && reclist_get_num_records(s->psession->reclist) == 0)
+ if (!strcmp(block, "preferred")
+ && !session_is_preferred_clients_ready(s->psession)
+ && reclist_get_num_records(s->psession->reclist) == 0)
{
// if there is already a watch/block. we do not block this one
if (session_set_watch(s->psession, SESSION_WATCH_SHOW_PREF,
show_records_ready, c, c) == 0)
{
- yaz_log(c->http_sessions->log_level,
- "Session %u: Blocking on command show (preferred targets)", s->session_id);
+ session_log(s->psession, c->http_sessions->log_level,
+ "Blocking on command show (preferred targets)");
release_session(c, s);
return;
}
else
{
- yaz_log(YLOG_WARN, "Session %u: Attempt to block multiple times on show (preferred targets) block. Not supported!",
- s->session_id);
- if (report_error) {
- error(rs, PAZPAR2_ALREADY_BLOCKED, "show (preferred targets)");
+ session_log(s->psession, YLOG_WARN, "Attempt to block"
+ " multiple times on show (preferred targets) block."
+ " Not supported!");
+ if (report_error)
+ {
+ error(rs, PAZPAR2_ALREADY_BLOCKED,
+ "show (preferred targets)");
release_session(c, s);
return;
}
- else {
- yaz_log(YLOG_WARN, "Session %u: Ignoring show(preferred) block. Returning current result.", s->session_id);
- }
+ else
+ session_log(s->psession, YLOG_WARN,
+ "Ignoring show(preferred) block."
+ " Returning current result");
}
}
@@ -1211,19 +1328,22 @@ static void cmd_show(struct http_channel *c)
if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
show_records_ready, c, c) != 0)
{
- yaz_log(YLOG_WARN, "Session %u: Attempt to block multiple times on show block. Not supported!", s->session_id);
- if (report_error) {
+ session_log(s->psession, YLOG_WARN, "Attempt to block"
+ " multiple times on show block. Not supported!");
+ if (report_error)
+ {
error(rs, PAZPAR2_ALREADY_BLOCKED, "show");
release_session(c, s);
return;
}
- else {
- yaz_log(YLOG_WARN, "Session %u: Ignoring show block. Returning current result.", s->session_id);
- }
+ else
+ session_log(s->psession, YLOG_WARN, "Ignoring show block."
+ " Returning current result");
}
else
{
- yaz_log(c->http_sessions->log_level, "Session %u: Blocking on command show", s->session_id);
+ session_log(s->psession, c->http_sessions->log_level,
+ "Blocking on command show");
release_session(c, s);
return;
}
@@ -1238,7 +1358,7 @@ static void cmd_ping(struct http_channel *c)
struct http_session *s = locate_session(c);
if (!s)
return;
- response_open(c, "ping");
+ response_open_ok(c, "ping");
response_close(c, "ping");
release_session(c, s);
}
@@ -1253,11 +1373,18 @@ static void cmd_search(struct http_channel *c)
const char *maxrecs = http_argbyname(rq, "maxrecs");
const char *startrecs = http_argbyname(rq, "startrecs");
const char *limit = http_argbyname(rq, "limit");
+ const char *sort = http_argbyname(rq, "sort");
+ const char *mergekey = http_argbyname(rq, "mergekey");
+ const char *rank = http_argbyname(rq, "rank");
enum pazpar2_error_code code;
const char *addinfo = 0;
+ const char *addinfo2 = 0;
+ struct reclist_sortparms *sp;
+ struct conf_service *service = 0;
if (!s)
return;
+
if (!query)
{
error(rs, PAZPAR2_MISSING_PARAMETER, "query");
@@ -1270,15 +1397,26 @@ static void cmd_search(struct http_channel *c)
release_session(c, s);
return;
}
+ service = s->psession->service;
+ if (!sort)
+ sort = service->default_sort;
+
+ if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
+ {
+ error(c->response, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
+ release_session(c, s);
+ return;
+ }
+
code = session_search(s->psession, query, startrecs, maxrecs, filter, limit,
- &addinfo, "relevance", 0);
+ &addinfo, &addinfo2, sp, mergekey, rank);
if (code)
{
- error(rs, code, addinfo);
+ error2(rs, code, addinfo, addinfo2);
release_session(c, s);
return;
}
- response_open(c, "search");
+ response_open_ok(c, "search");
response_close(c, "search");
release_session(c, s);
}
@@ -1303,45 +1441,69 @@ static void cmd_stat(struct http_channel *c)
progress = (stat.num_clients - clients) / (float)stat.num_clients;
}
- response_open_no_status(c, "stat");
- wrbuf_printf(c->wrbuf, "%d\n", clients);
- wrbuf_printf(c->wrbuf, "" ODR_INT_PRINTF "\n", stat.num_hits);
- wrbuf_printf(c->wrbuf, "%d\n", stat.num_records);
- wrbuf_printf(c->wrbuf, "%d\n", stat.num_clients);
- wrbuf_printf(c->wrbuf, "%d\n", stat.num_no_connection);
- wrbuf_printf(c->wrbuf, "%d\n", stat.num_connecting);
- wrbuf_printf(c->wrbuf, "%d\n", stat.num_working);
- wrbuf_printf(c->wrbuf, "%d\n", stat.num_idle);
- wrbuf_printf(c->wrbuf, "%d\n", stat.num_failed);
- wrbuf_printf(c->wrbuf, "%d\n", stat.num_error);
- wrbuf_printf(c->wrbuf, "\n", progress);
+ response_open_command(c, "stat");
+ wrbuf_printf(c->wrbuf, "\n %d\n", clients);
+ wrbuf_printf(c->wrbuf, " " ODR_INT_PRINTF "\n", stat.num_hits);
+ wrbuf_printf(c->wrbuf, " %d\n", stat.num_records);
+ wrbuf_printf(c->wrbuf, " %d\n", stat.num_clients);
+ wrbuf_printf(c->wrbuf, " %d\n", stat.num_no_connection);
+ wrbuf_printf(c->wrbuf, " %d\n", stat.num_connecting);
+ wrbuf_printf(c->wrbuf, " %d\n", stat.num_working);
+ wrbuf_printf(c->wrbuf, " %d\n", stat.num_idle);
+ wrbuf_printf(c->wrbuf, " %d\n", stat.num_failed);
+ wrbuf_printf(c->wrbuf, " %d\n", stat.num_error);
+ wrbuf_printf(c->wrbuf, " \n", progress);
response_close(c, "stat");
release_session(c, s);
}
+static void cmd_stop(struct http_channel *c)
+{
+ struct http_session *s = locate_session(c);
+ if (!s)
+ return;
+ response_open_ok(c, "stop");
+ session_stop(s->psession);
+ response_close(c, "stop");
+ release_session(c, s);
+}
+
static void cmd_info(struct http_channel *c)
{
char yaz_version_str[20];
+ char yaz_sha1_str[42];
- response_open_no_status(c, "info");
- wrbuf_puts(c->wrbuf, " \n");
+ response_open_command(c, "info");
+ wrbuf_puts(c->wrbuf, "\n \n");
wrbuf_puts(c->wrbuf, " wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
#endif
wrbuf_puts(c->wrbuf, ">");
wrbuf_xmlputs(c->wrbuf, VERSION);
- wrbuf_puts(c->wrbuf, "");
+ wrbuf_puts(c->wrbuf, "\n");
- yaz_version(yaz_version_str, 0);
+ yaz_version(yaz_version_str, yaz_sha1_str);
wrbuf_puts(c->wrbuf, " wrbuf, YAZ_VERSION);
+ wrbuf_puts(c->wrbuf, "\" sha1=\"");
+ wrbuf_xmlputs(c->wrbuf, yaz_sha1_str);
wrbuf_puts(c->wrbuf, "\">");
wrbuf_xmlputs(c->wrbuf, yaz_version_str);
wrbuf_puts(c->wrbuf, "\n");
wrbuf_puts(c->wrbuf, " \n");
-
+#if HAVE_UNISTD_H
+ {
+ char hostname_str[64];
+ if (gethostname(hostname_str, sizeof(hostname_str)) == 0)
+ {
+ wrbuf_puts(c->wrbuf, " ");
+ wrbuf_xmlputs(c->wrbuf, hostname_str);
+ wrbuf_puts(c->wrbuf, "\n");
+ }
+ }
+#endif
info_services(c->server, c->wrbuf);
response_close(c, "info");
@@ -1361,9 +1523,11 @@ struct {
{ "exit", cmd_exit },
{ "session-status", cmd_session_status },
{ "server-status", cmd_server_status },
+ { "service", cmd_service },
{ "ping", cmd_ping },
{ "record", cmd_record },
{ "info", cmd_info },
+ { "stop", cmd_stop },
{0,0}
};