synthetic ESI for JSONP

Eric Jensen ej at
Tue Jan 5 23:56:48 CET 2010

I am playing with a VCL for doing ESI on "synthetic" responses in order to serve JSONP callback requests.  Basically, to support JSONP a javascript client makes a request with a randomly generated "&callback=blah135297" parameter and we must return responses that look like "blah135297(CACHEABLE JSON RESPONSE HERE);"

I've come up with the following example solution to construct the ESI template for this within varnish itself:

sub vcl_recv {
  if (req.url ~ "callback") {
        set req.http.X-Callback = regsub(req.url, ".*[\?&]callback=([/@.:A-Za-z0-9_]+).*", "\1");
        set req.http.X-ESI-Url = req.url;
        set req.url = "/VARNISHDUMMY";

sub vcl_fetch {
 if (req.http.X-ESI-Url) {
      synthetic req.http.X-Callback "(<esi:include src=%22" req.http.X-ESI-Url "%22/>);";

This works, but it has two undesirable properties that I'm looking for suggestions on how to fix:

1.  It makes an unnecessary dummy request to the backend.  Is there some way I can create a noop backend, set some parameter to make the request itself a noop, or just avoid the actual fetch altogether but still get into vcl_fetch which is the only place you can call esi?

2.  The response code, headers, etc. from the dummy request are returned to the client instead of the meaningful ones from the ESI request.  Is there some way to copy the ESI response into the one to be returned?  Can I access it from C somehow?


More information about the varnish-dev mailing list