]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Add url_rewrite helper section
authorAmos Jeffries <squid3@treenet.co.nz>
Sat, 11 Jul 2009 09:38:29 +0000 (21:38 +1200)
committerAmos Jeffries <squid3@treenet.co.nz>
Sat, 11 Jul 2009 09:38:29 +0000 (21:38 +1200)
 - 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

12 files changed:
CREDITS
compat/compat.h
compat/helper_debug.h [new file with mode: 0644]
configure.in
helpers/ntlm_auth/smb_lm/ntlm_smb_lm_auth.c
helpers/ntlm_auth/smb_lm/ntlm_smb_lm_auth.h
helpers/url_rewrite/Makefile.am [new file with mode: 0644]
helpers/url_rewrite/fake/Makefile.am [new file with mode: 0644]
helpers/url_rewrite/fake/config.test [new file with mode: 0755]
helpers/url_rewrite/fake/fake.cc [new file with mode: 0644]
helpers/url_rewrite/fake/fake.h [new file with mode: 0644]
helpers/url_rewrite/fake/url_fake_rewrite.sh [new file with mode: 0644]

diff --git a/CREDITS b/CREDITS
index e9daf0d32d3fa88bbba022a7fc084d52a14ec374..4a00651cc0bbc047024e302015612ead910b9a60 100644 (file)
--- 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.
 
index bacbc5aae2f75d485f401602856dad467c7e9c55..69f0be36eb98eef6c17692fa3537c5bbd0cd88cc 100644 (file)
 #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 (file)
index 0000000..59b5ff2
--- /dev/null
@@ -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 <stdio.h>
+#endif
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#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 */
index bac2065b148a039ca81a556e4e0cf951f59e1392..223e7c5f94366bca84e8c6d19504ccf4fb0c418c 100644 (file)
@@ -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
 ])
 
index 80a9c3008c8a57ca3adfe233836e9051a4f3e6c2..597150bf8f42dc05a0603f9f31b17d87eb15a5bd 100644 (file)
@@ -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[])
index de63809b057bf5f146a8296f6795e8e4d6498ccc..50a2aa199c64ff322a034b8145760d56a99d7afd 100644 (file)
 
 
 /* Debugging stuff */
-
+#ifndef debug /* already provided */
 #ifdef __GNUC__                        /* this is really a gcc-ism */
 #ifdef DEBUG
 #include <stdio.h>
 #include <unistd.h>
 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 (file)
index 0000000..f439bcb
--- /dev/null
@@ -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 (file)
index 0000000..a2085c6
--- /dev/null
@@ -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 (executable)
index 0000000..039e4d0
--- /dev/null
@@ -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 (file)
index 0000000..5b9cfd9
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * AUTHOR: Amos Jeffries <squid3@treenet.co.nz>
+ *
+ * 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 (file)
index 0000000..e69de29
diff --git a/helpers/url_rewrite/fake/url_fake_rewrite.sh b/helpers/url_rewrite/fake/url_fake_rewrite.sh
new file mode 100644 (file)
index 0000000..bcfdf26
--- /dev/null
@@ -0,0 +1,28 @@
+#!/bin/sh
+#
+# Author: Amos Jeffries <squid3@treenet.co.nz>
+#
+# 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