In Files

  • net/http.rb

Net::HTTPSession

Attributes

proxy_address[R]
proxy_pass[R]
proxy_port[R]
proxy_user[R]
address[R]

The host name to connect to.

close_on_empty_response[RW]
open_timeout[RW]

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

port[R]

The port number to connect to.

read_timeout[R]

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.

Public Class Methods

Proxy(p_addr, p_port = nil, p_user = nil, p_pass = nil) click to toggle source

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
                :
}
 
               # File net/http.rb, line 666
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
            
default_port() click to toggle source

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

 
               # File net/http.rb, line 427
def HTTP.default_port
  http_default_port()
end
            
get(uri_or_host, path = nil, port = nil) click to toggle source

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')
 
               # File net/http.rb, line 368
def HTTP.get(uri_or_host, path = nil, port = nil)
  get_response(uri_or_host, path, port).body
end
            
get_print(uri_or_host, path = nil, port = nil) click to toggle source

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'
 
               # File net/http.rb, line 349
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
            
get_response(uri_or_host, path = nil, port = nil, &block) click to toggle source

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
 
               # File net/http.rb, line 384
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
            
http_default_port() click to toggle source

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

 
               # File net/http.rb, line 432
def HTTP.http_default_port
  80
end
            
https_default_port() click to toggle source

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

 
               # File net/http.rb, line 437
def HTTP.https_default_port
  443
end
            
is_version_1_1?() click to toggle source
Alias for: version_1_1?
is_version_1_2?() click to toggle source
Alias for: version_1_2?
new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil) click to toggle source

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.

 
               # File net/http.rb, line 463
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
            
Also aliased as: newobj
new(address, port = nil) click to toggle source

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

 
               # File net/http.rb, line 473
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
            
newobj(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil) click to toggle source
Alias for: new
post_form(url, params) click to toggle source

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" }
 
               # File net/http.rb, line 413
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
            
proxy_class?() click to toggle source

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

 
               # File net/http.rb, line 684
def proxy_class?
  @is_proxy_class
end
            
start(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil) click to toggle source

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.

 
               # File net/http.rb, line 452
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
            
version_1_1() click to toggle source

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

 
               # File net/http.rb, line 313
def HTTP.version_1_1
  @newimpl = false
end
            
version_1_1?() click to toggle source

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

 
               # File net/http.rb, line 325
def HTTP.version_1_1?
  not @newimpl
end
            
Also aliased as: is_version_1_1?
version_1_2() click to toggle source

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
 
               # File net/http.rb, line 307
def HTTP.version_1_2
  @newimpl = true
end
            
version_1_2?() click to toggle source

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

 
               # File net/http.rb, line 319
def HTTP.version_1_2?
  @newimpl
end
            
Also aliased as: is_version_1_2?

Public Instance Methods

active?() click to toggle source
Alias for: started?
copy(path, initheader = nil) click to toggle source

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

 
               # File net/http.rb, line 965
def copy(path, initheader = nil)
  request(Copy.new(path, initheader))
end
            
delete(path, initheader = {'Depth' => 'Infinity'}) click to toggle source

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

 
               # File net/http.rb, line 953
def delete(path, initheader = {'Depth' => 'Infinity'})
  request(Delete.new(path, initheader))
end
            
finish() click to toggle source

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

 
               # File net/http.rb, line 626
def finish
  raise IOError, 'HTTP session not yet started' unless started?
  do_finish
end
            
get(path, initheader = {}, dest = nil) click to toggle source

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
}
 
               # File net/http.rb, line 806
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["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
            
get2(path, initheader = nil) click to toggle source
Alias for: request_get
head(path, initheader = nil) click to toggle source

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']
 
               # File net/http.rb, line 859
def head(path, initheader = nil) 
  res = request(Head.new(path, initheader))
  res.value unless @newimpl
  res
end
            
head2(path, initheader = nil, &block) click to toggle source
Alias for: request_head
inspect() click to toggle source
 
               # File net/http.rb, line 496
def inspect
  "#<#{self.class} #{@address}:#{@port} open=#{started?}>"
end
            
lock(path, body, initheader = nil) click to toggle source

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

 
               # File net/http.rb, line 929
def lock(path, body, initheader = nil)
  request(Lock.new(path, initheader), body)
end
            
mkcol(path, body = nil, initheader = nil) click to toggle source

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

 
               # File net/http.rb, line 971
def mkcol(path, body = nil, initheader = nil)
  request(Mkcol.new(path, initheader), body)
end
            
move(path, initheader = nil) click to toggle source

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

 
               # File net/http.rb, line 959
def move(path, initheader = nil)
  request(Move.new(path, initheader))
end
            
options(path, initheader = nil) click to toggle source

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

 
               # File net/http.rb, line 941
def options(path, initheader = nil)
  request(Options.new(path, initheader))
end
            
post(path, data, initheader = nil, dest = nil) click to toggle source

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.

 
               # File net/http.rb, line 902
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
            
post2(path, data, initheader = nil) click to toggle source
Alias for: request_post
propfind(path, body = nil, initheader = {'Depth' => '0'}) click to toggle source

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

 
               # File net/http.rb, line 947
def propfind(path, body = nil, initheader = {'Depth' => '0'})
  request(Propfind.new(path, initheader), body)
end
            
proppatch(path, body, initheader = nil) click to toggle source

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

 
               # File net/http.rb, line 923
def proppatch(path, body, initheader = nil)
  request(Proppatch.new(path, initheader), body)
end
            
proxy?() click to toggle source

True if self is a HTTP proxy class.

 
               # File net/http.rb, line 695
def proxy?
  self.class.proxy_class?
end
            
proxy_address() click to toggle source

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

 
               # File net/http.rb, line 700
def proxy_address
  self.class.proxy_address
end
            
Also aliased as: proxyaddr
proxy_pass() click to toggle source

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

 
               # File net/http.rb, line 715
def proxy_pass
  self.class.proxy_pass
end
            
proxy_port() click to toggle source

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

 
               # File net/http.rb, line 705
def proxy_port
  self.class.proxy_port
end
            
Also aliased as: proxyport
proxy_user() click to toggle source

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

 
               # File net/http.rb, line 710
def proxy_user
  self.class.proxy_user
end
            
proxyaddr() click to toggle source
Alias for: proxy_address
proxyport() click to toggle source
Alias for: proxy_port
read_timeout=(sec) click to toggle source

Setter for the #read_timeout attribute.

 
               # File net/http.rb, line 531
def read_timeout=(sec)
  @socket.read_timeout = sec if @socket
  @read_timeout = sec
end
            
request(req, body = nil) click to toggle source

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 Net::HTTPResponse#read_body, if desired.

Returns a HTTPResponse object.

This method never raises Net::* exceptions.

 
               # File net/http.rb, line 1092
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
            
request_get(path, initheader = nil) click to toggle source

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 Net::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
}
 
               # File net/http.rb, line 1006
def request_get(path, initheader = nil, &block) # :yield: +response+
  request(Get.new(path, initheader), &block)
end
            
Also aliased as: get2
request_head(path, initheader = nil, &block) click to toggle source

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']
 
               # File net/http.rb, line 1020
def request_head(path, initheader = nil, &block)
  request(Head.new(path, initheader), &block)
end
            
Also aliased as: head2
request_post(path, data, initheader = nil) click to toggle source

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 Net::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
}
 
               # File net/http.rb, line 1050
def request_post(path, data, initheader = nil, &block) # :yield: +response+
  request Post.new(path, initheader), data, &block
end
            
Also aliased as: post2
send_request(name, path, data = nil, header = nil) click to toggle source

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
 
               # File net/http.rb, line 1074
def send_request(name, path, data = nil, header = nil)
  r = HTTPGenericRequest.new(name,(data ? true : false),true,path,header)
  request r, data
end
            
set_debug_output(output) click to toggle source

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 { .... }
 
               # File net/http.rb, line 509
def set_debug_output(output)
  warn 'Net::HTTP#set_debug_output called after HTTP started' if started?
  @debug_output = output
end
            
start() click to toggle source

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.

 
               # File net/http.rb, line 559
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
            
started?() click to toggle source

returns true if the HTTP session is started.

 
               # File net/http.rb, line 537
def started?
  @started
end
            
Also aliased as: active?
trace(path, initheader = nil) click to toggle source

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

 
               # File net/http.rb, line 977
def trace(path, initheader = nil)
  request(Trace.new(path, initheader))
end
            
unlock(path, body, initheader = nil) click to toggle source

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

 
               # File net/http.rb, line 935
def unlock(path, body, initheader = nil)
  request(Unlock.new(path, initheader), body)
end
            
use_ssl?() click to toggle source

returns true if use SSL/TLS with HTTP.

 
               # File net/http.rb, line 546
def use_ssl?
  false   # redefined in net/https
end