diff options
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | ext/openssl/lib/net/https.rb | 92 | ||||
-rw-r--r-- | lib/net/http.rb | 95 |
3 files changed, 99 insertions, 94 deletions
@@ -1,3 +1,9 @@ +Sat Mar 6 02:00:19 2004 Minero Aoki <aamine@loveruby.net> + + * lib/net/http.rb: net/https is merged. + + * ext/openssl/lib/net/https.rb: ditto. + Fri Mar 6 00:39:21 2004 Kazuo Saito <ksaito@uranus.dti.ne.jp> * oniggnu.h: imported from Oniguruma library. diff --git a/ext/openssl/lib/net/https.rb b/ext/openssl/lib/net/https.rb index fb7f53c555..772a504e74 100644 --- a/ext/openssl/lib/net/https.rb +++ b/ext/openssl/lib/net/https.rb @@ -33,7 +33,7 @@ Simple HTTP client is here: path = $3 end h = Net::HTTP.new(host, port) - h.get2(path){ |resp| print resp.body } + h.request_get(path) {|res| print res.body } It can be replaced by follow one: @@ -47,7 +47,7 @@ It can be replaced by follow one: end h = Net::HTTP.new(host, port) h.use_ssl = true if scheme == "https" # enable SSL/TLS - h.get2(path){ |resp| print resp.body } + h.request_get(path) {|res| print res.body } === Instance Methods @@ -98,91 +98,5 @@ It can be replaced by follow one: =end -require 'net/protocols' +# HTTPS implementation is merged in to net/http. require 'net/http' - -module Net - class HTTP - class Conn < HTTPRequest - REQUEST_HAS_BODY=false - RESPONSE_HAS_BODY=false - METHOD="connect" - - def initialize - super nil, nil - end - - def exec( sock, addr, port, ver ) - @socket = sock - request(addr, port, ver) - end - - def request( addr, port, ver ) - @socket.writeline sprintf('CONNECT %s:%s HTTP/%s', addr, port, ver) - @socket.writeline '' - end - end - - module ProxyMod - def edit_path( path ) - if use_ssl - 'https://' + addr_port + path - else - 'http://' + addr_port + path - end - end - end - - def self.socket_type - SSLIO - end - - attr_reader :use_ssl - attr_writer :key, :cert - attr_writer :ca_file, :ca_path - attr_writer :verify_mode, :verify_callback, :verify_depth - attr_writer :cert_store, :timeout - attr_reader :peer_cert - - alias :default_initialize :initialize - - def initialize(*args) - default_initialize(*args) - @key = @cert = @ca_file = @ca_path = @verify_mode = - @verify_callback = @verify_depth = @timeout = @cert_store = nil - @already_connected = false - end - - def use_ssl=(flag) - if @already_connected && !@use_ssl - raise ProtocolError, "connection is alrady set up" - end - @use_ssl = flag - end - - def on_connect - if use_ssl - if proxy? - Conn.new.exec(@socket, @address, @port, "1.0") - resp = HTTPResponse.read_new(@socket) - if resp.code != '200' - raise resp.message - end - end - @socket.key = @key if @key - @socket.cert = @cert if @cert - @socket.ca_file = @ca_file - @socket.ca_path = @ca_path - @socket.verify_mode = @verify_mode - @socket.verify_callback = @verify_callback - @socket.verify_depth = @verify_depth - @socket.timeout = @timeout - @socket.cert_store = @cert_store - @socket.ssl_connect - @peer_cert = @socket.peer_cert - end - @already_connected = true - end - - end -end diff --git a/lib/net/http.rb b/lib/net/http.rb index 798083ee4b..540c878c01 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -25,6 +25,10 @@ require 'net/protocol' require 'uri' +begin + require 'net/protocols' +rescue LoadError +end module Net # :nodoc: @@ -305,9 +309,19 @@ module Net # :nodoc: # The default port to use for HTTP requests; defaults to 80. def HTTP.default_port + http_default_port() + end + + # The default port to use for HTTP requests; defaults to 80. + def HTTP.http_default_port 80 end + # The default port to use for HTTPS requests; defaults to 80. + def HTTP.https_default_port + 443 + end + def HTTP.socket_type #:nodoc: obsolete InternetMessageIO end @@ -352,6 +366,19 @@ module Net # :nodoc: @read_timeout = 60 @debug_output = nil + + # ssl + @use_ssl = false + @key = nil + @cert = nil + @ca_file = nil + @ca_path = nil + @verify_mode = nil + @verify_callback = nil + @verify_depth = nil + @ssl_timeout = nil + @cert_store = nil + @peer_cert = nil end def inspect @@ -403,6 +430,31 @@ module Net # :nodoc: attr_accessor :close_on_empty_response + # returns true if use SSL/TLS with HTTP. + def use_ssl? + @use_ssl + end + + alias use_ssl use_ssl? #:nodoc: + + # turn on/off SSL. + # This flag must be set before starting session. + # If you change use_ssl value after session started, + # a Net::HTTP object raises IOError. + def use_ssl=(flag) + flag = (flag ? true : false) + raise IOError, "use_ssl value changed but session already started" if started? and @use_ssl != flag + @use_ssl = flag + end + + attr_writer :key, :cert + attr_writer :ca_file, :ca_path + attr_writer :verify_mode, :verify_callback, :verify_depth + attr_writer :cert_store, :ssl_timeout + attr_reader :peer_cert + + alias timeout= ssl_timeout= # for backward compatibility + # Opens TCP connection and HTTP session. # # When this method is called with block, gives a HTTP object @@ -427,9 +479,34 @@ module Net # :nodoc: end def do_start - @socket = self.class.socket_type.open(conn_address(), conn_port(), - @open_timeout, @read_timeout, - @debug_output) + if use_ssl? + require 'net/protocols' + sockclass = SSLIO + else + sockclass = InternetMessageIO + end + @socket = sockclass.open(conn_address(), conn_port(), + @open_timeout, @read_timeout, @debug_output) + if use_ssl? + if proxy? + @socket.writeline sprintf('CONNECT %s:%s HTTP/%s', + @address, @port, HTTP_VERSION) + @socket.writeline '' + res = HTTPResponse.read_new(@socket) + res.value + end + @socket.key = @key if @key + @socket.cert = @cert if @cert + @socket.ca_file = @ca_file + @socket.ca_path = @ca_path + @socket.verify_mode = @verify_mode + @socket.verify_callback = @verify_callback + @socket.verify_depth = @verify_depth + @socket.timeout = @ssl_timeout + @socket.cert_store = @cert_store + @socket.ssl_connect + @peer_cert = @socket.peer_cert + end on_connect @started = true end @@ -565,7 +642,11 @@ module Net # :nodoc: end def edit_path(path) - 'http://' + addr_port() + path + if use_ssl? + "https://#{addr_port()}#{path}" + else + "http://#{addr_port()}#{path}" + end end end @@ -944,7 +1025,11 @@ module Net # :nodoc: private def addr_port - address + (port == HTTP.default_port ? '' : ":#{port}") + if use_ssl? + address() + (port == HTTP.https_default_port ? '' : ":#{port()}") + else + address() + (port == HTTP.http_default_port ? '' : ":#{port()}") + end end def D(msg) |