Class: HTTPClient
- Inherits:
-
Object
show all
- Includes:
- Util
- Defined in:
- lib/httpclient.rb
Overview
DESCRIPTION
HTTPClient -- Client to retrieve web resources via HTTP.
How to create your client.
1. Create simple client.
clnt = HTTPClient.new
2. Accessing resources through HTTP proxy.
clnt = HTTPClient.new("http://myproxy:8080")
3. Set User-Agent and From in HTTP request header.(nil means "No proxy")
clnt = HTTPClient.new(nil, "MyAgent", "[email protected]")
How to retrieve web resources.
1. Get content of specified URL.
puts clnt.get_content("http://www.ruby-lang.org/en/")
2. Do HEAD request.
res = clnt.head(uri)
3. Do GET request with query.
res = clnt.get(uri)
4. Do POST request.
res = clnt.post(uri)
res = clnt.get|post|head(uri, proxy)
Defined Under Namespace
Modules: SocketWrap, Util
Classes: AuthFilterBase, BasicAuth, Connection, DebugSocket, DigestAuth, LoopBackSocket, NegotiateAuth, ProxyAuth, RetryableResponse, SSLConfig, SSLSocketWrap, Session, SessionManager, Site, WWWAuth
Constant Summary
collapse
- VERSION =
'2.1.1'
- RUBY_VERSION_STRING =
"ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
- SSLEnabled =
begin
require 'openssl'
true
rescue LoadError
false
end
- SSPIEnabled =
begin
require 'win32/sspi'
true
rescue LoadError
false
end
- DEBUG_SSL =
true
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#connect_timeout ⇒ Object
-
#connect_timeout=(connect_timeout) ⇒ Object
-
#debug_dev ⇒ Object
-
#debug_dev=(dev) ⇒ Object
-
#default_redirect_uri_callback(uri, res) ⇒ Object
-
#delete(uri, extheader = {}, &block) ⇒ Object
-
#delete_async(uri, extheader = {}) ⇒ Object
-
#get(uri, query = nil, extheader = {}, &block) ⇒ Object
-
#get_async(uri, query = nil, extheader = {}) ⇒ Object
-
#get_content(uri, query = nil, extheader = {}, &block) ⇒ Object
SYNOPSIS Client#get_content(uri, query = nil, extheader = {}, &block = nil).
-
#head(uri, query = nil, extheader = {}) ⇒ Object
-
#head_async(uri, query = nil, extheader = {}) ⇒ Object
-
#initialize(proxy = nil, agent_name = nil, from = nil) ⇒ HTTPClient
constructor
SYNOPSIS Client.new(proxy = nil, agent_name = nil, from = nil).
-
#no_proxy ⇒ Object
-
#no_proxy=(no_proxy) ⇒ Object
-
#options(uri, extheader = {}, &block) ⇒ Object
-
#options_async(uri, extheader = {}) ⇒ Object
-
#post(uri, body = nil, extheader = {}, &block) ⇒ Object
-
#post_async(uri, body = nil, extheader = {}) ⇒ Object
-
#post_content(uri, body = nil, extheader = {}, &block) ⇒ Object
-
#protocol_version ⇒ Object
-
#protocol_version=(protocol_version) ⇒ Object
-
#proxy ⇒ Object
-
#proxy=(proxy) ⇒ Object
-
#put(uri, body = nil, extheader = {}, &block) ⇒ Object
-
#put_async(uri, body = nil, extheader = {}) ⇒ Object
-
#receive_timeout ⇒ Object
-
#receive_timeout=(receive_timeout) ⇒ Object
-
#redirect_uri_callback=(redirect_uri_callback) ⇒ Object
-
#request(method, uri, query = nil, body = nil, extheader = {}, &block) ⇒ Object
-
#request_async(method, uri, query = nil, body = nil, extheader = {}) ⇒ Object
-
#reset(uri) ⇒ Object
-
#reset_all ⇒ Object
-
#save_cookie_store ⇒ Object
-
#send_timeout ⇒ Object
-
#send_timeout=(send_timeout) ⇒ Object
-
#set_auth(uri, user, passwd) ⇒ Object
-
#set_basic_auth(uri, user, passwd) ⇒ Object
for backward compatibility.
-
#set_cookie_store(filename) ⇒ Object
-
#set_proxy_auth(user, passwd) ⇒ Object
-
#socket_sync=(socket_sync) ⇒ Object
if your ruby is older than 2005-09-06, do not set socket_sync = false to avoid an SSL socket blocking bug in openssl/buffering.rb.
-
#strict_redirect_uri_callback(uri, res) ⇒ Object
-
#test_loopback_http_response ⇒ Object
-
#trace(uri, query = nil, body = nil, extheader = {}, &block) ⇒ Object
-
#trace_async(uri, query = nil, body = nil, extheader = {}) ⇒ Object
Methods included from Util
hash_find_value, parse_challenge_param, uri_dirname, uri_part_of, #urify
Constructor Details
#initialize(proxy = nil, agent_name = nil, from = nil) ⇒ HTTPClient
SYNOPSIS
Client.new(proxy = nil, agent_name = nil, from = nil)
ARGS
proxy A String of HTTP proxy URL. ex. "http://proxy:8080".
agent_name A String for "User-Agent" HTTP request header.
from A String for "From" HTTP request header.
DESCRIPTION
Create an instance.
SSLConfig cannot be re-initialized. Create new client.
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
|
# File 'lib/httpclient.rb', line 1646
def initialize(proxy = nil, agent_name = nil, from = nil)
@proxy = nil @no_proxy = nil
@agent_name = agent_name
@from = from
@www_auth = WWWAuth.new
@proxy_auth = ProxyAuth.new
@request_filter = [@proxy_auth, @www_auth]
@debug_dev = nil
@redirect_uri_callback = method(:default_redirect_uri_callback)
@test_loopback_response = []
@session_manager = SessionManager.new
@session_manager.agent_name = @agent_name
@session_manager.from = @from
@session_manager.ssl_config = @ssl_config = SSLConfig.new(self)
@cookie_manager = WebAgent::CookieManager.new
load_environment
self.proxy = proxy if proxy
end
|
Instance Attribute Details
#agent_name ⇒ Object
Returns the value of attribute agent_name.
1612
1613
1614
|
# File 'lib/httpclient.rb', line 1612
def agent_name
@agent_name
end
|
#cookie_manager ⇒ Object
Returns the value of attribute cookie_manager.
1615
1616
1617
|
# File 'lib/httpclient.rb', line 1615
def cookie_manager
@cookie_manager
end
|
#from ⇒ Object
Returns the value of attribute from.
1613
1614
1615
|
# File 'lib/httpclient.rb', line 1613
def from
@from
end
|
#proxy_auth ⇒ Object
Returns the value of attribute proxy_auth.
1618
1619
1620
|
# File 'lib/httpclient.rb', line 1618
def proxy_auth
@proxy_auth
end
|
#request_filter ⇒ Object
Returns the value of attribute request_filter.
1617
1618
1619
|
# File 'lib/httpclient.rb', line 1617
def request_filter
@request_filter
end
|
#ssl_config ⇒ Object
Returns the value of attribute ssl_config.
1614
1615
1616
|
# File 'lib/httpclient.rb', line 1614
def ssl_config
@ssl_config
end
|
#test_loopback_response ⇒ Object
Returns the value of attribute test_loopback_response.
1616
1617
1618
|
# File 'lib/httpclient.rb', line 1616
def test_loopback_response
@test_loopback_response
end
|
#www_auth ⇒ Object
Returns the value of attribute www_auth.
1619
1620
1621
|
# File 'lib/httpclient.rb', line 1619
def www_auth
@www_auth
end
|
Instance Method Details
#connect_timeout ⇒ Object
1685
1686
1687
|
# File 'lib/httpclient.rb', line 1685
def connect_timeout
@session_manager.connect_timeout
end
|
#connect_timeout=(connect_timeout) ⇒ Object
1689
1690
1691
1692
|
# File 'lib/httpclient.rb', line 1689
def connect_timeout=(connect_timeout)
reset_all
@session_manager.connect_timeout = connect_timeout
end
|
#debug_dev ⇒ Object
1666
1667
1668
|
# File 'lib/httpclient.rb', line 1666
def debug_dev
@debug_dev
end
|
#debug_dev=(dev) ⇒ Object
1670
1671
1672
1673
1674
|
# File 'lib/httpclient.rb', line 1670
def debug_dev=(dev)
@debug_dev = dev
reset_all
@session_manager.debug_dev = dev
end
|
#default_redirect_uri_callback(uri, res) ⇒ Object
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
|
# File 'lib/httpclient.rb', line 1823
def default_redirect_uri_callback(uri, res)
newuri = URI.parse(res.['location'][0])
unless newuri.is_a?(URI::HTTP)
newuri = uri + newuri
STDERR.puts(
"could be a relative URI in location header which is not recommended")
STDERR.puts(
"'The field value consists of a single absolute URI' in HTTP spec")
end
puts "Redirect to: #{newuri}" if $DEBUG
newuri
end
|
#delete(uri, extheader = {}, &block) ⇒ Object
1852
1853
1854
|
# File 'lib/httpclient.rb', line 1852
def delete(uri, = {}, &block)
request('DELETE', uri, nil, nil, , &block)
end
|
#delete_async(uri, extheader = {}) ⇒ Object
1902
1903
1904
|
# File 'lib/httpclient.rb', line 1902
def delete_async(uri, = {})
request_async('DELETE', uri, nil, nil, )
end
|
#get(uri, query = nil, extheader = {}, &block) ⇒ Object
1840
1841
1842
|
# File 'lib/httpclient.rb', line 1840
def get(uri, query = nil, = {}, &block)
request('GET', uri, query, nil, , &block)
end
|
#get_async(uri, query = nil, extheader = {}) ⇒ Object
1890
1891
1892
|
# File 'lib/httpclient.rb', line 1890
def get_async(uri, query = nil, = {})
request_async('GET', uri, query, nil, )
end
|
#get_content(uri, query = nil, extheader = {}, &block) ⇒ Object
SYNOPSIS
Client
ARGS
uri an_URI or a_string of uri to connect.
query a_hash or an_array of query part. e.g. { "a" => "b" }.
Give an array to pass multiple value like
[["a" => "b"], ["a" => "c"]].
extheader a_hash of extra headers like { "SOAPAction" => "urn:foo" }.
&block Give a block to get chunked message-body of response like
get_content(uri) { |chunked_body| ... }
Size of each chunk may not be the same.
DESCRIPTION
Get a_sring of message-body of response.
1805
1806
1807
1808
1809
|
# File 'lib/httpclient.rb', line 1805
def get_content(uri, query = nil, = {}, &block)
follow_redirect(uri, query) { |uri, query|
get(uri, query, , &block)
}.content
end
|
#head(uri, query = nil, extheader = {}) ⇒ Object
1836
1837
1838
|
# File 'lib/httpclient.rb', line 1836
def head(uri, query = nil, = {})
request('HEAD', uri, query, nil, )
end
|
#head_async(uri, query = nil, extheader = {}) ⇒ Object
1886
1887
1888
|
# File 'lib/httpclient.rb', line 1886
def head_async(uri, query = nil, = {})
request_async('HEAD', uri, query, nil, )
end
|
#no_proxy ⇒ Object
1735
1736
1737
|
# File 'lib/httpclient.rb', line 1735
def no_proxy
@no_proxy
end
|
#no_proxy=(no_proxy) ⇒ Object
1739
1740
1741
1742
|
# File 'lib/httpclient.rb', line 1739
def no_proxy=(no_proxy)
@no_proxy = no_proxy
reset_all
end
|
#options(uri, extheader = {}, &block) ⇒ Object
1856
1857
1858
|
# File 'lib/httpclient.rb', line 1856
def options(uri, = {}, &block)
request('OPTIONS', uri, nil, nil, , &block)
end
|
#options_async(uri, extheader = {}) ⇒ Object
1906
1907
1908
|
# File 'lib/httpclient.rb', line 1906
def options_async(uri, = {})
request_async('OPTIONS', uri, nil, nil, )
end
|
#post(uri, body = nil, extheader = {}, &block) ⇒ Object
1844
1845
1846
|
# File 'lib/httpclient.rb', line 1844
def post(uri, body = nil, = {}, &block)
request('POST', uri, nil, body, , &block)
end
|
#post_async(uri, body = nil, extheader = {}) ⇒ Object
1894
1895
1896
|
# File 'lib/httpclient.rb', line 1894
def post_async(uri, body = nil, = {})
request_async('POST', uri, nil, body, )
end
|
#post_content(uri, body = nil, extheader = {}, &block) ⇒ Object
1811
1812
1813
1814
1815
|
# File 'lib/httpclient.rb', line 1811
def post_content(uri, body = nil, = {}, &block)
follow_redirect(uri, nil) { |uri, query|
post(uri, body, , &block)
}.content
end
|
#protocol_version ⇒ Object
1676
1677
1678
|
# File 'lib/httpclient.rb', line 1676
def protocol_version
@session_manager.protocol_version
end
|
#protocol_version=(protocol_version) ⇒ Object
1680
1681
1682
1683
|
# File 'lib/httpclient.rb', line 1680
def protocol_version=(protocol_version)
reset_all
@session_manager.protocol_version = protocol_version
end
|
#proxy ⇒ Object
1712
1713
1714
|
# File 'lib/httpclient.rb', line 1712
def proxy
@proxy
end
|
#proxy=(proxy) ⇒ Object
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
|
# File 'lib/httpclient.rb', line 1716
def proxy=(proxy)
if proxy.nil?
@proxy = nil
@proxy_auth.reset_challenge
else
@proxy = urify(proxy)
if @proxy.scheme == nil or @proxy.scheme.downcase != 'http' or
@proxy.host == nil or @proxy.port == nil
raise ArgumentError.new("unsupported proxy `#{proxy}'")
end
@proxy_auth.reset_challenge
if @proxy.user || @proxy.password
@proxy_auth.set_auth(@proxy.user, @proxy.password)
end
end
reset_all
@proxy
end
|
#put(uri, body = nil, extheader = {}, &block) ⇒ Object
1848
1849
1850
|
# File 'lib/httpclient.rb', line 1848
def put(uri, body = nil, = {}, &block)
request('PUT', uri, nil, body, , &block)
end
|
#put_async(uri, body = nil, extheader = {}) ⇒ Object
1898
1899
1900
|
# File 'lib/httpclient.rb', line 1898
def put_async(uri, body = nil, = {})
request_async('PUT', uri, nil, body, )
end
|
#receive_timeout ⇒ Object
1703
1704
1705
|
# File 'lib/httpclient.rb', line 1703
def receive_timeout
@session_manager.receive_timeout
end
|
#receive_timeout=(receive_timeout) ⇒ Object
1707
1708
1709
1710
|
# File 'lib/httpclient.rb', line 1707
def receive_timeout=(receive_timeout)
reset_all
@session_manager.receive_timeout = receive_timeout
end
|
#redirect_uri_callback=(redirect_uri_callback) ⇒ Object
1781
1782
1783
|
# File 'lib/httpclient.rb', line 1781
def redirect_uri_callback=(redirect_uri_callback)
@redirect_uri_callback = redirect_uri_callback
end
|
#request(method, uri, query = nil, body = nil, extheader = {}, &block) ⇒ Object
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
|
# File 'lib/httpclient.rb', line 1864
def request(method, uri, query = nil, body = nil, = {}, &block)
uri = urify(uri)
conn = Connection.new
res = nil
retry_count = 5
while retry_count > 0
begin
prepare_request(method, uri, query, body, ) do |req, proxy|
do_get_block(req, proxy, conn, &block)
end
res = conn.pop
break
rescue RetryableResponse
res = conn.pop
retry_count -= 1
end
end
res
end
|
#request_async(method, uri, query = nil, body = nil, extheader = {}) ⇒ Object
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
|
# File 'lib/httpclient.rb', line 1914
def request_async(method, uri, query = nil, body = nil, = {})
uri = urify(uri)
conn = Connection.new
t = Thread.new(conn) { |tconn|
prepare_request(method, uri, query, body, ) do |req, proxy|
do_get_stream(req, proxy, tconn)
end
}
conn.async_thread = t
conn
end
|
#reset(uri) ⇒ Object
1934
1935
1936
1937
|
# File 'lib/httpclient.rb', line 1934
def reset(uri)
uri = urify(uri)
@session_manager.reset(uri)
end
|
#reset_all ⇒ Object
1939
1940
1941
|
# File 'lib/httpclient.rb', line 1939
def reset_all
@session_manager.reset_all
end
|
#save_cookie_store ⇒ Object
1777
1778
1779
|
# File 'lib/httpclient.rb', line 1777
def save_cookie_store
@cookie_manager.save_cookies
end
|
#send_timeout ⇒ Object
1694
1695
1696
|
# File 'lib/httpclient.rb', line 1694
def send_timeout
@session_manager.send_timeout
end
|
#send_timeout=(send_timeout) ⇒ Object
1698
1699
1700
1701
|
# File 'lib/httpclient.rb', line 1698
def send_timeout=(send_timeout)
reset_all
@session_manager.send_timeout = send_timeout
end
|
#set_auth(uri, user, passwd) ⇒ Object
1750
1751
1752
1753
1754
|
# File 'lib/httpclient.rb', line 1750
def set_auth(uri, user, passwd)
uri = urify(uri)
@www_auth.set_auth(uri, user, passwd)
reset_all
end
|
#set_basic_auth(uri, user, passwd) ⇒ Object
for backward compatibility
1757
1758
1759
1760
1761
|
# File 'lib/httpclient.rb', line 1757
def set_basic_auth(uri, user, passwd)
uri = urify(uri)
@www_auth.basic_auth.set(uri, user, passwd)
reset_all
end
|
#set_cookie_store(filename) ⇒ Object
1769
1770
1771
1772
1773
1774
1775
|
# File 'lib/httpclient.rb', line 1769
def set_cookie_store(filename)
if @cookie_manager.cookies_file
raise RuntimeError.new("overriding cookie file location")
end
@cookie_manager.cookies_file = filename
@cookie_manager.load_cookies if filename
end
|
#set_proxy_auth(user, passwd) ⇒ Object
1763
1764
1765
1766
1767
|
# File 'lib/httpclient.rb', line 1763
def set_proxy_auth(user, passwd)
uri = urify(uri)
@proxy_auth.set_auth(user, passwd)
reset_all
end
|
#socket_sync=(socket_sync) ⇒ Object
if your ruby is older than 2005-09-06, do not set socket_sync = false to avoid an SSL socket blocking bug in openssl/buffering.rb.
1746
1747
1748
|
# File 'lib/httpclient.rb', line 1746
def socket_sync=(socket_sync)
@session_manager.socket_sync = socket_sync
end
|
#strict_redirect_uri_callback(uri, res) ⇒ Object
1817
1818
1819
1820
1821
|
# File 'lib/httpclient.rb', line 1817
def strict_redirect_uri_callback(uri, res)
newuri = URI.parse(res.['location'][0])
puts "Redirect to: #{newuri}" if $DEBUG
newuri
end
|
#test_loopback_http_response ⇒ Object
1785
1786
1787
|
# File 'lib/httpclient.rb', line 1785
def test_loopback_http_response
@session_manager.test_loopback_http_response
end
|
#trace(uri, query = nil, body = nil, extheader = {}, &block) ⇒ Object
1860
1861
1862
|
# File 'lib/httpclient.rb', line 1860
def trace(uri, query = nil, body = nil, = {}, &block)
request('TRACE', uri, query, body, , &block)
end
|
#trace_async(uri, query = nil, body = nil, extheader = {}) ⇒ Object
1910
1911
1912
|
# File 'lib/httpclient.rb', line 1910
def trace_async(uri, query = nil, body = nil, = {})
request_async('TRACE', uri, query, body, )
end
|