HACK to force ALREADY_BLOCKED response
[pazpar2-moved-to-github.git] / src / http_command.c
index 8a13590..2809989 100644 (file)
@@ -126,7 +126,7 @@ void http_sessions_destroy(http_sessions_t hs)
         {
             struct http_session *s_next = s->next;
             iochan_destroy(s->timeout_iochan);
-            destroy_session(s->psession);
+            session_destroy(s->psession);
             nmem_destroy(s->nmem);
             s = s_next;
         }
@@ -199,7 +199,7 @@ void http_session_destroy(struct http_session *s)
     {   /* destroying for real */
         yaz_log(http_sessions->log_level, "%p HTTP Session %u destroyed", s, s->session_id);
         iochan_destroy(s->timeout_iochan);
-        destroy_session(s->psession);
+        session_destroy(s->psession);
         http_session_use(-1);
         nmem_destroy(s->nmem);
     }
@@ -229,7 +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 on command in session."},
+        { PAZPAR2_ALREADY_BLOCKED, "Already blocked in session on: "},
         { PAZPAR2_LAST_ERROR, "Last error"},
         { 0, 0 }
     };
@@ -509,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);
@@ -518,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, "<activeclients>%d</activeclients>\n", status);
 
@@ -535,6 +532,46 @@ 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;
+    yaz_log(c->http_sessions->log_level, "termlist watch released");
+    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)
@@ -610,7 +647,8 @@ static void bytarget_response(struct http_channel *c) {
 
     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>");
@@ -628,6 +666,14 @@ static void bytarget_response(struct http_channel *c) {
 
         wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
         wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
+        if (ht[i].diagnostic)
+        {
+            wrbuf_puts(c->wrbuf, "<addinfo>");
+            if (ht[i].addinfo)
+                wrbuf_xmlputs(c->wrbuf, ht[i].addinfo);
+            wrbuf_puts(c->wrbuf, "</addinfo>\n");
+        }
+
         wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
 
         wrbuf_puts(c->wrbuf, "<state>");
@@ -653,7 +699,7 @@ static void bytarget_response(struct http_channel *c) {
 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);
 }
 
@@ -664,16 +710,20 @@ static void cmd_bytarget(struct http_channel *c)
     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;
 
-    if (block && strcmp("1",block) == 0)
+    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
@@ -899,7 +949,7 @@ static void cmd_record(struct http_channel *c)
 static void cmd_record_ready(void *data)
 {
     struct http_channel *c = (struct http_channel *) data;
-
+    yaz_log(c->http_sessions->log_level, "record watch released");
     cmd_record(c);
 }
 
@@ -981,10 +1031,12 @@ static void show_records(struct http_channel *c, int active)
 static void show_records_ready(void *data)
 {
     struct http_channel *c = (struct http_channel *) data;
-
+    yaz_log(c->http_sessions->log_level, "show watch released");
     show_records(c, -1);
 }
 
+int show_count = 0;
+
 static void cmd_show(struct http_channel *c)
 {
     struct http_request  *rq = c->request;
@@ -1024,6 +1076,7 @@ static void cmd_show(struct http_channel *c)
             }
             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);
@@ -1034,8 +1087,11 @@ 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,
-                                  show_records_ready, c, c) != 0)
+                                  show_records_ready, c, c) != 0
+//                || (++show_count % 6 == 0)
+                )
             {
+                yaz_log(YLOG_WARN, "Attempt to block multiple times on show block. Not supported!");
                 error(rs, PAZPAR2_ALREADY_BLOCKED, "show"); 
             }
             else