glebtv-httpclient 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- 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,186 @@
|
|
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
|
+
# Gets a string to be used as a binary buffer
|
168
|
+
def get_buf
|
169
|
+
buf = ''
|
170
|
+
defined?(Encoding::ASCII_8BIT) && buf.force_encoding(Encoding::ASCII_8BIT)
|
171
|
+
buf
|
172
|
+
end
|
173
|
+
module_function :get_buf
|
174
|
+
|
175
|
+
# Checks if the given URI is https.
|
176
|
+
def https?(uri)
|
177
|
+
uri.scheme && uri.scheme.downcase == 'https'
|
178
|
+
end
|
179
|
+
|
180
|
+
def http?(uri)
|
181
|
+
uri.scheme && uri.scheme.downcase == 'http'
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
end
|