http2 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/Gemfile.lock +1 -1
  2. data/VERSION +1 -1
  3. data/http2.gemspec +8 -8
  4. data/lib/http2.rb +44 -14
  5. metadata +4 -6
@@ -8,7 +8,7 @@ GEM
8
8
  git (>= 1.2.5)
9
9
  rake
10
10
  rdoc
11
- json (1.7.4)
11
+ json (1.7.5)
12
12
  rake (0.9.2.2)
13
13
  rdoc (3.12)
14
14
  json (~> 1.4)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.10
1
+ 0.0.11
@@ -4,14 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{http2}
8
- s.version = "0.0.10"
7
+ s.name = "http2"
8
+ s.version = "0.0.11"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kasper Johansen"]
12
- s.date = %q{2012-08-16}
13
- s.description = %q{A lightweight framework for doing http-connections in Ruby. Supports cookies, keep-alive, compressing and much more.}
14
- s.email = %q{k@spernj.org}
12
+ s.date = "2012-09-30"
13
+ s.description = "A lightweight framework for doing http-connections in Ruby. Supports cookies, keep-alive, compressing and much more."
14
+ s.email = "k@spernj.org"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
17
  "README.rdoc"
@@ -33,11 +33,11 @@ Gem::Specification.new do |s|
33
33
  "spec/http2_spec.rb",
34
34
  "spec/spec_helper.rb"
35
35
  ]
36
- s.homepage = %q{http://github.com/kaspernj/http2}
36
+ s.homepage = "http://github.com/kaspernj/http2"
37
37
  s.licenses = ["MIT"]
38
38
  s.require_paths = ["lib"]
39
- s.rubygems_version = %q{1.6.2}
40
- s.summary = %q{A lightweight framework for doing http-connections in Ruby. Supports cookies, keep-alive, compressing and much more.}
39
+ s.rubygems_version = "1.8.24"
40
+ s.summary = "A lightweight framework for doing http-connections in Ruby. Supports cookies, keep-alive, compressing and much more."
41
41
 
42
42
  if s.respond_to? :specification_version then
43
43
  s.specification_version = 3
@@ -71,14 +71,14 @@ class Http2
71
71
 
72
72
  #Returns boolean based on the if the object is connected and the socket is working.
73
73
  #===Examples
74
- # print "Socket is working." if http.socket_working?
74
+ # puts "Socket is working." if http.socket_working?
75
75
  def socket_working?
76
76
  return false if !@sock or @sock.closed?
77
77
 
78
78
  if @keepalive_timeout and @request_last
79
79
  between = Time.now.to_i - @request_last.to_i
80
80
  if between >= @keepalive_timeout
81
- print "Http2: We are over the keepalive-wait - returning false for socket_working?.\n" if @debug
81
+ puts "Http2: We are over the keepalive-wait - returning false for socket_working?." if @debug
82
82
  return false
83
83
  end
84
84
  end
@@ -111,7 +111,7 @@ class Http2
111
111
  #Reconnects to the host.
112
112
  def reconnect
113
113
  require "socket"
114
- print "Http2: Reconnect.\n" if @debug
114
+ puts "Http2: Reconnect." if @debug
115
115
 
116
116
  #Reset variables.
117
117
  @keepalive_max = nil
@@ -176,8 +176,8 @@ class Http2
176
176
  raise "Invalid arguments: '#{args.class.name}'."
177
177
  end
178
178
 
179
- if args[:url].to_s.strip.empty?
180
- raise "Empty URL given: '#{args[:url]}'."
179
+ if !args.key?(:url) or !args[:url]
180
+ raise "No URL given: '#{args[:url]}'."
181
181
  elsif args[:url].to_s.split("\n").length > 1
182
182
  raise "Multiple lines given in URL: '#{args[:url]}'."
183
183
  end
@@ -218,10 +218,10 @@ class Http2
218
218
 
219
219
  begin
220
220
  raise Errno::EPIPE, "The socket is closed." if !@sock or @sock.closed?
221
- @sock.puts(str)
221
+ self.sock_puts(str)
222
222
  rescue Errno::EPIPE #this can also be thrown by puts.
223
223
  self.reconnect
224
- @sock.puts(str)
224
+ self.sock_puts(str)
225
225
  end
226
226
 
227
227
  @request_last = Time.now
@@ -235,11 +235,17 @@ class Http2
235
235
  return args[:default_headers] if args[:default_headers]
236
236
 
237
237
  headers = {
238
- "Host" => @args[:host],
239
238
  "Connection" => "Keep-Alive",
240
239
  "User-Agent" => @uagent
241
240
  }
242
241
 
242
+ #Possible to give custom host-argument.
243
+ if args[:host]
244
+ headers["Host"] = args[:host]
245
+ else
246
+ headers["Host"] = @args[:host]
247
+ end
248
+
243
249
  if !@args.key?(:encoding_gzip) or @args[:encoding_gzip]
244
250
  headers["Accept-Encoding"] = "gzip"
245
251
  else
@@ -401,7 +407,7 @@ class Http2
401
407
  File.open(praw.path, "r") do |fp|
402
408
  begin
403
409
  while data = fp.sysread(4096)
404
- @sock.write(data)
410
+ self.sock_write(data)
405
411
  end
406
412
  rescue EOFError
407
413
  #ignore.
@@ -412,6 +418,17 @@ class Http2
412
418
  end
413
419
  end
414
420
 
421
+ def sock_write(str)
422
+ str = str.to_s
423
+ return nil if str.empty?
424
+ count = @sock.write(str)
425
+ raise "Couldnt write to socket: '#{count}', '#{str}'." if count <= 0
426
+ end
427
+
428
+ def sock_puts(str)
429
+ self.sock_write("#{str}#{@nl}")
430
+ end
431
+
415
432
  #Returns a header-string which normally would be used for a request in the given state.
416
433
  def header_str(headers_hash, args = {})
417
434
  if @cookies.length > 0 and (!args.key?(:cookies) or args[:cookies])
@@ -451,6 +468,8 @@ class Http2
451
468
  @mode = "headers"
452
469
  @resp = Http2::Response.new(:request_args => args)
453
470
 
471
+ rec_count = 0
472
+
454
473
  loop do
455
474
  begin
456
475
  if @length and @length > 0 and @mode == "body"
@@ -459,11 +478,22 @@ class Http2
459
478
  line = @sock.gets
460
479
  end
461
480
 
481
+ if line
482
+ rec_count += line.length
483
+ elsif !line and rec_count <= 0
484
+ @sock = nil
485
+ raise Errno::ECONNABORTED, "Server closed the connection before being able to read anything (KeepAliveMax: '#{@keepalive_max}', Connection: '#{@connection}', PID: '#{Process.pid}')."
486
+ end
487
+
462
488
  print "<#{@mode}>: '#{line}'\n" if @debug
463
- rescue Errno::ECONNRESET
464
- print "Http2: The connection was reset while reading - breaking gently...\n" if @debug
465
- @sock = nil
466
- break
489
+ rescue Errno::ECONNRESET => e
490
+ if rec_count > 0
491
+ print "Http2: The connection was reset while reading - breaking gently...\n" if @debug
492
+ @sock = nil
493
+ break
494
+ else
495
+ raise Errno::ECONNABORTED, "Server closed the connection before being able to read anything (KeepAliveMax: '#{@keepalive_max}', Connection: '#{@connection}', PID: '#{Process.pid}')."
496
+ end
467
497
  end
468
498
 
469
499
  break if line.to_s == ""
@@ -512,7 +542,7 @@ class Http2
512
542
  @resp = nil
513
543
  @mode = nil
514
544
 
515
- raise "No status-code was received from the server.\n\nHeaders:\n#{resp.headers}\n\nBody:\n#{resp.args[:body]}" if !resp.args[:code]
545
+ raise "No status-code was received from the server. Headers: '#{resp.headers}' Body: '#{resp.args[:body]}'." if !resp.args[:code]
516
546
 
517
547
  if resp.args[:code].to_s == "302" and resp.header?("location") and (!@args.key?(:follow_redirects) or @args[:follow_redirects])
518
548
  require "uri"
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: http2
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.10
5
+ version: 0.0.11
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kasper Johansen
@@ -10,8 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-08-16 00:00:00 +02:00
14
- default_executable:
13
+ date: 2012-09-30 00:00:00 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: rspec
@@ -82,7 +81,6 @@ files:
82
81
  - lib/http2.rb
83
82
  - spec/http2_spec.rb
84
83
  - spec/spec_helper.rb
85
- has_rdoc: true
86
84
  homepage: http://github.com/kaspernj/http2
87
85
  licenses:
88
86
  - MIT
@@ -96,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
94
  requirements:
97
95
  - - ">="
98
96
  - !ruby/object:Gem::Version
99
- hash: 4399535538021775808
97
+ hash: -3252344485534143672
100
98
  segments:
101
99
  - 0
102
100
  version: "0"
@@ -109,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
107
  requirements: []
110
108
 
111
109
  rubyforge_project:
112
- rubygems_version: 1.6.2
110
+ rubygems_version: 1.8.24
113
111
  signing_key:
114
112
  specification_version: 3
115
113
  summary: A lightweight framework for doing http-connections in Ruby. Supports cookies, keep-alive, compressing and much more.