summaryrefslogtreecommitdiff
path: root/lib/net/http
diff options
context:
space:
mode:
authordrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-12-21 20:36:07 +0000
committerdrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-12-21 20:36:07 +0000
commit570b766901c1a80f5a317012eb81ee9d1f301073 (patch)
tree14d373b6b3a8f52d66970279004a545b174cc01e /lib/net/http
parent34a3668c30753dcd2632aaf84d3ee5d29599eac1 (diff)
* lib/net/http.rb: Requests may be created with a URI which sets the
Host header. Responses contain the requested URI for easier redirect following. [ruby-trunk - Feature #6482] * lib/net/http/generic_request.rb: ditto. * lib/net/http/response.rb: ditto.j * NEWS (net/http): Updated for above. * test/net/http/test_http.rb: Tests for above. * test/net/http/test_http.rb: ditto. * test/net/http/test_httpresponse.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38546 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/net/http')
-rw-r--r--lib/net/http/generic_request.rb35
-rw-r--r--lib/net/http/response.rb9
2 files changed, 43 insertions, 1 deletions
diff --git a/lib/net/http/generic_request.rb b/lib/net/http/generic_request.rb
index bcf87d35be..704f159245 100644
--- a/lib/net/http/generic_request.rb
+++ b/lib/net/http/generic_request.rb
@@ -7,10 +7,22 @@ class Net::HTTPGenericRequest
include Net::HTTPHeader
- def initialize(m, reqbody, resbody, path, initheader = nil)
+ def initialize(m, reqbody, resbody, uri_or_path, initheader = nil)
@method = m
@request_has_body = reqbody
@response_has_body = resbody
+
+ if URI === uri_or_path then
+ @uri = uri_or_path.dup
+ host = @uri.hostname
+ host += ":#{@uri.port}" if @uri.port != @uri.class::DEFAULT_PORT
+ path = uri_or_path.request_uri
+ else
+ @uri = nil
+ host = nil
+ path = uri_or_path
+ end
+
raise ArgumentError, "no HTTP request path given" unless path
raise ArgumentError, "HTTP request path is empty" if path.empty?
@path = path
@@ -29,6 +41,7 @@ class Net::HTTPGenericRequest
initialize_http_header initheader
self['Accept'] ||= '*/*'
self['User-Agent'] ||= 'Ruby'
+ self['Host'] ||= host
@body = nil
@body_stream = nil
@body_data = nil
@@ -36,6 +49,7 @@ class Net::HTTPGenericRequest
attr_reader :method
attr_reader :path
+ attr_reader :uri
def inspect
"\#<#{self.class} #{@method}>"
@@ -82,6 +96,8 @@ class Net::HTTPGenericRequest
#
def exec(sock, ver, path) #:nodoc: internal use only
+ self['host'] = "#{@uri.host}:#{@uri.port}" if @uri
+
if @body
send_request_with_body sock, ver, path, @body
elsif @body_stream
@@ -93,6 +109,23 @@ class Net::HTTPGenericRequest
end
end
+ def update_uri(host, port, ssl) # :nodoc: internal use only
+ return unless @uri
+
+ @uri.host ||= host
+ @uri.port = port
+
+ scheme = ssl ? 'https' : 'http'
+
+ # convert the class of the URI
+ unless scheme == @uri.scheme then
+ new_uri = @uri.to_s.sub(/^https?/, scheme)
+ @uri = URI new_uri
+ end
+
+ @uri
+ end
+
private
class Chunker #:nodoc:
diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb
index 6b044c3d34..bde3b5b440 100644
--- a/lib/net/http/response.rb
+++ b/lib/net/http/response.rb
@@ -79,6 +79,7 @@ class Net::HTTPResponse
initialize_http_header nil
@body = nil
@read = false
+ @uri = nil
end
# The HTTP version supported by the server.
@@ -93,6 +94,10 @@ class Net::HTTPResponse
attr_reader :message
alias msg message # :nodoc: obsolete
+ # The URI used to fetch this response. The response URI is only available
+ # if a URI was used to create the request.
+ attr_reader :uri
+
def inspect
"#<#{self.class} #{@code} #{@message} readbody=#{@read}>"
end
@@ -118,6 +123,10 @@ class Net::HTTPResponse
error! unless self.kind_of?(Net::HTTPSuccess)
end
+ def uri= uri # :nodoc:
+ @uri = uri.dup if uri
+ end
+
#
# header (for backward compatibility only; DO NOT USE)
#