# Windows cgihandler Error: Premature end of script headers

**URL:** <https://www.ruby-forum.com/t/windows-cgihandler-error-premature-end-of-script-headers/88946>\
**Category:** Ruby\
**Created:** [March 15, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/windows-cgihandler-error-premature-end-of-script-headers/88946 "2007-03-15T20:44:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [March 15, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/windows-cgihandler-error-premature-end-of-script-headers/88946/1 "2007-03-15T20:44:31Z")

</div>

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:  
[http://microjet.ath.cx/webrickguide/html/CGIHandler.html](http://microjet.ath.cx/webrickguide/html/CGIHandler.html)

I keep getting HTTP 500 - Internal server error, and in the Ruby command  
line PREMATURE END OF SCRIPT HEADERS.

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.

test.cgi:  
#!g:\ruby\bin\ruby.exe  
print “Content-type: text/plain\r\n\r\n”  
ENV.keys.sort.each{|k| puts “#{k} ==\> #{ENV[k]}”}

cgi\_handler.rb:  
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 = HTTPServer.new(config)  
yield server if block\_given?  
[‘INT’, ‘TERM’].each {|signal|  
trap(signal) {server.shutdown}  
}  
server.start

end

#start\_webrick(:DocumentRoot =\>  
“g:/infoproweb/webfolder/cgi-bin/ruby”,:CGIInterpreter =\>  
“g:/ruby/bin/ruby.exe”)  
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,  
{:FancyIndexing=\>true})  
}

---

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [March 22, 2007, 6:36am UTC](https://www.ruby-forum.com/t/windows-cgihandler-error-premature-end-of-script-headers/88946/2 "2007-03-22T06:36:37Z")

</div>

Any suggestions at all on this would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [March 14, 2009, 1:14pm UTC](https://www.ruby-forum.com/t/windows-cgihandler-error-premature-end-of-script-headers/88946/3 "2009-03-14T13:14:02Z")

</div>

Kevin Layman wrote:

> Any suggestions at all on this would be greatly appreciated.

Hi had the same problem recently in Windows (with Ruby 1.8.6-26), after  
my script was already working in Ubuntu Linux.

I’m surprised that no one has provided an answer to this problem yet.

There could be several factors involved:

- your Windows security policies levels;
- what program gets associated with the execution of cgi scripts.

Doing a bit of debugging, I located the problem down to the last line on  
the file: c:\ruby\lib\ruby\1.8\webrick\httpservlet\cgi\_runner.rb  
where is says: exec ENV[“SCRIPT\_FILENAME”]

I replaced that line with the following code:

# — from here —

Ruby = File::join(::Config::CONFIG[‘bindir’],  
::Config::CONFIG[‘ruby\_install\_name’])  
Ruby \<\< ::Config::CONFIG[‘EXEEXT’]

if /mswin|bccwin|mingw/ =~ RUBY\_PLATFORM  
exec “#{Ruby}”, ENV[“SCRIPT\_FILENAME”]  
else  
exec ENV[“SCRIPT\_FILENAME”]  
end

# — to here —

This just builds a constant Ruby with the full path to “ruby.exe”, and  
(if you’re running on Windows) passes it as an extra first parameter  
“c:\ruby\bin\ruby.exe” , to the Kernel.exec() method.

Save the file and restart the server. It worked for me.

---

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [May 22, 2009, 1:58am UTC](https://www.ruby-forum.com/t/windows-cgihandler-error-premature-end-of-script-headers/88946/4 "2009-05-22T01:58:41Z")

</div>

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

A more 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 line at the top of the file:  
if “1.9.1” == RUBY\_VERSION  
require ‘rbconfig’ #constants telling where Ruby runs from  
end

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’],  
RbConfig::CONFIG[‘ruby\_install\_name’])  
Ruby \<\< RbConfig::CONFIG[‘EXEEXT’]  
else # use ::Config  
Ruby = File::join(::Config::CONFIG[‘bindir’],  
::Config::CONFIG[‘ruby\_install\_name’])  
Ruby \<\< ::Config::CONFIG[‘EXEEXT’]  
end

if /mswin|bccwin|mingw/ =~ RUBY\_PLATFORM  
exec “#{Ruby}”, ENV[“SCRIPT\_FILENAME”]  
else  
exec ENV[“SCRIPT\_FILENAME”]  
end

# — to here —

Save the file and restart the webrick server.

Explanation:  
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.
