ICU chain "facet" honors YAC ICU element <display/>.
[pazpar2-moved-to-github.git] / src / http_command.c
index 9d464d6..b46ba7b 100644 (file)
@@ -41,10 +41,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include "settings.h"
 #include "client.h"
 
+#ifdef HAVE_MALLINFO
 #include <malloc.h>
 
-void print_meminfo(WRBUF wrbuf) {
-#ifdef __GNUC__
+void print_meminfo(WRBUF wrbuf)
+{
     struct mallinfo minfo;
     minfo = mallinfo();
     wrbuf_printf(wrbuf, "  <memory>\n"
@@ -60,8 +61,10 @@ void print_meminfo(WRBUF wrbuf) {
                         "  </memory>\n", 
                  minfo.arena, minfo.uordblks, minfo.fordblks,minfo.ordblks, minfo.keepcost, minfo.hblks, minfo.hblkhd, minfo.arena + minfo.hblkhd, minfo.uordblks + minfo.hblkhd);
 
-#endif
 }
+#else
+#define print_meminfo(x)
+#endif
 
 
 // Update this when the protocol changes
@@ -99,7 +102,7 @@ int http_session_use(int delta)
     g_http_sessions += delta;
     sessions = g_http_sessions;
     yaz_mutex_leave(g_http_session_mutex);
-    yaz_log(YLOG_DEBUG, "%s sesions=%d", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), sessions);
+    yaz_log(YLOG_DEBUG, "%s sessions=%d", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), sessions);
     return sessions;
 
 }
@@ -180,7 +183,8 @@ void http_session_destroy(struct http_session *s)
     yaz_log(http_sessions->log_level, "%p HTTP Session %u destroyed", s, s->session_id);
     yaz_mutex_enter(http_sessions->mutex);
     /* only if http_session has no active http sessions on it can be destroyed */
-    if (s->destroy_counter == s->activity_counter) {
+    if (s->destroy_counter == s->activity_counter)
+    {
         struct http_session **p = 0;
         must_destroy = 1;
         for (p = &http_sessions->session_list; *p; p = &(*p)->next)
@@ -322,7 +326,9 @@ static struct http_session *locate_session(struct http_channel *c)
 }
 
 // Call after use of locate_session, in order to increment the destroy_counter
-static void release_session(struct http_channel *c, struct http_session *session) {
+static void release_session(struct http_channel *c,
+                            struct http_session *session)
+{
     http_sessions_t http_sessions = c->http_sessions;
     yaz_mutex_enter(http_sessions->mutex);
     if (session)
@@ -473,13 +479,14 @@ static void cmd_settings(struct http_channel *c)
 
         xmlFreeDoc(doc);
     }
-    if (process_settings(s->psession, rq, rs) < 0) {
-        release_session(c,s);
+    if (process_settings(s->psession, rq, rs) < 0)
+    {
+        release_session(c, s);
         return;
     }
     rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<settings><status>OK</status></settings>";
     http_send_response(c);
-    release_session(c,s);
+    release_session(c, s);
 }
 
 // Compares two hitsbytarget nodes by hitcount
@@ -536,14 +543,11 @@ static void cmd_termlist(struct http_channel *c)
     struct http_response *rs = c->response;
     struct http_request *rq = c->request;
     struct http_session *s = locate_session(c);
-    struct termlist_score **p;
-    int len;
-    int i;
     const char *name = http_argbyname(rq, "name");
     const char *nums = http_argbyname(rq, "num");
     int num = 15;
     int status;
-    WRBUF debug_log = wrbuf_alloc();
+    WRBUF debug_log = 0;
 
     if (!s)
         return;
@@ -557,6 +561,8 @@ static void cmd_termlist(struct http_channel *c)
     if (nums)
         num = atoi(nums);
 
+    debug_log = wrbuf_alloc();
+
     wrbuf_rewind(c->wrbuf);
 
     wrbuf_puts(c->wrbuf, "<termlist>\n");
@@ -573,31 +579,37 @@ static void cmd_termlist(struct http_channel *c)
         wrbuf_puts(c->wrbuf, "<list name=\"");
         wrbuf_xmlputs(c->wrbuf, tname);
         wrbuf_puts(c->wrbuf, "\">\n");
-        if (!strcmp(tname, "xtargets")) {
+        if (!strcmp(tname, "xtargets"))
+        {
             int targets = targets_termlist(c->wrbuf, s->psession, num, c->nmem);
             wrbuf_printf(debug_log, " xtargets: %d", targets);
         }
         else
         {
-            p = termlist(s->psession, tname, &len);
+            int len;
+            struct termlist_score **p = 
+                get_termlist_score(s->psession, tname, &len);
             if (p && len)
                 wrbuf_printf(debug_log, " %s: %d", tname, len);
-            if (p) {
-                for (i = 0; i < len && i < num; i++){
+            if (p)
+            {
+                int i;
+                for (i = 0; i < len && i < num; i++)
+                {
                     // prevnt sending empty term elements
-                    if (!p[i]->term || !p[i]->term[0])
+                    if (!p[i]->display_term || !p[i]->display_term[0])
                         continue;
 
                     wrbuf_puts(c->wrbuf, "<term>");
                     wrbuf_puts(c->wrbuf, "<name>");
-                    wrbuf_xmlputs(c->wrbuf, p[i]->term);
+                    wrbuf_xmlputs(c->wrbuf, p[i]->display_term);
                     wrbuf_puts(c->wrbuf, "</name>");
-                        
+                    
                     wrbuf_printf(c->wrbuf, 
                                  "<frequency>%d</frequency>", 
                                  p[i]->frequency);
                     wrbuf_puts(c->wrbuf, "</term>\n");
-               }
+                }
             }
         }
         wrbuf_puts(c->wrbuf, "</list>\n");
@@ -610,7 +622,7 @@ static void cmd_termlist(struct http_channel *c)
     wrbuf_destroy(debug_log);
     rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
     http_send_response(c);
-    release_session(c,s);
+    release_session(c, s);
 }
 
 size_t session_get_memory_status(struct session *session);
@@ -624,7 +636,8 @@ static void session_status(struct http_channel *c, struct http_session *s)
     wrbuf_printf(c->wrbuf, "<session_nmem>%zu</session_nmem>\n", session_nmem);
 }
 
-static void cmd_session_status(struct http_channel *c) {
+static void cmd_session_status(struct http_channel *c)
+{
     struct http_response *rs = c->response;
     struct http_session *s = locate_session(c);
     if (!s)
@@ -636,7 +649,7 @@ static void cmd_session_status(struct http_channel *c) {
     wrbuf_puts(c->wrbuf, "</sessionstatus>\n");
     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
     http_send_response(c);
-    release_session(c,s);
+    release_session(c, s);
 
 }
 
@@ -667,7 +680,8 @@ static void cmd_server_status(struct http_channel *c)
 /*    struct http_session *p;                           */
 /*
     yaz_mutex_enter(http_sessions->mutex);
-    for (p = http_sessions->session_list; p; p = p->next) {
+    for (p = http_sessions->session_list; p; p = p->next)
+    {
         p->activity_counter++;
         wrbuf_puts(c->wrbuf, "<session-status>\n");
         wrbuf_printf(c->wrbuf, "<id>%s</id>\n", p->session_id);
@@ -735,7 +749,7 @@ static void cmd_bytarget(struct http_channel *c)
     wrbuf_puts(c->wrbuf, "</bytarget>");
     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
     http_send_response(c);
-    release_session(c,s);
+    release_session(c, s);
 }
 
 static void write_metadata(WRBUF w, struct conf_service *service,
@@ -1054,7 +1068,8 @@ static void cmd_show(struct http_channel *c)
 
     if (block)
     {
-        if (!strcmp(block, "preferred") && !session_is_preferred_clients_ready(s->psession) && reclist_get_num_records(s->psession->reclist) == 0) {
+        if (!strcmp(block, "preferred") && !session_is_preferred_clients_ready(s->psession) && reclist_get_num_records(s->psession->reclist) == 0)
+        {
             // 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)
@@ -1062,7 +1077,7 @@ static void cmd_show(struct http_channel *c)
                 yaz_log(c->http_sessions->log_level,
                         "%p Session %u: Blocking on cmd_show. Waiting for preferred targets", s, s->session_id);
             }
-            release_session(c,s);
+            release_session(c, s);
             return;
 
         }
@@ -1074,12 +1089,12 @@ static void cmd_show(struct http_channel *c)
             {
                 yaz_log(c->http_sessions->log_level, "%p Session %u: Blocking on cmd_show", s, s->session_id);
             }
-            release_session(c,s);
+            release_session(c, s);
             return;
         }
     }
     show_records(c, status);
-    release_session(c,s);
+    release_session(c, s);
 }
 
 static void cmd_ping(struct http_channel *c)
@@ -1093,31 +1108,6 @@ static void cmd_ping(struct http_channel *c)
     release_session(c, s);
 }
 
-static int utf_8_valid(const char *str)
-{
-    yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
-    if (cd)
-    {
-        /* check that query is UTF-8 encoded */
-        char *inbuf = (char *) str; /* we know iconv does not alter this */
-        size_t inbytesleft = strlen(inbuf);
-
-        size_t outbytesleft = strlen(inbuf) + 10;
-        char *out = xmalloc(outbytesleft);
-        char *outbuf = out;
-        size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
-
-        /* if OK, try flushing the rest  */
-        if (r != (size_t) (-1))
-            r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
-        yaz_iconv_close(cd);
-        xfree(out);
-        if (r == (size_t) (-1))
-            return 0;
-    }
-    return 1;
-}
-
 static void cmd_search(struct http_channel *c)
 {
     struct http_request *rq = c->request;
@@ -1127,6 +1117,7 @@ static void cmd_search(struct http_channel *c)
     const char *filter = http_argbyname(rq, "filter");
     const char *maxrecs = http_argbyname(rq, "maxrecs");
     const char *startrecs = http_argbyname(rq, "startrecs");
+    const char *limit = http_argbyname(rq, "limit");
     enum pazpar2_error_code code;
     const char *addinfo = 0;
 
@@ -1135,25 +1126,26 @@ static void cmd_search(struct http_channel *c)
     if (!query)
     {
         error(rs, PAZPAR2_MISSING_PARAMETER, "query");
-        release_session(c,s);
+        release_session(c, s);
         return;
     }
-    if (!utf_8_valid(query))
+    if (!yaz_utf8_check(query))
     {
         error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
-        release_session(c,s);
+        release_session(c, s);
         return;
     }
-    code = search(s->psession, query, startrecs, maxrecs, filter, &addinfo);
+    code = search(s->psession, query, startrecs, maxrecs, filter, limit,
+                  &addinfo);
     if (code)
     {
         error(rs, code, addinfo);
-        release_session(c,s);
+        release_session(c, s);
         return;
     }
     rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<search><status>OK</status></search>";
     http_send_response(c);
-    release_session(c,s);
+    release_session(c, s);
 }
 
 
@@ -1172,7 +1164,8 @@ static void cmd_stat(struct http_channel *c)
     clients = session_active_clients(s->psession);
     statistics(s->psession, &stat);
 
-    if (stat.num_clients > 0) {
+    if (stat.num_clients > 0)
+    {
        progress = (stat.num_clients  - clients) / (float)stat.num_clients;
     }
 
@@ -1192,7 +1185,7 @@ static void cmd_stat(struct http_channel *c)
     wrbuf_puts(c->wrbuf, "</stat>");
     rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
     http_send_response(c);
-    release_session(c,s);
+    release_session(c, s);
 }
 
 static void cmd_info(struct http_channel *c)