Ruby 2.1.0 リファレンスマニュアル > ライブラリ一覧 > cgi/coreライブラリ > CGIクラス
クラスの継承リスト: CGI < CGI::QueryExtension < Object < Kernel < BasicObject
CGI スクリプトを書くために必要な機能を提供するクラスです。
accept_charset -> String[permalink][rdoc]受けとることができるキャラクタセットを文字列で返します。 デフォルトは UTF-8 です。
accept_charset=(charset)[permalink][rdoc]受けとることができるキャラクタセットを設定します。
[SEE_ALSO] 多言語化
parse(query) -> Hash[permalink][rdoc]与えられたクエリ文字列をパースします。
例:
require "cgi"
params = CGI.parse("query_string")
# {"name1" => ["value1", "value2", ...],
# "name2" => ["value1", "value2", ...], ... }
accept_charset -> String[permalink][rdoc]受けとることができるキャラクタセットを文字列で返します。 デフォルトは UTF-8 です。
[SEE_ALSO] CGI.accept_charset, CGI.accept_charset=
header(options = "text/html") -> String[permalink][rdoc]HTTP ヘッダを options に従って生成します。 CGI#out と違い、標準出力には出力しません。 CGI#out を使わずに自力で HTML を出力したい場合などに使います。 このメソッドは文字列エンコーディングを変換しません。
ヘッダのキーとしては以下が利用可能です。
Content-Type ヘッダです。デフォルトは "text/html" です。
ボディのキャラクタセットを Content-Type ヘッダに追加します。
真偽値を指定します。真ならば、HTTP のバージョン、ステータスコード、 Date ヘッダをセットします。また Server と Connection の各ヘッダにもデフォルト値をセットします。 偽を指定する場合は、これらの値を明示的にセットしてください。
HTTP のステータスコードを指定します。 このリストの下に利用可能なステータスコードのリストがあります。
サーバソフトウェアの名称指定します。Server ヘッダに対応します。
接続の種類を指定します。Connection ヘッダに対応します。
送信するコンテンツの長さを指定します。Content-Length ヘッダに対応します。
送信するコンテンツの言語を指定します。Content-Language ヘッダに対応します。
送信するコンテンツの有効期限を Time のインスタンスで指定します。 Expires ヘッダに対応します。
クッキーとして文字列か CGI::Cookie のインスタンス、またはそれらの配列かハッシュを指定します。 一つ以上の Set-Cookie ヘッダに対応します。
status パラメータには以下の文字列が使えます。
"OK" --> "200 OK" "PARTIAL_CONTENT" --> "206 Partial Content" "MULTIPLE_CHOICES" --> "300 Multiple Choices" "MOVED" --> "301 Moved Permanently" "REDIRECT" --> "302 Found" "NOT_MODIFIED" --> "304 Not Modified" "BAD_REQUEST" --> "400 Bad Request" "AUTH_REQUIRED" --> "401 Authorization Required" "FORBIDDEN" --> "403 Forbidden" "NOT_FOUND" --> "404 Not Found" "METHOD_NOT_ALLOWED" --> "405 Method Not Allowed" "NOT_ACCEPTABLE" --> "406 Not Acceptable" "LENGTH_REQUIRED" --> "411 Length Required" "PRECONDITION_FAILED" --> "412 Rrecondition Failed" "SERVER_ERROR" --> "500 Internal Server Error" "NOT_IMPLEMENTED" --> "501 Method Not Implemented" "BAD_GATEWAY" --> "502 Bad Gateway" "VARIANT_ALSO_VARIES" --> "506 Variant Also Negotiates"
例:
header
# Content-Type: text/html
header("text/plain")
# Content-Type: text/plain
header({"nph" => true,
"status" => "OK", # == "200 OK"
# "status" => "200 GOOD",
"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 + 30,
"cookie" => [cookie1, cookie2],
"my_header1" => "my_value"
"my_header2" => "my_value"})
例:
cgi = CGI.new('html3')
print cgi.header({"charset" => "shift_jis", "status" => "OK"})
print "<html><head><title>TITLE</title></head>\r\n"
print "<body>BODY</body></html>\r\n"
[SEE_ALSO] [ruby-list:35911]
nph? -> bool[permalink][rdoc]out(options = "text/html") { .... }[permalink][rdoc]HTTP ヘッダと、ブロックで与えられた文字列を標準出力に出力します。
HEADリクエスト (REQUEST_METHOD == "HEAD") の場合は HTTP ヘッダのみを出力します。
charset が "iso-2022-jp"・"euc-jp"・"shift_jis" のいずれかで ある場合は文字列エンコーディングを自動変換し、language を "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" }
[SEE_ALSO] CGI#header
print(*strings)[permalink][rdoc][TODO]
引数の文字列を標準出力に出力します。 cgi.print は $DEFAULT_OUTPUT.print と等価です。
例:
cgi = CGI.new cgi.print "This line is a part of content body.\r\n"
CR -> String[permalink][rdoc]キャリッジリターンを表す文字列です。
EOL -> String[permalink][rdoc]改行文字です。
HTTP_STATUS -> Hash[permalink][rdoc]HTTP のステータスコードを表すハッシュです。
LF -> String[permalink][rdoc]ラインフィードを表す文字列です。
MAX_MULTIPART_COUNT -> Fixnum[permalink][rdoc]Maximum number of request parameters when multipart
MAX_MULTIPART_LENGTH -> Fixnum[permalink][rdoc]Maximum content length of multipart data
NEEDS_BINMODE -> bool[permalink][rdoc]ファイルを開くときにバイナリモードが必要かどうかを表す定数です。 プラットフォーム依存の定数です。
PATH_SEPARATOR -> Hash[permalink][rdoc]パスの区切り文字を格納します。