Class: RackPhpProxy

Inherits:
Rack::Proxy show all
Defined in:
lib/rack_proxy_examples/rack_php_proxy.rb

Overview

Open localhost:3000/test.php to trigger proxy

Constant Summary

Constants inherited from Rack::Proxy

Rack::Proxy::VERSION

Instance Method Summary collapse

Methods inherited from Rack::Proxy

#call, extract_http_request_headers, #initialize, normalize_headers, #rewrite_env

Constructor Details

This class inherits a constructor from Rack::Proxy

Instance Method Details

#perform_request(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rack_proxy_examples/rack_php_proxy.rb', line 6

def perform_request(env)
  request = Rack::Request.new(env)
  if request.path =~ %r{\.php}
    env["HTTP_HOST"] = ENV["HTTP_HOST"] ? URI(ENV["HTTP_HOST"]).host : "localhost"
    ENV["PHP_PATH"] ||= '/manual/en/tutorial.firstpage.php'
     
    # Rails 3 & 4
    env["REQUEST_PATH"] = ENV["PHP_PATH"] || "/php/#{request.fullpath}"
    # Rails 5 and above
    env['PATH_INFO'] = ENV["PHP_PATH"] || "/php/#{request.fullpath}"

    env['content-length'] = nil
    
    super(env)
  else
    @app.call(env)
  end
end

#rewrite_response(triplet) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/rack_proxy_examples/rack_php_proxy.rb', line 25

def rewrite_response(triplet)
  status, headers, body = triplet
  
  # if you proxy depending on the backend, it appears that content-length isn't calculated correctly
  # resulting in only partial responses being sent to users
  # you can remove it or recalculate it here
  headers["content-length"] = nil

  triplet
end