-/* $Id: http_command.c,v 1.50 2007-06-12 09:26:40 adam Exp $
+/* $Id: http_command.c,v 1.52 2007-06-13 13:04:34 adam Exp $
Copyright (c) 2006-2007, Index Data.
This file is part of Pazpar2.
*/
/*
- * $Id: http_command.c,v 1.50 2007-06-12 09:26:40 adam Exp $
+ * $Id: http_command.c,v 1.52 2007-06-13 13:04:34 adam Exp $
*/
#include <stdio.h>
};
static struct http_session *session_list = 0;
-
void http_session_destroy(struct http_session *s);
static void session_timeout(IOCHAN i, int event)
*p = (*p)->next;
break;
}
+ yaz_log(YLOG_LOG, "Destroying session %u", s->session_id);
iochan_destroy(s->timeout_iochan);
destroy_session(s->psession);
nmem_destroy(s->nmem);
}
+static const char *get_msg(enum pazpar2_error_code code)
+{
+ struct pazpar2_error_msg {
+ enum pazpar2_error_code code;
+ const char *msg;
+ };
+ static const struct pazpar2_error_msg ar[] = {
+ { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
+ { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
+ { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
+ { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
+ { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
+ { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
+ { PAZPAR2_RECORD_MISSING, "Record missing"},
+ { PAZPAR2_NO_TARGETS, "No targets"},
+ { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
+ { 0, 0 }
+ };
+ int i = 0;
+ while (ar[i].msg)
+ {
+ if (code == ar[i].code)
+ return ar[i].msg;
+ i++;
+ }
+ return "No error";
+}
+
static void error(struct http_response *rs,
- const char *code, const char *msg, const char *extra)
+ enum pazpar2_error_code code,
+ const char *addinfo)
{
struct http_channel *c = rs->channel;
char text[1024];
- char *sep = extra ? ": " : "";
+ const char *http_status = "417";
+ const char *msg = get_msg(code);
rs->msg = nmem_strdup(c->nmem, msg);
- strcpy(rs->code, code);
+ strcpy(rs->code, http_status);
yaz_snprintf(text, sizeof(text),
- "<error code=\"general\">%s%s%s</error>", msg, sep,
- extra ? extra : "");
+ "<error code=\"%d\" msg=\"%s\">%s</error>", (int) code,
+ msg, addinfo ? addinfo : "");
- yaz_log(YLOG_WARN, "HTTP %s %s%s%s", code, msg, sep,
- extra ? extra : "");
+ yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
+ msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
rs->payload = nmem_strdup(c->nmem, text);
http_send_response(c);
}
if (!session)
{
- error(rs, "417", "Must supply session", 0);
+ error(rs, PAZPAR2_MISSING_PARAMETER, "session");
return 0;
}
id = atoi(session);
iochan_activity(p->timeout_iochan);
return p;
}
- error(rs, "417", "Session does not exist, or it has expired", 0);
+ error(rs, PAZPAR2_NO_SESSION, session);
return 0;
}
nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
if (num != 2)
{
- error(rs, "417", "Malformed setting argument", a->name);
+ error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
return -1;
}
setting = res[0];
return;
if (!(ht = hitsbytarget(s->psession, &count)))
{
- error(rs, "500", "Failed to retrieve hitcounts", 0);
+ error(rs, PAZPAR2_HITCOUNTS_FAILED, 0);
return;
}
wrbuf_rewind(c->wrbuf);
return;
if (!idstr)
{
- error(rs, "417", "Must supply id", 0);
+ error(rs, PAZPAR2_MISSING_PARAMETER, "id");
return;
}
wrbuf_rewind(c->wrbuf);
id = atoi(idstr);
if (!(rec = show_single(s->psession, id)))
{
- error(rs, "500", "Record missing", 0);
+ error(rs, PAZPAR2_RECORD_MISSING, idstr);
return;
}
wrbuf_puts(c->wrbuf, "<record>\n");
sort = "relevance";
if (!(sp = reclist_parse_sortparms(c->nmem, sort)))
{
- error(rs, "500", "Bad sort parameters", 0);
+ error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
return;
}
struct http_session *s = locate_session(rq, rs);
char *query = http_argbyname(rq, "query");
char *filter = http_argbyname(rq, "filter");
- char *res;
+ enum pazpar2_error_code code;
+ const char *addinfo = 0;
if (!s)
return;
if (!query)
{
- error(rs, "417", "Must supply query", 0);
+ error(rs, PAZPAR2_MISSING_PARAMETER, "query");
return;
}
if (!utf_8_valid(query))
{
- error(rs, "417", "Query not UTF-8 encoded", 0);
+ error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
return;
}
- res = search(s->psession, query, filter);
- if (res)
+ code = search(s->psession, query, filter, &addinfo);
+ if (code)
{
- error(rs, "417", res, 0);
+ error(rs, code, addinfo);
return;
}
rs->payload = "<search><status>OK</status></search>";
if (!command)
{
- error(rs, "417", "Must supply command", 0);
+ error(rs, PAZPAR2_MISSING_PARAMETER, "command");
return;
}
for (i = 0; commands[i].name; i++)
break;
}
if (!commands[i].name)
- error(rs, "417", "Unknown command", command);
+ error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
return;
}