对于 ruby​​/webrick,我需要 Windows 来识别 shebang (#!) 符号

发布于 2024-07-07 19:11:18 字数 1243 浏览 18 评论 0原文

(请耐心等待,我保证这会影响到 shebang 和 windows。)

我已经将最简单的 WEBRick 服务器放在一起了:

require 'webrick'
include WEBrick

s = HTTPServer.new(:Port=>2000, :DocumentRoot=>Dir::pwd)
s.start

再简单不过了。 这个基本服务器确实接受http连接(firefox、internetexploder、wget、TELENT)并适当地处理它们,只要我只是获取静态文档。 但是,如果我将目录中的其中一个文件设置为具有 .cgi 扩展名,我会收到 500 返回信息,并在服务器终端上显示以下内容:

ERROR CGIHandler: c:/rubyCGI/test.cgi: 
C:/...[snip]...webrick/httpservlet/cgi_runner.rb:45: in 'exec': Exec format error - ...[snip]...

我在命令行上做了一些操作来模拟正在发生的情况在 cgi_runner.rb 的第 45 行中

c:\>ruby
exec "c:/rubyCGI/test.cgi"
^Z
(same error erupts)

c:\>ruby
exec "ruby c:/rubyCGI/test.cgi"
^Z
Content-type: text/html

Mares eat oats and does eat oats and I'll be home for Christmas.

显然,WEBrick 尚未获得登陆 Windows 的许可。 你通常头痛的企业偏执使我无法修改 webrick,所以我可以在 c:/rubyCGI/test.cgi 中获得操作系统(windows)识别的 shebang 表示法,这样我就不必每次都明确地告诉它使用哪个解释器? 我可以将所有 .cgi 文件指定为与 ruby​​ 关联,但从长远来看,这会受到限制。

更新: 自从发布这篇文章以来,我突然想到,可能根本不可能从 ruby​​ 运行 CGI Web 服务器; ruby 没有分叉支持。 由于无法分叉进程,cgi 服务器必须一次执行每个 cgi 脚本,在第一个请求完成时忽略所有并发请求。 虽然这对于某些人来说可能是可以接受的,但它不适用于我的应用程序。 尽管如此,我仍然对我最初的问题的答案非常感兴趣——让 shebang 在 Windows 下工作。

(Bear with me, I promise this gets to shebang and windows.)

I have about the simplest of WEBRick servers put together:

require 'webrick'
include WEBrick

s = HTTPServer.new(:Port=>2000, :DocumentRoot=>Dir::pwd)
s.start

Couldn't be simpler. This basic server does accept http connections (firefox, internet exploder, wget, TELENT) and deals with them appropriately, as long as I'm just fetching static documents. If, however, I set one of the files in the directory to have a .cgi extension, I get a 500 back and the following on the server's terminal:

ERROR CGIHandler: c:/rubyCGI/test.cgi: 
C:/...[snip]...webrick/httpservlet/cgi_runner.rb:45: in 'exec': Exec format error - ...[snip]...

I've done a few things on the command line to mimic what is going on in line 45 of cgi_runner.rb

c:\>ruby
exec "c:/rubyCGI/test.cgi"
^Z
(same error erupts)

c:\>ruby
exec "ruby c:/rubyCGI/test.cgi"
^Z
Content-type: text/html

Mares eat oats and does eat oats and I'll be home for Christmas.

Clearly, WEBrick hasn't been cleared for landing on windows. Your usual headaches of corporate paranoia prevent me from modifying webrick, so can I get the shebang notation in c:/rubyCGI/test.cgi recognized by the OS (windows) so I don't have to explicitly tell it each time which interpreter to use? I could assign all .cgi files to be associated with ruby, but that would be limiting in the long run.

UPDATE:
Since posting this, it has occurred to me that it may not be possible at all to run a cgi web server from ruby; ruby has no forking support. With no ability to fork a process, a cgi server would have to execute each cgi script one-at-a-time, neglecting all concurrent requests while the first one completed. While this may be acceptable for some, it would not work for my application. Nevertheless, I would still be very interested in an answer to my original question—that of getting shebang working under windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼前雾蒙蒙 2024-07-14 19:11:18

我认为您想要的是将文件扩展名与 Ruby 相关联。 我认为不可能让 !# 符号在 Windows 上工作,但可以让 Windows 使用特定的解释器自动启动脚本(如第二个示例中所示)。 此处详细讨论了您想要执行的操作。您特别想要标题为“为不关联的文件类型创建文件关联”的部分。 我认为这会实现你想要做的事情。

I think what you want is to associate the file extension with Ruby. I don't think it's possible to get the !# notation to work on Windows but it is possible to get Windows to automatically launch a script with a particular interpreter (as in your second example). A good step by step discussion of what you'd want to do is here. You specifically want the section headed: "To create file associations for unassociated file types". I think that will accomplish what you're trying to do.

‖放下 2024-07-14 19:11:18

适用于 Ruby 1.8.6.pxxx 和 1.9.1.p0 的通用解决方案
Windows 如下:

编辑文件: c:\ruby\lib\ruby\1.9.1\webrick\httpservlet\cgi_runner.rb

在文件顶部添加以下行:

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

现在,找到最后一行:执行 ENV["SCRIPT_FILENAME"]
注释掉该行并添加以下代码:

# --- 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 ---

保存文件并重新启动 webrick 服务器。

解释:
此代码只是构建一个变量“Ruby”,其完整路径为
“ruby.exe”,以及
(如果您在 Windows 上运行)它会传递附加参数
"c:\ruby\bin\ruby.exe" ,到 Kernel.exec() 方法,这样你的
脚本可以被执行。

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.9.1\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
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 variable '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.

随心而道 2024-07-14 19:11:18

并不是真的要争论......但是当 mongrel 速度更快并且使用 Windows 进行本机编译时,为什么还要麻烦 webrick 呢? 当然,这意味着不需要 Shebang。

Not really to argue... but why bother webrick when mongrel is much faster and with native compiled with windows? And of coz, that means no shebang is needed.

梦途 2024-07-14 19:11:18

实际上,Windows 可以识别脚本文件中的 shebang 表示法。 它可以通过相对较短的脚本(例如 Ruby 或 AutoIt)来完成。 脚本文件的第一行只需要一个相当简单的解析器,以及一些文件操作。 当需要脚本文件的交叉兼容性或 Windows 文件扩展名不够时,我已经这样做过几次。

Actually, it is possible to get Windows to recognize shebang notation in script files. It can be done in a relatively short script in say, Ruby or AutoIt. Only a rather simple parser for the first line of a script file is required, along with some file manipulation. I have done this a couple times when either cross-compatibilty of script files was required or when Windows file extensions did not suffice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文