X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fhttp_command.c;h=d4970a45c76e12610767a05127897dc071ec4453;hb=fc794a5a4623194e7e5cff1ea63ecdc80575312e;hp=a4f7405b5905d7294ab80389761af815921a77e7;hpb=44981e9f52420f6a42bb1ac55712f74a31d17020;p=pazpar2-moved-to-github.git diff --git a/src/http_command.c b/src/http_command.c index a4f7405..d4970a4 100644 --- a/src/http_command.c +++ b/src/http_command.c @@ -229,6 +229,7 @@ static const char *get_msg(enum pazpar2_error_code code) { PAZPAR2_RECORD_FAIL, "Record command failed"}, { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"}, { PAZPAR2_NO_SERVICE, "No service"}, + { PAZPAR2_ALREADY_BLOCKED, "Already blocked in session on: "}, { PAZPAR2_LAST_ERROR, "Last error"}, { 0, 0 } }; @@ -320,8 +321,8 @@ unsigned int make_sessionid(void) static struct http_session *locate_session(struct http_channel *c) { - struct http_response *rs = c->response; struct http_request *rq = c->request; + struct http_response *rs = c->response; struct http_session *p; const char *session = http_argbyname(rq, "session"); http_sessions_t http_sessions = c->http_sessions; @@ -508,7 +509,7 @@ static void cmd_settings(struct http_channel *c) release_session(c, s); } -static void cmd_termlist(struct http_channel *c) +static void termlist_response(struct http_channel *c) { struct http_request *rq = c->request; struct http_session *s = locate_session(c); @@ -517,14 +518,11 @@ static void cmd_termlist(struct http_channel *c) int num = 15; int status; - if (!s) - return; - - status = session_active_clients(s->psession); - if (nums) num = atoi(nums); + status = session_active_clients(s->psession); + response_open_no_status(c, "termlist"); wrbuf_printf(c->wrbuf, "%d\n", status); @@ -534,6 +532,45 @@ static void cmd_termlist(struct http_channel *c) release_session(c, s); } +static void termlist_result_ready(void *data) +{ + struct http_channel *c = (struct http_channel) data; + termlist_response(c); +} + +static void cmd_termlist(struct http_channel *c) +{ + struct http_request *rq = c->request; + struct http_response *rs = c->response; + struct http_session *s = locate_session(c); + const char *block = http_argbyname(rq, "block"); + int active_clients; + if (!s) + return; + + active_clients = session_active_clients(s->psession); + + if (block && !strcmp("1", block) && active_clients) + { + // if there is already a watch/block. we do not block this one + if (session_set_watch(s->psession, SESSION_WATCH_TERMLIST, + termlist_result_ready, c, c) != 0) + { + yaz_log(YLOG_WARN, "Attempt to block multiple times on termlist block. Not supported!"); + error(rs, PAZPAR2_ALREADY_BLOCKED, "termlist"); + } + else + { + yaz_log(c->http_sessions->log_level, "%p Session %u: Blocking on command termlist", s, s->session_id); + } + release_session(c, s); + return; + } + + termlist_response(c); + release_session(c, s); +} + size_t session_get_memory_status(struct session *session); static void session_status(struct http_channel *c, struct http_session *s) @@ -600,20 +637,17 @@ static void cmd_server_status(struct http_channel *c) xmalloc_trav(0); } - -static void cmd_bytarget(struct http_channel *c) -{ - struct http_request *rq = c->request; - struct http_session *s = locate_session(c); +static void bytarget_response(struct http_channel *c) { + int count, i; struct hitsbytarget *ht; + struct http_request *rq = c->request; const char *settings = http_argbyname(rq, "settings"); - int count, i; + struct http_session *s = locate_session(c); - if (!s) - return; ht = get_hitsbytarget(s->psession, &count, c->nmem); response_open(c, "bytarget"); - + if (count == 0) + yaz_log(YLOG_WARN, "Empty bytarget Response. No targets found!"); for (i = 0; i < count; i++) { wrbuf_puts(c->wrbuf, "\n"); @@ -642,12 +676,58 @@ static void cmd_bytarget(struct http_channel *c) wrbuf_puts(c->wrbuf, ht[i].settings_xml); wrbuf_puts(c->wrbuf, "\n"); } + 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, ""); + } wrbuf_puts(c->wrbuf, ""); } response_close(c, "bytarget"); release_session(c, s); } +static void bytarget_result_ready(void *data) +{ + struct http_channel *c = (struct http_channel *) data; + yaz_log(c->http_sessions->log_level, "bytarget watch released"); + bytarget_response(c); +} + + +static void cmd_bytarget(struct http_channel *c) +{ + struct http_request *rq = c->request; + struct http_response *rs = c->response; + struct http_session *s = locate_session(c); + const char *block = http_argbyname(rq, "block"); + int no_active; + + if (!s) + return; + + no_active = session_active_clients(s->psession); + + 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, "Attempt to block multiple times on bytarget block. Not supported!"); + error(rs, PAZPAR2_ALREADY_BLOCKED, "bytarget"); + } + else + { + yaz_log(c->http_sessions->log_level, "%p Session %u: Blocking on command bytarget", s, s->session_id); + } + release_session(c, s); + return; + } + bytarget_response(c); + release_session(c, s); +} + static void write_metadata(WRBUF w, struct conf_service *service, struct record_metadata **ml, int full) { @@ -696,7 +776,7 @@ static void write_subrecord(struct record *r, WRBUF w, client_get_database(r->client), PZ_NAME); wrbuf_puts(w, "client)); + wrbuf_xmlputs(w, client_get_id(r->client)); wrbuf_puts(w, "\" "); wrbuf_puts(w, "name=\""); @@ -898,6 +978,7 @@ static void show_records(struct http_channel *c, int active) error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort"); release_session(c, s); return; + } @@ -947,14 +1028,28 @@ static void show_records_ready(void *data) static void cmd_show(struct http_channel *c) { - struct http_request *rq = c->request; + struct http_request *rq = c->request; + struct http_response *rs = c->response; struct http_session *s = locate_session(c); const char *block = http_argbyname(rq, "block"); + const char *sort = http_argbyname(rq, "sort"); + struct reclist_sortparms *sp; int status; if (!s) return; + if (!sort) + sort = "relevance"; + + if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service))) + { + error(c->response, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort"); + release_session(c, s); + return; + } + session_sort(s->psession, sp->name, sp->increasing); + status = session_active_clients(s->psession); if (block) @@ -963,22 +1058,32 @@ static void cmd_show(struct http_channel *c) { // 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) + show_records_ready, c, c) == 0) { yaz_log(c->http_sessions->log_level, - "%p Session %u: Blocking on cmd_show. Waiting for preferred targets", s, s->session_id); + "%p Session %u: Blocking on command show (preferred targets)", s, s->session_id); + } + else + { + yaz_log(YLOG_WARN, "Attempt to block multiple times on show (preferred targets) block. Not supported!"); + error(rs, PAZPAR2_ALREADY_BLOCKED, "show (preferred targets)"); } release_session(c, s); return; } - else if (status && reclist_get_num_records(s->psession->reclist) == 0) + else if (status) { // if there is already a watch/block. we do not block this one if (session_set_watch(s->psession, SESSION_WATCH_SHOW, show_records_ready, c, c) != 0) { - yaz_log(c->http_sessions->log_level, "%p Session %u: Blocking on cmd_show", s, s->session_id); + yaz_log(YLOG_WARN, "Attempt to block multiple times on show block. Not supported!"); + error(rs, PAZPAR2_ALREADY_BLOCKED, "show"); + } + else + { + yaz_log(c->http_sessions->log_level, "%p Session %u: Blocking on command show", s, s->session_id); } release_session(c, s); return; @@ -1025,8 +1130,8 @@ static void cmd_search(struct http_channel *c) release_session(c, s); return; } - code = search(s->psession, query, startrecs, maxrecs, filter, limit, - &addinfo); + code = session_search(s->psession, query, startrecs, maxrecs, filter, limit, + &addinfo, "relevance", 0); if (code) { error(rs, code, addinfo); @@ -1035,7 +1140,6 @@ static void cmd_search(struct http_channel *c) } response_open(c, "search"); response_close(c, "search"); - http_send_response(c); release_session(c, s); }