http-request 1.2.2 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/http-request.rb CHANGED
@@ -26,7 +26,7 @@ module HttpRequest
26
26
  :timeout => 30, # default seconds to timeout a request
27
27
  :auto_redirect => true,
28
28
  :max_redirects => 3,
29
- :keep_alive => true
29
+ :keep_alive => 60
30
30
  }
31
31
  @config.tap { @config.merge!(options) if !options.empty? }
32
32
  end
@@ -35,8 +35,7 @@ module HttpRequest
35
35
  [:get, :post, :put, :delete, :head].each do |verb|
36
36
  define_method(verb) do |*args|
37
37
  url, headers, body = args
38
- @client ||= open(url)
39
- @client.send verb, *args
38
+ open(url).send verb, *args
40
39
  end
41
40
  end
42
41
  end
@@ -22,7 +22,7 @@ module HttpRequest
22
22
  @open_url ||= url
23
23
 
24
24
  # Let's close any dead connections as we open new ones
25
- cleanup_dead_connections!
25
+ sweep_dead_connections!
26
26
 
27
27
  # Reuse existing connection or open a new one..
28
28
  conn = connection(url)
@@ -95,9 +95,9 @@ module HttpRequest
95
95
 
96
96
  private
97
97
 
98
- def cleanup_dead_connections!
99
- dead_conns = connections.collect {|conn| conn if conn.closed? }.compact
100
- dead_conns.each {|conn| @connections.delete(conn) }
98
+ def sweep_dead_connections!
99
+ dead_conns = connections.collect {|conn| conn if conn.dead? }.compact
100
+ dead_conns.each {|conn| conn.close; @connections.delete(conn) }
101
101
  end
102
102
  end
103
103
  end
@@ -1,7 +1,8 @@
1
1
  module HttpRequest
2
2
  class Connection
3
- attr_reader :uri, :http
4
- attr_accessor :user_agent, :max_retries, :timeout, :auto_redirect, :max_redirects, :keep_alive
3
+ attr_reader :uri, :http, :time_since_last_io
4
+ attr_accessor :user_agent, :max_retries, :timeout, :auto_redirect,
5
+ :max_redirects, :keep_alive
5
6
 
6
7
  def initialize(url, options={})
7
8
  # Connection settings
@@ -34,6 +35,8 @@ module HttpRequest
34
35
  if socket && socket.respond_to?(:io) && Socket.const_defined?(:TCP_NODELAY)
35
36
  socket.io.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true)
36
37
  end
38
+
39
+ @time_since_last_io = Time.now.to_i
37
40
  end
38
41
 
39
42
  def ssl?
@@ -52,6 +55,19 @@ module HttpRequest
52
55
  !(!http.nil? && http.started?)
53
56
  end
54
57
 
58
+ def dead?
59
+ return false if !opened? # Can't be dead if it hasn't strated
60
+ return !keep_alive? || socket.closed?
61
+ end
62
+
63
+ def keep_alive?
64
+ if keep_alive > 0 && time_since_last_io
65
+ (Time.now.to_i - time_since_last_io) < keep_alive
66
+ else
67
+ false
68
+ end
69
+ end
70
+
55
71
  def socket
56
72
  return if http.nil?
57
73
  http.instance_variable_get(:@socket)
@@ -87,7 +103,7 @@ module HttpRequest
87
103
 
88
104
  # Default headers
89
105
  headers['User-Agent'] ||= user_agent
90
- headers['Connection'] ||= 'keep-alive' if keep_alive
106
+ headers['Connection'] ||= 'keep-alive' if keep_alive?
91
107
  headers['Content-Length'] ||= body.bytesize.to_s if body
92
108
  headers['Accept-Encoding'] ||= 'gzip' # TODO: add deflate support
93
109
 
@@ -96,10 +112,14 @@ module HttpRequest
96
112
  req = req_klass.new(uri.request_uri, headers)
97
113
  req.body = body if !body.nil? && !body.empty?
98
114
 
115
+ # do auth if user and password provided
116
+ req.basic_auth uri.user, uri.password if uri.user && uri.password
117
+
99
118
  # Make the HTTP request
100
119
  retries = max_retries
101
120
  begin
102
121
  response = http.request(req)
122
+ @time_since_last_io = Time.now.to_i
103
123
  rescue EOFError, Errno::EPIPE
104
124
  # Something happened to our connection, lets try this again
105
125
  if retries > 0
@@ -111,7 +131,7 @@ module HttpRequest
111
131
 
112
132
  # Let's make sure we close the connection if we don't intend to
113
133
  # have persistent connections..
114
- close if !keep_alive
134
+ close if !keep_alive?
115
135
 
116
136
  response
117
137
  end
@@ -15,7 +15,7 @@ module HttpRequest
15
15
  # Auto decompress content
16
16
  content_encoding = @header['content-encoding'].to_s.downcase
17
17
  if !content_encoding.empty? && content_encoding =~ /gzip/
18
- gz = Zlib::GzipReader.new(StringIO.new(@body))
18
+ gz = Zlib::GzipReader.new(StringIO.new(@body), :external_encoding => @body.encoding)
19
19
  @body = gz.read
20
20
  end
21
21
 
@@ -24,13 +24,13 @@ module HttpRequest
24
24
  charset = content_type.match(/charset=([a-z0-9-]*)/i).to_s
25
25
  if !charset.empty?
26
26
  # Encode to suggested charset
27
- encoding = Encoding.find(charset.split('=')[1]) rescue 'utf-8'
28
- @body.force_encoding(encoding)
27
+ encoding = Encoding.find(charset.split('=')[1]) rescue nil
28
+ @body.force_encoding(encoding) if !encoding.nil?
29
29
  else
30
30
  # Encode to utf-8 by default for any ascii content types
31
31
  # that don't specify the charset
32
- mime_type = MIME::Types[content_type].first
33
- @body.force_encoding('utf-8') if mime_type && mime_type.ascii?
32
+ # mime_type = MIME::Types[content_type].first
33
+ # @body.force_encoding('utf-8') if mime_type && mime_type.ascii?
34
34
  end
35
35
  end
36
36
 
@@ -1,3 +1,3 @@
1
1
  module HttpRequest
2
- VERSION = '1.2.2'
2
+ VERSION = '1.2.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http-request
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-13 00:00:00.000000000 Z
12
+ date: 2013-03-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: addressable
@@ -102,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
102
102
  version: '0'
103
103
  segments:
104
104
  - 0
105
- hash: 4195676129085579668
105
+ hash: 4305832184891146306
106
106
  required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  none: false
108
108
  requirements:
@@ -111,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  version: 1.3.6
112
112
  requirements: []
113
113
  rubyforge_project:
114
- rubygems_version: 1.8.24
114
+ rubygems_version: 1.8.25
115
115
  signing_key:
116
116
  specification_version: 3
117
117
  summary: Simple HTTP client built with Net/HTTP