relay: do forward data if the http request type was HEAD
[project/uhttpd.git] / relay.c
diff --git a/relay.c b/relay.c
index 5de5cb4..7331a0a 100644 (file)
--- a/relay.c
+++ b/relay.c
@@ -28,6 +28,7 @@ void uh_relay_free(struct relay *r)
        if (r->proc.pending)
                kill(r->proc.pid, SIGKILL);
 
+       uloop_timeout_cancel(&r->timeout);
        uloop_process_delete(&r->proc);
        ustream_free(&r->sfd.stream);
        close(r->sfd.fd.fd);
@@ -55,6 +56,7 @@ static void relay_error(struct relay *r)
        struct ustream *s = &r->sfd.stream;
        int len;
 
+       r->error = true;
        s->eof = true;
        ustream_get_read_buf(s, &len);
        if (len)
@@ -76,6 +78,9 @@ static void relay_process_headers(struct relay *r)
                char *val;
 
                buf = ustream_get_read_buf(s, &len);
+               if (!buf || !len)
+                       break;
+
                newline = strchr(buf, '\n');
                if (!newline)
                        break;
@@ -112,7 +117,11 @@ static void relay_read_cb(struct ustream *s, int bytes)
        char *buf;
        int len;
 
-       relay_process_headers(r);
+       if (r->process_done)
+               uloop_timeout_set(&r->timeout, 1);
+
+       if (!r->error)
+               relay_process_headers(r);
 
        if (r->header_cb) {
                /*
@@ -130,15 +139,23 @@ static void relay_read_cb(struct ustream *s, int bytes)
        }
 
        buf = ustream_get_read_buf(s, &len);
-       uh_chunk_write(cl, buf, len);
+       if (!buf || !len)
+               return;
+
+       if (!r->skip_data)
+               uh_chunk_write(cl, buf, len);
+
        ustream_consume(s, len);
 }
 
-static void relay_close_if_done(struct relay *r)
+static void relay_close_if_done(struct uloop_timeout *timeout)
 {
+       struct relay *r = container_of(timeout, struct relay, timeout);
        struct ustream *s = &r->sfd.stream;
 
-       if (!s->eof || ustream_pending_data(s, false))
+       while (ustream_poll(&r->sfd.stream));
+
+       if (!(r->process_done || s->eof) || ustream_pending_data(s, false))
                return;
 
        uh_relay_close(r, r->ret);
@@ -149,17 +166,16 @@ static void relay_state_cb(struct ustream *s)
        struct relay *r = container_of(s, struct relay, sfd.stream);
 
        if (r->process_done)
-               relay_close_if_done(r);
+               uloop_timeout_set(&r->timeout, 1);
 }
 
 static void relay_proc_cb(struct uloop_process *proc, int ret)
 {
        struct relay *r = container_of(proc, struct relay, proc);
 
-       ustream_poll(&r->sfd.stream);
        r->process_done = true;
        r->ret = ret;
-       relay_close_if_done(r);
+       uloop_timeout_set(&r->timeout, 1);
 }
 
 void uh_relay_kill(struct client *cl, struct relay *r)
@@ -184,4 +200,6 @@ void uh_relay_open(struct client *cl, struct relay *r, int fd, int pid)
        r->proc.pid = pid;
        r->proc.cb = relay_proc_cb;
        uloop_process_add(&r->proc);
+
+       r->timeout.cb = relay_close_if_done;
 }