]> err.no Git - varnish/commitdiff
Add a -vcl+backend argument to the varnish command which autogenerates
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Tue, 17 Jun 2008 07:53:21 +0000 (07:53 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Tue, 17 Jun 2008 07:53:21 +0000 (07:53 +0000)
backend stanzas for the servers we know about.

git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2717 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/bin/varnishtest/vtc.h
varnish-cache/bin/varnishtest/vtc_server.c
varnish-cache/bin/varnishtest/vtc_varnish.c

index b227b7df1d9d5ae0aee8c3e61b0408807a6ca402..cc1e8ff4d984219bc688922f0af12937ba492852 100644 (file)
@@ -28,6 +28,8 @@
 
 typedef void cmd_f(char **av, void *priv);
 
+struct vsb;
+
 struct cmds {
        const char      *name;
        cmd_f           *cmd;
@@ -47,3 +49,5 @@ void cmd_varnish(char **av, void *priv);
 void http_process(const char *ident, const char *spec, int sock, int client);
 
 void vct_dump(const char *ident, const char *pfx, const char *str);
+
+void cmd_server_genvcl(struct vsb *vsb);
index 5b168a2524f927b00a1622b5bc49acaba47ac3ca..460538f670d0896c68444d193c2b2a92fa1ffadb 100644 (file)
@@ -40,6 +40,7 @@
 
 #include "vqueue.h"
 #include "miniobj.h"
+#include "vsb.h"
 #include "vss.h"
 #include "libvarnish.h"
 
@@ -116,6 +117,7 @@ server_new(char *name)
        AN(s);
        s->name = name;
        s->listen = ":9080";
+       AZ(VSS_parse(s->listen, &s->addr, &s->port));
        s->repeat = 1;
        s->depth = 1;
        s->sock = -1;
@@ -135,7 +137,6 @@ server_start(struct server *s)
        CHECK_OBJ_NOTNULL(s, SERVER_MAGIC);
        printf("##   %-4s Starting server\n", s->name);
        if (s->sock < 0) {
-               AZ(VSS_parse(s->listen, &s->addr, &s->port));
                naddr = VSS_resolve(s->addr, s->port, &s->vss_addr);
                if (naddr != 1) {
                        fprintf(stderr,
@@ -174,6 +175,25 @@ server_wait(struct server *s)
        s->sock = -1;
 }
 
+/**********************************************************************
+ * Generate VCL backend decls for our servers
+ */
+
+void
+cmd_server_genvcl(struct vsb *vsb)
+{
+       struct server *s;
+
+       VTAILQ_FOREACH(s, &servers, list) {
+               vsb_printf(vsb,
+                   "backend %s { .host = \"%s\"; .port = \"%s\"; }\n",
+                   s->name,
+                   s->addr == NULL ? "localhost" : s->addr,
+                   s->port);
+       }
+}
+
+
 /**********************************************************************
  * Server command dispatch
  */
@@ -213,6 +233,7 @@ cmd_server(char **av, void *priv)
                }
                if (!strcmp(*av, "-listen")) {
                        s->listen = av[1];
+                       AZ(VSS_parse(s->listen, &s->addr, &s->port));
                        av++;
                        continue;
                }
index f928c5c4d7d32b8011ff7a40a2df5e5e250f6878..571236eee3c2daee6572c4f01af32a94dd3193d8 100644 (file)
@@ -104,6 +104,28 @@ varnish_ask_cli(struct varnish *v, const char *cmd, char **repl)
        return (retval);
 }
 
+static void
+varnish_cli_encode(struct vsb *vsb, const char *str)
+{
+
+       for (; *str != '\0'; str++) {
+               switch (*str) {
+               case '\\':
+               case '"':
+                       vsb_printf(vsb, "\\%c", *str); break;
+               case '\n':
+                       vsb_printf(vsb, "\\n"); break;
+               case '\t':
+                       vsb_printf(vsb, "\\t"); break;
+               default:
+                       if (isgraph(*str) || *str == ' ')
+                               vsb_putc(vsb, *str);
+                       else
+                               vsb_printf(vsb, "\\x%02x", *str);
+               }
+       }
+}
+
 /**********************************************************************
  * Allocate and initialize a varnish
  */
@@ -281,6 +303,55 @@ varnish_vcl(struct varnish *v, char *vcl)
        vsb_delete(vsb);
 }
 
+/**********************************************************************
+ * Load a VCL program prefixed by backend decls for our servers
+ */
+
+static void
+varnish_vclbackend(struct varnish *v, char *vcl)
+{
+       struct vsb *vsb, *vsb2;
+       char *p;
+       unsigned u;
+
+       vsb = vsb_newauto();
+       AN(vsb);
+
+       vsb2 = vsb_newauto();
+       AN(vsb2);
+
+       cmd_server_genvcl(vsb2);
+       vsb_finish(vsb2);
+       AZ(vsb_overflowed(vsb2));
+
+       v->vcl_nbr++;
+       vsb_printf(vsb, "vcl.inline vcl%d \"", v->vcl_nbr);
+
+       varnish_cli_encode(vsb, vsb_data(vsb2));
+
+       if (*vcl == '{') {
+               p = strchr(++vcl, '\0');
+               if (p > vcl && p[-1] == '}')
+                       p[-1] = '\0';
+       }
+       varnish_cli_encode(vsb, vcl);
+
+       vsb_printf(vsb, "\"", *vcl);
+       vsb_finish(vsb);
+       AZ(vsb_overflowed(vsb));
+
+       u = varnish_ask_cli(v, vsb_data(vsb), NULL);
+       assert(u == CLIS_OK);
+       vsb_clear(vsb);
+       vsb_printf(vsb, "vcl.use vcl%d", v->vcl_nbr);
+       vsb_finish(vsb);
+       AZ(vsb_overflowed(vsb));
+       u = varnish_ask_cli(v, vsb_data(vsb), NULL);
+       assert(u == CLIS_OK);
+       vsb_delete(vsb);
+       vsb_delete(vsb2);
+}
+
 /**********************************************************************
  * Varnish server cmd dispatch
  */
@@ -336,6 +407,11 @@ cmd_varnish(char **av, void *priv)
                        varnish_start(v);
                        continue;
                }
+               if (!strcmp(*av, "-vcl+backend")) {
+                       varnish_vclbackend(v, av[1]);
+                       av++;
+                       continue;
+               }
                if (!strcmp(*av, "-vcl")) {
                        varnish_vcl(v, av[1]);
                        av++;