astro-em-http-request 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
@@ -198,7 +198,14 @@ module EventMachine
198
198
 
199
199
  # start HTTP request once we establish connection to host
200
200
  def connection_completed
201
+ if @options[:max_connection_duration]
202
+ EM.add_timer(@options[:max_connection_duration]) {
203
+ @aborted = true
204
+ on_error("Max Connection Duration Exceeded (#{@options[:max_connection_duration]}s.)")
205
+ }
206
+ end
201
207
  ssl = @options[:tls] || @options[:ssl] || {}
208
+ @bytes_received = 0
202
209
  start_tls(ssl) if @uri.scheme == "https" #or @uri.port == 443 # A user might not want https even on port 443.
203
210
 
204
211
  send_request_header
@@ -276,8 +283,13 @@ module EventMachine
276
283
  end
277
284
 
278
285
  def receive_data(data)
279
- @data << data
280
- dispatch
286
+ @bytes_received += data.size
287
+ if @options[:max_bytes].nil? or @options[:max_bytes] > @bytes_received
288
+ @data << data
289
+ dispatch
290
+ else
291
+ on_error("Bytes received exceeds limit (#{@bytes_received} vs. #{ @options[:max_bytes]})")
292
+ end
281
293
  end
282
294
 
283
295
  # Called when part of the body has been read
@@ -302,7 +314,12 @@ module EventMachine
302
314
  end
303
315
 
304
316
  def unbind
305
- if @state == :finished || (@state == :body && @bytes_remaining.nil?)
317
+ if @state == :finished || (
318
+ @state == :body &&
319
+ @bytes_remaining.nil? &&
320
+ (!@options[:max_bytes] or @bytes_received < @options[:max_bytes]) &&
321
+ (!@options[:max_connection_duration] or !@aborted)
322
+ )
306
323
  succeed(self)
307
324
  else
308
325
  fail(self)
@@ -48,6 +48,16 @@ module EventMachine
48
48
  # host: String
49
49
  # Manually specify TCP connect host address, independent of
50
50
  # Host: header
51
+ #
52
+ # max_bytes: Integer
53
+ # Explictly specify a maximum number of bytes that this connection may receive before shutting down.
54
+ # Default : none.
55
+ #
56
+ # max_connection_duration: Integer
57
+ # Explictly specify a maximum number of second for the connection. This is useful to avoid dealing with never ending connections (AtomStream-like)
58
+ # Default : none.
59
+ #
60
+
51
61
 
52
62
  def get options = {}; send_request(:get, options); end
53
63
  def head options = {}; send_request(:head, options); end
data/test/test_request.rb CHANGED
@@ -444,4 +444,27 @@ describe EventMachine::HttpRequest do
444
444
  }
445
445
  }
446
446
  end
447
+
448
+ it "should return an error if the response size exceeds the :max_bytes option" do
449
+ EventMachine.run {
450
+ http = EventMachine::HttpRequest.new("http://127.0.0.1:8080/").get(:max_bytes => 3)
451
+ http.errback { |http|
452
+ http.errors.should match /Bytes received exceeds limit/
453
+ EventMachine.stop
454
+ }
455
+ http.callback { failed }
456
+ }
457
+ end
458
+
459
+ it "should return an error if the connection duration exceeds the :max_connection_duration option" do
460
+ EventMachine.run {
461
+ http = EventMachine::HttpRequest.new("http://updates.sixapart.com/atom-stream.xml").get(:max_connection_duration => 3)
462
+ http.errback { |http|
463
+ http.errors.should match /Max Connection Duration Exceeded/
464
+ EventMachine.stop
465
+ }
466
+ http.callback { failed }
467
+ }
468
+ end
469
+
447
470
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: astro-em-http-request
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Grigorik
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-10-31 00:00:00 +01:00
13
+ date: 2009-11-03 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency