summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-07-26 13:15:31 +0000
committeraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-07-26 13:15:31 +0000
commit8e1f07d6b37c034a85bc1a36a9d109076716587c (patch)
treec0c1cd85173caa9f4e44f81e81c4d880c9f50028
parent6b80faa6fb245c3fa8639584c3a5981ac7e31ad6 (diff)
* lib/net/http.rb: sync with HEAD (rev 1.132).
* lib/net/http.rb (Net::HTTP#post, request_post, request): should set Content-Type: x-www-form-urlencoded by default. * lib/net/http.rb (Net::HTTPHeader#content_type): should return nil when there's no Content-Type. * lib/net/http.rb (Net::HTTPHeader#sub_type): should return nil when there's no sub Content-Type (e.g. "Content-Type: text"). * lib/net/http.rb (Net::HTTPHeader#type_params): wrongly failed when there's no Content-Type. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@10612 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog16
-rw-r--r--lib/net/http.rb57
-rw-r--r--test/net/http/test_httpheader.rb116
3 files changed, 96 insertions, 93 deletions
diff --git a/ChangeLog b/ChangeLog
index 66211bdaed..327060aa59 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+Wed Jul 26 22:13:45 2006 Minero Aoki <aamine@loveruby.net>
+
+ * lib/net/http.rb: sync with HEAD (rev 1.132).
+
+ * lib/net/http.rb (Net::HTTP#post, request_post, request): should
+ set Content-Type: x-www-form-urlencoded by default.
+
+ * lib/net/http.rb (Net::HTTPHeader#content_type): should return
+ nil when there's no Content-Type.
+
+ * lib/net/http.rb (Net::HTTPHeader#sub_type): should return nil
+ when there's no sub Content-Type (e.g. "Content-Type: text").
+
+ * lib/net/http.rb (Net::HTTPHeader#type_params): wrongly failed
+ when there's no Content-Type.
+
Wed Jul 26 18:35:38 2006 Minero Aoki <aamine@loveruby.net>
* ext/strscan/strscan.c: sync with HEAD (rev 1.25).
diff --git a/lib/net/http.rb b/lib/net/http.rb
index 43ef6e1ad3..968ea497b5 100644
--- a/lib/net/http.rb
+++ b/lib/net/http.rb
@@ -833,6 +833,10 @@ module Net #:nodoc:
# end
# }
#
+ # You should set Content-Type: header field for POST.
+ # If no Content-Type: field given, this method uses
+ # "application/x-www-form-urlencoded" by default.
+ #
def post(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
res = nil
request(Post.new(path, initheader), data) {|r|
@@ -843,7 +847,6 @@ module Net #:nodoc:
res.value
return res, res.body
end
-
res
end
@@ -1176,7 +1179,7 @@ module Net #:nodoc:
#
def add_field(key, val)
if @header.key?(key.downcase)
- @header[key.downcase].concat [val]
+ @header[key.downcase].push val
else
@header[key.downcase] = [val]
end
@@ -1363,35 +1366,60 @@ module Net #:nodoc:
r.end - r.begin
end
+ # Returns a content type string such as "text/html".
+ # This method returns nil if Content-Type: header field does not exist.
def content_type
- "#{main_type()}/#{sub_type()}"
+ return nil unless main_type()
+ if sub_type()
+ then "#{main_type()}/#{sub_type()}"
+ else main_type()
+ end
end
+ # Returns a content type string such as "text".
+ # This method returns nil if Content-Type: header field does not exist.
def main_type
return nil unless @header['content-type']
self['Content-Type'].split(';').first.to_s.split('/')[0].to_s.strip
end
+ # Returns a content type string such as "html".
+ # This method returns nil if Content-Type: header field does not exist
+ # or sub-type is not given (e.g. "Content-Type: text").
def sub_type
return nil unless @header['content-type']
- self['Content-Type'].split(';').first.to_s.split('/')[1].to_s.strip
+ main, sub = *self['Content-Type'].split(';').first.to_s.split('/')
+ return nil unless sub
+ sub.strip
end
+ # Returns content type parameters as a Hash as like
+ # {"charset" => "iso-2022-jp"}.
def type_params
result = {}
- self['Content-Type'].to_s.split(';')[1..-1].each do |param|
+ list = self['Content-Type'].to_s.split(';')
+ list.shift
+ list.each do |param|
k, v = *param.split('=', 2)
result[k.strip] = v.strip
end
result
end
+ # Set Content-Type: header field by +type+ and +params+.
+ # +type+ must be a String, +params+ must be a Hash.
def set_content_type(type, params = {})
@header['content-type'] = [type + params.map{|k,v|"; #{k}=#{v}"}.join('')]
end
alias content_type= set_content_type
+ # Set header fields and a body from HTML form data.
+ # +params+ should be a Hash containing HTML form data.
+ # Optional argument +sep+ means data record separator.
+ #
+ # This method also set Content-Type: header field to
+ # application/x-www-form-urlencoded.
def set_form_data(params, sep = '&')
self.body = params.map {|k,v| "#{urlencode(k.to_s)}=#{urlencode(v.to_s)}" }.join(sep)
self.content_type = 'application/x-www-form-urlencoded'
@@ -1504,20 +1532,17 @@ module Net #:nodoc:
def send_request_with_body(sock, ver, path, body)
self.content_length = body.length
delete 'Transfer-Encoding'
- unless content_type()
- warn 'net/http: warning: Content-Type did not set; using application/x-www-form-urlencoded' if $VERBOSE
- set_content_type 'application/x-www-form-urlencoded'
- end
+ supply_default_content_type
write_header sock, ver, path
sock.write body
end
def send_request_with_body_stream(sock, ver, path, f)
- raise ArgumentError, "Content-Length not given and Transfer-Encoding is not `chunked'" unless content_length() or chunked?
- unless content_type()
- warn 'net/http: warning: Content-Type did not set; using application/x-www-form-urlencoded' if $VERBOSE
- set_content_type 'application/x-www-form-urlencoded'
+ unless content_length() or chunked?
+ raise ArgumentError,
+ "Content-Length not given and Transfer-Encoding is not `chunked'"
end
+ supply_default_content_type
write_header sock, ver, path
if chunked?
while s = f.read(1024)
@@ -1531,6 +1556,12 @@ module Net #:nodoc:
end
end
+ def supply_default_content_type
+ return if content_type()
+ warn 'net/http: warning: Content-Type did not set; using application/x-www-form-urlencoded' if $VERBOSE
+ set_content_type 'application/x-www-form-urlencoded'
+ end
+
def write_header(sock, ver, path)
buf = "#{@method} #{path} HTTP/#{ver}\r\n"
each_capitalized do |k,v|
diff --git a/test/net/http/test_httpheader.rb b/test/net/http/test_httpheader.rb
index 2c64e31f10..ca2bf9f46e 100644
--- a/test/net/http/test_httpheader.rb
+++ b/test/net/http/test_httpheader.rb
@@ -6,8 +6,10 @@ class HTTPHeaderTest < Test::Unit::TestCase
class C
include Net::HTTPHeader
def initialize
- initialize_http_header({})
+ @header = {}
+ @body = nil
end
+ attr_accessor :body
end
def setup
@@ -49,91 +51,25 @@ class HTTPHeaderTest < Test::Unit::TestCase
@c['Next-Header'] = 'next string'
assert_equal 'next string', @c['next-header']
end
-
+
def test_add_field
- @c.add_field 'My-Header', 'a'
- assert_equal 'a', @c['My-Header']
- assert_equal ['a'], @c.get_fields('My-Header')
- @c.add_field 'My-Header', 'b'
- assert_equal 'a, b', @c['My-Header']
- assert_equal ['a', 'b'], @c.get_fields('My-Header')
- @c.add_field 'My-Header', 'c'
- assert_equal 'a, b, c', @c['My-Header']
- assert_equal ['a', 'b', 'c'], @c.get_fields('My-Header')
- @c.add_field 'My-Header', 'd, d'
- assert_equal 'a, b, c, d, d', @c['My-Header']
- assert_equal ['a', 'b', 'c', 'd, d'], @c.get_fields('My-Header')
end
def test_get_fields
- @c['My-Header'] = 'test string'
- assert_equal ['test string'], @c.get_fields('my-header')
- assert_equal ['test string'], @c.get_fields('My-header')
- assert_equal ['test string'], @c.get_fields('my-Header')
-
- assert_nil @c.get_fields('not-found')
- assert_nil @c.get_fields('Not-Found')
-
- @c.get_fields('my-header').push 'junk'
- assert_equal ['test string'], @c.get_fields('my-header')
- @c.get_fields('my-header').clear
- assert_equal ['test string'], @c.get_fields('my-header')
end
def test_delete
- @c['My-Header'] = 'test'
- assert_equal 'test', @c['My-Header']
- assert_nil @c['not-found']
- @c.delete 'My-Header'
- assert_nil @c['My-Header']
- assert_nil @c['not-found']
- @c.delete 'My-Header'
- @c.delete 'My-Header'
- assert_nil @c['My-Header']
- assert_nil @c['not-found']
end
def test_each
- @c['My-Header'] = 'test'
- @c.each do |k, v|
- assert_equal 'my-header', k
- assert_equal 'test', v
- end
- @c.each do |k, v|
- assert_equal 'my-header', k
- assert_equal 'test', v
- end
end
def test_each_key
- @c['My-Header'] = 'test'
- @c.each_key do |k|
- assert_equal 'my-header', k
- end
- @c.each_key do |k|
- assert_equal 'my-header', k
- end
end
def test_each_value
- @c['My-Header'] = 'test'
- @c.each_value do |v|
- assert_equal 'test', v
- end
- @c.each_value do |v|
- assert_equal 'test', v
- end
end
- def test_canonical_each
- @c['my-header'] = ['a', 'b']
- @c.canonical_each do |k,v|
- assert_equal 'My-Header', k
- assert_equal 'a, b', v
- end
- end
-
-=begin
def test_each_capitalized
@c['my-header'] = ['a', 'b']
@c.each_capitalized do |k,v|
@@ -141,16 +77,8 @@ class HTTPHeaderTest < Test::Unit::TestCase
assert_equal 'a, b', v
end
end
-=end
def test_key?
- @c['My-Header'] = 'test'
- assert_equal true, @c.key?('My-Header')
- assert_equal true, @c.key?('my-header')
- assert_equal false, @c.key?('Not-Found')
- assert_equal false, @c.key?('not-found')
- assert_equal false, @c.key?('')
- assert_equal false, @c.key?('x' * 1024)
end
def test_to_hash
@@ -227,7 +155,6 @@ class HTTPHeaderTest < Test::Unit::TestCase
assert_equal len, @c.content_length
end
-=begin
def test_content_length=
@c.content_length = 0
assert_equal 0, @c.content_length
@@ -238,48 +165,77 @@ class HTTPHeaderTest < Test::Unit::TestCase
@c.content_length = 10000000000000
assert_equal 10000000000000, @c.content_length
end
-=end
-=begin
def test_content_type
+ assert_nil @c.content_type
@c.content_type = 'text/html'
assert_equal 'text/html', @c.content_type
@c.content_type = 'application/pdf'
assert_equal 'application/pdf', @c.content_type
@c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'}
assert_equal 'text/html', @c.content_type
+ @c.content_type = 'text'
+ assert_equal 'text', @c.content_type
end
def test_main_type
+ assert_nil @c.main_type
@c.content_type = 'text/html'
assert_equal 'text', @c.main_type
@c.content_type = 'application/pdf'
assert_equal 'application', @c.main_type
@c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'}
assert_equal 'text', @c.main_type
+ @c.content_type = 'text'
+ assert_equal 'text', @c.main_type
end
def test_sub_type
+ assert_nil @c.sub_type
@c.content_type = 'text/html'
assert_equal 'html', @c.sub_type
@c.content_type = 'application/pdf'
assert_equal 'pdf', @c.sub_type
@c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'}
assert_equal 'html', @c.sub_type
+ @c.content_type = 'text'
+ assert_nil @c.sub_type
end
def test_type_params
+ assert_equal({}, @c.type_params)
@c.content_type = 'text/html'
assert_equal({}, @c.type_params)
@c.content_type = 'application/pdf'
assert_equal({}, @c.type_params)
@c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'}
assert_equal({'charset' => 'iso-2022-jp'}, @c.type_params)
+ @c.content_type = 'text'
+ assert_equal({}, @c.type_params)
end
def test_set_content_type
end
-=end
+
+ def test_form_data=
+ @c.form_data = {"cmd"=>"search", "q"=>"ruby", "max"=>"50"}
+ assert_equal 'application/x-www-form-urlencoded', @c.content_type
+ assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split('&').sort
+ end
+
+ def test_set_form_data
+ @c.set_form_data "cmd"=>"search", "q"=>"ruby", "max"=>"50"
+ assert_equal 'application/x-www-form-urlencoded', @c.content_type
+ assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split('&').sort
+
+ @c.set_form_data "cmd"=>"search", "q"=>"ruby", "max"=>50
+ assert_equal 'application/x-www-form-urlencoded', @c.content_type
+ assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split('&').sort
+
+ @c.set_form_data({"cmd"=>"search", "q"=>"ruby", "max"=>"50"}, ';')
+ assert_equal 'application/x-www-form-urlencoded', @c.content_type
+ assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split(';').sort
+ end
def test_basic_auth
end