Created
May 26, 2010 00:17
-
-
Save lightcap/413864 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #\ -w -p 8765 | |
| use Rack::ContentLength | |
| use Rack::Reloader, 0 | |
| require "net/http" | |
| 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 | |
| use Rack::Proxy do |req| | |
| if req.path =~ %r{^/gateway$} | |
| URI.parse("http://ey.stg.dcgateway.pvpowered.com/gateway/") | |
| end | |
| end | |
| app = proc do |env| | |
| [200, {"Content-Type" => "text/plain"}, ["Ha, Ha, Ha"]] | |
| end | |
| # app = proc do |env| | |
| # req = Rack::Request.new(env) | |
| # if req.post? | |
| # body = req["data"] | |
| # else | |
| # body = "not a post" | |
| # end | |
| # [ 302, {'Location' => 'https://ey.stg.dcgateway.pvpowered.com:443/gateway', 'Content-Type' => 'text/plain'}, ['Redirecting...']] | |
| # end | |
| run app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment