What is the bare minimum I need to require in Ruby (not Rails or any other framework) to easily obtain request data like GETs?
我需要在Ruby(不是Rails或任何其他框架)中轻松获取GET等请求数据的最低要求是什么?
I would like to avoid having to use ruby on rails or any other framework to get this data, so the ideal answer wouldn't have a framework dependency.
我想避免在rails或任何其他框架上使用ruby来获取这些数据,因此理想的答案不会有框架依赖。
My current setup has a ruby file (script
) in a cgi-bin
on apache (site.com/script
)
我当前的设置在apache(site.com/script)上的cgi-bin中有一个ruby文件(脚本)
#!/usr/bin/env ruby
# bare minimum to output content
puts "Content-type: text/html"
puts
puts "it works!" #works fine.
# how would I obtain request data like GET parameters here
# for a url like site.com/script?hi=there
# in PHP, I would $_GET['hi']
3 个解决方案
#1
5
Ah, solved my problem using the CGI class.
啊,使用CGI类解决了我的问题。
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/cgi/rdoc/CGI.html
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/cgi/rdoc/CGI.html
require 'cgi'
cgi = CGI.new
cgi.params.each do |key, val|
puts key
puts val
end
so site.com/script?hi=there
would output hi\nthere
所以site.com/script?hi=there会输出hi \ nthere
#2
5
Please don't do this without a framework. There are some super lightweight ones like Sinatra that do as little as possible to ensure you have the support you need to do this correctly. CGI died in the 1990s and doesn't need to come back now.
如果没有框架,请不要这样做。像Sinatra这样的超轻量级产品尽可能少,以确保您获得正确执行此操作所需的支持。 CGI在20世纪90年代去世,现在不需要回来。
To write a Sinatra application you basically define a method, then deploy it to your server. Sure, you may grumble about how much work that is, but if you don't have a proper deployment procedure you're in trouble before you even start.
要编写Sinatra应用程序,您基本上定义一个方法,然后将其部署到您的服务器。当然,你可能会抱怨工作量是多少,但如果你没有适当的部署程序,那么在你开始之前就遇到了麻烦。
Ruby has a lot of infrastructure built up around things like Rack that hook into Apache through modules like Passenger that do a lot more than cgi-bin
ever did without all the risks associated with it.
Ruby有许多基础架构构建的基础架构,它通过像Passenger这样的模块连接到Apache,它比cgi-bin做得更多,没有与之相关的所有风险。
A typical Sinatra app looks like:
典型的Sinatra应用程序如下所示:
get '/example/:id' do
"Example #{params[:id]}!"
end
There's really nothing to it and it ends up being a lot less work than the old CGI way.
它真的没什么可比的,它最终比旧的CGI方式少得多。
#3
2
If your web server is calling the script as a CGI program, then the contents would be in the QUERY_STRING
environment variable, accessible through the ENV
object. Without a framework, you will have to parse this into individual name-value pairs to get something like PHP's $_GET
.
如果您的Web服务器将脚本作为CGI程序调用,则内容将位于QUERY_STRING环境变量中,可通过ENV对象访问。如果没有框架,则必须将其解析为单独的名称 - 值对,以获得类似PHP的$ _GET。
Because Ruby is a general-purpose programming language, it doesn't have core classes and methods for dealing with HTTP requests or the CGI environment. That is why it is recommended to use a framework, such as Ruby on Rails, to handle the protocol-level parsing and setup. There are too many places to make mistakes, and it is not worth reinventing the wheel.
因为Ruby是一种通用编程语言,所以它没有用于处理HTTP请求或CGI环境的核心类和方法。这就是为什么建议使用Ruby on Rails等框架来处理协议级解析和设置的原因。有太多的地方犯错误,不值得重新发明*。
#1
5
Ah, solved my problem using the CGI class.
啊,使用CGI类解决了我的问题。
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/cgi/rdoc/CGI.html
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/cgi/rdoc/CGI.html
require 'cgi'
cgi = CGI.new
cgi.params.each do |key, val|
puts key
puts val
end
so site.com/script?hi=there
would output hi\nthere
所以site.com/script?hi=there会输出hi \ nthere
#2
5
Please don't do this without a framework. There are some super lightweight ones like Sinatra that do as little as possible to ensure you have the support you need to do this correctly. CGI died in the 1990s and doesn't need to come back now.
如果没有框架,请不要这样做。像Sinatra这样的超轻量级产品尽可能少,以确保您获得正确执行此操作所需的支持。 CGI在20世纪90年代去世,现在不需要回来。
To write a Sinatra application you basically define a method, then deploy it to your server. Sure, you may grumble about how much work that is, but if you don't have a proper deployment procedure you're in trouble before you even start.
要编写Sinatra应用程序,您基本上定义一个方法,然后将其部署到您的服务器。当然,你可能会抱怨工作量是多少,但如果你没有适当的部署程序,那么在你开始之前就遇到了麻烦。
Ruby has a lot of infrastructure built up around things like Rack that hook into Apache through modules like Passenger that do a lot more than cgi-bin
ever did without all the risks associated with it.
Ruby有许多基础架构构建的基础架构,它通过像Passenger这样的模块连接到Apache,它比cgi-bin做得更多,没有与之相关的所有风险。
A typical Sinatra app looks like:
典型的Sinatra应用程序如下所示:
get '/example/:id' do
"Example #{params[:id]}!"
end
There's really nothing to it and it ends up being a lot less work than the old CGI way.
它真的没什么可比的,它最终比旧的CGI方式少得多。
#3
2
If your web server is calling the script as a CGI program, then the contents would be in the QUERY_STRING
environment variable, accessible through the ENV
object. Without a framework, you will have to parse this into individual name-value pairs to get something like PHP's $_GET
.
如果您的Web服务器将脚本作为CGI程序调用,则内容将位于QUERY_STRING环境变量中,可通过ENV对象访问。如果没有框架,则必须将其解析为单独的名称 - 值对,以获得类似PHP的$ _GET。
Because Ruby is a general-purpose programming language, it doesn't have core classes and methods for dealing with HTTP requests or the CGI environment. That is why it is recommended to use a framework, such as Ruby on Rails, to handle the protocol-level parsing and setup. There are too many places to make mistakes, and it is not worth reinventing the wheel.
因为Ruby是一种通用编程语言,所以它没有用于处理HTTP请求或CGI环境的核心类和方法。这就是为什么建议使用Ruby on Rails等框架来处理协议级解析和设置的原因。有太多的地方犯错误,不值得重新发明*。