2003-08-01 11:41:41 -04:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# = net/http.rb
|
2003-08-01 11:41:41 -04:00
|
|
|
#
|
2007-03-16 09:17:10 -04:00
|
|
|
# Copyright (c) 1999-2007 Yukihiro Matsumoto
|
|
|
|
# Copyright (c) 1999-2007 Minero Aoki
|
2005-09-13 02:41:59 -04:00
|
|
|
# Copyright (c) 2001 GOTOU Yuuzou
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-03-06 12:08:21 -05:00
|
|
|
# Written and maintained by Minero Aoki <aamine@loveruby.net>.
|
|
|
|
# HTTPS support added by GOTOU Yuuzou <gotoyuzo@notwork.org>.
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# This file is derived from "http-access.rb".
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
|
|
|
# Documented by Minero Aoki; converted to RDoc by William Webber.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10: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 09:04:25 -04:00
|
|
|
# Ruby Distribution License or GNU General Public License.
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# See Net::HTTP for an overview and examples.
|
|
|
|
#
|
1999-12-17 10:00:13 -05:00
|
|
|
|
2000-06-12 12:42:46 -04:00
|
|
|
require 'net/protocol'
|
2002-07-11 17:33:38 -04:00
|
|
|
require 'uri'
|
2011-08-09 13:56:40 -04:00
|
|
|
autoload :OpenSSL, 'openssl'
|
2000-06-12 12:42:46 -04:00
|
|
|
|
2005-09-13 02:41:59 -04:00
|
|
|
module Net #:nodoc:
|
2000-06-12 12:42:46 -04:00
|
|
|
|
2003-08-21 05:07:57 -04:00
|
|
|
# :stopdoc:
|
2000-06-12 12:42:46 -04:00
|
|
|
class HTTPBadResponse < StandardError; end
|
2001-02-22 18:23:57 -05:00
|
|
|
class HTTPHeaderSyntaxError < StandardError; end
|
2003-08-21 05:07:57 -04:00
|
|
|
# :startdoc:
|
2000-06-12 12:42:46 -04:00
|
|
|
|
2010-11-27 14:22:53 -05:00
|
|
|
# == An HTTP client API for Ruby.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# Net::HTTP provides a rich library which can be used to build HTTP
|
|
|
|
# user-agents. For more details about HTTP see
|
|
|
|
# [RFC2616](http://www.ietf.org/rfc/rfc2616.txt)
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# Net::HTTP is designed to work closely with URI. URI::HTTP#host,
|
|
|
|
# URI::HTTP#port and URI::HTTP#request_uri are designed to work with
|
|
|
|
# Net::HTTP.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# If you are only performing a few GET requests you should try OpenURI.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# == Simple Examples
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 12:31:20 -05:00
|
|
|
# All examples assume you have loaded Net::HTTP with:
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
#
|
|
|
|
# This will also require 'uri' so you don't need to require it separately.
|
|
|
|
#
|
|
|
|
# The Net::HTTP methods in the following section do not persist
|
2010-12-01 10:07:35 -05:00
|
|
|
# connections. They are not recommended if you are performing many HTTP
|
|
|
|
# requests.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# === GET
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# Net::HTTP.get('example.com', '/index.html') # => String
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# === GET by URI
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# uri = URI('http://example.com/index.html?count=10')
|
2010-12-01 10:07:35 -05:00
|
|
|
# Net::HTTP.get(uri) # => String
|
2005-04-20 12:53:27 -04:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# === GET with Dynamic Parameters
|
2005-04-20 12:53:27 -04:00
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# uri = URI('http://example.com/index.html')
|
2010-12-01 10:07:35 -05:00
|
|
|
# params = { :limit => 10, :page => 3 }
|
|
|
|
# uri.query = URI.encode_www_form(params)
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# res = Net::HTTP.get_response(uri)
|
|
|
|
# puts res.body if res.is_a?(Net::HTTPSuccess)
|
2005-04-20 12:53:27 -04:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# === POST
|
2010-11-27 14:22:53 -05:00
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# uri = URI('http://www.example.com/search.cgi')
|
2010-12-01 10:07:35 -05:00
|
|
|
# res = Net::HTTP.post_form(uri, 'q' => 'ruby', 'max' => '50')
|
|
|
|
# puts res.body
|
2010-11-27 14:22:53 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# === POST with Multiple Values
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# uri = URI('http://www.example.com/search.cgi')
|
2010-12-01 10:07:35 -05:00
|
|
|
# res = Net::HTTP.post_form(uri, 'q' => ['ruby', 'perl'], 'max' => '50')
|
|
|
|
# puts res.body
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# == How to use Net::HTTP
|
2005-04-20 12:53:27 -04:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# The following example code can be used as the basis of a HTTP user-agent
|
2010-12-01 12:31:20 -05:00
|
|
|
# which can perform a variety of request types using persistent
|
|
|
|
# connections.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# uri = URI('http://example.com/some_path?query=string')
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# Net::HTTP.start(uri.host, uri.port) do |http|
|
2010-12-01 10:07:35 -05:00
|
|
|
# request = Net::HTTP::Get.new uri.request_uri
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# response = http.request request # Net::HTTPResponse object
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# Net::HTTP::start immediately creates a connection to an HTTP server which
|
|
|
|
# is kept open for the duration of the block. The connection will remain
|
|
|
|
# open for multiple requests in the block if the server indicates it
|
|
|
|
# supports persistent connections.
|
2010-11-29 21:56:14 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# The request types Net::HTTP supports are listed below in the section "HTTP
|
|
|
|
# Request Classes".
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# If you wish to re-use a connection across multiple HTTP requests without
|
|
|
|
# automatically closing it you can use ::new instead of ::start. #request
|
|
|
|
# will automatically open a connection to the server if one is not currently
|
2011-07-22 00:58:10 -04:00
|
|
|
# open. You can manually close the connection with #finish.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# === Response Data
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# uri = URI('http://example.com/index.html')
|
2010-12-01 10:07:35 -05:00
|
|
|
# res = Net::HTTP.get_response(uri)
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# # Headers
|
|
|
|
# res['Set-Cookie'] # => String
|
|
|
|
# res.get_fields('set-cookie') # => Array
|
|
|
|
# res.to_hash['set-cookie'] # => Array
|
|
|
|
# puts "Headers: #{res.to_hash.inspect}"
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# # Status
|
|
|
|
# puts res.code # => '200'
|
|
|
|
# puts res.message # => 'OK'
|
|
|
|
# puts res.class.name # => 'HTTPOK'
|
2003-12-02 09:18:49 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# # Body
|
|
|
|
# puts res.body if res.response_body_permitted?
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-31 09:04:25 -04:00
|
|
|
# === Following Redirection
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# Each Net::HTTPResponse object belongs to a class for its response code.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# For example, all 2XX responses are instances of a Net::HTTPSuccess
|
|
|
|
# subclass, a 3XX response is an instance of a Net::HTTPRedirection
|
|
|
|
# subclass and a 200 response is an instance of the Net::HTTPOK class. For
|
|
|
|
# details of response classes, see the section "HTTP Response Classes"
|
|
|
|
# below.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# Using a case statement you can handle various types of responses properly:
|
|
|
|
#
|
|
|
|
# def fetch(uri_str, limit = 10)
|
|
|
|
# # You should choose a better exception.
|
|
|
|
# raise ArgumentError, 'too many HTTP redirects' if limit == 0
|
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# response = Net::HTTP.get_response(URI(uri_str))
|
2010-12-01 10:07:35 -05:00
|
|
|
#
|
|
|
|
# case response
|
|
|
|
# when Net::HTTPSuccess then
|
|
|
|
# response
|
|
|
|
# when Net::HTTPRedirection then
|
|
|
|
# location = response['location']
|
|
|
|
# warn "redirected to #{location}"
|
|
|
|
# fetch(location, limit - 1)
|
|
|
|
# else
|
|
|
|
# response.value
|
2003-08-31 09:04:25 -04:00
|
|
|
# end
|
2010-12-01 10:07:35 -05:00
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# print fetch('http://www.ruby-lang.org')
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# === POST
|
2010-11-29 21:56:14 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# A POST can be made using the Net::HTTP::Post request class. This example
|
|
|
|
# creates a urlencoded POST body:
|
2010-11-29 21:56:14 -05:00
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# uri = URI('http://www.example.com/todo.cgi')
|
2010-12-01 10:07:35 -05:00
|
|
|
# req = Net::HTTP::Post.new(uri.path)
|
|
|
|
# req.set_form_data('from' => '2005-01-01', 'to' => '2005-03-31')
|
2010-11-29 21:56:14 -05:00
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# res = Net::HTTP.start(uri.hostname, uri.port) do |http|
|
2010-12-01 10:07:35 -05:00
|
|
|
# http.request(req)
|
|
|
|
# end
|
2010-11-29 21:56:14 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# case res
|
|
|
|
# when Net::HTTPSuccess, Net::HTTPRedirection
|
|
|
|
# # OK
|
|
|
|
# else
|
|
|
|
# res.value
|
|
|
|
# end
|
2010-11-29 21:56:14 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# At this time Net::HTTP does not support multipart/form-data. To send
|
|
|
|
# multipart/form-data use Net::HTTPRequest#body= and
|
|
|
|
# Net::HTTPRequest#content_type=:
|
2010-11-29 21:56:14 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# req = Net::HTTP::Post.new(uri.path)
|
|
|
|
# req.body = multipart_data
|
|
|
|
# req.content_type = 'multipart/form-data'
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# Other requests that can contain a body such as PUT can be created in the
|
|
|
|
# same way using the corresponding request class (Net::HTTP::Put).
|
2005-09-13 02:41:59 -04:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# === Setting Headers
|
2005-09-13 02:41:59 -04:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# The following example performs a conditional GET using the
|
|
|
|
# If-Modified-Since header. If the files has not been modified since the
|
|
|
|
# time in the header a Not Modified response will be returned. See RFC 2616
|
|
|
|
# section 9.3 for further details.
|
2005-09-13 02:41:59 -04:00
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# uri = URI('http://example.com/cached_response')
|
2010-12-01 10:07:35 -05:00
|
|
|
# file = File.stat 'cached_response'
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# req = Net::HTTP::Get.new(uri.request_uri)
|
|
|
|
# req['If-Modified-Since'] = file.mtime.rfc2822
|
2010-11-29 21:56:14 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# res = Net::HTTP.start(uri.hostname, uri.port) {|http|
|
|
|
|
# http.request(req)
|
|
|
|
# }
|
2010-11-29 21:56:14 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# open 'cached_response', 'w' do |io|
|
|
|
|
# io.write res.body
|
|
|
|
# end if res.is_a?(Net::HTTPSuccess)
|
|
|
|
#
|
|
|
|
# === Basic Authentication
|
|
|
|
#
|
|
|
|
# Basic authentication is performed according to
|
|
|
|
# [RFC2617](http://www.ietf.org/rfc/rfc2617.txt)
|
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# uri = URI('http://example.com/index.html?key=value')
|
2010-12-01 10:07:35 -05:00
|
|
|
#
|
|
|
|
# req = Net::HTTP::Get.new(uri.request_uri)
|
|
|
|
# req.basic_auth 'user', 'pass'
|
|
|
|
#
|
|
|
|
# res = Net::HTTP.start(uri.hostname, uri.port) {|http|
|
|
|
|
# http.request(req)
|
|
|
|
# }
|
|
|
|
# puts res.body
|
2010-11-29 21:56:14 -05:00
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# === Streaming Response Bodies
|
|
|
|
#
|
|
|
|
# By default Net::HTTP reads an entire response into memory. If you are
|
|
|
|
# handling large files or wish to implement a progress bar you can instead
|
|
|
|
# stream the body directly to an IO.
|
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# uri = URI('http://example.com/large_file')
|
2010-12-01 10:07:35 -05:00
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# Net::HTTP.start(uri.host, uri.port) do |http|
|
2010-12-01 10:07:35 -05:00
|
|
|
# request = Net::HTTP::Get.new uri.request_uri
|
|
|
|
#
|
|
|
|
# http.request request do |response|
|
|
|
|
# open 'large_file', 'w' do |io|
|
|
|
|
# response.read_body do |chunk|
|
|
|
|
# io.write chunk
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# === HTTPS
|
|
|
|
#
|
|
|
|
# HTTPS is enabled for an HTTP connection by Net::HTTP#use_ssl=.
|
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# uri = URI('https://secure.example.com/some_path?query=string')
|
2010-12-01 10:07:35 -05:00
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# Net::HTTP.start(uri.host, uri.port,
|
|
|
|
# :use_ssl => uri.scheme == 'https').start do |http|
|
2010-12-01 10:07:35 -05:00
|
|
|
# request = Net::HTTP::Get.new uri.request_uri
|
|
|
|
#
|
|
|
|
# response = http.request request # Net::HTTPResponse object
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# In previous versions of ruby you would need to require 'net/https' to use
|
|
|
|
# HTTPS. This is no longer true.
|
|
|
|
#
|
|
|
|
# === Proxies
|
|
|
|
#
|
|
|
|
# Net::HTTP::Proxy has the same methods as Net::HTTP but its instances always
|
|
|
|
# connect via the proxy instead of directly to the given host.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# Net::HTTP::Proxy returns a Net::HTTP instance when proxy_addr is nil so
|
|
|
|
# there is no need for conditional code.
|
|
|
|
#
|
|
|
|
# See Net::HTTP::Proxy for further details and examples such as proxies that
|
|
|
|
# require a username and password.
|
|
|
|
#
|
|
|
|
# == HTTP Request Classes
|
|
|
|
#
|
|
|
|
# Here is the HTTP request class hierarchy.
|
|
|
|
#
|
|
|
|
# * Net::HTTPRequest
|
|
|
|
# * Net::HTTP::Get
|
|
|
|
# * Net::HTTP::Head
|
|
|
|
# * Net::HTTP::Post
|
|
|
|
# * Net::HTTP::Put
|
|
|
|
# * Net::HTTP::Proppatch
|
|
|
|
# * Net::HTTP::Lock
|
|
|
|
# * Net::HTTP::Unlock
|
|
|
|
# * Net::HTTP::Options
|
|
|
|
# * Net::HTTP::Propfind
|
|
|
|
# * Net::HTTP::Delete
|
|
|
|
# * Net::HTTP::Move
|
|
|
|
# * Net::HTTP::Copy
|
|
|
|
# * Net::HTTP::Mkcol
|
|
|
|
# * Net::HTTP::Trace
|
|
|
|
#
|
|
|
|
# == HTTP Response Classes
|
|
|
|
#
|
|
|
|
# Here is HTTP response class hierarchy. All classes are defined in Net
|
|
|
|
# module and are subclasses of Net::HTTPResponse.
|
|
|
|
#
|
2011-08-26 18:22:37 -04:00
|
|
|
# HTTPUnknownResponse:: For unhandled HTTP extensions
|
2010-12-01 10:07:35 -05:00
|
|
|
# HTTPInformation:: 1xx
|
|
|
|
# HTTPContinue:: 100
|
|
|
|
# HTTPSwitchProtocol:: 101
|
|
|
|
# HTTPSuccess:: 2xx
|
|
|
|
# HTTPOK:: 200
|
|
|
|
# HTTPCreated:: 201
|
|
|
|
# HTTPAccepted:: 202
|
|
|
|
# HTTPNonAuthoritativeInformation:: 203
|
|
|
|
# HTTPNoContent:: 204
|
|
|
|
# HTTPResetContent:: 205
|
|
|
|
# HTTPPartialContent:: 206
|
|
|
|
# HTTPRedirection:: 3xx
|
|
|
|
# HTTPMultipleChoice:: 300
|
|
|
|
# HTTPMovedPermanently:: 301
|
|
|
|
# HTTPFound:: 302
|
|
|
|
# HTTPSeeOther:: 303
|
|
|
|
# HTTPNotModified:: 304
|
|
|
|
# HTTPUseProxy:: 305
|
|
|
|
# HTTPTemporaryRedirect:: 307
|
|
|
|
# HTTPClientError:: 4xx
|
|
|
|
# HTTPBadRequest:: 400
|
|
|
|
# HTTPUnauthorized:: 401
|
|
|
|
# HTTPPaymentRequired:: 402
|
|
|
|
# HTTPForbidden:: 403
|
|
|
|
# HTTPNotFound:: 404
|
|
|
|
# HTTPMethodNotAllowed:: 405
|
|
|
|
# HTTPNotAcceptable:: 406
|
|
|
|
# HTTPProxyAuthenticationRequired:: 407
|
|
|
|
# HTTPRequestTimeOut:: 408
|
|
|
|
# HTTPConflict:: 409
|
|
|
|
# HTTPGone:: 410
|
|
|
|
# HTTPLengthRequired:: 411
|
|
|
|
# HTTPPreconditionFailed:: 412
|
|
|
|
# HTTPRequestEntityTooLarge:: 413
|
|
|
|
# HTTPRequestURITooLong:: 414
|
|
|
|
# HTTPUnsupportedMediaType:: 415
|
|
|
|
# HTTPRequestedRangeNotSatisfiable:: 416
|
|
|
|
# HTTPExpectationFailed:: 417
|
|
|
|
# HTTPServerError:: 5xx
|
|
|
|
# HTTPInternalServerError:: 500
|
|
|
|
# HTTPNotImplemented:: 501
|
|
|
|
# HTTPBadGateway:: 502
|
|
|
|
# HTTPServiceUnavailable:: 503
|
|
|
|
# HTTPGatewayTimeOut:: 504
|
|
|
|
# HTTPVersionNotSupported:: 505
|
|
|
|
#
|
|
|
|
# There is also the Net::HTTPBadResponse exception which is raised when
|
|
|
|
# there is a protocol error.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-05-02 11:19:20 -04:00
|
|
|
class HTTP < Protocol
|
1999-12-17 10:00:13 -05:00
|
|
|
|
2003-08-21 05:07:57 -04:00
|
|
|
# :stopdoc:
|
2003-05-02 10:35:01 -04:00
|
|
|
Revision = %q$Revision$.split[1]
|
|
|
|
HTTPVersion = '1.1'
|
2007-12-10 11:53:52 -05:00
|
|
|
begin
|
|
|
|
require 'zlib'
|
|
|
|
require 'stringio' #for our purposes (unpacking gzip) lump these together
|
|
|
|
HAVE_ZLIB=true
|
|
|
|
rescue LoadError
|
|
|
|
HAVE_ZLIB=false
|
|
|
|
end
|
2003-08-21 05:07:57 -04:00
|
|
|
# :startdoc:
|
2002-02-07 15:22:39 -05:00
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
# Turns on net/http 1.2 (ruby 1.8) features.
|
2010-11-29 21:56:14 -05:00
|
|
|
# Defaults to ON in ruby 1.8 or later.
|
2002-02-19 07:33:52 -05:00
|
|
|
def HTTP.version_1_2
|
2010-12-09 20:08:45 -05:00
|
|
|
true
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
2001-02-06 06:14:51 -05:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Returns true if net/http is in version 1.2 mode.
|
2003-08-01 11:41:41 -04:00
|
|
|
# Defaults to true.
|
|
|
|
def HTTP.version_1_2?
|
2010-12-09 20:08:45 -05:00
|
|
|
true
|
2001-02-06 06:14:51 -05:00
|
|
|
end
|
|
|
|
|
2011-05-16 18:00:48 -04:00
|
|
|
def HTTP.version_1_1? #:nodoc:
|
2010-12-09 20:08:45 -05:00
|
|
|
false
|
2003-08-01 11:41:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class << HTTP
|
|
|
|
alias is_version_1_1? version_1_1? #:nodoc:
|
|
|
|
alias is_version_1_2? version_1_2? #:nodoc:
|
|
|
|
end
|
|
|
|
|
2001-12-30 14:18:45 -05:00
|
|
|
#
|
|
|
|
# short cut methods
|
|
|
|
#
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# Gets the body text from the target and outputs it to $stdout. The
|
|
|
|
# target can either be specified as
|
|
|
|
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# Net::HTTP.get_print URI('http://www.example.com/index.html')
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
|
|
|
# or:
|
|
|
|
#
|
2005-07-26 09:05:31 -04:00
|
|
|
# Net::HTTP.get_print 'www.example.com', '/index.html'
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2005-07-26 09:05:31 -04:00
|
|
|
def HTTP.get_print(uri_or_host, path = nil, port = nil)
|
|
|
|
get_response(uri_or_host, path, port) {|res|
|
|
|
|
res.read_body do |chunk|
|
|
|
|
$stdout.print chunk
|
|
|
|
end
|
2001-12-30 14:18:45 -05:00
|
|
|
}
|
2002-03-26 06:18:02 -05:00
|
|
|
nil
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Sends a GET request to the target and returns the HTTP response
|
2003-08-01 10:01:14 -04:00
|
|
|
# as a string. The target can either be specified as
|
|
|
|
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# print Net::HTTP.get(URI('http://www.example.com/index.html'))
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
|
|
|
# or:
|
2003-03-27 22:20:13 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# print Net::HTTP.get('www.example.com', '/index.html')
|
2003-03-27 22:20:13 -05:00
|
|
|
#
|
2005-07-26 09:05:31 -04:00
|
|
|
def HTTP.get(uri_or_host, path = nil, port = nil)
|
|
|
|
get_response(uri_or_host, path, port).body
|
2002-07-11 17:33:38 -04:00
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Sends a GET request to the target and returns the HTTP response
|
2003-08-01 10:01:14 -04:00
|
|
|
# as a Net::HTTPResponse object. The target can either be specified as
|
|
|
|
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# res = Net::HTTP.get_response(URI('http://www.example.com/index.html'))
|
2003-08-01 10:01:14 -04:00
|
|
|
# print res.body
|
|
|
|
#
|
|
|
|
# or:
|
|
|
|
#
|
|
|
|
# res = Net::HTTP.get_response('www.example.com', '/index.html')
|
|
|
|
# print res.body
|
|
|
|
#
|
2005-07-26 09:05:31 -04:00
|
|
|
def HTTP.get_response(uri_or_host, path = nil, port = nil, &block)
|
|
|
|
if path
|
|
|
|
host = uri_or_host
|
|
|
|
new(host, port || HTTP.default_port).start {|http|
|
|
|
|
return http.request_get(path, &block)
|
|
|
|
}
|
2002-03-26 06:18:02 -05:00
|
|
|
else
|
2005-07-26 09:05:31 -04:00
|
|
|
uri = uri_or_host
|
2011-11-26 06:44:45 -05:00
|
|
|
start(uri.hostname, uri.port,
|
|
|
|
:use_ssl => uri.scheme == 'https') {|http|
|
2005-07-26 09:05:31 -04:00
|
|
|
return http.request_get(uri.request_uri, &block)
|
|
|
|
}
|
2002-03-26 06:18:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Posts HTML form data to the specified URI object.
|
|
|
|
# The form data must be provided as a Hash mapping from String to String.
|
|
|
|
# Example:
|
2005-04-20 09:55:26 -04:00
|
|
|
#
|
|
|
|
# { "cmd" => "search", "q" => "ruby", "max" => "50" }
|
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# This method also does Basic Authentication iff +url+.user exists.
|
2010-12-09 20:21:13 -05:00
|
|
|
# But userinfo for authentication is deprecated (RFC3986).
|
|
|
|
# So this feature will be removed.
|
2005-04-20 09:55:26 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
# require 'uri'
|
|
|
|
#
|
2010-12-01 11:40:16 -05:00
|
|
|
# HTTP.post_form URI('http://www.example.com/search.cgi'),
|
2005-04-20 09:55:26 -04:00
|
|
|
# { "q" => "ruby", "max" => "50" }
|
|
|
|
#
|
|
|
|
def HTTP.post_form(url, params)
|
2011-06-21 13:44:44 -04:00
|
|
|
req = Post.new(url.request_uri)
|
2005-04-20 09:55:26 -04:00
|
|
|
req.form_data = params
|
|
|
|
req.basic_auth url.user, url.password if url.user
|
2011-11-26 06:44:45 -05:00
|
|
|
start(url.hostname, url.port,
|
|
|
|
:use_ssl => uri.scheme == 'https' ) {|http|
|
2005-04-20 09:55:26 -04:00
|
|
|
http.request(req)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2001-02-23 23:53:50 -05:00
|
|
|
#
|
2003-05-02 10:35:01 -04:00
|
|
|
# HTTP session management
|
2001-02-23 23:53:50 -05:00
|
|
|
#
|
2000-11-10 18:31:32 -05:00
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# The default port to use for HTTP requests; defaults to 80.
|
2003-05-02 10:35:01 -04:00
|
|
|
def HTTP.default_port
|
2004-03-05 12:00:51 -05:00
|
|
|
http_default_port()
|
|
|
|
end
|
|
|
|
|
|
|
|
# The default port to use for HTTP requests; defaults to 80.
|
|
|
|
def HTTP.http_default_port
|
2003-05-02 10:35:01 -04:00
|
|
|
80
|
|
|
|
end
|
|
|
|
|
2004-04-21 04:26:33 -04:00
|
|
|
# The default port to use for HTTPS requests; defaults to 443.
|
2004-03-05 12:00:51 -05:00
|
|
|
def HTTP.https_default_port
|
|
|
|
443
|
|
|
|
end
|
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
def HTTP.socket_type #:nodoc: obsolete
|
2005-09-13 02:41:59 -04:00
|
|
|
BufferedIO
|
2003-05-02 10:35:01 -04:00
|
|
|
end
|
2001-02-06 06:14:51 -05:00
|
|
|
|
2011-08-10 08:28:19 -04:00
|
|
|
# :call-seq:
|
2010-01-18 01:32:14 -05:00
|
|
|
# HTTP.start(address, port, p_addr, p_port, p_user, p_pass, &block)
|
|
|
|
# HTTP.start(address, port=nil, p_addr=nil, p_port=nil, p_user=nil, p_pass=nil, opt, &block)
|
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# Creates a new Net::HTTP object, then additionally opens the TCP
|
|
|
|
# connection and HTTP session.
|
2010-01-12 00:56:28 -05:00
|
|
|
#
|
2011-08-26 18:22:37 -04:00
|
|
|
# Arguments are the following:
|
2010-01-12 00:56:28 -05:00
|
|
|
# _address_ :: hostname or IP address of the server
|
|
|
|
# _port_ :: port of the server
|
|
|
|
# _p_addr_ :: address of proxy
|
|
|
|
# _p_port_ :: port of proxy
|
|
|
|
# _p_user_ :: user of proxy
|
|
|
|
# _p_pass_ :: pass of proxy
|
|
|
|
# _opt_ :: optional hash
|
|
|
|
#
|
|
|
|
# _opt_ sets following values by its accessor.
|
|
|
|
# The keys are ca_file, ca_path, cert, cert_store, ciphers,
|
|
|
|
# close_on_empty_response, key, open_timeout, read_timeout, ssl_timeout,
|
|
|
|
# ssl_version, use_ssl, verify_callback, verify_depth and verify_mode.
|
|
|
|
# If you set :use_ssl as true, you can use https and default value of
|
|
|
|
# verify_mode is set as OpenSSL::SSL::VERIFY_PEER.
|
|
|
|
#
|
|
|
|
# If the optional block is given, the newly
|
2009-03-05 22:56:38 -05:00
|
|
|
# created Net::HTTP object is passed to it and closed when the
|
2004-04-21 04:26:33 -04:00
|
|
|
# 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
|
2010-11-29 21:56:14 -05:00
|
|
|
# itself, and the caller is responsible for closing it upon completion
|
|
|
|
# using the finish() method.
|
2010-01-12 00:56:28 -05:00
|
|
|
def HTTP.start(address, *arg, &block) # :yield: +http+
|
|
|
|
arg.pop if opt = Hash.try_convert(arg[-1])
|
|
|
|
port, p_addr, p_port, p_user, p_pass = *arg
|
2010-01-12 02:48:48 -05:00
|
|
|
port = https_default_port if !port && opt && opt[:use_ssl]
|
2010-01-12 00:56:28 -05:00
|
|
|
http = new(address, port, p_addr, p_port, p_user, p_pass)
|
|
|
|
|
2010-01-12 02:48:48 -05:00
|
|
|
if opt
|
2011-06-05 21:53:41 -04:00
|
|
|
if opt[:use_ssl]
|
|
|
|
opt = {verify_mode: OpenSSL::SSL::VERIFY_PEER}.update(opt)
|
|
|
|
end
|
2010-01-12 02:48:48 -05:00
|
|
|
http.methods.grep(/\A(\w+)=\z/) do |meth|
|
|
|
|
key = $1.to_sym
|
|
|
|
opt.key?(key) or next
|
|
|
|
http.__send__(meth, opt[key])
|
|
|
|
end
|
2010-01-12 00:56:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
http.start(&block)
|
2004-04-21 04:26:33 -04:00
|
|
|
end
|
2000-06-27 09:36:17 -04:00
|
|
|
|
2004-04-21 04:26:33 -04:00
|
|
|
class << HTTP
|
2002-02-19 07:33:52 -05:00
|
|
|
alias newobj new
|
2004-04-21 04:26:33 -04:00
|
|
|
end
|
2000-11-10 18:31:32 -05:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Creates a new Net::HTTP object without opening a TCP connection or
|
|
|
|
# HTTP session.
|
|
|
|
# The +address+ should be a DNS hostname or IP address.
|
|
|
|
# If +p_addr+ is given, creates a Net::HTTP object with proxy support.
|
2004-04-21 04:26:33 -04:00
|
|
|
def HTTP.new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil)
|
2010-12-09 20:08:45 -05:00
|
|
|
Proxy(p_addr, p_port, p_user, p_pass).newobj(address, port)
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Creates a new Net::HTTP object for the specified server address,
|
|
|
|
# without opening the TCP connection or initializing the HTTP session.
|
|
|
|
# The +address+ should be a DNS hostname or IP address.
|
2003-12-29 06:08:47 -05:00
|
|
|
def initialize(address, port = nil)
|
2003-05-02 10:35:01 -04:00
|
|
|
@address = address
|
2003-12-29 06:08:47 -05:00
|
|
|
@port = (port || HTTP.default_port)
|
2002-02-19 07:33:52 -05:00
|
|
|
@curr_http_version = HTTPVersion
|
2007-05-06 04:53:51 -04:00
|
|
|
@no_keepalive_server = false
|
2002-03-26 06:18:02 -05:00
|
|
|
@close_on_empty_response = false
|
2003-05-02 10:35:01 -04:00
|
|
|
@socket = nil
|
|
|
|
@started = false
|
2005-04-20 09:55:26 -04:00
|
|
|
@open_timeout = nil
|
2003-05-02 10:35:01 -04:00
|
|
|
@read_timeout = 60
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
@continue_timeout = nil
|
2003-05-02 10:35:01 -04:00
|
|
|
@debug_output = nil
|
2004-03-05 12:00:51 -05:00
|
|
|
@use_ssl = false
|
2004-03-06 12:08:21 -05:00
|
|
|
@ssl_context = nil
|
2007-09-23 18:21:18 -04:00
|
|
|
@enable_post_connection_check = true
|
2007-12-10 11:53:52 -05:00
|
|
|
@compression = nil
|
2007-12-24 15:47:11 -05:00
|
|
|
@sspi_enabled = false
|
2011-11-26 14:59:11 -05:00
|
|
|
SSL_IVNAMES.each do |ivname|
|
|
|
|
instance_variable_set ivname, nil
|
2008-08-12 01:18:22 -04:00
|
|
|
end
|
2003-05-02 10:35:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
2003-08-01 11:41:41 -04:00
|
|
|
"#<#{self.class} #{@address}:#{@port} open=#{started?}>"
|
2003-05-02 10:35:01 -04:00
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# *WARNING* This method opens a serious security hole.
|
2003-08-31 09:04:25 -04:00
|
|
|
# Never use this method in production code.
|
2003-08-05 12:12:05 -04:00
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# Sets an output stream for debugging.
|
2003-08-05 12:12:05 -04:00
|
|
|
#
|
|
|
|
# http = Net::HTTP.new
|
|
|
|
# http.set_debug_output $stderr
|
|
|
|
# http.start { .... }
|
|
|
|
#
|
2003-12-29 06:08:47 -05:00
|
|
|
def set_debug_output(output)
|
2003-11-15 19:48:17 -05:00
|
|
|
warn 'Net::HTTP#set_debug_output called after HTTP started' if started?
|
2003-08-05 12:12:05 -04:00
|
|
|
@debug_output = output
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# The DNS host name or IP address to connect to.
|
2003-05-02 10:35:01 -04:00
|
|
|
attr_reader :address
|
2003-08-01 11:41:41 -04:00
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# The port number to connect to.
|
2003-05-02 10:35:01 -04:00
|
|
|
attr_reader :port
|
|
|
|
|
2011-06-16 01:24:12 -04:00
|
|
|
# Number of seconds to wait for the connection to open. Any number
|
|
|
|
# may be used, including Floats for fractional seconds. If the HTTP
|
|
|
|
# object cannot open a connection in this many seconds, it raises a
|
|
|
|
# TimeoutError exception.
|
2003-05-02 10:35:01 -04:00
|
|
|
attr_accessor :open_timeout
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Number of seconds to wait for one block to be read (via one read(2)
|
2011-06-16 01:24:12 -04:00
|
|
|
# call). Any number may be used, including Floats for fractional
|
|
|
|
# seconds. If the HTTP object cannot read data in this many seconds,
|
2003-08-01 10:01:14 -04:00
|
|
|
# it raises a TimeoutError exception.
|
2003-05-02 10:35:01 -04:00
|
|
|
attr_reader :read_timeout
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Setter for the read_timeout attribute.
|
2003-12-29 06:08:47 -05:00
|
|
|
def read_timeout=(sec)
|
2003-05-02 10:35:01 -04:00
|
|
|
@socket.read_timeout = sec if @socket
|
|
|
|
@read_timeout = sec
|
|
|
|
end
|
|
|
|
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
# Seconds to wait for 100 Continue response. If the HTTP object does not
|
|
|
|
# receive a response in this many seconds it sends the request body.
|
|
|
|
attr_reader :continue_timeout
|
|
|
|
|
|
|
|
# Setter for the continue_timeout attribute.
|
|
|
|
def continue_timeout=(sec)
|
|
|
|
@socket.continue_timeout = sec if @socket
|
|
|
|
@continue_timeout = sec
|
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Returns true if the HTTP session has been started.
|
2003-05-02 10:35:01 -04:00
|
|
|
def started?
|
|
|
|
@started
|
|
|
|
end
|
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
alias active? started? #:nodoc: obsolete
|
2003-05-02 10:35:01 -04:00
|
|
|
|
2002-03-26 06:18:02 -05:00
|
|
|
attr_accessor :close_on_empty_response
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Returns true if SSL/TLS is being used with HTTP.
|
2004-03-05 12:00:51 -05:00
|
|
|
def use_ssl?
|
2011-06-13 06:21:07 -04:00
|
|
|
@use_ssl
|
2010-01-08 07:17:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Turn on/off SSL.
|
|
|
|
# This flag must be set before starting session.
|
|
|
|
# If you change use_ssl value after session started,
|
|
|
|
# a Net::HTTP object raises IOError.
|
|
|
|
def use_ssl=(flag)
|
2011-08-09 13:56:40 -04:00
|
|
|
flag = flag ? true : false
|
2010-01-08 07:17:15 -05:00
|
|
|
if started? and @use_ssl != flag
|
|
|
|
raise IOError, "use_ssl value changed, but session already started"
|
|
|
|
end
|
|
|
|
@use_ssl = flag
|
|
|
|
end
|
|
|
|
|
2011-11-26 14:59:11 -05:00
|
|
|
SSL_IVNAMES = [
|
2011-11-26 06:06:53 -05:00
|
|
|
:@ca_file,
|
|
|
|
:@ca_path,
|
|
|
|
:@cert,
|
|
|
|
:@cert_store,
|
|
|
|
:@ciphers,
|
|
|
|
:@key,
|
|
|
|
:@ssl_timeout,
|
|
|
|
:@ssl_version,
|
|
|
|
:@verify_callback,
|
|
|
|
:@verify_depth,
|
|
|
|
:@verify_mode,
|
2011-11-10 18:35:35 -05:00
|
|
|
]
|
2011-11-26 14:59:11 -05:00
|
|
|
SSL_ATTRIBUTES = [
|
|
|
|
:ca_file,
|
|
|
|
:ca_path,
|
|
|
|
:cert,
|
|
|
|
:cert_store,
|
|
|
|
:ciphers,
|
|
|
|
:key,
|
|
|
|
:ssl_timeout,
|
|
|
|
:ssl_version,
|
|
|
|
:verify_callback,
|
|
|
|
:verify_depth,
|
|
|
|
:verify_mode,
|
|
|
|
]
|
2010-01-08 07:17:15 -05:00
|
|
|
|
2010-11-30 03:06:19 -05:00
|
|
|
# Sets path of a CA certification file in PEM format.
|
|
|
|
#
|
|
|
|
# The file can contain several CA certificates.
|
|
|
|
attr_accessor :ca_file
|
|
|
|
|
|
|
|
# Sets path of a CA certification directory containing certifications in
|
|
|
|
# PEM format.
|
|
|
|
attr_accessor :ca_path
|
|
|
|
|
|
|
|
# Sets an OpenSSL::X509::Certificate object as client certificate.
|
|
|
|
# (This method is appeared in Michal Rokos's OpenSSL extension).
|
|
|
|
attr_accessor :cert
|
|
|
|
|
|
|
|
# Sets the X509::Store to verify peer certificate.
|
|
|
|
attr_accessor :cert_store
|
|
|
|
|
|
|
|
# Sets the available ciphers. See OpenSSL::SSL::SSLContext#ciphers=
|
|
|
|
attr_accessor :ciphers
|
|
|
|
|
|
|
|
# Sets an OpenSSL::PKey::RSA or OpenSSL::PKey::DSA object.
|
|
|
|
# (This method is appeared in Michal Rokos's OpenSSL extension.)
|
|
|
|
attr_accessor :key
|
|
|
|
|
|
|
|
# Sets the SSL timeout seconds.
|
|
|
|
attr_accessor :ssl_timeout
|
|
|
|
|
|
|
|
# Sets the SSL version. See OpenSSL::SSL::SSLContext#ssl_version=
|
|
|
|
attr_accessor :ssl_version
|
|
|
|
|
|
|
|
# Sets the verify callback for the server certification verification.
|
|
|
|
attr_accessor :verify_callback
|
|
|
|
|
|
|
|
# Sets the maximum depth for the certificate chain verification.
|
|
|
|
attr_accessor :verify_depth
|
|
|
|
|
|
|
|
# Sets the flags for server the certification verification at beginning of
|
|
|
|
# SSL/TLS session.
|
|
|
|
#
|
|
|
|
# OpenSSL::SSL::VERIFY_NONE or OpenSSL::SSL::VERIFY_PEER are acceptable.
|
|
|
|
attr_accessor :verify_mode
|
|
|
|
|
|
|
|
# Returns the X.509 certificates the server presented.
|
2010-01-08 07:17:15 -05:00
|
|
|
def peer_cert
|
|
|
|
if not use_ssl? or not @socket
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
@socket.io.peer_cert
|
2004-03-05 12:00:51 -05:00
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Opens a TCP connection and HTTP session.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# When this method is called with a block, it passes the Net::HTTP
|
|
|
|
# object to the block, and closes the TCP connection and HTTP session
|
|
|
|
# after the block has been executed.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# When called with a block, it returns the return value of the
|
|
|
|
# block; otherwise, it returns self.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2003-08-21 05:07:57 -04:00
|
|
|
def start # :yield: http
|
2003-05-02 10: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 14:12:20 -04:00
|
|
|
do_finish
|
2003-05-02 10:35:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
do_start
|
|
|
|
self
|
|
|
|
end
|
2002-02-19 07:33:52 -05:00
|
|
|
|
|
|
|
def do_start
|
2004-03-06 12:08:21 -05:00
|
|
|
connect
|
|
|
|
@started = true
|
|
|
|
end
|
|
|
|
private :do_start
|
|
|
|
|
|
|
|
def connect
|
2004-03-08 09:16:21 -05:00
|
|
|
D "opening connection to #{conn_address()}..."
|
2004-03-06 12:08:21 -05:00
|
|
|
s = timeout(@open_timeout) { TCPSocket.open(conn_address(), conn_port()) }
|
2004-03-08 09:16:21 -05:00
|
|
|
D "opened"
|
2004-03-05 12:00:51 -05:00
|
|
|
if use_ssl?
|
2007-12-20 11:21:22 -05:00
|
|
|
ssl_parameters = Hash.new
|
2009-02-28 02:19:55 -05:00
|
|
|
iv_list = instance_variables
|
2011-11-26 14:59:11 -05:00
|
|
|
SSL_IVNAMES.each_with_index do |ivname, i|
|
2009-02-28 02:19:55 -05:00
|
|
|
if iv_list.include?(ivname) and
|
2011-11-26 14:59:11 -05:00
|
|
|
value = instance_variable_get(ivname)
|
|
|
|
ssl_parameters[SSL_ATTRIBUTES[i]] = value if value
|
2007-12-20 11:21:22 -05:00
|
|
|
end
|
2004-03-05 12:00:51 -05:00
|
|
|
end
|
2007-12-22 03:31:53 -05:00
|
|
|
@ssl_context = OpenSSL::SSL::SSLContext.new
|
|
|
|
@ssl_context.set_params(ssl_parameters)
|
2004-03-06 12:08:21 -05:00
|
|
|
s = OpenSSL::SSL::SSLSocket.new(s, @ssl_context)
|
|
|
|
s.sync_close = true
|
|
|
|
end
|
|
|
|
@socket = BufferedIO.new(s)
|
|
|
|
@socket.read_timeout = @read_timeout
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
@socket.continue_timeout = @continue_timeout
|
2004-03-06 12:08:21 -05:00
|
|
|
@socket.debug_output = @debug_output
|
2004-04-21 04:26:33 -04:00
|
|
|
if use_ssl?
|
2011-01-12 02:26:18 -05:00
|
|
|
begin
|
|
|
|
if proxy?
|
|
|
|
@socket.writeline sprintf('CONNECT %s:%s HTTP/%s',
|
|
|
|
@address, @port, HTTPVersion)
|
|
|
|
@socket.writeline "Host: #{@address}:#{@port}"
|
|
|
|
if proxy_user
|
|
|
|
credential = ["#{proxy_user}:#{proxy_pass}"].pack('m')
|
|
|
|
credential.delete!("\r\n")
|
|
|
|
@socket.writeline "Proxy-Authorization: Basic #{credential}"
|
|
|
|
end
|
|
|
|
@socket.writeline ''
|
|
|
|
HTTPResponse.read_new(@socket).value
|
2005-06-30 01:58:42 -04:00
|
|
|
end
|
2011-02-07 08:37:43 -05:00
|
|
|
# Server Name Indication (SNI) RFC 3546
|
|
|
|
s.hostname = @address if s.respond_to? :hostname=
|
2011-01-12 02:26:18 -05:00
|
|
|
timeout(@open_timeout) { s.connect }
|
|
|
|
if @ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE
|
|
|
|
s.post_connection_check(@address)
|
|
|
|
end
|
|
|
|
rescue => exception
|
|
|
|
D "Conn close because of connect error #{exception}"
|
|
|
|
@socket.close if @socket and not @socket.closed?
|
|
|
|
raise exception
|
2007-09-23 18:21:18 -04:00
|
|
|
end
|
2004-03-05 12:00:51 -05:00
|
|
|
end
|
2003-05-02 10:35:01 -04:00
|
|
|
on_connect
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
2004-03-06 12:08:21 -05:00
|
|
|
private :connect
|
2002-02-19 07:33:52 -05:00
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
def on_connect
|
|
|
|
end
|
|
|
|
private :on_connect
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Finishes the HTTP session and closes the TCP connection.
|
|
|
|
# Raises IOError if the session has not been started.
|
2003-05-02 10:35:01 -04:00
|
|
|
def finish
|
2004-03-29 02:54:38 -05:00
|
|
|
raise IOError, 'HTTP session not yet started' unless started?
|
2003-09-29 14:12:20 -04:00
|
|
|
do_finish
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_finish
|
|
|
|
@started = false
|
2003-05-02 10:35:01 -04:00
|
|
|
@socket.close if @socket and not @socket.closed?
|
|
|
|
@socket = nil
|
|
|
|
end
|
2003-09-29 14:12:20 -04:00
|
|
|
private :do_finish
|
2002-02-19 07:33:52 -05:00
|
|
|
|
|
|
|
#
|
|
|
|
# proxy
|
|
|
|
#
|
|
|
|
|
|
|
|
public
|
|
|
|
|
|
|
|
# no proxy
|
|
|
|
@is_proxy_class = false
|
|
|
|
@proxy_addr = nil
|
|
|
|
@proxy_port = nil
|
2002-11-21 06:13:17 -05:00
|
|
|
@proxy_user = nil
|
2002-11-21 05:59:50 -05:00
|
|
|
@proxy_pass = nil
|
2002-02-19 07:33:52 -05:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Creates an HTTP proxy class which behaves like Net::HTTP, but
|
|
|
|
# performs all access via the specified proxy.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# The arguments are the DNS name or IP address of the proxy host,
|
|
|
|
# the port to use to access the proxy, and a username and password
|
|
|
|
# if authorization is required to use the proxy.
|
|
|
|
#
|
|
|
|
# You can replace any use of the Net::HTTP class with use of the
|
|
|
|
# proxy class created.
|
|
|
|
#
|
|
|
|
# If +p_addr+ is nil, this method returns self (a Net::HTTP object).
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-11-30 03:06:19 -05:00
|
|
|
# # Example
|
|
|
|
# proxy_class = Net::HTTP::Proxy('proxy.example.com', 8080)
|
|
|
|
#
|
|
|
|
# proxy_class.start('www.ruby-lang.org') {|http|
|
|
|
|
# # connecting proxy.foo.org:8080
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# You may use them to work with authorization-enabled proxies:
|
|
|
|
#
|
|
|
|
# proxy_host = 'your.proxy.example'
|
|
|
|
# proxy_port = 8080
|
|
|
|
# proxy_user = 'user'
|
|
|
|
# proxy_pass = 'pass'
|
|
|
|
#
|
|
|
|
# proxy = Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass)
|
|
|
|
# proxy.start('www.example.com') { |http|
|
|
|
|
# # always connect to your.proxy.example:8080 using specified username
|
|
|
|
# # and password
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# Note that net/http does not use the HTTP_PROXY environment variable.
|
|
|
|
# If you want to use a proxy, you must set it explicitly.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-29 06:08:47 -05:00
|
|
|
def HTTP.Proxy(p_addr, p_port = nil, p_user = nil, p_pass = nil)
|
2002-11-21 06:13:17 -05:00
|
|
|
return self unless p_addr
|
2002-02-19 07:33:52 -05:00
|
|
|
delta = ProxyDelta
|
|
|
|
proxyclass = Class.new(self)
|
|
|
|
proxyclass.module_eval {
|
2003-08-01 11: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 07:33:52 -05:00
|
|
|
}
|
|
|
|
proxyclass
|
|
|
|
end
|
|
|
|
|
|
|
|
class << HTTP
|
2003-08-01 10:01:14 -04:00
|
|
|
# returns true if self is a class which was created by HTTP::Proxy.
|
2001-06-26 19:49:21 -04:00
|
|
|
def proxy_class?
|
|
|
|
@is_proxy_class
|
2000-11-10 18:31:32 -05:00
|
|
|
end
|
|
|
|
|
2011-05-16 18:15:32 -04:00
|
|
|
# Address of proxy host. If Net::HTTP does not use a proxy, nil.
|
2001-06-26 19:49:21 -04:00
|
|
|
attr_reader :proxy_address
|
2011-05-16 18:15:32 -04:00
|
|
|
|
|
|
|
# Port number of proxy host. If Net::HTTP does not use a proxy, nil.
|
2001-06-26 19:49:21 -04:00
|
|
|
attr_reader :proxy_port
|
2011-05-16 18:15:32 -04:00
|
|
|
|
|
|
|
# User name for accessing proxy. If Net::HTTP does not use a proxy, nil.
|
2002-11-21 06:13:17 -05:00
|
|
|
attr_reader :proxy_user
|
2011-05-16 18:15:32 -04:00
|
|
|
|
|
|
|
# User password for accessing proxy. If Net::HTTP does not use a proxy,
|
|
|
|
# nil.
|
2002-11-21 05:59:50 -05:00
|
|
|
attr_reader :proxy_pass
|
2000-11-10 18:31:32 -05:00
|
|
|
end
|
|
|
|
|
2004-04-21 04:26:33 -04:00
|
|
|
# True if self is a HTTP proxy class.
|
2000-11-10 18:31:32 -05:00
|
|
|
def proxy?
|
2002-03-26 06:18:02 -05:00
|
|
|
self.class.proxy_class?
|
2000-11-10 18:31:32 -05:00
|
|
|
end
|
|
|
|
|
2011-05-16 18:15:32 -04:00
|
|
|
# A convenience method for accessing value of proxy_address from Net::HTTP.
|
2000-11-10 18:31:32 -05:00
|
|
|
def proxy_address
|
2002-03-26 06:18:02 -05:00
|
|
|
self.class.proxy_address
|
2000-11-10 18:31:32 -05:00
|
|
|
end
|
|
|
|
|
2011-05-16 18:15:32 -04:00
|
|
|
# A convenience method for accessing value of proxy_port from Net::HTTP.
|
2000-11-10 18:31:32 -05:00
|
|
|
def proxy_port
|
2002-03-26 06:18:02 -05:00
|
|
|
self.class.proxy_port
|
2000-11-10 18:31:32 -05:00
|
|
|
end
|
|
|
|
|
2011-05-16 18:15:32 -04:00
|
|
|
# A convenience method for accessing value of proxy_user from Net::HTTP.
|
2002-11-21 06:13:17 -05:00
|
|
|
def proxy_user
|
|
|
|
self.class.proxy_user
|
|
|
|
end
|
|
|
|
|
2011-05-16 18:15:32 -04:00
|
|
|
# A convenience method for accessing value of proxy_pass from Net::HTTP.
|
2002-11-21 06:13:17 -05:00
|
|
|
def proxy_pass
|
|
|
|
self.class.proxy_pass
|
|
|
|
end
|
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
alias proxyaddr proxy_address #:nodoc: obsolete
|
|
|
|
alias proxyport proxy_port #:nodoc: obsolete
|
2001-06-26 19:49:21 -04:00
|
|
|
|
2002-02-07 15:22:39 -05:00
|
|
|
private
|
|
|
|
|
2002-11-21 06:13:17 -05:00
|
|
|
# without proxy
|
2000-06-27 09:36:17 -04:00
|
|
|
|
2002-02-07 15:22:39 -05:00
|
|
|
def conn_address
|
2004-04-21 04:26:33 -04:00
|
|
|
address()
|
2002-02-07 15:22:39 -05:00
|
|
|
end
|
2000-11-10 18:31:32 -05:00
|
|
|
|
2002-02-07 15:22:39 -05:00
|
|
|
def conn_port
|
2004-04-21 04:26:33 -04:00
|
|
|
port()
|
2002-02-07 15:22:39 -05:00
|
|
|
end
|
2001-02-06 06:14:51 -05:00
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def edit_path(path)
|
2002-02-07 15:22:39 -05:00
|
|
|
path
|
|
|
|
end
|
2001-02-06 06:14:51 -05:00
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
module ProxyDelta #:nodoc: internal use only
|
2001-02-06 06:14:51 -05:00
|
|
|
private
|
2002-02-07 15:22:39 -05:00
|
|
|
|
2001-12-30 14:18:45 -05:00
|
|
|
def conn_address
|
2002-11-21 06:13:17 -05:00
|
|
|
proxy_address()
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def conn_port
|
2002-11-21 06:13:17 -05:00
|
|
|
proxy_port()
|
2001-02-06 06:14:51 -05:00
|
|
|
end
|
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def edit_path(path)
|
2005-06-30 01:58:42 -04:00
|
|
|
use_ssl? ? path : "http://#{addr_port()}#{path}"
|
2001-02-06 06:14:51 -05:00
|
|
|
end
|
2002-02-07 15:22:39 -05:00
|
|
|
end
|
2001-02-06 06:14:51 -05:00
|
|
|
|
|
|
|
#
|
2003-05-02 10:35:01 -04:00
|
|
|
# HTTP operations
|
2001-02-06 06:14:51 -05:00
|
|
|
#
|
2000-11-10 18:31:32 -05:00
|
|
|
|
2001-02-06 06:14:51 -05:00
|
|
|
public
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Gets data from +path+ on the connected-to host.
|
2007-12-10 11:53:52 -05:00
|
|
|
# +initheader+ must be a Hash like { 'Accept' => '*/*', ... },
|
|
|
|
# and it defaults to an empty hash.
|
|
|
|
# If +initheader+ doesn't have the key 'accept-encoding', then
|
|
|
|
# a value of "gzip;q=1.0,deflate;q=0.6,identity;q=0.3" is used,
|
2009-03-05 22:56:38 -05:00
|
|
|
# so that gzip compression is used in preference to deflate
|
|
|
|
# compression, which is used in preference to no compression.
|
2007-12-10 11:53:52 -05:00
|
|
|
# Ruby doesn't have libraries to support the compress (Lempel-Ziv)
|
|
|
|
# compression, so that is not supported. The intent of this is
|
|
|
|
# to reduce bandwidth by default. If this routine sets up
|
|
|
|
# compression, then it does the decompression also, removing
|
|
|
|
# the header as well to prevent confusion. Otherwise
|
2009-03-05 22:56:38 -05:00
|
|
|
# it leaves the body as it found it.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2010-12-09 20:08:45 -05:00
|
|
|
# This method returns a Net::HTTPResponse object.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
|
|
|
# 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 11:41:41 -04:00
|
|
|
# +dest+ argument is obsolete.
|
|
|
|
# It still works but you must not use it.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2010-12-09 20:08:45 -05:00
|
|
|
# This method never raises an exception.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
|
|
|
# response = http.get('/index.html')
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# # using block
|
|
|
|
# File.open('result.txt', 'w') {|f|
|
2003-08-01 11:41:41 -04:00
|
|
|
# http.get('/~foo/') do |str|
|
|
|
|
# f.write str
|
|
|
|
# end
|
2003-08-01 10:01:14 -04:00
|
|
|
# }
|
2003-08-01 11:41:41 -04:00
|
|
|
#
|
2007-12-10 11:53:52 -05:00
|
|
|
def get(path, initheader = {}, dest = nil, &block) # :yield: +body_segment+
|
2001-12-30 14:18:45 -05:00
|
|
|
res = nil
|
2007-12-10 11:53:52 -05:00
|
|
|
if HAVE_ZLIB
|
|
|
|
unless initheader.keys.any?{|k| k.downcase == "accept-encoding"}
|
2011-01-04 23:58:57 -05:00
|
|
|
initheader = initheader.merge({
|
|
|
|
"accept-encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
|
|
|
|
})
|
2007-12-10 11:53:52 -05:00
|
|
|
@compression = true
|
|
|
|
end
|
|
|
|
end
|
2002-11-21 06:13:17 -05:00
|
|
|
request(Get.new(path, initheader)) {|r|
|
2007-12-10 11:53:52 -05:00
|
|
|
if r.key?("content-encoding") and @compression
|
|
|
|
@compression = nil # Clear it till next set.
|
|
|
|
the_body = r.read_body dest, &block
|
|
|
|
case r["content-encoding"]
|
|
|
|
when "gzip"
|
2010-10-11 07:23:35 -04:00
|
|
|
r.body= Zlib::GzipReader.new(StringIO.new(the_body), encoding: "ASCII-8BIT").read
|
2007-12-10 11:53:52 -05:00
|
|
|
r.delete("content-encoding")
|
|
|
|
when "deflate"
|
|
|
|
r.body= Zlib::Inflate.inflate(the_body);
|
|
|
|
r.delete("content-encoding")
|
|
|
|
when "identity"
|
|
|
|
; # nothing needed
|
2009-03-05 22:56:38 -05:00
|
|
|
else
|
2007-12-10 11:53:52 -05:00
|
|
|
; # Don't do anything dramatic, unless we need to later
|
|
|
|
end
|
|
|
|
else
|
|
|
|
r.read_body dest, &block
|
|
|
|
end
|
2003-08-01 11:41:41 -04:00
|
|
|
res = r
|
2001-12-30 14:18:45 -05:00
|
|
|
}
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Gets only the header from +path+ on the connected-to host.
|
|
|
|
# +header+ is a Hash like { 'Accept' => '*/*', ... }.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# This method returns a Net::HTTPResponse object.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-09 20:08:45 -05:00
|
|
|
# This method never raises an exception.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# response = nil
|
|
|
|
# Net::HTTP.start('some.www.server', 80) {|http|
|
2003-08-01 11:41:41 -04:00
|
|
|
# response = http.head('/index.html')
|
2003-08-01 10:01:14 -04:00
|
|
|
# }
|
|
|
|
# p response['content-type']
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
def head(path, initheader = nil)
|
2010-12-09 20:08:45 -05:00
|
|
|
request(Head.new(path, initheader))
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Posts +data+ (must be a String) to +path+. +header+ must be a Hash
|
|
|
|
# like { 'Accept' => '*/*', ... }.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-09 20:08:45 -05:00
|
|
|
# This method returns a Net::HTTPResponse object.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# If called with a block, yields each fragment of the
|
2010-11-29 21:56:14 -05:00
|
|
|
# entity body in turn as a string as it is read from
|
2003-08-01 10:01:14 -04:00
|
|
|
# the socket. Note that in this case, the returned response
|
|
|
|
# object will *not* contain a (meaningful) body.
|
|
|
|
#
|
2005-07-26 09:05:31 -04:00
|
|
|
# +dest+ argument is obsolete.
|
|
|
|
# It still works but you must not use it.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-12-09 20:08:45 -05:00
|
|
|
# This method never raises exception.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# response = http.post('/cgi-bin/search.rb', 'query=foo')
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# # using block
|
|
|
|
# File.open('result.txt', 'w') {|f|
|
2003-08-01 11:41:41 -04:00
|
|
|
# http.post('/cgi-bin/search.rb', 'query=foo') do |str|
|
|
|
|
# f.write str
|
|
|
|
# end
|
2003-08-01 10:01:14 -04:00
|
|
|
# }
|
2003-12-29 06:08:47 -05:00
|
|
|
#
|
2006-07-26 09:00:26 -04:00
|
|
|
# You should set Content-Type: header field for POST.
|
|
|
|
# If no Content-Type: field given, this method uses
|
|
|
|
# "application/x-www-form-urlencoded" by default.
|
|
|
|
#
|
2003-12-29 06:08:47 -05:00
|
|
|
def post(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
|
2010-06-01 08:30:52 -04:00
|
|
|
send_entity(path, data, initheader, dest, Post, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sends a PATCH request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
def patch(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
|
|
|
|
send_entity(path, data, initheader, dest, Patch, &block)
|
2000-12-24 15:01:44 -05:00
|
|
|
end
|
2000-03-26 03:48:15 -05:00
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def put(path, data, initheader = nil) #:nodoc:
|
2010-12-09 20:08:45 -05:00
|
|
|
request(Put.new(path, initheader), data)
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
|
|
|
|
2004-03-05 09:13:31 -05:00
|
|
|
# Sends a PROPPATCH request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
def proppatch(path, body, initheader = nil)
|
|
|
|
request(Proppatch.new(path, initheader), body)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sends a LOCK request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
def lock(path, body, initheader = nil)
|
|
|
|
request(Lock.new(path, initheader), body)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sends a UNLOCK request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
def unlock(path, body, initheader = nil)
|
|
|
|
request(Unlock.new(path, initheader), body)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sends a OPTIONS request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
def options(path, initheader = nil)
|
|
|
|
request(Options.new(path, initheader))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sends a PROPFIND request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
def propfind(path, body = nil, initheader = {'Depth' => '0'})
|
|
|
|
request(Propfind.new(path, initheader), body)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sends a DELETE request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
def delete(path, initheader = {'Depth' => 'Infinity'})
|
|
|
|
request(Delete.new(path, initheader))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sends a MOVE request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
def move(path, initheader = nil)
|
|
|
|
request(Move.new(path, initheader))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sends a COPY request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
def copy(path, initheader = nil)
|
|
|
|
request(Copy.new(path, initheader))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sends a MKCOL request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
def mkcol(path, body = nil, initheader = nil)
|
|
|
|
request(Mkcol.new(path, initheader), body)
|
|
|
|
end
|
|
|
|
|
2004-05-18 13:29:55 -04:00
|
|
|
# Sends a TRACE request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
def trace(path, initheader = nil)
|
|
|
|
request(Trace.new(path, initheader))
|
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Sends a GET request to the +path+.
|
|
|
|
# Returns the response as a Net::HTTPResponse object.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# When called with a block, passes an HTTPResponse object to the block.
|
|
|
|
# The body of the response will not have been read yet;
|
|
|
|
# the block can process it using HTTPResponse#read_body,
|
2003-08-01 10:01:14 -04:00
|
|
|
# if desired.
|
|
|
|
#
|
|
|
|
# Returns the response.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# This method never raises Net::* exceptions.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# response = http.request_get('/index.html')
|
2010-11-29 21:56:14 -05:00
|
|
|
# # The entity body is already read in this case.
|
2003-08-01 10:01:14 -04:00
|
|
|
# p response['content-type']
|
|
|
|
# puts response.body
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# # Using a block
|
2003-08-01 10:01:14 -04:00
|
|
|
# http.request_get('/index.html') {|response|
|
2003-08-01 11:41:41 -04:00
|
|
|
# p response['content-type']
|
|
|
|
# response.read_body do |str| # read body now
|
|
|
|
# print str
|
|
|
|
# end
|
2003-08-01 10:01:14 -04:00
|
|
|
# }
|
|
|
|
#
|
2003-12-29 06:08:47 -05:00
|
|
|
def request_get(path, initheader = nil, &block) # :yield: +response+
|
|
|
|
request(Get.new(path, initheader), &block)
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Sends a HEAD request to the +path+ and returns the response
|
|
|
|
# as a Net::HTTPResponse object.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
|
|
|
# Returns the response.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# This method never raises Net::* exceptions.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# response = http.request_head('/index.html')
|
|
|
|
# p response['content-type']
|
|
|
|
#
|
2003-12-29 06:08:47 -05:00
|
|
|
def request_head(path, initheader = nil, &block)
|
|
|
|
request(Head.new(path, initheader), &block)
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Sends a POST request to the +path+.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# Returns the response as a Net::HTTPResponse object.
|
|
|
|
#
|
|
|
|
# When called with a block, the block is passed an HTTPResponse
|
|
|
|
# object. The body of that response will not have been read yet;
|
|
|
|
# the block can process it using HTTPResponse#read_body, if desired.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
|
|
|
# Returns the response.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# This method never raises Net::* exceptions.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# # example
|
|
|
|
# response = http.request_post('/cgi-bin/nice.rb', 'datadatadata...')
|
|
|
|
# p response.status
|
2010-11-29 21:56:14 -05:00
|
|
|
# puts response.body # body is already read in this case
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# # using block
|
|
|
|
# http.request_post('/cgi-bin/nice.rb', 'datadatadata...') {|response|
|
2003-08-01 11: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 10:01:14 -04:00
|
|
|
# }
|
2003-12-29 06:08:47 -05:00
|
|
|
#
|
|
|
|
def request_post(path, data, initheader = nil, &block) # :yield: +response+
|
2002-11-21 06:13:17 -05:00
|
|
|
request Post.new(path, initheader), data, &block
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def request_put(path, data, initheader = nil, &block) #:nodoc:
|
2002-11-21 06:13:17 -05:00
|
|
|
request Put.new(path, initheader), data, &block
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
|
|
|
|
2003-08-05 12: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 14:18:45 -05:00
|
|
|
|
2002-02-19 07:33:52 -05:00
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Sends an HTTP request to the HTTP server.
|
2010-11-29 21:56:14 -05:00
|
|
|
# Also sends a DATA string if +data+ is given.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# Returns a Net::HTTPResponse object.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# This method never raises Net::* exceptions.
|
|
|
|
#
|
|
|
|
# response = http.send_request('GET', '/index.html')
|
|
|
|
# puts response.body
|
|
|
|
#
|
2003-12-29 06:08:47 -05:00
|
|
|
def send_request(name, path, data = nil, header = nil)
|
2003-08-01 10:01:14 -04:00
|
|
|
r = HTTPGenericRequest.new(name,(data ? true : false),true,path,header)
|
|
|
|
request r, data
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
2000-04-18 05:39:02 -04:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Sends an HTTPRequest object +req+ to the HTTP server.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# If +req+ is a Net::HTTP::Post or Net::HTTP::Put request containing
|
|
|
|
# data, the data is also sent. Providing data for a Net::HTTP::Head or
|
|
|
|
# Net::HTTP::Get request results in an ArgumentError.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# Returns an HTTPResponse object.
|
|
|
|
#
|
|
|
|
# When called with a block, passes an HTTPResponse object to the block.
|
|
|
|
# The body of the response will not have been read yet;
|
|
|
|
# the block can process it using HTTPResponse#read_body,
|
|
|
|
# if desired.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# This method never raises Net::* exceptions.
|
2003-12-29 06:08:47 -05:00
|
|
|
#
|
|
|
|
def request(req, body = nil, &block) # :yield: +response+
|
2002-11-21 06:13:17 -05:00
|
|
|
unless started?
|
2001-12-07 05:04:25 -05:00
|
|
|
start {
|
2004-02-08 00:24:47 -05:00
|
|
|
req['connection'] ||= 'close'
|
2003-08-01 11:41:41 -04:00
|
|
|
return request(req, body, &block)
|
2001-12-07 05:04:25 -05:00
|
|
|
}
|
|
|
|
end
|
2002-11-21 06:13:17 -05:00
|
|
|
if proxy_user()
|
2007-03-16 09:17:10 -04:00
|
|
|
req.proxy_basic_auth proxy_user(), proxy_pass() unless use_ssl?
|
2002-11-21 06:13:17 -05:00
|
|
|
end
|
2004-03-06 13:11:40 -05:00
|
|
|
req.set_body_internal body
|
2007-03-16 09:17:10 -04:00
|
|
|
res = transport_request(req, &block)
|
|
|
|
if sspi_auth?(res)
|
|
|
|
sspi_auth(req)
|
|
|
|
res = transport_request(req, &block)
|
|
|
|
end
|
2002-02-19 07:33:52 -05:00
|
|
|
res
|
2000-04-18 05:39:02 -04:00
|
|
|
end
|
|
|
|
|
2001-02-06 06:14:51 -05:00
|
|
|
private
|
1999-12-17 10:00:13 -05:00
|
|
|
|
2010-06-01 08:30:52 -04:00
|
|
|
# Executes a request which uses a representation
|
|
|
|
# and returns its body.
|
|
|
|
def send_entity(path, data, initheader, dest, type, &block)
|
|
|
|
res = nil
|
|
|
|
request(type.new(path, initheader), data) {|r|
|
|
|
|
r.read_body dest, &block
|
|
|
|
res = r
|
|
|
|
}
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2007-03-16 09:17:10 -04:00
|
|
|
def transport_request(req)
|
|
|
|
begin_transport req
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
res = catch(:response) {
|
|
|
|
req.exec @socket, @curr_http_version, edit_path(req.path)
|
|
|
|
begin
|
|
|
|
res = HTTPResponse.read_new(@socket)
|
|
|
|
end while res.kind_of?(HTTPContinue)
|
|
|
|
res.reading_body(@socket, req.response_body_permitted?) {
|
|
|
|
yield res if block_given?
|
|
|
|
}
|
|
|
|
res
|
2007-03-16 09:17:10 -04:00
|
|
|
}
|
|
|
|
end_transport req, res
|
|
|
|
res
|
2009-09-26 00:27:07 -04:00
|
|
|
rescue => exception
|
|
|
|
D "Conn close because of error #{exception}"
|
2010-10-17 21:20:37 -04:00
|
|
|
@socket.close if @socket and not @socket.closed?
|
2009-09-26 00:27:07 -04:00
|
|
|
raise exception
|
2007-03-16 09:17:10 -04:00
|
|
|
end
|
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def begin_transport(req)
|
2007-05-06 04:53:51 -04:00
|
|
|
connect if @socket.closed?
|
2002-11-21 06:13:17 -05:00
|
|
|
if not req.response_body_permitted? and @close_on_empty_response
|
2004-04-13 04:55:55 -04:00
|
|
|
req['connection'] ||= 'close'
|
2001-02-06 06:14:51 -05:00
|
|
|
end
|
2004-04-13 04:55:55 -04:00
|
|
|
req['host'] ||= addr_port()
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
2000-03-27 10:52:27 -05:00
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def end_transport(req, res)
|
2002-02-19 07:33:52 -05:00
|
|
|
@curr_http_version = res.http_version
|
2007-05-06 04:53:51 -04:00
|
|
|
if @socket.closed?
|
|
|
|
D 'Conn socket closed'
|
|
|
|
elsif not res.body and @close_on_empty_response
|
2002-03-26 06:18:02 -05:00
|
|
|
D 'Conn close'
|
2001-02-23 23:53:50 -05:00
|
|
|
@socket.close
|
2003-12-29 06:08:47 -05:00
|
|
|
elsif keep_alive?(req, res)
|
2001-02-23 23:53:50 -05:00
|
|
|
D 'Conn keep-alive'
|
2001-02-06 06:14:51 -05:00
|
|
|
else
|
2001-02-23 23:53:50 -05:00
|
|
|
D 'Conn close'
|
2000-03-05 05:25:53 -05:00
|
|
|
@socket.close
|
|
|
|
end
|
1999-12-29 06:14:04 -05:00
|
|
|
end
|
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def keep_alive?(req, res)
|
2007-05-06 04:53:51 -04:00
|
|
|
return false if req.connection_close?
|
|
|
|
if @curr_http_version <= '1.0'
|
|
|
|
res.connection_keep_alive?
|
|
|
|
else # HTTP/1.1 or later
|
|
|
|
not res.connection_close?
|
|
|
|
end
|
1999-12-29 06:14:04 -05:00
|
|
|
end
|
1999-12-17 10:00:13 -05:00
|
|
|
|
2007-03-16 09:17:10 -04:00
|
|
|
def sspi_auth?(res)
|
|
|
|
return false unless @sspi_enabled
|
|
|
|
if res.kind_of?(HTTPProxyAuthenticationRequired) and
|
|
|
|
proxy? and res["Proxy-Authenticate"].include?("Negotiate")
|
|
|
|
begin
|
|
|
|
require 'win32/sspi'
|
|
|
|
true
|
|
|
|
rescue LoadError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sspi_auth(req)
|
|
|
|
n = Win32::SSPI::NegotiateAuth.new
|
|
|
|
req["Proxy-Authorization"] = "Negotiate #{n.get_initial_token}"
|
|
|
|
# Some versions of ISA will close the connection if this isn't present.
|
|
|
|
req["Connection"] = "Keep-Alive"
|
|
|
|
req["Proxy-Connection"] = "Keep-Alive"
|
|
|
|
res = transport_request(req)
|
|
|
|
authphrase = res["Proxy-Authenticate"] or return res
|
|
|
|
req["Proxy-Authorization"] = "Negotiate #{n.complete_authentication(authphrase)}"
|
|
|
|
rescue => err
|
|
|
|
raise HTTPAuthenticationError.new('HTTP authentication failed', err)
|
|
|
|
end
|
|
|
|
|
2001-02-22 18:23:57 -05:00
|
|
|
#
|
|
|
|
# utils
|
|
|
|
#
|
|
|
|
|
2001-02-23 23:53:50 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def addr_port
|
2004-03-05 12:00:51 -05:00
|
|
|
if use_ssl?
|
|
|
|
address() + (port == HTTP.https_default_port ? '' : ":#{port()}")
|
|
|
|
else
|
|
|
|
address() + (port == HTTP.http_default_port ? '' : ":#{port()}")
|
|
|
|
end
|
2001-02-23 23:53:50 -05:00
|
|
|
end
|
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def D(msg)
|
|
|
|
return unless @debug_output
|
|
|
|
@debug_output << msg
|
|
|
|
@debug_output << "\n"
|
2001-02-23 23:53:50 -05:00
|
|
|
end
|
|
|
|
|
2001-01-13 14:07:15 -05:00
|
|
|
end
|
2000-12-24 15:01:44 -05:00
|
|
|
|
2002-02-19 07:33:52 -05: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 02:04:40 -04:00
|
|
|
|
2004-03-06 12:08:21 -05:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# The HTTPHeader module defines methods for reading and writing
|
|
|
|
# HTTP headers.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# It is used as a mixin by other classes, to provide hash-like
|
|
|
|
# access to HTTP header values. Unlike raw hash access, HTTPHeader
|
|
|
|
# provides access via case-insensitive keys. It also provides
|
|
|
|
# methods for accessing commonly-used HTTP header values in more
|
|
|
|
# convenient formats.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2001-02-22 18:23:57 -05:00
|
|
|
module HTTPHeader
|
1999-12-17 10:00:13 -05:00
|
|
|
|
2004-03-08 09:16:21 -05:00
|
|
|
def initialize_http_header(initheader)
|
|
|
|
@header = {}
|
|
|
|
return unless initheader
|
|
|
|
initheader.each do |key, value|
|
|
|
|
warn "net/http: warning: duplicated HTTP header: #{key}" if key?(key) and $VERBOSE
|
|
|
|
@header[key.downcase] = [value.strip]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
def size #:nodoc: obsolete
|
2001-02-22 18:23:57 -05:00
|
|
|
@header.size
|
1999-12-29 06:14:04 -05:00
|
|
|
end
|
1999-12-17 10:00:13 -05:00
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
alias length size #:nodoc: obsolete
|
2000-06-12 12:42:46 -04:00
|
|
|
|
2003-08-01 10: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 06:08:47 -05:00
|
|
|
def [](key)
|
2004-03-06 15:35:19 -05:00
|
|
|
a = @header[key.downcase] or return nil
|
|
|
|
a.join(', ')
|
2001-01-13 14:07:15 -05:00
|
|
|
end
|
2000-10-11 01:27:56 -04:00
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Sets the header field corresponding to the case-insensitive key.
|
2003-12-29 06:08:47 -05:00
|
|
|
def []=(key, val)
|
2004-03-06 15:35:19 -05:00
|
|
|
unless val
|
|
|
|
@header.delete key.downcase
|
|
|
|
return val
|
|
|
|
end
|
2006-02-05 04:50:38 -05:00
|
|
|
@header[key.downcase] = [val]
|
2004-03-06 15:35:19 -05:00
|
|
|
end
|
|
|
|
|
2006-02-05 04:50:38 -05:00
|
|
|
# [Ruby 1.8.3]
|
2010-11-29 21:56:14 -05:00
|
|
|
# Adds a value to a named header field, instead of replacing its value.
|
2005-09-13 02:41:59 -04:00
|
|
|
# Second argument +val+ must be a String.
|
|
|
|
# See also #[]=, #[] and #get_fields.
|
2004-03-06 15:35:19 -05:00
|
|
|
#
|
2005-04-20 09:55:26 -04:00
|
|
|
# request.add_field 'X-My-Header', 'a'
|
2004-03-06 15:35:19 -05:00
|
|
|
# p request['X-My-Header'] #=> "a"
|
2005-09-13 02:41:59 -04:00
|
|
|
# p request.get_fields('X-My-Header') #=> ["a"]
|
2005-04-20 09:55:26 -04:00
|
|
|
# request.add_field 'X-My-Header', 'b'
|
2004-03-06 15:35:19 -05:00
|
|
|
# p request['X-My-Header'] #=> "a, b"
|
2005-09-13 02:41:59 -04:00
|
|
|
# p request.get_fields('X-My-Header') #=> ["a", "b"]
|
2005-04-20 09:55:26 -04:00
|
|
|
# request.add_field 'X-My-Header', 'c'
|
2004-03-06 15:35:19 -05:00
|
|
|
# p request['X-My-Header'] #=> "a, b, c"
|
|
|
|
# p request.get_fields('X-My-Header') #=> ["a", "b", "c"]
|
|
|
|
#
|
2005-04-20 09:55:26 -04:00
|
|
|
def add_field(key, val)
|
2004-03-08 07:40:34 -05:00
|
|
|
if @header.key?(key.downcase)
|
2006-03-05 04:35:32 -05:00
|
|
|
@header[key.downcase].push val
|
2004-03-06 15:35:19 -05:00
|
|
|
else
|
2006-02-05 04:50:38 -05:00
|
|
|
@header[key.downcase] = [val]
|
2004-03-06 15:35:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-02-05 04:50:38 -05:00
|
|
|
# [Ruby 1.8.3]
|
2005-09-13 02:41:59 -04:00
|
|
|
# Returns an array of header field strings corresponding to the
|
|
|
|
# case-insensitive +key+. This method allows you to get duplicated
|
|
|
|
# header fields without any processing. See also #[].
|
2004-03-06 15:35:19 -05:00
|
|
|
#
|
|
|
|
# p response.get_fields('Set-Cookie')
|
|
|
|
# #=> ["session=al98axx; expires=Fri, 31-Dec-1999 23:58:23",
|
|
|
|
# "query=rubyscript; expires=Fri, 31-Dec-1999 23:58:23"]
|
|
|
|
# p response['Set-Cookie']
|
|
|
|
# #=> "session=al98axx; expires=Fri, 31-Dec-1999 23:58:23, query=rubyscript; expires=Fri, 31-Dec-1999 23:58:23"
|
|
|
|
#
|
|
|
|
def get_fields(key)
|
|
|
|
return nil unless @header[key.downcase]
|
|
|
|
@header[key.downcase].dup
|
2002-11-21 06:13:17 -05:00
|
|
|
end
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Returns the header field corresponding to the case-insensitive key.
|
2009-09-10 12:28:52 -04:00
|
|
|
# Returns the default value +args+, or the result of the block, or
|
2010-11-29 21:56:14 -05:00
|
|
|
# raises an IndexError if there's no header field named +key+
|
2009-09-10 12:28:52 -04:00
|
|
|
# See Hash#fetch
|
2003-12-29 06:08:47 -05:00
|
|
|
def fetch(key, *args, &block) #:yield: +key+
|
2004-03-06 15:35:19 -05:00
|
|
|
a = @header.fetch(key.downcase, *args, &block)
|
2009-09-13 16:30:46 -04:00
|
|
|
a.kind_of?(Array) ? a.join(', ') : a
|
2001-01-13 14:07:15 -05:00
|
|
|
end
|
2000-11-07 06:27:16 -05:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Iterates through the header names and values, passing in the name
|
|
|
|
# and value to the code block supplied.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# response.header.each_header {|key,value| puts "#{key} = #{value}" }
|
|
|
|
#
|
2004-03-06 15:35:19 -05:00
|
|
|
def each_header #:yield: +key+, +value+
|
2009-09-13 19:35:09 -04:00
|
|
|
block_given? or return enum_for(__method__)
|
2004-03-06 15:35:19 -05:00
|
|
|
@header.each do |k,va|
|
|
|
|
yield k, va.join(', ')
|
|
|
|
end
|
2001-01-13 14:07:15 -05:00
|
|
|
end
|
2000-11-07 06:27:16 -05:00
|
|
|
|
2001-12-13 14:15:21 -05:00
|
|
|
alias each each_header
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Iterates through the header names in the header, passing
|
|
|
|
# each header name to the code block.
|
2004-03-06 15:35:19 -05:00
|
|
|
def each_name(&block) #:yield: +key+
|
2009-09-13 19:35:09 -04:00
|
|
|
block_given? or return enum_for(__method__)
|
2004-12-15 13:27:44 -05:00
|
|
|
@header.each_key(&block)
|
2004-03-06 15:35:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
alias each_key each_name
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Iterates through the header names in the header, passing
|
|
|
|
# capitalized header names to the code block.
|
|
|
|
#
|
|
|
|
# Note that header names are capitalized systematically;
|
|
|
|
# capitalization may not match that used by the remote HTTP
|
|
|
|
# server in its response.
|
2009-09-13 19:35:09 -04:00
|
|
|
def each_capitalized_name #:yield: +key+
|
|
|
|
block_given? or return enum_for(__method__)
|
2004-03-06 15:35:19 -05:00
|
|
|
@header.each_key do |k|
|
|
|
|
yield capitalize(k)
|
|
|
|
end
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
2001-01-13 14:07:15 -05:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Iterates through header values, passing each value to the
|
|
|
|
# code block.
|
2004-03-06 15:35:19 -05:00
|
|
|
def each_value #:yield: +value+
|
2009-09-13 19:35:09 -04:00
|
|
|
block_given? or return enum_for(__method__)
|
2004-03-06 15:35:19 -05:00
|
|
|
@header.each_value do |va|
|
|
|
|
yield va.join(', ')
|
|
|
|
end
|
1999-12-29 06:14:04 -05:00
|
|
|
end
|
1999-12-17 10:00:13 -05:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Removes a header field, specified by case-insensitive key.
|
2003-12-29 06:08:47 -05:00
|
|
|
def delete(key)
|
2002-11-21 06:13:17 -05:00
|
|
|
@header.delete(key.downcase)
|
2002-10-17 03:27:00 -04:00
|
|
|
end
|
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
# true if +key+ header exists.
|
2003-12-29 06:08:47 -05:00
|
|
|
def key?(key)
|
2002-11-21 06:13:17 -05:00
|
|
|
@header.key?(key.downcase)
|
2000-02-21 10:25:37 -05:00
|
|
|
end
|
1999-12-29 06:14:04 -05:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Returns a Hash consisting of header names and values.
|
|
|
|
# e.g.
|
|
|
|
# {"cache-control" => "private",
|
|
|
|
# "content-type" => "text/html",
|
|
|
|
# "date" => "Wed, 22 Jun 2005 22:11:50 GMT"}
|
2001-02-22 18:23:57 -05:00
|
|
|
def to_hash
|
|
|
|
@header.dup
|
|
|
|
end
|
2001-02-07 02:23:09 -05:00
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
# As for #each_header, except the keys are provided in capitalized form.
|
2010-11-29 21:56:14 -05:00
|
|
|
#
|
|
|
|
# Note that header names are capitalized systematically;
|
|
|
|
# capitalization may not match that used by the remote HTTP
|
|
|
|
# server in its response.
|
2003-12-29 06:08:47 -05:00
|
|
|
def each_capitalized
|
2009-09-13 19:35:09 -04:00
|
|
|
block_given? or return enum_for(__method__)
|
2001-02-22 18:23:57 -05:00
|
|
|
@header.each do |k,v|
|
2005-01-01 03:45:50 -05:00
|
|
|
yield capitalize(k), v.join(', ')
|
2001-02-22 18:23:57 -05:00
|
|
|
end
|
|
|
|
end
|
1999-12-29 06:14:04 -05:00
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
alias canonical_each each_capitalized
|
|
|
|
|
2004-03-06 15:35:19 -05:00
|
|
|
def capitalize(name)
|
|
|
|
name.split(/-/).map {|s| s.capitalize }.join('-')
|
2000-03-27 10:52:27 -05:00
|
|
|
end
|
2003-12-29 06:08:47 -05:00
|
|
|
private :capitalize
|
2000-03-27 10:52:27 -05:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Returns an Array of Range objects which represent the Range:
|
|
|
|
# HTTP header field, or +nil+ if there is no such header.
|
2001-02-22 18:23:57 -05:00
|
|
|
def range
|
2004-03-06 15:35:19 -05:00
|
|
|
return nil unless @header['range']
|
|
|
|
self['Range'].split(/,/).map {|spec|
|
2003-08-01 11: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 07:10:58 -05:00
|
|
|
}
|
2001-02-22 18:23:57 -05:00
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Sets the HTTP Range: header.
|
|
|
|
# Accepts either a Range object as a single argument,
|
|
|
|
# or a beginning index and a length from that index.
|
|
|
|
# Example:
|
2004-03-06 15:35:19 -05:00
|
|
|
#
|
|
|
|
# req.range = (0..1023)
|
|
|
|
# req.set_range 0, 1023
|
|
|
|
#
|
|
|
|
def set_range(r, e = nil)
|
|
|
|
unless r
|
|
|
|
@header.delete 'range'
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
r = (r...r+e) if e
|
2001-02-22 18:23:57 -05:00
|
|
|
case r
|
|
|
|
when Numeric
|
2004-03-06 15:35:19 -05:00
|
|
|
n = r.to_i
|
|
|
|
rangestr = (n > 0 ? "0-#{n-1}" : "-#{-n}")
|
2001-02-22 18:23:57 -05:00
|
|
|
when Range
|
|
|
|
first = r.first
|
|
|
|
last = r.last
|
2003-12-29 06:08:47 -05:00
|
|
|
last -= 1 if r.exclude_end?
|
2002-11-21 06:13:17 -05:00
|
|
|
if last == -1
|
2003-12-29 06:08:47 -05:00
|
|
|
rangestr = (first > 0 ? "#{first}-" : "-#{-first}")
|
2001-02-22 18:23:57 -05:00
|
|
|
else
|
2003-12-29 06:08:47 -05: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-22 18:23:57 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
raise TypeError, 'Range/Integer is required'
|
|
|
|
end
|
2004-03-06 15:35:19 -05:00
|
|
|
@header['range'] = ["bytes=#{rangestr}"]
|
2001-02-22 18:23:57 -05:00
|
|
|
r
|
|
|
|
end
|
|
|
|
|
2004-03-06 15:35:19 -05:00
|
|
|
alias range= set_range
|
2001-02-23 23:53:50 -05:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Returns an Integer object which represents the HTTP Content-Length:
|
|
|
|
# header field, or +nil+ if that field was not provided.
|
2001-02-22 18:23:57 -05:00
|
|
|
def content_length
|
2004-03-06 15:35:19 -05:00
|
|
|
return nil unless key?('Content-Length')
|
|
|
|
len = self['Content-Length'].slice(/\d+/) or
|
2003-12-29 06:08:47 -05:00
|
|
|
raise HTTPHeaderSyntaxError, 'wrong Content-Length format'
|
|
|
|
len.to_i
|
2001-02-22 18:23:57 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2004-03-06 15:35:19 -05:00
|
|
|
def content_length=(len)
|
|
|
|
unless len
|
|
|
|
@header.delete 'content-length'
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
@header['content-length'] = [len.to_i.to_s]
|
|
|
|
end
|
2001-02-22 18:23:57 -05:00
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Returns "true" if the "transfer-encoding" header is present and
|
2009-03-05 22:56:38 -05:00
|
|
|
# set to "chunked". This is an HTTP/1.1 feature, allowing the
|
2003-08-01 10:01:14 -04:00
|
|
|
# the content to be sent in "chunks" without at the outset
|
|
|
|
# stating the entire content length.
|
2001-02-22 18:23:57 -05:00
|
|
|
def chunked?
|
2004-03-06 15:35:19 -05:00
|
|
|
return false unless @header['transfer-encoding']
|
|
|
|
field = self['Transfer-Encoding']
|
|
|
|
(/(?:\A|[^\-\w])chunked(?![\-\w])/i =~ field) ? true : false
|
2001-02-22 18:23:57 -05:00
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Returns a Range object which represents the value of the Content-Range:
|
|
|
|
# header field.
|
|
|
|
# For a partial entity body, this indicates where this fragment
|
2003-08-01 10:01:14 -04:00
|
|
|
# fits inside the full entity body, as range of byte offsets.
|
2001-02-22 18:23:57 -05:00
|
|
|
def content_range
|
2003-12-29 06:08:47 -05:00
|
|
|
return nil unless @header['content-range']
|
2004-03-06 15:35:19 -05:00
|
|
|
m = %r<bytes\s+(\d+)-(\d+)/(\d+|\*)>i.match(self['Content-Range']) or
|
2003-12-29 06:08:47 -05:00
|
|
|
raise HTTPHeaderSyntaxError, 'wrong Content-Range format'
|
2010-01-18 01:49:46 -05:00
|
|
|
m[1].to_i .. m[2].to_i
|
2001-02-22 18:23:57 -05:00
|
|
|
end
|
|
|
|
|
2004-03-06 15:35:19 -05:00
|
|
|
# The length of the range represented in Content-Range: header.
|
2001-02-22 18:23:57 -05:00
|
|
|
def range_length
|
2003-12-29 06:08:47 -05:00
|
|
|
r = content_range() or return nil
|
2010-01-18 01:49:46 -05:00
|
|
|
r.end - r.begin + 1
|
2001-02-22 18:23:57 -05:00
|
|
|
end
|
|
|
|
|
2006-07-26 09:00:26 -04:00
|
|
|
# Returns a content type string such as "text/html".
|
|
|
|
# This method returns nil if Content-Type: header field does not exist.
|
2004-03-06 15:35:19 -05:00
|
|
|
def content_type
|
2006-07-26 09:00:26 -04:00
|
|
|
return nil unless main_type()
|
|
|
|
if sub_type()
|
|
|
|
then "#{main_type()}/#{sub_type()}"
|
|
|
|
else main_type()
|
|
|
|
end
|
2004-03-06 15:35:19 -05:00
|
|
|
end
|
|
|
|
|
2006-07-26 09:00:26 -04:00
|
|
|
# Returns a content type string such as "text".
|
|
|
|
# This method returns nil if Content-Type: header field does not exist.
|
2004-03-06 15:35:19 -05:00
|
|
|
def main_type
|
|
|
|
return nil unless @header['content-type']
|
|
|
|
self['Content-Type'].split(';').first.to_s.split('/')[0].to_s.strip
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-07-26 09:00:26 -04:00
|
|
|
# Returns a content type string such as "html".
|
|
|
|
# This method returns nil if Content-Type: header field does not exist
|
|
|
|
# or sub-type is not given (e.g. "Content-Type: text").
|
2004-03-06 15:35:19 -05:00
|
|
|
def sub_type
|
|
|
|
return nil unless @header['content-type']
|
2010-11-08 15:59:01 -05:00
|
|
|
_, sub = *self['Content-Type'].split(';').first.to_s.split('/')
|
2006-07-26 09:00:26 -04:00
|
|
|
return nil unless sub
|
|
|
|
sub.strip
|
2004-03-06 15:35:19 -05:00
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Any parameters specified for the content type, returned as a Hash.
|
|
|
|
# For example, a header of Content-Type: text/html; charset=EUC-JP
|
|
|
|
# would result in type_params returning {'charset' => 'EUC-JP'}
|
2004-03-06 15:35:19 -05:00
|
|
|
def type_params
|
|
|
|
result = {}
|
2006-07-26 09:00:26 -04:00
|
|
|
list = self['Content-Type'].to_s.split(';')
|
|
|
|
list.shift
|
|
|
|
list.each do |param|
|
2004-03-06 15:35:19 -05:00
|
|
|
k, v = *param.split('=', 2)
|
|
|
|
result[k.strip] = v.strip
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Sets the content type in an HTTP header.
|
|
|
|
# The +type+ should be a full HTTP content type, e.g. "text/html".
|
|
|
|
# The +params+ are an optional Hash of parameters to add after the
|
|
|
|
# content type, e.g. {'charset' => 'iso-8859-1'}
|
2004-03-06 15:35:19 -05:00
|
|
|
def set_content_type(type, params = {})
|
|
|
|
@header['content-type'] = [type + params.map{|k,v|"; #{k}=#{v}"}.join('')]
|
|
|
|
end
|
|
|
|
|
|
|
|
alias content_type= set_content_type
|
|
|
|
|
2006-07-26 09:00:26 -04:00
|
|
|
# Set header fields and a body from HTML form data.
|
2010-12-12 20:04:37 -05:00
|
|
|
# +params+ should be an Array of Arrays or
|
|
|
|
# a Hash containing HTML form data.
|
2006-07-26 09:00:26 -04:00
|
|
|
# Optional argument +sep+ means data record separator.
|
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# Values are URL encoded as necessary and the content-type is set to
|
|
|
|
# application/x-www-form-urlencoded
|
2007-05-06 05:43:26 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# http.form_data = {"q" => "ruby", "lang" => "en"}
|
|
|
|
# http.form_data = {"q" => ["ruby", "perl"], "lang" => "en"}
|
|
|
|
# http.set_form_data({"q" => "ruby", "lang" => "en"}, ';')
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2005-04-20 09:55:26 -04:00
|
|
|
def set_form_data(params, sep = '&')
|
2010-12-01 20:46:45 -05:00
|
|
|
query = URI.encode_www_form(params)
|
|
|
|
query.gsub!(/&/, sep) if sep != '&'
|
|
|
|
self.body = query
|
2005-04-20 09:55:26 -04:00
|
|
|
self.content_type = 'application/x-www-form-urlencoded'
|
|
|
|
end
|
|
|
|
|
|
|
|
alias form_data= set_form_data
|
|
|
|
|
2010-12-12 20:04:37 -05:00
|
|
|
# Set a HTML form data set.
|
|
|
|
# +params+ is the form data set; it is an Array of Arrays or a Hash
|
|
|
|
# +enctype is the type to encode the form data set.
|
|
|
|
# It is application/x-www-form-urlencoded or multipart/form-data.
|
|
|
|
# +formpot+ is an optional hash to specify the detail.
|
|
|
|
#
|
|
|
|
# boundary:: the boundary of the multipart message
|
|
|
|
# charset:: the charset of the message. All names and the values of
|
|
|
|
# non-file fields are encoded as the charset.
|
|
|
|
#
|
|
|
|
# Each item of params is an array and contains following items:
|
|
|
|
# +name+:: the name of the field
|
|
|
|
# +value+:: the value of the field, it should be a String or a File
|
|
|
|
# +opt+:: an optional hash to specify additional information
|
|
|
|
#
|
|
|
|
# Each item is a file field or a normal field.
|
|
|
|
# If +value+ is a File object or the +opt+ have a filename key,
|
|
|
|
# the item is treated as a file field.
|
|
|
|
#
|
|
|
|
# If Transfer-Encoding is set as chunked, this send the request in
|
|
|
|
# chunked encoding. Because chunked encoding is HTTP/1.1 feature,
|
|
|
|
# you must confirm the server to support HTTP/1.1 before sending it.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# http.set_form([["q", "ruby"], ["lang", "en"]])
|
|
|
|
#
|
|
|
|
# See also RFC 2388, RFC 2616, HTML 4.01, and HTML5
|
|
|
|
#
|
|
|
|
def set_form(params, enctype='application/x-www-form-urlencoded', formopt={})
|
|
|
|
@body_data = params
|
|
|
|
@body = nil
|
|
|
|
@body_stream = nil
|
|
|
|
@form_option = formopt
|
|
|
|
case enctype
|
|
|
|
when /\Aapplication\/x-www-form-urlencoded\z/i,
|
|
|
|
/\Amultipart\/form-data\z/i
|
|
|
|
self.content_type = enctype
|
|
|
|
else
|
|
|
|
raise ArgumentError, "invalid enctype: #{enctype}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Set the Authorization: header for "Basic" authorization.
|
2003-12-29 06:08:47 -05:00
|
|
|
def basic_auth(account, password)
|
2004-03-06 15:35:19 -05:00
|
|
|
@header['authorization'] = [basic_encode(account, password)]
|
2002-11-21 06:13:17 -05:00
|
|
|
end
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Set Proxy-Authorization: header for "Basic" authorization.
|
2003-12-29 06:08:47 -05:00
|
|
|
def proxy_basic_auth(account, password)
|
2004-03-06 15:35:19 -05:00
|
|
|
@header['proxy-authorization'] = [basic_encode(account, password)]
|
2002-11-21 06:13:17 -05:00
|
|
|
end
|
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def basic_encode(account, password)
|
2004-03-06 15:35:19 -05:00
|
|
|
'Basic ' + ["#{account}:#{password}"].pack('m').delete("\r\n")
|
2001-02-23 23:53:50 -05:00
|
|
|
end
|
2002-11-21 06:13:17 -05:00
|
|
|
private :basic_encode
|
2001-02-23 23:53:50 -05:00
|
|
|
|
2007-05-06 04:53:51 -04:00
|
|
|
def connection_close?
|
|
|
|
tokens(@header['connection']).include?('close') or
|
|
|
|
tokens(@header['proxy-connection']).include?('close')
|
|
|
|
end
|
|
|
|
|
|
|
|
def connection_keep_alive?
|
|
|
|
tokens(@header['connection']).include?('keep-alive') or
|
|
|
|
tokens(@header['proxy-connection']).include?('keep-alive')
|
|
|
|
end
|
|
|
|
|
|
|
|
def tokens(vals)
|
|
|
|
return [] unless vals
|
|
|
|
vals.map {|v| v.split(',') }.flatten\
|
|
|
|
.reject {|str| str.strip.empty? }\
|
2007-05-12 05:25:56 -04:00
|
|
|
.map {|tok| tok.strip.downcase }
|
2007-05-06 04:53:51 -04:00
|
|
|
end
|
|
|
|
private :tokens
|
|
|
|
|
2001-02-22 18:23:57 -05:00
|
|
|
end
|
|
|
|
|
2004-03-06 12:08:21 -05:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# HTTPGenericRequest is the parent of the HTTPRequest class.
|
|
|
|
# Do not use this directly; use a subclass of HTTPRequest.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# Mixes in the HTTPHeader module to provide easier access to HTTP headers.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2001-07-03 14:13:13 -04:00
|
|
|
class HTTPGenericRequest
|
2001-02-22 18:23:57 -05:00
|
|
|
|
2001-12-13 14:15:21 -05:00
|
|
|
include HTTPHeader
|
2001-02-22 18:23:57 -05:00
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def initialize(m, reqbody, resbody, path, initheader = nil)
|
2001-07-03 14:13:13 -04:00
|
|
|
@method = m
|
|
|
|
@request_has_body = reqbody
|
|
|
|
@response_has_body = resbody
|
2007-09-17 08:07:07 -04:00
|
|
|
raise ArgumentError, "no HTTP request path given" unless path
|
2004-01-29 05:28:39 -05:00
|
|
|
raise ArgumentError, "HTTP request path is empty" if path.empty?
|
2001-02-22 18:23:57 -05:00
|
|
|
@path = path
|
2004-03-08 09:16:21 -05:00
|
|
|
initialize_http_header initheader
|
|
|
|
self['Accept'] ||= '*/*'
|
2007-11-04 11:03:30 -05:00
|
|
|
self['User-Agent'] ||= 'Ruby'
|
2004-03-06 13:11:40 -05:00
|
|
|
@body = nil
|
|
|
|
@body_stream = nil
|
2010-12-12 20:04:37 -05:00
|
|
|
@body_data = nil
|
2001-02-22 18:23:57 -05:00
|
|
|
end
|
|
|
|
|
2001-07-03 14:13:13 -04:00
|
|
|
attr_reader :method
|
2001-02-22 18:23:57 -05:00
|
|
|
attr_reader :path
|
|
|
|
|
|
|
|
def inspect
|
2002-03-26 06:18:02 -05:00
|
|
|
"\#<#{self.class} #{@method}>"
|
2001-02-22 18:23:57 -05:00
|
|
|
end
|
|
|
|
|
2001-07-03 14:13:13 -04:00
|
|
|
def request_body_permitted?
|
|
|
|
@request_has_body
|
|
|
|
end
|
|
|
|
|
|
|
|
def response_body_permitted?
|
|
|
|
@response_has_body
|
2001-02-23 23:53:50 -05:00
|
|
|
end
|
|
|
|
|
2004-03-06 13:11:40 -05:00
|
|
|
def body_exist?
|
|
|
|
warn "Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?" if $VERBOSE
|
|
|
|
response_body_permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :body
|
|
|
|
|
|
|
|
def body=(str)
|
|
|
|
@body = str
|
|
|
|
@body_stream = nil
|
2010-12-12 20:04:37 -05:00
|
|
|
@body_data = nil
|
2004-03-06 13:11:40 -05:00
|
|
|
str
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :body_stream
|
|
|
|
|
|
|
|
def body_stream=(input)
|
|
|
|
@body = nil
|
|
|
|
@body_stream = input
|
2010-12-12 20:04:37 -05:00
|
|
|
@body_data = nil
|
2004-03-06 13:11:40 -05:00
|
|
|
input
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_body_internal(str) #:nodoc: internal use only
|
|
|
|
raise ArgumentError, "both of body argument and HTTPRequest#body set" if str and (@body or @body_stream)
|
|
|
|
self.body = str if str
|
|
|
|
end
|
2001-07-03 14:13:13 -04:00
|
|
|
|
2001-02-06 06:14:51 -05:00
|
|
|
#
|
|
|
|
# write
|
|
|
|
#
|
1999-12-29 06:14:04 -05:00
|
|
|
|
2004-03-06 13:11:40 -05:00
|
|
|
def exec(sock, ver, path) #:nodoc: internal use only
|
|
|
|
if @body
|
|
|
|
send_request_with_body sock, ver, path, @body
|
|
|
|
elsif @body_stream
|
|
|
|
send_request_with_body_stream sock, ver, path, @body_stream
|
2010-12-12 20:04:37 -05:00
|
|
|
elsif @body_data
|
|
|
|
send_request_with_body_data sock, ver, path, @body_data
|
2001-07-03 14:13:13 -04:00
|
|
|
else
|
2004-03-06 13:11:40 -05:00
|
|
|
write_header sock, ver, path
|
2000-11-07 06:27:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2002-03-26 06:18:02 -05:00
|
|
|
private
|
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def send_request_with_body(sock, ver, path, body)
|
2008-08-12 02:32:50 -04:00
|
|
|
self.content_length = body.bytesize
|
2004-03-06 15:35:19 -05:00
|
|
|
delete 'Transfer-Encoding'
|
2006-07-26 09:00:26 -04:00
|
|
|
supply_default_content_type
|
2004-03-06 13:11:40 -05:00
|
|
|
write_header sock, ver, path
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
wait_for_continue sock, ver if sock.continue_timeout
|
2002-11-21 06:13:17 -05:00
|
|
|
sock.write body
|
2001-01-13 14:07:15 -05:00
|
|
|
end
|
|
|
|
|
2004-03-06 13:11:40 -05:00
|
|
|
def send_request_with_body_stream(sock, ver, path, f)
|
2006-07-26 09:00:26 -04:00
|
|
|
unless content_length() or chunked?
|
|
|
|
raise ArgumentError,
|
|
|
|
"Content-Length not given and Transfer-Encoding is not `chunked'"
|
2004-03-06 13:11:40 -05:00
|
|
|
end
|
2006-07-26 09:00:26 -04:00
|
|
|
supply_default_content_type
|
2004-03-06 13:11:40 -05:00
|
|
|
write_header sock, ver, path
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
wait_for_continue sock, ver if sock.continue_timeout
|
2004-03-06 13:11:40 -05:00
|
|
|
if chunked?
|
|
|
|
while s = f.read(1024)
|
|
|
|
sock.write(sprintf("%x\r\n", s.length) << s << "\r\n")
|
|
|
|
end
|
|
|
|
sock.write "0\r\n\r\n"
|
|
|
|
else
|
|
|
|
while s = f.read(1024)
|
|
|
|
sock.write s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-12 20:04:37 -05:00
|
|
|
def send_request_with_body_data(sock, ver, path, params)
|
|
|
|
if /\Amultipart\/form-data\z/i !~ self.content_type
|
|
|
|
self.content_type = 'application/x-www-form-urlencoded'
|
|
|
|
return send_request_with_body(sock, ver, path, URI.encode_www_form(params))
|
|
|
|
end
|
|
|
|
|
|
|
|
opt = @form_option.dup
|
2011-06-05 21:53:41 -04:00
|
|
|
require 'securerandom' unless defined?(SecureRandom)
|
2010-12-12 20:04:37 -05:00
|
|
|
opt[:boundary] ||= SecureRandom.urlsafe_base64(40)
|
|
|
|
self.set_content_type(self.content_type, boundary: opt[:boundary])
|
|
|
|
if chunked?
|
|
|
|
write_header sock, ver, path
|
|
|
|
encode_multipart_form_data(sock, params, opt)
|
|
|
|
else
|
|
|
|
require 'tempfile'
|
|
|
|
file = Tempfile.new('multipart')
|
2010-12-13 23:05:03 -05:00
|
|
|
file.binmode
|
2010-12-12 20:04:37 -05:00
|
|
|
encode_multipart_form_data(file, params, opt)
|
|
|
|
file.rewind
|
|
|
|
self.content_length = file.size
|
|
|
|
write_header sock, ver, path
|
|
|
|
IO.copy_stream(file, sock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def encode_multipart_form_data(out, params, opt)
|
|
|
|
charset = opt[:charset]
|
|
|
|
boundary = opt[:boundary]
|
2011-06-05 21:53:41 -04:00
|
|
|
require 'securerandom' unless defined?(SecureRandom)
|
2010-12-12 20:04:37 -05:00
|
|
|
boundary ||= SecureRandom.urlsafe_base64(40)
|
|
|
|
chunked_p = chunked?
|
|
|
|
|
|
|
|
buf = ''
|
|
|
|
params.each do |key, value, h={}|
|
|
|
|
key = quote_string(key, charset)
|
|
|
|
filename =
|
|
|
|
h.key?(:filename) ? h[:filename] :
|
|
|
|
value.respond_to?(:to_path) ? File.basename(value.to_path) :
|
|
|
|
nil
|
|
|
|
|
|
|
|
buf << "--#{boundary}\r\n"
|
|
|
|
if filename
|
|
|
|
filename = quote_string(filename, charset)
|
|
|
|
type = h[:content_type] || 'application/octet-stream'
|
|
|
|
buf << "Content-Disposition: form-data; " \
|
|
|
|
"name=\"#{key}\"; filename=\"#{filename}\"\r\n" \
|
|
|
|
"Content-Type: #{type}\r\n\r\n"
|
|
|
|
if !out.respond_to?(:write) || !value.respond_to?(:read)
|
|
|
|
# if +out+ is not an IO or +value+ is not an IO
|
|
|
|
buf << (value.respond_to?(:read) ? value.read : value)
|
|
|
|
elsif value.respond_to?(:size) && chunked_p
|
|
|
|
# if +out+ is an IO and +value+ is a File, use IO.copy_stream
|
|
|
|
flush_buffer(out, buf, chunked_p)
|
|
|
|
out << "%x\r\n" % value.size if chunked_p
|
|
|
|
IO.copy_stream(value, out)
|
|
|
|
out << "\r\n" if chunked_p
|
|
|
|
else
|
|
|
|
# +out+ is an IO, and +value+ is not a File but an IO
|
|
|
|
flush_buffer(out, buf, chunked_p)
|
|
|
|
1 while flush_buffer(out, value.read(4096), chunked_p)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# non-file field:
|
|
|
|
# HTML5 says, "The parts of the generated multipart/form-data
|
|
|
|
# resource that correspond to non-file fields must not have a
|
|
|
|
# Content-Type header specified."
|
|
|
|
buf << "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n"
|
|
|
|
buf << (value.respond_to?(:read) ? value.read : value)
|
|
|
|
end
|
|
|
|
buf << "\r\n"
|
|
|
|
end
|
|
|
|
buf << "--#{boundary}--\r\n"
|
|
|
|
flush_buffer(out, buf, chunked_p)
|
|
|
|
out << "0\r\n\r\n" if chunked_p
|
|
|
|
end
|
|
|
|
|
|
|
|
def quote_string(str, charset)
|
|
|
|
str = str.encode(charset, fallback:->(c){'&#%d;'%c.encode("UTF-8").ord}) if charset
|
|
|
|
str = str.gsub(/[\\"]/, '\\\\\&')
|
|
|
|
end
|
|
|
|
|
|
|
|
def flush_buffer(out, buf, chunked_p)
|
|
|
|
return unless buf
|
|
|
|
out << "%x\r\n"%buf.bytesize if chunked_p
|
|
|
|
out << buf
|
|
|
|
out << "\r\n" if chunked_p
|
|
|
|
buf.clear
|
|
|
|
end
|
|
|
|
|
2006-07-26 09:00:26 -04:00
|
|
|
def supply_default_content_type
|
|
|
|
return if content_type()
|
|
|
|
warn 'net/http: warning: Content-Type did not set; using application/x-www-form-urlencoded' if $VERBOSE
|
|
|
|
set_content_type 'application/x-www-form-urlencoded'
|
|
|
|
end
|
|
|
|
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
##
|
|
|
|
# Waits up to the continue timeout for a response from the server provided
|
|
|
|
# we're speaking HTTP 1.1 and are expecting a 100-continue response.
|
|
|
|
|
|
|
|
def wait_for_continue(sock, ver)
|
|
|
|
if ver >= '1.1' and @header['expect'] and
|
|
|
|
@header['expect'].include?('100-continue')
|
|
|
|
if IO.select([sock.io], nil, nil, sock.continue_timeout)
|
|
|
|
res = HTTPResponse.read_new(sock)
|
|
|
|
unless res.kind_of?(Net::HTTPContinue)
|
|
|
|
throw :response, res
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-03-06 13:11:40 -05:00
|
|
|
def write_header(sock, ver, path)
|
2003-03-11 06:01:22 -05:00
|
|
|
buf = "#{@method} #{path} HTTP/#{ver}\r\n"
|
2003-12-29 06:08:47 -05:00
|
|
|
each_capitalized do |k,v|
|
2004-03-06 13:11:40 -05:00
|
|
|
buf << "#{k}: #{v}\r\n"
|
2001-01-13 14:07:15 -05:00
|
|
|
end
|
2003-03-11 06:01:22 -05:00
|
|
|
buf << "\r\n"
|
|
|
|
sock.write buf
|
2001-07-03 14:13:13 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2001-01-13 14:07:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# HTTP request class.
|
|
|
|
# This class wraps together the request header and the request path.
|
|
|
|
# You cannot use this class directly. Instead, you should use one of its
|
|
|
|
# subclasses: Net::HTTP::Get, Net::HTTP::Post, Net::HTTP::Head.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2001-07-03 14:13:13 -04:00
|
|
|
class HTTPRequest < HTTPGenericRequest
|
2003-08-01 10:01:14 -04:00
|
|
|
|
|
|
|
# Creates HTTP request object.
|
2003-12-29 06:08:47 -05:00
|
|
|
def initialize(path, initheader = nil)
|
2002-03-26 06:18:02 -05:00
|
|
|
super self.class::METHOD,
|
|
|
|
self.class::REQUEST_HAS_BODY,
|
|
|
|
self.class::RESPONSE_HAS_BODY,
|
2001-12-30 14:18:45 -05:00
|
|
|
path, initheader
|
2001-07-03 14:13:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2005-04-20 12:53:27 -04:00
|
|
|
class HTTP # reopen
|
2005-09-13 02:41:59 -04:00
|
|
|
#
|
2010-11-27 14:22:53 -05:00
|
|
|
# HTTP/1.1 methods --- RFC2616
|
2005-09-13 02:41:59 -04:00
|
|
|
#
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# See Net::HTTPGenericRequest for attributes and methods.
|
|
|
|
# See Net::HTTP for usage examples.
|
2001-12-13 14:15:21 -05:00
|
|
|
class Get < HTTPRequest
|
2001-02-07 02:23:09 -05:00
|
|
|
METHOD = 'GET'
|
2001-07-03 14:13:13 -04:00
|
|
|
REQUEST_HAS_BODY = false
|
|
|
|
RESPONSE_HAS_BODY = true
|
2001-02-07 02:23:09 -05:00
|
|
|
end
|
2001-02-06 06:14:51 -05:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# See Net::HTTPGenericRequest for attributes and methods.
|
|
|
|
# See Net::HTTP for usage examples.
|
2001-12-13 14:15:21 -05:00
|
|
|
class Head < HTTPRequest
|
2001-02-07 02:23:09 -05:00
|
|
|
METHOD = 'HEAD'
|
2001-07-03 14:13:13 -04:00
|
|
|
REQUEST_HAS_BODY = false
|
|
|
|
RESPONSE_HAS_BODY = false
|
2001-02-07 02:23:09 -05:00
|
|
|
end
|
2001-02-06 06:14:51 -05:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# See Net::HTTPGenericRequest for attributes and methods.
|
|
|
|
# See Net::HTTP for usage examples.
|
2001-12-13 14:15:21 -05:00
|
|
|
class Post < HTTPRequest
|
2001-02-07 02:23:09 -05:00
|
|
|
METHOD = 'POST'
|
2001-07-03 14:13:13 -04:00
|
|
|
REQUEST_HAS_BODY = true
|
|
|
|
RESPONSE_HAS_BODY = true
|
2001-02-07 02:23:09 -05:00
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# See Net::HTTPGenericRequest for attributes and methods.
|
|
|
|
# See Net::HTTP for usage examples.
|
2001-12-13 14:15:21 -05:00
|
|
|
class Put < HTTPRequest
|
2001-02-07 02:23:09 -05:00
|
|
|
METHOD = 'PUT'
|
2001-07-03 14:13:13 -04:00
|
|
|
REQUEST_HAS_BODY = true
|
|
|
|
RESPONSE_HAS_BODY = true
|
2001-02-07 02:23:09 -05:00
|
|
|
end
|
2001-02-06 06:14:51 -05:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# See Net::HTTPGenericRequest for attributes and methods.
|
|
|
|
# See Net::HTTP for usage examples.
|
2005-09-13 02:41:59 -04:00
|
|
|
class Delete < HTTPRequest
|
|
|
|
METHOD = 'DELETE'
|
|
|
|
REQUEST_HAS_BODY = false
|
2004-03-05 09:13:31 -05:00
|
|
|
RESPONSE_HAS_BODY = true
|
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# See Net::HTTPGenericRequest for attributes and methods.
|
2004-03-05 09:13:31 -05:00
|
|
|
class Options < HTTPRequest
|
|
|
|
METHOD = 'OPTIONS'
|
|
|
|
REQUEST_HAS_BODY = false
|
|
|
|
RESPONSE_HAS_BODY = false
|
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# See Net::HTTPGenericRequest for attributes and methods.
|
2005-09-13 02:41:59 -04:00
|
|
|
class Trace < HTTPRequest
|
|
|
|
METHOD = 'TRACE'
|
|
|
|
REQUEST_HAS_BODY = false
|
|
|
|
RESPONSE_HAS_BODY = true
|
|
|
|
end
|
|
|
|
|
2010-06-01 08:30:52 -04:00
|
|
|
#
|
|
|
|
# PATCH method --- RFC5789
|
|
|
|
#
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# See Net::HTTPGenericRequest for attributes and methods.
|
2010-06-01 08:30:52 -04:00
|
|
|
class Patch < HTTPRequest
|
|
|
|
METHOD = 'PATCH'
|
|
|
|
REQUEST_HAS_BODY = true
|
|
|
|
RESPONSE_HAS_BODY = true
|
|
|
|
end
|
|
|
|
|
2005-09-13 02:41:59 -04:00
|
|
|
#
|
|
|
|
# WebDAV methods --- RFC2518
|
|
|
|
#
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# See Net::HTTPGenericRequest for attributes and methods.
|
2004-03-05 09:13:31 -05:00
|
|
|
class Propfind < HTTPRequest
|
|
|
|
METHOD = 'PROPFIND'
|
|
|
|
REQUEST_HAS_BODY = true
|
|
|
|
RESPONSE_HAS_BODY = true
|
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# See Net::HTTPGenericRequest for attributes and methods.
|
2005-09-13 02:41:59 -04:00
|
|
|
class Proppatch < HTTPRequest
|
|
|
|
METHOD = 'PROPPATCH'
|
|
|
|
REQUEST_HAS_BODY = true
|
2004-03-05 09:13:31 -05:00
|
|
|
RESPONSE_HAS_BODY = true
|
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# See Net::HTTPGenericRequest for attributes and methods.
|
2005-09-13 02:41:59 -04:00
|
|
|
class Mkcol < HTTPRequest
|
|
|
|
METHOD = 'MKCOL'
|
|
|
|
REQUEST_HAS_BODY = true
|
2004-03-05 09:13:31 -05:00
|
|
|
RESPONSE_HAS_BODY = true
|
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# See Net::HTTPGenericRequest for attributes and methods.
|
2004-03-05 09:13:31 -05:00
|
|
|
class Copy < HTTPRequest
|
|
|
|
METHOD = 'COPY'
|
|
|
|
REQUEST_HAS_BODY = false
|
|
|
|
RESPONSE_HAS_BODY = true
|
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# See Net::HTTPGenericRequest for attributes and methods.
|
2005-09-13 02:41:59 -04:00
|
|
|
class Move < HTTPRequest
|
|
|
|
METHOD = 'MOVE'
|
|
|
|
REQUEST_HAS_BODY = false
|
|
|
|
RESPONSE_HAS_BODY = true
|
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# See Net::HTTPGenericRequest for attributes and methods.
|
2005-09-13 02:41:59 -04:00
|
|
|
class Lock < HTTPRequest
|
|
|
|
METHOD = 'LOCK'
|
2004-03-05 09:13:31 -05:00
|
|
|
REQUEST_HAS_BODY = true
|
|
|
|
RESPONSE_HAS_BODY = true
|
|
|
|
end
|
2004-05-18 13:29:55 -04:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# See Net::HTTPGenericRequest for attributes and methods.
|
2005-09-13 02:41:59 -04:00
|
|
|
class Unlock < HTTPRequest
|
|
|
|
METHOD = 'UNLOCK'
|
|
|
|
REQUEST_HAS_BODY = true
|
2004-05-18 13:29:55 -04:00
|
|
|
RESPONSE_HAS_BODY = true
|
|
|
|
end
|
2001-02-07 12:17:51 -05:00
|
|
|
end
|
2001-02-06 06:14:51 -05:00
|
|
|
|
|
|
|
|
2001-01-13 14:07:15 -05:00
|
|
|
###
|
2003-05-02 10:35:01 -04:00
|
|
|
### Response
|
2001-01-13 14:07:15 -05:00
|
|
|
###
|
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
# HTTP exception class.
|
2010-11-29 21:56:14 -05:00
|
|
|
# You cannot use HTTPExceptions directly; instead, you must use
|
|
|
|
# its subclasses.
|
2003-05-02 10:35:01 -04:00
|
|
|
module HTTPExceptions
|
2003-12-29 06:08:47 -05:00
|
|
|
def initialize(msg, res) #:nodoc:
|
2003-05-02 10:35:01 -04:00
|
|
|
super msg
|
|
|
|
@response = res
|
|
|
|
end
|
|
|
|
attr_reader :response
|
2003-08-05 12:12:05 -04:00
|
|
|
alias data response #:nodoc: obsolete
|
2003-05-02 10:35:01 -04:00
|
|
|
end
|
|
|
|
class HTTPError < ProtocolError
|
|
|
|
include HTTPExceptions
|
|
|
|
end
|
|
|
|
class HTTPRetriableError < ProtoRetriableError
|
|
|
|
include HTTPExceptions
|
|
|
|
end
|
|
|
|
class HTTPServerException < ProtoServerError
|
2003-08-01 10:01:14 -04:00
|
|
|
# We cannot use the name "HTTPServerError", it is the name of the response.
|
2003-05-02 10:35:01 -04:00
|
|
|
include HTTPExceptions
|
|
|
|
end
|
|
|
|
class HTTPFatalError < ProtoFatalError
|
|
|
|
include HTTPExceptions
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# HTTP response class.
|
|
|
|
#
|
|
|
|
# This class wraps together the response header and the response body (the
|
|
|
|
# entity requested).
|
|
|
|
#
|
|
|
|
# It mixes in the HTTPHeader module, which provides access to response
|
|
|
|
# header values both via hash-like methods and via individual readers.
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# Note that each possible HTTP response code defines its own
|
2003-08-01 10:01:14 -04:00
|
|
|
# HTTPResponse subclass. These are listed below.
|
2010-11-29 21:56:14 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# All classes are
|
|
|
|
# defined under the Net module. Indentation indicates inheritance.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# xxx HTTPResponse
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# 1xx HTTPInformation
|
2009-03-05 22:56:38 -05:00
|
|
|
# 100 HTTPContinue
|
2003-08-01 10:01:14 -04:00
|
|
|
# 101 HTTPSwitchProtocol
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# 2xx HTTPSuccess
|
|
|
|
# 200 HTTPOK
|
|
|
|
# 201 HTTPCreated
|
|
|
|
# 202 HTTPAccepted
|
|
|
|
# 203 HTTPNonAuthoritativeInformation
|
|
|
|
# 204 HTTPNoContent
|
|
|
|
# 205 HTTPResetContent
|
|
|
|
# 206 HTTPPartialContent
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# 3xx HTTPRedirection
|
|
|
|
# 300 HTTPMultipleChoice
|
|
|
|
# 301 HTTPMovedPermanently
|
|
|
|
# 302 HTTPFound
|
|
|
|
# 303 HTTPSeeOther
|
|
|
|
# 304 HTTPNotModified
|
|
|
|
# 305 HTTPUseProxy
|
|
|
|
# 307 HTTPTemporaryRedirect
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# 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
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# 5xx HTTPServerError
|
|
|
|
# 500 HTTPInternalServerError
|
|
|
|
# 501 HTTPNotImplemented
|
|
|
|
# 502 HTTPBadGateway
|
|
|
|
# 503 HTTPServiceUnavailable
|
|
|
|
# 504 HTTPGatewayTimeOut
|
|
|
|
# 505 HTTPVersionNotSupported
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-01 10:01:14 -04:00
|
|
|
# xxx HTTPUnknownResponse
|
2003-08-01 11:41:41 -04:00
|
|
|
#
|
2002-02-19 07:33:52 -05:00
|
|
|
class HTTPResponse
|
2010-11-30 03:06:19 -05:00
|
|
|
# true if the response has a body.
|
2003-08-01 11:41:41 -04:00
|
|
|
def HTTPResponse.body_permitted?
|
2002-02-19 07:33:52 -05:00
|
|
|
self::HAS_BODY
|
|
|
|
end
|
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
def HTTPResponse.exception_type # :nodoc: internal use only
|
2002-02-19 07:33:52 -05:00
|
|
|
self::EXCEPTION_TYPE
|
|
|
|
end
|
2005-09-13 02:41:59 -04:00
|
|
|
end # reopened after
|
2002-02-19 07:33:52 -05:00
|
|
|
|
2003-08-21 05:07:57 -04:00
|
|
|
# :stopdoc:
|
|
|
|
|
2002-02-19 07:33:52 -05:00
|
|
|
class HTTPUnknownResponse < HTTPResponse
|
|
|
|
HAS_BODY = true
|
2003-05-02 10:35:01 -04:00
|
|
|
EXCEPTION_TYPE = HTTPError
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPInformation < HTTPResponse # 1xx
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = false
|
2003-05-02 10:35:01 -04:00
|
|
|
EXCEPTION_TYPE = HTTPError
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPSuccess < HTTPResponse # 2xx
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
2003-05-02 10:35:01 -04:00
|
|
|
EXCEPTION_TYPE = HTTPError
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPRedirection < HTTPResponse # 3xx
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
2003-05-02 10:35:01 -04:00
|
|
|
EXCEPTION_TYPE = HTTPRetriableError
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPClientError < HTTPResponse # 4xx
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
2003-05-02 10:35:01 -04:00
|
|
|
EXCEPTION_TYPE = HTTPServerException # for backward compatibility
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPServerError < HTTPResponse # 5xx
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
2003-05-02 10:35:01 -04:00
|
|
|
EXCEPTION_TYPE = HTTPFatalError # for backward compatibility
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
|
|
|
|
class HTTPContinue < HTTPInformation # 100
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = false
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPSwitchProtocol < HTTPInformation # 101
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = false
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
|
|
|
|
class HTTPOK < HTTPSuccess # 200
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPCreated < HTTPSuccess # 201
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPAccepted < HTTPSuccess # 202
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPNonAuthoritativeInformation < HTTPSuccess # 203
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPNoContent < HTTPSuccess # 204
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = false
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPResetContent < HTTPSuccess # 205
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = false
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPPartialContent < HTTPSuccess # 206
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
|
|
|
|
class HTTPMultipleChoice < HTTPRedirection # 300
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPMovedPermanently < HTTPRedirection # 301
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPFound < HTTPRedirection # 302
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
HTTPMovedTemporarily = HTTPFound
|
|
|
|
class HTTPSeeOther < HTTPRedirection # 303
|
|
|
|
HAS_BODY = true
|
|
|
|
end
|
|
|
|
class HTTPNotModified < HTTPRedirection # 304
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = false
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPUseProxy < HTTPRedirection # 305
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = false
|
|
|
|
end
|
2002-03-26 06:18:02 -05: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 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPPaymentRequired < HTTPClientError # 402
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPForbidden < HTTPClientError # 403
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPNotFound < HTTPClientError # 404
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPMethodNotAllowed < HTTPClientError # 405
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPNotAcceptable < HTTPClientError # 406
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPProxyAuthenticationRequired < HTTPClientError # 407
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPRequestTimeOut < HTTPClientError # 408
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPConflict < HTTPClientError # 409
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPGone < HTTPClientError # 410
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPLengthRequired < HTTPClientError # 411
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPPreconditionFailed < HTTPClientError # 412
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPRequestEntityTooLarge < HTTPClientError # 413
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPRequestURITooLong < HTTPClientError # 414
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
HTTPRequestURITooLarge = HTTPRequestURITooLong
|
|
|
|
class HTTPUnsupportedMediaType < HTTPClientError # 415
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPRequestedRangeNotSatisfiable < HTTPClientError # 416
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPExpectationFailed < HTTPClientError # 417
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
|
|
|
|
class HTTPInternalServerError < HTTPServerError # 500
|
|
|
|
HAS_BODY = true
|
|
|
|
end
|
|
|
|
class HTTPNotImplemented < HTTPServerError # 501
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPBadGateway < HTTPServerError # 502
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPServiceUnavailable < HTTPServerError # 503
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPGatewayTimeOut < HTTPServerError # 504
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
class HTTPVersionNotSupported < HTTPServerError # 505
|
2002-02-19 07:33:52 -05:00
|
|
|
HAS_BODY = true
|
|
|
|
end
|
|
|
|
|
2003-08-21 05:07:57 -04:00
|
|
|
# :startdoc:
|
|
|
|
|
2002-02-19 07:33:52 -05:00
|
|
|
|
2005-09-13 02:41:59 -04:00
|
|
|
class HTTPResponse # reopen
|
2001-02-22 18:23:57 -05:00
|
|
|
|
2001-02-07 12:17:51 -05:00
|
|
|
CODE_CLASS_TO_OBJ = {
|
2002-02-19 07:33:52 -05:00
|
|
|
'1' => HTTPInformation,
|
|
|
|
'2' => HTTPSuccess,
|
|
|
|
'3' => HTTPRedirection,
|
|
|
|
'4' => HTTPClientError,
|
|
|
|
'5' => HTTPServerError
|
2000-10-11 01:27:56 -04:00
|
|
|
}
|
2001-02-07 12:17:51 -05:00
|
|
|
CODE_TO_OBJ = {
|
2002-02-19 07:33:52 -05:00
|
|
|
'100' => HTTPContinue,
|
2000-10-12 05:54:32 -04:00
|
|
|
'101' => HTTPSwitchProtocol,
|
2000-10-11 01: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 06:18:02 -05:00
|
|
|
'302' => HTTPFound,
|
|
|
|
'303' => HTTPSeeOther,
|
2000-10-11 01:27:56 -04:00
|
|
|
'304' => HTTPNotModified,
|
|
|
|
'305' => HTTPUseProxy,
|
2002-03-26 06:18:02 -05:00
|
|
|
'307' => HTTPTemporaryRedirect,
|
2000-10-11 01: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 07:33:52 -05:00
|
|
|
'411' => HTTPLengthRequired,
|
2000-10-11 01:27:56 -04:00
|
|
|
'412' => HTTPPreconditionFailed,
|
|
|
|
'413' => HTTPRequestEntityTooLarge,
|
2002-03-26 06:18:02 -05:00
|
|
|
'414' => HTTPRequestURITooLong,
|
2000-10-11 01:27:56 -04:00
|
|
|
'415' => HTTPUnsupportedMediaType,
|
2002-03-26 06:18:02 -05:00
|
|
|
'416' => HTTPRequestedRangeNotSatisfiable,
|
|
|
|
'417' => HTTPExpectationFailed,
|
2000-10-11 01:27:56 -04:00
|
|
|
|
2006-06-22 21:52:11 -04:00
|
|
|
'500' => HTTPInternalServerError,
|
2000-10-11 01:27:56 -04:00
|
|
|
'501' => HTTPNotImplemented,
|
|
|
|
'502' => HTTPBadGateway,
|
|
|
|
'503' => HTTPServiceUnavailable,
|
|
|
|
'504' => HTTPGatewayTimeOut,
|
|
|
|
'505' => HTTPVersionNotSupported
|
2000-03-27 10:52:27 -05:00
|
|
|
}
|
|
|
|
|
2004-03-06 12:08:21 -05:00
|
|
|
class << HTTPResponse
|
2003-12-29 06:08:47 -05:00
|
|
|
def read_new(sock) #:nodoc: internal use only
|
2002-02-22 07:10:58 -05:00
|
|
|
httpv, code, msg = read_status_line(sock)
|
2002-03-26 06:18:02 -05:00
|
|
|
res = response_class(code).new(httpv, code, msg)
|
2002-02-22 07:10:58 -05:00
|
|
|
each_response_header(sock) do |k,v|
|
2005-04-20 09:55:26 -04:00
|
|
|
res.add_field k, v
|
2002-02-22 07:10:58 -05:00
|
|
|
end
|
2002-02-19 07:33:52 -05:00
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def read_status_line(sock)
|
2002-02-19 07:33:52 -05:00
|
|
|
str = sock.readline
|
|
|
|
m = /\AHTTP(?:\/(\d+\.\d+))?\s+(\d\d\d)\s*(.*)\z/in.match(str) or
|
2003-12-29 06:08:47 -05:00
|
|
|
raise HTTPBadResponse, "wrong status line: #{str.dump}"
|
|
|
|
m.captures
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
2001-07-03 14:13:13 -04:00
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def response_class(code)
|
2002-02-19 07:33:52 -05:00
|
|
|
CODE_TO_OBJ[code] or
|
|
|
|
CODE_CLASS_TO_OBJ[code[0,1]] or
|
|
|
|
HTTPUnknownResponse
|
|
|
|
end
|
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def each_response_header(sock)
|
2009-10-30 09:09:03 -04:00
|
|
|
key = value = nil
|
2002-11-21 06:13:17 -05:00
|
|
|
while true
|
|
|
|
line = sock.readuntil("\n", true).sub(/\s+\z/, '')
|
2001-07-03 14:13:13 -04:00
|
|
|
break if line.empty?
|
2009-10-31 04:18:32 -04:00
|
|
|
if line[0] == ?\s or line[0] == ?\t and value
|
2009-10-30 09:09:03 -04:00
|
|
|
value << ' ' unless value.empty?
|
|
|
|
value << line.strip
|
|
|
|
else
|
|
|
|
yield key, value if key
|
|
|
|
key, value = line.strip.split(/\s*:\s*/, 2)
|
|
|
|
raise HTTPBadResponse, 'wrong header line format' if value.nil?
|
|
|
|
end
|
2001-07-03 14:13:13 -04:00
|
|
|
end
|
2009-10-30 09:09:03 -04:00
|
|
|
yield key, value if key
|
2001-07-03 14:13:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# next is to fix bug in RDoc, where the private inside class << self
|
|
|
|
# spills out.
|
2009-03-05 22:56:38 -05:00
|
|
|
public
|
2001-07-03 14:13:13 -04:00
|
|
|
|
2002-02-19 07:33:52 -05:00
|
|
|
include HTTPHeader
|
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def initialize(httpv, code, msg) #:nodoc: internal use only
|
2002-02-19 07:33:52 -05:00
|
|
|
@http_version = httpv
|
|
|
|
@code = code
|
|
|
|
@message = msg
|
2004-03-08 09:16:21 -05:00
|
|
|
initialize_http_header nil
|
2001-01-13 14:07:15 -05:00
|
|
|
@body = nil
|
|
|
|
@read = false
|
|
|
|
end
|
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
# The HTTP version supported by the server.
|
2001-02-07 12:17:51 -05:00
|
|
|
attr_reader :http_version
|
2003-08-01 10:01:14 -04:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# The HTTP result code string. For example, '302'. You can also
|
|
|
|
# determine the response type by examining which response subclass
|
|
|
|
# the response object is an instance of.
|
2002-02-19 07:33:52 -05:00
|
|
|
attr_reader :code
|
2003-08-01 10:01:14 -04:00
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# The HTTP result message sent by the server. For example, 'Not Found'.
|
2002-02-19 07:33:52 -05:00
|
|
|
attr_reader :message
|
2003-08-05 12:12:05 -04:00
|
|
|
alias msg message # :nodoc: obsolete
|
2001-02-07 12:17:51 -05:00
|
|
|
|
2001-01-13 14:07:15 -05:00
|
|
|
def inspect
|
2004-03-06 13:11:40 -05:00
|
|
|
"#<#{self.class} #{@code} #{@message} readbody=#{@read}>"
|
2001-01-13 14:07:15 -05:00
|
|
|
end
|
|
|
|
|
2002-02-19 07:33:52 -05:00
|
|
|
#
|
|
|
|
# response <-> exception relationship
|
|
|
|
#
|
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
def code_type #:nodoc:
|
2002-03-26 06:18:02 -05:00
|
|
|
self.class
|
2001-01-13 14:07:15 -05:00
|
|
|
end
|
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
def error! #:nodoc:
|
2002-03-26 06:18:02 -05:00
|
|
|
raise error_type().new(@code + ' ' + @message.dump, self)
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
def error_type #:nodoc:
|
2002-03-26 06:18:02 -05:00
|
|
|
self.class::EXCEPTION_TYPE
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Raises an HTTP error if the response is not 2xx (success).
|
2003-08-05 12:12:05 -04:00
|
|
|
def value
|
2004-03-06 12:08:21 -05:00
|
|
|
error! unless self.kind_of?(HTTPSuccess)
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
2001-01-13 14:07:15 -05:00
|
|
|
|
2001-02-06 06:14:51 -05:00
|
|
|
#
|
2002-03-26 06:18:02 -05:00
|
|
|
# header (for backward compatibility only; DO NOT USE)
|
2001-02-06 06:14:51 -05:00
|
|
|
#
|
2000-12-24 14:39:15 -05:00
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
def response #:nodoc:
|
2004-03-05 06:59:09 -05:00
|
|
|
warn "#{caller(1)[0]}: warning: HTTPResponse#response is obsolete" if $VERBOSE
|
2001-01-13 14:07:15 -05:00
|
|
|
self
|
|
|
|
end
|
2000-12-24 14:39:15 -05:00
|
|
|
|
2004-03-05 06:59:09 -05:00
|
|
|
def header #:nodoc:
|
|
|
|
warn "#{caller(1)[0]}: warning: HTTPResponse#header is obsolete" if $VERBOSE
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_header #:nodoc:
|
|
|
|
warn "#{caller(1)[0]}: warning: HTTPResponse#read_header is obsolete" if $VERBOSE
|
|
|
|
self
|
|
|
|
end
|
2001-01-13 14:07:15 -05:00
|
|
|
|
2001-02-06 06:14:51 -05:00
|
|
|
#
|
2001-01-13 14:07:15 -05:00
|
|
|
# body
|
2001-02-06 06:14:51 -05:00
|
|
|
#
|
2001-01-13 14:07:15 -05:00
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def reading_body(sock, reqmethodallowbody) #:nodoc: internal use only
|
2002-03-26 06:18:02 -05:00
|
|
|
@socket = sock
|
|
|
|
@body_exist = reqmethodallowbody && self.class.body_permitted?
|
2002-11-21 06:13:17 -05:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
self.body # ensure to read body
|
|
|
|
ensure
|
|
|
|
@socket = nil
|
|
|
|
end
|
2002-03-26 06:18:02 -05:00
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Gets the entity body returned by the remote HTTP server.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2010-11-29 21:56:14 -05:00
|
|
|
# If a block is given, the body is passed to the block, and
|
|
|
|
# the body is provided in fragments, as it is read in from the socket.
|
|
|
|
#
|
|
|
|
# Calling this method a second or subsequent time for the same
|
|
|
|
# HTTPResponse object will return the value already read.
|
2003-08-01 11:41:41 -04:00
|
|
|
#
|
|
|
|
# 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 06:08:47 -05:00
|
|
|
def read_body(dest = nil, &block)
|
2002-11-21 06:13:17 -05:00
|
|
|
if @read
|
|
|
|
raise IOError, "#{self.class}\#read_body called twice" if dest or block
|
2001-12-30 14:18:45 -05:00
|
|
|
return @body
|
2001-01-13 14:07:15 -05:00
|
|
|
end
|
2001-12-30 14:18:45 -05:00
|
|
|
to = procdest(dest, block)
|
|
|
|
stream_check
|
2002-11-21 06:13:17 -05:00
|
|
|
if @body_exist
|
2001-12-30 14:18:45 -05:00
|
|
|
read_body_0 to
|
|
|
|
@body = to
|
|
|
|
else
|
|
|
|
@body = nil
|
2001-01-13 14:07:15 -05:00
|
|
|
end
|
2001-12-30 14:18:45 -05:00
|
|
|
@read = true
|
2001-01-13 14:07:15 -05:00
|
|
|
|
|
|
|
@body
|
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Returns the full entity body.
|
2003-08-01 11:41:41 -04:00
|
|
|
#
|
|
|
|
# Calling this method a second or subsequent time will return the
|
2010-11-29 21:56:14 -05:00
|
|
|
# string already read.
|
2003-08-01 11:41:41 -04:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2007-12-10 11:53:52 -05:00
|
|
|
# Because it may be necessary to modify the body, Eg, decompression
|
|
|
|
# this method facilitates that.
|
|
|
|
def body=(value)
|
|
|
|
@body = value
|
|
|
|
end
|
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
alias entity body #:nodoc: obsolete
|
2001-01-13 14:07:15 -05:00
|
|
|
|
2001-02-06 06:14:51 -05:00
|
|
|
private
|
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def read_body_0(dest)
|
2002-11-21 06:13:17 -05:00
|
|
|
if chunked?
|
2000-11-16 09:03:20 -05:00
|
|
|
read_chunked dest
|
2003-12-29 06:08:47 -05:00
|
|
|
return
|
1999-12-29 06:14:04 -05:00
|
|
|
end
|
2003-12-29 06:08:47 -05: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 06:27:16 -05:00
|
|
|
end
|
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def read_chunked(dest)
|
1999-12-29 06:14:04 -05:00
|
|
|
len = nil
|
|
|
|
total = 0
|
2002-11-21 06:13:17 -05:00
|
|
|
while true
|
1999-12-29 06:14:04 -05:00
|
|
|
line = @socket.readline
|
2002-11-21 06:13:17 -05:00
|
|
|
hexlen = line.slice(/[0-9a-fA-F]+/) or
|
2003-12-29 06:08:47 -05:00
|
|
|
raise HTTPBadResponse, "wrong chunk size line: #{line}"
|
2002-11-21 06:13:17 -05:00
|
|
|
len = hexlen.hex
|
1999-12-29 06:14:04 -05:00
|
|
|
break if len == 0
|
2010-05-03 19:42:26 -04:00
|
|
|
begin
|
|
|
|
@socket.read len, dest
|
|
|
|
ensure
|
|
|
|
total += len
|
|
|
|
@socket.read 2 # \r\n
|
|
|
|
end
|
1999-12-20 15:48:49 -05:00
|
|
|
end
|
2002-11-21 06:13:17 -05:00
|
|
|
until @socket.readline.empty?
|
2002-02-19 07:33:52 -05:00
|
|
|
# none
|
1999-12-29 06:14:04 -05:00
|
|
|
end
|
2000-03-26 03:48:15 -05:00
|
|
|
end
|
|
|
|
|
2001-01-13 14:07:15 -05:00
|
|
|
def stream_check
|
2004-03-29 02:54:38 -05:00
|
|
|
raise IOError, 'attempt to read body out of block' if @socket.closed?
|
2001-01-13 14:07:15 -05:00
|
|
|
end
|
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def procdest(dest, block)
|
2004-03-29 02:54:38 -05:00
|
|
|
raise ArgumentError, 'both arg and block given for HTTP method' \
|
2003-12-29 06:08:47 -05:00
|
|
|
if dest and block
|
2002-11-21 06:13:17 -05:00
|
|
|
if block
|
2002-02-19 07:33:52 -05:00
|
|
|
ReadAdapter.new(block)
|
2001-01-13 14:07:15 -05:00
|
|
|
else
|
2001-02-17 15:40:50 -05:00
|
|
|
dest || ''
|
2001-01-13 14:07:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-12-20 15:48:49 -05: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 02:04:40 -04:00
|
|
|
|
2003-08-21 05:07:57 -04:00
|
|
|
# :enddoc:
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
#--
|
2002-02-19 07:33:52 -05:00
|
|
|
# for backward compatibility
|
2003-07-01 22:05:35 -04:00
|
|
|
class HTTP
|
|
|
|
ProxyMod = ProxyDelta
|
|
|
|
end
|
2003-08-21 05:07:57 -04:00
|
|
|
module NetPrivate
|
2003-07-01 22:05:35 -04:00
|
|
|
HTTPRequest = ::Net::HTTPRequest
|
|
|
|
end
|
|
|
|
|
2002-02-19 07:33:52 -05:00
|
|
|
HTTPInformationCode = HTTPInformation
|
|
|
|
HTTPSuccessCode = HTTPSuccess
|
|
|
|
HTTPRedirectionCode = HTTPRedirection
|
|
|
|
HTTPRetriableCode = HTTPRedirection
|
|
|
|
HTTPClientErrorCode = HTTPClientError
|
|
|
|
HTTPFatalErrorCode = HTTPClientError
|
|
|
|
HTTPServerErrorCode = HTTPServerError
|
2001-12-13 14:15:21 -05:00
|
|
|
HTTPResponceReceiver = HTTPResponse
|
2001-06-26 19:49:21 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
end # module Net
|