Escaping double quotes in probe request

Dridi Boukelmoune dridi at varni.sh
Sat Jul 28 22:03:09 UTC 2018


On Thu, Jul 19, 2018 at 10:32 AM, Igor Zivkovic <igzivkov at gmail.com> wrote:
> Hello,
>
> Is there a way to escape double quotes in probe requests? I need to send a
> JSON request and I've tried %22 but checking the packets with tcpdump it
> seems Varnish doesn't convert escapes back to double quotes. For example:
>
>   .probe = {
>     .request =
>       "POST /probe.php HTTP/1.1"
>       "Host: virtualhost.example.com"
>       "Content-Type: application/json"
>       "Connection: close"
>       ""
>       "{%22key%22:%22value%22}";
>   }

Hello Igor,

I wrote a test case showing how to do that:

    varnishtest "probe json body"

    barrier b1 cond 2

    server s1 {
        rxreq
        txresp
        expect req.method == POST
        expect req.url == "/probe.php"
        expect req.http.Host == virtualhost.example.com
        expect req.http.Content-Type == "application/json"
        expect req.http.Connection == close
        expect req.body ~ key
        expect req.body ~ value
        barrier b1 sync
    } -start

    varnish v1 -vcl+backend {
        probe default {
            .request =
                "POST /probe.php HTTP/1.1"
                "Host: virtualhost.example.com"
                "Content-Type: application/json"
                "Content-Length: 19"
                "Connection: close"
                ""
                {"{ "key": "value" }"};
        }
    } -start

    barrier b1 sync

There are a couple gotchas to be aware of, so I'll explain what I did here.

First, I'm not aware of escape sequences for double quotes in VCL but
we have "long" string (that may span multiple lines) that use
different delimiters {" and "} where a regular double quote " doesn't
have a special meaning.

But when it comes to JSON, you may easily conflict with the separators
if you want to keep your payload compact. That's because { opens an
object and is followed by a key between double quotes. So you need at
least one blank between any { followed by a " or " followed by a } in
your payload.

Finally, in order to have the test case pass, I had to put the content
length but I'm not sure whether this is needed or whether
varnishtest's mock server doesn't handle the case it should (or maybe
we have a command other than rxreq for the case where content length
is omitted). I'm really not motivated to investigate that last point
today.

Cheers


More information about the varnish-misc mailing list