z-http-request 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gemtest +0 -0
- data/.gitignore +10 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +8 -0
- data/Gemfile +17 -0
- data/README.md +38 -0
- data/Rakefile +3 -0
- data/benchmarks/clients.rb +170 -0
- data/benchmarks/em-excon.rb +87 -0
- data/benchmarks/em-profile.gif +0 -0
- data/benchmarks/em-profile.txt +65 -0
- data/benchmarks/server.rb +48 -0
- data/examples/.gitignore +1 -0
- data/examples/digest_auth/client.rb +25 -0
- data/examples/digest_auth/server.rb +28 -0
- data/examples/fetch.rb +30 -0
- data/examples/fibered-http.rb +51 -0
- data/examples/multi.rb +25 -0
- data/examples/oauth-tweet.rb +35 -0
- data/examples/socks5.rb +23 -0
- data/lib/z-http/client.rb +318 -0
- data/lib/z-http/core_ext/bytesize.rb +6 -0
- data/lib/z-http/decoders.rb +254 -0
- data/lib/z-http/http_client_options.rb +51 -0
- data/lib/z-http/http_connection.rb +214 -0
- data/lib/z-http/http_connection_options.rb +44 -0
- data/lib/z-http/http_encoding.rb +142 -0
- data/lib/z-http/http_header.rb +83 -0
- data/lib/z-http/http_status_codes.rb +57 -0
- data/lib/z-http/middleware/digest_auth.rb +112 -0
- data/lib/z-http/middleware/json_response.rb +15 -0
- data/lib/z-http/middleware/oauth.rb +40 -0
- data/lib/z-http/middleware/oauth2.rb +28 -0
- data/lib/z-http/multi.rb +57 -0
- data/lib/z-http/request.rb +23 -0
- data/lib/z-http/version.rb +5 -0
- data/lib/z-http-request.rb +1 -0
- data/lib/z-http.rb +18 -0
- data/spec/client_spec.rb +892 -0
- data/spec/digest_auth_spec.rb +48 -0
- data/spec/dns_spec.rb +44 -0
- data/spec/encoding_spec.rb +49 -0
- data/spec/external_spec.rb +150 -0
- data/spec/fixtures/google.ca +16 -0
- data/spec/fixtures/gzip-sample.gz +0 -0
- data/spec/gzip_spec.rb +68 -0
- data/spec/helper.rb +30 -0
- data/spec/middleware_spec.rb +143 -0
- data/spec/multi_spec.rb +104 -0
- data/spec/pipelining_spec.rb +66 -0
- data/spec/redirect_spec.rb +321 -0
- data/spec/socksify_proxy_spec.rb +60 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/ssl_spec.rb +20 -0
- data/spec/stallion.rb +296 -0
- data/spec/stub_server.rb +42 -0
- data/z-http-request.gemspec +33 -0
- metadata +248 -0
@@ -0,0 +1,28 @@
|
|
1
|
+
module ZMachine
|
2
|
+
module Middleware
|
3
|
+
class OAuth2
|
4
|
+
include ZMachine::HttpEncoding
|
5
|
+
attr_accessor :access_token
|
6
|
+
|
7
|
+
def initialize(opts={})
|
8
|
+
self.access_token = opts[:access_token] or raise "No :access_token provided"
|
9
|
+
end
|
10
|
+
|
11
|
+
def request(client, head, body)
|
12
|
+
uri = client.req.uri.dup
|
13
|
+
update_uri! uri
|
14
|
+
client.req.set_uri uri
|
15
|
+
|
16
|
+
[head, body]
|
17
|
+
end
|
18
|
+
|
19
|
+
def update_uri!(uri)
|
20
|
+
if uri.query.nil?
|
21
|
+
uri.query = encode_param(:access_token, access_token)
|
22
|
+
else
|
23
|
+
uri.query += "&#{encode_param(:access_token, access_token)}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/z-http/multi.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
module ZMachine
|
2
|
+
|
3
|
+
# ZMachine based Multi request client, based on a streaming HTTPRequest class,
|
4
|
+
# which allows you to open multiple parallel connections and return only when all
|
5
|
+
# of them finish. (i.e. ideal for parallelizing workloads)
|
6
|
+
#
|
7
|
+
# == Example
|
8
|
+
#
|
9
|
+
# ZMachine.run {
|
10
|
+
#
|
11
|
+
# multi = ZMachine::MultiRequest.new
|
12
|
+
#
|
13
|
+
# # add multiple requests to the multi-handler
|
14
|
+
# multi.add(:a, ZMachine::HttpRequest.new('http://www.google.com/').get)
|
15
|
+
# multi.add(:b, ZMachine::HttpRequest.new('http://www.yahoo.com/').get)
|
16
|
+
#
|
17
|
+
# multi.callback {
|
18
|
+
# p multi.responses[:callback]
|
19
|
+
# p multi.responses[:errback]
|
20
|
+
#
|
21
|
+
# ZMachine.stop
|
22
|
+
# }
|
23
|
+
# }
|
24
|
+
#
|
25
|
+
|
26
|
+
class MultiRequest
|
27
|
+
include ZMachine::Deferrable
|
28
|
+
|
29
|
+
attr_reader :requests, :responses
|
30
|
+
|
31
|
+
def initialize
|
32
|
+
@requests = {}
|
33
|
+
@responses = {:callback => {}, :errback => {}}
|
34
|
+
end
|
35
|
+
|
36
|
+
def add(name, conn)
|
37
|
+
raise 'Duplicate Multi key' if @requests.key? name
|
38
|
+
|
39
|
+
@requests[name] = conn
|
40
|
+
|
41
|
+
conn.callback { @responses[:callback][name] = conn; check_progress }
|
42
|
+
conn.errback { @responses[:errback][name] = conn; check_progress }
|
43
|
+
end
|
44
|
+
|
45
|
+
def finished?
|
46
|
+
(@responses[:callback].size + @responses[:errback].size) == @requests.size
|
47
|
+
end
|
48
|
+
|
49
|
+
protected
|
50
|
+
|
51
|
+
# invoke callback if all requests have completed
|
52
|
+
def check_progress
|
53
|
+
succeed(self) if finished?
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ZMachine
|
2
|
+
class HttpRequest
|
3
|
+
@middleware = []
|
4
|
+
|
5
|
+
def self.new(uri, options={})
|
6
|
+
uri = uri.clone
|
7
|
+
connopt = HttpConnectionOptions.new(uri, options)
|
8
|
+
|
9
|
+
c = HttpConnection.new
|
10
|
+
c.connopts = connopt
|
11
|
+
c.uri = uri
|
12
|
+
c
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.use(klass, *args, &block)
|
16
|
+
@middleware << klass.new(*args, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.middleware
|
20
|
+
@middleware
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'z-http'
|
data/lib/z-http.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'zmachine'
|
2
|
+
require 'addressable/uri'
|
3
|
+
require 'http/parser'
|
4
|
+
|
5
|
+
require 'base64'
|
6
|
+
require 'socket'
|
7
|
+
|
8
|
+
require 'z-http/core_ext/bytesize'
|
9
|
+
require 'z-http/http_connection'
|
10
|
+
require 'z-http/http_header'
|
11
|
+
require 'z-http/http_encoding'
|
12
|
+
require 'z-http/http_status_codes'
|
13
|
+
require 'z-http/http_client_options'
|
14
|
+
require 'z-http/http_connection_options'
|
15
|
+
require 'z-http/client'
|
16
|
+
require 'z-http/multi'
|
17
|
+
require 'z-http/request'
|
18
|
+
require 'z-http/decoders'
|