-/* $Id: http_command.c,v 1.50 2007-06-12 09:26:40 adam Exp $
+/* $Id: http_command.c,v 1.51 2007-06-13 08:04:03 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.51 2007-06-13 08:04:03 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)
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;
}
-/* $Id: logic.c,v 1.40 2007-06-11 12:08:23 adam Exp $
+/* $Id: logic.c,v 1.41 2007-06-13 08:04:03 adam Exp $
Copyright (c) 2006-2007, Index Data.
This file is part of Pazpar2.
return res;
}
-char *search(struct session *se, char *query, char *filter)
+enum pazpar2_error_code search(struct session *se,
+ char *query, char *filter,
+ const char **addinfo)
{
int live_channels = 0;
struct client *cl;
yaz_log(YLOG_DEBUG, "Search");
+ *addinfo = 0;
nmem_reset(se->nmem);
criteria = parse_filter(se->nmem, filter);
se->requestid++;
se->expected_maxrecs = maxrecs;
}
else
- return "NOTARGETS";
+ return PAZPAR2_NO_TARGETS;
se->relevance = 0;
for (cl = se->clients; cl; cl = client_next_in_session(cl))
{
if (prepare_session_database(se, client_get_database(cl)) < 0)
- return "CONFIG_ERROR";
+ {
+ *addinfo = client_get_database(cl)->database->url;
+ return PAZPAR2_CONFIG_TARGET;
+ }
// Query must parse for all targets
- if (client_parse_query(cl, query) < 0)
- return "QUERY";
+ if (client_parse_query(cl, query) < 0)
+ {
+ *addinfo = "query";
+ return PAZPAR2_MALFORMED_PARAMETER_VALUE;
+ }
}
for (cl = se->clients; cl; cl = client_next_in_session(cl))
client_prep_connection(cl);
- return 0;
+ return PAZPAR2_NO_ERROR;
}
// Creates a new session_database object for a database
-/* $Id: pazpar2.h,v 1.39 2007-06-12 13:02:38 adam Exp $
+/* $Id: pazpar2.h,v 1.40 2007-06-13 08:04:03 adam Exp $
Copyright (c) 2006-2007, Index Data.
This file is part of Pazpar2.
struct record;
struct client;
+
+enum pazpar2_error_code {
+ PAZPAR2_NO_ERROR = 0,
+
+ PAZPAR2_NO_SESSION,
+ PAZPAR2_MISSING_PARAMETER,
+ PAZPAR2_MALFORMED_PARAMETER_VALUE,
+ PAZPAR2_MALFORMED_PARAMETER_ENCODING,
+ PAZPAR2_MALFORMED_SETTING,
+ PAZPAR2_HITCOUNTS_FAILED,
+ PAZPAR2_RECORD_MISSING,
+ PAZPAR2_NO_TARGETS,
+ PAZPAR2_CONFIG_TARGET
+};
+
// Represents a (virtual) database on a host
struct database {
struct host *host;
void destroy_session(struct session *s);
int load_targets(struct session *s, const char *fn);
void statistics(struct session *s, struct statistics *stat);
-char *search(struct session *s, char *query, char *filter);
+enum pazpar2_error_code search(struct session *s, char *query,
+ char *filter, const char **addinfo);
struct record_cluster **show(struct session *s, struct reclist_sortparms *sp, int start,
int *num, int *total, int *sumhits, NMEM nmem_show);
struct record_cluster *show_single(struct session *s, int id);
void (*work)(void *data), void *data,
const char *pidfile, const char *uid);
+
#endif
/*