问题
I am using Varnish 4.0.
My backend is adding to some responses an http header "x-count"
I would like to log the value of "x-count" into a file with a line break.
I assumed i should do it in VCL deliver.
Here is what i have so far :
sub vcl_deliver {
if (resp.http.x-count-this:) {
set resp.http.X-infodbg = "xx";
C{
FILE *fp;
fp = fopen("/tmp/test.txt", "w+");
fputs(VRT_GetHdr(sp, HDR_OBJ, "\013x-count-this:"), fp);
fputs("\n", fp);
fclose(fp);
}C
}
}
Of course it doesnt work and there is a couple of errors ..
./vcl.gK2lu7uM.c: In function ‘VGC_function_vcl_deliver’: ./vcl.gK2lu7uM.c:1049:22: error: ‘sp’ undeclared (first use in this function) ./vcl.gK2lu7uM.c:1049:22: note: each undeclared identifier is reported only once for each function it appears in ./vcl.gK2lu7uM.c:1049:5: error: passing argument 2 of ‘VRT_GetHdr’ makes pointer from integer without a cast [-Werror] ./vcl.gK2lu7uM.c:330:7: note: expected ‘const struct gethdr_s *’ but argument is of type ‘int’ ./vcl.gK2lu7uM.c:1049:5: error: too many arguments to function ‘VRT_GetHdr’ ./vcl.gK2lu7uM.c:330:7: note: declared here
I have to say that i simply copy/pasted "sp" from some examples, but i have no idea where it comes from (i suppose the inline C was in a different context and therefore it was declared there but not in vcl_deliver)
回答1:
So the probably undocumented differences between Varnish 4 and 3 in the above examples are :
- VRT_GetHdr is now VRT_GetHdr(context, struct gethdr_s)
- sp doesn't exist, but there is a "ctx" variable
Found this, there :
http://jan.bogutzki.de/Artikel/395/set-ttl-in-varnish-4.html
char *stuffid;
const struct gethdr_s hdr = { HDR_BERESP, "\015x-count-this:" };
stuffid = VRT_GetHdr(ctx, &hdr);
And now a different story: Varnish is crashing as soon as the backend sends back "count-this", but that is a different problem :p (my crappy C code probably)
回答2:
I don't have Varnish 4.0 handy to test this out, but I was able to get your example working with Varnish 3.0. When I tried the VCL as is, I wasn't getting the exact error you are though. The first change:
if (resp.http.x-count-this:) {
needs to be:
if (resp.http.x-count-this) {
The colon should be left off of the header name when referred to this way. Next:
fputs(VRT_GetHdr(sp, HDR_OBJ, "\013x-count-this:"), fp);
needs to be:
fputs(VRT_GetHdr(sp, HDR_OBJ, "\015x-count-this:"), fp);
The length value in that string needs to be in octal for some reason, and 13
in octal is 15
. Making those changes got this to work for me. That being said, you many want to look into using open
and fcntl
instead of fopen
since without file locking I'm not sure what the effect of multiple requests contending for that file would be.
来源:https://stackoverflow.com/questions/25688448/inline-c-varnish-vcl-deliver