httpclient-jgraichen 2.3.4.2
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.txt +759 -0
- data/bin/httpclient +65 -0
- data/lib/hexdump.rb +50 -0
- data/lib/http-access2.rb +55 -0
- data/lib/http-access2/cookie.rb +1 -0
- data/lib/http-access2/http.rb +1 -0
- data/lib/httpclient.rb +1156 -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 +1046 -0
- data/lib/httpclient/include_client.rb +83 -0
- data/lib/httpclient/session.rb +1028 -0
- data/lib/httpclient/ssl_config.rb +405 -0
- data/lib/httpclient/timeout.rb +140 -0
- data/lib/httpclient/util.rb +178 -0
- data/lib/httpclient/version.rb +3 -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 +348 -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 +1783 -0
- data/test/test_include_client.rb +52 -0
- data/test/test_ssl.rb +235 -0
- metadata +100 -0
@@ -0,0 +1,178 @@
|
|
1
|
+
# HTTPClient - HTTP client library.
|
2
|
+
# Copyright (C) 2000-2012 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
|
3
|
+
#
|
4
|
+
# This program is copyrighted free software by NAKAMURA, Hiroshi. You can
|
5
|
+
# redistribute it and/or modify it under the same terms of Ruby's license;
|
6
|
+
# either the dual license version in 2003, or any later version.
|
7
|
+
|
8
|
+
|
9
|
+
unless ''.respond_to?(:bytesize)
|
10
|
+
class String
|
11
|
+
alias bytesize size
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
if RUBY_VERSION < "1.9.3"
|
16
|
+
require 'uri'
|
17
|
+
module URI
|
18
|
+
class Generic
|
19
|
+
def hostname
|
20
|
+
v = self.host
|
21
|
+
/\A\[(.*)\]\z/ =~ v ? $1 : v
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class HTTPClient
|
28
|
+
|
29
|
+
|
30
|
+
# A module for common function.
|
31
|
+
module Util
|
32
|
+
|
33
|
+
# URI abstraction; Addressable::URI or URI
|
34
|
+
require 'uri'
|
35
|
+
begin
|
36
|
+
require 'addressable/uri'
|
37
|
+
# Older versions doesn't have #default_port
|
38
|
+
unless Addressable::URI.instance_methods.include?(:default_port) # 1.9 only
|
39
|
+
raise LoadError
|
40
|
+
end
|
41
|
+
class AddressableURI < Addressable::URI
|
42
|
+
# Overwrites the original definition just for one line...
|
43
|
+
def authority
|
44
|
+
self.host && @authority ||= (begin
|
45
|
+
authority = ""
|
46
|
+
if self.userinfo != nil
|
47
|
+
authority << "#{self.userinfo}@"
|
48
|
+
end
|
49
|
+
authority << self.host
|
50
|
+
if self.port != self.default_port # ...HERE! Compares with default_port because self.port is not nil in this wrapper.
|
51
|
+
authority << ":#{self.port}"
|
52
|
+
end
|
53
|
+
authority
|
54
|
+
end)
|
55
|
+
end
|
56
|
+
|
57
|
+
# HTTPClient expects urify("http://foo/").port to be not nil but 80 like URI.
|
58
|
+
def port
|
59
|
+
super || default_port
|
60
|
+
end
|
61
|
+
|
62
|
+
# Captured from uri/generic.rb
|
63
|
+
def hostname
|
64
|
+
v = self.host
|
65
|
+
/\A\[(.*)\]\z/ =~ v ? $1 : v
|
66
|
+
end
|
67
|
+
end
|
68
|
+
AddressableEnabled = true
|
69
|
+
rescue LoadError
|
70
|
+
AddressableEnabled = false
|
71
|
+
end
|
72
|
+
|
73
|
+
# Keyword argument helper.
|
74
|
+
# args:: given arguments.
|
75
|
+
# *field:: a list of arguments to be extracted.
|
76
|
+
#
|
77
|
+
# You can extract 3 arguments (a, b, c) with:
|
78
|
+
#
|
79
|
+
# include Util
|
80
|
+
# def my_method(*args)
|
81
|
+
# a, b, c = keyword_argument(args, :a, :b, :c)
|
82
|
+
# ...
|
83
|
+
# end
|
84
|
+
# my_method(1, 2, 3)
|
85
|
+
# my_method(:b => 2, :a = 1)
|
86
|
+
#
|
87
|
+
# instead of;
|
88
|
+
#
|
89
|
+
# def my_method(a, b, c)
|
90
|
+
# ...
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
def keyword_argument(args, *field)
|
94
|
+
if args.size == 1 and Hash === args[0]
|
95
|
+
h = args[0]
|
96
|
+
if field.any? { |f| h.key?(f) }
|
97
|
+
return h.values_at(*field)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
args
|
101
|
+
end
|
102
|
+
|
103
|
+
# Keyword argument to hash helper.
|
104
|
+
# args:: given arguments.
|
105
|
+
# *field:: a list of arguments to be extracted.
|
106
|
+
#
|
107
|
+
# Returns hash which has defined keys. When a Hash given, returns it
|
108
|
+
# including undefined keys. When an Array given, returns a Hash which only
|
109
|
+
# includes defined keys.
|
110
|
+
def argument_to_hash(args, *field)
|
111
|
+
return nil if args.empty?
|
112
|
+
if args.size == 1 and Hash === args[0]
|
113
|
+
h = args[0]
|
114
|
+
if field.any? { |f| h.key?(f) }
|
115
|
+
return h
|
116
|
+
end
|
117
|
+
end
|
118
|
+
h = {}
|
119
|
+
field.each_with_index do |e, idx|
|
120
|
+
h[e] = args[idx]
|
121
|
+
end
|
122
|
+
h
|
123
|
+
end
|
124
|
+
|
125
|
+
# Gets an URI instance.
|
126
|
+
def urify(uri)
|
127
|
+
if uri.nil?
|
128
|
+
nil
|
129
|
+
elsif uri.is_a?(URI)
|
130
|
+
uri
|
131
|
+
elsif AddressableEnabled
|
132
|
+
AddressableURI.parse(uri.to_s)
|
133
|
+
else
|
134
|
+
URI.parse(uri.to_s)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
module_function :urify
|
138
|
+
|
139
|
+
# Returns true if the given 2 URIs have a part_of relationship.
|
140
|
+
# * the same scheme
|
141
|
+
# * the same host String (no host resolution or IP-addr conversion)
|
142
|
+
# * the same port number
|
143
|
+
# * target URI's path starts with base URI's path.
|
144
|
+
def uri_part_of(uri, part)
|
145
|
+
((uri.scheme == part.scheme) and
|
146
|
+
(uri.host == part.host) and
|
147
|
+
(uri.port == part.port) and
|
148
|
+
uri.path.upcase.index(part.path.upcase) == 0)
|
149
|
+
end
|
150
|
+
module_function :uri_part_of
|
151
|
+
|
152
|
+
# Returns parent directory URI of the given URI.
|
153
|
+
def uri_dirname(uri)
|
154
|
+
uri = uri.clone
|
155
|
+
uri.path = uri.path.sub(/\/[^\/]*\z/, '/')
|
156
|
+
uri
|
157
|
+
end
|
158
|
+
module_function :uri_dirname
|
159
|
+
|
160
|
+
# Finds a value of a Hash.
|
161
|
+
def hash_find_value(hash, &block)
|
162
|
+
v = hash.find(&block)
|
163
|
+
v ? v[1] : nil
|
164
|
+
end
|
165
|
+
module_function :hash_find_value
|
166
|
+
|
167
|
+
# Checks if the given URI is https.
|
168
|
+
def https?(uri)
|
169
|
+
uri.scheme && uri.scheme.downcase == 'https'
|
170
|
+
end
|
171
|
+
|
172
|
+
def http?(uri)
|
173
|
+
uri.scheme && uri.scheme.downcase == 'http'
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
end
|
data/lib/oauthclient.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# HTTPClient - HTTP client library.
|
2
|
+
# Copyright (C) 2000-2009 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
|
3
|
+
#
|
4
|
+
# This program is copyrighted free software by NAKAMURA, Hiroshi. You can
|
5
|
+
# redistribute it and/or modify it under the same terms of Ruby's license;
|
6
|
+
# either the dual license version in 2003, or any later version.
|
7
|
+
|
8
|
+
|
9
|
+
require 'httpclient'
|
10
|
+
|
11
|
+
module HTTP
|
12
|
+
class Message
|
13
|
+
attr_accessor :oauth_params
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
# OAuthClient provides OAuth related methods in addition to HTTPClient.
|
19
|
+
#
|
20
|
+
# See sample/ dir how to use OAuthClient. There are sample clients for Twitter,
|
21
|
+
# FriendFeed and Google Buzz.
|
22
|
+
class OAuthClient < HTTPClient
|
23
|
+
|
24
|
+
# HTTPClient::OAuth::Config:: OAuth configurator.
|
25
|
+
attr_accessor :oauth_config
|
26
|
+
|
27
|
+
# Creates a OAuthClient instance which provides OAuth related methods in
|
28
|
+
# addition to HTTPClient.
|
29
|
+
#
|
30
|
+
# Method signature is as same as HTTPClient. See HTTPClient.new
|
31
|
+
def initialize(*arg)
|
32
|
+
super
|
33
|
+
@oauth_config = HTTPClient::OAuth::Config.new
|
34
|
+
self.www_auth.oauth.set_config(nil, @oauth_config)
|
35
|
+
self.www_auth.oauth.challenge(nil)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Get request token.
|
39
|
+
# uri:: URI for request token.
|
40
|
+
# callback:: callback String. This can be nil for OAuth 1.0a
|
41
|
+
# param:: Additional query parameter Hash.
|
42
|
+
#
|
43
|
+
# It returns a HTTP::Message instance as a response. When the request is made
|
44
|
+
# successfully, you can retrieve a pair of request token and secret like
|
45
|
+
# following;
|
46
|
+
# res = client.get_request_token(...)
|
47
|
+
# token = res.oauth_params['oauth_token']
|
48
|
+
# secret = res.oauth_params['oauth_token_secret']
|
49
|
+
def get_request_token(uri, callback = nil, param = nil)
|
50
|
+
oauth_config.token = nil
|
51
|
+
oauth_config.secret = nil
|
52
|
+
oauth_config.callback = callback
|
53
|
+
oauth_config.verifier = nil
|
54
|
+
res = request(oauth_config.http_method, uri, param)
|
55
|
+
filter_response(res)
|
56
|
+
res
|
57
|
+
end
|
58
|
+
|
59
|
+
# Get access token.
|
60
|
+
# uri:: URI for request token.
|
61
|
+
# request_token:: a request token String. See get_access_token.
|
62
|
+
# request_token_secret:: a request secret String. See get_access_token.
|
63
|
+
# verifier:: a verifier tag String.
|
64
|
+
#
|
65
|
+
# When the request succeeds and the server returns a pair of access token
|
66
|
+
# and secret, oauth_config.token and oauth_token.secret are updated with
|
67
|
+
# the access token. Then you can call OAuthClient#get, #post, #delete, etc.
|
68
|
+
# All requests are signed.
|
69
|
+
def get_access_token(uri, request_token, request_token_secret, verifier = nil)
|
70
|
+
oauth_config.token = request_token
|
71
|
+
oauth_config.secret = request_token_secret
|
72
|
+
oauth_config.callback = nil
|
73
|
+
oauth_config.verifier = verifier
|
74
|
+
res = request(oauth_config.http_method, uri)
|
75
|
+
filter_response(res)
|
76
|
+
oauth_config.verifier = nil
|
77
|
+
res
|
78
|
+
end
|
79
|
+
|
80
|
+
# Parse response and returns a Hash.
|
81
|
+
def get_oauth_response(res)
|
82
|
+
enc = res.header['content-encoding']
|
83
|
+
body = nil
|
84
|
+
if enc and enc[0] and enc[0].downcase == 'gzip'
|
85
|
+
body = Zlib::GzipReader.wrap(StringIO.new(res.content)) { |gz| gz.read }
|
86
|
+
else
|
87
|
+
body = res.content
|
88
|
+
end
|
89
|
+
body.split('&').inject({}) { |r, e|
|
90
|
+
key, value = e.split('=', 2)
|
91
|
+
r[unescape(key)] = unescape(value)
|
92
|
+
r
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
def unescape(escaped)
|
99
|
+
escaped ? ::HTTP::Message.unescape(escaped) : nil
|
100
|
+
end
|
101
|
+
|
102
|
+
def filter_response(res)
|
103
|
+
if res.status == 200
|
104
|
+
if res.oauth_params = get_oauth_response(res)
|
105
|
+
oauth_config.token = res.oauth_params['oauth_token']
|
106
|
+
oauth_config.secret = res.oauth_params['oauth_token_secret']
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
data/sample/async.rb
ADDED
data/sample/auth.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'httpclient'
|
2
|
+
|
3
|
+
c = HTTPClient.new
|
4
|
+
c.debug_dev = STDOUT
|
5
|
+
|
6
|
+
# for Proxy authentication: supports Basic, Negotiate and NTLM.
|
7
|
+
#c.set_proxy_auth("admin", "admin")
|
8
|
+
|
9
|
+
# for WWW authentication: supports Basic, Digest and Negotiate.
|
10
|
+
c.set_auth("http://dev.ctor.org/http-access2/", "user", "user")
|
11
|
+
p c.get("http://dev.ctor.org/http-access2/login")
|
data/sample/cookie.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$:.unshift(File.join('..', 'lib'))
|
4
|
+
require 'httpclient'
|
5
|
+
|
6
|
+
proxy = ENV['HTTP_PROXY']
|
7
|
+
clnt = HTTPClient.new(proxy)
|
8
|
+
clnt.set_cookie_store("cookie.dat")
|
9
|
+
clnt.debug_dev = STDOUT if $DEBUG
|
10
|
+
|
11
|
+
while urlstr = ARGV.shift
|
12
|
+
response = clnt.get(urlstr){ |data|
|
13
|
+
print data
|
14
|
+
}
|
15
|
+
p response.contenttype
|
16
|
+
end
|
17
|
+
|
18
|
+
clnt.save_cookie_store
|
data/sample/dav.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'httpclient'
|
3
|
+
|
4
|
+
class DAV
|
5
|
+
attr_reader :headers
|
6
|
+
|
7
|
+
def initialize(uri = nil)
|
8
|
+
@uri = nil
|
9
|
+
@headers = {}
|
10
|
+
open(uri) if uri
|
11
|
+
proxy = ENV['HTTP_PROXY'] || ENV['http_proxy'] || nil
|
12
|
+
@client = HTTPClient.new(proxy)
|
13
|
+
end
|
14
|
+
|
15
|
+
def open(uri)
|
16
|
+
@uri = if uri.is_a?(URI)
|
17
|
+
uri
|
18
|
+
else
|
19
|
+
URI.parse(uri)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def set_basic_auth(user_id, passwd)
|
24
|
+
@client.set_basic_auth(@uri, user_id, passwd)
|
25
|
+
end
|
26
|
+
|
27
|
+
# TODO: propget/propset support
|
28
|
+
|
29
|
+
def propfind(target)
|
30
|
+
target_uri = @uri + target
|
31
|
+
res = @client.propfind(target_uri)
|
32
|
+
res.body.content
|
33
|
+
end
|
34
|
+
|
35
|
+
def get(target, local = nil)
|
36
|
+
local ||= target
|
37
|
+
target_uri = @uri + target
|
38
|
+
if FileTest.exist?(local)
|
39
|
+
raise RuntimeError.new("File #{ local } exists.")
|
40
|
+
end
|
41
|
+
f = File.open(local, "wb")
|
42
|
+
res = @client.get(target_uri, nil, @headers) do |data|
|
43
|
+
f << data
|
44
|
+
end
|
45
|
+
f.close
|
46
|
+
STDOUT.puts("#{ res.header['content-length'][0] } bytes saved to file #{ target }.")
|
47
|
+
end
|
48
|
+
|
49
|
+
def debug_dev=(dev)
|
50
|
+
@client.debug_dev = dev
|
51
|
+
end
|
52
|
+
|
53
|
+
def get_content(target)
|
54
|
+
target_uri = @uri + target
|
55
|
+
@client.get_content(target_uri, nil, @headers)
|
56
|
+
end
|
57
|
+
|
58
|
+
def put_content(target, content)
|
59
|
+
target_uri = @uri + target
|
60
|
+
res = @client.put(target_uri, content, @headers)
|
61
|
+
if res.status < 200 or res.status >= 300
|
62
|
+
raise "HTTP PUT failed: #{res.inspect}"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class Mock
|
67
|
+
attr_reader :headers
|
68
|
+
|
69
|
+
def initialize(uri = nil)
|
70
|
+
@uri = nil
|
71
|
+
@headers = {}
|
72
|
+
open(uri) if uri
|
73
|
+
|
74
|
+
@cache = {}
|
75
|
+
end
|
76
|
+
|
77
|
+
def open(uri)
|
78
|
+
@uri = uri.is_a?(URI) ? uri : URI.parse(uri)
|
79
|
+
end
|
80
|
+
|
81
|
+
def set_basic_auth(user_id, passwd)
|
82
|
+
# ignore
|
83
|
+
end
|
84
|
+
|
85
|
+
def propfind(target)
|
86
|
+
# not found
|
87
|
+
nil
|
88
|
+
end
|
89
|
+
|
90
|
+
def get(target, local = nil)
|
91
|
+
# ignore
|
92
|
+
end
|
93
|
+
|
94
|
+
def get_content(target)
|
95
|
+
@cache[target]
|
96
|
+
end
|
97
|
+
|
98
|
+
def put_content(target, content)
|
99
|
+
@cache[target] = content
|
100
|
+
nil
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
data/sample/howto.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$:.unshift(File.join('..', 'lib'))
|
4
|
+
require 'httpclient'
|
5
|
+
|
6
|
+
proxy = ENV['HTTP_PROXY']
|
7
|
+
clnt = HTTPClient.new(proxy)
|
8
|
+
clnt.set_cookie_store("cookie.dat")
|
9
|
+
target = ARGV.shift || "http://localhost/foo.cgi"
|
10
|
+
|
11
|
+
puts
|
12
|
+
puts '= GET content directly'
|
13
|
+
puts clnt.get_content(target)
|
14
|
+
|
15
|
+
puts '= GET result object'
|
16
|
+
result = clnt.get(target)
|
17
|
+
puts '== Header object'
|
18
|
+
p result.header
|
19
|
+
puts "== Content-type"
|
20
|
+
p result.contenttype
|
21
|
+
puts '== Body object'
|
22
|
+
p result.body
|
23
|
+
puts '== Content'
|
24
|
+
print result.content
|
25
|
+
puts '== GET with Block'
|
26
|
+
clnt.get(target) do |str|
|
27
|
+
puts str
|
28
|
+
end
|
29
|
+
|
30
|
+
puts
|
31
|
+
puts '= GET with query'
|
32
|
+
puts clnt.get(target, { "foo" => "bar", "baz" => "quz" }).content
|
33
|
+
|
34
|
+
puts
|
35
|
+
puts '= GET with query 2'
|
36
|
+
puts clnt.get(target, [["foo", "bar1"], ["foo", "bar2"]]).content
|
37
|
+
|
38
|
+
clnt.debug_dev = STDERR
|
39
|
+
puts
|
40
|
+
puts '= GET with extra header'
|
41
|
+
puts clnt.get(target, nil, { "SOAPAction" => "HelloWorld" }).content
|
42
|
+
|
43
|
+
puts
|
44
|
+
puts '= GET with extra header 2'
|
45
|
+
puts clnt.get(target, nil, [["Accept", "text/plain"], ["Accept", "text/html"]]).content
|
46
|
+
|
47
|
+
clnt.debug_dev = nil
|
48
|
+
|
49
|
+
clnt.save_cookie_store
|