当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Ruby CGI.out用法及代码示例


本文简要介绍ruby语言中 CGI.out 的用法。

用法

out(content_type_string='text/html')
out(headers_hash)

将 HTTP 标头和正文打印到 $DEFAULT_OUTPUT ($>)

content_type_string

如果传递了一个字符串,则假定它是内容类型。

headers_hash

这是一个 Hash 的标头,类似于 http_header 使用的标头。

block

需要一个块,并且应该评估响应的主体。

Content-Length 是根据内容块返回的 String 的大小自动计算的。

如果 ENV['REQUEST_METHOD'] == "HEAD" ,则仅输出标头(仍需要内容块,但会被忽略)。

如果字符集是 “iso-2022-jp” 或 “euc-jp” 或 “shift_jis”,则内容将转换为此字符集,并且语言设置为 “ja”。

例子:

cgi = CGI.new
cgi.out{ "string" }
  # Content-Type: text/html
  # Content-Length: 6
  #
  # string

cgi.out("text/plain") { "string" }
  # Content-Type: text/plain
  # Content-Length: 6
  #
  # string

cgi.out("nph"        => true,
        "status"     => "OK",  # == "200 OK"
        "server"     => ENV['SERVER_SOFTWARE'],
        "connection" => "close",
        "type"       => "text/html",
        "charset"    => "iso-2022-jp",
          # Content-Type: text/html; charset=iso-2022-jp
        "language"   => "ja",
        "expires"    => Time.now + (3600 * 24 * 30),
        "cookie"     => [cookie1, cookie2],
        "my_header1" => "my_value",
        "my_header2" => "my_value") { "string" }
   # HTTP/1.1 200 OK
   # Date: Sun, 15 May 2011 17:35:54 GMT
   # Server: Apache 2.2.0
   # Connection: close
   # Content-Type: text/html; charset=iso-2022-jp
   # Content-Length: 6
   # Content-Language: ja
   # Expires: Tue, 14 Jun 2011 17:35:54 GMT
   # Set-Cookie: foo
   # Set-Cookie: bar
   # my_header1: my_value
   # my_header2: my_value
   #
   # string

相关用法


注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 CGI.out。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。