First ICU chain integration in relevance ranking of pazpar2.
[pazpar2-moved-to-github.git] / src / http_command.c
index b68aa36..d0e68c9 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: http_command.c,v 1.35 2007-04-15 03:26:47 quinn Exp $
+/* $Id: http_command.c,v 1.43 2007-05-23 09:57:54 adam Exp $
    Copyright (c) 2006-2007, Index Data.
 
 This file is part of Pazpar2.
@@ -20,7 +20,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  */
 
 /*
- * $Id: http_command.c,v 1.35 2007-04-15 03:26:47 quinn Exp $
+ * $Id: http_command.c,v 1.43 2007-05-23 09:57:54 adam Exp $
  */
 
 #include <stdio.h>
@@ -31,7 +31,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <strings.h>
 #include <ctype.h>
 #include <sys/time.h>
-
+#include <yaz/snprintf.h>
 #if HAVE_CONFIG_H
 #include <cconfig.h>
 #endif
@@ -45,9 +45,10 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include "http.h"
 #include "http_command.h"
 #include "settings.h"
+#include "client.h"
 
-extern struct parameters global_parameters;
-extern IOCHAN channel_list;
+// Update this when the protocol changes
+#define PAZPAR2_PROTOCOL_VERSION "1"
 
 struct http_session {
     IOCHAN timeout_iochan;     // NOTE: This is NOT associated with a socket
@@ -82,8 +83,8 @@ struct http_session *http_session_create()
     r->timeout_iochan = iochan_create(-1, session_timeout, 0);
     iochan_setdata(r->timeout_iochan, r);
     iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout);
-    r->timeout_iochan->next = channel_list;
-    channel_list = r->timeout_iochan;
+
+    pazpar2_add_channel(r->timeout_iochan);
     return r;
 }
 
@@ -102,25 +103,34 @@ void http_session_destroy(struct http_session *s)
     nmem_destroy(s->nmem);
 }
 
-static void error(struct http_response *rs, char *code, char *msg, char *txt)
+static void error(struct http_response *rs, 
+                  const char *code, const char *msg, const char *extra)
 {
     struct http_channel *c = rs->channel;
-    char tmp[1024];
+    char text[1024];
+    char *sep = extra ? ": " : "";
 
-    if (!txt)
-        txt = msg;
     rs->msg = nmem_strdup(c->nmem, msg);
     strcpy(rs->code, code);
-    sprintf(tmp, "<error code=\"general\">%s</error>", txt);
-    rs->payload = nmem_strdup(c->nmem, tmp);
+
+    yaz_snprintf(text, sizeof(text),
+                 "<error code=\"general\">%s%s%s</error>", msg, sep,
+                 extra ? extra : "");
+
+    yaz_log(YLOG_WARN, "HTTP %s %s%s%s", code, msg, sep,
+            extra ? extra : "");
+    rs->payload = nmem_strdup(c->nmem, text);
     http_send_response(c);
 }
 
 unsigned int make_sessionid()
 {
+    static int seq = 0;
+#if 1
+    return ++seq;
+#else
     struct timeval t;
     unsigned int res;
-    static int seq = 0;
 
     seq++;
     if (gettimeofday(&t, 0) < 0)
@@ -128,6 +138,7 @@ unsigned int make_sessionid()
     res = t.tv_sec;
     res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
     return res;
+#endif
 }
 
 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
@@ -171,8 +182,7 @@ static int process_settings(struct session *se, struct http_request *rq,
             nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
             if (num != 2)
             {
-                error(rs, "417", "Malformed setting argument", 0);
-                yaz_log(YLOG_WARN, "Malformed setting: %s", a->name);
+                error(rs, "417", "Malformed setting argument", a->name);
                 return -1;
             }
             setting = res[0];
@@ -201,7 +211,8 @@ static void cmd_init(struct http_channel *c)
     s->session_id = sesid;
     if (process_settings(s->psession, c->request, c->response) < 0)
         return;
-    sprintf(buf, "<init><status>OK</status><session>%u</session></init>", sesid);
+    sprintf(buf, "<init><status>OK</status><session>%u</session>"
+            "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>", sesid);
     rs->payload = nmem_strdup(c->nmem, buf);
     http_send_response(c);
 }
@@ -362,7 +373,7 @@ static void write_metadata(WRBUF w, struct conf_service *service,
             continue;
         for (md = ml[imeta]; md; md = md->next)
         {
-            wrbuf_printf(w, "<md-%s>", cmd->name);
+            wrbuf_printf(w, "\n<md-%s>", cmd->name);
             switch (cmd->type)
             {
                 case Metadata_type_generic:
@@ -384,10 +395,10 @@ static void write_metadata(WRBUF w, struct conf_service *service,
 static void write_subrecord(struct record *r, WRBUF w,
         struct conf_service *service, int show_details)
 {
-    char *name = session_setting_oneval(r->client->database, PZ_NAME);
+    char *name = session_setting_oneval(client_get_database(r->client), PZ_NAME);
 
-    wrbuf_printf(w, "<location id=\"%s\" name=\"%s\">\n",
-            r->client->database->database->url,
+    wrbuf_printf(w, "<location id=\"%s\" name=\"%s\">",
+            client_get_database(r->client)->database->url,
             *name ? name : "Unknown");
     if (show_details)
         write_metadata(w, service, r->metadata, 1);
@@ -540,6 +551,31 @@ static void cmd_ping(struct http_channel *c)
     http_send_response(c);
 }
 
+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;
@@ -556,10 +592,15 @@ static void cmd_search(struct http_channel *c)
         error(rs, "417", "Must supply query", 0);
         return;
     }
+    if (!utf_8_valid(query))
+    {
+        error(rs, "417", "Query not UTF-8 encoded", 0);
+        return;
+    }
     res = search(s->psession, query, filter);
     if (res)
     {
-        error(rs, "417", res, res);
+        error(rs, "417", res, 0);
         return;
     }
     rs->payload = "<search><status>OK</status></search>";
@@ -661,7 +702,7 @@ void http_command(struct http_channel *c)
             break;
         }
     if (!commands[i].name)
-        error(rs, "417", "Unknown command", 0);
+        error(rs, "417", "Unknown command", command);
 
     return;
 }