Sinatra Proxy
In general since it is JSON/XML data I am interested in then an iframe will not work so I have to create a proxy. Below is what I do using Sinatra.
Required Gems
- Sinatra
- Net::HTTP
- CGI
- URI
- index.html - static file that includes all other files
- public - place where other static files will go
- javascript
- stylesheets
- images
/remote/*
The * tells sinatra to blindly take everything up to the "?" and put it in params[:splat]. I then split out the first item for the server name and port. You could also split out username/password if you want, but I rarely use HTTP Basic Auth so I never do.
I delete the key 'splat' and anything I do not want to forward so that they are not URL encoded to the remote.
urlencode
A simple function that re-encodes params that were placed in the params hash. Sinatra does a good job of taking params that look like color[]=red&color[]=blue and turning them into {"color" => ["red","blue"]}. Therefore I check the value's class for Array and if so I add the "[]" to the param name.
Proxy
The proxy code is a call to Net::HTTP returning the response body. I could also have returned the headers, but in this case I do not care.
<script src="https://gist.github.com/1151485.js?file=proxy.rb"></script>
In Action
| /remote/medic/this | -> | /medic:80/this |
| /remote/medic:4567/this | -> | /medic:4567/this |
| /remote/medic:4567/this?_dc=1 | -> | /medic:4567/this |
| /remote/medic/this?foo=bar | -> | /medic:80/this?foo=bar |
| /remote/medic/this?foo[]=bar&foo[]=baz | -> | /medic:80/this?foo[]=bar&foo[]=baz |