Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:tenderlove/phuby
* 'master' of github.com:tenderlove/phuby: switching to rack::utils cleaning up the php handler a rack adapter for phuby arrays move across
- Loading branch information
Showing
4 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'phuby' | ||
require 'rack' | ||
require 'rack/showexceptions' | ||
require 'logger' | ||
|
||
### | ||
# Rack::Phrack is a Rack handler that will evaulate and serve PHP files. | ||
# | ||
# The handler creates a new PHP runtime if it detects that a PHP file has | ||
# been requested. | ||
class Rack::Phrack < Rack::File | ||
class Events < Struct.new(:code, :headers, :body) | ||
def write string | ||
body << string | ||
end | ||
|
||
def header value, op | ||
k, v = value.split(': ', 2) | ||
case k | ||
when 'Location' | ||
self.code = 302 | ||
v = Rack::Utils.unescape v | ||
when 'Set-Cookie' | ||
v = [headers[k], v].compact.join "\n" | ||
end | ||
headers[k] = v | ||
end | ||
|
||
def send_headers response_code | ||
end | ||
end | ||
|
||
def call env | ||
events = Events.new 200, {}, '' | ||
file = File.join(@root, env['PATH_INFO']) | ||
file = File.join(file, "index.php") if File.directory?(file) | ||
|
||
return super unless file =~ /php$/ | ||
|
||
Dir.chdir(File.dirname(file)) do | ||
Phuby::Runtime.php do |rt| | ||
|
||
rt['_LOGGER'] = Logger.new($stderr) | ||
|
||
# Set the timezone. *shrug* | ||
rt.eval("date_default_timezone_set('America/Los_Angeles');") | ||
|
||
# Set the GET parameters in the PHP runtime | ||
params(env['QUERY_STRING']) { |k,v| rt["_GET"][k] = v } | ||
|
||
# Set the POST parameters in the PHP runtime | ||
params(env['rack.input'].read) { |k,v| rt["_POST"][k] = v } | ||
|
||
# Set the cookies in the PHP runtime | ||
env['HTTP_COOKIE'].split('; ').each do |pair| | ||
k, v = pair.split '=' | ||
rt["_COOKIE"][k] = Rack::Utils.unescape v | ||
end | ||
|
||
# Set other misc info. | ||
env.each { |k,v| | ||
next if k =~ /^rack/ | ||
rt['_SERVER'][k] = v || '' | ||
} | ||
rt["_SERVER"]['REQUEST_URI'] = env['PATH_INFO'] | ||
|
||
# Evaluate the PHP file | ||
rt.with_events(events) do | ||
File.open(file, 'rb') { |f| rt.eval f } | ||
end | ||
end | ||
end | ||
events.to_a | ||
end | ||
|
||
private | ||
def params string | ||
string.split('&').each do |pair| | ||
yield pair.split('=').map { |x| Rack::Utils.unescape(x) } | ||
end | ||
end | ||
end | ||
|
||
if $0 == __FILE__ | ||
Rack::Handler::WEBrick.run( | ||
Rack::ShowExceptions.new(Rack::Phrack.new(ARGV[0] || Dir.pwd)), | ||
:Port => 10101) | ||
end |
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
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
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