From 5d7657a6b0c7c5f72dbee90f1025e9facab0b4c9 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Fri, 31 Oct 2014 12:38:57 +0200 Subject: Initial rewrite for Varnish 4.x * configure.ac: Check for include/vapi/vsm.h in varnish source tree. Change version number to 1.0.91-vcl4 * NEWS: Update version number. * src/Makefile.am (AM_CPPFLAGS): Add $(VARNISHSRC)/bin/varnishd (vmodtool,vmodtoolargs): New variables. (vcc_if.c vcc_if.h): Rewrite the rule. * src/vmod_dbrw.c: Include pthread.h Change path to cache.h (vmod_config,vmod_rewrite): Change declaration. (expand_backref,findmatch): Change declaration. Use WS_Copy instead of WS_Dup. * src/vmod_dbrw.vcc: Rewrite. --- NEWS | 4 +++- configure.ac | 4 ++-- src/Makefile.am | 9 +++++--- src/vmod_dbrw.c | 41 +++++++++++++++++----------------- src/vmod_dbrw.vcc | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 94 insertions(+), 30 deletions(-) diff --git a/NEWS b/NEWS index 933c3de..d36806a 100644 --- a/NEWS +++ b/NEWS @@ -5,7 +5,9 @@ See the end of file for copying conditions. Please send vmod-dbrw bug reports to -Version 1.0.91, (git) +Version 1.0.91-vcl4, (git) + +Rewritten for Varnish v4.x Version 1.0, 2013-07-20 diff --git a/configure.ac b/configure.ac index 618f304..e1c55eb 100644 --- a/configure.ac +++ b/configure.ac @@ -14,7 +14,7 @@ # You should have received a copy of the GNU General Public License # along with vmod-dbrw. If not, see . AC_PREREQ(2.69) -AC_INIT([vmod-dbrw], 1.0.91, [gray@gnu.org]) +AC_INIT([vmod-dbrw], 1.0.91-vcl4, [gray@gnu.org]) AC_CONFIG_AUX_DIR([build-aux]) AC_CONFIG_MACRO_DIR([m4]) AC_CONFIG_SRCDIR(src/vmod_dbrw.vcc) @@ -64,7 +64,7 @@ case "$VARNISHSRC" in esac VARNISHSRC=`cd $VARNISHSRC && pwd` -AC_CHECK_FILE([$VARNISHSRC/include/varnishapi.h], +AC_CHECK_FILE([$VARNISHSRC/include/vapi/vsm.h], [], [AC_MSG_FAILURE(["$VARNISHSRC" is not a Varnish source directory])]) diff --git a/src/Makefile.am b/src/Makefile.am index ed7d376..dde9116 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,7 +14,7 @@ # You should have received a copy of the GNU General Public License # along with vmod-dbrw. If not, see . -AM_CPPFLAGS = -I$(VARNISHSRC)/include -I$(VARNISHSRC) +AM_CPPFLAGS = -I$(VARNISHSRC)/include -I$(VARNISHSRC)/bin/varnishd -I$(VARNISHSRC) vmoddir = $(VMODDIR) vmod_LTLIBRARIES = libvmod_dbrw.la @@ -40,8 +40,11 @@ endif BUILT_SOURCES = vcc_if.c vcc_if.h -vcc_if.c vcc_if.h: $(VARNISHSRC)/lib/libvmod_std/vmod.py $(top_srcdir)/src/vmod_dbrw.vcc - @PYTHON@ $(VARNISHSRC)/lib/libvmod_std/vmod.py $(top_srcdir)/src/vmod_dbrw.vcc +vmodtool = $(VARNISHSRC)/lib/libvcc/vmodtool.py +vmodtoolargs = --strict + +vcc_if.c vcc_if.h: $(vmodtool) $(top_srcdir)/src/vmod_dbrw.vcc + @PYTHON@ $(vmodtool) $(vmodtoolargs) $(top_srcdir)/src/vmod_dbrw.vcc EXTRA_DIST = \ vmod_dbrw.vcc diff --git a/src/vmod_dbrw.c b/src/vmod_dbrw.c index f7723a2..284376f 100644 --- a/src/vmod_dbrw.c +++ b/src/vmod_dbrw.c @@ -19,7 +19,8 @@ #include "wordsplit.h" #include "vrt.h" #include "vcc_if.h" -#include "bin/varnishd/cache.h" +#include "pthread.h" +#include "bin/varnishd/cache/cache.h" static pthread_once_t thread_once = PTHREAD_ONCE_INIT; static pthread_key_t thread_key; @@ -202,9 +203,9 @@ parse_flags(const char *arg, int *qdisp, int *flags, char status[]) debug=N QUERY - Query to obtain the redirection target */ -void -vmod_config(struct sess *sp, struct vmod_priv *priv, const char *bkname, - const char *param, const char *query) +VCL_VOID +vmod_config(const struct vrt_ctx *ctx, struct vmod_priv *priv, + VCL_STRING bkname, VCL_STRING param, VCL_STRING query) { char *s; struct wordsplit ws; @@ -260,18 +261,18 @@ vmod_config(struct sess *sp, struct vmod_priv *priv, const char *bkname, } static char * -expand_backref(struct sess *sp, const char *str, const char *val, +expand_backref(const struct vrt_ctx *ctx, const char *str, const char *val, size_t matchcount, regmatch_t *matches, char *qry) { unsigned u; char *b, *p; - u = WS_Reserve(sp->wrk->ws, 0); - p = b = sp->wrk->ws->f; + u = WS_Reserve(ctx->ws, 0); + p = b = ctx->ws->f; while (*val) { if (u == 0) { - WS_Release(sp->wrk->ws, 0); + WS_Release(ctx->ws, 0); return NULL; } if (*val == '\\' || *val == '$') { @@ -284,7 +285,7 @@ expand_backref(struct sess *sp, const char *str, const char *val, matches[n].rm_so; if (len > u) { - WS_Release(sp->wrk->ws, 0); + WS_Release(ctx->ws, 0); return NULL; } @@ -310,7 +311,7 @@ expand_backref(struct sess *sp, const char *str, const char *val, if (qry) { size_t len = strlen(qry); if (len + 1 >= u) { - WS_Release(sp->wrk->ws, 0); + WS_Release(ctx->ws, 0); return NULL; } if (strchr(b, '?')) @@ -323,12 +324,12 @@ expand_backref(struct sess *sp, const char *str, const char *val, } if (u == 0) { - WS_Release(sp->wrk->ws, 0); + WS_Release(ctx->ws, 0); return NULL; } *p = 0; - WS_ReleaseP(sp->wrk->ws, p); + WS_ReleaseP(ctx->ws, p); return b; } @@ -336,7 +337,7 @@ expand_backref(struct sess *sp, const char *str, const char *val, #define ISEMPTY(s) ((s) == NULL || (s)[0] == 0) static char * -findmatch(struct sess *sp, struct dbrw_connection *conn, char **param) +findmatch(const struct vrt_ctx *ctx, struct dbrw_connection *conn, char **param) { unsigned i; unsigned nt, nf; @@ -353,7 +354,7 @@ findmatch(struct sess *sp, struct dbrw_connection *conn, char **param) debug(conn->conf, 2, ("query returned %u tuples, %u columns", nt, nf)); if (nf < 3) - return WS_Dup(sp->ws, sql_get_column(conn, 0, 0)); + return WS_Copy(ctx->ws, sql_get_column(conn, 0, 0), -1); /* Three or four fields: result @@ -375,7 +376,7 @@ findmatch(struct sess *sp, struct dbrw_connection *conn, char **param) char *qry = NULL; if (ISEMPTY(pat)) { - res = WS_Dup(sp->ws, sql_get_column(conn, i, 0)); + res = WS_Copy(ctx->ws, sql_get_column(conn, i, 0), -1); break; } @@ -442,7 +443,7 @@ findmatch(struct sess *sp, struct dbrw_connection *conn, char **param) rc = regexec(&re, val, matchcount, conn->matches, 0); if (rc == 0) - res = expand_backref(sp, val, + res = expand_backref(ctx, val, sql_get_column(conn, i, 0), matchcount, conn->matches, qry); @@ -452,7 +453,7 @@ findmatch(struct sess *sp, struct dbrw_connection *conn, char **param) if (status[0]) { debug(conn->conf, 1, ("setting status %s", status)); - VRT_SetHdr(sp, HDR_REQ, + VRT_SetHdr(ctx, HDR_REQ, "\023X-VMOD-DBRW-Status:", status, vrt_magic_string_end); @@ -496,8 +497,8 @@ get_connection(struct dbrw_config *conf) return cp; } -const char * -vmod_rewrite(struct sess *sp, struct vmod_priv *priv, const char *arg) +VCL_STRING +vmod_rewrite(const struct vrt_ctx *ctx, struct vmod_priv *priv, VCL_STRING arg) { struct dbrw_config *conf = priv->priv; struct dbrw_connection *cp; @@ -564,7 +565,7 @@ vmod_rewrite(struct sess *sp, struct vmod_priv *priv, const char *arg) return NULL; } - res = findmatch(sp, cp, wsenv.ws_wordv); + res = findmatch(ctx, cp, wsenv.ws_wordv); wordsplit_free(&wsenv); sql_free_result(cp); diff --git a/src/vmod_dbrw.vcc b/src/vmod_dbrw.vcc index 515883f..a2ec9d2 100644 --- a/src/vmod_dbrw.vcc +++ b/src/vmod_dbrw.vcc @@ -1,5 +1,63 @@ -Module dbrw -Init dbrw_init -Function STRING rewrite(PRIV_VCL, STRING) -Function VOID config(PRIV_VCL, STRING, STRING, STRING) +# This file is part of vmod-dbrw +# Copyright (C) 2013-2014 Sergey Poznyakoff +# +# Vmod-dbrw is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# Vmod-dbrw is distributed in the hope that it will be useful, +# 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. +# +# You should have received a copy of the GNU General Public License +# along with vmod-dbrw. If not, see . +$Module dbrw 3 Database-driven rewrite rules for Varnish Cache + +DESCRIPTION +=========== + +This module for Varnish Cache implements database-driven rewrite rules. +These rules may be similar to *RewriteRule* directives implemented by +**mod_rewrite** module in Apache or to *Redirect* directives of its +**mod_alias** module. What distinguishes the **vmod-dbrw** rules from +these, is that they are handled by Varnish, before the request reaches the +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. + +Example + 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. + +Example + set req.http.X-Redirect-To = + dbrw.rewrite("host=" + req.http.Host + ";" + + "url=" + req.url); + +SEE ALSO +======== + +* vcl(7) +* varnishd(1) + -- cgit v1.2.1