aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authoraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-07-26 13:00:26 +0000
committeraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-07-26 13:00:26 +0000
commite1173b2c78ad8537da935606d61ecbc775c152b1 (patch)
treea285ff9dcbb07e75b7ad819de842850d122a2894 /lib
parentc9500ee8d94e0ff1ac600b14998ba23bc7e743ef (diff)
downloadruby-e1173b2c78ad8537da935606d61ecbc775c152b1.tar.gz
* 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/trunk@10611 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/net/http.rb55
1 files changed, 43 insertions, 12 deletions
diff --git a/lib/net/http.rb b/lib/net/http.rb
index f6e8e9dce3..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
@@ -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 main_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|