【问题标题】:Ruby parsing arguments of current URIRuby 解析当前 URI 的参数
【发布时间】:2012-01-25 18:15:22
【问题描述】:

我正在使用 ruby​​ 生成 CGI(不在 Rails 上)。

我想解析本地运行的脚本的参数(使用 localhost:3000/apage.cgi?key=value 之类的请求),就像我为命令行参数解析 ARGV[0] 一样。

当我尝试:params = CGI.parse(request.query_string) 时,我得到“未定义的局部变量或方法请求”。

编辑:这是代码。这就是为什么我没有把它放在首位:)

#!/usr/bin/env ruby
require 'cgi'
print "Content-type: text/plain\n\n"
#print CGI.parse(URI.parse(request.fullpath).query)

【问题讨论】:

  • 你能给出完整的代码吗?乍一看,您使用的是 request 方法或变量,这在您的代码中没有定义。
  • 准确地说,需要哪个模块才能访问它? (或者干脆用纯 ruby​​ 获取当前的基本 url?)此时,我只是想输出发送到我当前脚本的参数。
  • CGI 接口假定您将从标准输入(en.wikipedia.org/wiki/Common_Gateway_Interface)读取请求。此外,CGI 服务器设置环境变量,这可能很有用(如 REQUEST_URI)。为了从 STDIN 中读取数据,使用 get 方法。要访问环境变量,请使用 ENV['variable']。
  • 非常感谢,看来 print ENV['QUERY_STRING'] 是我要找的。​​span>

标签: ruby cgi arguments


【解决方案1】:

你的脚本是对的,你在这段代码中真的没有request。最好使用 fcgi 而不是 cgi,这里是示例:

#!/usr/bin/env ruby

require 'fcgi'
require 'rack'


# this class made fcgi env compatible with rack
class Rack::PathInfoRewriter
  def initialize
  end

  def call(env)
    # here you can place initialization of you app
    request = Rack::Request.new env

    # here is params. You can also find all info about request in this object  
    request.params 

    # you app should return result in such format
    [200, "Content-type: text/plain", ['html']]
  end
end

Rack::Handler::FastCGI.run Rack::PathInfoRewriter.new

【讨论】:

    【解决方案2】:

    我假设你有支持 CGI 接口的网络服务器。

    试试这个脚本:

    require 'cgi'
    
    cgi = CGI.new
    cgi.out{ ENV['REQUEST_URI'] } 
    

    这将回复包含您的请求查询的文本。

    【讨论】:

      【解决方案3】:

      当我需要解析 URL 时,我使用 Ruby 的内置 URI 模块或 Addressable gem。

      使用 URI,您需要附加协议以获得正确的行为:

      URI.parse('http://' + 'localhost:3000/apage.cgi?key=value').query # => "key=value"
      

      您可以通过更多的按摩来创建值的哈希:

      Hash[URI.parse('http://localhost:3000/apage.cgi?key=value').query.split('&').map{ |q| q.split('=') }]
      {
          "key" => "value"
      }
      
      Hash[URI.parse('http://localhost:3000/apage.cgi?key=value&foo=bar').query.split('&').map{ |q| q.split('=') }]
      {
          "key" => "value",
          "foo" => "bar"
      }
      

      同样,使用 Addressable:

      require "addressable/uri" # => true
      uri = Addressable::URI.parse('http://' + 'localhost:3000/apage.cgi?key=value')
      #<Addressable::URI:0x100c80978
          @uri_string = nil,
          @validation_deferred = false,
          attr_accessor :authority = nil,
          attr_accessor :host = "localhost",
          attr_accessor :path = "/apage.cgi",
          attr_accessor :port = 3000,
          attr_accessor :query = "key=value",
          attr_accessor :scheme = "http",
          attr_reader :hash = nil,
          attr_reader :normalized_host = nil,
          attr_reader :normalized_path = nil,
          attr_reader :normalized_port = nil,
          attr_reader :normalized_query = nil,
          attr_reader :normalized_scheme = nil
      >
      uri.query # => "key=value"
      uri.query_values # => {"key" => "value"}
      

      【讨论】:

        猜你喜欢
        • 2017-09-06
        • 1970-01-01
        • 1970-01-01
        • 2013-09-14
        • 2023-01-26
        • 1970-01-01
        • 2014-10-23
        • 2011-08-01
        相关资源
        最近更新 更多