Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

phuby webrick server will now serve GET requests

  • Loading branch information...
commit 2ecc0ce5ff47d60e04f2cf05efb91b6372aee081 1 parent d331e34
@tenderlove authored
View
15 bin/phuby_server
@@ -0,0 +1,15 @@
+#!/usr/bin/env ruby
+
+require 'rubygems'
+require 'phuby'
+require 'webrick'
+
+s = WEBrick::HTTPServer.new(
+ :Port => 10101,
+ :DocumentRoot => ARGV[0]
+)
+
+s.mount('/', Phuby::PHPHandler)
+
+trap('INT') { s.shutdown }
+s.start
View
1  lib/phuby.rb
@@ -2,6 +2,7 @@
require 'phuby/events'
require 'phuby/runtime'
require 'phuby/array'
+require 'phuby/php_handler'
module Phuby
VERSION = '1.0.0'
View
44 lib/phuby/php_handler.rb
@@ -0,0 +1,44 @@
+require 'webrick'
+
+module Phuby
+ class PHPHandler < WEBrick::HTTPServlet::FileHandler
+ class Events < Phuby::Events
+ def initialize req, res
+ super()
+ @req = req
+ @res = res
+ end
+
+ def write string
+ @res.body ||= ''
+ @res.body << string
+ end
+ end
+
+ def initialize server, root = server.config[:DocumentRoot] || Dir.pwd, *args
+ super
+ end
+
+ def do_GET req, res
+ return super(req, res) unless req.path =~ /\.php$/
+
+ file = File.join(@root, req.path)
+
+ Dir.chdir(@root) do
+ Phuby::Runtime.php do |rt|
+ req.query.each do |k,v|
+ rt['_GET'][k] = v
+ end
+
+ events = Events.new req, res
+
+ rt.with_events(events) do
+ File.open(file, 'rb') { |f|
+ rt.eval f
+ }
+ end
+ end
+ end
+ end
+ end
+end
View
22 test/assets/htdocs/index.php
@@ -0,0 +1,22 @@
+<html>
+ <head>
+ <title>
+Hello
+ </title>
+ </head>
+ <body>
+ <h1>Get Params</h1>
+ <table id="get">
+<?php
+ foreach($_GET as $key => $val) {
+?>
+ <tr>
+ <td><?php echo $key; ?></td>
+ <td><?php echo $val; ?></td>
+ </tr>
+<?php
+ }
+?>
+ </table>
+ </body>
+</html>
View
4 test/helper.rb
@@ -1,11 +1,13 @@
-Process.setrlimit(Process::RLIMIT_CORE, Process::RLIM_INFINITY) unless RUBY_PLATFORM =~ /(java|mswin|mingw)/i
+#Process.setrlimit(Process::RLIMIT_CORE, Process::RLIM_INFINITY) unless RUBY_PLATFORM =~ /(java|mswin|mingw)/i
require 'test/unit'
+require 'nokogiri'
require 'phuby'
module Phuby
class TestCase < Test::Unit::TestCase
ASSETS_DIR = File.join(File.dirname(__FILE__), 'assets')
+ HTDOCS_DIR = File.join(File.dirname(__FILE__), 'assets', 'htdocs')
unless RUBY_VERSION >= '1.9'
undef :default_test
View
35 test/test_php_handler.rb
@@ -0,0 +1,35 @@
+require 'helper'
+
+class TestPHPHandler < Phuby::TestCase
+ FakeServer = Struct.new(:config)
+
+ class FakeRequest
+ attr_accessor :request_uri, :query, :path
+ def initialize uri
+ @request_uri = URI.parse "http://localhost#{uri}"
+ @query = @request_uri.query ? Hash[
+ *@request_uri.query.split('&').map { |param| param.split('=') }.flatten
+ ] : {}
+ @path = @request_uri.path
+ end
+ end
+
+ class FakeResponse < Struct.new(:body)
+ end
+
+ def setup
+ @server = FakeServer.new(
+ :DocumentRoot => HTDOCS_DIR
+ )
+ end
+
+ def test_get
+ req = FakeRequest.new('/index.php?a=b&c=d')
+ res = FakeResponse.new('')
+
+ handler = Phuby::PHPHandler.new @server
+ handler.do_GET req, res
+
+ assert_match 'Get Params', res.body
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.