Skip to content

HTTPServlet Handler Example

tka edited this page Feb 18, 2013 · 8 revisions

Fire.app 1.6 and Above

  1. Save this file to http_servlet_handler.rb, and put it in your project's root folder
  2. create something.html.php, write some php code
  3. open http://127.0.0.1:24680/something.php you will see the page
class PHPHandler 
  def initialize(app)  
    @app = app  
  end  

  def call(env) 
    if env["PATH_INFO"] =~ /\/$/
      env["PATH_INFO"] += "index.php"
    end

    if env["PATH_INFO"] =~ /\.php$/
      php_path =  env["PATH_INFO"][1..-1]
      body = %x{php #{php_path}}
      [200, {"Content-Type" => "text/html"}, [body]]
    else
      status, headers, body = @app.call(env)
      [status, headers, body]
    end 
  end  
end

use PHPHandler

Fire.app 1.2~1.5

  1. Save this file to http_servlet_handler.rb, and put it in your project's root folder
  2. create something.html.php, write some php code
  3. open http://127.0.0.1:24680/something.php you will see the page
class PHPHandler 
  def initialize(app)  
    @app = app  
  end  

  def call(env) 
    if env["PATH_INFO"] =~ /\.php$/
      php_path =  env["PATH_INFO"][1..-1]
      body = %x{php #{php_path}}
      [200, {"Content-Type" => "text/html"}, body]
    else
      status, headers, body = @app.call(env)
      [status, headers, body]
    end 
  end  
end

use PHPHandler