Forum: Ruby Webrick problem, PREMATURE END OF SCRIPT HEADERS

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
D6b6cdd2e4ae01b86d993ef58528993c?d=identicon&s=25 Kevin Layman (cujjjo)
on 2007-03-22 06:39
I've downloaded and installed Ruby (v1.8.5) w/the latest Windows
installer and am trying to run some cgi's per these examples:

I keep getting HTTP 500 - Internal server error, and in the Ruby command

I've got the path to the Ruby executable in test.cgi, and the next line
outputs a valid header, and this cgi will execute correctly when run by
Webrick directly.  Programs are below.

print "Content-type: text/plain\r\n\r\n"
ENV.keys.sort.each{|k| puts "#{k} ==> #{ENV[k]}"}

require 'webrick'

include WEBrick    # let's import the namespace so
                   # I don't have to keep typing
                   # WEBrick:: in this documentation.

def start_webrick(config = {})
  # always listen on port 8080
  config.update(:Port => 8080)
  server =
  yield server if block_given?
  ['INT', 'TERM'].each {|signal|
    trap(signal) {server.shutdown}


#start_webrick(:DocumentRoot =>
"g:/infoproweb/webfolder/cgi-bin/ruby",:CGIInterpreter =>
start_webrick(:DocumentRoot => "g:/infoproweb/webfolder/")

start_webrick {|server|
  #cgi_dir = File.expand_path('~/cgi-bin/ruby')
  cgi_dir = "g:/infoproweb/webfolder/cgi-bin/ruby"
  server.mount("/cgi-bin/ruby", HTTPServlet::FileHandler, cgi_dir,
81fc062528fff2d38b393dab081873fe?d=identicon&s=25 PerfectDayToChaseTornados (Guest)
on 2007-03-22 21:21
(Received via mailing list)
"Kevin Layman" <> wrote in message
>                   # I don't have to keep typing
>  server.start
>  #cgi_dir = File.expand_path('~/cgi-bin/ruby')
>  cgi_dir = "g:/infoproweb/webfolder/cgi-bin/ruby"
>  server.mount("/cgi-bin/ruby", HTTPServlet::FileHandler, cgi_dir,
>    {:FancyIndexing=>true})
> }
> --
> Posted via

You probably have something else already running on that port. Either
Webrick on a different port or stop whatever is bound to that port.
C2253345d045285df6751f7d65ff8569?d=identicon&s=25 Paul Stickney (Guest)
on 2007-03-22 21:42
(Received via mailing list)
Try to run that program using ruby directly.
It might be something as simple as a syntax error.

Starting WEBrick from a ``CGI'' like that looks kind of odd...
Ff3e75c39d71f0f2118d6824411935ea?d=identicon&s=25 Diego Guillen (rubyfacil)
on 2009-05-22 02:08
Hi, I think the problem is that webrick doesn't know how to execute the
"shebang" line found in cgi scripts on Windows (in Linux it's not a

I found the same problem more recently with Ruby 1.9.1 on Windows.

A generic solution that works for both Ruby 1.8.6.pxxx and 1.9.1.p0 on
Windows is the following:

Edit the file: c:\ruby\lib\ruby\1.8\webrick\httpservlet\cgi_runner.rb

Add the following lines at the top of the file:

if "1.9.1" == RUBY_VERSION
  require 'rbconfig'  #constants telling where Ruby runs from

Now, locate the last line where is says:  exec ENV["SCRIPT_FILENAME"]
Comment that line out and add the following code:

# --- from here ---
if "1.9.1" == RUBY_VERSION  #use RbConfig
  Ruby = File::join(RbConfig::CONFIG['bindir'],
  Ruby << RbConfig::CONFIG['EXEEXT']
else                        # use ::Config
  Ruby = File::join(::Config::CONFIG['bindir'],
  Ruby << ::Config::CONFIG['EXEEXT']

if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
  exec "#{Ruby}", ENV["SCRIPT_FILENAME"]
# --- to here ---

Save the file and restart the webrick server.

This code just builds a constant 'Ruby' with the full path to
"ruby.exe", and
(if you're running on Windows) it passes the additional parameter
"c:\ruby\bin\ruby.exe" , to the Kernel.exec() method, so that your
script can be executed.
This topic is locked and can not be replied to.