http2 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -8,9 +8,9 @@ GEM
8
8
  git (>= 1.2.5)
9
9
  rake
10
10
  rdoc
11
- json (1.7.5)
12
- rake (0.9.2.2)
13
- rdoc (3.12)
11
+ json (1.7.7)
12
+ rake (10.0.4)
13
+ rdoc (3.12.2)
14
14
  json (~> 1.4)
15
15
  rspec (2.8.0)
16
16
  rspec-core (~> 2.8.0)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.13
1
+ 0.0.14
data/http2.gemspec CHANGED
@@ -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.13"
7
+ s.name = "http2"
8
+ s.version = "0.0.14"
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-05}
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 = "2013-05-02"
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.23"
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
data/lib/http2.rb CHANGED
@@ -26,13 +26,19 @@ class Http2
26
26
 
27
27
  attr_reader :cookies, :args
28
28
 
29
+ VALID_ARGUMENTS_INITIALIZE = [:host, :port, :ssl, :nl, :user_agent, :raise_errors, :follow_redirects, :debug, :encoding_gzip, :autostate]
29
30
  def initialize(args = {})
30
31
  args = {:host => args} if args.is_a?(String)
31
32
  raise "Arguments wasnt a hash." if !args.is_a?(Hash)
32
33
 
34
+ args.each do |key, val|
35
+ raise "Invalid key: '#{key}'." if !VALID_ARGUMENTS_INITIALIZE.include?(key)
36
+ end
37
+
33
38
  @args = args
34
39
  @cookies = {}
35
40
  @debug = @args[:debug]
41
+ @autostate_values = {} if @args[:autostate]
36
42
 
37
43
  require "monitor"
38
44
  @mutex = Monitor.new
@@ -57,6 +63,12 @@ class Http2
57
63
  @uagent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"
58
64
  end
59
65
 
66
+ if !@args.key?(:raise_errors) or @args[:raise_errors]
67
+ @raise_errors = true
68
+ else
69
+ @raise_errors = false
70
+ end
71
+
60
72
  raise "No host was given." if !@args[:host]
61
73
  self.reconnect
62
74
 
@@ -303,16 +315,23 @@ class Http2
303
315
  return praw
304
316
  end
305
317
 
318
+ VALID_ARGUMENTS_POST = [:post, :url]
306
319
  #Posts to a certain page.
307
320
  #===Examples
308
321
  # res = http.post("login.php", {"username" => "John Doe", "password" => 123)
309
322
  def post(args)
323
+ args.each do |key, val|
324
+ raise "Invalid key: '#{key}'." if !VALID_ARGUMENTS_POST.include?(key)
325
+ end
326
+
310
327
  args = self.parse_args(args)
328
+ phash = args[:post] ? args[:post].clone : {}
329
+ autostate_set_on_post_hash(phash) if @args[:autostate]
311
330
 
312
331
  @mutex.synchronize do
313
332
  print "Doing post.\n" if @debug
314
333
 
315
- praw = Http2.post_convert_data(args[:post])
334
+ praw = Http2.post_convert_data(phash)
316
335
 
317
336
  header_str = "POST /#{args[:url]} HTTP/1.1#{@nl}"
318
337
  header_str << self.header_str(self.default_headers(args).merge("Content-Type" => "application/x-www-form-urlencoded", "Content-Length" => praw.length), args)
@@ -333,6 +352,9 @@ class Http2
333
352
  def post_multipart(*args)
334
353
  args = self.parse_args(*args)
335
354
 
355
+ phash = args[:post].clone
356
+ autostate_set_on_post_hash(phash) if @args[:autostate]
357
+
336
358
  #Generate random string.
337
359
  boundary = rand(36**50).to_s(36)
338
360
 
@@ -340,7 +362,7 @@ class Http2
340
362
  require "tempfile"
341
363
 
342
364
  Tempfile.open("http2_post_multipart_tmp_#{boundary}") do |praw|
343
- args[:post].each do |key, val|
365
+ phash.each do |key, val|
344
366
  praw << "--#{boundary}#{@nl}"
345
367
 
346
368
  if val.class.name.to_s == "Tempfile" and val.respond_to?(:original_filename)
@@ -565,15 +587,17 @@ class Http2
565
587
  http = Http2.new(args)
566
588
  return http.get(url)
567
589
  end
568
- elsif resp.args[:code].to_s == "500"
590
+ elsif @raise_errors and resp.args[:code].to_i == 500
569
591
  err = Http2::Errors::Internalserver.new(resp.body)
570
592
  err.response = resp
571
593
  raise err
572
- elsif resp.args[:code].to_s == "403"
594
+ elsif @raise_errors and resp.args[:code].to_i == 403
573
595
  err = Http2::Errors::Noaccess.new(resp.body)
574
596
  err.response = resp
575
597
  raise err
576
598
  else
599
+ autostate_register(resp) if @args[:autostate]
600
+
577
601
  return resp
578
602
  end
579
603
  end
@@ -666,4 +690,28 @@ class Http2
666
690
  raise "Dont know how to read HTTP version: '#{@resp.args[:http_version]}'."
667
691
  end
668
692
  end
693
+
694
+ private
695
+
696
+ #Registers the states from a result.
697
+ def autostate_register(res)
698
+ puts "Http2: Running autostate-register on result." if @debug
699
+ @autostate_values.clear
700
+
701
+ res.body.to_s.scan(/<input type="hidden" name="__(EVENTTARGET|EVENTARGUMENT|VIEWSTATE|LASTFOCUS)" id="(.*?)" value="(.*?)" \/>/) do |match|
702
+ name = "__#{match[0]}"
703
+ id = match[1]
704
+ value = match[2]
705
+
706
+ puts "Http2: Registered autostate-value with name '#{name}' and value '#{value}'." if @debug
707
+ @autostate_values[name] = Http2::Utils.urldec(value)
708
+ end
709
+
710
+ raise "No states could be found." if @autostate_values.empty?
711
+ end
712
+
713
+ #Sets the states on the given post-hash.
714
+ def autostate_set_on_post_hash(phash)
715
+ phash.merge!(@autostate_values)
716
+ end
669
717
  end
data/spec/http2_spec.rb CHANGED
@@ -108,6 +108,6 @@ describe "Http2" do
108
108
 
109
109
  it "should be able to convert URL's to 'is.gd'-short-urls" do
110
110
  isgd = Http2.isgdlink("https://github.com/kaspernj/http2")
111
- raise "Expected isgd-var to be 'http://is.gd/Z7kHSl' but it wasnt: '#{isgd}'." if isgd != "http://is.gd/Z7kHSl"
111
+ raise "Expected isgd-var to be valid but it wasnt: '#{isgd}'." if !isgd.match(/^http:\/\/is\.gd\/([A-z\d]+)$/)
112
112
  end
113
113
  end
metadata CHANGED
@@ -1,61 +1,80 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
5
4
  prerelease:
5
+ version: 0.0.14
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kasper Johansen
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-05 00:00:00.000000000 +02:00
13
- default_executable:
12
+ date: 2013-05-02 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
- name: rspec
17
- requirement: &21138800 !ruby/object:Gem::Requirement
15
+ version_requirements: !ruby/object:Gem::Requirement
18
16
  none: false
19
17
  requirements:
20
18
  - - ~>
21
19
  - !ruby/object:Gem::Version
22
20
  version: 2.8.0
21
+ name: rspec
23
22
  type: :development
24
23
  prerelease: false
25
- version_requirements: *21138800
24
+ requirement: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.8.0
26
30
  - !ruby/object:Gem::Dependency
27
- name: rdoc
28
- requirement: &21138280 !ruby/object:Gem::Requirement
31
+ version_requirements: !ruby/object:Gem::Requirement
29
32
  none: false
30
33
  requirements:
31
34
  - - ~>
32
35
  - !ruby/object:Gem::Version
33
36
  version: '3.12'
37
+ name: rdoc
34
38
  type: :development
35
39
  prerelease: false
36
- version_requirements: *21138280
40
+ requirement: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.12'
37
46
  - !ruby/object:Gem::Dependency
38
- name: bundler
39
- requirement: &21135240 !ruby/object:Gem::Requirement
47
+ version_requirements: !ruby/object:Gem::Requirement
40
48
  none: false
41
49
  requirements:
42
50
  - - ! '>='
43
51
  - !ruby/object:Gem::Version
44
52
  version: 1.0.0
53
+ name: bundler
45
54
  type: :development
46
55
  prerelease: false
47
- version_requirements: *21135240
56
+ requirement: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
48
62
  - !ruby/object:Gem::Dependency
49
- name: jeweler
50
- requirement: &21134660 !ruby/object:Gem::Requirement
63
+ version_requirements: !ruby/object:Gem::Requirement
51
64
  none: false
52
65
  requirements:
53
66
  - - ~>
54
67
  - !ruby/object:Gem::Version
55
68
  version: 1.8.4
69
+ name: jeweler
56
70
  type: :development
57
71
  prerelease: false
58
- version_requirements: *21134660
72
+ requirement: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.4
59
78
  description: A lightweight framework for doing http-connections in Ruby. Supports
60
79
  cookies, keep-alive, compressing and much more.
61
80
  email: k@spernj.org
@@ -80,7 +99,6 @@ files:
80
99
  - lib/http2.rb
81
100
  - spec/http2_spec.rb
82
101
  - spec/spec_helper.rb
83
- has_rdoc: true
84
102
  homepage: http://github.com/kaspernj/http2
85
103
  licenses:
86
104
  - MIT
@@ -93,10 +111,10 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
111
  requirements:
94
112
  - - ! '>='
95
113
  - !ruby/object:Gem::Version
96
- version: '0'
97
114
  segments:
98
115
  - 0
99
- hash: -1035862485575419894
116
+ hash: 2678064752276625690
117
+ version: '0'
100
118
  required_rubygems_version: !ruby/object:Gem::Requirement
101
119
  none: false
102
120
  requirements:
@@ -105,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
123
  version: '0'
106
124
  requirements: []
107
125
  rubyforge_project:
108
- rubygems_version: 1.6.2
126
+ rubygems_version: 1.8.23
109
127
  signing_key:
110
128
  specification_version: 3
111
129
  summary: A lightweight framework for doing http-connections in Ruby. Supports cookies,