Created
October 11, 2009 21:11
-
-
Save tomlea/207884 to your computer and use it in GitHub Desktop.
Rack Middleware to proxy requests to a remote server. This is usefull for proxying AJAX request to remote services.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "net/http" | |
require "enumerator" | |
# Example Usage: | |
# | |
# use Rack::Proxy do |req| | |
# if req.path =~ %r{^/remote/service.php$} | |
# URI.parse("http://remote-service-provider.com/service-end-point.php?#{req.query}") | |
# end | |
# end | |
# | |
# run proc{|env| [200, {"Content-Type" => "text/plain"}, ["Ha ha ha"]] } | |
# | |
class Rack::Proxy | |
def initialize(app, &block) | |
self.class.send(:define_method, :uri_for, &block) | |
@app = app | |
end | |
def call(env) | |
req = Rack::Request.new(env) | |
method = req.request_method.downcase | |
method[0..0] = method[0..0].upcase | |
return @app.call(env) unless uri = uri_for(req) | |
sub_request = Net::HTTP.const_get(method).new("#{uri.path}#{"?" if uri.query}#{uri.query}") | |
if sub_request.request_body_permitted? and req.body | |
sub_request.body_stream = req.body | |
sub_request.content_length = req.content_length | |
sub_request.content_type = req.content_type | |
end | |
sub_request["X-Forwarded-For"] = (req.env["X-Forwarded-For"].to_s.split(/, +/) + [req.env['REMOTE_ADDR']]).join(", ") | |
sub_request["Accept-Encoding"] = req.accept_encoding | |
sub_request["Referer"] = req.referer | |
sub_response = Net::HTTP.start(uri.host, uri.port) do |http| | |
http.request(sub_request) | |
end | |
headers = {} | |
sub_response.each_header do |k,v| | |
headers[k] = v unless k.to_s =~ /cookie|content-length|transfer-encoding/i | |
end | |
[sub_response.code.to_i, headers, [sub_response.read_body]] | |
end | |
end |
Great piece of code. Thanks!
will this work for proxying websocket request?
In don’t think so, as it’s rebuilding the request.
… On 29 Apr 2020, at 19:40, Rahul Bennapalan ***@***.***> wrote:
***@***.*** commented on this gist.
will this work for proxying websocket request?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
is there any other module that you are aware, in RoR, to proxy WebSocket requests?
You’re probably going to want the we sockets to bypass rails all together, so I’d expect you can do this in your nginx config or similar. I’ve not worked on this kind of thing for a VERY long time, so pure speculation.
… On 30 Apr 2020, at 04:33, Rahul Bennapalan ***@***.***> wrote:
***@***.*** commented on this gist.
is there any other module that you are aware, in RoR, to proxy WebSocket requests?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
ok, got it. thanks, Tom Lea.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does Rack::Request validate the method? Probably not - so an attacker could execute some custom code here. Better to explicitely check the allowed method names.