Permalink
Browse files

* version: 2.1.2 -> 2.1.3-SNAPSHOT

 * exception handling cleanups.  see #187.
   * raises HTTPClient::ConfigurationError for environment problem (trying to do SSL without openssl installed for example)
   * raises HTTPClient::BadResponse for HTTP response problem.
  • Loading branch information...
1 parent 4fc7a2b commit e3ae9e2150f5ba99affe9ae1587d699de91fa6f3 nahi committed Dec 12, 2008
Showing with 79 additions and 50 deletions.
  1. +36 −0 Rakefile
  2. +25 −11 lib/httpclient.rb
  3. +4 −19 lib/httpclient/auth.rb
  4. +5 −11 lib/httpclient/session.rb
  5. +4 −4 test/test_http-access2.rb
  6. +5 −5 test/test_httpclient.rb
View
@@ -0,0 +1,36 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+task :default => :gem
+
+begin
+ require 'rake/gempackagetask'
+ load 'httpclient.gemspec'
+ Rake::GemPackageTask.new(SPEC) do |pkg|
+ pkg.need_zip = true
+ pkg.need_tar = true
+ end
+rescue LoadError
+end
+
+begin
+ require 'rcov/rcovtask'
+ Rcov::RcovTask.new("coverage") do |rcov|
+ rcov.libs << 'lib'
+ rcov.pattern = 'test/test_*.rb'
+ end
+rescue LoadError
+end
+
+Rake::TestTask.new("test") do |test|
+ test.libs << 'lib'
+ test.warning = true
+ test.pattern = 'test/test_*.rb'
+end
+
+Rake::RDocTask.new("doc") do |rdoc|
+ rdoc.rdoc_dir = 'doc'
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
View
@@ -49,13 +49,22 @@
#
class HTTPClient
- VERSION = '2.1.2'
+ VERSION = '2.1.3-SNAPSHOT'
RUBY_VERSION_STRING = "ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
s = %w$Id$
RCS_FILE, RCS_REVISION = s[1][/.*(?=,v$)/], s[2]
LIB_NAME = "(#{RCS_FILE}/#{RCS_REVISION}, #{RUBY_VERSION_STRING})"
- include Util
+ class ConfigurationError < StandardError
+ end
+
+ class BadResponse < RuntimeError
+ end
+
+ # for backward compatibility
+ class Session
+ BadResponse = ::HTTPClient::BadResponse
+ end
attr_reader :agent_name
attr_reader :from
@@ -69,6 +78,8 @@ class HTTPClient
attr_accessor :follow_redirect_count
attr_accessor :protocol_retry_count
+ include Util
+
class << self
%w(get_content head get post put delete options propfind trace).each do |name|
eval <<-EOD
@@ -79,9 +90,6 @@ def #{name}(*arg)
end
end
- class RetryableResponse < StandardError
- end
-
PROPFIND_DEFAULT_EXTHEADER = { 'Depth' => '0' }
# SYNOPSIS
@@ -176,7 +184,7 @@ def proxy=(proxy)
@proxy = urify(proxy)
if @proxy.scheme == nil or @proxy.scheme.downcase != 'http' or
@proxy.host == nil or @proxy.port == nil
- raise ArgumentError.new("unsupported proxy `#{proxy}'")
+ raise ArgumentError.new("unsupported proxy #{proxy}")
end
@proxy_auth.reset_challenge
if @proxy.user || @proxy.password
@@ -282,7 +290,7 @@ def post_content(uri, body = nil, extheader = {}, &block)
def strict_redirect_uri_callback(uri, res)
newuri = URI.parse(res.header['location'][0])
unless newuri.is_a?(URI::HTTP)
- raise RuntimeError.new("unexpected location: #{newuri}")
+ raise BadResponse.new("unexpected location: #{newuri}")
end
puts "Redirect to: #{newuri}" if $DEBUG
newuri
@@ -411,6 +419,12 @@ def reset_all
private
+ class RetryableResponse < StandardError # :nodoc:
+ end
+
+ class KeepAliveDisconnected < StandardError # :nodoc:
+ end
+
def do_request(req, proxy, &block)
conn = Connection.new
res = nil
@@ -483,16 +497,16 @@ def follow_redirect(uri, block = nil)
uri = @redirect_uri_callback.call(uri, res)
retry_number += 1
else
- raise RuntimeError.new("Unexpected response: #{res.header.inspect}")
+ raise BadResponse.new("unexpected response: #{res.header.inspect}")
end
end
- raise RuntimeError.new("Retry count exceeded.")
+ raise BadResponse.new("retry count exceeded")
end
def protect_keep_alive_disconnected
begin
yield
- rescue Session::KeepAliveDisconnected
+ rescue KeepAliveDisconnected
yield
end
end
@@ -608,7 +622,7 @@ def do_get_header(req, res, sess)
if /^([^:]+)\s*:\s*(.*)$/ =~ line
res.header.set($1, $2)
else
- STDERR.puts("Unparsable header: '#{line}'.") if $DEBUG
+ STDERR.puts("Unparsable header: '#{line}'") if $DEBUG
end
end
if @cookie_manager && res.header['set-cookie']
View
@@ -7,6 +7,7 @@
require 'digest/md5'
+require 'httpclient/session'
class HTTPClient
@@ -29,33 +30,17 @@ class HTTPClient
class AuthFilterBase
- def reset_challenge
- raise NotImplementedError
- end
-
- def filter_request(req)
- raise NotImplementedError
- end
-
- def filter_response(req, res)
- raise NotImplementedError
- end
-
private
def parse_authentication_header(res, tag)
challenge = res.header[tag]
- unless challenge
- raise RuntimeError.new("no #{tag} header exists: #{res}")
- end
- challenge.collect { |c| parse_challenge_header(c) }
+ return nil unless challenge
+ challenge.collect { |c| parse_challenge_header(c) }.compact
end
def parse_challenge_header(challenge)
scheme, param_str = challenge.scan(/\A(\S+)(?:\s+(.*))?\z/)[0]
- if scheme.nil?
- raise RuntimeError.new("unsupported challenge: #{challenge}")
- end
+ return nil if scheme.nil?
return scheme, param_str
end
end
View
@@ -227,7 +227,7 @@ def each_sess
class SSLSocketWrap
def initialize(socket, context, debug_dev = nil)
unless SSLEnabled
- raise RuntimeError.new("Ruby/OpenSSL module is required for httpclient.")
+ raise ConfigurationError.new('Ruby/OpenSSL module is required')
end
@context = context
@socket = socket
@@ -245,7 +245,7 @@ def post_connection_check(host)
return
elsif @ssl_socket.peer_cert.nil? and
check_mask(verify_mode, OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT)
- raise OpenSSL::SSL::SSLError, "no peer cert"
+ raise OpenSSL::SSL::SSLError.new('no peer cert')
end
hostname = host.host
if @ssl_socket.respond_to?(:post_connection_check) and RUBY_VERSION > "1.8.4"
@@ -447,12 +447,6 @@ class Error < StandardError
class InvalidState < Error
end
- class BadResponse < Error
- end
-
- class KeepAliveDisconnected < Error
- end
-
attr_reader :dest # Destination site
attr_reader :src # Source site
attr_accessor :proxy # Proxy site
@@ -553,7 +547,7 @@ def get_status
version = status = reason = nil
begin
if @state != :META
- raise InvalidState.new("get_status must be called at the beginning of a session.")
+ raise InvalidState.new("get_status must be called at the beginning of a session")
end
version, status, reason = read_header
rescue
@@ -735,7 +729,7 @@ def connect_ssl_proxy(socket, uri)
res.version, res.status, res.reason = @version, @status, @reason
@headers.each do |line|
unless /^([^:]+)\s*:\s*(.*)$/ =~ line
- raise BadResponse.new("unparsable header: '#{line}'.") if $DEBUG
+ raise BadResponse.new("unparsable header: '#{line}'") if $DEBUG
end
res.header.set($1, $2)
end
@@ -821,7 +815,7 @@ def parse_header(socket)
while true
line = socket.gets("\n")
unless line
- raise BadResponse.new('Unexpected EOF.')
+ raise BadResponse.new('Unexpected EOF')
end
line.sub!(/\r?\n\z/, '')
break if line.empty?
@@ -207,10 +207,10 @@ def test_get_content
assert_equal('hello', @client.get_content(@url + 'hello'))
assert_equal('hello', @client.get_content(@url + 'redirect1'))
assert_equal('hello', @client.get_content(@url + 'redirect2'))
- assert_raises(RuntimeError) do
+ assert_raises(HTTPClient::BadResponse) do
@client.get_content(@url + 'notfound')
end
- assert_raises(RuntimeError) do
+ assert_raises(HTTPClient::BadResponse) do
@client.get_content(@url + 'redirect_self')
end
called = false
@@ -227,10 +227,10 @@ def test_post_content
assert_equal('hello', @client.post_content(@url + 'hello'))
assert_equal('hello', @client.post_content(@url + 'redirect1'))
assert_equal('hello', @client.post_content(@url + 'redirect2'))
- assert_raises(RuntimeError) do
+ assert_raises(HTTPClient::BadResponse) do
@client.post_content(@url + 'notfound')
end
- assert_raises(RuntimeError) do
+ assert_raises(HTTPClient::BadResponse) do
@client.post_content(@url + 'redirect_self')
end
called = false
View
@@ -262,7 +262,7 @@ def test_redirect_relative
assert_equal('hello', @client.get_content(@url + 'redirect1'))
@client.reset_all
@client.test_loopback_http_response << "HTTP/1.0 302 OK\nLocation: hello\n\n"
- assert_raises(RuntimeError) do
+ assert_raises(HTTPClient::BadResponse) do
assert_equal('hello', @client.get_content(@url + 'redirect1'))
end
end
@@ -279,10 +279,10 @@ def test_get_content
@client.reset(url)
@client.reset(@url)
@client.reset(url)
- assert_raises(RuntimeError) do
+ assert_raises(HTTPClient::BadResponse) do
@client.get_content(@url + 'notfound')
end
- assert_raises(RuntimeError) do
+ assert_raises(HTTPClient::BadResponse) do
@client.get_content(@url + 'redirect_self')
end
called = false
@@ -311,10 +311,10 @@ def test_post_content
assert_equal('hello', @client.post_content(@url + 'hello'))
assert_equal('hello', @client.post_content(@url + 'redirect1'))
assert_equal('hello', @client.post_content(@url + 'redirect2'))
- assert_raises(RuntimeError) do
+ assert_raises(HTTPClient::BadResponse) do
@client.post_content(@url + 'notfound')
end
- assert_raises(RuntimeError) do
+ assert_raises(HTTPClient::BadResponse) do
@client.post_content(@url + 'redirect_self')
end
called = false

0 comments on commit e3ae9e2

Please sign in to comment.