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: Util
Classes: AuthFilterBase, BasicAuth, Connection, DebugSocket, DigestAuth, NegotiateAuth, ProxyAuth, RetryableResponse, SSLConfig, SSLSocketWrap, Session, SessionManager, Site, WWWAuth
Constant Summary
collapse
- VERSION =
'2.1.0'
- 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
-
#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.
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
|
# File 'lib/httpclient.rb', line 1572
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.
1538
1539
1540
|
# File 'lib/httpclient.rb', line 1538
def agent_name
@agent_name
end
|
#cookie_manager ⇒ Object
Returns the value of attribute cookie_manager.
1541
1542
1543
|
# File 'lib/httpclient.rb', line 1541
def cookie_manager
@cookie_manager
end
|
#from ⇒ Object
Returns the value of attribute from.
1539
1540
1541
|
# File 'lib/httpclient.rb', line 1539
def from
@from
end
|
#proxy_auth ⇒ Object
Returns the value of attribute proxy_auth.
1544
1545
1546
|
# File 'lib/httpclient.rb', line 1544
def proxy_auth
@proxy_auth
end
|
#request_filter ⇒ Object
Returns the value of attribute request_filter.
1543
1544
1545
|
# File 'lib/httpclient.rb', line 1543
def request_filter
@request_filter
end
|
#ssl_config ⇒ Object
Returns the value of attribute ssl_config.
1540
1541
1542
|
# File 'lib/httpclient.rb', line 1540
def ssl_config
@ssl_config
end
|
#test_loopback_response ⇒ Object
Returns the value of attribute test_loopback_response.
1542
1543
1544
|
# File 'lib/httpclient.rb', line 1542
def test_loopback_response
@test_loopback_response
end
|
#www_auth ⇒ Object
Returns the value of attribute www_auth.
1545
1546
1547
|
# File 'lib/httpclient.rb', line 1545
def www_auth
@www_auth
end
|
Instance Method Details
#connect_timeout ⇒ Object
1611
1612
1613
|
# File 'lib/httpclient.rb', line 1611
def connect_timeout
@session_manager.connect_timeout
end
|
#connect_timeout=(connect_timeout) ⇒ Object
1615
1616
1617
1618
|
# File 'lib/httpclient.rb', line 1615
def connect_timeout=(connect_timeout)
reset_all
@session_manager.connect_timeout = connect_timeout
end
|
#debug_dev ⇒ Object
1592
1593
1594
|
# File 'lib/httpclient.rb', line 1592
def debug_dev
@debug_dev
end
|
#debug_dev=(dev) ⇒ Object
1596
1597
1598
1599
1600
|
# File 'lib/httpclient.rb', line 1596
def debug_dev=(dev)
@debug_dev = dev
reset_all
@session_manager.debug_dev = dev
end
|
#default_redirect_uri_callback(uri, res) ⇒ Object
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
|
# File 'lib/httpclient.rb', line 1745
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
1774
1775
1776
|
# File 'lib/httpclient.rb', line 1774
def delete(uri, = {}, &block)
request('DELETE', uri, nil, nil, , &block)
end
|
#delete_async(uri, extheader = {}) ⇒ Object
1824
1825
1826
|
# File 'lib/httpclient.rb', line 1824
def delete_async(uri, = {})
request_async('DELETE', uri, nil, nil, )
end
|
#get(uri, query = nil, extheader = {}, &block) ⇒ Object
1762
1763
1764
|
# File 'lib/httpclient.rb', line 1762
def get(uri, query = nil, = {}, &block)
request('GET', uri, query, nil, , &block)
end
|
#get_async(uri, query = nil, extheader = {}) ⇒ Object
1812
1813
1814
|
# File 'lib/httpclient.rb', line 1812
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.
1727
1728
1729
1730
1731
|
# File 'lib/httpclient.rb', line 1727
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
1758
1759
1760
|
# File 'lib/httpclient.rb', line 1758
def head(uri, query = nil, = {})
request('HEAD', uri, query, nil, )
end
|
#head_async(uri, query = nil, extheader = {}) ⇒ Object
1808
1809
1810
|
# File 'lib/httpclient.rb', line 1808
def head_async(uri, query = nil, = {})
request_async('HEAD', uri, query, nil, )
end
|
#no_proxy ⇒ Object
1661
1662
1663
|
# File 'lib/httpclient.rb', line 1661
def no_proxy
@no_proxy
end
|
#no_proxy=(no_proxy) ⇒ Object
1665
1666
1667
1668
|
# File 'lib/httpclient.rb', line 1665
def no_proxy=(no_proxy)
@no_proxy = no_proxy
reset_all
end
|
#options(uri, extheader = {}, &block) ⇒ Object
1778
1779
1780
|
# File 'lib/httpclient.rb', line 1778
def options(uri, = {}, &block)
request('OPTIONS', uri, nil, nil, , &block)
end
|
#options_async(uri, extheader = {}) ⇒ Object
1828
1829
1830
|
# File 'lib/httpclient.rb', line 1828
def options_async(uri, = {})
request_async('OPTIONS', uri, nil, nil, )
end
|
#post(uri, body = nil, extheader = {}, &block) ⇒ Object
1766
1767
1768
|
# File 'lib/httpclient.rb', line 1766
def post(uri, body = nil, = {}, &block)
request('POST', uri, nil, body, , &block)
end
|
#post_async(uri, body = nil, extheader = {}) ⇒ Object
1816
1817
1818
|
# File 'lib/httpclient.rb', line 1816
def post_async(uri, body = nil, = {})
request_async('POST', uri, nil, body, )
end
|
#post_content(uri, body = nil, extheader = {}, &block) ⇒ Object
1733
1734
1735
1736
1737
|
# File 'lib/httpclient.rb', line 1733
def post_content(uri, body = nil, = {}, &block)
follow_redirect(uri, nil) { |uri, query|
post(uri, body, , &block)
}.content
end
|
#protocol_version ⇒ Object
1602
1603
1604
|
# File 'lib/httpclient.rb', line 1602
def protocol_version
@session_manager.protocol_version
end
|
#protocol_version=(protocol_version) ⇒ Object
1606
1607
1608
1609
|
# File 'lib/httpclient.rb', line 1606
def protocol_version=(protocol_version)
reset_all
@session_manager.protocol_version = protocol_version
end
|
#proxy ⇒ Object
1638
1639
1640
|
# File 'lib/httpclient.rb', line 1638
def proxy
@proxy
end
|
#proxy=(proxy) ⇒ Object
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
|
# File 'lib/httpclient.rb', line 1642
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
1770
1771
1772
|
# File 'lib/httpclient.rb', line 1770
def put(uri, body = nil, = {}, &block)
request('PUT', uri, nil, body, , &block)
end
|
#put_async(uri, body = nil, extheader = {}) ⇒ Object
1820
1821
1822
|
# File 'lib/httpclient.rb', line 1820
def put_async(uri, body = nil, = {})
request_async('PUT', uri, nil, body, )
end
|
#receive_timeout ⇒ Object
1629
1630
1631
|
# File 'lib/httpclient.rb', line 1629
def receive_timeout
@session_manager.receive_timeout
end
|
#receive_timeout=(receive_timeout) ⇒ Object
1633
1634
1635
1636
|
# File 'lib/httpclient.rb', line 1633
def receive_timeout=(receive_timeout)
reset_all
@session_manager.receive_timeout = receive_timeout
end
|
#redirect_uri_callback=(redirect_uri_callback) ⇒ Object
1707
1708
1709
|
# File 'lib/httpclient.rb', line 1707
def redirect_uri_callback=(redirect_uri_callback)
@redirect_uri_callback = redirect_uri_callback
end
|
#request(method, uri, query = nil, body = nil, extheader = {}, &block) ⇒ Object
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
|
# File 'lib/httpclient.rb', line 1786
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
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
|
# File 'lib/httpclient.rb', line 1836
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
1856
1857
1858
1859
|
# File 'lib/httpclient.rb', line 1856
def reset(uri)
uri = urify(uri)
@session_manager.reset(uri)
end
|
#reset_all ⇒ Object
1861
1862
1863
|
# File 'lib/httpclient.rb', line 1861
def reset_all
@session_manager.reset_all
end
|
#save_cookie_store ⇒ Object
1703
1704
1705
|
# File 'lib/httpclient.rb', line 1703
def save_cookie_store
@cookie_manager.save_cookies
end
|
#send_timeout ⇒ Object
1620
1621
1622
|
# File 'lib/httpclient.rb', line 1620
def send_timeout
@session_manager.send_timeout
end
|
#send_timeout=(send_timeout) ⇒ Object
1624
1625
1626
1627
|
# File 'lib/httpclient.rb', line 1624
def send_timeout=(send_timeout)
reset_all
@session_manager.send_timeout = send_timeout
end
|
#set_auth(uri, user, passwd) ⇒ Object
1676
1677
1678
1679
1680
|
# File 'lib/httpclient.rb', line 1676
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
1683
1684
1685
1686
1687
|
# File 'lib/httpclient.rb', line 1683
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
1695
1696
1697
1698
1699
1700
1701
|
# File 'lib/httpclient.rb', line 1695
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
1689
1690
1691
1692
1693
|
# File 'lib/httpclient.rb', line 1689
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.
1672
1673
1674
|
# File 'lib/httpclient.rb', line 1672
def socket_sync=(socket_sync)
@session_manager.socket_sync = socket_sync
end
|
#strict_redirect_uri_callback(uri, res) ⇒ Object
1739
1740
1741
1742
1743
|
# File 'lib/httpclient.rb', line 1739
def strict_redirect_uri_callback(uri, res)
newuri = URI.parse(res.['location'][0])
puts "Redirect to: #{newuri}" if $DEBUG
newuri
end
|
#trace(uri, query = nil, body = nil, extheader = {}, &block) ⇒ Object
1782
1783
1784
|
# File 'lib/httpclient.rb', line 1782
def trace(uri, query = nil, body = nil, = {}, &block)
request('TRACE', uri, query, body, , &block)
end
|
#trace_async(uri, query = nil, body = nil, extheader = {}) ⇒ Object
1832
1833
1834
|
# File 'lib/httpclient.rb', line 1832
def trace_async(uri, query = nil, body = nil, = {})
request_async('TRACE', uri, query, body, )
end
|