http2 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/VERSION +1 -1
  2. data/http2.gemspec +2 -2
  3. data/lib/http2.rb +71 -69
  4. data/spec/http2_spec.rb +17 -4
  5. metadata +11 -11
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.12
1
+ 0.0.13
data/http2.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{http2}
8
- s.version = "0.0.12"
8
+ s.version = "0.0.13"
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-10-03}
12
+ s.date = %q{2012-10-05}
13
13
  s.description = %q{A lightweight framework for doing http-connections in Ruby. Supports cookies, keep-alive, compressing and much more.}
14
14
  s.email = %q{k@spernj.org}
15
15
  s.extra_rdoc_files = [
data/lib/http2.rb CHANGED
@@ -339,83 +339,79 @@ class Http2
339
339
  #Use tempfile to store contents to avoid eating memory if posting something really big.
340
340
  require "tempfile"
341
341
 
342
- praw = Tempfile.open("http2_post_multipart_tmp_#{boundary}")
343
- args[:post].each do |key, val|
344
- praw << "--#{boundary}#{@nl}"
345
-
346
- if val.class.name.to_s == "Tempfile" and val.respond_to?(:original_filename)
347
- praw << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{val.original_filename}\";#{@nl}"
348
- praw << "Content-Length: #{val.to_s.bytesize}#{@nl}"
349
- elsif val.is_a?(Hash) and val[:filename]
350
- praw << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{val[:filename]}\";#{@nl}"
342
+ Tempfile.open("http2_post_multipart_tmp_#{boundary}") do |praw|
343
+ args[:post].each do |key, val|
344
+ praw << "--#{boundary}#{@nl}"
351
345
 
352
- if val[:content]
353
- praw << "Content-Length: #{val[:content].to_s.bytesize}#{@nl}"
354
- elsif val[:fpath]
355
- praw << "Content-Length: #{File.size(val[:fpath])}#{@nl}"
346
+ if val.class.name.to_s == "Tempfile" and val.respond_to?(:original_filename)
347
+ praw << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{val.original_filename}\";#{@nl}"
348
+ praw << "Content-Length: #{val.to_s.bytesize}#{@nl}"
349
+ elsif val.is_a?(Hash) and val[:filename]
350
+ praw << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{val[:filename]}\";#{@nl}"
351
+
352
+ if val[:content]
353
+ praw << "Content-Length: #{val[:content].to_s.bytesize}#{@nl}"
354
+ elsif val[:fpath]
355
+ praw << "Content-Length: #{File.size(val[:fpath])}#{@nl}"
356
+ else
357
+ raise "Could not figure out where to get content from."
358
+ end
356
359
  else
357
- raise "Could not figure out where to get content from."
360
+ praw << "Content-Disposition: form-data; name=\"#{key}\";#{@nl}"
361
+ praw << "Content-Length: #{val.to_s.bytesize}#{@nl}"
358
362
  end
359
- else
360
- praw << "Content-Disposition: form-data; name=\"#{key}\";#{@nl}"
361
- praw << "Content-Length: #{val.to_s.bytesize}#{@nl}"
362
- end
363
-
364
- praw << "Content-Type: text/plain#{@nl}"
365
- praw << @nl
366
-
367
- if val.class.name.to_s == "StringIO"
368
- praw << val.read
369
- elsif val.is_a?(Hash) and val[:content]
370
- praw << val[:content].to_s
371
- elsif val.is_a?(Hash) and val[:fpath]
372
- File.open(val[:fpath], "r") do |fp|
373
- begin
374
- while data = fp.sysread(4096)
375
- praw << data
363
+
364
+ praw << "Content-Type: text/plain#{@nl}"
365
+ praw << @nl
366
+
367
+ if val.class.name.to_s == "StringIO"
368
+ praw << val.read
369
+ elsif val.is_a?(Hash) and val[:content]
370
+ praw << val[:content].to_s
371
+ elsif val.is_a?(Hash) and val[:fpath]
372
+ File.open(val[:fpath], "r") do |fp|
373
+ begin
374
+ while data = fp.sysread(4096)
375
+ praw << data
376
+ end
377
+ rescue EOFError
378
+ #ignore.
376
379
  end
377
- rescue EOFError
378
- #ignore.
379
380
  end
381
+ else
382
+ praw << val.to_s
380
383
  end
381
- else
382
- praw << val.to_s
384
+
385
+ praw << @nl
383
386
  end
384
387
 
385
- praw << @nl
386
- end
387
-
388
- praw << "--#{boundary}--"
389
- praw.close(false)
390
-
391
-
392
- #Generate header-string containing 'praw'-variable.
393
- header_str = "POST /#{args[:url]} HTTP/1.1#{@nl}"
394
- header_str << self.header_str(self.default_headers(args).merge(
395
- "Content-Type" => "multipart/form-data; boundary=#{boundary}",
396
- "Content-Length" => praw.size
397
- ), args)
398
- header_str << @nl
399
-
400
-
401
- #Debug.
402
- print "Headerstr: #{header_str}\n" if @debug
403
-
404
-
405
- #Write and return.
406
- @mutex.synchronize do
407
- self.write(header_str)
408
- File.open(praw.path, "r") do |fp|
409
- begin
410
- while data = fp.sysread(4096)
411
- self.sock_write(data)
412
- end
413
- rescue EOFError
414
- #ignore.
388
+ praw << "--#{boundary}--"
389
+
390
+
391
+ #Generate header-string containing 'praw'-variable.
392
+ header_str = "POST /#{args[:url]} HTTP/1.1#{@nl}"
393
+ header_str << self.header_str(self.default_headers(args).merge(
394
+ "Content-Type" => "multipart/form-data; boundary=#{boundary}",
395
+ "Content-Length" => praw.size
396
+ ), args)
397
+ header_str << @nl
398
+
399
+
400
+ #Debug.
401
+ print "Headerstr: #{header_str}\n" if @debug
402
+
403
+
404
+ #Write and return.
405
+ @mutex.synchronize do
406
+ self.write(header_str)
407
+
408
+ praw.rewind
409
+ praw.lines do |data|
410
+ self.sock_write(data)
415
411
  end
412
+
413
+ return self.read_response(args)
416
414
  end
417
-
418
- return self.read_response(args)
419
415
  end
420
416
  end
421
417
 
@@ -532,8 +528,14 @@ class Http2
532
528
  io = StringIO.new(@resp.args[:body])
533
529
  gz = Zlib::GzipReader.new(io)
534
530
  untrusted_str = gz.read
535
- ic = Iconv.new("UTF-8//IGNORE", "UTF-8")
536
- valid_string = ic.iconv(untrusted_str + " ")[0..-2]
531
+
532
+ begin
533
+ valid_string = ic.encode("utf-8")
534
+ rescue
535
+ ic = Iconv.new("UTF-8//IGNORE", "UTF-8")
536
+ valid_string = ic.iconv(untrusted_str + " ")[0..-2]
537
+ end
538
+
537
539
  @resp.args[:body] = valid_string
538
540
  end
539
541
 
data/spec/http2_spec.rb CHANGED
@@ -64,13 +64,26 @@ describe "Http2" do
64
64
  it "should be able to do multipart-requests and keep-alive when using multipart." do
65
65
  Http2.new(:host => "www.partyworm.dk", :follow_redirects => false, :encoding_gzip => false, :debug => false) do |http|
66
66
  0.upto(5) do
67
+ fpath = File.realpath(__FILE__)
68
+ fpath2 = "#{File.realpath(File.dirname(__FILE__))}/../lib/http2.rb"
69
+
67
70
  resp = http.post_multipart(:url => "multipart_test.php", :post => {
68
- "test_var" => "true"
71
+ "test_var" => "true",
72
+ "test_file1" => {
73
+ :fpath => fpath,
74
+ :filename => "specfile"
75
+ },
76
+ "test_file2" => {
77
+ :fpath => fpath2,
78
+ :filename => "http2.rb"
79
+ }
69
80
  })
70
81
 
71
- if resp.body != "multipart-test-test_var=true"
72
- raise "Expected body to be 'multipart-test-test_var=true' but it wasnt: '#{resp.body}'."
73
- end
82
+ data = JSON.parse(resp.body)
83
+
84
+ raise "Expected 'test_var' post to be 'true' but it wasnt: '#{data["post"]["test_var"]}'." if data["post"]["test_var"] != "true"
85
+ raise "Expected 'test_file1' to be the same as file but it wasnt:\n#{data["files_data"]["test_file1"]}\n\n#{File.read(fpath)}" if data["files_data"]["test_file1"] != File.read(fpath)
86
+ raise "Expected 'test_file2' to be the same as file but it wasnt:\n#{data["files_data"]["test_file2"]}\n\n#{File.read(fpath)}" if data["files_data"]["test_file2"] != File.read(fpath2)
74
87
  end
75
88
  end
76
89
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-03 00:00:00.000000000 +02:00
12
+ date: 2012-10-05 00:00:00.000000000 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &20083460 !ruby/object:Gem::Requirement
17
+ requirement: &21138800 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 2.8.0
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *20083460
25
+ version_requirements: *21138800
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rdoc
28
- requirement: &20076900 !ruby/object:Gem::Requirement
28
+ requirement: &21138280 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '3.12'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *20076900
36
+ version_requirements: *21138280
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: bundler
39
- requirement: &20066380 !ruby/object:Gem::Requirement
39
+ requirement: &21135240 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 1.0.0
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *20066380
47
+ version_requirements: *21135240
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: jeweler
50
- requirement: &20060500 !ruby/object:Gem::Requirement
50
+ requirement: &21134660 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: 1.8.4
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *20060500
58
+ version_requirements: *21134660
59
59
  description: A lightweight framework for doing http-connections in Ruby. Supports
60
60
  cookies, keep-alive, compressing and much more.
61
61
  email: k@spernj.org
@@ -96,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
96
  version: '0'
97
97
  segments:
98
98
  - 0
99
- hash: -4041781090872138595
99
+ hash: -1035862485575419894
100
100
  required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  none: false
102
102
  requirements: