http2 0.0.14 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/Gemfile.lock +2 -2
  2. data/README.rdoc +27 -1
  3. data/VERSION +1 -1
  4. data/http2.gemspec +2 -2
  5. data/lib/http2.rb +56 -14
  6. metadata +3 -3
data/Gemfile.lock CHANGED
@@ -8,8 +8,8 @@ GEM
8
8
  git (>= 1.2.5)
9
9
  rake
10
10
  rdoc
11
- json (1.7.7)
12
- rake (10.0.4)
11
+ json (1.8.0)
12
+ rake (10.1.0)
13
13
  rdoc (3.12.2)
14
14
  json (~> 1.4)
15
15
  rspec (2.8.0)
data/README.rdoc CHANGED
@@ -1,6 +1,32 @@
1
1
  = http2
2
2
 
3
- Description goes here.
3
+ Example of usage:
4
+
5
+ require "rubygems"
6
+ require "http2"
7
+
8
+ Http2.new(:host => "www.google.dk") do |http|
9
+ #Get-request.
10
+ res = http.get("path/to/something")
11
+ puts res.body
12
+ puts "All response-headers: #{res.headers}"
13
+ puts "Specific header: #{res.header("HeaderName")}"
14
+
15
+ #Post-request.
16
+ res = http.post(:url => "path/to/something", :post => {
17
+ "some_post_val" => "some_value"
18
+ })
19
+
20
+ #Post-multipart (upload).
21
+ res = http.post_multipart(:url => "path/to/something", :post => {
22
+ "test_file1" => {
23
+ :fpath => fpath,
24
+ :filename => "specfile"
25
+ }
26
+ })
27
+
28
+ puts "Cookies until now: #{http.cookies}"
29
+ end
4
30
 
5
31
  == Contributing to http2
6
32
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.14
1
+ 0.0.15
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.14"
8
+ s.version = "0.0.15"
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-05-02"
12
+ s.date = "2013-06-21"
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/lib/http2.rb CHANGED
@@ -26,7 +26,7 @@ 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
+ VALID_ARGUMENTS_INITIALIZE = [:host, :port, :ssl, :nl, :user_agent, :raise_errors, :follow_redirects, :debug, :encoding_gzip, :autostate, :basic_auth, :extra_headers]
30
30
  def initialize(args = {})
31
31
  args = {:host => args} if args.is_a?(String)
32
32
  raise "Arguments wasnt a hash." if !args.is_a?(Hash)
@@ -40,7 +40,7 @@ class Http2
40
40
  @debug = @args[:debug]
41
41
  @autostate_values = {} if @args[:autostate]
42
42
 
43
- require "monitor"
43
+ require "monitor" unless ::Kernel.const_defined?(:Monitor)
44
44
  @mutex = Monitor.new
45
45
 
46
46
  if !@args[:port]
@@ -81,6 +81,20 @@ class Http2
81
81
  end
82
82
  end
83
83
 
84
+ #Closes current connection if any, changes the arguments on the object and reconnects keeping all cookies and other stuff intact.
85
+ def change(args)
86
+ self.close
87
+ @args.merge!(args)
88
+ self.reconnect
89
+ end
90
+
91
+ #Closes the current connection if any.
92
+ def close
93
+ @sock.close if @sock and !@sock.closed?
94
+ @sock_ssl.close if @sock_ssl and !@sock_ssl.closed?
95
+ @sock_plain.close if @sock_plain and !@sock_plain.closed?
96
+ end
97
+
84
98
  #Returns boolean based on the if the object is connected and the socket is working.
85
99
  #===Examples
86
100
  # puts "Socket is working." if http.socket_working?
@@ -161,7 +175,7 @@ class Http2
161
175
 
162
176
  if @args[:ssl]
163
177
  print "Http2: Initializing SSL.\n" if @debug
164
- require "openssl"
178
+ require "openssl" unless ::Kernel.const_defined?(:OpenSSL)
165
179
 
166
180
  ssl_context = OpenSSL::SSL::SSLContext.new
167
181
  #ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
@@ -204,7 +218,13 @@ class Http2
204
218
  def get(args)
205
219
  args = self.parse_args(args)
206
220
 
207
- header_str = "GET /#{args[:url]} HTTP/1.1#{@nl}"
221
+ if args.key?(:method) && args[:method]
222
+ method = args[:method].to_s.upcase
223
+ else
224
+ method = "GET"
225
+ end
226
+
227
+ header_str = "#{method} /#{args[:url]} HTTP/1.1#{@nl}"
208
228
  header_str << self.header_str(self.default_headers(args), args)
209
229
  header_str << @nl
210
230
 
@@ -221,6 +241,10 @@ class Http2
221
241
  end
222
242
  end
223
243
 
244
+ def delete(args)
245
+ return self.get(args.merge(:method => :delete))
246
+ end
247
+
224
248
  #Tries to write a string to the socket. If it fails it reconnects and tries again.
225
249
  def write(str)
226
250
  #Reset variables.
@@ -265,7 +289,16 @@ class Http2
265
289
  end
266
290
 
267
291
  if @args[:basic_auth]
268
- headers["Authorization"] = "Basic #{Base64.encode64("#{@args[:basic_auth][:user]}:#{@args[:basic_auth][:passwd]}")}"
292
+ require "base64" unless ::Kernel.const_defined?(:Base64)
293
+ headers["Authorization"] = "Basic #{Base64.encode64("#{@args[:basic_auth][:user]}:#{@args[:basic_auth][:passwd]}").strip}"
294
+ end
295
+
296
+ if @args[:extra_headers]
297
+ headers.merge!(@args[:extra_headers])
298
+ end
299
+
300
+ if args[:headers]
301
+ headers.merge!(args[:headers])
269
302
  end
270
303
 
271
304
  return headers
@@ -315,7 +348,7 @@ class Http2
315
348
  return praw
316
349
  end
317
350
 
318
- VALID_ARGUMENTS_POST = [:post, :url]
351
+ VALID_ARGUMENTS_POST = [:post, :url, :headers, :json]
319
352
  #Posts to a certain page.
320
353
  #===Examples
321
354
  # res = http.post("login.php", {"username" => "John Doe", "password" => 123)
@@ -325,21 +358,30 @@ class Http2
325
358
  end
326
359
 
327
360
  args = self.parse_args(args)
328
- phash = args[:post] ? args[:post].clone : {}
329
- autostate_set_on_post_hash(phash) if @args[:autostate]
361
+ content_type = "application/x-www-form-urlencoded"
362
+
363
+ if args[:json]
364
+ require "json" unless ::Kernel.const_defined?(:JSON)
365
+ praw = JSON.generate(args[:json])
366
+ content_type = "application/json"
367
+ elsif args[:post].is_a?(String)
368
+ praw = args[:post]
369
+ else
370
+ phash = args[:post] ? args[:post].clone : {}
371
+ autostate_set_on_post_hash(phash) if @args[:autostate]
372
+ praw = Http2.post_convert_data(phash)
373
+ end
330
374
 
331
375
  @mutex.synchronize do
332
- print "Doing post.\n" if @debug
333
-
334
- praw = Http2.post_convert_data(phash)
376
+ puts "Doing post." if @debug
335
377
 
336
378
  header_str = "POST /#{args[:url]} HTTP/1.1#{@nl}"
337
- header_str << self.header_str(self.default_headers(args).merge("Content-Type" => "application/x-www-form-urlencoded", "Content-Length" => praw.length), args)
379
+ header_str << self.header_str({"Content-Length" => praw.length, "Content-Type" => content_type}.merge(self.default_headers(args)), args)
338
380
  header_str << @nl
339
381
  header_str << praw
340
382
  header_str << @nl
341
383
 
342
- print "Header str: #{header_str}\n" if @debug
384
+ puts "Header str: #{header_str}" if @debug
343
385
 
344
386
  self.write(header_str)
345
387
  return self.read_response(args)
@@ -569,7 +611,7 @@ class Http2
569
611
 
570
612
  raise "No status-code was received from the server. Headers: '#{resp.headers}' Body: '#{resp.args[:body]}'." if !resp.args[:code]
571
613
 
572
- if resp.args[:code].to_s == "302" and resp.header?("location") and (!@args.key?(:follow_redirects) or @args[:follow_redirects])
614
+ if (resp.args[:code].to_s == "302" || resp.args[:code].to_s == "307") and resp.header?("location") and (!@args.key?(:follow_redirects) or @args[:follow_redirects])
573
615
  require "uri"
574
616
  uri = URI.parse(resp.header("location"))
575
617
  url = uri.path
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: http2
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.14
5
+ version: 0.0.15
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-05-02 00:00:00.000000000 Z
12
+ date: 2013-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  version_requirements: !ruby/object:Gem::Requirement
@@ -113,7 +113,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
113
  - !ruby/object:Gem::Version
114
114
  segments:
115
115
  - 0
116
- hash: 2678064752276625690
116
+ hash: -3116192511012734724
117
117
  version: '0'
118
118
  required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  none: false