aboutsummaryrefslogtreecommitdiffstats
path: root/src/tipideed
diff options
context:
space:
mode:
Diffstat (limited to 'src/tipideed')
-rw-r--r--src/tipideed/deps-exe/tipideed1
-rw-r--r--src/tipideed/redirection.c7
-rw-r--r--src/tipideed/rproxy.c97
-rw-r--r--src/tipideed/tipideed-internal.h5
-rw-r--r--src/tipideed/tipideed.c5
5 files changed, 110 insertions, 5 deletions
diff --git a/src/tipideed/deps-exe/tipideed b/src/tipideed/deps-exe/tipideed
index a47d715..67d619a 100644
--- a/src/tipideed/deps-exe/tipideed
+++ b/src/tipideed/deps-exe/tipideed
@@ -4,6 +4,7 @@ harden.o
options.o
regular.o
redirection.o
+rproxy.o
send_file.o
stream.o
trace.o
diff --git a/src/tipideed/redirection.c b/src/tipideed/redirection.c
index e525e66..ce32e44 100644
--- a/src/tipideed/redirection.c
+++ b/src/tipideed/redirection.c
@@ -15,14 +15,15 @@ void respond_30x (tipidee_rql const *rql, tipidee_redirection const *rd)
static unsigned int const status[4] = { 307, 308, 302, 301 } ;
tipidee_defaulttext dt ;
tain deadline ;
- tipidee_util_defaulttext(status[rd->type], &dt) ;
- tipidee_response_status(buffer_1, rql, status[rd->type], dt.reason) ;
+ unsigned int type = rd->type & 3 ;
+ tipidee_util_defaulttext(status[type], &dt) ;
+ tipidee_response_status(buffer_1, rql, status[type], dt.reason) ;
tipidee_response_header_writeall_G(buffer_1, g.rhdr, g.rhdrn, 0) ;
buffer_putsnoflush(buffer_1, "Content-Length: 0\r\nLocation: ") ;
buffer_putsnoflush(buffer_1, rd->location) ;
if (rd->sub) buffer_putsnoflush(buffer_1, rd->sub) ;
buffer_putnoflush(buffer_1, "\r\n\r\n", 4) ;
- tipidee_log_answer(g.logv, rql, status[rd->type], 0) ;
+ tipidee_log_answer(g.logv, rql, status[type], 0) ;
tain_add_g(&deadline, &g.writetto) ;
if (!buffer_timed_flush_g(buffer_1, &deadline))
strerr_diefu1sys(111, "write to stdout") ;
diff --git a/src/tipideed/rproxy.c b/src/tipideed/rproxy.c
new file mode 100644
index 0000000..5b0ec29
--- /dev/null
+++ b/src/tipideed/rproxy.c
@@ -0,0 +1,97 @@
+/* ISC license. */
+
+#include <stdint.h>
+#include <unistd.h>
+#include <strings.h>
+
+#include <skalibs/uint16.h>
+#include <skalibs/fmtscan.h>
+#include <skalibs/tai.h>
+#include <skalibs/buffer.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/socket.h>
+#include <skalibs/ip46.h>
+#include <skalibs/unix-timed.h>
+
+#include <tipidee/tipidee.h>
+#include "tipideed-internal.h"
+
+#define putit(b, s, deadline) if (buffer_timed_puts_g(b, s, deadline) == -1) die500sys(rql, 111, docroot, "write to ", fn)
+
+static void do_rproxy (tipidee_rql const *rql, int fd, char const *sub, char const *docroot, tipidee_headers const *hdr, char const *body, size_t bodylen, tain const *deadline, char const *fn)
+{
+ char buf[4096] ;
+ buffer b = BUFFER_INIT(&buffer_write, fd, buf, 4096) ;
+ putit(&b, tipidee_method_tostr(rql->m), deadline) ;
+ putit(&b, " ", deadline) ;
+ putit(&b, rql->uri.path, deadline) ;
+ if (rql->uri.query)
+ {
+ putit(&b, "?", deadline) ;
+ putit(&b, rql->uri.query, deadline) ;
+ }
+ putit(&b, " HTTP/1.", deadline) ;
+ putit(&b, rql->http_minor == 1 ? "1" : "0", deadline) ;
+ putit(&b, "\r\nConnection: close\r\n", deadline) ;
+ for (uint32_t i = 0 ; i < hdr->n ; i++)
+ {
+ if (!strcasecmp(hdr->buf + hdr->list[i].left, "Connection")) continue ;
+ putit(&b, hdr->buf + hdr->list[i].left, deadline) ;
+ putit(&b, ": ", deadline) ;
+ putit(&b, hdr->buf + hdr->list[i].right, deadline) ;
+ putit(&b, "\r\n", deadline) ;
+ }
+ putit(&b, "\r\n", deadline) ;
+ if (!buffer_timed_flush_g(&b, deadline)) die500sys(rql, 111, docroot, "write to ", fn) ;
+ if (bodylen)
+ {
+ if (timed_write_g(fd, body, bodylen, deadline) < bodylen) die500sys(rql, 111, docroot, "write to ", fn) ;
+ }
+ fd_shutdown(fd, 1) ;
+ if (ndelay_off(fd) == -1) die500sys(rql, 111, docroot, "set socket parameters for ", fn) ;
+ fd_cat(fd, 1) ;
+ _exit(0) ; /* expensive but the only way to avoid implementing a full proxy */
+}
+
+static void rproxy_unix (tipidee_rql const *rql, char const *socketpath, char const *sub, char const *docroot, tipidee_headers const *hdr, char const *body, size_t bodylen)
+{
+ tain deadline ;
+ int fd = ipc_stream_nbcoe() ;
+ if (fd == -1) die500sys(rql, 111, docroot, "create socket") ;
+ tain_add_g(&deadline, &g.readtto) ;
+ if (!ipc_timed_connect_g(fd, socketpath, &deadline)) die500sys(rql, 111, docroot, "connect to ", socketpath) ;
+ do_rproxy(rql, fd, sub, docroot, hdr, body, bodylen, &deadline, socketpath) ;
+ fd_close(fd) ;
+}
+
+static void rproxy_tcp (tipidee_rql const *rql, char const *ip, uint16_t port, int is6, char const *sub, char const *docroot, tipidee_headers const *hdr, char const *body, size_t bodylen)
+{
+ tain deadline ;
+ size_t m = 0 ;
+ int fd = socket_tcp46_nbcoe(is6) ;
+ char fmt[IP6_FMT + UINT16_FMT + 2] ;
+ if (fd == -1) die500sys(rql, 111, docroot, "create socket") ;
+ tain_add_g(&deadline, &g.readtto) ;
+ if (is6) fmt[m++] = '[' ;
+ m += is6 ? ip6_fmt(fmt + m, ip) : ip4_fmt(fmt + m, ip) ;
+ if (is6) fmt[m++] = ']' ;
+ fmt[m++] = ':' ;
+ m += uint16_fmt(fmt + m, port) ;
+ fmt[m++] = 0 ;
+
+ if (!(is6 ? socket_deadlineconnstamp6_g(fd, ip, port, &deadline) : socket_deadlineconnstamp4_g(fd, ip, port, &deadline)))
+ die500sys(rql, 111, docroot, "connect to ", fmt) ;
+ do_rproxy(rql, fd, sub, docroot, hdr, body, bodylen, &deadline, fmt) ;
+ fd_close(fd) ;
+}
+
+void rproxy (tipidee_rql const *rql, tipidee_redirection const *rd, char const *docroot, tipidee_headers const *hdr, char const *body, size_t bodylen)
+{
+ if (rd->type & 16) rproxy_unix(rql, rd->location, rd->sub, docroot, hdr, body, bodylen) ;
+ else
+ {
+ uint16_t port ;
+ uint16_unpack_big(rd->location, &port) ;
+ rproxy_tcp(rql, rd->location + 2, port, !!(rd->type & 8), rd->sub, docroot, hdr, body, bodylen) ;
+ }
+}
diff --git a/src/tipideed/tipideed-internal.h b/src/tipideed/tipideed-internal.h
index e8cf102..d3a66f6 100644
--- a/src/tipideed/tipideed-internal.h
+++ b/src/tipideed/tipideed-internal.h
@@ -124,6 +124,11 @@ extern void respond_416 (tipidee_rql const *, char const *, uint64_t) ;
extern void respond_30x (tipidee_rql const *, tipidee_redirection const *) ;
+ /* rproxy */
+
+extern void rproxy (tipidee_rql const *, tipidee_redirection const *, char const *, tipidee_headers const *, char const *, size_t) ;
+
+
/* trace */
extern int respond_trace (tipidee_rql const *, tipidee_headers const *) ;
diff --git a/src/tipideed/tipideed.c b/src/tipideed/tipideed.c
index c32cd2c..6cfd4ec 100644
--- a/src/tipideed/tipideed.c
+++ b/src/tipideed/tipideed.c
@@ -233,7 +233,7 @@ static inline int serve (tipidee_rql *rql, char const *docroot, char *uribuf, ti
memcpy(fn + docrootlen, rql->uri.path, pathlen) ;
fn[docrootlen + pathlen] = 0 ;
- /* Redirection */
+ /* Redirection or reverse proxy */
if (rql->m != TIPIDEE_METHOD_OPTIONS)
{
@@ -242,7 +242,8 @@ static inline int serve (tipidee_rql *rql, char const *docroot, char *uribuf, ti
if (e == -1) die500sys(rql, 111, docroot, "get redirection data for ", fn) ;
if (e)
{
- respond_30x(rql, &rd) ;
+ if (rd.type & 32) rproxy(rql, &rd, docroot, hdr, body, bodylen) ;
+ else respond_30x(rql, &rd) ;
return 0 ;
}
}