* 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
This commit is contained in:
aamine 2006-07-26 13:15:31 +00:00
parent 6b80faa6fb
commit 8e1f07d6b3
3 changed files with 96 additions and 93 deletions

View file

@ -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|