Fixed check for application/x-www-form-urlencoded parameters.
[pazpar2-moved-to-github.git] / src / http.c
index e4655e5..2ad82ce 100644 (file)
@@ -1,45 +1,79 @@
-/*
- * $Id: http.c,v 1.17 2007-03-30 02:45:07 quinn Exp $
- */
+/* This file is part of Pazpar2.
+   Copyright (C) 2006-2008 Index Data
+
+Pazpar2 is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+*/
+
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #include <stdio.h>
+#ifdef WIN32
+#include <winsock.h>
+typedef int socklen_t;
+#endif
+
+#if HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
+#endif
+
 #include <sys/types.h>
-#include <sys/uio.h>
+
+#include <yaz/snprintf.h>
+#if HAVE_UNISTD_H
 #include <unistd.h>
+#endif
+
 #include <stdlib.h>
-#include <strings.h>
+#include <string.h>
 #include <ctype.h>
 #include <fcntl.h>
+#if HAVE_NETDB_H
 #include <netdb.h>
+#endif
+
 #include <errno.h>
 #include <assert.h>
 #include <string.h>
 
-#if HAVE_CONFIG_H
-#include <cconfig.h>
+#if HAVE_NETINET_IN_H
+#include <netinet/in.h>
 #endif
 
-#include <netinet/in.h>
+#if HAVE_ARPA_INET_H
+#include <arpa/inet.h>
+#endif
 
 #include <yaz/yaz-util.h>
 #include <yaz/comstack.h>
-#include <netdb.h>
+#include <yaz/nmem.h>
 
-#include "cconfig.h"
 #include "util.h"
 #include "eventl.h"
 #include "pazpar2.h"
 #include "http.h"
 #include "http_command.h"
 
+#define MAX_HTTP_HEADER 4096
+
 static void proxy_io(IOCHAN i, int event);
-static struct http_channel *http_create(void);
+static struct http_channel *http_create(const char *addr);
 static void http_destroy(IOCHAN i);
 
-extern IOCHAN channel_list;
-extern struct parameters global_parameters;
-
 // If this is set, we proxy normal HTTP requests
 static struct sockaddr_in *proxy_addr = 0; 
 static char proxy_url[256] = "";
@@ -47,6 +81,24 @@ static char myurl[256] = "";
 static struct http_buf *http_buf_freelist = 0;
 static struct http_channel *http_channel_freelist = 0;
 
+struct http_channel_observer_s {
+    void *data;
+    void *data2;
+    http_channel_destroy_t destroy;
+    struct http_channel_observer_s *next;
+    struct http_channel *chan;
+};
+
+
+static const char *http_lookup_header(struct http_header *header,
+                                      const char *name)
+{
+    for (; header; header = header->next)
+        if (!strcasecmp(name, header->name))
+            return header->value;
+    return 0;
+}
+
 static struct http_buf *http_buf_create()
 {
     struct http_buf *r;
@@ -81,17 +133,6 @@ static void http_buf_destroy_queue(struct http_buf *b)
     }
 }
 
-#ifdef GAGA
-// Calculate length of chain
-static int http_buf_len(struct http_buf *b)
-{
-    int sum = 0;
-    for (; b; b = b->next)
-        sum += b->len;
-    return sum;
-}
-#endif
-
 static struct http_buf *http_buf_bybuf(char *b, int len)
 {
     struct http_buf *res = 0;
@@ -128,7 +169,7 @@ static struct http_buf *http_buf_bywrbuf(WRBUF wrbuf)
 
 // Non-destructively collapse chain of buffers into a string (max *len)
 // Return
-static int http_buf_peek(struct http_buf *b, char *buf, int len)
+static void http_buf_peek(struct http_buf *b, char *buf, int len)
 {
     int rd = 0;
     while (b && rd < len)
@@ -141,7 +182,14 @@ static int http_buf_peek(struct http_buf *b, char *buf, int len)
         b = b->next;
     }
     buf[rd] = '\0';
-    return rd;
+}
+
+static int http_buf_size(struct http_buf *b)
+{
+    int sz = 0;
+    for (; b; b = b->next)
+        sz += b->len;
+    return sz;
 }
 
 // Ddestructively munch up to len  from head of queue.
@@ -182,12 +230,13 @@ static void urldecode(char *i, char *o)
             *(o++) = ' ';
             i++;
         }
-        else if (*i == '%')
+        else if (*i == '%' && i[1] && i[2])
         {
+            int v;
             i++;
-            sscanf(i, "%2hhx", o);
+            sscanf(i, "%2x", &v);
+            *o++ = v;
             i += 2;
-            o++;
         }
         else
             *(o++) = *(i++);
@@ -249,48 +298,87 @@ struct http_response *http_create_response(struct http_channel *c)
     r->channel = c;
     r->headers = 0;
     r->payload = 0;
+    r->content_type = "text/xml";
     return r;
 }
 
+
+static const char *next_crlf(const char *cp, size_t *skipped)
+{
+    const char *next_cp = strchr(cp, '\n');
+    if (next_cp)
+    {
+        if (next_cp > cp && next_cp[-1] == '\r')
+            *skipped = next_cp - cp - 1;
+        else
+            *skipped = next_cp - cp;
+        next_cp++;
+    }
+    return next_cp;
+}
+
 // Check if buf contains a package (minus payload)
-static int package_check(const char *buf)
+static int package_check(const char *buf, int sz)
 {
+    int content_len = 0;
     int len = 0;
-    while (*buf) // Check if we have a sequence of lines terminated by an empty line
+
+    while (*buf)
     {
-        char *b = strstr(buf, "\r\n");
+        size_t skipped = 0;
+        const char *b = next_crlf(buf, &skipped);
 
         if (!b)
-            return 0;
-
-        len += (b - buf) + 2;
-        if (b == buf)
-            return len;
-        buf = b + 2;
+        {
+            // we did not find CRLF.. See if buffer is too large..
+            if (sz >= MAX_HTTP_HEADER-1)
+                return MAX_HTTP_HEADER-1; // yes. Return that (will fail later)
+            break;
+        }
+        len += (b - buf);
+        if (skipped == 0)
+        {
+            // CRLF CRLF , i.e. end of header
+            if (len + content_len <= sz)
+                return len + content_len;
+            break;
+        }
+        buf = b;
+        // following first skip of \r\n so that we don't consider Method
+        if (!strncasecmp(buf, "Content-Length:", 15))
+        {
+            const char *cp = buf+15;
+            while (*cp == ' ')
+                cp++;
+            content_len = 0;
+            while (*cp && isdigit(*cp))
+                content_len = content_len*10 + (*cp++ - '0');
+            if (content_len < 0) /* prevent negative offsets */
+                content_len = 0;
+        }
     }
-    return 0;
+    return 0;     // incomplete request
 }
 
 // Check if we have a request. Return 0 or length
-// (including trailing CRNL) FIXME: Does not deal gracefully with requests
-// carrying payload but this is kind of OK since we will reject anything
-// other than an empty GET
 static int request_check(struct http_buf *queue)
 {
-    char tmp[4096];
+    char tmp[MAX_HTTP_HEADER];
 
-    http_buf_peek(queue, tmp, 4096);
-    return package_check(tmp);
+    // only peek at the header..
+    http_buf_peek(queue, tmp, MAX_HTTP_HEADER-1);
+    // still we only return non-zero if the complete request is received..
+    return package_check(tmp, http_buf_size(queue));
 }
 
 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
 {
-    char tmp[4096];
+    char tmp[MAX_HTTP_HEADER];
     struct http_response *r = http_create_response(c);
     char *p, *p2;
     struct http_header **hp = &r->headers;
 
-    if (len >= 4096)
+    if (len >= MAX_HTTP_HEADER)
         return 0;
     memcpy(tmp, buf, len);
     for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
@@ -334,23 +422,62 @@ struct http_response *http_parse_response_buf(struct http_channel *c, const char
     return r;
 }
 
-struct http_request *http_parse_request(struct http_channel *c, struct http_buf **queue,
-        int len)
+static int http_parse_arguments(struct http_request *r, NMEM nmem,
+                                const char *args)
+{
+    const char *p2 = args;
+
+    while (*p2)
+    {
+        struct http_argument *a;
+        const char *equal = strchr(p2, '=');
+        const char *eoa = strchr(p2, '&');
+        if (!equal)
+        {
+            yaz_log(YLOG_WARN, "Expected '=' in argument");
+            return -1;
+        }
+        if (!eoa)
+            eoa = equal + strlen(equal); // last argument
+        else if (equal > eoa)
+        {
+            yaz_log(YLOG_WARN, "Missing '&' in argument");
+            return -1;
+        }
+        a = nmem_malloc(nmem, sizeof(struct http_argument));
+        a->name = nmem_strdupn(nmem, p2, equal - p2);
+        a->value = nmem_strdupn(nmem, equal+1, eoa - equal - 1);
+        urldecode(a->name, a->name);
+        urldecode(a->value, a->value);
+        a->next = r->arguments;
+        r->arguments = a;
+        p2 = eoa;
+        while (*p2 == '&')
+            p2++;
+    }
+    return 0;
+}
+
+struct http_request *http_parse_request(struct http_channel *c,
+                                        struct http_buf **queue,
+                                        int len)
 {
     struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
     char *p, *p2;
-    char tmp[4096];
-    char *buf = tmp;
+    char *start = nmem_malloc(c->nmem, len+1);
+    char *buf = start;
 
-    if (len > 4096)
-        return 0;
     if (http_buf_read(queue, buf, len) < len)
+    {
+        yaz_log(YLOG_WARN, "http_buf_read < len (%d)", len);
         return 0;
-
+    }
     r->search = "";
     r->channel = c;
     r->arguments = 0;
     r->headers = 0;
+    r->content_buf = 0;
+    r->content_len = 0;
     // Parse first line
     for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
         *(p2++) = *p;
@@ -363,13 +490,13 @@ struct http_request *http_parse_request(struct http_channel *c, struct http_buf
 
     if (!(buf = strchr(buf, ' ')))
     {
-        yaz_log(YLOG_WARN, "Syntax error in request (1)");
+        yaz_log(YLOG_WARN, "Missing Request-URI in HTTP request");
         return 0;
     }
     buf++;
     if (!(p = strchr(buf, ' ')))
     {
-        yaz_log(YLOG_WARN, "Syntax error in request (2)");
+        yaz_log(YLOG_WARN, "HTTP Request-URI not terminated (too long?)");
         return 0;
     }
     *(p++) = '\0';
@@ -380,29 +507,7 @@ struct http_request *http_parse_request(struct http_channel *c, struct http_buf
     {
         r->search = nmem_strdup(c->nmem, p2);
         // Parse Arguments
-        while (*p2)
-        {
-            struct http_argument *a;
-            char *equal = strchr(p2, '=');
-            char *eoa = strchr(p2, '&');
-            if (!equal)
-            {
-                yaz_log(YLOG_WARN, "Expected '=' in argument");
-                return 0;
-            }
-            if (!eoa)
-                eoa = equal + strlen(equal); // last argument
-            else
-                *(eoa++) = '\0';
-            a = nmem_malloc(c->nmem, sizeof(struct http_argument));
-            *(equal++) = '\0';
-            a->name = nmem_strdup(c->nmem, p2);
-            urldecode(equal, equal);
-            a->value = nmem_strdup(c->nmem, equal);
-            a->next = r->arguments;
-            r->arguments = a;
-            p2 = eoa;
-        }
+        http_parse_arguments(r, c->nmem, p2);
     }
     buf = p;
 
@@ -410,12 +515,15 @@ struct http_request *http_parse_request(struct http_channel *c, struct http_buf
         strcpy(r->http_version, "1.0");
     else
     {
-        buf += 5;
-        if (!(p = strstr(buf, "\r\n")))
+        size_t skipped;
+        buf += 5; // strlen("HTTP/")
+
+        p = (char*) next_crlf(buf, &skipped);
+        if (!p || skipped < 3 || skipped > 5)
             return 0;
-        *(p++) = '\0';
-        p++;
-        strcpy(r->http_version, buf);
+
+        memcpy(r->http_version, buf, skipped);
+        r->http_version[skipped] = '\0';
         buf = p;
     }
     strcpy(c->version, r->http_version);
@@ -423,32 +531,70 @@ struct http_request *http_parse_request(struct http_channel *c, struct http_buf
     r->headers = 0;
     while (*buf)
     {
-        if (!(p = strstr(buf, "\r\n")))
+        size_t skipped;
+
+        p = (char *) next_crlf(buf, &skipped);
+        if (!p)
+        {
             return 0;
-        if (p == buf)
+        }
+        else if (skipped == 0)
+        {
+            buf = p;
             break;
+        }
         else
         {
+            char *cp;
+            char *n_v = nmem_malloc(c->nmem, skipped+1);
             struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
-            if (!(p2 = strchr(buf, ':')))
+
+            memcpy(n_v, buf, skipped);
+            n_v[skipped] = '\0';
+
+            if (!(cp = strchr(n_v, ':')))
                 return 0;
-            *(p2++) = '\0';
-            h->name = nmem_strdup(c->nmem, buf);
-            while (isspace(*p2))
-                p2++;
-            if (p2 >= p) // Empty header?
-            {
-                buf = p + 2;
-                continue;
-            }
-            *p = '\0';
-            h->value = nmem_strdup(c->nmem, p2);
+            h->name = nmem_strdupn(c->nmem, n_v, cp - n_v);
+            cp++;
+            while (isspace(*cp))
+                cp++;
+            h->value = nmem_strdup(c->nmem, cp);
             h->next = r->headers;
             r->headers = h;
-            buf = p + 2;
+            buf = p;
         }
     }
 
+    // determine if we do keep alive
+    if (!strcmp(c->version, "1.0"))
+    {
+        const char *v = http_lookup_header(r->headers, "Connection");
+        if (v && !strcmp(v, "Keep-Alive"))
+            c->keep_alive = 1;
+        else
+            c->keep_alive = 0;
+    }
+    else
+    {
+        const char *v = http_lookup_header(r->headers, "Connection");
+        if (v && !strcmp(v, "close"))
+            c->keep_alive = 0;
+        else
+            c->keep_alive = 1;
+    }
+    if (buf < start + len)
+    {
+        const char *content_type = http_lookup_header(r->headers,
+                                                      "Content-Type");
+        r->content_len = start + len - buf;
+        r->content_buf = buf;
+
+        if (!yaz_strcmp_del("application/x-www-form-urlencoded",
+                            content_type, "; "))
+        {
+            http_parse_arguments(r, c->nmem, r->content_buf);
+        }
+    }
     return r;
 }
 
@@ -458,14 +604,28 @@ static struct http_buf *http_serialize_response(struct http_channel *c,
     struct http_header *h;
 
     wrbuf_rewind(c->wrbuf);
-    wrbuf_printf(c->wrbuf, "HTTP/1.1 %s %s\r\n", r->code, r->msg);
+    wrbuf_printf(c->wrbuf, "HTTP/%s %s %s\r\n", c->version, r->code, r->msg);
     for (h = r->headers; h; h = h->next)
         wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
     if (r->payload)
     {
-        wrbuf_printf(c->wrbuf, "Content-length: %d\r\n", r->payload ?
+        wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ?
                 (int) strlen(r->payload) : 0);
-        wrbuf_printf(c->wrbuf, "Content-type: text/xml\r\n");
+        wrbuf_printf(c->wrbuf, "Content-Type: %s\r\n", r->content_type);
+        if (!strcmp(r->content_type, "text/xml"))
+        {
+            xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload));
+            if (doc)
+            {
+                xmlFreeDoc(doc);
+            }
+            else
+            {
+                yaz_log(YLOG_WARN, "Sending non-wellformed "
+                        "response (bug #1162");
+                yaz_log(YLOG_WARN, "payload: %s", r->payload);
+            }
+        }
     }
     wrbuf_puts(c->wrbuf, "\r\n");
 
@@ -480,20 +640,10 @@ static struct http_buf *http_serialize_request(struct http_request *r)
 {
     struct http_channel *c = r->channel;
     struct http_header *h;
-    struct http_argument *a;
 
     wrbuf_rewind(c->wrbuf);
-    wrbuf_printf(c->wrbuf, "%s %s", r->method, r->path);
-
-    if (r->arguments)
-    {
-        wrbuf_putc(c->wrbuf, '?');
-        for (a = r->arguments; a; a = a->next) {
-            if (a != r->arguments)
-                wrbuf_putc(c->wrbuf, '&');
-            wrbuf_printf(c->wrbuf, "%s=%s", a->name, a->value);
-        }
-    }
+    wrbuf_printf(c->wrbuf, "%s %s%s%s", r->method, r->path,
+                 *r->search ? "?" : "", r->search);
 
     wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
 
@@ -501,7 +651,14 @@ static struct http_buf *http_serialize_request(struct http_request *r)
         wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
 
     wrbuf_puts(c->wrbuf, "\r\n");
-    
+
+    if (r->content_buf)
+        wrbuf_write(c->wrbuf, r->content_buf, r->content_len);
+
+#if 0
+    yaz_log(YLOG_LOG, "WRITING TO PROXY:\n%s\n----",
+            wrbuf_cstr(c->wrbuf));
+#endif
     return http_buf_bywrbuf(c->wrbuf);
 }
 
@@ -542,7 +699,33 @@ struct http_header * http_header_append(struct http_channel *ch,
     return hp;
 }
 
-    
+   
+static int is_inprogress(void)
+{
+#ifdef WIN32
+    if (WSAGetLastError() == WSAEWOULDBLOCK)
+        return 1;
+#else
+    if (errno == EINPROGRESS)
+        return 1;
+#endif
+    return 0;
+} 
+
+static void enable_nonblock(int sock)
+{
+    int flags;
+#ifdef WIN32
+    flags = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
+    if (ioctlsocket(sock, FIONBIO, &flags) < 0)
+        yaz_log(YLOG_FATAL|YLOG_ERRNO, "ioctlsocket");
+#else
+    if ((flags = fcntl(sock, F_GETFL, 0)) < 0) 
+        yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
+    if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
+        yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
+#endif
+}
 
 static int http_proxy(struct http_request *rq)
 {
@@ -559,7 +742,6 @@ static int http_proxy(struct http_request *rq)
         int sock;
         struct protoent *pe;
         int one = 1;
-        int flags;
 
         if (!(pe = getprotobyname("tcp"))) {
             abort();
@@ -572,35 +754,30 @@ static int http_proxy(struct http_request *rq)
         if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
                         &one, sizeof(one)) < 0)
             abort();
-        if ((flags = fcntl(sock, F_GETFL, 0)) < 0) 
-            yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
-        if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
-            yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
+        enable_nonblock(sock);
         if (connect(sock, (struct sockaddr *) proxy_addr, 
                     sizeof(*proxy_addr)) < 0)
-            if (errno != EINPROGRESS)
+        {
+            if (!is_inprogress()) 
             {
                 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
                 return -1;
             }
-
+        }
         p = xmalloc(sizeof(struct http_proxy));
         p->oqueue = 0;
         p->channel = c;
         p->first_response = 1;
         c->proxy = p;
         // We will add EVENT_OUTPUT below
-        p->iochan = iochan_create(sock, 0, proxy_io, EVENT_INPUT);
+        p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT);
         iochan_setdata(p->iochan, p);
-        p->iochan->next = channel_list;
-        channel_list = p->iochan;
+        pazpar2_add_channel(p->iochan);
     }
 
     // Do _not_ modify Host: header, just checking it's existence
-    for (hp = rq->headers; hp; hp = hp->next)
-        if (!strcmp(hp->name, "Host"))
-            break;
-    if (!hp)
+
+    if (!http_lookup_header(rq->headers, "Host"))
     {
         yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
         return -1;
@@ -610,23 +787,20 @@ static int http_proxy(struct http_request *rq)
     {
         hp = rq->headers;
         hp = http_header_append(c, hp, 
-                                PACKAGE_NAME "-version", PACKAGE_VERSION);
+                                "X-Pazpar2-Version", PACKAGE_VERSION);
         hp = http_header_append(c, hp, 
-                                PACKAGE_NAME "-server-host", ser->myurl);
+                                "X-Pazpar2-Server-Host", ser->host);
         sprintf(server_port, "%d",  ser->port);
         hp = http_header_append(c, hp, 
-                                PACKAGE_NAME "-server-port", server_port);
+                                "X-Pazpar2-Server-Port", server_port);
         sprintf(server_via,  "1.1 %s:%s (%s/%s)",  
-                ser->myurl, server_port, PACKAGE_NAME, PACKAGE_VERSION);
-        hp = http_header_append(c, hp, 
-                                "Via" , server_via);
-        //hp = http_header_append(c, hp,"Client-ip", 
-        //                        c->iochan->addr_str);
-        hp = http_header_append(c, hp,"X-Forwarded-For", 
-                                c->iochan->addr_str);
-      }
-
+                ser->host, server_port, PACKAGE_NAME, PACKAGE_VERSION);
+        hp = http_header_append(c, hp, "Via" , server_via);
+        hp = http_header_append(c, hp, "X-Forwarded-For", c->addr);
+    }
+    
     requestbuf = http_serialize_request(rq);
+
     http_buf_enqueue(&p->oqueue, requestbuf);
     iochan_setflag(p->iochan, EVENT_OUTPUT);
     return 0;
@@ -652,6 +826,22 @@ void http_send_response(struct http_channel *ch)
     }
 }
 
+static void http_error(struct http_channel *hc, int no, const char *msg)
+{
+    struct http_response *rs = http_create_response(hc);
+
+    hc->response = rs;
+    hc->keep_alive = 0;  // not keeping this HTTP session alive
+
+    sprintf(rs->code, "%d", no);
+
+    rs->msg = nmem_strdup(hc->nmem, msg);
+    rs->payload = nmem_malloc(hc->nmem, 100);
+    yaz_snprintf(rs->payload, 99, "<error>HTTP Error %d: %s</error>\n",
+                 no, msg);
+    http_send_response(hc);
+}
+
 static void http_io(IOCHAN i, int event)
 {
     struct http_channel *hc = iochan_getdata(i);
@@ -663,7 +853,7 @@ static void http_io(IOCHAN i, int event)
 
         case EVENT_INPUT:
             htbuf = http_buf_create();
-            res = read(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1);
+            res = recv(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1, 0);
             if (res == -1 && errno == EAGAIN)
             {
                 http_buf_destroy(htbuf);
@@ -675,51 +865,47 @@ static void http_io(IOCHAN i, int event)
                 http_destroy(i);
                 return;
             }
-            if (res > 0)
-            {
-                htbuf->buf[res] = '\0';
-                htbuf->len = res;
-                http_buf_enqueue(&hc->iqueue, htbuf);
-            }
-
-            if (hc->state == Http_Busy)
-                return;
-            if ((reqlen = request_check(hc->iqueue)) <= 2)
-                return;
+            htbuf->buf[res] = '\0';
+            htbuf->len = res;
+            http_buf_enqueue(&hc->iqueue, htbuf);
 
-            nmem_reset(hc->nmem);
-            if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
+            while (1)
             {
-                yaz_log(YLOG_WARN, "Failed to parse request");
-                http_destroy(i);
-                return;
-            }
-            hc->response = 0;
-            yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
-                    hc->request->path,
-                    *hc->request->search ? "?" : "",
-                    hc->request->search);
-            if (http_weshouldproxy(hc->request))
-                http_proxy(hc->request);
-            else
-            {
-                // Execute our business logic!
-                hc->state = Http_Busy;
-                http_command(hc);
-            }
-            if (hc->iqueue)
-            {
-                yaz_log(YLOG_DEBUG, "We think we have more input to read. Forcing event");
-                iochan_setevent(i, EVENT_INPUT);
+                if (hc->state == Http_Busy)
+                    return;
+                reqlen = request_check(hc->iqueue);
+                if (reqlen <= 2)
+                    return;
+                // we have a complete HTTP request
+                nmem_reset(hc->nmem);
+                if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
+                {
+                    yaz_log(YLOG_WARN, "Failed to parse request");
+                    http_error(hc, 400, "Bad Request");
+                    return;
+                }
+                hc->response = 0;
+                yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
+                        hc->request->path,
+                        *hc->request->search ? "?" : "",
+                        hc->request->search);
+                if (hc->request->content_buf)
+                    yaz_log(YLOG_LOG, "%s", hc->request->content_buf);
+                if (http_weshouldproxy(hc->request))
+                    http_proxy(hc->request);
+                else
+                {
+                    // Execute our business logic!
+                    hc->state = Http_Busy;
+                    http_command(hc);
+                }
             }
-
             break;
-
         case EVENT_OUTPUT:
             if (hc->oqueue)
             {
                 struct http_buf *wb = hc->oqueue;
-                res = write(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len);
+                res = send(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len, 0);
                 if (res <= 0)
                 {
                     yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
@@ -737,7 +923,7 @@ static void http_io(IOCHAN i, int event)
                     wb->offset += res;
                 }
                 if (!hc->oqueue) {
-                    if (!strcmp(hc->version, "1.0"))
+                    if (!hc->keep_alive)
                     {
                         http_destroy(i);
                         return;
@@ -760,25 +946,6 @@ static void http_io(IOCHAN i, int event)
     }
 }
 
-#ifdef GAGA
-// If this hostname contains our proxy host as a prefix, replace with myurl
-static char *sub_hostname(struct http_channel *c, char *buf)
-{
-    char tmp[1024];
-    if (strlen(buf) > 1023)
-        return buf;
-    if (strncmp(buf, "http://", 7))
-        return buf;
-    if (!strncmp(buf + 7, proxy_url, strlen(proxy_url)))
-    {
-        strcpy(tmp, myurl);
-        strcat(tmp, buf + strlen(proxy_url) + 7);
-        return nmem_strdup(c->nmem, tmp);
-    }
-    return buf;
-}
-#endif
-
 // Handles I/O on a client connection to a backend web server (proxy mode)
 static void proxy_io(IOCHAN pi, int event)
 {
@@ -792,15 +959,19 @@ static void proxy_io(IOCHAN pi, int event)
 
         case EVENT_INPUT:
             htbuf = http_buf_create();
-            res = read(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1);
-            if (res == 0 || (res < 0 && errno != EINPROGRESS))
+            res = recv(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1, 0);
+            if (res == 0 || (res < 0 && !is_inprogress()))
             {
                 if (hc->oqueue)
                 {
                     yaz_log(YLOG_WARN, "Proxy read came up short");
                     // Close channel and alert client HTTP channel that we're gone
                     http_buf_destroy(htbuf);
+#ifdef WIN32
+                    closesocket(iochan_getfd(pi));
+#else
                     close(iochan_getfd(pi));
+#endif
                     iochan_destroy(pi);
                     pc->iochan = 0;
                 }
@@ -815,33 +986,6 @@ static void proxy_io(IOCHAN pi, int event)
                 htbuf->buf[res] = '\0';
                 htbuf->offset = 0;
                 htbuf->len = res;
-#ifdef GAGA
-                if (pc->first_response) // Check if this is a redirect
-                {
-                    int len;
-                    if ((len = package_check(htbuf->buf)))
-                    {
-                        struct http_response *res = http_parse_response_buf(hc, htbuf->buf, len);
-                        if (res)
-                        {
-                            struct http_header *h;
-                            for (h = res->headers; h; h = h->next)
-                                if (!strcmp(h->name, "Location"))
-                                {
-                                    // We found a location header. Rewrite it.
-                                    struct http_buf *buf;
-                                    h->value = sub_hostname(hc, h->value);
-                                    buf = http_serialize_response(hc, res);
-                                    yaz_log(YLOG_LOG, "Proxy rewrite");
-                                    http_buf_enqueue(&hc->oqueue, buf);
-                                    htbuf->offset = len;
-                                    break;
-                                }
-                        }
-                    }
-                    pc->first_response = 0;
-                }
-#endif
                 // Write any remaining payload
                 if (htbuf->len - htbuf->offset > 0)
                     http_buf_enqueue(&hc->oqueue, htbuf);
@@ -854,7 +998,7 @@ static void proxy_io(IOCHAN pi, int event)
                 iochan_clearflag(pi, EVENT_OUTPUT);
                 return;
             }
-            res = write(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len);
+            res = send(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len, 0);
             if (res <= 0)
             {
                 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
@@ -862,7 +1006,7 @@ static void proxy_io(IOCHAN pi, int event)
                 return;
             }
             if (res == htbuf->len)
-            {
+            { 
                 struct http_buf *np = htbuf->next;
                 http_buf_destroy(htbuf);
                 pc->oqueue = np;
@@ -883,6 +1027,9 @@ static void proxy_io(IOCHAN pi, int event)
     }
 }
 
+static void http_fire_observers(struct http_channel *c);
+static void http_destroy_observers(struct http_channel *c);
+
 // Cleanup channel
 static void http_destroy(IOCHAN i)
 {
@@ -892,19 +1039,31 @@ static void http_destroy(IOCHAN i)
     {
         if (s->proxy->iochan)
         {
+#ifdef WIN32
+            closesocket(iochan_getfd(s->proxy->iochan));
+#else
             close(iochan_getfd(s->proxy->iochan));
+#endif
             iochan_destroy(s->proxy->iochan);
         }
         http_buf_destroy_queue(s->proxy->oqueue);
         xfree(s->proxy);
     }
+    http_buf_destroy_queue(s->iqueue);
+    http_buf_destroy_queue(s->oqueue);
+    http_fire_observers(s);
+    http_destroy_observers(s);
     s->next = http_channel_freelist;
     http_channel_freelist = s;
+#ifdef WIN32
+    closesocket(iochan_getfd(i));
+#else
     close(iochan_getfd(i));
+#endif
     iochan_destroy(i);
 }
 
-static struct http_channel *http_create(void)
+static struct http_channel *http_create(const char *addr)
 {
     struct http_channel *r = http_channel_freelist;
 
@@ -924,8 +1083,16 @@ static struct http_channel *http_create(void)
     r->iochan = 0;
     r->iqueue = r->oqueue = 0;
     r->state = Http_Idle;
+    r->keep_alive = 0;
     r->request = 0;
     r->response = 0;
+    if (!addr)
+    {
+        yaz_log(YLOG_WARN, "Invalid HTTP forward address");
+        exit(1);
+    }
+    strcpy(r->addr, addr);
+    r->observers = 0;
     return r;
 }
 
@@ -938,7 +1105,6 @@ static void http_accept(IOCHAN i, int event)
     socklen_t len;
     int s;
     IOCHAN c;
-    int flags;
     struct http_channel *ch;
 
     len = sizeof addr;
@@ -947,24 +1113,22 @@ static void http_accept(IOCHAN i, int event)
         yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
         return;
     }
-    if ((flags = fcntl(s, F_GETFL, 0)) < 0) 
-        yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
-    if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
-        yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
+    enable_nonblock(s);
 
     yaz_log(YLOG_DEBUG, "New command connection");
-    c = iochan_create(s, &addr, http_io, EVENT_INPUT | EVENT_EXCEPT);
-
-    ch = http_create();
+    c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT);
+    
+    ch = http_create(inet_ntoa(addr.sin_addr));
     ch->iochan = c;
     iochan_setdata(c, ch);
 
-    c->next = channel_list;
-    channel_list = c;
+    pazpar2_add_channel(c);
 }
 
+static int listener_socket = 0;
+
 /* Create a http-channel listener, syntax [host:]port */
-void http_init(const char *addr)
+int http_init(const char *addr)
 {
     IOCHAN c;
     int l;
@@ -972,9 +1136,9 @@ void http_init(const char *addr)
     struct sockaddr_in myaddr;
     int one = 1;
     const char *pp;
-    int port;
+    short port;
 
-    yaz_log(YLOG_LOG, "HTTP listener is %s", addr);
+    yaz_log(YLOG_LOG, "HTTP listener %s", addr);
 
     memset(&myaddr, 0, sizeof myaddr);
     myaddr.sin_family = AF_INET;
@@ -987,11 +1151,11 @@ void http_init(const char *addr)
 
         strncpy(hostname, addr, len);
         hostname[len] = '\0';
-        if (!(he = gethostbyname(hostname)))
-        {
+        if (!(he = gethostbyname(hostname))){
             yaz_log(YLOG_FATAL, "Unable to resolve '%s'", hostname);
-            exit(1);
+            return 1;
         }
+        
         memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
         port = atoi(pp + 1);
     }
@@ -1000,37 +1164,60 @@ void http_init(const char *addr)
         port = atoi(addr);
         myaddr.sin_addr.s_addr = INADDR_ANY;
     }
+
     myaddr.sin_port = htons(port);
 
     if (!(p = getprotobyname("tcp"))) {
-        abort();
+        return 1;
     }
     if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
         yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
     if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
                     &one, sizeof(one)) < 0)
-        abort();
+        return 1;
 
     if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0) 
+    {
         yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
+        return 1;
+    }
     if (listen(l, SOMAXCONN) < 0) 
+    {
         yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
+        return 1;
+    }
+
+    listener_socket = l;
 
-    c = iochan_create(l, &myaddr, http_accept, EVENT_INPUT | EVENT_EXCEPT);
-    c->next = channel_list;
-    channel_list = c;
+    c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT);
+    pazpar2_add_channel(c);
+    return 0;
+}
+
+void http_close_server(void)
+{
+    /* break the event_loop (select) by closing down the HTTP listener sock */
+    if (listener_socket)
+    {
+#ifdef WIN32
+        closesocket(listener_socket);
+#else
+        close(listener_socket);
+#endif
+    }
 }
 
 void http_set_proxyaddr(char *host, char *base_url)
 {
     char *p;
-    int port;
+    short port;
     struct hostent *he;
 
     strcpy(myurl, base_url);
     strcpy(proxy_url, host);
     p = strchr(host, ':');
     yaz_log(YLOG_DEBUG, "Proxying for %s", host);
+    yaz_log(YLOG_LOG, "HTTP backend  %s", proxy_url);
     if (p) {
         port = atoi(p + 1);
         *p = '\0';
@@ -1048,6 +1235,62 @@ void http_set_proxyaddr(char *host, char *base_url)
     proxy_addr->sin_port = htons(port);
 }
 
+static void http_fire_observers(struct http_channel *c)
+{
+    http_channel_observer_t p = c->observers;
+    while (p)
+    {
+        p->destroy(p->data, c, p->data2);
+        p = p->next;
+    }
+}
+
+static void http_destroy_observers(struct http_channel *c)
+{
+    while (c->observers)
+    {
+        http_channel_observer_t obs = c->observers;
+        c->observers = obs->next;
+        xfree(obs);
+    }
+}
+
+http_channel_observer_t http_add_observer(struct http_channel *c, void *data,
+                                          http_channel_destroy_t des)
+{
+    http_channel_observer_t obs = xmalloc(sizeof(*obs));
+    obs->chan = c;
+    obs->data = data;
+    obs->data2 = 0;
+    obs->destroy= des;
+    obs->next = c->observers;
+    c->observers = obs;
+    return obs;
+}
+
+void http_remove_observer(http_channel_observer_t obs)
+{
+    struct http_channel *c = obs->chan;
+    http_channel_observer_t found, *p = &c->observers;
+    while (*p != obs)
+        p = &(*p)->next;
+    found = *p;
+    assert(found);
+    *p = (*p)->next;
+    xfree(found);
+}
+
+struct http_channel *http_channel_observer_chan(http_channel_observer_t obs)
+{
+    return obs->chan;
+}
+
+void http_observer_set_data2(http_channel_observer_t obs, void *data2)
+{
+    obs->data2 = data2;
+}
+
+
 /*
  * Local variables:
  * c-basic-offset: 4