summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog8
-rw-r--r--doc/net/http.rd.ja502
-rw-r--r--lib/net/http.rb79
3 files changed, 565 insertions, 24 deletions
diff --git a/ChangeLog b/ChangeLog
index 9d4d20f124..f95b1113c4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Fri Jul 12 06:34:05 2002 Minero Aoki <[email protected]>
+
+ * lib/net/http.rb: rename HTTP.get_uri get_response.
+
+ * lib/net/http.rb: HTTP.get_print accepts URI objects.
+
+ * lib/net/http.rb: HTTP.get did not work with URI.
+
Fri Jul 12 02:15:58 2002 Nobuyoshi Nakada <[email protected]>
* string.c (rb_str_match): fix for string match.
diff --git a/doc/net/http.rd.ja b/doc/net/http.rd.ja
new file mode 100644
index 0000000000..0e39329197
--- /dev/null
+++ b/doc/net/http.rd.ja
@@ -0,0 +1,502 @@
+=begin
+
+= net/http.rb
+
+== ���Υ饤�֥��ˤĤ���
+
+���ѥǡ���ž���ץ��ȥ��� HTTP version 1.1 �򰷤��饤�֥��Ǥ���
+������ [RFC2616] ((<URL:http://www.ietf.org/rfc/rfc2616.txt>)) ��
+�𤤤Ƥ��ޤ���
+
+== ������
+
+=== �����֥����Ф���ɥ�����Ȥ����� (GET)
+
+ require 'net/http'
+ Net::HTTP.start( 'some.www.server', 80 ) {|http|
+ response = http.get('/index.html')
+ puts response.body
+ }
+
+�ޤ��ʲ���Ʊ����̣��û���񤤤���ΤǤ���
+
+ require 'net/http'
+ Net::HTTP.get_print 'some.www.server', '/index.html'
+ # or
+ Net::HTTP.get_print URI.parse('http://www.example.com/index.html')
+
+=== �ե�����ξ������������ (POST)
+
+ require 'net/http'
+ Net::HTTP.start( 'some.www.server', 80 ) {|http|
+ response = http.post('/cgi-bin/any.rhtml',
+ 'querytype=subject&target=ruby')
+ }
+
+=== �ץ�������ͳ�Υ�������
+
+Net::HTTP �Υ��饹�᥽�å� Net::HTTP.Proxy �ϡ���˥ץ�������ͳ��
+��³����褦��ư��򤹤롢���������饹����������֤��ޤ������Υ��饹��
+Net::HTTP ��Ѿ����Ƥ���Τ� Net::HTTP ������Ʊ���褦�˻Ȥ��ޤ���
+
+ require 'net/http'
+
+ $proxy_addr = 'your.proxy.addr'
+ $proxy_port = 8080
+ :
+ Net::HTTP::Proxy($proxy_addr, $proxy_port).start('some.www.server') {|http|
+ # always connect to your.proxy.addr:8080
+ :
+ }
+
+�ޤ� Net::HTTP.Proxy ���������� nil ���� Net::HTTP ���Ȥ��֤��Τ�
+��Υ����ɤΤ褦�˽񤤤Ƥ����Хץ������ʤ��ξ��ˤ��б��Ǥ��ޤ���
+
+=== ������쥯�Ȥ��б�����
+
+ require 'net/http'
+
+ def read_uri( uri_str )
+ response = Net::HTTP.get_response(URI.parse(uri_str))
+ case response
+ when Net::HTTPSuccess then response
+ when Net::HTTPRedirection then read_uri(response['location'])
+ else
+ response.error!
+ end
+ end
+
+ print read_uri('http://www.ruby-lang.org')
+
+HTTPSuccess �� HTTPRedirection �� HTTPResponse ���饹�β��̥��饹�Ǥ���
+HTTPResponse ���֥������ȤϤ��줾�� HTTP �쥹�ݥ󥹤Υ��ơ������ˤ�ä�
+�ۤ륯�饹��°���Ƥ��ꡢ���Υ��饹�Ƿ�̤�ʬ��Ǥ��ޤ����ɤΤ褦�ʥ��饹��
+�Ѱդ���Ƥ���Τ��ˤĤ��Ƥϡ�HTTP �쥹�ݥ󥹥��饹���פ���򸫤Ƥ���������
+
+=== Basic ǧ��
+
+ require 'net/http'
+
+ req = Net::HTTP::Get.new('/need-auth.cgi')
+ req.basic_auth 'account', 'password'
+ Net::HTTP.start( 'auth.some.domain' ) {|http|
+ response = http.request(req)
+ print response.body
+ }
+
+=== HTTP �쥹�ݥ󥹥��饹��
+
+�ʲ��� HTTP 1.1 �Υꥶ��ȥ����ɤȤ�����б�����쥹�ݥ󥹥��饹��
+�����ޤ������饹�Ϥ��٤� Net �⥸�塼�������������Ƥ��ꡢ
+����ǥ�Ȥ��Ѿ��ط���ɽ�路�Ƥ��ޤ���
+
+ xxx HTTPResponse
+
+ 1xx HTTPInformation
+ 100 HTTPContinue
+ 101 HTTPSwitchProtocol
+
+ 2xx HTTPSuccess
+ 200 HTTPOK
+ 201 HTTPCreated
+ 202 HTTPAccepted
+ 203 HTTPNonAuthoritativeInformation
+ 204 HTTPNoContent
+ 205 HTTPResetContent
+ 206 HTTPPartialContent
+
+ 3xx HTTPRedirection
+ 300 HTTPMultipleChoice
+ 301 HTTPMovedPermanently
+ 302 HTTPFound
+ 303 HTTPSeeOther
+ 304 HTTPNotModified
+ 305 HTTPUseProxy
+ 307 HTTPTemporaryRedirect
+
+ 4xx HTTPClientError
+ 400 HTTPBadRequest
+ 401 HTTPUnauthorized
+ 402 HTTPPaymentRequired
+ 403 HTTPForbidden
+ 404 HTTPNotFound
+ 405 HTTPMethodNotAllowed
+ 406 HTTPNotAcceptable
+ 407 HTTPProxyAuthenticationRequired
+ 408 HTTPRequestTimeOut
+ 409 HTTPConflict
+ 410 HTTPGone
+ 411 HTTPLengthRequired
+ 412 HTTPPreconditionFailed
+ 413 HTTPRequestEntityTooLarge
+ 414 HTTPRequestURITooLong
+ 415 HTTPUnsupportedMediaType
+ 416 HTTPRequestedRangeNotSatisfiable
+ 417 HTTPExpectationFailed
+
+ 5xx HTTPServerError
+ 500 HTTPInternalServerError
+ 501 HTTPNotImplemented
+ 502 HTTPBadGateway
+ 503 HTTPServiceUnavailable
+ 504 HTTPGatewayTimeOut
+ 505 HTTPVersionNotSupported
+
+ xxx HTTPUnknownResponse
+
+== ���������ͤؤ��ѹ��Ȱܹ����֤ˤĤ���
+
+Ruby 1.6 �ˤ� http.rb 1.1��Ruby 1.7 �ˤ� http.rb 1.2 ��ź��
+����Ƥ��ޤ��������δ֤ǤϤ��ʤ��礭�����ͤ��Ѥ�äƤ��ޤ���
+�����������˻��ͤ��ѹ�����ΤǤʤ���ξ���μ�������¸������
+�������ߤ��뤳�Ȥˤ��ޤ�����
+
+�᥽�å� HTTP.version_1_2��HTTP.version_1_1 ��Ƥ֤�
+���Τ��Ȥ���������� Net::HTTP ���֥������ȤϤ��줾���
+�С������λ��ͤ�ư���褦�ˤʤ�ޤ����ʲ��ϻ�����Ǥ���
+
+ # example
+ Net::HTTP.start {|http1| ...(http1 has 1.2 features)... }
+
+ Net::HTTP.version_1_1
+ Net::HTTP.start {|http2| ...(http2 has 1.1 features)... }
+
+ Net::HTTP.version_1_2
+ Net::HTTP.start {|http3| ...(http3 has 1.2 features)... }
+
+���ε�ǽ�ϥ���åɥ����դǤϤ���ޤ���
+
+== class Net::HTTP
+
+=== ���饹�᥽�å�
+
+: new( address, port = 80, proxy_addr = nil, proxy_port = nil )
+ ������ HTTP ���֥������Ȥ��������ޤ���address �� HTTP �����С��� FQDN �ǡ�
+ port ����³����ݡ����ֹ�Ǥ������Υ᥽�åɤǤϤޤ���³�Ϥ��ޤ���
+
+ proxy_addr ��Ϳ����ȥץ�������𤷤���³���륪�֥������Ȥ��������ޤ���
+
+: start( address, port = 80, proxy_addr = nil, proxy_port = nil )
+ ������ Net::HTTP ���֥������Ȥ������� HTTP ���å�����
+ ���Ϥ����������֤��ޤ���
+
+: start( address, port = 80, proxy_addr = nil, proxy_port = nil ) {|http| .... }
+ ������ Net::HTTP ���֥������Ȥ��������֥��å����Ϥ��ޤ���
+ �֥��å��¹���Τ� HTTP ���å�����ݻ����ޤ���
+
+ �֥��å����֤��ͤ򤽤Τޤ��֤��ޤ���
+
+: get_print( uri )
+: get_print( address, path, port = 80 )
+ uri �ޤ��� address path port �ǻ��ꤵ�줿����ƥ��ƥ���
+ ������ stdout �˽��Ϥ��ޤ���
+
+ Net::HTTP.get_print URI.parse('http://www.example.com')
+
+: get( uri )
+: get( address, path, port = 80 )
+ uri �ޤ��� address path port �ǻ��ꤵ�줿����ƥ��ƥ���
+ ������ʸ������֤��ޤ���
+
+ print Net::HTTP.get(URI.parse('http://www.example.com'))
+
+: get_response( uri )
+: get_response( address, path, port = 80 )
+ uri �ޤ��� address path port �ǻ��ꤵ�줿����ƥ��ƥ���
+ ������ Net::HTTPResponse ���֥������Ȥ��֤��ޤ���
+
+ res = Net::HTTP.get_response(URI.parse('http://www.example.com'))
+ print res.body
+
+: Proxy( address, port = 80 )
+ ��˻��ꤵ�줿�ץ���������³���륯�饹��������֤��ޤ���
+ ���Υ��饹�� Net::HTTP ��Ѿ����Ƥ���Τ� Net::HTTP ������
+ Ʊ���褦�˻Ȥ��ޤ���
+
+ address �� nil �ΤȤ��� Net::HTTP ���饹�򤽤Τޤ��֤��ޤ���
+
+ # example
+ proxy_class = Net::HTTP::Proxy( 'proxy.foo.org', 8080 )
+ :
+ proxy_class.start( 'www.ruby-lang.org' ) {|http|
+ # connecting proxy.foo.org:8080
+ :
+ }
+
+: proxy_class?
+ ���Ȥ� (Proxy �᥽�åɤˤ�äƺ������줿) �ץ������ѤΥ��饹�ʤ�п���
+
+: port
+ HTTP �Υǥե���ȥݡ��� (80)��
+
+=== �᥽�å�
+
+: start
+: start {|http| .... }
+ TCP ���ͥ�������ĥ�� HTTP ���å����򳫻Ϥ��ޤ���
+ ���Ǥ˥��å���󤬳��Ϥ��Ƥ������㳰 IOError ��ȯ�����ޤ���
+
+ ���ƥ졼���Ȥ��ƸƤФ줿���ϥ֥��å��δ֤������å�����
+ �ݻ������֥��å���λ�ȤȤ�˼�ưŪ�˥��å�����λ���ޤ���
+
+: started?
+ HTTP ���å���󤬳��Ϥ���Ƥ����鿿��
+
+: address
+ ��³���륢�ɥ쥹
+
+: port
+ ��³����ݡ����ֹ�
+
+: open_timeout
+: open_timeout=(n)
+ ��³�����Ԥĺ����ÿ��������ÿ����äƤ⥳�ͥ������
+ �����ʤ�����㳰 TimeoutError ��ȯ�����ޤ���
+
+: read_timeout
+: read_timeout=(n)
+ �ɤߤ��� (read(1) ���) �ǥ֥��å����Ƥ褤�����ÿ���
+ �����ÿ����äƤ��ɤߤ���ʤ�����㳰 TimeoutError ��ȯ�����ޤ���
+
+: finish
+ HTTP ���å�����λ���ޤ������å���󳫻����ˤ��Υ᥽�åɤ�
+ �ƤФ줿�����㳰 IOError ��ȯ�����ޤ���
+
+: proxy?
+ �ץ�������𤷤���³����ʤ鿿��
+
+: proxy_address
+ �ץ�������ͳ����³���� HTTP ���֥������Ȥʤ�ץ������Υ��ɥ쥹��
+ �����Ǥʤ��ʤ� nil��
+
+: proxy_port
+ �ץ�������ͳ����³���� HTTP ���֥������Ȥʤ�ץ������Υݡ��ȡ�
+ �����Ǥʤ��ʤ� nil��
+
+: get( path, header = nil )
+: get( path, header = nil ) {|str| .... }
+ �����о�� path �ˤ��륨��ƥ��ƥ���������ޤ����ޤ� header �� nil
+ �Ǥʤ���С��ꥯ�����Ȥ�����Ȥ��ˤ������Ƥ� HTTP �إå��Ȥ��ƽ�
+ ���ߤޤ���header �ϥϥå���ǡ��֥إå�̾ => ���ơפΤ褦�ʷ�����
+ �ʤ���Ф����ޤ���
+
+ �֤��ͤϡ��С������ 1.1 �Ǥ� HTTPResponse �ȥ���ƥ��ƥ��ܥǥ�ʸ�����
+ �����Ǥ�����Ǥ���1.2 �Ǥ� HTTPResponse �����ҤȤĤΤߤǤ������ξ�硢
+ ����ƥ��ƥ��ܥǥ��� response.body �������ޤ���
+
+ �֥��å��ȤȤ�˸ƤФ줿���ϥ���ƥ��ƥ��ܥǥ��򾯤��Ťĥ֥��å���
+ Ϳ���ޤ���
+
+ 1.1 �Ǥ� 3xx (�ƻ�Բ�ǽ�ʥ��顼)���Ф��Ƥ��㳰��ȯ�����ޤ������ξ��
+ HTTPResponse ���㳰���֥������Ȥ��� err.response �����뤳�Ȥ��Ǥ��ޤ���
+ ���� 1.2 �Ǥ������㳰��ȯ�����ޤ���
+
+ # version 1.1 (bundled with Ruby 1.6)
+ response, body = http.get( '/index.html' )
+
+ # version 1.2 (bundled with Ruby 1.7 or later)
+ response = http.get( '/index.html' )
+
+ # compatible in both version
+ response , = http.get( '/index.html' )
+ response.body
+
+ # using block
+ File.open( 'save.txt', 'w' ) {|f|
+ http.get( '/~foo/', nil ) do |str|
+ f.write str
+ end
+ }
+
+: head( path, header = nil )
+ �����о�� path �ˤ��륨��ƥ��ƥ��Υإå��Τߤ�������ޤ���
+ �ޤ� header �� nil �Ǥʤ���Хꥯ�����Ȥ�����Ȥ��ˤ������Ƥ�
+ HTTP �إå��Ȥ��ƽ񤭤��ߤޤ���header �ϥϥå���ǡ�
+ �֥إå�̾ => ���ơפΤ褦�ʷ����Ǥʤ���Ф����ޤ���
+
+ HTTPResponse ���֥������Ȥ��֤��ޤ���
+
+ 1.1 �Ǥ� 3xx (�ƻ�Բ�ǽ�ʥ��顼)���Ф��Ƥ��㳰��ȯ�����ޤ������ξ��
+ HTTPResponse ���㳰���֥������Ȥ��� err.response �����뤳�Ȥ��Ǥ��ޤ���
+ ���� 1.2 �Ǥ������㳰��ȯ�����ޤ���
+
+ response = nil
+ Net::HTTP.start( 'some.www.server', 80 ) {|http|
+ response = http.head( '/index.html' )
+ }
+ p response['content-type']
+
+: post( path, data, header = nil )
+: post( path, data, header = nil ) {|str| .... }
+ �����о�� path �ˤ��륨��ƥ��ƥ����Ф�ʸ���� data ��
+ ����ޤ����쥹�ݥ󥹤� << �᥽�åɤ�Ȥä� dest �˽�
+ ���ޤ�ޤ���header �� get �᥽�åɤ�Ʊ���Ǥ���
+ HTTPResponse ���֥������Ȥ� dest ��������֤��ޤ���
+
+ ���ƥ졼���Ȥ��ƸƤӤ����줿�Ȥ��ϥ���ƥ��ƥ��ܥǥ��򾯤��Ť�
+ �֥��å���Ϳ���ޤ���
+
+ 1.1 �Ǥ� 3xx (�ƻ�Բ�ǽ�ʥ��顼)���Ф��Ƥ��㳰��ȯ�����ޤ������ξ��
+ HTTPResponse ���㳰���֥������Ȥ��� err.response �����뤳�Ȥ��Ǥ��ޤ���
+ ���� 1.2 �Ǥ������㳰��ȯ�����ޤ���
+
+ # version 1.1
+ response, body = http.post( '/cgi-bin/search.rb', 'query=subject&target=ruby' )
+
+ # version 1.2
+ response = http.post( '/cgi-bin/search.rb', 'query=subject&target=ruby' )
+
+ # compatible in both version
+ response , = http.post( '/cgi-bin/search.rb', 'query=subject&target=ruby' )
+
+ # using block
+ File.open( 'save.html', 'w' ) {|f|
+ http.post( '/cgi-bin/search.rb',
+ 'query=subject&target=ruby' ) do |str|
+ f.write str
+ end
+ }
+
+: request_get( path, header = nil )
+: request_get( path, header = nil ) {|response| .... }
+ path �ˤ��륨��ƥ��ƥ���������ޤ���HTTPResponse
+ ���֥������Ȥ��֤��ޤ���
+
+ �֥��å��ȤȤ�˸ƤӽФ��줿�Ȥ��ϡ��֥��å��¹������³��
+ �ݻ������ޤ� HTTPResponse ���֥������Ȥ�֥��å����Ϥ��ޤ���
+
+ ���Υ᥽�åɤ� HTTP �ץ��ȥ���˴�Ϣ�����㳰��ȯ�������ޤ���
+
+ # example
+ response = http.request_get( '/index.html' )
+ p response['content-type']
+ puts response.body # body is already read
+
+ # using block
+ http.request_get( '/index.html' ) {|response|
+ p response['content-type']
+ response.read_body do |str| # read body now
+ print str
+ end
+ }
+
+: request_post( path, data, header = nil )
+: request_post( path, data, header = nil ) {|response| .... }
+ path �ˤ��륨��ƥ��ƥ���������ޤ���HTTPResponse
+ ���֥������Ȥ��֤��ޤ���
+
+ �֥��å��ȤȤ�˸ƤӽФ��줿�Ȥ��ϡ��ܥǥ����ɤߤ�������
+ HTTPResponse ���֥������Ȥ�֥��å����Ϥ��ޤ���
+
+ ���Υ᥽�åɤ� HTTP �ץ��ȥ���˴�Ϣ�����㳰��ȯ�������ޤ���
+
+ # example
+ response = http.post2( '/cgi-bin/nice.rb', 'datadatadata...' )
+ p response.status
+ puts response.body # body is already read
+
+ # using block
+ http.post2( '/cgi-bin/nice.rb', 'datadatadata...' ) {|response|
+ p response.status
+ p response['content-type']
+ response.read_body do |str| # read body now
+ print str
+ end
+ }
+
+: request( request [, data] )
+: request( request [, data] ) {|response| .... }
+ HTTPResquest ���֥������� request ���������ޤ���POST/PUT �λ��� data ��
+ Ϳ�����ޤ� (POST/PUT �ʳ��� data ��Ϳ����� ArgumentError ��ȯ�����ޤ�)��
+
+ �֥��å��ȤȤ�˸ƤӤ����줿�Ȥ��ϥܥǥ����ɤߤ��ޤ��� HTTPResponse
+ ���֥������Ȥ�֥��å���Ϳ���ޤ���
+
+ ���Υ᥽�åɤ� HTTP �ץ��ȥ���˴�Ϣ�����㳰��ȯ�������ޤ���
+
+== class Net::HTTPRequest
+
+HTTP �ꥯ�����Ȥ���ݲ����륯�饹���ºݤˤϲ��̥��饹��
+Net::HTTP::Get, Post, Head ��Ȥ��ޤ���
+
+=== ���饹�᥽�å�
+
+: new
+ HTTP �ꥯ�����ȥ��֥������Ȥ��������ޤ���
+
+=== �᥽�å�
+
+: self[ key ]
+ key �إå��ե�����ɤ�ʸ����
+ key ����ʸ����ʸ������̤��ޤ���
+
+: self[ key ] = val
+ key �إå��ե�����ɤ� val �򥻥åȤ��ޤ���
+ key ����ʸ����ʸ������̤��ޤ���
+
+: each {|name, val| .... }
+ �إå�̾�Ȥ����ͤ��Ф��뤯�꤫�������إå�̾�Ͼ�ʸ�������줵��ޤ���
+
+: basic_auth( account, password )
+ Authrization: �إå��� basic auth �Ѥ˥��åȤ��ޤ���
+
+: range
+ Range: �إå��μ����ϰϤ� Range ���֥������Ȥ��֤��ޤ���
+
+: range = r
+: set_range( i, len )
+ �ϰϤ���ꤷ�ƥ���ƥ��ƥ���������뤿��Υإå� Range: �򥻥åȤ��ޤ���
+ r �� Range ���֥������ȡ�i, len �ϻ�����Ĺ���Ǥ���
+
+: content_length
+ Content-Length: �إå����� (����)��
+
+: content_range
+ Content-Range: �إå����� (Range)��
+
+== class Net::HTTPResponse
+
+HTTP �쥹�ݥ󥹤Υ��饹�Ǥ���
+�������إå��ե������̾�Ǥ����硢��ʸ����ʸ������̤��ޤ���
+
+=== �᥽�å�
+
+: self[ key ]
+ key �إå��ե������(ʸ����)�Ǥ������Ȥ��Х��� 'content-length'
+ ���Ф��Ƥ� '2048' �Τ褦��ʸ���������ޤ���
+ key ����ʸ����ʸ������̤��ޤ���
+
+: self[ key ] = val
+ key �إå��ե�����ɤ� value �����ꤷ�ޤ���
+ key ����ʸ����ʸ������̤��ޤ���
+
+: key?( key )
+ key �Ȥ����إå��ե�����ɤ�����п���
+ key ����ʸ����ʸ������̤��ޤ���
+
+: each {|name,value| .... }
+ ���٤ƤΥإå��ե������̾�Ȥ����ͤΥڥ����Ф��뤯�꤫������
+
+: canonical_each {|name,value| .... }
+ �إå��ե�����ɤ�����̾�Ȥ����ͤΥڥ����Ф��Ʒ����֤��ޤ���
+
+: code
+ HTTP �Υꥶ��ȥ����ɤǤ����㤨�� '302' �ʤɤǤ���
+
+: message
+ HTTP �����Ф��ꥶ��ȥ����ɤ��ղä����֤���å������Ǥ���
+ �㤨�� 'Not Found' �ʤɤǤ���
+
+: read_body( dest = '' )
+ ����ƥ��ƥ��ܥǥ�������� dest �� << �᥽�åɤ�Ȥäƽ񤭤��ߤޤ���
+ Ʊ�� HTTPResponse ���֥������Ȥ��Ф������ʾ�ƤФ줿��硢
+ ����ܤ���Ϥʤˤ⤻���˰���ܤ��֤��ͤ򤽤Τޤ��֤��ޤ���
+
+: read_body {|str| .... }
+ ����ƥ��ƥ��ܥǥ��򾯤��Ťļ������ƽ缡�֥��å���Ϳ���ޤ���
+
+: body
+ ����ƥ��ƥ��ܥǥ��Ǥ���read_body ��Ƥ�Ǥ���Ф��ΰ��� dest��
+ �Ƥ�Ǥ��ʤ���Х���ƥ��ƥ��ܥǥ���ʸ����Ȥ����ɤߤ�����֤��ޤ���
+
+=end
diff --git a/lib/net/http.rb b/lib/net/http.rb
index 4bd781bb89..0a1a714a0d 100644
--- a/lib/net/http.rb
+++ b/lib/net/http.rb
@@ -37,13 +37,15 @@ For details of HTTP, refer [RFC2616]
require 'net/http'
Net::HTTP.get_print 'some.www.server', '/index.html'
+ # or
+ Net::HTTP.get_print URI.parse('http://www.example.com/index.html')
=== Posting Form Data
require 'net/http'
Net::HTTP.start( 'some.www.server', 80 ) {|http|
- response = http.post( '/cgi-bin/any.rhtml',
- 'querytype=subject&target=ruby' )
+ response = http.post('/cgi-bin/any.rhtml',
+ 'querytype=subject&target=ruby')
}
=== Accessing via Proxy
@@ -56,22 +58,21 @@ proxy, instead of given host.
$proxy_addr = 'your.proxy.addr'
$proxy_port = 8080
- :
- Net::HTTP::Proxy($proxy_addr, $proxy_port).start( 'some.www.server' ) {|http|
- # always connect to your.proxy.addr:8080
- :
+ :
+ Net::HTTP::Proxy($proxy_addr, $proxy_port).start('some.www.server') {|http|
+ # always connect to your.proxy.addr:8080
+ :
}
Since Net::HTTP.Proxy() returns Net::HTTP itself when $proxy_addr is nil,
there's no need to change code if there's proxy or not.
-=== Redirect
+=== Following Redirection
require 'net/http'
- require 'uri'
- def read_uri( uri )
- response = HTTP.get_uri(URI.parse(uri))
+ def read_uri( uri_str )
+ response = Net::HTTP.get_response(URI.parse(uri_str))
case response
when Net::HTTPSuccess then response
when Net::HTTPRedirection then read_uri(response['location'])
@@ -80,7 +81,7 @@ there's no need to change code if there's proxy or not.
end
end
- p read_uri('http://www.ruby-lang.org')
+ print read_uri('http://www.ruby-lang.org')
Net::HTTPSuccess and Net::HTTPRedirection is a HTTPResponse class.
All HTTPResponse objects belong to its own response class which
@@ -192,12 +193,26 @@ This function is not thread-safe.
This method returns the return value of the block.
+: get_print( uri )
+: get_print( address, path, port = 80 )
+ gets entity body from the target and output it to stdout.
+
+ Net::HTTP.get_print URI.parse('http://www.example.com')
+
+: get( uri )
: get( address, path, port = 80 )
- gets entity body from path and returns it.
- return value is a String.
+ send GET request to the target and get a response.
+ This method returns a String.
-: get_print( address, path, port = 80 )
- gets entity body from path and output it to $stdout.
+ print Net::HTTP.get(URI.parse('http://www.example.com'))
+
+: get_response( uri )
+: get_response( address, path, port = 80 )
+ send GET request to the target and get a response.
+ This method returns a Net::HTTPResponse object.
+
+ res = Net::HTTP.get_response(URI.parse('http://www.example.com'))
+ print res.body
: Proxy( address, port = 80 )
creates a HTTP proxy class.
@@ -492,6 +507,7 @@ All arguments named KEY is case-insensitive.
=end
require 'net/protocol'
+require 'uri'
module Net
@@ -534,33 +550,48 @@ module Net
# short cut methods
#
- def HTTP.get_print( addr, path, port = nil )
- new( addr, port || HTTP.default_port ).start {|http|
+ def HTTP.get_print( arg1, arg2 = nil, port = nil )
+ if arg2
+ addr, path = arg1, arg2
+ else
+ uri = arg1
+ addr = uri.host
+ path = uri.request_uri
+ port = uri.port
+ end
+ new(addr, port || HTTP.default_port).start {|http|
http.get path, nil, $stdout
}
nil
end
def HTTP.get( arg1, arg2 = nil, arg3 = nil )
+ get_response(arg1,arg2,arg3).body
+ end
+
+ def HTTP.get_response( arg1, arg2 = nil, arg3 = nil )
if arg2 then
- get_path(arg1, arg2, arg3).body
+ get_by_path(arg1, arg2, arg3)
else
- get_uri(arg1).body
+ get_by_uri(arg1)
end
end
- def HTTP.get_path( addr, path, port = nil )
+ def HTTP.get_by_path( addr, path, port = nil )
new( addr, port || HTTP.default_port ).start {|http|
return http.request(Get.new(path))
}
end
- private_class_method :get_path
+ private_class_method :get_by_path
- def HTTP.get_uri( uri )
- new(uri.addr, uri.port).start {|http|
- return http.request(Get.new(http_path(uri)))
+ def HTTP.get_by_uri( uri )
+ # Should we allow this?
+ # uri = URI.parse(uri) unless uri.respond_to?(:host)
+ new(uri.host, uri.port).start {|http|
+ return http.request(Get.new(uri.request_uri))
}
end
+ private_class_method :get_by_uri
#