From: Amos Jeffries Date: Sat, 11 Jul 2009 09:38:29 +0000 (+1200) Subject: Add url_rewrite helper section X-Git-Tag: SQUID_3_2_0_1~741^2~19 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fdbb3b19d56573065b7e392e27161ae77ce85aaa;p=thirdparty%2Fsquid.git Add url_rewrite helper section - Introduces --enable-url-rewrite-helpers configure option identical to other helper options. Selects which of the new helpers to build. - Adds a fake url_rewrite program in C++ binary and shell scripts formats each perform no re-write but can log all requests made. TODO: * add other helpers for various purposes. * make these demo/debug helpers capable of concurrent requests --- diff --git a/CREDITS b/CREDITS index e9daf0d32d..4a00651cc0 100644 --- a/CREDITS +++ b/CREDITS @@ -391,8 +391,9 @@ lib/libTrie/*: include/IPAddress.h lib/IPAddress.cc: include/rfc3596.h lib/rfc3596.cc: src/ICMPv6.h src/ICMPv6.cc: +helpers/url_rewrite/fake/ fake.h, fake.cc, url_fake_rewrite.sh: - This code is copyright (C) 2007 by Treehouse Networks Ltd + This code is copyright (C) 2007-2009 by Treehouse Networks Ltd of New Zealand. It is published and Licensed as an extension of squid under the same conditions as the main squid application. diff --git a/compat/compat.h b/compat/compat.h index bacbc5aae2..69f0be36eb 100644 --- a/compat/compat.h +++ b/compat/compat.h @@ -74,10 +74,14 @@ #include "compat/stdvarargs.h" #include "compat/assert.h" + /*****************************************************/ /* component-specific portabilities */ /*****************************************************/ +/* helper debugging requires some hacks to be clean */ +#include "compat/helper_debug.h" + /* Valgrind API macros changed between two versions squid supports */ #include "compat/valgrind.h" diff --git a/compat/helper_debug.h b/compat/helper_debug.h new file mode 100644 index 0000000000..59b5ff2428 --- /dev/null +++ b/compat/helper_debug.h @@ -0,0 +1,45 @@ +#ifndef SQUID_CONFIG_H +#include "config.h" +#endif + +#ifndef COMPAT_HELPER_DEBUG_H +#define COMPAT_HELPER_DEBUG_H + +/* + * A debug method for use of external helpers. + * It shunts the debug messages down stderr for logging by Squid + * of display to the user instead of corrupting the stdout data stream. + */ + +#if HAVE_STDIO_H +#include +#endif +#if HAVE_UNISTD_H +#include +#endif + +/* Debugging stuff */ + +/* the macro overload style is really a gcc-ism */ +#ifdef __GNUC__ + +SQUIDCEXTERN int debug_enabled; + +#define helper_debug(X...) \ + if (debug_enabled) { \ + fprintf(stderr, "%s(%d): pid=%ld :", __FILE__, __LINE__, (long)getpid() ); \ + fprintf(stderr,X); \ + } + +#else /* __GNUC__ */ + +/* TODO: non-GCC compilers can't do the above macro define yet. */ +inline void +helper_debug(char *format,...) +{ + ; // nothing to do. +} +#endif + + +#endif /* COMPAT_HELPER_DEBUG_H */ diff --git a/configure.in b/configure.in index bac2065b14..223e7c5f94 100644 --- a/configure.in +++ b/configure.in @@ -1854,6 +1854,48 @@ if test -n "$EXTERNAL_ACL_HELPERS"; then fi AC_SUBST(EXTERNAL_ACL_HELPERS) +dnl Select url_rewrite helpers to build +URL_REWRITE_HELPERS=all +AC_ARG_ENABLE(url-rewrite-helpers, + AC_HELP_STRING([--enable-url-rewrite-helpers="list of helpers"], + [This option selects which url_rewrite helpers to + build and install as part of the normal build + process. For a list of available helpers see the + helpers/url_rewrite directory.]), +[ case "$enableval" in + yes) + URL_REWRITE_HELPERS=all + ;; + no) + URL_REWRITE_HELPERS="" + ;; + *) + URL_REWRITE_HELPERS="`echo $enableval| sed -e 's/,/ /g;s/ */ /g'`" + ;; + esac +]) +if test "$URL_REWRITE_HELPERS" = "all" ; then + URL_REWRITE_HELPERS="" + for dir in $srcdir/helpers/url_rewrite/*; do + helper="`basename $dir`" + if test -f $dir/config.test && sh $dir/config.test "$@"; then + URL_REWRITE_HELPERS="$URL_REWRITE_HELPERS $helper" + fi + done +fi +if test -n "$URL_REWRITE_HELPERS"; then + for helper in $URL_REWRITE_HELPERS; do + if test -f $srcdir/helpers/url_rewrite/$helper/Makefile.in; then + : + else + AC_MSG_ERROR(url_rewrite helper $helper does not exist) + fi + done + AC_MSG_NOTICE([url_rewrite helpers built: $URL_REWRITE_HELPERS]) +fi +AC_SUBST(URL_REWRITE_HELPERS) + + AC_ARG_WITH(valgrind-debug, AC_HELP_STRING([--with-valgrind-debug], [Include debug instrumentation for use with valgrind]), @@ -3915,6 +3957,8 @@ AC_CONFIG_FILES([\ helpers/external_acl/wbinfo_group/Makefile \ helpers/external_acl/mswin_ad_group/Makefile \ helpers/external_acl/mswin_lm_group/Makefile \ + helpers/url_rewrite/Makefile \ + helpers/url_rewrite/fake/Makefile \ tools/Makefile ]) diff --git a/helpers/ntlm_auth/smb_lm/ntlm_smb_lm_auth.c b/helpers/ntlm_auth/smb_lm/ntlm_smb_lm_auth.c index 80a9c3008c..597150bf8f 100644 --- a/helpers/ntlm_auth/smb_lm/ntlm_smb_lm_auth.c +++ b/helpers/ntlm_auth/smb_lm/ntlm_smb_lm_auth.c @@ -155,7 +155,7 @@ usage() my_program_name, my_program_name); } -char debug_enabled=0; +int debug_enabled=0; void process_options(int argc, char *argv[]) diff --git a/helpers/ntlm_auth/smb_lm/ntlm_smb_lm_auth.h b/helpers/ntlm_auth/smb_lm/ntlm_smb_lm_auth.h index de63809b05..50a2aa199c 100644 --- a/helpers/ntlm_auth/smb_lm/ntlm_smb_lm_auth.h +++ b/helpers/ntlm_auth/smb_lm/ntlm_smb_lm_auth.h @@ -45,13 +45,13 @@ /* Debugging stuff */ - +#ifndef debug /* already provided */ #ifdef __GNUC__ /* this is really a gcc-ism */ #ifdef DEBUG #include #include static const char *__foo; -extern char debug_enabled; +extern int debug_enabled; #define debug(X...) if (debug_enabled) { \ fprintf(stderr,"ntlm-auth[%ld](%s:%d): ", (long)getpid(), \ ((__foo=strrchr(__FILE__,'/'))==NULL?__FILE__:__foo+1),\ @@ -65,9 +65,8 @@ static void debug(char *format,...) { } - #endif - +#endif /* debug already defined */ /* A couple of harmless helper macros */ #define SEND(X) debug("sending '%s' to squid\n",X); printf(X "\n"); diff --git a/helpers/url_rewrite/Makefile.am b/helpers/url_rewrite/Makefile.am new file mode 100644 index 0000000000..f439bcb2bc --- /dev/null +++ b/helpers/url_rewrite/Makefile.am @@ -0,0 +1,3 @@ + +DIST_SUBDIRS = fake +SUBDIRS = @URL_REWRITE_HELPERS@ diff --git a/helpers/url_rewrite/fake/Makefile.am b/helpers/url_rewrite/fake/Makefile.am new file mode 100644 index 0000000000..a2085c6420 --- /dev/null +++ b/helpers/url_rewrite/fake/Makefile.am @@ -0,0 +1,6 @@ +include $(top_srcdir)/src/Common.am + +libexec_PROGRAMS = url_fake_rewrite url_fake_rewrite.sh + +url_fake_rewrite_SOURCES = fake.cc +url_fake_rewrite_LDADD = $(COMPAT_LIBS) diff --git a/helpers/url_rewrite/fake/config.test b/helpers/url_rewrite/fake/config.test new file mode 100755 index 0000000000..039e4d0069 --- /dev/null +++ b/helpers/url_rewrite/fake/config.test @@ -0,0 +1,2 @@ +#!/bin/sh +exit 0 diff --git a/helpers/url_rewrite/fake/fake.cc b/helpers/url_rewrite/fake/fake.cc new file mode 100644 index 0000000000..5b9cfd931d --- /dev/null +++ b/helpers/url_rewrite/fake/fake.cc @@ -0,0 +1,109 @@ +/* + * AUTHOR: Amos Jeffries + * + * Example url re-writer program for Squid. + * + * This code gets the url and returns it. No re-writing is done. + * It is intended for testing use and as a base for further implementation. + * + * + * This code is copyright (C) 2009 by Treehouse Networks Ltd + * of New Zealand. It is published and Licensed as an extension of + * squid under the same conditions as the main squid application. + */ + +#include "config.h" + +#define BUFFER_SIZE 10240 + +/** + * options: + * -d enable debugging. + * -h interface help. + */ +char *my_program_name = NULL; +int concurrent_detected = -1; + + +static void +usage(void) +{ + fprintf(stderr, + "Usage: %s [-d] [-v] [-h]\n" + " -d enable debugging.\n" + " -h this message\n\n", + my_program_name); +} + +static void +process_options(int argc, char *argv[]) +{ + int opt, had_error = 0; + + opterr = 0; + while (-1 != (opt = getopt(argc, argv, "hd"))) { + switch (opt) { + case 'd': + debug_enabled = 1; + break; + case 'h': + usage(); + exit(0); + case '?': + opt = optopt; + /* fall thru to default */ + default: + fprintf(stderr, "unknown option: -%c. Exiting\n", opt); + usage(); + had_error = 1; + } + } + if (had_error) + exit(1); +} + +bool +detect_concurrent(const char *) +{ + // TODO: scan the char* input and see if it is 100% numeric. + // if so, enable concurrent support IDs. +} + +int +main(int argc, char *argv[]) +{ + char buf[BUFFER_SIZE]; + int buflen = 0; + char helper_command[3]; + + setbuf(stdout, NULL); + setbuf(stderr, NULL); + + my_program_name = argv[0]; + + process_options(argc, argv); + + helper_debug("%s build " __DATE__ ", " __TIME__ " starting up...\n", my_program_name); + + while (fgets(buf, BUFFER_SIZE, stdin) != NULL) { + + if ((p = strchr(buf, '\n')) != NULL) { + *p = '\0'; /* strip \n */ + buflen = p - buf; /* length is known already */ + } + else + buflen = strlen(buf); /* keep this so we only scan the buffer for \0 once per loop */ + +/* TODO: later. + if (concurrent_detected < 0) + detect_concurrent(buf); +// */ + + helper_debug("Got %d bytes '%s' from Squid\n", buflen, buf); + + /* send 'no-change' result back to Squid */ + fprintf(stdout,"\n"); + } + helper_debug("%s build " __DATE__ ", " __TIME__ " shutting down...\n", my_program_name); + exit(0); +} diff --git a/helpers/url_rewrite/fake/fake.h b/helpers/url_rewrite/fake/fake.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/helpers/url_rewrite/fake/url_fake_rewrite.sh b/helpers/url_rewrite/fake/url_fake_rewrite.sh new file mode 100644 index 0000000000..bcfdf26f56 --- /dev/null +++ b/helpers/url_rewrite/fake/url_fake_rewrite.sh @@ -0,0 +1,28 @@ +#!/bin/sh +# +# Author: Amos Jeffries +# +# This code is copyright (C) 2009 by Treehouse Networks Ltd +# of New Zealand. It is published and Licensed as an extension of +# squid under the same conditions as the main squid application. +# + +if test "${1}" = "-d" ; then + echo "Usage: $0 [-h] [-d logfile]" + echo " -h Help: this help text" + echo " -d logfile Debug: log all data received to the named file" + exit 1 +fi + +DEBUG=0 +if test "${1}" = "-d" ; then + DEBUG=1 + LOG="${2}" +fi + +while read url rest; do + if test ${DEBUG} ; then + echo "$url $rest" >>${LOG} + fi + echo # blank line for no change, or replace with another URL. +done