http2 0.0.22 → 0.0.23

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.22
1
+ 0.0.23
data/http2.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "http2"
8
- s.version = "0.0.22"
8
+ s.version = "0.0.23"
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 = "2013-06-30"
12
+ s.date = "2013-11-15"
13
13
  s.description = "A lightweight framework for doing http-connections in Ruby. Supports cookies, keep-alive, compressing and much more."
14
14
  s.email = "k@spernj.org"
15
15
  s.extra_rdoc_files = [
data/include/errors.rb CHANGED
@@ -1,17 +1,11 @@
1
1
  #This class holds various classes for error-handeling.
2
2
  class Http2::Errors
3
- #Raised when trying to access something you dont have access to.
4
- class Noaccess < RuntimeError
3
+ class Http2error < RuntimeError
5
4
  attr_accessor :response
6
5
  end
7
6
 
8
- #Raised when an internal error occurs on the servers side.
9
- class Internalserver < RuntimeError
10
- attr_accessor :response
11
- end
12
-
13
- #Raised when a page is not found.
14
- class Notfound < RuntimeError
15
- attr_accessor :response
16
- end
7
+ class Noaccess < Http2error; end
8
+ class Internalserver < Http2error; end
9
+ class Notfound < Http2error; end
10
+ class Badrequest < Http2error; end
17
11
  end
data/lib/http2.rb CHANGED
@@ -286,8 +286,6 @@ class Http2
286
286
 
287
287
  if !@args.key?(:encoding_gzip) or @args[:encoding_gzip]
288
288
  headers["Accept-Encoding"] = "gzip"
289
- else
290
- #headers["Accept-Encoding"] = "none"
291
289
  end
292
290
 
293
291
  if @args[:basic_auth]
@@ -350,7 +348,7 @@ class Http2
350
348
  return praw
351
349
  end
352
350
 
353
- VALID_ARGUMENTS_POST = [:post, :url, :default_headers, :headers, :json, :method, :cookies, :on_content]
351
+ VALID_ARGUMENTS_POST = [:post, :url, :default_headers, :headers, :json, :method, :cookies, :on_content, :content_type]
354
352
  #Posts to a certain page.
355
353
  #===Examples
356
354
  # res = http.post("login.php", {"username" => "John Doe", "password" => 123)
@@ -360,7 +358,6 @@ class Http2
360
358
  end
361
359
 
362
360
  args = self.parse_args(args)
363
- content_type = "application/x-www-form-urlencoded"
364
361
 
365
362
  if args.key?(:method) && args[:method]
366
363
  method = args[:method].to_s.upcase
@@ -370,7 +367,7 @@ class Http2
370
367
 
371
368
  if args[:json]
372
369
  require "json" unless ::Kernel.const_defined?(:JSON)
373
- praw = JSON.generate(args[:json])
370
+ praw = args[:json].to_json
374
371
  content_type = "application/json"
375
372
  elsif args[:post].is_a?(String)
376
373
  praw = args[:post]
@@ -379,15 +376,16 @@ class Http2
379
376
  autostate_set_on_post_hash(phash) if @args[:autostate]
380
377
  praw = Http2.post_convert_data(phash)
381
378
  end
379
+
380
+ content_type = args[:content_type] || content_type || "application/x-www-form-urlencoded"
382
381
 
383
382
  @mutex.synchronize do
384
- puts "Doing post." if @debug
383
+ puts "Http2: Doing post." if @debug
385
384
 
386
385
  header_str = "#{method} /#{args[:url]} HTTP/1.1#{@nl}"
387
386
  header_str << self.header_str({"Content-Length" => praw.bytesize, "Content-Type" => content_type}.merge(self.default_headers(args)), args)
388
387
  header_str << @nl
389
388
  header_str << praw
390
- header_str << @nl
391
389
 
392
390
  puts "Http2: Header str: #{header_str}" if @debug
393
391
 
@@ -509,10 +507,15 @@ class Http2
509
507
  first = false if first
510
508
 
511
509
  if cookie_data.is_a?(Hash)
512
- cstr << "#{Http2::Utils.urlenc(cookie_data["name"])}=#{Http2::Utils.urlenc(cookie_data["value"])}"
510
+ name = cookie_data["name"]
511
+ value = cookie_data["value"]
513
512
  else
514
- cstr << "#{Http2::Utils.urlenc(cookie_name)}=#{Http2::Utils.urlenc(cookie_data)}"
513
+ name = cookie_name
514
+ value = cookie_data
515
515
  end
516
+
517
+ raise "Unexpected lines: #{value.lines.to_a.length}." if value.lines.to_a.length != 1
518
+ cstr << "#{Http2::Utils.urlenc(name)}=#{Http2::Utils.urlenc(value)}"
516
519
  end
517
520
 
518
521
  headers_hash["Cookie"] = cstr
@@ -568,7 +571,7 @@ class Http2
568
571
 
569
572
  break if line.to_s == ""
570
573
 
571
- if @mode == "headers" and line == @nl
574
+ if @mode == "headers" and (line == "\n" || line == "\r\n")
572
575
  puts "Http2: Changing mode to body!" if @debug
573
576
  raise "No headers was given at all? Possibly corrupt state after last request?" if @resp.headers.empty?
574
577
  break if @length == 0
@@ -654,6 +657,10 @@ class Http2
654
657
  err = Http2::Errors::Noaccess.new(resp.body)
655
658
  err.response = resp
656
659
  raise err
660
+ elsif @raise_errors && resp.args[:code].to_i == 400
661
+ err = Http2::Errors::Badrequest.new(resp.body)
662
+ err.response = resp
663
+ raise err
657
664
  elsif @raise_errors && resp.args[:code].to_i == 404
658
665
  err = Http2::Errors::Notfound.new(resp.body)
659
666
  err.response = resp
@@ -734,14 +741,14 @@ class Http2
734
741
 
735
742
  if len > 0
736
743
  read = @sock.read(len)
737
- return "break" if read == "" or read == @nl
744
+ return "break" if read == "" or (read == "\n" || read == "\r\n")
738
745
  @resp.args[:body] << read
739
746
  self.on_content_call(args, read)
740
747
  end
741
748
 
742
749
  nl = @sock.gets
743
750
  if len == 0
744
- if nl == @nl
751
+ if nl == "\n" || nl == "\r\n"
745
752
  return "break"
746
753
  else
747
754
  raise "Dont know what to do :'-("
data/spec/http2_spec.rb CHANGED
@@ -97,12 +97,13 @@ describe "Http2" do
97
97
  ]
98
98
  urls = ["robots.txt"]
99
99
 
100
- http = Http2.new(:host => "www.partyworm.dk", :debug => false)
101
- 0.upto(105) do |count|
102
- url = urls[rand(urls.size)]
103
- #print "Doing request #{count} of 200 (#{url}).\n"
104
- #res = http.get(url)
105
- #raise "Body was empty." if res.body.to_s.length <= 0
100
+ Http2.new(:host => "www.partyworm.dk", :debug => false) do |http|
101
+ 0.upto(105) do |count|
102
+ url = urls[rand(urls.size)]
103
+ #print "Doing request #{count} of 200 (#{url}).\n"
104
+ res = http.get(url)
105
+ raise "Body was empty." if res.body.to_s.length <= 0
106
+ end
106
107
  end
107
108
  end
108
109
 
@@ -113,7 +114,38 @@ describe "Http2" do
113
114
 
114
115
  it "should raise exception when something is not found" do
115
116
  expect{
116
- res = Http2.new(:host => "www.partyworm.dk").get("something_that_does_not_exist.php")
117
+ Http2.new(:host => "www.partyworm.dk") do |http|
118
+ http.get("something_that_does_not_exist.php")
119
+ end
117
120
  }.to raise_error
118
121
  end
122
+
123
+ it "should be able to post json" do
124
+ Http2.new(:host => "http2test.kaspernj.org") do |http|
125
+ res = http.post(
126
+ :url => "/jsontest.php",
127
+ :json => {:testkey => "testvalue"}
128
+ )
129
+
130
+ data = JSON.parse(res.body)
131
+ data["_SERVER"]["CONTENT_TYPE"].should eql("application/json")
132
+ data["PHP_JSON_INPUT"]["testkey"].should eql("testvalue")
133
+ end
134
+ end
135
+
136
+ it "should be able to post custom content types" do
137
+ require "json"
138
+
139
+ Http2.new(:host => "http2test.kaspernj.org") do |http|
140
+ res = http.post(
141
+ :url => "/content_type_test.php",
142
+ :content_type => "plain/text",
143
+ :post => "test1_test2_test3"
144
+ )
145
+
146
+ data = JSON.parse(res.body)
147
+ data["_SERVER"]["CONTENT_TYPE"].should eql("plain/text")
148
+ data["PHP_INPUT"].should eql("test1_test2_test3")
149
+ end
150
+ end
119
151
  end
metadata CHANGED
@@ -1,75 +1,75 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.22
5
4
  prerelease:
5
+ version: 0.0.23
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kasper Johansen
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-30 00:00:00.000000000 Z
12
+ date: 2013-11-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rspec
16
- requirement: !ruby/object:Gem::Requirement
15
+ version_requirements: !ruby/object:Gem::Requirement
17
16
  none: false
18
17
  requirements:
19
18
  - - ~>
20
19
  - !ruby/object:Gem::Version
21
20
  version: 2.8.0
21
+ name: rspec
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
24
+ requirement: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: 2.8.0
30
30
  - !ruby/object:Gem::Dependency
31
- name: rdoc
32
- requirement: !ruby/object:Gem::Requirement
31
+ version_requirements: !ruby/object:Gem::Requirement
33
32
  none: false
34
33
  requirements:
35
34
  - - ~>
36
35
  - !ruby/object:Gem::Version
37
36
  version: '3.12'
37
+ name: rdoc
38
38
  type: :development
39
39
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
40
+ requirement: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
45
  version: '3.12'
46
46
  - !ruby/object:Gem::Dependency
47
- name: bundler
48
- requirement: !ruby/object:Gem::Requirement
47
+ version_requirements: !ruby/object:Gem::Requirement
49
48
  none: false
50
49
  requirements:
51
50
  - - ! '>='
52
51
  - !ruby/object:Gem::Version
53
52
  version: 1.0.0
53
+ name: bundler
54
54
  type: :development
55
55
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
56
+ requirement: !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: 1.0.0
62
62
  - !ruby/object:Gem::Dependency
63
- name: jeweler
64
- requirement: !ruby/object:Gem::Requirement
63
+ version_requirements: !ruby/object:Gem::Requirement
65
64
  none: false
66
65
  requirements:
67
66
  - - ~>
68
67
  - !ruby/object:Gem::Version
69
68
  version: 1.8.4
69
+ name: jeweler
70
70
  type: :development
71
71
  prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
72
+ requirement: !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ~>
@@ -111,10 +111,10 @@ required_ruby_version: !ruby/object:Gem::Requirement
111
111
  requirements:
112
112
  - - ! '>='
113
113
  - !ruby/object:Gem::Version
114
- version: '0'
115
114
  segments:
116
115
  - 0
117
- hash: -1664380654006357926
116
+ hash: -1592181993674694658
117
+ version: '0'
118
118
  required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  none: false
120
120
  requirements: