Remove mergekey declaration
[pazpar2-moved-to-github.git] / src / http_command.c
index 73a8b1f..9989ac8 100644 (file)
@@ -33,17 +33,18 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <yaz/snprintf.h>
 #include <yaz/yaz-util.h>
 
-#include "util.h"
 #include "eventl.h"
+#include "parameters.h"
 #include "pazpar2.h"
 #include "http.h"
-#include "http_command.h"
 #include "settings.h"
 #include "client.h"
 
 // Update this when the protocol changes
 #define PAZPAR2_PROTOCOL_VERSION "1"
 
+#define HTTP_COMMAND_RESPONSE_PREFIX "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+
 struct http_session {
     IOCHAN timeout_iochan;     // NOTE: This is NOT associated with a socket
     struct session *psession;
@@ -53,7 +54,8 @@ struct http_session {
     struct http_session *next;
 };
 
-static struct http_session *session_list = 0;
+static struct http_session *session_list = 0; /* thread pr */
+
 void http_session_destroy(struct http_session *s);
 
 static void session_timeout(IOCHAN i, int event)
@@ -75,7 +77,7 @@ struct http_session *http_session_create(struct conf_service *service)
     session_list = r;
     r->timeout_iochan = iochan_create(-1, session_timeout, 0);
     iochan_setdata(r->timeout_iochan, r);
-    iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout);
+    iochan_settimeout(r->timeout_iochan, service->session_timeout);
 
     pazpar2_add_channel(r->timeout_iochan);
     return r;
@@ -140,7 +142,7 @@ static void error(struct http_response *rs,
     rs->msg = nmem_strdup(c->nmem, msg);
     strcpy(rs->code, http_status);
 
-    wrbuf_printf(text, "<error code=\"%d\" msg=\"%s\">", (int) code,
+    wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code,
                msg);
     if (addinfo)
         wrbuf_xmlputs(text, addinfo);
@@ -155,7 +157,7 @@ static void error(struct http_response *rs,
 
 unsigned int make_sessionid(void)
 {
-    static int seq = 0;
+    static int seq = 0; /* thread pr */
     unsigned int res;
 
     seq++;
@@ -185,7 +187,7 @@ unsigned int make_sessionid(void)
 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
 {
     struct http_session *p;
-    char *session = http_argbyname(rq, "session");
+    const char *session = http_argbyname(rq, "session");
     unsigned int id;
 
     if (!session)
@@ -207,7 +209,7 @@ static struct http_session *locate_session(struct http_request *rq, struct http_
 // Decode settings parameters and apply to session
 // Syntax: setting[target]=value
 static int process_settings(struct session *se, struct http_request *rq,
-        struct http_response *rs)
+                            struct http_response *rs)
 {
     struct http_argument *a;
 
@@ -242,48 +244,103 @@ static void cmd_exit(struct http_channel *c)
 
 static void cmd_init(struct http_channel *c)
 {
-    unsigned int sesid;
     char buf[1024];
-    const char *clear = http_argbyname(c->request, "clear");
-    const char *service_name = http_argbyname(c->request, "service");
-    struct conf_service *service = locate_service(c->server,
-                                                  service_name);
-    struct http_session *s = http_session_create(service);
+    struct http_request *r = c->request;
+    const char *clear = http_argbyname(r, "clear");
+    const char *content_type = http_lookup_header(r->headers, "Content-Type");
+    unsigned int sesid;
+    struct http_session *s;
     struct http_response *rs = c->response;
-
+    struct conf_service *service = 0; /* no service (yet) */
+    
+    if (r->content_len && content_type && 
+        !yaz_strcmp_del("text/xml", content_type, "; "))
+    {
+        xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
+        xmlNode *root_n;
+        if (!doc)
+        {
+            error(rs, PAZPAR2_MALFORMED_SETTING, 0);
+            return;
+        }
+        root_n = xmlDocGetRootElement(doc);
+        service = service_create(c->server, root_n);
+        xmlFreeDoc(doc);
+        if (!service)
+        {
+            error(rs, PAZPAR2_MALFORMED_SETTING, 0);
+            return;
+        }
+    }
+    
     if (!service)
     {
-        error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "service");
-        return;
+        const char *service_name = http_argbyname(c->request, "service");
+        service = locate_service(c->server, service_name);
+        if (!service)
+        {
+            error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "service");
+            return;
+        }
+        service_incref(service);
     }
-
+    s = http_session_create(service);
+    
     yaz_log(YLOG_DEBUG, "HTTP Session init");
     if (!clear || *clear == '0')
         session_init_databases(s->psession);
     else
         yaz_log(YLOG_LOG, "No databases preloaded");
+    
     sesid = make_sessionid();
     s->session_id = sesid;
     if (process_settings(s->psession, c->request, c->response) < 0)
         return;
-    sprintf(buf, "<init><status>OK</status><session>%u</session>"
+    sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX "<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);
 }
 
+static void apply_local_setting(void *client_data,
+                                struct setting *set)
+{
+    struct session *se =  (struct session *) client_data;
+
+    session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
+                          nmem_strdup(se->session_nmem, set->name),
+                          nmem_strdup(se->session_nmem, set->value));
+}
+
 static void cmd_settings(struct http_channel *c)
 {
     struct http_response *rs = c->response;
     struct http_request *rq = c->request;
     struct http_session *s = locate_session(rq, rs);
+    const char *content_type = http_lookup_header(rq->headers, "Content-Type");
 
     if (!s)
         return;
 
+    if (rq->content_len && content_type && 
+        !yaz_strcmp_del("text/xml", content_type, "; "))
+    {
+        xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
+        xmlNode *root_n;
+        if (!doc)
+        {
+            error(rs, PAZPAR2_MALFORMED_SETTING, 0);
+            return;
+        }
+        root_n = xmlDocGetRootElement(doc);
+
+        settings_read_node_x(root_n, s->psession, apply_local_setting);
+
+        xmlFreeDoc(doc);
+    }
     if (process_settings(s->psession, rq, rs) < 0)
         return;
-    rs->payload = "<settings><status>OK</status></settings>";
+    rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<settings><status>OK</status></settings>";
     http_send_response(c);
 }
 
@@ -322,7 +379,8 @@ static void targets_termlist(WRBUF wrbuf, struct session *se, int num,
             wrbuf_xmlputs(wrbuf, ht[i].name);
         wrbuf_puts(wrbuf, "</name>\n");
         
-        wrbuf_printf(wrbuf, "<frequency>%d</frequency>\n", ht[i].hits);
+        wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
+                     ht[i].hits);
         
         wrbuf_puts(wrbuf, "<state>");
         wrbuf_xmlputs(wrbuf, ht[i].state);
@@ -342,8 +400,8 @@ static void cmd_termlist(struct http_channel *c)
     struct termlist_score **p;
     int len;
     int i;
-    char *name = http_argbyname(rq, "name");
-    char *nums = http_argbyname(rq, "num");
+    const char *name = http_argbyname(rq, "name");
+    const char *nums = http_argbyname(rq, "num");
     int num = 15;
     int status;
 
@@ -366,7 +424,7 @@ static void cmd_termlist(struct http_channel *c)
     while (*name)
     {
         char tname[256];
-        char *tp;
+        const char *tp;
 
         if (!(tp = strchr(name, ',')))
             tp = name + strlen(name);
@@ -415,13 +473,14 @@ static void cmd_bytarget(struct http_channel *c)
     struct http_request *rq = c->request;
     struct http_session *s = locate_session(rq, rs);
     struct hitsbytarget *ht;
+    const char *settings = http_argbyname(rq, "settings");
     int count, i;
 
     if (!s)
         return;
     ht = hitsbytarget(s->psession, &count, c->nmem);
     wrbuf_rewind(c->wrbuf);
-    wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
+    wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<bytarget><status>OK</status>");
 
     for (i = 0; i < count; i++)
     {
@@ -438,15 +497,21 @@ static void cmd_bytarget(struct http_channel *c)
             wrbuf_puts(c->wrbuf, "</name>\n");
         }
 
-        wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
+        wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
         wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
         wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
 
         wrbuf_puts(c->wrbuf, "<state>");
         wrbuf_xmlputs(c->wrbuf, ht[i].state);
         wrbuf_puts(c->wrbuf, "</state>\n");
-
+        if (settings && *settings == '1')
+        {
+            wrbuf_puts(c->wrbuf, "<settings>\n");
+            wrbuf_puts(c->wrbuf, wrbuf_cstr(ht[i].settings_xml));
+            wrbuf_puts(c->wrbuf, "</settings>\n");
+        }
         wrbuf_puts(c->wrbuf, "</target>");
+        wrbuf_destroy(ht[i].settings_xml);
     }
 
     wrbuf_puts(c->wrbuf, "</bytarget>");
@@ -467,8 +532,16 @@ static void write_metadata(WRBUF w, struct conf_service *service,
             continue;
         for (md = ml[imeta]; md; md = md->next)
         {
-            wrbuf_printf(w, "\n<md-%s>", cmd->name);
+            struct record_metadata_attr *attr = md->attributes;
+            wrbuf_printf(w, "\n<md-%s", cmd->name);
 
+            for (; attr; attr = attr->next)
+            {
+                wrbuf_printf(w, " %s=\"", attr->name);
+                wrbuf_xmlputs(w, attr->value);
+                wrbuf_puts(w, "\"");
+            }
+            wrbuf_puts(w, ">");
             switch (cmd->type)
             {
                 case Metadata_type_generic:
@@ -561,13 +634,14 @@ static void cmd_record(struct http_channel *c)
     struct http_session *s = locate_session(rq, rs);
     struct record_cluster *rec, *prev_r, *next_r;
     struct record *r;
-    struct conf_service *service = s->psession->service;
+    struct conf_service *service;
     const char *idstr = http_argbyname(rq, "id");
     const char *offsetstr = http_argbyname(rq, "offset");
     const char *binarystr = http_argbyname(rq, "binary");
     
     if (!s)
         return;
+    service = s->psession->service;
     if (!idstr)
     {
         error(rs, PAZPAR2_MISSING_PARAMETER, "id");
@@ -576,7 +650,11 @@ static void cmd_record(struct http_channel *c)
     wrbuf_rewind(c->wrbuf);
     if (!(rec = show_single(s->psession, idstr, &prev_r, &next_r)))
     {
-        if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
+        if (session_active_clients(s->psession) == 0)
+        {
+            error(rs, PAZPAR2_RECORD_MISSING, idstr);
+        }
+        else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
                               cmd_record_ready, c, c) != 0)
         {
             error(rs, PAZPAR2_RECORD_MISSING, idstr);
@@ -623,7 +701,7 @@ static void cmd_record(struct http_channel *c)
     }
     else
     {
-        wrbuf_puts(c->wrbuf, "<record>\n");
+        wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<record>\n");
         wrbuf_puts(c->wrbuf, "<recid>");
         wrbuf_xmlputs(c->wrbuf, rec->recid);
         wrbuf_puts(c->wrbuf, "</recid>\n");
@@ -664,13 +742,13 @@ static void show_records(struct http_channel *c, int active)
     struct http_session *s = locate_session(rq, rs);
     struct record_cluster **rl;
     struct reclist_sortparms *sp;
-    char *start = http_argbyname(rq, "start");
-    char *num = http_argbyname(rq, "num");
-    char *sort = http_argbyname(rq, "sort");
+    const char *start = http_argbyname(rq, "start");
+    const char *num = http_argbyname(rq, "num");
+    const char *sort = http_argbyname(rq, "sort");
     int startn = 0;
     int numn = 20;
     int total;
-    int total_hits;
+    Odr_int total_hits;
     int i;
 
     if (!s)
@@ -695,10 +773,10 @@ static void show_records(struct http_channel *c, int active)
     rl = show(s->psession, sp, startn, &numn, &total, &total_hits, c->nmem);
 
     wrbuf_rewind(c->wrbuf);
-    wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
+    wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<show>\n<status>OK</status>\n");
     wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
     wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
-    wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
+    wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
     wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
     wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
 
@@ -715,6 +793,8 @@ static void show_records(struct http_channel *c, int active)
             write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
         if (ccount > 1)
             wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
+       if (strstr(sort, "relevance"))
+           wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n", rec->relevance);
         wrbuf_puts(c->wrbuf, "<recid>");
         wrbuf_xmlputs(c->wrbuf, rec->recid);
         wrbuf_puts(c->wrbuf, "</recid>\n");
@@ -738,7 +818,7 @@ static void cmd_show(struct http_channel *c)
     struct http_request *rq = c->request;
     struct http_response *rs = c->response;
     struct http_session *s = locate_session(rq, rs);
-    char *block = http_argbyname(rq, "block");
+    const char *block = http_argbyname(rq, "block");
     int status;
 
     if (!s)
@@ -748,7 +828,7 @@ static void cmd_show(struct http_channel *c)
 
     if (block)
     {
-        if (status && (!s->psession->reclist || !s->psession->reclist->num_records))
+        if (status && 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,
@@ -770,7 +850,7 @@ static void cmd_ping(struct http_channel *c)
     struct http_session *s = locate_session(rq, rs);
     if (!s)
         return;
-    rs->payload = "<ping><status>OK</status></ping>";
+    rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<ping><status>OK</status></ping>";
     http_send_response(c);
 }
 
@@ -804,8 +884,10 @@ static void cmd_search(struct http_channel *c)
     struct http_request *rq = c->request;
     struct http_response *rs = c->response;
     struct http_session *s = locate_session(rq, rs);
-    char *query = http_argbyname(rq, "query");
-    char *filter = http_argbyname(rq, "filter");
+    const char *query = http_argbyname(rq, "query");
+    const char *filter = http_argbyname(rq, "filter");
+    const char *maxrecs = http_argbyname(rq, "maxrecs");
+    const char *startrecs = http_argbyname(rq, "startrecs");
     enum pazpar2_error_code code;
     const char *addinfo = 0;
 
@@ -821,13 +903,13 @@ static void cmd_search(struct http_channel *c)
         error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
         return;
     }
-    code = search(s->psession, query, filter, &addinfo);
+    code = search(s->psession, query, startrecs, maxrecs, filter, &addinfo);
     if (code)
     {
         error(rs, code, addinfo);
         return;
     }
-    rs->payload = "<search><status>OK</status></search>";
+    rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<search><status>OK</status></search>";
     http_send_response(c);
 }
 
@@ -853,9 +935,9 @@ static void cmd_stat(struct http_channel *c)
     }
 
     wrbuf_rewind(c->wrbuf);
-    wrbuf_puts(c->wrbuf, "<stat>");
+    wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<stat>");
     wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
-    wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
+    wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
     wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
     wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
     wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
@@ -876,7 +958,7 @@ static void cmd_info(struct http_channel *c)
     struct http_response *rs = c->response;
 
     wrbuf_rewind(c->wrbuf);
-    wrbuf_puts(c->wrbuf, "<info>\n");
+    wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<info>\n");
     wrbuf_puts(c->wrbuf, " <version>\n");
     wrbuf_puts(c->wrbuf, "<pazpar2");
 #ifdef PAZPAR2_VERSION_SHA1
@@ -921,7 +1003,7 @@ struct {
 
 void http_command(struct http_channel *c)
 {
-    char *command = http_argbyname(c->request, "command");
+    const char *command = http_argbyname(c->request, "command");
     struct http_response *rs = http_create_response(c);
     int i;