Masterkey: detailed view working.
[pazpar2-moved-to-github.git] / src / http.c
index 291f0d9..1e5058a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: http.c,v 1.10 2007-02-05 16:15:41 quinn Exp $
+ * $Id: http.c,v 1.12 2007-03-15 16:50:56 quinn Exp $
  */
 
 #include <stdio.h>
@@ -168,7 +168,8 @@ static int http_buf_read(struct http_buf **b, char *buf, int len)
     return rd;
 }
 
-void static urldecode(char *i, char *o)
+// Buffers may overlap.
+static void urldecode(char *i, char *o)
 {
     while (*i)
     {
@@ -190,6 +191,23 @@ void static urldecode(char *i, char *o)
     *o = '\0';
 }
 
+// Warning: Buffers may not overlap
+void urlencode(const char *i, char *o)
+{
+    while (*i)
+    {
+        if (strchr(" /:", *i))
+        {
+            sprintf(o, "%%%.2X", (int) *i);
+            o += 3;
+        }
+        else
+            *(o++) = *i;
+        i++;
+    }
+    *o = '\0';
+}
+
 void http_addheader(struct http_response *r, const char *name, const char *value)
 {
     struct http_channel *c = r->channel;
@@ -734,8 +752,8 @@ static void proxy_io(IOCHAN pi, int event)
             else
             {
                 htbuf->buf[res] = '\0';
+                htbuf->offset = 0;
                 htbuf->len = res;
-                int offset = 0;
                 if (pc->first_response) // Check if this is a redirect
                 {
                     int len;
@@ -752,8 +770,9 @@ static void proxy_io(IOCHAN pi, int event)
                                     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);
-                                    offset = len;
+                                    htbuf->offset = len;
                                     break;
                                 }
                         }
@@ -761,15 +780,8 @@ static void proxy_io(IOCHAN pi, int event)
                     pc->first_response = 0;
                 }
                 // Write any remaining payload
-                if (htbuf->len - offset > 0)
-                {
-                    if (offset > 0)
-                    {
-                        memmove(htbuf->buf, htbuf->buf + offset, htbuf->len - offset);
-                        htbuf->len -= offset;
-                    }
-                    http_buf_enqueue(&hc->oqueue, htbuf + offset);
-                }
+                if (htbuf->len - htbuf->offset > 0)
+                    http_buf_enqueue(&hc->oqueue, htbuf);
             }
             iochan_setflag(hc->iochan, EVENT_OUTPUT);
             break;