php-server 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +1 -0
- data/bin/phpserver +42 -0
- data/lib/php-server.rb +32 -0
- data/lib/php-server/phphandler.rb +96 -0
- data/lib/php-server/version.rb +4 -0
- data/php-server.gemspec +19 -0
- data/public/index.php +0 -0
- metadata +58 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Eido NABESHIMA
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# PHPServer
|
2
|
+
|
3
|
+
Simple PHP server for development.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'php-server'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install php-server
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
$ phpserver
|
22
|
+
|
23
|
+
### CakePHP
|
24
|
+
|
25
|
+
$ phpserver cakephp
|
26
|
+
|
27
|
+
### FuelPHP
|
28
|
+
|
29
|
+
$ phpserver fuelphp
|
30
|
+
|
31
|
+
## Required
|
32
|
+
|
33
|
+
* php-cgi
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/phpserver
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "optparse"
|
4
|
+
require "php-server"
|
5
|
+
|
6
|
+
type = ""
|
7
|
+
|
8
|
+
option = {
|
9
|
+
:DocumentRoot => './',
|
10
|
+
:Port => 3000,
|
11
|
+
:PHPPath => '/usr/local/bin'
|
12
|
+
}
|
13
|
+
|
14
|
+
opt = OptionParser.new
|
15
|
+
opt.on('-d VAL', '--dir=VAL') {|v| option[:DocumentRoot] = v }
|
16
|
+
opt.on('-p VAL', '--port=VAL') {|v| option[:Port] = v.to_i }
|
17
|
+
opt.on('-t VAL', '--type=VAL') {|v| type = v }
|
18
|
+
|
19
|
+
opt.order! ARGV
|
20
|
+
type = ARGV.shift; opt.parse! ARGV unless ARGV.empty?
|
21
|
+
|
22
|
+
case type
|
23
|
+
when 'fuelphp', 'fuel'
|
24
|
+
option[:DocumentRoot] = './public'
|
25
|
+
when 'cakephp', 'cake'
|
26
|
+
option[:DocumentRoot] = './app/webroot'
|
27
|
+
end
|
28
|
+
|
29
|
+
s = PHPServer::HTTPServer.new option
|
30
|
+
|
31
|
+
case type
|
32
|
+
when 'fuelphp', 'fuel'
|
33
|
+
s.rewrite /\/(?<url>assets.*)/, '/\k<url>'
|
34
|
+
s.rewrite /\/(?<url>.*)/, '/index.php/\k<url>'
|
35
|
+
when 'cakephp', 'cake'
|
36
|
+
s.rewrite /\/(?<url>(css|js|img|files).*)/, '/\k<url>'
|
37
|
+
s.rewrite /\/(?<url>.*)/, '/index.php'
|
38
|
+
end
|
39
|
+
|
40
|
+
trap('INT') { s.shutdown }
|
41
|
+
|
42
|
+
s.start
|
data/lib/php-server.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'webrick'
|
2
|
+
require 'php-server/phphandler'
|
3
|
+
|
4
|
+
module PHPServer
|
5
|
+
include WEBrick
|
6
|
+
|
7
|
+
class HTTPServer < HTTPServer
|
8
|
+
def initialize *arg
|
9
|
+
@rewrite_rules = []
|
10
|
+
super
|
11
|
+
mount '/', WEBrick::HTTPServlet::FileHandler, config[:DocumentRoot], :FancyIndexing => true, :HandlerTable => { 'php' => PHPServer::PHPHandler }
|
12
|
+
end
|
13
|
+
|
14
|
+
def rewrite pattern, subst
|
15
|
+
@logger.info "rewrite rule #{pattern.inspect} -> #{subst}."
|
16
|
+
@rewrite_rules << [pattern, subst]
|
17
|
+
end
|
18
|
+
|
19
|
+
def service req, res
|
20
|
+
path = req.path
|
21
|
+
@rewrite_rules.each do |pattern, subst|
|
22
|
+
if pattern =~ path
|
23
|
+
new_path = path.gsub pattern, subst
|
24
|
+
@logger.info "rewrote url from #{path} to #{new_path}"
|
25
|
+
req.instance_variable_set "@path", new_path
|
26
|
+
break
|
27
|
+
end
|
28
|
+
end
|
29
|
+
super req, res
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
#
|
2
|
+
# phphandler.rb -- PHPHandler Class
|
3
|
+
#
|
4
|
+
# This class is based on cgihandler.rb from the WEBrick bundle.
|
5
|
+
#
|
6
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
7
|
+
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
8
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
9
|
+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
10
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
11
|
+
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
12
|
+
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
13
|
+
#
|
14
|
+
|
15
|
+
require 'rbconfig'
|
16
|
+
require 'tempfile'
|
17
|
+
require 'webrick'
|
18
|
+
require 'webrick/config'
|
19
|
+
require 'webrick/httpservlet/abstract'
|
20
|
+
|
21
|
+
module PHPServer
|
22
|
+
include WEBrick
|
23
|
+
|
24
|
+
class PHPHandler < HTTPServlet::AbstractServlet
|
25
|
+
PHPCGI = 'php-cgi'
|
26
|
+
|
27
|
+
def initialize(server, name)
|
28
|
+
super(server, name)
|
29
|
+
@phpcmd = File.join(@server[:PHPPath], PHPCGI)
|
30
|
+
@php_fullpath_script = name
|
31
|
+
end
|
32
|
+
|
33
|
+
def do_GET(req, res)
|
34
|
+
data = nil
|
35
|
+
status = -1
|
36
|
+
|
37
|
+
meta = req.meta_vars
|
38
|
+
meta["SCRIPT_FILENAME"] = @php_fullpath_script
|
39
|
+
meta["PATH"] = @config[:PHPPath]
|
40
|
+
meta["REDIRECT_STATUS"] = "200" # php-cgi/apache specific value
|
41
|
+
if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
|
42
|
+
meta["SystemRoot"] = ENV["SystemRoot"]
|
43
|
+
end
|
44
|
+
ENV.update(meta)
|
45
|
+
|
46
|
+
cgi_in = IO::popen(@phpcmd, "r+b")
|
47
|
+
begin
|
48
|
+
cgi_in.sync = true
|
49
|
+
|
50
|
+
if req.body and req.body.bytesize > 0
|
51
|
+
cgi_in.write(req.body)
|
52
|
+
end
|
53
|
+
cgi_in.close_write
|
54
|
+
ensure
|
55
|
+
data = cgi_in.read
|
56
|
+
cgi_in.close_read
|
57
|
+
status = $?.exitstatus
|
58
|
+
sleep 0.1 if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
|
59
|
+
end
|
60
|
+
|
61
|
+
@script_filename = meta['SCRIPT_NAME']
|
62
|
+
if status != 0
|
63
|
+
@logger.error("PHPHandler: #{@script_filename} exit with #{status}")
|
64
|
+
end
|
65
|
+
|
66
|
+
data = "" unless data
|
67
|
+
raw_header, body = data.split(/^[\xd\xa]+/, 2)
|
68
|
+
raise WEBrick::HTTPStatus::InternalServerError,
|
69
|
+
"PHPHandler: Premature end of script headers: #{@script_filename}" if body.nil?
|
70
|
+
|
71
|
+
begin
|
72
|
+
header = WEBrick::HTTPUtils::parse_header(raw_header)
|
73
|
+
if /^(\d+)/ =~ header['status'][0]
|
74
|
+
res.status = $1.to_i
|
75
|
+
header.delete('status')
|
76
|
+
end
|
77
|
+
if header.has_key?('location')
|
78
|
+
# RFC 3875 6.2.3, 6.2.4
|
79
|
+
res.status = 302 unless (300...400) === res.status
|
80
|
+
end
|
81
|
+
if header.has_key?('set-cookie')
|
82
|
+
header['set-cookie'].each { |k|
|
83
|
+
res.cookies << WEBrick::Cookie.parse_set_cookie(k)
|
84
|
+
}
|
85
|
+
header.delete('set-cookie')
|
86
|
+
end
|
87
|
+
header.each { |key, val| res[key] = val.join(", ") }
|
88
|
+
rescue => ex
|
89
|
+
raise WEBrick::HTTPStatus::InternalServerError, ex.message
|
90
|
+
end
|
91
|
+
res.body = body
|
92
|
+
end
|
93
|
+
|
94
|
+
alias do_POST do_GET
|
95
|
+
end
|
96
|
+
end
|
data/php-server.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'php-server/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "php-server"
|
8
|
+
gem.version = PHPServer::VERSION
|
9
|
+
gem.authors = ["Eido NABESHIMA"]
|
10
|
+
gem.email = ["closer009@gmail.com"]
|
11
|
+
gem.description = %q{Webrick server: PHP mounted}
|
12
|
+
gem.summary = %q{Webrick server: PHP mounted}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
data/public/index.php
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: php-server
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Eido NABESHIMA
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-27 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! 'Webrick server: PHP mounted'
|
15
|
+
email:
|
16
|
+
- closer009@gmail.com
|
17
|
+
executables:
|
18
|
+
- phpserver
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/phpserver
|
28
|
+
- lib/php-server.rb
|
29
|
+
- lib/php-server/phphandler.rb
|
30
|
+
- lib/php-server/version.rb
|
31
|
+
- php-server.gemspec
|
32
|
+
- public/index.php
|
33
|
+
homepage: ''
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
none: false
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
none: false
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.23
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: ! 'Webrick server: PHP mounted'
|
57
|
+
test_files: []
|
58
|
+
has_rdoc:
|