use pipes instead of a socketpair, EOF handling is broken with sockets
[project/uhttpd.git] / proc.c
diff --git a/proc.c b/proc.c
index 9d662ad..aa9b8fc 100644 (file)
--- a/proc.c
+++ b/proc.c
@@ -17,6 +17,7 @@
  *  limitations under the License.
  */
 
+#include <arpa/inet.h>
 #include <libubox/blobmsg.h>
 #include "uhttpd.h"
 
@@ -81,10 +82,20 @@ enum extra_vars {
        VAR_PATH_INFO,
        VAR_USER,
        VAR_REDIRECT,
+       VAR_SERVER_NAME,
+       VAR_SERVER_ADDR,
+       VAR_SERVER_PORT,
+       VAR_REMOTE_NAME,
+       VAR_REMOTE_ADDR,
+       VAR_REMOTE_PORT,
 
        __VAR_MAX,
 };
 
+static char local_addr[INET6_ADDRSTRLEN], remote_addr[INET6_ADDRSTRLEN];
+static char local_port[6], remote_port[6];
+static char redirect_status[4];
+
 static struct env_var extra_vars[] = {
        [_VAR_GW] = { "GATEWAY_INTERFACE", "CGI/1.1" },
        [_VAR_SOFTWARE] = { "SERVER_SOFTWARE", "uhttpd" },
@@ -97,7 +108,13 @@ static struct env_var extra_vars[] = {
        [VAR_METHOD] = { "REQUEST_METHOD" },
        [VAR_PATH_INFO] = { "PATH_INFO" },
        [VAR_USER] = { "REMOTE_USER" },
-       [VAR_REDIRECT] = { "REDIRECT_STATUS" },
+       [VAR_REDIRECT] = { "REDIRECT_STATUS", redirect_status },
+       [VAR_SERVER_NAME] = { "SERVER_NAME", local_addr },
+       [VAR_SERVER_ADDR] = { "SERVER_ADDR", local_addr },
+       [VAR_SERVER_PORT] = { "SERVER_PORT", local_port },
+       [VAR_REMOTE_NAME] = { "REMOTE_HOST", remote_addr },
+       [VAR_REMOTE_ADDR] = { "REMOTE_ADDR", remote_addr },
+       [VAR_REMOTE_PORT] = { "REMOTE_PORT", remote_port },
 };
 
 struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi)
@@ -106,10 +123,11 @@ struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi)
        struct blob_attr *data = cl->hdr.head;
        struct env_var *vars = (void *) uh_buf;
        struct blob_attr *tb[__HDR_MAX];
-       static char buf[4];
+       const char *url;
        int len;
        int i;
 
+       url = blobmsg_data(blob_data(cl->hdr.head));
        len = ARRAY_SIZE(proc_header_env);
        len += ARRAY_SIZE(extra_vars);
        len *= sizeof(struct env_var);
@@ -120,14 +138,16 @@ struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi)
        extra_vars[VAR_SCRIPT_FILE].value = pi->phys;
        extra_vars[VAR_DOCROOT].value = pi->root;
        extra_vars[VAR_QUERY].value = pi->query ? pi->query : "";
-       extra_vars[VAR_REQUEST].value = req->url;
+       extra_vars[VAR_REQUEST].value = url;
        extra_vars[VAR_PROTO].value = http_versions[req->version];
        extra_vars[VAR_METHOD].value = http_methods[req->method];
        extra_vars[VAR_PATH_INFO].value = pi->info;
        extra_vars[VAR_USER].value = req->realm ? req->realm->user : NULL;
 
-       snprintf(buf, sizeof(buf), "%d", req->redirect_status);
-       extra_vars[VAR_REDIRECT].value = buf;
+       snprintf(redirect_status, sizeof(redirect_status),
+                "%d", req->redirect_status);
+       inet_ntop(cl->srv_addr.family, &cl->srv_addr.in, local_addr, sizeof(local_addr));
+       inet_ntop(cl->peer_addr.family, &cl->peer_addr.in, remote_addr, sizeof(remote_addr));
 
        blobmsg_parse(hdr_policy, __HDR_MAX, tb, blob_data(data), blob_len(data));
        for (i = 0; i < ARRAY_SIZE(proc_header_env); i++) {
@@ -169,7 +189,7 @@ static void proc_handle_header(struct relay *r, const char *name, const char *va
        char *sep;
        char buf[4];
 
-       if (strcmp(name, "Status")) {
+       if (!strcmp(name, "Status")) {
                sep = strchr(val, ' ');
                if (sep != val + 3)
                        return;
@@ -178,6 +198,7 @@ static void proc_handle_header(struct relay *r, const char *name, const char *va
                buf[3] = 0;
                snprintf(status_buf, sizeof(status_buf), "%s", sep + 1);
                cl->dispatch.proc.status_msg = status_buf;
+               cl->dispatch.proc.status_code = atoi(buf);
                return;
        }
 
@@ -197,45 +218,130 @@ static void proc_handle_header_end(struct relay *r)
        ustream_printf(cl->us, "\r\n");
 }
 
+static void proc_write_close(struct client *cl)
+{
+       struct dispatch_proc *p = &cl->dispatch.proc;
+
+       if (p->wrfd.fd < 0)
+               return;
+
+       uloop_fd_delete(&p->wrfd);
+       close(p->wrfd.fd);
+       p->wrfd.fd = -1;
+}
+
 static void proc_free(struct client *cl)
 {
+       proc_write_close(cl);
        uh_relay_free(&cl->dispatch.proc.r);
 }
 
+static void proc_write_cb(struct uloop_fd *fd, unsigned int events)
+{
+       struct client *cl = container_of(fd, struct client, dispatch.proc.wrfd);
+
+       client_poll_post_data(cl);
+}
+
+static int proc_data_send(struct client *cl, const char *data, int len)
+{
+       struct dispatch_proc *p = &cl->dispatch.proc;
+       int retlen = 0;
+       int ret;
+
+       while (len) {
+               ret = write(p->wrfd.fd, data, len);
+
+               if (ret < 0) {
+                       if (errno == EINTR)
+                               continue;
+
+                       if (errno == EAGAIN || errno == EWOULDBLOCK)
+                               break;
+
+                       /* error, no retry */
+                       len = 0;
+                       break;
+               }
+
+               if (!ret)
+                       break;
+
+               retlen += ret;
+               len -= ret;
+               data += ret;
+       }
+
+       if (len)
+               uloop_fd_add(&p->wrfd, ULOOP_WRITE);
+       else
+               uloop_fd_delete(&p->wrfd);
+
+       return retlen;
+}
+
 bool uh_create_process(struct client *cl, struct path_info *pi,
-                      void (*cb)(struct client *cl, struct path_info *pi, int fd))
+                      void (*cb)(struct client *cl, struct path_info *pi))
 {
-       int fds[2];
+       struct dispatch *d = &cl->dispatch;
+       struct dispatch_proc *proc = &d->proc;
+       int rfd[2], wfd[2];
        int pid;
 
-       blob_buf_init(&cl->dispatch.proc.hdr, 0);
-       cl->dispatch.proc.status_code = 200;
-       cl->dispatch.proc.status_msg = "OK";
+       blob_buf_init(&proc->hdr, 0);
+       proc->status_code = 200;
+       proc->status_msg = "OK";
 
-       if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
+       if (pipe(rfd))
                return false;
 
+       if (pipe(wfd))
+               goto close_rfd;
+
        pid = fork();
-       if (pid < 0) {
-               close(fds[0]);
-               close(fds[1]);
-               return false;
-       }
+       if (pid < 0)
+               goto close_wfd;
 
        if (!pid) {
-               close(fds[0]);
+               close(0);
+               close(1);
+
+               dup2(rfd[1], 1);
+               dup2(wfd[0], 0);
+
+               close(rfd[0]);
+               close(rfd[1]);
+               close(wfd[0]);
+               close(wfd[1]);
+
                uh_close_fds();
-               cb(cl, pi, fds[1]);
+               cb(cl, pi);
                exit(0);
        }
 
-       close(fds[1]);
-       uh_relay_open(cl, &cl->dispatch.proc.r, fds[0], pid);
-       cl->dispatch.free = proc_free;
-       cl->dispatch.close_fds = proc_close_fds;
-       cl->dispatch.proc.r.header_cb = proc_handle_header;
-       cl->dispatch.proc.r.header_end = proc_handle_header_end;
-       cl->dispatch.proc.r.close = proc_handle_close;
+       close(rfd[1]);
+       close(wfd[0]);
+
+       proc->wrfd.fd = wfd[1];
+       uh_relay_open(cl, &proc->r, rfd[0], pid);
+
+       d->free = proc_free;
+       d->close_fds = proc_close_fds;
+       d->data_send = proc_data_send;
+       d->data_done = proc_write_close;
+       proc->r.header_cb = proc_handle_header;
+       proc->r.header_end = proc_handle_header_end;
+       proc->r.close = proc_handle_close;
+       proc->wrfd.cb = proc_write_cb;
 
        return true;
+
+close_wfd:
+       close(wfd[0]);
+       close(wfd[1]);
+close_rfd:
+       close(rfd[0]);
+       close(rfd[1]);
+
+       return false;
 }