如何将 ruby​​ Thin 与 CGI 脚本一起使用?

发布于 2024-08-13 04:37:01 字数 1313 浏览 6 评论 0 原文

我编写了一些 ruby​​ CGI 脚本(使用 Ruby CGI 类),并使用 lighttpd 从生产服务器提供这些脚本。我想使用 Thin 在我的开发服务器上测试它们。基本上,我想将所有 CGI 脚本放在一个目录中,并在该目录中开始精简。然后,任何对 http://localhost:3000/> 的请求应该只执行

更新:

这个rackup文件似乎有效。我不确定这是否是最好的解决方案,但对于开发环境来说应该没问题。

run(lambda do |env|
  require 'rubygems'
  require 'systemu'
  script = env['REQUEST_PATH'][1..-1] + '.rb'
  response = '' 
  err = ''
  systemu(['ruby', script], 'stdout' => response, 'stderr' => err, 'env' => { 
    'foo' => 'bar' })
  if err.length > 0 
    [ 500, {'Content-Type' => 'text/plain'}, err ]
  else
    idx = 0
    status = -1
    headers = {}
    while true
      line_end = response.index("\n", idx)
      line = response[idx..line_end].strip
      idx = line_end+1

      if status < 0
        if line =~ /(\d\d\d)/
          status = $1.to_i
        else
          raise "Invalid status line: #{line}"
        end
      elsif line.empty?
        break
      else
        name, value = line.split /: ?/
        headers[name] = value
      end
    end
    content = response[idx..-1]
    [status, headers, content]
  end
end)

I've written some ruby CGI scripts (using the Ruby CGI class) that I serve from my production server using lighttpd. I want to test them on my development server using thin. Basically, I want to drop all my CGI scripts in a directory and start thin in that directory. Then, any requests to http://localhost:3000/<script> should just execute <script> in the current directory and return the results. If thin has a built-in way of doing this, I can't find it. I would imagine the Rack config file for this is easy if you know what you're doing, but I don't.

Update:

This rackup file seems to work. I'm not sure if it's the best solution, but it should be fine for a development environment.

run(lambda do |env|
  require 'rubygems'
  require 'systemu'
  script = env['REQUEST_PATH'][1..-1] + '.rb'
  response = '' 
  err = ''
  systemu(['ruby', script], 'stdout' => response, 'stderr' => err, 'env' => { 
    'foo' => 'bar' })
  if err.length > 0 
    [ 500, {'Content-Type' => 'text/plain'}, err ]
  else
    idx = 0
    status = -1
    headers = {}
    while true
      line_end = response.index("\n", idx)
      line = response[idx..line_end].strip
      idx = line_end+1

      if status < 0
        if line =~ /(\d\d\d)/
          status = $1.to_i
        else
          raise "Invalid status line: #{line}"
        end
      elsif line.empty?
        break
      else
        name, value = line.split /: ?/
        headers[name] = value
      end
    end
    content = response[idx..-1]
    [status, headers, content]
  end
end)

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

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

发布评论

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

评论(1

天暗了我发光 2024-08-20 04:37:01

我有点不清楚为什么 Rack 是必要的。如果您使用 Ruby 的内置 CGI 模块编写脚本,您应该能够告诉 Thin 将目录视为 cgi-bin,就像 Apache ScriptAlias 指令,Ruby CGI 将处理剩下的事情。如果 Thin 不能做到这一点,也许 lighttpd 会是一个更好的解决方案。

I'm a little unclear as to why Rack is necessary at all. If you wrote the script using Ruby's built-in CGI module, you should be able to just tell thin to treat the directory as a cgi-bin,just like the Apache ScriptAlias directive, and Ruby CGI will take care of the rest. If thin can't do this, perhaps lighttpd would be a better solution.

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