要約
HTTP ヘッダのためのモジュールです。
このモジュールを mix-in に @header という(ハッシュを代入してある) 変数への「大文字小文字を無視した」ハッシュ的アクセスメソッドを提供します。またよくある HTTP ヘッダへの便利なアクセスメソッドも用意します。
目次
- インスタンスメソッド
-
- []
- []=
- add_field
- basic_auth
- canonical_each
- chunked?
- content_length
- content_length=
- content_range
- content_type
- content_type=
- delete
- each
- each_capitalized
- each_capitalized_name
- each_header
- each_key
- each_name
- each_value
- fetch
- form_data=
- get_fields
- key?
- length
- main_type
- method
- proxy_basic_auth
- range
- range=
- range_length
- set_content_type
- set_form_data
- set_range
- size
- sub_type
- type_params
インスタンスメソッド
self[key] -> String|nil[permalink][rdoc][edit]-
key ヘッダフィールドを返します。
たとえばキー 'content-length' に対しては '2048' のような文字列が得られます。キーが存在しなければ nil を返します。
例require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) p req['user-agent'] # => Ruby一種類のヘッダフィールドが一つのヘッダの中に複数存在する場合にはそれを全て ", " で連結した文字列を返します。 key は大文字小文字を区別しません。
- [PARAM]
key: - ヘッダフィールド名を文字列で与えます。
[SEE_ALSO] Net::HTTPHeader#[]=, Net::HTTPHeader#add_field, Net::HTTPHeader#get_fields
- [PARAM]
self[key] = val[permalink][rdoc][edit]-
key ヘッダフィールドに文字列 val をセットします。
key に元々設定されていた値は破棄されます。 key は大文字小文字を区別しません。 val に nil を与えるとそのフィールドを削除します。
- [PARAM]
key: - ヘッダフィールド名を文字列で与えます。
- [PARAM]
val: - keyで指定したフィールドにセットする文字列を与えます。
require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) p req['user-agent'] # => Ruby req['user-agent'] = "update" p req['user-agent'] # => update[SEE_ALSO] Net::HTTPHeader#[], Net::HTTPHeader#add_field, Net::HTTPHeader#get_fields
- [PARAM]
add_field(key, val) -> ()[permalink][rdoc][edit]-
key ヘッダフィールドに val を追加します。
key に元々設定されていた値は破棄されず、それに val 追加されます。
- [PARAM]
key: - ヘッダフィールド名を文字列で与えます。
- [PARAM]
val: - keyで指定したフィールドに追加する文字列を与えます。
[SEE_ALSO] Net::HTTPHeader#[], Net::HTTPHeader#[]=, Net::HTTPHeader#get_fields
例request.add_field 'X-My-Header', 'a' p request['X-My-Header'] #=> "a" p request.get_fields('X-My-Header') #=> ["a"] request.add_field 'X-My-Header', 'b' p request['X-My-Header'] #=> "a, b" p request.get_fields('X-My-Header') #=> ["a", "b"] request.add_field 'X-My-Header', 'c' p request['X-My-Header'] #=> "a, b, c" p request.get_fields('X-My-Header') #=> ["a", "b", "c"] - [PARAM]
basic_auth(account, password) -> [String][permalink][rdoc][edit]-
Authorization: ヘッダを BASIC 認証用にセットします。
- [PARAM]
account: - アカウント名を文字列で与えます。
- [PARAM]
password: - パスワードを文字列で与えます。
require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) p req.basic_auth("user", "pass") # => ["Basic dXNlcjpwYXNz"] - [PARAM]
each_capitalized {|name, value| .... } -> ()[permalink][rdoc][edit]canonical_each {|name, value| .... } -> ()-
ヘッダフィールドの正規化名とその値のペアをブロックに渡し、呼びだします。
正規化名は name に対し
name.downcase.split(/-/).capitalize.join('-')で求まる文字列です。
chunked? -> bool[permalink][rdoc][edit]-
Transfer-Encoding: ヘッダフィールドが "chunked" である場合に真を返します。
Transfer-Encoding: ヘッダフィールドが存在しなかったり、 "chunked" 以外である場合には偽を返します。
例require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) p req.chunked? # => false req["Transfer-Encoding"] = "chunked" p req.chunked? # => true content_length -> Integer|nil[permalink][rdoc][edit]-
Content-Length: ヘッダフィールドの表している値を整数で返します。
ヘッダが設定されていない場合には nil を返します。
- [EXCEPTION]
Net::HTTPHeaderSyntaxError: - フィールドの値が不正である場合に発生します。
require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) p req.content_length # => nil req.content_length = 10 p req.content_length # => 10 - [EXCEPTION]
content_length=(len)[permalink][rdoc][edit]-
Content-Length: ヘッダフィールドに値を設定します。
len に nil を与えると Content-Length: ヘッダフィールドを削除します。
- [PARAM]
len: - 設定する値を整数で与えます。
require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) p req.content_length # => nil req.content_length = 10 # => 10 p req.content_length # => 10 - [PARAM]
content_range -> Range|nil[permalink][rdoc][edit]-
Content-Range: ヘッダフィールドの値を Range で返します。 Range の表わす長さは Net::HTTPHeader#range_length で得られます。
ヘッダが設定されていない場合には nil を返します。
例require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) p req.content_range # => nil req['Content-Range'] = "bytes 0-499/1234" p req.content_range # => 0..499 content_type -> String|nil[permalink][rdoc][edit]-
"text/html" のような Content-Type を表す文字列を返します。
Content-Type: ヘッダフィールドが存在しない場合には nil を返します。
例require 'net/http' uri = URI.parse('http://www.example.com/comments.cgi?post=comment') req = Net::HTTP::Post.new(uri.request_uri) p req.content_type # => nil req.content_type = 'multipart/form-data' p req.content_type # => "multipart/form-data" content_type=(type)[permalink][rdoc][edit]set_content_type(type, params = {})-
type と params から Content-Type: ヘッダフィールドの値を設定します。
- [PARAM]
type: - メディアタイプを文字列で指定します。
- [PARAM]
params: - パラメータ属性をハッシュで指定します。
require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) p req.content_type # => nil req.content_type = 'multipart/form-data' # => "multipart/form-data" p req.content_type # => "multipart/form-data" - [PARAM]
delete(key) -> [String] | nil[permalink][rdoc][edit]-
key ヘッダフィールドを削除します。
- [PARAM]
key: - 削除するフィールド名
- [RETURN]
- 取り除かれたフィールドの値を返します。 key ヘッダフィールドが存在しなかった場合には nil を返します。
require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) req.content_length = 10 p req.content_length # => 10 p req.delete("Content-Length") # => ["10"] p req.content_length # => nil - [PARAM]
each {|name, val| .... } -> ()[permalink][rdoc][edit]each_header {|name, val| .... } -> ()-
保持しているヘッダ名とその値をそれぞれブロックに渡して呼びだします。
ヘッダ名は小文字で統一されます。 val は ", " で連結した文字列がブロックに渡されます。
例require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) req.each_header { |key,value| puts "#{key} = #{value}" } # => accept-encoding = gzip;q=1.0,deflate;q=0.6,identity;q=0.3 # => accept = */* # => user-agent = Ruby each_capitalized_name {|name| .... } -> ()[permalink][rdoc][edit]-
保持しているヘッダ名を正規化 ('x-my-header' -> 'X-My-Header') して、ブロックに渡します。
例require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) req.each_capitalized_name { |key| puts key } # => Accept-Encoding # => Accept # => User-Agent each_name {|name| ... } -> ()[permalink][rdoc][edit]each_key {|name| ... } -> ()-
保持しているヘッダ名をブロックに渡して呼びだします。
ヘッダ名は小文字で統一されます。
例require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) req.each_name { |name| puts name } # => accept-encoding # => accept # => user-agent each_value {|value| .... } -> ()[permalink][rdoc][edit]-
保持しているヘッダの値をブロックに渡し、呼びだします。
渡される文字列は ", " で連結したものです。
例require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) req.each_value { |value| puts value } # => gzip;q=1.0,deflate;q=0.6,identity;q=0.3 # => */* # => Ruby fetch(key) -> String[permalink][rdoc][edit]fetch(key, default) -> Stringfetch(key) {|hash| .... } -> String-
key ヘッダフィールドを返します。
たとえばキー 'content-length' に対しては '2048' のような文字列が得られます。キーが存在しなければ nil を返します。
該当するキーが登録されていない時には、引数 default が与えられていればその値を、ブロックが与えられていればそのブロックを評価した値を返します。
一種類のヘッダフィールドが一つのヘッダの中に複数存在する場合にはそれを全て ", " で連結した文字列を返します。 key は大文字小文字を区別しません。
- [PARAM]
key: - ヘッダフィールド名を文字列で与えます。
- [PARAM]
default: - 該当するキーが登録されていない時の返り値を指定します。
- [EXCEPTION]
IndexError: - 引数defaultもブロックも与えられてない時、キーの探索に 失敗すると発生します。
例 key のみ指定。key が存在しないrequire 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) p req.fetch("user-agent") # => "Ruby"
例 key , default を指定require 'net/http' begin req.fetch("content-length") rescue => e e # => #<KeyError: key not found: "content-length"> end
例 key とブロックを指定require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) p req.fetch("content-length", "default") # => "default"require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) p req.fetch("content-length") { |e| 99 } # => 99[SEE_ALSO] Net::HTTPHeader#[]
- [PARAM]
form_data=(params)[permalink][rdoc][edit]set_form_data(params, sep = '&') -> ()-
HTMLのフォームのデータ params からヘッダフィールドとボディを設定します。
ヘッダフィールド Content-Type: には 'application/x-www-form-urlencoded' が設定されます。
- [PARAM]
params: - HTML のフォームデータの Hash を与えます。
- [PARAM]
sep: - データのセパレータを文字列で与えます。
例 set_form_datarequire 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) req.form_data = {"q" => ["ruby", "perl"], "lang" => "en"} # => {"q"=>["ruby", "perl"], "lang"=>"en"}require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) p req.set_form_data({"q" => "ruby", "lang" => "en"}, ';') # => "application/x-www-form-urlencoded" - [PARAM]
get_fields(key) -> [String][permalink][rdoc][edit]-
key ヘッダフィールドの値 (文字列) を配列で返します。
たとえばキー 'content-length' に対しては ['2048'] のような文字列が得られます。一種類のヘッダフィールドが一つのヘッダの中に複数存在することがありえます。 key は大文字小文字を区別しません。
- [PARAM]
key: - ヘッダフィールド名を文字列で与えます。
require 'net/http' uri = URI.parse('http://www.example.com/index.html') res = Net::HTTP.get_response(uri) p res.get_fields('accept-ranges') # => ["none"][SEE_ALSO] Net::HTTPHeader#[], Net::HTTPHeader#[]=, Net::HTTPHeader#add_field
- [PARAM]
key?(key) -> bool[permalink][rdoc][edit]-
key というヘッダフィールドがあれば真を返します。 key は大文字小文字を区別しません。
- [PARAM]
key: - 探すヘッダフィールド名を文字列で与えます。
require 'net/http' uri = URI.parse('http://www.example.com/index.html') res = Net::HTTP.get_response(uri) p res.key?('content-type') # => true p res.key?('nonexist-header') # => false - [PARAM]
size -> Integer[permalink][rdoc][edit]length -> Integer-
このメソッドは obsolete です。
ヘッダフィールドの数を返します。
main_type -> String|nil[permalink][rdoc][edit]-
"text/html" における "text" のようなタイプを表す文字列を返します。
Content-Type: ヘッダフィールドが存在しない場合には nil を返します。
例require 'net/http' uri = URI.parse('http://www.example.com/index.html') res = Net::HTTP.get_response(uri) p res.main_type # => "text" method -> String[permalink][rdoc][edit]-
リクエストの HTTP メソッドを文字列で返します。
例require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) p req.method # => "GET" proxy_basic_auth(account, password) -> [String][permalink][rdoc][edit]-
Proxy 認証のために Proxy-Authorization: ヘッダをセットします。
- [PARAM]
account: - アカウント名を文字列で与えます。
- [PARAM]
password: - パスワードを文字列で与えます。
require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) p req.proxy_basic_auth("account", "password") # => ["Basic YWNjb3VudDpwYXNzd29yZA=="] - [PARAM]
range -> Range|nil[permalink][rdoc][edit]-
Range: ヘッダの示す範囲を Range オブジェクトで返します。
ヘッダにない場合は nil を返します。
- [EXCEPTION]
Net::HTTPHeaderSyntaxError: - Range:ヘッダの中身が規格通りでない場合に発生します。
例 Net::HTTPHeaderSyntaxErrorrequire 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) req['range'] = "bytes=1-5" p req.range # => [1..5]require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) req['range'] = "invalid" req.range # ~> Net::HTTPHeaderSyntaxError - [EXCEPTION]
range=(r)[permalink][rdoc][edit]range=(n)set_range(i, len) -> ()set_range(r) -> ()set_range(n) -> ()-
範囲を指定してエンティティを取得するためのヘッダ Range: をセットします。
以下は同じことを表しています。
例req.range = 0..1023 req.range = 0...1024 req.range = 1024 req.set_range(0, 1024) req.set_range(0..1023) req.set_range(0...1024) req.set_range(1024)特別な場合として、 n に負数を与えた場合にnは最初から(-n)バイトまでの範囲を表します。 r を x..-1 とした場合には、x が正ならば x バイト目から最後までの範囲を、 x が負ならば最初から x バイト目までの範囲を表します。
- [PARAM]
r: - 範囲を Range オブジェクトで与えます。
- [PARAM]
i: - 範囲の始点を整数で与えます。
- [PARAM]
len: - 範囲の長さを整数で与えます。
- [PARAM]
n: - 0からの長さを整数で与えます。
- [PARAM]
range_length -> Integer|nil[permalink][rdoc][edit]-
Content-Range: ヘッダフィールドの表している長さを整数で返します。
ヘッダが設定されていない場合には nil を返します。
- [EXCEPTION]
Net::HTTPHeaderSyntaxError: - Content-Range: ヘッダフィールドの値が不正である場合に発生します。
require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) req['Content-Range'] = "bytes 1-500/1000" p req.range_length # => 5001 以外から始まる範囲の場合は、末尾の値との差分に 1 を足した値になります。
例require 'net/http' uri = URI.parse('http://www.example.com/index.html') req = Net::HTTP::Get.new(uri.request_uri) req['Content-Range'] = "bytes 200-699/1000" p req.range_length # => 500 - [EXCEPTION]
sub_type -> String|nil[permalink][rdoc][edit]-
"text/html" における "html" のようなサブタイプを表す文字列を返します。
Content-Type: ヘッダフィールドが存在しない場合には nil を返します。
例require 'net/http' uri = URI.parse('http://www.example.com/index.html') res = Net::HTTP.get_response(uri) p res.sub_type # => "html" type_params -> Hash[permalink][rdoc][edit]-
Content-Type のパラメータを {"charset" => "iso-2022-jp"} という形の Hash で返します。
Content-Type: ヘッダフィールドが存在しない場合には空のハッシュを返します。
例require 'net/http' uri = URI.parse('http://www.example.com/index.html') res = Net::HTTP.get_response(uri) p res.type_params # => {"charset"=>"UTF-8"}