From 4a87f75fcf72746c8881fa49328aacdef89b9bd6 Mon Sep 17 00:00:00 2001 From: Pascal Bach Date: Sat, 11 Jul 2015 11:20:18 +0000 Subject: [PATCH] [mod_proxy] add unix domain socket support (fixes #2653) If the server is set to a path like value (starting with "/") mod_proxy will try to establish a connection via unix domain socket. Signed-off-by: Pascal Bach git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@3002 152afb58-edef-0310-8abb-c4023f1b3aa9 --- NEWS | 1 + src/mod_proxy.c | 48 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/NEWS b/NEWS index a327802a..49e3fbfa 100644 --- a/NEWS +++ b/NEWS @@ -27,6 +27,7 @@ NEWS * [mod_magnet] fix segfault when accessing not existing lighty.req_env[] entry (found by coverity) * fix segfault when temp file for upload couldn't be created (found by coverity) * mime.conf: add some new mime types, remove .dat, .sha1, .md5, update .vcf + * [mod_proxy] add unix domain socket support (fixes #2653) - 1.4.35 - 2014-03-12 * [network/ssl] fix build error if TLSEXT is disabled diff --git a/src/mod_proxy.c b/src/mod_proxy.c index 78210727..b7e543fe 100644 --- a/src/mod_proxy.c +++ b/src/mod_proxy.c @@ -361,6 +361,9 @@ static void proxy_connection_close(server *srv, handler_ctx *hctx) { static int proxy_establish_connection(server *srv, handler_ctx *hctx) { struct sockaddr *proxy_addr; struct sockaddr_in proxy_addr_in; +#if defined(HAVE_SYS_UN_H) + struct sockaddr_un proxy_addr_un; +#endif #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON) struct sockaddr_in6 proxy_addr_in6; #endif @@ -371,6 +374,15 @@ static int proxy_establish_connection(server *srv, handler_ctx *hctx) { int proxy_fd = hctx->fd; +#if defined(HAVE_SYS_UN_H) + if (strstr(host->host->ptr, "/")) { + memset(&proxy_addr_un, 0, sizeof(proxy_addr_un)); + proxy_addr_un.sun_family = AF_UNIX; + strcpy(proxy_addr_un.sun_path, host->host->ptr); + servlen = sizeof(proxy_addr_un); + proxy_addr = (struct sockaddr *) &proxy_addr_un; + } else +#endif #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON) if (strstr(host->host->ptr, ":")) { memset(&proxy_addr_in6, 0, sizeof(proxy_addr_in6)); @@ -416,27 +428,27 @@ static int proxy_establish_connection(server *srv, handler_ctx *hctx) { } static void proxy_set_header(connection *con, const char *key, const char *value) { - data_string *ds_dst; + data_string *ds_dst; - if (NULL == (ds_dst = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) { - ds_dst = data_string_init(); - } + if (NULL == (ds_dst = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) { + ds_dst = data_string_init(); + } - buffer_copy_string(ds_dst->key, key); - buffer_copy_string(ds_dst->value, value); - array_insert_unique(con->request.headers, (data_unset *)ds_dst); + buffer_copy_string(ds_dst->key, key); + buffer_copy_string(ds_dst->value, value); + array_insert_unique(con->request.headers, (data_unset *)ds_dst); } static void proxy_append_header(connection *con, const char *key, const char *value) { - data_string *ds_dst; + data_string *ds_dst; - if (NULL == (ds_dst = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) { - ds_dst = data_string_init(); - } + if (NULL == (ds_dst = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) { + ds_dst = data_string_init(); + } - buffer_copy_string(ds_dst->key, key); - buffer_append_string(ds_dst->value, value); - array_insert_unique(con->request.headers, (data_unset *)ds_dst); + buffer_copy_string(ds_dst->key, key); + buffer_append_string(ds_dst->value, value); + array_insert_unique(con->request.headers, (data_unset *)ds_dst); } @@ -716,6 +728,14 @@ static handler_t proxy_write_request(server *srv, handler_ctx *hctx) { break; case PROXY_STATE_INIT: +#if defined(HAVE_SYS_UN_H) + if (strstr(host->host->ptr,"/")) { + if (-1 == (hctx->fd = socket(AF_UNIX, SOCK_STREAM, 0))) { + log_error_write(srv, __FILE__, __LINE__, "ss", "socket failed: ", strerror(errno)); + return HANDLER_ERROR; + } + } else +#endif #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON) if (strstr(host->host->ptr,":")) { if (-1 == (hctx->fd = socket(AF_INET6, SOCK_STREAM, 0))) {