* 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
This commit is contained in:
parent
75cff6289c
commit
9d0758b3b0
10
ChangeLog
10
ChangeLog
@ -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>
|
Wed Apr 20 10:53:30 2005 WATANABE Hirofumi <eban@ruby-lang.org>
|
||||||
|
|
||||||
* lib/rdoc/parsers/parse_rb.rb (lex_init): use IRB module.
|
* lib/rdoc/parsers/parse_rb.rb (lex_init): use IRB module.
|
||||||
|
@ -255,7 +255,7 @@ module Net # :nodoc:
|
|||||||
# print Net::HTTP.get('www.example.com', '/index.html')
|
# print Net::HTTP.get('www.example.com', '/index.html')
|
||||||
#
|
#
|
||||||
def HTTP.get(arg1, arg2 = nil, arg3 = nil)
|
def HTTP.get(arg1, arg2 = nil, arg3 = nil)
|
||||||
get_response(arg1,arg2,arg3).body
|
get_response(arg1, arg2, arg3).body
|
||||||
end
|
end
|
||||||
|
|
||||||
# Send a GET request to the target and return the response
|
# Send a GET request to the target and return the response
|
||||||
@ -294,6 +294,30 @@ module Net # :nodoc:
|
|||||||
end
|
end
|
||||||
private_class_method :get_by_uri
|
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
|
# HTTP session management
|
||||||
#
|
#
|
||||||
@ -353,7 +377,7 @@ module Net # :nodoc:
|
|||||||
@close_on_empty_response = false
|
@close_on_empty_response = false
|
||||||
@socket = nil
|
@socket = nil
|
||||||
@started = false
|
@started = false
|
||||||
@open_timeout = 30
|
@open_timeout = nil
|
||||||
@read_timeout = 60
|
@read_timeout = 60
|
||||||
@debug_output = nil
|
@debug_output = nil
|
||||||
@use_ssl = false
|
@use_ssl = false
|
||||||
@ -1051,15 +1075,15 @@ module Net # :nodoc:
|
|||||||
|
|
||||||
# Adds header name and field instead of replace.
|
# 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"
|
# 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"
|
# 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['X-My-Header'] #=> "a, b, c"
|
||||||
# p request.get_fields('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)
|
if @header.key?(key.downcase)
|
||||||
@header[key.downcase].concat Array(val)
|
@header[key.downcase].concat Array(val)
|
||||||
else
|
else
|
||||||
@ -1276,6 +1300,18 @@ module Net # :nodoc:
|
|||||||
|
|
||||||
alias content_type= set_content_type
|
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.
|
# Set the Authorization: header for "Basic" authorization.
|
||||||
def basic_auth(account, password)
|
def basic_auth(account, password)
|
||||||
@header['authorization'] = [basic_encode(account, password)]
|
@header['authorization'] = [basic_encode(account, password)]
|
||||||
@ -1839,7 +1875,7 @@ module Net # :nodoc:
|
|||||||
httpv, code, msg = read_status_line(sock)
|
httpv, code, msg = read_status_line(sock)
|
||||||
res = response_class(code).new(httpv, code, msg)
|
res = response_class(code).new(httpv, code, msg)
|
||||||
each_response_header(sock) do |k,v|
|
each_response_header(sock) do |k,v|
|
||||||
res.add_header k, v
|
res.add_field k, v
|
||||||
end
|
end
|
||||||
res
|
res
|
||||||
end
|
end
|
||||||
|
@ -7,7 +7,9 @@ class HTTPHeaderTest < Test::Unit::TestCase
|
|||||||
include Net::HTTPHeader
|
include Net::HTTPHeader
|
||||||
def initialize
|
def initialize
|
||||||
@header = {}
|
@header = {}
|
||||||
|
@body = nil
|
||||||
end
|
end
|
||||||
|
attr_accessor :body
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@ -203,6 +205,26 @@ class HTTPHeaderTest < Test::Unit::TestCase
|
|||||||
def test_set_content_type
|
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
|
def test_basic_auth
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user