Removed the GAGA code sections.
[pazpar2-moved-to-github.git] / src / http.c
index 781a7d0..cdf2c26 100644 (file)
@@ -1,7 +1,5 @@
-/* $Id: http.c,v 1.39 2007-09-26 08:53:53 adam Exp $
-   Copyright (c) 2006-2007, Index Data.
-
-This file is part of Pazpar2.
+/* 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
@@ -14,10 +12,10 @@ 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 Pazpar2; see the file LICENSE.  If not, write to the
-Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.
- */
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+*/
 
 #include <stdio.h>
 #include <sys/socket.h>
@@ -68,7 +66,8 @@ static struct http_channel *http_channel_freelist = 0;
 
 struct http_channel_observer_s {
     void *data;
-    void (*destroy)(void *data, struct http_channel *chan);
+    void *data2;
+    http_channel_destroy_t destroy;
     struct http_channel_observer_s *next;
     struct http_channel *chan;
 };
@@ -117,17 +116,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;
@@ -292,6 +280,7 @@ 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;
 }
 
@@ -483,13 +472,13 @@ struct http_request *http_parse_request(struct http_channel *c,
 
     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';
@@ -603,8 +592,8 @@ static struct http_buf *http_serialize_response(struct http_channel *c,
     {
         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");
-        if (1)
+        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)
@@ -913,25 +902,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)
 {
@@ -968,33 +938,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)
-                        {
-                            const char *location = http_lookup_header(
-                                res->header, "Location");
-                            if (location)
-                            {
-                                // We found a location header. Rewrite it.
-                                struct http_buf *buf;
-                                h->value = sub_hostname(hc, location);
-                                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);
@@ -1227,7 +1170,7 @@ static void http_fire_observers(struct http_channel *c)
     http_channel_observer_t p = c->observers;
     while (p)
     {
-        p->destroy(p->data, c);
+        p->destroy(p->data, c, p->data2);
         p = p->next;
     }
 }
@@ -1248,6 +1191,7 @@ http_channel_observer_t http_add_observer(struct http_channel *c, void *data,
     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;
@@ -1271,6 +1215,12 @@ 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