Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

added php handler #220

Closed
wants to merge 1 commit into from

4 participants

@questmaster

Hi there, I added an php handler for webrick based on the cgihandler. I think it is helpful and would be happy to get it included in ruby.

Regards
Daniel

@cjapes

I don't see the need for a php handler to be included in ruby.

@tenderlove
Collaborator

/cc @nahi

@questmaster

Well, I see two possible scenarios for php support in webrick. First, we use webrick as a small and light weight webserver for a small project. We also depend on a 'legacy' php-webapp which we can use with the php-handler. A second one I have seen here: http://stackoverflow.com/questions/5437664/using-webrick-to-serve-php-web-applications . For people switching from php to ruby/ror it may be easier if they can run their former php projects in the new ruby environment.

What do you think?

@marcandre
Collaborator

I think this should be released as a separate gem.

I doubt @nahi or anyone in ruby-core would want to support this.

Also, this patch can't be accepted as is, since it doesn't include any test.

In general, we're trying for move functionality out of the std lib into gems (easier to update, support, view as an independent , etc...)

@cjapes

I agree with @marcandre

@questmaster

Ok, I see your point. I will add tests and see how to build a gem...

Thanks for your comments!

@marcandre
Collaborator

Good. I'll close this pull request. To make a gem, use bundler. http://railscasts.com/episodes/245-new-gem-with-bundler

@marcandre marcandre closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 9, 2012
  1. @questmaster

    added php handler

    questmaster authored
This page is out of date. Refresh to see the latest.
Showing with 97 additions and 0 deletions.
  1. +97 −0 lib/webrick/httpservlet/phphandler.rb
View
97 lib/webrick/httpservlet/phphandler.rb
@@ -0,0 +1,97 @@
+#
+# phphandler.rb -- PHPHandler Class
+#
+# This class is based on cgihandler.rb from the WEBrick bundle.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
+# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+require 'rbconfig'
+require 'tempfile'
+require 'webrick/config'
+require 'webrick/httpservlet/abstract'
+
+module WEBrick
+ module HTTPServlet
+
+ class PHPHandler < AbstractServlet
+ PHPCGI = 'php-cgi'
+
+ def initialize(server, name)
+ super(server, name)
+ @phpcmd = File.join(@server[:PHPPath], PHPCGI).gsub("/", "\\")
+ @php_fullpath_script = name
+ end
+
+ def do_GET(req, res)
+ data = nil
+ status = -1
+
+ meta = req.meta_vars
+ meta["SCRIPT_FILENAME"] = @php_fullpath_script
+ meta["PATH"] = @config[:PHPPath]
+ meta["REDIRECT_STATUS"] = "200" # php-cgi/apache specific value
+ if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
+ meta["SystemRoot"] = ENV["SystemRoot"]
+ end
+ ENV.update(meta)
+
+ cgi_in = IO::popen(@phpcmd, "r+b")
+ begin
+ cgi_in.sync = true
+
+ if req.body and req.body.bytesize > 0
+ cgi_in.write(req.body)
+ end
+ cgi_in.close_write
+ ensure
+ data = cgi_in.read
+ cgi_in.close_read
+ status = $?.exitstatus
+ sleep 0.1 if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
+ end
+
+ @script_filename = meta['SCRIPT_NAME']
+ if status != 0
+ @logger.error("PHPHandler: #{@script_filename} exit with #{status}")
+ end
+
+ data = "" unless data
+ raw_header, body = data.split(/^[\xd\xa]+/, 2)
+ raise HTTPStatus::InternalServerError,
+ "PHPHandler: Premature end of script headers: #{@script_filename}" if body.nil?
+
+ begin
+ header = HTTPUtils::parse_header(raw_header)
+ if /^(\d+)/ =~ header['status'][0]
+ res.status = $1.to_i
+ header.delete('status')
+ end
+ if header.has_key?('location')
+ # RFC 3875 6.2.3, 6.2.4
+ res.status = 302 unless (300...400) === res.status
+ end
+ if header.has_key?('set-cookie')
+ header['set-cookie'].each { |k|
+ res.cookies << Cookie.parse_set_cookie(k)
+ }
+ header.delete('set-cookie')
+ end
+ header.each { |key, val| res[key] = val.join(", ") }
+ rescue => ex
+ raise HTTPStatus::InternalServerError, ex.message
+ end
+ res.body = body
+ end
+
+ alias do_POST do_GET
+ end
+
+ end
+end
Something went wrong with that request. Please try again.