Investigating empty bytarget response. More logging
[pazpar2-moved-to-github.git] / src / http_command.c
index 963740f..f25f478 100644 (file)
@@ -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;
@@ -600,20 +601,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<target>");
@@ -642,12 +640,55 @@ static void cmd_bytarget(struct http_channel *c)
             wrbuf_puts(c->wrbuf, ht[i].settings_xml);
             wrbuf_puts(c->wrbuf, "</settings>\n");
         }
+        if (ht[i].suggestions_xml && ht[i].suggestions_xml[0]) {
+            wrbuf_puts(c->wrbuf, "<suggestions>");
+            wrbuf_puts(c->wrbuf, ht[i].suggestions_xml);
+            wrbuf_puts(c->wrbuf, "</suggestions>");
+        }
         wrbuf_puts(c->wrbuf, "</target>");
     }
     response_close(c, "bytarget");
     release_session(c, s);
 }
 
+static void bytarget_result_ready(void *data)
+{
+    struct http_channel *c = (struct http_channel *) data;
+
+    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");
+
+    if (!s)
+        return;
+
+    if (block && strcmp("1",block) == 0)
+    {
+        // 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)
 {
@@ -948,7 +989,8 @@ 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");
@@ -977,10 +1019,15 @@ 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;
@@ -992,7 +1039,12 @@ 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(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;