summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn Strauss <gstrauss@gluelogic.com>2019-05-14 01:00:29 -0400
committerGlenn Strauss <gstrauss@gluelogic.com>2019-05-14 01:03:08 -0400
commitcb1c49a74a1e63bbb2a8572a8c0b06e45d80668b (patch)
tree622972a2f89f68280eaffa67e1f347206b478800
parent42942ec97c591b716724209cbe553273d086b4b6 (diff)
downloadlighttpd1.4-cb1c49a74a1e63bbb2a8572a8c0b06e45d80668b.tar.gz
lighttpd1.4-cb1c49a74a1e63bbb2a8572a8c0b06e45d80668b.zip
[mod_magnet] expose server addr (local IP) to lua
expose server addr (local IP) to lua via lighty.env["request.server-addr"] (read-only)
-rw-r--r--src/mod_magnet.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/mod_magnet.c b/src/mod_magnet.c
index 9af3482f..70d380e0 100644
--- a/src/mod_magnet.c
+++ b/src/mod_magnet.c
@@ -9,6 +9,7 @@
#include "plugin.h"
#include "mod_magnet_cache.h"
+#include "sock_addr.h"
#include "stat_cache.h"
#include "status_counter.h"
#include "etag.h"
@@ -489,6 +490,7 @@ typedef struct {
MAGNET_ENV_REQUEST_ORIG_URI,
MAGNET_ENV_REQUEST_PATH_INFO,
MAGNET_ENV_REQUEST_REMOTE_IP,
+ MAGNET_ENV_REQUEST_SERVER_ADDR,
MAGNET_ENV_REQUEST_PROTOCOL
} type;
} magnet_env_t;
@@ -510,6 +512,8 @@ static const magnet_env_t magnet_env[] = {
{ "request.orig-uri", MAGNET_ENV_REQUEST_ORIG_URI },
{ "request.path-info", MAGNET_ENV_REQUEST_PATH_INFO },
{ "request.remote-ip", MAGNET_ENV_REQUEST_REMOTE_IP },
+ { "request.remote-addr", MAGNET_ENV_REQUEST_REMOTE_IP },
+ { "request.server-addr", MAGNET_ENV_REQUEST_SERVER_ADDR },
{ "request.protocol", MAGNET_ENV_REQUEST_PROTOCOL },
{ NULL, MAGNET_ENV_UNSET }
@@ -546,6 +550,35 @@ static buffer *magnet_env_get_buffer_by_id(server *srv, connection *con, int id)
case MAGNET_ENV_REQUEST_ORIG_URI: dest = con->request.orig_uri; break;
case MAGNET_ENV_REQUEST_PATH_INFO: dest = con->request.pathinfo; break;
case MAGNET_ENV_REQUEST_REMOTE_IP: dest = con->dst_addr_buf; break;
+ case MAGNET_ENV_REQUEST_SERVER_ADDR:
+ dest = srv->tmp_buf;
+ buffer_clear(dest);
+ switch (con->srv_socket->addr.plain.sa_family) {
+ case AF_INET:
+ case AF_INET6:
+ if (sock_addr_is_addr_wildcard(&con->srv_socket->addr)) {
+ sock_addr addrbuf;
+ socklen_t addrlen = sizeof(addrbuf);
+ if (0 == getsockname(con->fd,(struct sockaddr *)&addrbuf,&addrlen)){
+ char buf[INET6_ADDRSTRLEN + 1];
+ const char *s = sock_addr_inet_ntop(&addrbuf, buf, sizeof(buf)-1);
+ if (NULL != s)
+ buffer_copy_string_len(dest, s, strlen(s));
+ }
+ }
+ else {
+ buffer_copy_buffer(dest, con->srv_socket->srv_token);
+ if (dest->ptr[0] != '[' || dest->ptr[buffer_string_length(dest)-1] != ']') {
+ char *s = strrchr(dest->ptr, ':');
+ if (s != NULL) /* local IP without port */
+ buffer_string_set_length(dest, s - dest->ptr);
+ }
+ }
+ break;
+ default:
+ break;
+ }
+ break;
case MAGNET_ENV_REQUEST_PROTOCOL:
buffer_copy_string(srv->tmp_buf, get_http_version_name(con->request.http_version));
dest = srv->tmp_buf;