Class: Net::HTTP

Inherits:
Protocol show all
Defined in:
lib/net/http.rb,
lib/net/http.rb,
lib/net/http.rb

Overview

-- for backward compatibility

Defined Under Namespace

Modules: ProxyDelta Classes: Copy, Delete, Get, Head, Lock, Mkcol, Move, Options, Post, Propfind, Proppatch, Put, Trace, Unlock

Constant Summary

Revision =

:stopdoc:

%q$Revision: 27605 $.split[1]
HTTPVersion =
'1.1'
SSL_ATTRIBUTES =
ssl_version key cert ca_file ca_path cert_store ciphers
  verify_mode verify_callback verify_depth ssl_timeout
)
ProxyMod =
ProxyDelta

Class Attribute Summary (collapse)

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (HTTP) initialize(address, port = nil)

Creates a new Net::HTTP object for the specified address. This method does not open the TCP connection.



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/net/http.rb', line 510

def initialize(address, port = nil)
  @address = address
  @port    = (port || HTTP.default_port)
  @curr_http_version = HTTPVersion
  @no_keepalive_server = false
  @close_on_empty_response = false
  @socket  = nil
  @started = false
  @open_timeout = nil
  @read_timeout = 60
  @debug_output = nil
  @use_ssl = false
  @ssl_context = nil
  @enable_post_connection_check = true
  @compression = nil
  @sspi_enabled = false
  if defined?(SSL_ATTRIBUTES)
    SSL_ATTRIBUTES.each do |name|
      instance_variable_set "@#{name}", nil
    end
  end
end

Class Attribute Details

+ (Object) proxy_address (readonly)

Returns the value of attribute proxy_address



754
755
756
# File 'lib/net/http.rb', line 754

def proxy_address
  @proxy_address
end

+ (Object) proxy_pass (readonly)

Returns the value of attribute proxy_pass



757
758
759
# File 'lib/net/http.rb', line 757

def proxy_pass
  @proxy_pass
end

+ (Object) proxy_port (readonly)

Returns the value of attribute proxy_port



755
756
757
# File 'lib/net/http.rb', line 755

def proxy_port
  @proxy_port
end

+ (Object) proxy_user (readonly)

Returns the value of attribute proxy_user



756
757
758
# File 'lib/net/http.rb', line 756

def proxy_user
  @proxy_user
end

Instance Attribute Details

- (Object) address (readonly)

The host name to connect to.



552
553
554
# File 'lib/net/http.rb', line 552

def address
  @address
end

- (Object) close_on_empty_response

Returns the value of attribute close_on_empty_response



580
581
582
# File 'lib/net/http.rb', line 580

def close_on_empty_response
  @close_on_empty_response
end

- (Object) open_timeout

Seconds to wait until connection is opened. If the HTTP object cannot open a connection in this many seconds, it raises a TimeoutError exception.



560
561
562
# File 'lib/net/http.rb', line 560

def open_timeout
  @open_timeout
end

- (Object) port (readonly)

The port number to connect to.



555
556
557
# File 'lib/net/http.rb', line 555

def port
  @port
end

- (Object) read_timeout

Seconds to wait until reading one block (by one read(2) call). If the HTTP object cannot open a connection in this many seconds, it raises a TimeoutError exception.



565
566
567
# File 'lib/net/http.rb', line 565

def read_timeout
  @read_timeout
end

Class Method Details

+ (Object) default_port

The default port to use for HTTP requests; defaults to 80.



428
429
430
# File 'lib/net/http.rb', line 428

def HTTP.default_port
  http_default_port()
end

+ (Object) get(uri_or_host, path = nil, port = nil)

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:

print Net::HTTP.get('www.example.com', '/index.html')


369
370
371
# File 'lib/net/http.rb', line 369

def HTTP.get(uri_or_host, path = nil, port = nil)
  get_response(uri_or_host, path, port).body
end

+ (Object) get_print(uri_or_host, path = nil, port = nil)

Get body from target and output it to $stdout. The target can either be specified as (uri), or as (host, path, port = 80); so:

Net::HTTP.get_print URI.parse('http://www.example.com/index.html')

or:

Net::HTTP.get_print 'www.example.com', '/index.html'


350
351
352
353
354
355
356
357
# File 'lib/net/http.rb', line 350

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
  }
  nil
end

+ (Object) get_response(uri_or_host, path = nil, port = nil, &block)

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


385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/net/http.rb', line 385

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)
    }
  else
    uri = uri_or_host
    new(uri.host, uri.port).start {|http|
      return http.request_get(uri.request_uri, &block)
    }
  end
end

+ (Object) http_default_port

The default port to use for HTTP requests; defaults to 80.



433
434
435
# File 'lib/net/http.rb', line 433

def HTTP.http_default_port
  80
end

+ (Object) https_default_port

The default port to use for HTTPS requests; defaults to 443.



438
439
440
# File 'lib/net/http.rb', line 438

def HTTP.https_default_port
  443
end

+ (Object) new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil)

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.



500
501
502
503
504
505
506
# File 'lib/net/http.rb', line 500

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
end

+ (Object) newobj



494
# File 'lib/net/http.rb', line 494

alias newobj new

+ (Object) post_form(url, params)

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" }


414
415
416
417
418
419
420
421
# File 'lib/net/http.rb', line 414

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

+ (Object) Proxy(p_addr, p_port = nil, p_user = nil, p_pass = nil)

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|
  # connecting proxy.foo.org:8080
                :
}


732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# File 'lib/net/http.rb', line 732

def HTTP.Proxy(p_addr, p_port = nil, p_user = nil, p_pass = nil)
  return self unless p_addr
  delta = ProxyDelta
  proxyclass = Class.new(self)
  proxyclass.module_eval {
    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
  }
  proxyclass
end

+ (Boolean) proxy_class?

returns true if self is a class which was created by HTTP::Proxy.

Returns:

  • (Boolean)


750
751
752
# File 'lib/net/http.rb', line 750

def proxy_class?
  @is_proxy_class
end

+ (Object) socket_type

:nodoc: obsolete



442
443
444
# File 'lib/net/http.rb', line 442

def HTTP.socket_type   #:nodoc: obsolete
  BufferedIO
end

+ (Object) start(address, *arg, &block)

call-seq:

HTTP.start(address, port, p_addr, p_port, p_user, p_pass, &block)
HTTP.start(address, port=nil, p_addr=nil, p_port=nil, p_user=nil, p_pass=nil, opt, &block)

creates a new Net::HTTP object and opens its TCP connection and HTTP session.

Argments are following:

address

hostname or IP address of the server

port

port of the server

p_addr

address of proxy

p_port

port of proxy

p_user

user of proxy

p_pass

pass of proxy

opt

optional hash

opt sets following values by its accessor. The keys are ca_file, ca_path, cert, cert_store, ciphers, close_on_empty_response, key, open_timeout, read_timeout, ssl_timeout, ssl_version, use_ssl, verify_callback, verify_depth and verify_mode. If you set :use_ssl as true, you can use https and default value of verify_mode is set as OpenSSL::SSL::VERIFY_PEER.

If the optional block is given, the newly 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.



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/net/http.rb', line 475

def HTTP.start(address, *arg, &block) # :yield: +http+
  arg.pop if opt = Hash.try_convert(arg[-1])
  port, p_addr, p_port, p_user, p_pass = *arg
  port = https_default_port if !port && opt && opt[:use_ssl]
  http = new(address, port, p_addr, p_port, p_user, p_pass)

  if opt
    opt = {verify_mode: OpenSSL::SSL::VERIFY_PEER}.update(opt) if opt[:use_ssl]
    http.methods.grep(/\A(\w+)=\z/) do |meth|
      key = $1.to_sym
      opt.key?(key) or next
      http.__send__(meth, opt[key])
    end
  end

  http.start(&block)
end

+ (Object) version_1_1

Turns on net/http 1.1 (ruby 1.6) features. Defaults to OFF in ruby 1.8.



314
315
316
# File 'lib/net/http.rb', line 314

def HTTP.version_1_1
  @newimpl = false
end

+ (Boolean) version_1_1? Also known as: is_version_1_1?

true if net/http is in version 1.1 compatible mode. Defaults to true.

Returns:

  • (Boolean)


326
327
328
# File 'lib/net/http.rb', line 326

def HTTP.version_1_1?
  not @newimpl
end

+ (Object) version_1_2

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


308
309
310
# File 'lib/net/http.rb', line 308

def HTTP.version_1_2
  @newimpl = true
end

+ (Boolean) version_1_2? Also known as: is_version_1_2?

true if net/http is in version 1.2 mode. Defaults to true.

Returns:

  • (Boolean)


320
321
322
# File 'lib/net/http.rb', line 320

def HTTP.version_1_2?
  @newimpl
end

Instance Method Details

- (Object) copy(path, initheader = nil)

Sends a COPY request to the path and gets a response, as an HTTPResponse object.



1033
1034
1035
# File 'lib/net/http.rb', line 1033

def copy(path, initheader = nil)
  request(Copy.new(path, initheader))
end

- (Object) delete(path, initheader = {'Depth' => 'Infinity'})

Sends a DELETE request to the path and gets a response, as an HTTPResponse object.



1021
1022
1023
# File 'lib/net/http.rb', line 1021

def delete(path, initheader = {'Depth' => 'Infinity'})
  request(Delete.new(path, initheader))
end

- (Object) finish

Finishes HTTP session and closes TCP connection. Raises IOError if not started.

Raises:

  • (IOError)


692
693
694
695
# File 'lib/net/http.rb', line 692

def finish
  raise IOError, 'HTTP session not yet started' unless started?
  do_finish
end

- (Object) get(path, initheader = {}, dest = nil, &block)

Gets data from path on the connected-to host. initheader must be a Hash like { 'Accept' => '/', ... }, and it defaults to an empty hash. If initheader doesn't have the key 'accept-encoding', then a value of "gzip;q=1.0,deflate;q=0.6,identity;q=0.3" is used, so that gzip compression is used in preference to deflate compression, which is used in preference to no compression. Ruby doesn't have libraries to support the compress (Lempel-Ziv) compression, so that is not supported. The intent of this is to reduce bandwidth by default. If this routine sets up compression, then it does the decompression also, removing the header as well to prevent confusion. Otherwise it leaves the body as it found it.

In version 1.1 (ruby 1.6), this method returns a pair of objects, a Net::HTTPResponse object and the entity body string. In version 1.2 (ruby 1.8), this method returns a Net::HTTPResponse 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.

dest argument is obsolete. It still works but you must not use it.

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')

# version 1.2 (bundled with Ruby 1.8 or later)
response = http.get('/index.html')

# using block
File.open('result.txt', 'w') {|f|
  http.get('/~foo/') do |str|
    f.write str
  end
}


872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
# File 'lib/net/http.rb', line 872

def get(path, initheader = {}, dest = nil, &block) # :yield: +body_segment+
  res = nil
  if HAVE_ZLIB
    unless  initheader.keys.any?{|k| k.downcase == "accept-encoding"}
      initheader = initheader.merge({
        "accept-encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
      })
      @compression = true
    end
  end
  request(Get.new(path, initheader)) {|r|
    if r.key?("content-encoding") and @compression
      @compression = nil # Clear it till next set.
      the_body = r.read_body dest, &block
      case r["content-encoding"]
      when "gzip"
        r.body= Zlib::GzipReader.new(StringIO.new(the_body)).read
        r.delete("content-encoding")
      when "deflate"
        r.body= Zlib::Inflate.inflate(the_body);
        r.delete("content-encoding")
      when "identity"
        ; # nothing needed
      else
        ; # Don't do anything dramatic, unless we need to later
      end
    else
      r.read_body dest, &block
    end
    res = r
  }
  unless @newimpl
    res.value
    return res, res.body
  end

  res
end

- (Object) head(path, initheader = nil)

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|
  response = http.head('/index.html')
}
p response['content-type']


927
928
929
930
931
# File 'lib/net/http.rb', line 927

def head(path, initheader = nil)
  res = request(Head.new(path, initheader))
  res.value unless @newimpl
  res
end

- (Object) inspect



533
534
535
# File 'lib/net/http.rb', line 533

def inspect
  "#<#{self.class} #{@address}:#{@port} open=#{started?}>"
end

- (Object) lock(path, body, initheader = nil)

Sends a LOCK request to the path and gets a response, as an HTTPResponse object.



997
998
999
# File 'lib/net/http.rb', line 997

def lock(path, body, initheader = nil)
  request(Lock.new(path, initheader), body)
end

- (Object) mkcol(path, body = nil, initheader = nil)

Sends a MKCOL request to the path and gets a response, as an HTTPResponse object.



1039
1040
1041
# File 'lib/net/http.rb', line 1039

def mkcol(path, body = nil, initheader = nil)
  request(Mkcol.new(path, initheader), body)
end

- (Object) move(path, initheader = nil)

Sends a MOVE request to the path and gets a response, as an HTTPResponse object.



1027
1028
1029
# File 'lib/net/http.rb', line 1027

def move(path, initheader = nil)
  request(Move.new(path, initheader))
end

- (Object) options(path, initheader = nil)

Sends a OPTIONS request to the path and gets a response, as an HTTPResponse object.



1009
1010
1011
# File 'lib/net/http.rb', line 1009

def options(path, initheader = nil)
  request(Options.new(path, initheader))
end

- (Object) peer_cert

return the X.509 certificates the server presented.



606
607
608
609
610
611
# File 'lib/net/http.rb', line 606

def peer_cert
  if not use_ssl? or not @socket
    return nil
  end
  @socket.io.peer_cert
end

- (Object) post(path, data, initheader = nil, dest = nil, &block)

Posts data (must be a String) to path. header must be a Hash like { 'Accept' => '/', ... }.

In version 1.1 (ruby 1.6), this method returns a pair of objects, a Net::HTTPResponse object and an entity body string. In version 1.2 (ruby 1.8), this method returns a Net::HTTPResponse object.

If called with a block, yields each fragment of the entity body in turn as a string as it are read from the socket. Note that in this case, the returned response object will not contain a (meaningful) body.

dest argument is obsolete. It still works but you must not use it.

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|
  http.post('/cgi-bin/search.rb', 'query=foo') do |str|
    f.write str
  end
}

You should set Content-Type: header field for POST. If no Content-Type: field given, this method uses "application/x-www-form-urlencoded" by default.



970
971
972
973
974
975
976
977
978
979
980
981
# File 'lib/net/http.rb', line 970

def post(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
  res = nil
  request(Post.new(path, initheader), data) {|r|
    r.read_body dest, &block
    res = r
  }
  unless @newimpl
    res.value
    return res, res.body
  end
  res
end

- (Object) propfind(path, body = nil, initheader = {'Depth' => '0'})

Sends a PROPFIND request to the path and gets a response, as an HTTPResponse object.



1015
1016
1017
# File 'lib/net/http.rb', line 1015

def propfind(path, body = nil, initheader = {'Depth' => '0'})
  request(Propfind.new(path, initheader), body)
end

- (Object) proppatch(path, body, initheader = nil)

Sends a PROPPATCH request to the path and gets a response, as an HTTPResponse object.



991
992
993
# File 'lib/net/http.rb', line 991

def proppatch(path, body, initheader = nil)
  request(Proppatch.new(path, initheader), body)
end

- (Boolean) proxy?

True if self is a HTTP proxy class.

Returns:

  • (Boolean)


761
762
763
# File 'lib/net/http.rb', line 761

def proxy?
  self.class.proxy_class?
end

- (Object) proxy_address Also known as: proxyaddr

Address of proxy host. If self does not use a proxy, nil.



766
767
768
# File 'lib/net/http.rb', line 766

def proxy_address
  self.class.proxy_address
end

- (Object) proxy_pass

User password for accessing proxy. If self does not use a proxy, nil.



781
782
783
# File 'lib/net/http.rb', line 781

def proxy_pass
  self.class.proxy_pass
end

- (Object) proxy_port Also known as: proxyport

Port number of proxy host. If self does not use a proxy, nil.



771
772
773
# File 'lib/net/http.rb', line 771

def proxy_port
  self.class.proxy_port
end

- (Object) proxy_user

User name for accessing proxy. If self does not use a proxy, nil.



776
777
778
# File 'lib/net/http.rb', line 776

def proxy_user
  self.class.proxy_user
end

- (Object) put(path, data, initheader = nil)

:nodoc:



983
984
985
986
987
# File 'lib/net/http.rb', line 983

def put(path, data, initheader = nil)   #:nodoc:
  res = request(Put.new(path, initheader), data)
  res.value unless @newimpl
  res
end

- (Object) request(req, body = nil, &block)

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.



1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
# File 'lib/net/http.rb', line 1160

def request(req, body = nil, &block)  # :yield: +response+
  unless started?
    start {
      req['connection'] ||= 'close'
      return request(req, body, &block)
    }
  end
  if proxy_user()
    req.proxy_basic_auth proxy_user(), proxy_pass() unless use_ssl?
  end
  req.set_body_internal body
  res = transport_request(req, &block)
  if sspi_auth?(res)
    sspi_auth(req)
    res = transport_request(req, &block)
  end
  res
end

- (Object) request_get(path, initheader = nil, &block) Also known as: get2

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|
  p response['content-type']
  response.read_body do |str|   # read body now
    print str
  end
}


1074
1075
1076
# File 'lib/net/http.rb', line 1074

def request_get(path, initheader = nil, &block) # :yield: +response+
  request(Get.new(path, initheader), &block)
end

- (Object) request_head(path, initheader = nil, &block) Also known as: head2

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']


1088
1089
1090
# File 'lib/net/http.rb', line 1088

def request_head(path, initheader = nil, &block)
  request(Head.new(path, initheader), &block)
end

- (Object) request_post(path, data, initheader = nil, &block) Also known as: post2

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|
  p response.status
  p response['content-type']
  response.read_body do |str|   # read body now
    print str
  end
}


1118
1119
1120
# File 'lib/net/http.rb', line 1118

def request_post(path, data, initheader = nil, &block) # :yield: +response+
  request Post.new(path, initheader), data, &block
end

- (Object) request_put(path, data, initheader = nil, &block) Also known as: put2

:nodoc:



1122
1123
1124
# File 'lib/net/http.rb', line 1122

def request_put(path, data, initheader = nil, &block)   #:nodoc:
  request Put.new(path, initheader), data, &block
end

- (Object) send_request(name, path, data = nil, header = nil)

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


1142
1143
1144
1145
# File 'lib/net/http.rb', line 1142

def send_request(name, path, data = nil, header = nil)
  r = HTTPGenericRequest.new(name,(data ? true : false),true,path,header)
  request r, data
end

- (Object) set_debug_output(output)

WARNING This method causes serious security hole. Never use this method in production code.

Set an output stream for debugging.

http = Net::HTTP.new
http.set_debug_output $stderr
http.start { .... }


546
547
548
549
# File 'lib/net/http.rb', line 546

def set_debug_output(output)
  warn 'Net::HTTP#set_debug_output called after HTTP started' if started?
  @debug_output = output
end

- (Object) start

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.

Raises:

  • (IOError)


622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'lib/net/http.rb', line 622

def start  # :yield: http
  raise IOError, 'HTTP session already opened' if @started
  if block_given?
    begin
      do_start
      return yield(self)
    ensure
      do_finish
    end
  end
  do_start
  self
end

- (Boolean) started? Also known as: active?

returns true if the HTTP session is started.

Returns:

  • (Boolean)


574
575
576
# File 'lib/net/http.rb', line 574

def started?
  @started
end

- (Object) trace(path, initheader = nil)

Sends a TRACE request to the path and gets a response, as an HTTPResponse object.



1045
1046
1047
# File 'lib/net/http.rb', line 1045

def trace(path, initheader = nil)
  request(Trace.new(path, initheader))
end

- (Object) unlock(path, body, initheader = nil)

Sends a UNLOCK request to the path and gets a response, as an HTTPResponse object.



1003
1004
1005
# File 'lib/net/http.rb', line 1003

def unlock(path, body, initheader = nil)
  request(Unlock.new(path, initheader), body)
end

- (Object) use_ssl=(flag)

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.



591
592
593
594
595
596
597
# File 'lib/net/http.rb', line 591

def use_ssl=(flag)
  flag = (flag ? true : false)
  if started? and @use_ssl != flag
    raise IOError, "use_ssl value changed, but session already started"
  end
  @use_ssl = flag
end

- (Boolean) use_ssl?

returns true if use SSL/TLS with HTTP.

Returns:

  • (Boolean)


583
584
585
# File 'lib/net/http.rb', line 583

def use_ssl?
  @use_ssl
end