#include "vqueue.h"
#include "miniobj.h"
+#include "vsb.h"
#include "vss.h"
#include "libvarnish.h"
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;
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,
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
*/
}
if (!strcmp(*av, "-listen")) {
s->listen = av[1];
+ AZ(VSS_parse(s->listen, &s->addr, &s->port));
av++;
continue;
}
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
*/
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
*/
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++;