diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2014-10-31 15:55:19 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2014-10-31 15:55:19 +0200 |
commit | 7965195e932d5ed4844dbef1e2da6e871aea6fe1 (patch) | |
tree | 11a30844929feeb08fd5fc9299f17c51583d52b3 | |
parent | 5d7657a6b0c7c5f72dbee90f1025e9facab0b4c9 (diff) | |
download | vmod-dbrw-7965195e932d5ed4844dbef1e2da6e871aea6fe1.tar.gz vmod-dbrw-7965195e932d5ed4844dbef1e2da6e871aea6fe1.tar.bz2 |
Build for both v3 and v4
* NEWS: Update version number.
* configure.ac: Version 1.9.90
(VARNISHVERSION): New variable.
(VARNISH3,VARNISH4): New conditionals.
* src/Makefile.am (nodist_libvmod_dbrw_la_SOURCES): New variable.
(vmodtool,vmodtoolargs,vccfile): Define depending on Varnish version.
(.vcc.vcc3): New rule
(vcc_if.c vcc_if.h): Depend on $(vccfile)
Don't distribute vcc_if.c and vcc_if.h.
* src/vmod_dbrw.c: Provide data types and access macros depending
on the version of Varnish.
-rw-r--r-- | NEWS | 4 | ||||
-rw-r--r-- | configure.ac | 17 | ||||
-rw-r--r-- | src/Makefile.am | 28 | ||||
-rw-r--r-- | src/vmod_dbrw.c | 39 | ||||
-rw-r--r-- | src/vmod_dbrw.vcc | 26 |
5 files changed, 75 insertions, 39 deletions
@@ -2,15 +2,15 @@ vmod-dbrw -- history of user-visible changes. 2013-07-20 Copyright (C) 2013 Sergey Poznyakoff See the end of file for copying conditions. Please send vmod-dbrw bug reports to <gray@gnu.org> -Version 1.0.91-vcl4, (git) +Version 1.9.90, (git) -Rewritten for Varnish v4.x +Builds for both Varnish v3.x and v4.x Version 1.0, 2013-07-20 Initial release diff --git a/configure.ac b/configure.ac index e1c55eb..ce9087c 100644 --- a/configure.ac +++ b/configure.ac @@ -11,13 +11,13 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with vmod-dbrw. If not, see <http://www.gnu.org/licenses/>. AC_PREREQ(2.69) -AC_INIT([vmod-dbrw], 1.0.91-vcl4, [gray@gnu.org]) +AC_INIT([vmod-dbrw], 1.9.90, [gray@gnu.org]) AC_CONFIG_AUX_DIR([build-aux]) AC_CONFIG_MACRO_DIR([m4]) AC_CONFIG_SRCDIR(src/vmod_dbrw.vcc) AM_CONFIG_HEADER(config.h) AC_CANONICAL_SYSTEM @@ -61,16 +61,23 @@ fi case "$VARNISHSRC" in /*) ;; *) AC_MSG_ERROR([varnish source path must be absolute pathname]) esac VARNISHSRC=`cd $VARNISHSRC && pwd` -AC_CHECK_FILE([$VARNISHSRC/include/vapi/vsm.h], - [], - [AC_MSG_FAILURE(["$VARNISHSRC" is not a Varnish source directory])]) - +VARNISHVERSION= +AC_CHECK_FILE([$VARNISHSRC/include/varnishapi.h], + [VARNISHVERSION=3], + [AC_CHECK_FILE([$VARNISHSRC/include/vapi/vsm.h], + [VARNISHVERSION=4], + [AC_MSG_FAILURE(["$VARNISHSRC" is not a Varnish source directory])]) ]) + +AC_DEFINE_UNQUOTED([VARNISHVERSION],$VARNISHVERSION,[Varnish major version number]) +AM_CONDITIONAL([VARNISH3],[test $VARNISHVERSION -eq 3]) +AM_CONDITIONAL([VARNISH4],[test $VARNISHVERSION -eq 4]) + # Check that varnishtest is built in the varnish source directory AC_CHECK_FILE([$VARNISHSRC/bin/varnishtest/varnishtest], [], [AC_MSG_FAILURE([cannot find "$VARNISHSRC/bin/varnishtest/varnishtest". Please build your varnish source directory])] ) diff --git a/src/Makefile.am b/src/Makefile.am index dde9116..f7a5a45 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -23,31 +23,45 @@ libvmod_dbrw_la_LDFLAGS = -module -export-dynamic -avoid-version libvmod_dbrw_la_LIBADD=@MYSQLLIBS@ @PGSQLLIBS@ libvmod_dbrw_la_SOURCES = \ dbrw.h\ sql.c\ vmod_dbrw.c\ - vcc_if.c vcc_if.h\ wordsplit.h\ wordsplit.c +nodist_libvmod_dbrw_la_SOURCES = vcc_if.c vcc_if.h + +vmod_dbrw.lo: vcc_if.h if USE_MYSQL libvmod_dbrw_la_SOURCES += mysql.c endif if USE_PGSQL libvmod_dbrw_la_SOURCES += pgsql.c endif -BUILT_SOURCES = vcc_if.c vcc_if.h +CLEANFILES = vcc_if.c vcc_if.h *.rst + +if VARNISH4 + vmodtool = $(VARNISHSRC)/lib/libvcc/vmodtool.py + vmodtoolargs = --strict + vccfile = $(top_srcdir)/src/vmod_dbrw.vcc +else + vmodtool = $(VARNISHSRC)/lib/libvmod_std/vmod.py + vmodtoolargs = + vccfile = vmod_dbrw.vcc3 + +vmod_dbrw.vcc3: $(top_srcdir)/src/vmod_dbrw.vcc + CLEANFILES += vmod_dbrw.vcc3 +endif -vmodtool = $(VARNISHSRC)/lib/libvcc/vmodtool.py -vmodtoolargs = --strict +.vcc.vcc3: + sed -n '/^\$$/{s///;s/\(Module *[^ ][^ ]*\).*/\1/;p}' $< > $@ -vcc_if.c vcc_if.h: $(vmodtool) $(top_srcdir)/src/vmod_dbrw.vcc - @PYTHON@ $(vmodtool) $(vmodtoolargs) $(top_srcdir)/src/vmod_dbrw.vcc +vcc_if.c vcc_if.h: $(vmodtool) $(vccfile) + @PYTHON@ $(vmodtool) $(vmodtoolargs) $(vccfile) EXTRA_DIST = \ vmod_dbrw.vcc -CLEANFILES = $(builddir)/vcc_if.c $(builddir)/vcc_if.h diff --git a/src/vmod_dbrw.c b/src/vmod_dbrw.c index 284376f..9db6a0b 100644 --- a/src/vmod_dbrw.c +++ b/src/vmod_dbrw.c @@ -17,13 +17,24 @@ #include "dbrw.h" #include <stdarg.h> #include "wordsplit.h" #include "vrt.h" #include "vcc_if.h" #include "pthread.h" -#include "bin/varnishd/cache/cache.h" +#if VARNISHVERSION == 3 +# include "bin/varnishd/cache.h" +# define VCL_VOID void +# define VCL_STRING const char * +# define WS_Copy(w,s,l) WS_Dup(w,l) +# define DBRW_CTX struct sess * +# define WSPTR(s) ((s)->wrk->ws) +#else +# include "bin/varnishd/cache/cache.h" +# define DBRW_CTX const struct vrt_ctx * +# define WSPTR(s) ((s)->ws) +#endif static pthread_once_t thread_once = PTHREAD_ONCE_INIT; static pthread_key_t thread_key; enum { QDISP_NONE, @@ -201,13 +212,13 @@ parse_flags(const char *arg, int *qdisp, int *flags, char status[]) password=S options=S debug=N QUERY - Query to obtain the redirection target */ VCL_VOID -vmod_config(const struct vrt_ctx *ctx, struct vmod_priv *priv, +vmod_config(DBRW_CTX ctx, struct vmod_priv *priv, VCL_STRING bkname, VCL_STRING param, VCL_STRING query) { char *s; struct wordsplit ws; struct dbrw_config *conf = priv->priv; @@ -258,37 +269,37 @@ vmod_config(const struct vrt_ctx *ctx, struct vmod_priv *priv, conf->query = NULL; conf->param = NULL; } } static char * -expand_backref(const struct vrt_ctx *ctx, const char *str, const char *val, +expand_backref(DBRW_CTX ctx, const char *str, const char *val, size_t matchcount, regmatch_t *matches, char *qry) { unsigned u; char *b, *p; - u = WS_Reserve(ctx->ws, 0); - p = b = ctx->ws->f; + u = WS_Reserve(WSPTR(ctx), 0); + p = b = WSPTR(ctx)->f; while (*val) { if (u == 0) { - WS_Release(ctx->ws, 0); + WS_Release(WSPTR(ctx), 0); return NULL; } if (*val == '\\' || *val == '$') { if (val[1] >= 0 && val[1] <= '9') { int n = val[1] - '0'; if (n < matchcount) { size_t len; len = matches[n].rm_eo - matches[n].rm_so; if (len > u) { - WS_Release(ctx->ws, 0); + WS_Release(WSPTR(ctx), 0); return NULL; } memcpy(p, str + matches[n].rm_so, len); p += len; @@ -308,39 +319,39 @@ expand_backref(const struct vrt_ctx *ctx, const char *str, const char *val, u--; } if (qry) { size_t len = strlen(qry); if (len + 1 >= u) { - WS_Release(ctx->ws, 0); + WS_Release(WSPTR(ctx), 0); return NULL; } if (strchr(b, '?')) *p++ = '&'; else *p++ = '?'; memcpy(p, qry, len); p += len; u -= len; } if (u == 0) { - WS_Release(ctx->ws, 0); + WS_Release(WSPTR(ctx), 0); return NULL; } *p = 0; - WS_ReleaseP(ctx->ws, p); + WS_ReleaseP(WSPTR(ctx), p); return b; } #define ISEMPTY(s) ((s) == NULL || (s)[0] == 0) static char * -findmatch(const struct vrt_ctx *ctx, struct dbrw_connection *conn, char **param) +findmatch(DBRW_CTX ctx, struct dbrw_connection *conn, char **param) { unsigned i; unsigned nt, nf; char *res; struct wordsplit ws; int wsflags = WRDSF_NOCMD | WRDSF_QUOTE | @@ -351,13 +362,13 @@ findmatch(const struct vrt_ctx *ctx, struct dbrw_connection *conn, char **param) (nf = sql_num_fields(conn)) == 0) return NULL; debug(conn->conf, 2, ("query returned %u tuples, %u columns", nt, nf)); if (nf < 3) - return WS_Copy(ctx->ws, sql_get_column(conn, 0, 0), -1); + return WS_Copy(WSPTR(ctx), sql_get_column(conn, 0, 0), -1); /* Three or four fields: result pattern value [flags] @@ -373,13 +384,13 @@ findmatch(const struct vrt_ctx *ctx, struct dbrw_connection *conn, char **param) int rflags = conn->conf->regflags; char status[HTTP_STATUS_LEN+1]; int qdisp = conn->conf->qdisp; char *qry = NULL; if (ISEMPTY(pat)) { - res = WS_Copy(ctx->ws, sql_get_column(conn, i, 0), -1); + res = WS_Copy(WSPTR(ctx), sql_get_column(conn, i, 0), -1); break; } debug(conn->conf, 1, ("considering \"%s\" ~ \"%s\"", pat, val)); if (wordsplit(val, &ws, wsflags)) { dbrw_error("cannot expand string `%s': %s", @@ -495,13 +506,13 @@ get_connection(struct dbrw_config *conf) } } return cp; } VCL_STRING -vmod_rewrite(const struct vrt_ctx *ctx, struct vmod_priv *priv, VCL_STRING arg) +vmod_rewrite(DBRW_CTX ctx, struct vmod_priv *priv, VCL_STRING arg) { struct dbrw_config *conf = priv->priv; struct dbrw_connection *cp; struct wordsplit ws, wsenv; int i, rc; char *res; diff --git a/src/vmod_dbrw.vcc b/src/vmod_dbrw.vcc index a2ec9d2..10fef6f 100644 --- a/src/vmod_dbrw.vcc +++ b/src/vmod_dbrw.vcc @@ -28,33 +28,37 @@ httpd server, and that they are stored in an SQL database, which makes them easily manageable. $Init dbrw_init $Function VOID config(PRIV_VCL, STRING, STRING, STRING) Description - Configures the module and provides it with the data - necessary to connect and use the database. It is normally called - from the **vcl_init** subroutine. + Configures the module and provides it with the data + necessary to connect and use the database. It is normally called + from the **vcl_init** subroutine. Example - dbrw.config("mysql", - "database=rewrite;user=varnish;password=guessme", - {"SELECT dest - FROM redirects - WHERE host='$host' - AND url='$url'"}); + :: + + dbrw.config("mysql", + "database=rewrite;user=varnish;password=guessme", + {"SELECT dest + FROM redirects + WHERE host='$host' + AND url='$url'"}); $Function STRING rewrite(PRIV_VCL, STRING) Description Rewrites its argument using the database configured in the previous - call to `**config** and returns the obtained value. + call to **config** and returns the obtained value. Example - set req.http.X-Redirect-To = + :: + + set req.http.X-Redirect-To = dbrw.rewrite("host=" + req.http.Host + ";" + "url=" + req.url); SEE ALSO ======== |