Skip to content

Commit

Permalink
Add Net::HTTP.get(full_url) and Net::HTTP.get_response(full_url)
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Mar 14, 2011
1 parent b7e8e16 commit bcc5428
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
19 changes: 16 additions & 3 deletions README.markdown
@@ -1,7 +1,20 @@
This repo contains a number of experimental modifications to the version of Net::HTTP that ships with Ruby 1.9. TODOs:

* <del>Porting the code to work on Ruby 1.8</del>
* Add missing tests for features, including gzip
* Fixing gzip when using requests that a block as an iterator
* Adding support for getting the response as a stream, rather than having to read the entire body at once.
* <del>Fix gzip in GET requests when using a block as iterator</del>
* <del>Add support for gzip and deflate responses from any request</del>
* <del>Add support for incremental gzip and deflate with chunked
encoding</del>
* <del>Add support for leaving the socket open when making a request, so
it's possible to make requests that do not block on the body being
returned</del>
* <del>Clean up tests so that it's possible to combine tests for
features, instead of using brittle checks for specific classes</del>
* <del>Add support for Net::HTTP.get(path), instead of needing to pass a
URI or deconstruct the URL yourself</del>
* In keepalive situations, make sure to read any remaining body from the
socket before initiating a new connection.
* Add support for partial reads from the response
* Document and clean up the semantics of when #body can be called after
a request
* Other features as I think of them
20 changes: 17 additions & 3 deletions lib/net2/http.rb
Expand Up @@ -28,6 +28,17 @@
require "net2/backports"
end

module URI
class Generic
unless URI::Generic.allocate.respond_to?(:hostname)
def hostname
v = self.host
/\A\[(.*)\]\z/ =~ v ? $1 : v
end
end
end
end

module Net2 #:nodoc:

# :stopdoc:
Expand Down Expand Up @@ -452,12 +463,15 @@ def self.get(uri_or_host, path = nil, port = nil)
# print res.body
#
def self.get_response(uri_or_host, path = nil, port = nil, &block)
if path
host = uri_or_host
else
if uri_or_host.respond_to?(:hostname)
host = uri_or_host.hostname
port = uri_or_host.port
path = uri_or_host.request_uri
elsif path
host = uri_or_host
else
uri = URI.parse(uri_or_host)
return get_response(uri, &block)
end

http = new(host, port || HTTP.default_port).start
Expand Down
6 changes: 6 additions & 0 deletions test/http_test_base.rb
Expand Up @@ -3,6 +3,12 @@ module TestNetHTTP_version_1_1_methods
def test_s_get
assert_equal $test_net_http_data,
Net::HTTP.get(config('host'), '/', config('port'))

assert_equal $test_net_http_data,
Net::HTTP.get("http://#{config('host')}:#{config('port')}/")

assert_equal $test_net_http_data,
Net::HTTP.get(URI.parse("http://#{config('host')}:#{config('port')}/"))
end

def test_head
Expand Down

0 comments on commit bcc5428

Please sign in to comment.