2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
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
|
|
|
|
2017-10-23 21:22:08 -04:00
|
|
|
require_relative 'protocol'
|
2002-07-11 17:33:38 -04:00
|
|
|
require 'uri'
|
2018-12-14 04:44:20 -05: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
|
2018-10-21 16:00:46 -04:00
|
|
|
# [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
|
|
|
#
|
2018-10-21 16:00:46 -04:00
|
|
|
# The following example code can be used as the basis of an 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|
|
2012-12-21 15:36:07 -05:00
|
|
|
# request = Net::HTTP::Get.new 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
|
|
|
#
|
2018-01-30 13:32:40 -05:00
|
|
|
# If you wish to re-use a connection across multiple HTTP requests without
|
|
|
|
# automatically closing it you can use ::new and then call #start and
|
|
|
|
# #finish manually.
|
|
|
|
#
|
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
|
|
|
#
|
2012-12-21 15:36:07 -05:00
|
|
|
# For all the Net::HTTP request objects and shortcut request methods you may
|
|
|
|
# supply either a String for the request path or a URI from which Net::HTTP
|
|
|
|
# will extract the request path.
|
|
|
|
#
|
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
|
2018-10-21 16:00:46 -04:00
|
|
|
# creates a URL encoded 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')
|
2012-12-21 15:36:07 -05:00
|
|
|
# req = Net::HTTP::Post.new(uri)
|
2010-12-01 10:07:35 -05:00
|
|
|
# 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
|
|
|
#
|
2018-10-21 16:00:46 -04:00
|
|
|
# To send multipart/form-data use Net::HTTPHeader#set_form:
|
2010-11-29 21:56:14 -05:00
|
|
|
#
|
2012-12-21 15:36:07 -05:00
|
|
|
# req = Net::HTTP::Post.new(uri)
|
2018-10-20 07:09:34 -04:00
|
|
|
# req.set_form([['upload', File.open('foo.bar')]], '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
|
|
|
#
|
2012-12-21 15:36:07 -05:00
|
|
|
# req = Net::HTTP::Get.new(uri)
|
2010-12-01 10:07:35 -05:00
|
|
|
# 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
|
2018-10-21 16:00:46 -04:00
|
|
|
# [RFC2617](http://www.ietf.org/rfc/rfc2617.txt).
|
2010-12-01 10:07:35 -05:00
|
|
|
#
|
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
|
|
|
#
|
2012-12-21 15:36:07 -05:00
|
|
|
# req = Net::HTTP::Get.new(uri)
|
2010-12-01 10:07:35 -05:00
|
|
|
# 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|
|
2012-12-21 15:36:07 -05:00
|
|
|
# request = Net::HTTP::Get.new uri
|
2010-12-01 10:07:35 -05:00
|
|
|
#
|
|
|
|
# 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
|
|
|
#
|
2016-04-21 22:27:10 -04:00
|
|
|
# Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
|
2012-12-21 15:36:07 -05:00
|
|
|
# request = Net::HTTP::Get.new uri
|
2010-12-01 10:07:35 -05:00
|
|
|
# response = http.request request # Net::HTTPResponse object
|
|
|
|
# end
|
|
|
|
#
|
2016-04-21 22:27:10 -04:00
|
|
|
# Or if you simply want to make a GET request, you may pass in an URI
|
2018-10-21 16:00:46 -04:00
|
|
|
# object that has an HTTPS URL. Net::HTTP automatically turns on TLS
|
2016-04-21 22:27:10 -04:00
|
|
|
# verification if the URI object has a 'https' URI scheme.
|
|
|
|
#
|
|
|
|
# uri = URI('https://example.com/')
|
|
|
|
# Net::HTTP.get(uri) # => String
|
|
|
|
#
|
2013-07-18 09:50:32 -04:00
|
|
|
# In previous versions of Ruby you would need to require 'net/https' to use
|
2016-04-21 22:27:10 -04:00
|
|
|
# HTTPS. This is no longer true.
|
2010-12-01 10:07:35 -05:00
|
|
|
#
|
|
|
|
# === Proxies
|
|
|
|
#
|
2012-07-19 21:56:21 -04:00
|
|
|
# Net::HTTP will automatically create a proxy from the +http_proxy+
|
|
|
|
# environment variable if it is present. To disable use of +http_proxy+,
|
|
|
|
# pass +nil+ for the proxy address.
|
|
|
|
#
|
|
|
|
# You may also create a custom proxy:
|
2010-12-01 10:07:35 -05:00
|
|
|
#
|
|
|
|
# proxy_addr = 'your.proxy.host'
|
|
|
|
# proxy_port = 8080
|
|
|
|
#
|
2012-07-19 21:56:21 -04:00
|
|
|
# Net::HTTP.new('example.com', nil, proxy_addr, proxy_port).start { |http|
|
|
|
|
# # always proxy via your.proxy.addr:8080
|
2010-12-01 10:07:35 -05:00
|
|
|
# }
|
|
|
|
#
|
2012-07-19 21:56:21 -04:00
|
|
|
# See Net::HTTP.new for further details and examples such as proxies that
|
2010-12-01 10:07:35 -05:00
|
|
|
# require a username and password.
|
|
|
|
#
|
2012-07-19 18:43:38 -04:00
|
|
|
# === Compression
|
|
|
|
#
|
|
|
|
# Net::HTTP automatically adds Accept-Encoding for compression of response
|
|
|
|
# bodies and automatically decompresses gzip and deflate responses unless a
|
|
|
|
# Range header was sent.
|
|
|
|
#
|
|
|
|
# Compression can be disabled through the Accept-Encoding: identity header.
|
|
|
|
#
|
2010-12-01 10:07:35 -05:00
|
|
|
# == HTTP Request Classes
|
|
|
|
#
|
|
|
|
# Here is the HTTP request class hierarchy.
|
|
|
|
#
|
|
|
|
# * Net::HTTPRequest
|
|
|
|
# * Net::HTTP::Get
|
|
|
|
# * Net::HTTP::Head
|
|
|
|
# * Net::HTTP::Post
|
2012-11-30 00:45:23 -05:00
|
|
|
# * Net::HTTP::Patch
|
2010-12-01 10:07:35 -05:00
|
|
|
# * 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
|
2012-06-13 11:20:27 -04:00
|
|
|
# HTTPMultiStatus:: 207
|
2013-12-08 12:11:46 -05:00
|
|
|
# HTTPIMUsed:: 226
|
2010-12-01 10:07:35 -05:00
|
|
|
# HTTPRedirection:: 3xx
|
2012-06-13 11:20:27 -04:00
|
|
|
# HTTPMultipleChoices:: 300
|
2010-12-01 10:07:35 -05:00
|
|
|
# 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
|
2012-06-13 11:20:27 -04:00
|
|
|
# HTTPUnprocessableEntity:: 422
|
|
|
|
# HTTPLocked:: 423
|
|
|
|
# HTTPFailedDependency:: 424
|
|
|
|
# HTTPUpgradeRequired:: 426
|
2012-06-08 18:40:14 -04:00
|
|
|
# HTTPPreconditionRequired:: 428
|
|
|
|
# HTTPTooManyRequests:: 429
|
|
|
|
# HTTPRequestHeaderFieldsTooLarge:: 431
|
2015-12-30 06:53:15 -05:00
|
|
|
# HTTPUnavailableForLegalReasons:: 451
|
2010-12-01 10:07:35 -05:00
|
|
|
# HTTPServerError:: 5xx
|
|
|
|
# HTTPInternalServerError:: 500
|
|
|
|
# HTTPNotImplemented:: 501
|
|
|
|
# HTTPBadGateway:: 502
|
|
|
|
# HTTPServiceUnavailable:: 503
|
|
|
|
# HTTPGatewayTimeOut:: 504
|
|
|
|
# HTTPVersionNotSupported:: 505
|
2012-06-13 11:20:27 -04:00
|
|
|
# HTTPInsufficientStorage:: 507
|
2012-06-08 18:40:14 -04:00
|
|
|
# HTTPNetworkAuthenticationRequired:: 511
|
2010-12-01 10:07:35 -05:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
2013-07-18 09:50:32 -04:00
|
|
|
# Turns on net/http 1.2 (Ruby 1.8) features.
|
|
|
|
# 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|
|
2012-12-21 15:36:07 -05:00
|
|
|
return http.request_get(uri, &block)
|
2005-07-26 09:05:31 -04:00
|
|
|
}
|
2002-03-26 06:18:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-05 10:39:06 -04:00
|
|
|
# Posts data to the specified URI object.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
# require 'uri'
|
|
|
|
#
|
|
|
|
# Net::HTTP.post URI('http://www.example.com/api/search'),
|
|
|
|
# { "q" => "ruby", "max" => "50" }.to_json,
|
|
|
|
# "Content-Type" => "application/json"
|
|
|
|
#
|
|
|
|
def HTTP.post(url, data, header = nil)
|
|
|
|
start(url.hostname, url.port,
|
|
|
|
:use_ssl => url.scheme == 'https' ) {|http|
|
2017-09-15 12:53:55 -04:00
|
|
|
http.post(url, data, header)
|
2016-11-05 10:39:06 -04:00
|
|
|
}
|
|
|
|
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'
|
|
|
|
#
|
2013-02-07 17:35:25 -05:00
|
|
|
# Net::HTTP.post_form URI('http://www.example.com/search.cgi'),
|
|
|
|
# { "q" => "ruby", "max" => "50" }
|
2005-04-20 09:55:26 -04:00
|
|
|
#
|
|
|
|
def HTTP.post_form(url, params)
|
2012-12-21 15:36:07 -05:00
|
|
|
req = Post.new(url)
|
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,
|
2011-11-26 15:03:32 -05:00
|
|
|
:use_ssl => url.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)
|
2017-05-19 11:13:42 -04:00
|
|
|
# HTTP.start(address, port=nil, p_addr=:ENV, p_port=nil, p_user=nil, p_pass=nil, opt, &block)
|
2010-01-18 01:32:14 -05:00
|
|
|
#
|
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,
|
2018-06-06 04:03:47 -04:00
|
|
|
# close_on_empty_response, key, open_timeout, read_timeout, write_timeout, ssl_timeout,
|
2010-01-12 00:56:28 -05:00
|
|
|
# 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
|
2017-05-19 11:13:42 -04:00
|
|
|
p_addr = :ENV if arg.size < 2
|
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
|
|
|
|
2012-07-21 11:45:10 -04:00
|
|
|
class << HTTP
|
|
|
|
alias newobj new # :nodoc:
|
|
|
|
end
|
|
|
|
|
2010-11-29 21:56:14 -05:00
|
|
|
# Creates a new Net::HTTP object without opening a TCP connection or
|
|
|
|
# HTTP session.
|
2012-07-19 21:56:21 -04:00
|
|
|
#
|
|
|
|
# The +address+ should be a DNS hostname or IP address, the +port+ is the
|
|
|
|
# port the server operates on. If no +port+ is given the default port for
|
|
|
|
# HTTP or HTTPS is used.
|
|
|
|
#
|
|
|
|
# If none of the +p_+ arguments are given, the proxy host and port are
|
|
|
|
# taken from the +http_proxy+ environment variable (or its uppercase
|
|
|
|
# equivalent) if present. If the proxy requires authentication you must
|
|
|
|
# supply it by hand. See URI::Generic#find_proxy for details of proxy
|
|
|
|
# detection from the environment. To disable proxy detection set +p_addr+
|
|
|
|
# to nil.
|
|
|
|
#
|
2017-11-07 15:13:48 -05:00
|
|
|
# If you are connecting to a custom proxy, +p_addr+ specifies the DNS name
|
|
|
|
# or IP address of the proxy host, +p_port+ the port to use to access the
|
|
|
|
# proxy, +p_user+ and +p_pass+ the username and password if authorization
|
|
|
|
# is required to use the proxy, and p_no_proxy hosts which do not
|
2017-09-28 06:51:37 -04:00
|
|
|
# use the proxy.
|
2012-07-19 21:56:21 -04:00
|
|
|
#
|
2017-09-28 06:51:37 -04:00
|
|
|
def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil)
|
2012-07-19 21:56:21 -04:00
|
|
|
http = super address, port
|
|
|
|
|
|
|
|
if proxy_class? then # from Net::HTTP::Proxy()
|
|
|
|
http.proxy_from_env = @proxy_from_env
|
|
|
|
http.proxy_address = @proxy_address
|
|
|
|
http.proxy_port = @proxy_port
|
|
|
|
http.proxy_user = @proxy_user
|
|
|
|
http.proxy_pass = @proxy_pass
|
|
|
|
elsif p_addr == :ENV then
|
|
|
|
http.proxy_from_env = true
|
|
|
|
else
|
2017-09-28 06:51:37 -04:00
|
|
|
if p_addr && p_no_proxy && !URI::Generic.use_proxy?(p_addr, p_addr, p_port, p_no_proxy)
|
|
|
|
p_addr = nil
|
|
|
|
p_port = nil
|
|
|
|
end
|
2012-07-19 21:56:21 -04:00
|
|
|
http.proxy_address = p_addr
|
2012-07-21 11:45:10 -04:00
|
|
|
http.proxy_port = p_port || default_port
|
2012-07-19 21:56:21 -04:00
|
|
|
http.proxy_user = p_user
|
|
|
|
http.proxy_pass = p_pass
|
|
|
|
end
|
|
|
|
|
|
|
|
http
|
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)
|
2012-07-11 14:37:18 -04:00
|
|
|
@local_host = nil
|
|
|
|
@local_port = nil
|
2002-02-19 07:33:52 -05:00
|
|
|
@curr_http_version = HTTPVersion
|
2012-01-20 00:56:35 -05:00
|
|
|
@keep_alive_timeout = 2
|
|
|
|
@last_communicated = nil
|
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
|
2015-11-07 11:48:49 -05:00
|
|
|
@open_timeout = 60
|
2003-05-02 10:35:01 -04:00
|
|
|
@read_timeout = 60
|
2018-06-06 04:03:47 -04:00
|
|
|
@write_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
|
2017-09-26 10:00:25 -04:00
|
|
|
@max_retries = 1
|
2003-05-02 10:35:01 -04:00
|
|
|
@debug_output = nil
|
2012-07-19 21:56:21 -04:00
|
|
|
|
|
|
|
@proxy_from_env = false
|
|
|
|
@proxy_uri = nil
|
|
|
|
@proxy_address = nil
|
|
|
|
@proxy_port = nil
|
|
|
|
@proxy_user = nil
|
|
|
|
@proxy_pass = nil
|
|
|
|
|
2004-03-05 12:00:51 -05:00
|
|
|
@use_ssl = false
|
2004-03-06 12:08:21 -05:00
|
|
|
@ssl_context = nil
|
2012-07-24 20:05:59 -04:00
|
|
|
@ssl_session = 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
|
|
|
#
|
2013-07-03 22:53:16 -04:00
|
|
|
# http = Net::HTTP.new(hostname)
|
2003-08-05 12:12:05 -04:00
|
|
|
# http.set_debug_output $stderr
|
|
|
|
# http.start { .... }
|
|
|
|
#
|
2003-12-29 06:08:47 -05:00
|
|
|
def set_debug_output(output)
|
2017-12-12 06:56:25 -05:00
|
|
|
warn 'Net::HTTP#set_debug_output called after HTTP started', uplevel: 1 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
|
|
|
|
|
2014-01-19 00:43:26 -05:00
|
|
|
# The local host used to establish the connection.
|
2012-07-11 14:37:18 -04:00
|
|
|
attr_accessor :local_host
|
|
|
|
|
2014-01-19 00:43:26 -05:00
|
|
|
# The local port used to establish the connection.
|
2012-07-11 14:37:18 -04:00
|
|
|
attr_accessor :local_port
|
|
|
|
|
2012-07-19 21:56:21 -04:00
|
|
|
attr_writer :proxy_from_env
|
|
|
|
attr_writer :proxy_address
|
|
|
|
attr_writer :proxy_port
|
|
|
|
attr_writer :proxy_user
|
|
|
|
attr_writer :proxy_pass
|
|
|
|
|
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
|
2015-12-20 20:24:12 -05:00
|
|
|
# Net::OpenTimeout exception. The default value is 60 seconds.
|
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,
|
2013-03-30 21:47:47 -04:00
|
|
|
# it raises a Net::ReadTimeout exception. The default value is 60 seconds.
|
2003-05-02 10:35:01 -04:00
|
|
|
attr_reader :read_timeout
|
|
|
|
|
2018-06-08 13:20:20 -04:00
|
|
|
# Number of seconds to wait for one block to be written (via one write(2)
|
2018-06-06 04:03:47 -04:00
|
|
|
# call). Any number may be used, including Floats for fractional
|
|
|
|
# seconds. If the HTTP object cannot write data in this many seconds,
|
|
|
|
# it raises a Net::WriteTimeout exception. The default value is 60 seconds.
|
2018-06-08 13:20:20 -04:00
|
|
|
# Net::WriteTimeout is not raised on Windows.
|
2018-06-06 04:03:47 -04:00
|
|
|
attr_reader :write_timeout
|
|
|
|
|
2017-09-26 10:00:25 -04:00
|
|
|
# Maximum number of times to retry an idempotent request in case of
|
|
|
|
# Net::ReadTimeout, IOError, EOFError, Errno::ECONNRESET,
|
2017-09-27 08:32:57 -04:00
|
|
|
# Errno::ECONNABORTED, Errno::EPIPE, OpenSSL::SSL::SSLError,
|
|
|
|
# Timeout::Error.
|
2017-11-07 15:13:48 -05:00
|
|
|
# Should be a non-negative integer number. Zero means no retries.
|
2017-09-26 10:00:25 -04:00
|
|
|
# The default value is 1.
|
|
|
|
def max_retries=(retries)
|
|
|
|
retries = retries.to_int
|
|
|
|
if retries < 0
|
|
|
|
raise ArgumentError, 'max_retries should be non-negative integer number'
|
|
|
|
end
|
|
|
|
@max_retries = retries
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :max_retries
|
|
|
|
|
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
|
|
|
|
|
2018-06-06 04:03:47 -04:00
|
|
|
# Setter for the write_timeout attribute.
|
|
|
|
def write_timeout=(sec)
|
|
|
|
@socket.write_timeout = sec if @socket
|
|
|
|
@write_timeout = sec
|
|
|
|
end
|
|
|
|
|
2013-03-30 21:47:47 -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. The
|
|
|
|
# default value is +nil+.
|
* 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
|
|
|
attr_reader :continue_timeout
|
|
|
|
|
|
|
|
# Setter for the continue_timeout attribute.
|
|
|
|
def continue_timeout=(sec)
|
|
|
|
@socket.continue_timeout = sec if @socket
|
|
|
|
@continue_timeout = sec
|
|
|
|
end
|
|
|
|
|
2012-01-20 00:56:35 -05:00
|
|
|
# Seconds to reuse the connection of the previous request.
|
|
|
|
# If the idle time is less than this Keep-Alive Timeout,
|
|
|
|
# Net::HTTP reuses the TCP/IP socket used by the previous communication.
|
|
|
|
# The default value is 2 seconds.
|
|
|
|
attr_accessor :keep_alive_timeout
|
|
|
|
|
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,
|
2017-10-21 12:25:22 -04:00
|
|
|
:@min_version,
|
|
|
|
:@max_version,
|
2011-11-26 06:06:53 -05:00
|
|
|
:@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,
|
2017-10-21 12:25:22 -04:00
|
|
|
:min_version,
|
|
|
|
:max_version,
|
2011-11-26 14:59:11 -05:00
|
|
|
: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
|
|
|
|
|
2017-10-21 12:25:22 -04:00
|
|
|
# Sets the minimum SSL version. See OpenSSL::SSL::SSLContext#min_version=
|
|
|
|
attr_accessor :min_version
|
|
|
|
|
|
|
|
# Sets the maximum SSL version. See OpenSSL::SSL::SSLContext#max_version=
|
|
|
|
attr_accessor :max_version
|
|
|
|
|
2010-11-30 03:06:19 -05:00
|
|
|
# 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
|
2012-07-19 21:56:21 -04:00
|
|
|
if proxy? then
|
|
|
|
conn_address = proxy_address
|
|
|
|
conn_port = proxy_port
|
|
|
|
else
|
|
|
|
conn_address = address
|
|
|
|
conn_port = port
|
|
|
|
end
|
|
|
|
|
2012-12-21 15:36:07 -05:00
|
|
|
D "opening connection to #{conn_address}:#{conn_port}..."
|
2018-12-30 20:09:30 -05:00
|
|
|
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) {
|
|
|
|
begin
|
|
|
|
TCPSocket.open(conn_address, conn_port, @local_host, @local_port)
|
|
|
|
rescue => e
|
|
|
|
raise e, "Failed to open TCP connection to " +
|
|
|
|
"#{conn_address}:#{conn_port} (#{e.message})"
|
|
|
|
end
|
|
|
|
}
|
2013-07-25 05:33:12 -04:00
|
|
|
s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
2004-03-08 09:16:21 -05:00
|
|
|
D "opened"
|
2004-03-05 12:00:51 -05:00
|
|
|
if use_ssl?
|
2016-11-21 00:02:58 -05:00
|
|
|
if proxy?
|
|
|
|
plain_sock = BufferedIO.new(s, read_timeout: @read_timeout,
|
2018-06-06 04:03:47 -04:00
|
|
|
write_timeout: @write_timeout,
|
2016-11-21 00:02:58 -05:00
|
|
|
continue_timeout: @continue_timeout,
|
|
|
|
debug_output: @debug_output)
|
|
|
|
buf = "CONNECT #{@address}:#{@port} HTTP/#{HTTPVersion}\r\n"
|
|
|
|
buf << "Host: #{@address}:#{@port}\r\n"
|
|
|
|
if proxy_user
|
|
|
|
credential = ["#{proxy_user}:#{proxy_pass}"].pack('m0')
|
|
|
|
buf << "Proxy-Authorization: Basic #{credential}\r\n"
|
|
|
|
end
|
|
|
|
buf << "\r\n"
|
|
|
|
plain_sock.write(buf)
|
|
|
|
HTTPResponse.read_new(plain_sock).value
|
|
|
|
# assuming nothing left in buffers after successful CONNECT response
|
|
|
|
end
|
|
|
|
|
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)
|
2018-08-08 10:13:55 -04:00
|
|
|
@ssl_context.session_cache_mode =
|
|
|
|
OpenSSL::SSL::SSLContext::SESSION_CACHE_CLIENT |
|
|
|
|
OpenSSL::SSL::SSLContext::SESSION_CACHE_NO_INTERNAL_STORE
|
|
|
|
@ssl_context.session_new_cb = proc {|sock, sess| @ssl_session = sess }
|
2012-12-21 15:36:07 -05:00
|
|
|
D "starting SSL for #{conn_address}:#{conn_port}..."
|
2004-03-06 12:08:21 -05:00
|
|
|
s = OpenSSL::SSL::SSLSocket.new(s, @ssl_context)
|
|
|
|
s.sync_close = true
|
2016-11-21 00:02:58 -05:00
|
|
|
# Server Name Indication (SNI) RFC 3546
|
|
|
|
s.hostname = @address if s.respond_to? :hostname=
|
|
|
|
if @ssl_session and
|
|
|
|
Process.clock_gettime(Process::CLOCK_REALTIME) < @ssl_session.time.to_f + @ssl_session.timeout
|
2018-08-08 10:13:55 -04:00
|
|
|
s.session = @ssl_session
|
2016-11-21 00:02:58 -05:00
|
|
|
end
|
|
|
|
ssl_socket_connect(s, @open_timeout)
|
|
|
|
if @ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE
|
|
|
|
s.post_connection_check(@address)
|
|
|
|
end
|
2018-10-20 07:29:38 -04:00
|
|
|
D "SSL established, protocol: #{s.ssl_version}, cipher: #{s.cipher[0]}"
|
2004-03-06 12:08:21 -05:00
|
|
|
end
|
2016-11-14 02:53:32 -05:00
|
|
|
@socket = BufferedIO.new(s, read_timeout: @read_timeout,
|
2018-06-06 04:03:47 -04:00
|
|
|
write_timeout: @write_timeout,
|
2016-11-14 02:53:32 -05:00
|
|
|
continue_timeout: @continue_timeout,
|
|
|
|
debug_output: @debug_output)
|
2003-05-02 10:35:01 -04:00
|
|
|
on_connect
|
2016-11-21 00:02:58 -05:00
|
|
|
rescue => exception
|
|
|
|
if s
|
|
|
|
D "Conn close because of connect error #{exception}"
|
|
|
|
s.close
|
|
|
|
end
|
|
|
|
raise
|
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
|
2016-11-21 18:05:41 -05:00
|
|
|
@socket.close if @socket
|
2003-05-02 10:35:01 -04:00
|
|
|
@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
|
2012-07-19 21:56:21 -04:00
|
|
|
@proxy_from_env = false
|
2002-02-19 07:33:52 -05:00
|
|
|
@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
|
|
|
#
|
2012-07-19 21:56:21 -04:00
|
|
|
# This class is obsolete. You may pass these same parameters directly to
|
|
|
|
# Net::HTTP.new. See Net::HTTP.new for details of the arguments.
|
|
|
|
def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil)
|
2002-11-21 06:13:17 -05:00
|
|
|
return self unless p_addr
|
2012-07-19 21:56:21 -04:00
|
|
|
|
|
|
|
Class.new(self) {
|
2003-08-01 11:41:41 -04:00
|
|
|
@is_proxy_class = true
|
2012-07-19 21:56:21 -04:00
|
|
|
|
|
|
|
if p_addr == :ENV then
|
|
|
|
@proxy_from_env = true
|
|
|
|
@proxy_address = nil
|
|
|
|
@proxy_port = nil
|
|
|
|
else
|
2012-07-21 11:51:14 -04:00
|
|
|
@proxy_from_env = false
|
2012-07-19 21:56:21 -04:00
|
|
|
@proxy_address = p_addr
|
2012-07-21 11:45:10 -04:00
|
|
|
@proxy_port = p_port || default_port
|
2012-07-19 21:56:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@proxy_user = p_user
|
|
|
|
@proxy_pass = p_pass
|
2002-02-19 07:33:52 -05:00
|
|
|
}
|
|
|
|
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?
|
2012-12-03 20:01:37 -05:00
|
|
|
defined?(@is_proxy_class) ? @is_proxy_class : false
|
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
|
|
|
|
|
2012-07-19 21:56:21 -04:00
|
|
|
# True if requests for this connection will be proxied
|
2000-11-10 18:31:32 -05:00
|
|
|
def proxy?
|
2016-06-28 02:19:58 -04:00
|
|
|
!!(@proxy_from_env ? proxy_uri : @proxy_address)
|
2012-07-19 21:56:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# True if the proxy for this connection is determined from the environment
|
|
|
|
def proxy_from_env?
|
|
|
|
@proxy_from_env
|
2000-11-10 18:31:32 -05:00
|
|
|
end
|
|
|
|
|
2012-07-19 21:56:21 -04:00
|
|
|
# The proxy URI determined from the environment for this connection.
|
|
|
|
def proxy_uri # :nodoc:
|
2016-06-28 02:20:20 -04:00
|
|
|
return if @proxy_uri == false
|
2014-08-05 15:37:37 -04:00
|
|
|
@proxy_uri ||= URI::HTTP.new(
|
|
|
|
"http".freeze, nil, address, port, nil, nil, nil, nil, nil
|
2016-06-28 02:20:20 -04:00
|
|
|
).find_proxy || false
|
|
|
|
@proxy_uri || nil
|
2012-07-19 21:56:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# The address of the proxy server, if one is configured.
|
2000-11-10 18:31:32 -05:00
|
|
|
def proxy_address
|
2012-07-19 21:56:21 -04:00
|
|
|
if @proxy_from_env then
|
2015-12-14 03:05:35 -05:00
|
|
|
proxy_uri&.hostname
|
2012-07-19 21:56:21 -04:00
|
|
|
else
|
|
|
|
@proxy_address
|
|
|
|
end
|
2000-11-10 18:31:32 -05:00
|
|
|
end
|
|
|
|
|
2012-07-19 21:56:21 -04:00
|
|
|
# The port of the proxy server, if one is configured.
|
2000-11-10 18:31:32 -05:00
|
|
|
def proxy_port
|
2012-07-19 21:56:21 -04:00
|
|
|
if @proxy_from_env then
|
2015-12-14 03:05:35 -05:00
|
|
|
proxy_uri&.port
|
2012-07-19 21:56:21 -04:00
|
|
|
else
|
|
|
|
@proxy_port
|
|
|
|
end
|
2000-11-10 18:31:32 -05:00
|
|
|
end
|
|
|
|
|
2017-04-23 12:19:23 -04:00
|
|
|
# [Bug #12921]
|
|
|
|
if /linux|freebsd|darwin/ =~ RUBY_PLATFORM
|
|
|
|
ENVIRONMENT_VARIABLE_IS_MULTIUSER_SAFE = true
|
|
|
|
else
|
|
|
|
ENVIRONMENT_VARIABLE_IS_MULTIUSER_SAFE = false
|
|
|
|
end
|
|
|
|
|
|
|
|
# The username of the proxy server, if one is configured.
|
2002-11-21 06:13:17 -05:00
|
|
|
def proxy_user
|
2017-04-23 12:19:23 -04:00
|
|
|
if ENVIRONMENT_VARIABLE_IS_MULTIUSER_SAFE && @proxy_from_env
|
|
|
|
proxy_uri&.user
|
|
|
|
else
|
|
|
|
@proxy_user
|
|
|
|
end
|
2002-11-21 06:13:17 -05:00
|
|
|
end
|
|
|
|
|
2017-04-23 12:19:23 -04:00
|
|
|
# The password of the proxy server, if one is configured.
|
2002-11-21 06:13:17 -05:00
|
|
|
def proxy_pass
|
2017-04-23 12:19:23 -04:00
|
|
|
if ENVIRONMENT_VARIABLE_IS_MULTIUSER_SAFE && @proxy_from_env
|
|
|
|
proxy_uri&.password
|
|
|
|
else
|
|
|
|
@proxy_pass
|
|
|
|
end
|
2002-11-21 06:13:17 -05:00
|
|
|
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
|
|
|
|
|
2012-07-19 21:56:21 -04:00
|
|
|
# without proxy, obsolete
|
2000-06-27 09:36:17 -04:00
|
|
|
|
2012-07-19 21:56:21 -04:00
|
|
|
def conn_address # :nodoc:
|
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
|
|
|
|
2012-07-19 21:56:21 -04:00
|
|
|
def conn_port # :nodoc:
|
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)
|
2015-04-03 19:18:20 -04:00
|
|
|
if proxy?
|
|
|
|
if path.start_with?("ftp://") || use_ssl?
|
|
|
|
path
|
|
|
|
else
|
|
|
|
"http://#{addr_port}#{path}"
|
|
|
|
end
|
2012-07-19 21:56:21 -04:00
|
|
|
else
|
|
|
|
path
|
|
|
|
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
|
|
|
|
|
2012-12-21 15:36:07 -05:00
|
|
|
# Retrieves data from +path+ on the connected-to host which may be an
|
|
|
|
# absolute path String or a URI to extract the path from.
|
|
|
|
#
|
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
|
|
|
#
|
2014-07-03 13:59:49 -04:00
|
|
|
def get(path, initheader = nil, dest = nil, &block) # :yield: +body_segment+
|
2001-12-30 14:18:45 -05:00
|
|
|
res = nil
|
2002-11-21 06:13:17 -05:00
|
|
|
request(Get.new(path, initheader)) {|r|
|
2012-07-19 18:43:38 -04:00
|
|
|
r.read_body dest, &block
|
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)
|
2015-01-02 07:07:04 -05:00
|
|
|
has_response_body = name != 'HEAD'
|
|
|
|
r = HTTPGenericRequest.new(name,(data ? true : false),has_response_body,path,header)
|
2003-08-01 10:01:14 -04:00
|
|
|
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
|
|
|
|
|
2012-01-20 00:56:35 -05:00
|
|
|
IDEMPOTENT_METHODS_ = %w/GET HEAD PUT DELETE OPTIONS TRACE/ # :nodoc:
|
|
|
|
|
2007-03-16 09:17:10 -04:00
|
|
|
def transport_request(req)
|
2012-01-20 00:56:35 -05:00
|
|
|
count = 0
|
2012-02-23 20:10:07 -05:00
|
|
|
begin
|
|
|
|
begin_transport req
|
|
|
|
res = catch(:response) {
|
|
|
|
req.exec @socket, @curr_http_version, edit_path(req.path)
|
|
|
|
begin
|
|
|
|
res = HTTPResponse.read_new(@socket)
|
2013-02-13 20:08:19 -05:00
|
|
|
res.decode_content = req.decode_content
|
2016-11-05 10:08:16 -04:00
|
|
|
end while res.kind_of?(HTTPInformation)
|
2012-12-21 15:36:07 -05:00
|
|
|
|
|
|
|
res.uri = req.uri
|
|
|
|
|
2012-02-23 20:10:07 -05:00
|
|
|
res
|
* 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
|
|
|
}
|
2015-11-20 00:57:10 -05:00
|
|
|
res.reading_body(@socket, req.response_body_permitted?) {
|
|
|
|
yield res if block_given?
|
|
|
|
}
|
2012-04-23 09:48:31 -04:00
|
|
|
rescue Net::OpenTimeout
|
|
|
|
raise
|
|
|
|
rescue Net::ReadTimeout, IOError, EOFError,
|
2019-01-21 01:23:55 -05:00
|
|
|
Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPIPE, Errno::ETIMEDOUT,
|
2013-02-14 00:32:58 -05:00
|
|
|
# avoid a dependency on OpenSSL
|
|
|
|
defined?(OpenSSL::SSL) ? OpenSSL::SSL::SSLError : IOError,
|
|
|
|
Timeout::Error => exception
|
2017-09-26 10:00:25 -04:00
|
|
|
if count < max_retries && IDEMPOTENT_METHODS_.include?(req.method)
|
2012-02-23 20:10:07 -05:00
|
|
|
count += 1
|
2016-11-21 18:05:41 -05:00
|
|
|
@socket.close if @socket
|
2012-02-23 20:10:07 -05:00
|
|
|
D "Conn close because of error #{exception}, and retry"
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
D "Conn close because of error #{exception}"
|
2016-11-21 18:05:41 -05:00
|
|
|
@socket.close if @socket
|
2012-02-23 20:10:07 -05:00
|
|
|
raise
|
2012-01-20 00:56:35 -05:00
|
|
|
end
|
2012-02-27 23:51:37 -05:00
|
|
|
|
|
|
|
end_transport req, res
|
|
|
|
res
|
|
|
|
rescue => exception
|
|
|
|
D "Conn close because of error #{exception}"
|
2016-11-21 18:05:41 -05:00
|
|
|
@socket.close if @socket
|
2012-02-27 23:51:37 -05:00
|
|
|
raise exception
|
2007-03-16 09:17:10 -04:00
|
|
|
end
|
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def begin_transport(req)
|
2012-01-20 00:56:35 -05:00
|
|
|
if @socket.closed?
|
|
|
|
connect
|
2015-11-27 09:07:35 -05:00
|
|
|
elsif @last_communicated
|
|
|
|
if @last_communicated + @keep_alive_timeout < Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
|
|
D 'Conn close because of keep_alive_timeout'
|
|
|
|
@socket.close
|
|
|
|
connect
|
2015-11-27 10:03:24 -05:00
|
|
|
elsif @socket.io.to_io.wait_readable(0) && @socket.eof?
|
2015-11-27 09:07:35 -05:00
|
|
|
D "Conn close because of EOF"
|
|
|
|
@socket.close
|
|
|
|
connect
|
|
|
|
end
|
2012-01-20 00:56:35 -05:00
|
|
|
end
|
|
|
|
|
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
|
2012-12-21 15:36:07 -05:00
|
|
|
|
2014-08-05 15:10:05 -04:00
|
|
|
req.update_uri address, port, use_ssl?
|
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
|
2012-01-20 00:56:35 -05:00
|
|
|
@last_communicated = nil
|
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'
|
2015-05-28 21:40:26 -04:00
|
|
|
@last_communicated = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
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
|
2017-10-26 11:29:36 -04:00
|
|
|
addr = address
|
|
|
|
addr = "[#{addr}]" if addr.include?(":")
|
|
|
|
default_port = use_ssl? ? HTTP.https_default_port : HTTP.http_default_port
|
|
|
|
default_port == port ? addr : "#{addr}:#{port}"
|
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-07-03 14:13:13 -04:00
|
|
|
end
|
|
|
|
|
2012-05-22 16:36:21 -04:00
|
|
|
end
|
2001-07-03 14:13:13 -04:00
|
|
|
|
2017-10-23 21:22:08 -04:00
|
|
|
require_relative 'http/exceptions'
|
2001-02-06 06:14:51 -05:00
|
|
|
|
2017-10-23 21:22:08 -04:00
|
|
|
require_relative 'http/header'
|
2004-03-05 09:13:31 -05:00
|
|
|
|
2017-10-23 21:22:08 -04:00
|
|
|
require_relative 'http/generic_request'
|
|
|
|
require_relative 'http/request'
|
|
|
|
require_relative 'http/requests'
|
2005-09-13 02:41:59 -04:00
|
|
|
|
2017-10-23 21:22:08 -04:00
|
|
|
require_relative 'http/response'
|
|
|
|
require_relative 'http/responses'
|
2004-03-05 09:13:31 -05:00
|
|
|
|
2017-10-23 21:22:08 -04:00
|
|
|
require_relative 'http/proxy_delta'
|
2003-07-01 22:05:35 -04:00
|
|
|
|
2017-10-23 21:22:08 -04:00
|
|
|
require_relative 'http/backward'
|