How to get the value of a key that may appear in header or url

1. The problem I’m having:

How to get the value of a key that may appear in header or url

I have an uuid parameter in the url or in the Referer part of the header, which may only appear in one of them. And I want to send it to another server to authorize using reverse_proxy

The uuid parameter may appear in the url:

https://domain/?uuid=val_a

or in header:

Referer: https://domain/?uuid=val_a

3. Caddy version:

v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=

4. How I installed and ran Caddy:

d. My complete Caddy config:

{
        debug
        order respond after reverse_proxy
}
domain.com {
        root /path/path
                encode zstd gzip
                @uuid  expression header_regexp('uuid', 'Referer', 'uuid=([0-9a-zA-Z_]+)') || {query}.matches(r'([0-9a-zA-Z_]+)')

        reverse_proxy @uuid 127.0.0.1:6111{
                method GET
                rewrite /verify?uuid={re.1}  ### How to get the correct value.
                ### {re.1} not woring when uuid is only in url

                header_up X-Forwarded-Method {method}
                header_up X-Forwarded-Uri {uri}

                @good status 2xx
                handle_response @good {
                        file_server
                }
        }
        respond 404

}
         

5. Links to relevant resources:

You could do something like this, I think:

@uuid-header header_regexp Referer uuid=([0-9a-zA-Z_]+)
vars @uuid-header uuid {re.1}

@uuid-query query uuid=*
vars @uuid-query uuid {query.uuid}

Then later:

rewrite /verify?uuid={vars.uuid}

If the UUID is defined in both, then the query one will win. You can flip the order if necessary. But they’ll only set the var if the matcher matches.

1 Like

Thanks for your solution. It helped me solve the problem
And I added a named matcher used in reverse_proxy

@uuidboth `{vars.uuid} != null`
reverse_proxy @uuidboth  ... 

You don’t need an expression matcher for that, there’s a vars matcher

But to be clear, that won’t be true only if both are set, it’ll also be true when either-or is set, as well as both.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.