Permalink
Browse files

a rack adapter for phuby

  • Loading branch information...
1 parent b42a853 commit afd1a97abe5c9117c16d508dca282c3066ba1639 @tenderlove committed Oct 22, 2009
Showing with 91 additions and 1 deletion.
  1. +83 −0 bin/phrack
  2. +2 −1 ext/phuby/phuby_conversions.c
  3. +6 −0 test/test_object.rb
View
@@ -0,0 +1,83 @@
+#!/usr/bin/env ruby
+
+require 'phuby'
+require 'rack'
+
+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 = CGI.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
+ code, headers, body = 200, {}, ''
+ events = Events.new code, headers, body
+ 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.eval("date_default_timezone_set('America/Los_Angeles');")
+
+ params(env['QUERY_STRING']) do |k,v|
+ rt["_GET"][k] = v
+ end
+
+ params(env['rack.input'].read) do |k,v|
+ rt["_POST"][k] = v
+ end
+
+ env['HTTP_COOKIE'].split('; ').each do |pair|
+ k, v = pair.split '='
+ rt["_COOKIE"][k] = CGI.unescape(v)
+ end
+
+ env.each { |k,v|
+ next if k =~ /^rack/
+ rt['_SERVER'][k] = v || ''
+ }
+ rt["_SERVER"]['REQUEST_URI'] = env['PATH_INFO']
+
+ 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| CGI.unescape(x) }
+ end
+ end
+end
+
+if $0 == __FILE__
+ require 'rack'
+ require 'rack/showexceptions'
+
+ Rack::Handler::WEBrick.run(
+ Rack::ShowExceptions.new(Rack::Phrack.new(Dir.pwd)), :Port => 10101)
+end
@@ -26,6 +26,7 @@ zval * Phuby_value_to_zval(VALUE rt, VALUE value)
ZVAL_STRINGL(php_value, StringValuePtr(value), RSTRING_LEN(value), 1);
break;
case T_OBJECT:
+ case T_DATA:
{
object_init_ex(php_value, php_ruby_proxy);
VALUE map = rb_iv_get(rt, "@proxy_map");
@@ -49,7 +50,7 @@ zval * Phuby_value_to_zval(VALUE rt, VALUE value)
ZVAL_NULL(php_value);
break;
default:
- rb_raise(rb_eRuntimeError, "Can't convert ruby object: %d", TYPE(value));
+ rb_raise(rb_eRuntimeError, "Can't convert ruby object: %s %d", rb_class2name(CLASS_OF(value)), TYPE(value));
}
return php_value;
View
@@ -17,6 +17,12 @@ def value x
end
end
+ def test_stringio
+ Phuby::Runtime.php do |rt|
+ rt['x'] = StringIO.new('')
+ end
+ end
+
def test_method_call
x = FunObject.new
Phuby::Runtime.php do |rt|

0 comments on commit afd1a97

Please sign in to comment.