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
|
|
|
#
|
2005-09-13 02:41:59 -04:00
|
|
|
# Copyright (c) 1999-2005 Yukihiro Matsumoto
|
|
|
|
# Copyright (c) 1999-2005 Minero Aoki
|
|
|
|
# Copyright (c) 2001 GOTOU Yuuzou
|
2003-08-01 10:01:14 -04: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.
|
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.
|
|
|
|
#
|
2005-09-13 02:41:59 -04:00
|
|
|
# See Net::HTTP for an overview and examples.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
|
|
|
# NOTE: You can find Japanese version of this document here:
|
2003-12-03 20:27:39 -05:00
|
|
|
# http://www.ruby-lang.org/ja/man/?cmd=view;name=net%2Fhttp.rb
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2003-08-31 09:04:25 -04:00
|
|
|
#--
|
2003-08-01 10:01:14 -04:00
|
|
|
# $Id$
|
|
|
|
#++
|
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'
|
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
|
|
|
|
2003-08-31 09:04:25 -04:00
|
|
|
# == What Is This Library?
|
|
|
|
#
|
|
|
|
# This library provides your program functions to access WWW
|
|
|
|
# documents via HTTP, Hyper Text Transfer Protocol version 1.1.
|
|
|
|
# For details of HTTP, refer [RFC2616]
|
2004-02-01 04:27:17 -05:00
|
|
|
# (http://www.ietf.org/rfc/rfc2616.txt).
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
|
|
|
# == Examples
|
|
|
|
#
|
|
|
|
# === Getting Document From WWW Server
|
|
|
|
#
|
2005-04-20 12:53:27 -04:00
|
|
|
# Example #1: Simple GET+print
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
|
|
|
# require 'net/http'
|
2005-04-20 12:53:27 -04:00
|
|
|
# Net::HTTP.get_print 'www.example.com', '/index.html'
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
2005-04-20 12:53:27 -04:00
|
|
|
# Example #2: Simple GET+print by URL
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
|
|
|
# require 'net/http'
|
2005-04-20 12:53:27 -04:00
|
|
|
# require 'uri'
|
|
|
|
# Net::HTTP.get_print URI.parse('http://www.example.com/index.html')
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
2005-04-20 12:53:27 -04:00
|
|
|
# Example #3: More generic GET+print
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
# require 'uri'
|
2005-04-20 12:53:27 -04:00
|
|
|
#
|
|
|
|
# url = URI.parse('http://www.example.com/index.html')
|
|
|
|
# res = Net::HTTP.start(url.host, url.port) {|http|
|
|
|
|
# http.get('/index.html')
|
|
|
|
# }
|
|
|
|
# puts res.body
|
|
|
|
#
|
|
|
|
# Example #4: More generic GET+print
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
#
|
|
|
|
# url = URI.parse('http://www.example.com/index.html')
|
|
|
|
# req = Net::HTTP::Get.new(url.path)
|
|
|
|
# res = Net::HTTP.start(url.host, url.port) {|http|
|
|
|
|
# http.request(req)
|
|
|
|
# }
|
|
|
|
# puts res.body
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
|
|
|
# === Posting Form Data
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
2005-04-20 12:53:27 -04:00
|
|
|
# require 'uri'
|
|
|
|
#
|
|
|
|
# #1: Simple POST
|
|
|
|
# res = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
|
|
|
|
# {'q'=>'ruby', 'max'=>'50'})
|
|
|
|
# puts res.body
|
|
|
|
#
|
|
|
|
# #2: POST with basic authentication
|
|
|
|
# res = Net::HTTP.post_form(URI.parse('http://jack:pass@www.example.com/todo.cgi'),
|
|
|
|
# {'from'=>'2005-01-01', 'to'=>'2005-03-31'})
|
|
|
|
# puts res.body
|
|
|
|
#
|
|
|
|
# #3: Detailed control
|
|
|
|
# url = URI.parse('http://www.example.com/todo.cgi')
|
|
|
|
# req = Net::HTTP::Post.new(url.path)
|
|
|
|
# req.basic_auth 'jack', 'pass'
|
|
|
|
# req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
|
|
|
|
# res = Net::HTTP.new(url.host, url.port).start { http.request(req) }
|
|
|
|
# case res
|
|
|
|
# when Net::HTTPSuccess, Net::HTTPRedirection
|
|
|
|
# # OK
|
|
|
|
# else
|
|
|
|
# res.error!
|
|
|
|
# end
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
|
|
|
# === Accessing via Proxy
|
|
|
|
#
|
|
|
|
# Net::HTTP.Proxy creates http proxy class. It has same
|
|
|
|
# methods of Net::HTTP but its instances always connect to
|
|
|
|
# proxy, instead of given host.
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
#
|
|
|
|
# proxy_addr = 'your.proxy.host'
|
|
|
|
# proxy_port = 8080
|
|
|
|
# :
|
|
|
|
# Net::HTTP::Proxy(proxy_addr, proxy_port).start('www.example.com') {|http|
|
2004-02-08 00:24:47 -05:00
|
|
|
# # always connect to your.proxy.addr:8080
|
2003-08-31 09:04:25 -04:00
|
|
|
# :
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# Since Net::HTTP.Proxy returns Net::HTTP itself when proxy_addr is nil,
|
|
|
|
# there's no need to change code if there's proxy or not.
|
|
|
|
#
|
|
|
|
# There are two additional parameters in Net::HTTP.Proxy which allow to
|
|
|
|
# specify proxy user name and password:
|
|
|
|
#
|
|
|
|
# Net::HTTP::Proxy(proxy_addr, proxy_port, proxy_user = nil, proxy_pass = nil)
|
|
|
|
#
|
|
|
|
# You may use them to work with authorization-enabled proxies:
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
# require 'uri'
|
|
|
|
#
|
|
|
|
# proxy_host = 'your.proxy.host'
|
|
|
|
# proxy_port = 8080
|
|
|
|
# uri = URI.parse(ENV['http_proxy'])
|
|
|
|
# proxy_user, proxy_pass = uri.userinfo.split(/:/) if uri.userinfo
|
|
|
|
# Net::HTTP::Proxy(proxy_host, proxy_port,
|
|
|
|
# proxy_user, proxy_pass).start('www.example.com') {|http|
|
|
|
|
# # always connect to your.proxy.addr:8080 using specified username and password
|
|
|
|
# :
|
|
|
|
# }
|
2003-12-02 09:18:49 -05:00
|
|
|
#
|
|
|
|
# Note that net/http never rely on HTTP_PROXY environment variable.
|
|
|
|
# If you want to use proxy, set it explicitly.
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
|
|
|
# === Following Redirection
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
# require 'uri'
|
|
|
|
#
|
|
|
|
# def fetch( uri_str, limit = 10 )
|
|
|
|
# # You should choose better exception.
|
2004-03-29 02:54:38 -05:00
|
|
|
# raise ArgumentError, 'HTTP redirect too deep' if limit == 0
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
|
|
|
# response = Net::HTTP.get_response(URI.parse(uri_str))
|
|
|
|
# case response
|
|
|
|
# when Net::HTTPSuccess then response
|
|
|
|
# when Net::HTTPRedirection then fetch(response['location'], limit - 1)
|
|
|
|
# else
|
|
|
|
# response.error!
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# print fetch('http://www.ruby-lang.org')
|
|
|
|
#
|
|
|
|
# Net::HTTPSuccess and Net::HTTPRedirection is a HTTPResponse class.
|
|
|
|
# All HTTPResponse objects belong to its own response class which
|
|
|
|
# indicate HTTP result status. For details of response classes,
|
|
|
|
# see section "HTTP Response Classes".
|
|
|
|
#
|
|
|
|
# === Basic Authentication
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
#
|
|
|
|
# Net::HTTP.start('www.example.com') {|http|
|
|
|
|
# req = Net::HTTP::Get.new('/secret-page.html')
|
|
|
|
# req.basic_auth 'account', 'password'
|
|
|
|
# response = http.request(req)
|
|
|
|
# print response.body
|
|
|
|
# }
|
|
|
|
#
|
2005-09-13 02:41:59 -04:00
|
|
|
# === HTTP Request Classes
|
|
|
|
#
|
|
|
|
# Here is 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
|
|
|
|
#
|
2003-08-31 09:04:25 -04:00
|
|
|
# === HTTP Response Classes
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
2005-09-13 02:41:59 -04:00
|
|
|
# Here is HTTP response class hierarchy.
|
|
|
|
# All classes are defined in Net module.
|
|
|
|
#
|
|
|
|
# HTTPResponse
|
|
|
|
# HTTPUnknownResponse
|
|
|
|
# HTTPInformation # 1xx
|
|
|
|
# HTTPContinue # 100
|
|
|
|
# HTTPSwitchProtocl # 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
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
|
|
|
# == Switching Net::HTTP versions
|
|
|
|
#
|
|
|
|
# You can use net/http.rb 1.1 features (bundled with Ruby 1.6)
|
|
|
|
# by calling HTTP.version_1_1. Calling Net::HTTP.version_1_2
|
|
|
|
# allows you to use 1.2 features again.
|
|
|
|
#
|
|
|
|
# # example
|
2004-02-08 00:24:47 -05:00
|
|
|
# Net::HTTP.start {|http1| ...(http1 has 1.2 features)... }
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
|
|
|
# Net::HTTP.version_1_1
|
2004-02-08 00:24:47 -05:00
|
|
|
# Net::HTTP.start {|http2| ...(http2 has 1.1 features)... }
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
|
|
|
# Net::HTTP.version_1_2
|
2004-02-08 00:24:47 -05:00
|
|
|
# Net::HTTP.start {|http3| ...(http3 has 1.2 features)... }
|
2003-08-31 09:04:25 -04:00
|
|
|
#
|
|
|
|
# This function is NOT thread-safe.
|
2003-08-01 10:01:14 -04: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'
|
2005-09-13 02:41:59 -04:00
|
|
|
@newimpl = true
|
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.
|
|
|
|
# Defaults to ON in ruby 1.8.
|
|
|
|
#
|
|
|
|
# I strongly recommend to call this method always.
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
# Net::HTTP.version_1_2
|
|
|
|
#
|
2002-02-19 07:33:52 -05:00
|
|
|
def HTTP.version_1_2
|
2004-05-18 13:23:22 -04:00
|
|
|
@newimpl = true
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
2002-02-07 15:22:39 -05:00
|
|
|
|
2003-08-31 09:04:25 -04:00
|
|
|
# Turns on net/http 1.1 (ruby 1.6) features.
|
2003-08-01 11:41:41 -04:00
|
|
|
# Defaults to OFF in ruby 1.8.
|
2002-02-19 07:33:52 -05:00
|
|
|
def HTTP.version_1_1
|
2004-05-18 13:23:22 -04:00
|
|
|
@newimpl = false
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
2001-02-06 06:14:51 -05:00
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
# true if net/http is in version 1.2 mode.
|
|
|
|
# Defaults to true.
|
|
|
|
def HTTP.version_1_2?
|
2004-05-18 13:23:22 -04:00
|
|
|
@newimpl
|
2001-02-06 06:14:51 -05:00
|
|
|
end
|
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
# true if net/http is in version 1.1 compatible mode.
|
|
|
|
# Defaults to true.
|
|
|
|
def HTTP.version_1_1?
|
2004-05-18 13:23:22 -04:00
|
|
|
not @newimpl
|
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
|
|
|
#
|
|
|
|
# Get body from target and output it to +$stdout+. The
|
|
|
|
# target can either be specified as (+uri+), or as
|
|
|
|
# (+host+, +path+, +port+ = 80); so:
|
|
|
|
#
|
|
|
|
# Net::HTTP.get_print URI.parse('http://www.example.com/index.html')
|
|
|
|
#
|
|
|
|
# or:
|
|
|
|
#
|
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
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Send a GET request to the target and return the response
|
|
|
|
# as a string. The target can either be specified as
|
|
|
|
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
|
|
|
|
#
|
|
|
|
# print Net::HTTP.get(URI.parse('http://www.example.com/index.html'))
|
|
|
|
#
|
|
|
|
# or:
|
2003-03-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
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Send a GET request to the target and return the response
|
|
|
|
# as a Net::HTTPResponse object. The target can either be specified as
|
|
|
|
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
|
|
|
|
#
|
|
|
|
# res = Net::HTTP.get_response(URI.parse('http://www.example.com/index.html'))
|
|
|
|
# print res.body
|
|
|
|
#
|
|
|
|
# or:
|
|
|
|
#
|
|
|
|
# res = Net::HTTP.get_response('www.example.com', '/index.html')
|
|
|
|
# print res.body
|
|
|
|
#
|
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
|
|
|
|
new(uri.host, uri.port).start {|http|
|
|
|
|
return http.request_get(uri.request_uri, &block)
|
|
|
|
}
|
2002-03-26 06:18:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-04-20 09:55:26 -04:00
|
|
|
# Posts HTML form data to the +URL+.
|
|
|
|
# Form data must be represented as a Hash of String to String, e.g:
|
|
|
|
#
|
|
|
|
# { "cmd" => "search", "q" => "ruby", "max" => "50" }
|
|
|
|
#
|
|
|
|
# This method also does Basic Authentication iff +URL+.user exists.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# require 'net/http'
|
|
|
|
# require 'uri'
|
|
|
|
#
|
|
|
|
# HTTP.post_form URI.parse('http://www.example.com/search.cgi'),
|
|
|
|
# { "q" => "ruby", "max" => "50" }
|
|
|
|
#
|
|
|
|
def HTTP.post_form(url, params)
|
|
|
|
req = Post.new(url.path)
|
|
|
|
req.form_data = params
|
|
|
|
req.basic_auth url.user, url.password if url.user
|
|
|
|
new(url.host, url.port).start {|http|
|
|
|
|
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
|
|
|
|
2004-04-21 04:26:33 -04:00
|
|
|
# creates a new Net::HTTP object and opens its TCP connection and
|
|
|
|
# HTTP session. If the optional block is given, the newly
|
|
|
|
# created Net::HTTP object is passed to it and closed when the
|
|
|
|
# block finishes. In this case, the return value of this method
|
|
|
|
# is the return value of the block. If no block is given, the
|
|
|
|
# return value of this method is the newly created Net::HTTP object
|
|
|
|
# itself, and the caller is responsible for closing it upon completion.
|
|
|
|
def HTTP.start(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil, &block) # :yield: +http+
|
|
|
|
new(address, port, p_addr, p_port, p_user, p_pass).start(&block)
|
|
|
|
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
|
|
|
|
2004-04-21 04:26:33 -04:00
|
|
|
# Creates a new Net::HTTP object.
|
|
|
|
# If +proxy_addr+ is given, creates an Net::HTTP object with proxy support.
|
|
|
|
# This method does not open the TCP connection.
|
|
|
|
def HTTP.new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil)
|
|
|
|
h = Proxy(p_addr, p_port, p_user, p_pass).newobj(address, port)
|
|
|
|
h.instance_eval {
|
|
|
|
@newimpl = ::Net::HTTP.version_1_2?
|
|
|
|
}
|
|
|
|
h
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Creates a new Net::HTTP object for the specified +address+.
|
|
|
|
# This method does not open the TCP connection.
|
2003-12-29 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
|
|
|
|
@seems_1_0_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
|
|
|
|
@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
|
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
|
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
# *WARNING* This method causes 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
|
|
|
#
|
|
|
|
# Set an output stream for debugging.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# The host name 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
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Seconds to wait until connection is opened.
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-18 19:19:47 -04:00
|
|
|
# If the HTTP object cannot open a connection 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_accessor :open_timeout
|
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
# Seconds to wait until reading one block (by one read(2) call).
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-18 19:19:47 -04:00
|
|
|
# If the HTTP object cannot open a connection 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
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# returns true if the HTTP session is 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
|
|
|
|
|
2004-03-05 12:00:51 -05:00
|
|
|
# returns true if use SSL/TLS with HTTP.
|
|
|
|
def use_ssl?
|
2004-03-06 12:08:21 -05:00
|
|
|
false # redefined in net/https
|
2004-03-05 12:00:51 -05:00
|
|
|
end
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Opens TCP connection and HTTP session.
|
|
|
|
#
|
|
|
|
# When this method is called with block, gives a HTTP object
|
|
|
|
# to the block and closes the TCP connection / HTTP session
|
|
|
|
# after the block executed.
|
|
|
|
#
|
|
|
|
# When called with a block, returns the return value of the
|
|
|
|
# block; otherwise, returns self.
|
|
|
|
#
|
2003-08-21 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?
|
2004-03-06 12:08:21 -05:00
|
|
|
unless @ssl_context.verify_mode
|
|
|
|
warn "warning: peer certificate won't be verified in this SSL session"
|
|
|
|
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
2004-03-05 12:00:51 -05:00
|
|
|
end
|
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
|
|
|
|
@socket.debug_output = @debug_output
|
2004-04-21 04:26:33 -04:00
|
|
|
if use_ssl?
|
|
|
|
if proxy?
|
|
|
|
@socket.writeline sprintf('CONNECT %s:%s HTTP/%s',
|
|
|
|
@address, @port, HTTPVersion)
|
2005-06-30 01:58:42 -04:00
|
|
|
@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
|
2004-04-21 04:26:33 -04:00
|
|
|
@socket.writeline ''
|
|
|
|
HTTPResponse.read_new(@socket).value
|
|
|
|
end
|
|
|
|
s.connect
|
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
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Finishes HTTP session and closes TCP connection.
|
2003-09-29 14:12:20 -04:00
|
|
|
# Raises IOError if not 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
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Creates an HTTP proxy class.
|
|
|
|
# Arguments are address/port of proxy host and username/password
|
|
|
|
# if authorization on proxy server is required.
|
|
|
|
# You can replace the HTTP class with created proxy class.
|
|
|
|
#
|
|
|
|
# If ADDRESS is nil, this method returns self (Net::HTTP).
|
|
|
|
#
|
|
|
|
# # Example
|
|
|
|
# proxy_class = Net::HTTP::Proxy('proxy.example.com', 8080)
|
|
|
|
# :
|
|
|
|
# proxy_class.start('www.ruby-lang.org') {|http|
|
2003-08-01 11:41:41 -04:00
|
|
|
# # connecting proxy.foo.org:8080
|
2003-08-01 10:01:14 -04: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
|
|
|
|
|
2001-06-26 19:49:21 -04:00
|
|
|
attr_reader :proxy_address
|
|
|
|
attr_reader :proxy_port
|
2002-11-21 06:13:17 -05:00
|
|
|
attr_reader :proxy_user
|
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
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Address of proxy host. If self does not use a proxy, nil.
|
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
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Port number of proxy host. If self does not use a proxy, nil.
|
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
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# User name for accessing proxy. If self does not use a proxy, nil.
|
2002-11-21 06:13:17 -05:00
|
|
|
def proxy_user
|
|
|
|
self.class.proxy_user
|
|
|
|
end
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# User password for accessing proxy. If self does not use a proxy, nil.
|
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.
|
|
|
|
# +header+ must be a Hash like { 'Accept' => '*/*', ... }.
|
|
|
|
#
|
2003-08-01 11:41:41 -04:00
|
|
|
# In version 1.1 (ruby 1.6), this method returns a pair of objects,
|
2003-08-01 10:01:14 -04:00
|
|
|
# a Net::HTTPResponse object and the entity body string.
|
2003-08-01 11:41:41 -04:00
|
|
|
# In version 1.2 (ruby 1.8), this method returns a Net::HTTPResponse
|
2003-08-01 10:01:14 -04:00
|
|
|
# object.
|
|
|
|
#
|
|
|
|
# If called with a block, yields each fragment of the
|
|
|
|
# entity body in turn as a string as it is read from
|
|
|
|
# the socket. Note that in this case, the returned response
|
|
|
|
# object will *not* contain a (meaningful) body.
|
|
|
|
#
|
2003-08-01 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
|
|
|
#
|
|
|
|
# In version 1.1, this method might raise an exception for
|
|
|
|
# 3xx (redirect). In this case you can get a HTTPResponse object
|
|
|
|
# by "anException.response".
|
|
|
|
#
|
|
|
|
# In version 1.2, this method never raises exception.
|
|
|
|
#
|
|
|
|
# # version 1.1 (bundled with Ruby 1.6)
|
|
|
|
# response, body = http.get('/index.html')
|
|
|
|
#
|
2003-08-01 11:41:41 -04:00
|
|
|
# # version 1.2 (bundled with Ruby 1.8 or later)
|
2003-08-01 10:01:14 -04:00
|
|
|
# response = http.get('/index.html')
|
|
|
|
#
|
|
|
|
# # 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
|
|
|
#
|
2003-12-29 06:08:47 -05: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|
|
2003-08-01 11:41:41 -04:00
|
|
|
r.read_body dest, &block
|
|
|
|
res = r
|
2001-12-30 14:18:45 -05:00
|
|
|
}
|
2002-11-21 06:13:17 -05:00
|
|
|
unless @newimpl
|
2001-12-30 14:18:45 -05:00
|
|
|
res.value
|
|
|
|
return res, res.body
|
|
|
|
end
|
2000-03-26 03:48:15 -05:00
|
|
|
|
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' => '*/*', ... }.
|
|
|
|
#
|
|
|
|
# This method returns a Net::HTTPResponse object.
|
|
|
|
#
|
|
|
|
# In version 1.1, this method might raise an exception for
|
|
|
|
# 3xx (redirect). On the case you can get a HTTPResponse object
|
|
|
|
# by "anException.response".
|
|
|
|
# In version 1.2, this method never raises an exception.
|
|
|
|
#
|
|
|
|
# response = nil
|
|
|
|
# Net::HTTP.start('some.www.server', 80) {|http|
|
2003-08-01 11:41:41 -04:00
|
|
|
# response = http.head('/index.html')
|
2003-08-01 10:01:14 -04:00
|
|
|
# }
|
|
|
|
# p response['content-type']
|
|
|
|
#
|
2003-12-29 06:08:47 -05:00
|
|
|
def head(path, initheader = nil)
|
2002-11-21 06:13:17 -05:00
|
|
|
res = request(Head.new(path, initheader))
|
2003-08-01 11:41:41 -04:00
|
|
|
res.value unless @newimpl
|
2001-12-30 14:18:45 -05:00
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Posts +data+ (must be a String) to +path+. +header+ must be a Hash
|
|
|
|
# like { 'Accept' => '*/*', ... }.
|
|
|
|
#
|
2003-08-01 11:41:41 -04:00
|
|
|
# In version 1.1 (ruby 1.6), this method returns a pair of objects, a
|
2003-08-01 10:01:14 -04:00
|
|
|
# Net::HTTPResponse object and an entity body string.
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-18 19:19:47 -04:00
|
|
|
# In version 1.2 (ruby 1.8), 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 are read from
|
|
|
|
# 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.
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
|
|
|
# In version 1.1, this method might raise an exception for
|
|
|
|
# 3xx (redirect). In this case you can get an HTTPResponse object
|
|
|
|
# by "anException.response".
|
|
|
|
# In version 1.2, this method never raises exception.
|
|
|
|
#
|
|
|
|
# # version 1.1
|
|
|
|
# response, body = http.post('/cgi-bin/search.rb', 'query=foo')
|
|
|
|
#
|
|
|
|
# # version 1.2
|
|
|
|
# response = http.post('/cgi-bin/search.rb', 'query=foo')
|
|
|
|
#
|
|
|
|
# # using block
|
|
|
|
# File.open('result.txt', 'w') {|f|
|
2003-08-01 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
|
|
|
#
|
|
|
|
def post(path, data, 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(Post.new(path, initheader), data) {|r|
|
2003-08-01 11:41:41 -04:00
|
|
|
r.read_body dest, &block
|
|
|
|
res = r
|
2001-12-30 14:18:45 -05:00
|
|
|
}
|
2002-11-21 06:13:17 -05:00
|
|
|
unless @newimpl
|
2001-12-30 14:18:45 -05:00
|
|
|
res.value
|
|
|
|
return res, res.body
|
|
|
|
end
|
|
|
|
|
|
|
|
res
|
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:
|
2002-11-21 06:13:17 -05:00
|
|
|
res = request(Put.new(path, initheader), data)
|
2003-12-29 06:08:47 -05:00
|
|
|
res.value unless @newimpl
|
2001-12-30 14:18:45 -05:00
|
|
|
res
|
|
|
|
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
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Sends a GET request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
#
|
|
|
|
# When called with a block, yields an HTTPResponse object.
|
|
|
|
# The body of this response will not have been read yet;
|
|
|
|
# the caller can process it using HTTPResponse#read_body,
|
|
|
|
# if desired.
|
|
|
|
#
|
|
|
|
# Returns the response.
|
|
|
|
#
|
|
|
|
# This method never raises Net::* exceptions.
|
|
|
|
#
|
|
|
|
# response = http.request_get('/index.html')
|
|
|
|
# # The entity body is already read here.
|
|
|
|
# p response['content-type']
|
|
|
|
# puts response.body
|
|
|
|
#
|
|
|
|
# # using block
|
|
|
|
# http.request_get('/index.html') {|response|
|
2003-08-01 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
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Sends a HEAD request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
#
|
|
|
|
# Returns the response.
|
|
|
|
#
|
|
|
|
# This method never raises Net::* exceptions.
|
|
|
|
#
|
|
|
|
# response = http.request_head('/index.html')
|
|
|
|
# p response['content-type']
|
|
|
|
#
|
2003-12-29 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
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Sends a POST request to the +path+ and gets a response,
|
|
|
|
# as an HTTPResponse object.
|
|
|
|
#
|
|
|
|
# When called with a block, yields an HTTPResponse object.
|
|
|
|
# The body of this response will not have been read yet;
|
|
|
|
# the caller can process it using HTTPResponse#read_body,
|
|
|
|
# if desired.
|
|
|
|
#
|
|
|
|
# Returns the response.
|
|
|
|
#
|
|
|
|
# This method never raises Net::* exceptions.
|
|
|
|
#
|
|
|
|
# # example
|
|
|
|
# response = http.request_post('/cgi-bin/nice.rb', 'datadatadata...')
|
|
|
|
# p response.status
|
|
|
|
# puts response.body # body is already read
|
|
|
|
#
|
|
|
|
# # using block
|
|
|
|
# http.request_post('/cgi-bin/nice.rb', 'datadatadata...') {|response|
|
2003-08-01 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.
|
|
|
|
# This method also sends DATA string if DATA is given.
|
|
|
|
#
|
|
|
|
# Returns a HTTPResponse object.
|
|
|
|
#
|
|
|
|
# This method never raises Net::* exceptions.
|
|
|
|
#
|
|
|
|
# response = http.send_request('GET', '/index.html')
|
|
|
|
# puts response.body
|
|
|
|
#
|
2003-12-29 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
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Sends an HTTPRequest object REQUEST to the HTTP server.
|
|
|
|
# This method also sends DATA string if REQUEST is a post/put request.
|
|
|
|
# Giving DATA for get/head request causes ArgumentError.
|
|
|
|
#
|
|
|
|
# When called with a block, yields an HTTPResponse object.
|
|
|
|
# The body of this response will not have been read yet;
|
|
|
|
# the caller can process it using HTTPResponse#read_body,
|
|
|
|
# if desired.
|
|
|
|
#
|
|
|
|
# Returns a HTTPResponse object.
|
|
|
|
#
|
|
|
|
# This method never raises Net::* exceptions.
|
2003-12-29 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()
|
2005-06-30 01:58:42 -04:00
|
|
|
unless use_ssl?
|
|
|
|
req.proxy_basic_auth proxy_user(), proxy_pass()
|
|
|
|
end
|
2002-11-21 06:13:17 -05:00
|
|
|
end
|
2003-12-29 06:08:47 -05:00
|
|
|
|
2004-03-06 13:11:40 -05:00
|
|
|
req.set_body_internal body
|
2002-02-19 07:33:52 -05:00
|
|
|
begin_transport req
|
2004-03-06 13:11:40 -05:00
|
|
|
req.exec @socket, @curr_http_version, edit_path(req.path)
|
2003-12-29 06:08:47 -05:00
|
|
|
begin
|
|
|
|
res = HTTPResponse.read_new(@socket)
|
2004-03-06 12:08:21 -05:00
|
|
|
end while res.kind_of?(HTTPContinue)
|
2003-12-29 06:08:47 -05:00
|
|
|
res.reading_body(@socket, req.response_body_permitted?) {
|
|
|
|
yield res if block_given?
|
|
|
|
}
|
2002-02-19 07:33:52 -05:00
|
|
|
end_transport req, res
|
|
|
|
|
|
|
|
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
|
|
|
|
2003-12-29 06:08:47 -05:00
|
|
|
def begin_transport(req)
|
2002-11-21 06:13:17 -05:00
|
|
|
if @socket.closed?
|
2004-03-06 12:08:21 -05:00
|
|
|
connect
|
1999-12-29 06:14:04 -05:00
|
|
|
end
|
2002-11-21 06:13:17 -05:00
|
|
|
if @seems_1_0_server
|
2004-04-13 04:55:55 -04:00
|
|
|
req['connection'] ||= 'close'
|
2002-03-26 06:18:02 -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
|
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
|
2002-11-21 06:13:17 -05:00
|
|
|
if 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'
|
2002-11-21 06:13:17 -05:00
|
|
|
if @socket.closed?
|
2001-02-23 23:53:50 -05:00
|
|
|
D 'Conn (but seems 1.0 server)'
|
2001-02-06 06:14:51 -05:00
|
|
|
@seems_1_0_server = true
|
|
|
|
end
|
|
|
|
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)
|
|
|
|
return false if /close/i =~ req['connection'].to_s
|
|
|
|
return false if @seems_1_0_server
|
|
|
|
return true if /keep-alive/i =~ res['connection'].to_s
|
|
|
|
return false if /close/i =~ res['connection'].to_s
|
|
|
|
return true if /keep-alive/i =~ res['proxy-connection'].to_s
|
|
|
|
return false if /close/i =~ res['proxy-connection'].to_s
|
|
|
|
(@curr_http_version == '1.1')
|
1999-12-29 06:14:04 -05:00
|
|
|
end
|
1999-12-17 10:00:13 -05:00
|
|
|
|
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
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
|
|
|
# Header module.
|
|
|
|
#
|
|
|
|
# Provides access to @header in the mixed-into class as a hash-like
|
|
|
|
# object, except with case-insensitive keys. Also provides
|
|
|
|
# methods for accessing commonly-used header values in a more
|
|
|
|
# convenient format.
|
|
|
|
#
|
2001-02-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
|
|
|
|
@header[key.downcase] = Array(val).map {|s| s.to_str }
|
|
|
|
end
|
|
|
|
|
2005-09-13 02:41:59 -04:00
|
|
|
# Adds header field instead of replace.
|
|
|
|
# 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)
|
2004-03-06 15:35:19 -05:00
|
|
|
@header[key.downcase].concat Array(val)
|
|
|
|
else
|
|
|
|
@header[key.downcase] = Array(val).dup
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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.
|
|
|
|
# Returns the default value +args+, or the result of the block, or nil,
|
|
|
|
# if there's no header field named key. See Hash#fetch
|
2003-12-29 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)
|
|
|
|
a.join(', ')
|
2001-01-13 14:07:15 -05:00
|
|
|
end
|
2000-11-07 06:27:16 -05:00
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
# Iterates for each header names and values.
|
2004-03-06 15:35:19 -05:00
|
|
|
def each_header #:yield: +key+, +value+
|
|
|
|
@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
|
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
# Iterates for each header names.
|
2004-03-06 15:35:19 -05:00
|
|
|
def each_name(&block) #:yield: +key+
|
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
|
|
|
|
|
|
|
|
# Iterates for each capitalized header names.
|
|
|
|
def each_capitalized_name(&block) #:yield: +key+
|
|
|
|
@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
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
# Iterates for each header values.
|
2004-03-06 15:35:19 -05:00
|
|
|
def each_value #:yield: +value+
|
|
|
|
@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
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
# Removes a header field.
|
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
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
# Returns a Hash consist of header names and values.
|
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.
|
|
|
|
def each_capitalized
|
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
|
|
|
|
2004-03-06 15:35:19 -05:00
|
|
|
# Returns an Array of Range objects which represents Range: header field,
|
2003-08-01 10:01:14 -04:00
|
|
|
# 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
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Set Range: header from Range (arg r) or beginning index and
|
2004-03-06 15:35:19 -05:00
|
|
|
# length from it (arg idx&len).
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Returns an Integer object which represents the Content-Length: header field
|
|
|
|
# or +nil+ if that field is not provided.
|
2001-02-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
|
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
|
|
|
|
# set to "chunked". This is an HTTP/1.1 feature, allowing the
|
|
|
|
# the content to be sent in "chunks" without at the outset
|
|
|
|
# stating the entire content length.
|
2001-02-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
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# Returns a Range object which represents Content-Range: header field.
|
|
|
|
# This indicates, for a partial entity body, where this fragment
|
|
|
|
# fits inside the full entity body, as range of byte offsets.
|
2001-02-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'
|
2001-02-22 18:23:57 -05:00
|
|
|
m[1].to_i .. m[2].to_i + 1
|
|
|
|
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
|
|
|
|
r.end - r.begin
|
2001-02-22 18:23:57 -05:00
|
|
|
end
|
|
|
|
|
2004-03-06 15:35:19 -05:00
|
|
|
def content_type
|
|
|
|
"#{main_type()}/#{sub_type()}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def main_type
|
|
|
|
return nil unless @header['content-type']
|
|
|
|
self['Content-Type'].split(';').first.to_s.split('/')[0].to_s.strip
|
|
|
|
end
|
|
|
|
|
|
|
|
def sub_type
|
|
|
|
return nil unless @header['content-type']
|
|
|
|
self['Content-Type'].split(';').first.to_s.split('/')[1].to_s.strip
|
|
|
|
end
|
|
|
|
|
|
|
|
def type_params
|
|
|
|
result = {}
|
|
|
|
self['Content-Type'].to_s.split(';')[1..-1].each do |param|
|
|
|
|
k, v = *param.split('=', 2)
|
|
|
|
result[k.strip] = v.strip
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_content_type(type, params = {})
|
|
|
|
@header['content-type'] = [type + params.map{|k,v|"; #{k}=#{v}"}.join('')]
|
|
|
|
end
|
|
|
|
|
|
|
|
alias content_type= set_content_type
|
|
|
|
|
2005-04-20 09:55:26 -04:00
|
|
|
def set_form_data(params, sep = '&')
|
|
|
|
self.body = params.map {|k,v| "#{urlencode(k.to_s)}=#{urlencode(v.to_s)}" }.join(sep)
|
|
|
|
self.content_type = 'application/x-www-form-urlencoded'
|
|
|
|
end
|
|
|
|
|
|
|
|
alias form_data= set_form_data
|
|
|
|
|
|
|
|
def urlencode(str)
|
|
|
|
str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }
|
|
|
|
end
|
|
|
|
private :urlencode
|
|
|
|
|
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
|
|
|
|
2001-02-22 18:23:57 -05:00
|
|
|
end
|
|
|
|
|
2004-03-06 12:08:21 -05:00
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
|
|
|
# Parent of HTTPRequest class. Do not use this directly; use
|
|
|
|
# a subclass of HTTPRequest.
|
|
|
|
#
|
|
|
|
# Mixes in the HTTPHeader module.
|
|
|
|
#
|
2001-07-03 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
|
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'] ||= '*/*'
|
2004-03-06 13:11:40 -05:00
|
|
|
@body = nil
|
|
|
|
@body_stream = 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
|
|
|
|
str
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :body_stream
|
|
|
|
|
|
|
|
def body_stream=(input)
|
|
|
|
@body = nil
|
|
|
|
@body_stream = input
|
|
|
|
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
|
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)
|
2004-03-06 15:35:19 -05:00
|
|
|
self.content_length = body.length
|
|
|
|
delete 'Transfer-Encoding'
|
|
|
|
unless content_type()
|
2003-12-29 06:08:47 -05:00
|
|
|
warn 'net/http: warning: Content-Type did not set; using application/x-www-form-urlencoded' if $VERBOSE
|
2004-03-06 15:35:19 -05:00
|
|
|
set_content_type 'application/x-www-form-urlencoded'
|
2001-12-13 14:15:21 -05:00
|
|
|
end
|
2004-03-06 13:11:40 -05:00
|
|
|
write_header sock, ver, path
|
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)
|
2004-03-06 15:35:19 -05:00
|
|
|
raise ArgumentError, "Content-Length not given and Transfer-Encoding is not `chunked'" unless content_length() or chunked?
|
|
|
|
unless content_type()
|
2004-03-06 13:11:40 -05:00
|
|
|
warn 'net/http: warning: Content-Type did not set; using application/x-www-form-urlencoded' if $VERBOSE
|
2004-03-06 15:35:19 -05:00
|
|
|
set_content_type 'application/x-www-form-urlencoded'
|
2004-03-06 13:11:40 -05:00
|
|
|
end
|
|
|
|
write_header sock, ver, path
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2001-01-13 14:07:15 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
#
|
|
|
|
# HTTP request class. This class wraps request header and entity path.
|
|
|
|
# You *must* use its subclass, Net::HTTP::Get, Post, Head.
|
|
|
|
#
|
2001-07-03 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
|
|
|
#
|
|
|
|
# HTTP 1.1 methods --- RFC2616
|
|
|
|
#
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
class Options < HTTPRequest
|
|
|
|
METHOD = 'OPTIONS'
|
|
|
|
REQUEST_HAS_BODY = false
|
|
|
|
RESPONSE_HAS_BODY = false
|
|
|
|
end
|
|
|
|
|
2005-09-13 02:41:59 -04:00
|
|
|
class Trace < HTTPRequest
|
|
|
|
METHOD = 'TRACE'
|
|
|
|
REQUEST_HAS_BODY = false
|
|
|
|
RESPONSE_HAS_BODY = true
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# WebDAV methods --- RFC2518
|
|
|
|
#
|
|
|
|
|
2004-03-05 09:13:31 -05:00
|
|
|
class Propfind < HTTPRequest
|
|
|
|
METHOD = 'PROPFIND'
|
|
|
|
REQUEST_HAS_BODY = true
|
|
|
|
RESPONSE_HAS_BODY = true
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
class Copy < HTTPRequest
|
|
|
|
METHOD = 'COPY'
|
|
|
|
REQUEST_HAS_BODY = false
|
|
|
|
RESPONSE_HAS_BODY = true
|
|
|
|
end
|
|
|
|
|
2005-09-13 02:41:59 -04:00
|
|
|
class Move < HTTPRequest
|
|
|
|
METHOD = 'MOVE'
|
|
|
|
REQUEST_HAS_BODY = false
|
|
|
|
RESPONSE_HAS_BODY = true
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
2003-08-01 10:01:14 -04:00
|
|
|
# HTTP response class. This class wraps response header and entity.
|
|
|
|
# Mixes in the HTTPHeader module, which provides access to response
|
|
|
|
# header values both via hash-like methods and individual readers.
|
|
|
|
# Note that each possible HTTP response code defines its own
|
|
|
|
# HTTPResponse subclass. These are listed below.
|
|
|
|
# All classes are
|
|
|
|
# defined under the Net module. Indentation indicates inheritance.
|
|
|
|
#
|
|
|
|
# xxx HTTPResponse
|
|
|
|
#
|
|
|
|
# 1xx HTTPInformation
|
|
|
|
# 100 HTTPContinue
|
|
|
|
# 101 HTTPSwitchProtocol
|
|
|
|
#
|
|
|
|
# 2xx HTTPSuccess
|
|
|
|
# 200 HTTPOK
|
|
|
|
# 201 HTTPCreated
|
|
|
|
# 202 HTTPAccepted
|
|
|
|
# 203 HTTPNonAuthoritativeInformation
|
|
|
|
# 204 HTTPNoContent
|
|
|
|
# 205 HTTPResetContent
|
|
|
|
# 206 HTTPPartialContent
|
|
|
|
#
|
|
|
|
# 3xx HTTPRedirection
|
|
|
|
# 300 HTTPMultipleChoice
|
|
|
|
# 301 HTTPMovedPermanently
|
|
|
|
# 302 HTTPFound
|
|
|
|
# 303 HTTPSeeOther
|
|
|
|
# 304 HTTPNotModified
|
|
|
|
# 305 HTTPUseProxy
|
|
|
|
# 307 HTTPTemporaryRedirect
|
|
|
|
#
|
|
|
|
# 4xx HTTPClientError
|
|
|
|
# 400 HTTPBadRequest
|
|
|
|
# 401 HTTPUnauthorized
|
|
|
|
# 402 HTTPPaymentRequired
|
|
|
|
# 403 HTTPForbidden
|
|
|
|
# 404 HTTPNotFound
|
|
|
|
# 405 HTTPMethodNotAllowed
|
|
|
|
# 406 HTTPNotAcceptable
|
|
|
|
# 407 HTTPProxyAuthenticationRequired
|
|
|
|
# 408 HTTPRequestTimeOut
|
|
|
|
# 409 HTTPConflict
|
|
|
|
# 410 HTTPGone
|
|
|
|
# 411 HTTPLengthRequired
|
|
|
|
# 412 HTTPPreconditionFailed
|
|
|
|
# 413 HTTPRequestEntityTooLarge
|
|
|
|
# 414 HTTPRequestURITooLong
|
|
|
|
# 415 HTTPUnsupportedMediaType
|
|
|
|
# 416 HTTPRequestedRangeNotSatisfiable
|
|
|
|
# 417 HTTPExpectationFailed
|
|
|
|
#
|
|
|
|
# 5xx HTTPServerError
|
|
|
|
# 500 HTTPInternalServerError
|
|
|
|
# 501 HTTPNotImplemented
|
|
|
|
# 502 HTTPBadGateway
|
|
|
|
# 503 HTTPServiceUnavailable
|
|
|
|
# 504 HTTPGatewayTimeOut
|
|
|
|
# 505 HTTPVersionNotSupported
|
|
|
|
#
|
|
|
|
# xxx HTTPUnknownResponse
|
2003-08-01 11:41:41 -04:00
|
|
|
#
|
2002-02-19 07:33:52 -05:00
|
|
|
class HTTPResponse
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-18 19:19:47 -04:00
|
|
|
# true if the response has 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
|
|
|
|
2002-02-19 07:33:52 -05:00
|
|
|
'501' => 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)
|
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?
|
2002-02-19 07:33:52 -05:00
|
|
|
m = /\A([^:]+):\s*/.match(line) or
|
2003-12-29 06:08:47 -05:00
|
|
|
raise HTTPBadResponse, 'wrong header line format'
|
2002-02-22 07:10:58 -05:00
|
|
|
yield m[1], m.post_match
|
2001-07-03 14:13:13 -04:00
|
|
|
end
|
|
|
|
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.
|
|
|
|
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
|
|
|
|
|
|
|
# HTTP result code string. For example, '302'. You can also
|
|
|
|
# determine the response type by which response subclass the
|
|
|
|
# response object is an instance of.
|
2002-02-19 07:33:52 -05:00
|
|
|
attr_reader :code
|
2003-08-01 10:01:14 -04:00
|
|
|
|
|
|
|
# HTTP result message. 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
|
|
|
|
|
2003-06-06 03:05:52 -04:00
|
|
|
# For backward compatibility.
|
|
|
|
# To allow Net::HTTP 1.1 style assignment
|
|
|
|
# e.g.
|
|
|
|
# response, body = Net::HTTP.get(....)
|
|
|
|
#
|
|
|
|
def to_ary
|
2003-11-19 00:45:44 -05:00
|
|
|
warn "net/http.rb: warning: Net::HTTP v1.1 style assignment found at #{caller(1)[0]}; use `response = http.get(...)' instead." if $VERBOSE
|
2004-03-05 05:42:43 -05:00
|
|
|
res = self.dup
|
|
|
|
class << res
|
|
|
|
undef to_ary
|
|
|
|
end
|
|
|
|
[res, res.body]
|
2003-06-06 03:05:52 -04: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
|
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
# Raises HTTP error if the response is not 2xx.
|
|
|
|
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
|
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
# Gets entity body. If the block given, yields it to +block+.
|
2003-08-01 10:01:14 -04:00
|
|
|
# The body is provided in fragments, as it is read in from the socket.
|
|
|
|
#
|
|
|
|
# Calling this method a second or subsequent time will return the
|
2003-08-01 11:41:41 -04:00
|
|
|
# already read string.
|
|
|
|
#
|
|
|
|
# http.request_get('/index.html') {|res|
|
|
|
|
# puts res.read_body
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# http.request_get('/index.html') {|res|
|
|
|
|
# p res.read_body.object_id # 538149362
|
|
|
|
# p res.read_body.object_id # 538149362
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# # using iterator
|
|
|
|
# http.request_get('/index.html') {|res|
|
|
|
|
# res.read_body do |segment|
|
|
|
|
# print segment
|
|
|
|
# end
|
|
|
|
# }
|
|
|
|
#
|
2003-12-29 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
|
|
|
|
|
2003-08-01 11:41:41 -04:00
|
|
|
# Returns the entity body.
|
|
|
|
#
|
|
|
|
# Calling this method a second or subsequent time will return the
|
|
|
|
# already read string.
|
|
|
|
#
|
|
|
|
# http.request_get('/index.html') {|res|
|
|
|
|
# puts res.body
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# http.request_get('/index.html') {|res|
|
|
|
|
# p res.body.object_id # 538149362
|
|
|
|
# p res.body.object_id # 538149362
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
def body
|
|
|
|
read_body()
|
|
|
|
end
|
|
|
|
|
2003-08-05 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
|
2001-12-30 14:18:45 -05:00
|
|
|
@socket.read len, dest; total += len
|
1999-12-29 06:14:04 -05:00
|
|
|
@socket.read 2 # \r\n
|
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
|