diff options
author | aamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-04-20 13:55:26 +0000 |
---|---|---|
committer | aamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-04-20 13:55:26 +0000 |
commit | 9d0758b3b0f85a0a3ca26e1e83b4a6f04c460873 (patch) | |
tree | 2edb32aa0e4707af11c9a3215f08de4fd83f6a6e | |
parent | 75cff6289c53155bfa73e7cc98bad0b12fab317f (diff) | |
download | ruby-9d0758b3b0f85a0a3ca26e1e83b4a6f04c460873.tar.gz |
* lib/net/http.rb: new method Net::HTTP.post_form.
* lib/net/http.rb: new method Net::HTTPHeader#set_form_data and its alias #form_data=.
* lib/net/http.rb: Net::HTTPHeader#add_header -> add_field (adjustted to Ruby 1.8).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8362 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 10 | ||||
-rw-r--r-- | lib/net/http.rb | 50 | ||||
-rw-r--r-- | test/net/http/test_httpheader.rb | 22 |
3 files changed, 75 insertions, 7 deletions
@@ -1,3 +1,13 @@ +Wed Apr 20 22:54:54 2005 Minero Aoki <aamine@loveruby.net> + + * lib/net/http.rb: new method Net::HTTP.post_form. + + * lib/net/http.rb: new method Net::HTTPHeader#set_form_data and + its alias #form_data=. + + * lib/net/http.rb: Net::HTTPHeader#add_header -> add_field + (adjustted to Ruby 1.8). + Wed Apr 20 10:53:30 2005 WATANABE Hirofumi <eban@ruby-lang.org> * lib/rdoc/parsers/parse_rb.rb (lex_init): use IRB module. diff --git a/lib/net/http.rb b/lib/net/http.rb index 5a6858a8a4..dd4c6b3140 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -255,7 +255,7 @@ module Net # :nodoc: # print Net::HTTP.get('www.example.com', '/index.html') # def HTTP.get(arg1, arg2 = nil, arg3 = nil) - get_response(arg1,arg2,arg3).body + get_response(arg1, arg2, arg3).body end # Send a GET request to the target and return the response @@ -294,6 +294,30 @@ module Net # :nodoc: end private_class_method :get_by_uri + # Posts HTML form data to the +URL+. + # Form data must be represented as a Hash of String to String, e.g: + # + # { "cmd" => "search", "q" => "ruby", "max" => "50" } + # + # This method also does Basic Authentication iff +URL+.user exists. + # + # Example: + # + # require 'net/http' + # require 'uri' + # + # HTTP.post_form URI.parse('http://www.example.com/search.cgi'), + # { "q" => "ruby", "max" => "50" } + # + def HTTP.post_form(url, params) + req = Post.new(url.path) + req.form_data = params + req.basic_auth url.user, url.password if url.user + new(url.host, url.port).start {|http| + http.request(req) + } + end + # # HTTP session management # @@ -353,7 +377,7 @@ module Net # :nodoc: @close_on_empty_response = false @socket = nil @started = false - @open_timeout = 30 + @open_timeout = nil @read_timeout = 60 @debug_output = nil @use_ssl = false @@ -1051,15 +1075,15 @@ module Net # :nodoc: # Adds header name and field instead of replace. # - # request.add_header 'X-My-Header', 'a' + # request.add_field 'X-My-Header', 'a' # p request['X-My-Header'] #=> "a" - # request.add_header 'X-My-Header', 'b' + # request.add_field 'X-My-Header', 'b' # p request['X-My-Header'] #=> "a, b" - # request.add_header 'X-My-Header', 'c' + # 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"] # - def add_header(key, val) + def add_field(key, val) if @header.key?(key.downcase) @header[key.downcase].concat Array(val) else @@ -1276,6 +1300,18 @@ module Net # :nodoc: alias content_type= set_content_type + 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' + end + + alias form_data= set_form_data + + def urlencode(str) + str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) } + end + private :urlencode + # Set the Authorization: header for "Basic" authorization. def basic_auth(account, password) @header['authorization'] = [basic_encode(account, password)] @@ -1839,7 +1875,7 @@ module Net # :nodoc: httpv, code, msg = read_status_line(sock) res = response_class(code).new(httpv, code, msg) each_response_header(sock) do |k,v| - res.add_header k, v + res.add_field k, v end res end diff --git a/test/net/http/test_httpheader.rb b/test/net/http/test_httpheader.rb index 1a8751e28c..3d5fa141ef 100644 --- a/test/net/http/test_httpheader.rb +++ b/test/net/http/test_httpheader.rb @@ -7,7 +7,9 @@ class HTTPHeaderTest < Test::Unit::TestCase include Net::HTTPHeader def initialize @header = {} + @body = nil end + attr_accessor :body end def setup @@ -203,6 +205,26 @@ class HTTPHeaderTest < Test::Unit::TestCase def test_set_content_type 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 |