/*
* uhttpd - Tiny single-threaded httpd
*
- * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
- * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
+ * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
*
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <arpa/inet.h>
#include "uhttpd.h"
#define __headers \
- __header(accept) \
- __header(accept_charset) \
- __header(accept_encoding) \
- __header(accept_language) \
- __header(authorization) \
- __header(connection) \
- __header(cookie) \
- __header(host) \
- __header(referer) \
- __header(user_agent) \
- __header(content_type) \
- __header(content_length)
+ __header(accept, accept) \
+ __header(accept_charset, accept-charset) \
+ __header(accept_encoding, accept-encoding) \
+ __header(accept_language, accept-language) \
+ __header(authorization, authorization) \
+ __header(connection, connection) \
+ __header(cookie, cookie) \
+ __header(host, host) \
+ __header(origin, origin) \
+ __header(referer, referer) \
+ __header(user_agent, user-agent) \
+ __header(content_type, content-type) \
+ __header(content_length, content-length) \
+ __header(x_http_method_override, x-http-method-override) \
+ __header(http_auth_user, http-auth-user) \
+ __header(http_auth_pass, http-auth-pass)
#undef __header
#define __header __enum_header
{ "HTTP_CONNECTION", HDR_connection },
{ "HTTP_COOKIE", HDR_cookie },
{ "HTTP_HOST", HDR_host },
+ { "HTTP_ORIGIN", HDR_origin },
{ "HTTP_REFERER", HDR_referer },
{ "HTTP_USER_AGENT", HDR_user_agent },
+ { "HTTP_X_HTTP_METHOD_OVERRIDE", HDR_x_http_method_override },
+ { "HTTP_AUTH_USER", HDR_http_auth_user },
+ { "HTTP_AUTH_PASS", HDR_http_auth_pass },
{ "CONTENT_TYPE", HDR_content_type },
{ "CONTENT_LENGTH", HDR_content_length },
};
VAR_METHOD,
VAR_PATH_INFO,
VAR_USER,
+ VAR_HTTPS,
VAR_REDIRECT,
VAR_SERVER_NAME,
VAR_SERVER_ADDR,
[VAR_METHOD] = { "REQUEST_METHOD" },
[VAR_PATH_INFO] = { "PATH_INFO" },
[VAR_USER] = { "REMOTE_USER" },
+ [VAR_HTTPS] = { "HTTPS" },
[VAR_REDIRECT] = { "REDIRECT_STATUS", redirect_status },
[VAR_SERVER_NAME] = { "SERVER_NAME", local_addr },
[VAR_SERVER_ADDR] = { "SERVER_ADDR", local_addr },
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;
+ extra_vars[VAR_HTTPS].value = cl->tls ? "on" : NULL;
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));
+ snprintf(local_port, sizeof(local_port), "%d", cl->srv_addr.port);
inet_ntop(cl->peer_addr.family, &cl->peer_addr.in, remote_addr, sizeof(remote_addr));
+ snprintf(remote_port, sizeof(remote_port), "%d", cl->peer_addr.port);
blobmsg_parse(hdr_policy, __HDR_MAX, tb, blob_data(data), blob_len(data));
for (i = 0; i < ARRAY_SIZE(proc_header_env); i++) {
cur = tb[proc_header_env[i].idx];
vars[i].name = proc_header_env[i].name;
- vars[i].value = cur ? blobmsg_data(cur) : "";
+ vars[i].value = cur ? blobmsg_data(cur) : NULL;
}
memcpy(&vars[i], extra_vars, sizeof(extra_vars));
static void proc_close_fds(struct client *cl)
{
- close(cl->dispatch.proc.r.sfd.fd.fd);
+ struct dispatch_proc *p = &cl->dispatch.proc;
+
+ close(p->r.sfd.fd.fd);
+ if (p->wrfd.fd >= 0)
+ close(p->wrfd.fd);
}
static void proc_handle_close(struct relay *r, int ret)
uh_http_header(cl, cl->dispatch.proc.status_code, cl->dispatch.proc.status_msg);
blob_for_each_attr(cur, cl->dispatch.proc.hdr.head, rem)
- ustream_printf(cl->us, "%s: %s\r\n", blobmsg_name(cur), blobmsg_data(cur));
+ ustream_printf(cl->us, "%s: %s\r\n", blobmsg_name(cur),
+ blobmsg_get_string(cur));
ustream_printf(cl->us, "\r\n");
-}
-static void proc_free(struct client *cl)
-{
- uh_relay_free(&cl->dispatch.proc.r);
+ if (cl->request.method == UH_HTTP_MSG_HEAD)
+ r->skip_data = true;
}
static void proc_write_close(struct client *cl)
{
- shutdown(cl->dispatch.proc.r.sfd.fd.fd, SHUT_WR);
+ 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_relay_write_cb(struct ustream *us, int bytes)
+static void proc_free(struct client *cl)
{
- struct client *cl = container_of(us, struct client, dispatch.proc.r.sfd.stream);
+ struct dispatch_proc *p = &cl->dispatch.proc;
- if (ustream_pending_data(us, true))
- return;
+ uloop_timeout_cancel(&p->timeout);
+ blob_buf_free(&p->hdr);
+ proc_write_close(cl);
+ uh_relay_free(&p->r);
+}
+
+static void proc_write_cb(struct uloop_fd *fd, unsigned int events)
+{
+ struct client *cl = container_of(fd, struct client, dispatch.proc.wrfd);
- cl->dispatch.data_blocked = false;
- us->notify_write = NULL;
client_poll_post_data(cl);
}
-static void proc_relay_write_close_cb(struct ustream *us, int bytes)
+static void proc_relay_write_cb(struct client *cl)
{
- struct client *cl = container_of(us, struct client, dispatch.proc.r.sfd.stream);
+ struct dispatch_proc *p = &cl->dispatch.proc;
- if (ustream_pending_data(us, true))
+ if (ustream_pending_data(cl->us, true))
return;
- proc_write_close(cl);
+ ustream_set_read_blocked(&p->r.sfd.stream, false);
+ p->r.sfd.stream.notify_read(&p->r.sfd.stream, 0);
}
-static void proc_data_send(struct client *cl, const char *data, int len)
+static int proc_data_send(struct client *cl, const char *data, int len)
{
- struct ustream *us = &cl->dispatch.proc.r.sfd.stream;
+ 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;
- ustream_write(us, data, len, false);
- if (ustream_pending_data(us, true)) {
- cl->dispatch.data_blocked = true;
- us->notify_write = proc_relay_write_cb;
+ /* consume all data */
+ ret = len;
+ }
+
+ 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;
}
-static void proc_data_done(struct client *cl)
+static void proc_timeout_cb(struct uloop_timeout *timeout)
{
- struct ustream *us = &cl->dispatch.proc.r.sfd.stream;
+ struct dispatch_proc *proc = container_of(timeout, struct dispatch_proc, timeout);
+ struct client *cl = container_of(proc, struct client, dispatch.proc);
- if (ustream_pending_data(us, true)) {
- us->notify_write = proc_relay_write_close_cb;
- return;
- }
-
- proc_write_close(cl);
+ uh_relay_kill(cl, &proc->r);
}
-bool uh_create_process(struct client *cl, struct path_info *pi,
- void (*cb)(struct client *cl, struct path_info *pi, int fd))
+bool uh_create_process(struct client *cl, struct path_info *pi, char *url,
+ void (*cb)(struct client *cl, struct path_info *pi, char *url))
{
struct dispatch *d = &cl->dispatch;
- int fds[2];
+ struct dispatch_proc *proc = &d->proc;
+ int rfd[2], wfd[2];
int pid;
- blob_buf_init(&cl->dispatch.proc.hdr, 0);
- d->proc.status_code = 200;
- d->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, url);
exit(0);
}
- close(fds[1]);
- uh_relay_open(cl, &cl->dispatch.proc.r, fds[0], pid);
+ 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_data_done;
- d->proc.r.header_cb = proc_handle_header;
- d->proc.r.header_end = proc_handle_header_end;
- d->proc.r.close = proc_handle_close;
+ d->data_done = proc_write_close;
+ d->write_cb = proc_relay_write_cb;
+ 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;
+ proc->timeout.cb = proc_timeout_cb;
+ if (conf.script_timeout > 0)
+ uloop_timeout_set(&proc->timeout, conf.script_timeout * 1000);
return true;
+
+close_wfd:
+ close(wfd[0]);
+ close(wfd[1]);
+close_rfd:
+ close(rfd[0]);
+ close(rfd[1]);
+
+ return false;
}