Ratpack as HTTP Proxy?

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Ratpack as HTTP Proxy?

Christian Baranowski
Is there a simple way to use Ratpack as (reverse) http proxy?

Sometimes I want to delegate a HTTP request to another host, is there any support in Ratpack for this, something like:

get("info") {
   response.send(target.get("info"))
}
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack as HTTP Proxy?

Luke Daley
Administrator
There's nothing like this right now.

At some point we will provide a HTTP client API which would make this possible.

Please raise an issue if this is important to you.
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack as HTTP Proxy?

Sameer Vohra
Hi Luke,
Any more interest on this?
Also, I was wondering if it would be easy to do this now using RxJava and Reactor so one can roll their on simple high performance proxy?

Cheers,
Sameer
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack as HTTP Proxy?

Luke Daley
Administrator
Hi Sameer,


RxJava and Reactor don’t really come into the picture here.

This was released in .12 but the API may change in the next release or two.
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack as HTTP Proxy?

Massimo Lusetti
Hi all,
  with some work on your side is possible to leverage ratpack as a proxy.

I've done some work on a custom app based on ratpack which, on a particular event it forwards the request to a node instance to let it run a reactjs server side app and it's working fine.

The situation is not ideal and could be improved but ratpack is doing an amazing job on its side.

Cheers
rus
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack as HTTP Proxy?

rus
@Massimo Lusetti - is what you've done shareable?  I'd be interested to see whether we've covered your use case in the new proxy functionality.  Proxying should now require very little work.
rus
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack as HTTP Proxy?

rus
In reply to this post by Sameer Vohra
@Sameer Vohra - here is an example that is actually acting as a proxy.  It copies the received request headers and adds the "via" header on the way back out.  I don't think it conforms exactly as a proxy should but might be a better example than the tests.

https://gist.github.com/rhart/eb1b701f348a155f2dad
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack as HTTP Proxy?

Luke Daley
Administrator
I want to point out that this is actually efficient proxying. We don’t read the upstream body into memory before sending it out.
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack as HTTP Proxy?

Sameer Vohra
In reply to this post by rus
Thanks for the prompt replies @Luke & @rus.
This is a great starting point :)
Specially liked the ability to be able to modify headers.

Regards,
Sameer
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack as HTTP Proxy?

Massimo Lusetti
In reply to this post by rus
I'm sorry rus but at the moment is not shareable.

Anyway there's a node instance which answer to requests from a reactjs/webpack app.

The requests are all prefixed by a value so we catch then inside our ratpack pipeline chain and forward the request "as is" to our node instance.

The great thing is that is all stateless so we can scale I there will be the need, but now we have plenty of room to increase the traffic :-)

I plan to upgrade the current setup to latest and greatest in the next month.
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack as HTTP Proxy?

Phil
In reply to this post by rus
Hi Rus,

I know this is pretty old but I was looking at using a ratpack groovy script as a development proxy/router (basically to mock out some stuff provided by aws in other environments).

I based it on your above examples but am having trouble supporting POST (and presumably PUT etc)..

I added support to proxy the http method but it hangs if a request body is provided (see code in GIST and behaviour in comment):

https://gist.github.com/parker0phil/beba337e684cd1435d3cacb5ed13bdc1

Any ideas?

rus
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack as HTTP Proxy?

rus
Commented on gist
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack as HTTP Proxy?

Phil
Thanks Rus. I figured I needed to send the body but the requestspec buffer (and the body flatmap) wasn't obvious to me. Works like a charm and I've commented the full solution/output on the Gist for future reference.

Thanks again.
rus
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack as HTTP Proxy?

rus
Awesome.  Yeah, the request body being a promise kind of makes the flow a little unintuitive.  I wonder if RequestSpec could take a promise for the body.  Will take a look.  Happy Ratpacking :)