http 0.8.0.pre2 → 0.8.0.pre3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +1 -1
- data/lib/http/client.rb +1 -1
- data/lib/http/connection.rb +17 -1
- data/lib/http/options.rb +6 -1
- data/lib/http/version.rb +1 -1
- data/spec/lib/http/client_spec.rb +10 -1
- data/spec/lib/http/options/merge_spec.rb +2 -0
- data/spec/support/connection_reuse_shared.rb +12 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5758483fce79ce2698462f1ffedc148d01d75c25
|
4
|
+
data.tar.gz: ac44a7864edf119f4287c9304537982887cdcdcd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17cf0d470610ce06db0ff695674ae80a59b1fb1b3a6403f1851aaf15284ecdba86e1584bc99ae9bcdb3fe431909111fe02e45f586afdaed0866a7a867cf481f3
|
7
|
+
data.tar.gz: 76101412bb754229a66b1612c3ccb656265abde996c643a1b7021f47ba05760ac85d98ff20f3c1f517a99da7f445101928e3fe168c8cce8b64a7d76ef418cdea
|
data/CHANGES.md
CHANGED
data/lib/http/client.rb
CHANGED
@@ -94,7 +94,7 @@ module HTTP
|
|
94
94
|
|
95
95
|
# We re-create the connection object because we want to let prior requests
|
96
96
|
# lazily load the body as long as possible, and this mimics prior functionality.
|
97
|
-
elsif
|
97
|
+
elsif @connection && (!@connection.keep_alive? || @connection.expired?)
|
98
98
|
close
|
99
99
|
end
|
100
100
|
end
|
data/lib/http/connection.rb
CHANGED
@@ -3,7 +3,7 @@ require "http/response/parser"
|
|
3
3
|
module HTTP
|
4
4
|
# A connection to the HTTP server
|
5
5
|
class Connection
|
6
|
-
attr_reader :socket, :parser, :persistent,
|
6
|
+
attr_reader :socket, :parser, :persistent, :keep_alive_timeout,
|
7
7
|
:pending_request, :pending_response
|
8
8
|
|
9
9
|
# Attempt to read this much data
|
@@ -12,11 +12,15 @@ module HTTP
|
|
12
12
|
def initialize(req, options)
|
13
13
|
@persistent = options.persistent?
|
14
14
|
|
15
|
+
@keep_alive_timeout = options[:keep_alive_timeout]
|
16
|
+
|
15
17
|
@parser = Response::Parser.new
|
16
18
|
|
17
19
|
@socket = options[:socket_class].open(req.socket_host, req.socket_port)
|
18
20
|
|
19
21
|
start_tls(req.uri.host, options[:ssl_socket_class], options[:ssl_context]) if req.uri.is_a?(URI::HTTPS) && !req.using_proxy?
|
22
|
+
|
23
|
+
reset_timer
|
20
24
|
end
|
21
25
|
|
22
26
|
# Send a request to the server
|
@@ -71,6 +75,7 @@ module HTTP
|
|
71
75
|
close unless keep_alive?
|
72
76
|
|
73
77
|
parser.reset
|
78
|
+
reset_timer
|
74
79
|
|
75
80
|
@pending_response = nil
|
76
81
|
end
|
@@ -88,6 +93,17 @@ module HTTP
|
|
88
93
|
!!@keep_alive && !socket.closed?
|
89
94
|
end
|
90
95
|
|
96
|
+
# Whether our connection has expired
|
97
|
+
def expired?
|
98
|
+
!@conn_expires_at || @conn_expires_at < Time.now
|
99
|
+
end
|
100
|
+
|
101
|
+
def reset_timer
|
102
|
+
@conn_expires_at = Time.now + keep_alive_timeout if persistent
|
103
|
+
end
|
104
|
+
|
105
|
+
private :reset_timer
|
106
|
+
|
91
107
|
# Store whether the connection should be kept alive.
|
92
108
|
# Once we reset the parser, we lose all of this state.
|
93
109
|
def set_keep_alive
|
data/lib/http/options.rb
CHANGED
@@ -44,6 +44,7 @@ module HTTP
|
|
44
44
|
:socket_class => self.class.default_socket_class,
|
45
45
|
:ssl_socket_class => self.class.default_ssl_socket_class,
|
46
46
|
:cache => self.class.default_cache,
|
47
|
+
:keep_alive_timeout => 5,
|
47
48
|
:headers => {}}
|
48
49
|
|
49
50
|
opts_w_defaults = defaults.merge(options)
|
@@ -58,7 +59,11 @@ module HTTP
|
|
58
59
|
self.headers.merge(headers)
|
59
60
|
end
|
60
61
|
|
61
|
-
%w(
|
62
|
+
%w(
|
63
|
+
proxy params form json body follow response
|
64
|
+
socket_class ssl_socket_class ssl_context
|
65
|
+
persistent keep_alive_timeout
|
66
|
+
).each do |method_name|
|
62
67
|
def_option method_name
|
63
68
|
end
|
64
69
|
|
data/lib/http/version.rb
CHANGED
@@ -169,16 +169,25 @@ RSpec.describe HTTP::Client do
|
|
169
169
|
|
170
170
|
include_context "handles shared connections" do
|
171
171
|
let(:reuse_conn) { nil }
|
172
|
+
let(:keep_alive_timeout) { 5 }
|
173
|
+
|
172
174
|
let(:server) { dummy }
|
173
|
-
let(:client)
|
175
|
+
let(:client) do
|
176
|
+
described_class.new(
|
177
|
+
:persistent => reuse_conn,
|
178
|
+
:keep_alive_timeout => keep_alive_timeout
|
179
|
+
)
|
180
|
+
end
|
174
181
|
end
|
175
182
|
|
176
183
|
describe "SSL" do
|
177
184
|
let(:reuse_conn) { nil }
|
185
|
+
let(:keep_alive_timeout) { 5 }
|
178
186
|
|
179
187
|
let(:client) do
|
180
188
|
described_class.new(
|
181
189
|
:persistent => reuse_conn,
|
190
|
+
:keep_alive_timeout => keep_alive_timeout,
|
182
191
|
:ssl_context => OpenSSL::SSL::SSLContext.new.tap do |context|
|
183
192
|
context.options = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS[:options]
|
184
193
|
|
@@ -32,6 +32,7 @@ RSpec.describe HTTP::Options, "merge" do
|
|
32
32
|
:form => {:bar => "bar"},
|
33
33
|
:body => "body-bar",
|
34
34
|
:json => {:bar => "bar"},
|
35
|
+
:keep_alive_timeout => 10,
|
35
36
|
:headers => {:accept => "xml", :bar => "bar"},
|
36
37
|
:proxy => {:proxy_address => "127.0.0.1", :proxy_port => 8080})
|
37
38
|
|
@@ -42,6 +43,7 @@ RSpec.describe HTTP::Options, "merge" do
|
|
42
43
|
:body => "body-bar",
|
43
44
|
:json => {:bar => "bar"},
|
44
45
|
:persistent => "https://www.googe.com",
|
46
|
+
:keep_alive_timeout => 10,
|
45
47
|
:headers => {"Foo" => "foo", "Accept" => "xml", "Bar" => "bar"},
|
46
48
|
:proxy => {:proxy_address => "127.0.0.1", :proxy_port => 8080},
|
47
49
|
:follow => nil,
|
@@ -62,6 +62,18 @@ RSpec.shared_context "handles shared connections" do
|
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
+
context "with a Keep-Alive timeout of 0" do
|
66
|
+
let(:keep_alive_timeout) { 0 }
|
67
|
+
|
68
|
+
it "automatically opens a new socket" do
|
69
|
+
first_socket = client.get("#{server.endpoint}/socket/1").body.to_s
|
70
|
+
sleep 0.1
|
71
|
+
second_socket = client.get("#{server.endpoint}/socket/2").body.to_s
|
72
|
+
|
73
|
+
expect(first_socket).to_not eq(second_socket)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
65
77
|
context "with a change in host" do
|
66
78
|
it "errors" do
|
67
79
|
expect { client.get("https://invalid.com/socket") }.to raise_error(/Persistence is enabled/i)
|