glebtv-httpclient 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.rdoc +108 -0
- data/bin/httpclient +65 -0
- data/lib/glebtv-httpclient.rb +1 -0
- data/lib/hexdump.rb +50 -0
- data/lib/http-access2/cookie.rb +1 -0
- data/lib/http-access2/http.rb +1 -0
- data/lib/http-access2.rb +55 -0
- data/lib/httpclient/auth.rb +899 -0
- data/lib/httpclient/cacert.p7s +1912 -0
- data/lib/httpclient/connection.rb +88 -0
- data/lib/httpclient/cookie.rb +438 -0
- data/lib/httpclient/http.rb +1050 -0
- data/lib/httpclient/include_client.rb +83 -0
- data/lib/httpclient/session.rb +1031 -0
- data/lib/httpclient/ssl_config.rb +403 -0
- data/lib/httpclient/timeout.rb +140 -0
- data/lib/httpclient/util.rb +186 -0
- data/lib/httpclient/version.rb +3 -0
- data/lib/httpclient.rb +1157 -0
- data/lib/oauthclient.rb +110 -0
- data/sample/async.rb +8 -0
- data/sample/auth.rb +11 -0
- data/sample/cookie.rb +18 -0
- data/sample/dav.rb +103 -0
- data/sample/howto.rb +49 -0
- data/sample/oauth_buzz.rb +57 -0
- data/sample/oauth_friendfeed.rb +59 -0
- data/sample/oauth_twitter.rb +61 -0
- data/sample/ssl/0cert.pem +22 -0
- data/sample/ssl/0key.pem +30 -0
- data/sample/ssl/1000cert.pem +19 -0
- data/sample/ssl/1000key.pem +18 -0
- data/sample/ssl/htdocs/index.html +10 -0
- data/sample/ssl/ssl_client.rb +22 -0
- data/sample/ssl/webrick_httpsd.rb +29 -0
- data/sample/stream.rb +21 -0
- data/sample/thread.rb +27 -0
- data/sample/wcat.rb +21 -0
- data/test/ca-chain.cert +44 -0
- data/test/ca.cert +23 -0
- data/test/client.cert +19 -0
- data/test/client.key +15 -0
- data/test/helper.rb +129 -0
- data/test/htdigest +1 -0
- data/test/htpasswd +2 -0
- data/test/runner.rb +2 -0
- data/test/server.cert +19 -0
- data/test/server.key +15 -0
- data/test/sslsvr.rb +65 -0
- data/test/subca.cert +21 -0
- data/test/test_auth.rb +321 -0
- data/test/test_cookie.rb +412 -0
- data/test/test_hexdump.rb +14 -0
- data/test/test_http-access2.rb +507 -0
- data/test/test_httpclient.rb +1801 -0
- data/test/test_include_client.rb +52 -0
- data/test/test_ssl.rb +235 -0
- metadata +102 -0
@@ -0,0 +1,83 @@
|
|
1
|
+
# It is useful to re-use a HTTPClient instance for multiple requests, to
|
2
|
+
# re-use HTTP 1.1 persistent connections.
|
3
|
+
#
|
4
|
+
# To do that, you sometimes want to store an HTTPClient instance in a global/
|
5
|
+
# class variable location, so it can be accessed and re-used.
|
6
|
+
#
|
7
|
+
# This mix-in makes it easy to create class-level access to one or more
|
8
|
+
# HTTPClient instances. The HTTPClient instances are lazily initialized
|
9
|
+
# on first use (to, for instance, avoid interfering with WebMock/VCR),
|
10
|
+
# and are initialized in a thread-safe manner. Note that a
|
11
|
+
# HTTPClient, once initialized, is safe for use in multiple threads.
|
12
|
+
#
|
13
|
+
# Note that you `extend` HTTPClient::IncludeClient, not `include.
|
14
|
+
#
|
15
|
+
# require 'httpclient/include_client'
|
16
|
+
# class Widget
|
17
|
+
# extend HTTPClient::IncludeClient
|
18
|
+
#
|
19
|
+
# include_http_client
|
20
|
+
# # and/or, specify more stuff
|
21
|
+
# include_http_client('http://myproxy:8080', :method_name => :my_client) do |client|
|
22
|
+
# # any init you want
|
23
|
+
# client.set_cookie_store nil
|
24
|
+
# client.
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# That creates two HTTPClient instances available at the class level.
|
29
|
+
# The first will be available from Widget.http_client (default method
|
30
|
+
# name for `include_http_client`), with default initialization.
|
31
|
+
#
|
32
|
+
# The second will be available at Widget.my_client, with the init arguments
|
33
|
+
# provided, further initialized by the block provided.
|
34
|
+
#
|
35
|
+
# In addition to a class-level method, for convenience instance-level methods
|
36
|
+
# are also provided. Widget.http_client is identical to Widget.new.http_client
|
37
|
+
#
|
38
|
+
#
|
39
|
+
class HTTPClient
|
40
|
+
module IncludeClient
|
41
|
+
|
42
|
+
|
43
|
+
def include_http_client(*args, &block)
|
44
|
+
# We're going to dynamically define a class
|
45
|
+
# to hold our state, namespaced, as well as possibly dynamic
|
46
|
+
# name of cover method.
|
47
|
+
method_name = (args.last.delete(:method_name) if args.last.kind_of? Hash) || :http_client
|
48
|
+
args.pop if args.last == {} # if last arg was named methods now empty, remove it.
|
49
|
+
|
50
|
+
# By the amazingness of closures, we can create these things
|
51
|
+
# in local vars here and use em in our method, we don't even
|
52
|
+
# need iVars for state.
|
53
|
+
client_instance = nil
|
54
|
+
client_mutex = Mutex.new
|
55
|
+
client_args = args
|
56
|
+
client_block = block
|
57
|
+
|
58
|
+
# to define a _class method_ on the specific class that's currently
|
59
|
+
# `self`, we have to use this bit of metaprogramming, sorry.
|
60
|
+
(class << self; self ; end).instance_eval do
|
61
|
+
define_method(method_name) do
|
62
|
+
# implementation copied from ruby stdlib singleton
|
63
|
+
# to create this global obj thread-safely.
|
64
|
+
return client_instance if client_instance
|
65
|
+
client_mutex.synchronize do
|
66
|
+
return client_instance if client_instance
|
67
|
+
# init HTTPClient with specified args/block
|
68
|
+
client_instance = HTTPClient.new(*client_args)
|
69
|
+
client_block.call(client_instance) if client_block
|
70
|
+
end
|
71
|
+
return client_instance
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# And for convenience, an _instance method_ on the class that just
|
76
|
+
# delegates to the class method.
|
77
|
+
define_method(method_name) do
|
78
|
+
self.class.send(method_name)
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|