Build a rewriting proxy on top of http://golang.org/src/pkg/net/http/httputil/reverseproxy.go

3,591 views
Skip to first unread message

a.ku...@tomorrow-focus.de

unread,
Aug 20, 2013, 2:54:16 PM8/20/13
to golan...@googlegroups.com
Hi all,

a small project has materialised which might finally give me chance to build something non-trivial in go. We need a small proxy server that is able to rewrite Http requests and responses according to arbitrary logic. After reviewing what is available, it seems like building it ourselves is actually the best solution. So my questions are:

1. I can see from the docs that I can implement Director func(*http.Request) to rewrite the Http request. But what about the response, would I have to implement the Transport interface for that? Would that be straightforward?
2. Are there any other obvious problems with my approach?

I think go would be a perfect fit for us for a multitude of reasons here (strong http standard lib, static binary, scalable, small memory footprint,..), so I am looking forward to your feedback!

Cheers,
Arian

omarsharif...@gmail.com

unread,
Aug 20, 2013, 4:30:20 PM8/20/13
to golan...@googlegroups.com, a.ku...@tomorrow-focus.de
oooo spooky! I have the same question: How can I rewrite the response URL so that it matches the originally requested URL, rather than the target URL?

Cheers!
Ben

Kyle Lemons

unread,
Aug 20, 2013, 5:27:22 PM8/20/13
to Ben Davies, golang-nuts, a.ku...@tomorrow-focus.de
For things like this I would recommend writing your own, and not trying to shoehorn it into ReverseProxy.  It's actually a pretty straightforward function that you can copy and adapt.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

omarsharif...@gmail.com

unread,
Aug 20, 2013, 5:41:29 PM8/20/13
to golan...@googlegroups.com, Ben Davies, a.ku...@tomorrow-focus.de
(Arian, apologies for hijacking your thread!)

I have an example simple reverse proxy here:


How would I rewrite the URL in the response to match the originally requested URL?

Cheers,
Ben

Kyle Lemons

unread,
Aug 20, 2013, 6:58:26 PM8/20/13
to Ben Davies, golang-nuts, a.ku...@tomorrow-focus.de
Write your own responsewriter.  YOu may have to buffer the entire response, which is why I suggested not using reverseproxy at all, just make an HTTP request, transform the response, and write it out.
Reply all
Reply to author
Forward
0 new messages