2003-08-01 19:41:41 +04:00
|
|
|
#
|
2003-08-01 18:01:14 +04:00
|
|
|
# = net/http.rb
|
2003-08-01 19:41:41 +04:00
|
|
|
#
|
2003-08-01 18:01:14 +04:00
|
|
|
# Copyright (c) 1999-2003 Yukihiro Matsumoto
|
|
|
|
# Copyright (c) 1999-2003 Minero Aoki
|
|
|
|
#
|
2003-08-31 17:04:25 +04:00
|
|
|
# Written & maintained by Minero Aoki <aamine@loveruby.net>.
|
|
|
|
#
|
2003-08-01 18:01:14 +04:00
|
|
|
# This file is derived from "http-access.rb".
|
2003-08-31 17:04:25 +04:00
|
|
|
#
|
|
|
|
# Documented by Minero Aoki; converted to RDoc by William Webber.
|
2003-08-01 18:01:14 +04:00
|
|
|
#
|
|
|
|
# This program is free software. You can re-distribute and/or
|
|
|
|
# modify this program under the same terms of ruby itself ---
|
2003-08-31 17:04:25 +04:00
|
|
|
# Ruby Distribution License or GNU General Public License.
|
|
|
|
#
|
|
|
|
# See Net:::HTTP for an overview and examples.
|
2003-08-01 18:01:14 +04:00
|
|
|
#
|
|
|
|
# NOTE: You can find Japanese version of this document here:
|
2003-12-04 04:27:39 +03:00
|
|
|
# http://www.ruby-lang.org/ja/man/?cmd=view;name=net%2Fhttp.rb
|
2003-08-01 18:01:14 +04:00
|
|
|
#
|
2003-08-31 17:04:25 +04:00
|
|
|
#--
|
2003-08-01 18:01:14 +04:00
|
|
|
# $Id$
|
|
|
|
#++
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2000-06-12 20:42:46 +04:00
|
|
|
require 'net/protocol'
|
2002-07-12 01:33:38 +04:00
|
|
|
require 'uri'
|
2000-06-12 20:42:46 +04:00
|
|
|
|
|
|
|
|
2003-08-21 13:07:57 +04:00
|
|
|
module Net # :nodoc:
|
2000-06-12 20:42:46 +04:00
|
|
|
|
2003-08-21 13:07:57 +04:00
|
|
|
# :stopdoc:
|
2000-06-12 20:42:46 +04:00
|
|
|
class HTTPBadResponse < StandardError; end
|
2001-02-23 02:23:57 +03:00
|
|
|
class HTTPHeaderSyntaxError < StandardError; end
|
2003-08-21 13:07:57 +04:00
|
|
|
# :startdoc:
|
2000-06-12 20:42:46 +04:00
|
|
|
|
2003-08-31 17:04:25 +04:00
|
|
|
# == What Is This Library?
|
|
|
|
#
|
|
|
|
# This library provides your program functions to access WWW
|
|
|
|
# documents via HTTP, Hyper Text Transfer Protocol version 1.1.
|
|
|
|
# For details of HTTP, refer [RFC2616]
|
|
|
|
# ((<URL:http://www.ietf.org/rfc/rfc2616.txt>)).
|
|
|
|
#
|
|
|
|
# == Examples
|
|
|
|
#
|
|
|
|
# === Getting Document From WWW Server
|
|
|
|
#
|
|
|
|
# (formal version)
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
# Net::HTTP.start('www.example.com', 80) { |http|
|
|
|
|
# response = http.get('/index.html')
|
|
|
|
# puts response.body
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# (shorter version)
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
# Net::HTTP.get_print 'www.example.com', '/index.html'
|
|
|
|
#
|
|
|
|
# or
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
# require 'uri'
|
|
|
|
# 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/search.rb', 'query=ruby')
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# === Accessing via Proxy
|
|
|
|
#
|
|
|
|
# Net::HTTP.Proxy creates http proxy class. It has same
|
|
|
|
# methods of Net::HTTP but its instances always connect to
|
|
|
|
# proxy, instead of given host.
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
#
|
|
|
|
# proxy_addr = 'your.proxy.host'
|
|
|
|
# proxy_port = 8080
|
|
|
|
# :
|
|
|
|
# Net::HTTP::Proxy(proxy_addr, proxy_port).start('www.example.com') {|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.
|
|
|
|
#
|
|
|
|
# There are two additional parameters in Net::HTTP.Proxy which allow to
|
|
|
|
# specify proxy user name and password:
|
|
|
|
#
|
|
|
|
# Net::HTTP::Proxy(proxy_addr, proxy_port, proxy_user = nil, proxy_pass = nil)
|
|
|
|
#
|
|
|
|
# You may use them to work with authorization-enabled proxies:
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
# require 'uri'
|
|
|
|
#
|
|
|
|
# proxy_host = 'your.proxy.host'
|
|
|
|
# proxy_port = 8080
|
|
|
|
# uri = URI.parse(ENV['http_proxy'])
|
|
|
|
# proxy_user, proxy_pass = uri.userinfo.split(/:/) if uri.userinfo
|
|
|
|
# Net::HTTP::Proxy(proxy_host, proxy_port,
|
|
|
|
# proxy_user, proxy_pass).start('www.example.com') {|http|
|
|
|
|
# # always connect to your.proxy.addr:8080 using specified username and password
|
|
|
|
# :
|
|
|
|
# }
|
2003-12-02 17:18:49 +03:00
|
|
|
#
|
|
|
|
# Note that net/http never rely on HTTP_PROXY environment variable.
|
|
|
|
# If you want to use proxy, set it explicitly.
|
2003-08-31 17:04:25 +04:00
|
|
|
#
|
|
|
|
# === Following Redirection
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
# require 'uri'
|
|
|
|
#
|
|
|
|
# def fetch( uri_str, limit = 10 )
|
|
|
|
# # You should choose better exception.
|
|
|
|
# raise ArgumentError, 'http redirect too deep' if limit == 0
|
|
|
|
#
|
|
|
|
# response = Net::HTTP.get_response(URI.parse(uri_str))
|
|
|
|
# case response
|
|
|
|
# when Net::HTTPSuccess then response
|
|
|
|
# when Net::HTTPRedirection then fetch(response['location'], limit - 1)
|
|
|
|
# else
|
|
|
|
# response.error!
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# print fetch('http://www.ruby-lang.org')
|
|
|
|
#
|
|
|
|
# Net::HTTPSuccess and Net::HTTPRedirection is a HTTPResponse class.
|
|
|
|
# All HTTPResponse objects belong to its own response class which
|
|
|
|
# indicate HTTP result status. For details of response classes,
|
|
|
|
# see section "HTTP Response Classes".
|
|
|
|
#
|
|
|
|
# === Basic Authentication
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
#
|
|
|
|
# Net::HTTP.start('www.example.com') {|http|
|
|
|
|
# req = Net::HTTP::Get.new('/secret-page.html')
|
|
|
|
# req.basic_auth 'account', 'password'
|
|
|
|
# response = http.request(req)
|
|
|
|
# print response.body
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# === HTTP Response Classes
|
2003-08-01 18:01:14 +04:00
|
|
|
#
|
2003-08-31 17:04:25 +04:00
|
|
|
# TODO: write me.
|
|
|
|
#
|
|
|
|
# == Switching Net::HTTP versions
|
|
|
|
#
|
|
|
|
# You can use net/http.rb 1.1 features (bundled with Ruby 1.6)
|
|
|
|
# by calling HTTP.version_1_1. Calling Net::HTTP.version_1_2
|
|
|
|
# allows you to use 1.2 features again.
|
|
|
|
#
|
|
|
|
# # example
|
2003-12-29 14:08:47 +03:00
|
|
|
# Net::HTTP.start {|http1| ...(http1 has 1.2 features)... }
|
2003-08-31 17:04:25 +04:00
|
|
|
#
|
|
|
|
# Net::HTTP.version_1_1
|
2003-12-29 14:08:47 +03:00
|
|
|
# Net::HTTP.start {|http2| ...(http2 has 1.1 features)... }
|
2003-08-31 17:04:25 +04:00
|
|
|
#
|
|
|
|
# Net::HTTP.version_1_2
|
2003-12-29 14:08:47 +03:00
|
|
|
# Net::HTTP.start {|http3| ...(http3 has 1.2 features)... }
|
2003-08-31 17:04:25 +04:00
|
|
|
#
|
|
|
|
# This function is NOT thread-safe.
|
2003-08-01 18:01:14 +04:00
|
|
|
#
|
2003-05-02 19:19:20 +04:00
|
|
|
class HTTP < Protocol
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2003-08-21 13:07:57 +04:00
|
|
|
# :stopdoc:
|
2003-05-02 18:35:01 +04:00
|
|
|
Revision = %q$Revision$.split[1]
|
|
|
|
HTTPVersion = '1.1'
|
2003-08-21 13:07:57 +04:00
|
|
|
@@newimpl = true # for backward compatability
|
|
|
|
# :startdoc:
|
2002-02-07 23:22:39 +03:00
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
# Turns on net/http 1.2 (ruby 1.8) features.
|
|
|
|
# Defaults to ON in ruby 1.8.
|
|
|
|
#
|
|
|
|
# I strongly recommend to call this method always.
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
# Net::HTTP.version_1_2
|
|
|
|
#
|
2002-02-19 15:33:52 +03:00
|
|
|
def HTTP.version_1_2
|
|
|
|
@@newimpl = true
|
|
|
|
end
|
2002-02-07 23:22:39 +03:00
|
|
|
|
2003-08-31 17:04:25 +04:00
|
|
|
# Turns on net/http 1.1 (ruby 1.6) features.
|
2003-08-01 19:41:41 +04:00
|
|
|
# Defaults to OFF in ruby 1.8.
|
2002-02-19 15:33:52 +03:00
|
|
|
def HTTP.version_1_1
|
|
|
|
@@newimpl = false
|
|
|
|
end
|
2001-02-06 14:14:51 +03:00
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
# true if net/http is in version 1.2 mode.
|
|
|
|
# Defaults to true.
|
|
|
|
def HTTP.version_1_2?
|
2002-02-19 15:33:52 +03:00
|
|
|
@@newimpl
|
2001-02-06 14:14:51 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
# true if net/http is in version 1.1 compatible mode.
|
|
|
|
# Defaults to true.
|
|
|
|
def HTTP.version_1_1?
|
|
|
|
not @@newimpl
|
|
|
|
end
|
|
|
|
|
|
|
|
class << HTTP
|
|
|
|
alias is_version_1_1? version_1_1? #:nodoc:
|
|
|
|
alias is_version_1_2? version_1_2? #:nodoc:
|
|
|
|
end
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def HTTP.setimplversion(obj) #:nodoc:
|
2002-02-19 15:33:52 +03:00
|
|
|
f = @@newimpl
|
|
|
|
obj.instance_eval { @newimpl = f }
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
2002-02-19 15:33:52 +03:00
|
|
|
private_class_method :setimplversion
|
2001-12-30 22:18:45 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# short cut methods
|
|
|
|
#
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
#
|
|
|
|
# Get body from target and output it to +$stdout+. The
|
|
|
|
# target can either be specified as (+uri+), or as
|
|
|
|
# (+host+, +path+, +port+ = 80); so:
|
|
|
|
#
|
|
|
|
# Net::HTTP.get_print URI.parse('http://www.example.com/index.html')
|
|
|
|
#
|
|
|
|
# or:
|
|
|
|
#
|
|
|
|
# Net::HTTP.get_print('www.example.com', '/index.html')
|
|
|
|
#
|
2003-12-29 14:08:47 +03:00
|
|
|
def HTTP.get_print(arg1, arg2 = nil, port = nil)
|
2002-07-12 01:33:38 +04:00
|
|
|
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|
|
2003-08-01 19:41:41 +04:00
|
|
|
http.get path, nil, $stdout
|
2001-12-30 22:18:45 +03:00
|
|
|
}
|
2002-03-26 14:18:02 +03:00
|
|
|
nil
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Send a GET request to the target and return the response
|
|
|
|
# as a string. The target can either be specified as
|
|
|
|
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
|
|
|
|
#
|
|
|
|
# print Net::HTTP.get(URI.parse('http://www.example.com/index.html'))
|
|
|
|
#
|
|
|
|
# or:
|
2003-03-28 06:20:13 +03:00
|
|
|
#
|
2003-08-01 18:01:14 +04:00
|
|
|
# print Net::HTTP.get('www.example.com', '/index.html')
|
2003-03-28 06:20:13 +03:00
|
|
|
#
|
2003-12-29 14:08:47 +03:00
|
|
|
def HTTP.get(arg1, arg2 = nil, arg3 = nil)
|
2002-07-12 01:33:38 +04:00
|
|
|
get_response(arg1,arg2,arg3).body
|
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Send a GET request to the target and return the response
|
|
|
|
# as a Net::HTTPResponse object. The target can either be specified as
|
|
|
|
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
|
|
|
|
#
|
|
|
|
# res = Net::HTTP.get_response(URI.parse('http://www.example.com/index.html'))
|
|
|
|
# print res.body
|
|
|
|
#
|
|
|
|
# or:
|
|
|
|
#
|
|
|
|
# res = Net::HTTP.get_response('www.example.com', '/index.html')
|
|
|
|
# print res.body
|
|
|
|
#
|
2003-12-29 14:08:47 +03:00
|
|
|
def HTTP.get_response(arg1, arg2 = nil, arg3 = nil)
|
2002-11-21 14:13:17 +03:00
|
|
|
if arg2
|
2002-07-12 01:33:38 +04:00
|
|
|
get_by_path(arg1, arg2, arg3)
|
2002-03-26 14:18:02 +03:00
|
|
|
else
|
2002-07-12 01:33:38 +04:00
|
|
|
get_by_uri(arg1)
|
2002-03-26 14:18:02 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def HTTP.get_by_path(addr, path, port = nil) #:nodoc:
|
2002-11-21 14:13:17 +03:00
|
|
|
new(addr, port || HTTP.default_port).start {|http|
|
2003-08-01 19:41:41 +04:00
|
|
|
return http.request(Get.new(path))
|
2002-03-26 14:18:02 +03:00
|
|
|
}
|
|
|
|
end
|
2002-07-12 01:33:38 +04:00
|
|
|
private_class_method :get_by_path
|
2002-03-26 14:18:02 +03:00
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def HTTP.get_by_uri(uri) #:nodoc:
|
2002-07-12 01:33:38 +04:00
|
|
|
# Should we allow this?
|
|
|
|
# uri = URI.parse(uri) unless uri.respond_to?(:host)
|
|
|
|
new(uri.host, uri.port).start {|http|
|
2003-08-01 19:41:41 +04:00
|
|
|
return http.request(Get.new(uri.request_uri))
|
2001-12-30 22:18:45 +03:00
|
|
|
}
|
2001-01-13 22:07:15 +03:00
|
|
|
end
|
2002-07-12 01:33:38 +04:00
|
|
|
private_class_method :get_by_uri
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-02-24 07:53:50 +03:00
|
|
|
#
|
2003-05-02 18:35:01 +04:00
|
|
|
# HTTP session management
|
2001-02-24 07:53:50 +03:00
|
|
|
#
|
2000-11-11 02:31:32 +03:00
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# The default port to use for HTTP requests; defaults to 80.
|
2003-05-02 18:35:01 +04:00
|
|
|
def HTTP.default_port
|
|
|
|
80
|
|
|
|
end
|
|
|
|
|
2003-08-05 20:12:05 +04:00
|
|
|
def HTTP.socket_type #:nodoc: obsolete
|
2003-05-02 18:35:01 +04:00
|
|
|
InternetMessageIO
|
|
|
|
end
|
2001-02-06 14:14:51 +03:00
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
class << HTTP
|
2003-08-01 18:01:14 +04:00
|
|
|
# creates a new Net::HTTP object and opens its TCP connection and
|
|
|
|
# HTTP session. If the optional block is given, the newly
|
|
|
|
# created Net::HTTP object is passed to it and closed when the
|
|
|
|
# block finishes. In this case, the return value of this method
|
|
|
|
# is the return value of the block. If no block is given, the
|
|
|
|
# return value of this method is the newly created Net::HTTP object
|
|
|
|
# itself, and the caller is responsible for closing it upon completion.
|
2003-12-29 14:08:47 +03:00
|
|
|
def start(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil, &block) # :yield: +http+
|
2002-11-21 14:13:17 +03:00
|
|
|
new(address, port, p_addr, p_port, p_user, p_pass).start(&block)
|
2000-06-27 17:36:17 +04:00
|
|
|
end
|
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
alias newobj new
|
2000-11-11 02:31:32 +03:00
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Creates a new Net::HTTP object.
|
|
|
|
# If +proxy_addr+ is given, creates an Net::HTTP object with proxy support.
|
|
|
|
# This method does not open the TCP connection.
|
2003-12-29 14:08:47 +03:00
|
|
|
def new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil)
|
2002-11-21 14:13:17 +03:00
|
|
|
obj = Proxy(p_addr, p_port, p_user, p_pass).newobj(address, port)
|
2002-02-19 15:33:52 +03:00
|
|
|
setimplversion obj
|
|
|
|
obj
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Creates a new Net::HTTP object for the specified +address+.
|
|
|
|
# This method does not open the TCP connection.
|
2003-12-29 14:08:47 +03:00
|
|
|
def initialize(address, port = nil)
|
2003-05-02 18:35:01 +04:00
|
|
|
@address = address
|
2003-12-29 14:08:47 +03:00
|
|
|
@port = (port || HTTP.default_port)
|
2003-05-02 18:35:01 +04:00
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
@curr_http_version = HTTPVersion
|
|
|
|
@seems_1_0_server = false
|
2002-03-26 14:18:02 +03:00
|
|
|
@close_on_empty_response = false
|
2003-05-02 18:35:01 +04:00
|
|
|
@socket = nil
|
|
|
|
@started = false
|
|
|
|
|
|
|
|
@open_timeout = 30
|
|
|
|
@read_timeout = 60
|
|
|
|
|
|
|
|
@debug_output = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
2003-08-01 19:41:41 +04:00
|
|
|
"#<#{self.class} #{@address}:#{@port} open=#{started?}>"
|
2003-05-02 18:35:01 +04:00
|
|
|
end
|
|
|
|
|
2003-08-05 20:12:05 +04:00
|
|
|
# *WARNING* This method causes serious security hole.
|
2003-08-31 17:04:25 +04:00
|
|
|
# Never use this method in production code.
|
2003-08-05 20:12:05 +04:00
|
|
|
#
|
|
|
|
# Set an output stream for debugging.
|
|
|
|
#
|
|
|
|
# http = Net::HTTP.new
|
|
|
|
# http.set_debug_output $stderr
|
|
|
|
# http.start { .... }
|
|
|
|
#
|
2003-12-29 14:08:47 +03:00
|
|
|
def set_debug_output(output)
|
2003-11-16 03:48:17 +03:00
|
|
|
warn 'Net::HTTP#set_debug_output called after HTTP started' if started?
|
2003-08-05 20:12:05 +04:00
|
|
|
@debug_output = output
|
2002-02-19 15:33:52 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# The host name to connect to.
|
2003-05-02 18:35:01 +04:00
|
|
|
attr_reader :address
|
2003-08-01 19:41:41 +04:00
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# The port number to connect to.
|
2003-05-02 18:35:01 +04:00
|
|
|
attr_reader :port
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Seconds to wait until connection is opened.
|
|
|
|
# If the HTTP object cannot open a conection in this many seconds,
|
|
|
|
# it raises a TimeoutError exception.
|
2003-05-02 18:35:01 +04:00
|
|
|
attr_accessor :open_timeout
|
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
# Seconds to wait until reading one block (by one read(2) call).
|
2003-08-01 18:01:14 +04:00
|
|
|
# If the HTTP object cannot open a conection in this many seconds,
|
|
|
|
# it raises a TimeoutError exception.
|
2003-05-02 18:35:01 +04:00
|
|
|
attr_reader :read_timeout
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Setter for the read_timeout attribute.
|
2003-12-29 14:08:47 +03:00
|
|
|
def read_timeout=(sec)
|
2003-05-02 18:35:01 +04:00
|
|
|
@socket.read_timeout = sec if @socket
|
|
|
|
@read_timeout = sec
|
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# returns true if the HTTP session is started.
|
2003-05-02 18:35:01 +04:00
|
|
|
def started?
|
|
|
|
@started
|
|
|
|
end
|
|
|
|
|
2003-08-05 20:12:05 +04:00
|
|
|
alias active? started? #:nodoc: obsolete
|
2003-05-02 18:35:01 +04:00
|
|
|
|
2002-03-26 14:18:02 +03:00
|
|
|
attr_accessor :close_on_empty_response
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Opens TCP connection and HTTP session.
|
|
|
|
#
|
|
|
|
# When this method is called with block, gives a HTTP object
|
|
|
|
# to the block and closes the TCP connection / HTTP session
|
|
|
|
# after the block executed.
|
|
|
|
#
|
|
|
|
# When called with a block, returns the return value of the
|
|
|
|
# block; otherwise, returns self.
|
|
|
|
#
|
2003-08-21 13:07:57 +04:00
|
|
|
def start # :yield: http
|
2003-05-02 18:35:01 +04:00
|
|
|
raise IOError, 'HTTP session already opened' if @started
|
|
|
|
if block_given?
|
|
|
|
begin
|
|
|
|
do_start
|
|
|
|
return yield(self)
|
|
|
|
ensure
|
2003-09-29 22:12:20 +04:00
|
|
|
do_finish
|
2003-05-02 18:35:01 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
do_start
|
|
|
|
self
|
|
|
|
end
|
2002-02-19 15:33:52 +03:00
|
|
|
|
|
|
|
def do_start
|
2003-05-02 18:35:01 +04:00
|
|
|
@socket = self.class.socket_type.open(conn_address(), conn_port(),
|
|
|
|
@open_timeout, @read_timeout,
|
|
|
|
@debug_output)
|
|
|
|
on_connect
|
|
|
|
@started = true
|
2002-02-19 15:33:52 +03:00
|
|
|
end
|
2003-05-02 18:35:01 +04:00
|
|
|
private :do_start
|
2002-02-19 15:33:52 +03:00
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
def on_connect
|
|
|
|
end
|
|
|
|
private :on_connect
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Finishes HTTP session and closes TCP connection.
|
2003-09-29 22:12:20 +04:00
|
|
|
# Raises IOError if not started.
|
2003-05-02 18:35:01 +04:00
|
|
|
def finish
|
2003-09-29 22:12:20 +04:00
|
|
|
raise IOError, 'HTTP session not started yet' unless started?
|
|
|
|
do_finish
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_finish
|
|
|
|
@started = false
|
2003-05-02 18:35:01 +04:00
|
|
|
@socket.close if @socket and not @socket.closed?
|
|
|
|
@socket = nil
|
|
|
|
end
|
2003-09-29 22:12:20 +04:00
|
|
|
private :do_finish
|
2002-02-19 15:33:52 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# proxy
|
|
|
|
#
|
|
|
|
|
|
|
|
public
|
|
|
|
|
|
|
|
# no proxy
|
|
|
|
@is_proxy_class = false
|
|
|
|
@proxy_addr = nil
|
|
|
|
@proxy_port = nil
|
2002-11-21 14:13:17 +03:00
|
|
|
@proxy_user = nil
|
2002-11-21 13:59:50 +03:00
|
|
|
@proxy_pass = nil
|
2002-02-19 15:33:52 +03:00
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Creates an HTTP proxy class.
|
|
|
|
# Arguments are address/port of proxy host and username/password
|
|
|
|
# if authorization on proxy server is required.
|
|
|
|
# You can replace the HTTP class with created proxy class.
|
|
|
|
#
|
|
|
|
# If ADDRESS is nil, this method returns self (Net::HTTP).
|
|
|
|
#
|
|
|
|
# # Example
|
|
|
|
# proxy_class = Net::HTTP::Proxy('proxy.example.com', 8080)
|
|
|
|
# :
|
|
|
|
# proxy_class.start('www.ruby-lang.org') {|http|
|
2003-08-01 19:41:41 +04:00
|
|
|
# # connecting proxy.foo.org:8080
|
2003-08-01 18:01:14 +04:00
|
|
|
# :
|
|
|
|
# }
|
|
|
|
#
|
2003-12-29 14:08:47 +03:00
|
|
|
def HTTP.Proxy(p_addr, p_port = nil, p_user = nil, p_pass = nil)
|
2002-11-21 14:13:17 +03:00
|
|
|
return self unless p_addr
|
2002-02-19 15:33:52 +03:00
|
|
|
delta = ProxyDelta
|
|
|
|
proxyclass = Class.new(self)
|
|
|
|
proxyclass.module_eval {
|
2003-08-01 19:41:41 +04:00
|
|
|
include delta
|
|
|
|
# with proxy
|
|
|
|
@is_proxy_class = true
|
|
|
|
@proxy_address = p_addr
|
|
|
|
@proxy_port = p_port || default_port()
|
|
|
|
@proxy_user = p_user
|
|
|
|
@proxy_pass = p_pass
|
2002-02-19 15:33:52 +03:00
|
|
|
}
|
|
|
|
proxyclass
|
|
|
|
end
|
|
|
|
|
|
|
|
class << HTTP
|
2003-08-01 18:01:14 +04:00
|
|
|
# returns true if self is a class which was created by HTTP::Proxy.
|
2001-06-27 03:49:21 +04:00
|
|
|
def proxy_class?
|
|
|
|
@is_proxy_class
|
2000-11-11 02:31:32 +03:00
|
|
|
end
|
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
attr_reader :proxy_address
|
|
|
|
attr_reader :proxy_port
|
2002-11-21 14:13:17 +03:00
|
|
|
attr_reader :proxy_user
|
2002-11-21 13:59:50 +03:00
|
|
|
attr_reader :proxy_pass
|
2000-11-11 02:31:32 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# True if self is a HTTP proxy class
|
2000-11-11 02:31:32 +03:00
|
|
|
def proxy?
|
2002-03-26 14:18:02 +03:00
|
|
|
self.class.proxy_class?
|
2000-11-11 02:31:32 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Address of proxy host. If self does not use a proxy, nil.
|
2000-11-11 02:31:32 +03:00
|
|
|
def proxy_address
|
2002-03-26 14:18:02 +03:00
|
|
|
self.class.proxy_address
|
2000-11-11 02:31:32 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Port number of proxy host. If self does not use a proxy, nil.
|
2000-11-11 02:31:32 +03:00
|
|
|
def proxy_port
|
2002-03-26 14:18:02 +03:00
|
|
|
self.class.proxy_port
|
2000-11-11 02:31:32 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# User name for accessing proxy. If self does not use a proxy, nil.
|
2002-11-21 14:13:17 +03:00
|
|
|
def proxy_user
|
|
|
|
self.class.proxy_user
|
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# User password for accessing proxy. If self does not use a proxy, nil.
|
2002-11-21 14:13:17 +03:00
|
|
|
def proxy_pass
|
|
|
|
self.class.proxy_pass
|
|
|
|
end
|
|
|
|
|
2003-08-05 20:12:05 +04:00
|
|
|
alias proxyaddr proxy_address #:nodoc: obsolete
|
|
|
|
alias proxyport proxy_port #:nodoc: obsolete
|
2001-06-27 03:49:21 +04:00
|
|
|
|
2002-02-07 23:22:39 +03:00
|
|
|
private
|
|
|
|
|
2002-11-21 14:13:17 +03:00
|
|
|
# without proxy
|
2000-06-27 17:36:17 +04:00
|
|
|
|
2002-02-07 23:22:39 +03:00
|
|
|
def conn_address
|
|
|
|
address
|
|
|
|
end
|
2000-11-11 02:31:32 +03:00
|
|
|
|
2002-02-07 23:22:39 +03:00
|
|
|
def conn_port
|
|
|
|
port
|
|
|
|
end
|
2001-02-06 14:14:51 +03:00
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def edit_path(path)
|
2002-02-07 23:22:39 +03:00
|
|
|
path
|
|
|
|
end
|
2001-02-06 14:14:51 +03:00
|
|
|
|
2003-08-05 20:12:05 +04:00
|
|
|
module ProxyDelta #:nodoc: internal use only
|
2001-02-06 14:14:51 +03:00
|
|
|
private
|
2002-02-07 23:22:39 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def conn_address
|
2002-11-21 14:13:17 +03:00
|
|
|
proxy_address()
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def conn_port
|
2002-11-21 14:13:17 +03:00
|
|
|
proxy_port()
|
2001-02-06 14:14:51 +03:00
|
|
|
end
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def edit_path(path)
|
2001-12-30 22:18:45 +03:00
|
|
|
'http://' + addr_port() + path
|
2001-02-06 14:14:51 +03:00
|
|
|
end
|
2002-02-07 23:22:39 +03:00
|
|
|
end
|
2001-02-06 14:14:51 +03:00
|
|
|
|
|
|
|
#
|
2003-05-02 18:35:01 +04:00
|
|
|
# HTTP operations
|
2001-02-06 14:14:51 +03:00
|
|
|
#
|
2000-11-11 02:31:32 +03:00
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
public
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Gets data from +path+ on the connected-to host.
|
|
|
|
# +header+ must be a Hash like { 'Accept' => '*/*', ... }.
|
|
|
|
#
|
2003-08-01 19:41:41 +04:00
|
|
|
# In version 1.1 (ruby 1.6), this method returns a pair of objects,
|
2003-08-01 18:01:14 +04:00
|
|
|
# a Net::HTTPResponse object and the entity body string.
|
2003-08-01 19:41:41 +04:00
|
|
|
# In version 1.2 (ruby 1.8), this method returns a Net::HTTPResponse
|
2003-08-01 18:01:14 +04:00
|
|
|
# object.
|
|
|
|
#
|
|
|
|
# If called with a block, yields each fragment of the
|
|
|
|
# entity body in turn as a string as it is read from
|
|
|
|
# the socket. Note that in this case, the returned response
|
|
|
|
# object will *not* contain a (meaningful) body.
|
|
|
|
#
|
2003-08-01 19:41:41 +04:00
|
|
|
# +dest+ argument is obsolete.
|
|
|
|
# It still works but you must not use it.
|
2003-08-01 18:01:14 +04:00
|
|
|
#
|
|
|
|
# In version 1.1, this method might raise an exception for
|
|
|
|
# 3xx (redirect). In this case you can get a HTTPResponse object
|
|
|
|
# by "anException.response".
|
|
|
|
#
|
|
|
|
# In version 1.2, this method never raises exception.
|
|
|
|
#
|
|
|
|
# # version 1.1 (bundled with Ruby 1.6)
|
|
|
|
# response, body = http.get('/index.html')
|
|
|
|
#
|
2003-08-01 19:41:41 +04:00
|
|
|
# # version 1.2 (bundled with Ruby 1.8 or later)
|
2003-08-01 18:01:14 +04:00
|
|
|
# response = http.get('/index.html')
|
|
|
|
#
|
|
|
|
# # using block
|
|
|
|
# File.open('result.txt', 'w') {|f|
|
2003-08-01 19:41:41 +04:00
|
|
|
# http.get('/~foo/') do |str|
|
|
|
|
# f.write str
|
|
|
|
# end
|
2003-08-01 18:01:14 +04:00
|
|
|
# }
|
2003-08-01 19:41:41 +04:00
|
|
|
#
|
2003-12-29 14:08:47 +03:00
|
|
|
def get(path, initheader = nil, dest = nil, &block) # :yield: +body_segment+
|
2001-12-30 22:18:45 +03:00
|
|
|
res = nil
|
2002-11-21 14:13:17 +03:00
|
|
|
request(Get.new(path, initheader)) {|r|
|
2003-08-01 19:41:41 +04:00
|
|
|
r.read_body dest, &block
|
|
|
|
res = r
|
2001-12-30 22:18:45 +03:00
|
|
|
}
|
2002-11-21 14:13:17 +03:00
|
|
|
unless @newimpl
|
2001-12-30 22:18:45 +03:00
|
|
|
res.value
|
|
|
|
return res, res.body
|
|
|
|
end
|
2000-03-26 12:48:15 +04:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Gets only the header from +path+ on the connected-to host.
|
|
|
|
# +header+ is a Hash like { 'Accept' => '*/*', ... }.
|
|
|
|
#
|
|
|
|
# This method returns a Net::HTTPResponse object.
|
|
|
|
#
|
|
|
|
# In version 1.1, this method might raise an exception for
|
|
|
|
# 3xx (redirect). On the case you can get a HTTPResponse object
|
|
|
|
# by "anException.response".
|
|
|
|
# In version 1.2, this method never raises an exception.
|
|
|
|
#
|
|
|
|
# response = nil
|
|
|
|
# Net::HTTP.start('some.www.server', 80) {|http|
|
2003-08-01 19:41:41 +04:00
|
|
|
# response = http.head('/index.html')
|
2003-08-01 18:01:14 +04:00
|
|
|
# }
|
|
|
|
# p response['content-type']
|
|
|
|
#
|
2003-12-29 14:08:47 +03:00
|
|
|
def head(path, initheader = nil)
|
2002-11-21 14:13:17 +03:00
|
|
|
res = request(Head.new(path, initheader))
|
2003-08-01 19:41:41 +04:00
|
|
|
res.value unless @newimpl
|
2001-12-30 22:18:45 +03:00
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Posts +data+ (must be a String) to +path+. +header+ must be a Hash
|
|
|
|
# like { 'Accept' => '*/*', ... }.
|
|
|
|
#
|
2003-08-01 19:41:41 +04:00
|
|
|
# In version 1.1 (ruby 1.6), this method returns a pair of objects, a
|
2003-08-01 18:01:14 +04:00
|
|
|
# Net::HTTPResponse object and an entity body string.
|
2003-08-01 19:41:41 +04:00
|
|
|
# In version 1.2 (ruby 1.8), this method returns a Net::HTTPReponse object.
|
2003-08-01 18:01:14 +04:00
|
|
|
#
|
|
|
|
# If called with a block, yields each fragment of the
|
|
|
|
# entity body in turn as a string as it are read from
|
|
|
|
# the socket. Note that in this case, the returned response
|
|
|
|
# object will *not* contain a (meaningful) body.
|
|
|
|
#
|
|
|
|
# +dest+ is an alternative method of collecting the body. It
|
|
|
|
# must be an object responding to the "<<" operator (such as
|
|
|
|
# a String or an Array). Each fragment of the entity body
|
|
|
|
# will be "<<"-ed in turn onto +dest+ if provided, and it will
|
|
|
|
# also become the body of the returned response object.
|
|
|
|
#
|
|
|
|
# You must *not* provide both +dest+ and a block; doing so
|
|
|
|
# will result in an ArgumentError.
|
|
|
|
#
|
|
|
|
# In version 1.1, this method might raise an exception for
|
|
|
|
# 3xx (redirect). In this case you can get an HTTPResponse object
|
|
|
|
# by "anException.response".
|
|
|
|
# In version 1.2, this method never raises exception.
|
|
|
|
#
|
|
|
|
# # version 1.1
|
|
|
|
# response, body = http.post('/cgi-bin/search.rb', 'query=foo')
|
|
|
|
#
|
|
|
|
# # version 1.2
|
|
|
|
# response = http.post('/cgi-bin/search.rb', 'query=foo')
|
|
|
|
#
|
|
|
|
# # using block
|
|
|
|
# File.open('result.txt', 'w') {|f|
|
2003-08-01 19:41:41 +04:00
|
|
|
# http.post('/cgi-bin/search.rb', 'query=foo') do |str|
|
|
|
|
# f.write str
|
|
|
|
# end
|
2003-08-01 18:01:14 +04:00
|
|
|
# }
|
2003-12-29 14:08:47 +03:00
|
|
|
#
|
|
|
|
def post(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
|
2001-12-30 22:18:45 +03:00
|
|
|
res = nil
|
2002-11-21 14:13:17 +03:00
|
|
|
request(Post.new(path, initheader), data) {|r|
|
2003-08-01 19:41:41 +04:00
|
|
|
r.read_body dest, &block
|
|
|
|
res = r
|
2001-12-30 22:18:45 +03:00
|
|
|
}
|
2002-11-21 14:13:17 +03:00
|
|
|
unless @newimpl
|
2001-12-30 22:18:45 +03:00
|
|
|
res.value
|
|
|
|
return res, res.body
|
|
|
|
end
|
|
|
|
|
|
|
|
res
|
2000-12-24 23:01:44 +03:00
|
|
|
end
|
2000-03-26 12:48:15 +04:00
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def put(path, data, initheader = nil) #:nodoc:
|
2002-11-21 14:13:17 +03:00
|
|
|
res = request(Put.new(path, initheader), data)
|
2003-12-29 14:08:47 +03:00
|
|
|
res.value unless @newimpl
|
2001-12-30 22:18:45 +03:00
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Sends a GET request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
#
|
|
|
|
# When called with a block, yields an HTTPResponse object.
|
|
|
|
# The body of this response will not have been read yet;
|
|
|
|
# the caller can process it using HTTPResponse#read_body,
|
|
|
|
# if desired.
|
|
|
|
#
|
|
|
|
# Returns the response.
|
|
|
|
#
|
|
|
|
# This method never raises Net::* exceptions.
|
|
|
|
#
|
|
|
|
# response = http.request_get('/index.html')
|
|
|
|
# # The entity body is already read here.
|
|
|
|
# p response['content-type']
|
|
|
|
# puts response.body
|
|
|
|
#
|
|
|
|
# # using block
|
|
|
|
# http.request_get('/index.html') {|response|
|
2003-08-01 19:41:41 +04:00
|
|
|
# p response['content-type']
|
|
|
|
# response.read_body do |str| # read body now
|
|
|
|
# print str
|
|
|
|
# end
|
2003-08-01 18:01:14 +04:00
|
|
|
# }
|
|
|
|
#
|
2003-12-29 14:08:47 +03:00
|
|
|
def request_get(path, initheader = nil, &block) # :yield: +response+
|
|
|
|
request(Get.new(path, initheader), &block)
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Sends a HEAD request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
#
|
|
|
|
# Returns the response.
|
|
|
|
#
|
|
|
|
# This method never raises Net::* exceptions.
|
|
|
|
#
|
|
|
|
# response = http.request_head('/index.html')
|
|
|
|
# p response['content-type']
|
|
|
|
#
|
2003-12-29 14:08:47 +03:00
|
|
|
def request_head(path, initheader = nil, &block)
|
|
|
|
request(Head.new(path, initheader), &block)
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Sends a POST request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
#
|
|
|
|
# When called with a block, yields an HTTPResponse object.
|
|
|
|
# The body of this response will not have been read yet;
|
|
|
|
# the caller can process it using HTTPResponse#read_body,
|
|
|
|
# if desired.
|
|
|
|
#
|
|
|
|
# Returns the response.
|
|
|
|
#
|
|
|
|
# This method never raises Net::* exceptions.
|
|
|
|
#
|
|
|
|
# # example
|
|
|
|
# response = http.request_post('/cgi-bin/nice.rb', 'datadatadata...')
|
|
|
|
# p response.status
|
|
|
|
# puts response.body # body is already read
|
|
|
|
#
|
|
|
|
# # using block
|
|
|
|
# http.request_post('/cgi-bin/nice.rb', 'datadatadata...') {|response|
|
2003-08-01 19:41:41 +04:00
|
|
|
# p response.status
|
|
|
|
# p response['content-type']
|
|
|
|
# response.read_body do |str| # read body now
|
|
|
|
# print str
|
|
|
|
# end
|
2003-08-01 18:01:14 +04:00
|
|
|
# }
|
2003-12-29 14:08:47 +03:00
|
|
|
#
|
|
|
|
def request_post(path, data, initheader = nil, &block) # :yield: +response+
|
2002-11-21 14:13:17 +03:00
|
|
|
request Post.new(path, initheader), data, &block
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def request_put(path, data, initheader = nil, &block) #:nodoc:
|
2002-11-21 14:13:17 +03:00
|
|
|
request Put.new(path, initheader), data, &block
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
|
|
|
|
2003-08-05 20:12:05 +04:00
|
|
|
alias get2 request_get #:nodoc: obsolete
|
|
|
|
alias head2 request_head #:nodoc: obsolete
|
|
|
|
alias post2 request_post #:nodoc: obsolete
|
|
|
|
alias put2 request_put #:nodoc: obsolete
|
2001-12-30 22:18:45 +03:00
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Sends an HTTP request to the HTTP server.
|
|
|
|
# This method also sends DATA string if DATA is given.
|
|
|
|
#
|
|
|
|
# Returns a HTTPResponse object.
|
|
|
|
#
|
|
|
|
# This method never raises Net::* exceptions.
|
|
|
|
#
|
|
|
|
# response = http.send_request('GET', '/index.html')
|
|
|
|
# puts response.body
|
|
|
|
#
|
2003-12-29 14:08:47 +03:00
|
|
|
def send_request(name, path, data = nil, header = nil)
|
2003-08-01 18:01:14 +04:00
|
|
|
r = HTTPGenericRequest.new(name,(data ? true : false),true,path,header)
|
|
|
|
request r, data
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
2000-04-18 13:39:02 +04:00
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Sends an HTTPRequest object REQUEST to the HTTP server.
|
|
|
|
# This method also sends DATA string if REQUEST is a post/put request.
|
|
|
|
# Giving DATA for get/head request causes ArgumentError.
|
|
|
|
#
|
|
|
|
# When called with a block, yields an HTTPResponse object.
|
|
|
|
# The body of this response will not have been read yet;
|
|
|
|
# the caller can process it using HTTPResponse#read_body,
|
|
|
|
# if desired.
|
|
|
|
#
|
|
|
|
# Returns a HTTPResponse object.
|
|
|
|
#
|
|
|
|
# This method never raises Net::* exceptions.
|
2003-12-29 14:08:47 +03:00
|
|
|
#
|
|
|
|
def request(req, body = nil, &block) # :yield: +response+
|
2002-11-21 14:13:17 +03:00
|
|
|
unless started?
|
2001-12-07 13:04:25 +03:00
|
|
|
start {
|
2003-08-01 19:41:41 +04:00
|
|
|
req['connection'] = 'close'
|
|
|
|
return request(req, body, &block)
|
2001-12-07 13:04:25 +03:00
|
|
|
}
|
|
|
|
end
|
2002-11-21 14:13:17 +03:00
|
|
|
if proxy_user()
|
|
|
|
req.proxy_basic_auth proxy_user(), proxy_pass()
|
|
|
|
end
|
2003-12-29 14:08:47 +03:00
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
begin_transport req
|
2003-12-29 14:08:47 +03:00
|
|
|
req.exec @socket, @curr_http_version, edit_path(req.path), body
|
|
|
|
begin
|
|
|
|
res = HTTPResponse.read_new(@socket)
|
|
|
|
end while HTTPContinue === res
|
|
|
|
res.reading_body(@socket, req.response_body_permitted?) {
|
|
|
|
yield res if block_given?
|
|
|
|
}
|
2002-02-19 15:33:52 +03:00
|
|
|
end_transport req, res
|
|
|
|
|
|
|
|
res
|
2000-04-18 13:39:02 +04:00
|
|
|
end
|
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
private
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def begin_transport(req)
|
2002-11-21 14:13:17 +03:00
|
|
|
if @socket.closed?
|
2003-05-02 18:35:01 +04:00
|
|
|
@socket.reopen @open_timeout
|
|
|
|
on_connect
|
1999-12-29 14:14:04 +03:00
|
|
|
end
|
2002-11-21 14:13:17 +03:00
|
|
|
if @seems_1_0_server
|
2002-03-26 14:18:02 +03:00
|
|
|
req['connection'] = 'close'
|
|
|
|
end
|
2002-11-21 14:13:17 +03:00
|
|
|
if not req.response_body_permitted? and @close_on_empty_response
|
2001-02-06 14:14:51 +03:00
|
|
|
req['connection'] = 'close'
|
|
|
|
end
|
2002-11-21 14:13:17 +03:00
|
|
|
req['host'] = addr_port()
|
2002-02-19 15:33:52 +03:00
|
|
|
end
|
2000-03-27 19:52:27 +04:00
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def end_transport(req, res)
|
2002-02-19 15:33:52 +03:00
|
|
|
@curr_http_version = res.http_version
|
2002-11-21 14:13:17 +03:00
|
|
|
if not res.body and @close_on_empty_response
|
2002-03-26 14:18:02 +03:00
|
|
|
D 'Conn close'
|
2001-02-24 07:53:50 +03:00
|
|
|
@socket.close
|
2003-12-29 14:08:47 +03:00
|
|
|
elsif keep_alive?(req, res)
|
2001-02-24 07:53:50 +03:00
|
|
|
D 'Conn keep-alive'
|
2002-11-21 14:13:17 +03:00
|
|
|
if @socket.closed?
|
2001-02-24 07:53:50 +03:00
|
|
|
D 'Conn (but seems 1.0 server)'
|
2001-02-06 14:14:51 +03:00
|
|
|
@seems_1_0_server = true
|
|
|
|
end
|
|
|
|
else
|
2001-02-24 07:53:50 +03:00
|
|
|
D 'Conn close'
|
2000-03-05 13:25:53 +03:00
|
|
|
@socket.close
|
|
|
|
end
|
1999-12-29 14:14:04 +03:00
|
|
|
end
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def keep_alive?(req, res)
|
|
|
|
return false if /close/i =~ req['connection'].to_s
|
|
|
|
return false if @seems_1_0_server
|
|
|
|
return true if /keep-alive/i =~ res['connection'].to_s
|
|
|
|
return false if /close/i =~ res['connection'].to_s
|
|
|
|
return true if /keep-alive/i =~ res['proxy-connection'].to_s
|
|
|
|
return false if /close/i =~ res['proxy-connection'].to_s
|
|
|
|
(@curr_http_version == '1.1')
|
1999-12-29 14:14:04 +03:00
|
|
|
end
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-02-23 02:23:57 +03:00
|
|
|
#
|
|
|
|
# utils
|
|
|
|
#
|
|
|
|
|
2001-02-24 07:53:50 +03:00
|
|
|
private
|
|
|
|
|
|
|
|
def addr_port
|
2002-02-22 15:10:58 +03:00
|
|
|
address + (port == HTTP.default_port ? '' : ":#{port}")
|
2001-02-24 07:53:50 +03:00
|
|
|
end
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def D(msg)
|
|
|
|
return unless @debug_output
|
|
|
|
@debug_output << msg
|
|
|
|
@debug_output << "\n"
|
2001-02-24 07:53:50 +03:00
|
|
|
end
|
|
|
|
|
2001-01-13 22:07:15 +03:00
|
|
|
end
|
2000-12-24 23:01:44 +03:00
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
HTTPSession = HTTP
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
#
|
|
|
|
# Header module.
|
|
|
|
#
|
|
|
|
# Provides access to @header in the mixed-into class as a hash-like
|
|
|
|
# object, except with case-insensitive keys. Also provides
|
|
|
|
# methods for accessing commonly-used header values in a more
|
|
|
|
# convenient format.
|
|
|
|
#
|
2001-02-23 02:23:57 +03:00
|
|
|
module HTTPHeader
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2003-08-05 20:12:05 +04:00
|
|
|
def size #:nodoc: obsolete
|
2001-02-23 02:23:57 +03:00
|
|
|
@header.size
|
1999-12-29 14:14:04 +03:00
|
|
|
end
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2003-08-05 20:12:05 +04:00
|
|
|
alias length size #:nodoc: obsolete
|
2000-06-12 20:42:46 +04:00
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Returns the header field corresponding to the case-insensitive key.
|
|
|
|
# For example, a key of "Content-Type" might return "text/html"
|
2003-12-29 14:08:47 +03:00
|
|
|
def [](key)
|
2002-11-21 14:13:17 +03:00
|
|
|
@header[key.downcase]
|
2001-01-13 22:07:15 +03:00
|
|
|
end
|
2000-10-11 09:27:56 +04:00
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Sets the header field corresponding to the case-insensitive key.
|
2003-12-29 14:08:47 +03:00
|
|
|
def []=(key, val)
|
2002-11-21 14:13:17 +03:00
|
|
|
@header[key.downcase] = val
|
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Returns the header field corresponding to the case-insensitive key.
|
|
|
|
# Returns the default value +args+, or the result of the block, or nil,
|
|
|
|
# if there's no header field named key. See Hash#fetch
|
2003-12-29 14:08:47 +03:00
|
|
|
def fetch(key, *args, &block) #:yield: +key+
|
2002-11-21 14:13:17 +03:00
|
|
|
@header.fetch(key.downcase, *args, &block)
|
2001-01-13 22:07:15 +03:00
|
|
|
end
|
2000-11-07 14:27:16 +03:00
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
# Iterates for each header names and values.
|
2003-12-29 14:08:47 +03:00
|
|
|
def each_header(&block) #:yield: +key+, +value+
|
2002-11-21 14:13:17 +03:00
|
|
|
@header.each(&block)
|
2001-01-13 22:07:15 +03:00
|
|
|
end
|
2000-11-07 14:27:16 +03:00
|
|
|
|
2001-12-13 22:15:21 +03:00
|
|
|
alias each each_header
|
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
# Iterates for each header names.
|
2003-12-29 14:08:47 +03:00
|
|
|
def each_key(&block) #:yield: +key+
|
2002-11-21 14:13:17 +03:00
|
|
|
@header.each_key(&block)
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
2001-01-13 22:07:15 +03:00
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
# Iterates for each header values.
|
2003-12-29 14:08:47 +03:00
|
|
|
def each_value(&block) #:yield: +value+
|
2002-11-21 14:13:17 +03:00
|
|
|
@header.each_value(&block)
|
1999-12-29 14:14:04 +03:00
|
|
|
end
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
# Removes a header field.
|
2003-12-29 14:08:47 +03:00
|
|
|
def delete(key)
|
2002-11-21 14:13:17 +03:00
|
|
|
@header.delete(key.downcase)
|
2002-10-17 11:27:00 +04:00
|
|
|
end
|
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
# true if +key+ header exists.
|
2003-12-29 14:08:47 +03:00
|
|
|
def key?(key)
|
2002-11-21 14:13:17 +03:00
|
|
|
@header.key?(key.downcase)
|
2000-02-21 18:25:37 +03:00
|
|
|
end
|
1999-12-29 14:14:04 +03:00
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
# Returns a Hash consist of header names and values.
|
2001-02-23 02:23:57 +03:00
|
|
|
def to_hash
|
|
|
|
@header.dup
|
|
|
|
end
|
2001-02-07 10:23:09 +03:00
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
# As for #each_header, except the keys are provided in capitalized form.
|
|
|
|
def each_capitalized
|
2001-02-23 02:23:57 +03:00
|
|
|
@header.each do |k,v|
|
2003-12-29 14:08:47 +03:00
|
|
|
yield capitalize(k), v
|
2001-02-23 02:23:57 +03:00
|
|
|
end
|
|
|
|
end
|
1999-12-29 14:14:04 +03:00
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
alias canonical_each each_capitalized
|
|
|
|
|
|
|
|
def capitalize(k)
|
2002-11-21 14:13:17 +03:00
|
|
|
k.split(/-/).map {|i| i.capitalize }.join('-')
|
2000-03-27 19:52:27 +04:00
|
|
|
end
|
2003-12-29 14:08:47 +03:00
|
|
|
private :capitalize
|
2000-03-27 19:52:27 +04:00
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Returns a Range object which represents Range: header field,
|
|
|
|
# or +nil+ if there is no such header.
|
2001-02-23 02:23:57 +03:00
|
|
|
def range
|
2002-02-22 15:10:58 +03:00
|
|
|
s = @header['range'] or return nil
|
2002-11-21 14:13:17 +03:00
|
|
|
s.split(/,/).map {|spec|
|
2003-08-01 19:41:41 +04:00
|
|
|
m = /bytes\s*=\s*(\d+)?\s*-\s*(\d+)?/i.match(spec) or
|
|
|
|
raise HTTPHeaderSyntaxError, "wrong Range: #{spec}"
|
|
|
|
d1 = m[1].to_i
|
|
|
|
d2 = m[2].to_i
|
|
|
|
if m[1] and m[2] then d1..d2
|
|
|
|
elsif m[1] then d1..-1
|
|
|
|
elsif m[2] then -d2..-1
|
|
|
|
else
|
|
|
|
raise HTTPHeaderSyntaxError, 'range is not specified'
|
|
|
|
end
|
2002-02-22 15:10:58 +03:00
|
|
|
}
|
2001-02-23 02:23:57 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Set Range: header from Range (arg r) or beginning index and
|
|
|
|
# length from it (arg i&len).
|
2003-12-29 14:08:47 +03:00
|
|
|
def range=(r, fin = nil)
|
2001-12-30 22:18:45 +03:00
|
|
|
r = (r ... r + fin) if fin
|
2001-02-23 02:23:57 +03:00
|
|
|
case r
|
|
|
|
when Numeric
|
2003-12-29 14:08:47 +03:00
|
|
|
rangestr = (r > 0 ? "0-#{r.to_i - 1}" : "-#{-r.to_i}")
|
2001-02-23 02:23:57 +03:00
|
|
|
when Range
|
|
|
|
first = r.first
|
|
|
|
last = r.last
|
2003-12-29 14:08:47 +03:00
|
|
|
last -= 1 if r.exclude_end?
|
2002-11-21 14:13:17 +03:00
|
|
|
if last == -1
|
2003-12-29 14:08:47 +03:00
|
|
|
rangestr = (first > 0 ? "#{first}-" : "-#{-first}")
|
2001-02-23 02:23:57 +03:00
|
|
|
else
|
2003-12-29 14:08:47 +03:00
|
|
|
raise HTTPHeaderSyntaxError, 'range.first is negative' if first < 0
|
|
|
|
raise HTTPHeaderSyntaxError, 'range.last is negative' if last < 0
|
|
|
|
raise HTTPHeaderSyntaxError, 'must be .first < .last' if first > last
|
|
|
|
rangestr = "#{first}-#{last}"
|
2001-02-23 02:23:57 +03:00
|
|
|
end
|
|
|
|
else
|
|
|
|
raise TypeError, 'Range/Integer is required'
|
|
|
|
end
|
2003-12-29 14:08:47 +03:00
|
|
|
@header['range'] = "bytes=#{rangestr}"
|
2001-02-23 02:23:57 +03:00
|
|
|
r
|
|
|
|
end
|
|
|
|
|
2001-02-24 07:53:50 +03:00
|
|
|
alias set_range range=
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Returns an Integer object which represents the Content-Length: header field
|
|
|
|
# or +nil+ if that field is not provided.
|
2001-02-23 02:23:57 +03:00
|
|
|
def content_length
|
2004-01-07 14:42:21 +03:00
|
|
|
return nil unless @header.key?('content-length')
|
|
|
|
len = @header['content-length'].slice(/\d+/) or
|
2003-12-29 14:08:47 +03:00
|
|
|
raise HTTPHeaderSyntaxError, 'wrong Content-Length format'
|
|
|
|
len.to_i
|
2001-02-23 02:23:57 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Returns "true" if the "transfer-encoding" header is present and
|
|
|
|
# set to "chunked". This is an HTTP/1.1 feature, allowing the
|
|
|
|
# the content to be sent in "chunks" without at the outset
|
|
|
|
# stating the entire content length.
|
2001-02-23 02:23:57 +03:00
|
|
|
def chunked?
|
2003-12-29 14:08:47 +03:00
|
|
|
field = @header['transfer-encoding'] or return false
|
|
|
|
(/(?:\A|[^\-\w])chunked(?:[^\-\w]|\z)/i =~ field) ? true : false
|
2001-02-23 02:23:57 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Returns a Range object which represents Content-Range: header field.
|
|
|
|
# This indicates, for a partial entity body, where this fragment
|
|
|
|
# fits inside the full entity body, as range of byte offsets.
|
2001-02-23 02:23:57 +03:00
|
|
|
def content_range
|
2003-12-29 14:08:47 +03:00
|
|
|
return nil unless @header['content-range']
|
|
|
|
m = %r<bytes\s+(\d+)-(\d+)/(\d+|\*)>i.match(@header['content-range']) or
|
|
|
|
raise HTTPHeaderSyntaxError, 'wrong Content-Range format'
|
2001-02-23 02:23:57 +03:00
|
|
|
m[1].to_i .. m[2].to_i + 1
|
|
|
|
end
|
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
# The length of the range represented in Range: header.
|
2001-02-23 02:23:57 +03:00
|
|
|
def range_length
|
2003-12-29 14:08:47 +03:00
|
|
|
r = content_range() or return nil
|
|
|
|
r.end - r.begin
|
2001-02-23 02:23:57 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Set the Authorization: header for "Basic" authorization.
|
2003-12-29 14:08:47 +03:00
|
|
|
def basic_auth(account, password)
|
2002-11-21 14:13:17 +03:00
|
|
|
@header['authorization'] = basic_encode(account, password)
|
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# Set Proxy-Authorization: header for "Basic" authorization.
|
2003-12-29 14:08:47 +03:00
|
|
|
def proxy_basic_auth(account, password)
|
2003-06-24 11:42:46 +04:00
|
|
|
@header['proxy-authorization'] = basic_encode(account, password)
|
2002-11-21 14:13:17 +03:00
|
|
|
end
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def basic_encode(account, password)
|
2002-11-21 14:13:17 +03:00
|
|
|
'Basic ' + ["#{account}:#{password}"].pack('m').strip
|
2001-02-24 07:53:50 +03:00
|
|
|
end
|
2002-11-21 14:13:17 +03:00
|
|
|
private :basic_encode
|
2001-02-24 07:53:50 +03:00
|
|
|
|
2001-02-23 02:23:57 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
#
|
|
|
|
# Parent of HTTPRequest class. Do not use this directly; use
|
|
|
|
# a subclass of HTTPRequest.
|
|
|
|
#
|
|
|
|
# Mixes in the HTTPHeader module.
|
|
|
|
#
|
2001-07-03 22:13:13 +04:00
|
|
|
class HTTPGenericRequest
|
2001-02-23 02:23:57 +03:00
|
|
|
|
2001-12-13 22:15:21 +03:00
|
|
|
include HTTPHeader
|
2001-02-23 02:23:57 +03:00
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def initialize(m, reqbody, resbody, path, initheader = nil)
|
2001-07-03 22:13:13 +04:00
|
|
|
@method = m
|
|
|
|
@request_has_body = reqbody
|
|
|
|
@response_has_body = resbody
|
2004-01-29 13:28:39 +03:00
|
|
|
raise ArgumentError, "HTTP request path is empty" if path.empty?
|
2001-02-23 02:23:57 +03:00
|
|
|
@path = path
|
2001-07-03 22:13:13 +04:00
|
|
|
|
2002-11-21 14:13:17 +03:00
|
|
|
@header = {}
|
2001-12-30 22:18:45 +03:00
|
|
|
return unless initheader
|
|
|
|
initheader.each do |k,v|
|
2001-02-23 02:23:57 +03:00
|
|
|
key = k.downcase
|
2003-12-29 14:08:47 +03:00
|
|
|
warn "net/http: warning: duplicated HTTP header: #{k}" if @header.key?(key) and $VERBOSE
|
2002-11-21 14:13:17 +03:00
|
|
|
@header[key] = v.strip
|
2001-02-23 02:23:57 +03:00
|
|
|
end
|
2002-11-21 14:13:17 +03:00
|
|
|
@header['accept'] ||= '*/*'
|
2001-02-23 02:23:57 +03:00
|
|
|
end
|
|
|
|
|
2001-07-03 22:13:13 +04:00
|
|
|
attr_reader :method
|
2001-02-23 02:23:57 +03:00
|
|
|
attr_reader :path
|
|
|
|
|
|
|
|
def inspect
|
2002-03-26 14:18:02 +03:00
|
|
|
"\#<#{self.class} #{@method}>"
|
2001-02-23 02:23:57 +03:00
|
|
|
end
|
|
|
|
|
2001-07-03 22:13:13 +04:00
|
|
|
def request_body_permitted?
|
|
|
|
@request_has_body
|
|
|
|
end
|
|
|
|
|
|
|
|
def response_body_permitted?
|
|
|
|
@response_has_body
|
2001-02-24 07:53:50 +03:00
|
|
|
end
|
|
|
|
|
2001-07-03 22:13:13 +04:00
|
|
|
alias body_exist? response_body_permitted?
|
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
#
|
|
|
|
# write
|
|
|
|
#
|
1999-12-29 14:14:04 +03:00
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def exec(sock, ver, path, body) #:nodoc: internal use only
|
2002-11-21 14:13:17 +03:00
|
|
|
if body
|
2003-12-29 14:08:47 +03:00
|
|
|
raise ArgumentError, 'HTTP request body is not permitted' \
|
|
|
|
unless request_body_permitted?
|
2002-02-19 15:33:52 +03:00
|
|
|
send_request_with_body sock, ver, path, body
|
2001-07-03 22:13:13 +04:00
|
|
|
else
|
2002-02-19 15:33:52 +03:00
|
|
|
request sock, ver, path
|
2000-11-07 14:27:16 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2002-03-26 14:18:02 +03:00
|
|
|
private
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def send_request_with_body(sock, ver, path, body)
|
2003-08-01 19:41:41 +04:00
|
|
|
@header['content-length'] = body.length.to_s
|
2001-02-24 07:53:50 +03:00
|
|
|
@header.delete 'transfer-encoding'
|
2002-11-21 14:13:17 +03:00
|
|
|
unless @header['content-type']
|
2003-12-29 14:08:47 +03:00
|
|
|
warn 'net/http: warning: Content-Type did not set; using application/x-www-form-urlencoded' if $VERBOSE
|
2001-12-13 22:15:21 +03:00
|
|
|
@header['content-type'] = 'application/x-www-form-urlencoded'
|
|
|
|
end
|
2001-07-03 22:13:13 +04:00
|
|
|
request sock, ver, path
|
2002-11-21 14:13:17 +03:00
|
|
|
sock.write body
|
2001-01-13 22:07:15 +03:00
|
|
|
end
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def request(sock, ver, path)
|
2003-03-11 14:01:22 +03:00
|
|
|
buf = "#{@method} #{path} HTTP/#{ver}\r\n"
|
2003-12-29 14:08:47 +03:00
|
|
|
each_capitalized do |k,v|
|
2003-03-11 14:01:22 +03:00
|
|
|
buf << k + ': ' + v + "\r\n"
|
2001-01-13 22:07:15 +03:00
|
|
|
end
|
2003-03-11 14:01:22 +03:00
|
|
|
buf << "\r\n"
|
|
|
|
sock.write buf
|
2001-07-03 22:13:13 +04:00
|
|
|
end
|
2001-01-13 22:07:15 +03:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
#
|
|
|
|
# HTTP request class. This class wraps request header and entity path.
|
|
|
|
# You *must* use its subclass, Net::HTTP::Get, Post, Head.
|
|
|
|
#
|
2001-07-03 22:13:13 +04:00
|
|
|
class HTTPRequest < HTTPGenericRequest
|
2003-08-01 18:01:14 +04:00
|
|
|
|
|
|
|
# Creates HTTP request object.
|
2003-12-29 14:08:47 +03:00
|
|
|
def initialize(path, initheader = nil)
|
2002-03-26 14:18:02 +03:00
|
|
|
super self.class::METHOD,
|
|
|
|
self.class::REQUEST_HAS_BODY,
|
|
|
|
self.class::RESPONSE_HAS_BODY,
|
2001-12-30 22:18:45 +03:00
|
|
|
path, initheader
|
2001-07-03 22:13:13 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2001-02-07 10:23:09 +03:00
|
|
|
class HTTP
|
2001-02-06 14:14:51 +03:00
|
|
|
|
2001-12-13 22:15:21 +03:00
|
|
|
class Get < HTTPRequest
|
2001-02-07 10:23:09 +03:00
|
|
|
METHOD = 'GET'
|
2001-07-03 22:13:13 +04:00
|
|
|
REQUEST_HAS_BODY = false
|
|
|
|
RESPONSE_HAS_BODY = true
|
2001-02-07 10:23:09 +03:00
|
|
|
end
|
2001-02-06 14:14:51 +03:00
|
|
|
|
2001-12-13 22:15:21 +03:00
|
|
|
class Head < HTTPRequest
|
2001-02-07 10:23:09 +03:00
|
|
|
METHOD = 'HEAD'
|
2001-07-03 22:13:13 +04:00
|
|
|
REQUEST_HAS_BODY = false
|
|
|
|
RESPONSE_HAS_BODY = false
|
2001-02-07 10:23:09 +03:00
|
|
|
end
|
2001-02-06 14:14:51 +03:00
|
|
|
|
2001-12-13 22:15:21 +03:00
|
|
|
class Post < HTTPRequest
|
2001-02-07 10:23:09 +03:00
|
|
|
METHOD = 'POST'
|
2001-07-03 22:13:13 +04:00
|
|
|
REQUEST_HAS_BODY = true
|
|
|
|
RESPONSE_HAS_BODY = true
|
2001-02-07 10:23:09 +03:00
|
|
|
end
|
|
|
|
|
2001-12-13 22:15:21 +03:00
|
|
|
class Put < HTTPRequest
|
2001-02-07 10:23:09 +03:00
|
|
|
METHOD = 'PUT'
|
2001-07-03 22:13:13 +04:00
|
|
|
REQUEST_HAS_BODY = true
|
|
|
|
RESPONSE_HAS_BODY = true
|
2001-02-07 10:23:09 +03:00
|
|
|
end
|
2001-02-06 14:14:51 +03:00
|
|
|
|
2001-02-07 20:17:51 +03:00
|
|
|
end
|
2001-02-06 14:14:51 +03:00
|
|
|
|
|
|
|
|
2001-01-13 22:07:15 +03:00
|
|
|
###
|
2003-05-02 18:35:01 +04:00
|
|
|
### Response
|
2001-01-13 22:07:15 +03:00
|
|
|
###
|
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
# HTTP exception class.
|
|
|
|
# You must use its subclasses.
|
2003-05-02 18:35:01 +04:00
|
|
|
module HTTPExceptions
|
2003-12-29 14:08:47 +03:00
|
|
|
def initialize(msg, res) #:nodoc:
|
2003-05-02 18:35:01 +04:00
|
|
|
super msg
|
|
|
|
@response = res
|
|
|
|
end
|
|
|
|
attr_reader :response
|
2003-08-05 20:12:05 +04:00
|
|
|
alias data response #:nodoc: obsolete
|
2003-05-02 18:35:01 +04:00
|
|
|
end
|
|
|
|
class HTTPError < ProtocolError
|
|
|
|
include HTTPExceptions
|
|
|
|
end
|
|
|
|
class HTTPRetriableError < ProtoRetriableError
|
|
|
|
include HTTPExceptions
|
|
|
|
end
|
|
|
|
class HTTPServerException < ProtoServerError
|
2003-08-01 18:01:14 +04:00
|
|
|
# We cannot use the name "HTTPServerError", it is the name of the response.
|
2003-05-02 18:35:01 +04:00
|
|
|
include HTTPExceptions
|
|
|
|
end
|
|
|
|
class HTTPFatalError < ProtoFatalError
|
|
|
|
include HTTPExceptions
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# HTTP response class. This class wraps response header and entity.
|
|
|
|
# Mixes in the HTTPHeader module, which provides access to response
|
|
|
|
# header values both via hash-like methods and individual readers.
|
|
|
|
# Note that each possible HTTP response code defines its own
|
|
|
|
# HTTPResponse subclass. These are listed below.
|
|
|
|
# All classes are
|
|
|
|
# defined under the Net module. Indentation indicates inheritance.
|
|
|
|
#
|
|
|
|
# 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
|
2003-08-01 19:41:41 +04:00
|
|
|
#
|
2002-02-19 15:33:52 +03:00
|
|
|
class HTTPResponse
|
2003-08-01 19:41:41 +04:00
|
|
|
# true if the reponse has body.
|
|
|
|
def HTTPResponse.body_permitted?
|
2002-02-19 15:33:52 +03:00
|
|
|
self::HAS_BODY
|
|
|
|
end
|
|
|
|
|
2003-08-05 20:12:05 +04:00
|
|
|
def HTTPResponse.exception_type # :nodoc: internal use only
|
2002-02-19 15:33:52 +03:00
|
|
|
self::EXCEPTION_TYPE
|
|
|
|
end
|
2003-08-01 19:41:41 +04:00
|
|
|
end # redefined after
|
2002-02-19 15:33:52 +03:00
|
|
|
|
2003-08-21 13:07:57 +04:00
|
|
|
# :stopdoc:
|
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
class HTTPUnknownResponse < HTTPResponse
|
|
|
|
HAS_BODY = true
|
2003-05-02 18:35:01 +04:00
|
|
|
EXCEPTION_TYPE = HTTPError
|
2002-02-19 15:33:52 +03:00
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPInformation < HTTPResponse # 1xx
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = false
|
2003-05-02 18:35:01 +04:00
|
|
|
EXCEPTION_TYPE = HTTPError
|
2002-02-19 15:33:52 +03:00
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPSuccess < HTTPResponse # 2xx
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
2003-05-02 18:35:01 +04:00
|
|
|
EXCEPTION_TYPE = HTTPError
|
2002-02-19 15:33:52 +03:00
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPRedirection < HTTPResponse # 3xx
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
2003-05-02 18:35:01 +04:00
|
|
|
EXCEPTION_TYPE = HTTPRetriableError
|
2002-02-19 15:33:52 +03:00
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPClientError < HTTPResponse # 4xx
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
2003-05-02 18:35:01 +04:00
|
|
|
EXCEPTION_TYPE = HTTPServerException # for backward compatibility
|
2002-02-19 15:33:52 +03:00
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPServerError < HTTPResponse # 5xx
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
2003-05-02 18:35:01 +04:00
|
|
|
EXCEPTION_TYPE = HTTPFatalError # for backward compatibility
|
2002-02-19 15:33:52 +03:00
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
|
|
|
|
class HTTPContinue < HTTPInformation # 100
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = false
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPSwitchProtocol < HTTPInformation # 101
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = false
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
|
|
|
|
class HTTPOK < HTTPSuccess # 200
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPCreated < HTTPSuccess # 201
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPAccepted < HTTPSuccess # 202
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPNonAuthoritativeInformation < HTTPSuccess # 203
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPNoContent < HTTPSuccess # 204
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = false
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPResetContent < HTTPSuccess # 205
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = false
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPPartialContent < HTTPSuccess # 206
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
|
|
|
|
class HTTPMultipleChoice < HTTPRedirection # 300
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPMovedPermanently < HTTPRedirection # 301
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPFound < HTTPRedirection # 302
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
HTTPMovedTemporarily = HTTPFound
|
|
|
|
class HTTPSeeOther < HTTPRedirection # 303
|
|
|
|
HAS_BODY = true
|
|
|
|
end
|
|
|
|
class HTTPNotModified < HTTPRedirection # 304
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = false
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPUseProxy < HTTPRedirection # 305
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = false
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
# 306 unused
|
|
|
|
class HTTPTemporaryRedirect < HTTPRedirection # 307
|
|
|
|
HAS_BODY = true
|
|
|
|
end
|
|
|
|
|
|
|
|
class HTTPBadRequest < HTTPClientError # 400
|
|
|
|
HAS_BODY = true
|
|
|
|
end
|
|
|
|
class HTTPUnauthorized < HTTPClientError # 401
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPPaymentRequired < HTTPClientError # 402
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPForbidden < HTTPClientError # 403
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPNotFound < HTTPClientError # 404
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPMethodNotAllowed < HTTPClientError # 405
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPNotAcceptable < HTTPClientError # 406
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPProxyAuthenticationRequired < HTTPClientError # 407
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPRequestTimeOut < HTTPClientError # 408
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPConflict < HTTPClientError # 409
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPGone < HTTPClientError # 410
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPLengthRequired < HTTPClientError # 411
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPPreconditionFailed < HTTPClientError # 412
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPRequestEntityTooLarge < HTTPClientError # 413
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPRequestURITooLong < HTTPClientError # 414
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
HTTPRequestURITooLarge = HTTPRequestURITooLong
|
|
|
|
class HTTPUnsupportedMediaType < HTTPClientError # 415
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPRequestedRangeNotSatisfiable < HTTPClientError # 416
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPExpectationFailed < HTTPClientError # 417
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
|
|
|
|
class HTTPInternalServerError < HTTPServerError # 500
|
|
|
|
HAS_BODY = true
|
|
|
|
end
|
|
|
|
class HTTPNotImplemented < HTTPServerError # 501
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPBadGateway < HTTPServerError # 502
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPServiceUnavailable < HTTPServerError # 503
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPGatewayTimeOut < HTTPServerError # 504
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
class HTTPVersionNotSupported < HTTPServerError # 505
|
2002-02-19 15:33:52 +03:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
|
|
|
|
2003-08-21 13:07:57 +04:00
|
|
|
# :startdoc:
|
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
|
|
|
|
class HTTPResponse # redefine
|
2001-02-23 02:23:57 +03:00
|
|
|
|
2001-02-07 20:17:51 +03:00
|
|
|
CODE_CLASS_TO_OBJ = {
|
2002-02-19 15:33:52 +03:00
|
|
|
'1' => HTTPInformation,
|
|
|
|
'2' => HTTPSuccess,
|
|
|
|
'3' => HTTPRedirection,
|
|
|
|
'4' => HTTPClientError,
|
|
|
|
'5' => HTTPServerError
|
2000-10-11 09:27:56 +04:00
|
|
|
}
|
2001-02-07 20:17:51 +03:00
|
|
|
CODE_TO_OBJ = {
|
2002-02-19 15:33:52 +03:00
|
|
|
'100' => HTTPContinue,
|
2000-10-12 13:54:32 +04:00
|
|
|
'101' => HTTPSwitchProtocol,
|
2000-10-11 09:27:56 +04:00
|
|
|
|
|
|
|
'200' => HTTPOK,
|
|
|
|
'201' => HTTPCreated,
|
|
|
|
'202' => HTTPAccepted,
|
|
|
|
'203' => HTTPNonAuthoritativeInformation,
|
|
|
|
'204' => HTTPNoContent,
|
|
|
|
'205' => HTTPResetContent,
|
|
|
|
'206' => HTTPPartialContent,
|
|
|
|
|
|
|
|
'300' => HTTPMultipleChoice,
|
|
|
|
'301' => HTTPMovedPermanently,
|
2002-03-26 14:18:02 +03:00
|
|
|
'302' => HTTPFound,
|
|
|
|
'303' => HTTPSeeOther,
|
2000-10-11 09:27:56 +04:00
|
|
|
'304' => HTTPNotModified,
|
|
|
|
'305' => HTTPUseProxy,
|
2002-03-26 14:18:02 +03:00
|
|
|
'307' => HTTPTemporaryRedirect,
|
2000-10-11 09:27:56 +04:00
|
|
|
|
|
|
|
'400' => HTTPBadRequest,
|
|
|
|
'401' => HTTPUnauthorized,
|
|
|
|
'402' => HTTPPaymentRequired,
|
|
|
|
'403' => HTTPForbidden,
|
|
|
|
'404' => HTTPNotFound,
|
|
|
|
'405' => HTTPMethodNotAllowed,
|
|
|
|
'406' => HTTPNotAcceptable,
|
|
|
|
'407' => HTTPProxyAuthenticationRequired,
|
|
|
|
'408' => HTTPRequestTimeOut,
|
|
|
|
'409' => HTTPConflict,
|
|
|
|
'410' => HTTPGone,
|
2002-02-19 15:33:52 +03:00
|
|
|
'411' => HTTPLengthRequired,
|
2000-10-11 09:27:56 +04:00
|
|
|
'412' => HTTPPreconditionFailed,
|
|
|
|
'413' => HTTPRequestEntityTooLarge,
|
2002-03-26 14:18:02 +03:00
|
|
|
'414' => HTTPRequestURITooLong,
|
2000-10-11 09:27:56 +04:00
|
|
|
'415' => HTTPUnsupportedMediaType,
|
2002-03-26 14:18:02 +03:00
|
|
|
'416' => HTTPRequestedRangeNotSatisfiable,
|
|
|
|
'417' => HTTPExpectationFailed,
|
2000-10-11 09:27:56 +04:00
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
'501' => HTTPInternalServerError,
|
2000-10-11 09:27:56 +04:00
|
|
|
'501' => HTTPNotImplemented,
|
|
|
|
'502' => HTTPBadGateway,
|
|
|
|
'503' => HTTPServiceUnavailable,
|
|
|
|
'504' => HTTPGatewayTimeOut,
|
|
|
|
'505' => HTTPVersionNotSupported
|
2000-03-27 19:52:27 +04:00
|
|
|
}
|
|
|
|
|
2001-07-03 22:13:13 +04:00
|
|
|
|
|
|
|
class << self
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def read_new(sock) #:nodoc: internal use only
|
2002-02-22 15:10:58 +03:00
|
|
|
httpv, code, msg = read_status_line(sock)
|
2002-03-26 14:18:02 +03:00
|
|
|
res = response_class(code).new(httpv, code, msg)
|
2002-02-22 15:10:58 +03:00
|
|
|
each_response_header(sock) do |k,v|
|
2003-12-29 14:08:47 +03:00
|
|
|
if res.key?(k)
|
2002-02-22 15:10:58 +03:00
|
|
|
res[k] << ', ' << v
|
|
|
|
else
|
|
|
|
res[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def read_status_line(sock)
|
2002-02-19 15:33:52 +03:00
|
|
|
str = sock.readline
|
|
|
|
m = /\AHTTP(?:\/(\d+\.\d+))?\s+(\d\d\d)\s*(.*)\z/in.match(str) or
|
2003-12-29 14:08:47 +03:00
|
|
|
raise HTTPBadResponse, "wrong status line: #{str.dump}"
|
|
|
|
m.captures
|
2002-02-19 15:33:52 +03:00
|
|
|
end
|
2001-07-03 22:13:13 +04:00
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def response_class(code)
|
2002-02-19 15:33:52 +03:00
|
|
|
CODE_TO_OBJ[code] or
|
|
|
|
CODE_CLASS_TO_OBJ[code[0,1]] or
|
|
|
|
HTTPUnknownResponse
|
|
|
|
end
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def each_response_header(sock)
|
2002-11-21 14:13:17 +03:00
|
|
|
while true
|
|
|
|
line = sock.readuntil("\n", true).sub(/\s+\z/, '')
|
2001-07-03 22:13:13 +04:00
|
|
|
break if line.empty?
|
2002-02-19 15:33:52 +03:00
|
|
|
m = /\A([^:]+):\s*/.match(line) or
|
2003-12-29 14:08:47 +03:00
|
|
|
raise HTTPBadResponse, 'wrong header line format'
|
2002-02-22 15:10:58 +03:00
|
|
|
yield m[1], m.post_match
|
2001-07-03 22:13:13 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
# next is to fix bug in RDoc, where the private inside class << self
|
|
|
|
# spills out.
|
|
|
|
public
|
2001-07-03 22:13:13 +04:00
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
include HTTPHeader
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def initialize(httpv, code, msg) #:nodoc: internal use only
|
2002-02-19 15:33:52 +03:00
|
|
|
@http_version = httpv
|
|
|
|
@code = code
|
|
|
|
@message = msg
|
2001-01-13 22:07:15 +03:00
|
|
|
|
|
|
|
@header = {}
|
|
|
|
@body = nil
|
|
|
|
@read = false
|
|
|
|
end
|
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
# The HTTP version supported by the server.
|
2001-02-07 20:17:51 +03:00
|
|
|
attr_reader :http_version
|
2003-08-01 18:01:14 +04:00
|
|
|
|
|
|
|
# HTTP result code string. For example, '302'. You can also
|
|
|
|
# determine the response type by which response subclass the
|
|
|
|
# response object is an instance of.
|
2002-02-19 15:33:52 +03:00
|
|
|
attr_reader :code
|
2003-08-01 18:01:14 +04:00
|
|
|
|
|
|
|
# HTTP result message. For example, 'Not Found'.
|
2002-02-19 15:33:52 +03:00
|
|
|
attr_reader :message
|
2003-08-05 20:12:05 +04:00
|
|
|
alias msg message # :nodoc: obsolete
|
2001-02-07 20:17:51 +03:00
|
|
|
|
2001-01-13 22:07:15 +03:00
|
|
|
def inspect
|
2002-03-26 14:18:02 +03:00
|
|
|
"#<#{self.class} #{@code} readbody=#{@read}>"
|
2001-01-13 22:07:15 +03:00
|
|
|
end
|
|
|
|
|
2003-06-06 11:05:52 +04:00
|
|
|
# For backward compatibility.
|
|
|
|
# To allow Net::HTTP 1.1 style assignment
|
|
|
|
# e.g.
|
|
|
|
# response, body = Net::HTTP.get(....)
|
|
|
|
#
|
|
|
|
def to_ary
|
2003-11-19 08:45:44 +03:00
|
|
|
warn "net/http.rb: warning: Net::HTTP v1.1 style assignment found at #{caller(1)[0]}; use `response = http.get(...)' instead." if $VERBOSE
|
2003-06-06 11:05:52 +04:00
|
|
|
[self, body()]
|
|
|
|
end
|
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
#
|
|
|
|
# response <-> exception relationship
|
|
|
|
#
|
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
def code_type #:nodoc:
|
2002-03-26 14:18:02 +03:00
|
|
|
self.class
|
2001-01-13 22:07:15 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
def error! #:nodoc:
|
2002-03-26 14:18:02 +03:00
|
|
|
raise error_type().new(@code + ' ' + @message.dump, self)
|
2002-02-19 15:33:52 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
def error_type #:nodoc:
|
2002-03-26 14:18:02 +03:00
|
|
|
self.class::EXCEPTION_TYPE
|
2002-02-19 15:33:52 +03:00
|
|
|
end
|
|
|
|
|
2003-08-05 20:12:05 +04:00
|
|
|
# Raises HTTP error if the response is not 2xx.
|
|
|
|
def value
|
2002-11-21 14:13:17 +03:00
|
|
|
error! unless HTTPSuccess === self
|
2002-02-19 15:33:52 +03:00
|
|
|
end
|
2001-01-13 22:07:15 +03:00
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
#
|
2002-03-26 14:18:02 +03:00
|
|
|
# header (for backward compatibility only; DO NOT USE)
|
2001-02-06 14:14:51 +03:00
|
|
|
#
|
2000-12-24 22:39:15 +03:00
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
def response #:nodoc:
|
2001-01-13 22:07:15 +03:00
|
|
|
self
|
|
|
|
end
|
2000-12-24 22:39:15 +03:00
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
alias header response #:nodoc:
|
|
|
|
alias read_header response #:nodoc:
|
2001-01-13 22:07:15 +03:00
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
#
|
2001-01-13 22:07:15 +03:00
|
|
|
# body
|
2001-02-06 14:14:51 +03:00
|
|
|
#
|
2001-01-13 22:07:15 +03:00
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def reading_body(sock, reqmethodallowbody) #:nodoc: internal use only
|
2002-03-26 14:18:02 +03:00
|
|
|
@socket = sock
|
|
|
|
@body_exist = reqmethodallowbody && self.class.body_permitted?
|
2002-11-21 14:13:17 +03:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
self.body # ensure to read body
|
|
|
|
ensure
|
|
|
|
@socket = nil
|
|
|
|
end
|
2002-03-26 14:18:02 +03:00
|
|
|
end
|
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
# Gets entity body. If the block given, yields it to +block+.
|
2003-08-01 18:01:14 +04:00
|
|
|
# The body is provided in fragments, as it is read in from the socket.
|
|
|
|
#
|
|
|
|
# Calling this method a second or subsequent time will return the
|
2003-08-01 19:41:41 +04:00
|
|
|
# already read string.
|
|
|
|
#
|
|
|
|
# http.request_get('/index.html') {|res|
|
|
|
|
# puts res.read_body
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# http.request_get('/index.html') {|res|
|
|
|
|
# p res.read_body.object_id # 538149362
|
|
|
|
# p res.read_body.object_id # 538149362
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# # using iterator
|
|
|
|
# http.request_get('/index.html') {|res|
|
|
|
|
# res.read_body do |segment|
|
|
|
|
# print segment
|
|
|
|
# end
|
|
|
|
# }
|
|
|
|
#
|
2003-12-29 14:08:47 +03:00
|
|
|
def read_body(dest = nil, &block)
|
2002-11-21 14:13:17 +03:00
|
|
|
if @read
|
|
|
|
raise IOError, "#{self.class}\#read_body called twice" if dest or block
|
2001-12-30 22:18:45 +03:00
|
|
|
return @body
|
2001-01-13 22:07:15 +03:00
|
|
|
end
|
2001-12-30 22:18:45 +03:00
|
|
|
to = procdest(dest, block)
|
|
|
|
stream_check
|
2002-11-21 14:13:17 +03:00
|
|
|
if @body_exist
|
2001-12-30 22:18:45 +03:00
|
|
|
read_body_0 to
|
|
|
|
@body = to
|
|
|
|
else
|
|
|
|
@body = nil
|
2001-01-13 22:07:15 +03:00
|
|
|
end
|
2001-12-30 22:18:45 +03:00
|
|
|
@read = true
|
2001-01-13 22:07:15 +03:00
|
|
|
|
|
|
|
@body
|
|
|
|
end
|
|
|
|
|
2003-08-01 19:41:41 +04:00
|
|
|
# Returns the entity body.
|
|
|
|
#
|
|
|
|
# Calling this method a second or subsequent time will return the
|
|
|
|
# already read string.
|
|
|
|
#
|
|
|
|
# http.request_get('/index.html') {|res|
|
|
|
|
# puts res.body
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# http.request_get('/index.html') {|res|
|
|
|
|
# p res.body.object_id # 538149362
|
|
|
|
# p res.body.object_id # 538149362
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
def body
|
|
|
|
read_body()
|
|
|
|
end
|
|
|
|
|
2003-08-05 20:12:05 +04:00
|
|
|
alias entity body #:nodoc: obsolete
|
2001-01-13 22:07:15 +03:00
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
private
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def read_body_0(dest)
|
2002-11-21 14:13:17 +03:00
|
|
|
if chunked?
|
2000-11-16 17:03:20 +03:00
|
|
|
read_chunked dest
|
2003-12-29 14:08:47 +03:00
|
|
|
return
|
1999-12-29 14:14:04 +03:00
|
|
|
end
|
2003-12-29 14:08:47 +03:00
|
|
|
clen = content_length()
|
|
|
|
if clen
|
|
|
|
@socket.read clen, dest, true # ignore EOF
|
|
|
|
return
|
|
|
|
end
|
|
|
|
clen = range_length()
|
|
|
|
if clen
|
|
|
|
@socket.read clen, dest
|
|
|
|
return
|
|
|
|
end
|
|
|
|
@socket.read_all dest
|
2000-11-07 14:27:16 +03:00
|
|
|
end
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def read_chunked(dest)
|
1999-12-29 14:14:04 +03:00
|
|
|
len = nil
|
|
|
|
total = 0
|
2002-11-21 14:13:17 +03:00
|
|
|
while true
|
1999-12-29 14:14:04 +03:00
|
|
|
line = @socket.readline
|
2002-11-21 14:13:17 +03:00
|
|
|
hexlen = line.slice(/[0-9a-fA-F]+/) or
|
2003-12-29 14:08:47 +03:00
|
|
|
raise HTTPBadResponse, "wrong chunk size line: #{line}"
|
2002-11-21 14:13:17 +03:00
|
|
|
len = hexlen.hex
|
1999-12-29 14:14:04 +03:00
|
|
|
break if len == 0
|
2001-12-30 22:18:45 +03:00
|
|
|
@socket.read len, dest; total += len
|
1999-12-29 14:14:04 +03:00
|
|
|
@socket.read 2 # \r\n
|
1999-12-20 23:48:49 +03:00
|
|
|
end
|
2002-11-21 14:13:17 +03:00
|
|
|
until @socket.readline.empty?
|
2002-02-19 15:33:52 +03:00
|
|
|
# none
|
1999-12-29 14:14:04 +03:00
|
|
|
end
|
2000-03-26 12:48:15 +04:00
|
|
|
end
|
|
|
|
|
2001-01-13 22:07:15 +03:00
|
|
|
def stream_check
|
2002-11-21 14:13:17 +03:00
|
|
|
raise IOError, 'try to read body out of block' if @socket.closed?
|
2001-01-13 22:07:15 +03:00
|
|
|
end
|
|
|
|
|
2003-12-29 14:08:47 +03:00
|
|
|
def procdest(dest, block)
|
|
|
|
raise ArgumentError, 'both of arg and block are given for HTTP method' \
|
|
|
|
if dest and block
|
2002-11-21 14:13:17 +03:00
|
|
|
if block
|
2002-02-19 15:33:52 +03:00
|
|
|
ReadAdapter.new(block)
|
2001-01-13 22:07:15 +03:00
|
|
|
else
|
2001-02-17 23:40:50 +03:00
|
|
|
dest || ''
|
2001-01-13 22:07:15 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-12-20 23:48:49 +03:00
|
|
|
end
|
|
|
|
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
|
2003-08-21 13:07:57 +04:00
|
|
|
# :enddoc:
|
|
|
|
|
2003-08-01 18:01:14 +04:00
|
|
|
#--
|
2002-02-19 15:33:52 +03:00
|
|
|
# for backward compatibility
|
2003-07-02 06:05:35 +04:00
|
|
|
class HTTP
|
|
|
|
ProxyMod = ProxyDelta
|
|
|
|
end
|
2003-08-21 13:07:57 +04:00
|
|
|
module NetPrivate
|
2003-07-02 06:05:35 +04:00
|
|
|
HTTPRequest = ::Net::HTTPRequest
|
|
|
|
end
|
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
HTTPInformationCode = HTTPInformation
|
|
|
|
HTTPSuccessCode = HTTPSuccess
|
|
|
|
HTTPRedirectionCode = HTTPRedirection
|
|
|
|
HTTPRetriableCode = HTTPRedirection
|
|
|
|
HTTPClientErrorCode = HTTPClientError
|
|
|
|
HTTPFatalErrorCode = HTTPClientError
|
|
|
|
HTTPServerErrorCode = HTTPServerError
|
2001-12-13 22:15:21 +03:00
|
|
|
HTTPResponceReceiver = HTTPResponse
|
2001-06-27 03:49:21 +04:00
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
end # module Net
|