Sort args in request

1. The problem I’m having:

Hello, I am trying to optimize a public, read-only api by heavy use of caching. All data is public so I don’t have to worry about Authorization header or its cousins.

I am using caddy as the frontend for some time for SSL termination, certificate generation and dynamic reverse proxy (using the caddy docker plugin), but I still rely on nginx in the backend to do most of the work related to redirects and caching.

For this particular case, I needed to sort the GET query arguments so that I ensure a single cache entry for a particular request, no matter the arguments order; I did that with nginx lua. I was then able to remove an argument from the args list which might identify the client and use the curated argument list as the cache key query.

It works, but it took a while to figure out and the implementation is a bit ugly (lua to sort args, nginx map and regexes to pull out the identifier argument), which led me to wonder if there is a way in caddy to sort the arguments of a query - like caddy does, by magic :smiley:, or perhaps a plugin.

I remember clearly the shock when I discovered the local_certs option :smiley:

This should be a first step in the right direction, as then I might be able to also use the caddy cache plugin for that.

Thank you so much for ideas!

2. Error messages and/or full log output:

Instead the error, I would like to change this request from the client:

https://a.host.example.com/some/route?z=1&x=2&b=bar&a=foo

into this request to the upstream server:

https://a.host.example.com/some/route?a=foo&b=bar&x=2&z=1

3. Caddy version:

2.7.5

4. How I installed and ran Caddy:

a. System environment:

docker, docker-compose

b. Command:

caddy docker-proxy --caddyfile-path /etc/caddy/Caddyfile

c. Service/unit/compose file:

N/A

d. My complete Caddy config:

N/A

5. Links to relevant resources:

Yeah you can certainly do that with a plugin.

Should be quite easy.

Since you’re using Caddy Docker Proxy (CDP), you’ll need a custom Dockerfile to build with your plugin, see the CDP README which has an example.

1 Like

Erm, I was asking if you have an idea if currently there is a plugin or a way to do that. :slight_smile:

Not that I know of. But it’s a trivial thing to implement with a plugin, so that’s what I recommend you do.

Roger, thank you!

1 Like

Ok, there it is GitHub - teodorescuserban/caddy-argsort: Caddy module that will alphabetically sort the request query arguments.
It was really trivial, since url.Values.Encode() does sort the arguments by default.

I fiddled with it and it looks to perform correctly. I will have to write some tests though.

I would really appreciate your input, @francislavoie

1 Like

@francislavoie what I wanted to say is if you can have a look on that minimal plugin if / when you can.

Do you think it is possible to include this functionality in the core at some point? While I am fine using the plugin, it does look like a bit of overkill :slight_smile:

Thank you!

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