Forward based on request body pattern match

Hello,

Imagine that I would like to use caddy go mod, programmatically, to do the following:
Every time a request comes, the request body should be evaluated (by a custom function or a pattern), and if matched, then forward/proxy the request to host X or host Y (with a given policy, which caddy already supports).

So, I imagine:

  • load a configuration specifying patterns in the request body and to where it should be proxied through (proto://host:port)
  • start caddy on port X
  • every time a request arrives, evaluate the request body based on such configuration (e.g., “if the body contains X then forward the request to host X”)

I believe that caddy would allow me to do something like this:

  • Specify a “middleware function” (out of caddy, but following some kind of interface) that gets in between the moment the request arrives and is forwarded to other steps in the pipeline (load balancer, etc).
  • The only problem is that the outcome of such “middleware function” can also impact to where the request should be proxied to (I mean, this function could provide an array of hosts, for example).

I don’t know if I was pretty clear on the description because I could not look into v2 code yet (I just have v1 experience)… but, could you shed some light on where I could start looking into?

Cheers,
Guil.

Hi @gsmachado,

Just thinking on this, I’m wondering if it wouldn’t be simpler to write a new matcher module, rather than writing a handler.

Currently there is no matcher module for request body content, but it should be fairly easy to plug one in.

All the current standard matcher modules are here: https://github.com/caddyserver/caddy/blob/master/modules/caddyhttp/matchers.go

They generally compare against a given *http.Request, which does give you access to the request body: https://golang.org/pkg/net/http/#Request

Implementing a matcher module for this purpose would allow you to simply use the existing core terminating handlers, just specifying based on whatever you match in the request body itself.

For more general info on extending Caddy v2, refer to https://caddyserver.com/docs/extending-caddy.

1 Like

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