Commit 8700e538 authored by Dridi Boukelmoune's avatar Dridi Boukelmoune

varnishtest: Replace macro_get() with macro_cat()

The latter operates on a VSB, which is always what call sites are doing
anyway. It also takes the responsibility of ignoring unknown macros, in
preparation for more responsibilities that will also require the ability
to fail a test case.

Conflicts:
	bin/varnishtest/vtc_http.c
parent c89cc48a
...@@ -187,8 +187,8 @@ macro_undef(struct vtclog *vl, const char *instance, const char *name) ...@@ -187,8 +187,8 @@ macro_undef(struct vtclog *vl, const char *instance, const char *name)
AZ(pthread_mutex_unlock(&macro_mtx)); AZ(pthread_mutex_unlock(&macro_mtx));
} }
char * void
macro_get(const char *b, const char *e) macro_cat(struct vtclog *vl, struct vsb *vsb, const char *b, const char *e)
{ {
struct macro *m; struct macro *m;
int l; int l;
...@@ -205,7 +205,9 @@ macro_get(const char *b, const char *e) ...@@ -205,7 +205,9 @@ macro_get(const char *b, const char *e)
retval = malloc(64); retval = malloc(64);
AN(retval); AN(retval);
VTIM_format(t, retval); VTIM_format(t, retval);
return (retval); VSB_cat(vsb, retval);
free(retval);
return;
} }
AZ(pthread_mutex_lock(&macro_mtx)); AZ(pthread_mutex_lock(&macro_mtx));
...@@ -215,9 +217,19 @@ macro_get(const char *b, const char *e) ...@@ -215,9 +217,19 @@ macro_get(const char *b, const char *e)
break; break;
} }
if (m != NULL) if (m != NULL)
retval = strdup(m->val); REPLACE(retval, m->val);
AZ(pthread_mutex_unlock(&macro_mtx)); AZ(pthread_mutex_unlock(&macro_mtx));
return (retval);
if (retval == NULL) {
if (!ign_unknown_macro)
vtc_fatal(vl, "Macro ${%.*s} not found",
(int)(e - b), b);
VSB_printf(vsb, "${%.*s}", (int)(e - b), b);
return;
}
VSB_cat(vsb, retval);
free(retval);
} }
struct vsb * struct vsb *
...@@ -242,7 +254,6 @@ macro_expand(struct vtclog *vl, const char *text) ...@@ -242,7 +254,6 @@ macro_expand(struct vtclog *vl, const char *text)
{ {
struct vsb *vsb; struct vsb *vsb;
const char *p, *q; const char *p, *q;
char *m;
vsb = VSB_new_auto(); vsb = VSB_new_auto();
AN(vsb); AN(vsb);
...@@ -262,19 +273,7 @@ macro_expand(struct vtclog *vl, const char *text) ...@@ -262,19 +273,7 @@ macro_expand(struct vtclog *vl, const char *text)
assert(p[1] == '{'); assert(p[1] == '{');
assert(q[0] == '}'); assert(q[0] == '}');
p += 2; p += 2;
m = macro_get(p, q); macro_cat(vl, vsb, p, q);
if (m == NULL) {
if (!ign_unknown_macro) {
VSB_destroy(&vsb);
vtc_fatal(vl, "Macro ${%.*s} not found",
(int)(q - p), p);
NEEDLESS(return (NULL));
}
VSB_printf(vsb, "${%.*s}", (int)(q - p), p);
} else {
VSB_printf(vsb, "%s", m);
free(m);
}
text = q + 1; text = q + 1;
} }
AZ(VSB_finish(vsb)); AZ(VSB_finish(vsb));
...@@ -509,7 +508,6 @@ exec_file(const char *fn, const char *script, const char *tmpdir, ...@@ -509,7 +508,6 @@ exec_file(const char *fn, const char *script, const char *tmpdir,
FILE *f; FILE *f;
struct vsb *vsb; struct vsb *vsb;
const char *p; const char *p;
char *q;
(void)signal(SIGPIPE, SIG_IGN); (void)signal(SIGPIPE, SIG_IGN);
...@@ -529,12 +527,8 @@ exec_file(const char *fn, const char *script, const char *tmpdir, ...@@ -529,12 +527,8 @@ exec_file(const char *fn, const char *script, const char *tmpdir,
vsb = VSB_new_auto(); vsb = VSB_new_auto();
AN(vsb); AN(vsb);
if (*fn != '/') { if (*fn != '/')
q = macro_get("pwd", NULL); macro_cat(vltop, vsb, "pwd", NULL);
AN(q);
VSB_cat(vsb, q);
free(q);
}
p = strrchr(fn, '/'); p = strrchr(fn, '/');
if (p != NULL) { if (p != NULL) {
VSB_putc(vsb, '/'); VSB_putc(vsb, '/');
......
...@@ -116,7 +116,7 @@ void macro_undef(struct vtclog *vl, const char *instance, const char *name); ...@@ -116,7 +116,7 @@ void macro_undef(struct vtclog *vl, const char *instance, const char *name);
void macro_def(struct vtclog *vl, const char *instance, const char *name, void macro_def(struct vtclog *vl, const char *instance, const char *name,
const char *fmt, ...) const char *fmt, ...)
v_printflike_(4, 5); v_printflike_(4, 5);
char *macro_get(const char *, const char *); void macro_cat(struct vtclog *, struct vsb *, const char *, const char *);
struct vsb *macro_expand(struct vtclog *vl, const char *text); struct vsb *macro_expand(struct vtclog *vl, const char *text);
struct vsb *macro_expandf(struct vtclog *vl, const char *, ...) struct vsb *macro_expandf(struct vtclog *vl, const char *, ...)
v_printflike_(2, 3); v_printflike_(2, 3);
......
...@@ -849,11 +849,9 @@ http_tx_parse_args(char * const *av, struct vtclog *vl, struct http *hp, ...@@ -849,11 +849,9 @@ http_tx_parse_args(char * const *av, struct vtclog *vl, struct http *hp,
int bodylen = 0; int bodylen = 0;
char *b, *c; char *b, *c;
char *nullbody; char *nullbody;
char *m;
int nolen = 0; int nolen = 0;
int l; int l;
(void)vl;
nullbody = body; nullbody = body;
for (; *av != NULL; av++) { for (; *av != NULL; av++) {
...@@ -926,10 +924,9 @@ http_tx_parse_args(char * const *av, struct vtclog *vl, struct http *hp, ...@@ -926,10 +924,9 @@ http_tx_parse_args(char * const *av, struct vtclog *vl, struct http *hp,
break; break;
} }
if (!nohost) { if (!nohost) {
m = macro_get("localhost", NULL); VSB_cat(hp->vsb, "Host: ");
AN(m); macro_cat(vl, hp->vsb, "localhost", NULL);
VSB_printf(hp->vsb, "Host: %s%s", m, nl); VSB_cat(hp->vsb, nl);
free(m);
} }
if (body != NULL && !nolen) if (body != NULL && !nolen)
VSB_printf(hp->vsb, "Content-Length: %d%s", bodylen, nl); VSB_printf(hp->vsb, "Content-Length: %d%s", bodylen, nl);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment