Support Apache 2.4 client IP address lookups
authorDan Scott <dscott@laurentian.ca>
Thu, 17 Jan 2013 02:59:22 +0000 (21:59 -0500)
committerDan Scott <dscott@laurentian.ca>
Sat, 19 Jan 2013 18:45:38 +0000 (13:45 -0500)
The Apache 2.4 API replaces the conn_rec->remote_ip member with
conn_rec->client_ip or conn_rec->useragent_ip (per
http://httpd.apache.org/docs/2.4/developer/new_api_2_4.html). client_ip
seems to be the best choice for us, to avoid everything being logged
from the load balancer or proxy instead of the client's address.

This code detects the version of Apache and uses client_ip if it is 2.4
or greater.

Signed-off-by: Dan Scott <dscott@laurentian.ca>
Signed-off-by: Bill Erickson <berick@esilibrary.com>

configure.ac
src/gateway/Makefile.am
src/gateway/osrf_http_translator.c
src/gateway/osrf_json_gateway.c

index 3bca402..d23354d 100644 (file)
@@ -203,6 +203,29 @@ if ! test -d "$APR_HEADERS"; then
 fi
 AC_SUBST([APR_HEADERS])
 
+# The following Apache version detection code is adapted from
+# http://www.gnu.org/software/autoconf-archive/ax_prog_apache.html
+# licensed under version 2 of the GNU General Public License, or
+# (at your discretion) any later version.
+#
+# Copyright (c) 2008 Loic Dachary <loic@senga.org>
+#
+# Collect apache version number. If for nothing else, this
+# guarantees that httpd is a working apache executable.
+#
+changequote(<<, >>)dnl
+APACHE=`$APXS2 -q progname`
+APACHE_READABLE_VERSION=`$APACHE -v | grep 'Server version' | sed -e 's;.*Apache/\([0-9\.][0-9\.]*\).*;\1;'`
+changequote([, ])dnl
+APACHE_VERSION=`echo $APACHE_READABLE_VERSION | sed -e 's/\.//g'`
+if test -z "$APACHE_VERSION" ; then
+    AC_MSG_ERROR("could not determine apache version number");
+fi
+APACHE_MAJOR=`expr $APACHE_VERSION : '\(..\)'`
+APACHE_MINOR=`expr $APACHE_VERSION : '..\(.*\)'`
+AM_CONDITIONAL(APACHE_MIN_24, test "$APACHE_MAJOR" -ge "24")
+AC_SUBST([APACHE_MIN_24])
+
 AC_ARG_WITH([libxml],
 [  --with-libxml=path               location of the libxml2 headers (default is /usr/include/libxml2/))],
 [LIBXML2_HEADERS=${withval}],
@@ -371,6 +394,7 @@ fi
         AC_MSG_RESULT(APXS2 location:                  ${APXS2})
         AC_MSG_RESULT(Apache headers location:         ${APACHE2_HEADERS})
         AC_MSG_RESULT(APR headers location:            ${APR_HEADERS})
+        AC_MSG_RESULT(Apache version:                  ${APACHE_READABLE_VERSION})
         AC_MSG_RESULT(libxml2 headers location:        ${LIBXML2_HEADERS})
 
 AC_MSG_RESULT([----------------------------------------------------------------------])
index 3dafbdc..425a824 100644 (file)
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-
+#
+if APACHE_MIN_24
+HAVE_APACHE_MIN_24 = -DAPACHE_MIN_24
+endif
 
 EXTRA_DIST = @srcdir@/apachetools.c @srcdir@/apachetools.h @srcdir@/osrf_json_gateway.c @srcdir@/osrf_http_translator.c
 
-AM_CFLAGS = -D_LARGEFILE64_SOURCE -Wall -I@abs_top_srcdir@/include/ -I$(LIBXML2_HEADERS) -I$(APACHE2_HEADERS) -I$(APR_HEADERS)
+AM_CFLAGS = -D_LARGEFILE64_SOURCE $(HAVE_APACHE_MIN_24) -Wall -I@abs_top_srcdir@/include/ -I$(LIBXML2_HEADERS) -I$(APACHE2_HEADERS) -I$(APR_HEADERS)
 AM_LDFLAGS = -L$(LIBDIR) -L@top_builddir@/src/libopensrf
 AP_LIBEXECDIR = `$(APXS2) -q LIBEXECDIR`
 
index f6d492b..2fed04a 100644 (file)
@@ -117,7 +117,11 @@ static osrfHttpTranslator* osrfNewHttpTranslator(request_rec* apreq) {
     trans->disconnectOnly = 0;
     trans->connecting = 0;
     trans->disconnecting = 0;
+#ifdef APACHE_MIN_24
+    trans->remoteHost = apreq->connection->client_ip;
+#else
     trans->remoteHost = apreq->connection->remote_ip;
+#endif
     trans->messages = NULL;
 
     /* load the message body */
index ecc02a2..7d5f3f7 100644 (file)
@@ -288,8 +288,13 @@ static int osrf_json_gateway_method_handler (request_rec *r) {
                const char* authtoken = apr_table_get(r->headers_in, "X-OILS-Authtoken");
                if(!authtoken) authtoken = "";
                growing_buffer* act = buffer_init(128);
+#ifdef APACHE_MIN_24
+               buffer_fadd(act, "[%s] [%s] [%s] %s %s", r->connection->client_ip,
+                       authtoken, osrf_locale, service, method );
+#else
                buffer_fadd(act, "[%s] [%s] [%s] %s %s", r->connection->remote_ip,
                        authtoken, osrf_locale, service, method );
+#endif
 
                const char* str; int i = 0;
                int redact_params = 0;